|
| 1 | +/** |
| 2 | + * AES Key Wrap/Unwrap defined in RFC 3394 |
| 3 | + * |
| 4 | + * @author aosterhage [[email protected]] |
| 5 | + * @copyright Crown Copyright 2025 |
| 6 | + * @license Apache-2.0 |
| 7 | + */ |
| 8 | + |
| 9 | +import Utils from "../Utils.mjs"; |
| 10 | +import forge from "node-forge"; |
| 11 | + |
| 12 | +/** |
| 13 | + * AES Key Wrap algorithm defined in RFC 3394. |
| 14 | + * |
| 15 | + * @param {string} plaintext |
| 16 | + * @param {string} kek |
| 17 | + * @param {string} iv |
| 18 | + * @returns {string} ciphertext |
| 19 | + */ |
| 20 | +export function aesKeyWrap(plaintext, kek, iv) { |
| 21 | + const cipher = forge.cipher.createCipher("AES-ECB", kek); |
| 22 | + |
| 23 | + let A = iv; |
| 24 | + const R = []; |
| 25 | + for (let i = 0; i < plaintext.length; i += 8) { |
| 26 | + R.push(plaintext.substring(i, i + 8)); |
| 27 | + } |
| 28 | + let cntLower = 1, cntUpper = 0; |
| 29 | + for (let j = 0; j < 6; j++) { |
| 30 | + for (let i = 0; i < R.length; i++) { |
| 31 | + cipher.start(); |
| 32 | + cipher.update(forge.util.createBuffer(A + R[i])); |
| 33 | + cipher.finish(); |
| 34 | + const B = cipher.output.getBytes(); |
| 35 | + const msbBuffer = Utils.strToArrayBuffer(B.substring(0, 8)); |
| 36 | + const msbView = new DataView(msbBuffer); |
| 37 | + msbView.setUint32(0, msbView.getUint32(0) ^ cntUpper); |
| 38 | + msbView.setUint32(4, msbView.getUint32(4) ^ cntLower); |
| 39 | + A = Utils.arrayBufferToStr(msbBuffer, false); |
| 40 | + R[i] = B.substring(8, 16); |
| 41 | + cntLower++; |
| 42 | + if (cntLower > 0xffffffff) { |
| 43 | + cntUpper++; |
| 44 | + cntLower = 0; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + return A + R.join(""); |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * AES Key Unwrap algorithm defined in RFC 3394. |
| 54 | + * |
| 55 | + * @param {string} ciphertext |
| 56 | + * @param {string} kek |
| 57 | + * @returns {[string, string]} [plaintext, iv] |
| 58 | + */ |
| 59 | +export function aesKeyUnwrap(ciphertext, kek) { |
| 60 | + const cipher = forge.cipher.createCipher("AES-ECB", kek); |
| 61 | + cipher.start(); |
| 62 | + cipher.update(forge.util.createBuffer("")); |
| 63 | + cipher.finish(); |
| 64 | + const paddingBlock = cipher.output.getBytes(); |
| 65 | + |
| 66 | + const decipher = forge.cipher.createDecipher("AES-ECB", kek); |
| 67 | + |
| 68 | + let A = ciphertext.substring(0, 8); |
| 69 | + const R = []; |
| 70 | + for (let i = 8; i < ciphertext.length; i += 8) { |
| 71 | + R.push(ciphertext.substring(i, i + 8)); |
| 72 | + } |
| 73 | + let cntLower = R.length >>> 0; |
| 74 | + let cntUpper = (R.length / ((1 << 30) * 4)) >>> 0; |
| 75 | + cntUpper = cntUpper * 6 + ((cntLower * 6 / ((1 << 30) * 4)) >>> 0); |
| 76 | + cntLower = cntLower * 6 >>> 0; |
| 77 | + for (let j = 5; j >= 0; j--) { |
| 78 | + for (let i = R.length - 1; i >= 0; i--) { |
| 79 | + const aBuffer = Utils.strToArrayBuffer(A); |
| 80 | + const aView = new DataView(aBuffer); |
| 81 | + aView.setUint32(0, aView.getUint32(0) ^ cntUpper); |
| 82 | + aView.setUint32(4, aView.getUint32(4) ^ cntLower); |
| 83 | + A = Utils.arrayBufferToStr(aBuffer, false); |
| 84 | + decipher.start(); |
| 85 | + decipher.update(forge.util.createBuffer(A + R[i] + paddingBlock)); |
| 86 | + decipher.finish(); |
| 87 | + const B = decipher.output.getBytes(); |
| 88 | + A = B.substring(0, 8); |
| 89 | + R[i] = B.substring(8, 16); |
| 90 | + cntLower--; |
| 91 | + if (cntLower < 0) { |
| 92 | + cntUpper--; |
| 93 | + cntLower = 0xffffffff; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + return [R.join(""), A]; |
| 99 | +} |
0 commit comments