Added contingency for when browser does not support NIST curves
This commit is contained in:
parent
9e6005ee39
commit
9cbfbf453b
|
@ -150,17 +150,23 @@ Curve.prototype.keyFromPublic = function (pub) {
|
||||||
Curve.prototype.genKeyPair = async function () {
|
Curve.prototype.genKeyPair = async function () {
|
||||||
var keyPair;
|
var keyPair;
|
||||||
if (webCrypto && config.use_native && this.web) {
|
if (webCrypto && config.use_native && this.web) {
|
||||||
keyPair = await webGenKeyPair(this.name);
|
// If browser doesn't support a curve, we'll catch it
|
||||||
|
try {
|
||||||
|
keyPair = await webGenKeyPair(this.name);
|
||||||
|
return new KeyPair(this.curve, keyPair);
|
||||||
|
} catch (err) {
|
||||||
|
util.print_debug("Browser did not support signing: " + err.message);
|
||||||
|
}
|
||||||
} else if (nodeCrypto && config.use_native && this.node) {
|
} else if (nodeCrypto && config.use_native && this.node) {
|
||||||
keyPair = await nodeGenKeyPair(this.name);
|
keyPair = await nodeGenKeyPair(this.name);
|
||||||
|
return new KeyPair(this.curve, keyPair);
|
||||||
|
}
|
||||||
|
var compact = this.curve.curve.type === 'edwards' || this.curve.curve.type === 'mont';
|
||||||
|
var r = await this.curve.genKeyPair();
|
||||||
|
if (this.keyType === enums.publicKey.eddsa) {
|
||||||
|
keyPair = { secret: r.getSecret() };
|
||||||
} else {
|
} else {
|
||||||
var compact = this.curve.curve.type === 'edwards' || this.curve.curve.type === 'mont';
|
keyPair = { pub: r.getPublic('array', compact), priv: r.getPrivate().toArray() };
|
||||||
var r = await this.curve.genKeyPair();
|
|
||||||
if (this.keyType === enums.publicKey.eddsa) {
|
|
||||||
keyPair = { secret: r.getSecret() };
|
|
||||||
} else {
|
|
||||||
keyPair = { pub: r.getPublic('array', compact), priv: r.getPrivate().toArray() };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return new KeyPair(this.curve, keyPair);
|
return new KeyPair(this.curve, keyPair);
|
||||||
};
|
};
|
||||||
|
|
|
@ -65,13 +65,17 @@ KeyPair.prototype.sign = async function (message, hash_algo) {
|
||||||
message = util.str2Uint8Array(message);
|
message = util.str2Uint8Array(message);
|
||||||
}
|
}
|
||||||
if (webCrypto && config.use_native && this.curve.web) {
|
if (webCrypto && config.use_native && this.curve.web) {
|
||||||
return webSign(this.curve, hash_algo, message, this.keyPair);
|
// If browser doesn't support a curve, we'll catch it
|
||||||
|
try {
|
||||||
|
return webSign(this.curve, hash_algo, message, this.keyPair);
|
||||||
|
} catch (err) {
|
||||||
|
util.print_debug("Browser did not support signing: " + err.message);
|
||||||
|
}
|
||||||
} else if (nodeCrypto && config.use_native && this.curve.node) {
|
} else if (nodeCrypto && config.use_native && this.curve.node) {
|
||||||
return nodeSign(this.curve, hash_algo, message, this.keyPair);
|
return nodeSign(this.curve, hash_algo, message, this.keyPair);
|
||||||
} else {
|
|
||||||
const digest = (typeof hash_algo === 'undefined') ? message : hash.digest(hash_algo, message);
|
|
||||||
return this.keyPair.sign(digest);
|
|
||||||
}
|
}
|
||||||
|
const digest = (typeof hash_algo === 'undefined') ? message : hash.digest(hash_algo, message);
|
||||||
|
return this.keyPair.sign(digest);
|
||||||
};
|
};
|
||||||
|
|
||||||
KeyPair.prototype.verify = async function (message, signature, hash_algo) {
|
KeyPair.prototype.verify = async function (message, signature, hash_algo) {
|
||||||
|
@ -79,13 +83,17 @@ KeyPair.prototype.verify = async function (message, signature, hash_algo) {
|
||||||
message = util.str2Uint8Array(message);
|
message = util.str2Uint8Array(message);
|
||||||
}
|
}
|
||||||
if (webCrypto && config.use_native && this.curve.web) {
|
if (webCrypto && config.use_native && this.curve.web) {
|
||||||
return webVerify(this.curve, hash_algo, signature, message, this.keyPair.getPublic());
|
// If browser doesn't support a curve, we'll catch it
|
||||||
|
try {
|
||||||
|
return webVerify(this.curve, hash_algo, signature, message, this.keyPair.getPublic());
|
||||||
|
} catch (err) {
|
||||||
|
util.print_debug("Browser did not support signing: " + err.message);
|
||||||
|
}
|
||||||
} else if (nodeCrypto && config.use_native && this.curve.node) {
|
} else if (nodeCrypto && config.use_native && this.curve.node) {
|
||||||
return nodeVerify(this.curve, hash_algo, signature, message, this.keyPair.getPublic());
|
return nodeVerify(this.curve, hash_algo, signature, message, this.keyPair.getPublic());
|
||||||
} else {
|
|
||||||
const digest = (typeof hash_algo === 'undefined') ? message : hash.digest(hash_algo, message);
|
|
||||||
return this.keyPair.verify(digest, signature);
|
|
||||||
}
|
}
|
||||||
|
const digest = (typeof hash_algo === 'undefined') ? message : hash.digest(hash_algo, message);
|
||||||
|
return this.keyPair.verify(digest, signature);
|
||||||
};
|
};
|
||||||
|
|
||||||
KeyPair.prototype.derive = function (pub) {
|
KeyPair.prototype.derive = function (pub) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user