@enbox/crypto 0.0.1 → 0.0.3

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 (182) hide show
  1. package/README.md +34 -102
  2. package/dist/browser.js +6 -10
  3. package/dist/browser.js.map +4 -4
  4. package/dist/browser.mjs +6 -10
  5. package/dist/browser.mjs.map +4 -4
  6. package/dist/esm/algorithms/aes-gcm.js +1 -1
  7. package/dist/esm/algorithms/aes-gcm.js.map +1 -1
  8. package/dist/esm/algorithms/ecdsa.js +9 -5
  9. package/dist/esm/algorithms/ecdsa.js.map +1 -1
  10. package/dist/esm/algorithms/eddsa.js +9 -5
  11. package/dist/esm/algorithms/eddsa.js.map +1 -1
  12. package/dist/esm/algorithms/sha-2.js +1 -1
  13. package/dist/esm/algorithms/sha-2.js.map +1 -1
  14. package/dist/esm/crypto-error.js +41 -0
  15. package/dist/esm/crypto-error.js.map +1 -0
  16. package/dist/esm/index.js +3 -0
  17. package/dist/esm/index.js.map +1 -1
  18. package/dist/esm/jose/jwk.js +52 -26
  19. package/dist/esm/jose/jwk.js.map +1 -1
  20. package/dist/esm/local-key-manager.js +3 -2
  21. package/dist/esm/local-key-manager.js.map +1 -1
  22. package/dist/esm/primitives/aes-ctr.js.map +1 -1
  23. package/dist/esm/primitives/aes-gcm.js.map +1 -1
  24. package/dist/esm/primitives/aes-kw.js +246 -0
  25. package/dist/esm/primitives/aes-kw.js.map +1 -0
  26. package/dist/esm/primitives/concat-kdf.js +1 -1
  27. package/dist/esm/primitives/concat-kdf.js.map +1 -1
  28. package/dist/esm/primitives/ed25519.js +3 -3
  29. package/dist/esm/primitives/ed25519.js.map +1 -1
  30. package/dist/esm/primitives/hkdf.js +79 -0
  31. package/dist/esm/primitives/hkdf.js.map +1 -0
  32. package/dist/esm/primitives/pbkdf2.js +49 -0
  33. package/dist/esm/primitives/pbkdf2.js.map +1 -1
  34. package/dist/esm/primitives/secp256k1.js +4 -4
  35. package/dist/esm/primitives/secp256k1.js.map +1 -1
  36. package/dist/esm/primitives/secp256r1.js +4 -4
  37. package/dist/esm/primitives/secp256r1.js.map +1 -1
  38. package/dist/esm/primitives/x25519.js +1 -1
  39. package/dist/esm/primitives/x25519.js.map +1 -1
  40. package/dist/esm/primitives/xchacha20-poly1305.js +48 -3
  41. package/dist/esm/primitives/xchacha20-poly1305.js.map +1 -1
  42. package/dist/esm/primitives/xchacha20.js +1 -1
  43. package/dist/esm/primitives/xchacha20.js.map +1 -1
  44. package/dist/esm/utils.js.map +1 -1
  45. package/dist/types/algorithms/aes-ctr.d.ts +1 -1
  46. package/dist/types/algorithms/aes-ctr.d.ts.map +1 -1
  47. package/dist/types/algorithms/aes-gcm.d.ts +2 -2
  48. package/dist/types/algorithms/aes-gcm.d.ts.map +1 -1
  49. package/dist/types/algorithms/ecdsa.d.ts +1 -1
  50. package/dist/types/algorithms/ecdsa.d.ts.map +1 -1
  51. package/dist/types/algorithms/eddsa.d.ts +2 -2
  52. package/dist/types/algorithms/eddsa.d.ts.map +1 -1
  53. package/dist/types/algorithms/sha-2.d.ts +1 -1
  54. package/dist/types/algorithms/sha-2.d.ts.map +1 -1
  55. package/dist/types/crypto-error.d.ts +29 -0
  56. package/dist/types/crypto-error.d.ts.map +1 -0
  57. package/dist/types/index.d.ts +3 -0
  58. package/dist/types/index.d.ts.map +1 -1
  59. package/dist/types/jose/jwk.d.ts.map +1 -1
  60. package/dist/types/local-key-manager.d.ts +3 -3
  61. package/dist/types/local-key-manager.d.ts.map +1 -1
  62. package/dist/types/primitives/aes-kw.d.ts +103 -0
  63. package/dist/types/primitives/aes-kw.d.ts.map +1 -0
  64. package/dist/types/primitives/concat-kdf.d.ts +1 -1
  65. package/dist/types/primitives/concat-kdf.d.ts.map +1 -1
  66. package/dist/types/primitives/hkdf.d.ts +90 -0
  67. package/dist/types/primitives/hkdf.d.ts.map +1 -0
  68. package/dist/types/primitives/pbkdf2.d.ts +58 -0
  69. package/dist/types/primitives/pbkdf2.d.ts.map +1 -1
  70. package/dist/types/primitives/xchacha20-poly1305.d.ts +47 -0
  71. package/dist/types/primitives/xchacha20-poly1305.d.ts.map +1 -1
  72. package/dist/types/types/cipher.d.ts +1 -1
  73. package/dist/types/types/crypto-api.d.ts +3 -3
  74. package/dist/types/types/crypto-api.d.ts.map +1 -1
  75. package/dist/types/types/params-direct.d.ts +79 -1
  76. package/dist/types/types/params-direct.d.ts.map +1 -1
  77. package/dist/utils.js.map +2 -2
  78. package/package.json +21 -34
  79. package/src/algorithms/aes-ctr.ts +1 -1
  80. package/src/algorithms/aes-gcm.ts +3 -2
  81. package/src/algorithms/ecdsa.ts +13 -7
  82. package/src/algorithms/eddsa.ts +9 -9
  83. package/src/algorithms/sha-2.ts +2 -2
  84. package/src/crypto-error.ts +45 -0
  85. package/src/index.ts +3 -0
  86. package/src/jose/jwk.ts +32 -32
  87. package/src/local-key-manager.ts +14 -13
  88. package/src/primitives/aes-ctr.ts +1 -1
  89. package/src/primitives/aes-gcm.ts +5 -5
  90. package/src/primitives/aes-kw.ts +269 -0
  91. package/src/primitives/concat-kdf.ts +4 -2
  92. package/src/primitives/ed25519.ts +6 -6
  93. package/src/primitives/hkdf.ts +121 -0
  94. package/src/primitives/pbkdf2.ts +91 -0
  95. package/src/primitives/secp256k1.ts +6 -6
  96. package/src/primitives/secp256r1.ts +6 -6
  97. package/src/primitives/x25519.ts +3 -3
  98. package/src/primitives/xchacha20-poly1305.ts +57 -4
  99. package/src/primitives/xchacha20.ts +1 -1
  100. package/src/types/cipher.ts +1 -1
  101. package/src/types/crypto-api.ts +5 -5
  102. package/src/types/params-direct.ts +97 -1
  103. package/src/utils.ts +2 -2
  104. package/dist/cjs/algorithms/aes-ctr.js +0 -188
  105. package/dist/cjs/algorithms/aes-ctr.js.map +0 -1
  106. package/dist/cjs/algorithms/aes-gcm.js +0 -196
  107. package/dist/cjs/algorithms/aes-gcm.js.map +0 -1
  108. package/dist/cjs/algorithms/crypto-algorithm.js +0 -13
  109. package/dist/cjs/algorithms/crypto-algorithm.js.map +0 -1
  110. package/dist/cjs/algorithms/ecdsa.js +0 -352
  111. package/dist/cjs/algorithms/ecdsa.js.map +0 -1
  112. package/dist/cjs/algorithms/eddsa.js +0 -325
  113. package/dist/cjs/algorithms/eddsa.js.map +0 -1
  114. package/dist/cjs/algorithms/sha-2.js +0 -119
  115. package/dist/cjs/algorithms/sha-2.js.map +0 -1
  116. package/dist/cjs/index.js +0 -41
  117. package/dist/cjs/index.js.map +0 -1
  118. package/dist/cjs/jose/jwe.js +0 -3
  119. package/dist/cjs/jose/jwe.js.map +0 -1
  120. package/dist/cjs/jose/jwk.js +0 -278
  121. package/dist/cjs/jose/jwk.js.map +0 -1
  122. package/dist/cjs/jose/jws.js +0 -3
  123. package/dist/cjs/jose/jws.js.map +0 -1
  124. package/dist/cjs/jose/jwt.js +0 -3
  125. package/dist/cjs/jose/jwt.js.map +0 -1
  126. package/dist/cjs/jose/utils.js +0 -60
  127. package/dist/cjs/jose/utils.js.map +0 -1
  128. package/dist/cjs/local-key-manager.js +0 -521
  129. package/dist/cjs/local-key-manager.js.map +0 -1
  130. package/dist/cjs/package.json +0 -1
  131. package/dist/cjs/primitives/aes-ctr.js +0 -398
  132. package/dist/cjs/primitives/aes-ctr.js.map +0 -1
  133. package/dist/cjs/primitives/aes-gcm.js +0 -425
  134. package/dist/cjs/primitives/aes-gcm.js.map +0 -1
  135. package/dist/cjs/primitives/concat-kdf.js +0 -215
  136. package/dist/cjs/primitives/concat-kdf.js.map +0 -1
  137. package/dist/cjs/primitives/ed25519.js +0 -651
  138. package/dist/cjs/primitives/ed25519.js.map +0 -1
  139. package/dist/cjs/primitives/pbkdf2.js +0 -120
  140. package/dist/cjs/primitives/pbkdf2.js.map +0 -1
  141. package/dist/cjs/primitives/secp256k1.js +0 -958
  142. package/dist/cjs/primitives/secp256k1.js.map +0 -1
  143. package/dist/cjs/primitives/secp256r1.js +0 -959
  144. package/dist/cjs/primitives/secp256r1.js.map +0 -1
  145. package/dist/cjs/primitives/sha256.js +0 -93
  146. package/dist/cjs/primitives/sha256.js.map +0 -1
  147. package/dist/cjs/primitives/x25519.js +0 -498
  148. package/dist/cjs/primitives/x25519.js.map +0 -1
  149. package/dist/cjs/primitives/xchacha20-poly1305.js +0 -340
  150. package/dist/cjs/primitives/xchacha20-poly1305.js.map +0 -1
  151. package/dist/cjs/primitives/xchacha20.js +0 -316
  152. package/dist/cjs/primitives/xchacha20.js.map +0 -1
  153. package/dist/cjs/types/cipher.js +0 -3
  154. package/dist/cjs/types/cipher.js.map +0 -1
  155. package/dist/cjs/types/crypto-api.js +0 -3
  156. package/dist/cjs/types/crypto-api.js.map +0 -1
  157. package/dist/cjs/types/hasher.js +0 -3
  158. package/dist/cjs/types/hasher.js.map +0 -1
  159. package/dist/cjs/types/identifier.js +0 -3
  160. package/dist/cjs/types/identifier.js.map +0 -1
  161. package/dist/cjs/types/key-compressor.js +0 -3
  162. package/dist/cjs/types/key-compressor.js.map +0 -1
  163. package/dist/cjs/types/key-converter.js +0 -3
  164. package/dist/cjs/types/key-converter.js.map +0 -1
  165. package/dist/cjs/types/key-deriver.js +0 -3
  166. package/dist/cjs/types/key-deriver.js.map +0 -1
  167. package/dist/cjs/types/key-generator.js +0 -3
  168. package/dist/cjs/types/key-generator.js.map +0 -1
  169. package/dist/cjs/types/key-io.js +0 -3
  170. package/dist/cjs/types/key-io.js.map +0 -1
  171. package/dist/cjs/types/key-wrapper.js +0 -3
  172. package/dist/cjs/types/key-wrapper.js.map +0 -1
  173. package/dist/cjs/types/params-direct.js +0 -3
  174. package/dist/cjs/types/params-direct.js.map +0 -1
  175. package/dist/cjs/types/params-enclosed.js +0 -3
  176. package/dist/cjs/types/params-enclosed.js.map +0 -1
  177. package/dist/cjs/types/params-kms.js +0 -3
  178. package/dist/cjs/types/params-kms.js.map +0 -1
  179. package/dist/cjs/types/signer.js +0 -3
  180. package/dist/cjs/types/signer.js.map +0 -1
  181. package/dist/cjs/utils.js +0 -173
  182. package/dist/cjs/utils.js.map +0 -1
