@enbox/crypto 0.0.2 → 0.0.3

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.
Files changed (182) hide show
  1. package/README.md +34 -102
  2. package/dist/browser.js +6 -10
  3. package/dist/browser.js.map +4 -4
  4. package/dist/browser.mjs +6 -10
  5. package/dist/browser.mjs.map +4 -4
  6. package/dist/esm/algorithms/aes-gcm.js +1 -1
  7. package/dist/esm/algorithms/aes-gcm.js.map +1 -1
  8. package/dist/esm/algorithms/ecdsa.js +9 -5
  9. package/dist/esm/algorithms/ecdsa.js.map +1 -1
  10. package/dist/esm/algorithms/eddsa.js +9 -5
  11. package/dist/esm/algorithms/eddsa.js.map +1 -1
  12. package/dist/esm/algorithms/sha-2.js +1 -1
  13. package/dist/esm/algorithms/sha-2.js.map +1 -1
  14. package/dist/esm/crypto-error.js +41 -0
  15. package/dist/esm/crypto-error.js.map +1 -0
  16. package/dist/esm/index.js +3 -0
  17. package/dist/esm/index.js.map +1 -1
  18. package/dist/esm/jose/jwk.js +52 -26
  19. package/dist/esm/jose/jwk.js.map +1 -1
  20. package/dist/esm/local-key-manager.js +3 -2
  21. package/dist/esm/local-key-manager.js.map +1 -1
  22. package/dist/esm/primitives/aes-ctr.js.map +1 -1
  23. package/dist/esm/primitives/aes-gcm.js.map +1 -1
  24. package/dist/esm/primitives/aes-kw.js +246 -0
  25. package/dist/esm/primitives/aes-kw.js.map +1 -0
  26. package/dist/esm/primitives/concat-kdf.js +1 -1
  27. package/dist/esm/primitives/concat-kdf.js.map +1 -1
  28. package/dist/esm/primitives/ed25519.js +3 -3
  29. package/dist/esm/primitives/ed25519.js.map +1 -1
  30. package/dist/esm/primitives/hkdf.js +79 -0
  31. package/dist/esm/primitives/hkdf.js.map +1 -0
  32. package/dist/esm/primitives/pbkdf2.js +49 -0
  33. package/dist/esm/primitives/pbkdf2.js.map +1 -1
  34. package/dist/esm/primitives/secp256k1.js +4 -4
  35. package/dist/esm/primitives/secp256k1.js.map +1 -1
  36. package/dist/esm/primitives/secp256r1.js +4 -4
  37. package/dist/esm/primitives/secp256r1.js.map +1 -1
  38. package/dist/esm/primitives/x25519.js +1 -1
  39. package/dist/esm/primitives/x25519.js.map +1 -1
  40. package/dist/esm/primitives/xchacha20-poly1305.js +48 -3
  41. package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -1
  42. package/dist/esm/primitives/xchacha20.js +1 -1
  43. package/dist/esm/primitives/xchacha20.js.map +1 -1
  44. package/dist/esm/utils.js.map +1 -1
  45. package/dist/types/algorithms/aes-ctr.d.ts +1 -1
  46. package/dist/types/algorithms/aes-ctr.d.ts.map +1 -1
  47. package/dist/types/algorithms/aes-gcm.d.ts +2 -2
  48. package/dist/types/algorithms/aes-gcm.d.ts.map +1 -1
  49. package/dist/types/algorithms/ecdsa.d.ts +1 -1
  50. package/dist/types/algorithms/ecdsa.d.ts.map +1 -1
  51. package/dist/types/algorithms/eddsa.d.ts +2 -2
  52. package/dist/types/algorithms/eddsa.d.ts.map +1 -1
  53. package/dist/types/algorithms/sha-2.d.ts +1 -1
  54. package/dist/types/algorithms/sha-2.d.ts.map +1 -1
  55. package/dist/types/crypto-error.d.ts +29 -0
  56. package/dist/types/crypto-error.d.ts.map +1 -0
  57. package/dist/types/index.d.ts +3 -0
  58. package/dist/types/index.d.ts.map +1 -1
  59. package/dist/types/jose/jwk.d.ts.map +1 -1
  60. package/dist/types/local-key-manager.d.ts +3 -3
  61. package/dist/types/local-key-manager.d.ts.map +1 -1
  62. package/dist/types/primitives/aes-kw.d.ts +103 -0
  63. package/dist/types/primitives/aes-kw.d.ts.map +1 -0
  64. package/dist/types/primitives/concat-kdf.d.ts +1 -1
  65. package/dist/types/primitives/concat-kdf.d.ts.map +1 -1
  66. package/dist/types/primitives/hkdf.d.ts +90 -0
  67. package/dist/types/primitives/hkdf.d.ts.map +1 -0
  68. package/dist/types/primitives/pbkdf2.d.ts +58 -0
  69. package/dist/types/primitives/pbkdf2.d.ts.map +1 -1
  70. package/dist/types/primitives/xchacha20-poly1305.d.ts +47 -0
  71. package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -1
  72. package/dist/types/types/cipher.d.ts +1 -1
  73. package/dist/types/types/crypto-api.d.ts +3 -3
  74. package/dist/types/types/crypto-api.d.ts.map +1 -1
  75. package/dist/types/types/params-direct.d.ts +79 -1
  76. package/dist/types/types/params-direct.d.ts.map +1 -1
  77. package/dist/utils.js.map +2 -2
  78. package/package.json +26 -39
  79. package/src/algorithms/aes-ctr.ts +1 -1
  80. package/src/algorithms/aes-gcm.ts +3 -2
  81. package/src/algorithms/ecdsa.ts +13 -7
  82. package/src/algorithms/eddsa.ts +9 -9
  83. package/src/algorithms/sha-2.ts +2 -2
  84. package/src/crypto-error.ts +45 -0
  85. package/src/index.ts +3 -0
  86. package/src/jose/jwk.ts +32 -32
  87. package/src/local-key-manager.ts +14 -13
  88. package/src/primitives/aes-ctr.ts +1 -1
  89. package/src/primitives/aes-gcm.ts +5 -5
  90. package/src/primitives/aes-kw.ts +269 -0
  91. package/src/primitives/concat-kdf.ts +4 -2
  92. package/src/primitives/ed25519.ts +6 -6
  93. package/src/primitives/hkdf.ts +121 -0
  94. package/src/primitives/pbkdf2.ts +91 -0
  95. package/src/primitives/secp256k1.ts +6 -6
  96. package/src/primitives/secp256r1.ts +6 -6
  97. package/src/primitives/x25519.ts +3 -3
  98. package/src/primitives/xchacha20-poly1305.ts +57 -4
  99. package/src/primitives/xchacha20.ts +1 -1
  100. package/src/types/cipher.ts +1 -1
  101. package/src/types/crypto-api.ts +5 -5
  102. package/src/types/params-direct.ts +97 -1
  103. package/src/utils.ts +2 -2
  104. package/dist/cjs/algorithms/aes-ctr.js +0 -188
  105. package/dist/cjs/algorithms/aes-ctr.js.map +0 -1
  106. package/dist/cjs/algorithms/aes-gcm.js +0 -196
  107. package/dist/cjs/algorithms/aes-gcm.js.map +0 -1
  108. package/dist/cjs/algorithms/crypto-algorithm.js +0 -13
  109. package/dist/cjs/algorithms/crypto-algorithm.js.map +0 -1
  110. package/dist/cjs/algorithms/ecdsa.js +0 -352
  111. package/dist/cjs/algorithms/ecdsa.js.map +0 -1
  112. package/dist/cjs/algorithms/eddsa.js +0 -325
  113. package/dist/cjs/algorithms/eddsa.js.map +0 -1
  114. package/dist/cjs/algorithms/sha-2.js +0 -119
  115. package/dist/cjs/algorithms/sha-2.js.map +0 -1
  116. package/dist/cjs/index.js +0 -41
  117. package/dist/cjs/index.js.map +0 -1
  118. package/dist/cjs/jose/jwe.js +0 -3
  119. package/dist/cjs/jose/jwe.js.map +0 -1
  120. package/dist/cjs/jose/jwk.js +0 -278
  121. package/dist/cjs/jose/jwk.js.map +0 -1
  122. package/dist/cjs/jose/jws.js +0 -3
  123. package/dist/cjs/jose/jws.js.map +0 -1
  124. package/dist/cjs/jose/jwt.js +0 -3
  125. package/dist/cjs/jose/jwt.js.map +0 -1
  126. package/dist/cjs/jose/utils.js +0 -60
  127. package/dist/cjs/jose/utils.js.map +0 -1
  128. package/dist/cjs/local-key-manager.js +0 -521
  129. package/dist/cjs/local-key-manager.js.map +0 -1
  130. package/dist/cjs/package.json +0 -1
  131. package/dist/cjs/primitives/aes-ctr.js +0 -398
  132. package/dist/cjs/primitives/aes-ctr.js.map +0 -1
  133. package/dist/cjs/primitives/aes-gcm.js +0 -425
  134. package/dist/cjs/primitives/aes-gcm.js.map +0 -1
  135. package/dist/cjs/primitives/concat-kdf.js +0 -215
  136. package/dist/cjs/primitives/concat-kdf.js.map +0 -1
  137. package/dist/cjs/primitives/ed25519.js +0 -651
  138. package/dist/cjs/primitives/ed25519.js.map +0 -1
  139. package/dist/cjs/primitives/pbkdf2.js +0 -120
  140. package/dist/cjs/primitives/pbkdf2.js.map +0 -1
  141. package/dist/cjs/primitives/secp256k1.js +0 -958
  142. package/dist/cjs/primitives/secp256k1.js.map +0 -1
  143. package/dist/cjs/primitives/secp256r1.js +0 -959
  144. package/dist/cjs/primitives/secp256r1.js.map +0 -1
  145. package/dist/cjs/primitives/sha256.js +0 -93
  146. package/dist/cjs/primitives/sha256.js.map +0 -1
  147. package/dist/cjs/primitives/x25519.js +0 -498
  148. package/dist/cjs/primitives/x25519.js.map +0 -1
  149. package/dist/cjs/primitives/xchacha20-poly1305.js +0 -340
  150. package/dist/cjs/primitives/xchacha20-poly1305.js.map +0 -1
  151. package/dist/cjs/primitives/xchacha20.js +0 -316
  152. package/dist/cjs/primitives/xchacha20.js.map +0 -1
  153. package/dist/cjs/types/cipher.js +0 -3
  154. package/dist/cjs/types/cipher.js.map +0 -1
  155. package/dist/cjs/types/crypto-api.js +0 -3
  156. package/dist/cjs/types/crypto-api.js.map +0 -1
  157. package/dist/cjs/types/hasher.js +0 -3
  158. package/dist/cjs/types/hasher.js.map +0 -1
  159. package/dist/cjs/types/identifier.js +0 -3
  160. package/dist/cjs/types/identifier.js.map +0 -1
  161. package/dist/cjs/types/key-compressor.js +0 -3
  162. package/dist/cjs/types/key-compressor.js.map +0 -1
  163. package/dist/cjs/types/key-converter.js +0 -3
  164. package/dist/cjs/types/key-converter.js.map +0 -1
  165. package/dist/cjs/types/key-deriver.js +0 -3
  166. package/dist/cjs/types/key-deriver.js.map +0 -1
  167. package/dist/cjs/types/key-generator.js +0 -3
  168. package/dist/cjs/types/key-generator.js.map +0 -1
  169. package/dist/cjs/types/key-io.js +0 -3
  170. package/dist/cjs/types/key-io.js.map +0 -1
  171. package/dist/cjs/types/key-wrapper.js +0 -3
  172. package/dist/cjs/types/key-wrapper.js.map +0 -1
  173. package/dist/cjs/types/params-direct.js +0 -3
  174. package/dist/cjs/types/params-direct.js.map +0 -1
  175. package/dist/cjs/types/params-enclosed.js +0 -3
  176. package/dist/cjs/types/params-enclosed.js.map +0 -1
  177. package/dist/cjs/types/params-kms.js +0 -3
  178. package/dist/cjs/types/params-kms.js.map +0 -1
  179. package/dist/cjs/types/signer.js +0 -3
  180. package/dist/cjs/types/signer.js.map +0 -1
  181. package/dist/cjs/utils.js +0 -173
  182. package/dist/cjs/utils.js.map +0 -1
