UNPKG

@unvision/jose

Version:

Implementation of the RFCs of the JOSE Working Group.

79 lines 3.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.RSA_OAEP_512 = exports.RSA_OAEP_384 = exports.RSA_OAEP_256 = exports.RSA_OAEP = exports.RSA1_5 = void 0; const crypto_1 = require("crypto"); const invalid_jsonwebkey_exception_1 = require("../../../exceptions/invalid-jsonwebkey.exception"); const jsonwebencryption_key_wrap_algorithm_1 = require("./jsonwebencryption-key-wrap.algorithm"); /** * Implementation of the JSON Web Encryption RSA Key Wrap Algorithm. * * @see https://www.rfc-editor.org/rfc/rfc7518.html#section-4.2 * @see https://www.rfc-editor.org/rfc/rfc7518.html#section-4.3 */ class RsaAlgorithm extends jsonwebencryption_key_wrap_algorithm_1.JsonWebEncryptionKeyWrapAlgorithm { /** * Instantiates a new JSON Web Encryption RSA Key Wrap Algorithm to Wrap and Unwrap Content Encryption Keys. * * @param algorithm Name of the JSON Web Encryption Key Wrap Algorithm. * @param padding RSA Encryption Padding used by the JSON Web Encryption Key Wrap Algorithm. * @param hash Name of the Hash Algorithm. */ constructor(algorithm, padding, hash) { super(algorithm, 'RSA'); this.padding = padding; this.hash = hash; } /** * Wraps the provided Content Encryption Key using the provide JSON Web Key. * * @param enc JSON Web Encryption Content Encryption Algorithm. * @param key JSON Web Key used to Wrap the provided Content Encryption Key. * @returns Generated Content Encryption Key, Wrapped Content Encryption Key and optional JSON Web Encryption Header. */ async wrap(enc, key) { this.validateJsonWebKey(key); const cryptoKey = key['cryptoKey']; const cek = await enc.generateContentEncryptionKey(); const ek = (0, crypto_1.publicEncrypt)({ key: cryptoKey, oaepHash: this.hash, padding: this.padding }, cek); return [cek, ek]; } /** * Unwraps the provided Encrypted Key using the provided JSON Web Key. * * @param enc JSON Web Encrytpion Content Encryption Algorithm. * @param key JSON Web Key used to Unwrap the Wrapped Content Encryption Key. * @param ek Wrapped Content Encryption Key. * @returns Unwrapped Content Encryption Key. */ async unwrap(enc, key, ek) { this.validateJsonWebKey(key); const cryptoKey = key['cryptoKey']; if (cryptoKey.type !== 'private') { throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('An RSA Private JSON Web Key is needed to Unwrap the provided Wrapped Content Encryption Key.'); } const cek = (0, crypto_1.privateDecrypt)({ key: cryptoKey, oaepHash: this.hash, padding: this.padding }, ek); enc.validateContentEncryptionKey(cek); return cek; } } /** * RSAES-PKCS1-v1_5. */ exports.RSA1_5 = new RsaAlgorithm('RSA1_5', crypto_1.constants.RSA_PKCS1_PADDING); /** * RSAES OAEP using default parameters. */ exports.RSA_OAEP = new RsaAlgorithm('RSA-OAEP', crypto_1.constants.RSA_PKCS1_OAEP_PADDING, 'SHA1'); /** * RSAES OAEP using SHA-256 and MGF1 with SHA-256. */ exports.RSA_OAEP_256 = new RsaAlgorithm('RSA-OAEP-256', crypto_1.constants.RSA_PKCS1_OAEP_PADDING, 'SHA256'); /** * RSAES OAEP using SHA-384 and MGF1 with SHA-384. */ exports.RSA_OAEP_384 = new RsaAlgorithm('RSA-OAEP-384', crypto_1.constants.RSA_PKCS1_OAEP_PADDING, 'SHA384'); /** * RSAES OAEP using SHA-512 and MGF1 with SHA-512. */ exports.RSA_OAEP_512 = new RsaAlgorithm('RSA-OAEP-512', crypto_1.constants.RSA_PKCS1_OAEP_PADDING, 'SHA512'); //# sourceMappingURL=rsa.algorithm.js.map