@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,243 @@
1
+ import type { Jwk } from '../jose/jwk.js';
2
+ import type { Signer } from '../types/signer.js';
3
+ import type { AsymmetricKeyGenerator } from '../types/key-generator.js';
4
+ import type {
5
+ SignParams,
6
+ VerifyParams,
7
+ GenerateKeyParams,
8
+ GetPublicKeyParams,
9
+ ComputePublicKeyParams,
10
+ } from '../types/params-direct.js';
11
+
12
+ import { Ed25519 } from '../primitives/ed25519.js';
13
+ import { CryptoAlgorithm } from './crypto-algorithm.js';
14
+ import { isOkpPrivateJwk, isOkpPublicJwk } from '../jose/jwk.js';
15
+
16
+ /**
17
+ * The `EdDsaGenerateKeyParams` interface defines the algorithm-specific parameters that should be
18
+ * passed into the `generateKey()` method when using the EdDSA algorithm.
19
+ */
20
+ export interface EdDsaGenerateKeyParams extends GenerateKeyParams {
21
+ /**
22
+ * A string defining the type of key to generate. The value must be one of the following:
23
+ * - `"Ed25519"`: EdDSA using the Ed25519 curve.
24
+ */
25
+ algorithm: 'Ed25519';
26
+ }
27
+
28
+ /**
29
+ * The `EdDsaAlgorithm` class provides a concrete implementation for cryptographic operations using
30
+ * the Edwards-curve Digital Signature Algorithm (EdDSA). This class implements both
31
+ * {@link Signer | `Signer`} and { @link AsymmetricKeyGenerator | `AsymmetricKeyGenerator`}
32
+ * interfaces, providing private key generation, public key derivation, and creation/verification
33
+ * of signatures.
34
+ *
35
+ * This class is typically accessed through implementations that extend the
36
+ * {@link CryptoApi | `CryptoApi`} interface.
37
+ */
38
+ export class EdDsaAlgorithm extends CryptoAlgorithm
39
+ implements AsymmetricKeyGenerator<EdDsaGenerateKeyParams, Jwk, GetPublicKeyParams>,
40
+ Signer<SignParams, VerifyParams> {
41
+
42
+ /**
43
+ * Derives the public key in JWK format from a given private key.
44
+ *
45
+ * @remarks
46
+ * This method takes a private key in JWK format and derives its corresponding public key,
47
+ * also in JWK format. The process ensures that the derived public key correctly corresponds to
48
+ * the given private key.
49
+ *
50
+ * @example
51
+ * ```ts
52
+ * const eddsa = new EdDsaAlgorithm();
53
+ * const privateKey = { ... }; // A Jwk object representing a private key
54
+ * const publicKey = await eddsa.computePublicKey({ key: privateKey });
55
+ * ```
56
+ *
57
+ * @param params - The parameters for the public key derivation.
58
+ * @param params.key - The private key in JWK format from which to derive the public key.
59
+ *
60
+ * @returns A Promise that resolves to the derived public key in JWK format.
61
+ */
62
+ public async computePublicKey({ key }:
63
+ ComputePublicKeyParams
64
+ ): Promise<Jwk> {
65
+ if (!isOkpPrivateJwk(key)) throw new TypeError('Invalid key provided. Must be an octet key pair (OKP) private key.');
66
+
67
+ switch (key.crv) {
68
+
69
+ case 'Ed25519': {
70
+ const publicKey = await Ed25519.computePublicKey({ key });
71
+ publicKey.alg = 'EdDSA';
72
+ return publicKey;
73
+ }
74
+
75
+ default: {
76
+ throw new Error(`Unsupported curve: ${key.crv}`);
77
+ }
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Generates a new private key with the specified algorithm in JSON Web Key (JWK) format.
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * const eddsa = new EdDsaAlgorithm();
87
+ * const privateKey = await eddsa.generateKey({ algorithm: 'Ed25519' });
88
+ * ```
89
+ *
90
+ * @param params - The parameters for key generation.
91
+ * @param params.algorithm - The algorithm to use for key generation.
92
+ *
93
+ * @returns A Promise that resolves to the generated private key in JWK format.
94
+ */
95
+ async generateKey({ algorithm }:
96
+ EdDsaGenerateKeyParams
97
+ ): Promise<Jwk> {
98
+ switch (algorithm) {
99
+
100
+ case 'Ed25519': {
101
+ const privateKey = await Ed25519.generateKey();
102
+ privateKey.alg = 'EdDSA';
103
+ return privateKey;
104
+ }
105
+ }
106
+ }
107
+
108
+ /**
109
+ * Retrieves the public key properties from a given private key in JWK format.
110
+ *
111
+ * @remarks
112
+ * This method extracts the public key portion from an EdDSA private key in JWK format. It does
113
+ * so by removing the private key property 'd' and making a shallow copy, effectively yielding the
114
+ * public key.
115
+ *
116
+ * Note: This method offers a significant performance advantage, being about 100 times faster
117
+ * than `computePublicKey()`. However, it does not mathematically validate the private key, nor
118
+ * does it derive the public key from the private key. It simply extracts existing public key
119
+ * properties from the private key object. This makes it suitable for scenarios where speed is
120
+ * critical and the private key's integrity is already assured.
121
+ *
122
+ * @example
123
+ * ```ts
124
+ * const eddsa = new EdDsaAlgorithm();
125
+ * const privateKey = { ... }; // A Jwk object representing a private key
126
+ * const publicKey = await eddsa.getPublicKey({ key: privateKey });
127
+ * ```
128
+ *
129
+ * @param params - The parameters for retrieving the public key properties.
130
+ * @param params.key - The private key in JWK format.
131
+ *
132
+ * @returns A Promise that resolves to the public key in JWK format.
133
+ */
134
+ public async getPublicKey({ key }:
135
+ GetPublicKeyParams
136
+ ): Promise<Jwk> {
137
+ if (!isOkpPrivateJwk(key)) throw new TypeError('Invalid key provided. Must be an octet key pair (OKP) private key.');
138
+
139
+ switch (key.crv) {
140
+
141
+ case 'Ed25519': {
142
+ const publicKey = await Ed25519.getPublicKey({ key });
143
+ publicKey.alg = 'EdDSA';
144
+ return publicKey;
145
+ }
146
+
147
+ default: {
148
+ throw new Error(`Unsupported curve: ${key.crv}`);
149
+ }
150
+ }
151
+ }
152
+
153
+ /**
154
+ * Generates an EdDSA signature of given data using a private key.
155
+ *
156
+ * @remarks
157
+ * This method uses the signature algorithm determined by the given `algorithm` to sign the
158
+ * provided data.
159
+ *
160
+ * The signature can later be verified by parties with access to the corresponding
161
+ * public key, ensuring that the data has not been tampered with and was indeed signed by the
162
+ * holder of the private key.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * const eddsa = new EdDsaAlgorithm();
167
+ * const data = new TextEncoder().encode('Message');
168
+ * const privateKey = { ... }; // A Jwk object representing a private key
169
+ * const signature = await eddsa.sign({
170
+ * key: privateKey,
171
+ * data
172
+ * });
173
+ * ```
174
+ *
175
+ * @param params - The parameters for the signing operation.
176
+ * @param params.key - The private key to use for signing, represented in JWK format.
177
+ * @param params.data - The data to sign.
178
+ *
179
+ * @returns A Promise resolving to the digital signature as a `Uint8Array`.
180
+ */
181
+ public async sign({ key, data }:
182
+ SignParams
183
+ ): Promise<Uint8Array> {
184
+ if (!isOkpPrivateJwk(key)) throw new TypeError('Invalid key provided. Must be an octet key pair (OKP) private key.');
185
+
186
+ switch (key.crv) {
187
+
188
+ case 'Ed25519': {
189
+ return await Ed25519.sign({ key, data });
190
+ }
191
+
192
+ default: {
193
+ throw new Error(`Unsupported curve: ${key.crv}`);
194
+ }
195
+ }
196
+ }
197
+
198
+ /**
199
+ * Verifies an EdDSA signature associated with the provided data using the provided key.
200
+ *
201
+ * @remarks
202
+ * This method uses the signature algorithm determined by the `crv` property of the provided key
203
+ * to check the validity of a digital signature against the original data. It confirms whether the
204
+ * signature was created by the holder of the corresponding private key and that the data has not
205
+ * been tampered with.
206
+ *s
207
+ * @example
208
+ * ```ts
209
+ * const eddsa = new EdDsaAlgorithm();
210
+ * const publicKey = { ... }; // Public key in JWK format corresponding to the private key that signed the data
211
+ * const signature = new Uint8Array([...]); // Signature to verify
212
+ * const data = new TextEncoder().encode('Message');
213
+ * const isValid = await eddsa.verify({
214
+ * key: publicKey,
215
+ * signature,
216
+ * data
217
+ * });
218
+ * ```
219
+ *
220
+ * @param params - The parameters for the verification operation.
221
+ * @param params.key - The key to use for verification.
222
+ * @param params.signature - The signature to verify.
223
+ * @param params.data - The data to verify.
224
+ *
225
+ * @returns A Promise resolving to a boolean indicating whether the signature is valid.
226
+ */
227
+ public async verify({ key, signature, data }:
228
+ VerifyParams
229
+ ): Promise<boolean> {
230
+ if (!isOkpPublicJwk(key)) throw new TypeError('Invalid key provided. Must be an octet key pair (OKP) public key.');
231
+
232
+ switch (key.crv) {
233
+
234
+ case 'Ed25519': {
235
+ return await Ed25519.verify({ key, signature, data });
236
+ }
237
+
238
+ default: {
239
+ throw new Error(`Unsupported curve: ${key.crv}`);
240
+ }
241
+ }
242
+ }
243
+ }
@@ -0,0 +1,65 @@
1
+ import type { Hasher } from '../types/hasher.js';
2
+ import type { DigestParams } from '../types/params-direct.js';
3
+
4
+ import { Sha256 } from '../primitives/sha256.js';
5
+ import { CryptoAlgorithm } from './crypto-algorithm.js';
6
+
7
+ /**
8
+ * The `Sha2DigestParams` interface defines the algorithm-specific parameters that should be
9
+ * passed into the `digest()` method when using the SHA-2 algorithm.
10
+ */
11
+ export interface Sha2DigestParams extends DigestParams {
12
+ /**
13
+ * A string defining the name of hash function to use. The value must be one of the following:
14
+ * - `"SHA-256"`: Generates a 256-bit digest.
15
+ */
16
+ algorithm: 'SHA-256';
17
+ }
18
+
19
+ /**
20
+ * The `Sha2Algorithm` class is an implementation of the {@link Hasher | `Hasher`} interface for the
21
+ * SHA-2 family of cryptographic hash functions. The `digest` method takes the algorithm identifier
22
+ * of the hash function and arbitrary data as input and returns the hash digest of the data.
23
+ *
24
+ * This class is typically accessed through implementations that extend the
25
+ * {@link CryptoApi | `CryptoApi`} interface.
26
+ */
27
+ export class Sha2Algorithm extends CryptoAlgorithm
28
+ implements Hasher<Sha2DigestParams> {
29
+
30
+ /**
31
+ * Generates a hash digest of the provided data.
32
+ *
33
+ * @remarks
34
+ * A digest is the output of the hash function. It's a fixed-size string of bytes
35
+ * that uniquely represents the data input into the hash function. The digest is often used for
36
+ * data integrity checks, as any alteration in the input data results in a significantly
37
+ * different digest.
38
+ *
39
+ * It takes the algorithm identifier of the hash function and data to digest as input and returns
40
+ * the digest of the data.
41
+ *
42
+ * @example
43
+ * ```ts
44
+ * const sha2 = new Sha2Algorithm();
45
+ * const data = new TextEncoder().encode('Messsage');
46
+ * const digest = await sha2.digest({ data });
47
+ * ```
48
+ *
49
+ * @param params - The parameters for the digest operation.
50
+ * @param params.algorithm - The name of hash function to use.
51
+ * @param params.data - The data to digest.
52
+ *
53
+ * @returns A Promise which will be fulfilled with the hash digest.
54
+ */
55
+ public async digest({ algorithm, data }: Sha2DigestParams): Promise<Uint8Array> {
56
+ switch (algorithm) {
57
+
58
+ case 'SHA-256': {
59
+ const hash = await Sha256.digest({ data });
60
+ return hash;
61
+ }
62
+ }
63
+
64
+ }
65
+ }
package/src/index.ts ADDED
@@ -0,0 +1,42 @@
1
+ export * from './local-key-manager.js';
2
+ export * from './utils.js';
3
+
4
+ export * from './algorithms/aes-ctr.js';
5
+ export * from './algorithms/aes-gcm.js';
6
+ export * from './algorithms/crypto-algorithm.js';
7
+ export * from './algorithms/ecdsa.js';
8
+ export * from './algorithms/eddsa.js';
9
+ export * from './algorithms/sha-2.js';
10
+
11
+ export * from './jose/jwe.js';
12
+ export * from './jose/jwk.js';
13
+ export * from './jose/jws.js';
14
+ export * from './jose/jwt.js';
15
+ export * from './jose/utils.js';
16
+
17
+ export * from './primitives/aes-ctr.js';
18
+ export * from './primitives/aes-gcm.js';
19
+ export * from './primitives/concat-kdf.js';
20
+ export * from './primitives/ed25519.js';
21
+ export * from './primitives/secp256r1.js';
22
+ export * from './primitives/pbkdf2.js';
23
+ export * from './primitives/secp256k1.js';
24
+ export * from './primitives/sha256.js';
25
+ export * from './primitives/x25519.js';
26
+ export * from './primitives/xchacha20.js';
27
+ export * from './primitives/xchacha20-poly1305.js';
28
+
29
+ export type * from './types/cipher.js';
30
+ export type * from './types/crypto-api.js';
31
+ export type * from './types/hasher.js';
32
+ export type * from './types/identifier.js';
33
+ export type * from './types/key-compressor.js';
34
+ export type * from './types/key-converter.js';
35
+ export type * from './types/key-deriver.js';
36
+ export type * from './types/key-generator.js';
37
+ export type * from './types/key-io.js';
38
+ export type * from './types/key-wrapper.js';
39
+ export type * from './types/params-direct.js';
40
+ export type * from './types/params-enclosed.js';
41
+ export type * from './types/params-kms.js';
42
+ export type * from './types/signer.js';
@@ -0,0 +1,196 @@
1
+ import type { JoseHeaderParams } from './jws.js';
2
+
3
+ /**
4
+ * JSON Web Encryption (JWE) Header Parameters
5
+ *
6
+ * The Header Parameter names for use in JWEs are registered in the IANA "JSON Web Signature and
7
+ * Encryption Header Parameters" registry.
8
+ *
9
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7516#section-4.1 | RFC 7516, Section 4.1}
10
+ */
11
+ export interface JweHeaderParams extends JoseHeaderParams {
12
+ /**
13
+ * Algorithm Header Parameter
14
+ *
15
+ * Identifies the cryptographic algorithm used to encrypt or determine the value of the Content
16
+ * Encryption Key (CEK). The encrypted content is not usable if the "alg" value does not represent
17
+ * a supported algorithm, or if the recipient does not have a key that can be used with that
18
+ * algorithm.
19
+ *
20
+ * "alg" values should either be registered in the IANA "JSON Web Signature and Encryption
21
+ * Algorithms" registry or be a value that contains a Collision-Resistant Name. The "alg" value is
22
+ * a case-sensitive ASCII string. This Header Parameter MUST be present and MUST be understood
23
+ * and processed by implementations.
24
+ *
25
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7516#section-4.1.1 | RFC 7516, Section 4.1.1}
26
+ */
27
+ alg:
28
+ // AES Key Wrap with default initial value using 128-bit key
29
+ | 'A128KW'
30
+ // AES Key Wrap with default initial value using 192-bit key
31
+ | 'A192KW'
32
+ // AES Key Wrap with default initial value using 256-bit key
33
+ | 'A256KW'
34
+ // Direct use of a shared symmetric key as the CEK
35
+ | 'dir'
36
+ // Elliptic Curve Diffie-Hellman Ephemeral Static key agreement using Concat KDF
37
+ | 'ECDH-ES'
38
+ // ECDH-ES using Concat KDF and CEK wrapped with "A128KW"
39
+ | 'ECDH-ES+A128KW'
40
+ // ECDH-ES using Concat KDF and CEK wrapped with "A192KW"
41
+ | 'ECDH-ES+A192KW'
42
+ // ECDH-ES using Concat KDF and CEK wrapped with "A256KW"
43
+ | 'ECDH-ES+A256KW'
44
+ // Key wrapping with AES GCM using 128-bit key
45
+ | 'A128GCMKW'
46
+ // Key wrapping with AES GCM using 192-bit key
47
+ | 'A192GCMKW'
48
+ // Key wrapping with AES GCM using 256-bit key
49
+ | 'A256GCMKW'
50
+ // PBES2 with HMAC SHA-256 and "A128KW" wrapping
51
+ | 'PBES2-HS256+A128KW'
52
+ // PBES2 with HMAC SHA-384 and "A192KW" wrapping
53
+ | 'PBES2-HS384+A192KW'
54
+ // PBES2 with HMAC SHA-512 and "A256KW" wrapping
55
+ | 'PBES2-HS512+A256KW'
56
+ // PBES2 with HMAC SHA-512 and "XC20PKW" wrapping
57
+ | 'PBES2-HS512+XC20PKW'
58
+ // an unregistered, case-sensitive, collision-resistant string
59
+ | string;
60
+
61
+ /**
62
+ * Agreement PartyUInfo Header Parameter
63
+ *
64
+ * The "apu" (agreement PartyUInfo) value is a base64url-encoded octet sequence containing
65
+ * information about the producer of the JWE. This information is used by the recipient to
66
+ * determine the key agreement algorithm and key encryption algorithm to use to decrypt the JWE.
67
+ *
68
+ * Note: This parameter is intended only for use when the recipient is a key agreement algorithm
69
+ * that uses public key cryptography.
70
+ */
71
+ apu?: Uint8Array;
72
+
73
+ /**
74
+ * Agreement PartyVInfo Header Parameter
75
+ *
76
+ * The "apv" (agreement PartyVInfo) value is a base64url-encoded octet sequence containing
77
+ * information about the recipient of the JWE. This information is used by the recipient to
78
+ * determine the key agreement algorithm and key encryption algorithm to use to decrypt the JWE.
79
+ *
80
+ * Note: This parameter is intended only for use when the recipient is a key agreement algorithm
81
+ * that uses public key cryptography.
82
+ */
83
+ apv?: Uint8Array;
84
+
85
+ /**
86
+ * Critical Header Parameter
87
+ *
88
+ * Indicates that extensions to JOSE RFCs are being used that MUST be understood and processed.
89
+ */
90
+ crit?: string[]
91
+
92
+ /**
93
+ * Encryption Algorithm Header Parameter
94
+ *
95
+ * Identifies the content encryption algorithm used to encrypt and integrity-protect (also
96
+ * known as "authenticated encryption") the plaintext and to integrity-protect the Additional
97
+ * Authenticated Data (AAD), if any. This algorithm MUST be an AEAD algorithm with a specified
98
+ * key length.
99
+ *
100
+ * The encrypted content is not usable if the "enc" value does not represent a supported
101
+ * algorithm. "enc" values should either be registered in the IANA "JSON Web Signature and
102
+ * Encryption Algorithms" registry or be a value that contains a Collision-Resistant Name. The
103
+ * "enc" value is a case-sensitive ASCII string containing a StringOrURI value. This Header
104
+ * Parameter MUST be present and MUST be understood and processed by implementations.
105
+ *
106
+ * @see {@link https://datatracker.ietf.org/doc/html/rfc7516#section-4.1.2 | RFC 7516, Section 4.1.2}
107
+ */
108
+ enc:
109
+ // AES_128_CBC_HMAC_SHA_256 authenticated encryption algorithm,
110
+ // as defined in RFC 7518, Section 5.2.3
111
+ | 'A128CBC-HS256'
112
+ // AES_192_CBC_HMAC_SHA_384 authenticated encryption algorithm,
113
+ // as defined in RFC 7518, Section 5.2.4
114
+ | 'A192CBC-HS384'
115
+ // AES_256_CBC_HMAC_SHA_512 authenticated encryption algorithm,
116
+ // as defined in RFC 7518, Section 5.2.5
117
+ | 'A256CBC-HS512'
118
+ // AES GCM using 128-bit key
119
+ | 'A128GCM'
120
+ // AES GCM using 192-bit key
121
+ | 'A192GCM'
122
+ // AES GCM using 256-bit key
123
+ | 'A256GCM'
124
+ // XChaCha20-Poly1305 authenticated encryption algorithm
125
+ | 'XC20P'
126
+ // an unregistered, case-sensitive, collision-resistant string
127
+ | string;
128
+
129
+ /**
130
+ * Ephemeral Public Key Header Parameter
131
+ *
132
+ * The "epk" (ephemeral public key) value created by the originator for the use in key agreement
133
+ * algorithms. It is the ephemeral public key that corresponds to the key used to encrypt the
134
+ * JWE. This value is represented as a JSON Web Key (JWK).
135
+ *
136
+ * Note: This parameter is intended only for use when the recipient is a key agreement algorithm
137
+ * that uses public key cryptography.
138
+ */
139
+ epk?: Uint8Array;
140
+
141
+ /**
142
+ * Initialization Vector Header Parameter
143
+ *
144
+ * The "iv" (initialization vector) value is a base64url-encoded octet sequence used by the
145
+ * specified "enc" algorithm. The length of this Initialization Vector value MUST be exactly
146
+ * equal to the value that would be produced by the "enc" algorithm.
147
+ *
148
+ * Note: With symmetric encryption algorithms such as AES GCM, this Header Parameter MUST
149
+ * be present and MUST be understood and processed by implementations.
150
+ */
151
+ iv?: Uint8Array;
152
+
153
+ /**
154
+ * PBES2 Count Header Parameter
155
+ *
156
+ * The "p2c" (PBES2 count) value is an integer indicating the number of iterations of the PBKDF2
157
+ * algorithm performed during key derivation.
158
+ *
159
+ * Note: The iteration count adds computational expense, ideally compounded by the possible range
160
+ * of keys introduced by the salt. A minimum iteration count of 1000 is RECOMMENDED.
161
+ */
162
+ p2c?: number;
163
+
164
+ /**
165
+ * PBES2 Salt Input Header Parameter
166
+ *
167
+ * The "p2s" (PBES2 salt) value is a base64url-encoded octet sequence used as the salt value
168
+ * input to the PBKDF2 algorithm during key derivation.
169
+ *
170
+ * The salt value used is (UTF8(Alg) || 0x00 || Salt Input), where Alg is the "alg" (algorithm)
171
+ * Header Parameter value.
172
+ *
173
+ * Note: The salt value is used to ensure that each key derived from the master key is
174
+ * independent of every other key. A suitable source of salt value is a sequence of
175
+ * cryptographically random bytes containing 8 or more octets.
176
+ */
177
+ p2s?: string;
178
+
179
+ /**
180
+ * Authentication Tag Header Parameter
181
+ *
182
+ * The "tag" value is a base64url-encoded octet sequence containing the value of the
183
+ * Authentication Tag output by the specified "enc" algorithm. The length of this
184
+ * Authentication Tag value MUST be exactly equal to the value that would be produced by the
185
+ * "enc" algorithm.
186
+ *
187
+ * Note: With authenticated encryption algorithms such as AES GCM, this Header Parameter MUST
188
+ * be present and MUST be understood and processed by implementations.
189
+ */
190
+ tag?: Uint8Array;
191
+
192
+ /**
193
+ * Additional Public or Private Header Parameter names.
194
+ */
195
+ [key: string]: unknown
196
+ }