Home Reference Source

src/crypt/aes-decryptor.ts

  1. import { sliceUint8 } from '../utils/typed-array';
  2.  
  3. // PKCS7
  4. export function removePadding(array: Uint8Array): Uint8Array {
  5. const outputBytes = array.byteLength;
  6. const paddingBytes =
  7. outputBytes && new DataView(array.buffer).getUint8(outputBytes - 1);
  8. if (paddingBytes) {
  9. return sliceUint8(array, 0, outputBytes - paddingBytes);
  10. }
  11. return array;
  12. }
  13.  
  14. export default class AESDecryptor {
  15. private rcon: Array<number> = [
  16. 0x0,
  17. 0x1,
  18. 0x2,
  19. 0x4,
  20. 0x8,
  21. 0x10,
  22. 0x20,
  23. 0x40,
  24. 0x80,
  25. 0x1b,
  26. 0x36,
  27. ];
  28. private subMix: Array<Uint32Array> = [
  29. new Uint32Array(256),
  30. new Uint32Array(256),
  31. new Uint32Array(256),
  32. new Uint32Array(256),
  33. ];
  34. private invSubMix: Array<Uint32Array> = [
  35. new Uint32Array(256),
  36. new Uint32Array(256),
  37. new Uint32Array(256),
  38. new Uint32Array(256),
  39. ];
  40. private sBox: Uint32Array = new Uint32Array(256);
  41. private invSBox: Uint32Array = new Uint32Array(256);
  42. private key: Uint32Array = new Uint32Array(0);
  43.  
  44. private ksRows: number = 0;
  45. private keySize: number = 0;
  46. private keySchedule!: Uint32Array;
  47. private invKeySchedule!: Uint32Array;
  48.  
  49. constructor() {
  50. this.initTable();
  51. }
  52.  
  53. // Using view.getUint32() also swaps the byte order.
  54. uint8ArrayToUint32Array_(arrayBuffer) {
  55. const view = new DataView(arrayBuffer);
  56. const newArray = new Uint32Array(4);
  57. for (let i = 0; i < 4; i++) {
  58. newArray[i] = view.getUint32(i * 4);
  59. }
  60.  
  61. return newArray;
  62. }
  63.  
  64. initTable() {
  65. const sBox = this.sBox;
  66. const invSBox = this.invSBox;
  67. const subMix = this.subMix;
  68. const subMix0 = subMix[0];
  69. const subMix1 = subMix[1];
  70. const subMix2 = subMix[2];
  71. const subMix3 = subMix[3];
  72. const invSubMix = this.invSubMix;
  73. const invSubMix0 = invSubMix[0];
  74. const invSubMix1 = invSubMix[1];
  75. const invSubMix2 = invSubMix[2];
  76. const invSubMix3 = invSubMix[3];
  77.  
  78. const d = new Uint32Array(256);
  79. let x = 0;
  80. let xi = 0;
  81. let i = 0;
  82. for (i = 0; i < 256; i++) {
  83. if (i < 128) {
  84. d[i] = i << 1;
  85. } else {
  86. d[i] = (i << 1) ^ 0x11b;
  87. }
  88. }
  89.  
  90. for (i = 0; i < 256; i++) {
  91. let sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4);
  92. sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63;
  93. sBox[x] = sx;
  94. invSBox[sx] = x;
  95.  
  96. // Compute multiplication
  97. const x2 = d[x];
  98. const x4 = d[x2];
  99. const x8 = d[x4];
  100.  
  101. // Compute sub/invSub bytes, mix columns tables
  102. let t = (d[sx] * 0x101) ^ (sx * 0x1010100);
  103. subMix0[x] = (t << 24) | (t >>> 8);
  104. subMix1[x] = (t << 16) | (t >>> 16);
  105. subMix2[x] = (t << 8) | (t >>> 24);
  106. subMix3[x] = t;
  107.  
  108. // Compute inv sub bytes, inv mix columns tables
  109. t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100);
  110. invSubMix0[sx] = (t << 24) | (t >>> 8);
  111. invSubMix1[sx] = (t << 16) | (t >>> 16);
  112. invSubMix2[sx] = (t << 8) | (t >>> 24);
  113. invSubMix3[sx] = t;
  114.  
  115. // Compute next counter
  116. if (!x) {
  117. x = xi = 1;
  118. } else {
  119. x = x2 ^ d[d[d[x8 ^ x2]]];
  120. xi ^= d[d[xi]];
  121. }
  122. }
  123. }
  124.  
  125. expandKey(keyBuffer: ArrayBuffer) {
  126. // convert keyBuffer to Uint32Array
  127. const key = this.uint8ArrayToUint32Array_(keyBuffer);
  128. let sameKey = true;
  129. let offset = 0;
  130.  
  131. while (offset < key.length && sameKey) {
  132. sameKey = key[offset] === this.key[offset];
  133. offset++;
  134. }
  135.  
  136. if (sameKey) {
  137. return;
  138. }
  139.  
  140. this.key = key;
  141. const keySize = (this.keySize = key.length);
  142.  
  143. if (keySize !== 4 && keySize !== 6 && keySize !== 8) {
  144. throw new Error('Invalid aes key size=' + keySize);
  145. }
  146.  
  147. const ksRows = (this.ksRows = (keySize + 6 + 1) * 4);
  148. let ksRow;
  149. let invKsRow;
  150.  
  151. const keySchedule = (this.keySchedule = new Uint32Array(ksRows));
  152. const invKeySchedule = (this.invKeySchedule = new Uint32Array(ksRows));
  153. const sbox = this.sBox;
  154. const rcon = this.rcon;
  155.  
  156. const invSubMix = this.invSubMix;
  157. const invSubMix0 = invSubMix[0];
  158. const invSubMix1 = invSubMix[1];
  159. const invSubMix2 = invSubMix[2];
  160. const invSubMix3 = invSubMix[3];
  161.  
  162. let prev;
  163. let t;
  164.  
  165. for (ksRow = 0; ksRow < ksRows; ksRow++) {
  166. if (ksRow < keySize) {
  167. prev = keySchedule[ksRow] = key[ksRow];
  168. continue;
  169. }
  170. t = prev;
  171.  
  172. if (ksRow % keySize === 0) {
  173. // Rot word
  174. t = (t << 8) | (t >>> 24);
  175.  
  176. // Sub word
  177. t =
  178. (sbox[t >>> 24] << 24) |
  179. (sbox[(t >>> 16) & 0xff] << 16) |
  180. (sbox[(t >>> 8) & 0xff] << 8) |
  181. sbox[t & 0xff];
  182.  
  183. // Mix Rcon
  184. t ^= rcon[(ksRow / keySize) | 0] << 24;
  185. } else if (keySize > 6 && ksRow % keySize === 4) {
  186. // Sub word
  187. t =
  188. (sbox[t >>> 24] << 24) |
  189. (sbox[(t >>> 16) & 0xff] << 16) |
  190. (sbox[(t >>> 8) & 0xff] << 8) |
  191. sbox[t & 0xff];
  192. }
  193.  
  194. keySchedule[ksRow] = prev = (keySchedule[ksRow - keySize] ^ t) >>> 0;
  195. }
  196.  
  197. for (invKsRow = 0; invKsRow < ksRows; invKsRow++) {
  198. ksRow = ksRows - invKsRow;
  199. if (invKsRow & 3) {
  200. t = keySchedule[ksRow];
  201. } else {
  202. t = keySchedule[ksRow - 4];
  203. }
  204.  
  205. if (invKsRow < 4 || ksRow <= 4) {
  206. invKeySchedule[invKsRow] = t;
  207. } else {
  208. invKeySchedule[invKsRow] =
  209. invSubMix0[sbox[t >>> 24]] ^
  210. invSubMix1[sbox[(t >>> 16) & 0xff]] ^
  211. invSubMix2[sbox[(t >>> 8) & 0xff]] ^
  212. invSubMix3[sbox[t & 0xff]];
  213. }
  214.  
  215. invKeySchedule[invKsRow] = invKeySchedule[invKsRow] >>> 0;
  216. }
  217. }
  218.  
  219. // Adding this as a method greatly improves performance.
  220. networkToHostOrderSwap(word) {
  221. return (
  222. (word << 24) |
  223. ((word & 0xff00) << 8) |
  224. ((word & 0xff0000) >> 8) |
  225. (word >>> 24)
  226. );
  227. }
  228.  
  229. decrypt(inputArrayBuffer: ArrayBuffer, offset: number, aesIV: ArrayBuffer) {
  230. const nRounds = this.keySize + 6;
  231. const invKeySchedule = this.invKeySchedule;
  232. const invSBOX = this.invSBox;
  233.  
  234. const invSubMix = this.invSubMix;
  235. const invSubMix0 = invSubMix[0];
  236. const invSubMix1 = invSubMix[1];
  237. const invSubMix2 = invSubMix[2];
  238. const invSubMix3 = invSubMix[3];
  239.  
  240. const initVector = this.uint8ArrayToUint32Array_(aesIV);
  241. let initVector0 = initVector[0];
  242. let initVector1 = initVector[1];
  243. let initVector2 = initVector[2];
  244. let initVector3 = initVector[3];
  245.  
  246. const inputInt32 = new Int32Array(inputArrayBuffer);
  247. const outputInt32 = new Int32Array(inputInt32.length);
  248.  
  249. let t0, t1, t2, t3;
  250. let s0, s1, s2, s3;
  251. let inputWords0, inputWords1, inputWords2, inputWords3;
  252.  
  253. let ksRow, i;
  254. const swapWord = this.networkToHostOrderSwap;
  255.  
  256. while (offset < inputInt32.length) {
  257. inputWords0 = swapWord(inputInt32[offset]);
  258. inputWords1 = swapWord(inputInt32[offset + 1]);
  259. inputWords2 = swapWord(inputInt32[offset + 2]);
  260. inputWords3 = swapWord(inputInt32[offset + 3]);
  261.  
  262. s0 = inputWords0 ^ invKeySchedule[0];
  263. s1 = inputWords3 ^ invKeySchedule[1];
  264. s2 = inputWords2 ^ invKeySchedule[2];
  265. s3 = inputWords1 ^ invKeySchedule[3];
  266.  
  267. ksRow = 4;
  268.  
  269. // Iterate through the rounds of decryption
  270. for (i = 1; i < nRounds; i++) {
  271. t0 =
  272. invSubMix0[s0 >>> 24] ^
  273. invSubMix1[(s1 >> 16) & 0xff] ^
  274. invSubMix2[(s2 >> 8) & 0xff] ^
  275. invSubMix3[s3 & 0xff] ^
  276. invKeySchedule[ksRow];
  277. t1 =
  278. invSubMix0[s1 >>> 24] ^
  279. invSubMix1[(s2 >> 16) & 0xff] ^
  280. invSubMix2[(s3 >> 8) & 0xff] ^
  281. invSubMix3[s0 & 0xff] ^
  282. invKeySchedule[ksRow + 1];
  283. t2 =
  284. invSubMix0[s2 >>> 24] ^
  285. invSubMix1[(s3 >> 16) & 0xff] ^
  286. invSubMix2[(s0 >> 8) & 0xff] ^
  287. invSubMix3[s1 & 0xff] ^
  288. invKeySchedule[ksRow + 2];
  289. t3 =
  290. invSubMix0[s3 >>> 24] ^
  291. invSubMix1[(s0 >> 16) & 0xff] ^
  292. invSubMix2[(s1 >> 8) & 0xff] ^
  293. invSubMix3[s2 & 0xff] ^
  294. invKeySchedule[ksRow + 3];
  295. // Update state
  296. s0 = t0;
  297. s1 = t1;
  298. s2 = t2;
  299. s3 = t3;
  300.  
  301. ksRow = ksRow + 4;
  302. }
  303.  
  304. // Shift rows, sub bytes, add round key
  305. t0 =
  306. (invSBOX[s0 >>> 24] << 24) ^
  307. (invSBOX[(s1 >> 16) & 0xff] << 16) ^
  308. (invSBOX[(s2 >> 8) & 0xff] << 8) ^
  309. invSBOX[s3 & 0xff] ^
  310. invKeySchedule[ksRow];
  311. t1 =
  312. (invSBOX[s1 >>> 24] << 24) ^
  313. (invSBOX[(s2 >> 16) & 0xff] << 16) ^
  314. (invSBOX[(s3 >> 8) & 0xff] << 8) ^
  315. invSBOX[s0 & 0xff] ^
  316. invKeySchedule[ksRow + 1];
  317. t2 =
  318. (invSBOX[s2 >>> 24] << 24) ^
  319. (invSBOX[(s3 >> 16) & 0xff] << 16) ^
  320. (invSBOX[(s0 >> 8) & 0xff] << 8) ^
  321. invSBOX[s1 & 0xff] ^
  322. invKeySchedule[ksRow + 2];
  323. t3 =
  324. (invSBOX[s3 >>> 24] << 24) ^
  325. (invSBOX[(s0 >> 16) & 0xff] << 16) ^
  326. (invSBOX[(s1 >> 8) & 0xff] << 8) ^
  327. invSBOX[s2 & 0xff] ^
  328. invKeySchedule[ksRow + 3];
  329. ksRow = ksRow + 3;
  330.  
  331. // Write
  332. outputInt32[offset] = swapWord(t0 ^ initVector0);
  333. outputInt32[offset + 1] = swapWord(t3 ^ initVector1);
  334. outputInt32[offset + 2] = swapWord(t2 ^ initVector2);
  335. outputInt32[offset + 3] = swapWord(t1 ^ initVector3);
  336.  
  337. // reset initVector to last 4 unsigned int
  338. initVector0 = inputWords0;
  339. initVector1 = inputWords1;
  340. initVector2 = inputWords2;
  341. initVector3 = inputWords3;
  342.  
  343. offset = offset + 4;
  344. }
  345.  
  346. return outputInt32.buffer;
  347. }
  348. }