1
|
'use strict';
|
2
|
|
3
|
const assert = require('assert');
|
4
|
const HmacDRBG = require('../');
|
5
|
const hash = require('hash.js');
|
6
|
|
7
|
describe('Hmac_DRBG', () => {
|
8
|
it('should support hmac-drbg-sha256', () => {
|
9
|
function doDrbg(opt) {
|
10
|
const drbg = HmacDRBG({
|
11
|
hash: hash.sha256,
|
12
|
entropy: opt.entropy,
|
13
|
entropyEnc: 'utf8',
|
14
|
nonce: opt.nonce,
|
15
|
nonceEnc: 'utf8',
|
16
|
pers: opt.pers,
|
17
|
persEnc: 'utf8'
|
18
|
});
|
19
|
return drbg.generate(opt.size, 'hex');
|
20
|
}
|
21
|
|
22
|
const test = [
|
23
|
{
|
24
|
entropy: 'totally random0123456789',
|
25
|
nonce: 'secret nonce',
|
26
|
pers: 'my drbg',
|
27
|
size: 32,
|
28
|
res: '018ec5f8e08c41e5ac974eb129ac297c5388ee1864324fa13d9b15cf98d9a157'
|
29
|
},
|
30
|
{
|
31
|
entropy: 'totally random0123456789',
|
32
|
nonce: 'secret nonce',
|
33
|
pers: null,
|
34
|
size: 32,
|
35
|
res: 'ed5d61ecf0ef38258e62f03bbb49f19f2cd07ba5145a840d83b134d5963b3633'
|
36
|
}
|
37
|
];
|
38
|
for (let i = 0; i < test.length; i++)
|
39
|
assert.equal(doDrbg(test[i]), test[i].res);
|
40
|
});
|
41
|
|
42
|
describe('NIST vector', function() {
|
43
|
require('./fixtures/hmac-drbg-nist.json').forEach(function (opt) {
|
44
|
it('should not fail at ' + opt.name, function() {
|
45
|
const drbg = HmacDRBG({
|
46
|
hash: hash.sha256,
|
47
|
entropy: opt.entropy,
|
48
|
nonce: opt.nonce,
|
49
|
pers: opt.pers
|
50
|
});
|
51
|
|
52
|
let last;
|
53
|
for (let i = 0; i < opt.add.length; i++) {
|
54
|
let add = opt.add[i];
|
55
|
last = drbg.generate(opt.expected.length / 2, 'hex', add);
|
56
|
}
|
57
|
assert.equal(last, opt.expected);
|
58
|
});
|
59
|
});
|
60
|
});
|
61
|
|
62
|
describe('reseeding', function() {
|
63
|
it('should reseed', function() {
|
64
|
const entropy = 'totally random string with many chars that I typed ' +
|
65
|
'in agony';
|
66
|
const nonce = 'nonce';
|
67
|
const pers = 'pers';
|
68
|
|
69
|
const original = HmacDRBG({
|
70
|
hash: hash.sha256,
|
71
|
entropy,
|
72
|
nonce,
|
73
|
pers
|
74
|
});
|
75
|
const reseeded = HmacDRBG({
|
76
|
hash: hash.sha256,
|
77
|
entropy,
|
78
|
nonce,
|
79
|
pers
|
80
|
});
|
81
|
|
82
|
assert.strictEqual(original.generate(32, 'hex'),
|
83
|
reseeded.generate(32, 'hex'));
|
84
|
|
85
|
reseeded.reseed('another absolutely random string');
|
86
|
|
87
|
assert.notEqual(original.generate(32, 'hex'),
|
88
|
reseeded.generate(32, 'hex'));
|
89
|
});
|
90
|
});
|
91
|
});
|