@unvision/jose
Version:
Implementation of the RFCs of the JOSE Working Group.
80 lines • 3.45 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.A256KW = exports.A192KW = exports.A128KW = 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 AES Key Wrap Algorithm.
*
* @see https://www.rfc-editor.org/rfc/rfc7518.html#section-4.4
*/
class AesAlgorithm extends jsonwebencryption_key_wrap_algorithm_1.JsonWebEncryptionKeyWrapAlgorithm {
/**
* Instantiates a new JSON Web Encryption AES Key Wrap Algorithm to Wrap and Unwrap Content Encryption Keys.
*
* @param algorithm Name of the JSON Web Encryption Key Wrap Algorithm.
*/
constructor(algorithm) {
super(algorithm, 'oct');
this.keySize = Number.parseInt(this.algorithm.substring(1, 4));
this.cipher = `aes${this.keySize}-wrap`;
}
/**
* 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 cipher = (0, crypto_1.createCipheriv)(this.cipher, cryptoKey, Buffer.alloc(8, 0xa6));
const cek = await enc.generateContentEncryptionKey();
const ek = Buffer.concat([cipher.update(cek), cipher.final()]);
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'];
const decipher = (0, crypto_1.createDecipheriv)(this.cipher, cryptoKey, Buffer.alloc(8, 0xa6));
const cek = Buffer.concat([decipher.update(ek), decipher.final()]);
enc.validateContentEncryptionKey(cek);
return cek;
}
/**
* Checks if the provided JSON Web Key can be used by the requesting JSON Web Encryption AES Key Wrap Algorithm.
*
* @param key JSON Web Key to be checked.
* @throws {InvalidJsonWebKeyException} The provided JSON Web Key is invalid.
*/
validateJsonWebKey(key) {
super.validateJsonWebKey(key);
const exportedKey = key['cryptoKey'].export();
if (exportedKey.length * 8 !== this.keySize) {
throw new invalid_jsonwebkey_exception_1.InvalidJsonWebKeyException('Invalid JSON Web Key Secret Size.');
}
}
}
/**
* AES Key Wrap with default initial value using 128-bit key.
*/
exports.A128KW = new AesAlgorithm('A128KW');
/**
* AES Key Wrap with default initial value using 192-bit key.
*/
exports.A192KW = new AesAlgorithm('A192KW');
/**
* AES Key Wrap with default initial value using 256-bit key.
*/
exports.A256KW = new AesAlgorithm('A256KW');
//# sourceMappingURL=aes.algorithm.js.map