@shieldfive/crypto 1.0.0-alpha.2

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 (104) hide show
  1. package/CHANGELOG.md +81 -0
  2. package/LICENSE +201 -0
  3. package/README.md +241 -0
  4. package/SECURITY.md +123 -0
  5. package/dist/format/header.d.ts +60 -0
  6. package/dist/format/header.d.ts.map +1 -0
  7. package/dist/format/header.js +203 -0
  8. package/dist/format/header.js.map +1 -0
  9. package/dist/identity/index.d.ts +117 -0
  10. package/dist/identity/index.d.ts.map +1 -0
  11. package/dist/identity/index.js +217 -0
  12. package/dist/identity/index.js.map +1 -0
  13. package/dist/index.d.ts +52 -0
  14. package/dist/index.d.ts.map +1 -0
  15. package/dist/index.js +124 -0
  16. package/dist/index.js.map +1 -0
  17. package/dist/internal/encoding.d.ts +33 -0
  18. package/dist/internal/encoding.d.ts.map +1 -0
  19. package/dist/internal/encoding.js +136 -0
  20. package/dist/internal/encoding.js.map +1 -0
  21. package/dist/internal/hkdf.d.ts +14 -0
  22. package/dist/internal/hkdf.d.ts.map +1 -0
  23. package/dist/internal/hkdf.js +27 -0
  24. package/dist/internal/hkdf.js.map +1 -0
  25. package/dist/internal/hmac.d.ts +9 -0
  26. package/dist/internal/hmac.d.ts.map +1 -0
  27. package/dist/internal/hmac.js +18 -0
  28. package/dist/internal/hmac.js.map +1 -0
  29. package/dist/internal/runtime.d.ts +29 -0
  30. package/dist/internal/runtime.d.ts.map +1 -0
  31. package/dist/internal/runtime.js +67 -0
  32. package/dist/internal/runtime.js.map +1 -0
  33. package/dist/internal/sodium.d.ts +12 -0
  34. package/dist/internal/sodium.d.ts.map +1 -0
  35. package/dist/internal/sodium.js +34 -0
  36. package/dist/internal/sodium.js.map +1 -0
  37. package/dist/internal/types.d.ts +94 -0
  38. package/dist/internal/types.d.ts.map +1 -0
  39. package/dist/internal/types.js +65 -0
  40. package/dist/internal/types.js.map +1 -0
  41. package/dist/kdf/argon2id.d.ts +72 -0
  42. package/dist/kdf/argon2id.d.ts.map +1 -0
  43. package/dist/kdf/argon2id.js +97 -0
  44. package/dist/kdf/argon2id.js.map +1 -0
  45. package/dist/migration/v0-bridge.d.ts +94 -0
  46. package/dist/migration/v0-bridge.d.ts.map +1 -0
  47. package/dist/migration/v0-bridge.js +203 -0
  48. package/dist/migration/v0-bridge.js.map +1 -0
  49. package/dist/streams/aes-gcm-v1.d.ts +76 -0
  50. package/dist/streams/aes-gcm-v1.d.ts.map +1 -0
  51. package/dist/streams/aes-gcm-v1.js +322 -0
  52. package/dist/streams/aes-gcm-v1.js.map +1 -0
  53. package/dist/suites/aes-gcm-v0/api.d.ts +48 -0
  54. package/dist/suites/aes-gcm-v0/api.d.ts.map +1 -0
  55. package/dist/suites/aes-gcm-v0/api.js +110 -0
  56. package/dist/suites/aes-gcm-v0/api.js.map +1 -0
  57. package/dist/suites/aes-gcm-v1/api.d.ts +54 -0
  58. package/dist/suites/aes-gcm-v1/api.d.ts.map +1 -0
  59. package/dist/suites/aes-gcm-v1/api.js +179 -0
  60. package/dist/suites/aes-gcm-v1/api.js.map +1 -0
  61. package/dist/suites/aes-gcm-v1/index.d.ts +56 -0
  62. package/dist/suites/aes-gcm-v1/index.d.ts.map +1 -0
  63. package/dist/suites/aes-gcm-v1/index.js +174 -0
  64. package/dist/suites/aes-gcm-v1/index.js.map +1 -0
  65. package/dist/suites/pq-hybrid-v1/api.d.ts +40 -0
  66. package/dist/suites/pq-hybrid-v1/api.d.ts.map +1 -0
  67. package/dist/suites/pq-hybrid-v1/api.js +157 -0
  68. package/dist/suites/pq-hybrid-v1/api.js.map +1 -0
  69. package/dist/suites/pq-hybrid-v1/index.d.ts +101 -0
  70. package/dist/suites/pq-hybrid-v1/index.d.ts.map +1 -0
  71. package/dist/suites/pq-hybrid-v1/index.js +225 -0
  72. package/dist/suites/pq-hybrid-v1/index.js.map +1 -0
  73. package/dist/suites/xchacha-v1/api.d.ts +29 -0
  74. package/dist/suites/xchacha-v1/api.d.ts.map +1 -0
  75. package/dist/suites/xchacha-v1/api.js +151 -0
  76. package/dist/suites/xchacha-v1/api.js.map +1 -0
  77. package/dist/suites/xchacha-v1/index.d.ts +50 -0
  78. package/dist/suites/xchacha-v1/index.d.ts.map +1 -0
  79. package/dist/suites/xchacha-v1/index.js +133 -0
  80. package/dist/suites/xchacha-v1/index.js.map +1 -0
  81. package/package.json +112 -0
  82. package/spec/format-v0.md +81 -0
  83. package/spec/format-v1.md +245 -0
  84. package/spec/key-derivation.md +122 -0
  85. package/spec/threat-model.md +152 -0
  86. package/src/format/header.ts +284 -0
  87. package/src/identity/index.ts +358 -0
  88. package/src/index.ts +197 -0
  89. package/src/internal/encoding.ts +153 -0
  90. package/src/internal/hkdf.ts +47 -0
  91. package/src/internal/hmac.ts +33 -0
  92. package/src/internal/runtime.ts +80 -0
  93. package/src/internal/sodium.ts +38 -0
  94. package/src/internal/types.ts +128 -0
  95. package/src/kdf/argon2id.ts +164 -0
  96. package/src/migration/v0-bridge.ts +347 -0
  97. package/src/streams/aes-gcm-v1.ts +443 -0
  98. package/src/suites/aes-gcm-v0/api.ts +155 -0
  99. package/src/suites/aes-gcm-v1/api.ts +291 -0
  100. package/src/suites/aes-gcm-v1/index.ts +266 -0
  101. package/src/suites/pq-hybrid-v1/api.ts +259 -0
  102. package/src/suites/pq-hybrid-v1/index.ts +343 -0
  103. package/src/suites/xchacha-v1/api.ts +251 -0
  104. package/src/suites/xchacha-v1/index.ts +213 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,81 @@
