@reallyme/crypto 0.1.8 → 0.3.0

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 (61) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +148 -18
  3. package/dist/aead.d.ts +4 -0
  4. package/dist/aead.js +40 -16
  5. package/dist/aesKw.d.ts +8 -2
  6. package/dist/aesKw.js +69 -12
  7. package/dist/algorithms.d.ts +12 -4
  8. package/dist/algorithms.js +8 -2
  9. package/dist/argon2id.d.ts +3 -0
  10. package/dist/argon2id.js +18 -3
  11. package/dist/cryptoFacade.d.ts +66 -5
  12. package/dist/cryptoFacade.js +99 -38
  13. package/dist/errors.d.ts +1 -1
  14. package/dist/hkdf.d.ts +1 -0
  15. package/dist/hkdf.js +5 -1
  16. package/dist/hmac.d.ts +3 -0
  17. package/dist/hmac.js +24 -3
  18. package/dist/hpke.d.ts +4 -1
  19. package/dist/hpke.js +45 -14
  20. package/dist/index.d.ts +14 -13
  21. package/dist/index.js +12 -10
  22. package/dist/jwaConcatKdf.js +8 -4
  23. package/dist/jwk.d.ts +2 -1
  24. package/dist/jwk.js +158 -122
  25. package/dist/kmac.d.ts +11 -0
  26. package/dist/kmac.js +49 -0
  27. package/dist/memory.d.ts +8 -0
  28. package/dist/memory.js +13 -0
  29. package/dist/mlDsa.d.ts +5 -0
  30. package/dist/mlDsa.js +43 -9
  31. package/dist/mlKem.d.ts +5 -2
  32. package/dist/mlKem.js +37 -26
  33. package/dist/operationResponse.d.ts +16 -0
  34. package/dist/operationResponse.js +46 -0
  35. package/dist/p256Ecdh.js +5 -1
  36. package/dist/p384Ecdh.js +5 -1
  37. package/dist/p521Ecdh.js +5 -1
  38. package/dist/pbkdf2.d.ts +6 -8
  39. package/dist/pbkdf2.js +12 -12
  40. package/dist/proto/generated/reallyme/crypto/v1/crypto_pb.d.ts +2859 -128
  41. package/dist/proto/generated/reallyme/crypto/v1/crypto_pb.js +992 -132
  42. package/dist/proto.d.ts +99 -7
  43. package/dist/proto.js +741 -16
  44. package/dist/rsa.d.ts +4 -1
  45. package/dist/rsa.js +23 -16
  46. package/dist/slhDsa.d.ts +5 -0
  47. package/dist/slhDsa.js +40 -9
  48. package/dist/validateBytes.d.ts +7 -1
  49. package/dist/validateBytes.js +63 -3
  50. package/dist/wasm/reallyme_crypto_wasm.js +77 -365
  51. package/dist/wasm/reallyme_crypto_wasm_bg.wasm +0 -0
  52. package/dist/wasmModuleTypes.d.ts +9 -21
  53. package/dist/wasmProvider.d.ts +12 -29
  54. package/dist/wasmProvider.js +25 -43
  55. package/dist/xWing.d.ts +5 -4
  56. package/dist/xWing.js +40 -40
  57. package/package.json +20 -2
  58. package/dist/codecs.d.ts +0 -35
  59. package/dist/codecs.js +0 -216
  60. package/dist/proto/generated/reallyme/codec/v1/codec_pb.d.ts +0 -253
  61. package/dist/proto/generated/reallyme/codec/v1/codec_pb.js +0 -160
