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

42 lines 1.59 kB
import { utf8ToBytes } from "@noble/ciphers/utils"; import { HkdfSha256, HkdfSha384, HkdfSha512 } from "@hpke/core"; import { encodeVarLenData } from "../codec/variableLength"; import { encodeUint16, encodeUint32 } from "../codec/number"; import { bytesToBuffer } from "../util/byteArray"; export function makeKdfImpl(k) { return { async extract(salt, ikm) { const result = await k.extract(bytesToBuffer(salt), bytesToBuffer(ikm)); return new Uint8Array(result); }, async expand(prk, info, len) { const result = await k.expand(bytesToBuffer(prk), bytesToBuffer(info), len); return new Uint8Array(result); }, size: k.hashSize, }; } export function makeKdf(kdfAlg) { switch (kdfAlg) { case "HKDF-SHA256": return new HkdfSha256(); case "HKDF-SHA384": return new HkdfSha384(); case "HKDF-SHA512": return new HkdfSha512(); } } export function expandWithLabel(secret, label, context, length, kdf) { return kdf.expand(secret, new Uint8Array([ ...encodeUint16(length), ...encodeVarLenData(utf8ToBytes(`MLS 1.0 ${label}`)), ...encodeVarLenData(context), ]), length); } export async function deriveSecret(secret, label, kdf) { return expandWithLabel(secret, label, new Uint8Array(), kdf.size, kdf); } export async function deriveTreeSecret(secret, label, generation, length, kdf) { return expandWithLabel(secret, label, encodeUint32(generation), length, kdf); } //# sourceMappingURL=kdf.js.map