@@ -1,398 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- var __rest = (this && this.__rest) || function (s, e) {
39
- var t = {};
40
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
41
- t[p] = s[p];
42
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
43
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
44
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
45
- t[p[i]] = s[p[i]];
46
- }
47
- return t;
48
- };
49
- Object.defineProperty(exports, "__esModule", { value: true });
50
- exports.AesCtr = void 0;
51
- var common_1 = require("@enbox/common");
52
- var crypto_1 = require("@noble/ciphers/crypto");
53
- var jwk_js_1 = require("../jose/jwk.js");
54
- /**
55
- * Constant defining the AES block size in bits.
56
- *
57
- * @remarks
58
- * In AES Counter (CTR) mode, the counter length must match the block size of the AES algorithm,
59
- * which is 128 bits. NIST publication 800-38A, which provides guidelines for block cipher modes of
60
- * operation, specifies this requirement. Maintaining a counter length of 128 bits is essential for
61
- * the correct operation and security of AES-CTR.
62
- *
63
- * This implementation does not support counter lengths that are different from the value defined by
64
- * this constant.
65
- *
66
- * @see {@link https://doi.org/10.6028/NIST.SP.800-38A | NIST SP 800-38A}
67
- */
68
- var AES_BLOCK_SIZE = 128;
69
- /**
70
- * Constant defining the AES key length values in bits.
71
- *
72
- * @remarks
73
- * NIST publication FIPS 197 states:
74
- * > The AES algorithm is capable of using cryptographic keys of 128, 192, and 256 bits to encrypt
75
- * > and decrypt data in blocks of 128 bits.
76
- *
77
- * This implementation does not support key lengths that are different from the three values
78
- * defined by this constant.
79
- *
80
- * @see {@link https://doi.org/10.6028/NIST.FIPS.197-upd1 | NIST FIPS 197}
81
- */
82
- var AES_KEY_LENGTHS = [128, 192, 256];
83
- /**
84
- * Constant defining the maximum length of the counter in bits.
85
- *
86
- * @remarks
87
- * The rightmost bits of the counter block are used as the actual counter value, while the leftmost
88
- * bits are used as the nonce. The maximum length of the counter is 128 bits, which is the same as
89
- * the AES block size.
90
- */
91
- var COUNTER_MAX_LENGTH = AES_BLOCK_SIZE;
92
- /**
93
- * The `AesCtr` class provides a comprehensive set of utilities for cryptographic operations
94
- * using the Advanced Encryption Standard (AES) in Counter (CTR) mode. This class includes
95
- * methods for key generation, encryption, decryption, and conversions between raw byte arrays
96
- * and JSON Web Key (JWK) formats. It is designed to support AES-CTR, a symmetric key algorithm
97
- * that is widely used in various cryptographic applications for its efficiency and security.
98
- *
99
- * AES-CTR mode operates as a stream cipher using a block cipher (AES) and is well-suited for
100
- * scenarios where parallel processing is beneficial or where the same key is required to
101
- * encrypt multiple data blocks. The class adheres to standard cryptographic practices, ensuring
102
- * compatibility and security in its implementations.
103
- *
104
- * Key Features:
105
- * - Key Generation: Generate AES symmetric keys in JWK format.
106
- * - Key Conversion: Transform keys between raw byte arrays and JWK formats.
107
- * - Encryption: Encrypt data using AES-CTR with the provided symmetric key.
108
- * - Decryption: Decrypt data encrypted with AES-CTR using the corresponding symmetric key.
109
- *
110
- * The methods in this class are asynchronous, returning Promises to accommodate various
111
- * JavaScript environments.
112
- *
113
- * @example
114
- * ```ts
115
- * // Key Generation
116
- * const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
117
- * const privateKey = await AesCtr.generateKey({ length });
118
- *
119
- * // Encryption
120
- * const data = new TextEncoder().encode('Messsage');
121
- * const counter = new Uint8Array(16); // 16-byte (128-bit) counter block
122
- * const encryptedData = await AesCtr.encrypt({
123
- * data,
124
- * counter,
125
- * key: privateKey,
126
- * length: 64 // Length of the counter in bits
127
- * });
128
- *
129
- * // Decryption
130
- * const decryptedData = await AesCtr.decrypt({
131
- * data: encryptedData,
132
- * counter,
133
- * key: privateKey,
134
- * length: 64 // Length of the counter in bits
135
- * });
136
- *
137
- * // Key Conversion
138
- * const privateKeyBytes = await AesCtr.privateKeyToBytes({ privateKey });
139
- * ```
140
- */
141
- var AesCtr = /** @class */ (function () {
142
- function AesCtr() {
143
- }
144
- /**
145
- * Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
146
- *
147
- * @remarks
148
- * This method takes a symmetric key represented as a byte array (Uint8Array) and
149
- * converts it into a JWK object for use with AES (Advanced Encryption Standard)
150
- * in Counter (CTR) mode. The conversion process involves encoding the key into
151
- * base64url format and setting the appropriate JWK parameters.
152
- *
153
- * The resulting JWK object includes the following properties:
154
- * - `kty`: Key Type, set to 'oct' for Octet Sequence (representing a symmetric key).
155
- * - `k`: The symmetric key, base64url-encoded.
156
- * - `kid`: Key ID, generated based on the JWK thumbprint.
157
- *
158
- * @example
159
- * ```ts
160
- * const privateKeyBytes = new Uint8Array([...]); // Replace with actual symmetric key bytes
161
- * const privateKey = await AesCtr.bytesToPrivateKey({ privateKeyBytes });
162
- * ```
163
- *
164
- * @param params - The parameters for the symmetric key conversion.
165
- * @param params.privateKeyBytes - The raw symmetric key as a Uint8Array.
166
- *
167
- * @returns A Promise that resolves to the symmetric key in JWK format.
168
- */
169
- AesCtr.bytesToPrivateKey = function (_a) {
170
- return __awaiter(this, arguments, void 0, function (_b) {
171
- var privateKey, _c;
172
- var privateKeyBytes = _b.privateKeyBytes;
173
- return __generator(this, function (_d) {
174
- switch (_d.label) {
175
- case 0:
176
- privateKey = {
177
- k: common_1.Convert.uint8Array(privateKeyBytes).toBase64Url(),
178
- kty: 'oct'
179
- };
180
- // Compute the JWK thumbprint and set as the key ID.
181
- _c = privateKey;
182
- return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: privateKey })];
183
- case 1:
184
- // Compute the JWK thumbprint and set as the key ID.
185
- _c.kid = _d.sent();
186
- return [2 /*return*/, privateKey];
187
- }
188
- });
189
- });
190
- };
191
- /**
192
- * Decrypts the provided data using AES in Counter (CTR) mode.
193
- *
194
- * @remarks
195
- * This method performs AES-CTR decryption on the given encrypted data using the specified key.
196
- * Similar to the encryption process, it requires an initial counter block and the length
197
- * of the counter block, along with the encrypted data and the decryption key. The method
198
- * returns the decrypted data as a Uint8Array.
199
- *
200
- * @example
201
- * ```ts
202
- * const encryptedData = new Uint8Array([...]); // Encrypted data
203
- * const counter = new Uint8Array(16); // 16-byte (128-bit) counter block used during encryption
204
- * const key = { ... }; // A Jwk object representing the same AES key used for encryption
205
- * const decryptedData = await AesCtr.decrypt({
206
- * data: encryptedData,
207
- * counter,
208
- * key,
209
- * length: 64 // Length of the counter in bits
210
- * });
211
- * ```
212
- *
213
- * @param params - The parameters for the decryption operation.
214
- * @param params.key - The key to use for decryption, represented in JWK format.
215
- * @param params.data - The encrypted data to decrypt, as a Uint8Array.
216
- * @param params.counter - The initial value of the counter block.
217
- * @param params.length - The number of bits in the counter block that are used for the actual counter.
218
- *
219
- * @returns A Promise that resolves to the decrypted data as a Uint8Array.
220
- */
221
- AesCtr.decrypt = function (_a) {
222
- return __awaiter(this, arguments, void 0, function (_b) {
223
- var webCrypto, webCryptoKey, plaintextBuffer, plaintext;
224
- var key = _b.key, data = _b.data, counter = _b.counter, length = _b.length;
225
- return __generator(this, function (_c) {
226
- switch (_c.label) {
227
- case 0:
228
- // Validate the initial counter block length matches the AES block size.
229
- if (counter.byteLength !== AES_BLOCK_SIZE / 8) {
230
- throw new TypeError("The counter must be ".concat(AES_BLOCK_SIZE, " bits in length"));
231
- }
232
- // Validate the length of the counter.
233
- if (length === 0 || length > COUNTER_MAX_LENGTH) {
234
- throw new TypeError("The 'length' property must be in the range 1 to ".concat(COUNTER_MAX_LENGTH));
235
- }
236
- webCrypto = (0, crypto_1.getWebcryptoSubtle)();
237
- return [4 /*yield*/, webCrypto.importKey('jwk', key, { name: 'AES-CTR' }, true, ['decrypt'])];
238
- case 1:
239
- webCryptoKey = _c.sent();
240
- return [4 /*yield*/, webCrypto.decrypt({ name: 'AES-CTR', counter: counter, length: length }, webCryptoKey, data)];
241
- case 2:
242
- plaintextBuffer = _c.sent();
243
- plaintext = new Uint8Array(plaintextBuffer);
244
- return [2 /*return*/, plaintext];
245
- }
246
- });
247
- });
248
- };
249
- /**
250
- * Encrypts the provided data using AES in Counter (CTR) mode.
251
- *
252
- * @remarks
253
- * This method performs AES-CTR encryption on the given data using the specified key.
254
- * It requires the initial counter block and the length of the counter block, alongside
255
- * the data and key. The method is designed to work asynchronously and returns the
256
- * encrypted data as a Uint8Array.
257
- *
258
- * @example
259
- * ```ts
260
- * const data = new TextEncoder().encode('Messsage');
261
- * const counter = new Uint8Array(16); // 16-byte (128-bit) counter block
262
- * const key = { ... }; // A Jwk object representing an AES key
263
- * const encryptedData = await AesCtr.encrypt({
264
- * data,
265
- * counter,
266
- * key,
267
- * length: 64 // Length of the counter in bits
268
- * });
269
- * ```
270
- *
271
- * @param params - The parameters for the encryption operation.
272
- * @param params.key - The key to use for encryption, represented in JWK format.
273
- * @param params.data - The data to encrypt, represented as a Uint8Array.
274
- * @param params.counter - The initial value of the counter block.
275
- * @param params.length - The number of bits in the counter block that are used for the actual counter.
276
- *
277
- * @returns A Promise that resolves to the encrypted data as a Uint8Array.
278
- */
279
- AesCtr.encrypt = function (_a) {
280
- return __awaiter(this, arguments, void 0, function (_b) {
281
- var webCrypto, webCryptoKey, ciphertextBuffer, ciphertext;
282
- var key = _b.key, data = _b.data, counter = _b.counter, length = _b.length;
283
- return __generator(this, function (_c) {
284
- switch (_c.label) {
285
- case 0:
286
- // Validate the initial counter block value length.
287
- if (counter.byteLength !== AES_BLOCK_SIZE / 8) {
288
- throw new TypeError("The counter must be ".concat(AES_BLOCK_SIZE, " bits in length"));
289
- }
290
- // Validate the length of the counter.
291
- if (length === 0 || length > COUNTER_MAX_LENGTH) {
292
- throw new TypeError("The 'length' property must be in the range 1 to ".concat(COUNTER_MAX_LENGTH));
293
- }
294
- webCrypto = (0, crypto_1.getWebcryptoSubtle)();
295
- return [4 /*yield*/, webCrypto.importKey('jwk', key, { name: 'AES-CTR' }, true, ['encrypt', 'decrypt'])];
296
- case 1:
297
- webCryptoKey = _c.sent();
298
- return [4 /*yield*/, webCrypto.encrypt({ name: 'AES-CTR', counter: counter, length: length }, webCryptoKey, data)];
299
- case 2:
300
- ciphertextBuffer = _c.sent();
301
- ciphertext = new Uint8Array(ciphertextBuffer);
302
- return [2 /*return*/, ciphertext];
303
- }
304
- });
305
- });
306
- };
307
- /**
308
- * Generates a symmetric key for AES in Counter (CTR) mode in JSON Web Key (JWK) format.
309
- *
310
- * @remarks
311
- * This method creates a new symmetric key of a specified length suitable for use with
312
- * AES-CTR encryption. It uses cryptographically secure random number generation to
313
- * ensure the uniqueness and security of the key. The generated key adheres to the JWK
314
- * format, making it compatible with common cryptographic standards and easy to use in
315
- * various cryptographic processes.
316
- *
317
- * The generated key includes the following components:
318
- * - `kty`: Key Type, set to 'oct' for Octet Sequence.
319
- * - `k`: The symmetric key component, base64url-encoded.
320
- * - `kid`: Key ID, generated based on the JWK thumbprint.
321
- *
322
- * @example
323
- * ```ts
324
- * const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
325
- * const privateKey = await AesCtr.generateKey({ length });
326
- * ```
327
- *
328
- * @param params - The parameters for the key generation.
329
- * @param params.length - The length of the key in bits. Common lengths are 128, 192, and 256 bits.
330
- *
331
- * @returns A Promise that resolves to the generated symmetric key in JWK format.
332
- */
333
- AesCtr.generateKey = function (_a) {
334
- return __awaiter(this, arguments, void 0, function (_b) {
335
- var webCrypto, webCryptoKey, _c, ext, key_ops, privateKey, _d;
336
- var length = _b.length;
337
- return __generator(this, function (_e) {
338
- switch (_e.label) {
339
- case 0:
340
- // Validate the key length.
341
- if (!AES_KEY_LENGTHS.includes(length)) {
342
- throw new RangeError("The key length is invalid: Must be ".concat(AES_KEY_LENGTHS.join(', '), " bits"));
343
- }
344
- webCrypto = (0, crypto_1.getWebcryptoSubtle)();
345
- return [4 /*yield*/, webCrypto.generateKey({ name: 'AES-CTR', length: length }, true, ['encrypt'])];
346
- case 1:
347
- webCryptoKey = _e.sent();
348
- return [4 /*yield*/, webCrypto.exportKey('jwk', webCryptoKey)];
349
- case 2:
350
- _c = _e.sent(), ext = _c.ext, key_ops = _c.key_ops, privateKey = __rest(_c, ["ext", "key_ops"]);
351
- // Compute the JWK thumbprint and set as the key ID.
352
- _d = privateKey;
353
- return [4 /*yield*/, (0, jwk_js_1.computeJwkThumbprint)({ jwk: privateKey })];
354
- case 3:
355
- // Compute the JWK thumbprint and set as the key ID.
356
- _d.kid = _e.sent();
357
- return [2 /*return*/, privateKey];
358
- }
359
- });
360
- });
361
- };
362
- /**
363
- * Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
364
- *
365
- * @remarks
366
- * This method takes a symmetric key in JWK format and extracts its raw byte representation.
367
- * It decodes the 'k' parameter of the JWK value, which represents the symmetric key in base64url
368
- * encoding, into a byte array.
369
- *
370
- * @example
371
- * ```ts
372
- * const privateKey = { ... }; // A symmetric key in JWK format
373
- * const privateKeyBytes = await AesCtr.privateKeyToBytes({ privateKey });
374
- * ```
375
- *
376
- * @param params - The parameters for the symmetric key conversion.
377
- * @param params.privateKey - The symmetric key in JWK format.
378
- *
379
- * @returns A Promise that resolves to the symmetric key as a Uint8Array.
380
- */
381
- AesCtr.privateKeyToBytes = function (_a) {
382
- return __awaiter(this, arguments, void 0, function (_b) {
383
- var privateKeyBytes;
384
- var privateKey = _b.privateKey;
385
- return __generator(this, function (_c) {
386
- // Verify the provided JWK represents a valid oct private key.
387
- if (!(0, jwk_js_1.isOctPrivateJwk)(privateKey)) {
388
- throw new Error("AesCtr: The provided key is not a valid oct private key.");
389
- }
390
- privateKeyBytes = common_1.Convert.base64Url(privateKey.k).toUint8Array();
391
- return [2 /*return*/, privateKeyBytes];
392
- });
393
- });
394
- };
395
- return AesCtr;
396
- }());
397
- exports.AesCtr = AesCtr;
398
- //# sourceMappingURL=aes-ctr.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":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