@enbox/crypto 0.0.2 → 0.0.4

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 (226) hide show
  1. package/README.md +34 -102
  2. package/dist/browser.mjs +6 -10
  3. package/dist/browser.mjs.map +4 -4
  4. package/dist/esm/algorithms/aes-ctr.js +1 -1
  5. package/dist/esm/algorithms/aes-gcm.js +35 -2
  6. package/dist/esm/algorithms/aes-gcm.js.map +1 -1
  7. package/dist/esm/algorithms/aes-kw.js +154 -0
  8. package/dist/esm/algorithms/aes-kw.js.map +1 -0
  9. package/dist/esm/algorithms/ecdsa.js +119 -6
  10. package/dist/esm/algorithms/ecdsa.js.map +1 -1
  11. package/dist/esm/algorithms/eddsa.js +99 -6
  12. package/dist/esm/algorithms/eddsa.js.map +1 -1
  13. package/dist/esm/algorithms/hkdf.js +53 -0
  14. package/dist/esm/algorithms/hkdf.js.map +1 -0
  15. package/dist/esm/algorithms/pbkdf2.js +55 -0
  16. package/dist/esm/algorithms/pbkdf2.js.map +1 -0
  17. package/dist/esm/algorithms/sha-2.js +2 -2
  18. package/dist/esm/algorithms/sha-2.js.map +1 -1
  19. package/dist/esm/algorithms/x25519.js +125 -0
  20. package/dist/esm/algorithms/x25519.js.map +1 -0
  21. package/dist/esm/crypto-error.js +41 -0
  22. package/dist/esm/crypto-error.js.map +1 -0
  23. package/dist/esm/index.js +8 -0
  24. package/dist/esm/index.js.map +1 -1
  25. package/dist/esm/jose/jwk.js +52 -26
  26. package/dist/esm/jose/jwk.js.map +1 -1
  27. package/dist/esm/local-key-manager.js +9 -3
  28. package/dist/esm/local-key-manager.js.map +1 -1
  29. package/dist/esm/primitives/aes-ctr.js.map +1 -1
  30. package/dist/esm/primitives/aes-gcm.js.map +1 -1
  31. package/dist/esm/primitives/aes-kw.js +246 -0
  32. package/dist/esm/primitives/aes-kw.js.map +1 -0
  33. package/dist/esm/primitives/concat-kdf.js +1 -1
  34. package/dist/esm/primitives/concat-kdf.js.map +1 -1
  35. package/dist/esm/primitives/ecies-secp256k1.js +79 -0
  36. package/dist/esm/primitives/ecies-secp256k1.js.map +1 -0
  37. package/dist/esm/primitives/ed25519.js +3 -3
  38. package/dist/esm/primitives/ed25519.js.map +1 -1
  39. package/dist/esm/primitives/hkdf.js +79 -0
  40. package/dist/esm/primitives/hkdf.js.map +1 -0
  41. package/dist/esm/primitives/pbkdf2.js +49 -0
  42. package/dist/esm/primitives/pbkdf2.js.map +1 -1
  43. package/dist/esm/primitives/secp256k1.js +4 -4
  44. package/dist/esm/primitives/secp256k1.js.map +1 -1
  45. package/dist/esm/primitives/secp256r1.js +4 -4
  46. package/dist/esm/primitives/secp256r1.js.map +1 -1
  47. package/dist/esm/primitives/x25519.js +10 -17
  48. package/dist/esm/primitives/x25519.js.map +1 -1
  49. package/dist/esm/primitives/xchacha20-poly1305.js +48 -3
  50. package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -1
  51. package/dist/esm/primitives/xchacha20.js +1 -1
  52. package/dist/esm/primitives/xchacha20.js.map +1 -1
  53. package/dist/esm/utils.js +30 -0
  54. package/dist/esm/utils.js.map +1 -1
  55. package/dist/types/algorithms/aes-ctr.d.ts +2 -2
  56. package/dist/types/algorithms/aes-ctr.d.ts.map +1 -1
  57. package/dist/types/algorithms/aes-gcm.d.ts +25 -5
  58. package/dist/types/algorithms/aes-gcm.d.ts.map +1 -1
  59. package/dist/types/algorithms/aes-kw.d.ts +129 -0
  60. package/dist/types/algorithms/aes-kw.d.ts.map +1 -0
  61. package/dist/types/algorithms/ecdsa.d.ts +49 -4
  62. package/dist/types/algorithms/ecdsa.d.ts.map +1 -1
  63. package/dist/types/algorithms/eddsa.d.ts +49 -4
  64. package/dist/types/algorithms/eddsa.d.ts.map +1 -1
  65. package/dist/types/algorithms/hkdf.d.ts +35 -0
  66. package/dist/types/algorithms/hkdf.d.ts.map +1 -0
  67. package/dist/types/algorithms/pbkdf2.d.ts +35 -0
  68. package/dist/types/algorithms/pbkdf2.d.ts.map +1 -0
  69. package/dist/types/algorithms/sha-2.d.ts +2 -2
  70. package/dist/types/algorithms/sha-2.d.ts.map +1 -1
  71. package/dist/types/algorithms/x25519.d.ts +76 -0
  72. package/dist/types/algorithms/x25519.d.ts.map +1 -0
  73. package/dist/types/crypto-error.d.ts +29 -0
  74. package/dist/types/crypto-error.d.ts.map +1 -0
  75. package/dist/types/index.d.ts +8 -0
  76. package/dist/types/index.d.ts.map +1 -1
  77. package/dist/types/jose/jwk.d.ts.map +1 -1
  78. package/dist/types/local-key-manager.d.ts +6 -6
  79. package/dist/types/local-key-manager.d.ts.map +1 -1
  80. package/dist/types/primitives/aes-kw.d.ts +103 -0
  81. package/dist/types/primitives/aes-kw.d.ts.map +1 -0
  82. package/dist/types/primitives/concat-kdf.d.ts +1 -1
  83. package/dist/types/primitives/concat-kdf.d.ts.map +1 -1
  84. package/dist/types/primitives/ecies-secp256k1.d.ts +53 -0
  85. package/dist/types/primitives/ecies-secp256k1.d.ts.map +1 -0
  86. package/dist/types/primitives/hkdf.d.ts +90 -0
  87. package/dist/types/primitives/hkdf.d.ts.map +1 -0
  88. package/dist/types/primitives/pbkdf2.d.ts +58 -0
  89. package/dist/types/primitives/pbkdf2.d.ts.map +1 -1
  90. package/dist/types/primitives/x25519.d.ts +9 -16
  91. package/dist/types/primitives/x25519.d.ts.map +1 -1
  92. package/dist/types/primitives/xchacha20-poly1305.d.ts +47 -0
  93. package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -1
  94. package/dist/types/types/cipher.d.ts +1 -1
  95. package/dist/types/types/crypto-api.d.ts +54 -6
  96. package/dist/types/types/crypto-api.d.ts.map +1 -1
  97. package/dist/types/types/key-converter.d.ts +37 -15
  98. package/dist/types/types/key-converter.d.ts.map +1 -1
  99. package/dist/types/types/key-deriver.d.ts +41 -0
  100. package/dist/types/types/key-deriver.d.ts.map +1 -1
  101. package/dist/types/types/key-io.d.ts +37 -0
  102. package/dist/types/types/key-io.d.ts.map +1 -1
  103. package/dist/types/types/params-direct.d.ts +96 -1
  104. package/dist/types/types/params-direct.d.ts.map +1 -1
  105. package/dist/types/types/params-kms.d.ts +55 -0
  106. package/dist/types/types/params-kms.d.ts.map +1 -1
  107. package/dist/types/utils.d.ts +19 -0
  108. package/dist/types/utils.d.ts.map +1 -1
  109. package/dist/utils.js +1 -1
  110. package/dist/utils.js.map +4 -4
  111. package/package.json +29 -45
  112. package/src/algorithms/aes-ctr.ts +2 -2
  113. package/src/algorithms/aes-gcm.ts +41 -4
  114. package/src/algorithms/aes-kw.ts +182 -0
  115. package/src/algorithms/ecdsa.ts +145 -8
  116. package/src/algorithms/eddsa.ts +117 -10
  117. package/src/algorithms/hkdf.ts +54 -0
  118. package/src/algorithms/pbkdf2.ts +57 -0
  119. package/src/algorithms/sha-2.ts +3 -3
  120. package/src/algorithms/x25519.ts +153 -0
  121. package/src/crypto-error.ts +45 -0
  122. package/src/index.ts +8 -0
  123. package/src/jose/jwk.ts +32 -32
  124. package/src/local-key-manager.ts +22 -16
  125. package/src/primitives/aes-ctr.ts +1 -1
  126. package/src/primitives/aes-gcm.ts +5 -5
  127. package/src/primitives/aes-kw.ts +269 -0
  128. package/src/primitives/concat-kdf.ts +4 -2
  129. package/src/primitives/ecies-secp256k1.ts +113 -0
  130. package/src/primitives/ed25519.ts +6 -6
  131. package/src/primitives/hkdf.ts +121 -0
  132. package/src/primitives/pbkdf2.ts +91 -0
  133. package/src/primitives/secp256k1.ts +6 -6
  134. package/src/primitives/secp256r1.ts +6 -6
  135. package/src/primitives/x25519.ts +12 -19
  136. package/src/primitives/xchacha20-poly1305.ts +57 -4
  137. package/src/primitives/xchacha20.ts +1 -1
  138. package/src/types/cipher.ts +1 -1
  139. package/src/types/crypto-api.ts +129 -11
  140. package/src/types/key-converter.ts +33 -7
  141. package/src/types/key-deriver.ts +49 -0
  142. package/src/types/key-io.ts +40 -0
  143. package/src/types/params-direct.ts +118 -1
  144. package/src/types/params-kms.ts +67 -0
  145. package/src/utils.ts +55 -2
  146. package/dist/browser.js +0 -64
  147. package/dist/browser.js.map +0 -7
  148. package/dist/cjs/algorithms/aes-ctr.js +0 -188
  149. package/dist/cjs/algorithms/aes-ctr.js.map +0 -1
  150. package/dist/cjs/algorithms/aes-gcm.js +0 -196
  151. package/dist/cjs/algorithms/aes-gcm.js.map +0 -1
  152. package/dist/cjs/algorithms/crypto-algorithm.js +0 -13
  153. package/dist/cjs/algorithms/crypto-algorithm.js.map +0 -1
  154. package/dist/cjs/algorithms/ecdsa.js +0 -352
  155. package/dist/cjs/algorithms/ecdsa.js.map +0 -1
  156. package/dist/cjs/algorithms/eddsa.js +0 -325
  157. package/dist/cjs/algorithms/eddsa.js.map +0 -1
  158. package/dist/cjs/algorithms/sha-2.js +0 -119
  159. package/dist/cjs/algorithms/sha-2.js.map +0 -1
  160. package/dist/cjs/index.js +0 -41
  161. package/dist/cjs/index.js.map +0 -1
  162. package/dist/cjs/jose/jwe.js +0 -3
  163. package/dist/cjs/jose/jwe.js.map +0 -1
  164. package/dist/cjs/jose/jwk.js +0 -278
  165. package/dist/cjs/jose/jwk.js.map +0 -1
  166. package/dist/cjs/jose/jws.js +0 -3
  167. package/dist/cjs/jose/jws.js.map +0 -1
  168. package/dist/cjs/jose/jwt.js +0 -3
  169. package/dist/cjs/jose/jwt.js.map +0 -1
  170. package/dist/cjs/jose/utils.js +0 -60
  171. package/dist/cjs/jose/utils.js.map +0 -1
  172. package/dist/cjs/local-key-manager.js +0 -521
  173. package/dist/cjs/local-key-manager.js.map +0 -1
  174. package/dist/cjs/package.json +0 -1
  175. package/dist/cjs/primitives/aes-ctr.js +0 -398
  176. package/dist/cjs/primitives/aes-ctr.js.map +0 -1
  177. package/dist/cjs/primitives/aes-gcm.js +0 -425
  178. package/dist/cjs/primitives/aes-gcm.js.map +0 -1
  179. package/dist/cjs/primitives/concat-kdf.js +0 -215
  180. package/dist/cjs/primitives/concat-kdf.js.map +0 -1
  181. package/dist/cjs/primitives/ed25519.js +0 -651
  182. package/dist/cjs/primitives/ed25519.js.map +0 -1
  183. package/dist/cjs/primitives/pbkdf2.js +0 -120
  184. package/dist/cjs/primitives/pbkdf2.js.map +0 -1
  185. package/dist/cjs/primitives/secp256k1.js +0 -958
  186. package/dist/cjs/primitives/secp256k1.js.map +0 -1
  187. package/dist/cjs/primitives/secp256r1.js +0 -959
  188. package/dist/cjs/primitives/secp256r1.js.map +0 -1
  189. package/dist/cjs/primitives/sha256.js +0 -93
  190. package/dist/cjs/primitives/sha256.js.map +0 -1
  191. package/dist/cjs/primitives/x25519.js +0 -498
  192. package/dist/cjs/primitives/x25519.js.map +0 -1
  193. package/dist/cjs/primitives/xchacha20-poly1305.js +0 -340
  194. package/dist/cjs/primitives/xchacha20-poly1305.js.map +0 -1
  195. package/dist/cjs/primitives/xchacha20.js +0 -316
  196. package/dist/cjs/primitives/xchacha20.js.map +0 -1
  197. package/dist/cjs/types/cipher.js +0 -3
  198. package/dist/cjs/types/cipher.js.map +0 -1
  199. package/dist/cjs/types/crypto-api.js +0 -3
  200. package/dist/cjs/types/crypto-api.js.map +0 -1
  201. package/dist/cjs/types/hasher.js +0 -3
  202. package/dist/cjs/types/hasher.js.map +0 -1
  203. package/dist/cjs/types/identifier.js +0 -3
  204. package/dist/cjs/types/identifier.js.map +0 -1
  205. package/dist/cjs/types/key-compressor.js +0 -3
  206. package/dist/cjs/types/key-compressor.js.map +0 -1
  207. package/dist/cjs/types/key-converter.js +0 -3
  208. package/dist/cjs/types/key-converter.js.map +0 -1
  209. package/dist/cjs/types/key-deriver.js +0 -3
  210. package/dist/cjs/types/key-deriver.js.map +0 -1
  211. package/dist/cjs/types/key-generator.js +0 -3
  212. package/dist/cjs/types/key-generator.js.map +0 -1
  213. package/dist/cjs/types/key-io.js +0 -3
  214. package/dist/cjs/types/key-io.js.map +0 -1
  215. package/dist/cjs/types/key-wrapper.js +0 -3
  216. package/dist/cjs/types/key-wrapper.js.map +0 -1
  217. package/dist/cjs/types/params-direct.js +0 -3
  218. package/dist/cjs/types/params-direct.js.map +0 -1
  219. package/dist/cjs/types/params-enclosed.js +0 -3
  220. package/dist/cjs/types/params-enclosed.js.map +0 -1
  221. package/dist/cjs/types/params-kms.js +0 -3
  222. package/dist/cjs/types/params-kms.js.map +0 -1
  223. package/dist/cjs/types/signer.js +0 -3
  224. package/dist/cjs/types/signer.js.map +0 -1
  225. package/dist/cjs/utils.js +0 -173
  226. package/dist/cjs/utils.js.map +0 -1
