UNPKG

@credo-ts/core

Version:

<p align="center"> <br /> <img alt="Credo Logo" src="https://github.com/openwallet-foundation/credo-ts/blob/c7886cb8377ceb8ee4efe8d264211e561a75072d/images/credo-logo.png" height="250px" /> </p> <h1 align="center"><b>Credo - Core</b></h1

114 lines (113 loc) • 4.84 kB
import { KnownJwaContentEncryptionAlgorithms, isJwaHpkeAlgorithm } from "../jwk/jwa.mjs"; import { zAnyUint8Array } from "../../../utils/zod.mjs"; import { zKmsJwkPrivateOct } from "../jwk/kty/oct/octJwk.mjs"; import { zKmsKeyId } from "./common.mjs"; import { zKmsKeyAgreementDecryptOptions } from "./KmsKeyAgreementDecryptOptions.mjs"; import { z as z$1 } from "zod"; //#region src/modules/kms/options/KmsDecryptOptions.ts const zKmsDecryptDataDecryptionAesGcm = z$1.object({ algorithm: z$1.enum([ KnownJwaContentEncryptionAlgorithms.A128GCM, KnownJwaContentEncryptionAlgorithms.A192GCM, KnownJwaContentEncryptionAlgorithms.A256GCM ]), iv: zAnyUint8Array.refine((iv) => iv.length === 12, "iv must be 12 bytes for AES GCM"), aad: z$1.optional(zAnyUint8Array), tag: zAnyUint8Array }); const zKmsDecryptDataDecryptionAesCbc = z$1.object({ algorithm: z$1.enum([KnownJwaContentEncryptionAlgorithms.A128CBC, KnownJwaContentEncryptionAlgorithms.A256CBC]), iv: zAnyUint8Array.refine((iv) => iv.length === 16, "iv must be 16 bytes for AES CBC") }); const zKmsDecryptDataDecryptionAesCbcHmac = z$1.object({ algorithm: z$1.enum([ KnownJwaContentEncryptionAlgorithms.A128CBC_HS256, KnownJwaContentEncryptionAlgorithms.A192CBC_HS384, KnownJwaContentEncryptionAlgorithms.A256CBC_HS512 ]), iv: zAnyUint8Array.refine((iv) => iv.length === 16, "iv must be 16 bytes for AES CBC with HMAC"), aad: z$1.optional(zAnyUint8Array), tag: zAnyUint8Array }); const zKmsDecryptDataDecryptionSalsa = z$1.object({ algorithm: z$1.enum([KnownJwaContentEncryptionAlgorithms["XSALSA20-POLY1305"]]), iv: zAnyUint8Array.optional() }); const zKmsDecryptDataDecryptionC20p = z$1.object({ algorithm: z$1.enum([KnownJwaContentEncryptionAlgorithms.C20P, KnownJwaContentEncryptionAlgorithms.XC20P]), iv: zAnyUint8Array, aad: z$1.optional(zAnyUint8Array), tag: zAnyUint8Array }); /** * Integrated-encryption HPKE. The HPKE suite (see the `HPKE-*` key agreement algorithms) fixes the * AEAD, and both the content encryption key and the nonce are derived from the HPKE key schedule. * The tag is part of the HPKE ciphertext, so the only parameter left to the caller is the aad. */ const zKmsDecryptDataDecryptionHpke = z$1.object({ algorithm: z$1.literal("HPKE"), aad: z$1.optional(zAnyUint8Array) }); const zKmsDecryptDataDecryption = z$1.discriminatedUnion("algorithm", [ zKmsDecryptDataDecryptionAesCbc, zKmsDecryptDataDecryptionAesCbcHmac, zKmsDecryptDataDecryptionAesGcm, zKmsDecryptDataDecryptionC20p, zKmsDecryptDataDecryptionSalsa, zKmsDecryptDataDecryptionHpke ]); const zKmsDecryptOptions = z$1.object({ /** * The key to use for decrypting. There are three possible formats: * - a key id, pointing to a symmetric (oct) jwk that can be used directly for decryption * - a private symmetric (oct) jwk object that can be used directly for decryption * - an object configuring key agreement, based on an existing asymmetric key */ key: z$1.union([ z$1.object({ keyId: zKmsKeyId, privateJwk: z$1.never().optional(), keyAgreement: z$1.never().optional() }), z$1.object({ privateJwk: zKmsJwkPrivateOct.describe("A private oct (symmetric) jwk"), keyId: z$1.never().optional(), keyAgreement: z$1.never().optional() }), z$1.object({ keyAgreement: zKmsKeyAgreementDecryptOptions, keyId: z$1.never().optional(), privateJwk: z$1.never().optional() }) ]), /** * The decryption algorithm used to decrypt the data/content. * In JWE this parameter is referred to as "enc". * * Must be 'HPKE' for the integrated-encryption HPKE algorithms, where the suite fixes the AEAD * and only the aad is left to the caller. */ decryption: zKmsDecryptDataDecryption.describe("Options related to the decryption algorithm to use for decrypting the data"), /** * The encrypted data to decrypt */ encrypted: zAnyUint8Array.describe("The encrypted data to decrypt") }).check(({ value, issues }) => { const usesIntegratedEncryption = value.key.keyAgreement && isJwaHpkeAlgorithm(value.key.keyAgreement.algorithm); const usesHpkeContentDecryption = value.decryption.algorithm === "HPKE"; if (usesIntegratedEncryption && !usesHpkeContentDecryption) issues.push({ code: "custom", input: value.decryption, path: ["decryption", "algorithm"], message: `'decryption.algorithm' must be 'HPKE' for key agreement algorithm '${value.key.keyAgreement?.algorithm}', as the HPKE suite defines the content encryption algorithm` }); else if (!usesIntegratedEncryption && usesHpkeContentDecryption) issues.push({ code: "custom", input: value.decryption, path: ["decryption", "algorithm"], message: `'decryption.algorithm' 'HPKE' can only be used with an integrated-encryption HPKE key agreement algorithm` }); }); //#endregion export { zKmsDecryptOptions }; //# sourceMappingURL=KmsDecryptOptions.mjs.map