UNPKG

ts-mls

Version:

[![CI](https://github.com/LukaJCB/ts-mls/actions/workflows/ci.yml/badge.svg)](https://github.com/LukaJCB/ts-mls/actions/workflows/ci.yml) [![npm version](https://badge.fury.io/js/ts-mls.svg)](https://badge.fury.io/js/ts-mls) [![Coverage Status](https://co

92 lines 4.03 kB
import { concatUint8Arrays, bytesToArrayBuffer } from "../../util/byteArray.js"; import { CryptoError } from "../../mlsError.js"; export async function makeGenericHpke(hpkealg, aead, cs) { return { async open(privateKey, kemOutput, ciphertext, info, aad) { try { const result = await cs.open({ recipientKey: privateKey, enc: bytesToArrayBuffer(kemOutput), info: bytesToArrayBuffer(info) }, bytesToArrayBuffer(ciphertext), aad ? bytesToArrayBuffer(aad) : new ArrayBuffer()); return new Uint8Array(result); } catch (e) { throw new CryptoError(`${e}`); } }, async seal(publicKey, plaintext, info, aad) { const result = await cs.seal({ recipientPublicKey: publicKey, info: bytesToArrayBuffer(info) }, bytesToArrayBuffer(plaintext), aad ? bytesToArrayBuffer(aad) : new ArrayBuffer()); return { ct: new Uint8Array(result.ct), enc: new Uint8Array(result.enc), }; }, async exportSecret(publicKey, exporterContext, length, info) { const context = await cs.createSenderContext({ recipientPublicKey: publicKey, info: bytesToArrayBuffer(info) }); return { enc: new Uint8Array(context.enc), secret: new Uint8Array(await context.export(bytesToArrayBuffer(exporterContext), length)), }; }, async importSecret(privateKey, exporterContext, kemOutput, length, info) { try { const context = await cs.createRecipientContext({ recipientKey: privateKey, info: bytesToArrayBuffer(info), enc: bytesToArrayBuffer(kemOutput), }); return new Uint8Array(await context.export(bytesToArrayBuffer(exporterContext), length)); } catch (e) { throw new CryptoError(`${e}`); } }, async importPrivateKey(k) { try { // See https://github.com/mlswg/mls-implementations/issues/176#issuecomment-1817043142 const key = hpkealg.kem === "DHKEM-P521-HKDF-SHA512" ? prepadPrivateKeyP521(k) : k; return (await cs.kem.deserializePrivateKey(bytesToArrayBuffer(key))); } catch (e) { throw new CryptoError(`${e}`); } }, async importPublicKey(k) { try { return (await cs.kem.deserializePublicKey(bytesToArrayBuffer(k))); } catch (e) { throw new CryptoError(`${e}`); } }, async exportPublicKey(k) { return new Uint8Array(await cs.kem.serializePublicKey(k)); }, async exportPrivateKey(k) { return new Uint8Array(await cs.kem.serializePrivateKey(k)); }, async encryptAead(key, nonce, aad, plaintext) { return aead.encrypt(key, nonce, aad ? aad : new Uint8Array(), plaintext); }, async decryptAead(key, nonce, aad, ciphertext) { try { return await aead.decrypt(key, nonce, aad ? aad : new Uint8Array(), ciphertext); } catch (e) { throw new CryptoError(`${e}`); } }, async deriveKeyPair(ikm) { const kp = await cs.kem.deriveKeyPair(bytesToArrayBuffer(ikm)); return { privateKey: kp.privateKey, publicKey: kp.publicKey }; }, async generateKeyPair() { const kp = await cs.kem.generateKeyPair(); return { privateKey: kp.privateKey, publicKey: kp.publicKey }; }, keyLength: cs.aead.keySize, nonceLength: cs.aead.nonceSize, }; } function prepadPrivateKeyP521(k) { const lengthDifference = 66 - k.byteLength; return concatUint8Arrays(new Uint8Array(lengthDifference), k); } //# sourceMappingURL=hpke.js.map