1
+ # Changelog
2
+
3
+ All notable changes to `@shieldfive/crypto` will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [1.0.0-alpha.2] — 2026-05
11
+
12
+ ### Fixed
13
+
14
+ - **Migration bridge accepts the production wire-name `process`.** The
15
+ v0 bridge previously only accepted `{ type: 'chunk_request', index }`
16
+ for chunk requests, but the existing ShieldFive production worker
17
+ uses `{ type: 'process', index }`. This made the bridge silently
18
+ drop messages from unmodified production callers — a Phase 1
19
+ regression. The bridge now accepts both names; `process` is the
20
+ canonical/documented wire name and `chunk_request` is preserved as
21
+ an alias for back-compat. Caught during the Phase 1 migration in
22
+ the ShieldFive main repository before any production rollout.
23
+
24
+ ### Added
25
+
26
+ - `./package.json` is now exposed via `exports`, so consumers can
27
+ read the package version with `require('@shieldfive/crypto/package.json')`
28
+ or `import('@shieldfive/crypto/package.json')`.
29
+
30
+ ## [1.0.0-alpha.1] — 2026-05
31
+
32
+ The first public release.
33
+
34
+ ### Added
35
+
36
+ - **Format v1**: self-describing on-disk encrypted-file format with
37
+ cipher-suite agility, file-id-bound chunk AAD, and authenticated header
38
+ MAC. Specification at `spec/format-v1.md`.
39
+ - **Suite 0x01 — `aes-256-gcm-v1`**: WebCrypto-only AES-256-GCM AEAD with
40
+ per-chunk file-id-bound AAD. No WASM dependency.
41
+ - **Suite 0x02 — `xchacha20-poly1305-v1`**: XChaCha20-Poly1305 AEAD via
42
+ libsodium (optional peer dependency).
43
+ - **Suite 0x03 — `pq-hybrid-xchacha-mlkem1024-v1`** (default): hybrid
44
+ ML-KEM-1024 + XChaCha20-Poly1305 with HKDF-SHA-256 share combination.
45
+ - **Legacy v0 reader**: read-only support for the pre-v1 ShieldFive
46
+ production format. There is no v0 writer.
47
+ - **Auto-routing decryptor**: `autoDecryptBlob` dispatches to the correct
48
+ suite based on the header.
49
+ - **Threat model document** at `spec/threat-model.md`.
50
+ - **64 tests** covering: round-trip encryption for all suites, truncation
51
+ detection, per-chunk tampering, header tampering, chunk reordering,
52
+ cross-file splice prevention, wrong-key rejection, and parser edge cases.
53
+ - Apache 2.0 license.
54
+ - `SECURITY.md` with safe-harbor clause and disclosure timelines.
55
+
56
+ ### Security architecture
57
+
58
+ - Every chunk's AEAD authenticator binds chunk index, total chunk count,
59
+ final-chunk flag, and file_id. Truncation, reordering, and cross-file
60
+ splice attacks fail at the AEAD layer.
61
+ - Header MAC is keyed by HKDF-derived material from the content key. A
62
+ successful header MAC verification proves the right content key was
63
+ supplied before any chunk is processed.
64
+ - Post-quantum hybrid construction: an adversary must break both
65
+ ML-KEM-1024 *and* the classical wrap to compromise a file. ML-KEM-1024
66
+ is FIPS 203 (NIST PQC standard, security category 5).
67
+
68
+ ### Known limitations
69
+
70
+ - The streaming API (`src/streams/`) is not yet implemented; current API
71
+ reads/writes whole blobs.
72
+ - A formal third-party security audit has not yet been performed; planned
73
+ for v1.0.0 stable.
74
+ - No file-size obfuscation (padding) is applied. Padding may be added as
75
+ an optional flag in a future minor format version.
76
+ - ML-KEM keypair derivation from the user master secret is deterministic
77
+ via HKDF-SHA-256; this is an availability-vs-defense-in-depth tradeoff
78
+ documented in `spec/threat-model.md`.
79
+
80
+ [1.0.0-alpha.1]: https://github.com/shieldfive-labs/crypto/releases/tag/v1.0.0-alpha.1
81
+ [Unreleased]: https://github.com/shieldfive-labs/crypto/compare/v1.0.0-alpha.1...HEAD
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 describing the origin of the Work and
141
+ 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 2026 ShieldFive Labs
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
200
+ implied. See the License for the specific language governing permissions and
201
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,241 @@
1
+ # @shieldfive/crypto
2
+
3
+ > Post-quantum-hybrid client-side encryption with a self-describing,
4
+ > cipher-suite-agile on-disk format. The cryptographic core of
5
+ > [ShieldFive](https://shieldfive.com), released as a standalone library.
6
+
7
+ [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](LICENSE)
8
+ [![Tests](https://img.shields.io/badge/tests-64%2F64-brightgreen.svg)](tests/)
9
+ [![Status](https://img.shields.io/badge/status-alpha-orange.svg)](#status)
10
+
11
+ ## Status & honest scope
12
+
13
+ This library is **alpha**. The wire format is frozen for the v1 release;
14
+ the public TypeScript API may make minor adjustments before v1.0.0 stable.
15
+ It has **not** undergone external cryptographic audit. Until it does,
16
+ treat this library as a serious work-in-progress rather than a finished
17
+ product.
18
+
19
+ The design choices documented in [`spec/`](spec/) are deliberate and
20
+ reviewable. The implementation is covered by 64 passing tests including
21
+ truncation, reordering, splice, and tampering detection across all four
22
+ suites. That is enough for internal dogfooding. It is **not** enough to
23
+ claim "most secure crypto library" — that claim requires external review
24
+ this library has not yet received.
25
+
26
+ ## Design positions
27
+
28
+ This library makes specific design choices that differ from existing
29
+ open-source client-side encryption libraries in the encrypted-cloud-storage
30
+ space. The table below summarizes those positions; each row links to the
31
+ spec section that documents the choice.
32
+
33
+ | Property | This library | Proton OpenPGP.js | Internxt SDK | MEGA SDK |
34
+ | ------------------------------------------ | :----------: | :---------------: | :----------: | :------: |
35
+ | Post-quantum hybrid by default | ✅ | ❌ | ⚠️ | ❌ |
36
+ | ML-KEM-1024 (NIST PQC level 5) | ✅ | ❌ | ❌ (Kyber-512) | ❌ |
37
+ | Self-describing on-disk file format | ✅ | ✅ | ⚠️ | ⚠️ |
38
+ | Cipher-suite agile (one byte selects) | ✅ | ⚠️ | ❌ | ❌ |
39
+ | Truncation detection bound to AEAD | ✅ | ✅ | ❌ | ❌ |
40
+ | Cross-file splice prevention (file_id AAD) | ✅ | ✅ | ❌ | ❌ |
41
+ | Streaming AEAD with random-access chunks | ✅ | ⚠️ | ✅ | ✅ |
42
+ | WebCrypto-only path (no WASM required) | ✅ | ❌ | ❌ | ❌ |
43
+ | Apache 2.0 (patent grant) | ✅ | ⚠️ (LGPL) | ⚠️ | ⚠️ |
44
+
45
+ ⚠️ = partial or feature-flagged. See [`spec/threat-model.md`](spec/threat-model.md)
46
+ for the comparison methodology and source citations.
47
+
48
+ ## Quick start
49
+
50
+ ```bash
51
+ npm install @shieldfive/crypto
52
+ # Optional, only if you use suites 0x02 or 0x03:
53
+ npm install libsodium-wrappers-sumo
54
+ ```
55
+
56
+ ### Encrypt a file with the post-quantum hybrid suite (default)
57
+
58
+ ```ts
59
+ import {
60
+ encryptBlob,
61
+ decryptBlob,
62
+ generateMlKemKeypair,
63
+ } from '@shieldfive/crypto/pq-hybrid-v1'
64
+
65
+ // Recipient generates a long-lived ML-KEM-1024 keypair (typically derived
66
+ // from their master secret — see deriveMlKemKeypair).
67
+ const { publicKey, secretKey } = generateMlKemKeypair()
68
+
69
+ // A classical envelope key (e.g. the user's vault key, 32 bytes).
70
+ const envelopeKey = crypto.getRandomValues(new Uint8Array(32))
71
+
72
+ // Encrypt
73
+ const file = new File([myData], 'document.pdf')
74
+ const result = await encryptBlob({
75
+ blob: file,
76
+ recipientPublicKey: publicKey,
77
+ envelopeKey,
78
+ })
79
+
80
+ // Upload result.blob to your storage backend. It is self-describing.
81
+
82
+ // Later, decrypt
83
+ const plaintext = await decryptBlob({
84
+ blob: result.blob,
85
+ recipientSecretKey: secretKey,
86
+ envelopeKey,
87
+ })
88
+ ```
89
+
90
+ ### Encrypt with the WebCrypto-only suite (no WASM dependency)
91
+
92
+ ```ts
93
+ import { encryptBlob, decryptBlob } from '@shieldfive/crypto/aes-gcm-v1'
94
+
95
+ const result = await encryptBlob({ blob: file })
96
+ // result.contentKey is your 32-byte fresh per-file key — store it wrapped
97
+ // under your envelope key.
98
+
99
+ const plaintext = await decryptBlob({
100
+ blob: result.blob,
101
+ contentKey: result.contentKey,
102
+ })
103
+ ```
104
+
105
+ ### Auto-routing decryptor
106
+
107
+ If you accept files written by any v1 suite and don't know which in advance:
108
+
109
+ ```ts
110
+ import { autoDecryptBlob } from '@shieldfive/crypto'
111
+
112
+ const plaintext = await autoDecryptBlob({
113
+ blob: encryptedBlob,
114
+ contentKey, // for aes-gcm-v1 / xchacha-v1
115
+ recipientSecretKey, // for pq-hybrid-v1
116
+ envelopeKey, // for pq-hybrid-v1
117
+ })
118
+ ```
119
+
120
+ ### Reading legacy v0 files
121
+
122
+ The pre-v1 ShieldFive production format is supported read-only:
123
+
124
+ ```ts
125
+ import { decryptV0 } from '@shieldfive/crypto/legacy-v0'
126
+
127
+ const plaintext = await decryptV0({
128
+ blob: legacyBlob,
129
+ contentKey,
130
+ noncePrefix, // 4 bytes, from the database
131
+ chunkSize, // 5 * 1024 * 1024 typically
132
+ })
133
+ ```
134
+
135
+ There is no `encryptV0`. New writes must use a v1 suite.
136
+
137
+ ## File format
138
+
139
+ The complete v1 wire format is documented in
140
+ [`spec/format-v1.md`](spec/format-v1.md). The short version:
141
+
142
+ ```
143
+ header := magic(5) = "SF5\x01\x00"
144
+ || suite(1) = 0x01 | 0x02 | 0x03
145
+ || flags(1) = 0x00 (reserved)
146
+ || file_id(16) = random per-file
147
+ || chunk_size(4) = uint32 BE
148
+ || total_chunks(8) = uint64 BE
149
+ || plaintext_size(8) = uint64 BE
150
+ || suite_payload_len(2)
151
+ || suite_payload(variable)
152
+ || header_mac(32) = HMAC-SHA-256, keyed from content key
153
+
154
+ chunk_i := length_prefix(4) || AEAD-ciphertext-with-tag
155
+ ```
156
+
157
+ Every chunk's AEAD authenticator binds the chunk index, total chunk count,
158
+ final-chunk flag, and file_id. This makes truncation, reordering, and
159
+ cross-file splicing structurally detectable rather than relying on
160
+ application-layer hashes.
161
+
162
+ ## Cipher suites
163
+
164
+ | ID | Name | Default | Notes |
165
+ | ---- | ----------------------------------- | ------- | -------------------------------------- |
166
+ | 0x01 | `aes-256-gcm-v1` | | WebCrypto-only, hardware-accelerated |
167
+ | 0x02 | `xchacha20-poly1305-v1` | | libsodium, 192-bit nonces |
168
+ | 0x03 | `pq-hybrid-xchacha-mlkem1024-v1` | ✅ | XChaCha20-Poly1305 + ML-KEM-1024 hybrid |
169
+
170
+ ML-KEM-1024 is FIPS 203 (NIST PQC standard, security level 5). Files
171
+ encrypted with suite 0x03 remain confidential against an adversary who
172
+ breaks *either* the classical or the post-quantum primitive — only a
173
+ break of *both* compromises the file.
174
+
175
+ ## Threat model
176
+
177
+ Documented at length in [`spec/threat-model.md`](spec/threat-model.md). The
178
+ short summary: this library protects file confidentiality and integrity
179
+ against an honest-but-curious server, an active network adversary, and a
180
+ "harvest now, decrypt later" quantum adversary. It does *not* protect
181
+ against malicious client delivery, endpoint compromise, side channels in
182
+ the host crypto runtime, or metadata leakage at the storage layer. Those
183
+ are addressed by the host application, not the crypto layer.
184
+
185
+ ## Performance
186
+
187
+ Benchmarks live in `bench/throughput.ts`. Run them on your hardware:
188
+
189
+ ```bash
190
+ npx tsx bench/throughput.ts
191
+ ```
192
+
193
+ Throughput depends heavily on CPU model, AES-NI availability, and Node
194
+ version. The PQ KEM is a constant per-file cost (~ML-KEM-1024 keygen +
195
+ encapsulation latency), independent of file size. Streaming chunk
196
+ throughput tracks the underlying classical AEAD.
197
+
198
+ We do not publish reference numbers here because numbers without source
199
+ hardware are misleading; numbers with source hardware get cherry-picked.
200
+ Run the benchmark on your target hardware and report what you see.
201
+
202
+ ## Building from source
203
+
204
+ ```bash
205
+ npm install
206
+ npm test
207
+ npm run build # emits dist/
208
+ ```
209
+
210
+ Requires Node 20+.
211
+
212
+ ## Status
213
+
214
+ **Alpha.** The wire format is frozen for the v1 release, but the public
215
+ TypeScript API may make small adjustments before 1.0.0 stable. Test
216
+ coverage is comprehensive (64 tests, all four suites, all architectural
217
+ guarantees verified). A formal third-party security audit is planned for
218
+ the 1.0.0 stable milestone — until that audit lands, treat this library
219
+ as a serious work-in-progress rather than a finished product.
220
+
221
+ ## License
222
+
223
+ [Apache 2.0](LICENSE). The Apache 2.0 patent grant is important for a
224
+ crypto library; we chose it over MIT for that reason.
225
+
226
+ ## Reporting vulnerabilities
227
+
228
+ See [`SECURITY.md`](SECURITY.md). Coordinate with us at
229
+ `security@shieldfive.com` (PGP key in SECURITY.md). 72-hour acknowledgement,
230
+ 30-day target patch window for high-severity issues. Researchers acting
231
+ in good faith are protected under the safe-harbor clause.
232
+
233
+ ## Acknowledgements
234
+
235
+ - [@noble/post-quantum](https://github.com/paulmillr/noble-post-quantum)
236
+ by Paul Miller — clean, audited ML-KEM-1024 implementation.
237
+ - [libsodium](https://github.com/jedisct1/libsodium) — XChaCha20-Poly1305
238
+ and XSalsa20-Poly1305 secretbox.
239
+ - The NIST PQC competition and FIPS 203 authors.
240
+
241
+ — *ShieldFive Labs*
package/SECURITY.md ADDED
@@ -0,0 +1,123 @@
1
+ # Security Policy
2
+
3
+ ShieldFive Labs takes the security of `@shieldfive/crypto` seriously. This
4
+ document describes how to report a vulnerability, what we commit to, and
5
+ the safe-harbor terms for security researchers.
6
+
7
+ ## Reporting a vulnerability
8
+
9
+ **Do not open a public GitHub issue for security reports.** Instead, email
10
+ us directly:
11
+
12
+ - **Email:** `security@shieldfive.com`
13
+ - **PGP key fingerprint:** *(to be published at v1.0.0 stable release)*
14
+ - **Public PGP key:** `https://shieldfive.com/.well-known/security/pgp-key.txt`
15
+
16
+ If you cannot encrypt the report, send it in plaintext anyway — we would
17
+ rather hear about a vulnerability in plaintext than not at all. We will
18
+ follow up over an encrypted channel.
19
+
20
+ ### What to include
21
+
22
+ 1. A description of the vulnerability and its impact.
23
+ 2. Steps to reproduce, including a minimal proof-of-concept if possible.
24
+ 3. The library version and runtime environment (Node version, browser,
25
+ bundler).
26
+ 4. Whether the issue is already public or has been disclosed elsewhere.
27
+ 5. Your name and a way to contact you (or "anonymous" if you prefer).
28
+
29
+ ## Our commitments
30
+
31
+ | Severity | Acknowledgement | Initial response | Patch target |
32
+ | ----------------------------- | :-------------: | :--------------: | :----------: |
33
+ | Critical (key recovery, RCE) | 24 hours | 48 hours | 7 days |
34
+ | High (integrity bypass) | 48 hours | 5 days | 14 days |
35
+ | Medium (DoS, info leakage) | 72 hours | 7 days | 30 days |
36
+ | Low (defense-in-depth) | 7 days | 14 days | 90 days |
37
+
38
+ We will:
39
+
40
+ - Acknowledge your report within the windows above.
41
+ - Keep you informed of our investigation.
42
+ - Credit you in the release notes (with your permission, or anonymously).
43
+ - Coordinate public disclosure with you. We default to a 90-day disclosure
44
+ window, but will move faster if there is active exploitation in the
45
+ wild and slower if a coordinated multi-party fix is in progress.
46
+ - Publish a CVE when appropriate.
47
+
48
+ We will *not*:
49
+
50
+ - Take legal action against researchers acting in good faith (see
51
+ Safe Harbor below).
52
+ - Demand silence as a condition of bounty or credit.
53
+
54
+ ## Safe Harbor
55
+
56
+ Security research conducted in accordance with this policy is authorized.
57
+ Specifically, we will not pursue civil claims or refer law enforcement
58
+ investigations against researchers who:
59
+
60
+ 1. Make a good-faith effort to avoid privacy violations, destruction of
61
+ data, and interruption or degradation of our services.
62
+ 2. Do not access, modify, or exfiltrate data belonging to anyone other
63
+ than themselves or research accounts.
64
+ 3. Report the vulnerability promptly through this policy's channels.
65
+ 4. Do not exploit the vulnerability beyond what is necessary to confirm
66
+ it.
67
+ 5. Do not publicly disclose the vulnerability before we have had a
68
+ reasonable opportunity to remediate it (the timelines above).
69
+
70
+ Activities consistent with this policy are considered authorized conduct,
71
+ and we will not initiate legal action against you. If a third party
72
+ initiates legal action against you for activities conducted under this
73
+ policy, we will make this authorization clear.
74
+
75
+ ## Bug bounty
76
+
77
+ We do not currently operate a paid bug bounty program for the open-source
78
+ crypto library. We do operate a paid bounty for vulnerabilities in the
79
+ ShieldFive product itself; see https://shieldfive.com/security for details.
80
+
81
+ For library vulnerabilities, we offer:
82
+
83
+ - Public credit (CVE, release notes, dedicated thank-you page).
84
+ - ShieldFive product credits or merchandise for high-impact reports.
85
+ - For critical findings, ad-hoc bounties at our discretion.
86
+
87
+ ## Out of scope
88
+
89
+ The following are *not* in scope for this library's security policy and
90
+ should be reported elsewhere or not at all:
91
+
92
+ - Vulnerabilities in dependencies (`@noble/post-quantum`,
93
+ `libsodium-wrappers-sumo`) — report those upstream.
94
+ - Vulnerabilities in the host application running this library — those
95
+ are the application's responsibility.
96
+ - Side-channel attacks against the underlying crypto runtime
97
+ (browser SubtleCrypto, libsodium) — those are out of scope per the
98
+ threat model in `spec/threat-model.md`.
99
+ - Theoretical attacks on the underlying primitives (AES, ChaCha20,
100
+ ML-KEM-1024) — these belong in the academic literature.
101
+ - Vulnerabilities that require an attacker to already control the user's
102
+ device.
103
+
104
+ ## Cryptographic specifications
105
+
106
+ Implementation choices and threat model are documented in:
107
+
108
+ - [`spec/format-v1.md`](spec/format-v1.md) — wire format specification
109
+ - [`spec/format-v0.md`](spec/format-v0.md) — legacy format (read-only)
110
+ - [`spec/threat-model.md`](spec/threat-model.md) — adversary model and
111
+ comparisons
112
+
113
+ If your report concerns a property claimed in those documents not actually
114
+ holding, please cite the specific paragraph.
115
+
116
+ ## Acknowledgements
117
+
118
+ (This list will be populated as reports come in. Researchers are credited
119
+ with their consent.)
120
+
121
+ ---
122
+
123
+ Last updated: v1.0.0-alpha.1