@enbox/crypto 0.0.1

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