@shieldfive/crypto 1.0.0-alpha.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +228 -0
- package/LICENSE +201 -0
- package/README.md +275 -0
- package/SECURITY.md +130 -0
- package/dist/format/header.d.ts +73 -0
- package/dist/format/header.d.ts.map +1 -0
- package/dist/format/header.js +230 -0
- package/dist/format/header.js.map +1 -0
- package/dist/identity/index.d.ts +119 -0
- package/dist/identity/index.d.ts.map +1 -0
- package/dist/identity/index.js +217 -0
- package/dist/identity/index.js.map +1 -0
- package/dist/identity/sign.d.ts +92 -0
- package/dist/identity/sign.d.ts.map +1 -0
- package/dist/identity/sign.js +182 -0
- package/dist/identity/sign.js.map +1 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +146 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/encoding.d.ts +33 -0
- package/dist/internal/encoding.d.ts.map +1 -0
- package/dist/internal/encoding.js +135 -0
- package/dist/internal/encoding.js.map +1 -0
- package/dist/internal/hkdf.d.ts +14 -0
- package/dist/internal/hkdf.d.ts.map +1 -0
- package/dist/internal/hkdf.js +27 -0
- package/dist/internal/hkdf.js.map +1 -0
- package/dist/internal/hmac.d.ts +9 -0
- package/dist/internal/hmac.d.ts.map +1 -0
- package/dist/internal/hmac.js +18 -0
- package/dist/internal/hmac.js.map +1 -0
- package/dist/internal/runtime.d.ts +29 -0
- package/dist/internal/runtime.d.ts.map +1 -0
- package/dist/internal/runtime.js +67 -0
- package/dist/internal/runtime.js.map +1 -0
- package/dist/internal/sodium.d.ts +12 -0
- package/dist/internal/sodium.d.ts.map +1 -0
- package/dist/internal/sodium.js +34 -0
- package/dist/internal/sodium.js.map +1 -0
- package/dist/internal/types.d.ts +98 -0
- package/dist/internal/types.d.ts.map +1 -0
- package/dist/internal/types.js +70 -0
- package/dist/internal/types.js.map +1 -0
- package/dist/kdf/argon2id.d.ts +72 -0
- package/dist/kdf/argon2id.d.ts.map +1 -0
- package/dist/kdf/argon2id.js +98 -0
- package/dist/kdf/argon2id.js.map +1 -0
- package/dist/migration/v0-bridge.d.ts +94 -0
- package/dist/migration/v0-bridge.d.ts.map +1 -0
- package/dist/migration/v0-bridge.js +203 -0
- package/dist/migration/v0-bridge.js.map +1 -0
- package/dist/streams/aes-gcm-v1.d.ts +144 -0
- package/dist/streams/aes-gcm-v1.d.ts.map +1 -0
- package/dist/streams/aes-gcm-v1.js +456 -0
- package/dist/streams/aes-gcm-v1.js.map +1 -0
- package/dist/streams/pq-hybrid-v1.d.ts +219 -0
- package/dist/streams/pq-hybrid-v1.d.ts.map +1 -0
- package/dist/streams/pq-hybrid-v1.js +473 -0
- package/dist/streams/pq-hybrid-v1.js.map +1 -0
- package/dist/suites/aes-gcm-v0/api.d.ts +48 -0
- package/dist/suites/aes-gcm-v0/api.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v0/api.js +110 -0
- package/dist/suites/aes-gcm-v0/api.js.map +1 -0
- package/dist/suites/aes-gcm-v1/api.d.ts +54 -0
- package/dist/suites/aes-gcm-v1/api.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v1/api.js +181 -0
- package/dist/suites/aes-gcm-v1/api.js.map +1 -0
- package/dist/suites/aes-gcm-v1/index.d.ts +59 -0
- package/dist/suites/aes-gcm-v1/index.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v1/index.js +177 -0
- package/dist/suites/aes-gcm-v1/index.js.map +1 -0
- package/dist/suites/aes-gcm-v2/api.d.ts +44 -0
- package/dist/suites/aes-gcm-v2/api.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v2/api.js +162 -0
- package/dist/suites/aes-gcm-v2/api.js.map +1 -0
- package/dist/suites/aes-gcm-v2/index.d.ts +63 -0
- package/dist/suites/aes-gcm-v2/index.d.ts.map +1 -0
- package/dist/suites/aes-gcm-v2/index.js +193 -0
- package/dist/suites/aes-gcm-v2/index.js.map +1 -0
- package/dist/suites/pq-hybrid-v1/api.d.ts +67 -0
- package/dist/suites/pq-hybrid-v1/api.d.ts.map +1 -0
- package/dist/suites/pq-hybrid-v1/api.js +190 -0
- package/dist/suites/pq-hybrid-v1/api.js.map +1 -0
- package/dist/suites/pq-hybrid-v1/index.d.ts +101 -0
- package/dist/suites/pq-hybrid-v1/index.d.ts.map +1 -0
- package/dist/suites/pq-hybrid-v1/index.js +225 -0
- package/dist/suites/pq-hybrid-v1/index.js.map +1 -0
- package/dist/suites/xchacha-v1/api.d.ts +29 -0
- package/dist/suites/xchacha-v1/api.d.ts.map +1 -0
- package/dist/suites/xchacha-v1/api.js +154 -0
- package/dist/suites/xchacha-v1/api.js.map +1 -0
- package/dist/suites/xchacha-v1/index.d.ts +50 -0
- package/dist/suites/xchacha-v1/index.d.ts.map +1 -0
- package/dist/suites/xchacha-v1/index.js +133 -0
- package/dist/suites/xchacha-v1/index.js.map +1 -0
- package/package.json +124 -0
- package/spec/format-v0.md +81 -0
- package/spec/format-v1.md +400 -0
- package/spec/key-derivation.md +167 -0
- package/spec/threat-model.md +242 -0
- package/src/format/header.ts +316 -0
- package/src/identity/index.ts +371 -0
- package/src/identity/sign.ts +221 -0
- package/src/index.ts +224 -0
- package/src/internal/encoding.ts +152 -0
- package/src/internal/hkdf.ts +47 -0
- package/src/internal/hmac.ts +33 -0
- package/src/internal/runtime.ts +80 -0
- package/src/internal/sodium.ts +38 -0
- package/src/internal/types.ts +134 -0
- package/src/kdf/argon2id.ts +165 -0
- package/src/migration/v0-bridge.ts +347 -0
- package/src/streams/aes-gcm-v1.ts +647 -0
- package/src/streams/pq-hybrid-v1.ts +722 -0
- package/src/suites/aes-gcm-v0/api.ts +155 -0
- package/src/suites/aes-gcm-v1/api.ts +292 -0
- package/src/suites/aes-gcm-v1/index.ts +269 -0
- package/src/suites/aes-gcm-v2/api.ts +268 -0
- package/src/suites/aes-gcm-v2/index.ts +289 -0
- package/src/suites/pq-hybrid-v1/api.ts +299 -0
- package/src/suites/pq-hybrid-v1/index.ts +343 -0
- package/src/suites/xchacha-v1/api.ts +254 -0
- package/src/suites/xchacha-v1/index.ts +213 -0
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sender-attribution signatures over the v1 wire format.
|
|
3
|
+
*
|
|
4
|
+
* The v1 file format already authenticates with HMAC under the content
|
|
5
|
+
* key (`header_mac`) and per-chunk AEAD tags, so a recipient can be sure
|
|
6
|
+
* the file decrypts to bytes the encryptor produced. What it does NOT
|
|
7
|
+
* tell them is WHO encrypted it — anyone with the content key could have
|
|
8
|
+
* written the file.
|
|
9
|
+
*
|
|
10
|
+
* This module adds an optional detached signature over
|
|
11
|
+
* `header_unauthenticated_bytes || concat(per-chunk MAC tags)` so that a
|
|
12
|
+
* recipient who trusts a sender's public key can verify authorship. The
|
|
13
|
+
* signature lives in an optional trailing structure (see
|
|
14
|
+
* `spec/format-v1.md § "Signature block"`); legacy files without that
|
|
15
|
+
* structure continue to decrypt unchanged, with the receiving side
|
|
16
|
+
* reporting `signature: null`.
|
|
17
|
+
*
|
|
18
|
+
* Algorithm 0x01 (Ed25519) is the only algorithm defined in v1. 0x02 is
|
|
19
|
+
* reserved for ML-DSA-65; the format leaves room for it but does not yet
|
|
20
|
+
* implement it.
|
|
21
|
+
*/
|
|
22
|
+
/** Signature algorithm: Ed25519 (RFC 8032). 32-byte key, 64-byte signature. */
|
|
23
|
+
export declare const SIGNATURE_ALGO_ED25519 = 1;
|
|
24
|
+
/** Signature algorithm reserved for ML-DSA-65 (FIPS 204). Not yet implemented. */
|
|
25
|
+
export declare const SIGNATURE_ALGO_ML_DSA_65 = 2;
|
|
26
|
+
export declare const ED25519_PUBLIC_KEY_LENGTH = 32;
|
|
27
|
+
export declare const ED25519_SECRET_KEY_LENGTH = 32;
|
|
28
|
+
export declare const ED25519_SIGNATURE_LENGTH = 64;
|
|
29
|
+
export interface SignatureBlock {
|
|
30
|
+
/** Algorithm identifier; see `SIGNATURE_ALGO_*`. */
|
|
31
|
+
algorithm: number;
|
|
32
|
+
/** Sender's verifying key. Length depends on the algorithm. */
|
|
33
|
+
publicKey: Uint8Array;
|
|
34
|
+
/** Detached signature over `signed_message`. */
|
|
35
|
+
signature: Uint8Array;
|
|
36
|
+
}
|
|
37
|
+
export declare class SignatureError extends Error {
|
|
38
|
+
readonly code: string;
|
|
39
|
+
constructor(code: string);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Produce a detached Ed25519 signature over
|
|
43
|
+
* `header_unauthenticated_bytes || concat(chunk_macs)`.
|
|
44
|
+
*
|
|
45
|
+
* `header` MUST be the exact bytes covered by the file's `header_mac`
|
|
46
|
+
* (i.e. the output of `buildHeaderUnauthenticated` or
|
|
47
|
+
* `parseHeader().unauthenticatedBytes`). `chunkMacs` MUST be the per-chunk
|
|
48
|
+
* AEAD tags in chunk-index order. For suites defined in v1 (AES-GCM-128,
|
|
49
|
+
* ChaCha20-Poly1305) the tag is the trailing 16 bytes of the chunk's
|
|
50
|
+
* ciphertext field.
|
|
51
|
+
*
|
|
52
|
+
* Returns the raw 64-byte signature. Callers wrap it in a signature block
|
|
53
|
+
* via `buildSignatureBlock`.
|
|
54
|
+
*/
|
|
55
|
+
export declare function signHeaderAndMacs(input: {
|
|
56
|
+
ed25519SecretKey: Uint8Array;
|
|
57
|
+
header: Uint8Array;
|
|
58
|
+
chunkMacs: ReadonlyArray<Uint8Array>;
|
|
59
|
+
}): Uint8Array;
|
|
60
|
+
/**
|
|
61
|
+
* Verify a detached Ed25519 signature produced by `signHeaderAndMacs`.
|
|
62
|
+
* Returns `true` on success, `false` on any failure (wrong length,
|
|
63
|
+
* malformed point, bad signature). Never throws on a verification
|
|
64
|
+
* mismatch — failures are not exceptional.
|
|
65
|
+
*/
|
|
66
|
+
export declare function verifyHeaderAndMacs(input: {
|
|
67
|
+
ed25519PublicKey: Uint8Array;
|
|
68
|
+
header: Uint8Array;
|
|
69
|
+
chunkMacs: ReadonlyArray<Uint8Array>;
|
|
70
|
+
signature: Uint8Array;
|
|
71
|
+
}): boolean;
|
|
72
|
+
/**
|
|
73
|
+
* Derive the Ed25519 public key from a 32-byte secret seed.
|
|
74
|
+
*/
|
|
75
|
+
export declare function deriveEd25519PublicKey(ed25519SecretKey: Uint8Array): Uint8Array;
|
|
76
|
+
/**
|
|
77
|
+
* Serialize a signature block:
|
|
78
|
+
* 1 byte algorithm || 2 bytes BE pubkey_len || pubkey || 2 bytes BE sig_len || sig
|
|
79
|
+
*/
|
|
80
|
+
export declare function buildSignatureBlock(block: SignatureBlock): Uint8Array;
|
|
81
|
+
/**
|
|
82
|
+
* Parse a signature block starting at `offset` in `bytes`. Returns the
|
|
83
|
+
* parsed block and the number of bytes consumed. Throws on malformed
|
|
84
|
+
* input (truncation, oversize, or — for known algorithms — wrong key /
|
|
85
|
+
* signature length). Unknown algorithms parse successfully; verification
|
|
86
|
+
* against an unknown algorithm is the caller's policy decision.
|
|
87
|
+
*/
|
|
88
|
+
export declare function parseSignatureBlock(bytes: Uint8Array, offset?: number): {
|
|
89
|
+
block: SignatureBlock;
|
|
90
|
+
consumed: number;
|
|
91
|
+
};
|
|
92
|
+
//# sourceMappingURL=sign.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sign.d.ts","sourceRoot":"","sources":["../../src/identity/sign.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAUH,+EAA+E;AAC/E,eAAO,MAAM,sBAAsB,IAAO,CAAA;AAE1C,kFAAkF;AAClF,eAAO,MAAM,wBAAwB,IAAO,CAAA;AAE5C,eAAO,MAAM,yBAAyB,KAAK,CAAA;AAC3C,eAAO,MAAM,yBAAyB,KAAK,CAAA;AAC3C,eAAO,MAAM,wBAAwB,KAAK,CAAA;AAE1C,MAAM,WAAW,cAAc;IAC7B,oDAAoD;IACpD,SAAS,EAAE,MAAM,CAAA;IACjB,+DAA+D;IAC/D,SAAS,EAAE,UAAU,CAAA;IACrB,gDAAgD;IAChD,SAAS,EAAE,UAAU,CAAA;CACtB;AAED,qBAAa,cAAe,SAAQ,KAAK;IACvC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;gBACT,IAAI,EAAE,MAAM;CAKzB;AAkBD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE;IACvC,gBAAgB,EAAE,UAAU,CAAA;IAC5B,MAAM,EAAE,UAAU,CAAA;IAClB,SAAS,EAAE,aAAa,CAAC,UAAU,CAAC,CAAA;CACrC,GAAG,UAAU,CAMb;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE;IACzC,gBAAgB,EAAE,UAAU,CAAA;IAC5B,MAAM,EAAE,UAAU,CAAA;IAClB,SAAS,EAAE,aAAa,CAAC,UAAU,CAAC,CAAA;IACpC,SAAS,EAAE,UAAU,CAAA;CACtB,GAAG,OAAO,CAUV;AAED;;GAEG;AACH,wBAAgB,sBAAsB,CACpC,gBAAgB,EAAE,UAAU,GAC3B,UAAU,CAKZ;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,cAAc,GAAG,UAAU,CAgCrE;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,UAAU,EACjB,MAAM,SAAI,GACT;IAAE,KAAK,EAAE,cAAc,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAqC7C"}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sender-attribution signatures over the v1 wire format.
|
|
3
|
+
*
|
|
4
|
+
* The v1 file format already authenticates with HMAC under the content
|
|
5
|
+
* key (`header_mac`) and per-chunk AEAD tags, so a recipient can be sure
|
|
6
|
+
* the file decrypts to bytes the encryptor produced. What it does NOT
|
|
7
|
+
* tell them is WHO encrypted it — anyone with the content key could have
|
|
8
|
+
* written the file.
|
|
9
|
+
*
|
|
10
|
+
* This module adds an optional detached signature over
|
|
11
|
+
* `header_unauthenticated_bytes || concat(per-chunk MAC tags)` so that a
|
|
12
|
+
* recipient who trusts a sender's public key can verify authorship. The
|
|
13
|
+
* signature lives in an optional trailing structure (see
|
|
14
|
+
* `spec/format-v1.md § "Signature block"`); legacy files without that
|
|
15
|
+
* structure continue to decrypt unchanged, with the receiving side
|
|
16
|
+
* reporting `signature: null`.
|
|
17
|
+
*
|
|
18
|
+
* Algorithm 0x01 (Ed25519) is the only algorithm defined in v1. 0x02 is
|
|
19
|
+
* reserved for ML-DSA-65; the format leaves room for it but does not yet
|
|
20
|
+
* implement it.
|
|
21
|
+
*/
|
|
22
|
+
import { ed25519 } from '@noble/curves/ed25519.js';
|
|
23
|
+
import { concatBytes, readUint16BE, uint16BE, } from '../internal/encoding.js';
|
|
24
|
+
/** Signature algorithm: Ed25519 (RFC 8032). 32-byte key, 64-byte signature. */
|
|
25
|
+
export const SIGNATURE_ALGO_ED25519 = 0x01;
|
|
26
|
+
/** Signature algorithm reserved for ML-DSA-65 (FIPS 204). Not yet implemented. */
|
|
27
|
+
export const SIGNATURE_ALGO_ML_DSA_65 = 0x02;
|
|
28
|
+
export const ED25519_PUBLIC_KEY_LENGTH = 32;
|
|
29
|
+
export const ED25519_SECRET_KEY_LENGTH = 32;
|
|
30
|
+
export const ED25519_SIGNATURE_LENGTH = 64;
|
|
31
|
+
export class SignatureError extends Error {
|
|
32
|
+
code;
|
|
33
|
+
constructor(code) {
|
|
34
|
+
super(`shieldfive-crypto: signature error: ${code}`);
|
|
35
|
+
this.name = 'SignatureError';
|
|
36
|
+
this.code = code;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
function buildSignedMessage(header, chunkMacs) {
|
|
40
|
+
let totalMacLen = 0;
|
|
41
|
+
for (const mac of chunkMacs)
|
|
42
|
+
totalMacLen += mac.length;
|
|
43
|
+
const out = new Uint8Array(header.length + totalMacLen);
|
|
44
|
+
out.set(header, 0);
|
|
45
|
+
let offset = header.length;
|
|
46
|
+
for (const mac of chunkMacs) {
|
|
47
|
+
out.set(mac, offset);
|
|
48
|
+
offset += mac.length;
|
|
49
|
+
}
|
|
50
|
+
return out;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Produce a detached Ed25519 signature over
|
|
54
|
+
* `header_unauthenticated_bytes || concat(chunk_macs)`.
|
|
55
|
+
*
|
|
56
|
+
* `header` MUST be the exact bytes covered by the file's `header_mac`
|
|
57
|
+
* (i.e. the output of `buildHeaderUnauthenticated` or
|
|
58
|
+
* `parseHeader().unauthenticatedBytes`). `chunkMacs` MUST be the per-chunk
|
|
59
|
+
* AEAD tags in chunk-index order. For suites defined in v1 (AES-GCM-128,
|
|
60
|
+
* ChaCha20-Poly1305) the tag is the trailing 16 bytes of the chunk's
|
|
61
|
+
* ciphertext field.
|
|
62
|
+
*
|
|
63
|
+
* Returns the raw 64-byte signature. Callers wrap it in a signature block
|
|
64
|
+
* via `buildSignatureBlock`.
|
|
65
|
+
*/
|
|
66
|
+
export function signHeaderAndMacs(input) {
|
|
67
|
+
if (input.ed25519SecretKey.length !== ED25519_SECRET_KEY_LENGTH) {
|
|
68
|
+
throw new SignatureError('ed25519_secret_key_wrong_length');
|
|
69
|
+
}
|
|
70
|
+
const message = buildSignedMessage(input.header, input.chunkMacs);
|
|
71
|
+
return ed25519.sign(message, input.ed25519SecretKey);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Verify a detached Ed25519 signature produced by `signHeaderAndMacs`.
|
|
75
|
+
* Returns `true` on success, `false` on any failure (wrong length,
|
|
76
|
+
* malformed point, bad signature). Never throws on a verification
|
|
77
|
+
* mismatch — failures are not exceptional.
|
|
78
|
+
*/
|
|
79
|
+
export function verifyHeaderAndMacs(input) {
|
|
80
|
+
if (input.ed25519PublicKey.length !== ED25519_PUBLIC_KEY_LENGTH)
|
|
81
|
+
return false;
|
|
82
|
+
if (input.signature.length !== ED25519_SIGNATURE_LENGTH)
|
|
83
|
+
return false;
|
|
84
|
+
const message = buildSignedMessage(input.header, input.chunkMacs);
|
|
85
|
+
try {
|
|
86
|
+
return ed25519.verify(input.signature, message, input.ed25519PublicKey);
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
// Malformed pubkey or sig encoding — noble throws; we treat as not-verified.
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Derive the Ed25519 public key from a 32-byte secret seed.
|
|
95
|
+
*/
|
|
96
|
+
export function deriveEd25519PublicKey(ed25519SecretKey) {
|
|
97
|
+
if (ed25519SecretKey.length !== ED25519_SECRET_KEY_LENGTH) {
|
|
98
|
+
throw new SignatureError('ed25519_secret_key_wrong_length');
|
|
99
|
+
}
|
|
100
|
+
return ed25519.getPublicKey(ed25519SecretKey);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Serialize a signature block:
|
|
104
|
+
* 1 byte algorithm || 2 bytes BE pubkey_len || pubkey || 2 bytes BE sig_len || sig
|
|
105
|
+
*/
|
|
106
|
+
export function buildSignatureBlock(block) {
|
|
107
|
+
if (block.algorithm < 0 ||
|
|
108
|
+
block.algorithm > 0xff ||
|
|
109
|
+
!Number.isInteger(block.algorithm)) {
|
|
110
|
+
throw new SignatureError('algorithm_out_of_range');
|
|
111
|
+
}
|
|
112
|
+
if (block.algorithm === 0x00) {
|
|
113
|
+
throw new SignatureError('algorithm_reserved');
|
|
114
|
+
}
|
|
115
|
+
if (block.publicKey.length > 0xffff) {
|
|
116
|
+
throw new SignatureError('public_key_too_large');
|
|
117
|
+
}
|
|
118
|
+
if (block.signature.length > 0xffff) {
|
|
119
|
+
throw new SignatureError('signature_too_large');
|
|
120
|
+
}
|
|
121
|
+
if (block.algorithm === SIGNATURE_ALGO_ED25519) {
|
|
122
|
+
if (block.publicKey.length !== ED25519_PUBLIC_KEY_LENGTH) {
|
|
123
|
+
throw new SignatureError('ed25519_public_key_wrong_length');
|
|
124
|
+
}
|
|
125
|
+
if (block.signature.length !== ED25519_SIGNATURE_LENGTH) {
|
|
126
|
+
throw new SignatureError('ed25519_signature_wrong_length');
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return concatBytes([
|
|
130
|
+
new Uint8Array([block.algorithm]),
|
|
131
|
+
uint16BE(block.publicKey.length),
|
|
132
|
+
block.publicKey,
|
|
133
|
+
uint16BE(block.signature.length),
|
|
134
|
+
block.signature,
|
|
135
|
+
]);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Parse a signature block starting at `offset` in `bytes`. Returns the
|
|
139
|
+
* parsed block and the number of bytes consumed. Throws on malformed
|
|
140
|
+
* input (truncation, oversize, or — for known algorithms — wrong key /
|
|
141
|
+
* signature length). Unknown algorithms parse successfully; verification
|
|
142
|
+
* against an unknown algorithm is the caller's policy decision.
|
|
143
|
+
*/
|
|
144
|
+
export function parseSignatureBlock(bytes, offset = 0) {
|
|
145
|
+
const FIXED_HEADER = 1 + 2; // algorithm + pubkey_len
|
|
146
|
+
if (bytes.length - offset < FIXED_HEADER) {
|
|
147
|
+
throw new SignatureError('block_truncated');
|
|
148
|
+
}
|
|
149
|
+
let pos = offset;
|
|
150
|
+
const algorithm = bytes[pos];
|
|
151
|
+
pos += 1;
|
|
152
|
+
if (algorithm === 0x00) {
|
|
153
|
+
throw new SignatureError('algorithm_reserved');
|
|
154
|
+
}
|
|
155
|
+
const pubkeyLen = readUint16BE(bytes, pos);
|
|
156
|
+
pos += 2;
|
|
157
|
+
if (bytes.length - pos < pubkeyLen + 2) {
|
|
158
|
+
throw new SignatureError('block_truncated');
|
|
159
|
+
}
|
|
160
|
+
const publicKey = bytes.slice(pos, pos + pubkeyLen);
|
|
161
|
+
pos += pubkeyLen;
|
|
162
|
+
const sigLen = readUint16BE(bytes, pos);
|
|
163
|
+
pos += 2;
|
|
164
|
+
if (bytes.length - pos < sigLen) {
|
|
165
|
+
throw new SignatureError('block_truncated');
|
|
166
|
+
}
|
|
167
|
+
const signature = bytes.slice(pos, pos + sigLen);
|
|
168
|
+
pos += sigLen;
|
|
169
|
+
if (algorithm === SIGNATURE_ALGO_ED25519) {
|
|
170
|
+
if (publicKey.length !== ED25519_PUBLIC_KEY_LENGTH) {
|
|
171
|
+
throw new SignatureError('ed25519_public_key_wrong_length');
|
|
172
|
+
}
|
|
173
|
+
if (signature.length !== ED25519_SIGNATURE_LENGTH) {
|
|
174
|
+
throw new SignatureError('ed25519_signature_wrong_length');
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return {
|
|
178
|
+
block: { algorithm, publicKey, signature },
|
|
179
|
+
consumed: pos - offset,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=sign.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sign.js","sourceRoot":"","sources":["../../src/identity/sign.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,0BAA0B,CAAA;AAElD,OAAO,EACL,WAAW,EACX,YAAY,EACZ,QAAQ,GACT,MAAM,yBAAyB,CAAA;AAEhC,+EAA+E;AAC/E,MAAM,CAAC,MAAM,sBAAsB,GAAG,IAAI,CAAA;AAE1C,kFAAkF;AAClF,MAAM,CAAC,MAAM,wBAAwB,GAAG,IAAI,CAAA;AAE5C,MAAM,CAAC,MAAM,yBAAyB,GAAG,EAAE,CAAA;AAC3C,MAAM,CAAC,MAAM,yBAAyB,GAAG,EAAE,CAAA;AAC3C,MAAM,CAAC,MAAM,wBAAwB,GAAG,EAAE,CAAA;AAW1C,MAAM,OAAO,cAAe,SAAQ,KAAK;IAC9B,IAAI,CAAQ;IACrB,YAAY,IAAY;QACtB,KAAK,CAAC,uCAAuC,IAAI,EAAE,CAAC,CAAA;QACpD,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAA;QAC5B,IAAI,CAAC,IAAI,GAAG,IAAI,CAAA;IAClB,CAAC;CACF;AAED,SAAS,kBAA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CAAC,GAAG,EAAE,GAAG,GAAG,SAAS,CAAC,CAAA;IACnD,GAAG,IAAI,SAAS,CAAA;IAChB,MAAM,MAAM,GAAG,YAAY,CAAC,KAAK,EAAE,GAAG,CAAC,CAAA;IACvC,GAAG,IAAI,CAAC,CAAA;IACR,IAAI,KAAK,CAAC,MAAM,GAAG,GAAG,GAAG,MAAM,EAAE,CAAC;QAChC,MAAM,IAAI,cAAc,CAAC,iBAAiB,CAAC,CAAA;IAC7C,CAAC;IACD,MAAM,SAAS,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,GAAG,MAAM,CAAC,CAAA;IAChD,GAAG,IAAI,MAAM,CAAA;IACb,IAAI,SAAS,KAAK,sBAAsB,EAAE,CAAC;QACzC,IAAI,SAAS,CAAC,MAAM,KAAK,yBAAyB,EAAE,CAAC;YACnD,MAAM,IAAI,cAAc,CAAC,iCAAiC,CAAC,CAAA;QAC7D,CAAC;QACD,IAAI,SAAS,CAAC,MAAM,KAAK,wBAAwB,EAAE,CAAC;YAClD,MAAM,IAAI,cAAc,CAAC,gCAAgC,CAAC,CAAA;QAC5D,CAAC;IACH,CAAC;IACD,OAAO;QACL,KAAK,EAAE,EAAE,SAAS,EAAE,SAAS,EAAE,SAAS,EAAE;QAC1C,QAAQ,EAAE,GAAG,GAAG,MAAM;KACvB,CAAA;AACH,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @shieldfive/crypto — open-source, post-quantum-hybrid client-side
|
|
3
|
+
* encryption for cloud storage.
|
|
4
|
+
*
|
|
5
|
+
* Public entry point. Most callers should import from a specific suite
|
|
6
|
+
* subpath instead of the bare module name:
|
|
7
|
+
*
|
|
8
|
+
* import { encryptBlob, decryptBlob } from '@shieldfive/crypto/pq-hybrid-v1'
|
|
9
|
+
* import { encryptBlob, decryptBlob } from '@shieldfive/crypto/aes-gcm-v2'
|
|
10
|
+
* import { decryptBlob } from '@shieldfive/crypto/aes-gcm-v1' // decrypt-only
|
|
11
|
+
* import { encryptBlob, decryptBlob } from '@shieldfive/crypto/xchacha-v1'
|
|
12
|
+
* import { decryptV0 } from '@shieldfive/crypto/legacy-v0'
|
|
13
|
+
*
|
|
14
|
+
* The umbrella `import { ... } from '@shieldfive/crypto'` exposes shared
|
|
15
|
+
* format primitives, type definitions, and an auto-routing decryptor that
|
|
16
|
+
* dispatches by suite ID.
|
|
17
|
+
*/
|
|
18
|
+
export { buildAuthenticatedHeader, buildChunkAad, buildHeaderUnauthenticated, deriveHeaderMacKey, HeaderError, parseHeader, verifyHeaderMac, } from './format/header.js';
|
|
19
|
+
export { CHUNK_AAD_DOMAIN, CHUNK_AAD_LENGTH, DEFAULT_CHUNK_SIZE, FORMAT_MAGIC, FORMAT_MAGIC_LENGTH, HEADER_FIXED_PREFIX_LENGTH, HEADER_SIZES, HKDF_INFO, MAX_CHUNK_SIZE, MAX_TOTAL_CHUNKS, SUITE, SUITE_NAMES, } from './internal/types.js';
|
|
20
|
+
export type { EncryptedFile, ParsedHeader, ProgressCallback, SuiteId, SuiteName, } from './internal/types.js';
|
|
21
|
+
export { base64ToBytes, bytesToBase64, bytesToHex, hexToBytes, concatBytes, } from './internal/encoding.js';
|
|
22
|
+
export { constantTimeEqual, randomBytes, zeroize, } from './internal/runtime.js';
|
|
23
|
+
import { decryptBlob as aesGcmV1DecryptBlob, decryptToBytes as aesGcmV1DecryptToBytes } from './suites/aes-gcm-v1/api.js';
|
|
24
|
+
export declare const aesGcmV1: {
|
|
25
|
+
decryptBlob: typeof aesGcmV1DecryptBlob;
|
|
26
|
+
decryptToBytes: typeof aesGcmV1DecryptToBytes;
|
|
27
|
+
};
|
|
28
|
+
export * as aesGcmV2 from './suites/aes-gcm-v2/api.js';
|
|
29
|
+
export * as xchachaV1 from './suites/xchacha-v1/api.js';
|
|
30
|
+
export * as pqHybridV1 from './suites/pq-hybrid-v1/api.js';
|
|
31
|
+
export * as legacyV0 from './suites/aes-gcm-v0/api.js';
|
|
32
|
+
export * as streamsAesGcmV1 from './streams/aes-gcm-v1.js';
|
|
33
|
+
export * as kdfArgon2id from './kdf/argon2id.js';
|
|
34
|
+
export * as identity from './identity/index.js';
|
|
35
|
+
export * as migrationV0 from './migration/v0-bridge.js';
|
|
36
|
+
export interface AutoDecryptOptions {
|
|
37
|
+
blob: Blob;
|
|
38
|
+
/** Used for AES-GCM-v1/v2 and XChaCha-v1 (32-byte raw content key) */
|
|
39
|
+
contentKey?: Uint8Array;
|
|
40
|
+
/** Required for PQ-hybrid: ML-KEM-1024 secret key (3168 bytes) */
|
|
41
|
+
recipientSecretKey?: Uint8Array;
|
|
42
|
+
/** Required for PQ-hybrid: classical 32-byte envelope key */
|
|
43
|
+
envelopeKey?: Uint8Array;
|
|
44
|
+
/** Optional progress callback (0..1) */
|
|
45
|
+
onProgress?: (progress: number) => void;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Decrypt a v1-format blob, automatically dispatching to the correct suite
|
|
49
|
+
* based on the header's suite byte. Throws if the suite requires inputs the
|
|
50
|
+
* caller didn't provide. For v0 (legacy) files, use `legacyV0.decryptV0`
|
|
51
|
+
* directly — v0 has no header, so the dispatcher cannot route to it.
|
|
52
|
+
*/
|
|
53
|
+
export declare function autoDecryptBlob(options: AutoDecryptOptions): Promise<Blob>;
|
|
54
|
+
/** Library version, also written into format documentation. */
|
|
55
|
+
export declare const SHIELDFIVE_CRYPTO_VERSION = "1.0.0-alpha.11";
|
|
56
|
+
/** Default cipher suite for new files when not specified by the caller. */
|
|
57
|
+
export declare const DEFAULT_SUITE: 3;
|
|
58
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAKH,OAAO,EACL,wBAAwB,EACxB,aAAa,EACb,0BAA0B,EAC1B,kBAAk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ACxB,wCAAwC;IACxC,UAAU,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,IAAI,CAAA;CACxC;AAED;;;;;GAKG;AACH,wBAAsB,eAAe,CACnC,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,IAAI,CAAC,CAsFf;AAED,+DAA+D;AAC/D,eAAO,MAAM,yBAAyB,mBAAmB,CAAA;AAEzD,2EAA2E;AAC3E,eAAO,MAAM,aAAa,GAAuC,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @shieldfive/crypto — open-source, post-quantum-hybrid client-side
|
|
3
|
+
* encryption for cloud storage.
|
|
4
|
+
*
|
|
5
|
+
* Public entry point. Most callers should import from a specific suite
|
|
6
|
+
* subpath instead of the bare module name:
|
|
7
|
+
*
|
|
8
|
+
* import { encryptBlob, decryptBlob } from '@shieldfive/crypto/pq-hybrid-v1'
|
|
9
|
+
* import { encryptBlob, decryptBlob } from '@shieldfive/crypto/aes-gcm-v2'
|
|
10
|
+
* import { decryptBlob } from '@shieldfive/crypto/aes-gcm-v1' // decrypt-only
|
|
11
|
+
* import { encryptBlob, decryptBlob } from '@shieldfive/crypto/xchacha-v1'
|
|
12
|
+
* import { decryptV0 } from '@shieldfive/crypto/legacy-v0'
|
|
13
|
+
*
|
|
14
|
+
* The umbrella `import { ... } from '@shieldfive/crypto'` exposes shared
|
|
15
|
+
* format primitives, type definitions, and an auto-routing decryptor that
|
|
16
|
+
* dispatches by suite ID.
|
|
17
|
+
*/
|
|
18
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
19
|
+
// Format primitives
|
|
20
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
21
|
+
export { buildAuthenticatedHeader, buildChunkAad, buildHeaderUnauthenticated, deriveHeaderMacKey, HeaderError, parseHeader, verifyHeaderMac, } from './format/header.js';
|
|
22
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
23
|
+
// Type definitions and constants
|
|
24
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
25
|
+
export { CHUNK_AAD_DOMAIN, CHUNK_AAD_LENGTH, DEFAULT_CHUNK_SIZE, FORMAT_MAGIC, FORMAT_MAGIC_LENGTH, HEADER_FIXED_PREFIX_LENGTH, HEADER_SIZES, HKDF_INFO, MAX_CHUNK_SIZE, MAX_TOTAL_CHUNKS, SUITE, SUITE_NAMES, } from './internal/types.js';
|
|
26
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
27
|
+
// Encoding utilities (base64, hex, uint codecs)
|
|
28
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
29
|
+
export { base64ToBytes, bytesToBase64, bytesToHex, hexToBytes, concatBytes, } from './internal/encoding.js';
|
|
30
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
31
|
+
// Runtime helpers
|
|
32
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
33
|
+
export { constantTimeEqual, randomBytes, zeroize, } from './internal/runtime.js';
|
|
34
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
35
|
+
// Re-export suite namespaces for direct consumption.
|
|
36
|
+
// The aes-gcm encrypt path on the umbrella is v2 (0x04). v1 (0x01) is
|
|
37
|
+
// exposed decrypt-only here because new files should never be written
|
|
38
|
+
// with the narrow 4-byte nonce prefix. Callers that genuinely need to
|
|
39
|
+
// re-encrypt as v1 (e.g. migration tooling) can still import from the
|
|
40
|
+
// @shieldfive/crypto/aes-gcm-v1 subpath.
|
|
41
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
42
|
+
import { decryptBlob as aesGcmV1DecryptBlob, decryptToBytes as aesGcmV1DecryptToBytes, } from './suites/aes-gcm-v1/api.js';
|
|
43
|
+
export const aesGcmV1 = {
|
|
44
|
+
decryptBlob: aesGcmV1DecryptBlob,
|
|
45
|
+
decryptToBytes: aesGcmV1DecryptToBytes,
|
|
46
|
+
};
|
|
47
|
+
export * as aesGcmV2 from './suites/aes-gcm-v2/api.js';
|
|
48
|
+
export * as xchachaV1 from './suites/xchacha-v1/api.js';
|
|
49
|
+
export * as pqHybridV1 from './suites/pq-hybrid-v1/api.js';
|
|
50
|
+
export * as legacyV0 from './suites/aes-gcm-v0/api.js';
|
|
51
|
+
export * as streamsAesGcmV1 from './streams/aes-gcm-v1.js';
|
|
52
|
+
export * as kdfArgon2id from './kdf/argon2id.js';
|
|
53
|
+
export * as identity from './identity/index.js';
|
|
54
|
+
export * as migrationV0 from './migration/v0-bridge.js';
|
|
55
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
56
|
+
// Auto-routing decryptor
|
|
57
|
+
// ──────────────────────────────────────────────────────────────────────
|
|
58
|
+
import { parseHeader, HeaderError } from './format/header.js';
|
|
59
|
+
import { SUITE } from './internal/types.js';
|
|
60
|
+
import * as aes from './suites/aes-gcm-v1/api.js';
|
|
61
|
+
import * as aesV2 from './suites/aes-gcm-v2/api.js';
|
|
62
|
+
import * as xchacha from './suites/xchacha-v1/api.js';
|
|
63
|
+
import * as pqHybrid from './suites/pq-hybrid-v1/api.js';
|
|
64
|
+
/**
|
|
65
|
+
* Decrypt a v1-format blob, automatically dispatching to the correct suite
|
|
66
|
+
* based on the header's suite byte. Throws if the suite requires inputs the
|
|
67
|
+
* caller didn't provide. For v0 (legacy) files, use `legacyV0.decryptV0`
|
|
68
|
+
* directly — v0 has no header, so the dispatcher cannot route to it.
|
|
69
|
+
*/
|
|
70
|
+
export async function autoDecryptBlob(options) {
|
|
71
|
+
const { blob } = options;
|
|
72
|
+
if (!(blob instanceof Blob)) {
|
|
73
|
+
throw new TypeError('autoDecryptBlob: blob must be a Blob or File');
|
|
74
|
+
}
|
|
75
|
+
const headerProbe = new Uint8Array(await blob.slice(0, Math.min(blob.size, 4096)).arrayBuffer());
|
|
76
|
+
let suite;
|
|
77
|
+
try {
|
|
78
|
+
const parsed = parseHeader(headerProbe);
|
|
79
|
+
suite = parsed.suite;
|
|
80
|
+
}
|
|
81
|
+
catch (err) {
|
|
82
|
+
// If parse fails because of a too-short probe, escalate. Suites with
|
|
83
|
+
// larger suite_payloads (notably PQ hybrid at 1664 bytes) need more.
|
|
84
|
+
if (err instanceof HeaderError && err.code === 'header_too_short') {
|
|
85
|
+
const bigger = new Uint8Array(await blob
|
|
86
|
+
.slice(0, Math.min(blob.size, 4 + 0xffff + 64))
|
|
87
|
+
.arrayBuffer());
|
|
88
|
+
const parsed = parseHeader(bigger);
|
|
89
|
+
suite = parsed.suite;
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
throw err;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
switch (suite) {
|
|
96
|
+
case SUITE.AES_256_GCM_V1: {
|
|
97
|
+
if (!options.contentKey) {
|
|
98
|
+
throw new Error('autoDecryptBlob: contentKey required for aes-256-gcm-v1');
|
|
99
|
+
}
|
|
100
|
+
return aes.decryptBlob({
|
|
101
|
+
blob,
|
|
102
|
+
contentKey: options.contentKey,
|
|
103
|
+
...(options.onProgress ? { onProgress: options.onProgress } : {}),
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
case SUITE.AES_256_GCM_V2: {
|
|
107
|
+
if (!options.contentKey) {
|
|
108
|
+
throw new Error('autoDecryptBlob: contentKey required for aes-256-gcm-v2');
|
|
109
|
+
}
|
|
110
|
+
return aesV2.decryptBlob({
|
|
111
|
+
blob,
|
|
112
|
+
contentKey: options.contentKey,
|
|
113
|
+
...(options.onProgress ? { onProgress: options.onProgress } : {}),
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
case SUITE.XCHACHA20_POLY1305_V1: {
|
|
117
|
+
if (!options.contentKey) {
|
|
118
|
+
throw new Error('autoDecryptBlob: contentKey required for xchacha20-poly1305-v1');
|
|
119
|
+
}
|
|
120
|
+
return xchacha.decryptBlob({
|
|
121
|
+
blob,
|
|
122
|
+
contentKey: options.contentKey,
|
|
123
|
+
...(options.onProgress ? { onProgress: options.onProgress } : {}),
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
case SUITE.PQ_HYBRID_XCHACHA_MLKEM1024_V1: {
|
|
127
|
+
if (!options.recipientSecretKey || !options.envelopeKey) {
|
|
128
|
+
throw new Error('autoDecryptBlob: recipientSecretKey and envelopeKey required for pq-hybrid-v1');
|
|
129
|
+
}
|
|
130
|
+
return pqHybrid.decryptBlob({
|
|
131
|
+
blob,
|
|
132
|
+
recipientSecretKey: options.recipientSecretKey,
|
|
133
|
+
envelopeKey: options.envelopeKey,
|
|
134
|
+
...(options.onProgress ? { onProgress: options.onProgress } : {}),
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
default: {
|
|
138
|
+
throw new HeaderError(`autoDecryptBlob: unknown suite 0x${suite.toString(16)}`);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
/** Library version, also written into format documentation. */
|
|
143
|
+
export const SHIELDFIVE_CRYPTO_VERSION = '1.0.0-alpha.11';
|
|
144
|
+
/** Default cipher suite for new files when not specified by the caller. */
|
|
145
|
+
export const DEFAULT_SUITE = SUITE.PQ_HYBRID_XCHACHA_MLKEM1024_V1;
|
|
146
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AAEH,yEAAyE;AACzE,oBAAoB;AACpB,yEAAyE;AACzE,OAAO,EACL,wBAAwB,EACxB,aAAa,EACb,0BAA0B,EAC1B,kBAAkB,EAClB,WAAW,EACX,WAAW,EACX,eAAe,GAChB,MAAM,oBAAoB,CAAA;AAE3B,yEAAyE;AACzE,iCAAiC;AACjC,yEAAyE;AACzE,OAAO,EACL,gBAAgB,EAChB,gBAAgB,EAChB,kBAAkB,EAClB,YAAY,EACZ,mBAAmB,EACnB,0BAA0B,EAC1B,YAAY,EACZ,SAAS,EACT,cAAc,EACd,gBAAgB,EAChB,KAAK,EACL,WAAW,GACZ,MAAM,qBAAqB,C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AAC,WAAW,EAAE,CAAC;gBACxD,MAAM,IAAI,KAAK,CACb,+EAA+E,CAChF,CAAA;YACH,CAAC;YACD,OAAO,QAAQ,CAAC,WAAW,CAAC;gBAC1B,IAAI;gBACJ,kBAAkB,EAAE,OAAO,CAAC,kBAAkB;gBAC9C,WAAW,EAAE,OAAO,CAAC,WAAW;gBAChC,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,UAAU,EAAE,OAAO,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;aAClE,CAAC,CAAA;QACJ,CAAC;QACD,OAAO,CAAC,CAAC,CAAC;YACR,MAAM,IAAI,WAAW,CACnB,oCAAqC,KAAgB,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CACrE,CAAA;QACH,CAAC;IACH,CAAC;AACH,CAAC;AAED,+DAA+D;AAC/D,MAAM,CAAC,MAAM,yBAAyB,GAAG,gBAAgB,CAAA;AAEzD,2EAA2E;AAC3E,MAAM,CAAC,MAAM,aAAa,GAAG,KAAK,CAAC,8BAA8B,CAAA"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cast helper: TypeScript 5.7+ distinguishes Uint8Array<ArrayBuffer> from
|
|
3
|
+
* Uint8Array<ArrayBufferLike>. Blob and many Web Crypto APIs require the
|
|
4
|
+
* narrower variant. This helper centralizes the cast so we don't sprinkle
|
|
5
|
+
* `as Uint8Array<ArrayBuffer>` across the codebase.
|
|
6
|
+
*
|
|
7
|
+
* Functionally a no-op at runtime; purely a type annotation.
|
|
8
|
+
*/
|
|
9
|
+
export declare function asBlobPart(bytes: Uint8Array): Uint8Array<ArrayBuffer>;
|
|
10
|
+
/**
|
|
11
|
+
* Byte-level encoding helpers.
|
|
12
|
+
*
|
|
13
|
+
* All multi-byte integer functions are explicitly big-endian to match the
|
|
14
|
+
* v1 wire format. Callers should prefer these over hand-rolled bit-shifting.
|
|
15
|
+
*/
|
|
16
|
+
export declare function bytesToBase64(bytes: Uint8Array): string;
|
|
17
|
+
export declare function base64ToBytes(value: string): Uint8Array;
|
|
18
|
+
export declare function bytesToHex(bytes: Uint8Array): string;
|
|
19
|
+
export declare function hexToBytes(hex: string): Uint8Array;
|
|
20
|
+
export declare function concatBytes(parts: ReadonlyArray<Uint8Array>): Uint8Array;
|
|
21
|
+
export declare function uint32BE(value: number): Uint8Array;
|
|
22
|
+
export declare function readUint32BE(bytes: Uint8Array, offset?: number): number;
|
|
23
|
+
export declare function uint16BE(value: number): Uint8Array;
|
|
24
|
+
export declare function readUint16BE(bytes: Uint8Array, offset?: number): number;
|
|
25
|
+
/**
|
|
26
|
+
* Encode a non-negative integer as 8 big-endian bytes. The library never
|
|
27
|
+
* emits values exceeding Number.MAX_SAFE_INTEGER (2^53 - 1) because the
|
|
28
|
+
* format spec caps total_chunks at 1e9 and plaintext_size is bounded by
|
|
29
|
+
* what JavaScript can sensibly handle.
|
|
30
|
+
*/
|
|
31
|
+
export declare function uint64BE(value: number): Uint8Array;
|
|
32
|
+
export declare function readUint64BE(bytes: Uint8Array, offset?: number): number;
|
|
33
|
+
//# sourceMappingURL=encoding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"encoding.d.ts","sourceRoot":"","sources":["../../src/internal/encoding.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAAC,WAAW,CAAC,CAErE;AAED;;;;;GAKG;AAEH,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAUvD;AAED,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAavD;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,CAMpD;AAID,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,UAAU,CAYlD;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,aAAa,CAAC,UAAU,CAAC,GAAG,UAAU,CAUxE;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAOlD;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,SAAI,GAAG,MAAM,CAQlE;AAED,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAOlD;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,SAAI,GAAG,MAAM,CAQlE;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,UAAU,CAWlD;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,UAAU,EAAE,MAAM,SAAI,GAAG,MAAM,CAYlE"}
|