@webbuf/acb3p256dh
v3.8.0
Published
AES+CBC encryption/decryption with blake3 mac and P-256 (NIST) Diffie-Hellman shared secret for web, node.js, deno and bun.
Downloads
59
Maintainers
Readme
@webbuf/acb3p256dh
Authenticated encryption with P-256 ECDH key exchange.
ACB3P256DH = AES + CBC + Blake3 MAC + P-256 Diffie-Hellman
Uses P-256 (NIST) ECDH to derive a shared secret, then encrypts with ACB3.
Installation
npm install @webbuf/acb3p256dhUsage
import { acb3p256dhEncrypt, acb3p256dhDecrypt } from "@webbuf/acb3p256dh";
import { p256PublicKeyCreate } from "@webbuf/p256";
import { WebBuf } from "@webbuf/webbuf";
import { FixedBuf } from "@webbuf/fixedbuf";
// Alice and Bob generate key pairs
const alicePrivKey = FixedBuf.fromRandom<32>(32);
const alicePubKey = p256PublicKeyCreate(alicePrivKey);
const bobPrivKey = FixedBuf.fromRandom<32>(32);
const bobPubKey = p256PublicKeyCreate(bobPrivKey);
// Alice encrypts a message to Bob
const plaintext = WebBuf.fromUtf8("Hello Bob!");
const ciphertext = acb3p256dhEncrypt(alicePrivKey, bobPubKey, plaintext);
// Bob decrypts the message from Alice
const decrypted = acb3p256dhDecrypt(bobPrivKey, alicePubKey, ciphertext);
console.log(decrypted.toUtf8()); // "Hello Bob!"How It Works
- Derives shared secret using P-256 ECDH:
p256SharedSecret(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:
p256SharedSecret(alicePriv, bobPub) - Bob:
p256SharedSecret(bobPriv, alicePub)
API
| Function | Description |
| ----------------------------------------------------- | ----------------------------- |
| acb3p256dhEncrypt(privKey, pubKey, plaintext, iv?) | Encrypt with ECDH-derived key |
| acb3p256dhDecrypt(privKey, pubKey, ciphertext) | Decrypt with ECDH-derived key |
License
MIT
