@push.rocks/smartcrypto 2.0.5 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.smartconfig.json +66 -0
- package/dist_ts/00_commitinfo_data.d.ts +1 -1
- package/dist_ts/00_commitinfo_data.js +4 -4
- package/dist_ts/index.d.ts +4 -0
- package/dist_ts/index.js +4 -1
- package/dist_ts/smartcrypto.classes.privatekey.d.ts +2 -3
- package/dist_ts/smartcrypto.classes.privatekey.js +1 -1
- package/dist_ts/smartcrypto.classes.publickey.d.ts +13 -3
- package/dist_ts/smartcrypto.classes.publickey.js +81 -1
- package/dist_ts/smartcrypto.errors.d.ts +17 -0
- package/dist_ts/smartcrypto.errors.js +27 -0
- package/dist_ts/smartcrypto.internal.d.ts +12 -0
- package/dist_ts/smartcrypto.internal.js +133 -0
- package/dist_ts/smartcrypto.webcrypto.d.ts +49 -0
- package/dist_ts/smartcrypto.webcrypto.js +179 -0
- package/dist_ts/smartcrypto.x25519.d.ts +46 -0
- package/dist_ts/smartcrypto.x25519.js +339 -0
- package/license +2 -1
- package/package.json +28 -9
- package/readme.hints.md +1 -0
- package/readme.md +146 -28
- package/ts/00_commitinfo_data.ts +3 -3
- package/ts/index.ts +7 -0
- package/ts/smartcrypto.classes.privatekey.ts +2 -2
- package/ts/smartcrypto.classes.publickey.ts +128 -2
- package/ts/smartcrypto.errors.ts +54 -0
- package/ts/smartcrypto.internal.ts +162 -0
- package/ts/smartcrypto.webcrypto.ts +266 -0
- package/ts/smartcrypto.x25519.ts +493 -0
- package/npmextra.json +0 -18
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
import { SmartCryptoError, createSmartCryptoError } from './smartcrypto.errors.js';
|
|
2
|
+
import {
|
|
3
|
+
concatBytes,
|
|
4
|
+
getWebCrypto,
|
|
5
|
+
requireLength,
|
|
6
|
+
requireUint8Array,
|
|
7
|
+
toArrayBuffer,
|
|
8
|
+
} from './smartcrypto.internal.js';
|
|
9
|
+
|
|
10
|
+
export const SMARTCRYPTO_AES_256_KEY_BYTES = 32;
|
|
11
|
+
export const SMARTCRYPTO_AES_GCM_NONCE_BYTES = 12;
|
|
12
|
+
export const SMARTCRYPTO_AES_GCM_TAG_BYTES = 16;
|
|
13
|
+
|
|
14
|
+
export interface IAesGcmCiphertext {
|
|
15
|
+
nonce: Uint8Array;
|
|
16
|
+
ciphertext: Uint8Array;
|
|
17
|
+
tag: Uint8Array;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface IAesGcmEncryptOptions {
|
|
21
|
+
key: Uint8Array;
|
|
22
|
+
plaintext: Uint8Array;
|
|
23
|
+
aad: Uint8Array;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface IAesGcmDecryptOptions {
|
|
27
|
+
key: Uint8Array;
|
|
28
|
+
encryptedData: IAesGcmCiphertext;
|
|
29
|
+
aad: Uint8Array;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface IWrapDekOptions {
|
|
33
|
+
dek: Uint8Array;
|
|
34
|
+
kek: Uint8Array;
|
|
35
|
+
wrapAad: Uint8Array;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface IUnwrapDekOptions {
|
|
39
|
+
wrappedDek: IAesGcmCiphertext;
|
|
40
|
+
kek: Uint8Array;
|
|
41
|
+
wrapAad: Uint8Array;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface IRewrapDekOptions {
|
|
45
|
+
wrappedDek: IAesGcmCiphertext;
|
|
46
|
+
currentKek: Uint8Array;
|
|
47
|
+
currentWrapAad: Uint8Array;
|
|
48
|
+
newKek: Uint8Array;
|
|
49
|
+
newWrapAad: Uint8Array;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const importAesKey = async (
|
|
53
|
+
rawKeyArg: Uint8Array,
|
|
54
|
+
usagesArg: KeyUsage[],
|
|
55
|
+
): Promise<CryptoKey> => {
|
|
56
|
+
const rawKey = requireLength(
|
|
57
|
+
requireUint8Array(rawKeyArg, 'INVALID_KEY'),
|
|
58
|
+
SMARTCRYPTO_AES_256_KEY_BYTES,
|
|
59
|
+
'INVALID_KEY',
|
|
60
|
+
);
|
|
61
|
+
const rawKeyBuffer = toArrayBuffer(rawKey);
|
|
62
|
+
try {
|
|
63
|
+
return await getWebCrypto().subtle.importKey(
|
|
64
|
+
'raw',
|
|
65
|
+
rawKeyBuffer,
|
|
66
|
+
{ name: 'AES-GCM' },
|
|
67
|
+
false,
|
|
68
|
+
usagesArg,
|
|
69
|
+
);
|
|
70
|
+
} catch {
|
|
71
|
+
throw createSmartCryptoError('INVALID_KEY');
|
|
72
|
+
} finally {
|
|
73
|
+
rawKey.fill(0);
|
|
74
|
+
new Uint8Array(rawKeyBuffer).fill(0);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const copyEncryptedData = (valueArg: IAesGcmCiphertext): IAesGcmCiphertext => {
|
|
79
|
+
if (valueArg === null || typeof valueArg !== 'object') {
|
|
80
|
+
throw createSmartCryptoError('INVALID_ARGUMENT');
|
|
81
|
+
}
|
|
82
|
+
return {
|
|
83
|
+
nonce: requireLength(
|
|
84
|
+
requireUint8Array(valueArg.nonce),
|
|
85
|
+
SMARTCRYPTO_AES_GCM_NONCE_BYTES,
|
|
86
|
+
'INVALID_ARGUMENT',
|
|
87
|
+
),
|
|
88
|
+
ciphertext: requireUint8Array(valueArg.ciphertext),
|
|
89
|
+
tag: requireLength(
|
|
90
|
+
requireUint8Array(valueArg.tag),
|
|
91
|
+
SMARTCRYPTO_AES_GCM_TAG_BYTES,
|
|
92
|
+
'INVALID_ARGUMENT',
|
|
93
|
+
),
|
|
94
|
+
};
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
/** Generates a raw 32-byte data-encryption key. */
|
|
98
|
+
export const generateAes256GcmKey = (): Uint8Array =>
|
|
99
|
+
getWebCrypto().getRandomValues(new Uint8Array(SMARTCRYPTO_AES_256_KEY_BYTES));
|
|
100
|
+
|
|
101
|
+
/** Generates a raw 32-byte data-encryption key. */
|
|
102
|
+
export const generateDek = generateAes256GcmKey;
|
|
103
|
+
|
|
104
|
+
/** Encrypts bytes with AES-256-GCM and a fresh 96-bit nonce. AAD is mandatory. */
|
|
105
|
+
export const aesGcmEncrypt = async (
|
|
106
|
+
optionsArg: IAesGcmEncryptOptions,
|
|
107
|
+
): Promise<IAesGcmCiphertext> => {
|
|
108
|
+
if (optionsArg === null || typeof optionsArg !== 'object') {
|
|
109
|
+
throw createSmartCryptoError('INVALID_ARGUMENT');
|
|
110
|
+
}
|
|
111
|
+
const plaintextInput = optionsArg.plaintext;
|
|
112
|
+
const aadInput = optionsArg.aad;
|
|
113
|
+
const keyInput = optionsArg.key;
|
|
114
|
+
if (
|
|
115
|
+
!(plaintextInput instanceof Uint8Array)
|
|
116
|
+
|| !(aadInput instanceof Uint8Array)
|
|
117
|
+
) {
|
|
118
|
+
throw createSmartCryptoError('INVALID_ARGUMENT');
|
|
119
|
+
}
|
|
120
|
+
let plaintext: Uint8Array | undefined;
|
|
121
|
+
let aad: Uint8Array | undefined;
|
|
122
|
+
let plaintextBuffer: ArrayBuffer | undefined;
|
|
123
|
+
let aadBuffer: ArrayBuffer | undefined;
|
|
124
|
+
let combined: Uint8Array | undefined;
|
|
125
|
+
try {
|
|
126
|
+
plaintext = new Uint8Array(plaintextInput);
|
|
127
|
+
aad = new Uint8Array(aadInput);
|
|
128
|
+
const crypto = getWebCrypto();
|
|
129
|
+
const nonce = crypto.getRandomValues(new Uint8Array(SMARTCRYPTO_AES_GCM_NONCE_BYTES));
|
|
130
|
+
const key = await importAesKey(keyInput, ['encrypt']);
|
|
131
|
+
plaintextBuffer = toArrayBuffer(plaintext);
|
|
132
|
+
aadBuffer = toArrayBuffer(aad);
|
|
133
|
+
combined = new Uint8Array(await crypto.subtle.encrypt(
|
|
134
|
+
{
|
|
135
|
+
name: 'AES-GCM',
|
|
136
|
+
iv: toArrayBuffer(nonce),
|
|
137
|
+
additionalData: aadBuffer,
|
|
138
|
+
tagLength: SMARTCRYPTO_AES_GCM_TAG_BYTES * 8,
|
|
139
|
+
},
|
|
140
|
+
key,
|
|
141
|
+
plaintextBuffer,
|
|
142
|
+
));
|
|
143
|
+
if (combined.byteLength < SMARTCRYPTO_AES_GCM_TAG_BYTES) {
|
|
144
|
+
throw createSmartCryptoError('CRYPTO_OPERATION_FAILED');
|
|
145
|
+
}
|
|
146
|
+
const tagOffset = combined.byteLength - SMARTCRYPTO_AES_GCM_TAG_BYTES;
|
|
147
|
+
return {
|
|
148
|
+
nonce: new Uint8Array(nonce),
|
|
149
|
+
ciphertext: combined.slice(0, tagOffset),
|
|
150
|
+
tag: combined.slice(tagOffset),
|
|
151
|
+
};
|
|
152
|
+
} catch (error) {
|
|
153
|
+
if (error instanceof SmartCryptoError) {
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
throw createSmartCryptoError('CRYPTO_OPERATION_FAILED');
|
|
157
|
+
} finally {
|
|
158
|
+
plaintext?.fill(0);
|
|
159
|
+
aad?.fill(0);
|
|
160
|
+
if (plaintextBuffer) {
|
|
161
|
+
new Uint8Array(plaintextBuffer).fill(0);
|
|
162
|
+
}
|
|
163
|
+
if (aadBuffer) {
|
|
164
|
+
new Uint8Array(aadBuffer).fill(0);
|
|
165
|
+
}
|
|
166
|
+
combined?.fill(0);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/** Decrypts a split AES-256-GCM ciphertext and authenticates mandatory AAD. */
|
|
171
|
+
export const aesGcmDecrypt = async (
|
|
172
|
+
optionsArg: IAesGcmDecryptOptions,
|
|
173
|
+
): Promise<Uint8Array> => {
|
|
174
|
+
if (optionsArg === null || typeof optionsArg !== 'object') {
|
|
175
|
+
throw createSmartCryptoError('INVALID_ARGUMENT');
|
|
176
|
+
}
|
|
177
|
+
const encryptedData = copyEncryptedData(optionsArg.encryptedData);
|
|
178
|
+
const aad = requireUint8Array(optionsArg.aad);
|
|
179
|
+
let aadBuffer: ArrayBuffer | undefined;
|
|
180
|
+
let combined: Uint8Array | undefined;
|
|
181
|
+
let combinedBuffer: ArrayBuffer | undefined;
|
|
182
|
+
try {
|
|
183
|
+
const key = await importAesKey(optionsArg.key, ['decrypt']);
|
|
184
|
+
aadBuffer = toArrayBuffer(aad);
|
|
185
|
+
combined = concatBytes(encryptedData.ciphertext, encryptedData.tag);
|
|
186
|
+
combinedBuffer = toArrayBuffer(combined);
|
|
187
|
+
return new Uint8Array(await getWebCrypto().subtle.decrypt(
|
|
188
|
+
{
|
|
189
|
+
name: 'AES-GCM',
|
|
190
|
+
iv: toArrayBuffer(encryptedData.nonce),
|
|
191
|
+
additionalData: aadBuffer,
|
|
192
|
+
tagLength: SMARTCRYPTO_AES_GCM_TAG_BYTES * 8,
|
|
193
|
+
},
|
|
194
|
+
key,
|
|
195
|
+
combinedBuffer,
|
|
196
|
+
));
|
|
197
|
+
} catch {
|
|
198
|
+
throw createSmartCryptoError('AUTHENTICATION_FAILED');
|
|
199
|
+
} finally {
|
|
200
|
+
aad.fill(0);
|
|
201
|
+
if (aadBuffer) {
|
|
202
|
+
new Uint8Array(aadBuffer).fill(0);
|
|
203
|
+
}
|
|
204
|
+
combined?.fill(0);
|
|
205
|
+
if (combinedBuffer) {
|
|
206
|
+
new Uint8Array(combinedBuffer).fill(0);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
|
|
211
|
+
/** Wraps one exact 32-byte DEK using an exact 32-byte KEK and independent AAD. */
|
|
212
|
+
export const wrapDek = async (optionsArg: IWrapDekOptions): Promise<IAesGcmCiphertext> => {
|
|
213
|
+
if (optionsArg === null || typeof optionsArg !== 'object') {
|
|
214
|
+
throw createSmartCryptoError('INVALID_ARGUMENT');
|
|
215
|
+
}
|
|
216
|
+
const dek = requireLength(
|
|
217
|
+
requireUint8Array(optionsArg.dek, 'INVALID_KEY'),
|
|
218
|
+
SMARTCRYPTO_AES_256_KEY_BYTES,
|
|
219
|
+
'INVALID_KEY',
|
|
220
|
+
);
|
|
221
|
+
try {
|
|
222
|
+
return await aesGcmEncrypt({
|
|
223
|
+
key: optionsArg.kek,
|
|
224
|
+
plaintext: dek,
|
|
225
|
+
aad: optionsArg.wrapAad,
|
|
226
|
+
});
|
|
227
|
+
} finally {
|
|
228
|
+
dek.fill(0);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
/** Unwraps and validates one exact 32-byte DEK. */
|
|
233
|
+
export const unwrapDek = async (optionsArg: IUnwrapDekOptions): Promise<Uint8Array> => {
|
|
234
|
+
if (optionsArg === null || typeof optionsArg !== 'object') {
|
|
235
|
+
throw createSmartCryptoError('INVALID_ARGUMENT');
|
|
236
|
+
}
|
|
237
|
+
const dek = await aesGcmDecrypt({
|
|
238
|
+
key: optionsArg.kek,
|
|
239
|
+
encryptedData: optionsArg.wrappedDek,
|
|
240
|
+
aad: optionsArg.wrapAad,
|
|
241
|
+
});
|
|
242
|
+
return requireLength(dek, SMARTCRYPTO_AES_256_KEY_BYTES, 'INVALID_KEY');
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
/** Rewraps a DEK without receiving or modifying its payload ciphertext. */
|
|
246
|
+
export const rewrapDek = async (
|
|
247
|
+
optionsArg: IRewrapDekOptions,
|
|
248
|
+
): Promise<IAesGcmCiphertext> => {
|
|
249
|
+
if (optionsArg === null || typeof optionsArg !== 'object') {
|
|
250
|
+
throw createSmartCryptoError('INVALID_ARGUMENT');
|
|
251
|
+
}
|
|
252
|
+
const dek = await unwrapDek({
|
|
253
|
+
wrappedDek: optionsArg.wrappedDek,
|
|
254
|
+
kek: optionsArg.currentKek,
|
|
255
|
+
wrapAad: optionsArg.currentWrapAad,
|
|
256
|
+
});
|
|
257
|
+
try {
|
|
258
|
+
return await wrapDek({
|
|
259
|
+
dek,
|
|
260
|
+
kek: optionsArg.newKek,
|
|
261
|
+
wrapAad: optionsArg.newWrapAad,
|
|
262
|
+
});
|
|
263
|
+
} finally {
|
|
264
|
+
dek.fill(0);
|
|
265
|
+
}
|
|
266
|
+
};
|