@7h3/protocol-pq 0.5.0 → 0.5.6
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 +202 -0
- package/NOTICE +5 -0
- package/dist/index.d.ts +3 -3
- package/dist/index.js +15 -7
- package/package.json +9 -8
- package/src/index.test.ts +26 -0
- package/src/index.ts +18 -9
- package/tsconfig.json +2 -2
- package/dist/aip-work/sdk/pq/src/index.d.ts +0 -30
- package/dist/aip-work/sdk/pq/src/index.js +0 -100
- package/dist/aip-work/src/protocol.d.ts +0 -66
- package/dist/aip-work/src/protocol.js +0 -294
- package/dist/index.test.d.ts +0 -1
- package/dist/index.test.js +0 -116
- package/dist/sdk/pq/src/index.d.ts +0 -30
- package/dist/sdk/pq/src/index.js +0 -100
- package/dist/src/index.d.ts +0 -30
- package/dist/src/index.js +0 -100
- package/dist/src/protocol.d.ts +0 -66
- package/dist/src/protocol.js +0 -294
|
@@ -1,294 +0,0 @@
|
|
|
1
|
-
const textEncoder = new TextEncoder();
|
|
2
|
-
const HMAC_KEY_CACHE_LIMIT = 256;
|
|
3
|
-
const hmacKeyCache = new Map();
|
|
4
|
-
const ED25519_KEY_CACHE_LIMIT = 256;
|
|
5
|
-
const ed25519PrivateKeyCache = new Map();
|
|
6
|
-
const ed25519PublicKeyCache = new Map();
|
|
7
|
-
function getBufferLike() {
|
|
8
|
-
const candidate = globalThis;
|
|
9
|
-
return candidate.Buffer ?? null;
|
|
10
|
-
}
|
|
11
|
-
function serializeHeaderCanonical(header) {
|
|
12
|
-
const parts = [
|
|
13
|
-
`"messageId":${JSON.stringify(header.messageId)}`,
|
|
14
|
-
`"nonce":${JSON.stringify(header.nonce)}`,
|
|
15
|
-
];
|
|
16
|
-
if (header.recipient !== undefined) {
|
|
17
|
-
parts.push(`"recipient":${JSON.stringify(header.recipient)}`);
|
|
18
|
-
}
|
|
19
|
-
parts.push(`"sender":${JSON.stringify(header.sender)}`);
|
|
20
|
-
parts.push(`"timestampMs":${header.timestampMs}`);
|
|
21
|
-
parts.push(`"ttlMs":${header.ttlMs}`);
|
|
22
|
-
parts.push(`"version":${JSON.stringify(header.version)}`);
|
|
23
|
-
return `{${parts.join(',')}}`;
|
|
24
|
-
}
|
|
25
|
-
function serializeBodyCanonical(body) {
|
|
26
|
-
const parts = [];
|
|
27
|
-
if (body.capability !== undefined) {
|
|
28
|
-
parts.push(`"capability":${JSON.stringify(body.capability)}`);
|
|
29
|
-
}
|
|
30
|
-
parts.push(`"content":${JSON.stringify(body.content)}`);
|
|
31
|
-
if (body.correlationId !== undefined) {
|
|
32
|
-
parts.push(`"correlationId":${JSON.stringify(body.correlationId)}`);
|
|
33
|
-
}
|
|
34
|
-
parts.push(`"intent":${JSON.stringify(body.intent)}`);
|
|
35
|
-
return `{${parts.join(',')}}`;
|
|
36
|
-
}
|
|
37
|
-
function getCachedHmacKey(secret) {
|
|
38
|
-
const cacheKey = `hs256:${secret}`;
|
|
39
|
-
const cached = hmacKeyCache.get(cacheKey);
|
|
40
|
-
if (cached)
|
|
41
|
-
return cached;
|
|
42
|
-
if (hmacKeyCache.size >= HMAC_KEY_CACHE_LIMIT) {
|
|
43
|
-
hmacKeyCache.clear();
|
|
44
|
-
}
|
|
45
|
-
const subtle = requireCryptoSubtle();
|
|
46
|
-
const imported = subtle
|
|
47
|
-
.importKey('raw', textEncoder.encode(secret), { name: 'HMAC', hash: 'SHA-256' }, false, ['sign', 'verify'])
|
|
48
|
-
.catch((error) => {
|
|
49
|
-
hmacKeyCache.delete(cacheKey);
|
|
50
|
-
throw error;
|
|
51
|
-
});
|
|
52
|
-
hmacKeyCache.set(cacheKey, imported);
|
|
53
|
-
return imported;
|
|
54
|
-
}
|
|
55
|
-
function getCachedEd25519PrivateKey(privateKeyPkcs8Base64Url) {
|
|
56
|
-
const cacheKey = `ed25519:pkcs8:${privateKeyPkcs8Base64Url}`;
|
|
57
|
-
const cached = ed25519PrivateKeyCache.get(cacheKey);
|
|
58
|
-
if (cached)
|
|
59
|
-
return cached;
|
|
60
|
-
if (ed25519PrivateKeyCache.size >= ED25519_KEY_CACHE_LIMIT) {
|
|
61
|
-
ed25519PrivateKeyCache.clear();
|
|
62
|
-
}
|
|
63
|
-
const subtle = requireCryptoSubtle();
|
|
64
|
-
const imported = subtle
|
|
65
|
-
.importKey('pkcs8', toArrayBuffer(fromBase64Url(privateKeyPkcs8Base64Url)), { name: 'Ed25519' }, false, ['sign'])
|
|
66
|
-
.catch((error) => {
|
|
67
|
-
ed25519PrivateKeyCache.delete(cacheKey);
|
|
68
|
-
throw error;
|
|
69
|
-
});
|
|
70
|
-
ed25519PrivateKeyCache.set(cacheKey, imported);
|
|
71
|
-
return imported;
|
|
72
|
-
}
|
|
73
|
-
function getCachedEd25519PublicKey(publicKeySpkiBase64Url) {
|
|
74
|
-
const cacheKey = `ed25519:spki:${publicKeySpkiBase64Url}`;
|
|
75
|
-
const cached = ed25519PublicKeyCache.get(cacheKey);
|
|
76
|
-
if (cached)
|
|
77
|
-
return cached;
|
|
78
|
-
if (ed25519PublicKeyCache.size >= ED25519_KEY_CACHE_LIMIT) {
|
|
79
|
-
ed25519PublicKeyCache.clear();
|
|
80
|
-
}
|
|
81
|
-
const subtle = requireCryptoSubtle();
|
|
82
|
-
const imported = subtle
|
|
83
|
-
.importKey('spki', toArrayBuffer(fromBase64Url(publicKeySpkiBase64Url)), { name: 'Ed25519' }, false, ['verify'])
|
|
84
|
-
.catch((error) => {
|
|
85
|
-
ed25519PublicKeyCache.delete(cacheKey);
|
|
86
|
-
throw error;
|
|
87
|
-
});
|
|
88
|
-
ed25519PublicKeyCache.set(cacheKey, imported);
|
|
89
|
-
return imported;
|
|
90
|
-
}
|
|
91
|
-
function toBase64Url(bytes) {
|
|
92
|
-
const bufferLike = getBufferLike();
|
|
93
|
-
const base64 = bufferLike ? bufferLike.from(bytes).toString('base64') : btoa(String.fromCharCode(...bytes));
|
|
94
|
-
return base64
|
|
95
|
-
.replace(/\+/g, '-')
|
|
96
|
-
.replace(/\//g, '_')
|
|
97
|
-
.replace(/=+$/g, '');
|
|
98
|
-
}
|
|
99
|
-
function fromBase64Url(value) {
|
|
100
|
-
const padded = value
|
|
101
|
-
.replace(/-/g, '+')
|
|
102
|
-
.replace(/_/g, '/')
|
|
103
|
-
.padEnd(Math.ceil(value.length / 4) * 4, '=');
|
|
104
|
-
const bufferLike = getBufferLike();
|
|
105
|
-
if (bufferLike) {
|
|
106
|
-
return new Uint8Array(bufferLike.from(padded, 'base64'));
|
|
107
|
-
}
|
|
108
|
-
const binary = atob(padded);
|
|
109
|
-
const bytes = new Uint8Array(binary.length);
|
|
110
|
-
for (let i = 0; i < binary.length; i += 1) {
|
|
111
|
-
bytes[i] = binary.charCodeAt(i);
|
|
112
|
-
}
|
|
113
|
-
return bytes;
|
|
114
|
-
}
|
|
115
|
-
function toArrayBuffer(bytes) {
|
|
116
|
-
return bytes.buffer.slice(bytes.byteOffset, bytes.byteOffset + bytes.byteLength);
|
|
117
|
-
}
|
|
118
|
-
export function canonicalizeEnvelope(envelope) {
|
|
119
|
-
return `{"body":${serializeBodyCanonical(envelope.body)},"header":${serializeHeaderCanonical(envelope.header)}}`;
|
|
120
|
-
}
|
|
121
|
-
function requireCryptoSubtle() {
|
|
122
|
-
if (typeof crypto === 'undefined' || !crypto.subtle) {
|
|
123
|
-
throw new Error('Web Crypto API is not available in this runtime');
|
|
124
|
-
}
|
|
125
|
-
return crypto.subtle;
|
|
126
|
-
}
|
|
127
|
-
async function hmacSign(payload, secret) {
|
|
128
|
-
const subtle = requireCryptoSubtle();
|
|
129
|
-
const key = await getCachedHmacKey(secret);
|
|
130
|
-
const signature = await subtle.sign('HMAC', key, textEncoder.encode(payload));
|
|
131
|
-
return toBase64Url(new Uint8Array(signature));
|
|
132
|
-
}
|
|
133
|
-
async function hmacVerify(payload, signature, secret) {
|
|
134
|
-
const subtle = requireCryptoSubtle();
|
|
135
|
-
const key = await getCachedHmacKey(secret);
|
|
136
|
-
const signatureBytes = fromBase64Url(signature);
|
|
137
|
-
return subtle.verify('HMAC', key, signatureBytes.buffer, textEncoder.encode(payload));
|
|
138
|
-
}
|
|
139
|
-
async function ed25519Sign(payload, privateKeyPkcs8Base64Url) {
|
|
140
|
-
const subtle = requireCryptoSubtle();
|
|
141
|
-
const key = await getCachedEd25519PrivateKey(privateKeyPkcs8Base64Url);
|
|
142
|
-
const signature = await subtle.sign('Ed25519', key, textEncoder.encode(payload));
|
|
143
|
-
return toBase64Url(new Uint8Array(signature));
|
|
144
|
-
}
|
|
145
|
-
async function ed25519Verify(payload, signature, publicKeySpkiBase64Url) {
|
|
146
|
-
const subtle = requireCryptoSubtle();
|
|
147
|
-
const key = await getCachedEd25519PublicKey(publicKeySpkiBase64Url);
|
|
148
|
-
const signatureBytes = fromBase64Url(signature);
|
|
149
|
-
return subtle.verify('Ed25519', key, signatureBytes.buffer, textEncoder.encode(payload));
|
|
150
|
-
}
|
|
151
|
-
export async function signCanonicalPayloadHmac(payload, secret) {
|
|
152
|
-
return hmacSign(payload, secret);
|
|
153
|
-
}
|
|
154
|
-
export async function verifyCanonicalPayloadHmac(payload, signature, secret) {
|
|
155
|
-
return hmacVerify(payload, signature, secret);
|
|
156
|
-
}
|
|
157
|
-
export async function generateEd25519KeypairBase64Url() {
|
|
158
|
-
const subtle = requireCryptoSubtle();
|
|
159
|
-
const pair = await subtle.generateKey({ name: 'Ed25519' }, true, ['sign', 'verify']);
|
|
160
|
-
const privateKeyRaw = await subtle.exportKey('pkcs8', pair.privateKey);
|
|
161
|
-
const publicKeyRaw = await subtle.exportKey('spki', pair.publicKey);
|
|
162
|
-
return {
|
|
163
|
-
privateKey: toBase64Url(new Uint8Array(privateKeyRaw)),
|
|
164
|
-
publicKey: toBase64Url(new Uint8Array(publicKeyRaw)),
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
export async function signCanonicalPayloadEd25519(payload, privateKeyPkcs8Base64Url) {
|
|
168
|
-
return ed25519Sign(payload, privateKeyPkcs8Base64Url);
|
|
169
|
-
}
|
|
170
|
-
export async function verifyCanonicalPayloadEd25519(payload, signature, publicKeySpkiBase64Url) {
|
|
171
|
-
return ed25519Verify(payload, signature, publicKeySpkiBase64Url);
|
|
172
|
-
}
|
|
173
|
-
export async function signEnvelopeHmac(envelope, secret, keyId = 'local-dev-key') {
|
|
174
|
-
const payload = canonicalizeEnvelope(envelope);
|
|
175
|
-
const signature = await hmacSign(payload, secret);
|
|
176
|
-
return {
|
|
177
|
-
...envelope,
|
|
178
|
-
signature: {
|
|
179
|
-
alg: 'HS256',
|
|
180
|
-
keyId,
|
|
181
|
-
value: signature,
|
|
182
|
-
},
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
export async function verifyEnvelopeHmac(envelope, secret) {
|
|
186
|
-
if (!envelope.signature)
|
|
187
|
-
return false;
|
|
188
|
-
if (envelope.signature.alg !== 'HS256')
|
|
189
|
-
return false;
|
|
190
|
-
const unsigned = {
|
|
191
|
-
header: envelope.header,
|
|
192
|
-
body: envelope.body,
|
|
193
|
-
};
|
|
194
|
-
const payload = canonicalizeEnvelope(unsigned);
|
|
195
|
-
return hmacVerify(payload, envelope.signature.value, secret);
|
|
196
|
-
}
|
|
197
|
-
export async function signEnvelopeEd25519(envelope, privateKeyPkcs8Base64Url, keyId = 'local-ed25519-key') {
|
|
198
|
-
const payload = canonicalizeEnvelope(envelope);
|
|
199
|
-
const signature = await ed25519Sign(payload, privateKeyPkcs8Base64Url);
|
|
200
|
-
return {
|
|
201
|
-
...envelope,
|
|
202
|
-
signature: {
|
|
203
|
-
alg: 'ED25519',
|
|
204
|
-
keyId,
|
|
205
|
-
value: signature,
|
|
206
|
-
},
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
export async function verifyEnvelopeEd25519(envelope, publicKeySpkiBase64Url) {
|
|
210
|
-
if (!envelope.signature)
|
|
211
|
-
return false;
|
|
212
|
-
if (envelope.signature.alg !== 'ED25519')
|
|
213
|
-
return false;
|
|
214
|
-
const unsigned = {
|
|
215
|
-
header: envelope.header,
|
|
216
|
-
body: envelope.body,
|
|
217
|
-
};
|
|
218
|
-
const payload = canonicalizeEnvelope(unsigned);
|
|
219
|
-
return ed25519Verify(payload, envelope.signature.value, publicKeySpkiBase64Url);
|
|
220
|
-
}
|
|
221
|
-
export async function verifyEnvelopeSignature(envelope, material) {
|
|
222
|
-
if (!envelope.signature)
|
|
223
|
-
return false;
|
|
224
|
-
if (envelope.signature.alg !== material.alg)
|
|
225
|
-
return false;
|
|
226
|
-
if (material.alg === 'HS256') {
|
|
227
|
-
return verifyEnvelopeHmac(envelope, material.secret);
|
|
228
|
-
}
|
|
229
|
-
return verifyEnvelopeEd25519(envelope, material.publicKey);
|
|
230
|
-
}
|
|
231
|
-
export async function verifyCanonicalPayloadSignature(payload, signature, material) {
|
|
232
|
-
if (!signature)
|
|
233
|
-
return false;
|
|
234
|
-
if (signature.alg !== material.alg)
|
|
235
|
-
return false;
|
|
236
|
-
if (material.alg === 'HS256') {
|
|
237
|
-
return verifyCanonicalPayloadHmac(payload, signature.value, material.secret);
|
|
238
|
-
}
|
|
239
|
-
return verifyCanonicalPayloadEd25519(payload, signature.value, material.publicKey);
|
|
240
|
-
}
|
|
241
|
-
export function validateEnvelope(envelope, nowMs = Date.now()) {
|
|
242
|
-
const diagnostics = [];
|
|
243
|
-
const header = envelope.header ?? {};
|
|
244
|
-
const body = envelope.body ?? {};
|
|
245
|
-
const version = typeof header.version === 'string' ? header.version : '';
|
|
246
|
-
const messageId = typeof header.messageId === 'string' ? header.messageId : '';
|
|
247
|
-
const sender = typeof header.sender === 'string' ? header.sender : '';
|
|
248
|
-
const nonce = typeof header.nonce === 'string' ? header.nonce : '';
|
|
249
|
-
const timestampMs = typeof header.timestampMs === 'number' ? header.timestampMs : 0;
|
|
250
|
-
const ttlMs = typeof header.ttlMs === 'number' ? header.ttlMs : 0;
|
|
251
|
-
const content = typeof body.content === 'string' ? body.content : '';
|
|
252
|
-
if (version !== '7h3/0.1') {
|
|
253
|
-
diagnostics.push({ level: 'error', message: `Unsupported protocol version '${version}'` });
|
|
254
|
-
}
|
|
255
|
-
if (!messageId.trim()) {
|
|
256
|
-
diagnostics.push({ level: 'error', message: 'Missing messageId' });
|
|
257
|
-
}
|
|
258
|
-
if (!sender.trim()) {
|
|
259
|
-
diagnostics.push({ level: 'error', message: 'Missing sender identity' });
|
|
260
|
-
}
|
|
261
|
-
if (!nonce.trim()) {
|
|
262
|
-
diagnostics.push({ level: 'error', message: 'Missing nonce — replay protection requires a unique nonce per message' });
|
|
263
|
-
}
|
|
264
|
-
if (ttlMs <= 0) {
|
|
265
|
-
diagnostics.push({ level: 'error', message: 'ttlMs must be greater than zero' });
|
|
266
|
-
}
|
|
267
|
-
if (timestampMs + ttlMs < nowMs) {
|
|
268
|
-
diagnostics.push({ level: 'error', message: 'Message TTL expired' });
|
|
269
|
-
}
|
|
270
|
-
if (!content.trim()) {
|
|
271
|
-
diagnostics.push({ level: 'warning', message: 'Empty content payload' });
|
|
272
|
-
}
|
|
273
|
-
return diagnostics;
|
|
274
|
-
}
|
|
275
|
-
export function createEnvelope(input) {
|
|
276
|
-
const nowMs = input.nowMs ?? Date.now();
|
|
277
|
-
return {
|
|
278
|
-
header: {
|
|
279
|
-
version: '7h3/0.1',
|
|
280
|
-
messageId: input.messageId ?? `msg-${nowMs}-${Math.random().toString(36).slice(2, 10)}`,
|
|
281
|
-
timestampMs: nowMs,
|
|
282
|
-
ttlMs: input.ttlMs ?? 60_000,
|
|
283
|
-
sender: input.sender,
|
|
284
|
-
recipient: input.recipient,
|
|
285
|
-
nonce: input.nonce ?? Math.random().toString(36).slice(2, 12),
|
|
286
|
-
},
|
|
287
|
-
body: {
|
|
288
|
-
intent: input.intent,
|
|
289
|
-
content: input.content,
|
|
290
|
-
capability: input.capability,
|
|
291
|
-
correlationId: input.correlationId,
|
|
292
|
-
},
|
|
293
|
-
};
|
|
294
|
-
}
|
package/dist/index.test.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/index.test.js
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from 'vitest';
|
|
2
|
-
import { generatePqKeyPair, signEnvelopePq, verifyEnvelopePq, signEnvelopeMlDsa65, signEnvelopeMlDsa87, verifyEnvelopeMlDsa65, verifyEnvelopeMlDsa87, createEnvelope, } from './index.js';
|
|
3
|
-
// Conformance vector envelope (deterministic for tests)
|
|
4
|
-
const baseEnvelope = {
|
|
5
|
-
header: {
|
|
6
|
-
version: '7h3/0.1',
|
|
7
|
-
messageId: 'test-msg-001',
|
|
8
|
-
timestampMs: 1_700_000_000_000,
|
|
9
|
-
ttlMs: 60_000,
|
|
10
|
-
sender: 'agent-alpha',
|
|
11
|
-
recipient: 'agent-beta',
|
|
12
|
-
nonce: 'abc123xyz',
|
|
13
|
-
},
|
|
14
|
-
body: {
|
|
15
|
-
intent: 'TASK',
|
|
16
|
-
content: 'Hello, post-quantum world!',
|
|
17
|
-
},
|
|
18
|
-
};
|
|
19
|
-
describe('generatePqKeyPair', () => {
|
|
20
|
-
it('1. ML-DSA-65 returns keypair with correct algorithm field', () => {
|
|
21
|
-
const kp = generatePqKeyPair('ML-DSA-65');
|
|
22
|
-
expect(kp.algorithm).toBe('ML-DSA-65');
|
|
23
|
-
expect(typeof kp.publicKey).toBe('string');
|
|
24
|
-
expect(typeof kp.privateKey).toBe('string');
|
|
25
|
-
expect(typeof kp.createdAt).toBe('number');
|
|
26
|
-
expect(kp.publicKey.length).toBeGreaterThan(0);
|
|
27
|
-
expect(kp.privateKey.length).toBeGreaterThan(0);
|
|
28
|
-
});
|
|
29
|
-
it('2. ML-DSA-87 returns keypair', () => {
|
|
30
|
-
const kp = generatePqKeyPair('ML-DSA-87');
|
|
31
|
-
expect(kp.algorithm).toBe('ML-DSA-87');
|
|
32
|
-
expect(typeof kp.publicKey).toBe('string');
|
|
33
|
-
expect(typeof kp.privateKey).toBe('string');
|
|
34
|
-
});
|
|
35
|
-
it('7. ML-DSA-65 public key is correct size (1952 bytes → ~2603 base64url chars)', () => {
|
|
36
|
-
const kp = generatePqKeyPair('ML-DSA-65');
|
|
37
|
-
// base64url: ceil(1952 / 3) * 4 = 2604 chars, minus up to 2 padding = 2603 or 2604
|
|
38
|
-
const decoded = Buffer.from(kp.publicKey.replace(/-/g, '+').replace(/_/g, '/').padEnd(Math.ceil(kp.publicKey.length / 4) * 4, '='), 'base64');
|
|
39
|
-
expect(decoded.length).toBe(1952);
|
|
40
|
-
});
|
|
41
|
-
it('8. ML-DSA-65 private key is correct size (4032 bytes secretKey)', () => {
|
|
42
|
-
const kp = generatePqKeyPair('ML-DSA-65');
|
|
43
|
-
const decoded = Buffer.from(kp.privateKey.replace(/-/g, '+').replace(/_/g, '/').padEnd(Math.ceil(kp.privateKey.length / 4) * 4, '='), 'base64');
|
|
44
|
-
expect(decoded.length).toBe(4032);
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
describe('ML-DSA-65 sign/verify', () => {
|
|
48
|
-
it('3. ML-DSA-65 sign + verify round trip', async () => {
|
|
49
|
-
const kp = generatePqKeyPair('ML-DSA-65');
|
|
50
|
-
const signed = await signEnvelopePq(baseEnvelope, kp.privateKey, 'ML-DSA-65');
|
|
51
|
-
expect(signed.signature?.alg).toBe('ML-DSA-65');
|
|
52
|
-
expect(typeof signed.signature?.value).toBe('string');
|
|
53
|
-
expect(signed.signature.value.length).toBeGreaterThan(0);
|
|
54
|
-
const valid = await verifyEnvelopePq(signed, kp.publicKey);
|
|
55
|
-
expect(valid).toBe(true);
|
|
56
|
-
});
|
|
57
|
-
it('4. ML-DSA-65 verify fails on tampered envelope', async () => {
|
|
58
|
-
const kp = generatePqKeyPair('ML-DSA-65');
|
|
59
|
-
const signed = await signEnvelopePq(baseEnvelope, kp.privateKey, 'ML-DSA-65');
|
|
60
|
-
const tampered = {
|
|
61
|
-
...signed,
|
|
62
|
-
body: { ...signed.body, content: 'TAMPERED content' },
|
|
63
|
-
};
|
|
64
|
-
const valid = await verifyEnvelopePq(tampered, kp.publicKey);
|
|
65
|
-
expect(valid).toBe(false);
|
|
66
|
-
});
|
|
67
|
-
});
|
|
68
|
-
describe('ML-DSA-87 sign/verify', () => {
|
|
69
|
-
it('5. ML-DSA-87 sign + verify round trip', async () => {
|
|
70
|
-
const kp = generatePqKeyPair('ML-DSA-87');
|
|
71
|
-
const signed = await signEnvelopePq(baseEnvelope, kp.privateKey, 'ML-DSA-87');
|
|
72
|
-
expect(signed.signature?.alg).toBe('ML-DSA-87');
|
|
73
|
-
const valid = await verifyEnvelopePq(signed, kp.publicKey);
|
|
74
|
-
expect(valid).toBe(true);
|
|
75
|
-
});
|
|
76
|
-
it('6. ML-DSA-87 verify fails with wrong public key', async () => {
|
|
77
|
-
const kp = generatePqKeyPair('ML-DSA-87');
|
|
78
|
-
const wrongKp = generatePqKeyPair('ML-DSA-87');
|
|
79
|
-
const signed = await signEnvelopePq(baseEnvelope, kp.privateKey, 'ML-DSA-87');
|
|
80
|
-
const valid = await verifyEnvelopePq(signed, wrongKp.publicKey);
|
|
81
|
-
expect(valid).toBe(false);
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
describe('algorithm-specific aliases', () => {
|
|
85
|
-
it('signEnvelopeMlDsa65 / verifyEnvelopeMlDsa65 round trip', async () => {
|
|
86
|
-
const kp = generatePqKeyPair('ML-DSA-65');
|
|
87
|
-
const envelope = createEnvelope({
|
|
88
|
-
sender: 'agent-a',
|
|
89
|
-
recipient: 'agent-b',
|
|
90
|
-
intent: 'PING',
|
|
91
|
-
content: 'alias test',
|
|
92
|
-
});
|
|
93
|
-
const signed = await signEnvelopeMlDsa65(envelope, kp.privateKey);
|
|
94
|
-
expect(signed.signature?.alg).toBe('ML-DSA-65');
|
|
95
|
-
const valid = await verifyEnvelopeMlDsa65(signed, kp.publicKey);
|
|
96
|
-
expect(valid).toBe(true);
|
|
97
|
-
});
|
|
98
|
-
it('signEnvelopeMlDsa87 / verifyEnvelopeMlDsa87 round trip', async () => {
|
|
99
|
-
const kp = generatePqKeyPair('ML-DSA-87');
|
|
100
|
-
const envelope = createEnvelope({
|
|
101
|
-
sender: 'agent-a',
|
|
102
|
-
recipient: 'agent-b',
|
|
103
|
-
intent: 'PONG',
|
|
104
|
-
content: 'ml-dsa-87 alias test',
|
|
105
|
-
});
|
|
106
|
-
const signed = await signEnvelopeMlDsa87(envelope, kp.privateKey);
|
|
107
|
-
expect(signed.signature?.alg).toBe('ML-DSA-87');
|
|
108
|
-
const valid = await verifyEnvelopeMlDsa87(signed, kp.publicKey);
|
|
109
|
-
expect(valid).toBe(true);
|
|
110
|
-
});
|
|
111
|
-
it('verifyEnvelopePq returns false when no signature', async () => {
|
|
112
|
-
const unsigned = { ...baseEnvelope };
|
|
113
|
-
const valid = await verifyEnvelopePq(unsigned, 'fakepublickey');
|
|
114
|
-
expect(valid).toBe(false);
|
|
115
|
-
});
|
|
116
|
-
});
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
export type { ProtocolEnvelope, ProtocolHeader, ProtocolBody } from '../../src/protocol.js';
|
|
2
|
-
export { canonicalizeEnvelope, createEnvelope } from '../../src/protocol.js';
|
|
3
|
-
import type { ProtocolEnvelope, ProtocolHeader, ProtocolBody } from '../../src/protocol.js';
|
|
4
|
-
export type PqAlgorithm = 'ML-DSA-65' | 'ML-DSA-87';
|
|
5
|
-
export interface PqKeyPair {
|
|
6
|
-
algorithm: PqAlgorithm;
|
|
7
|
-
publicKey: string;
|
|
8
|
-
privateKey: string;
|
|
9
|
-
createdAt: number;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Extended envelope type that carries a PQ signature.
|
|
13
|
-
* Structurally identical to ProtocolEnvelope but alg is widened.
|
|
14
|
-
*/
|
|
15
|
-
export interface PqProtocolEnvelope {
|
|
16
|
-
header: ProtocolHeader;
|
|
17
|
-
body: ProtocolBody;
|
|
18
|
-
signature?: {
|
|
19
|
-
alg: PqAlgorithm;
|
|
20
|
-
keyId: string;
|
|
21
|
-
value: string;
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
export declare function generatePqKeyPair(algorithm?: PqAlgorithm): PqKeyPair;
|
|
25
|
-
export declare function signEnvelopePq(envelope: Omit<ProtocolEnvelope, 'signature'>, privateKeyBase64Url: string, algorithm?: PqAlgorithm): Promise<PqProtocolEnvelope>;
|
|
26
|
-
export declare function verifyEnvelopePq(envelope: PqProtocolEnvelope, publicKeyBase64Url: string): Promise<boolean>;
|
|
27
|
-
export declare function signEnvelopeMlDsa65(envelope: Omit<ProtocolEnvelope, 'signature'>, privateKeyBase64Url: string): Promise<PqProtocolEnvelope>;
|
|
28
|
-
export declare function signEnvelopeMlDsa87(envelope: Omit<ProtocolEnvelope, 'signature'>, privateKeyBase64Url: string): Promise<PqProtocolEnvelope>;
|
|
29
|
-
export declare function verifyEnvelopeMlDsa65(envelope: PqProtocolEnvelope, publicKeyBase64Url: string): Promise<boolean>;
|
|
30
|
-
export declare function verifyEnvelopeMlDsa87(envelope: PqProtocolEnvelope, publicKeyBase64Url: string): Promise<boolean>;
|
package/dist/sdk/pq/src/index.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { ml_dsa65, ml_dsa87 } from '@noble/post-quantum/ml-dsa';
|
|
2
|
-
export { canonicalizeEnvelope, createEnvelope } from '../../src/protocol.js';
|
|
3
|
-
import { canonicalizeEnvelope } from '../../src/protocol.js';
|
|
4
|
-
// ─── Base64url helpers ───────────────────────────────────────────────────────
|
|
5
|
-
function toBase64Url(bytes) {
|
|
6
|
-
const g = globalThis;
|
|
7
|
-
if (g.Buffer) {
|
|
8
|
-
return g.Buffer.from(bytes)
|
|
9
|
-
.toString('base64')
|
|
10
|
-
.replace(/\+/g, '-')
|
|
11
|
-
.replace(/\//g, '_')
|
|
12
|
-
.replace(/=+$/g, '');
|
|
13
|
-
}
|
|
14
|
-
let binary = '';
|
|
15
|
-
for (let i = 0; i < bytes.length; i++)
|
|
16
|
-
binary += String.fromCharCode(bytes[i]);
|
|
17
|
-
return btoa(binary)
|
|
18
|
-
.replace(/\+/g, '-')
|
|
19
|
-
.replace(/\//g, '_')
|
|
20
|
-
.replace(/=+$/g, '');
|
|
21
|
-
}
|
|
22
|
-
function fromBase64Url(value) {
|
|
23
|
-
const padded = value
|
|
24
|
-
.replace(/-/g, '+')
|
|
25
|
-
.replace(/_/g, '/')
|
|
26
|
-
.padEnd(Math.ceil(value.length / 4) * 4, '=');
|
|
27
|
-
const g = globalThis;
|
|
28
|
-
if (g.Buffer) {
|
|
29
|
-
return new Uint8Array(g.Buffer.from(padded, 'base64'));
|
|
30
|
-
}
|
|
31
|
-
const binary = atob(padded);
|
|
32
|
-
const bytes = new Uint8Array(binary.length);
|
|
33
|
-
for (let i = 0; i < binary.length; i++)
|
|
34
|
-
bytes[i] = binary.charCodeAt(i);
|
|
35
|
-
return bytes;
|
|
36
|
-
}
|
|
37
|
-
// ─── Key generation ──────────────────────────────────────────────────────────
|
|
38
|
-
export function generatePqKeyPair(algorithm = 'ML-DSA-65') {
|
|
39
|
-
const impl = algorithm === 'ML-DSA-65' ? ml_dsa65 : ml_dsa87;
|
|
40
|
-
const seed = crypto.getRandomValues(new Uint8Array(32));
|
|
41
|
-
const { publicKey, secretKey } = impl.keygen(seed);
|
|
42
|
-
return {
|
|
43
|
-
algorithm,
|
|
44
|
-
publicKey: toBase64Url(publicKey),
|
|
45
|
-
privateKey: toBase64Url(secretKey),
|
|
46
|
-
createdAt: Date.now(),
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
// ─── Sign ────────────────────────────────────────────────────────────────────
|
|
50
|
-
export async function signEnvelopePq(envelope, privateKeyBase64Url, algorithm = 'ML-DSA-65') {
|
|
51
|
-
const impl = algorithm === 'ML-DSA-65' ? ml_dsa65 : ml_dsa87;
|
|
52
|
-
const canonical = canonicalizeEnvelope(envelope);
|
|
53
|
-
const message = new TextEncoder().encode(canonical);
|
|
54
|
-
const secretKey = fromBase64Url(privateKeyBase64Url);
|
|
55
|
-
const sigBytes = impl.sign(secretKey, message);
|
|
56
|
-
// keyId derived from first 16 chars of the secretKey base64url
|
|
57
|
-
const keyId = privateKeyBase64Url.slice(0, 16);
|
|
58
|
-
return {
|
|
59
|
-
header: envelope.header,
|
|
60
|
-
body: envelope.body,
|
|
61
|
-
signature: {
|
|
62
|
-
alg: algorithm,
|
|
63
|
-
keyId,
|
|
64
|
-
value: toBase64Url(sigBytes),
|
|
65
|
-
},
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
// ─── Verify ──────────────────────────────────────────────────────────────────
|
|
69
|
-
export async function verifyEnvelopePq(envelope, publicKeyBase64Url) {
|
|
70
|
-
if (!envelope.signature)
|
|
71
|
-
return false;
|
|
72
|
-
const alg = envelope.signature.alg;
|
|
73
|
-
if (alg !== 'ML-DSA-65' && alg !== 'ML-DSA-87')
|
|
74
|
-
return false;
|
|
75
|
-
const impl = alg === 'ML-DSA-65' ? ml_dsa65 : ml_dsa87;
|
|
76
|
-
const unsigned = { header: envelope.header, body: envelope.body };
|
|
77
|
-
const canonical = canonicalizeEnvelope(unsigned);
|
|
78
|
-
const message = new TextEncoder().encode(canonical);
|
|
79
|
-
const publicKey = fromBase64Url(publicKeyBase64Url);
|
|
80
|
-
const sigBytes = fromBase64Url(envelope.signature.value);
|
|
81
|
-
try {
|
|
82
|
-
return impl.verify(publicKey, message, sigBytes);
|
|
83
|
-
}
|
|
84
|
-
catch {
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
// ─── Algorithm-specific aliases ──────────────────────────────────────────────
|
|
89
|
-
export async function signEnvelopeMlDsa65(envelope, privateKeyBase64Url) {
|
|
90
|
-
return signEnvelopePq(envelope, privateKeyBase64Url, 'ML-DSA-65');
|
|
91
|
-
}
|
|
92
|
-
export async function signEnvelopeMlDsa87(envelope, privateKeyBase64Url) {
|
|
93
|
-
return signEnvelopePq(envelope, privateKeyBase64Url, 'ML-DSA-87');
|
|
94
|
-
}
|
|
95
|
-
export async function verifyEnvelopeMlDsa65(envelope, publicKeyBase64Url) {
|
|
96
|
-
return verifyEnvelopePq(envelope, publicKeyBase64Url);
|
|
97
|
-
}
|
|
98
|
-
export async function verifyEnvelopeMlDsa87(envelope, publicKeyBase64Url) {
|
|
99
|
-
return verifyEnvelopePq(envelope, publicKeyBase64Url);
|
|
100
|
-
}
|
package/dist/src/index.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
export type { ProtocolEnvelope, ProtocolHeader, ProtocolBody } from '../../src/protocol.js';
|
|
2
|
-
export { canonicalizeEnvelope, createEnvelope } from '../../src/protocol.js';
|
|
3
|
-
import type { ProtocolEnvelope, ProtocolHeader, ProtocolBody } from '../../src/protocol.js';
|
|
4
|
-
export type PqAlgorithm = 'ML-DSA-65' | 'ML-DSA-87';
|
|
5
|
-
export interface PqKeyPair {
|
|
6
|
-
algorithm: PqAlgorithm;
|
|
7
|
-
publicKey: string;
|
|
8
|
-
privateKey: string;
|
|
9
|
-
createdAt: number;
|
|
10
|
-
}
|
|
11
|
-
/**
|
|
12
|
-
* Extended envelope type that carries a PQ signature.
|
|
13
|
-
* Structurally identical to ProtocolEnvelope but alg is widened.
|
|
14
|
-
*/
|
|
15
|
-
export interface PqProtocolEnvelope {
|
|
16
|
-
header: ProtocolHeader;
|
|
17
|
-
body: ProtocolBody;
|
|
18
|
-
signature?: {
|
|
19
|
-
alg: PqAlgorithm;
|
|
20
|
-
keyId: string;
|
|
21
|
-
value: string;
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
export declare function generatePqKeyPair(algorithm?: PqAlgorithm): PqKeyPair;
|
|
25
|
-
export declare function signEnvelopePq(envelope: Omit<ProtocolEnvelope, 'signature'>, privateKeyBase64Url: string, algorithm?: PqAlgorithm): Promise<PqProtocolEnvelope>;
|
|
26
|
-
export declare function verifyEnvelopePq(envelope: PqProtocolEnvelope, publicKeyBase64Url: string): Promise<boolean>;
|
|
27
|
-
export declare function signEnvelopeMlDsa65(envelope: Omit<ProtocolEnvelope, 'signature'>, privateKeyBase64Url: string): Promise<PqProtocolEnvelope>;
|
|
28
|
-
export declare function signEnvelopeMlDsa87(envelope: Omit<ProtocolEnvelope, 'signature'>, privateKeyBase64Url: string): Promise<PqProtocolEnvelope>;
|
|
29
|
-
export declare function verifyEnvelopeMlDsa65(envelope: PqProtocolEnvelope, publicKeyBase64Url: string): Promise<boolean>;
|
|
30
|
-
export declare function verifyEnvelopeMlDsa87(envelope: PqProtocolEnvelope, publicKeyBase64Url: string): Promise<boolean>;
|
package/dist/src/index.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import { ml_dsa65, ml_dsa87 } from '@noble/post-quantum/ml-dsa';
|
|
2
|
-
export { canonicalizeEnvelope, createEnvelope } from '../../src/protocol.js';
|
|
3
|
-
import { canonicalizeEnvelope } from '../../src/protocol.js';
|
|
4
|
-
// ─── Base64url helpers ───────────────────────────────────────────────────────
|
|
5
|
-
function toBase64Url(bytes) {
|
|
6
|
-
const g = globalThis;
|
|
7
|
-
if (g.Buffer) {
|
|
8
|
-
return g.Buffer.from(bytes)
|
|
9
|
-
.toString('base64')
|
|
10
|
-
.replace(/\+/g, '-')
|
|
11
|
-
.replace(/\//g, '_')
|
|
12
|
-
.replace(/=+$/g, '');
|
|
13
|
-
}
|
|
14
|
-
let binary = '';
|
|
15
|
-
for (let i = 0; i < bytes.length; i++)
|
|
16
|
-
binary += String.fromCharCode(bytes[i]);
|
|
17
|
-
return btoa(binary)
|
|
18
|
-
.replace(/\+/g, '-')
|
|
19
|
-
.replace(/\//g, '_')
|
|
20
|
-
.replace(/=+$/g, '');
|
|
21
|
-
}
|
|
22
|
-
function fromBase64Url(value) {
|
|
23
|
-
const padded = value
|
|
24
|
-
.replace(/-/g, '+')
|
|
25
|
-
.replace(/_/g, '/')
|
|
26
|
-
.padEnd(Math.ceil(value.length / 4) * 4, '=');
|
|
27
|
-
const g = globalThis;
|
|
28
|
-
if (g.Buffer) {
|
|
29
|
-
return new Uint8Array(g.Buffer.from(padded, 'base64'));
|
|
30
|
-
}
|
|
31
|
-
const binary = atob(padded);
|
|
32
|
-
const bytes = new Uint8Array(binary.length);
|
|
33
|
-
for (let i = 0; i < binary.length; i++)
|
|
34
|
-
bytes[i] = binary.charCodeAt(i);
|
|
35
|
-
return bytes;
|
|
36
|
-
}
|
|
37
|
-
// ─── Key generation ──────────────────────────────────────────────────────────
|
|
38
|
-
export function generatePqKeyPair(algorithm = 'ML-DSA-65') {
|
|
39
|
-
const impl = algorithm === 'ML-DSA-65' ? ml_dsa65 : ml_dsa87;
|
|
40
|
-
const seed = crypto.getRandomValues(new Uint8Array(32));
|
|
41
|
-
const { publicKey, secretKey } = impl.keygen(seed);
|
|
42
|
-
return {
|
|
43
|
-
algorithm,
|
|
44
|
-
publicKey: toBase64Url(publicKey),
|
|
45
|
-
privateKey: toBase64Url(secretKey),
|
|
46
|
-
createdAt: Date.now(),
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
// ─── Sign ────────────────────────────────────────────────────────────────────
|
|
50
|
-
export async function signEnvelopePq(envelope, privateKeyBase64Url, algorithm = 'ML-DSA-65') {
|
|
51
|
-
const impl = algorithm === 'ML-DSA-65' ? ml_dsa65 : ml_dsa87;
|
|
52
|
-
const canonical = canonicalizeEnvelope(envelope);
|
|
53
|
-
const message = new TextEncoder().encode(canonical);
|
|
54
|
-
const secretKey = fromBase64Url(privateKeyBase64Url);
|
|
55
|
-
const sigBytes = impl.sign(secretKey, message);
|
|
56
|
-
// keyId derived from first 16 chars of the secretKey base64url
|
|
57
|
-
const keyId = privateKeyBase64Url.slice(0, 16);
|
|
58
|
-
return {
|
|
59
|
-
header: envelope.header,
|
|
60
|
-
body: envelope.body,
|
|
61
|
-
signature: {
|
|
62
|
-
alg: algorithm,
|
|
63
|
-
keyId,
|
|
64
|
-
value: toBase64Url(sigBytes),
|
|
65
|
-
},
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
// ─── Verify ──────────────────────────────────────────────────────────────────
|
|
69
|
-
export async function verifyEnvelopePq(envelope, publicKeyBase64Url) {
|
|
70
|
-
if (!envelope.signature)
|
|
71
|
-
return false;
|
|
72
|
-
const alg = envelope.signature.alg;
|
|
73
|
-
if (alg !== 'ML-DSA-65' && alg !== 'ML-DSA-87')
|
|
74
|
-
return false;
|
|
75
|
-
const impl = alg === 'ML-DSA-65' ? ml_dsa65 : ml_dsa87;
|
|
76
|
-
const unsigned = { header: envelope.header, body: envelope.body };
|
|
77
|
-
const canonical = canonicalizeEnvelope(unsigned);
|
|
78
|
-
const message = new TextEncoder().encode(canonical);
|
|
79
|
-
const publicKey = fromBase64Url(publicKeyBase64Url);
|
|
80
|
-
const sigBytes = fromBase64Url(envelope.signature.value);
|
|
81
|
-
try {
|
|
82
|
-
return impl.verify(publicKey, message, sigBytes);
|
|
83
|
-
}
|
|
84
|
-
catch {
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
// ─── Algorithm-specific aliases ──────────────────────────────────────────────
|
|
89
|
-
export async function signEnvelopeMlDsa65(envelope, privateKeyBase64Url) {
|
|
90
|
-
return signEnvelopePq(envelope, privateKeyBase64Url, 'ML-DSA-65');
|
|
91
|
-
}
|
|
92
|
-
export async function signEnvelopeMlDsa87(envelope, privateKeyBase64Url) {
|
|
93
|
-
return signEnvelopePq(envelope, privateKeyBase64Url, 'ML-DSA-87');
|
|
94
|
-
}
|
|
95
|
-
export async function verifyEnvelopeMlDsa65(envelope, publicKeyBase64Url) {
|
|
96
|
-
return verifyEnvelopePq(envelope, publicKeyBase64Url);
|
|
97
|
-
}
|
|
98
|
-
export async function verifyEnvelopeMlDsa87(envelope, publicKeyBase64Url) {
|
|
99
|
-
return verifyEnvelopePq(envelope, publicKeyBase64Url);
|
|
100
|
-
}
|