@bcts/crypto 1.0.0-alpha.5
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 +48 -0
- package/README.md +65 -0
- package/dist/index.cjs +658 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +403 -0
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +403 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.iife.js +652 -0
- package/dist/index.iife.js.map +1 -0
- package/dist/index.mjs +594 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +81 -0
- package/src/argon.ts +47 -0
- package/src/ecdsa-keys.ts +81 -0
- package/src/ecdsa-signing.ts +66 -0
- package/src/ed25519-signing.ts +72 -0
- package/src/error.ts +49 -0
- package/src/hash.ts +134 -0
- package/src/index.ts +115 -0
- package/src/memzero.ts +37 -0
- package/src/public-key-encryption.ts +68 -0
- package/src/schnorr-signing.ts +90 -0
- package/src/scrypt.ts +50 -0
- package/src/symmetric-encryption.ts +133 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,658 @@
|
|
|
1
|
+
let __noble_hashes_sha2 = require("@noble/hashes/sha2");
|
|
2
|
+
let __noble_hashes_hmac = require("@noble/hashes/hmac");
|
|
3
|
+
let __noble_hashes_pbkdf2 = require("@noble/hashes/pbkdf2");
|
|
4
|
+
let __noble_hashes_hkdf = require("@noble/hashes/hkdf");
|
|
5
|
+
let __noble_ciphers_chacha = require("@noble/ciphers/chacha");
|
|
6
|
+
let __noble_curves_ed25519 = require("@noble/curves/ed25519");
|
|
7
|
+
let __noble_curves_secp256k1 = require("@noble/curves/secp256k1");
|
|
8
|
+
let __bcts_rand = require("@bcts/rand");
|
|
9
|
+
let __noble_hashes_scrypt = require("@noble/hashes/scrypt");
|
|
10
|
+
let __noble_hashes_argon2 = require("@noble/hashes/argon2");
|
|
11
|
+
|
|
12
|
+
//#region src/error.ts
|
|
13
|
+
/**
|
|
14
|
+
* AEAD-specific error for authentication failures
|
|
15
|
+
*/
|
|
16
|
+
var AeadError = class extends Error {
|
|
17
|
+
constructor(message = "AEAD authentication failed") {
|
|
18
|
+
super(message);
|
|
19
|
+
this.name = "AeadError";
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Generic crypto error type
|
|
24
|
+
*/
|
|
25
|
+
var CryptoError = class CryptoError extends Error {
|
|
26
|
+
cause;
|
|
27
|
+
constructor(message, cause) {
|
|
28
|
+
super(message);
|
|
29
|
+
this.name = "CryptoError";
|
|
30
|
+
this.cause = cause;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Create a CryptoError for AEAD authentication failures.
|
|
34
|
+
*
|
|
35
|
+
* @param error - Optional underlying AeadError
|
|
36
|
+
* @returns A CryptoError wrapping the AEAD error
|
|
37
|
+
*/
|
|
38
|
+
static aead(error) {
|
|
39
|
+
return new CryptoError("AEAD error", error ?? new AeadError());
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Create a CryptoError for invalid parameter values.
|
|
43
|
+
*
|
|
44
|
+
* @param message - Description of the invalid parameter
|
|
45
|
+
* @returns A CryptoError describing the invalid parameter
|
|
46
|
+
*/
|
|
47
|
+
static invalidParameter(message) {
|
|
48
|
+
return new CryptoError(`Invalid parameter: ${message}`);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
//#endregion
|
|
53
|
+
//#region src/memzero.ts
|
|
54
|
+
/**
|
|
55
|
+
* Securely zero out a typed array.
|
|
56
|
+
*
|
|
57
|
+
* **IMPORTANT: This is a best-effort implementation.** Unlike the Rust reference
|
|
58
|
+
* implementation which uses `std::ptr::write_volatile()` for guaranteed volatile
|
|
59
|
+
* writes, JavaScript engines and JIT compilers can still potentially optimize
|
|
60
|
+
* away these zeroing operations. The check at the end helps prevent optimization,
|
|
61
|
+
* but it is not foolproof.
|
|
62
|
+
*
|
|
63
|
+
* For truly sensitive cryptographic operations, consider using the Web Crypto API's
|
|
64
|
+
* `crypto.subtle` with non-extractable keys when possible, as it provides stronger
|
|
65
|
+
* guarantees than what can be achieved with pure JavaScript.
|
|
66
|
+
*
|
|
67
|
+
* This function attempts to prevent the compiler from optimizing away
|
|
68
|
+
* the zeroing operation by using a verification check after the zeroing loop.
|
|
69
|
+
*/
|
|
70
|
+
function memzero(data) {
|
|
71
|
+
const len = data.length;
|
|
72
|
+
for (let i = 0; i < len; i++) data[i] = 0;
|
|
73
|
+
if (data.length > 0 && data[0] !== 0) throw new Error("memzero failed");
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Securely zero out an array of Uint8Arrays.
|
|
77
|
+
*/
|
|
78
|
+
function memzeroVecVecU8(arrays) {
|
|
79
|
+
for (const arr of arrays) memzero(arr);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
//#endregion
|
|
83
|
+
//#region src/hash.ts
|
|
84
|
+
const CRC32_SIZE = 4;
|
|
85
|
+
const SHA256_SIZE = 32;
|
|
86
|
+
const SHA512_SIZE = 64;
|
|
87
|
+
const CRC32_TABLE = new Uint32Array(256);
|
|
88
|
+
for (let i = 0; i < 256; i++) {
|
|
89
|
+
let crc = i;
|
|
90
|
+
for (let j = 0; j < 8; j++) crc = (crc & 1) !== 0 ? crc >>> 1 ^ 3988292384 : crc >>> 1;
|
|
91
|
+
CRC32_TABLE[i] = crc >>> 0;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Calculate CRC-32 checksum
|
|
95
|
+
*/
|
|
96
|
+
function crc32(data) {
|
|
97
|
+
let crc = 4294967295;
|
|
98
|
+
for (const byte of data) crc = CRC32_TABLE[(crc ^ byte) & 255] ^ crc >>> 8;
|
|
99
|
+
return (crc ^ 4294967295) >>> 0;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Calculate CRC-32 checksum and return as a 4-byte big-endian array
|
|
103
|
+
*/
|
|
104
|
+
function crc32Data(data) {
|
|
105
|
+
return crc32DataOpt(data, false);
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Calculate CRC-32 checksum and return as a 4-byte array
|
|
109
|
+
* @param data - Input data
|
|
110
|
+
* @param littleEndian - If true, returns little-endian; otherwise big-endian
|
|
111
|
+
*/
|
|
112
|
+
function crc32DataOpt(data, littleEndian) {
|
|
113
|
+
const checksum = crc32(data);
|
|
114
|
+
const result = new Uint8Array(4);
|
|
115
|
+
new DataView(result.buffer).setUint32(0, checksum, littleEndian);
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Calculate SHA-256 hash
|
|
120
|
+
*/
|
|
121
|
+
function sha256(data) {
|
|
122
|
+
return (0, __noble_hashes_sha2.sha256)(data);
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Calculate double SHA-256 hash (SHA-256 of SHA-256)
|
|
126
|
+
* This is the standard Bitcoin hashing function
|
|
127
|
+
*/
|
|
128
|
+
function doubleSha256(message) {
|
|
129
|
+
return sha256(sha256(message));
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Calculate SHA-512 hash
|
|
133
|
+
*/
|
|
134
|
+
function sha512(data) {
|
|
135
|
+
return (0, __noble_hashes_sha2.sha512)(data);
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Calculate HMAC-SHA-256
|
|
139
|
+
*/
|
|
140
|
+
function hmacSha256(key, message) {
|
|
141
|
+
return (0, __noble_hashes_hmac.hmac)(__noble_hashes_sha2.sha256, key, message);
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Calculate HMAC-SHA-512
|
|
145
|
+
*/
|
|
146
|
+
function hmacSha512(key, message) {
|
|
147
|
+
return (0, __noble_hashes_hmac.hmac)(__noble_hashes_sha2.sha512, key, message);
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Derive a key using PBKDF2 with HMAC-SHA-256
|
|
151
|
+
*/
|
|
152
|
+
function pbkdf2HmacSha256(password, salt, iterations, keyLen) {
|
|
153
|
+
return (0, __noble_hashes_pbkdf2.pbkdf2)(__noble_hashes_sha2.sha256, password, salt, {
|
|
154
|
+
c: iterations,
|
|
155
|
+
dkLen: keyLen
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Derive a key using PBKDF2 with HMAC-SHA-512
|
|
160
|
+
*/
|
|
161
|
+
function pbkdf2HmacSha512(password, salt, iterations, keyLen) {
|
|
162
|
+
return (0, __noble_hashes_pbkdf2.pbkdf2)(__noble_hashes_sha2.sha512, password, salt, {
|
|
163
|
+
c: iterations,
|
|
164
|
+
dkLen: keyLen
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* Derive a key using HKDF with HMAC-SHA-256
|
|
169
|
+
*/
|
|
170
|
+
function hkdfHmacSha256(keyMaterial, salt, keyLen) {
|
|
171
|
+
return (0, __noble_hashes_hkdf.hkdf)(__noble_hashes_sha2.sha256, keyMaterial, salt, void 0, keyLen);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Derive a key using HKDF with HMAC-SHA-512
|
|
175
|
+
*/
|
|
176
|
+
function hkdfHmacSha512(keyMaterial, salt, keyLen) {
|
|
177
|
+
return (0, __noble_hashes_hkdf.hkdf)(__noble_hashes_sha2.sha512, keyMaterial, salt, void 0, keyLen);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
//#endregion
|
|
181
|
+
//#region src/symmetric-encryption.ts
|
|
182
|
+
const SYMMETRIC_KEY_SIZE = 32;
|
|
183
|
+
const SYMMETRIC_NONCE_SIZE = 12;
|
|
184
|
+
const SYMMETRIC_AUTH_SIZE = 16;
|
|
185
|
+
/**
|
|
186
|
+
* Encrypt data using ChaCha20-Poly1305 AEAD cipher.
|
|
187
|
+
*
|
|
188
|
+
* **Security Warning**: The nonce MUST be unique for every encryption operation
|
|
189
|
+
* with the same key. Reusing a nonce completely breaks the security of the
|
|
190
|
+
* encryption scheme and can reveal plaintext.
|
|
191
|
+
*
|
|
192
|
+
* @param plaintext - The data to encrypt
|
|
193
|
+
* @param key - 32-byte encryption key
|
|
194
|
+
* @param nonce - 12-byte nonce (MUST be unique per encryption with the same key)
|
|
195
|
+
* @returns Tuple of [ciphertext, authTag] where authTag is 16 bytes
|
|
196
|
+
* @throws {CryptoError} If key is not 32 bytes or nonce is not 12 bytes
|
|
197
|
+
*/
|
|
198
|
+
function aeadChaCha20Poly1305Encrypt(plaintext, key, nonce) {
|
|
199
|
+
return aeadChaCha20Poly1305EncryptWithAad(plaintext, key, nonce, new Uint8Array(0));
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Encrypt data using ChaCha20-Poly1305 AEAD cipher with additional authenticated data.
|
|
203
|
+
*
|
|
204
|
+
* **Security Warning**: The nonce MUST be unique for every encryption operation
|
|
205
|
+
* with the same key. Reusing a nonce completely breaks the security of the
|
|
206
|
+
* encryption scheme and can reveal plaintext.
|
|
207
|
+
*
|
|
208
|
+
* @param plaintext - The data to encrypt
|
|
209
|
+
* @param key - 32-byte encryption key
|
|
210
|
+
* @param nonce - 12-byte nonce (MUST be unique per encryption with the same key)
|
|
211
|
+
* @param aad - Additional authenticated data (not encrypted, but integrity-protected)
|
|
212
|
+
* @returns Tuple of [ciphertext, authTag] where authTag is 16 bytes
|
|
213
|
+
* @throws {CryptoError} If key is not 32 bytes or nonce is not 12 bytes
|
|
214
|
+
*/
|
|
215
|
+
function aeadChaCha20Poly1305EncryptWithAad(plaintext, key, nonce, aad) {
|
|
216
|
+
if (key.length !== SYMMETRIC_KEY_SIZE) throw CryptoError.invalidParameter(`Key must be ${SYMMETRIC_KEY_SIZE} bytes`);
|
|
217
|
+
if (nonce.length !== SYMMETRIC_NONCE_SIZE) throw CryptoError.invalidParameter(`Nonce must be ${SYMMETRIC_NONCE_SIZE} bytes`);
|
|
218
|
+
const sealed = (0, __noble_ciphers_chacha.chacha20poly1305)(key, nonce, aad).encrypt(plaintext);
|
|
219
|
+
return [sealed.slice(0, sealed.length - SYMMETRIC_AUTH_SIZE), sealed.slice(sealed.length - SYMMETRIC_AUTH_SIZE)];
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Decrypt data using ChaCha20-Poly1305 AEAD cipher.
|
|
223
|
+
*
|
|
224
|
+
* @param ciphertext - The encrypted data
|
|
225
|
+
* @param key - 32-byte encryption key (must match key used for encryption)
|
|
226
|
+
* @param nonce - 12-byte nonce (must match nonce used for encryption)
|
|
227
|
+
* @param authTag - 16-byte authentication tag from encryption
|
|
228
|
+
* @returns Decrypted plaintext
|
|
229
|
+
* @throws {CryptoError} If key/nonce/authTag sizes are invalid
|
|
230
|
+
* @throws {CryptoError} If authentication fails (tampered data or wrong key/nonce)
|
|
231
|
+
*/
|
|
232
|
+
function aeadChaCha20Poly1305Decrypt(ciphertext, key, nonce, authTag) {
|
|
233
|
+
return aeadChaCha20Poly1305DecryptWithAad(ciphertext, key, nonce, new Uint8Array(0), authTag);
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Decrypt data using ChaCha20-Poly1305 AEAD cipher with additional authenticated data.
|
|
237
|
+
*
|
|
238
|
+
* @param ciphertext - The encrypted data
|
|
239
|
+
* @param key - 32-byte encryption key (must match key used for encryption)
|
|
240
|
+
* @param nonce - 12-byte nonce (must match nonce used for encryption)
|
|
241
|
+
* @param aad - Additional authenticated data (must exactly match AAD used for encryption)
|
|
242
|
+
* @param authTag - 16-byte authentication tag from encryption
|
|
243
|
+
* @returns Decrypted plaintext
|
|
244
|
+
* @throws {CryptoError} If key/nonce/authTag sizes are invalid
|
|
245
|
+
* @throws {CryptoError} If authentication fails (tampered data, wrong key/nonce, or AAD mismatch)
|
|
246
|
+
*/
|
|
247
|
+
function aeadChaCha20Poly1305DecryptWithAad(ciphertext, key, nonce, aad, authTag) {
|
|
248
|
+
if (key.length !== SYMMETRIC_KEY_SIZE) throw CryptoError.invalidParameter(`Key must be ${SYMMETRIC_KEY_SIZE} bytes`);
|
|
249
|
+
if (nonce.length !== SYMMETRIC_NONCE_SIZE) throw CryptoError.invalidParameter(`Nonce must be ${SYMMETRIC_NONCE_SIZE} bytes`);
|
|
250
|
+
if (authTag.length !== SYMMETRIC_AUTH_SIZE) throw CryptoError.invalidParameter(`Auth tag must be ${SYMMETRIC_AUTH_SIZE} bytes`);
|
|
251
|
+
const sealed = new Uint8Array(ciphertext.length + authTag.length);
|
|
252
|
+
sealed.set(ciphertext);
|
|
253
|
+
sealed.set(authTag, ciphertext.length);
|
|
254
|
+
try {
|
|
255
|
+
return (0, __noble_ciphers_chacha.chacha20poly1305)(key, nonce, aad).decrypt(sealed);
|
|
256
|
+
} catch (error) {
|
|
257
|
+
const aeadError = new AeadError(`Decryption failed: ${error instanceof Error ? error.message : "authentication error"}`);
|
|
258
|
+
throw CryptoError.aead(aeadError);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
//#endregion
|
|
263
|
+
//#region src/public-key-encryption.ts
|
|
264
|
+
const GENERIC_PRIVATE_KEY_SIZE = 32;
|
|
265
|
+
const GENERIC_PUBLIC_KEY_SIZE = 32;
|
|
266
|
+
const X25519_PRIVATE_KEY_SIZE = 32;
|
|
267
|
+
const X25519_PUBLIC_KEY_SIZE = 32;
|
|
268
|
+
/**
|
|
269
|
+
* Derive an X25519 agreement private key from key material.
|
|
270
|
+
* Uses HKDF with "agreement" as domain separation salt.
|
|
271
|
+
*/
|
|
272
|
+
function deriveAgreementPrivateKey(keyMaterial) {
|
|
273
|
+
return hkdfHmacSha256(keyMaterial, new TextEncoder().encode("agreement"), X25519_PRIVATE_KEY_SIZE);
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Derive a signing private key from key material.
|
|
277
|
+
* Uses HKDF with "signing" as domain separation salt.
|
|
278
|
+
*/
|
|
279
|
+
function deriveSigningPrivateKey(keyMaterial) {
|
|
280
|
+
return hkdfHmacSha256(keyMaterial, new TextEncoder().encode("signing"), 32);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* Generate a new random X25519 private key.
|
|
284
|
+
*/
|
|
285
|
+
function x25519NewPrivateKeyUsing(rng) {
|
|
286
|
+
return rng.randomData(X25519_PRIVATE_KEY_SIZE);
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Derive an X25519 public key from a private key.
|
|
290
|
+
*/
|
|
291
|
+
function x25519PublicKeyFromPrivateKey(privateKey) {
|
|
292
|
+
if (privateKey.length !== X25519_PRIVATE_KEY_SIZE) throw new Error(`Private key must be ${X25519_PRIVATE_KEY_SIZE} bytes`);
|
|
293
|
+
return __noble_curves_ed25519.x25519.getPublicKey(privateKey);
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Compute a shared secret using X25519 key agreement (ECDH).
|
|
297
|
+
*
|
|
298
|
+
* **Security Note**: The resulting shared secret should be used with a KDF
|
|
299
|
+
* (like HKDF) before using it as an encryption key. Never use the raw
|
|
300
|
+
* shared secret directly for encryption.
|
|
301
|
+
*
|
|
302
|
+
* @param x25519Private - 32-byte X25519 private key
|
|
303
|
+
* @param x25519Public - 32-byte X25519 public key from the other party
|
|
304
|
+
* @returns 32-byte shared secret
|
|
305
|
+
* @throws {Error} If private key is not 32 bytes or public key is not 32 bytes
|
|
306
|
+
*/
|
|
307
|
+
function x25519SharedKey(x25519Private, x25519Public) {
|
|
308
|
+
if (x25519Private.length !== X25519_PRIVATE_KEY_SIZE) throw new Error(`Private key must be ${X25519_PRIVATE_KEY_SIZE} bytes`);
|
|
309
|
+
if (x25519Public.length !== X25519_PUBLIC_KEY_SIZE) throw new Error(`Public key must be ${X25519_PUBLIC_KEY_SIZE} bytes`);
|
|
310
|
+
return __noble_curves_ed25519.x25519.getSharedSecret(x25519Private, x25519Public);
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
//#endregion
|
|
314
|
+
//#region src/ecdsa-keys.ts
|
|
315
|
+
const ECDSA_PRIVATE_KEY_SIZE = 32;
|
|
316
|
+
const ECDSA_PUBLIC_KEY_SIZE = 33;
|
|
317
|
+
const ECDSA_UNCOMPRESSED_PUBLIC_KEY_SIZE = 65;
|
|
318
|
+
const ECDSA_MESSAGE_HASH_SIZE = 32;
|
|
319
|
+
const ECDSA_SIGNATURE_SIZE = 64;
|
|
320
|
+
const SCHNORR_PUBLIC_KEY_SIZE = 32;
|
|
321
|
+
/**
|
|
322
|
+
* Generate a new random ECDSA private key using secp256k1.
|
|
323
|
+
*
|
|
324
|
+
* Note: Unlike some implementations, this directly returns the random bytes
|
|
325
|
+
* without validation. The secp256k1 library will handle any edge cases when
|
|
326
|
+
* the key is used.
|
|
327
|
+
*/
|
|
328
|
+
function ecdsaNewPrivateKeyUsing(rng) {
|
|
329
|
+
return rng.randomData(ECDSA_PRIVATE_KEY_SIZE);
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Derive a compressed ECDSA public key from a private key.
|
|
333
|
+
*/
|
|
334
|
+
function ecdsaPublicKeyFromPrivateKey(privateKey) {
|
|
335
|
+
if (privateKey.length !== ECDSA_PRIVATE_KEY_SIZE) throw new Error(`Private key must be ${ECDSA_PRIVATE_KEY_SIZE} bytes`);
|
|
336
|
+
return __noble_curves_secp256k1.secp256k1.getPublicKey(privateKey, true);
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Decompress a compressed public key to uncompressed format.
|
|
340
|
+
*/
|
|
341
|
+
function ecdsaDecompressPublicKey(compressed) {
|
|
342
|
+
if (compressed.length !== ECDSA_PUBLIC_KEY_SIZE) throw new Error(`Compressed public key must be ${ECDSA_PUBLIC_KEY_SIZE} bytes`);
|
|
343
|
+
return __noble_curves_secp256k1.secp256k1.ProjectivePoint.fromHex(compressed).toRawBytes(false);
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Compress an uncompressed public key.
|
|
347
|
+
*/
|
|
348
|
+
function ecdsaCompressPublicKey(uncompressed) {
|
|
349
|
+
if (uncompressed.length !== ECDSA_UNCOMPRESSED_PUBLIC_KEY_SIZE) throw new Error(`Uncompressed public key must be ${ECDSA_UNCOMPRESSED_PUBLIC_KEY_SIZE} bytes`);
|
|
350
|
+
return __noble_curves_secp256k1.secp256k1.ProjectivePoint.fromHex(uncompressed).toRawBytes(true);
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* Derive an ECDSA private key from key material using HKDF.
|
|
354
|
+
*
|
|
355
|
+
* Note: This directly returns the HKDF output without validation,
|
|
356
|
+
* matching the Rust reference implementation behavior.
|
|
357
|
+
*/
|
|
358
|
+
function ecdsaDerivePrivateKey(keyMaterial) {
|
|
359
|
+
return hkdfHmacSha256(keyMaterial, new TextEncoder().encode("signing"), ECDSA_PRIVATE_KEY_SIZE);
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Extract the x-only (Schnorr) public key from a private key.
|
|
363
|
+
* This is used for BIP-340 Schnorr signatures.
|
|
364
|
+
*/
|
|
365
|
+
function schnorrPublicKeyFromPrivateKey(privateKey) {
|
|
366
|
+
if (privateKey.length !== ECDSA_PRIVATE_KEY_SIZE) throw new Error(`Private key must be ${ECDSA_PRIVATE_KEY_SIZE} bytes`);
|
|
367
|
+
return __noble_curves_secp256k1.secp256k1.getPublicKey(privateKey, false).slice(1, 33);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
//#endregion
|
|
371
|
+
//#region src/ecdsa-signing.ts
|
|
372
|
+
/**
|
|
373
|
+
* Sign a message using ECDSA with secp256k1.
|
|
374
|
+
*
|
|
375
|
+
* The message is hashed with double SHA-256 before signing (Bitcoin standard).
|
|
376
|
+
*
|
|
377
|
+
* **Security Note**: The private key must be kept secret. ECDSA requires
|
|
378
|
+
* cryptographically secure random nonces internally; this is handled by
|
|
379
|
+
* the underlying library using RFC 6979 deterministic nonces.
|
|
380
|
+
*
|
|
381
|
+
* @param privateKey - 32-byte secp256k1 private key
|
|
382
|
+
* @param message - Message to sign (any length, will be double-SHA256 hashed)
|
|
383
|
+
* @returns 64-byte compact signature (r || s format)
|
|
384
|
+
* @throws {Error} If private key is not 32 bytes
|
|
385
|
+
*/
|
|
386
|
+
function ecdsaSign(privateKey, message) {
|
|
387
|
+
if (privateKey.length !== ECDSA_PRIVATE_KEY_SIZE) throw new Error(`Private key must be ${ECDSA_PRIVATE_KEY_SIZE} bytes`);
|
|
388
|
+
const messageHash = doubleSha256(message);
|
|
389
|
+
return __noble_curves_secp256k1.secp256k1.sign(messageHash, privateKey).toCompactRawBytes();
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Verify an ECDSA signature with secp256k1.
|
|
393
|
+
*
|
|
394
|
+
* The message is hashed with double SHA-256 before verification (Bitcoin standard).
|
|
395
|
+
*
|
|
396
|
+
* @param publicKey - 33-byte compressed secp256k1 public key
|
|
397
|
+
* @param signature - 64-byte compact signature (r || s format)
|
|
398
|
+
* @param message - Original message that was signed
|
|
399
|
+
* @returns `true` if signature is valid, `false` if signature verification fails
|
|
400
|
+
* @throws {Error} If public key is not 33 bytes or signature is not 64 bytes
|
|
401
|
+
*/
|
|
402
|
+
function ecdsaVerify(publicKey, signature, message) {
|
|
403
|
+
if (publicKey.length !== ECDSA_PUBLIC_KEY_SIZE) throw new Error(`Public key must be ${ECDSA_PUBLIC_KEY_SIZE} bytes`);
|
|
404
|
+
if (signature.length !== ECDSA_SIGNATURE_SIZE) throw new Error(`Signature must be ${ECDSA_SIGNATURE_SIZE} bytes`);
|
|
405
|
+
try {
|
|
406
|
+
const messageHash = doubleSha256(message);
|
|
407
|
+
return __noble_curves_secp256k1.secp256k1.verify(signature, messageHash, publicKey);
|
|
408
|
+
} catch {
|
|
409
|
+
return false;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
//#endregion
|
|
414
|
+
//#region src/schnorr-signing.ts
|
|
415
|
+
const SCHNORR_SIGNATURE_SIZE = 64;
|
|
416
|
+
/**
|
|
417
|
+
* Sign a message using Schnorr signature (BIP-340).
|
|
418
|
+
* Uses secure random auxiliary randomness.
|
|
419
|
+
*
|
|
420
|
+
* @param ecdsaPrivateKey - 32-byte private key
|
|
421
|
+
* @param message - Message to sign (not pre-hashed, per BIP-340)
|
|
422
|
+
* @returns 64-byte Schnorr signature
|
|
423
|
+
*/
|
|
424
|
+
function schnorrSign(ecdsaPrivateKey, message) {
|
|
425
|
+
return schnorrSignUsing(ecdsaPrivateKey, message, new __bcts_rand.SecureRandomNumberGenerator());
|
|
426
|
+
}
|
|
427
|
+
/**
|
|
428
|
+
* Sign a message using Schnorr signature with a custom RNG.
|
|
429
|
+
*
|
|
430
|
+
* @param ecdsaPrivateKey - 32-byte private key
|
|
431
|
+
* @param message - Message to sign
|
|
432
|
+
* @param rng - Random number generator for auxiliary randomness
|
|
433
|
+
* @returns 64-byte Schnorr signature
|
|
434
|
+
*/
|
|
435
|
+
function schnorrSignUsing(ecdsaPrivateKey, message, rng) {
|
|
436
|
+
return schnorrSignWithAuxRand(ecdsaPrivateKey, message, rng.randomData(32));
|
|
437
|
+
}
|
|
438
|
+
/**
|
|
439
|
+
* Sign a message using Schnorr signature with specific auxiliary randomness.
|
|
440
|
+
* This is useful for deterministic signing in tests.
|
|
441
|
+
*
|
|
442
|
+
* @param ecdsaPrivateKey - 32-byte private key
|
|
443
|
+
* @param message - Message to sign
|
|
444
|
+
* @param auxRand - 32-byte auxiliary randomness (per BIP-340)
|
|
445
|
+
* @returns 64-byte Schnorr signature
|
|
446
|
+
*/
|
|
447
|
+
function schnorrSignWithAuxRand(ecdsaPrivateKey, message, auxRand) {
|
|
448
|
+
if (ecdsaPrivateKey.length !== ECDSA_PRIVATE_KEY_SIZE) throw new Error(`Private key must be ${ECDSA_PRIVATE_KEY_SIZE} bytes`);
|
|
449
|
+
if (auxRand.length !== 32) throw new Error("Auxiliary randomness must be 32 bytes");
|
|
450
|
+
return __noble_curves_secp256k1.schnorr.sign(message, ecdsaPrivateKey, auxRand);
|
|
451
|
+
}
|
|
452
|
+
/**
|
|
453
|
+
* Verify a Schnorr signature (BIP-340).
|
|
454
|
+
*
|
|
455
|
+
* @param schnorrPublicKey - 32-byte x-only public key
|
|
456
|
+
* @param signature - 64-byte Schnorr signature
|
|
457
|
+
* @param message - Original message
|
|
458
|
+
* @returns true if signature is valid
|
|
459
|
+
*/
|
|
460
|
+
function schnorrVerify(schnorrPublicKey, signature, message) {
|
|
461
|
+
if (schnorrPublicKey.length !== SCHNORR_PUBLIC_KEY_SIZE) throw new Error(`Public key must be ${SCHNORR_PUBLIC_KEY_SIZE} bytes`);
|
|
462
|
+
if (signature.length !== SCHNORR_SIGNATURE_SIZE) throw new Error(`Signature must be ${SCHNORR_SIGNATURE_SIZE} bytes`);
|
|
463
|
+
try {
|
|
464
|
+
return __noble_curves_secp256k1.schnorr.verify(signature, message, schnorrPublicKey);
|
|
465
|
+
} catch {
|
|
466
|
+
return false;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
//#endregion
|
|
471
|
+
//#region src/ed25519-signing.ts
|
|
472
|
+
const ED25519_PUBLIC_KEY_SIZE = 32;
|
|
473
|
+
const ED25519_PRIVATE_KEY_SIZE = 32;
|
|
474
|
+
const ED25519_SIGNATURE_SIZE = 64;
|
|
475
|
+
/**
|
|
476
|
+
* Generate a new random Ed25519 private key.
|
|
477
|
+
*/
|
|
478
|
+
function ed25519NewPrivateKeyUsing(rng) {
|
|
479
|
+
return rng.randomData(ED25519_PRIVATE_KEY_SIZE);
|
|
480
|
+
}
|
|
481
|
+
/**
|
|
482
|
+
* Derive an Ed25519 public key from a private key.
|
|
483
|
+
*/
|
|
484
|
+
function ed25519PublicKeyFromPrivateKey(privateKey) {
|
|
485
|
+
if (privateKey.length !== ED25519_PRIVATE_KEY_SIZE) throw new Error(`Private key must be ${ED25519_PRIVATE_KEY_SIZE} bytes`);
|
|
486
|
+
return __noble_curves_ed25519.ed25519.getPublicKey(privateKey);
|
|
487
|
+
}
|
|
488
|
+
/**
|
|
489
|
+
* Sign a message using Ed25519.
|
|
490
|
+
*
|
|
491
|
+
* **Security Note**: The private key must be kept secret. The same private key
|
|
492
|
+
* can safely sign multiple messages.
|
|
493
|
+
*
|
|
494
|
+
* @param privateKey - 32-byte Ed25519 private key
|
|
495
|
+
* @param message - Message to sign (any length)
|
|
496
|
+
* @returns 64-byte Ed25519 signature
|
|
497
|
+
* @throws {Error} If private key is not 32 bytes
|
|
498
|
+
*/
|
|
499
|
+
function ed25519Sign(privateKey, message) {
|
|
500
|
+
if (privateKey.length !== ED25519_PRIVATE_KEY_SIZE) throw new Error(`Private key must be ${ED25519_PRIVATE_KEY_SIZE} bytes`);
|
|
501
|
+
return __noble_curves_ed25519.ed25519.sign(message, privateKey);
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Verify an Ed25519 signature.
|
|
505
|
+
*
|
|
506
|
+
* @param publicKey - 32-byte Ed25519 public key
|
|
507
|
+
* @param message - Original message that was signed
|
|
508
|
+
* @param signature - 64-byte Ed25519 signature
|
|
509
|
+
* @returns `true` if signature is valid, `false` if signature verification fails
|
|
510
|
+
* @throws {Error} If public key is not 32 bytes or signature is not 64 bytes
|
|
511
|
+
*/
|
|
512
|
+
function ed25519Verify(publicKey, message, signature) {
|
|
513
|
+
if (publicKey.length !== ED25519_PUBLIC_KEY_SIZE) throw new Error(`Public key must be ${ED25519_PUBLIC_KEY_SIZE} bytes`);
|
|
514
|
+
if (signature.length !== ED25519_SIGNATURE_SIZE) throw new Error(`Signature must be ${ED25519_SIGNATURE_SIZE} bytes`);
|
|
515
|
+
try {
|
|
516
|
+
return __noble_curves_ed25519.ed25519.verify(signature, message, publicKey);
|
|
517
|
+
} catch {
|
|
518
|
+
return false;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
//#endregion
|
|
523
|
+
//#region src/scrypt.ts
|
|
524
|
+
/**
|
|
525
|
+
* Derive a key using Scrypt with recommended parameters.
|
|
526
|
+
* Uses N=2^15 (32768), r=8, p=1 as recommended defaults.
|
|
527
|
+
*
|
|
528
|
+
* @param password - Password or passphrase
|
|
529
|
+
* @param salt - Salt value
|
|
530
|
+
* @param outputLen - Desired output length
|
|
531
|
+
* @returns Derived key
|
|
532
|
+
*/
|
|
533
|
+
function scrypt(password, salt, outputLen) {
|
|
534
|
+
return scryptOpt(password, salt, outputLen, 15, 8, 1);
|
|
535
|
+
}
|
|
536
|
+
/**
|
|
537
|
+
* Derive a key using Scrypt with custom parameters.
|
|
538
|
+
*
|
|
539
|
+
* @param password - Password or passphrase
|
|
540
|
+
* @param salt - Salt value
|
|
541
|
+
* @param outputLen - Desired output length
|
|
542
|
+
* @param logN - Log2 of the CPU/memory cost parameter N (must be <64)
|
|
543
|
+
* @param r - Block size parameter (must be >0)
|
|
544
|
+
* @param p - Parallelization parameter (must be >0)
|
|
545
|
+
* @returns Derived key
|
|
546
|
+
*/
|
|
547
|
+
function scryptOpt(password, salt, outputLen, logN, r, p) {
|
|
548
|
+
if (logN >= 64) throw new Error("logN must be <64");
|
|
549
|
+
if (r === 0) throw new Error("r must be >0");
|
|
550
|
+
if (p === 0) throw new Error("p must be >0");
|
|
551
|
+
return (0, __noble_hashes_scrypt.scrypt)(password, salt, {
|
|
552
|
+
N: 1 << logN,
|
|
553
|
+
r,
|
|
554
|
+
p,
|
|
555
|
+
dkLen: outputLen
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
//#endregion
|
|
560
|
+
//#region src/argon.ts
|
|
561
|
+
/**
|
|
562
|
+
* Derive a key using Argon2id with default parameters.
|
|
563
|
+
*
|
|
564
|
+
* @param password - Password or passphrase
|
|
565
|
+
* @param salt - Salt value (must be at least 8 bytes)
|
|
566
|
+
* @param outputLen - Desired output length
|
|
567
|
+
* @returns Derived key
|
|
568
|
+
*/
|
|
569
|
+
function argon2idHash(password, salt, outputLen) {
|
|
570
|
+
return argon2idHashOpt(password, salt, outputLen, 3, 65536, 4);
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Derive a key using Argon2id with custom parameters.
|
|
574
|
+
*
|
|
575
|
+
* @param password - Password or passphrase
|
|
576
|
+
* @param salt - Salt value (must be at least 8 bytes)
|
|
577
|
+
* @param outputLen - Desired output length
|
|
578
|
+
* @param iterations - Number of iterations (t)
|
|
579
|
+
* @param memory - Memory in KiB (m)
|
|
580
|
+
* @param parallelism - Degree of parallelism (p)
|
|
581
|
+
* @returns Derived key
|
|
582
|
+
*/
|
|
583
|
+
function argon2idHashOpt(password, salt, outputLen, iterations, memory, parallelism) {
|
|
584
|
+
return (0, __noble_hashes_argon2.argon2id)(password, salt, {
|
|
585
|
+
t: iterations,
|
|
586
|
+
m: memory,
|
|
587
|
+
p: parallelism,
|
|
588
|
+
dkLen: outputLen
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
//#endregion
|
|
593
|
+
exports.AeadError = AeadError;
|
|
594
|
+
exports.CRC32_SIZE = CRC32_SIZE;
|
|
595
|
+
exports.CryptoError = CryptoError;
|
|
596
|
+
exports.ECDSA_MESSAGE_HASH_SIZE = ECDSA_MESSAGE_HASH_SIZE;
|
|
597
|
+
exports.ECDSA_PRIVATE_KEY_SIZE = ECDSA_PRIVATE_KEY_SIZE;
|
|
598
|
+
exports.ECDSA_PUBLIC_KEY_SIZE = ECDSA_PUBLIC_KEY_SIZE;
|
|
599
|
+
exports.ECDSA_SIGNATURE_SIZE = ECDSA_SIGNATURE_SIZE;
|
|
600
|
+
exports.ECDSA_UNCOMPRESSED_PUBLIC_KEY_SIZE = ECDSA_UNCOMPRESSED_PUBLIC_KEY_SIZE;
|
|
601
|
+
exports.ED25519_PRIVATE_KEY_SIZE = ED25519_PRIVATE_KEY_SIZE;
|
|
602
|
+
exports.ED25519_PUBLIC_KEY_SIZE = ED25519_PUBLIC_KEY_SIZE;
|
|
603
|
+
exports.ED25519_SIGNATURE_SIZE = ED25519_SIGNATURE_SIZE;
|
|
604
|
+
exports.GENERIC_PRIVATE_KEY_SIZE = GENERIC_PRIVATE_KEY_SIZE;
|
|
605
|
+
exports.GENERIC_PUBLIC_KEY_SIZE = GENERIC_PUBLIC_KEY_SIZE;
|
|
606
|
+
exports.SCHNORR_PUBLIC_KEY_SIZE = SCHNORR_PUBLIC_KEY_SIZE;
|
|
607
|
+
exports.SCHNORR_SIGNATURE_SIZE = SCHNORR_SIGNATURE_SIZE;
|
|
608
|
+
exports.SHA256_SIZE = SHA256_SIZE;
|
|
609
|
+
exports.SHA512_SIZE = SHA512_SIZE;
|
|
610
|
+
exports.SYMMETRIC_AUTH_SIZE = SYMMETRIC_AUTH_SIZE;
|
|
611
|
+
exports.SYMMETRIC_KEY_SIZE = SYMMETRIC_KEY_SIZE;
|
|
612
|
+
exports.SYMMETRIC_NONCE_SIZE = SYMMETRIC_NONCE_SIZE;
|
|
613
|
+
exports.X25519_PRIVATE_KEY_SIZE = X25519_PRIVATE_KEY_SIZE;
|
|
614
|
+
exports.X25519_PUBLIC_KEY_SIZE = X25519_PUBLIC_KEY_SIZE;
|
|
615
|
+
exports.aeadChaCha20Poly1305Decrypt = aeadChaCha20Poly1305Decrypt;
|
|
616
|
+
exports.aeadChaCha20Poly1305DecryptWithAad = aeadChaCha20Poly1305DecryptWithAad;
|
|
617
|
+
exports.aeadChaCha20Poly1305Encrypt = aeadChaCha20Poly1305Encrypt;
|
|
618
|
+
exports.aeadChaCha20Poly1305EncryptWithAad = aeadChaCha20Poly1305EncryptWithAad;
|
|
619
|
+
exports.argon2idHash = argon2idHash;
|
|
620
|
+
exports.argon2idHashOpt = argon2idHashOpt;
|
|
621
|
+
exports.crc32 = crc32;
|
|
622
|
+
exports.crc32Data = crc32Data;
|
|
623
|
+
exports.crc32DataOpt = crc32DataOpt;
|
|
624
|
+
exports.deriveAgreementPrivateKey = deriveAgreementPrivateKey;
|
|
625
|
+
exports.deriveSigningPrivateKey = deriveSigningPrivateKey;
|
|
626
|
+
exports.doubleSha256 = doubleSha256;
|
|
627
|
+
exports.ecdsaCompressPublicKey = ecdsaCompressPublicKey;
|
|
628
|
+
exports.ecdsaDecompressPublicKey = ecdsaDecompressPublicKey;
|
|
629
|
+
exports.ecdsaDerivePrivateKey = ecdsaDerivePrivateKey;
|
|
630
|
+
exports.ecdsaNewPrivateKeyUsing = ecdsaNewPrivateKeyUsing;
|
|
631
|
+
exports.ecdsaPublicKeyFromPrivateKey = ecdsaPublicKeyFromPrivateKey;
|
|
632
|
+
exports.ecdsaSign = ecdsaSign;
|
|
633
|
+
exports.ecdsaVerify = ecdsaVerify;
|
|
634
|
+
exports.ed25519NewPrivateKeyUsing = ed25519NewPrivateKeyUsing;
|
|
635
|
+
exports.ed25519PublicKeyFromPrivateKey = ed25519PublicKeyFromPrivateKey;
|
|
636
|
+
exports.ed25519Sign = ed25519Sign;
|
|
637
|
+
exports.ed25519Verify = ed25519Verify;
|
|
638
|
+
exports.hkdfHmacSha256 = hkdfHmacSha256;
|
|
639
|
+
exports.hkdfHmacSha512 = hkdfHmacSha512;
|
|
640
|
+
exports.hmacSha256 = hmacSha256;
|
|
641
|
+
exports.hmacSha512 = hmacSha512;
|
|
642
|
+
exports.memzero = memzero;
|
|
643
|
+
exports.memzeroVecVecU8 = memzeroVecVecU8;
|
|
644
|
+
exports.pbkdf2HmacSha256 = pbkdf2HmacSha256;
|
|
645
|
+
exports.pbkdf2HmacSha512 = pbkdf2HmacSha512;
|
|
646
|
+
exports.schnorrPublicKeyFromPrivateKey = schnorrPublicKeyFromPrivateKey;
|
|
647
|
+
exports.schnorrSign = schnorrSign;
|
|
648
|
+
exports.schnorrSignUsing = schnorrSignUsing;
|
|
649
|
+
exports.schnorrSignWithAuxRand = schnorrSignWithAuxRand;
|
|
650
|
+
exports.schnorrVerify = schnorrVerify;
|
|
651
|
+
exports.scrypt = scrypt;
|
|
652
|
+
exports.scryptOpt = scryptOpt;
|
|
653
|
+
exports.sha256 = sha256;
|
|
654
|
+
exports.sha512 = sha512;
|
|
655
|
+
exports.x25519NewPrivateKeyUsing = x25519NewPrivateKeyUsing;
|
|
656
|
+
exports.x25519PublicKeyFromPrivateKey = x25519PublicKeyFromPrivateKey;
|
|
657
|
+
exports.x25519SharedKey = x25519SharedKey;
|
|
658
|
+
//# sourceMappingURL=index.cjs.map
|