@@ -1,9 +1,9 @@
1
1
  import type { AffinePoint } from '@noble/curves/abstract/weierstrass';
2
2
 
3
3
  import { Convert } from '@enbox/common';
4
- import { sha256 } from '@noble/hashes/sha256';
5
- import { secp256k1 } from '@noble/curves/secp256k1';
6
4
  import { numberToBytesBE } from '@noble/curves/abstract/utils';
5
+ import { secp256k1 } from '@noble/curves/secp256k1';
6
+ import { sha256 } from '@noble/hashes/sha256';
7
7
 
8
8
  import type { Jwk } from '../jose/jwk.js';
9
9
  import type { ComputePublicKeyParams, GetPublicKeyParams, SignParams, VerifyParams } from '../types/params-direct.js';
@@ -304,7 +304,7 @@ export class Secp256k1 {
304
304
  ComputePublicKeyParams
305
305
  ): Promise<Jwk> {
306
306
  // Convert the provided private key to a byte array.
307
- const privateKeyBytes = await Secp256k1.privateKeyToBytes({ privateKey: key });
307
+ const privateKeyBytes = await Secp256k1.privateKeyToBytes({ privateKey: key });
308
308
 
309
309
  // Get the elliptic curve point (x and y coordinates) for the provided private key.
310
310
  const point = await Secp256k1.getCurvePoint({ keyBytes: privateKeyBytes });
@@ -357,7 +357,7 @@ export class Secp256k1 {
357
357
  // into a single byte array.
358
358
  const compactSignature = signatureObject.toCompactRawBytes();
359
359
 
360
- return compactSignature;
360
+ return compactSignature;
361
361
  }
362
362
 
363
363
  /**
@@ -468,7 +468,7 @@ export class Secp256k1 {
468
468
  }
469
469
 
470
470
  // Remove the private key property ('d') and make a shallow copy of the provided key.
471
- let { d, ...publicKey } = key;
471
+ const { d, ...publicKey } = key;
472
472
 
473
473
  // If the key ID is undefined, set it to the JWK thumbprint.
474
474
  publicKey.kid ??= await computeJwkThumbprint({ jwk: publicKey });
@@ -732,7 +732,7 @@ export class Secp256k1 {
732
732
  // Check if points are on the Short Weierstrass curve.
733
733
  point.assertValidity();
734
734
 
735
- } catch(error: any) {
735
+ } catch {
736
736
  return false;
737
737
  }
738
738
 
@@ -1,9 +1,9 @@
1
1
  import type { AffinePoint } from '@noble/curves/abstract/weierstrass';
2
2
 
3
3
  import { Convert } from '@enbox/common';
4
- import { sha256 } from '@noble/hashes/sha256';
5
- import { secp256r1 } from '@noble/curves/p256';
6
4
  import { numberToBytesBE } from '@noble/curves/abstract/utils';
5
+ import { secp256r1 } from '@noble/curves/p256';
6
+ import { sha256 } from '@noble/hashes/sha256';
7
7
 
8
8
  import type { Jwk } from '../jose/jwk.js';
9
9
  import type { ComputePublicKeyParams, GetPublicKeyParams, SignParams, VerifyParams } from '../types/params-direct.js';
@@ -304,7 +304,7 @@ export class Secp256r1 {
304
304
  ComputePublicKeyParams
305
305
  ): Promise<Jwk> {
306
306
  // Convert the provided private key to a byte array.
307
- const privateKeyBytes = await Secp256r1.privateKeyToBytes({ privateKey: key });
307
+ const privateKeyBytes = await Secp256r1.privateKeyToBytes({ privateKey: key });
308
308
 
309
309
  // Get the elliptic curve point (x and y coordinates) for the provided private key.
310
310
  const point = await Secp256r1.getCurvePoint({ keyBytes: privateKeyBytes });
@@ -357,7 +357,7 @@ export class Secp256r1 {
357
357
  // into a single byte array.
358
358
  const compactSignature = signatureObject.toCompactRawBytes();
359
359
 
360
- return compactSignature;
360
+ return compactSignature;
361
361
  }
362
362
 
363
363
  /**
@@ -468,7 +468,7 @@ export class Secp256r1 {
468
468
  }
469
469
 
470
470
  // Remove the private key property ('d') and make a shallow copy of the provided key.
471
- let { d, ...publicKey } = key;
471
+ const { d, ...publicKey } = key;
472
472
 
473
473
  // If the key ID is undefined, set it to the JWK thumbprint.
474
474
  publicKey.kid ??= await computeJwkThumbprint({ jwk: publicKey });
@@ -732,7 +732,7 @@ export class Secp256r1 {
732
732
  // Check if points are on the Short Weierstrass curve.
733
733
  point.assertValidity();
734
734
 
735
- } catch(error: any) {
735
+ } catch {
736
736
  return false;
737
737
  }
738
738
 
@@ -79,7 +79,7 @@ export class X25519 {
79
79
  privateKeyBytes: Uint8Array;
80
80
  }): Promise<Jwk> {
81
81
  // Derive the public key from the private key.
82
- const publicKeyBytes = x25519.getPublicKey(privateKeyBytes);
82
+ const publicKeyBytes = x25519.getPublicKey(privateKeyBytes);
83
83
 
84
84
  // Construct the private key in JWK format.
85
85
  const privateKey: Jwk = {
@@ -168,7 +168,7 @@ export class X25519 {
168
168
  ComputePublicKeyParams
169
169
  ): Promise<Jwk> {
170
170
  // Convert the provided private key to a byte array.
171
- const privateKeyBytes = await X25519.privateKeyToBytes({ privateKey: key });
171
+ const privateKeyBytes = await X25519.privateKeyToBytes({ privateKey: key });
172
172
 
173
173
  // Derive the public key from the private key.
174
174
  const publicKeyBytes = x25519.getPublicKey(privateKeyBytes);
@@ -260,7 +260,7 @@ export class X25519 {
260
260
  }
261
261
 
262
262
  // Remove the private key property ('d') and make a shallow copy of the provided key.
263
- let { d, ...publicKey } = key;
263
+ const { d, ...publicKey } = key;
264
264
 
265
265
  // If the key ID is undefined, set it to the JWK thumbprint.
266
266
  publicKey.kid ??= await computeJwkThumbprint({ jwk: publicKey });
@@ -345,32 +345,25 @@ export class X25519 {
345
345
  }
346
346
 
347
347
  /**
348
- * Computes an RFC6090-compliant Elliptic Curve Diffie-Hellman (ECDH) shared secret
349
- * using secp256k1 private and public keys in JSON Web Key (JWK) format.
348
+ * Computes an X25519 Elliptic Curve Diffie-Hellman (ECDH) shared secret
349
+ * using X25519 private and public keys in JSON Web Key (JWK) format.
350
350
  *
351
351
  * @remarks
352
352
  * This method facilitates the ECDH key agreement protocol, which is a method of securely
353
353
  * deriving a shared secret between two parties based on their private and public keys.
354
354
  * It takes the private key of one party (privateKeyA) and the public key of another
355
- * party (publicKeyB) to compute a shared secret. The shared secret is derived from the
356
- * x-coordinate of the elliptic curve point resulting from the multiplication of the
357
- * public key with the private key.
358
- *
359
- * Note: When performing Elliptic Curve Diffie-Hellman (ECDH) key agreement,
360
- * the resulting shared secret is a point on the elliptic curve, which
361
- * consists of an x-coordinate and a y-coordinate. With a 256-bit curve like
362
- * secp256k1, each of these coordinates is 32 bytes (256 bits) long. However,
363
- * in the ECDH process, it's standard practice to use only the x-coordinate
364
- * of the shared secret point as the resulting shared key. This is because
365
- * the y-coordinate does not add to the entropy of the key, and both parties
366
- * can independently compute the x-coordinate. Consquently, this implementation
367
- * omits the y-coordinate for simplicity and standard compliance.
355
+ * party (publicKeyB) to compute a shared secret. The shared secret is the raw output
356
+ * of the X25519 function as defined in RFC 7748.
357
+ *
358
+ * Note: Unlike Weierstrass curves (e.g., secp256k1), X25519 is a Montgomery curve
359
+ * where the ECDH output is a single 32-byte scalar value, not an (x, y) point.
360
+ * The result is used directly as the shared secret.
368
361
  *
369
362
  * @example
370
363
  * ```ts
371
364
  * const privateKeyA = { ... }; // A Jwk object for party A
372
365
  * const publicKeyB = { ... }; // A PublicKeyJwk object for party B
373
- * const sharedSecret = await Secp256k1.sharedSecret({
366
+ * const sharedSecret = await X25519.sharedSecret({
374
367
  * privateKeyA,
375
368
  * publicKeyB
376
369
  * });
@@ -1,6 +1,6 @@
1
1
  import { Convert } from '@enbox/common';
2
- import { xchacha20poly1305 } from '@noble/ciphers/chacha';
3
2
  import { getWebcryptoSubtle } from '@noble/ciphers/webcrypto';
3
+ import { xchacha20poly1305 } from '@noble/ciphers/chacha';
4
4
 
5
5
  import type { Jwk } from '../jose/jwk.js';
6
6
 
@@ -147,7 +147,32 @@ export class XChaCha20Poly1305 {
147
147
  // Convert the private key from JWK format to bytes.
148
148
  const privateKeyBytes = await XChaCha20Poly1305.privateKeyToBytes({ privateKey: key });
149
149
 
150
- const xc20p = xchacha20poly1305(privateKeyBytes, nonce, additionalData);
150
+ return XChaCha20Poly1305.decryptRaw({ data, keyBytes: privateKeyBytes, nonce, additionalData });
151
+ }
152
+
153
+ /**
154
+ * Decrypts data using XChaCha20-Poly1305 with a raw byte array key.
155
+ *
156
+ * @remarks
157
+ * This is a lower-level method that accepts the key as a raw `Uint8Array` instead of a JWK.
158
+ * It is useful in scenarios where the key material is already in byte form (e.g., derived
159
+ * from ECDH + HKDF) and constructing a JWK would add unnecessary overhead.
160
+ *
161
+ * @param params - The parameters for the decryption operation.
162
+ * @param params.data - The encrypted data including the authentication tag.
163
+ * @param params.keyBytes - The 256-bit (32-byte) decryption key as a Uint8Array.
164
+ * @param params.nonce - The 24-byte nonce used during encryption.
165
+ * @param params.additionalData - Optional additional authenticated data.
166
+ *
167
+ * @returns A Promise that resolves to the decrypted plaintext as a Uint8Array.
168
+ */
169
+ public static async decryptRaw({ data, keyBytes, nonce, additionalData }: {
170
+ additionalData?: Uint8Array;
171
+ data: Uint8Array;
172
+ keyBytes: Uint8Array;
173
+ nonce: Uint8Array;
174
+ }): Promise<Uint8Array> {
175
+ const xc20p = xchacha20poly1305(keyBytes, nonce, additionalData);
151
176
  const plaintext = xc20p.decrypt(data);
152
177
 
153
178
  return plaintext;
@@ -186,7 +211,7 @@ export class XChaCha20Poly1305 {
186
211
  * @returns A Promise that resolves to a byte array containing the encrypted data and the
187
212
  * authentication tag.
188
213
  */
189
- public static async encrypt({ data, key, nonce, additionalData}: {
214
+ public static async encrypt({ data, key, nonce, additionalData }: {
190
215
  additionalData?: Uint8Array;
191
216
  data: Uint8Array;
192
217
  key: Jwk;
@@ -195,7 +220,35 @@ export class XChaCha20Poly1305 {
195
220
  // Convert the private key from JWK format to bytes.
196
221
  const privateKeyBytes = await XChaCha20Poly1305.privateKeyToBytes({ privateKey: key });
197
222
 
198
- const xc20p = xchacha20poly1305(privateKeyBytes, nonce, additionalData);
223
+ return XChaCha20Poly1305.encryptRaw({ data, keyBytes: privateKeyBytes, nonce, additionalData });
224
+ }
225
+
226
+ /**
227
+ * Encrypts data using XChaCha20-Poly1305 with a raw byte array key.
228
+ *
229
+ * @remarks
230
+ * This is a lower-level method that accepts the key as a raw `Uint8Array` instead of a JWK.
231
+ * It is useful in scenarios where the key material is already in byte form (e.g., derived
232
+ * from ECDH + HKDF) and constructing a JWK would add unnecessary overhead.
233
+ *
234
+ * The returned `Uint8Array` contains the ciphertext followed by the 16-byte Poly1305
235
+ * authentication tag.
236
+ *
237
+ * @param params - The parameters for the encryption operation.
238
+ * @param params.data - The plaintext data to encrypt.
239
+ * @param params.keyBytes - The 256-bit (32-byte) encryption key as a Uint8Array.
240
+ * @param params.nonce - A 24-byte nonce for the encryption process.
241
+ * @param params.additionalData - Optional additional authenticated data.
242
+ *
243
+ * @returns A Promise that resolves to the ciphertext + authentication tag as a Uint8Array.
244
+ */
245
+ public static async encryptRaw({ data, keyBytes, nonce, additionalData }: {
246
+ additionalData?: Uint8Array;
247
+ data: Uint8Array;
248
+ keyBytes: Uint8Array;
249
+ nonce: Uint8Array;
250
+ }): Promise<Uint8Array> {
251
+ const xc20p = xchacha20poly1305(keyBytes, nonce, additionalData);
199
252
  const ciphertext = xc20p.encrypt(data);
200
253
 
201
254
  return ciphertext;
@@ -1,6 +1,6 @@
1
1
  import { Convert } from '@enbox/common';
2
- import { xchacha20 } from '@noble/ciphers/chacha';
3
2
  import { getWebcryptoSubtle } from '@noble/ciphers/webcrypto';
3
+ import { xchacha20 } from '@noble/ciphers/chacha';
4
4
 
5
5
  import type { Jwk } from '../jose/jwk.js';
6
6
 
@@ -1,4 +1,4 @@
1
- import type { EnclosedEncryptParams, EnclosedDecryptParams } from './params-enclosed.js';
1
+ import type { EnclosedDecryptParams, EnclosedEncryptParams } from './params-enclosed.js';
2
2
 
3
3
  /**
4
4
  * The `Cipher` interface provides methods for encrypting and decrypting data.
@@ -1,18 +1,40 @@
1
+ import type { AsymmetricKeyConverter } from './key-converter.js';
2
+ import type { AsymmetricKeyGenerator } from './key-generator.js';
3
+ import type { Cipher } from './cipher.js';
1
4
  import type { Hasher } from './hasher.js';
2
- import type { Signer } from './signer.js';
5
+ import type { Jwk } from '../jose/jwk.js';
3
6
  import type { KeyIdentifier } from './identifier.js';
4
- import type { AsymmetricKeyGenerator } from './key-generator.js';
7
+ import type { KeyWrapper } from './key-wrapper.js';
8
+ import type { Signer } from './signer.js';
5
9
  import type {
6
- KmsSignParams,
10
+ BytesToPrivateKeyParams,
11
+ BytesToPublicKeyParams,
12
+ CipherParams,
13
+ DeriveKeyBytesParams,
14
+ DeriveKeyFromBytesParams,
15
+ DigestParams,
16
+ GenerateKeyParams,
17
+ GetPublicKeyParams,
18
+ PrivateKeyToBytesParams,
19
+ PublicKeyToBytesParams,
20
+ SignParams,
21
+ UnwrapKeyParams,
22
+ VerifyParams,
23
+ WrapKeyParams,
24
+ } from './params-direct.js';
25
+ import type { KeyBytesDeriver, SimpleKeyDeriver } from './key-deriver.js';
26
+ import type {
27
+ KmsCipherParams,
7
28
  KmsDigestParams,
8
- KmsVerifyParams,
9
- KmsGetKeyUriParams,
10
29
  KmsGenerateKeyParams,
30
+ KmsGetKeyUriParams,
11
31
  KmsGetPublicKeyParams,
32
+ KmsSignParams,
33
+ KmsVerifyParams,
12
34
  } from './params-kms.js';
13
35
 
14
36
  /**
15
- * The `CryptoApi` interface integrates key generation, hashing, and signing functionalities,
37
+ * The `DsaApi` interface integrates key generation, hashing, and signing functionalities,
16
38
  * designed for use with a Key Management System (KMS). It extends `AsymmetricKeyGenerator` for
17
39
  * generating asymmetric keys, `Hasher` for hash digest computations, and `Signer` for signing and
18
40
  * verifying operations.
@@ -32,25 +54,121 @@ import type {
32
54
  * identifier (e.g. JWK thumbprint, UUID generated by hosted KMS, etc.).
33
55
  * - Must support key generation, hashing, signing, and verifying operations.
34
56
  * - May be extended to support other cryptographic operations.
35
- * - Implementations of the `CryptoApi` interface can be passed as an argument to the public API
57
+ * - Implementations of the `DsaApi` interface can be passed as an argument to the public API
36
58
  * methods of Web5 libraries that involve key material (e.g., DID creation, VC signing, arbitrary
37
59
  * data signing/verification, etc.).
38
60
  */
39
- export interface CryptoApi<
61
+ export interface DsaApi<
40
62
  GenerateKeyInput = KmsGenerateKeyParams,
41
63
  GenerateKeyOutput = KeyIdentifier,
42
64
  GetPublicKeyInput = KmsGetPublicKeyParams,
43
65
  DigestInput = KmsDigestParams,
44
66
  SignInput = KmsSignParams,
45
67
  VerifyInput = KmsVerifyParams
46
- > extends AsymmetricKeyGenerator<GenerateKeyInput, GenerateKeyOutput, GetPublicKeyInput>,
68
+ > extends AsymmetricKeyGenerator<GenerateKeyInput, GenerateKeyOutput, GetPublicKeyInput>,
47
69
  Hasher<DigestInput>,
48
- Signer<SignInput, VerifyInput> {
70
+ Signer<SignInput, VerifyInput> {}
71
+
72
+ /**
73
+ * The `CryptoApi` interface extends {@link DsaApi} with encryption, key conversion,
74
+ * key derivation, and key wrapping capabilities.
75
+ *
76
+ * This is the full-featured cryptographic API used by agent-level code that needs direct-key
77
+ * cipher, key conversion, and key derivation operations beyond what the base `DsaApi` provides.
78
+ */
79
+ export interface CryptoApi<
80
+ GenerateKeyInput = GenerateKeyParams,
81
+ GenerateKeyOutput = Jwk,
82
+ GetPublicKeyInput = GetPublicKeyParams,
83
+ DigestInput = DigestParams,
84
+ SignInput = SignParams,
85
+ VerifyInput = VerifyParams,
86
+ EncryptInput = CipherParams,
87
+ DecryptInput = CipherParams,
88
+ BytesToPublicKeyInput = BytesToPublicKeyParams,
89
+ PublicKeyToBytesInput = PublicKeyToBytesParams,
90
+ BytesToPrivateKeyInput = BytesToPrivateKeyParams,
91
+ PrivateKeyToBytesInput = PrivateKeyToBytesParams,
92
+ DeriveKeyInput = DeriveKeyFromBytesParams,
93
+ DeriveKeyOutput = Jwk,
94
+ DeriveKeyBytesInput = DeriveKeyBytesParams,
95
+ DeriveKeyBytesOutput = Uint8Array,
96
+ WrapKeyInput = WrapKeyParams,
97
+ UnwrapKeyInput = UnwrapKeyParams
98
+ > extends
99
+ DsaApi<GenerateKeyInput, GenerateKeyOutput, GetPublicKeyInput, DigestInput, SignInput, VerifyInput>,
100
+ Cipher<EncryptInput, DecryptInput>,
101
+ AsymmetricKeyConverter<BytesToPublicKeyInput, PublicKeyToBytesInput, BytesToPrivateKeyInput, PrivateKeyToBytesInput>,
102
+ SimpleKeyDeriver<DeriveKeyInput, DeriveKeyOutput>,
103
+ KeyBytesDeriver<DeriveKeyBytesInput, DeriveKeyBytesOutput>,
104
+ KeyWrapper<WrapKeyInput, UnwrapKeyInput> {}
105
+
106
+ /** @deprecated Use {@link CryptoApi} instead. */
107
+ export type ExtendedCryptoApi<
108
+ GenerateKeyInput = GenerateKeyParams,
109
+ GenerateKeyOutput = Jwk,
110
+ GetPublicKeyInput = GetPublicKeyParams,
111
+ DigestInput = DigestParams,
112
+ SignInput = SignParams,
113
+ VerifyInput = VerifyParams,
114
+ EncryptInput = CipherParams,
115
+ DecryptInput = CipherParams,
116
+ BytesToPublicKeyInput = BytesToPublicKeyParams,
117
+ PublicKeyToBytesInput = PublicKeyToBytesParams,
118
+ BytesToPrivateKeyInput = BytesToPrivateKeyParams,
119
+ PrivateKeyToBytesInput = PrivateKeyToBytesParams,
120
+ DeriveKeyInput = DeriveKeyFromBytesParams,
121
+ DeriveKeyOutput = Jwk,
122
+ DeriveKeyBytesInput = DeriveKeyBytesParams,
123
+ DeriveKeyBytesOutput = Uint8Array,
124
+ WrapKeyInput = WrapKeyParams,
125
+ UnwrapKeyInput = UnwrapKeyParams
126
+ > = CryptoApi<
127
+ GenerateKeyInput, GenerateKeyOutput, GetPublicKeyInput, DigestInput, SignInput, VerifyInput,
128
+ EncryptInput, DecryptInput, BytesToPublicKeyInput, PublicKeyToBytesInput,
129
+ BytesToPrivateKeyInput, PrivateKeyToBytesInput, DeriveKeyInput, DeriveKeyOutput,
130
+ DeriveKeyBytesInput, DeriveKeyBytesOutput, WrapKeyInput, UnwrapKeyInput
131
+ >;
132
+
133
+ /**
134
+ * Parameters for configuring a {@link KeyManager} implementation.
135
+ */
136
+ export interface KeyManagerParams {
137
+ CipherInput?: unknown;
138
+ GenerateKeyInput?: unknown;
139
+ GenerateKeyOutput?: unknown;
140
+ GetPublicKeyInput?: unknown;
141
+ SignInput?: unknown;
142
+ VerifyInput?: unknown;
143
+ }
144
+
145
+ /**
146
+ * Default parameter types for {@link KeyManager}, using KMS-oriented types.
147
+ */
148
+ export interface DefaultKeyManagerParams {
149
+ CipherInput: KmsCipherParams;
150
+ GenerateKeyInput: KmsGenerateKeyParams;
151
+ GenerateKeyOutput: KeyIdentifier;
152
+ GetPublicKeyInput: KmsGetPublicKeyParams;
153
+ SignInput: KmsSignParams;
154
+ VerifyInput: KmsVerifyParams;
155
+ }
156
+
157
+ /**
158
+ * The `KeyManager` interface integrates key generation and signing capabilities.
159
+ *
160
+ * Concrete implementations of this interface are intended to be used as a Key Management System
161
+ * (KMS), which is responsible for generating and storing cryptographic keys.
162
+ */
163
+ export interface KeyManager<T extends KeyManagerParams = DefaultKeyManagerParams>
164
+ extends DsaApi<T['GenerateKeyInput'], T['GenerateKeyOutput'], T['GetPublicKeyInput'], KmsDigestParams, T['SignInput'], T['VerifyInput']> {
165
+
49
166
  /**
167
+ * Returns the Key URI for a given JWK.
50
168
  *
51
169
  * @param params - The parameters for getting the key URI.
52
170
  * @param params.key - The key to get the URI for.
53
171
  * @returns The key URI.
54
172
  */
55
173
  getKeyUri(params: KmsGetKeyUriParams): Promise<KeyIdentifier>;
56
- }
174
+ }
@@ -2,8 +2,16 @@ import type { Jwk } from '../jose/jwk.js';
2
2
 
3
3
  /**
4
4
  * `KeyConverter` interface for converting private keys between byte array and JWK formats.
5
+ *
6
+ * @typeParam BytesToPrivateKeyInput - The input type for `bytesToPrivateKey`. Defaults to
7
+ * `{ privateKeyBytes: Uint8Array }`.
8
+ * @typeParam PrivateKeyToBytesInput - The input type for `privateKeyToBytes`. Defaults to
9
+ * `{ privateKey: Jwk }`.
5
10
  */
6
- export interface KeyConverter {
11
+ export interface KeyConverter<
12
+ BytesToPrivateKeyInput = { privateKeyBytes: Uint8Array },
13
+ PrivateKeyToBytesInput = { privateKey: Jwk }
14
+ > {
7
15
 
8
16
  /**
9
17
  * Converts a private key from a byte array to JWK format.
@@ -13,7 +21,7 @@ export interface KeyConverter {
13
21
  *
14
22
  * @returns A Promise that resolves to the private key in JWK format.
15
23
  */
16
- bytesToPrivateKey(params: { privateKeyBytes: Uint8Array }): Promise<Jwk>;
24
+ bytesToPrivateKey(params: BytesToPrivateKeyInput): Promise<Jwk>;
17
25
 
18
26
  /**
19
27
  * Converts a private key from JWK format to a byte array.
@@ -23,14 +31,32 @@ export interface KeyConverter {
23
31
  *
24
32
  * @returns A Promise that resolves to the private key as a Uint8Array.
25
33
  */
26
- privateKeyToBytes(params: { privateKey: Jwk }): Promise<Uint8Array>;
34
+ privateKeyToBytes(params: PrivateKeyToBytesInput): Promise<Uint8Array>;
27
35
  }
28
36
 
29
37
  /**
30
- * `AsymmetricKeyConverter` interface extends {@link KeyConverter |`KeyConverter`}, adding support
38
+ * `AsymmetricKeyConverter` interface extends {@link KeyConverter | `KeyConverter`}, adding support
31
39
  * for public key conversions.
40
+ *
41
+ * When used with default type parameters, this interface includes all four conversion methods
42
+ * (bytes-to/from private key AND bytes-to/from public key). When used with explicit type
43
+ * parameters, both the private and public key conversion types can be customized.
44
+ *
45
+ * @typeParam BytesToPublicKeyInput - The input type for `bytesToPublicKey`. Defaults to
46
+ * `{ publicKeyBytes: Uint8Array }`.
47
+ * @typeParam PublicKeyToBytesInput - The input type for `publicKeyToBytes`. Defaults to
48
+ * `{ publicKey: Jwk }`.
49
+ * @typeParam BytesToPrivateKeyInput - The input type for `bytesToPrivateKey`. Defaults to
50
+ * `{ privateKeyBytes: Uint8Array }`.
51
+ * @typeParam PrivateKeyToBytesInput - The input type for `privateKeyToBytes`. Defaults to
52
+ * `{ privateKey: Jwk }`.
32
53
  */
33
- export interface AsymmetricKeyConverter extends KeyConverter {
54
+ export interface AsymmetricKeyConverter<
55
+ BytesToPublicKeyInput = { publicKeyBytes: Uint8Array },
56
+ PublicKeyToBytesInput = { publicKey: Jwk },
57
+ BytesToPrivateKeyInput = { privateKeyBytes: Uint8Array },
58
+ PrivateKeyToBytesInput = { privateKey: Jwk }
59
+ > extends KeyConverter<BytesToPrivateKeyInput, PrivateKeyToBytesInput> {
34
60
  /**
35
61
  * Converts a public key from a byte array to JWK format.
36
62
  *
@@ -39,7 +65,7 @@ export interface AsymmetricKeyConverter extends KeyConverter {
39
65
  *
40
66
  * @returns A Promise that resolves to the public key in JWK format.
41
67
  */
42
- bytesToPublicKey(params: { publicKeyBytes: Uint8Array }): Promise<Jwk>;
68
+ bytesToPublicKey(params: BytesToPublicKeyInput): Promise<Jwk>;
43
69
 
44
70
  /**
45
71
  * Converts a public key from JWK format to a byte array.
@@ -49,5 +75,5 @@ export interface AsymmetricKeyConverter extends KeyConverter {
49
75
  *
50
76
  * @returns A Promise that resolves to the public key as a Uint8Array.
51
77
  */
52
- publicKeyToBytes(params: { publicKey: Jwk }): Promise<Uint8Array>;
78
+ publicKeyToBytes(params: PublicKeyToBytesInput): Promise<Uint8Array>;
53
79
  }
@@ -40,4 +40,53 @@ export interface KeyDeriver<
40
40
  * @returns A Promise resolving to the derived key in the specified output format.
41
41
  */
42
42
  deriveKey(params: DeriveKeyInput): Promise<DeriveKeyOutput>;
43
+ }
44
+
45
+ /**
46
+ * The `SimpleKeyDeriver` interface provides a single `deriveKey()` method for key derivation,
47
+ * without the `deriveBits()` method that {@link KeyDeriver} includes.
48
+ *
49
+ * This is useful for implementations that only need key derivation (not raw bit derivation).
50
+ */
51
+ export interface SimpleKeyDeriver<
52
+ DeriveKeyInput,
53
+ DeriveKeyOutput,
54
+ > {
55
+ /**
56
+ * Derives a cryptographic key based on the provided input parameters.
57
+ *
58
+ * @param params - The parameters for the key derivation process.
59
+ *
60
+ * @returns A Promise resolving to the derived key in the specified output format.
61
+ */
62
+ deriveKey(params: DeriveKeyInput): Promise<DeriveKeyOutput>;
63
+ }
64
+
65
+ /**
66
+ * The `KeyBytesDeriver` interface provides a method for deriving a byte array using a key
67
+ * derivation algorithm.
68
+ *
69
+ * The `deriveKeyBytes()` method derives cryptographic bits from input data using the specified
70
+ * key derivation algorithm. This interface is designed to support various key derivation
71
+ * algorithms, accommodating different input and output types.
72
+ */
73
+ export interface KeyBytesDeriver<
74
+ DeriveKeyBytesInput,
75
+ DeriveKeyBytesOutput
76
+ > {
77
+ /**
78
+ * Generates a specified number of cryptographic bits from given input parameters.
79
+ *
80
+ * @remarks
81
+ * The `deriveKeyBytes()` method of the {@link KeyBytesDeriver | `KeyBytesDeriver`} interface is
82
+ * used to create cryptographic material such as initialization vectors or keys from various
83
+ * sources. The method takes in parameters specific to the chosen key derivation algorithm and
84
+ * outputs a promise that resolves to a `Uint8Array` containing the derived bits.
85
+ *
86
+ * @param params - The parameters for the key derivation process, specific to the chosen
87
+ * algorithm.
88
+ *
89
+ * @returns A Promise resolving to the derived bits in the specified format.
90
+ */
91
+ deriveKeyBytes(params: DeriveKeyBytesInput): Promise<DeriveKeyBytesOutput>;
43
92
  }
@@ -39,4 +39,44 @@ export interface KeyImporterExporter<
39
39
  * @returns A Promise resolving to the key identifier of the imported key.
40
40
  */
41
41
  importKey(params: ImportKeyInput): Promise<ImportKeyOutput>;
42
+ }
43
+
44
+ /**
45
+ * The `KeyExporter` interface provides a method for exporting cryptographic keys.
46
+ */
47
+ export interface KeyExporter<ExportKeyInput, ExportKeyOutput = Jwk> {
48
+ /**
49
+ * Exports a cryptographic key to an external JWK object.
50
+ *
51
+ * @param params - The parameters for the key export operation.
52
+ *
53
+ * @returns A Promise resolving to the exported key in JWK format.
54
+ */
55
+ exportKey(params: ExportKeyInput): Promise<ExportKeyOutput>;
56
+ }
57
+
58
+ /**
59
+ * The `KeyImporter` interface provides a method for importing cryptographic keys.
60
+ */
61
+ export interface KeyImporter<ImportKeyInput, ImportKeyOutput = void> {
62
+ /**
63
+ * Imports an external key in JWK format.
64
+ *
65
+ * @param params - The parameters for the key import operation.
66
+ *
67
+ * @returns A Promise resolving to the key identifier of the imported key.
68
+ */
69
+ importKey(params: ImportKeyInput): Promise<ImportKeyOutput>;
70
+ }
71
+
72
+ /**
73
+ * The `KeyDeleter` interface provides a method for deleting cryptographic keys.
74
+ */
75
+ export interface KeyDeleter<DeleteKeyInput> {
76
+ /**
77
+ * Deletes a cryptographic key from the key store.
78
+ *
79
+ * @param params - The parameters for the key deletion operation.
80
+ */
81
+ deleteKey(params: DeleteKeyInput): Promise<void>;
42
82
  }