@decryption/ciphers 0.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/LICENSE +22 -0
- package/README.md +75 -0
- package/_arx.d.ts +144 -0
- package/_arx.js +375 -0
- package/_poly1305.d.ts +72 -0
- package/_poly1305.js +367 -0
- package/_polyval.d.ts +120 -0
- package/_polyval.js +335 -0
- package/aes.d.ts +461 -0
- package/aes.js +1768 -0
- package/chacha.d.ts +248 -0
- package/chacha.js +579 -0
- package/esm/_arx.js +368 -0
- package/esm/_poly1305.js +363 -0
- package/esm/_polyval.js +329 -0
- package/esm/aes.js +1764 -0
- package/esm/chacha.js +574 -0
- package/esm/ff1.js +203 -0
- package/esm/index.js +26 -0
- package/esm/salsa.js +370 -0
- package/esm/utils.js +806 -0
- package/esm/webcrypto.js +166 -0
- package/ff1.d.ts +39 -0
- package/ff1.js +207 -0
- package/index.d.ts +0 -0
- package/index.js +26 -0
- package/package.json +45 -0
- package/salsa.d.ts +114 -0
- package/salsa.js +375 -0
- package/utils.d.ts +716 -0
- package/utils.js +841 -0
- package/webcrypto.d.ts +88 -0
- package/webcrypto.js +169 -0
package/webcrypto.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WebCrypto-based AES gcm/ctr/cbc, `managedNonce` and `randomBytes`.
|
|
3
|
+
* We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
|
4
|
+
* @module
|
|
5
|
+
*/
|
|
6
|
+
import { type AsyncCipher, type TArg, type TRet } from './utils';
|
|
7
|
+
type WebcryptoUtils = {
|
|
8
|
+
encrypt(key: TArg<Uint8Array>, keyParams: unknown, cryptParams: unknown, plaintext: TArg<Uint8Array>): Promise<TRet<Uint8Array>>;
|
|
9
|
+
decrypt(key: TArg<Uint8Array>, keyParams: unknown, cryptParams: unknown, ciphertext: TArg<Uint8Array>): Promise<TRet<Uint8Array>>;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Internal webcrypto utils. Can be overridden if crypto.subtle is not present,
|
|
13
|
+
* for example in React Native.
|
|
14
|
+
* Raw keys are re-imported on every call; this wrapper intentionally does not
|
|
15
|
+
* cache `CryptoKey` objects between operations.
|
|
16
|
+
*/
|
|
17
|
+
export declare const utils: TRet<WebcryptoUtils>;
|
|
18
|
+
/**
|
|
19
|
+
* AES-CBC implemented with WebCrypto.
|
|
20
|
+
* Uses WebCrypto's built-in PKCS padding behavior; exact IV-length checks are
|
|
21
|
+
* delegated to the backend instead of local `abytes(..., 16)` validation.
|
|
22
|
+
* @param key - AES key bytes.
|
|
23
|
+
* @param iv - 16-byte initialization vector.
|
|
24
|
+
* @returns Async cipher instance.
|
|
25
|
+
* @example
|
|
26
|
+
* Encrypts a block with the browser or Node WebCrypto backend.
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { cbc } from './webcrypto';
|
|
30
|
+
* import { randomBytes } from './utils';
|
|
31
|
+
* const key = randomBytes(16);
|
|
32
|
+
* const iv = randomBytes(16);
|
|
33
|
+
* const cipher = cbc(key, iv);
|
|
34
|
+
* await cipher.encrypt(new Uint8Array(16));
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
export declare const cbc: TRet<((key: TArg<Uint8Array>, iv: TArg<Uint8Array>) => AsyncCipher) & {
|
|
38
|
+
blockSize: number;
|
|
39
|
+
nonceLength: number;
|
|
40
|
+
}>;
|
|
41
|
+
/**
|
|
42
|
+
* AES-CTR implemented with WebCrypto.
|
|
43
|
+
* Uses WebCrypto's full 128-bit counter-length setting so the whole
|
|
44
|
+
* 16-byte counter block is incremented, matching sync `aes.ts:ctr`.
|
|
45
|
+
* @param key - AES key bytes.
|
|
46
|
+
* @param nonce - 16-byte counter block incremented as a full big-endian AES counter block.
|
|
47
|
+
* @returns Async cipher instance.
|
|
48
|
+
* @example
|
|
49
|
+
* Encrypts a short payload with WebCrypto AES-CTR.
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* import { ctr } from './webcrypto';
|
|
53
|
+
* import { randomBytes } from './utils';
|
|
54
|
+
* const key = randomBytes(16);
|
|
55
|
+
* const nonce = randomBytes(16);
|
|
56
|
+
* const cipher = ctr(key, nonce);
|
|
57
|
+
* await cipher.encrypt(new Uint8Array([1, 2, 3]));
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare const ctr: TRet<((key: TArg<Uint8Array>, nonce: TArg<Uint8Array>) => AsyncCipher) & {
|
|
61
|
+
blockSize: number;
|
|
62
|
+
nonceLength: number;
|
|
63
|
+
}>;
|
|
64
|
+
/**
|
|
65
|
+
* AES-GCM implemented with WebCrypto.
|
|
66
|
+
* AAD type normalization and nonce-shape enforcement beyond raw bytes are left
|
|
67
|
+
* to the backend WebCrypto implementation.
|
|
68
|
+
* @param key - AES key bytes.
|
|
69
|
+
* @param nonce - Nonce bytes.
|
|
70
|
+
* @param AAD - Additional authenticated data.
|
|
71
|
+
* @returns Async cipher instance.
|
|
72
|
+
* @example
|
|
73
|
+
* Encrypts and authenticates plaintext with WebCrypto AES-GCM.
|
|
74
|
+
*
|
|
75
|
+
* ```ts
|
|
76
|
+
* import { gcm } from './webcrypto';
|
|
77
|
+
* import { randomBytes } from './utils';
|
|
78
|
+
* const key = randomBytes(16);
|
|
79
|
+
* const nonce = randomBytes(12);
|
|
80
|
+
* const cipher = gcm(key, nonce);
|
|
81
|
+
* await cipher.encrypt(new Uint8Array([1, 2, 3]));
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
export declare const gcm: TRet<((key: TArg<Uint8Array>, nonce: TArg<Uint8Array>, AAD?: TArg<Uint8Array>) => AsyncCipher) & {
|
|
85
|
+
blockSize: number;
|
|
86
|
+
nonceLength: number;
|
|
87
|
+
}>;
|
|
88
|
+
export {};
|
package/webcrypto.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.gcm = exports.ctr = exports.cbc = exports.utils = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* WebCrypto-based AES gcm/ctr/cbc, `managedNonce` and `randomBytes`.
|
|
6
|
+
* We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
const utils_1 = require("./utils");
|
|
10
|
+
function getWebcryptoSubtle() {
|
|
11
|
+
const cr = typeof globalThis !== 'undefined' && globalThis.crypto;
|
|
12
|
+
if (cr && typeof cr.subtle === 'object' && cr.subtle != null)
|
|
13
|
+
return cr.subtle;
|
|
14
|
+
throw new Error('crypto.subtle must be defined');
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Internal webcrypto utils. Can be overridden if crypto.subtle is not present,
|
|
18
|
+
* for example in React Native.
|
|
19
|
+
* Raw keys are re-imported on every call; this wrapper intentionally does not
|
|
20
|
+
* cache `CryptoKey` objects between operations.
|
|
21
|
+
*/
|
|
22
|
+
exports.utils = {
|
|
23
|
+
async encrypt(key, keyParams, cryptParams, plaintext) {
|
|
24
|
+
const cr = getWebcryptoSubtle();
|
|
25
|
+
const iKey = await cr.importKey('raw', key, keyParams, true, ['encrypt']);
|
|
26
|
+
const ciphertext = await cr.encrypt(cryptParams, iKey, plaintext);
|
|
27
|
+
return new Uint8Array(ciphertext);
|
|
28
|
+
},
|
|
29
|
+
async decrypt(key, keyParams, cryptParams, ciphertext) {
|
|
30
|
+
const cr = getWebcryptoSubtle();
|
|
31
|
+
const iKey = await cr.importKey('raw', key, keyParams, true, ['decrypt']);
|
|
32
|
+
const plaintext = await cr.decrypt(cryptParams, iKey, ciphertext);
|
|
33
|
+
return new Uint8Array(plaintext);
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
const mode = {
|
|
37
|
+
CBC: 'AES-CBC',
|
|
38
|
+
CTR: 'AES-CTR',
|
|
39
|
+
GCM: 'AES-GCM',
|
|
40
|
+
};
|
|
41
|
+
function getCryptParams(algo, nonce, AAD) {
|
|
42
|
+
if (algo === mode.CBC)
|
|
43
|
+
return { name: mode.CBC, iv: nonce };
|
|
44
|
+
// WebCrypto allows 1..128 counter bits; use the full block to match sync ctr() / Node CTR wrap.
|
|
45
|
+
if (algo === mode.CTR)
|
|
46
|
+
return { name: mode.CTR, counter: nonce, length: 128 };
|
|
47
|
+
if (algo === mode.GCM) {
|
|
48
|
+
// Rely on the backend default tag length (128 bits) instead of setting it explicitly.
|
|
49
|
+
if (AAD)
|
|
50
|
+
return { name: mode.GCM, iv: nonce, additionalData: AAD };
|
|
51
|
+
else
|
|
52
|
+
return { name: mode.GCM, iv: nonce };
|
|
53
|
+
}
|
|
54
|
+
throw new Error('unknown aes block mode');
|
|
55
|
+
}
|
|
56
|
+
function generate(algo, nonceLength) {
|
|
57
|
+
(0, utils_1.anumber)(nonceLength);
|
|
58
|
+
const res = (key, nonce, AAD) => {
|
|
59
|
+
(0, utils_1.abytes)(key);
|
|
60
|
+
(0, utils_1.abytes)(nonce);
|
|
61
|
+
// Reject falsy non-byte AAD locally; otherwise false/0/''/null silently become "no AAD".
|
|
62
|
+
if (AAD !== undefined)
|
|
63
|
+
(0, utils_1.abytes)(AAD, undefined, 'AAD');
|
|
64
|
+
// Exact nonce-length enforcement and WebCrypto-specific AAD normalization are
|
|
65
|
+
// delegated to the backend; locally we only require byte-array inputs here.
|
|
66
|
+
// Keep caller key/nonce/AAD by reference; mutating them after
|
|
67
|
+
// construction changes later operations.
|
|
68
|
+
const keyParams = { name: algo, length: key.length * 8 };
|
|
69
|
+
const cryptParams = getCryptParams(algo, nonce, AAD);
|
|
70
|
+
let consumed = false;
|
|
71
|
+
return {
|
|
72
|
+
// keyLength,
|
|
73
|
+
encrypt(plaintext) {
|
|
74
|
+
(0, utils_1.abytes)(plaintext);
|
|
75
|
+
if (consumed)
|
|
76
|
+
throw new Error('Cannot encrypt() twice with same key / nonce');
|
|
77
|
+
consumed = true;
|
|
78
|
+
return exports.utils.encrypt(key, keyParams, cryptParams, plaintext);
|
|
79
|
+
},
|
|
80
|
+
decrypt(ciphertext) {
|
|
81
|
+
(0, utils_1.abytes)(ciphertext);
|
|
82
|
+
return exports.utils.decrypt(key, keyParams, cryptParams, ciphertext);
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
};
|
|
86
|
+
res.nonceLength = nonceLength;
|
|
87
|
+
res.blockSize = 16; // always for AES
|
|
88
|
+
return res;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* AES-CBC implemented with WebCrypto.
|
|
92
|
+
* Uses WebCrypto's built-in PKCS padding behavior; exact IV-length checks are
|
|
93
|
+
* delegated to the backend instead of local `abytes(..., 16)` validation.
|
|
94
|
+
* @param key - AES key bytes.
|
|
95
|
+
* @param iv - 16-byte initialization vector.
|
|
96
|
+
* @returns Async cipher instance.
|
|
97
|
+
* @example
|
|
98
|
+
* Encrypts a block with the browser or Node WebCrypto backend.
|
|
99
|
+
*
|
|
100
|
+
* ```ts
|
|
101
|
+
* import { cbc } from './webcrypto';
|
|
102
|
+
* import { randomBytes } from './utils';
|
|
103
|
+
* const key = randomBytes(16);
|
|
104
|
+
* const iv = randomBytes(16);
|
|
105
|
+
* const cipher = cbc(key, iv);
|
|
106
|
+
* await cipher.encrypt(new Uint8Array(16));
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
exports.cbc = (() => generate(mode.CBC, 16))();
|
|
110
|
+
/**
|
|
111
|
+
* AES-CTR implemented with WebCrypto.
|
|
112
|
+
* Uses WebCrypto's full 128-bit counter-length setting so the whole
|
|
113
|
+
* 16-byte counter block is incremented, matching sync `aes.ts:ctr`.
|
|
114
|
+
* @param key - AES key bytes.
|
|
115
|
+
* @param nonce - 16-byte counter block incremented as a full big-endian AES counter block.
|
|
116
|
+
* @returns Async cipher instance.
|
|
117
|
+
* @example
|
|
118
|
+
* Encrypts a short payload with WebCrypto AES-CTR.
|
|
119
|
+
*
|
|
120
|
+
* ```ts
|
|
121
|
+
* import { ctr } from './webcrypto';
|
|
122
|
+
* import { randomBytes } from './utils';
|
|
123
|
+
* const key = randomBytes(16);
|
|
124
|
+
* const nonce = randomBytes(16);
|
|
125
|
+
* const cipher = ctr(key, nonce);
|
|
126
|
+
* await cipher.encrypt(new Uint8Array([1, 2, 3]));
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
exports.ctr = (() => generate(mode.CTR, 16))();
|
|
130
|
+
/**
|
|
131
|
+
* AES-GCM implemented with WebCrypto.
|
|
132
|
+
* AAD type normalization and nonce-shape enforcement beyond raw bytes are left
|
|
133
|
+
* to the backend WebCrypto implementation.
|
|
134
|
+
* @param key - AES key bytes.
|
|
135
|
+
* @param nonce - Nonce bytes.
|
|
136
|
+
* @param AAD - Additional authenticated data.
|
|
137
|
+
* @returns Async cipher instance.
|
|
138
|
+
* @example
|
|
139
|
+
* Encrypts and authenticates plaintext with WebCrypto AES-GCM.
|
|
140
|
+
*
|
|
141
|
+
* ```ts
|
|
142
|
+
* import { gcm } from './webcrypto';
|
|
143
|
+
* import { randomBytes } from './utils';
|
|
144
|
+
* const key = randomBytes(16);
|
|
145
|
+
* const nonce = randomBytes(12);
|
|
146
|
+
* const cipher = gcm(key, nonce);
|
|
147
|
+
* await cipher.encrypt(new Uint8Array([1, 2, 3]));
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
exports.gcm = (() => generate(mode.GCM, 12))();
|
|
151
|
+
// // Type tests
|
|
152
|
+
// import { siv, gcm, ctr, ecb, cbc } from '../aes';
|
|
153
|
+
// import { xsalsa20poly1305 } from '../salsa';
|
|
154
|
+
// import { chacha20poly1305, xchacha20poly1305 } from '../chacha';
|
|
155
|
+
// const wsiv = managedNonce(siv);
|
|
156
|
+
// const wgcm = managedNonce(gcm);
|
|
157
|
+
// const wctr = managedNonce(ctr);
|
|
158
|
+
// const wcbc = managedNonce(cbc);
|
|
159
|
+
// const wsalsapoly = managedNonce(xsalsa20poly1305);
|
|
160
|
+
// const wchacha = managedNonce(chacha20poly1305);
|
|
161
|
+
// const wxchacha = managedNonce(xchacha20poly1305);
|
|
162
|
+
// // should fail
|
|
163
|
+
// const wcbc2 = managedNonce(managedNonce(cbc));
|
|
164
|
+
// const wctr = managedNonce(ctr);
|
|
165
|
+
// import { gcm as gcmSync } from '../aes';
|
|
166
|
+
// const x1 = managedNonce(gcmSync);
|
|
167
|
+
// // const x1: (key: Uint8Array, AAD?: Uint8Array | undefined) => Cipher
|
|
168
|
+
// const x2 = managedNonce(gcm);
|
|
169
|
+
// // const x2: (key: Uint8Array, AAD?: Uint8Array | undefined) => AsyncCipher
|