Create key from provided material

This commit is contained in:
Ismael Bejarano 2016-08-12 00:25:57 -03:00 committed by Sanjana Rajan
parent a5d9e6d09e
commit f87e759cb0
5 changed files with 27 additions and 11 deletions

View File

@ -228,7 +228,7 @@ export default {
} }
}, },
generateMpi: function(algo, bits, curve) { generateMpi: function(algo, bits, curve, material) {
switch (algo) { switch (algo) {
case 'rsa_encrypt': case 'rsa_encrypt':
case 'rsa_encrypt_sign': case 'rsa_encrypt_sign':
@ -247,7 +247,7 @@ export default {
}); });
case 'ecdsa': case 'ecdsa':
return publicKey.elliptic.generate(curve).then(function (key) { return publicKey.elliptic.generate(curve, material).then(function (key) {
return [ return [
new type_oid(key.oid), new type_oid(key.oid),
BigInteger2mpi(key.R), BigInteger2mpi(key.R),
@ -256,7 +256,7 @@ export default {
}); });
case 'ecdh': case 'ecdh':
return publicKey.elliptic.generate(curve).then(function (key) { return publicKey.elliptic.generate(curve, material).then(function (key) {
return [ return [
new type_oid(key.oid), new type_oid(key.oid),
BigInteger2mpi(key.R), BigInteger2mpi(key.R),

View File

@ -97,10 +97,15 @@ function get(oid_or_name) {
throw new Error('Not valid curve'); throw new Error('Not valid curve');
} }
function generate(curve) { function generate(curve, material) {
return new Promise(function (resolve) { return new Promise(function (resolve) {
curve = get(curve); curve = get(curve);
var keyPair = curve.genKeyPair(); var keyPair;
if (typeof(material) !== "undefined") {
keyPair = curve.keyFromPrivate(material);
} else {
keyPair = curve.genKeyPair();
}
resolve({ resolve({
oid: curve.oid, oid: curve.oid,
R: new BigInteger(keyPair.getPublic()), R: new BigInteger(keyPair.getPublic()),

View File

@ -1139,7 +1139,11 @@ export function generate(options) {
function generateSecretKey() { function generateSecretKey() {
secretKeyPacket = new packet.SecretKey(); secretKeyPacket = new packet.SecretKey();
secretKeyPacket.algorithm = enums.read(enums.publicKey, options.keyType); 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() { function generateSecretSubkey() {
@ -1148,8 +1152,12 @@ export function generate(options) {
if (subkeyType === enums.publicKey.ecdsa) { if (subkeyType === enums.publicKey.ecdsa) {
subkeyType = enums.publicKey.ecdh; subkeyType = enums.publicKey.ecdh;
} }
var material;
if (typeof(options.material) !== "undefined") {
material = options.material.subkey;
}
secretSubkeyPacket.algorithm = enums.read(enums.publicKey, subkeyType); secretSubkeyPacket.algorithm = enums.read(enums.publicKey, subkeyType);
return secretSubkeyPacket.generate(options.numBits, options.curve); return secretSubkeyPacket.generate(options.numBits, options.curve, material);
} }
} }

View File

@ -98,8 +98,8 @@ export function destroyWorker() {
* @static * @static
*/ */
export function generateKey({ userIds=[], passphrase, numBits=2048, unlocked=false, curve=null } = {}) { export function generateKey({ userIds=[], passphrase, numBits=2048, unlocked=false, curve=null, material=null } = {}) {
const options = formatUserIds({ userIds, passphrase, numBits, unlocked, curve }); const options = formatUserIds({ userIds, passphrase, numBits, unlocked, curve, material });
if (!util.getWebCryptoAll() && asyncProxy) { // use web worker if web crypto apis are not supported if (!util.getWebCryptoAll() && asyncProxy) { // use web worker if web crypto apis are not supported
return asyncProxy.delegate('generateKey', options); return asyncProxy.delegate('generateKey', options);
@ -459,6 +459,9 @@ function formatUserIds(options) {
if (!options.curve) { if (!options.curve) {
delete options.curve; delete options.curve;
} }
if (!options.material) {
delete options.material;
}
if (!options.userIds) { if (!options.userIds) {
return options; return options;
} }

View File

@ -273,10 +273,10 @@ SecretKey.prototype.decrypt = function (passphrase) {
return true; return true;
}; };
SecretKey.prototype.generate = function (bits, curve) { SecretKey.prototype.generate = function (bits, curve, material) {
var self = this; 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.mpi = mpi;
self.isDecrypted = true; self.isDecrypted = true;
}); });