@@ -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,14 +1,14 @@
1
+ import type { AsymmetricKeyGenerator } from './key-generator.js';
1
2
  import type { Hasher } from './hasher.js';
2
- import type { Signer } from './signer.js';
3
3
  import type { KeyIdentifier } from './identifier.js';
4
- import type { AsymmetricKeyGenerator } from './key-generator.js';
4
+ import type { Signer } from './signer.js';
5
5
  import type {
6
- KmsSignParams,
7
6
  KmsDigestParams,
8
- KmsVerifyParams,
9
- KmsGetKeyUriParams,
10
7
  KmsGenerateKeyParams,
8
+ KmsGetKeyUriParams,
11
9
  KmsGetPublicKeyParams,
10
+ KmsSignParams,
11
+ KmsVerifyParams,
12
12
  } from './params-kms.js';
13
13
 
14
14
  /**
@@ -1,11 +1,49 @@
1
- import type { Jwk } from '../jose/jwk.js';
2
1
  import type { AlgorithmIdentifier } from './identifier.js';
2
+ import type { Jwk } from '../jose/jwk.js';
3
+
4
+ /**
5
+ * Parameters for converting raw private key bytes to a JWK.
6
+ */
7
+ export interface BytesToPrivateKeyParams {
8
+ /** The algorithm identifier. */
9
+ algorithm: AlgorithmIdentifier;
10
+
11
+ /** The raw private key bytes. */
12
+ privateKeyBytes: Uint8Array;
13
+ }
14
+
15
+ /**
16
+ * Parameters for converting raw public key bytes to a JWK.
17
+ */
18
+ export interface BytesToPublicKeyParams {
19
+ /** The algorithm identifier. */
20
+ algorithm: AlgorithmIdentifier;
21
+
22
+ /** The raw public key bytes. */
23
+ publicKeyBytes: Uint8Array;
24
+ }
3
25
 
