Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix implementation, with additional tests #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
The HKDF Key Derivation Function
# node-hkdf

The HMAC-based Key Derivation Function for node.js.

spec: https://tools.ietf.org/html/rfc5869

## install

npm install hkdf

## use

const HKDF = require('hkdf');

var hkdf = new HKDF('sha256', 'salt123', 'initialKeyingMaterial');
hkdf.derive('info', 42, function(key) {
// key is a Buffer, that can be serialized however one desires
console.log(key.toString('hex'));
});

## license

Apache License 2.0
21 changes: 14 additions & 7 deletions lib/hkdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ var crypto = require("crypto");
function zeros(length) {
var buf = new Buffer(length);

// XXX is this the character zero, or the byte 0.
buf.fill("0");
buf.fill(0);

return buf.toString();
}
Expand All @@ -33,20 +32,28 @@ function HKDF(hashAlg, salt, ikm) {

HKDF.prototype = {
derive: function(info, size, cb) {
var prev = "";
var output = new Buffer(size);
var prev = new Buffer(0);
var output;
var buffers = [];
var num_blocks = Math.ceil(size / this.hashLength);
info = new Buffer(info);

for (var i=0; i<num_blocks; i++) {
var hmac = crypto.createHmac(this.hashAlg, this.prk);
var input = prev + info + (0x01 * (i+1));
// XXX is there a more optimal way to build up buffers?
var input = Buffer.concat([
prev,
info,
new Buffer(String.fromCharCode(i + 1))
]);
hmac.update(input);
prev = hmac.digest();
output.write(prev, this.hashLength * i, this.hashLength, 'binary');
buffers.push(prev);
}
output = Buffer.concat(buffers, size);

process.nextTick(function() {cb(output);});
}
};

module.exports = HKDF;
module.exports = HKDF;
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name" : "node-hkdf",
"version" : "0.0.1",
"private" : true,
"description" : "HKDF key derivation function",
"main" : "index",
"repository" : {
Expand Down
38 changes: 36 additions & 2 deletions test/all-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,30 @@ suite.addBatch({
"derives keys of the right length": function(err, key) {
assert.equal(key.length, 42);
}
},
"an hkdf with no salt" : {
topic: function() {
var self = this;
var hkdf = new HKDF('sha256', null, 'initialKeyingMaterial');
hkdf.derive('info', 42, function(key) {
// key is a Buffer, that can be serialized however one desires
self.callback(null, key);
});
},
"derives keys of the right length": function(err, key) {
// expected value generated using:
// https://github.com/warner/id-keywrapping-demo/blob/master/hkdf.py
assert.equal(key.toString('hex'), "5ff4745de6729d5a523337170ab6e12f202c81611bb8cb80e4b44675a16371cadd8e62eebc777b08bf83");
}
}
});

// test vectors from RFC5869
suite.addBatch({
"test vector": {
topic: function() {
var self = this;

var ikm = new Buffer("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", 'hex');
var salt = new Buffer("000102030405060708090a0b0c", 'hex');
var info = new Buffer("f0f1f2f3f4f5f6f7f8f9", 'hex');
Expand All @@ -39,7 +55,25 @@ suite.addBatch({
"works": function(err, output) {
assert.equal(output.toString('hex'), "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865");
}
},
"test another vector": {
topic: function() {
var self = this;

var ikm = new Buffer("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b", 'hex');
var salt = new Buffer(0); // empty
var info = ''; // empty

var hkdf = new HKDF('sha256', salt, ikm);
hkdf.derive(info, 42, function(key) {
// key is a Buffer, that can be serialized however one desires
self.callback(null, key);
});
},
"works": function(err, output) {
assert.equal(output.toString('hex'), "8da4e775a563c18f715f802a063c5a31b8a11f5c5ee1879ec3454e5f3c738d2d9d201395faa4b61a96c8");
}
}
})

suite.export(module);
suite.export(module);