@push.rocks/smartcrypto 2.1.0 → 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 +5 -1
- package/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/index.d.ts +4 -0
- package/dist_ts/index.js +4 -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 +9 -5
- package/readme.md +112 -57
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/index.ts +7 -0
- 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
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export type TSmartCryptoErrorCode =
|
|
2
|
+
| 'AUTHENTICATION_FAILED'
|
|
3
|
+
| 'CONTEXT_MISMATCH'
|
|
4
|
+
| 'CRYPTO_OPERATION_FAILED'
|
|
5
|
+
| 'INVALID_ARGUMENT'
|
|
6
|
+
| 'INVALID_ENVELOPE'
|
|
7
|
+
| 'INVALID_KEY'
|
|
8
|
+
| 'INVALID_SHARED_SECRET'
|
|
9
|
+
| 'RECIPIENT_MISMATCH'
|
|
10
|
+
| 'SIZE_LIMIT_EXCEEDED'
|
|
11
|
+
| 'UNSUPPORTED_PROFILE'
|
|
12
|
+
| 'UNSUPPORTED_VERSION';
|
|
13
|
+
|
|
14
|
+
export interface ISmartCryptoErrorJson {
|
|
15
|
+
name: 'SmartCryptoError';
|
|
16
|
+
code: TSmartCryptoErrorCode;
|
|
17
|
+
message: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const inspectSymbol = Symbol.for('nodejs.util.inspect.custom');
|
|
21
|
+
|
|
22
|
+
/** A stable error that intentionally retains no sensitive operation details. */
|
|
23
|
+
export class SmartCryptoError extends Error {
|
|
24
|
+
public readonly code: TSmartCryptoErrorCode;
|
|
25
|
+
public readonly cause: undefined;
|
|
26
|
+
|
|
27
|
+
constructor(codeArg: TSmartCryptoErrorCode) {
|
|
28
|
+
super(`SmartCrypto operation failed (${codeArg}).`);
|
|
29
|
+
this.name = 'SmartCryptoError';
|
|
30
|
+
this.code = codeArg;
|
|
31
|
+
Object.defineProperty(this, 'cause', {
|
|
32
|
+
configurable: false,
|
|
33
|
+
enumerable: false,
|
|
34
|
+
value: undefined,
|
|
35
|
+
writable: false,
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
public toJSON(): ISmartCryptoErrorJson {
|
|
40
|
+
return {
|
|
41
|
+
name: 'SmartCryptoError',
|
|
42
|
+
code: this.code,
|
|
43
|
+
message: this.message,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
public [inspectSymbol](): ISmartCryptoErrorJson {
|
|
48
|
+
return this.toJSON();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const createSmartCryptoError = (
|
|
53
|
+
codeArg: TSmartCryptoErrorCode,
|
|
54
|
+
): SmartCryptoError => new SmartCryptoError(codeArg);
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SmartCryptoError,
|
|
3
|
+
createSmartCryptoError,
|
|
4
|
+
type TSmartCryptoErrorCode,
|
|
5
|
+
} from './smartcrypto.errors.js';
|
|
6
|
+
|
|
7
|
+
const base64UrlPattern = /^[A-Za-z0-9_-]*$/;
|
|
8
|
+
|
|
9
|
+
export const getWebCrypto = (): Crypto => {
|
|
10
|
+
if (!globalThis.crypto?.subtle || typeof globalThis.crypto.getRandomValues !== 'function') {
|
|
11
|
+
throw createSmartCryptoError('CRYPTO_OPERATION_FAILED');
|
|
12
|
+
}
|
|
13
|
+
return globalThis.crypto;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export const requireUint8Array = (
|
|
17
|
+
valueArg: unknown,
|
|
18
|
+
codeArg: TSmartCryptoErrorCode = 'INVALID_ARGUMENT',
|
|
19
|
+
): Uint8Array => {
|
|
20
|
+
if (!(valueArg instanceof Uint8Array)) {
|
|
21
|
+
throw createSmartCryptoError(codeArg);
|
|
22
|
+
}
|
|
23
|
+
return new Uint8Array(valueArg);
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export const requireLength = (
|
|
27
|
+
valueArg: Uint8Array,
|
|
28
|
+
lengthArg: number,
|
|
29
|
+
codeArg: TSmartCryptoErrorCode,
|
|
30
|
+
): Uint8Array => {
|
|
31
|
+
if (valueArg.byteLength !== lengthArg) {
|
|
32
|
+
valueArg.fill(0);
|
|
33
|
+
throw createSmartCryptoError(codeArg);
|
|
34
|
+
}
|
|
35
|
+
return valueArg;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const toArrayBuffer = (valueArg: Uint8Array): ArrayBuffer => {
|
|
39
|
+
const result = new Uint8Array(valueArg.byteLength);
|
|
40
|
+
result.set(valueArg);
|
|
41
|
+
return result.buffer;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
export const concatBytes = (...valuesArg: Uint8Array[]): Uint8Array => {
|
|
45
|
+
const byteLength = valuesArg.reduce((sumArg, valueArg) => sumArg + valueArg.byteLength, 0);
|
|
46
|
+
const result = new Uint8Array(byteLength);
|
|
47
|
+
let offset = 0;
|
|
48
|
+
for (const value of valuesArg) {
|
|
49
|
+
result.set(value, offset);
|
|
50
|
+
offset += value.byteLength;
|
|
51
|
+
}
|
|
52
|
+
return result;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export const sha256 = async (valueArg: Uint8Array): Promise<Uint8Array> => {
|
|
56
|
+
const valueBuffer = toArrayBuffer(valueArg);
|
|
57
|
+
try {
|
|
58
|
+
return new Uint8Array(await getWebCrypto().subtle.digest('SHA-256', valueBuffer));
|
|
59
|
+
} finally {
|
|
60
|
+
new Uint8Array(valueBuffer).fill(0);
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export const bytesEqual = (leftArg: Uint8Array, rightArg: Uint8Array): boolean => {
|
|
65
|
+
if (leftArg.byteLength !== rightArg.byteLength) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
let difference = 0;
|
|
69
|
+
for (let index = 0; index < leftArg.byteLength; index++) {
|
|
70
|
+
difference |= leftArg[index] ^ rightArg[index];
|
|
71
|
+
}
|
|
72
|
+
return difference === 0;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export const isAllZero = (valueArg: Uint8Array): boolean => {
|
|
76
|
+
let combined = 0;
|
|
77
|
+
for (const value of valueArg) {
|
|
78
|
+
combined |= value;
|
|
79
|
+
}
|
|
80
|
+
return combined === 0;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
export const encodeBase64Url = (valueArg: Uint8Array): string => {
|
|
84
|
+
let binary = '';
|
|
85
|
+
const chunkSize = 0x8000;
|
|
86
|
+
for (let offset = 0; offset < valueArg.byteLength; offset += chunkSize) {
|
|
87
|
+
binary += String.fromCharCode(...valueArg.subarray(offset, offset + chunkSize));
|
|
88
|
+
}
|
|
89
|
+
return btoa(binary).replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/g, '');
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export const decodeCanonicalBase64Url = (
|
|
93
|
+
valueArg: unknown,
|
|
94
|
+
maximumBytesArg: number,
|
|
95
|
+
exactBytesArg?: number,
|
|
96
|
+
): Uint8Array => {
|
|
97
|
+
if (typeof valueArg !== 'string') {
|
|
98
|
+
throw createSmartCryptoError('INVALID_ENVELOPE');
|
|
99
|
+
}
|
|
100
|
+
const maximumEncodedLength = Math.ceil(maximumBytesArg * 4 / 3);
|
|
101
|
+
if (valueArg.length > maximumEncodedLength) {
|
|
102
|
+
throw createSmartCryptoError('SIZE_LIMIT_EXCEEDED');
|
|
103
|
+
}
|
|
104
|
+
if (!base64UrlPattern.test(valueArg) || valueArg.length % 4 === 1) {
|
|
105
|
+
throw createSmartCryptoError('INVALID_ENVELOPE');
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
const paddingLength = (4 - valueArg.length % 4) % 4;
|
|
109
|
+
const base64 = valueArg.replace(/-/g, '+').replace(/_/g, '/') + '='.repeat(paddingLength);
|
|
110
|
+
const binary = atob(base64);
|
|
111
|
+
if (binary.length > maximumBytesArg) {
|
|
112
|
+
throw createSmartCryptoError('SIZE_LIMIT_EXCEEDED');
|
|
113
|
+
}
|
|
114
|
+
if (exactBytesArg !== undefined && binary.length !== exactBytesArg) {
|
|
115
|
+
throw createSmartCryptoError('INVALID_ENVELOPE');
|
|
116
|
+
}
|
|
117
|
+
const result = new Uint8Array(binary.length);
|
|
118
|
+
for (let index = 0; index < binary.length; index++) {
|
|
119
|
+
result[index] = binary.charCodeAt(index);
|
|
120
|
+
}
|
|
121
|
+
if (encodeBase64Url(result) !== valueArg) {
|
|
122
|
+
throw createSmartCryptoError('INVALID_ENVELOPE');
|
|
123
|
+
}
|
|
124
|
+
return result;
|
|
125
|
+
} catch (error) {
|
|
126
|
+
if (error instanceof SmartCryptoError) {
|
|
127
|
+
throw error;
|
|
128
|
+
}
|
|
129
|
+
throw createSmartCryptoError('INVALID_ENVELOPE');
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
export const requirePlainDataObject = (
|
|
134
|
+
valueArg: unknown,
|
|
135
|
+
expectedKeysArg: readonly string[],
|
|
136
|
+
): Record<string, unknown> => {
|
|
137
|
+
if (valueArg === null || typeof valueArg !== 'object' || Array.isArray(valueArg)) {
|
|
138
|
+
throw createSmartCryptoError('INVALID_ENVELOPE');
|
|
139
|
+
}
|
|
140
|
+
const prototype = Object.getPrototypeOf(valueArg);
|
|
141
|
+
if (prototype !== Object.prototype && prototype !== null) {
|
|
142
|
+
throw createSmartCryptoError('INVALID_ENVELOPE');
|
|
143
|
+
}
|
|
144
|
+
const ownKeys = Reflect.ownKeys(valueArg);
|
|
145
|
+
if (ownKeys.some((keyArg) => typeof keyArg !== 'string')) {
|
|
146
|
+
throw createSmartCryptoError('INVALID_ENVELOPE');
|
|
147
|
+
}
|
|
148
|
+
const keys = ownKeys as string[];
|
|
149
|
+
if (
|
|
150
|
+
keys.length !== expectedKeysArg.length
|
|
151
|
+
|| [...keys].sort().some((keyArg, indexArg) => keyArg !== [...expectedKeysArg].sort()[indexArg])
|
|
152
|
+
) {
|
|
153
|
+
throw createSmartCryptoError('INVALID_ENVELOPE');
|
|
154
|
+
}
|
|
155
|
+
for (const key of keys) {
|
|
156
|
+
const descriptor = Object.getOwnPropertyDescriptor(valueArg, key);
|
|
157
|
+
if (!descriptor?.enumerable || !('value' in descriptor)) {
|
|
158
|
+
throw createSmartCryptoError('INVALID_ENVELOPE');
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return valueArg as Record<string, unknown>;
|
|
162
|
+
};
|
|
@@ -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
|
+
};
|