-
Notifications
You must be signed in to change notification settings - Fork 9
/
x448.ts
243 lines (222 loc) · 6.25 KB
/
x448.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { x448 } from "@noble/curves/ed448";
import type { DhkemPrimitives, KdfInterface } from "@hpke/core";
import {
DeriveKeyPairError,
DeserializeError,
NotSupportedError,
SerializeError,
XCryptoKey,
} from "@hpke/core";
const ALG_NAME = "X448";
const EMPTY = new Uint8Array();
// The key usages for KEM.
const KEM_USAGES: KeyUsage[] = ["deriveBits"];
// b"dkp_prk" deno-fmt-ignore
const LABEL_DKP_PRK: Uint8Array = new Uint8Array([
100, 107, 112, 95, 112, 114, 107,
]);
// b"sk"
const LABEL_SK: Uint8Array = new Uint8Array([115, 107]);
function base64UrlToBytes(v: string): Uint8Array {
const base64 = v.replace(/-/g, "+").replace(/_/g, "/");
const byteString = atob(base64);
const ret = new Uint8Array(byteString.length);
for (let i = 0; i < byteString.length; i++) {
ret[i] = byteString.charCodeAt(i);
}
return ret;
}
export class X448 implements DhkemPrimitives {
private _hkdf: KdfInterface;
private _nPk: number;
private _nSk: number;
constructor(hkdf: KdfInterface) {
this._hkdf = hkdf;
this._nPk = 56;
this._nSk = 56;
}
public async serializePublicKey(key: CryptoKey): Promise<ArrayBuffer> {
try {
return await this._serializePublicKey(key as XCryptoKey);
} catch (e: unknown) {
throw new SerializeError(e);
}
}
public async deserializePublicKey(key: ArrayBuffer): Promise<CryptoKey> {
try {
return await this._importRawKey(key, true);
} catch (e: unknown) {
throw new DeserializeError(e);
}
}
public async serializePrivateKey(key: CryptoKey): Promise<ArrayBuffer> {
try {
return await this._serializePrivateKey(key as XCryptoKey);
} catch (e: unknown) {
throw new SerializeError(e);
}
}
public async deserializePrivateKey(key: ArrayBuffer): Promise<CryptoKey> {
try {
return await this._importRawKey(key, false);
} catch (e: unknown) {
throw new DeserializeError(e);
}
}
public async importKey(
format: "raw" | "jwk",
key: ArrayBuffer | JsonWebKey,
isPublic: boolean,
): Promise<CryptoKey> {
try {
if (format === "raw") {
return await this._importRawKey(key as ArrayBuffer, isPublic);
}
// jwk
if (key instanceof ArrayBuffer) {
throw new Error("Invalid jwk key format");
}
return await this._importJWK(key as JsonWebKey, isPublic);
} catch (e: unknown) {
throw new DeserializeError(e);
}
}
public async generateKeyPair(): Promise<CryptoKeyPair> {
try {
const rawSk = x448.utils.randomPrivateKey();
const sk = new XCryptoKey(ALG_NAME, rawSk, "private", KEM_USAGES);
const pk = await this.derivePublicKey(sk);
return { publicKey: pk, privateKey: sk };
} catch (e: unknown) {
throw new NotSupportedError(e);
}
}
public async deriveKeyPair(ikm: ArrayBuffer): Promise<CryptoKeyPair> {
try {
const dkpPrk = await this._hkdf.labeledExtract(
EMPTY,
LABEL_DKP_PRK,
new Uint8Array(ikm),
);
const rawSk = await this._hkdf.labeledExpand(
dkpPrk,
LABEL_SK,
EMPTY,
this._nSk,
);
const sk = new XCryptoKey(
ALG_NAME,
new Uint8Array(rawSk),
"private",
KEM_USAGES,
);
return {
privateKey: sk,
publicKey: await this.derivePublicKey(sk),
};
} catch (e: unknown) {
throw new DeriveKeyPairError(e);
}
}
public async derivePublicKey(key: CryptoKey): Promise<CryptoKey> {
try {
return await this._derivePublicKey(key as XCryptoKey);
} catch (e: unknown) {
throw new DeserializeError(e);
}
}
public async dh(sk: CryptoKey, pk: CryptoKey): Promise<ArrayBuffer> {
try {
return await this._dh(sk as XCryptoKey, pk as XCryptoKey);
} catch (e: unknown) {
throw new SerializeError(e);
}
}
private _serializePublicKey(k: XCryptoKey): Promise<ArrayBuffer> {
return new Promise((resolve) => {
resolve(k.key.buffer);
});
}
private _serializePrivateKey(k: XCryptoKey): Promise<ArrayBuffer> {
return new Promise((resolve) => {
resolve(k.key.buffer);
});
}
private _importRawKey(
key: ArrayBuffer,
isPublic: boolean,
): Promise<CryptoKey> {
return new Promise((resolve, reject) => {
if (isPublic && key.byteLength !== this._nPk) {
reject(new Error("Invalid length of the key"));
}
if (!isPublic && (key.byteLength !== this._nSk)) {
reject(new Error("Invalid length of the key"));
}
resolve(
new XCryptoKey(
ALG_NAME,
new Uint8Array(key),
isPublic ? "public" : "private",
isPublic ? [] : KEM_USAGES,
),
);
});
}
private _importJWK(key: JsonWebKey, isPublic: boolean): Promise<CryptoKey> {
return new Promise((resolve, reject) => {
if (key.kty !== "OKP") {
reject(new Error(`Invalid kty: ${key.kty}`));
}
if (key.crv !== "X448") {
reject(new Error(`Invalid crv: ${key.crv}`));
}
if (isPublic) {
if (typeof key.d !== "undefined") {
reject(new Error("Invalid key: `d` should not be set"));
}
if (typeof key.x !== "string") {
reject(new Error("Invalid key: `x` not found"));
}
resolve(
new XCryptoKey(
ALG_NAME,
base64UrlToBytes(key.x as string),
"public",
),
);
} else {
if (typeof key.d !== "string") {
reject(new Error("Invalid key: `d` not found"));
}
resolve(
new XCryptoKey(
ALG_NAME,
base64UrlToBytes(key.d as string),
"private",
KEM_USAGES,
),
);
}
});
}
private _derivePublicKey(k: XCryptoKey): Promise<CryptoKey> {
return new Promise((resolve, reject) => {
try {
const pk = x448.getPublicKey(k.key);
resolve(new XCryptoKey(ALG_NAME, pk, "public"));
} catch (e: unknown) {
reject(e);
}
});
}
private _dh(sk: XCryptoKey, pk: XCryptoKey): Promise<ArrayBuffer> {
return new Promise((resolve, reject) => {
try {
resolve(x448.getSharedSecret(sk.key, pk.key).buffer);
} catch (e: unknown) {
reject(e);
}
});
}
}