Skip to content

Commit 9d1221a

Browse files
authored
Apply remaining changes
1 parent 0f09596 commit 9d1221a

5 files changed

Lines changed: 36 additions & 24 deletions

File tree

src/cryptoECC.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,19 @@ function MsrcryptoECC() {
20022002

20032003
var curvesInternal = {};
20042004

2005+
var curveElementLengths = {
2006+
"P-256": 32,
2007+
"P-384": 48,
2008+
"P-521": 66,
2009+
"BN-254": 32,
2010+
"NUMSP256D1": 32,
2011+
"NUMSP256T1": 32,
2012+
"NUMSP384D1": 48,
2013+
"NUMSP384T1": 48,
2014+
"NUMSP512D1": 64,
2015+
"NUMSP512T1": 64
2016+
};
2017+
20052018
var createCurve = function(curveName) {
20062019

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

2044+
var curveElementLength = function(curveName) {
2045+
if (!curveName) {
2046+
return undefined;
2047+
}
2048+
2049+
return curveElementLengths[curveName.toUpperCase()];
2050+
};
2051+
20312052
return {
20322053
createCurve: createCurve,
2054+
curveElementLength: curveElementLength,
20332055
curves: curvesInternal,
20342056
sec1EncodingFp: sec1EncodingFp,
20352057
validatePoint: validateEccPoint,

src/ecdh.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,7 @@ if ( typeof operations !== "undefined" ) {
156156

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

251246
// Accept keys with or without trimmed leading zeros and pad each
252247
// value to the curve's fixed element length (Chrome/Chromium behavior).
253-
var partLen = {
254-
"P-256": 32, "P-384": 48, "P-521": 66,
255-
"NUMSP256D1": 32, "NUMSP256T1": 32,
256-
"NUMSP384D1": 48, "NUMSP384T1": 48,
257-
"NUMSP512D1": 64, "NUMSP512T1": 64
258-
}[p.algorithm.namedCurve];
248+
var partLen = cryptoECC.curveElementLength(p.algorithm.namedCurve);
259249
if ( keyObject.x ) { keyObject.x = msrcryptoUtilities.padFront(keyObject.x, 0, partLen); }
260250
if ( keyObject.y ) { keyObject.y = msrcryptoUtilities.padFront(keyObject.y, 0, partLen); }
261251
if ( keyObject.d ) { keyObject.d = msrcryptoUtilities.padFront(keyObject.d, 0, partLen); }

src/ecdsa.js

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,7 @@ if (typeof operations !== "undefined") {
207207

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

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

308303
// Accept keys with or without trimmed leading zeros and pad each
309304
// value to the curve's fixed element length (Chrome/Chromium behavior).
310-
var partLen = {
311-
"P-256": 32, "P-384": 48, "P-521": 66,
312-
"NUMSP256D1": 32, "NUMSP256T1": 32,
313-
"NUMSP384D1": 48, "NUMSP384T1": 48,
314-
"NUMSP512D1": 64, "NUMSP512T1": 64
315-
}[p.algorithm.namedCurve];
305+
var partLen = cryptoECC.curveElementLength(p.algorithm.namedCurve);
316306
if ( keyObject.x ) { keyObject.x = msrcryptoUtilities.padFront(keyObject.x, 0, partLen); }
317307
if ( keyObject.y ) { keyObject.y = msrcryptoUtilities.padFront(keyObject.y, 0, partLen); }
318308
if ( keyObject.d ) { keyObject.d = msrcryptoUtilities.padFront(keyObject.d, 0, partLen); }

test/Test.Ecdh.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ function ecdhTests() {
125125
ts.keyGeneratePairTest(ecdhKeyAlg("P-521"), ["deriveKey", "deriveBits"], inspectEcdhKey, context(iterations, assert));
126126
});
127127

128+
QUnit.test(label + " generateKeyTest BN-254", function(assert) {
129+
ts.keyGeneratePairTest(ecdhKeyAlg("BN-254"), ["deriveKey", "deriveBits"], inspectEcdhKey, context(iterations, assert));
130+
});
131+
128132
QUnit.test(label + " ts.deriveKeyTest P-256 --> Aes-Cbc-256 ", function(assert) {
129133
ts.deriveKeyTest(ecdh.p256.DeriveKey, undefined, context(iterations, assert));
130134
});
@@ -226,6 +230,7 @@ var ecdhKeyLengths = {
226230
"P-256": 32,
227231
"P-384": 48,
228232
"P-521": 66,
233+
"BN-254": 32,
229234
"NUMSP256D1": 32,
230235
"NUMSP256T1": 32,
231236
"NUMSP384D1": 48,

test/Test.Ecdsa.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ function ecdsaTests() {
248248
ts.keyGeneratePairTest( ecdsaKeyAlg( "P-521" ), [VERIFY, SIGN], inspectEcdsaKey, context( iterations, assert ) );
249249
} );
250250

251+
QUnit.test(label + " generateKeyTest BN-254", function(assert) {
252+
ts.keyGeneratePairTest(ecdsaKeyAlg("BN-254"), [VERIFY, SIGN], inspectEcdsaKey, context(iterations, assert));
253+
});
254+
251255
QUnit.test(label + " generateKeyTest NUMSP256D1", function(assert) {
252256
ts.keyGeneratePairTest(ecdsaKeyAlg("NUMSP256D1"), [VERIFY, SIGN], inspectEcdsaKey, context(iterations, assert));
253257
});
@@ -423,6 +427,7 @@ var ecdsaKeyLengths = {
423427
"P-256": 32,
424428
"P-384": 48,
425429
"P-521": 66,
430+
"BN-254": 32,
426431
"NUMSP256D1": 32,
427432
"NUMSP256T1": 32,
428433
"NUMSP384D1": 48,

0 commit comments

Comments
 (0)