Skip to content

Commit cde6fe4

Browse files
authored
Merge pull request #9 from microsoft/hkdf-crt_concat
Hkdf-crt; concat;
2 parents 57e04ff + 8de8f5a commit cde6fe4

File tree

15 files changed

+1810
-228
lines changed

15 files changed

+1810
-228
lines changed

gulpfile.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ const fullBuild = [
5252
"scripts/rsa-pkcs1.js",
5353
"scripts/rsa-pss.js",
5454
"scripts/rsa.js",
55-
"scripts/kdf.js",
55+
"scripts/concat.js",
5656
"scripts/pbkdf2.js",
5757
"scripts/hkdf.js",
58+
"scripts/hkdf-ctr.js",
5859
"scripts/ecdh.js",
5960
"scripts/ecdsa.js",
6061
"scripts/subtle.js",

lib/msrcrypto.js

Lines changed: 147 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
//*******************************************************************************
1818
"use strict";
1919

20-
var msrCryptoVersion = "1.6.3";
20+
var msrCryptoVersion = "1.6.4";
2121

2222
(function(root, factory) {
2323

@@ -7236,106 +7236,76 @@ var msrCryptoVersion = "1.6.3";
72367236
operations.register("generateKey", "RSA-PSS", msrcryptoRsa.generateKeyPair);
72377237
}
72387238

7239-
var msrcryptoKdf = function(hashFunction) {
7239+
var msrcryptoConcatKdf = (function() {
72407240

7241-
var utils = msrcryptoUtilities;
7241+
function deriveBits(p) {
7242+
7243+
var hashName = p.algorithm.hash.name,
7244+
hashFunction = msrcryptoHashFunctions[hashName.toUpperCase()](),
7245+
alg = p.algorithm;
7246+
7247+
var otherInfo =
7248+
utils.toArray(alg.algorithmId).concat(
7249+
utils.toArray(alg.partyUInfo),
7250+
utils.toArray(alg.partyVInfo),
7251+
utils.toArray(alg.publicInfo) || [],
7252+
utils.toArray(alg.privateInfo) || []);
72427253

7243-
function deriveKey(secretBytes, otherInfo, keyOutputLength) {
7244-
var reps = Math.ceil(keyOutputLength / (hashFunction.hashLen / 8)),
7254+
var reps = Math.ceil(p.length / hashFunction.hashLen),
72457255
counter = 1,
7246-
digest = secretBytes.concat(otherInfo),
7256+
digest = p.keyData.concat(otherInfo),
72477257
output = [];
72487258

72497259
for (var i = 0; i < reps; i++) {
7250-
72517260
var data = utils.int32ToBytes(counter++).concat(digest);
7252-
72537261
var h = hashFunction.computeHash(data);
7254-
72557262
output = output.concat(h);
72567263
}
72577264

7258-
return output.slice(0, keyOutputLength);
7265+
return output.slice(0, p.length / 8);
7266+
72597267
}
72607268

72617269
return {
7262-
7263-
deriveKey: deriveKey
7264-
7270+
deriveBits: deriveBits
72657271
};
72667272

7267-
};
7273+
}());
72687274

7269-
var msrcryptoKdfInstance = null;
7275+
var msrcryptoConcatKdfInstance = null;
72707276

72717277
if (typeof operations !== "undefined") {
72727278

7273-
msrcryptoKdf.deriveKey = function(p) {
7274-
7275-
var utils = msrcryptoUtilities;
7276-
7277-
var hashName = p.algorithm.hash.name;
7278-
7279-
var hashFunction = msrcryptoHashFunctions[hashName.toUpperCase()]();
7280-
7281-
msrcryptoKdfInstance = msrcryptoKdf(hashFunction);
7282-
7283-
var alg = p.algorithm;
7284-
7285-
var otherInfo =
7286-
utils.toArray(alg.algorithmId).concat(
7287-
utils.toArray(alg.partyUInfo),
7288-
utils.toArray(alg.partyVInfo),
7289-
utils.toArray(alg.publicInfo),
7290-
utils.toArray(alg.privateInfo));
7279+
msrcryptoConcatKdf.importKey = function(p) {
7280+
var keyData;
72917281

7292-
var result =
7293-
msrcryptoKdfInstance.deriveKey(p.keyData, otherInfo, p.derivedKeyType.length);
7282+
if (p.format === "raw") {
7283+
keyData = msrcryptoUtilities.toArray(p.keyData);
7284+
} else {
7285+
throw new Error("unsupported import format");
7286+
}
72947287

7295-
msrcryptoKdfInstance = null;
7288+
if (p.extractable !== false) {
7289+
throw new Error("only extractable=false is supported.");
7290+
}
72967291

72977292
return {
7298-
type: "keyDerive",
7299-
keyData: result,
7293+
type: "keyImport",
7294+
keyData: keyData,
73007295
keyHandle: {
7301-
algorithm: p.derivedKeyType,
7302-
extractable: p.extractable,
7303-
usages: null || p.usages,
7296+
algorithm: {
7297+
name: "CONCAT"
7298+
},
7299+
extractable: false,
7300+
usages: p.usages,
73047301
type: "secret"
73057302
}
73067303
};
73077304

73087305
};
73097306

7310-
msrcryptoKdf.deriveBits = function(p) {
7311-
7312-
var hashName = p.algorithm.hash.name;
7313-
7314-
var hashFunction = msrcryptoHashFunctions[hashName.toUpperCase()]();
7315-
7316-
msrcryptoKdfInstance = msrcryptoKdf(hashFunction);
7317-
7318-
var alg = p.algorithm;
7319-
7320-
var otherInfo =
7321-
alg.algorithmId.concat(
7322-
alg.partyUInfo,
7323-
alg.partyVInfo,
7324-
alg.publicInfo || [],
7325-
alg.privateInfo || []);
7326-
7327-
var result =
7328-
msrcryptoKdfInstance.deriveKey(p.keyData, otherInfo, p.length);
7329-
7330-
msrcryptoKdfInstance = null;
7331-
7332-
return result;
7333-
7334-
};
7335-
7336-
operations.register("deriveKey", "concat", msrcryptoKdf.deriveKey);
7337-
operations.register("deriveBits", "concat", msrcryptoKdf.deriveBits);
7338-
7307+
operations.register("deriveBits", "CONCAT", msrcryptoConcatKdf.deriveBits);
7308+
operations.register("importKey", "CONCAT", msrcryptoConcatKdf.importKey);
73397309
}
73407310

73417311
var msrcryptoPbkdf2 = (function() {
@@ -7574,6 +7544,113 @@ var msrCryptoVersion = "1.6.3";
75747544
operations.register("importKey", "HKDF", msrcryptoHkdf.importKey);
75757545
}
75767546

7547+
var msrcryptoHkdfCtr = (function() {
7548+
7549+
function deriveBits(p) {
7550+
7551+
var algorithm = p.algorithm,
7552+
keyBytes = p.keyData,
7553+
bits = p.length,
7554+
labelBytes = algorithm.label,
7555+
contextBytes = algorithm.context,
7556+
byteLen = Math.ceil(bits / 8),
7557+
hLen,
7558+
output = [],
7559+
i,
7560+
hmacContext;
7561+
7562+
switch (algorithm.hash.name.toUpperCase()) {
7563+
case "SHA-1":
7564+
hLen = 20;
7565+
break;
7566+
case "SHA-256":
7567+
hLen = 32;
7568+
break;
7569+
case "SHA-384":
7570+
hLen = 48;
7571+
break;
7572+
case "SHA-512":
7573+
hLen = 64;
7574+
break;
7575+
default:
7576+
throw new Error("Unsupported hash algorithm.");
7577+
}
7578+
7579+
if (algorithm.label == null) {
7580+
throw new Error("HkdfCtrParams: label: Missing required property.");
7581+
}
7582+
7583+
if (algorithm.context == null) {
7584+
throw new Error("HkdfCtrParams: context: Missing required property.");
7585+
}
7586+
7587+
if (bits % 8 !== 0) {
7588+
throw new Error("The length provided for HKDF-CTR is not a multiple of 8 bits.");
7589+
}
7590+
7591+
if (byteLen > 255 * hLen) {
7592+
throw new Error("The length provided for HKDF-CTR is too large.");
7593+
}
7594+
7595+
hmacContext = {
7596+
workerid: 0,
7597+
keyHandle: {
7598+
algorithm: algorithm
7599+
},
7600+
keyData: keyBytes,
7601+
buffer: keyBytes
7602+
};
7603+
7604+
var fixed = labelBytes.concat([0], contextBytes, utils.int32ToBytes(bits));
7605+
7606+
for (i = 1; i <= Math.ceil(byteLen / hLen); i++) {
7607+
hmacContext.buffer = utils.int32ToBytes(i).concat(fixed);
7608+
output = output.concat(msrcryptoHmac.signHmac(hmacContext));
7609+
}
7610+
7611+
return output.slice(0, byteLen);
7612+
}
7613+
7614+
return {
7615+
deriveBits: deriveBits
7616+
};
7617+
7618+
}());
7619+
7620+
if (typeof operations !== "undefined") {
7621+
7622+
msrcryptoHkdfCtr.importKey = function(p) {
7623+
var keyData;
7624+
7625+
if (p.format === "raw") {
7626+
keyData = msrcryptoUtilities.toArray(p.keyData);
7627+
} else {
7628+
throw new Error("unsupported import format");
7629+
}
7630+
7631+
if (p.extractable !== false) {
7632+
throw new Error("only extractable=false is supported.");
7633+
}
7634+
7635+
return {
7636+
type: "keyImport",
7637+
keyData: keyData,
7638+
keyHandle: {
7639+
algorithm: {
7640+
name: "HKDF-CTR"
7641+
},
7642+
extractable: false,
7643+
usages: p.usages,
7644+
type: "secret"
7645+
}
7646+
};
7647+
7648+
};
7649+
7650+
operations.register("deriveBits", "HKDF-CTR", msrcryptoHkdfCtr.deriveBits);
7651+
operations.register("importKey", "HKDF-CTR", msrcryptoHkdfCtr.importKey);
7652+
}
7653+
75777654
var msrcryptoEcdh = function(curve) {
75787655

75797656
var btd = cryptoMath.bytesToDigits,

lib/msrcrypto.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)