feat: Shade E2EE library — M1-M3 complete
Signal Protocol implementation with full X3DH + Double Ratchet: - M1: Core types, CryptoProvider interface, KDF chain functions, SubtleCrypto+noble/curves provider, MemoryStorage - M2: X3DH key agreement (identity keys, signed prekeys, one-time prekeys, bundle processing for both initiator and responder) - M3: Double Ratchet (symmetric-key ratchet, DH ratchet, skipped message key cache, out-of-order delivery, AAD-bound headers) 68 tests, 0 failures — including full integration test of X3DH handshake → Double Ratchet conversation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
62
packages/shade-core/src/crypto.ts
Normal file
62
packages/shade-core/src/crypto.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* CryptoProvider — platform-agnostic interface for all cryptographic primitives.
|
||||
*
|
||||
* Implementations:
|
||||
* - @shade/crypto-web: SubtleCrypto (Bun, Node.js, browser)
|
||||
* - shade-android: Google Tink (Kotlin/Android)
|
||||
*/
|
||||
export interface CryptoProvider {
|
||||
// ─── X25519 (Diffie-Hellman) ───────────────────────────────
|
||||
|
||||
/** Generate an X25519 keypair (32-byte public + 32-byte private) */
|
||||
generateX25519KeyPair(): Promise<{ publicKey: Uint8Array; privateKey: Uint8Array }>;
|
||||
|
||||
/** Perform X25519 Diffie-Hellman: returns 32-byte shared secret */
|
||||
x25519(privateKey: Uint8Array, publicKey: Uint8Array): Promise<Uint8Array>;
|
||||
|
||||
// ─── Ed25519 (Signing) ─────────────────────────────────────
|
||||
|
||||
/** Generate an Ed25519 keypair (32-byte public + 32-byte private) */
|
||||
generateEd25519KeyPair(): Promise<{ publicKey: Uint8Array; privateKey: Uint8Array }>;
|
||||
|
||||
/** Sign a message with Ed25519. Returns a 64-byte signature. */
|
||||
sign(privateKey: Uint8Array, message: Uint8Array): Promise<Uint8Array>;
|
||||
|
||||
/** Verify an Ed25519 signature. Returns true if valid. */
|
||||
verify(publicKey: Uint8Array, message: Uint8Array, signature: Uint8Array): Promise<boolean>;
|
||||
|
||||
// ─── AES-256-GCM (Symmetric Encryption) ────────────────────
|
||||
|
||||
/** Encrypt plaintext with AES-256-GCM. Generates a random 12-byte nonce. */
|
||||
aesGcmEncrypt(
|
||||
key: Uint8Array,
|
||||
plaintext: Uint8Array,
|
||||
aad?: Uint8Array,
|
||||
): Promise<{ ciphertext: Uint8Array; nonce: Uint8Array }>;
|
||||
|
||||
/** Decrypt ciphertext with AES-256-GCM. Throws on authentication failure. */
|
||||
aesGcmDecrypt(
|
||||
key: Uint8Array,
|
||||
ciphertext: Uint8Array,
|
||||
nonce: Uint8Array,
|
||||
aad?: Uint8Array,
|
||||
): Promise<Uint8Array>;
|
||||
|
||||
// ─── Key Derivation ────────────────────────────────────────
|
||||
|
||||
/** HKDF-SHA256: derive `length` bytes from input keying material */
|
||||
hkdf(
|
||||
ikm: Uint8Array,
|
||||
salt: Uint8Array,
|
||||
info: Uint8Array,
|
||||
length: number,
|
||||
): Promise<Uint8Array>;
|
||||
|
||||
/** HMAC-SHA256: returns 32-byte MAC */
|
||||
hmacSha256(key: Uint8Array, data: Uint8Array): Promise<Uint8Array>;
|
||||
|
||||
// ─── Random ────────────────────────────────────────────────
|
||||
|
||||
/** Generate cryptographically secure random bytes */
|
||||
randomBytes(length: number): Uint8Array;
|
||||
}
|
||||
Reference in New Issue
Block a user