@webbuf/acb3dh
v3.5.0
Published
AES+CBC encryption/decryption with blake3 mac and secp256k1 Diffie-Hellman shared secret for web, node.js, deno and bun.
Maintainers
Readme
@webbuf/acb3dh
Authenticated encryption with ECDH key exchange.
ACB3DH = AES + CBC + Blake3 MAC + Diffie-Hellman
Uses secp256k1 ECDH to derive a shared secret, then encrypts with ACB3.
Installation
npm install @webbuf/acb3dhUsage
import { acb3dhEncrypt, acb3dhDecrypt } from "@webbuf/acb3dh";
import { publicKeyCreate } from "@webbuf/secp256k1";
import { WebBuf } from "@webbuf/webbuf";
import { FixedBuf } from "@webbuf/fixedbuf";
// Alice and Bob generate key pairs
const alicePrivKey = FixedBuf.fromRandom<32>(32);
const alicePubKey = publicKeyCreate(alicePrivKey);
const bobPrivKey = FixedBuf.fromRandom<32>(32);
const bobPubKey = publicKeyCreate(bobPrivKey);
// Alice encrypts a message to Bob
const plaintext = WebBuf.fromUtf8("Hello Bob!");
const ciphertext = acb3dhEncrypt(alicePrivKey, bobPubKey, plaintext);
// Bob decrypts the message from Alice
const decrypted = acb3dhDecrypt(bobPrivKey, alicePubKey, ciphertext);
console.log(decrypted.toUtf8()); // "Hello Bob!"How It Works
- Derives shared secret using ECDH:
sharedSecret(privKey, pubKey) - Hashes the shared secret with BLAKE3 to get the encryption key
- Encrypts/decrypts using ACB3 (AES-CBC + BLAKE3 MAC)
Both parties can derive the same shared secret:
- Alice:
sharedSecret(alicePriv, bobPub) - Bob:
sharedSecret(bobPriv, alicePub)
API
| Function | Description |
| ------------------------------------------------ | ----------------------------- |
| acb3dhEncrypt(privKey, pubKey, plaintext, iv?) | Encrypt with ECDH-derived key |
| acb3dhDecrypt(privKey, pubKey, ciphertext) | Decrypt with ECDH-derived key |
License
MIT