4
26
  /**
5
27
  * Parameters for computing a public key.
6
28
  */
7
29
  export interface ComputePublicKeyParams extends GetPublicKeyParams { }
8
30
 
31
+ /**
32
+ * Parameters for encryption and decryption operations.
33
+ *
34
+ * Intended for use with a Key Management System.
35
+ */
36
+ export interface CipherParams {
37
+ /** A {@link Jwk} containing the key to be used for encryption or decryption. */
38
+ key: Jwk;
39
+
40
+ /** Data to be encrypted or decrypted. */
41
+ data: Uint8Array;
42
+
43
+ /** Additional algorithm-specific parameters for encryption or decryption. */
44
+ [key: string]: unknown;
45
+ }
46
+
9
47
  /**
10
48
  * Parameters for decrypting data.
11
49
  */
@@ -42,6 +80,17 @@ export interface DeriveKeyParams {
42
80
  derivedKeyParams: unknown
43
81
  }
44
82
 
83
+ /**
84
+ * Parameters for derivation of cryptographic byte arrays.
85
+ */
86
+ export interface DeriveKeyBytesParams {
87
+ /** The base key to be used for derivation as a byte array. */
88
+ baseKeyBytes: Uint8Array;
89
+
90
+ /** The desired length of the derived key in bits. */
91
+ length: number;
92
+ }
93
+
45
94
  /**
46
95
  * Parameters for computing a hash digest.
47
96
  */
