@kindgi/crypto 0.0.0-bootstrap.0 → 0.1.1
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.
- package/LICENSE +201 -0
- package/README.md +112 -2
- package/dist/binding.d.ts +60 -0
- package/dist/binding.d.ts.map +1 -0
- package/dist/binding.js +37 -0
- package/dist/binding.js.map +1 -0
- package/dist/ed25519.d.ts +64 -0
- package/dist/ed25519.d.ts.map +1 -0
- package/dist/ed25519.js +169 -0
- package/dist/ed25519.js.map +1 -0
- package/dist/encoding.d.ts +40 -0
- package/dist/encoding.d.ts.map +1 -0
- package/dist/encoding.js +123 -0
- package/dist/encoding.js.map +1 -0
- package/dist/errors.d.ts +51 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +27 -0
- package/dist/errors.js.map +1 -0
- package/dist/hmac.d.ts +16 -0
- package/dist/hmac.d.ts.map +1 -0
- package/dist/hmac.js +59 -0
- package/dist/hmac.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +8 -0
- package/dist/index.js.map +1 -0
- package/dist/webhook.d.ts +74 -0
- package/dist/webhook.d.ts.map +1 -0
- package/dist/webhook.js +149 -0
- package/dist/webhook.js.map +1 -0
- package/package.json +48 -4
- package/src/binding.ts +103 -0
- package/src/ed25519.ts +228 -0
- package/src/encoding.ts +166 -0
- package/src/errors.ts +84 -0
- package/src/hmac.ts +62 -0
- package/src/index.ts +51 -0
- package/src/webhook.ts +205 -0
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 Kindgi Inc.
|
|
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
|
@@ -1,3 +1,113 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@kindgi/crypto`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Cryptographic primitives for Kindgi. Two algorithms, one package:
|
|
4
|
+
|
|
5
|
+
- **Ed25519** — asymmetric signatures. The signer holds a private key; verifiers hold only the public key. Used by signed audit-bundle exports, signed provenance exports, and any consumer that needs "prove this came from the party holding the private key" without trusting the verifier.
|
|
6
|
+
- **HMAC-SHA256** — symmetric authentication. Sender and receiver share a secret. Suited to webhook-style payload signing: a lowercase hex HMAC over the raw payload bytes, and [Standard Webhooks](#webhook-signatures-standard-webhooks) signatures.
|
|
7
|
+
|
|
8
|
+
Zero runtime dependencies beyond Node built-ins (`node:crypto`, `node:buffer`). Portable to Bun / Deno; browser use is possible but the current implementation targets Node's `crypto` module.
|
|
9
|
+
|
|
10
|
+
## Design
|
|
11
|
+
|
|
12
|
+
- **Uint8Array on internal APIs.** Wire formats (PEM, base64) are serialization helpers. Keys and signatures move around as raw bytes end-to-end so downstream code isn't forced to speak PEM.
|
|
13
|
+
- **No key persistence.** The framework never persists or generates key material on your behalf. Callers plug in via [`SigningKeyBinding`](#signingkeybinding) — a caller-provided key store (KMS, env-var-backed, in-memory-for-tests).
|
|
14
|
+
- **Typed errors for caller-recoverable failures.** Malformed keys, wrong-length signatures, and unrecognized PEM labels return `Result<T, CryptoError>`. Only system-level primitive failures throw.
|
|
15
|
+
|
|
16
|
+
## Ed25519
|
|
17
|
+
|
|
18
|
+
```ts
|
|
19
|
+
import {
|
|
20
|
+
generateEd25519KeyPair,
|
|
21
|
+
signEd25519,
|
|
22
|
+
verifyEd25519,
|
|
23
|
+
} from '@kindgi/crypto';
|
|
24
|
+
|
|
25
|
+
const { publicKey, privateKey } = generateEd25519KeyPair();
|
|
26
|
+
const message = new TextEncoder().encode('the payload to sign');
|
|
27
|
+
|
|
28
|
+
const sig = signEd25519(privateKey, message);
|
|
29
|
+
if (sig.kind !== 'ok') throw sig.error;
|
|
30
|
+
|
|
31
|
+
const verified = verifyEd25519(publicKey, message, sig.value);
|
|
32
|
+
if (verified.kind === 'ok' && verified.value) {
|
|
33
|
+
// authentic
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Serialization helpers speak DER SPKI (public) and DER PKCS8 (private) — interoperable with OpenSSL and any consumer that speaks RFC 5280 / RFC 5958:
|
|
38
|
+
|
|
39
|
+
- `serializePublicKeyPem` / `parsePublicKeyPem` — `-----BEGIN PUBLIC KEY-----` envelope.
|
|
40
|
+
- `serializePrivateKeyPem` / `parsePrivateKeyPem` — `-----BEGIN PRIVATE KEY-----` envelope.
|
|
41
|
+
- `serializePublicKeyBase64` / `parsePublicKeyBase64` — compact JSON-wire form (base64 of DER SPKI).
|
|
42
|
+
- `serializePrivateKeyBase64` / `parsePrivateKeyBase64` — same for private keys.
|
|
43
|
+
|
|
44
|
+
## HMAC-SHA256
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { hmacSha256Sign, hmacSha256Verify } from '@kindgi/crypto';
|
|
48
|
+
|
|
49
|
+
const secret = process.env.WEBHOOK_SIGNING_SECRET!;
|
|
50
|
+
const payload = JSON.stringify({ event: 'payment.succeeded' });
|
|
51
|
+
|
|
52
|
+
const sig = hmacSha256Sign(secret, payload); // 64-char lowercase hex
|
|
53
|
+
|
|
54
|
+
// Receiver:
|
|
55
|
+
if (!hmacSha256Verify(secret, payload, sig)) {
|
|
56
|
+
// reject request
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Verify uses `crypto.timingSafeEqual` — the comparison time does not vary with the mismatch position, so a signature-probing attacker cannot recover a valid signature byte-by-byte.
|
|
61
|
+
|
|
62
|
+
## Webhook signatures (Standard Webhooks)
|
|
63
|
+
|
|
64
|
+
Kindgi signs outbound webhooks in the [Standard Webhooks](https://www.standardwebhooks.com) format: headers `webhook-id`, `webhook-timestamp` and `webhook-signature: v1,<base64 HMAC-SHA256 of "id.timestamp.body">`, with a `whsec_…` secret per endpoint.
|
|
65
|
+
|
|
66
|
+
```ts
|
|
67
|
+
import { verifyWebhook } from '@kindgi/crypto';
|
|
68
|
+
|
|
69
|
+
// In the receiver, with the raw request body (before JSON parsing):
|
|
70
|
+
const result = verifyWebhook({ secret, headers: request.headers, body: rawBody });
|
|
71
|
+
if (result.kind !== 'ok') {
|
|
72
|
+
// reject: result.reason is 'missing-headers' | 'invalid-timestamp' |
|
|
73
|
+
// 'timestamp-out-of-tolerance' | 'invalid-secret' | 'no-matching-signature'
|
|
74
|
+
}
|
|
75
|
+
// result.id is the event id: deduplicate on it (delivery is at least once).
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
- `verifyWebhook` accepts a `Headers` object or a plain record, rejects timestamps more than 5 minutes off (`toleranceSeconds`), accepts any matching signature when several are present (secret rotation), and compares in constant time.
|
|
79
|
+
- Senders use `signWebhook` / `webhookHeaders` (pass `[newSecret, previousSecret]` during a rotation) and `generateWebhookSecret`.
|
|
80
|
+
- Interoperable with the reference Standard Webhooks libraries in both directions.
|
|
81
|
+
|
|
82
|
+
## SigningKeyBinding
|
|
83
|
+
|
|
84
|
+
The framework does not own key persistence. Consumers implement `SigningKeyBinding` against whatever store they run:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import type { SigningKeyBinding } from '@kindgi/crypto';
|
|
88
|
+
import { createInMemorySigningKeyBinding, generateEd25519KeyPair } from '@kindgi/crypto';
|
|
89
|
+
import type { SigningKeyId } from '@kindgi/types';
|
|
90
|
+
|
|
91
|
+
const { publicKey, privateKey } = generateEd25519KeyPair();
|
|
92
|
+
const binding: SigningKeyBinding = createInMemorySigningKeyBinding([
|
|
93
|
+
{
|
|
94
|
+
keyId: 'audit-bundle-v1' as SigningKeyId,
|
|
95
|
+
algorithm: 'ed25519',
|
|
96
|
+
publicKey,
|
|
97
|
+
privateKey,
|
|
98
|
+
},
|
|
99
|
+
]);
|
|
100
|
+
|
|
101
|
+
// Later, in a consumer:
|
|
102
|
+
const priv = binding.getPrivateKey('audit-bundle-v1' as SigningKeyId);
|
|
103
|
+
if (priv === null) throw new Error('key not provisioned');
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
`createInMemorySigningKeyBinding` is for tests and local development. Production deployments implement the interface against their own key store (a cloud KMS, a secrets vault, an HSM). This package never persists key material.
|
|
107
|
+
|
|
108
|
+
## Security notes
|
|
109
|
+
|
|
110
|
+
- **Never log private keys or shared secrets.** Journals, provenance blobs, structured logs, and wire payloads must not contain key material except by explicit caller intent.
|
|
111
|
+
- **`SigningKeyBinding.listKeys()` returns descriptors only** — key IDs and metadata, never the bytes.
|
|
112
|
+
- **Constant-time verify only.** HMAC uses `timingSafeEqual`; Ed25519 uses Node's underlying primitive which itself is constant-time.
|
|
113
|
+
- **Rotation and revocation are out of scope for this package**. Callers who need rotation should treat a `SigningKeyId` as a specific key version — issue a new ID when rotating, keep the old one for verifying earlier signatures.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `SigningKeyBinding` — the seam between the crypto primitives and a
|
|
3
|
+
* deployment's key store. The framework does not persist key material;
|
|
4
|
+
* it never has direct access to a KMS. Instead, callers implement this
|
|
5
|
+
* binding against whatever store they run (an env-var-backed key for
|
|
6
|
+
* local dev, a cloud KMS or secrets vault in production, a hardware
|
|
7
|
+
* token).
|
|
8
|
+
*
|
|
9
|
+
* Consumers that need to sign or verify look up a key by `SigningKeyId`
|
|
10
|
+
* through the binding. The binding returns `null` when a key is not
|
|
11
|
+
* present — callers translate that to the appropriate domain error
|
|
12
|
+
* (e.g., "signing key for tenant T is not provisioned").
|
|
13
|
+
*
|
|
14
|
+
* Not defined here: rotation, revocation, tenant-scoped key selection.
|
|
15
|
+
* Those are lifecycle concerns handled outside this package.
|
|
16
|
+
*/
|
|
17
|
+
import type { SigningKeyId } from '@kindgi/types';
|
|
18
|
+
/** Which signature algorithm the key is for. */
|
|
19
|
+
export type SigningAlgorithm = 'ed25519' | 'hmac-sha256';
|
|
20
|
+
/** Metadata about a key registered with a binding. */
|
|
21
|
+
export interface SigningKeyDescriptor {
|
|
22
|
+
readonly keyId: SigningKeyId;
|
|
23
|
+
readonly algorithm: SigningAlgorithm;
|
|
24
|
+
/** ISO-8601 timestamp when the key was first registered with this binding. */
|
|
25
|
+
readonly createdAt: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* A caller-provided key store. Deployments plug in KMS, env-var-backed,
|
|
29
|
+
* or in-memory-for-tests implementations. Framework code never persists
|
|
30
|
+
* or generates key material through this interface — it only reads.
|
|
31
|
+
*
|
|
32
|
+
* Contracts:
|
|
33
|
+
* - `getPublicKey` returns raw bytes. For Ed25519, 32 bytes; for
|
|
34
|
+
* HMAC-SHA256, the shared secret (the binding does not distinguish
|
|
35
|
+
* the "public" side for symmetric algorithms; verifiers hold the
|
|
36
|
+
* same secret as signers).
|
|
37
|
+
* - `getPrivateKey` returns raw bytes. For Ed25519, 32-byte seed;
|
|
38
|
+
* for HMAC-SHA256, the shared secret.
|
|
39
|
+
* - `null` return means "no key by that id" — never throw.
|
|
40
|
+
* - `listKeys` MUST NOT expose key material, only descriptors.
|
|
41
|
+
*/
|
|
42
|
+
export interface SigningKeyBinding {
|
|
43
|
+
getPublicKey(keyId: SigningKeyId): Uint8Array | null;
|
|
44
|
+
getPrivateKey(keyId: SigningKeyId): Uint8Array | null;
|
|
45
|
+
listKeys(): readonly SigningKeyDescriptor[];
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* In-memory implementation. Intended for tests and local development —
|
|
49
|
+
* production deployments implement `SigningKeyBinding` against their own
|
|
50
|
+
* KMS. Keys are held by strong reference; the binding does not touch
|
|
51
|
+
* disk or the network.
|
|
52
|
+
*/
|
|
53
|
+
export declare function createInMemorySigningKeyBinding(entries: readonly {
|
|
54
|
+
readonly keyId: SigningKeyId;
|
|
55
|
+
readonly algorithm: SigningAlgorithm;
|
|
56
|
+
readonly publicKey: Uint8Array;
|
|
57
|
+
readonly privateKey: Uint8Array;
|
|
58
|
+
readonly createdAt?: string;
|
|
59
|
+
}[]): SigningKeyBinding;
|
|
60
|
+
//# sourceMappingURL=binding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binding.d.ts","sourceRoot":"","sources":["../src/binding.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;GAeG;AAEH,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAElD,gDAAgD;AAChD,MAAM,MAAM,gBAAgB,GAAG,SAAS,GAAG,aAAa,CAAC;AAEzD,sDAAsD;AACtD,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;IAC7B,QAAQ,CAAC,SAAS,EAAE,gBAAgB,CAAC;IACrC,8EAA8E;IAC9E,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;CAC5B;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,WAAW,iBAAiB;IAChC,YAAY,CAAC,KAAK,EAAE,YAAY,GAAG,UAAU,GAAG,IAAI,CAAC;IACrD,aAAa,CAAC,KAAK,EAAE,YAAY,GAAG,UAAU,GAAG,IAAI,CAAC;IACtD,QAAQ,IAAI,SAAS,oBAAoB,EAAE,CAAC;CAC7C;AAED;;;;;GAKG;AACH,wBAAgB,+BAA+B,CAC7C,OAAO,EAAE,SAAS;IAChB,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;IAC7B,QAAQ,CAAC,SAAS,EAAE,gBAAgB,CAAC;IACrC,QAAQ,CAAC,SAAS,EAAE,UAAU,CAAC;IAC/B,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;IAChC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC7B,EAAE,GACF,iBAAiB,CAkCnB"}
|
package/dist/binding.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
/**
|
|
4
|
+
* In-memory implementation. Intended for tests and local development —
|
|
5
|
+
* production deployments implement `SigningKeyBinding` against their own
|
|
6
|
+
* KMS. Keys are held by strong reference; the binding does not touch
|
|
7
|
+
* disk or the network.
|
|
8
|
+
*/
|
|
9
|
+
export function createInMemorySigningKeyBinding(entries) {
|
|
10
|
+
const map = new Map();
|
|
11
|
+
for (const entry of entries) {
|
|
12
|
+
if (map.has(entry.keyId)) {
|
|
13
|
+
throw new Error(`Duplicate SigningKeyId in binding: "${entry.keyId}"`);
|
|
14
|
+
}
|
|
15
|
+
map.set(entry.keyId, {
|
|
16
|
+
publicKey: entry.publicKey,
|
|
17
|
+
privateKey: entry.privateKey,
|
|
18
|
+
descriptor: {
|
|
19
|
+
keyId: entry.keyId,
|
|
20
|
+
algorithm: entry.algorithm,
|
|
21
|
+
createdAt: entry.createdAt ?? new Date().toISOString(),
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
getPublicKey(keyId) {
|
|
27
|
+
return map.get(keyId)?.publicKey ?? null;
|
|
28
|
+
},
|
|
29
|
+
getPrivateKey(keyId) {
|
|
30
|
+
return map.get(keyId)?.privateKey ?? null;
|
|
31
|
+
},
|
|
32
|
+
listKeys() {
|
|
33
|
+
return Array.from(map.values(), (v) => v.descriptor);
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=binding.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binding.js","sourceRoot":"","sources":["../src/binding.ts"],"names":[],"mappings":"AAAA,sCAAsC;AACtC,iCAAiC;AAqDjC;;;;;GAKG;AACH,MAAM,UAAU,+BAA+B,CAC7C,OAMG;IAEH,MAAM,GAAG,GAAG,IAAI,GAAG,EAOhB,CAAC;IACJ,KAAK,MAAM,KAAK,IAAI,OAAO,EAAE,CAAC;QAC5B,IAAI,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,uCAAuC,KAAK,CAAC,KAAK,GAAG,CAAC,CAAC;QACzE,CAAC;QACD,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,EAAE;YACnB,SAAS,EAAE,KAAK,CAAC,SAAS;YAC1B,UAAU,EAAE,KAAK,CAAC,UAAU;YAC5B,UAAU,EAAE;gBACV,KAAK,EAAE,KAAK,CAAC,KAAK;gBAClB,SAAS,EAAE,KAAK,CAAC,SAAS;gBAC1B,SAAS,EAAE,KAAK,CAAC,SAAS,IAAI,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE;aACvD;SACF,CAAC,CAAC;IACL,CAAC;IACD,OAAO;QACL,YAAY,CAAC,KAAK;YAChB,OAAO,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,SAAS,IAAI,IAAI,CAAC;QAC3C,CAAC;QACD,aAAa,CAAC,KAAK;YACjB,OAAO,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,UAAU,IAAI,IAAI,CAAC;QAC5C,CAAC;QACD,QAAQ;YACN,OAAO,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC;QACvD,CAAC;KACF,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { Result } from '@kindgi/types';
|
|
2
|
+
import type { CryptoError, MalformedKeyError } from './errors.js';
|
|
3
|
+
/** Byte length of an Ed25519 signature (always 64). */
|
|
4
|
+
export declare const ED25519_SIGNATURE_BYTES = 64;
|
|
5
|
+
/** Raw 32-byte key material — the wire form Ed25519 primitives operate on. */
|
|
6
|
+
export interface Ed25519KeyPair {
|
|
7
|
+
readonly publicKey: Uint8Array;
|
|
8
|
+
readonly privateKey: Uint8Array;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Generate a fresh Ed25519 key pair. Intended for tests, local development,
|
|
12
|
+
* and deployments that provision keys at boot. Production key material
|
|
13
|
+
* typically lives in a KMS / HSM and is loaded through `SigningKeyBinding`.
|
|
14
|
+
*/
|
|
15
|
+
export declare function generateEd25519KeyPair(): Ed25519KeyPair;
|
|
16
|
+
/**
|
|
17
|
+
* Sign a message with an Ed25519 private key. Returns the 64-byte signature.
|
|
18
|
+
* Returns `Result` because the caller-supplied key may be malformed (wrong
|
|
19
|
+
* length, wrong bytes); underlying primitive errors (system-level) throw.
|
|
20
|
+
*/
|
|
21
|
+
export declare function signEd25519(privateKey: Uint8Array, message: Uint8Array): Result<Uint8Array, MalformedKeyError>;
|
|
22
|
+
/**
|
|
23
|
+
* Verify an Ed25519 signature. Returns `true` on cryptographic success,
|
|
24
|
+
* `false` on cryptographic failure. Malformed key/signature bytes return
|
|
25
|
+
* a typed error — the caller distinguishes "key/sig shape is wrong"
|
|
26
|
+
* (an operational bug) from "signature does not match" (an authenticity
|
|
27
|
+
* failure). Never returns `true` for a shape-error input.
|
|
28
|
+
*/
|
|
29
|
+
export declare function verifyEd25519(publicKey: Uint8Array, message: Uint8Array, signature: Uint8Array): Result<boolean, CryptoError>;
|
|
30
|
+
/**
|
|
31
|
+
* Serialize a raw Ed25519 public key to PEM (standard "PUBLIC KEY" label
|
|
32
|
+
* wrapping a DER SPKI envelope). Interoperable with OpenSSL and any
|
|
33
|
+
* consumer that speaks RFC 5280 SPKI.
|
|
34
|
+
*/
|
|
35
|
+
export declare function serializePublicKeyPem(publicKey: Uint8Array): string;
|
|
36
|
+
/**
|
|
37
|
+
* Parse a PEM "PUBLIC KEY" (DER SPKI Ed25519) into raw 32 bytes.
|
|
38
|
+
*/
|
|
39
|
+
export declare function parsePublicKeyPem(pem: string): Result<Uint8Array, CryptoError>;
|
|
40
|
+
/**
|
|
41
|
+
* Serialize a raw Ed25519 private key seed to PEM (standard "PRIVATE KEY"
|
|
42
|
+
* label wrapping a DER PKCS8 envelope). WARNING: private key material —
|
|
43
|
+
* never log this, never emit it in journals, never send it over the wire
|
|
44
|
+
* to a verifier.
|
|
45
|
+
*/
|
|
46
|
+
export declare function serializePrivateKeyPem(privateKey: Uint8Array): string;
|
|
47
|
+
/** Parse a PEM "PRIVATE KEY" (DER PKCS8 Ed25519) into raw 32 bytes. */
|
|
48
|
+
export declare function parsePrivateKeyPem(pem: string): Result<Uint8Array, CryptoError>;
|
|
49
|
+
/**
|
|
50
|
+
* Serialize a raw Ed25519 public key to base64-encoded DER SPKI. This is
|
|
51
|
+
* the compact JSON-wire form — smaller than PEM (no header lines) and
|
|
52
|
+
* still interoperable with OpenSSL by wrapping in PEM downstream.
|
|
53
|
+
*/
|
|
54
|
+
export declare function serializePublicKeyBase64(publicKey: Uint8Array): string;
|
|
55
|
+
/** Parse a base64-encoded DER SPKI Ed25519 public key into raw 32 bytes. */
|
|
56
|
+
export declare function parsePublicKeyBase64(b64: string): Result<Uint8Array, CryptoError>;
|
|
57
|
+
/**
|
|
58
|
+
* Serialize a raw Ed25519 private key seed to base64-encoded DER PKCS8.
|
|
59
|
+
* WARNING: private key material. See `serializePrivateKeyPem` note.
|
|
60
|
+
*/
|
|
61
|
+
export declare function serializePrivateKeyBase64(privateKey: Uint8Array): string;
|
|
62
|
+
/** Parse a base64-encoded DER PKCS8 Ed25519 private key into raw 32 bytes. */
|
|
63
|
+
export declare function parsePrivateKeyBase64(b64: string): Result<Uint8Array, CryptoError>;
|
|
64
|
+
//# sourceMappingURL=ed25519.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ed25519.d.ts","sourceRoot":"","sources":["../src/ed25519.ts"],"names":[],"mappings":"AAwBA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AAgB5C,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAElE,uDAAuD;AACvD,eAAO,MAAM,uBAAuB,KAAK,CAAC;AAE1C,8EAA8E;AAC9E,MAAM,WAAW,cAAc;IAC7B,QAAQ,CAAC,SAAS,EAAE,UAAU,CAAC;IAC/B,QAAQ,CAAC,UAAU,EAAE,UAAU,CAAC;CACjC;AAED;;;;GAIG;AACH,wBAAgB,sBAAsB,IAAI,cAAc,CAUvD;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,UAAU,EAAE,UAAU,EACtB,OAAO,EAAE,UAAU,GAClB,MAAM,CAAC,UAAU,EAAE,iBAAiB,CAAC,CAqBvC;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAC3B,SAAS,EAAE,UAAU,EACrB,OAAO,EAAE,UAAU,EACnB,SAAS,EAAE,UAAU,GACpB,MAAM,CAAC,OAAO,EAAE,WAAW,CAAC,CA0B9B;AAOD;;;;GAIG;AACH,wBAAgB,qBAAqB,CAAC,SAAS,EAAE,UAAU,GAAG,MAAM,CAEnE;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,UAAU,EAAE,WAAW,CAAC,CAS9E;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAErE;AAED,uEAAuE;AACvE,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,UAAU,EAAE,WAAW,CAAC,CAS/E;AAED;;;;GAIG;AACH,wBAAgB,wBAAwB,CAAC,SAAS,EAAE,UAAU,GAAG,MAAM,CAEtE;AAED,4EAA4E;AAC5E,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,UAAU,EAAE,WAAW,CAAC,CASjF;AAED;;;GAGG;AACH,wBAAgB,yBAAyB,CAAC,UAAU,EAAE,UAAU,GAAG,MAAM,CAExE;AAED,8EAA8E;AAC9E,wBAAgB,qBAAqB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAC,UAAU,EAAE,WAAW,CAAC,CASlF"}
|
package/dist/ed25519.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright (C) 2026 Kindgi Inc.
|
|
3
|
+
/**
|
|
4
|
+
* Ed25519 digital signatures. Asymmetric: the signer holds a private key,
|
|
5
|
+
* verifiers hold only the public key. Used by signed audit-bundle exports,
|
|
6
|
+
* signed provenance exports, and any consumer that needs "prove this
|
|
7
|
+
* artifact came from a party holding the private key" without trusting the
|
|
8
|
+
* verifier.
|
|
9
|
+
*
|
|
10
|
+
* All APIs operate on raw 32-byte Uint8Arrays for keys. PEM / Base64
|
|
11
|
+
* serialization is offered as helpers — the wire encoding is DER SPKI
|
|
12
|
+
* (public) and DER PKCS8 (private).
|
|
13
|
+
*/
|
|
14
|
+
import { createPrivateKey, createPublicKey, generateKeyPairSync, sign as nodeSign, verify as nodeVerify, } from 'node:crypto';
|
|
15
|
+
import { ED25519_PKCS8_LENGTH, ED25519_RAW_KEY_BYTES, ED25519_SPKI_LENGTH, fromBase64, fromPem, toBase64, toPem, unwrapPrivateKeyPkcs8, unwrapPublicKeySpki, wrapPrivateKeyPkcs8, wrapPublicKeySpki, } from './encoding.js';
|
|
16
|
+
import { malformedKey, malformedSignature } from './errors.js';
|
|
17
|
+
/** Byte length of an Ed25519 signature (always 64). */
|
|
18
|
+
export const ED25519_SIGNATURE_BYTES = 64;
|
|
19
|
+
/**
|
|
20
|
+
* Generate a fresh Ed25519 key pair. Intended for tests, local development,
|
|
21
|
+
* and deployments that provision keys at boot. Production key material
|
|
22
|
+
* typically lives in a KMS / HSM and is loaded through `SigningKeyBinding`.
|
|
23
|
+
*/
|
|
24
|
+
export function generateEd25519KeyPair() {
|
|
25
|
+
const { publicKey, privateKey } = generateKeyPairSync('ed25519');
|
|
26
|
+
const spki = publicKey.export({ format: 'der', type: 'spki' });
|
|
27
|
+
const pkcs8 = privateKey.export({ format: 'der', type: 'pkcs8' });
|
|
28
|
+
const pubUnwrap = unwrapPublicKeySpki(new Uint8Array(spki));
|
|
29
|
+
const privUnwrap = unwrapPrivateKeyPkcs8(new Uint8Array(pkcs8));
|
|
30
|
+
if (pubUnwrap.kind === 'err' || privUnwrap.kind === 'err') {
|
|
31
|
+
throw new Error('generateEd25519KeyPair: unexpected Node key export shape');
|
|
32
|
+
}
|
|
33
|
+
return { publicKey: pubUnwrap.value, privateKey: privUnwrap.value };
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Sign a message with an Ed25519 private key. Returns the 64-byte signature.
|
|
37
|
+
* Returns `Result` because the caller-supplied key may be malformed (wrong
|
|
38
|
+
* length, wrong bytes); underlying primitive errors (system-level) throw.
|
|
39
|
+
*/
|
|
40
|
+
export function signEd25519(privateKey, message) {
|
|
41
|
+
if (!(privateKey instanceof Uint8Array) || privateKey.length !== ED25519_RAW_KEY_BYTES) {
|
|
42
|
+
return malformedKey(`Ed25519 private key must be ${ED25519_RAW_KEY_BYTES} raw bytes, got ${privateKey.length}`);
|
|
43
|
+
}
|
|
44
|
+
if (!(message instanceof Uint8Array)) {
|
|
45
|
+
return malformedKey('Ed25519 message must be a Uint8Array');
|
|
46
|
+
}
|
|
47
|
+
let keyObject;
|
|
48
|
+
try {
|
|
49
|
+
keyObject = createPrivateKey({
|
|
50
|
+
key: Buffer.from(wrapPrivateKeyPkcs8(privateKey)),
|
|
51
|
+
format: 'der',
|
|
52
|
+
type: 'pkcs8',
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
catch (cause) {
|
|
56
|
+
return malformedKey('Ed25519 private key rejected by underlying primitive', cause);
|
|
57
|
+
}
|
|
58
|
+
const signature = nodeSign(null, message, keyObject);
|
|
59
|
+
return { kind: 'ok', value: new Uint8Array(signature) };
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Verify an Ed25519 signature. Returns `true` on cryptographic success,
|
|
63
|
+
* `false` on cryptographic failure. Malformed key/signature bytes return
|
|
64
|
+
* a typed error — the caller distinguishes "key/sig shape is wrong"
|
|
65
|
+
* (an operational bug) from "signature does not match" (an authenticity
|
|
66
|
+
* failure). Never returns `true` for a shape-error input.
|
|
67
|
+
*/
|
|
68
|
+
export function verifyEd25519(publicKey, message, signature) {
|
|
69
|
+
if (!(publicKey instanceof Uint8Array) || publicKey.length !== ED25519_RAW_KEY_BYTES) {
|
|
70
|
+
return malformedKey(`Ed25519 public key must be ${ED25519_RAW_KEY_BYTES} raw bytes, got ${publicKey.length}`);
|
|
71
|
+
}
|
|
72
|
+
if (!(message instanceof Uint8Array)) {
|
|
73
|
+
return malformedKey('Ed25519 message must be a Uint8Array');
|
|
74
|
+
}
|
|
75
|
+
if (!(signature instanceof Uint8Array) || signature.length !== ED25519_SIGNATURE_BYTES) {
|
|
76
|
+
return malformedSignature(`Ed25519 signature must be ${ED25519_SIGNATURE_BYTES} bytes, got ${signature.length}`);
|
|
77
|
+
}
|
|
78
|
+
let keyObject;
|
|
79
|
+
try {
|
|
80
|
+
keyObject = createPublicKey({
|
|
81
|
+
key: Buffer.from(wrapPublicKeySpki(publicKey)),
|
|
82
|
+
format: 'der',
|
|
83
|
+
type: 'spki',
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
catch (cause) {
|
|
87
|
+
return malformedKey('Ed25519 public key rejected by underlying primitive', cause);
|
|
88
|
+
}
|
|
89
|
+
const ok = nodeVerify(null, message, keyObject, signature);
|
|
90
|
+
return { kind: 'ok', value: ok };
|
|
91
|
+
}
|
|
92
|
+
// ============ serialization helpers ============
|
|
93
|
+
const PEM_PUBLIC_LABEL = 'PUBLIC KEY';
|
|
94
|
+
const PEM_PRIVATE_LABEL = 'PRIVATE KEY';
|
|
95
|
+
/**
|
|
96
|
+
* Serialize a raw Ed25519 public key to PEM (standard "PUBLIC KEY" label
|
|
97
|
+
* wrapping a DER SPKI envelope). Interoperable with OpenSSL and any
|
|
98
|
+
* consumer that speaks RFC 5280 SPKI.
|
|
99
|
+
*/
|
|
100
|
+
export function serializePublicKeyPem(publicKey) {
|
|
101
|
+
return toPem(wrapPublicKeySpki(publicKey), PEM_PUBLIC_LABEL);
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Parse a PEM "PUBLIC KEY" (DER SPKI Ed25519) into raw 32 bytes.
|
|
105
|
+
*/
|
|
106
|
+
export function parsePublicKeyPem(pem) {
|
|
107
|
+
const der = fromPem(pem, PEM_PUBLIC_LABEL);
|
|
108
|
+
if (der.kind === 'err')
|
|
109
|
+
return der;
|
|
110
|
+
if (der.value.length !== ED25519_SPKI_LENGTH) {
|
|
111
|
+
return malformedKey(`PEM decoded to ${der.value.length} bytes; Ed25519 SPKI is ${ED25519_SPKI_LENGTH}`);
|
|
112
|
+
}
|
|
113
|
+
return unwrapPublicKeySpki(der.value);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Serialize a raw Ed25519 private key seed to PEM (standard "PRIVATE KEY"
|
|
117
|
+
* label wrapping a DER PKCS8 envelope). WARNING: private key material —
|
|
118
|
+
* never log this, never emit it in journals, never send it over the wire
|
|
119
|
+
* to a verifier.
|
|
120
|
+
*/
|
|
121
|
+
export function serializePrivateKeyPem(privateKey) {
|
|
122
|
+
return toPem(wrapPrivateKeyPkcs8(privateKey), PEM_PRIVATE_LABEL);
|
|
123
|
+
}
|
|
124
|
+
/** Parse a PEM "PRIVATE KEY" (DER PKCS8 Ed25519) into raw 32 bytes. */
|
|
125
|
+
export function parsePrivateKeyPem(pem) {
|
|
126
|
+
const der = fromPem(pem, PEM_PRIVATE_LABEL);
|
|
127
|
+
if (der.kind === 'err')
|
|
128
|
+
return der;
|
|
129
|
+
if (der.value.length !== ED25519_PKCS8_LENGTH) {
|
|
130
|
+
return malformedKey(`PEM decoded to ${der.value.length} bytes; Ed25519 PKCS8 is ${ED25519_PKCS8_LENGTH}`);
|
|
131
|
+
}
|
|
132
|
+
return unwrapPrivateKeyPkcs8(der.value);
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Serialize a raw Ed25519 public key to base64-encoded DER SPKI. This is
|
|
136
|
+
* the compact JSON-wire form — smaller than PEM (no header lines) and
|
|
137
|
+
* still interoperable with OpenSSL by wrapping in PEM downstream.
|
|
138
|
+
*/
|
|
139
|
+
export function serializePublicKeyBase64(publicKey) {
|
|
140
|
+
return toBase64(wrapPublicKeySpki(publicKey));
|
|
141
|
+
}
|
|
142
|
+
/** Parse a base64-encoded DER SPKI Ed25519 public key into raw 32 bytes. */
|
|
143
|
+
export function parsePublicKeyBase64(b64) {
|
|
144
|
+
const der = fromBase64(b64);
|
|
145
|
+
if (der.kind === 'err')
|
|
146
|
+
return der;
|
|
147
|
+
if (der.value.length !== ED25519_SPKI_LENGTH) {
|
|
148
|
+
return malformedKey(`Base64 decoded to ${der.value.length} bytes; Ed25519 SPKI is ${ED25519_SPKI_LENGTH}`);
|
|
149
|
+
}
|
|
150
|
+
return unwrapPublicKeySpki(der.value);
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Serialize a raw Ed25519 private key seed to base64-encoded DER PKCS8.
|
|
154
|
+
* WARNING: private key material. See `serializePrivateKeyPem` note.
|
|
155
|
+
*/
|
|
156
|
+
export function serializePrivateKeyBase64(privateKey) {
|
|
157
|
+
return toBase64(wrapPrivateKeyPkcs8(privateKey));
|
|
158
|
+
}
|
|
159
|
+
/** Parse a base64-encoded DER PKCS8 Ed25519 private key into raw 32 bytes. */
|
|
160
|
+
export function parsePrivateKeyBase64(b64) {
|
|
161
|
+
const der = fromBase64(b64);
|
|
162
|
+
if (der.kind === 'err')
|
|
163
|
+
return der;
|
|
164
|
+
if (der.value.length !== ED25519_PKCS8_LENGTH) {
|
|
165
|
+
return malformedKey(`Base64 decoded to ${der.value.length} bytes; Ed25519 PKCS8 is ${ED25519_PKCS8_LENGTH}`);
|
|
166
|
+
}
|
|
167
|
+
return unwrapPrivateKeyPkcs8(der.value);
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=ed25519.js.map
|