-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathcrypto_kdf.js
70 lines (52 loc) · 1.96 KB
/
crypto_kdf.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const test = require('brittle')
const sodium = require('..')
test('crypto_kdf_keygen', function (t) {
const key = Buffer.alloc(sodium.crypto_kdf_KEYBYTES)
t.exception.all(function () {
sodium.crypto_kdf_keygen(Buffer.alloc(1))
})
sodium.crypto_kdf_keygen(key)
t.not(key, Buffer.alloc(key.length))
})
test('crypto_kdf_derive_from_key', function (t) {
const key = Buffer.alloc(sodium.crypto_kdf_KEYBYTES)
sodium.crypto_kdf_keygen(key)
const subkey = Buffer.alloc(sodium.crypto_kdf_BYTES_MIN)
sodium.crypto_kdf_derive_from_key(subkey, 0, Buffer.from('context_'), key)
t.not(subkey, Buffer.alloc(subkey.length))
const subkey2 = Buffer.alloc(sodium.crypto_kdf_BYTES_MIN)
sodium.crypto_kdf_derive_from_key(subkey2, 1, Buffer.from('context_'), key)
t.not(subkey, subkey2)
sodium.crypto_kdf_derive_from_key(subkey2, 0, Buffer.from('context_'), key)
t.alike(subkey, subkey2)
})
test('test vectors', function (assert) {
const fixtures = require('./fixtures/crypto_kdf.json')
for (let i = 0; i < fixtures.length; i++) {
const key = Buffer.from(fixtures[i].key, 'hex')
const subkeyLen = fixtures[i].subkey_len
const id = fixtures[i].id
const context = Buffer.from(fixtures[i].context, 'hex')
const shouldError = fixtures[i].error
const actual = Buffer.alloc(subkeyLen)
try {
sodium.crypto_kdf_derive_from_key(actual, id, context, key)
const expected = Buffer.from(fixtures[i].subkey, 'hex')
if (Buffer.compare(actual, expected) !== 0) {
assert.fail('Failed on fixture #' + i)
}
} catch (ex) {
if (shouldError === false) assert.fail('Failed on fixture #' + i)
}
}
assert.pass('Passed all fixtures')
assert.end()
})
test('constants', function (t) {
t.ok(sodium.crypto_kdf_PRIMITIVE)
t.ok(sodium.crypto_kdf_BYTES_MAX > 0)
t.ok(sodium.crypto_kdf_BYTES_MIN <= sodium.crypto_kdf_BYTES_MAX)
t.ok(sodium.crypto_kdf_CONTEXTBYTES > 0)
t.ok(sodium.crypto_kdf_KEYBYTES >= 16)
t.end()
})