aes-decryptor.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. // 代码来至 hls.js https://github.com/video-dev/hls.js
  2. function removePadding(buffer) {
  3. const outputBytes = buffer.byteLength;
  4. const paddingBytes = outputBytes && (new DataView(buffer)).getUint8(outputBytes - 1);
  5. if (paddingBytes) {
  6. return buffer.slice(0, outputBytes - paddingBytes);
  7. } else {
  8. return buffer;
  9. }
  10. }
  11. function AESDecryptor() {
  12. return {
  13. constructor() {
  14. this.rcon = [0x0, 0x1, 0x2, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
  15. this.subMix = [new Uint32Array(256), new Uint32Array(256), new Uint32Array(256), new Uint32Array(256)];
  16. this.invSubMix = [new Uint32Array(256), new Uint32Array(256), new Uint32Array(256), new Uint32Array(256)];
  17. this.sBox = new Uint32Array(256);
  18. this.invSBox = new Uint32Array(256);
  19. // Changes during runtime
  20. this.key = new Uint32Array(0);
  21. this.initTable();
  22. },
  23. // Using view.getUint32() also swaps the byte order.
  24. uint8ArrayToUint32Array_(arrayBuffer) {
  25. let view = new DataView(arrayBuffer);
  26. let newArray = new Uint32Array(4);
  27. for (let i = 0; i < 4; i++) {
  28. newArray[i] = view.getUint32(i * 4);
  29. }
  30. return newArray;
  31. },
  32. initTable() {
  33. let sBox = this.sBox;
  34. let invSBox = this.invSBox;
  35. let subMix = this.subMix;
  36. let subMix0 = subMix[0];
  37. let subMix1 = subMix[1];
  38. let subMix2 = subMix[2];
  39. let subMix3 = subMix[3];
  40. let invSubMix = this.invSubMix;
  41. let invSubMix0 = invSubMix[0];
  42. let invSubMix1 = invSubMix[1];
  43. let invSubMix2 = invSubMix[2];
  44. let invSubMix3 = invSubMix[3];
  45. let d = new Uint32Array(256);
  46. let x = 0;
  47. let xi = 0;
  48. let i = 0;
  49. for (i = 0; i < 256; i++) {
  50. if (i < 128) {
  51. d[i] = i << 1;
  52. } else {
  53. d[i] = (i << 1) ^ 0x11b;
  54. }
  55. }
  56. for (i = 0; i < 256; i++) {
  57. let sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4);
  58. sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63;
  59. sBox[x] = sx;
  60. invSBox[sx] = x;
  61. // Compute multiplication
  62. let x2 = d[x];
  63. let x4 = d[x2];
  64. let x8 = d[x4];
  65. // Compute sub/invSub bytes, mix columns tables
  66. let t = (d[sx] * 0x101) ^ (sx * 0x1010100);
  67. subMix0[x] = (t << 24) | (t >>> 8);
  68. subMix1[x] = (t << 16) | (t >>> 16);
  69. subMix2[x] = (t << 8) | (t >>> 24);
  70. subMix3[x] = t;
  71. // Compute inv sub bytes, inv mix columns tables
  72. t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100);
  73. invSubMix0[sx] = (t << 24) | (t >>> 8);
  74. invSubMix1[sx] = (t << 16) | (t >>> 16);
  75. invSubMix2[sx] = (t << 8) | (t >>> 24);
  76. invSubMix3[sx] = t;
  77. // Compute next counter
  78. if (!x) {
  79. x = xi = 1;
  80. } else {
  81. x = x2 ^ d[d[d[x8 ^ x2]]];
  82. xi ^= d[d[xi]];
  83. }
  84. }
  85. },
  86. expandKey(keyBuffer) {
  87. // convert keyBuffer to Uint32Array
  88. let key = this.uint8ArrayToUint32Array_(keyBuffer);
  89. let sameKey = true;
  90. let offset = 0;
  91. while (offset < key.length && sameKey) {
  92. sameKey = (key[offset] === this.key[offset]);
  93. offset++;
  94. }
  95. if (sameKey) {
  96. return;
  97. }
  98. this.key = key;
  99. let keySize = this.keySize = key.length;
  100. if (keySize !== 4 && keySize !== 6 && keySize !== 8) {
  101. throw new Error('Invalid aes key size=' + keySize);
  102. }
  103. let ksRows = this.ksRows = (keySize + 6 + 1) * 4;
  104. let ksRow;
  105. let invKsRow;
  106. let keySchedule = this.keySchedule = new Uint32Array(ksRows);
  107. let invKeySchedule = this.invKeySchedule = new Uint32Array(ksRows);
  108. let sbox = this.sBox;
  109. let rcon = this.rcon;
  110. let invSubMix = this.invSubMix;
  111. let invSubMix0 = invSubMix[0];
  112. let invSubMix1 = invSubMix[1];
  113. let invSubMix2 = invSubMix[2];
  114. let invSubMix3 = invSubMix[3];
  115. let prev;
  116. let t;
  117. for (ksRow = 0; ksRow < ksRows; ksRow++) {
  118. if (ksRow < keySize) {
  119. prev = keySchedule[ksRow] = key[ksRow];
  120. continue;
  121. }
  122. t = prev;
  123. if (ksRow % keySize === 0) {
  124. // Rot word
  125. t = (t << 8) | (t >>> 24);
  126. // Sub word
  127. t = (sbox[t >>> 24] << 24) | (sbox[(t >>> 16) & 0xff] << 16) | (sbox[(t >>> 8) & 0xff] << 8) | sbox[t & 0xff];
  128. // Mix Rcon
  129. t ^= rcon[(ksRow / keySize) | 0] << 24;
  130. } else if (keySize > 6 && ksRow % keySize === 4) {
  131. // Sub word
  132. t = (sbox[t >>> 24] << 24) | (sbox[(t >>> 16) & 0xff] << 16) | (sbox[(t >>> 8) & 0xff] << 8) | sbox[t & 0xff];
  133. }
  134. keySchedule[ksRow] = prev = (keySchedule[ksRow - keySize] ^ t) >>> 0;
  135. }
  136. for (invKsRow = 0; invKsRow < ksRows; invKsRow++) {
  137. ksRow = ksRows - invKsRow;
  138. if (invKsRow & 3) {
  139. t = keySchedule[ksRow];
  140. } else {
  141. t = keySchedule[ksRow - 4];
  142. }
  143. if (invKsRow < 4 || ksRow <= 4) {
  144. invKeySchedule[invKsRow] = t;
  145. } else {
  146. invKeySchedule[invKsRow] = invSubMix0[sbox[t >>> 24]] ^ invSubMix1[sbox[(t >>> 16) & 0xff]] ^ invSubMix2[sbox[(t >>> 8) & 0xff]] ^ invSubMix3[sbox[t & 0xff]];
  147. }
  148. invKeySchedule[invKsRow] = invKeySchedule[invKsRow] >>> 0;
  149. }
  150. },
  151. // Adding this as a method greatly improves performance.
  152. networkToHostOrderSwap(word) {
  153. return (word << 24) | ((word & 0xff00) << 8) | ((word & 0xff0000) >> 8) | (word >>> 24);
  154. },
  155. decrypt(inputArrayBuffer, offset, aesIV, removePKCS7Padding) {
  156. let nRounds = this.keySize + 6;
  157. let invKeySchedule = this.invKeySchedule;
  158. let invSBOX = this.invSBox;
  159. let invSubMix = this.invSubMix;
  160. let invSubMix0 = invSubMix[0];
  161. let invSubMix1 = invSubMix[1];
  162. let invSubMix2 = invSubMix[2];
  163. let invSubMix3 = invSubMix[3];
  164. let initVector = this.uint8ArrayToUint32Array_(aesIV);
  165. let initVector0 = initVector[0];
  166. let initVector1 = initVector[1];
  167. let initVector2 = initVector[2];
  168. let initVector3 = initVector[3];
  169. let inputInt32 = new Int32Array(inputArrayBuffer);
  170. let outputInt32 = new Int32Array(inputInt32.length);
  171. let t0, t1, t2, t3;
  172. let s0, s1, s2, s3;
  173. let inputWords0, inputWords1, inputWords2, inputWords3;
  174. let ksRow, i;
  175. let swapWord = this.networkToHostOrderSwap;
  176. while (offset < inputInt32.length) {
  177. inputWords0 = swapWord(inputInt32[offset]);
  178. inputWords1 = swapWord(inputInt32[offset + 1]);
  179. inputWords2 = swapWord(inputInt32[offset + 2]);
  180. inputWords3 = swapWord(inputInt32[offset + 3]);
  181. s0 = inputWords0 ^ invKeySchedule[0];
  182. s1 = inputWords3 ^ invKeySchedule[1];
  183. s2 = inputWords2 ^ invKeySchedule[2];
  184. s3 = inputWords1 ^ invKeySchedule[3];
  185. ksRow = 4;
  186. // Iterate through the rounds of decryption
  187. for (i = 1; i < nRounds; i++) {
  188. t0 = invSubMix0[s0 >>> 24] ^ invSubMix1[(s1 >> 16) & 0xff] ^ invSubMix2[(s2 >> 8) & 0xff] ^ invSubMix3[s3 & 0xff] ^ invKeySchedule[ksRow];
  189. t1 = invSubMix0[s1 >>> 24] ^ invSubMix1[(s2 >> 16) & 0xff] ^ invSubMix2[(s3 >> 8) & 0xff] ^ invSubMix3[s0 & 0xff] ^ invKeySchedule[ksRow + 1];
  190. t2 = invSubMix0[s2 >>> 24] ^ invSubMix1[(s3 >> 16) & 0xff] ^ invSubMix2[(s0 >> 8) & 0xff] ^ invSubMix3[s1 & 0xff] ^ invKeySchedule[ksRow + 2];
  191. t3 = invSubMix0[s3 >>> 24] ^ invSubMix1[(s0 >> 16) & 0xff] ^ invSubMix2[(s1 >> 8) & 0xff] ^ invSubMix3[s2 & 0xff] ^ invKeySchedule[ksRow + 3];
  192. // Update state
  193. s0 = t0;
  194. s1 = t1;
  195. s2 = t2;
  196. s3 = t3;
  197. ksRow = ksRow + 4;
  198. }
  199. // Shift rows, sub bytes, add round key
  200. t0 = ((invSBOX[s0 >>> 24] << 24) ^ (invSBOX[(s1 >> 16) & 0xff] << 16) ^ (invSBOX[(s2 >> 8) & 0xff] << 8) ^ invSBOX[s3 & 0xff]) ^ invKeySchedule[ksRow];
  201. t1 = ((invSBOX[s1 >>> 24] << 24) ^ (invSBOX[(s2 >> 16) & 0xff] << 16) ^ (invSBOX[(s3 >> 8) & 0xff] << 8) ^ invSBOX[s0 & 0xff]) ^ invKeySchedule[ksRow + 1];
  202. t2 = ((invSBOX[s2 >>> 24] << 24) ^ (invSBOX[(s3 >> 16) & 0xff] << 16) ^ (invSBOX[(s0 >> 8) & 0xff] << 8) ^ invSBOX[s1 & 0xff]) ^ invKeySchedule[ksRow + 2];
  203. t3 = ((invSBOX[s3 >>> 24] << 24) ^ (invSBOX[(s0 >> 16) & 0xff] << 16) ^ (invSBOX[(s1 >> 8) & 0xff] << 8) ^ invSBOX[s2 & 0xff]) ^ invKeySchedule[ksRow + 3];
  204. ksRow = ksRow + 3;
  205. // Write
  206. outputInt32[offset] = swapWord(t0 ^ initVector0);
  207. outputInt32[offset + 1] = swapWord(t3 ^ initVector1);
  208. outputInt32[offset + 2] = swapWord(t2 ^ initVector2);
  209. outputInt32[offset + 3] = swapWord(t1 ^ initVector3);
  210. // reset initVector to last 4 unsigned int
  211. initVector0 = inputWords0;
  212. initVector1 = inputWords1;
  213. initVector2 = inputWords2;
  214. initVector3 = inputWords3;
  215. offset = offset + 4;
  216. }
  217. return removePKCS7Padding ? removePadding(outputInt32.buffer) : outputInt32.buffer;
  218. },
  219. destroy() {
  220. this.key = undefined;
  221. this.keySize = undefined;
  222. this.ksRows = undefined;
  223. this.sBox = undefined;
  224. this.invSBox = undefined;
  225. this.subMix = undefined;
  226. this.invSubMix = undefined;
  227. this.keySchedule = undefined;
  228. this.invKeySchedule = undefined;
  229. this.rcon = undefined;
  230. },
  231. }
  232. }