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