@@ -91,6 +140,39 @@ export interface SignParams {
91
140
  data: Uint8Array;
92
141
  }
93
142
 
143
+ /**
144
+ * Parameters for converting a private key JWK to raw bytes.
145
+ */
146
+ export interface PrivateKeyToBytesParams {
147
+ /** The private key in JWK format. */
148
+ privateKey: Jwk;
149
+ }
150
+
151
+ /**
152
+ * Parameters for converting a public key JWK to raw bytes.
153
+ */
154
+ export interface PublicKeyToBytesParams {
155
+ /** The public key in JWK format. */
156
+ publicKey: Jwk;
157
+ }
158
+
159
+ /**
160
+ * Parameters for unwrapping a key.
161
+ */
162
+ export interface UnwrapKeyParams {
163
+ /** A {@link Jwk} containing the key used to decrypt the unwrapped key. */
164
+ decryptionKey: Jwk;
165
+
166
+ /** The wrapped private key as a byte array. */
167
+ wrappedKeyBytes: Uint8Array;
168
+
169
+ /** The algorithm identifier of the key encrypted in `wrappedKeyBytes`. */
170
+ wrappedKeyAlgorithm: string;
171
+
172
+ /** An object defining the algorithm-specific parameters for decrypting the `wrappedKeyBytes`. */
173
+ decryptParams?: unknown;
174
+ }
175
+
94
176
  /**
95
177
  * Parameters for verifying a signature.
96
178
  */
@@ -103,4 +185,18 @@ export interface VerifyParams {
103
185
 
104
186
  /** The data associated with the signature. */
105
187
  data: Uint8Array;
188
+ }
189
+
190
+ /**
191
+ * Parameters for wrapping a key.
192
+ */
193
+ export interface WrapKeyParams {
194
+ /** A {@link Jwk} containing the key used to encrypt the unwrapped key. */
195
+ encryptionKey: Jwk;
196
+
197
+ /** A {@link Jwk} containing the private key to be wrapped. */
198
+ unwrappedKey: Jwk;
199
+
200
+ /** An object defining the algorithm-specific parameters for encrypting the `unwrappedKey`. */
201
+ encryptParams?: unknown;
106
202
  }
package/src/utils.ts CHANGED
@@ -158,7 +158,7 @@ export class CryptoUtils {
158
158
  const rejectionRange = Math.pow(10, length);
159
159
  do {
160
160
  // Adjust the byte generation based on length.
161
- const randomBuffer = CryptoUtils.randomBytes(Math.ceil(length / 2) ); // 2 digits per byte.
161
+ const randomBuffer = CryptoUtils.randomBytes(Math.ceil(length / 2) ); // 2 digits per byte.
162
162
  const view = new DataView(randomBuffer.buffer);
163
163
  // Convert the buffer to integer and take modulus based on length.
164
164
  pin = view.getUint16(0, false) % rejectionRange;
@@ -172,7 +172,7 @@ export class CryptoUtils {
172
172
  const view = new DataView(randomBuffer.buffer);
173
173
  // Transform bytes to number (big endian).
174
174
  pin = view.getUint32(0, false) % rejectionRange;
175
- } while (pin > max); // Reject if the number is outside the desired range.
175
+ } while (pin > max); // Reject if the number is outside the desired range.
176
176
  }
