<!-- Utilities for manipulating ABVs -->
<script src="util.js"></script>
<!-- A simple wrapper around IndexedDB -->
<script src="simpledb.js"></script>
<!-- Test vectors drawn from the literature -->
<script src="./test-vectors.js"></script>
<!-- General testing framework -->
<script src="./test-array.js"></script>
<script>/* <![CDATA[*/ "use strict";
// -----------------------------------------------------------------------------
TestArray.addTest( "Deriving zero bits should produce an empty array",
function() { var that = this; var key = util.hex2abv("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
var alg = {
name: "HKDF",
hash: "SHA-256",
salt: new Uint8Array(),
info: new Uint8Array(),
};
// -----------------------------------------------------------------------------
TestArray.addTest( "Derive eight bits with HKDF, no salt or info given",
function() { var that = this; var key = util.hex2abv("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
var alg = {
name: "HKDF",
hash: "SHA-256",
salt: new Uint8Array(),
info: new Uint8Array(),
};
// -----------------------------------------------------------------------------
TestArray.addTest( "Deriving too many bits should fail",
function() { var that = this; var key = util.hex2abv("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
var alg = {
name: "HKDF",
hash: "SHA-256",
salt: new Uint8Array(),
info: new Uint8Array(),
};
function deriveBits(x) {
// The maximum length (in bytes) of output material for HKDF is 255 times
// the digest length. In this case, the digest length (in bytes) of
// SHA-256 is 32; 32*255 = 8160. deriveBits expects the length to be in
// bits, so 8160*8=65280 and add 1 to exceed the maximum length.
return crypto.subtle.deriveBits(alg, x, 65281);
}
// -----------------------------------------------------------------------------
TestArray.addTest( "Deriving with an unsupported PRF should fail",
function() { var that = this; var key = util.hex2abv("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
var alg = {
name: "HKDF",
hash: "HMAC",
salt: new Uint8Array(),
info: new Uint8Array(),
};
function deriveBits(x) {
return crypto.subtle.deriveBits(alg, x, 8);
}
// -----------------------------------------------------------------------------
TestArray.addTest( "Deriving with a non-HKDF key should fail",
function() { var that = this;
var alg = {
name: "HKDF",
hash: "HMAC",
salt: new Uint8Array(),
info: new Uint8Array(),
};
function deriveBits(x) {
return crypto.subtle.deriveBits(alg, x, 8);
}
// -----------------------------------------------------------------------------
TestArray.addTest( "Derive known values from test vectors (SHA-1 and SHA-256)",
function() { var that = this; var tests = tv.hkdf.slice();
if (!tests.length) {
error(that)("No tests found");
return;
}
function next() {
if (!tests.length) {
return Promise.resolve();
}
// -----------------------------------------------------------------------------
TestArray.addTest( "Derive known values from test vectors (JWK, SHA-256)",
function() { var that = this; var test = tv.hkdf[0]; var alg = {
name: "HKDF",
hash: test.prf,
salt: test.salt,
info: test.info,
};
// -----------------------------------------------------------------------------
TestArray.addTest( "Test wrapping/unwrapping an HKDF key",
function() { var that = this; var hkdfKey = util.hex2abv("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"); var alg = {name: "AES-GCM", length: 256, iv: new Uint8Array(16)}; var wrappingKey;
// -----------------------------------------------------------------------------
TestArray.addTest( "Unwrapping an HKDF key in PKCS8 format should fail",
function() { var that = this; var hkdfKey = util.hex2abv("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"); var alg = {name: "AES-GCM", length: 256, iv: new Uint8Array(16)}; var wrappingKey;
// -----------------------------------------------------------------------------
TestArray.addTest( "Derive an AES key using with HKDF",
function() { var that = this; var key = util.hex2abv("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
var alg = {
name: "HKDF",
hash: "SHA-256",
salt: new Uint8Array(),
info: new Uint8Array(),
};
function deriveKey(x) { var targetAlg = {name: "AES-GCM", length: 256};
return crypto.subtle.deriveKey(alg, x, targetAlg, false, ["encrypt"]);
}
// -----------------------------------------------------------------------------
TestArray.addTest( "Deriving an HKDF key with HKDF should fail",
function() { var that = this; var key = util.hex2abv("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b");
var alg = {
name: "HKDF",
hash: "SHA-256",
salt: new Uint8Array(),
info: new Uint8Array(),
};
function deriveKey(x) {
return crypto.subtle.deriveKey(alg, x, "HKDF", false, ["deriveBits"]);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.