@reallyme/crypto 0.3.9 → 0.3.11

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 (66) hide show
  1. package/README.md +10 -11
  2. package/dist/aead.js +1 -1
  3. package/dist/aesKw.js +1 -1
  4. package/dist/algorithms.js +1 -1
  5. package/dist/argon2id.js +1 -1
  6. package/dist/bip340Schnorr.js +1 -1
  7. package/dist/cryptoFacade.d.ts +60 -81
  8. package/dist/cryptoFacade.js +12 -408
  9. package/dist/cryptoFacadeAsymmetric.d.ts +20 -0
  10. package/dist/cryptoFacadeAsymmetric.js +252 -0
  11. package/dist/cryptoFacadeSymmetric.d.ts +27 -0
  12. package/dist/cryptoFacadeSymmetric.js +166 -0
  13. package/dist/digest.js +1 -1
  14. package/dist/ed25519.d.ts +2 -0
  15. package/dist/ed25519.js +45 -4
  16. package/dist/encodeEcdsaDer.js +1 -1
  17. package/dist/errors.js +1 -1
  18. package/dist/hkdf.js +1 -1
  19. package/dist/hmac.d.ts +3 -3
  20. package/dist/hmac.js +8 -5
  21. package/dist/hpke.d.ts +1 -1
  22. package/dist/hpke.js +4 -2
  23. package/dist/index.js +1 -1
  24. package/dist/jwaConcatKdf.js +1 -1
  25. package/dist/jwk.js +46 -11
  26. package/dist/kmac.js +1 -1
  27. package/dist/memory.js +1 -1
  28. package/dist/mlDsa.js +1 -1
  29. package/dist/mlKem.js +1 -1
  30. package/dist/operationResponse.js +1 -1
  31. package/dist/p256Ecdh.js +1 -1
  32. package/dist/p256Ecdsa.js +1 -1
  33. package/dist/p384Ecdh.js +1 -1
  34. package/dist/p384Ecdsa.js +1 -1
  35. package/dist/p521Ecdh.js +1 -1
  36. package/dist/p521Ecdsa.js +1 -1
  37. package/dist/pbkdf2.js +12 -4
  38. package/dist/proto/generated/reallyme/crypto/v1/crypto_pb.js +1 -1
  39. package/dist/proto.d.ts +8 -117
  40. package/dist/proto.js +6 -1085
  41. package/dist/protoAlgorithms.d.ts +21 -0
  42. package/dist/protoAlgorithms.js +323 -0
  43. package/dist/protoErrors.d.ts +27 -0
  44. package/dist/protoErrors.js +275 -0
  45. package/dist/protoIdentifiers.d.ts +14 -0
  46. package/dist/protoIdentifiers.js +112 -0
  47. package/dist/protoJwk.d.ts +10 -0
  48. package/dist/protoJwk.js +204 -0
  49. package/dist/protoValues.d.ts +63 -0
  50. package/dist/protoValues.js +228 -0
  51. package/dist/providerCatalog.js +1 -1
  52. package/dist/rsa.js +1 -1
  53. package/dist/secp256k1.js +1 -1
  54. package/dist/slhDsa.js +1 -1
  55. package/dist/validateBytes.js +1 -1
  56. package/dist/verifyBundledWasmModule.d.ts +63 -0
  57. package/dist/verifyBundledWasmModule.js +80 -0
  58. package/dist/wasm/reallyme_crypto_wasm.d.ts +383 -0
  59. package/dist/wasm/reallyme_crypto_wasm.js +8 -8
  60. package/dist/wasm/reallyme_crypto_wasm_bg.wasm +0 -0
  61. package/dist/wasmModuleTypes.js +1 -1
  62. package/dist/wasmProvider.d.ts +5 -0
  63. package/dist/wasmProvider.js +61 -3
  64. package/dist/x25519.js +1 -1
  65. package/dist/xWing.js +1 -1
  66. package/package.json +6 -5
package/dist/proto.js CHANGED
@@ -1,1088 +1,9 @@
1
- // SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
1
+ // SPDX-FileCopyrightText: 2026 ReallyMe LLC
2
2
  //
3
3
  // SPDX-License-Identifier: MIT OR Apache-2.0
4
- import { create, fromBinary, toBinary } from "@bufbuild/protobuf";
5
- import { ReallyMeCryptoError } from "./errors.js";
6
- import { MAX_JWKS_KEYS, ReallyMeJwk } from "./jwk.js";
7
- import { ensureByteArrayAtMost, MAX_CRYPTO_INPUT_LENGTH, } from "./validateBytes.js";
8
4
  export { AeadAlgorithm, AeadAlgorithmSchema, CryptoAlgorithmFamily, CryptoAlgorithmFamilySchema, CryptoAlgorithmIdentifierSchema, CryptoAeadOpenRequestSchema, CryptoAeadOpenResultSchema, CryptoAeadSealRequestSchema, CryptoAeadSealResultSchema, CryptoBackendErrorSchema, CryptoBip340SchnorrSignRequestSchema, CryptoErrorSchema, CryptoErrorReason, CryptoErrorReasonSchema, CryptoHashRequestSchema, CryptoHashResultSchema, CryptoHkdfDeriveRequestSchema, CryptoHkdfDeriveResultSchema, CryptoHpkeOpenRequestSchema, CryptoHpkeOpenResultSchema, CryptoHpkeSealRequestSchema, CryptoHpkeSealedMessageSchema, CryptoJwaConcatKdfSha256DeriveRequestSchema, CryptoJwaConcatKdfSha256DeriveResultSchema, CryptoKdfDeriveKeyRequestSchema, CryptoKdfDeriveKeyResultSchema, CryptoKemEncapsulationSchema, CryptoKemDecapsulateRequestSchema, CryptoKemDecapsulateResultSchema, CryptoKemEncapsulateRequestSchema, CryptoKemDeriveKeyPairRequestSchema, CryptoKemGenerateKeyPairRequestSchema, CryptoKeyAgreementDeriveKeyPairRequestSchema, CryptoKeyAgreementDeriveSharedSecretRequestSchema, CryptoKeyAgreementDeriveSharedSecretResultSchema, CryptoKeyPairSchema, CryptoKeyUnwrapRequestSchema, CryptoKeyUnwrapResultSchema, CryptoKeyWrapRequestSchema, CryptoKeyWrapResultSchema, CryptoMacAuthenticateRequestSchema, CryptoMacAuthenticateResultSchema, CryptoMacVerifyRequestSchema, CryptoOperationRequestSchema, CryptoOperationResponseSchema, CryptoOperationResultSchema, CryptoPrimitiveErrorSchema, CryptoProviderErrorSchema, CryptoProviderCapabilitySchema, CryptoProviderCapabilitySetSchema, CryptoProviderSupportStatus, CryptoProviderSupportStatusSchema, CryptoRsaVerifyRequestSchema, CryptoSignatureDeriveKeyPairRequestSchema, CryptoSignatureGenerateKeyPairRequestSchema, CryptoSignatureSignRequestSchema, CryptoSignatureSignResultSchema, CryptoSignatureVerifyRequestSchema, CryptoVerificationResultSchema, CryptoVerificationStatus, CryptoVerificationStatusSchema, HashAlgorithm, HashAlgorithmSchema, HpkeAeadId, HpkeAeadIdSchema, HpkeKdfId, HpkeKdfIdSchema, HpkeKemId, HpkeKemIdSchema, HpkeSuiteIdentifierSchema, JsonWebKeySchema, JsonWebKeySetSchema, KdfAlgorithm, KdfAlgorithmSchema, KemAlgorithm, KemAlgorithmSchema, KeyAgreementAlgorithm, KeyAgreementAlgorithmSchema, KeyWrapAlgorithm, KeyWrapAlgorithmSchema, MacAlgorithm, MacAlgorithmSchema, MulticodecKeyAlgorithm, MulticodecKeyAlgorithmSchema, SignatureAlgorithm, SignatureAlgorithmSchema, file_reallyme_crypto_v1_crypto, } from "./proto/generated/reallyme/crypto/v1/crypto_pb.js";
