-
Notifications
You must be signed in to change notification settings - Fork 9
/
hkdf.ts
217 lines (198 loc) · 5.36 KB
/
hkdf.ts
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import type { KdfInterface } from "@hpke/common";
import { EMPTY, InvalidParamError, KdfId, NativeAlgorithm } from "@hpke/common";
// b"HPKE-v1"
const HPKE_VERSION = new Uint8Array([72, 80, 75, 69, 45, 118, 49]);
export class HkdfNative extends NativeAlgorithm implements KdfInterface {
public readonly id: KdfId = KdfId.HkdfSha256;
public readonly hashSize: number = 0;
protected _suiteId: Uint8Array = EMPTY;
protected readonly algHash: HmacKeyGenParams = {
name: "HMAC",
hash: "SHA-256",
length: 256,
};
constructor() {
super();
}
public init(suiteId: Uint8Array): void {
this._suiteId = suiteId;
}
public buildLabeledIkm(label: Uint8Array, ikm: Uint8Array): Uint8Array {
this._checkInit();
const ret = new Uint8Array(
7 + this._suiteId.byteLength + label.byteLength + ikm.byteLength,
);
ret.set(HPKE_VERSION, 0);
ret.set(this._suiteId, 7);
ret.set(label, 7 + this._suiteId.byteLength);
ret.set(ikm, 7 + this._suiteId.byteLength + label.byteLength);
return ret;
}
public buildLabeledInfo(
label: Uint8Array,
info: Uint8Array,
len: number,
): Uint8Array {
this._checkInit();
const ret = new Uint8Array(
9 + this._suiteId.byteLength + label.byteLength + info.byteLength,
);
ret.set(new Uint8Array([0, len]), 0);
ret.set(HPKE_VERSION, 2);
ret.set(this._suiteId, 9);
ret.set(label, 9 + this._suiteId.byteLength);
ret.set(info, 9 + this._suiteId.byteLength + label.byteLength);
return ret;
}
public async extract(
salt: ArrayBuffer,
ikm: ArrayBuffer,
): Promise<ArrayBuffer> {
await this._setup();
if (salt.byteLength === 0) {
salt = new ArrayBuffer(this.hashSize);
}
if (salt.byteLength !== this.hashSize) {
throw new InvalidParamError(
"The salt length must be the same as the hashSize",
);
}
const key = await (this._api as SubtleCrypto).importKey(
"raw",
salt,
this.algHash,
false,
[
"sign",
],
);
return await (this._api as SubtleCrypto).sign("HMAC", key, ikm);
}
public async expand(
prk: ArrayBuffer,
info: ArrayBuffer,
len: number,
): Promise<ArrayBuffer> {
await this._setup();
const key = await (this._api as SubtleCrypto).importKey(
"raw",
prk,
this.algHash,
false,
[
"sign",
],
);
const okm = new ArrayBuffer(len);
const p = new Uint8Array(okm);
let prev = EMPTY;
const mid = new Uint8Array(info);
const tail = new Uint8Array(1);
if (len > 255 * this.hashSize) {
throw new Error("Entropy limit reached");
}
const tmp = new Uint8Array(this.hashSize + mid.length + 1);
for (let i = 1, cur = 0; cur < p.length; i++) {
tail[0] = i;
tmp.set(prev, 0);
tmp.set(mid, prev.length);
tmp.set(tail, prev.length + mid.length);
prev = new Uint8Array(
await (this._api as SubtleCrypto).sign(
"HMAC",
key,
tmp.slice(0, prev.length + mid.length + 1),
),
);
if (p.length - cur >= prev.length) {
p.set(prev, cur);
cur += prev.length;
} else {
p.set(prev.slice(0, p.length - cur), cur);
cur += p.length - cur;
}
}
return okm;
}
public async extractAndExpand(
salt: ArrayBuffer,
ikm: ArrayBuffer,
info: ArrayBuffer,
len: number,
): Promise<ArrayBuffer> {
await this._setup();
const baseKey = await (this._api as SubtleCrypto).importKey(
"raw",
ikm,
"HKDF",
false,
["deriveBits"],
);
return await (this._api as SubtleCrypto).deriveBits(
{
name: "HKDF",
hash: this.algHash.hash,
salt: salt,
info: info,
},
baseKey,
len * 8,
);
}
public async labeledExtract(
salt: ArrayBuffer,
label: Uint8Array,
ikm: Uint8Array,
): Promise<ArrayBuffer> {
return await this.extract(salt, this.buildLabeledIkm(label, ikm));
}
public async labeledExpand(
prk: ArrayBuffer,
label: Uint8Array,
info: Uint8Array,
len: number,
): Promise<ArrayBuffer> {
return await this.expand(prk, this.buildLabeledInfo(label, info, len), len);
}
protected _checkInit(): void {
if (this._suiteId === EMPTY) {
throw new Error("Not initialized. Call init()");
}
}
}
export class HkdfSha256Native extends HkdfNative {
/** KdfId.HkdfSha256 (0x0001) */
public readonly id: KdfId = KdfId.HkdfSha256;
/** 32 */
public readonly hashSize: number = 32;
/** The parameters for Web Cryptography API */
protected readonly algHash: HmacKeyGenParams = {
name: "HMAC",
hash: "SHA-256",
length: 256,
};
}
export class HkdfSha384Native extends HkdfNative {
/** KdfId.HkdfSha384 (0x0002) */
public readonly id: KdfId = KdfId.HkdfSha384;
/** 48 */
public readonly hashSize: number = 48;
/** The parameters for Web Cryptography API */
protected readonly algHash: HmacKeyGenParams = {
name: "HMAC",
hash: "SHA-384",
length: 384,
};
}
export class HkdfSha512Native extends HkdfNative {
/** KdfId.HkdfSha512 (0x0003) */
public readonly id: KdfId = KdfId.HkdfSha512;
/** 64 */
public readonly hashSize: number = 64;
/** The parameters for Web Cryptography API */
protected readonly algHash: HmacKeyGenParams = {
name: "HMAC",
hash: "SHA-512",
length: 512,
};
}