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
50 changes: 26 additions & 24 deletions dist/msrcrypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -3978,6 +3978,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 @@ -4004,8 +4017,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 Expand Up @@ -8089,12 +8111,7 @@ if ( typeof operations !== "undefined" ) {

var keyPairData = ecdhInstance.generateKey();

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 @@ -8177,12 +8194,7 @@ if ( typeof operations !== "undefined" ) {
keyObject.y = publicKey.y;
}

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 Expand Up @@ -8583,12 +8595,7 @@ if (typeof operations !== "undefined") {

var dtb = cryptoMath.digitsToBytes;

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 @@ -8677,12 +8684,7 @@ if (typeof operations !== "undefined") {
keyObject.y = publicKey.y;
}

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
2 changes: 1 addition & 1 deletion dist/msrcrypto.min.js

Large diffs are not rendered by default.

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
7 changes: 6 additions & 1 deletion 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 @@ -226,6 +230,7 @@ var ecdhKeyLengths = {
"P-256": 32,
"P-384": 48,
"P-521": 66,
"BN-254": 32,
"NUMSP256D1": 32,
"NUMSP256T1": 32,
"NUMSP384D1": 48,
Expand All @@ -248,7 +253,7 @@ var inspectEcdhKey = {
var expLenMax = ecdhKeyLengths[algorithm.namedCurve];
var expLenMin = expLenMax;

// has crv property equal to "P-521"
// has crv property equal to the algorithm's namedCurve
if (!validation.prop.string(keyObj, "crv", algorithm.namedCurve)) {
fail.push("key.crv !== " + algorithm.namedCurve);
}
Expand Down
7 changes: 6 additions & 1 deletion 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 @@ -423,6 +427,7 @@ var ecdsaKeyLengths = {
"P-256": 32,
"P-384": 48,
"P-521": 66,
"BN-254": 32,
"NUMSP256D1": 32,
"NUMSP256T1": 32,
"NUMSP384D1": 48,
Expand Down Expand Up @@ -453,7 +458,7 @@ var inspectEcdsaKey = {
var expLenMax = ecdsaKeyLengths[algorithm.namedCurve];
var expLenMin = expLenMax;

// has crv property equal to "P-521"
// has crv property equal to the algorithm's namedCurve
if (!validation.prop.string(keyObj, "crv", algorithm.namedCurve)) {
fail.push("key.crv !== " + algorithm.namedCurve);
}
Expand Down