9
- import { AeadAlgorithm, HashAlgorithm, HpkeAeadId, HpkeKdfId, HpkeKemId, KdfAlgorithm, KemAlgorithm, KeyAgreementAlgorithm, KeyWrapAlgorithm, MacAlgorithm, MulticodecKeyAlgorithm, SignatureAlgorithm, CryptoAlgorithmIdentifierSchema, CryptoAlgorithmFamily, CryptoBackendErrorSchema, CryptoErrorReason, CryptoErrorSchema, CryptoHpkeSealedMessageSchema, HpkeSuiteIdentifierSchema, CryptoKemEncapsulationSchema, CryptoKeyPairSchema, CryptoPrimitiveErrorSchema, CryptoProviderCapabilitySchema, CryptoProviderCapabilitySetSchema, CryptoProviderErrorSchema, CryptoProviderSupportStatus, CryptoVerificationResultSchema, CryptoVerificationStatus, JsonWebKeySchema, JsonWebKeySetSchema, } from "./proto/generated/reallyme/crypto/v1/crypto_pb.js";
10
- export const signatureAlgorithmFromProto = (value) => {
11
- switch (value) {
12
- case SignatureAlgorithm.ED25519:
13
- return "Ed25519";
14
- case SignatureAlgorithm.ECDSA_P256_SHA256:
15
- return "ECDSA-P256-SHA256";
16
- case SignatureAlgorithm.ECDSA_P384_SHA384:
17
- return "ECDSA-P384-SHA384";
18
- case SignatureAlgorithm.ECDSA_P521_SHA512:
19
- return "ECDSA-P521-SHA512";
20
- case SignatureAlgorithm.ECDSA_SECP256K1_SHA256:
21
- return "ECDSA-secp256k1-SHA256";
22
- case SignatureAlgorithm.BIP340_SCHNORR_SECP256K1_SHA256:
23
- return "BIP340-Schnorr-secp256k1-SHA256";
24
- case SignatureAlgorithm.RSA_PKCS1V15_SHA1:
25
- return "RSA-PKCS1v15-SHA1";
26
- case SignatureAlgorithm.RSA_PKCS1V15_SHA256:
27
- return "RSA-PKCS1v15-SHA256";
28
- case SignatureAlgorithm.RSA_PKCS1V15_SHA384:
29
- return "RSA-PKCS1v15-SHA384";
30
- case SignatureAlgorithm.RSA_PKCS1V15_SHA512:
31
- return "RSA-PKCS1v15-SHA512";
32
- case SignatureAlgorithm.RSA_PSS_SHA1_MGF1_SHA1:
33
- return "RSA-PSS-SHA1-MGF1-SHA1";
34
- case SignatureAlgorithm.RSA_PSS_SHA256_MGF1_SHA256:
35
- return "RSA-PSS-SHA256-MGF1-SHA256";
36
- case SignatureAlgorithm.RSA_PSS_SHA384_MGF1_SHA384:
37
- return "RSA-PSS-SHA384-MGF1-SHA384";
38
- case SignatureAlgorithm.RSA_PSS_SHA512_MGF1_SHA512:
39
- return "RSA-PSS-SHA512-MGF1-SHA512";
40
- case SignatureAlgorithm.ML_DSA_44:
41
- return "ML-DSA-44";
42
- case SignatureAlgorithm.ML_DSA_65:
43
- return "ML-DSA-65";
44
- case SignatureAlgorithm.ML_DSA_87:
45
- return "ML-DSA-87";
46
- case SignatureAlgorithm.SLH_DSA_SHA2_128S:
47
- return "SLH-DSA-SHA2-128s";
48
- default:
49
- throw new ReallyMeCryptoError("unsupported-algorithm");
50
- }
51
- };
52
- export const signatureAlgorithmToProto = (value) => {
53
- switch (value) {
54
- case "Ed25519":
55
- return SignatureAlgorithm.ED25519;
56
- case "ECDSA-P256-SHA256":
57
- return SignatureAlgorithm.ECDSA_P256_SHA256;
58
- case "ECDSA-P384-SHA384":
59
- return SignatureAlgorithm.ECDSA_P384_SHA384;
60
- case "ECDSA-P521-SHA512":
61
- return SignatureAlgorithm.ECDSA_P521_SHA512;
62
- case "ECDSA-secp256k1-SHA256":
63
- return SignatureAlgorithm.ECDSA_SECP256K1_SHA256;
64
- case "BIP340-Schnorr-secp256k1-SHA256":
65
- return SignatureAlgorithm.BIP340_SCHNORR_SECP256K1_SHA256;
66
- case "RSA-PKCS1v15-SHA1":
67
- return SignatureAlgorithm.RSA_PKCS1V15_SHA1;
68
- case "RSA-PKCS1v15-SHA256":
69
- return SignatureAlgorithm.RSA_PKCS1V15_SHA256;
70
- case "RSA-PKCS1v15-SHA384":
71
- return SignatureAlgorithm.RSA_PKCS1V15_SHA384;
72
- case "RSA-PKCS1v15-SHA512":
73
- return SignatureAlgorithm.RSA_PKCS1V15_SHA512;
74
- case "RSA-PSS-SHA1-MGF1-SHA1":
75
- return SignatureAlgorithm.RSA_PSS_SHA1_MGF1_SHA1;
76
- case "RSA-PSS-SHA256-MGF1-SHA256":
77
- return SignatureAlgorithm.RSA_PSS_SHA256_MGF1_SHA256;
78
- case "RSA-PSS-SHA384-MGF1-SHA384":
79
- return SignatureAlgorithm.RSA_PSS_SHA384_MGF1_SHA384;
80
- case "RSA-PSS-SHA512-MGF1-SHA512":
81
- return SignatureAlgorithm.RSA_PSS_SHA512_MGF1_SHA512;
82
- case "ML-DSA-44":
83
- return SignatureAlgorithm.ML_DSA_44;
84
- case "ML-DSA-65":
85
- return SignatureAlgorithm.ML_DSA_65;
86
- case "ML-DSA-87":
87
- return SignatureAlgorithm.ML_DSA_87;
88
- case "SLH-DSA-SHA2-128s":
89
- return SignatureAlgorithm.SLH_DSA_SHA2_128S;
90
- }
91
- };
92
- export const hashAlgorithmFromProto = (value) => {
93
- switch (value) {
94
- case HashAlgorithm.SHA2_256:
95
- return "SHA2-256";
96
- case HashAlgorithm.SHA2_384:
97
- return "SHA2-384";
98
- case HashAlgorithm.SHA2_512:
99
- return "SHA2-512";
100
- case HashAlgorithm.SHA3_224:
101
- return "SHA3-224";
102
- case HashAlgorithm.SHA3_256:
103
- return "SHA3-256";
104
- case HashAlgorithm.SHA3_384:
105
- return "SHA3-384";
106
- case HashAlgorithm.SHA3_512:
107
- return "SHA3-512";
108
- default:
109
- throw new ReallyMeCryptoError("unsupported-algorithm");
110
- }
111
- };
112
- export const hashAlgorithmToProto = (value) => {
113
- switch (value) {
114
- case "SHA2-256":
115
- return HashAlgorithm.SHA2_256;
116
- case "SHA2-384":
117
- return HashAlgorithm.SHA2_384;
118
- case "SHA2-512":
119
- return HashAlgorithm.SHA2_512;
120
- case "SHA3-224":
121
- return HashAlgorithm.SHA3_224;
122
- case "SHA3-256":
123
- return HashAlgorithm.SHA3_256;
124
- case "SHA3-384":
125
- return HashAlgorithm.SHA3_384;
126
- case "SHA3-512":
127
- return HashAlgorithm.SHA3_512;
128
- }
129
- };
130
- export const aeadAlgorithmFromProto = (value) => {
131
- switch (value) {
132
- case AeadAlgorithm.AES_128_GCM:
133
- return "AES-128-GCM";
134
- case AeadAlgorithm.AES_192_GCM:
135
- return "AES-192-GCM";
136
- case AeadAlgorithm.AES_256_GCM:
137
- return "AES-256-GCM";
138
- case AeadAlgorithm.AES_256_GCM_SIV:
139
- return "AES-256-GCM-SIV";
140
- case AeadAlgorithm.CHACHA20_POLY1305:
141
- return "ChaCha20-Poly1305";
142
- case AeadAlgorithm.XCHACHA20_POLY1305:
143
- return "XChaCha20-Poly1305";
144
- default:
145
- throw new ReallyMeCryptoError("unsupported-algorithm");
146
- }
147
- };
148
- export const aeadAlgorithmToProto = (value) => {
149
- switch (value) {
150
- case "AES-128-GCM":
151
- return AeadAlgorithm.AES_128_GCM;
152
- case "AES-192-GCM":
153
- return AeadAlgorithm.AES_192_GCM;
154
- case "AES-256-GCM":
155
- return AeadAlgorithm.AES_256_GCM;
156
- case "AES-256-GCM-SIV":
157
- return AeadAlgorithm.AES_256_GCM_SIV;
158
- case "ChaCha20-Poly1305":
159
- return AeadAlgorithm.CHACHA20_POLY1305;
160
- case "XChaCha20-Poly1305":
161
- return AeadAlgorithm.XCHACHA20_POLY1305;
162
- }
163
- };
164
- export const kemAlgorithmFromProto = (value) => {
165
- switch (value) {
166
- case KemAlgorithm.ML_KEM_512:
167
- return "ML-KEM-512";
168
- case KemAlgorithm.ML_KEM_768:
169
- return "ML-KEM-768";
170
- case KemAlgorithm.ML_KEM_1024:
171
- return "ML-KEM-1024";
172
- case KemAlgorithm.X_WING_768:
173
- return "X-Wing-768";
174
- default:
175
- throw new ReallyMeCryptoError("unsupported-algorithm");
176
- }
177
- };
178
- export const kemAlgorithmToProto = (value) => {
179
- switch (value) {
180
- case "ML-KEM-512":
181
- return KemAlgorithm.ML_KEM_512;
182
- case "ML-KEM-768":
183
- return KemAlgorithm.ML_KEM_768;
184
- case "ML-KEM-1024":
185
- return KemAlgorithm.ML_KEM_1024;
186
- case "X-Wing-768":
187
- return KemAlgorithm.X_WING_768;
188
- }
189
- };
190
- export const keyAgreementAlgorithmFromProto = (value) => {
191
- switch (value) {
192
- case KeyAgreementAlgorithm.X25519:
193
- return "X25519";
194
- case KeyAgreementAlgorithm.P256_ECDH:
195
- return "P-256-ECDH";
196
- case KeyAgreementAlgorithm.P384_ECDH:
197
- return "P-384-ECDH";
198
- case KeyAgreementAlgorithm.P521_ECDH:
199
- return "P-521-ECDH";
200
- default:
201
- throw new ReallyMeCryptoError("unsupported-algorithm");
202
- }
203
- };
204
- export const keyAgreementAlgorithmToProto = (value) => {
205
- switch (value) {
206
- case "X25519":
207
- return KeyAgreementAlgorithm.X25519;
208
- case "P-256-ECDH":
209
- return KeyAgreementAlgorithm.P256_ECDH;
210
- case "P-384-ECDH":
211
- return KeyAgreementAlgorithm.P384_ECDH;
212
- case "P-521-ECDH":
213
- return KeyAgreementAlgorithm.P521_ECDH;
214
- }
215
- };
216
- export const macAlgorithmFromProto = (value) => {
217
- switch (value) {
218
- case MacAlgorithm.HMAC_SHA256:
219
- return "HMAC-SHA-256";
220
- case MacAlgorithm.HMAC_SHA384:
221
- return "HMAC-SHA-384";
222
- case MacAlgorithm.HMAC_SHA512:
223
- return "HMAC-SHA-512";
224
- default:
225
- throw new ReallyMeCryptoError("unsupported-algorithm");
226
- }
227
- };
228
- export const macAlgorithmToProto = (value) => {
229
- switch (value) {
230
- case "HMAC-SHA-256":
231
- return MacAlgorithm.HMAC_SHA256;
232
- case "HMAC-SHA-384":
233
- return MacAlgorithm.HMAC_SHA384;
234
- case "HMAC-SHA-512":
235
- return MacAlgorithm.HMAC_SHA512;
236
- }
237
- };
238
- export const kdfAlgorithmFromProto = (value) => {
239
- switch (value) {
240
- case KdfAlgorithm.HKDF_SHA256:
241
- return "HKDF-SHA256";
242
- case KdfAlgorithm.HKDF_SHA384:
243
- return "HKDF-SHA384";
244
- case KdfAlgorithm.KMAC_256:
245
- return "KMAC256";
246
- case KdfAlgorithm.ARGON2ID:
247
- return "Argon2id";
248
- case KdfAlgorithm.PBKDF2_HMAC_SHA256:
249
- return "PBKDF2-HMAC-SHA-256";
250
- case KdfAlgorithm.PBKDF2_HMAC_SHA512:
251
- return "PBKDF2-HMAC-SHA-512";
252
- case KdfAlgorithm.JWA_CONCAT_KDF_SHA256:
253
- return "JWA-CONCAT-KDF-SHA256";
254
- default:
255
- throw new ReallyMeCryptoError("unsupported-algorithm");
256
- }
257
- };
258
- export const kdfAlgorithmToProto = (value) => {
259
- switch (value) {
260
- case "HKDF-SHA256":
261
- return KdfAlgorithm.HKDF_SHA256;
262
- case "HKDF-SHA384":
263
- return KdfAlgorithm.HKDF_SHA384;
264
- case "KMAC256":
265
- return KdfAlgorithm.KMAC_256;
266
- case "Argon2id":
267
- return KdfAlgorithm.ARGON2ID;
268
- case "PBKDF2-HMAC-SHA-256":
269
- return KdfAlgorithm.PBKDF2_HMAC_SHA256;
270
- case "PBKDF2-HMAC-SHA-512":
271
- return KdfAlgorithm.PBKDF2_HMAC_SHA512;
272
- case "JWA-CONCAT-KDF-SHA256":
273
- return KdfAlgorithm.JWA_CONCAT_KDF_SHA256;
274
- }
275
- };
276
- export const keyWrapAlgorithmFromProto = (value) => {
277
- switch (value) {
278
- case KeyWrapAlgorithm.AES_128_KW:
279
- return "AES-128-KW";
280
- case KeyWrapAlgorithm.AES_192_KW:
281
- return "AES-192-KW";
282
- case KeyWrapAlgorithm.AES_256_KW:
283
- return "AES-256-KW";
284
- default:
285
- throw new ReallyMeCryptoError("unsupported-algorithm");
286
- }
287
- };
288
- export const keyWrapAlgorithmToProto = (value) => {
289
- switch (value) {
290
- case "AES-128-KW":
291
- return KeyWrapAlgorithm.AES_128_KW;
292
- case "AES-192-KW":
293
- return KeyWrapAlgorithm.AES_192_KW;
294
- case "AES-256-KW":
295
- return KeyWrapAlgorithm.AES_256_KW;
296
- }
297
- };
298
- export const hpkeSuiteFromProto = (value) => {
299
- if (value.kem === HpkeKemId.DHKEM_P256_HKDF_SHA256 &&
300
- value.kdf === HpkeKdfId.HKDF_SHA256 &&
301
- value.aead === HpkeAeadId.AES_256_GCM) {
302
- return "DHKEM-P256-HKDF-SHA256-HKDF-SHA256-AES-256-GCM";
303
- }
304
- if (value.kem === HpkeKemId.DHKEM_X25519_HKDF_SHA256 &&
305
- value.kdf === HpkeKdfId.HKDF_SHA256 &&
306
- value.aead === HpkeAeadId.CHACHA20_POLY1305) {
307
- return "DHKEM-X25519-HKDF-SHA256-HKDF-SHA256-CHACHA20-POLY1305";
308
- }
309
- throw new ReallyMeCryptoError("unsupported-algorithm");
310
- };
311
- export const hpkeSuiteToProto = (value) => {
312
- switch (value) {
313
- case "DHKEM-P256-HKDF-SHA256-HKDF-SHA256-AES-256-GCM":
314
- return create(HpkeSuiteIdentifierSchema, {
315
- kem: HpkeKemId.DHKEM_P256_HKDF_SHA256,
316
- kdf: HpkeKdfId.HKDF_SHA256,
317
- aead: HpkeAeadId.AES_256_GCM,
318
- });
319
- case "DHKEM-X25519-HKDF-SHA256-HKDF-SHA256-CHACHA20-POLY1305":
320
- return create(HpkeSuiteIdentifierSchema, {
321
- kem: HpkeKemId.DHKEM_X25519_HKDF_SHA256,
322
- kdf: HpkeKdfId.HKDF_SHA256,
323
- aead: HpkeAeadId.CHACHA20_POLY1305,
324
- });
325
- }
326
- };
327
- const signatureAlgorithmIdentifierToProto = (value) => create(CryptoAlgorithmIdentifierSchema, {
328
- algorithm: { case: "signature", value: signatureAlgorithmToProto(value) },
329
- });
330
- const keyAgreementAlgorithmIdentifierToProto = (value) => create(CryptoAlgorithmIdentifierSchema, {
331
- algorithm: { case: "keyAgreement", value: keyAgreementAlgorithmToProto(value) },
332
- });
333
- const kemAlgorithmIdentifierToProto = (value) => create(CryptoAlgorithmIdentifierSchema, {
334
- algorithm: { case: "kem", value: kemAlgorithmToProto(value) },
335
- });
336
- const hpkeSuiteIdentifierToProto = (value) => create(CryptoAlgorithmIdentifierSchema, {
337
- algorithm: { case: "hpkeSuite", value: hpkeSuiteToProto(value) },
338
- });
339
- const signatureAlgorithmFromIdentifier = (value) => {
340
- if (value?.algorithm.case !== "signature") {
341
- throw new ReallyMeCryptoError("invalid-input");
342
- }
343
- return signatureAlgorithmFromProto(value.algorithm.value);
344
- };
345
- const keyAgreementAlgorithmFromIdentifier = (value) => {
346
- if (value?.algorithm.case !== "keyAgreement") {
347
- throw new ReallyMeCryptoError("invalid-input");
348
- }
349
- return keyAgreementAlgorithmFromProto(value.algorithm.value);
350
- };
351
- const kemAlgorithmFromIdentifier = (value) => {
352
- if (value?.algorithm.case !== "kem") {
353
- throw new ReallyMeCryptoError("invalid-input");
354
- }
355
- return kemAlgorithmFromProto(value.algorithm.value);
356
- };
357
- const hpkeSuiteFromIdentifier = (value) => {
358
- if (value?.algorithm.case !== "hpkeSuite") {
359
- throw new ReallyMeCryptoError("invalid-input");
360
- }
361
- return hpkeSuiteFromProto(value.algorithm.value);
362
- };
363
- export const multicodecKeyAlgorithmFromProto = (value) => {
364
- switch (value) {
365
- case MulticodecKeyAlgorithm.ED25519_PUB:
366
- return "ed25519-pub";
367
- case MulticodecKeyAlgorithm.X25519_PUB:
368
- return "x25519-pub";
369
- case MulticodecKeyAlgorithm.SECP256K1_PUB:
370
- return "secp256k1-pub";
371
- case MulticodecKeyAlgorithm.P256_PUB:
372
- return "p256-pub";
373
- case MulticodecKeyAlgorithm.P384_PUB:
374
- return "p384-pub";
375
- case MulticodecKeyAlgorithm.P521_PUB:
376
- return "p521-pub";
377
- case MulticodecKeyAlgorithm.ED448_PUB:
378
- return "ed448-pub";
379
- case MulticodecKeyAlgorithm.RSA_PUB:
380
- return "rsa-pub";
381
- case MulticodecKeyAlgorithm.ML_KEM_512_PUB:
382
- return "mlkem-512-pub";
383
- case MulticodecKeyAlgorithm.ML_KEM_768_PUB:
384
- return "mlkem-768-pub";
385
- case MulticodecKeyAlgorithm.ML_KEM_1024_PUB:
386
- return "mlkem-1024-pub";
387
- case MulticodecKeyAlgorithm.ML_DSA_44_PUB:
388
- return "mldsa-44-pub";
389
- case MulticodecKeyAlgorithm.ML_DSA_65_PUB:
390
- return "mldsa-65-pub";
391
- case MulticodecKeyAlgorithm.ML_DSA_87_PUB:
392
- return "mldsa-87-pub";
393
- default:
394
- throw new ReallyMeCryptoError("unsupported-algorithm");
395
- }
396
- };
397
- export const multicodecKeyAlgorithmToProto = (value) => {
398
- switch (value) {
399
- case "ed25519-pub":
400
- return MulticodecKeyAlgorithm.ED25519_PUB;
401
- case "x25519-pub":
402
- return MulticodecKeyAlgorithm.X25519_PUB;
403
- case "secp256k1-pub":
404
- return MulticodecKeyAlgorithm.SECP256K1_PUB;
405
- case "p256-pub":
406
- return MulticodecKeyAlgorithm.P256_PUB;
407
- case "p384-pub":
408
- return MulticodecKeyAlgorithm.P384_PUB;
409
- case "p521-pub":
410
- return MulticodecKeyAlgorithm.P521_PUB;
411
- case "ed448-pub":
412
- return MulticodecKeyAlgorithm.ED448_PUB;
413
- case "rsa-pub":
414
- return MulticodecKeyAlgorithm.RSA_PUB;
415
- case "mlkem-512-pub":
416
- return MulticodecKeyAlgorithm.ML_KEM_512_PUB;
417
- case "mlkem-768-pub":
418
- return MulticodecKeyAlgorithm.ML_KEM_768_PUB;
419
- case "mlkem-1024-pub":
420
- return MulticodecKeyAlgorithm.ML_KEM_1024_PUB;
421
- case "mldsa-44-pub":
422
- return MulticodecKeyAlgorithm.ML_DSA_44_PUB;
423
- case "mldsa-65-pub":
424
- return MulticodecKeyAlgorithm.ML_DSA_65_PUB;
425
- case "mldsa-87-pub":
426
- return MulticodecKeyAlgorithm.ML_DSA_87_PUB;
427
- }
428
- };
429
- // The largest supported canonical JWK is below 4 KiB. Keeping a full factor of
430
- // two for future public-key encodings prevents attacker-controlled allocations
431
- // and guarantees conversion never depends on an engine's argument-count limit.
432
- const MAX_JWK_CANONICAL_JCS_LENGTH = 8_192;
433
- const asciiToBytes = (value) => {
434
- if (value.length > MAX_JWK_CANONICAL_JCS_LENGTH) {
435
- throw new ReallyMeCryptoError("invalid-input");
436
- }
437
- const bytes = new Uint8Array(value.length);
438
- for (let index = 0; index < value.length; index += 1) {
439
- const codeUnit = value.charCodeAt(index);
440
- if (codeUnit > 0x7f) {
441
- throw new ReallyMeCryptoError("invalid-input");
442
- }
443
- bytes[index] = codeUnit;
444
- }
445
- return bytes;
446
- };
447
- const asciiFromBytes = (value) => {
448
- ensureByteArrayAtMost(value, MAX_JWK_CANONICAL_JCS_LENGTH);
449
- const characters = [];
450
- for (const byte of value) {
451
- if (byte > 0x7f) {
452
- throw new ReallyMeCryptoError("invalid-input");
453
- }
454
- characters.push(String.fromCharCode(byte));
455
- }
456
- return characters.join("");
457
- };
458
- const withJwkProtoBoundaryErrors = (operation) => {
459
- try {
460
- return operation();
461
- }
462
- catch (error) {
463
- if (error instanceof ReallyMeCryptoError) {
464
- throw error;
465
- }
466
- throw new ReallyMeCryptoError("invalid-input");
467
- }
468
- };
469
- const cryptoErrorReasonToFacadeError = (reason) => {
470
- switch (reason) {
471
- case CryptoErrorReason.PRIMITIVE_INVALID_SIGNATURE:
472
- case CryptoErrorReason.PRIMITIVE_VERIFICATION_FAILED:
473
- return new ReallyMeCryptoError("invalid-signature");
474
- case CryptoErrorReason.PROVIDER_UNSUPPORTED_ALGORITHM:
475
- case CryptoErrorReason.PROVIDER_UNSUPPORTED_BACKEND:
476
- return new ReallyMeCryptoError("unsupported-algorithm");
477
- case CryptoErrorReason.PROVIDER_UNAVAILABLE:
478
- case CryptoErrorReason.PROVIDER_RANDOMNESS_UNAVAILABLE:
479
- case CryptoErrorReason.PROVIDER_KEY_EXISTS:
480
- case CryptoErrorReason.PROVIDER_KEY_NOT_FOUND:
481
- case CryptoErrorReason.PROVIDER_ACCESS_DENIED:
482
- case CryptoErrorReason.PROVIDER_USER_AUTHENTICATION_REQUIRED:
483
- case CryptoErrorReason.PROVIDER_USER_CANCELED:
484
- case CryptoErrorReason.PROVIDER_HARDWARE_UNAVAILABLE:
485
- case CryptoErrorReason.PROVIDER_HARDWARE_REJECTED_KEY:
486
- case CryptoErrorReason.BACKEND_INVALID_STATE:
487
- case CryptoErrorReason.BACKEND_INTERNAL:
488
- return new ReallyMeCryptoError("provider-failure");
489
- case CryptoErrorReason.PRIMITIVE_INVALID_PARAMETER:
490
- case CryptoErrorReason.PRIMITIVE_INVALID_LENGTH:
491
- case CryptoErrorReason.PRIMITIVE_INVALID_KEY:
492
- case CryptoErrorReason.PRIMITIVE_INVALID_PUBLIC_KEY:
493
- case CryptoErrorReason.PRIMITIVE_INVALID_PRIVATE_KEY:
494
- case CryptoErrorReason.PRIMITIVE_INVALID_NONCE:
495
- case CryptoErrorReason.PRIMITIVE_INVALID_SALT:
496
- case CryptoErrorReason.PRIMITIVE_INVALID_PASSWORD:
497
- case CryptoErrorReason.PRIMITIVE_INVALID_ENCODING:
498
- case CryptoErrorReason.PRIMITIVE_INVALID_SHARED_SECRET:
499
- case CryptoErrorReason.PRIMITIVE_MALFORMED_CIPHERTEXT:
500
- case CryptoErrorReason.PRIMITIVE_INVALID_TAG:
501
- case CryptoErrorReason.PRIMITIVE_MALFORMED_PROTOBUF:
502
- case CryptoErrorReason.PRIMITIVE_MALFORMED_JSON:
503
- case CryptoErrorReason.PRIMITIVE_RESOURCE_LIMIT_EXCEEDED:
504
- case CryptoErrorReason.PRIMITIVE_MISSING_OPERATION:
505
- return new ReallyMeCryptoError("invalid-input");
506
- case CryptoErrorReason.PRIMITIVE_AUTHENTICATION_FAILED:
507
- return new ReallyMeCryptoError("authentication-failed");
508
- default:
509
- return new ReallyMeCryptoError("invalid-input");
510
- }
511
- };
512
- const invalidInputReasons = new Set([
513
- CryptoErrorReason.PRIMITIVE_INVALID_PARAMETER,
514
- CryptoErrorReason.PRIMITIVE_INVALID_LENGTH,
515
- CryptoErrorReason.PRIMITIVE_INVALID_KEY,
516
- CryptoErrorReason.PRIMITIVE_INVALID_PUBLIC_KEY,
517
- CryptoErrorReason.PRIMITIVE_INVALID_PRIVATE_KEY,
518
- CryptoErrorReason.PRIMITIVE_INVALID_NONCE,
519
- CryptoErrorReason.PRIMITIVE_INVALID_SALT,
520
- CryptoErrorReason.PRIMITIVE_INVALID_PASSWORD,
521
- CryptoErrorReason.PRIMITIVE_INVALID_ENCODING,
522
- CryptoErrorReason.PRIMITIVE_INVALID_SHARED_SECRET,
523
- CryptoErrorReason.PRIMITIVE_MALFORMED_CIPHERTEXT,
524
- CryptoErrorReason.PRIMITIVE_INVALID_TAG,
525
- CryptoErrorReason.PRIMITIVE_MALFORMED_PROTOBUF,
526
- CryptoErrorReason.PRIMITIVE_MALFORMED_JSON,
527
- CryptoErrorReason.PRIMITIVE_RESOURCE_LIMIT_EXCEEDED,
528
- CryptoErrorReason.PRIMITIVE_MISSING_OPERATION,
529
- ]);
530
- export const cryptoWireErrorToProto = (error) => {
531
- switch (error.branch) {
532
- case "primitive":
533
- return create(CryptoErrorSchema, {
534
- error: {
535
- case: "primitive",
536
- value: create(CryptoPrimitiveErrorSchema, {
537
- reason: error.reason,
538
- }),
539
- },
540
- });
541
- case "provider":
542
- return create(CryptoErrorSchema, {
543
- error: {
544
- case: "provider",
545
- value: create(CryptoProviderErrorSchema, {
546
- reason: error.reason,
547
- }),
548
- },
549
- });
550
- case "backend":
551
- return create(CryptoErrorSchema, {
552
- error: {
553
- case: "backend",
554
- value: create(CryptoBackendErrorSchema, {
555
- reason: error.reason,
556
- }),
557
- },
558
- });
559
- }
560
- };
561
- export const cryptoWireErrorFromProto = (value) => {
562
- switch (value.error.case) {
563
- case "primitive":
564
- return strictCryptoWireError("primitive", value.error.value.reason);
565
- case "provider":
566
- return strictCryptoWireError("provider", value.error.value.reason);
567
- case "backend":
568
- return strictCryptoWireError("backend", value.error.value.reason);
569
- default:
570
- return malformedCryptoErrorEnvelope();
571
- }
572
- };
573
- export const cryptoWireErrorToProtoBytes = (error) => toBinary(CryptoErrorSchema, cryptoWireErrorToProto(error));
574
- export const cryptoWireErrorFromProtoBytes = (bytes) => {
575
- try {
576
- return cryptoWireErrorFromProto(fromBinary(CryptoErrorSchema, bytes));
577
- }
578
- catch {
579
- return malformedCryptoErrorEnvelope();
580
- }
581
- };
582
- const strictCryptoWireError = (branch, reason) => {
583
- const result = cryptoWireErrorTryNew(branch, reason);
584
- return result.ok ? result.value : malformedCryptoErrorEnvelope();
585
- };
586
- export const cryptoWireErrorTryNew = (branch, reason) => {
587
- if (reason === CryptoErrorReason.UNSPECIFIED) {
588
- return { ok: false, error: "unspecified-reason" };
589
- }
590
- if (knownCryptoErrorReasons.has(reason) && !cryptoErrorReasonMatchesBranch(branch, reason)) {
591
- return { ok: false, error: "branch-reason-mismatch" };
592
- }
593
- if (!cryptoErrorReasonCodeMatchesBranch(branch, reason)) {
594
- return { ok: false, error: "reason-code-out-of-range" };
595
- }
596
- return { ok: true, value: { branch, reason, reasonCode: reason } };
597
- };
598
- const malformedCryptoErrorEnvelope = () => ({
599
- branch: "primitive",
600
- reason: CryptoErrorReason.PRIMITIVE_MALFORMED_PROTOBUF,
601
- reasonCode: CryptoErrorReason.PRIMITIVE_MALFORMED_PROTOBUF,
602
- });
603
- const cryptoErrorReasonCodeMatchesBranch = (branch, reasonCode) => {
604
- switch (branch) {
605
- case "primitive":
606
- return reasonCode >= 100 && reasonCode <= 199;
607
- case "provider":
608
- return reasonCode >= 200 && reasonCode <= 299;
609
- case "backend":
610
- return reasonCode >= 300 && reasonCode <= 399;
611
- }
612
- };
613
- const cryptoErrorReasonMatchesBranch = (branch, reason) => {
614
- switch (branch) {
615
- case "primitive":
616
- return primitiveCryptoErrorReasons.has(reason);
617
- case "provider":
618
- return providerCryptoErrorReasons.has(reason);
619
- case "backend":
620
- return backendCryptoErrorReasons.has(reason);
621
- }
622
- };
623
- const primitiveCryptoErrorReasons = new Set([
624
- CryptoErrorReason.PRIMITIVE_INVALID_PARAMETER,
625
- CryptoErrorReason.PRIMITIVE_INVALID_LENGTH,
626
- CryptoErrorReason.PRIMITIVE_INVALID_KEY,
627
- CryptoErrorReason.PRIMITIVE_INVALID_PUBLIC_KEY,
628
- CryptoErrorReason.PRIMITIVE_INVALID_PRIVATE_KEY,
629
- CryptoErrorReason.PRIMITIVE_INVALID_NONCE,
630
- CryptoErrorReason.PRIMITIVE_INVALID_SALT,
631
- CryptoErrorReason.PRIMITIVE_INVALID_PASSWORD,
632
- CryptoErrorReason.PRIMITIVE_INVALID_ENCODING,
633
- CryptoErrorReason.PRIMITIVE_INVALID_SIGNATURE,
634
- CryptoErrorReason.PRIMITIVE_VERIFICATION_FAILED,
635
- CryptoErrorReason.PRIMITIVE_AUTHENTICATION_FAILED,
636
- CryptoErrorReason.PRIMITIVE_MALFORMED_CIPHERTEXT,
637
- CryptoErrorReason.PRIMITIVE_INVALID_TAG,
638
- CryptoErrorReason.PRIMITIVE_INVALID_SHARED_SECRET,
639
- CryptoErrorReason.PRIMITIVE_MALFORMED_PROTOBUF,
640
- CryptoErrorReason.PRIMITIVE_MALFORMED_JSON,
641
- CryptoErrorReason.PRIMITIVE_RESOURCE_LIMIT_EXCEEDED,
642
- CryptoErrorReason.PRIMITIVE_MISSING_OPERATION,
643
- ]);
644
- const providerCryptoErrorReasons = new Set([
645
- CryptoErrorReason.PROVIDER_UNSUPPORTED_ALGORITHM,
646
- CryptoErrorReason.PROVIDER_UNSUPPORTED_BACKEND,
647
- CryptoErrorReason.PROVIDER_UNAVAILABLE,
648
- CryptoErrorReason.PROVIDER_RANDOMNESS_UNAVAILABLE,
649
- CryptoErrorReason.PROVIDER_KEY_EXISTS,
650
- CryptoErrorReason.PROVIDER_KEY_NOT_FOUND,
651
- CryptoErrorReason.PROVIDER_ACCESS_DENIED,
652
- CryptoErrorReason.PROVIDER_USER_AUTHENTICATION_REQUIRED,
653
- CryptoErrorReason.PROVIDER_USER_CANCELED,
654
- CryptoErrorReason.PROVIDER_HARDWARE_UNAVAILABLE,
655
- CryptoErrorReason.PROVIDER_HARDWARE_REJECTED_KEY,
656
- ]);
657
- const backendCryptoErrorReasons = new Set([
658
- CryptoErrorReason.BACKEND_INVALID_STATE,
659
- CryptoErrorReason.BACKEND_INTERNAL,
660
- ]);
661
- const knownCryptoErrorReasons = new Set([
662
- ...primitiveCryptoErrorReasons,
663
- ...providerCryptoErrorReasons,
664
- ...backendCryptoErrorReasons,
665
- ]);
666
- export const cryptoWireErrorToFacadeError = (error) => {
667
- if (!knownCryptoErrorReasons.has(error.reason)) {
668
- return new ReallyMeCryptoError("provider-failure");
669
- }
670
- const facadeError = cryptoErrorReasonToFacadeError(error.reason);
671
- if (facadeError.code !== "invalid-input" || invalidInputReasons.has(error.reason)) {
672
- return facadeError;
673
- }
674
- switch (error.branch) {
675
- case "primitive":
676
- return facadeError;
677
- case "provider":
678
- case "backend":
679
- return new ReallyMeCryptoError("provider-failure");
680
- }
681
- };
682
- export const cryptoErrorToProto = (error) => {
683
- switch (error.code) {
684
- case "invalid-input":
685
- return create(CryptoErrorSchema, {
686
- error: {
687
- case: "primitive",
688
- value: create(CryptoPrimitiveErrorSchema, {
689
- reason: CryptoErrorReason.PRIMITIVE_INVALID_PARAMETER,
690
- }),
691
- },
692
- });
693
- case "invalid-signature":
694
- return create(CryptoErrorSchema, {
695
- error: {
696
- case: "primitive",
697
- value: create(CryptoPrimitiveErrorSchema, {
698
- reason: CryptoErrorReason.PRIMITIVE_INVALID_SIGNATURE,
699
- }),
700
- },
701
- });
702
- case "authentication-failed":
703
- return create(CryptoErrorSchema, {
704
- error: {
705
- case: "primitive",
706
- value: create(CryptoPrimitiveErrorSchema, {
707
- reason: CryptoErrorReason.PRIMITIVE_AUTHENTICATION_FAILED,
708
- }),
709
- },
710
- });
711
- case "provider-failure":
712
- return create(CryptoErrorSchema, {
713
- error: {
714
- case: "backend",
715
- value: create(CryptoBackendErrorSchema, {
716
- reason: CryptoErrorReason.BACKEND_INTERNAL,
717
- }),
718
- },
719
- });
720
- case "unsupported-algorithm":
721
- return create(CryptoErrorSchema, {
722
- error: {
723
- case: "provider",
724
- value: create(CryptoProviderErrorSchema, {
725
- reason: CryptoErrorReason.PROVIDER_UNSUPPORTED_ALGORITHM,
726
- }),
727
- },
728
- });
729
- }
730
- };
731
- export const cryptoErrorToProtoBytes = (error) => toBinary(CryptoErrorSchema, cryptoErrorToProto(error));
732
- export const cryptoErrorFromProto = (value) => {
733
- return cryptoWireErrorToFacadeError(cryptoWireErrorFromProto(value));
734
- };
735
- export const cryptoErrorFromProtoBytes = (bytes) => {
736
- return cryptoWireErrorToFacadeError(cryptoWireErrorFromProtoBytes(bytes));
737
- };
738
- const signatureJwkAlgorithmToProto = (value) => {
739
- switch (value) {
740
- case "Ed25519":
741
- return SignatureAlgorithm.ED25519;
742
- case "P-256":
743
- return SignatureAlgorithm.ECDSA_P256_SHA256;
744
- case "secp256k1":
745
- return SignatureAlgorithm.ECDSA_SECP256K1_SHA256;
746
- case "ML-DSA-44":
747
- return SignatureAlgorithm.ML_DSA_44;
748
- case "ML-DSA-65":
749
- return SignatureAlgorithm.ML_DSA_65;
750
- case "ML-DSA-87":
751
- return SignatureAlgorithm.ML_DSA_87;
752
- case "SLH-DSA-SHA2-128s":
753
- return SignatureAlgorithm.SLH_DSA_SHA2_128S;
754
- default:
755
- throw new ReallyMeCryptoError("unsupported-algorithm");
756
- }
757
- };
758
- const jwkAlgorithmToProto = (value) => {
759
- switch (value) {
760
- case "X25519":
761
- return create(CryptoAlgorithmIdentifierSchema, {
762
- algorithm: {
763
- case: "keyAgreement",
764
- value: KeyAgreementAlgorithm.X25519,
765
- },
766
- });
767
- case "ML-KEM-512":
768
- return create(CryptoAlgorithmIdentifierSchema, {
769
- algorithm: {
770
- case: "kem",
771
- value: KemAlgorithm.ML_KEM_512,
772
- },
773
- });
774
- case "ML-KEM-768":
775
- return create(CryptoAlgorithmIdentifierSchema, {
776
- algorithm: {
777
- case: "kem",
778
- value: KemAlgorithm.ML_KEM_768,
779
- },
780
- });
781
- case "ML-KEM-1024":
782
- return create(CryptoAlgorithmIdentifierSchema, {
783
- algorithm: {
784
- case: "kem",
785
- value: KemAlgorithm.ML_KEM_1024,
786
- },
787
- });
788
- case "X-Wing-768":
789
- return create(CryptoAlgorithmIdentifierSchema, {
790
- algorithm: {
791
- case: "kem",
792
- value: KemAlgorithm.X_WING_768,
793
- },
794
- });
795
- default:
796
- return create(CryptoAlgorithmIdentifierSchema, {
797
- algorithm: {
798
- case: "signature",
799
- value: signatureJwkAlgorithmToProto(value),
800
- },
801
- });
802
- }
803
- };
804
- const signatureJwkAlgorithmFromProto = (value) => {
805
- switch (value) {
806
- case SignatureAlgorithm.ED25519:
807
- return "Ed25519";
808
- case SignatureAlgorithm.ECDSA_P256_SHA256:
809
- return "P-256";
810
- case SignatureAlgorithm.ECDSA_SECP256K1_SHA256:
811
- return "secp256k1";
812
- case SignatureAlgorithm.ML_DSA_44:
813
- return "ML-DSA-44";
814
- case SignatureAlgorithm.ML_DSA_65:
815
- return "ML-DSA-65";
816
- case SignatureAlgorithm.ML_DSA_87:
817
- return "ML-DSA-87";
818
- case SignatureAlgorithm.SLH_DSA_SHA2_128S:
819
- return "SLH-DSA-SHA2-128s";
820
- default:
821
- throw new ReallyMeCryptoError("unsupported-algorithm");
822
- }
823
- };
824
- const jwkAlgorithmFromProto = (value) => {
825
- switch (value.algorithm.case) {
826
- case "signature":
827
- return signatureJwkAlgorithmFromProto(value.algorithm.value);
828
- case "keyAgreement":
829
- if (value.algorithm.value === KeyAgreementAlgorithm.X25519) {
830
- return "X25519";
831
- }
832
- throw new ReallyMeCryptoError("unsupported-algorithm");
833
- case "kem":
834
- switch (value.algorithm.value) {
835
- case KemAlgorithm.ML_KEM_512:
836
- return "ML-KEM-512";
837
- case KemAlgorithm.ML_KEM_768:
838
- return "ML-KEM-768";
839
- case KemAlgorithm.ML_KEM_1024:
840
- return "ML-KEM-1024";
841
- case KemAlgorithm.X_WING_768:
842
- return "X-Wing-768";
843
- default:
844
- throw new ReallyMeCryptoError("unsupported-algorithm");
845
- }
846
- default:
847
- throw new ReallyMeCryptoError("unsupported-algorithm");
848
- }
849
- };
850
- export const jsonWebKeyToProto = (key) => create(JsonWebKeySchema, {
851
- algorithm: jwkAlgorithmToProto(key.algorithm),
852
- publicKey: key.publicKey,
853
- canonicalJcs: asciiToBytes(ReallyMeJwk.toJcs(key.jwk)),
854
- });
855
- export const jsonWebKeyToProtoBytes = (key) => toBinary(JsonWebKeySchema, jsonWebKeyToProto(key));
856
- export const jsonWebKeyFromProto = (value) => withJwkProtoBoundaryErrors(() => {
857
- // Read each field once so a hostile message proxy cannot change values
858
- // between validation and construction.
859
- const algorithmValue = value.algorithm;
860
- const publicKey = value.publicKey;
861
- const canonicalJcs = value.canonicalJcs;
862
- const algorithm = algorithmValue === undefined
863
- ? undefined
864
- : jwkAlgorithmFromProto(algorithmValue);
865
- if (algorithm === undefined) {
866
- throw new ReallyMeCryptoError("invalid-input");
867
- }
868
- ensureByteArrayAtMost(publicKey, MAX_CRYPTO_INPUT_LENGTH);
869
- const jwk = ReallyMeJwk.toJwk(algorithm, publicKey);
870
- if (canonicalJcs.length > 0) {
871
- const expected = ReallyMeJwk.toJcs(jwk);
872
- if (asciiFromBytes(canonicalJcs) !== expected) {
873
- throw new ReallyMeCryptoError("invalid-input");
874
- }
875
- }
876
- return { algorithm, publicKey, jwk };
877
- });
878
- export const jsonWebKeyFromProtoBytes = (bytes) => withJwkProtoBoundaryErrors(() => {
879
- ensureByteArrayAtMost(bytes, MAX_CRYPTO_INPUT_LENGTH);
880
- return jsonWebKeyFromProto(fromBinary(JsonWebKeySchema, bytes));
881
- });
882
- export const jsonWebKeySetToProto = (keySet) => create(JsonWebKeySetSchema, {
883
- keys: keySet.keys.map((key) => jsonWebKeyToProto(key)),
884
- });
885
- export const jsonWebKeySetToProtoBytes = (keySet) => toBinary(JsonWebKeySetSchema, jsonWebKeySetToProto(keySet));
886
- export const jsonWebKeySetFromProto = (value) => withJwkProtoBoundaryErrors(() => {
887
- const keys = value.keys;
888
- if (!Array.isArray(keys) || keys.length > MAX_JWKS_KEYS) {
889
- throw new ReallyMeCryptoError("invalid-input");
890
- }
891
- return { keys: keys.map((key) => jsonWebKeyFromProto(key)) };
892
- });
893
- export const jsonWebKeySetFromProtoBytes = (bytes) => withJwkProtoBoundaryErrors(() => {
894
- ensureByteArrayAtMost(bytes, MAX_CRYPTO_INPUT_LENGTH);
895
- return jsonWebKeySetFromProto(fromBinary(JsonWebKeySetSchema, bytes));
896
- });
897
- const keyPairToProto = (algorithm, keyPair) => create(CryptoKeyPairSchema, {
898
- algorithm,
899
- publicKey: keyPair.publicKey,
900
- secretKey: keyPair.secretKey,
901
- });
902
- export const signatureKeyPairToProto = (algorithm, keyPair) => keyPairToProto(signatureAlgorithmIdentifierToProto(algorithm), keyPair);
903
- export const signatureKeyPairToProtoBytes = (algorithm, keyPair) => toBinary(CryptoKeyPairSchema, signatureKeyPairToProto(algorithm, keyPair));
904
- export const signatureKeyPairFromProto = (value) => ({
905
- algorithm: signatureAlgorithmFromIdentifier(value.algorithm),
906
- keyPair: {
907
- publicKey: value.publicKey,
908
- secretKey: value.secretKey,
909
- },
910
- });
911
- export const signatureKeyPairFromProtoBytes = (bytes) => {
912
- try {
913
- return signatureKeyPairFromProto(fromBinary(CryptoKeyPairSchema, bytes));
914
- }
915
- catch (error) {
916
- if (error instanceof ReallyMeCryptoError) {
917
- throw error;
918
- }
919
- throw new ReallyMeCryptoError("invalid-input");
920
- }
921
- };
922
- export const keyAgreementKeyPairToProto = (algorithm, keyPair) => keyPairToProto(keyAgreementAlgorithmIdentifierToProto(algorithm), keyPair);
923
- export const keyAgreementKeyPairToProtoBytes = (algorithm, keyPair) => toBinary(CryptoKeyPairSchema, keyAgreementKeyPairToProto(algorithm, keyPair));
924
- export const keyAgreementKeyPairFromProto = (value) => ({
925
- algorithm: keyAgreementAlgorithmFromIdentifier(value.algorithm),
926
- keyPair: {
927
- publicKey: value.publicKey,
928
- secretKey: value.secretKey,
929
- },
930
- });
931
- export const keyAgreementKeyPairFromProtoBytes = (bytes) => {
932
- try {
933
- return keyAgreementKeyPairFromProto(fromBinary(CryptoKeyPairSchema, bytes));
934
- }
935
- catch (error) {
936
- if (error instanceof ReallyMeCryptoError) {
937
- throw error;
938
- }
939
- throw new ReallyMeCryptoError("invalid-input");
940
- }
941
- };
942
- export const kemKeyPairToProto = (algorithm, keyPair) => keyPairToProto(kemAlgorithmIdentifierToProto(algorithm), keyPair);
943
- export const kemKeyPairToProtoBytes = (algorithm, keyPair) => toBinary(CryptoKeyPairSchema, kemKeyPairToProto(algorithm, keyPair));
944
- export const kemKeyPairFromProto = (value) => ({
945
- algorithm: kemAlgorithmFromIdentifier(value.algorithm),
946
- keyPair: {
947
- publicKey: value.publicKey,
948
- secretKey: value.secretKey,
949
- },
950
- });
951
- export const kemKeyPairFromProtoBytes = (bytes) => {
952
- try {
953
- return kemKeyPairFromProto(fromBinary(CryptoKeyPairSchema, bytes));
954
- }
955
- catch (error) {
956
- if (error instanceof ReallyMeCryptoError) {
957
- throw error;
958
- }
959
- throw new ReallyMeCryptoError("invalid-input");
960
- }
961
- };
962
- export const kemEncapsulationToProto = (algorithm, encapsulation) => create(CryptoKemEncapsulationSchema, {
963
- algorithm: kemAlgorithmIdentifierToProto(algorithm),
964
- ciphertext: encapsulation.ciphertext,
965
- sharedSecret: encapsulation.sharedSecret,
966
- });
967
- export const kemEncapsulationToProtoBytes = (algorithm, encapsulation) => toBinary(CryptoKemEncapsulationSchema, kemEncapsulationToProto(algorithm, encapsulation));
968
- export const kemEncapsulationFromProto = (value) => ({
969
- algorithm: kemAlgorithmFromIdentifier(value.algorithm),
970
- encapsulation: {
971
- ciphertext: value.ciphertext,
972
- sharedSecret: value.sharedSecret,
973
- },
974
- });
975
- export const kemEncapsulationFromProtoBytes = (bytes) => {
976
- try {
977
- return kemEncapsulationFromProto(fromBinary(CryptoKemEncapsulationSchema, bytes));
978
- }
979
- catch (error) {
980
- if (error instanceof ReallyMeCryptoError) {
981
- throw error;
982
- }
983
- throw new ReallyMeCryptoError("invalid-input");
984
- }
985
- };
986
- export const hpkeSealedMessageToProto = (suite, sealedMessage) => create(CryptoHpkeSealedMessageSchema, {
987
- algorithm: hpkeSuiteIdentifierToProto(suite),
988
- encapsulatedKey: sealedMessage.encapsulatedKey,
989
- ciphertext: sealedMessage.ciphertext,
990
- });
991
- export const hpkeSealedMessageToProtoBytes = (suite, sealedMessage) => toBinary(CryptoHpkeSealedMessageSchema, hpkeSealedMessageToProto(suite, sealedMessage));
992
- export const hpkeSealedMessageFromProto = (value) => ({
993
- suite: hpkeSuiteFromIdentifier(value.algorithm),
994
- sealedMessage: {
995
- encapsulatedKey: value.encapsulatedKey,
996
- ciphertext: value.ciphertext,
997
- },
998
- });
999
- export const hpkeSealedMessageFromProtoBytes = (bytes) => {
1000
- try {
1001
- return hpkeSealedMessageFromProto(fromBinary(CryptoHpkeSealedMessageSchema, bytes));
1002
- }
1003
- catch (error) {
1004
- if (error instanceof ReallyMeCryptoError) {
1005
- throw error;
1006
- }
1007
- throw new ReallyMeCryptoError("invalid-input");
1008
- }
1009
- };
1010
- export const verificationResultToProto = (algorithm, valid) => create(CryptoVerificationResultSchema, {
1011
- algorithm,
1012
- status: valid ? CryptoVerificationStatus.VALID : CryptoVerificationStatus.INVALID,
1013
- });
1014
- export const verificationErrorToProto = (algorithm, error) => create(CryptoVerificationResultSchema, {
1015
- algorithm,
1016
- status: CryptoVerificationStatus.ERROR,
1017
- error: cryptoErrorToProto(error),
1018
- });
1019
- export const verificationResultToProtoBytes = (value) => toBinary(CryptoVerificationResultSchema, value);
1020
- export const verificationResultFromProtoBytes = (bytes) => {
1021
- try {
1022
- return fromBinary(CryptoVerificationResultSchema, bytes);
1023
- }
1024
- catch {
1025
- throw new ReallyMeCryptoError("invalid-input");
1026
- }
1027
- };
1028
- const providerSupportStatusToProto = (value) => {
1029
- switch (value) {
1030
- case "partial":
1031
- return CryptoProviderSupportStatus.PARTIAL;
1032
- case "provider-aware":
1033
- return CryptoProviderSupportStatus.PROVIDER_AWARE;
1034
- case "supported":
1035
- return CryptoProviderSupportStatus.SUPPORTED;
1036
- case "unsupported":
1037
- return CryptoProviderSupportStatus.UNSUPPORTED;
1038
- }
1039
- };
1040
- const providerSupportStatusFromProto = (value) => {
1041
- switch (value) {
1042
- case CryptoProviderSupportStatus.PARTIAL:
1043
- return "partial";
1044
- case CryptoProviderSupportStatus.PROVIDER_AWARE:
1045
- return "provider-aware";
1046
- case CryptoProviderSupportStatus.SUPPORTED:
1047
- return "supported";
1048
- case CryptoProviderSupportStatus.UNSUPPORTED:
1049
- return "unsupported";
1050
- default:
1051
- throw new ReallyMeCryptoError("invalid-input");
1052
- }
1053
- };
1054
- export const providerCapabilityToProto = (value) => create(CryptoProviderCapabilitySchema, {
1055
- algorithm: value.algorithm,
1056
- family: value.family,
1057
- providerNames: [...value.providerNames],
1058
- status: providerSupportStatusToProto(value.status),
1059
- usesRust: value.usesRust,
1060
- });
1061
- export const providerCapabilityFromProto = (value) => {
1062
- if (value.algorithm === undefined || value.family === CryptoAlgorithmFamily.UNSPECIFIED) {
1063
- throw new ReallyMeCryptoError("invalid-input");
1064
- }
1065
- return {
1066
- algorithm: value.algorithm,
1067
- family: value.family,
1068
- providerNames: [...value.providerNames],
1069
- status: providerSupportStatusFromProto(value.status),
1070
- usesRust: value.usesRust,
1071
- };
1072
- };
1073
- export const providerCapabilitySetToProto = (values) => create(CryptoProviderCapabilitySetSchema, {
1074
- capabilities: values.map((value) => providerCapabilityToProto(value)),
1075
- });
1076
- export const providerCapabilitySetToProtoBytes = (values) => toBinary(CryptoProviderCapabilitySetSchema, providerCapabilitySetToProto(values));
1077
- export const providerCapabilitySetFromProto = (value) => value.capabilities.map((capability) => providerCapabilityFromProto(capability));
1078
- export const providerCapabilitySetFromProtoBytes = (bytes) => {
1079
- try {
1080
- return providerCapabilitySetFromProto(fromBinary(CryptoProviderCapabilitySetSchema, bytes));
1081
- }
1082
- catch (error) {
1083
- if (error instanceof ReallyMeCryptoError) {
1084
- throw error;
1085
- }
1086
- throw new ReallyMeCryptoError("invalid-input");
1087
- }
1088
- };
5
+ export { aeadAlgorithmFromProto, aeadAlgorithmToProto, hashAlgorithmFromProto, hashAlgorithmToProto, hpkeSuiteFromProto, hpkeSuiteToProto, kdfAlgorithmFromProto, kdfAlgorithmToProto, kemAlgorithmFromProto, kemAlgorithmToProto, keyAgreementAlgorithmFromProto, keyAgreementAlgorithmToProto, keyWrapAlgorithmFromProto, keyWrapAlgorithmToProto, macAlgorithmFromProto, macAlgorithmToProto, signatureAlgorithmFromProto, signatureAlgorithmToProto, } from "./protoAlgorithms.js";
6
+ export { multicodecKeyAlgorithmFromProto, multicodecKeyAlgorithmToProto, } from "./protoIdentifiers.js";
7
+ export { cryptoErrorFromProto, cryptoErrorFromProtoBytes, cryptoErrorToProto, cryptoErrorToProtoBytes, cryptoWireErrorFromProto, cryptoWireErrorFromProtoBytes, cryptoWireErrorToFacadeError, cryptoWireErrorToProto, cryptoWireErrorToProtoBytes, cryptoWireErrorTryNew, } from "./protoErrors.js";
8
+ export { jsonWebKeyFromProto, jsonWebKeyFromProtoBytes, jsonWebKeySetFromProto, jsonWebKeySetFromProtoBytes, jsonWebKeySetToProto, jsonWebKeySetToProtoBytes, jsonWebKeyToProto, jsonWebKeyToProtoBytes, } from "./protoJwk.js";
9
+ export { hpkeSealedMessageFromProto, hpkeSealedMessageFromProtoBytes, hpkeSealedMessageToProto, hpkeSealedMessageToProtoBytes, kemEncapsulationFromProto, kemEncapsulationFromProtoBytes, kemEncapsulationToProto, kemEncapsulationToProtoBytes, kemKeyPairFromProto, kemKeyPairFromProtoBytes, kemKeyPairToProto, kemKeyPairToProtoBytes, keyAgreementKeyPairFromProto, keyAgreementKeyPairFromProtoBytes, keyAgreementKeyPairToProto, keyAgreementKeyPairToProtoBytes, providerCapabilityFromProto, providerCapabilitySetFromProto, providerCapabilitySetFromProtoBytes, providerCapabilitySetToProto, providerCapabilitySetToProtoBytes, providerCapabilityToProto, signatureKeyPairFromProto, signatureKeyPairFromProtoBytes, signatureKeyPairToProto, signatureKeyPairToProtoBytes, verificationErrorToProto, verificationResultFromProtoBytes, verificationResultToProto, verificationResultToProtoBytes, } from "./protoValues.js";