-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkdf.js
177 lines (147 loc) · 3.76 KB
/
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
const isBuffer = require('is-buffer')
/* eslint-disable camelcase */
const {
crypto_kdf_CONTEXTBYTES,
crypto_kdf_KEYBYTES,
crypto_kdf_derive_from_key,
crypto_kdf_keygen,
} = require('./sodium')
/**
* Generates a master key.
*
* @public
* @param {?(Buffer)} [key]
* @returns {Buffer}
* @throws TypeError
*/
function keygen(key) {
if (key && false === isBuffer(key)) {
throw new TypeError('kdf.keygen: Expecting key to be a buffer.')
}
if (undefined === key) {
key = Buffer.allocUnsafe(crypto_kdf_KEYBYTES)
}
if (crypto_kdf_KEYBYTES !== key.length) {
throw new TypeError(`kdf.keygen: Invalid key length: ${key.length}`)
}
crypto_kdf_keygen(key)
return key
}
/**
* Initializes key derivation.
*
* @public
* @param {Buffer} key
* @param {?(Buffer)} [buffer]
* @return {Object}
* @throws TypeError
*/
function init(key, buffer) {
if (key && false === isBuffer(key)) {
throw new TypeError('kdf.init: Expecting key to be a buffer.')
}
if (undefined === key) {
throw new TypeError('kdf.init: Expecting key to be defined.')
}
if (buffer) {
if (false === isBuffer(buffer)) {
throw new TypeError('kdf.init: Expecting context to be a buffer.')
}
if (crypto_kdf_CONTEXTBYTES !== buffer.length) {
throw new TypeError(`kdf.init: Invalid context length: ${buffer.length}`)
}
}
return {
buffer: buffer || Buffer.alloc(crypto_kdf_CONTEXTBYTES),
subkey: null,
key
}
}
/**
* Updates the subkey in the context object.
*
* @public
* @param {Object} ctx
* @param {Number} id
* @return {Buffer}
* @throws TypeError
*/
function update(ctx, id) {
if ('object' !== typeof ctx) {
throw new TypeError('kdf.update: Expecting ctx to be an object.')
}
if (ctx.subkey && !isBuffer(ctx.subkey)) {
throw new TypeError('kdf.update: Expecting ctx.subkey to be a buffer.')
}
if (!isBuffer(ctx.buffer)) {
throw new TypeError('kdf.update: Expecting ctx.buffer to be a buffer.')
}
if (crypto_kdf_CONTEXTBYTES !== ctx.buffer.length) {
throw new TypeError(`kdf.update: Invalid buffer length: ${ctx.buffer.length}.`)
}
if (!isBuffer(ctx.key)) {
throw new TypeError('kdf.update: Expecting ctx.key to be a buffer.')
}
if ('number' !== typeof id) {
throw new TypeError('kdf.update: Expecting id to be a number.')
}
if (id < 0 || id > (2 ** 64) - 1) {
throw new TypeError('kdf.update: Expecting id to be between 0 and (2^64)-1.')
}
ctx.subkey = ctx.subkey || Buffer.allocUnsafe(crypto_kdf_KEYBYTES)
crypto_kdf_derive_from_key(
ctx.subkey,
id, ctx.buffer,
ctx.key.slice(0, crypto_kdf_KEYBYTES)
)
return ctx.subkey
}
/**
* Final step to null the original context subkey.
*
* @public
* @param {Object} ctx
* @return {Buffer}
* @throws TypeError
*/
function final(ctx) {
if ('object' !== typeof ctx) {
throw new TypeError('kdf.final: Expecting ctx to be an object.')
}
const { subkey } = ctx
ctx.subkey = null
return subkey
}
/**
* Derives a subkey using the master key and context.
*
* @public
* @param {Buffer} key
* @param {Number} iterations
* @param {?(Buffer)} [buffer]
* @return {Buffer}
* @throws TypeError
*/
function derive(key, iterations, buffer) {
const ctx = init(key, buffer)
if (iterations && 'number' !== typeof iterations) {
throw new TypeError('kdf.derive: Expecting subkeyId to be a number.')
}
if (iterations < 1 || iterations > (2 ** 64) - 1) {
throw new TypeError('kdf.derive: Expecting iterations to be between 1 and (2^64)-1.')
}
if (undefined === iterations) {
throw new TypeError('kdf.derive: Expecting iterations to be defined.')
}
for (let i = 0; i < iterations; ++i) {
update(ctx, i + 1)
}
return final(ctx)
}
module.exports = {
derive,
keygen,
update,
final,
init,
}