Create key from provided material
This commit is contained in:
parent
a5d9e6d09e
commit
f87e759cb0
|
@ -228,7 +228,7 @@ export default {
|
|||
}
|
||||
},
|
||||
|
||||
generateMpi: function(algo, bits, curve) {
|
||||
generateMpi: function(algo, bits, curve, material) {
|
||||
switch (algo) {
|
||||
case 'rsa_encrypt':
|
||||
case 'rsa_encrypt_sign':
|
||||
|
@ -247,7 +247,7 @@ export default {
|
|||
});
|
||||
|
||||
case 'ecdsa':
|
||||
return publicKey.elliptic.generate(curve).then(function (key) {
|
||||
return publicKey.elliptic.generate(curve, material).then(function (key) {
|
||||
return [
|
||||
new type_oid(key.oid),
|
||||
BigInteger2mpi(key.R),
|
||||
|
@ -256,7 +256,7 @@ export default {
|
|||
});
|
||||
|
||||
case 'ecdh':
|
||||
return publicKey.elliptic.generate(curve).then(function (key) {
|
||||
return publicKey.elliptic.generate(curve, material).then(function (key) {
|
||||
return [
|
||||
new type_oid(key.oid),
|
||||
BigInteger2mpi(key.R),
|
||||
|
|
|
@ -97,10 +97,15 @@ function get(oid_or_name) {
|
|||
throw new Error('Not valid curve');
|
||||
}
|
||||
|
||||
function generate(curve) {
|
||||
function generate(curve, material) {
|
||||
return new Promise(function (resolve) {
|
||||
curve = get(curve);
|
||||
var keyPair = curve.genKeyPair();
|
||||
var keyPair;
|
||||
if (typeof(material) !== "undefined") {
|
||||
keyPair = curve.keyFromPrivate(material);
|
||||
} else {
|
||||
keyPair = curve.genKeyPair();
|
||||
}
|
||||
resolve({
|
||||
oid: curve.oid,
|
||||
R: new BigInteger(keyPair.getPublic()),
|
||||
|
|
12
src/key.js
12
src/key.js
|
@ -1139,7 +1139,11 @@ export function generate(options) {
|
|||
function generateSecretKey() {
|
||||
secretKeyPacket = new packet.SecretKey();
|
||||
secretKeyPacket.algorithm = enums.read(enums.publicKey, options.keyType);
|
||||
return secretKeyPacket.generate(options.numBits, options.curve);
|
||||
var material;
|
||||
if (typeof(options.material) !== "undefined") {
|
||||
material = options.material.key;
|
||||
}
|
||||
return secretKeyPacket.generate(options.numBits, options.curve, material);
|
||||
}
|
||||
|
||||
function generateSecretSubkey() {
|
||||
|
@ -1148,8 +1152,12 @@ export function generate(options) {
|
|||
if (subkeyType === enums.publicKey.ecdsa) {
|
||||
subkeyType = enums.publicKey.ecdh;
|
||||
}
|
||||
var material;
|
||||
if (typeof(options.material) !== "undefined") {
|
||||
material = options.material.subkey;
|
||||
}
|
||||
secretSubkeyPacket.algorithm = enums.read(enums.publicKey, subkeyType);
|
||||
return secretSubkeyPacket.generate(options.numBits, options.curve);
|
||||
return secretSubkeyPacket.generate(options.numBits, options.curve, material);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -98,8 +98,8 @@ export function destroyWorker() {
|
|||
* @static
|
||||
*/
|
||||
|
||||
export function generateKey({ userIds=[], passphrase, numBits=2048, unlocked=false, curve=null } = {}) {
|
||||
const options = formatUserIds({ userIds, passphrase, numBits, unlocked, curve });
|
||||
export function generateKey({ userIds=[], passphrase, numBits=2048, unlocked=false, curve=null, material=null } = {}) {
|
||||
const options = formatUserIds({ userIds, passphrase, numBits, unlocked, curve, material });
|
||||
|
||||
if (!util.getWebCryptoAll() && asyncProxy) { // use web worker if web crypto apis are not supported
|
||||
return asyncProxy.delegate('generateKey', options);
|
||||
|
@ -459,6 +459,9 @@ function formatUserIds(options) {
|
|||
if (!options.curve) {
|
||||
delete options.curve;
|
||||
}
|
||||
if (!options.material) {
|
||||
delete options.material;
|
||||
}
|
||||
if (!options.userIds) {
|
||||
return options;
|
||||
}
|
||||
|
|
|
@ -273,10 +273,10 @@ SecretKey.prototype.decrypt = function (passphrase) {
|
|||
return true;
|
||||
};
|
||||
|
||||
SecretKey.prototype.generate = function (bits, curve) {
|
||||
SecretKey.prototype.generate = function (bits, curve, material) {
|
||||
var self = this;
|
||||
|
||||
return crypto.generateMpi(self.algorithm, bits, curve).then(function(mpi) {
|
||||
return crypto.generateMpi(self.algorithm, bits, curve, material).then(function(mpi) {
|
||||
self.mpi = mpi;
|
||||
self.isDecrypted = true;
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue
Block a user