decrypt.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { concat, uint64be } from '../lib/buffer_utils.js';
  2. import checkIvLength from '../lib/check_iv_length.js';
  3. import checkCekLength from './check_cek_length.js';
  4. import timingSafeEqual from './timing_safe_equal.js';
  5. import { JOSENotSupported, JWEDecryptionFailed } from '../util/errors.js';
  6. import crypto, { isCryptoKey } from './webcrypto.js';
  7. import { checkEncCryptoKey } from '../lib/crypto_key.js';
  8. import invalidKeyInput from '../lib/invalid_key_input.js';
  9. import { types } from './is_key_like.js';
  10. async function cbcDecrypt(enc, cek, ciphertext, iv, tag, aad) {
  11. if (!(cek instanceof Uint8Array)) {
  12. throw new TypeError(invalidKeyInput(cek, 'Uint8Array'));
  13. }
  14. const keySize = parseInt(enc.slice(1, 4), 10);
  15. const encKey = await crypto.subtle.importKey('raw', cek.subarray(keySize >> 3), 'AES-CBC', false, ['decrypt']);
  16. const macKey = await crypto.subtle.importKey('raw', cek.subarray(0, keySize >> 3), {
  17. hash: `SHA-${keySize << 1}`,
  18. name: 'HMAC',
  19. }, false, ['sign']);
  20. const macData = concat(aad, iv, ciphertext, uint64be(aad.length << 3));
  21. const expectedTag = new Uint8Array((await crypto.subtle.sign('HMAC', macKey, macData)).slice(0, keySize >> 3));
  22. let macCheckPassed;
  23. try {
  24. macCheckPassed = timingSafeEqual(tag, expectedTag);
  25. }
  26. catch (_a) {
  27. }
  28. if (!macCheckPassed) {
  29. throw new JWEDecryptionFailed();
  30. }
  31. let plaintext;
  32. try {
  33. plaintext = new Uint8Array(await crypto.subtle.decrypt({ iv, name: 'AES-CBC' }, encKey, ciphertext));
  34. }
  35. catch (_b) {
  36. }
  37. if (!plaintext) {
  38. throw new JWEDecryptionFailed();
  39. }
  40. return plaintext;
  41. }
  42. async function gcmDecrypt(enc, cek, ciphertext, iv, tag, aad) {
  43. let encKey;
  44. if (cek instanceof Uint8Array) {
  45. encKey = await crypto.subtle.importKey('raw', cek, 'AES-GCM', false, ['decrypt']);
  46. }
  47. else {
  48. checkEncCryptoKey(cek, enc, 'decrypt');
  49. encKey = cek;
  50. }
  51. try {
  52. return new Uint8Array(await crypto.subtle.decrypt({
  53. additionalData: aad,
  54. iv,
  55. name: 'AES-GCM',
  56. tagLength: 128,
  57. }, encKey, concat(ciphertext, tag)));
  58. }
  59. catch (_a) {
  60. throw new JWEDecryptionFailed();
  61. }
  62. }
  63. const decrypt = async (enc, cek, ciphertext, iv, tag, aad) => {
  64. if (!isCryptoKey(cek) && !(cek instanceof Uint8Array)) {
  65. throw new TypeError(invalidKeyInput(cek, ...types, 'Uint8Array'));
  66. }
  67. checkIvLength(enc, iv);
  68. switch (enc) {
  69. case 'A128CBC-HS256':
  70. case 'A192CBC-HS384':
  71. case 'A256CBC-HS512':
  72. if (cek instanceof Uint8Array)
  73. checkCekLength(cek, parseInt(enc.slice(-3), 10));
  74. return cbcDecrypt(enc, cek, ciphertext, iv, tag, aad);
  75. case 'A128GCM':
  76. case 'A192GCM':
  77. case 'A256GCM':
  78. if (cek instanceof Uint8Array)
  79. checkCekLength(cek, parseInt(enc.slice(1, 4), 10));
  80. return gcmDecrypt(enc, cek, ciphertext, iv, tag, aad);
  81. default:
  82. throw new JOSENotSupported('Unsupported JWE Content Encryption Algorithm');
  83. }
  84. };
  85. export default decrypt;