Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/cryptoECC.js
Original file line number Diff line number Diff line change
Expand Up @@ -2002,6 +2002,19 @@ function MsrcryptoECC() {

var curvesInternal = {};

var curveElementLengths = {
"P-256": 32,
"P-384": 48,
"P-521": 66,
"BN-254": 32,
"NUMSP256D1": 32,
"NUMSP256T1": 32,
"NUMSP384D1": 48,
"NUMSP384T1": 48,
"NUMSP512D1": 64,
"NUMSP512T1": 64
};

var createCurve = function(curveName) {

var curveData = curvesInternal[curveName.toUpperCase()];
Expand All @@ -2028,8 +2041,17 @@ function MsrcryptoECC() {
return opp.validatePoint(point);
};

var curveElementLength = function(curveName) {
if (!curveName) {
return undefined;
}

return curveElementLengths[curveName.toUpperCase()];
};

return {
createCurve: createCurve,
curveElementLength: curveElementLength,
curves: curvesInternal,
sec1EncodingFp: sec1EncodingFp,
validatePoint: validateEccPoint,
Expand Down
14 changes: 2 additions & 12 deletions src/ecdh.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,7 @@ if ( typeof operations !== "undefined" ) {

// Pad each value to the curve's fixed element length so leading zeros
// are preserved (matches Chrome / Chromium-based Edge behavior).
var partLen = {
"P-256": 32, "P-384": 48, "P-521": 66,
"NUMSP256D1": 32, "NUMSP256T1": 32,
"NUMSP384D1": 48, "NUMSP384T1": 48,
"NUMSP512D1": 64, "NUMSP512T1": 64
}[p.algorithm.namedCurve];
var partLen = cryptoECC.curveElementLength(p.algorithm.namedCurve);
var pad = msrcryptoUtilities.padFront;
keyPairData.publicKey.x = pad(keyPairData.publicKey.x, 0, partLen);
keyPairData.publicKey.y = pad(keyPairData.publicKey.y, 0, partLen);
Expand Down Expand Up @@ -250,12 +245,7 @@ if ( typeof operations !== "undefined" ) {

// Accept keys with or without trimmed leading zeros and pad each
// value to the curve's fixed element length (Chrome/Chromium behavior).
var partLen = {
"P-256": 32, "P-384": 48, "P-521": 66,
"NUMSP256D1": 32, "NUMSP256T1": 32,
"NUMSP384D1": 48, "NUMSP384T1": 48,
"NUMSP512D1": 64, "NUMSP512T1": 64
}[p.algorithm.namedCurve];
var partLen = cryptoECC.curveElementLength(p.algorithm.namedCurve);
if ( keyObject.x ) { keyObject.x = msrcryptoUtilities.padFront(keyObject.x, 0, partLen); }
if ( keyObject.y ) { keyObject.y = msrcryptoUtilities.padFront(keyObject.y, 0, partLen); }
if ( keyObject.d ) { keyObject.d = msrcryptoUtilities.padFront(keyObject.d, 0, partLen); }
Expand Down
14 changes: 2 additions & 12 deletions src/ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,7 @@ if (typeof operations !== "undefined") {

// Pad each value to the curve's fixed element length so leading zeros
// are preserved (matches Chrome / Chromium-based Edge behavior).
var partLen = {
"P-256": 32, "P-384": 48, "P-521": 66,
"NUMSP256D1": 32, "NUMSP256T1": 32,
"NUMSP384D1": 48, "NUMSP384T1": 48,
"NUMSP512D1": 64, "NUMSP512T1": 64
}[p.algorithm.namedCurve];
var partLen = cryptoECC.curveElementLength(p.algorithm.namedCurve);

function padToCurveLength( array ) {
return msrcryptoUtilities.padFront(array, 0, partLen);
Expand Down Expand Up @@ -307,12 +302,7 @@ if (typeof operations !== "undefined") {

// Accept keys with or without trimmed leading zeros and pad each
// value to the curve's fixed element length (Chrome/Chromium behavior).
var partLen = {
"P-256": 32, "P-384": 48, "P-521": 66,
"NUMSP256D1": 32, "NUMSP256T1": 32,
"NUMSP384D1": 48, "NUMSP384T1": 48,
"NUMSP512D1": 64, "NUMSP512T1": 64
}[p.algorithm.namedCurve];
var partLen = cryptoECC.curveElementLength(p.algorithm.namedCurve);
if ( keyObject.x ) { keyObject.x = msrcryptoUtilities.padFront(keyObject.x, 0, partLen); }
if ( keyObject.y ) { keyObject.y = msrcryptoUtilities.padFront(keyObject.y, 0, partLen); }
if ( keyObject.d ) { keyObject.d = msrcryptoUtilities.padFront(keyObject.d, 0, partLen); }
Expand Down
22 changes: 9 additions & 13 deletions test/Test.Ecdh.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ function ecdhTests() {
ts.keyGeneratePairTest(ecdhKeyAlg("P-521"), ["deriveKey", "deriveBits"], inspectEcdhKey, context(iterations, assert));
});

QUnit.test(label + " generateKeyTest BN-254", function(assert) {
ts.keyGeneratePairTest(ecdhKeyAlg("BN-254"), ["deriveKey", "deriveBits"], inspectEcdhKey, context(iterations, assert));
});

QUnit.test(label + " ts.deriveKeyTest P-256 --> Aes-Cbc-256 ", function(assert) {
ts.deriveKeyTest(ecdh.p256.DeriveKey, undefined, context(iterations, assert));
});
Expand Down Expand Up @@ -222,17 +226,9 @@ function ecdhTests() {
});
}

var ecdhKeyLengths = {
"P-256": 32,
"P-384": 48,
"P-521": 66,
"NUMSP256D1": 32,
"NUMSP256T1": 32,
"NUMSP384D1": 48,
"NUMSP384T1": 48,
"NUMSP512D1": 64,
"NUMSP512T1": 64
};
function ecdhCurveElementLength(namedCurve) {
return cryptoECC.curveElementLength(namedCurve);
}

function ecdhKeyAlg(namedCurve) {
return {
Expand All @@ -245,7 +241,7 @@ var inspectEcdhKey = {
public: function(keyObj, algorithm, usages, reason) {

var fail = [];
var expLenMax = ecdhKeyLengths[algorithm.namedCurve];
var expLenMax = ecdhCurveElementLength(algorithm.namedCurve);
var expLenMin = expLenMax;

// has crv property equal to "P-521"
Expand Down Expand Up @@ -304,7 +300,7 @@ var inspectEcdhKey = {
// }
// }

var expLenMax = ecdhKeyLengths[algorithm.namedCurve];
var expLenMax = ecdhCurveElementLength(algorithm.namedCurve);
var expLenMin = expLenMax;

this.public(keyObj, algorithm, usages, reason);
Expand Down
22 changes: 9 additions & 13 deletions test/Test.Ecdsa.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ function ecdsaTests() {
ts.keyGeneratePairTest( ecdsaKeyAlg( "P-521" ), [VERIFY, SIGN], inspectEcdsaKey, context( iterations, assert ) );
} );

QUnit.test(label + " generateKeyTest BN-254", function(assert) {
ts.keyGeneratePairTest(ecdsaKeyAlg("BN-254"), [VERIFY, SIGN], inspectEcdsaKey, context(iterations, assert));
});

QUnit.test(label + " generateKeyTest NUMSP256D1", function(assert) {
ts.keyGeneratePairTest(ecdsaKeyAlg("NUMSP256D1"), [VERIFY, SIGN], inspectEcdsaKey, context(iterations, assert));
});
Expand Down Expand Up @@ -419,17 +423,9 @@ function ecdsaTests() {
});
}

var ecdsaKeyLengths = {
"P-256": 32,
"P-384": 48,
"P-521": 66,
"NUMSP256D1": 32,
"NUMSP256T1": 32,
"NUMSP384D1": 48,
"NUMSP384T1": 48,
"NUMSP512D1": 64,
"NUMSP512T1": 64
};
function ecdsaCurveElementLength(namedCurve) {
return cryptoECC.curveElementLength(namedCurve);
}

function ecdsaKeyAlg(curve) {
return {
Expand All @@ -450,7 +446,7 @@ var inspectEcdsaKey = {

var fail = [];

var expLenMax = ecdsaKeyLengths[algorithm.namedCurve];
var expLenMax = ecdsaCurveElementLength(algorithm.namedCurve);
var expLenMin = expLenMax;

// has crv property equal to "P-521"
Expand Down Expand Up @@ -493,7 +489,7 @@ var inspectEcdsaKey = {
},
private: function(keyObj, algorithm, usages, reason) {

var expLenMax = ecdsaKeyLengths[algorithm.namedCurve];
var expLenMax = ecdsaCurveElementLength(algorithm.namedCurve);
var expLenMin = expLenMax;

this.public(keyObj, algorithm, usages, reason);
Expand Down