UNPKG

@simbachain/cose-ts-secp256k1

Version:

Implemented Keys, Algorithms (RFC9053), COSE (RFC9052) and CWT (RFC8392) in TypeScript.

50 lines 1.89 kB
// (c) 2023-present, LDC Labs. All rights reserved. // See the file LICENSE for licensing terms. import { chacha20poly1305 } from '@noble/ciphers/chacha'; import * as iana from './iana'; import { assertBytes } from './map'; import { Key } from './key'; import { randomBytes, decodeCBOR } from './utils'; // TODO: more checks // ChaCha20Poly1305Key implements content encryption algorithm ChaCha20/Poly1305 for COSE as defined in RFC9053. // https://datatracker.ietf.org/doc/html/rfc9053#name-chacha20-and-poly1305. export class ChaCha20Poly1305Key extends Key { static fromBytes(data) { return new ChaCha20Poly1305Key(decodeCBOR(data)); } static generate(kid) { return ChaCha20Poly1305Key.fromSecret(randomBytes(32), kid); } static fromSecret(secret, kid) { assertBytes(secret, 'secret'); if (secret.length !== 32) { throw new Error(`cose-ts: ChaCha20Poly1305Key.fromSecret: secret size mismatch, expected 32, got ${secret.length}`); } const key = new ChaCha20Poly1305Key(); if (kid != null) { key.setKid(kid); } key.setParam(iana.SymmetricKeyParameterK, secret); return key; } constructor(kv) { super(kv); this.kty = iana.KeyTypeSymmetric; this.alg = iana.AlgorithmChaCha20Poly1305; } nonceSize() { return 12; } getSecretKey() { return this.getBytes(iana.SymmetricKeyParameterK, 'k'); } async encrypt(plaintext, nonce, aad) { const cipher = chacha20poly1305(this.getSecretKey(), nonce, aad); return Promise.resolve(cipher.encrypt(plaintext)); } async decrypt(ciphertext, nonce, aad) { const cipher = chacha20poly1305(this.getSecretKey(), nonce, aad); return Promise.resolve(cipher.decrypt(ciphertext)); } } //# sourceMappingURL=chacha20poly1305.js.map