@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,245 @@
1
+ import type { Jwk } from '../jose/jwk.js';
2
+ /**
3
+ * Constant defining the AES key length values in bits.
4
+ *
5
+ * @remarks
6
+ * NIST publication FIPS 197 states:
7
+ * > The AES algorithm is capable of using cryptographic keys of 128, 192, and 256 bits to encrypt
8
+ * > and decrypt data in blocks of 128 bits.
9
+ *
10
+ * This implementation does not support key lengths that are different from the three values
11
+ * defined by this constant.
12
+ *
13
+ * @see {@link https://doi.org/10.6028/NIST.FIPS.197-upd1 | NIST FIPS 197}
14
+ */
15
+ declare const AES_KEY_LENGTHS: readonly [128, 192, 256];
16
+ /**
17
+ * Constant defining the AES-GCM tag length values in bits.
18
+ *
19
+ * @remarks
20
+ * NIST Special Publication 800-38D, Section 5.2.1.2 states that the tag length:
21
+ * > may be any one of the following five values: 128, 120, 112, 104, or 96
22
+ *
23
+ * Although the NIST specification allows for tag lengths of 32 or 64 bits in certain applications,
24
+ * the use of shorter tag lengths can be problematic for GCM due to targeted forgery attacks. As a
25
+ * precaution, this implementation does not support tag lengths that are different from the five
26
+ * values defined by this constant. See Appendix C of the NIST SP 800-38D specification for
27
+ * additional guidance and details.
28
+ *
29
+ * @see {@link https://doi.org/10.6028/NIST.SP.800-38D | NIST SP 800-38D}
30
+ */
31
+ export declare const AES_GCM_TAG_LENGTHS: readonly [96, 104, 112, 120, 128];
32
+ /**
33
+ * The `AesGcm` class provides a comprehensive set of utilities for cryptographic operations
34
+ * using the Advanced Encryption Standard (AES) in Galois/Counter Mode (GCM). This class includes
35
+ * methods for key generation, encryption, decryption, and conversions between raw byte arrays
36
+ * and JSON Web Key (JWK) formats. It is designed to support AES-GCM, a symmetric key algorithm
37
+ * that is widely used for its efficiency, security, and provision of authenticated encryption.
38
+ *
39
+ * AES-GCM is particularly favored for scenarios that require both confidentiality and integrity
40
+ * of data. It integrates the counter mode of encryption with the Galois mode of authentication,
41
+ * offering high performance and parallel processing capabilities.
42
+ *
43
+ * Key Features:
44
+ * - Key Generation: Generate AES symmetric keys in JWK format.
45
+ * - Key Conversion: Transform keys between raw byte arrays and JWK formats.
46
+ * - Encryption: Encrypt data using AES-GCM with the provided symmetric key.
47
+ * - Decryption: Decrypt data encrypted with AES-GCM using the corresponding symmetric key.
48
+ *
49
+ * The methods in this class are asynchronous, returning Promises to accommodate various
50
+ * JavaScript environments.
51
+ *
52
+ * @example
53
+ * ```ts
54
+ * // Key Generation
55
+ * const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
56
+ * const privateKey = await AesGcm.generateKey({ length });
57
+ *
58
+ * // Encryption
59
+ * const data = new TextEncoder().encode('Messsage');
60
+ * const iv = new Uint8Array(12); // 12-byte initialization vector
61
+ * const encryptedData = await AesGcm.encrypt({
62
+ * data,
63
+ * iv,
64
+ * key: privateKey
65
+ * });
66
+ *
67
+ * // Decryption
68
+ * const decryptedData = await AesGcm.decrypt({
69
+ * data: encryptedData,
70
+ * iv,
71
+ * key: privateKey
72
+ * });
73
+ *
74
+ * // Key Conversion
75
+ * const privateKeyBytes = await AesGcm.privateKeyToBytes({ privateKey });
76
+ * ```
77
+ */
78
+ export declare class AesGcm {
79
+ /**
80
+ * Converts a raw private key in bytes to its corresponding JSON Web Key (JWK) format.
81
+ *
82
+ * @remarks
83
+ * This method accepts a symmetric key represented as a byte array (Uint8Array) and
84
+ * converts it into a JWK object for use with AES-GCM (Advanced Encryption Standard -
85
+ * Galois/Counter Mode). The conversion process involves encoding the key into
86
+ * base64url format and setting the appropriate JWK parameters.
87
+ *
88
+ * The resulting JWK object includes the following properties:
89
+ * - `kty`: Key Type, set to 'oct' for Octet Sequence (representing a symmetric key).
90
+ * - `k`: The symmetric key, base64url-encoded.
91
+ * - `kid`: Key ID, generated based on the JWK thumbprint.
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * const privateKeyBytes = new Uint8Array([...]); // Replace with actual symmetric key bytes
96
+ * const privateKey = await AesGcm.bytesToPrivateKey({ privateKeyBytes });
97
+ * ```
98
+ *
99
+ * @param params - The parameters for the symmetric key conversion.
100
+ * @param params.privateKeyBytes - The raw symmetric key as a Uint8Array.
101
+ *
102
+ * @returns A Promise that resolves to the symmetric key in JWK format.
103
+ */
104
+ static bytesToPrivateKey({ privateKeyBytes }: {
105
+ privateKeyBytes: Uint8Array;
106
+ }): Promise<Jwk>;
107
+ /**
108
+ * Decrypts the provided data using AES-GCM.
109
+ *
110
+ * @remarks
111
+ * This method performs AES-GCM decryption on the given encrypted data using the specified key.
112
+ * It requires an initialization vector (IV), the encrypted data along with the decryption key,
113
+ * and optionally, additional authenticated data (AAD). The method returns the decrypted data as a
114
+ * Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
115
+ * tag used when encrypting the data. If not specified, the default tag length of 128 bits is
116
+ * used.
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * const encryptedData = new Uint8Array([...]); // Encrypted data
121
+ * const iv = new Uint8Array([...]); // Initialization vector used during encryption
122
+ * const additionalData = new Uint8Array([...]); // Optional additional authenticated data
123
+ * const key = { ... }; // A Jwk object representing the AES key
124
+ * const decryptedData = await AesGcm.decrypt({
125
+ * data: encryptedData,
126
+ * iv,
127
+ * additionalData,
128
+ * key,
129
+ * tagLength: 128 // Optional tag length in bits
130
+ * });
131
+ * ```
132
+ *
133
+ * @param params - The parameters for the decryption operation.
134
+ * @param params.key - The key to use for decryption, represented in JWK format.
135
+ * @param params.data - The encrypted data to decrypt, represented as a Uint8Array.
136
+ * @param params.iv - The initialization vector, represented as a Uint8Array.
137
+ * @param params.additionalData - Optional additional authenticated data. Optional.
138
+ * @param params.tagLength - The length of the authentication tag in bits. Optional.
139
+ *
140
+ * @returns A Promise that resolves to the decrypted data as a Uint8Array.
141
+ */
142
+ static decrypt({ key, data, iv, additionalData, tagLength }: {
143
+ key: Jwk;
144
+ data: Uint8Array;
145
+ iv: Uint8Array;
146
+ additionalData?: Uint8Array;
147
+ tagLength?: typeof AES_GCM_TAG_LENGTHS[number];
148
+ }): Promise<Uint8Array>;
149
+ /**
150
+ * Encrypts the provided data using AES-GCM.
151
+ *
152
+ * @remarks
153
+ * This method performs AES-GCM encryption on the given data using the specified key.
154
+ * It requires an initialization vector (IV), the encrypted data along with the decryption key,
155
+ * and optionally, additional authenticated data (AAD). The method returns the encrypted data as a
156
+ * Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
157
+ * tag generated in the encryption operation and used for authentication in the corresponding
158
+ * decryption. If not specified, the default tag length of 128 bits is used.
159
+ *
160
+ * @example
161
+ * ```ts
162
+ * const data = new TextEncoder().encode('Messsage');
163
+ * const iv = new Uint8Array([...]); // Initialization vector
164
+ * const additionalData = new Uint8Array([...]); // Optional additional authenticated data
165
+ * const key = { ... }; // A Jwk object representing an AES key
166
+ * const encryptedData = await AesGcm.encrypt({
167
+ * data,
168
+ * iv,
169
+ * additionalData,
170
+ * key,
171
+ * tagLength: 128 // Optional tag length in bits
172
+ * });
173
+ * ```
174
+ *
175
+ * @param params - The parameters for the encryption operation.
176
+ * @param params.key - The key to use for encryption, represented in JWK format.
177
+ * @param params.data - The data to encrypt, represented as a Uint8Array.
178
+ * @param params.iv - The initialization vector, represented as a Uint8Array.
179
+ * @param params.additionalData - Optional additional authenticated data. Optional.
180
+ * @param params.tagLength - The length of the authentication tag in bits. Optional.
181
+ *
182
+ * @returns A Promise that resolves to the encrypted data as a Uint8Array.
183
+ */
184
+ static encrypt({ data, iv, key, additionalData, tagLength }: {
185
+ key: Jwk;
186
+ data: Uint8Array;
187
+ iv: Uint8Array;
188
+ additionalData?: Uint8Array;
189
+ tagLength?: typeof AES_GCM_TAG_LENGTHS[number];
190
+ }): Promise<Uint8Array>;
191
+ /**
192
+ * Generates a symmetric key for AES in Galois/Counter Mode (GCM) in JSON Web Key (JWK) format.
193
+ *
194
+ * @remarks
195
+ * This method creates a new symmetric key of a specified length suitable for use with
196
+ * AES-GCM encryption. It leverages cryptographically secure random number generation
197
+ * to ensure the uniqueness and security of the key. The generated key adheres to the JWK
198
+ * format, facilitating compatibility with common cryptographic standards and ease of use
199
+ * in various cryptographic applications.
200
+ *
201
+ * The generated key includes these components:
202
+ * - `kty`: Key Type, set to 'oct' for Octet Sequence, indicating a symmetric key.
203
+ * - `k`: The symmetric key component, base64url-encoded.
204
+ * - `kid`: Key ID, generated based on the JWK thumbprint, providing a unique identifier.
205
+ *
206
+ * @example
207
+ * ```ts
208
+ * const length = 256; // Length of the key in bits (e.g., 128, 192, 256)
209
+ * const privateKey = await AesGcm.generateKey({ length });
210
+ * ```
211
+ *
212
+ * @param params - The parameters for the key generation.
213
+ * @param params.length - The length of the key in bits. Common lengths are 128, 192, and 256 bits.
214
+ *
215
+ * @returns A Promise that resolves to the generated symmetric key in JWK format.
216
+ */
217
+ static generateKey({ length }: {
218
+ length: typeof AES_KEY_LENGTHS[number];
219
+ }): Promise<Jwk>;
220
+ /**
221
+ * Converts a private key from JSON Web Key (JWK) format to a raw byte array (Uint8Array).
222
+ *
223
+ * @remarks
224
+ * This method takes a symmetric key in JWK format and extracts its raw byte representation.
225
+ * It focuses on the 'k' parameter of the JWK, which represents the symmetric key component
226
+ * in base64url encoding. The method decodes this value into a byte array, providing
227
+ * the symmetric key in its raw binary form.
228
+ *
229
+ * @example
230
+ * ```ts
231
+ * const privateKey = { ... }; // A symmetric key in JWK format
232
+ * const privateKeyBytes = await AesGcm.privateKeyToBytes({ privateKey });
233
+ * ```
234
+ *
235
+ * @param params - The parameters for the symmetric key conversion.
236
+ * @param params.privateKey - The symmetric key in JWK format.
237
+ *
238
+ * @returns A Promise that resolves to the symmetric key as a Uint8Array.
239
+ */
240
+ static privateKeyToBytes({ privateKey }: {
241
+ privateKey: Jwk;
242
+ }): Promise<Uint8Array>;
243
+ }
244
+ export {};
245
+ //# sourceMappingURL=aes-gcm.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aes-gcm.d.ts","sourceRoot":"","sources":["../../../src/primitives/aes-gcm.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,gBAAgB,CAAC;AAmB1C;;;;;;;;;;;;GAYG;AACH,QAAA,MAAM,eAAe,0BAA2B,CAAC;AAEjD;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,mBAAmB,mCAAoC,CAAC;AAErE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,qBAAa,MAAM;IACjB;;;;;;;;;;;;;;;;;;;;;;;;KAwBC;WACmB,iBAAiB,CAAC,EAAE,eAAe,EAAE,EAAE;QACzD,eAAe,EAAE,UAAU,CAAC;KAC7B,GAAG,OAAO,CAAC,GAAG,CAAC;IAahB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAkCG;WACiB,OAAO,CAAC,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,cAAc,EAAE,SAAS,EAAE,EAAE;QACxE,GAAG,EAAE,GAAG,CAAC;QACT,IAAI,EAAE,UAAU,CAAC;QACjB,EAAE,EAAE,UAAU,CAAC;QACf,cAAc,CAAC,EAAE,UAAU,CAAC;QAC5B,SAAS,CAAC,EAAE,OAAO,mBAAmB,CAAC,MAAM,CAAC,CAAC;KAChD,GAAG,OAAO,CAAC,UAAU,CAAC;IAmCvB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAkCG;WACiB,OAAO,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,GAAG,EAAE,cAAc,EAAE,SAAS,EAAE,EAAE;QACxE,GAAG,EAAE,GAAG,CAAC;QACT,IAAI,EAAE,UAAU,CAAC;QACjB,EAAE,EAAE,UAAU,CAAC;QACf,cAAc,CAAC,EAAE,UAAU,CAAC;QAC5B,SAAS,CAAC,EAAE,OAAO,mBAAmB,CAAC,MAAM,CAAC,CAAC;KAChD,GAAG,OAAO,CAAC,UAAU,CAAC;IAmCvB;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;WACiB,WAAW,CAAC,EAAE,MAAM,EAAE,EAAE;QAC1C,MAAM,EAAE,OAAO,eAAe,CAAC,MAAM,CAAC,CAAC;KACxC,GAAG,OAAO,CAAC,GAAG,CAAC;IAuBhB;;;;;;;;;;;;;;;;;;;OAmBG;WACiB,iBAAiB,CAAC,EAAE,UAAU,EAAE,EAAE;QACpD,UAAU,EAAE,GAAG,CAAC;KACjB,GAAG,OAAO,CAAC,UAAU,CAAC;CAWxB"}
@@ -0,0 +1,160 @@
1
+ import { TypedArray } from '@noble/hashes/utils';
2
+ /**
3
+ * ConcatKDF FixedInfo Parameters.
4
+ *
5
+ * This implementation follows the recommended format for `FixedInfo` specified in section 5.8.2
6
+ * of the NIST.800-56A publication.
7
+ *
8
+ * @see {@link https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf | NIST.800-56A}
9
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7518#section-4.6.2 | RFC 7518, Section 4.6.2}
10
+ */
11
+ export type ConcatKdfFixedInfo = {
12
+ /**
13
+ * The algorithm the derived secret keying material will be used with.
14
+ */
15
+ algorithmId: string;
16
+ /**
17
+ * Information related to party U (initiator) involved in the key agreement
18
+ * transaction. It could be a public key, identifier, or any other data.
19
+ */
20
+ partyUInfo: string | TypedArray;
21
+ /**
22
+ * Information related to party V (receiver) involved in the key
23
+ * agreement transaction. Similar to partyUInfo, it could be a
24
+ * public key, identifier, etc.
25
+ */
26
+ partyVInfo: string | TypedArray;
27
+ /**
28
+ * Optional field. It is usually used to ensure the uniqueness of the
29
+ * derived keying material when the input keying material is used in
30
+ * multiple key-derivation key-agreement transactions. It is usually
31
+ * a public value such as the keyDataLen.
32
+ */
33
+ suppPubInfo?: number;
34
+ /**
35
+ * Optional field. It is used when it is desired to secretively
36
+ * bind additional information into the derived keying material.
37
+ * It is a secret value agreed upon by the entities who are party
38
+ * to the key agreement.
39
+ */
40
+ suppPrivInfo?: string | TypedArray;
41
+ };
42
+ /**
43
+ * An implementation of the Concatenation Key Derivation Function (ConcatKDF)
44
+ * as specified in NIST.800-56A, a single-step key-derivation function (SSKDF).
45
+ * ConcatKDF produces a derived key from a secret key (like a shared secret
46
+ * from ECDH), and other optional public information. This implementation
47
+ * specifically uses SHA-256 as the pseudorandom function (PRF).
48
+ *
49
+ * Note: This implementation allows for only a single round / repetition using the function
50
+ * `K(1) = H(counter || Z || FixedInfo)`, where:
51
+ * - `K(1)` is the derived key material after one round
52
+ * - `H` is the SHA-256 hashing function
53
+ * - `counter` is a 32-bit, big-endian bit string counter set to 0x00000001
54
+ * - `Z` is the shared secret value obtained from a key agreement protocol
55
+ * - `FixedInfo` is a bit string used to ensure that the derived keying material is adequately
56
+ * "bound" to the key-agreement transaction.
57
+ *
58
+ * @example
59
+ * ```ts
60
+ * // Key Derivation
61
+ * const derivedKeyingMaterial = await ConcatKdf.deriveKey({
62
+ * sharedSecret: utils.randomBytes(32),
63
+ * keyDataLen: 128,
64
+ * fixedInfo: {
65
+ * algorithmId: "A128GCM",
66
+ * partyUInfo: "Alice",
67
+ * partyVInfo: "Bob",
68
+ * suppPubInfo: 128,
69
+ * },
70
+ * });
71
+ * ```
72
+ *
73
+ * Additional Information:
74
+ *
75
+ * `Z`, or "shared secret":
76
+ * The shared secret value obtained from a key agreement protocol, such as
77
+ * Diffie-Hellman, ECDH (Elliptic Curve Diffie-Hellman). Importantly, this
78
+ * shared secret is not directly used as the encryption or authentication
79
+ * key, but as an input to a key derivation function (KDF), such as Concat
80
+ * KDF, to generate the actual key. This adds an extra layer of security, as
81
+ * even if the shared secret gets compromised, the actual encryption or
82
+ * authentication key stays safe. This shared secret `Z` value is kept
83
+ * confidential between the two parties in the key agreement protocol.
84
+ *
85
+ * @see {@link https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar3.pdf | NIST.800-56A}
86
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7518#section-4.6.2 | RFC 7518, Section 4.6.2}
87
+ */
88
+ export declare class ConcatKdf {
89
+ /**
90
+ * Derives a key of a specified length from the input parameters.
91
+ *
92
+ * @example
93
+ * ```ts
94
+ * // Key Derivation
95
+ * const derivedKeyingMaterial = await ConcatKdf.deriveKey({
96
+ * sharedSecret: utils.randomBytes(32),
97
+ * keyDataLen: 128,
98
+ * fixedInfo: {
99
+ * algorithmId: "A128GCM",
100
+ * partyUInfo: "Alice",
101
+ * partyVInfo: "Bob",
102
+ * suppPubInfo: 128,
103
+ * },
104
+ * });
105
+ * ```
106
+ *
107
+ * @param params - Input parameters for key derivation.
108
+ * @param params.keyDataLen - The desired length of the derived key in bits.
109
+ * @param params.sharedSecret - The shared secret key to derive from.
110
+ * @param params.fixedInfo - Additional public information to use in key derivation.
111
+ * @returns The derived key as a Uint8Array.
112
+ *
113
+ * @throws {Error} If the `keyDataLen` would require multiple rounds.
114
+ */
115
+ static deriveKey({ keyDataLen, fixedInfo, sharedSecret }: {
116
+ keyDataLen: number;
117
+ fixedInfo: ConcatKdfFixedInfo;
118
+ sharedSecret: Uint8Array;
119
+ }): Promise<Uint8Array>;
120
+ /**
121
+ * Computes the `FixedInfo` parameter for Concat KDF, which binds the derived key material to the
122
+ * context of the key agreement transaction.
123
+ *
124
+ * @remarks
125
+ * This implementation follows the recommended format for `FixedInfo` specified in section
126
+ * 5.8.1.2.1 of the NIST.800-56A publication.
127
+ *
128
+ * `FixedInfo` is a bit string equal to the following concatenation:
129
+ * `AlgorithmID || PartyUInfo || PartyVInfo {|| SuppPubInfo }{|| SuppPrivInfo }`.
130
+ *
131
+ * `SuppPubInfo` is the key length in bits, big endian encoded as a 32-bit number. For example,
132
+ * 128 would be [0, 0, 0, 128] and 256 would be [0, 0, 1, 0].
133
+ *
134
+ * @param params - Input data to construct FixedInfo.
135
+ * @returns FixedInfo as a Uint8Array.
136
+ */
137
+ private static computeFixedInfo;
138
+ /**
139
+ * Encodes input data as a length-prefixed byte string, or
140
+ * as a fixed-length bit string if specified.
141
+ *
142
+ * If variableLength = true, return the data in the form Datalen || Data,
143
+ * where Data is a variable-length string of zero or more (eight-bit)
144
+ * bytes, and Datalen is a fixed-length, big-endian counter that
145
+ * indicates the length (in bytes) of Data.
146
+ *
147
+ * If variableLength = false, return the data formatted as a
148
+ * fixed-length bit string.
149
+ *
150
+ * @param params - Input data and options for the conversion.
151
+ * @param params.data - The input data to encode. Must be a type convertible to Uint8Array by the Convert class.
152
+ * @param params.variableLength - Whether to output the data as variable length. Default is true.
153
+ *
154
+ * @returns The input data encoded as a Uint8Array.
155
+ *
156
+ * @throws {TypeError} If fixed-length data is not a number.
157
+ */
158
+ private static toDataLenData;
159
+ }
160
+ //# sourceMappingURL=concat-kdf.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"concat-kdf.d.ts","sourceRoot":"","sources":["../../../src/primitives/concat-kdf.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAe,MAAM,qBAAqB,CAAC;AAE9D;;;;;;;;GAQG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAC/B;;OAEG;IACH,WAAW,EAAE,MAAM,CAAC;IAEpB;;;OAGG;IACH,UAAU,EAAE,MAAM,GAAG,UAAU,CAAC;IAEhC;;;;OAIG;IACH,UAAU,EAAE,MAAM,GAAG,UAAU,CAAC;IAEhC;;;;;OAKG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;IAErB;;;;;OAKG;IACH,YAAY,CAAC,EAAE,MAAM,GAAG,UAAU,CAAC;CACpC,CAAA;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6CG;AACH,qBAAa,SAAS;IACpB;;;;;;;;;;;;;;;;;;;;;;;;;OAyBG;WACiB,SAAS,CAAC,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,EAAE;QACrE,UAAU,EAAE,MAAM,CAAC;QACnB,SAAS,EAAE,kBAAkB,CAAC;QAC9B,YAAY,EAAE,UAAU,CAAC;KAC1B,GAAG,OAAO,CAAC,UAAU,CAAC;IA4BvB;;;;;;;;;;;;;;;;OAgBG;IACH,OAAO,CAAC,MAAM,CAAC,gBAAgB;IAiB/B;;;;;;;;;;;;;;;;;;;OAmBG;IACH,OAAO,CAAC,MAAM,CAAC,aAAa;CA+B7B"}