177
177
 
178
178
  // Pad the PIN with leading zeros to the desired length.
@@ -1,188 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
- return new (P || (P = Promise))(function (resolve, reject) {
20
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
- step((generator = generator.apply(thisArg, _arguments || [])).next());
24
- });
25
- };
26
- var __generator = (this && this.__generator) || function (thisArg, body) {
27
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
- function verb(n) { return function (v) { return step([n, v]); }; }
30
- function step(op) {
31
- if (f) throw new TypeError("Generator is already executing.");
32
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
- if (y = 0, t) op = [op[0] & 2, t.value];
35
- switch (op[0]) {
36
- case 0: case 1: t = op; break;
37
- case 4: _.label++; return { value: op[1], done: false };
38
- case 5: _.label++; y = op[1]; op = [0]; continue;
39
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
- default:
41
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
- if (t[2]) _.ops.pop();
46
- _.trys.pop(); continue;
47
- }
48
- op = body.call(thisArg, _);
49
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
- }
52
- };
53
- Object.defineProperty(exports, "__esModule", { value: true });
54
- exports.AesCtrAlgorithm = void 0;
55
- var aes_ctr_js_1 = require("../primitives/aes-ctr.js");
56
- var crypto_algorithm_js_1 = require("./crypto-algorithm.js");
57
- /**
58
- * The `AesCtrAlgorithm` class provides a concrete implementation for cryptographic operations using
59
- * the AES algorithm in Counter (CTR) mode. This class implements both {@link Cipher | `Cipher`} and
60
- * { @link KeyGenerator | `KeyGenerator`} interfaces, providing key generation, encryption, and
61
- * decryption features.
62
- *
63
- * This class is typically accessed through implementations that extend the
64
- * {@link CryptoApi | `CryptoApi`} interface.
65
- */
66
- var AesCtrAlgorithm = /** @class */ (function (_super) {
67
- __extends(AesCtrAlgorithm, _super);
68
- function AesCtrAlgorithm() {
69
- return _super !== null && _super.apply(this, arguments) || this;
70
- }
71
- /**
72
- * Decrypts the provided data using AES-CTR.
73
- *
74
- * @remarks
75
- * This method performs AES-CTR decryption on the given encrypted data using the specified key.
76
- * Similar to the encryption process, it requires an initial counter block and the length
77
- * of the counter block, along with the encrypted data and the decryption key. The method
78
- * returns the decrypted data as a Uint8Array.
79
- *
80
- * @example
81
- * ```ts
82
- * const aesCtr = new AesCtrAlgorithm();
83
- * const encryptedData = new Uint8Array([...]); // Encrypted data
84
- * const counter = new Uint8Array(16); // 16-byte (128-bit) counter block used during encryption
85
- * const key = { ... }; // A Jwk object representing the same AES key used for encryption
86
- * const decryptedData = await aesCtr.decrypt({
87
- * data: encryptedData,
88
- * counter,
89
- * key,
90
- * length: 128 // Length of the counter in bits
91
- * });
92
- * ```
93
- *
94
- * @param params - The parameters for the decryption operation.
95
- *
96
- * @returns A Promise that resolves to the decrypted data as a Uint8Array.
97
- */
98
- AesCtrAlgorithm.prototype.decrypt = function (params) {
99
- return __awaiter(this, void 0, void 0, function () {
100
- var plaintext;
101
- return __generator(this, function (_a) {
102
- plaintext = aes_ctr_js_1.AesCtr.decrypt(params);
103
- return [2 /*return*/, plaintext];
104
- });
105
- });
106
- };
107
- /**
108
- * Encrypts the provided data using AES-CTR.
109
- *
110
- * @remarks
111
- * This method performs AES-CTR encryption on the given data using the specified key.
112
- * It requires the initial counter block and the length of the counter block, alongside
113
- * the data and key. The method is designed to work asynchronously and returns the
114
- * encrypted data as a Uint8Array.
115
- *
116
- * @example
117
- * ```ts
118
- * const aesCtr = new AesCtrAlgorithm();
119
- * const data = new TextEncoder().encode('Messsage');
120
- * const counter = new Uint8Array(16); // 16-byte (128-bit) counter block
121
- * const key = { ... }; // A Jwk object representing an AES key
122
- * const encryptedData = await aesCtr.encrypt({
123
- * data,
124
- * counter,
125
- * key,
126
- * length: 128 // Length of the counter in bits
127
- * });
128
- * ```
129
- *
130
- * @param params - The parameters for the encryption operation.
131
- *
132
- * @returns A Promise that resolves to the encrypted data as a Uint8Array.
133
- */
134
- AesCtrAlgorithm.prototype.encrypt = function (params) {
135
- return __awaiter(this, void 0, void 0, function () {
136
- var ciphertext;
137
- return __generator(this, function (_a) {
138
- ciphertext = aes_ctr_js_1.AesCtr.encrypt(params);
139
- return [2 /*return*/, ciphertext];
140
- });
141
- });
142
- };
143
- /**
144
- * Generates a symmetric key for AES in Counter (CTR) mode in JSON Web Key (JWK) format.
145
- *
146
- * @remarks
147
- * This method generates a symmetric AES key for use in CTR mode, based on the specified
148
- * `algorithm` parameter which determines the key length. It uses cryptographically secure random
149
- * number generation to ensure the uniqueness and security of the key. The key is returned in JWK
150
- * format.
151
- *
152
- * The generated key includes the following components:
153
- * - `kty`: Key Type, set to 'oct' for Octet Sequence.
154
- * - `k`: The symmetric key component, base64url-encoded.
155
- * - `kid`: Key ID, generated based on the JWK thumbprint.
156
- *
157
- * @example
158
- * ```ts
159
- * const aesCtr = new AesCtrAlgorithm();
160
- * const privateKey = await aesCtr.generateKey({ algorithm: 'A256CTR' });
161
- * ```
162
- *
163
- * @param params - The parameters for the key generation.
164
- *
165
- * @returns A Promise that resolves to the generated symmetric key in JWK format.
166
- */
167
- AesCtrAlgorithm.prototype.generateKey = function (_a) {
168
- return __awaiter(this, arguments, void 0, function (_b) {
169
- var length, privateKey;
170
- var algorithm = _b.algorithm;
171
- return __generator(this, function (_c) {
172
- switch (_c.label) {
173
- case 0:
174
- length = { A128CTR: 128, A192CTR: 192, A256CTR: 256 }[algorithm];
175
- return [4 /*yield*/, aes_ctr_js_1.AesCtr.generateKey({ length: length })];
176
- case 1:
177
- privateKey = _c.sent();
178
- // Set the `alg` property based on the specified algorithm.
179
- privateKey.alg = algorithm;
180
- return [2 /*return*/, privateKey];
181
- }
182
- });
183
- });
184
- };
185
- return AesCtrAlgorithm;
186
- }(crypto_algorithm_js_1.CryptoAlgorithm));
187
- exports.AesCtrAlgorithm = AesCtrAlgorithm;
188
- //# sourceMappingURL=aes-ctr.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"aes-ctr.js","sourceRoot":"","sources":["../../../src/algorithms/aes-ctr.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKA,uDAAkD;AAClD,6DAAwD;AA4BxD;;;;;;;;GAQG;AACH;IAAqC,mCAAe;IAApD;;IAgHA,CAAC;IA5GC;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACU,iCAAO,GAApB,UAAqB,MACS;;;;gBAEtB,SAAS,GAAG,mBAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;gBAEzC,sBAAO,SAAS,EAAC;;;KAClB;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;OA0BG;IACU,iCAAO,GAApB,UAAqB,MACS;;;;gBAEtB,UAAU,GAAG,mBAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;gBAE1C,sBAAO,UAAU,EAAC;;;KACnB;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACU,qCAAW,GAAxB;4DAAyB,EACA;;gBADE,SAAS,eAAA;;;;wBAI5B,MAAM,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,SAAS,CAAoB,CAAC;wBAGvE,qBAAM,mBAAM,CAAC,WAAW,CAAC,EAAE,MAAM,QAAA,EAAE,CAAC,EAAA;;wBAAjD,UAAU,GAAG,SAAoC;wBAEvD,2DAA2D;wBAC3D,UAAU,CAAC,GAAG,GAAG,SAAS,CAAC;wBAE3B,sBAAO,UAAU,EAAC;;;;KACnB;IACH,sBAAC;AAAD,CAAC,AAhHD,CAAqC,qCAAe,GAgHnD;AAhHY,0CAAe"}
@@ -1,196 +0,0 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
- return new (P || (P = Promise))(function (resolve, reject) {
20
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
21
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
22
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
23
- step((generator = generator.apply(thisArg, _arguments || [])).next());
24
- });
25
- };
26
- var __generator = (this && this.__generator) || function (thisArg, body) {
27
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
- function verb(n) { return function (v) { return step([n, v]); }; }
30
- function step(op) {
31
- if (f) throw new TypeError("Generator is already executing.");
32
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
33
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
- if (y = 0, t) op = [op[0] & 2, t.value];
35
- switch (op[0]) {
36
- case 0: case 1: t = op; break;
37
- case 4: _.label++; return { value: op[1], done: false };
38
- case 5: _.label++; y = op[1]; op = [0]; continue;
39
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
- default:
41
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
- if (t[2]) _.ops.pop();
46
- _.trys.pop(); continue;
47
- }
48
- op = body.call(thisArg, _);
49
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
- }
52
- };
53
- Object.defineProperty(exports, "__esModule", { value: true });
54
- exports.AesGcmAlgorithm = void 0;
55
- var crypto_algorithm_js_1 = require("./crypto-algorithm.js");
56
- var aes_gcm_js_1 = require("../primitives/aes-gcm.js");
57
- /**
58
- * The `AesGcmAlgorithm` class provides a concrete implementation for cryptographic operations using
59
- * the AES algorithm in Galois/Counter Mode (GCM). This class implements both
60
- * {@link Cipher | `Cipher`} and { @link KeyGenerator | `KeyGenerator`} interfaces, providing
61
- * key generation, encryption, and decryption features.
62
- *
63
- * This class is typically accessed through implementations that extend the
64
- * {@link CryptoApi | `CryptoApi`} interface.
65
- */
66
- var AesGcmAlgorithm = /** @class */ (function (_super) {
67
- __extends(AesGcmAlgorithm, _super);
68
- function AesGcmAlgorithm() {
69
- return _super !== null && _super.apply(this, arguments) || this;
70
- }
71
- /**
72
- * Decrypts the provided data using AES-GCM.
73
- *
74
- * @remarks
75
- * This method performs AES-GCM decryption on the given encrypted data using the specified key.
76
- * It requires an initialization vector (IV), the encrypted data along with the decryption key,
77
- * and optionally, additional authenticated data (AAD). The method returns the decrypted data as a
78
- * Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
79
- * tag used when encrypting the data. If not specified, the default tag length of 128 bits is
80
- * used.
81
- *
82
- * @example
83
- * ```ts
84
- * const aesGcm = new AesGcmAlgorithm();
85
- * const encryptedData = new Uint8Array([...]); // Encrypted data
86
- * const iv = new Uint8Array([...]); // Initialization vector used during encryption
87
- * const additionalData = new Uint8Array([...]); // Optional additional authenticated data
88
- * const key = { ... }; // A Jwk object representing the AES key
89
- * const decryptedData = await aesGcm.decrypt({
90
- * data: encryptedData,
91
- * iv,
92
- * additionalData,
93
- * key,
94
- * tagLength: 128 // Optional tag length in bits
95
- * });
96
- * ```
97
- *
98
- * @param params - The parameters for the decryption operation.
99
- *
100
- * @returns A Promise that resolves to the decrypted data as a Uint8Array.
101
- */
102
- AesGcmAlgorithm.prototype.decrypt = function (params) {
103
- return __awaiter(this, void 0, void 0, function () {
104
- var plaintext;
105
- return __generator(this, function (_a) {
106
- plaintext = aes_gcm_js_1.AesGcm.decrypt(params);
107
- return [2 /*return*/, plaintext];
108
- });
109
- });
110
- };
111
- /**
112
- * Encrypts the provided data using AES-GCM.
113
- *
114
- * @remarks
115
- * This method performs AES-GCM encryption on the given data using the specified key.
116
- * It requires an initialization vector (IV), the encrypted data along with the decryption key,
117
- * and optionally, additional authenticated data (AAD). The method returns the encrypted data as a
118
- * Uint8Array. The optional `tagLength` parameter specifies the size in bits of the authentication
119
- * tag generated in the encryption operation and used for authentication in the corresponding
120
- * decryption. If not specified, the default tag length of 128 bits is used.
121
- *
122
- * @example
123
- * ```ts
124
- * const aesGcm = new AesGcmAlgorithm();
125
- * const data = new TextEncoder().encode('Messsage');
126
- * const iv = new Uint8Array([...]); // Initialization vector
127
- * const additionalData = new Uint8Array([...]); // Optional additional authenticated data
128
- * const key = { ... }; // A Jwk object representing an AES key
129
- * const encryptedData = await aesGcm.encrypt({
130
- * data,
131
- * iv,
132
- * additionalData,
133
- * key,
134
- * tagLength: 128 // Optional tag length in bits
135
- * });
136
- * ```
137
- *
138
- * @param params - The parameters for the encryption operation.
139
- *
140
- * @returns A Promise that resolves to the encrypted data as a Uint8Array.
141
- */
142
- AesGcmAlgorithm.prototype.encrypt = function (params) {
143
- return __awaiter(this, void 0, void 0, function () {
144
- var ciphertext;
145
- return __generator(this, function (_a) {
146
- ciphertext = aes_gcm_js_1.AesGcm.encrypt(params);
147
- return [2 /*return*/, ciphertext];
148
- });
149
- });
150
- };
151
- /**
152
- * Generates a symmetric key for AES in Galois/Counter Mode (GCM) in JSON Web Key (JWK) format.
153
- *
154
- * @remarks
155
- * This method generates a symmetric AES key for use in GCM mode, based on the specified
156
- * `algorithm` parameter which determines the key length. It uses cryptographically secure random
157
- * number generation to ensure the uniqueness and security of the key. The key is returned in JWK
158
- * format.
159
- *
160
- * The generated key includes the following components:
161
- * - `kty`: Key Type, set to 'oct' for Octet Sequence.
162
- * - `k`: The symmetric key component, base64url-encoded.
163
- * - `kid`: Key ID, generated based on the JWK thumbprint.
164
- *
165
- * @example
166
- * ```ts
167
- * const aesGcm = new AesGcmAlgorithm();
168
- * const privateKey = await aesGcm.generateKey({ algorithm: 'A256GCM' });
169
- * ```
170
- *
171
- * @param params - The parameters for the key generation.
172
- *
173
- * @returns A Promise that resolves to the generated symmetric key in JWK format.
174
- */
175
- AesGcmAlgorithm.prototype.generateKey = function (_a) {
176
- return __awaiter(this, arguments, void 0, function (_b) {
177
- var length, privateKey;
178
- var algorithm = _b.algorithm;
179
- return __generator(this, function (_c) {
180
- switch (_c.label) {
181
- case 0:
182
- length = { A128GCM: 128, A192GCM: 192, A256GCM: 256 }[algorithm];
183
- return [4 /*yield*/, aes_gcm_js_1.AesGcm.generateKey({ length: length })];
184
- case 1:
185
- privateKey = _c.sent();
186
- // Set the `alg` property based on the specified algorithm.
187
- privateKey.alg = algorithm;
188
- return [2 /*return*/, privateKey];
189
- }
190
- });
191
- });
192
- };
193
- return AesGcmAlgorithm;
194
- }(crypto_algorithm_js_1.CryptoAlgorithm));
195
- exports.AesGcmAlgorithm = AesGcmAlgorithm;
196
- //# sourceMappingURL=aes-gcm.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"aes-gcm.js","sourceRoot":"","sources":["../../../src/algorithms/aes-gcm.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAKA,6DAAwD;AACxD,uDAAuE;AAmDvE;;;;;;;;GAQG;AACH;IAAqC,mCAAe;IAApD;;IAwHA,CAAC;IApHC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACU,iCAAO,GAApB,UAAqB,MACS;;;;gBAEtB,SAAS,GAAG,mBAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;gBAEzC,sBAAO,SAAS,EAAC;;;KAClB;IAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA8BG;IACU,iCAAO,GAApB,UAAqB,MACS;;;;gBAEtB,UAAU,GAAG,mBAAM,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;gBAE1C,sBAAO,UAAU,EAAC;;;KACnB;IAED;;;;;;;;;;;;;;;;;;;;;;;OAuBG;IACU,qCAAW,GAAxB;4DAAyB,EACA;;gBADE,SAAS,eAAA;;;;wBAI5B,MAAM,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,GAAG,EAAE,CAAC,SAAS,CAAoB,CAAC;wBAGvE,qBAAM,mBAAM,CAAC,WAAW,CAAC,EAAE,MAAM,QAAA,EAAE,CAAC,EAAA;;wBAAjD,UAAU,GAAG,SAAoC;wBAEvD,2DAA2D;wBAC3D,UAAU,CAAC,GAAG,GAAG,SAAS,CAAC;wBAE3B,sBAAO,UAAU,EAAC;;;;KACnB;IACH,sBAAC;AAAD,CAAC,AAxHD,CAAqC,qCAAe,GAwHnD;AAxHY,0CAAe"}
@@ -1,13 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CryptoAlgorithm = void 0;
4
- /**
5
- * Base class for all cryptographic algorithm implementations.
6
- */
7
- var CryptoAlgorithm = /** @class */ (function () {
8
- function CryptoAlgorithm() {
9
- }
10
- return CryptoAlgorithm;
11
- }());
12
- exports.CryptoAlgorithm = CryptoAlgorithm;
13
- //# sourceMappingURL=crypto-algorithm.js.map