package/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md CHANGED
@@ -9,14 +9,13 @@ SPDX-License-Identifier: Apache-2.0
9
9
  [![npm](https://img.shields.io/npm/v/@reallyme/crypto?label=npm&color=2563eb)](https://www.npmjs.com/package/@reallyme/crypto)
10
10
  [![License](https://img.shields.io/badge/license-Apache--2.0-blue)](https://github.com/reallyme/crypto/blob/main/LICENSE)
11
11
 
12
- ReallyMe Crypto provides a platform-agnostic cryptography API for TypeScript,
13
- Rust, Swift, and Kotlin.
12
+ `@reallyme/crypto` is the TypeScript SDK for ReallyMe Crypto. It provides typed,
13
+ synchronous APIs for Node.js and browsers, with explicit JavaScript and
14
+ package-owned WASM providers.
14
15
 
15
- Applications can implement cryptographic operations once and rely on identical
16
- algorithms, key formats, verification behavior, and protocol contracts across
17
- servers, browsers, iOS, Android, and WASM. Native platform providers are used
18
- where appropriate, while shared conformance vectors ensure byte-for-byte
19
- compatible behavior across every supported language.
16
+ The package shares algorithm identifiers, byte formats, typed failures, and
17
+ conformance vectors with the Rust, Swift, Kotlin, and Android SDKs. Availability
18
+ is provider-specific; unsupported routes fail closed.
20
19
 
21
20
  ## Why
22
21
 
@@ -24,10 +23,9 @@ Modern cryptography APIs differ across platforms. Algorithms are exposed
24
23
  differently, key formats vary, providers have different capabilities, and error
25
24
  behavior is inconsistent.
26
25
 
27
- ReallyMe Crypto provides one consistent cryptography contract across every
28
- supported platform. The same application logic can be shared between backend
29
- services, mobile applications, and browsers without maintaining separate
30
- cryptographic implementations.
26
+ ReallyMe Crypto makes platform differences explicit while preserving a stable
27
+ contract for every supported route. Applications can share protocol logic
28
+ without silently changing cryptographic providers.
31
29
 
32
30
  ## Installation
33
31
 
@@ -49,29 +47,146 @@ const digest = ReallyMeCrypto.hash(
49
47
  The API is synchronous. Signature verification throws `ReallyMeCryptoError` on
50
48
  invalid input rather than returning a boolean that can be accidentally ignored.
51
49
 
50
+ KDF facade selectors are family-specific: `deriveKey` accepts only PBKDF2,
51
+ `deriveHkdf` accepts only HKDF, and the JWA Concat KDF and KMAC methods accept
52
+ their exact selectors. Argon2id uses the dedicated `deriveArgon2id` route with
53
+ an immutable profile version rather than an iteration count.
54
+
52
55
  ## Post-Quantum and WASM
53
56
 
54
57
  Classical primitives are backed by pinned `@noble` packages. The primitives that
55
58
  must stay identical to Rust — ML-KEM, ML-DSA, SLH-DSA, X-Wing, Argon2id, HPKE,
56
59
  and others — are backed by a WASM module that ships prebuilt with the package.
57
60
 
58
- Install the WASM provider once at startup before using those algorithms;
59
- otherwise they fail closed with `provider-failure`.
61
+ Applications may either install the WASM provider once for the package-level
62
+ `ReallyMeCrypto` convenience API, or build isolated facade instances with an
63
+ explicit provider object. Missing WASM providers fail closed with
64
+ `provider-failure`. JWK/JWKS helpers delegate base64url and JCS canonicalization
65
+ to the published `@reallyme/codec` package, so applications that use those
66
+ helpers should also install the Codec WASM provider.
67
+
68
+ JSON and protobuf JWKS ingress routes accept at most 1,024 keys. Encoded JWK
69
+ and JWKS protobuf inputs are capped at one mebibyte before decoding, and an
70
+ embedded canonical JCS value is capped at 8 KiB. These limits apply equally to
71
+ the direct message adapters and byte-oriented helpers.
60
72
 
61
73
  ```ts
62
74
  import { readFileSync } from "node:fs";
63
- import { installReallyMeWasmProvider, ReallyMeCrypto } from "@reallyme/crypto";
64
- import * as wasmProvider from "@reallyme/crypto/wasm/reallyme_crypto_wasm.js";
75
+ import { installReallyMeCodecWasmProvider } from "@reallyme/codec";
76
+ import { initSync as initCodecWasm } from "@reallyme/codec/wasm/reallyme_codec_wasm.js";
77
+ import {
78
+ createReallyMeCrypto,
79
+ createReallyMeWasmProvider,
80
+ installReallyMeWasmProvider,
81
+ ReallyMeCrypto,
82
+ } from "@reallyme/crypto";
83
+ import { initSync as initCryptoWasm } from "@reallyme/crypto/wasm/reallyme_crypto_wasm.js";
84
+
85
+ // Provider constructors validate the dynamically imported module as `unknown`
86
+ // against the package's explicit required-export list.
87
+ const wasmProvider: unknown = await import(
88
+ "@reallyme/crypto/wasm/reallyme_crypto_wasm.js"
89
+ );
90
+ const codecWasmProvider: unknown = await import(
91
+ "@reallyme/codec/wasm/reallyme_codec_wasm.js"
92
+ );
65
93
 
66
94
  const wasmUrl = import.meta.resolve(
67
95
  "@reallyme/crypto/wasm/reallyme_crypto_wasm_bg.wasm",
68
96
  );
69
- wasmProvider.initSync({ module: readFileSync(new URL(wasmUrl)) });
97
+ initCryptoWasm({ module: readFileSync(new URL(wasmUrl)) });
70
98
  installReallyMeWasmProvider(wasmProvider);
99
+ const isolatedCrypto = createReallyMeCrypto({
100
+ wasmProvider: createReallyMeWasmProvider(wasmProvider),
101
+ });
102
+
103
+ const codecWasmUrl = import.meta.resolve(
104
+ "@reallyme/codec/wasm/reallyme_codec_wasm_bg.wasm",
105
+ );
106
+ initCodecWasm({ module: readFileSync(new URL(codecWasmUrl)) });
107
+ installReallyMeCodecWasmProvider(codecWasmProvider);
71
108
 
72
109
  const keyPair = ReallyMeCrypto.generateKemKeyPair("X-Wing-768");
110
+ const isolatedKeyPair = isolatedCrypto.generateKemKeyPair("X-Wing-768");
111
+ ```
112
+
113
+ The explicit facade form is preferred for Workers, SSR, tests, and multi-bundle
114
+ applications because each instance owns its provider routing and does not depend
115
+ on package-global mutable state.
116
+
117
+ The package-owned WASM provider derives ML-DSA, ML-KEM, and X-Wing seed-based
118
+ keypairs inside Rust and returns the public key produced by that implementation.
119
+ Custom provider objects are trusted providers: if an application supplies its
120
+ own seed-derived keypair provider, it is responsible for public-key
121
+ correspondence and must be covered by its own conformance evidence.
122
+
123
+ ## Raw WASM Module Contract
124
+
125
+ The raw `@reallyme/crypto/wasm/reallyme_crypto_wasm.js` export is an internal
126
+ provider artifact for installing or constructing `ReallyMeWasmProvider`
127
+ instances. Direct raw WASM calls are unsupported for application logic. Use the
128
+ typed TypeScript facades, which validate caller inputs, numeric ranges, provider
129
+ outputs, aliasing, and cleanup before invoking the raw module.
130
+
131
+ This distinction matters for numeric parameters: JavaScript-to-WASM glue can
132
+ coerce raw `number` arguments before Rust receives them. The supported facades
133
+ reject out-of-range Argon2id, KMAC, HPKE, and RSA selector or length values
134
+ before provider dispatch. Package checks verify that the README, package
135
+ exports, TypeScript declarations, and generated WASM glue continue to agree on
136
+ this support boundary and that no ambient global crypto-provider functions are
137
+ used.
138
+
139
+ Variable-length direct primitive inputs are capped at one mebibyte before any
140
+ WASM copy or provider dispatch. Authenticated ciphertext may additionally carry
141
+ the 16-byte tag. HPKE `info` is capped at 65,530 bytes so the fixed five-byte
142
+ key-schedule label remains within RFC 9180's two-byte context-length limit.
143
+
144
+ ### Browser WASM Lifecycle
145
+
146
+ Argon2id V1 and V2 require 256 MiB and 512 MiB of working memory respectively.
147
+ WebAssembly linear memory does not shrink, so an instance can retain that
148
+ resident allocation after derivation. Browser applications should run Argon2id
149
+ in a short-lived dedicated Worker and terminate the Worker after use, especially
150
+ on memory-constrained devices.
151
+
152
+ Treat a WebAssembly trap as fatal for that provider instance. The SDK maps the
153
+ trap to `provider-failure`, but does not guarantee that an instance interrupted
154
+ mid-operation remains reusable; discard its Worker or provider context before
155
+ performing another operation.
156
+
157
+ The generated loader resolves its `.wasm` file by relative URL and does not
158
+ embed a content hash. The npm tarball protects the installed package artifact,
159
+ but applications that separately host WASM bytes should use immutable asset
160
+ URLs and verify those bytes against a deployment-controlled digest before
161
+ initialization.
162
+
163
+ ## Memory Hygiene
164
+
165
+ Rust-owned secret buffers are zeroized by the Rust implementation and WASM
166
+ adapters. JavaScript `Uint8Array` values are best-effort only: engines, WASM
167
+ marshalling, providers, protobuf codecs, debuggers, and crash reporters can
168
+ create copies outside the SDK's control. Clear caller-owned byte arrays as soon
169
+ as practical:
170
+
171
+ ```ts
172
+ bestEffortClear(secretBytes);
73
173
  ```
74
174
 
175
+ Explicit provider results must be independently owned `Uint8Array` values that
176
+ do not overlap caller inputs or sibling result fields. The facade rejects
177
+ aliases as `provider-failure` without modifying caller storage. Malformed
178
+ provider-owned secret outputs are cleared before the same typed failure is
179
+ returned.
180
+
181
+ AES-KW provider results must have the exact RFC 3394 `plaintext + 8` or
182
+ `wrapped - 8` length. The facade wipes and rejects a returned typed array whose
183
+ length violates that contract. KMAC keys and customization strings are capped
184
+ at 4 KiB, contexts at 64 KiB, and outputs at 64 KiB before WASM copies are
185
+ created.
186
+
187
+ Do not move private keys, passwords, plaintext, shared secrets, or derived keys
188
+ through strings or JSON paths.
189
+
75
190
  ## Features
76
191
 
77
192
  - Platform-agnostic cryptography APIs.
@@ -79,11 +194,26 @@ const keyPair = ReallyMeCrypto.generateKemKeyPair("X-Wing-768");
79
194
  - Shared conformance vectors across Rust, Swift, Kotlin, and TypeScript.
80
195
  - Native providers where available, WASM-backed implementations where needed.
81
196
  - Typed errors and fail-closed verification behavior.
82
- - Protobuf algorithm identifiers for API and storage boundaries, via
83
- `@reallyme/crypto/proto`.
197
+ - Generated protobuf operations, algorithm identifiers, and typed wire errors
198
+ via `@reallyme/crypto/proto`.
84
199
  - JWK, multikey, multicodec, HPKE, ML-KEM, ML-DSA, Ed25519, secp256k1, X25519,
85
200
  AES-GCM, ChaCha20-Poly1305, and more.
86
201
 
202
+ ## Protobuf Process Boundary
203
+
204
+ `processOperationResponse(request)` accepts one generated binary
205
+ `CryptoOperationRequest`. `processOperationResponseJson(requestJson)` accepts
206
+ the strict generated ProtoJSON view only for non-secret hash, verification,
207
+ key-generation, encapsulation, and sender-export requests. Both return binary
208
+ `CryptoOperationResponse` bytes, so malformed input, unsupported algorithms,
209
+ and resource-limit failures preserve their structured protobuf branch and
210
+ reason. This is the single executable structured response contract.
211
+
212
+ ProtoJSON is request-only. Decode returned binary protobuf with the generated
213
+ exports from `@reallyme/crypto/proto`. Secret-bearing operation selectors are
214
+ rejected before JSON value deserialization and must use protobuf bytes. Clear
215
+ caller-owned `Uint8Array` values after use.
216
+
87
217
  ## Documentation
88
218
 
89
219
  The complete documentation, provider matrix, protocol contracts, and conformance
package/dist/aead.d.ts CHANGED
@@ -1,12 +1,16 @@
1
1
  import type { ReallyMeAeadAlgorithm } from "./algorithms.js";
2
+ import type { ReallyMeWasmProvider } from "./wasmProvider.js";
2
3
  export declare const AEAD_KEY_LENGTH = 32;
3
4
  export declare const AES_128_GCM_KEY_LENGTH = 16;
4
5
  export declare const AES_192_GCM_KEY_LENGTH = 24;
5
6
  export declare const AES_256_GCM_KEY_LENGTH = 32;
7
+ export declare const CHACHA20_POLY1305_KEY_LENGTH = 32;
6
8
  export declare const AEAD_NONCE_LENGTH = 12;
7
9
  export declare const XCHACHA20_POLY1305_NONCE_LENGTH = 24;
8
10
  export declare const AEAD_TAG_LENGTH = 16;
9
11
  export declare const ReallyMeAead: {
10
12
  readonly seal: (algorithm: ReallyMeAeadAlgorithm, key: Uint8Array, nonce: Uint8Array, aad: Uint8Array, plaintext: Uint8Array) => Uint8Array;
13
+ readonly sealWithProvider: (provider: ReallyMeWasmProvider, algorithm: ReallyMeAeadAlgorithm, key: Uint8Array, nonce: Uint8Array, aad: Uint8Array, plaintext: Uint8Array) => Uint8Array;
11
14
  readonly open: (algorithm: ReallyMeAeadAlgorithm, key: Uint8Array, nonce: Uint8Array, aad: Uint8Array, ciphertextWithTag: Uint8Array) => Uint8Array;
15
+ readonly openWithProvider: (provider: ReallyMeWasmProvider, algorithm: ReallyMeAeadAlgorithm, key: Uint8Array, nonce: Uint8Array, aad: Uint8Array, ciphertextWithTag: Uint8Array) => Uint8Array;
12
16
  };
package/dist/aead.js CHANGED
@@ -2,17 +2,20 @@
2
2
  //
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  import { ReallyMeCryptoError } from "./errors.js";
5
- import { ensureBytes } from "./validateBytes.js";
5
+ import { ensureByteArrayAtMost, ensureBytes, ensureIndependentByteArray, MAX_CRYPTO_INPUT_LENGTH, } from "./validateBytes.js";
6
6
  import { requireReallyMeWasmProvider } from "./wasmProvider.js";
7
7
  export const AEAD_KEY_LENGTH = 32;
8
8
  export const AES_128_GCM_KEY_LENGTH = 16;
9
9
  export const AES_192_GCM_KEY_LENGTH = 24;
10
10
  export const AES_256_GCM_KEY_LENGTH = 32;
11
+ export const CHACHA20_POLY1305_KEY_LENGTH = 32;
11
12
  export const AEAD_NONCE_LENGTH = 12;
12
13
  export const XCHACHA20_POLY1305_NONCE_LENGTH = 24;
13
14
  export const AEAD_TAG_LENGTH = 16;
14
15
  const aeadSuite = (algorithm) => {
15
- const provider = requireReallyMeWasmProvider();
16
+ return aeadSuiteWithProvider(algorithm, requireReallyMeWasmProvider());
17
+ };
18
+ const aeadSuiteWithProvider = (algorithm, provider) => {
16
19
  switch (algorithm) {
17
20
  case "AES-128-GCM":
18
21
  return {
@@ -44,40 +47,61 @@ const aeadSuite = (algorithm) => {
44
47
  };
45
48
  case "ChaCha20-Poly1305":
46
49
  return {
47
- keyLength: AES_256_GCM_KEY_LENGTH,
50
+ keyLength: CHACHA20_POLY1305_KEY_LENGTH,
48
51
  nonceLength: AEAD_NONCE_LENGTH,
49
52
  seal: provider.chacha20Poly1305Seal,
50
53
  open: provider.chacha20Poly1305Open,
51
54
  };
52
55
  case "XChaCha20-Poly1305":
53
56
  return {
54
- keyLength: AES_256_GCM_KEY_LENGTH,
57
+ keyLength: CHACHA20_POLY1305_KEY_LENGTH,
55
58
  nonceLength: XCHACHA20_POLY1305_NONCE_LENGTH,
56
59
  seal: provider.xchacha20Poly1305Seal,
57
60
  open: provider.xchacha20Poly1305Open,
58
61
  };
59
62
  }
60
63
  };
61
- const requireBytesOutput = (value) => {
64
+ const sealWithSuite = (suite, key, nonce, aad, plaintext) => {
65
+ ensureBytes(key, suite.keyLength);
66
+ ensureBytes(nonce, suite.nonceLength);
67
+ ensureByteArrayAtMost(aad, MAX_CRYPTO_INPUT_LENGTH);
68
+ ensureByteArrayAtMost(plaintext, MAX_CRYPTO_INPUT_LENGTH);
69
+ return requireBytesOutput(suite.seal(key, nonce, aad, plaintext), plaintext.length + AEAD_TAG_LENGTH, [key, nonce, aad, plaintext]);
70
+ };
71
+ const openWithSuite = (suite, key, nonce, aad, ciphertextWithTag) => {
72
+ ensureBytes(key, suite.keyLength);
73
+ ensureBytes(nonce, suite.nonceLength);
74
+ ensureByteArrayAtMost(aad, MAX_CRYPTO_INPUT_LENGTH);
75
+ ensureByteArrayAtMost(ciphertextWithTag, MAX_CRYPTO_INPUT_LENGTH + AEAD_TAG_LENGTH);
76
+ if (ciphertextWithTag.length < AEAD_TAG_LENGTH) {
77
+ throw new ReallyMeCryptoError("invalid-input");
78
+ }
79
+ return requireBytesOutput(suite.open(key, nonce, aad, ciphertextWithTag), ciphertextWithTag.length - AEAD_TAG_LENGTH, [key, nonce, aad, ciphertextWithTag]);
80
+ };
81
+ const requireBytesOutput = (value, expectedLength, inputs) => {
62
82
  if (!(value instanceof Uint8Array)) {
63
83
  throw new ReallyMeCryptoError("provider-failure");
64
84
  }
85
+ // Validate ownership before clearing malformed provider output so an alias
86
+ // cannot turn error cleanup into mutation of caller-owned secret material.
87
+ ensureIndependentByteArray(value, inputs);
88
+ if (value.length !== expectedLength) {
89
+ value.fill(0);
90
+ throw new ReallyMeCryptoError("provider-failure");
91
+ }
65
92
  return value;
66
93
  };
67
94
  export const ReallyMeAead = {
68
95
  seal(algorithm, key, nonce, aad, plaintext) {
69
- const suite = aeadSuite(algorithm);
70
- ensureBytes(key, suite.keyLength);
71
- ensureBytes(nonce, suite.nonceLength);
72
- return requireBytesOutput(suite.seal(key, nonce, aad, plaintext));
96
+ return sealWithSuite(aeadSuite(algorithm), key, nonce, aad, plaintext);
97
+ },
98
+ sealWithProvider(provider, algorithm, key, nonce, aad, plaintext) {
99
+ return sealWithSuite(aeadSuiteWithProvider(algorithm, provider), key, nonce, aad, plaintext);
73
100
  },
74
101
  open(algorithm, key, nonce, aad, ciphertextWithTag) {
75
- const suite = aeadSuite(algorithm);
76
- ensureBytes(key, suite.keyLength);
77
- ensureBytes(nonce, suite.nonceLength);
78
- if (ciphertextWithTag.length < AEAD_TAG_LENGTH) {
79
- throw new ReallyMeCryptoError("invalid-input");
80
- }
81
- return requireBytesOutput(suite.open(key, nonce, aad, ciphertextWithTag));
102
+ return openWithSuite(aeadSuite(algorithm), key, nonce, aad, ciphertextWithTag);
103
+ },
104
+ openWithProvider(provider, algorithm, key, nonce, aad, ciphertextWithTag) {
105
+ return openWithSuite(aeadSuiteWithProvider(algorithm, provider), key, nonce, aad, ciphertextWithTag);
82
106
  },
83
107
  };
package/dist/aesKw.d.ts CHANGED
@@ -1,3 +1,7 @@
1
+ import type { ReallyMeWasmProvider } from "./wasmProvider.js";
2
+ import type { ReallyMeKeyWrapAlgorithm } from "./algorithms.js";
3
+ export declare const AES_128_KW_KEK_LENGTH = 16;
4
+ export declare const AES_192_KW_KEK_LENGTH = 24;
1
5
  export declare const AES_256_KW_KEK_LENGTH = 32;
2
6
  export declare const AES_KW_BLOCK_LENGTH = 8;
3
7
  export declare const AES_KW_INTEGRITY_CHECK_LENGTH = 8;
@@ -5,6 +9,8 @@ export declare const AES_KW_MIN_KEY_DATA_LENGTH = 16;
5
9
  export declare const AES_KW_MIN_WRAPPED_KEY_LENGTH: number;
6
10
  export declare const AES_KW_MAX_KEY_DATA_LENGTH = 4096;
7
11
  export declare const ReallyMeAesKw: {
8
- readonly wrapKey: (wrappingKey: Uint8Array, keyToWrap: Uint8Array) => Uint8Array;
9
- readonly unwrapKey: (wrappingKey: Uint8Array, wrappedKey: Uint8Array) => Uint8Array;
12
+ readonly wrapKey: (algorithm: ReallyMeKeyWrapAlgorithm, wrappingKey: Uint8Array, keyToWrap: Uint8Array) => Uint8Array;
13
+ readonly wrapKeyWithProvider: (algorithm: ReallyMeKeyWrapAlgorithm, provider: ReallyMeWasmProvider, wrappingKey: Uint8Array, keyToWrap: Uint8Array) => Uint8Array;
14
+ readonly unwrapKey: (algorithm: ReallyMeKeyWrapAlgorithm, wrappingKey: Uint8Array, wrappedKey: Uint8Array) => Uint8Array;
15
+ readonly unwrapKeyWithProvider: (algorithm: ReallyMeKeyWrapAlgorithm, provider: ReallyMeWasmProvider, wrappingKey: Uint8Array, wrappedKey: Uint8Array) => Uint8Array;
10
16
  };
package/dist/aesKw.js CHANGED
@@ -2,8 +2,10 @@
2
2
  //
3
3
  // SPDX-License-Identifier: Apache-2.0
4
4
  import { ReallyMeCryptoError } from "./errors.js";
5
- import { ensureBytes } from "./validateBytes.js";
5
+ import { ensureByteArray, ensureBytes, ensureIndependentByteArray, } from "./validateBytes.js";
6
6
  import { requireReallyMeWasmProvider } from "./wasmProvider.js";
7
+ export const AES_128_KW_KEK_LENGTH = 16;
8
+ export const AES_192_KW_KEK_LENGTH = 24;
7
9
  export const AES_256_KW_KEK_LENGTH = 32;
8
10
  export const AES_KW_BLOCK_LENGTH = 8;
9
11
  export const AES_KW_INTEGRITY_CHECK_LENGTH = 8;
@@ -25,21 +27,76 @@ const validateWrappedKeyLength = (length) => {
25
27
  throw new ReallyMeCryptoError("invalid-input");
26
28
  }
27
29
  };
28
- const requireBytesOutput = (value) => {
30
+ const requireBytesOutput = (value, expectedLength, inputs) => {
29
31
  if (!(value instanceof Uint8Array)) {
30
32
  throw new ReallyMeCryptoError("provider-failure");
31
33
  }
34
+ // Reject aliases before wiping malformed output. Otherwise a provider could
35
+ // return a caller-owned key/input view and make validation erase that input.
36
+ ensureIndependentByteArray(value, inputs);
37
+ if (value.length !== expectedLength) {
38
+ value.fill(0);
39
+ throw new ReallyMeCryptoError("provider-failure");
40
+ }
32
41
  return value;
33
42
  };
43
+ const kekLengthForAlgorithm = (algorithm) => {
44
+ switch (algorithm) {
45
+ case "AES-128-KW":
46
+ return AES_128_KW_KEK_LENGTH;
47
+ case "AES-192-KW":
48
+ return AES_192_KW_KEK_LENGTH;
49
+ case "AES-256-KW":
50
+ return AES_256_KW_KEK_LENGTH;
51
+ }
52
+ };
53
+ const wrapFunctionForAlgorithm = (provider, algorithm) => {
54
+ switch (algorithm) {
55
+ case "AES-128-KW":
56
+ return provider.aes128KwWrapKey;
57
+ case "AES-192-KW":
58
+ return provider.aes192KwWrapKey;
59
+ case "AES-256-KW":
60
+ return provider.aes256KwWrapKey;
61
+ }
62
+ };
63
+ const unwrapFunctionForAlgorithm = (provider, algorithm) => {
64
+ switch (algorithm) {
65
+ case "AES-128-KW":
66
+ return provider.aes128KwUnwrapKey;
67
+ case "AES-192-KW":
68
+ return provider.aes192KwUnwrapKey;
69
+ case "AES-256-KW":
70
+ return provider.aes256KwUnwrapKey;
71
+ }
72
+ };
73
+ const wrapKey = (algorithm, wrappingKey, keyToWrap) => {
74
+ ensureBytes(wrappingKey, kekLengthForAlgorithm(algorithm));
75
+ ensureByteArray(keyToWrap);
76
+ validateKeyDataLength(keyToWrap.length);
77
+ return requireBytesOutput(wrapFunctionForAlgorithm(requireReallyMeWasmProvider(), algorithm)(wrappingKey, keyToWrap), keyToWrap.length + AES_KW_INTEGRITY_CHECK_LENGTH, [wrappingKey, keyToWrap]);
78
+ };
79
+ const wrapKeyWithProvider = (algorithm, provider, wrappingKey, keyToWrap) => {
80
+ ensureBytes(wrappingKey, kekLengthForAlgorithm(algorithm));
81
+ ensureByteArray(keyToWrap);
82
+ validateKeyDataLength(keyToWrap.length);
83
+ return requireBytesOutput(wrapFunctionForAlgorithm(provider, algorithm)(wrappingKey, keyToWrap), keyToWrap.length + AES_KW_INTEGRITY_CHECK_LENGTH, [wrappingKey, keyToWrap]);
84
+ };
85
+ const unwrapKey = (algorithm, wrappingKey, wrappedKey) => {
86
+ ensureBytes(wrappingKey, kekLengthForAlgorithm(algorithm));
87
+ ensureByteArray(wrappedKey);
88
+ validateWrappedKeyLength(wrappedKey.length);
89
+ return requireBytesOutput(unwrapFunctionForAlgorithm(requireReallyMeWasmProvider(), algorithm)(wrappingKey, wrappedKey), wrappedKey.length - AES_KW_INTEGRITY_CHECK_LENGTH, [wrappingKey, wrappedKey]);
90
+ };
91
+ const unwrapKeyWithProvider = (algorithm, provider, wrappingKey, wrappedKey) => {
92
+ ensureBytes(wrappingKey, kekLengthForAlgorithm(algorithm));
93
+ ensureByteArray(wrappedKey);
94
+ validateWrappedKeyLength(wrappedKey.length);
95
+ return requireBytesOutput(unwrapFunctionForAlgorithm(provider, algorithm)(wrappingKey, wrappedKey), wrappedKey.length - AES_KW_INTEGRITY_CHECK_LENGTH, [wrappingKey, wrappedKey]);
96
+ };
34
97
  export const ReallyMeAesKw = {
35
- wrapKey(wrappingKey, keyToWrap) {
36
- ensureBytes(wrappingKey, AES_256_KW_KEK_LENGTH);
37
- validateKeyDataLength(keyToWrap.length);
38
- return requireBytesOutput(requireReallyMeWasmProvider().aes256KwWrapKey(wrappingKey, keyToWrap));
39
- },
40
- unwrapKey(wrappingKey, wrappedKey) {
41
- ensureBytes(wrappingKey, AES_256_KW_KEK_LENGTH);
42
- validateWrappedKeyLength(wrappedKey.length);
43
- return requireBytesOutput(requireReallyMeWasmProvider().aes256KwUnwrapKey(wrappingKey, wrappedKey));
44
- },
98
+ wrapKey,
99
+ wrapKeyWithProvider,
100
+ unwrapKey,
101
+ unwrapKeyWithProvider,
45
102
  };