@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,25 @@
1
+ /**
2
+ * `KeyCompressor` interface for converting public keys between compressed and uncompressed form.
3
+ */
4
+ export interface KeyCompressor {
5
+
6
+ /**
7
+ * Converts a public key to its compressed form.
8
+ *
9
+ * @param params - The parameters for the public key compression.
10
+ * @param params.publicKeyBytes - The public key as a Uint8Array.
11
+ *
12
+ * @returns A Promise that resolves to the compressed public key as a Uint8Array.
13
+ */
14
+ compressPublicKey(params: { publicKeyBytes: Uint8Array }): Promise<Uint8Array>;
15
+
16
+ /**
17
+ * Converts a public key to its uncompressed form.
18
+ *
19
+ * @param params - The parameters for the public key decompression.
20
+ * @param params.publicKeyBytes - The public key as a Uint8Array.
21
+ *
22
+ * @returns A Promise that resolves to the uncompressed public key as a Uint8Array.
23
+ */
24
+ decompressPublicKey(params: { publicKeyBytes: Uint8Array }): Promise<Uint8Array>;
25
+ }
@@ -0,0 +1,53 @@
1
+ import type { Jwk } from '../jose/jwk.js';
2
+
3
+ /**
4
+ * `KeyConverter` interface for converting private keys between byte array and JWK formats.
5
+ */
6
+ export interface KeyConverter {
7
+
8
+ /**
9
+ * Converts a private key from a byte array to JWK format.
10
+ *
11
+ * @param params - The parameters for the private key conversion.
12
+ * @param params.privateKeyBytes - The raw private key as a Uint8Array.
13
+ *
14
+ * @returns A Promise that resolves to the private key in JWK format.
15
+ */
16
+ bytesToPrivateKey(params: { privateKeyBytes: Uint8Array }): Promise<Jwk>;
17
+
18
+ /**
19
+ * Converts a private key from JWK format to a byte array.
20
+ *
21
+ * @param params - The parameters for the private key conversion.
22
+ * @param params.privateKey - The private key in JWK format.
23
+ *
24
+ * @returns A Promise that resolves to the private key as a Uint8Array.
25
+ */
26
+ privateKeyToBytes(params: { privateKey: Jwk }): Promise<Uint8Array>;
27
+ }
28
+
29
+ /**
30
+ * `AsymmetricKeyConverter` interface extends {@link KeyConverter |`KeyConverter`}, adding support
31
+ * for public key conversions.
32
+ */
33
+ export interface AsymmetricKeyConverter extends KeyConverter {
34
+ /**
35
+ * Converts a public key from a byte array to JWK format.
36
+ *
37
+ * @param params - The parameters for the public key conversion.
38
+ * @param params.publicKeyBytes - The raw public key as a Uint8Array.
39
+ *
40
+ * @returns A Promise that resolves to the public key in JWK format.
41
+ */
42
+ bytesToPublicKey(params: { publicKeyBytes: Uint8Array }): Promise<Jwk>;
43
+
44
+ /**
45
+ * Converts a public key from JWK format to a byte array.
46
+ *
47
+ * @param params - The parameters for the public key conversion.
48
+ * @param params.publicKey - The public key in JWK format.
49
+ *
50
+ * @returns A Promise that resolves to the public key as a Uint8Array.
51
+ */
52
+ publicKeyToBytes(params: { publicKey: Jwk }): Promise<Uint8Array>;
53
+ }
@@ -0,0 +1,43 @@
1
+ /**
2
+ * The `KeyDeriver` interface provides methods for key derivation. It includes the methods
3
+ * `deriveBits()` to derive cryptographic bits and `deriveKey()` to derive JWK keys from input data
4
+ * using specific algorithms. This interface is designed to support various key derivation
5
+ * algorithms, accommodating different input and output types.
6
+ *
7
+ * Both methods return a Promise that resolves to the derived cryptographic material.
8
+ */
9
+ export interface KeyDeriver<
10
+ DeriveBitsInput,
11
+ DeriveKeyInput,
12
+ DeriveKeyOutput
13
+ > {
14
+ /**
15
+ * Generates a specified number of cryptographic bits from given input parameters.
16
+ *
17
+ * @remarks
18
+ * The `deriveBits()` method of the {@link KeyDeriver | `KeyDeriver`} interface is used to create
19
+ * cryptographic material such as initialization vectors or keys from various sources. The method
20
+ * takes in parameters specific to the chosen key derivation algorithm and outputs a promise that
21
+ * resolves to a `Uint8Array` containing the derived bits.
22
+ *
23
+ * @param params - The parameters for the bit derivation process, specific to the chosen algorithm.
24
+ *
25
+ * @returns A Promise resolving to the derived bits as a `Uint8Array`.
26
+ */
27
+ deriveBits(params: DeriveBitsInput): Promise<Uint8Array>;
28
+
29
+ /**
30
+ * Derives a cryptographic key in JWK format based on the provided input parameters.
31
+ *
32
+ * @remarks
33
+ * The `deriveKey()` method of the {@link KeyDeriver | `KeyDeriver`} interface is utilized to
34
+ * generate cryptographic keys for operations like encryption, decryption, or signing. The method
35
+ * takes in parameters tailored to the key derivation algorithm being used and returns a promise
36
+ * that resolves to the derived key.
37
+ *
38
+ * @param params - The parameters for the key derivation process, customized for the specific algorithm.
39
+ *
40
+ * @returns A Promise resolving to the derived key in the specified output format.
41
+ */
42
+ deriveKey(params: DeriveKeyInput): Promise<DeriveKeyOutput>;
43
+ }
@@ -0,0 +1,119 @@
1
+ import type { Jwk } from '../jose/jwk.js';
2
+
3
+ /**
4
+ * The `KeyGenerator` interface provides a method for cryptographic key generation. It includes
5
+ * the `generateKey()` method to produce keys for cryptographic operations, supporting various
6
+ * algorithms and configurations. This interface is adaptable to different key generation
7
+ * requirements and can produce keys in formats such as JWK.
8
+ *
9
+ * The method returns a Promise that resolves to the generated key in the specified format.
10
+ */
11
+ export interface KeyGenerator<
12
+ GenerateKeyInput,
13
+ GenerateKeyOutput
14
+ > {
15
+ /**
16
+ * Generates a cryptographic key based on the provided parameters.
17
+ *
18
+ * @remarks
19
+ * The `generateKey()` method of the {@link KeyGenerator | `KeyGenerator`} interface generates
20
+ * private keys suitable for various cryptographic operations. This method can adapt to different
21
+ * key generation algorithms and input parameters.
22
+ *
23
+ * @param params - Optional parameters for the key generation process, specific to the chosen
24
+ * algorithm.
25
+ *
26
+ * @returns A Promise resolving to the generated private key in the specified output format.
27
+ */
28
+ generateKey(params?: GenerateKeyInput): Promise<GenerateKeyOutput>;
29
+ }
30
+
31
+ /**
32
+ * The `AsymmetricKeyGenerator` interface extends {@link KeyGenerator | `KeyGenerator`}, adding
33
+ * methods specific to asymmetric public keys. It supports generating asymmetric private keys and
34
+ * obtaining the public key from a private key.
35
+ *
36
+ * This interface is designed for asymmetric cryptographic operations where both public and private
37
+ * keys are used.
38
+ */
39
+ export interface AsymmetricKeyGenerator<
40
+ GenerateKeyInput,
41
+ GenerateKeyOutput,
42
+ GetPublicKeyInput
43
+ > extends KeyGenerator<GenerateKeyInput, GenerateKeyOutput> {
44
+ /**
45
+ * Optional method that mathetmatically derives the public key in JWK format from a given private
46
+ * key.
47
+ *
48
+ * @param params - The parameters for public key computation.
49
+ *
50
+ * @returns A Promise resolving to the public key in JWK format.
51
+ */
52
+ computePublicKey?(params: GetPublicKeyInput): Promise<Jwk>;
53
+
54
+ /**
55
+ * Extracts the public key portion from the given public key in JWK format.
56
+ *
57
+ * @remarks
58
+ * Unlike `computePublicKey()`, the `getPublicKey()` method does not mathematically validate the
59
+ * private key, nor does it derive the public key from the private key. It simply extracts
60
+ * existing public key properties from the private key JWK object. This makes it suitable for
61
+ * scenarios where speed is critical and the private key's integrity is already assured.
62
+ *
63
+ * @param params - The parameters for public key retrieval.
64
+ *
65
+ * @returns A Promise resolving to the public key in JWK format.
66
+ */
67
+ getPublicKey(params: GetPublicKeyInput): Promise<Jwk>;
68
+ }
69
+
70
+ /**
71
+ * Infers the supported algorithm type from the `generateKey` method of a key generator.
72
+ *
73
+ * @remarks
74
+ * The `InferKeyGeneratorAlgorithm` utility type extracts the algorithm type from the input
75
+ * parameters of the `generateKey` method implemented in a key generator. This type is useful when
76
+ * working with various cryptographic key generators, as it enables TypeScript to infer the
77
+ * supported algorithms based on the key generator's implementation. This inference ensures type
78
+ * safety and improves developer experience by providing relevant suggestions and checks for the
79
+ * supported algorithms during development.
80
+ *
81
+ * This utility type can be particularly advantageous in contexts where the specific key generator
82
+ * may vary, but the code needs to adapt dynamically based on the supported algorithms of the
83
+ * provided key generator instance.
84
+ *
85
+ * @example
86
+ * ```ts
87
+ * export interface MyKmsGenerateKeyParams extends KmsGenerateKeyParams {
88
+ * algorithm: 'Ed25519' | 'secp256k1';
89
+ * }
90
+ *
91
+ * class MyKms implements KeyGenerator<MyKmsGenerateKeyParams, Jwk> {
92
+ * generateKey(params: MyKmsGenerateKeyParams): Promise<Jwk> {
93
+ * // Implementation for generating a key...
94
+ * }
95
+ * }
96
+ *
97
+ * type SupportedAlgorithms = InferKeyGeneratorAlgorithm<MyKms>;
98
+ * // `SupportedAlgorithms` will be inferred as 'Ed25519' | 'secp256k1'
99
+ * ```
100
+ *
101
+ * @template T - The type of the key generator from which to infer the algorithm type.
102
+ */
103
+ export type InferKeyGeneratorAlgorithm<T> = T extends {
104
+ /**
105
+ * The `generateKey` method signature from which the algorithm type is inferred.
106
+ * This is an internal implementation detail and not part of the public API.
107
+ */
108
+ generateKey(params: infer P): any;
109
+ }
110
+ ? P extends {
111
+ /**
112
+ * The `algorithm` property within the parameters of `generateKey`.
113
+ * This internal element is used to infer the algorithm type.
114
+ */
115
+ algorithm: infer A
116
+ }
117
+ ? A
118
+ : never
119
+ : never;
@@ -0,0 +1,42 @@
1
+ import type { Jwk } from '../jose/jwk.js';
2
+
3
+ /**
4
+ * The `KeyImporterExporter` interface provides methods for importing and exporting cryptographic
5
+ * keys. It includes `importKey()` for importing external keys, and `exportKey()` for exporting a
6
+ * cryptographic key to an external JWK object.
7
+ *
8
+ * This interface is designed to handle various key formats and is adaptable for different
9
+ * cryptographic environments and requirements.
10
+ */
11
+ export interface KeyImporterExporter<
12
+ ImportKeyInput,
13
+ ImportKeyOutput,
14
+ ExportKeyInput
15
+ > {
16
+ /**
17
+ * Exports a cryptographic key to an external JWK object.
18
+ *
19
+ * @remarks
20
+ * The `exportKey()` method of the {@link KeyImporterExporter | `KeyImporterExporter`} interface
21
+ * returns a cryptographic key in JWK format, facilitating interoperability and backup.
22
+ *
23
+ * @param params - The parameters for the key export operation.
24
+ *
25
+ * @returns A Promise resolving to the exported key in JWK format.
26
+ */
27
+ exportKey(params: ExportKeyInput): Promise<Jwk>;
28
+
29
+ /**
30
+ * Imports an external key in JWK format.
31
+ *
32
+ * @remarks
33
+ * The `importKey()` method of the {@link KeyImporterExporter | `KeyImporterExporter`} interface
34
+ * takes as input an external key in JWK format and typically returns a key identifier reference
35
+ * for the imported key.
36
+ *
37
+ * @param params - The parameters for the key import operation.
38
+ *
39
+ * @returns A Promise resolving to the key identifier of the imported key.
40
+ */
41
+ importKey(params: ImportKeyInput): Promise<ImportKeyOutput>;
42
+ }
@@ -0,0 +1,42 @@
1
+ import type { Jwk } from '../jose/jwk.js';
2
+
3
+ /**
4
+ * The `KeyWrapper` interface provides methods for wrapping and unwrapping cryptographic keys.
5
+ * It includes `wrapKey()` for securely encapsulating a key within another key, and `unwrapKey()`
6
+ * for extracting the original key from its wrapped state.
7
+ *
8
+ * This interface is crucial in scenarios where secure key management and exchange are required,
9
+ * ensuring that keys remain protected during transit or storage.
10
+ */
11
+ export interface KeyWrapper<
12
+ WrapKeyInput,
13
+ UnwrapKeyInput
14
+ > {
15
+ /**
16
+ * Wraps a cryptographic key using another key, typically for secure key transmission or storage.
17
+ *
18
+ * @remarks
19
+ * The `wrapKey()` method of the {@link KeyWrapper | `KeyWrapper`} interface secures a
20
+ * cryptographic key by encapsulating it within another key, producing a wrapped key represented
21
+ * as a `Uint8Array`.
22
+ *
23
+ * @param params - The parameters for the key wrapping operation.
24
+ *
25
+ * @returns A Promise resolving to the wrapped key as a `Uint8Array`.
26
+ */
27
+ wrapKey(params: WrapKeyInput): Promise<Uint8Array>;
28
+
29
+ /**
30
+ * Unwraps a previously wrapped cryptographic key, restoring it to its original form.
31
+ *
32
+ * @remarks
33
+ * The `unwrapKey()` method of the {@link KeyWrapper | `KeyWrapper`} interface reverses the
34
+ * wrapping process, extracting the original key from its wrapped state, typically for use in
35
+ * cryptographic operations.
36
+ *
37
+ * @param params - The parameters for the key unwrapping operation.
38
+ *
39
+ * @returns A Promise resolving to the unwrapped key in a cryptographic format, usually JWK.
40
+ */
41
+ unwrapKey(params: UnwrapKeyInput): Promise<Jwk>;
42
+ }
@@ -0,0 +1,106 @@
1
+ import type { Jwk } from '../jose/jwk.js';
2
+ import type { AlgorithmIdentifier } from './identifier.js';
3
+
4
+ /**
5
+ * Parameters for computing a public key.
6
+ */
7
+ export interface ComputePublicKeyParams extends GetPublicKeyParams { }
8
+
9
+ /**
10
+ * Parameters for decrypting data.
11
+ */
12
+ export interface DecryptParams {
13
+ /** A {@link Jwk} containing the key to be used for decryption. */
14
+ key: Jwk;
15
+
16
+ /** Data to be decrypted. */
17
+ data: Uint8Array;
18
+ }
19
+
20
+ /**
21
+ * Parameters for deriving bits.
22
+ */
23
+ export interface DeriveBitsParams {
24
+ /** A {@link Jwk} containing the base key to be used for derivation. */
25
+ key: Jwk;
26
+
27
+ /**
28
+ * The number of bits to derive. To be compatible with all browsers, the number should be a
29
+ * multiple of 8.
30
+ */
31
+ length: number;
32
+ }
33
+
34
+ /**
35
+ * Parameters for deriving a key.
36
+ */
37
+ export interface DeriveKeyParams {
38
+ /** A {@link Jwk} containing the base key to be used for derivation. */
39
+ key: Jwk;
40
+
41
+ /** An object defining the algorithm-specific parameters for the derived key. */
42
+ derivedKeyParams: unknown
43
+ }
44
+
45
+ /**
46
+ * Parameters for computing a hash digest.
47
+ */
48
+ export interface DigestParams {
49
+ /** The algorithm identifier. */
50
+ algorithm: AlgorithmIdentifier;
51
+
52
+ /** Data to be digested. */
53
+ data: Uint8Array;
54
+ }
55
+
56
+ /**
57
+ * Parameters for encrypting data.
58
+ */
59
+ export interface EncryptParams {
60
+ /** A {@link Jwk} containing the key to be used for encryption. */
61
+ key: Jwk;
62
+
63
+ /** Data to be encrypted. */
64
+ data: Uint8Array;
65
+ }
66
+
67
+ /**
68
+ * Parameters for generating a key.
69
+ */
70
+ export interface GenerateKeyParams {
71
+ /** The algorithm identifier. */
72
+ algorithm: AlgorithmIdentifier;
73
+ }
74
+
75
+ /**
76
+ * Parameters for retrieving a public key.
77
+ */
78
+ export interface GetPublicKeyParams {
79
+ /** A {@link Jwk} containing the key from which to derive the public key. */
80
+ key: Jwk;
81
+ }
82
+
83
+ /**
84
+ * Parameters for signing data.
85
+ */
86
+ export interface SignParams {
87
+ /** A {@link Jwk} containing the key used for signing. */
88
+ key: Jwk;
89
+
90
+ /** Data to be signed. */
91
+ data: Uint8Array;
92
+ }
93
+
94
+ /**
95
+ * Parameters for verifying a signature.
96
+ */
97
+ export interface VerifyParams {
98
+ /** A {@link Jwk} containing the key used for verification. */
99
+ key: Jwk;
100
+
101
+ /** The signature to verify. */
102
+ signature: Uint8Array;
103
+
104
+ /** The data associated with the signature. */
105
+ data: Uint8Array;
106
+ }
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Parameters for enclosed decryption operations.
3
+ *
4
+ * Note: This interface is intended to be used with a closure that captures the key and
5
+ * algorithm-specific parameters so that arbitrary data can be decrypted without exposing the key or
6
+ * parameters to the caller.
7
+ */
8
+ export interface EnclosedDecryptParams {
9
+ /** Data to be decrypted. */
10
+ data: Uint8Array;
11
+ }
12
+
13
+ /**
14
+ * Parameters for enclosed encryption operations.
15
+ *
16
+ * Note: This interface is intended to be used with a closure that captures the key and
17
+ * algorithm-specific parameters so that arbitrary data can be encrypted without exposing the key or
18
+ * parameters to the caller.
19
+ */
20
+ export interface EnclosedEncryptParams {
21
+ /** Data to be encrypted. */
22
+ data: Uint8Array;
23
+ }
24
+
25
+ /**
26
+ * Parameters for enclosed signing operations.
27
+ *
28
+ * Note: This interface is intended to be used with a closure that captures the key and
29
+ * algorithm-specific parameters so that arbitrary data can be signed without exposing the key or
30
+ * parameters to the caller.
31
+ */
32
+ export interface EnclosedSignParams {
33
+ /** Data to be signed. */
34
+ data: Uint8Array;
35
+ }
36
+
37
+ /**
38
+ * Parameters for enclosed verification operations.
39
+ *
40
+ * Note: This interface is intended to be used with a closure that captures the key and
41
+ * algorithm-specific parameters so that signatures of arbitrary data can be verified without
42
+ * exposing the key or parameters to the caller.
43
+ */
44
+ export interface EnclosedVerifyParams {
45
+ /** Signature to be verified. */
46
+ signature: Uint8Array;
47
+
48
+ /** Data associated with the signature. */
49
+ data: Uint8Array;
50
+ }
@@ -0,0 +1,156 @@
1
+ import type { Jwk } from '../jose/jwk.js';
2
+ import type { AlgorithmIdentifier, KeyIdentifier } from './identifier.js';
3
+
4
+ /**
5
+ * Parameters for KMS-based decryption operations. Intended for use with a Key Management System.
6
+ */
7
+ export interface KmsDecryptParams {
8
+ /** Identifier for the private key in the KMS. */
9
+ keyUri: KeyIdentifier;
10
+
11
+ /** Data to be decrypted. */
12
+ data: Uint8Array;
13
+ }
14
+
15
+ /**
16
+ * Parameters for KMS-based derivation of bits. Intended for use with a Key Management System.
17
+ */
18
+ export interface KmsDeriveBitsParams {
19
+ /** Identifier for the key used in derivation in the KMS. */
20
+ keyUri: KeyIdentifier;
21
+
22
+ /**
23
+ * The number of bits to derive. To be compatible with all browsers, the number should be a
24
+ * multiple of 8.
25
+ */
26
+ length: number;
27
+ }
28
+
29
+ /**
30
+ * Parameters for KMS-based key derivation. Intended for use with a Key Management System.
31
+ */
32
+ export interface KmsDeriveKeyParams {
33
+ /** Identifier for the base key used in derivation in the KMS. */
34
+ keyUri: KeyIdentifier;
35
+
36
+ /** An object defining the algorithm-specific parameters for the derived key. */
37
+ derivedKeyParams: unknown
38
+ }
39
+
40
+ /**
41
+ * Parameters for KMS-based digest computation. Intended for use with a Key Management System.
42
+ */
43
+ export interface KmsDigestParams {
44
+ /** The algorithm identifier. */
45
+ algorithm: AlgorithmIdentifier;
46
+
47
+ /** Data to be digested. */
48
+ data: Uint8Array;
49
+ }
50
+
51
+ /**
52
+ * Parameters for KMS-based encryption operations. Intended for use with a Key Management System.
53
+ */
54
+ export interface KmsEncryptParams {
55
+ /** Identifier for the private key in the KMS. */
56
+ keyUri: KeyIdentifier;
57
+
58
+ /** Data to be encrypted. */
59
+ data: Uint8Array;
60
+ }
61
+
62
+ /**
63
+ * Parameters for exporting a key from a KMS. Intended for use with a Key Management System.
64
+ */
65
+ export interface KmsExportKeyParams {
66
+ /** Identifier for the private key to be exported from the KMS. */
67
+ keyUri: KeyIdentifier;
68
+ }
69
+
70
+ /**
71
+ * Parameters for generating a key in a KMS. Intended for use with a Key Management System.
72
+ */
73
+ export interface KmsGenerateKeyParams {
74
+ /** The algorithm identifier. */
75
+ algorithm: AlgorithmIdentifier;
76
+ }
77
+
78
+ /**
79
+ * Parameters for computing the Key URI of a public key. Intended for use with a Key Management
80
+ * System.
81
+ */
82
+ export interface KmsGetKeyUriParams {
83
+ /** A {@link Jwk} containing the public key for which the Key URI will be computed. */
84
+ key: Jwk;
85
+ }
86
+
87
+ /**
88
+ * Parameters for retrieving a public key from a KMS using the private key's URI. Intended for use
89
+ * with a Key Management System.
90
+ */
91
+ export interface KmsGetPublicKeyParams {
92
+ /** Identifier for the private key in the KMS. */
93
+ keyUri: KeyIdentifier;
94
+ }
95
+
96
+ /**
97
+ * Parameters for importing a private key into a KMS. Intended for use with a Key Management System.
98
+ */
99
+ export interface KmsImportKeyParams {
100
+ /** A {@link Jwk} containing the key to be imported into the KMS. */
101
+ key: Jwk;
102
+ }
103
+
104
+ /**
105
+ * Parameters for KMS-based signing operations. Intended for use with a Key Management System.
106
+ */
107
+ export interface KmsSignParams {
108
+ /** Identifier for the signing private key in the KMS. */
109
+ keyUri: KeyIdentifier;
110
+
111
+ /** Data to be signed. */
112
+ data: Uint8Array;
113
+ }
114
+
115
+ /**
116
+ * Parameters for verifying a signature using a key from a KMS. Intended for use with a Key
117
+ * Management System.
118
+ */
119
+ export interface KmsVerifyParams {
120
+ /** A {@link Jwk} containing the public key to be used for verification. */
121
+ key: Jwk;
122
+
123
+ /** The signature to verify. */
124
+ signature: Uint8Array;
125
+
126
+ /** The data associated with the signature. */
127
+ data: Uint8Array;
128
+ }
129
+
130
+ /**
131
+ * Parameters for wrapping a key using a KMS. Intended for use with a Key Management System.
132
+ */
133
+ export interface KmsWrapKeyParams {
134
+ /** A {@link Jwk} containing the private key to be wrapped. */
135
+ key: Jwk;
136
+
137
+ /** Identifier for the private key in the KMS to be used for the wrapping operation. */
138
+ wrappingKeyId: KeyIdentifier;
139
+
140
+ /** Algorithm to be used for wrapping. */
141
+ wrapAlgorithm: AlgorithmIdentifier;
142
+ }
143
+
144
+ /**
145
+ * Parameters for unwrapping a key using a KMS. Intended for use with a Key Management System.
146
+ */
147
+ export interface KmsUnwrapKeyParams {
148
+ /** The wrapped key in a byte array. */
149
+ wrappedKey: Uint8Array;
150
+
151
+ /** Identifier for the private key in the KMS to be used for the unwrapping operation. */
152
+ unwrappingKeyId: KeyIdentifier;
153
+
154
+ /** Algorithm to be used for unwrapping. */
155
+ unwrapAlgorithm: AlgorithmIdentifier;
156
+ }
@@ -0,0 +1,50 @@
1
+ import type { EnclosedSignParams, EnclosedVerifyParams } from './params-enclosed.js';
2
+
3
+ /**
4
+ * The `Signer` interface provides methods for signing data and verifying signatures.
5
+ *
6
+ * It includes `sign()` for creating signatures and `verify()` for confirming the validity of
7
+ * signatures. The interface is designed to be flexible, accommodating various signing algorithms
8
+ * and their unique parameters.
9
+ *
10
+ * It defaults to using {@link EnclosedSignParams | `EnclosedSignParams`} and
11
+ * {@link EnclosedVerifyParams | `EnclosedVerifyParams`}, which are intended to be used with a
12
+ * closure that captures the key and algorithm-specific parameters so that arbitrary data can be
13
+ * signed and verified without exposing the key or parameters to the caller. However, the
14
+ * interface can be extended to support other parameter types, such as {@link SignParams |
15
+ * `SignParams`} and {@link VerifyParams | `VerifyParams`}, which are intended to be used when
16
+ * the key and algorithm-specific parameters are known to the caller.
17
+ */
18
+ export interface Signer<
19
+ SignInput = EnclosedSignParams,
20
+ VerifyInput = EnclosedVerifyParams
21
+ > {
22
+ /**
23
+ * Signs the provided data.
24
+ *
25
+ * @remarks
26
+ * The `sign()` method of the {@link Signer | `Signer`} interface generates a digital signature
27
+ * for the given data using a cryptographic key. This signature can be used to verify the data's
28
+ * authenticity and integrity.
29
+ *
30
+ * @param params - The parameters for the signing operation.
31
+ *
32
+ * @returns A Promise resolving to the digital signature as a `Uint8Array`.
33
+ */
34
+ sign(params: SignInput): Promise<Uint8Array>;
35
+
36
+ /**
37
+ * Verifies a digital signature associated the provided data.
38
+ *
39
+ * @remarks
40
+ * The `verify()` method of the {@link Signer | `Signer`} interface checks the validity of a
41
+ * digital signature against the original data and a cryptographic key. It confirms whether the
42
+ * signature was created by the holder of the corresponding private key and that the data has not
43
+ * been tampered with.
44
+ *
45
+ * @param params - The parameters for the verification operation.
46
+ *
47
+ * @returns A Promise resolving to a boolean indicating whether the signature is valid.
48
+ */
49
+ verify(params: VerifyInput): Promise<boolean>;
50
+ }