Skip to content

Commit 1b0cca2

Browse files
authored
Merge pull request #26 from microsoft/copilot/fix-bn-254-padding-issue
Refactor EC key padding length lookup into shared cryptoECC helper; add BN-254 support
2 parents 43147a1 + 36444a3 commit 1b0cca2

5 files changed

Lines changed: 44 additions & 50 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: 9 additions & 13 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
});
@@ -222,17 +226,9 @@ function ecdhTests() {
222226
});
223227
}
224228

225-
var ecdhKeyLengths = {
226-
"P-256": 32,
227-
"P-384": 48,
228-
"P-521": 66,
229-
"NUMSP256D1": 32,
230-
"NUMSP256T1": 32,
231-
"NUMSP384D1": 48,
232-
"NUMSP384T1": 48,
233-
"NUMSP512D1": 64,
234-
"NUMSP512T1": 64
235-
};
229+
function ecdhCurveElementLength(namedCurve) {
230+
return cryptoECC.curveElementLength(namedCurve);
231+
}
236232

237233
function ecdhKeyAlg(namedCurve) {
238234
return {
@@ -245,7 +241,7 @@ var inspectEcdhKey = {
245241
public: function(keyObj, algorithm, usages, reason) {
246242

247243
var fail = [];
248-
var expLenMax = ecdhKeyLengths[algorithm.namedCurve];
244+
var expLenMax = ecdhCurveElementLength(algorithm.namedCurve);
249245
var expLenMin = expLenMax;
250246

251247
// has crv property equal to "P-521"
@@ -304,7 +300,7 @@ var inspectEcdhKey = {
304300
// }
305301
// }
306302

307-
var expLenMax = ecdhKeyLengths[algorithm.namedCurve];
303+
var expLenMax = ecdhCurveElementLength(algorithm.namedCurve);
308304
var expLenMin = expLenMax;
309305

310306
this.public(keyObj, algorithm, usages, reason);

test/Test.Ecdsa.js

Lines changed: 9 additions & 13 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
});
@@ -419,17 +423,9 @@ function ecdsaTests() {
419423
});
420424
}
421425

422-
var ecdsaKeyLengths = {
423-
"P-256": 32,
424-
"P-384": 48,
425-
"P-521": 66,
426-
"NUMSP256D1": 32,
427-
"NUMSP256T1": 32,
428-
"NUMSP384D1": 48,
429-
"NUMSP384T1": 48,
430-
"NUMSP512D1": 64,
431-
"NUMSP512T1": 64
432-
};
426+
function ecdsaCurveElementLength(namedCurve) {
427+
return cryptoECC.curveElementLength(namedCurve);
428+
}
433429

434430
function ecdsaKeyAlg(curve) {
435431
return {
@@ -450,7 +446,7 @@ var inspectEcdsaKey = {
450446

451447
var fail = [];
452448

453-
var expLenMax = ecdsaKeyLengths[algorithm.namedCurve];
449+
var expLenMax = ecdsaCurveElementLength(algorithm.namedCurve);
454450
var expLenMin = expLenMax;
455451

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

496-
var expLenMax = ecdsaKeyLengths[algorithm.namedCurve];
492+
var expLenMax = ecdsaCurveElementLength(algorithm.namedCurve);
497493
var expLenMin = expLenMax;
498494

499495
this.public(keyObj, algorithm, usages, reason);

0 commit comments

Comments
 (0)