pdf-parser.js 5.2.26 → 5.2.28

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (221) hide show
  1. package/dist/{afm-widths-BYiX2L9D.cjs → afm-widths-BHD2HZIL.cjs} +553 -642
  2. package/dist/{afm-widths-blUDDjlI.js → afm-widths-B_IGbGky.js} +553 -642
  3. package/dist/afm-widths.cjs +1 -1
  4. package/dist/afm-widths.js +1 -1
  5. package/dist/annotations.cjs +5 -3
  6. package/dist/annotations.d.cts +2 -2
  7. package/dist/annotations.d.ts +2 -2
  8. package/dist/annotations.js +5 -3
  9. package/dist/attachments.d.cts +2 -2
  10. package/dist/attachments.d.ts +2 -2
  11. package/dist/builtin-encoding.cjs +17 -6
  12. package/dist/builtin-encoding.js +17 -6
  13. package/dist/bytes/crc32.cjs +5 -3
  14. package/dist/bytes/crc32.js +5 -3
  15. package/dist/bytes/flate.cjs +0 -1
  16. package/dist/bytes/flate.js +0 -1
  17. package/dist/cff-bounds.cjs +73 -45
  18. package/dist/cff-bounds.js +73 -45
  19. package/dist/cff.cjs +13 -6
  20. package/dist/cff.js +13 -6
  21. package/dist/cmap-table.cjs +54 -26
  22. package/dist/cmap-table.js +54 -26
  23. package/dist/cmap.cjs +5 -3
  24. package/dist/cmap.js +5 -3
  25. package/dist/content-read.d.cts +1 -1
  26. package/dist/content-read.d.ts +1 -1
  27. package/dist/content-write.cjs +21 -14
  28. package/dist/content-write.d.cts +2 -2
  29. package/dist/content-write.d.ts +2 -2
  30. package/dist/content-write.js +21 -14
  31. package/dist/crypto/aes.cjs +76 -55
  32. package/dist/crypto/aes.js +76 -55
  33. package/dist/crypto/md5.cjs +345 -149
  34. package/dist/crypto/md5.js +345 -149
  35. package/dist/crypto/rc4.cjs +5 -4
  36. package/dist/crypto/rc4.js +5 -4
  37. package/dist/crypto/sha2.cjs +92 -43
  38. package/dist/crypto/sha2.js +92 -43
  39. package/dist/document.cjs +17 -11
  40. package/dist/document.d.cts +1 -1
  41. package/dist/document.d.ts +1 -1
  42. package/dist/document.js +17 -11
  43. package/dist/{embedded-font-Bnhh4juV.d.ts → embedded-font-ByD7ZiQS.d.ts} +1 -1
  44. package/dist/{embedded-font-CEf-zBCA.d.cts → embedded-font-DgVBedw6.d.cts} +1 -1
  45. package/dist/embedded-font-write.d.cts +2 -2
  46. package/dist/embedded-font-write.d.ts +2 -2
  47. package/dist/embedded-font.cjs +4 -2
  48. package/dist/embedded-font.d.cts +1 -1
  49. package/dist/embedded-font.d.ts +1 -1
  50. package/dist/embedded-font.js +4 -2
  51. package/dist/encoding.cjs +1 -1
  52. package/dist/encoding.js +1 -1
  53. package/dist/encrypt-write.cjs +25 -21
  54. package/dist/encrypt-write.d.cts +1 -1
  55. package/dist/encrypt-write.d.ts +1 -1
  56. package/dist/encrypt-write.js +25 -21
  57. package/dist/encrypt.cjs +43 -64
  58. package/dist/encrypt.d.cts +1 -1
  59. package/dist/encrypt.d.ts +1 -1
  60. package/dist/encrypt.js +43 -64
  61. package/dist/filters.cjs +46 -26
  62. package/dist/filters.d.cts +1 -1
  63. package/dist/filters.d.ts +1 -1
  64. package/dist/filters.js +46 -26
  65. package/dist/font-face.cjs +2 -1
  66. package/dist/font-face.js +2 -1
  67. package/dist/font-read.cjs +12 -6
  68. package/dist/font-read.d.cts +1 -1
  69. package/dist/font-read.d.ts +1 -1
  70. package/dist/font-read.js +12 -6
  71. package/dist/font-registry.d.cts +3 -3
  72. package/dist/font-registry.d.ts +3 -3
  73. package/dist/font-tables.cjs +55 -28
  74. package/dist/font-tables.js +55 -28
  75. package/dist/form.cjs +13 -8
  76. package/dist/form.d.cts +2 -2
  77. package/dist/form.d.ts +2 -2
  78. package/dist/form.js +13 -8
  79. package/dist/gdef-table.cjs +6 -3
  80. package/dist/gdef-table.js +6 -3
  81. package/dist/{glyf-contours-C0epBNqP.d.ts → glyf-contours-B1PsI9oA.d.ts} +1 -1
  82. package/dist/{glyf-contours-BRpvd0rt.d.cts → glyf-contours-NHBpgks-.d.cts} +1 -1
  83. package/dist/glyf-contours.cjs +3 -1
  84. package/dist/glyf-contours.d.cts +1 -1
  85. package/dist/glyf-contours.d.ts +1 -1
  86. package/dist/glyf-contours.js +3 -1
  87. package/dist/glyf.cjs +22 -11
  88. package/dist/glyf.js +22 -11
  89. package/dist/gpos-table.cjs +37 -19
  90. package/dist/gpos-table.js +37 -19
  91. package/dist/gsub-table.cjs +40 -25
  92. package/dist/gsub-table.js +40 -25
  93. package/dist/hmtx-table.cjs +5 -3
  94. package/dist/hmtx-table.js +5 -3
  95. package/dist/image/ccitt-encode.cjs +8 -5
  96. package/dist/image/ccitt-encode.js +8 -5
  97. package/dist/image/ccitt.cjs +801 -209
  98. package/dist/image/ccitt.js +801 -209
  99. package/dist/image/jbig2-arith.cjs +340 -306
  100. package/dist/image/jbig2-arith.js +340 -306
  101. package/dist/image/jbig2-bitmap.cjs +8 -5
  102. package/dist/image/jbig2-bitmap.js +8 -5
  103. package/dist/image/jbig2-generic.cjs +240 -69
  104. package/dist/image/jbig2-generic.js +240 -69
  105. package/dist/image/jbig2-text.cjs +4 -2
  106. package/dist/image/jbig2-text.js +4 -2
  107. package/dist/image/jbig2.cjs +69 -31
  108. package/dist/image/jbig2.js +69 -31
  109. package/dist/image/jp2-boxes.cjs +52 -27
  110. package/dist/image/jp2-boxes.js +52 -27
  111. package/dist/image/jpeg-info.cjs +29 -24
  112. package/dist/image/jpeg-info.js +29 -24
  113. package/dist/image/jpeg2000-codestream.cjs +61 -32
  114. package/dist/image/jpeg2000-codestream.js +61 -32
  115. package/dist/image/jpeg2000-dwt.cjs +2 -1
  116. package/dist/image/jpeg2000-dwt.js +2 -1
  117. package/dist/image/jpeg2000-t1.cjs +21 -11
  118. package/dist/image/jpeg2000-t1.js +21 -11
  119. package/dist/image/jpeg2000-t2.cjs +12 -5
  120. package/dist/image/jpeg2000-t2.js +12 -5
  121. package/dist/image/jpeg2000-tagtree.cjs +8 -4
  122. package/dist/image/jpeg2000-tagtree.js +8 -4
  123. package/dist/image/jpeg2000.cjs +11 -6
  124. package/dist/image/jpeg2000.js +11 -6
  125. package/dist/image/png-decode.cjs +62 -39
  126. package/dist/image/png-decode.d.cts +3 -2
  127. package/dist/image/png-decode.d.ts +3 -2
  128. package/dist/image/png-decode.js +62 -39
  129. package/dist/image/png-filter.cjs +23 -15
  130. package/dist/image/png-filter.js +23 -15
  131. package/dist/images-read.cjs +41 -36
  132. package/dist/images-read.d.cts +2 -2
  133. package/dist/images-read.d.ts +2 -2
  134. package/dist/images-read.js +41 -36
  135. package/dist/index.cjs +1 -1
  136. package/dist/index.d.cts +1 -1
  137. package/dist/index.d.ts +1 -1
  138. package/dist/index.js +1 -1
  139. package/dist/interpret.cjs +68 -40
  140. package/dist/interpret.d.cts +1 -1
  141. package/dist/interpret.d.ts +1 -1
  142. package/dist/interpret.js +68 -40
  143. package/dist/lexer.cjs +86 -51
  144. package/dist/lexer.js +86 -51
  145. package/dist/math-content-write.cjs +9 -5
  146. package/dist/math-content-write.js +9 -5
  147. package/dist/math-font-write.cjs +2 -1
  148. package/dist/math-font-write.d.cts +1 -1
  149. package/dist/math-font-write.d.ts +1 -1
  150. package/dist/math-font-write.js +2 -1
  151. package/dist/math-font.cjs +16 -7
  152. package/dist/math-font.js +16 -7
  153. package/dist/math-table.cjs +25 -13
  154. package/dist/math-table.js +25 -13
  155. package/dist/matrix.cjs +3 -2
  156. package/dist/matrix.js +3 -2
  157. package/dist/measure.cjs +1 -1
  158. package/dist/measure.js +1 -1
  159. package/dist/names.cjs +7 -7
  160. package/dist/names.d.cts +2 -2
  161. package/dist/names.d.ts +2 -2
  162. package/dist/names.js +7 -7
  163. package/dist/navigation.cjs +16 -12
  164. package/dist/navigation.d.cts +4 -4
  165. package/dist/navigation.d.ts +4 -4
  166. package/dist/navigation.js +16 -12
  167. package/dist/{objects-B7HdMeWS.d.cts → objects-e2RS3-yT.d.cts} +3 -3
  168. package/dist/{objects-B7HdMeWS.d.ts → objects-e2RS3-yT.d.ts} +3 -3
  169. package/dist/objects.d.cts +1 -1
  170. package/dist/objects.d.ts +1 -1
  171. package/dist/optional-content.d.cts +2 -2
  172. package/dist/optional-content.d.ts +2 -2
  173. package/dist/ot-layout-common.cjs +22 -15
  174. package/dist/ot-layout-common.js +22 -15
  175. package/dist/parse.cjs +7 -5
  176. package/dist/parse.d.cts +1 -1
  177. package/dist/parse.d.ts +1 -1
  178. package/dist/parse.js +7 -5
  179. package/dist/pdf-text.cjs +5 -2
  180. package/dist/pdf-text.js +5 -2
  181. package/dist/predictors.cjs +14 -9
  182. package/dist/predictors.d.cts +1 -1
  183. package/dist/predictors.d.ts +1 -1
  184. package/dist/predictors.js +14 -9
  185. package/dist/raster.cjs +30 -19
  186. package/dist/raster.d.cts +8 -8
  187. package/dist/raster.d.ts +8 -8
  188. package/dist/raster.js +30 -19
  189. package/dist/read.cjs +29 -16
  190. package/dist/read.js +29 -16
  191. package/dist/serialize.cjs +6 -2
  192. package/dist/serialize.d.cts +1 -1
  193. package/dist/serialize.d.ts +1 -1
  194. package/dist/serialize.js +6 -2
  195. package/dist/sfnt-subset.cjs +25 -13
  196. package/dist/sfnt-subset.d.cts +1 -1
  197. package/dist/sfnt-subset.d.ts +1 -1
  198. package/dist/sfnt-subset.js +25 -13
  199. package/dist/sfnt.cjs +20 -10
  200. package/dist/sfnt.js +20 -10
  201. package/dist/structure.d.cts +2 -2
  202. package/dist/structure.d.ts +2 -2
  203. package/dist/text-group.cjs +0 -1
  204. package/dist/text-group.js +0 -1
  205. package/dist/text-layout.d.cts +1 -1
  206. package/dist/text-layout.d.ts +1 -1
  207. package/dist/tounicode.cjs +4 -2
  208. package/dist/tounicode.d.cts +1 -1
  209. package/dist/tounicode.d.ts +1 -1
  210. package/dist/tounicode.js +4 -2
  211. package/dist/winansi.cjs +1 -1
  212. package/dist/winansi.js +1 -1
  213. package/dist/write.cjs +20 -12
  214. package/dist/write.d.cts +1 -1
  215. package/dist/write.d.ts +1 -1
  216. package/dist/write.js +20 -12
  217. package/dist/xref.cjs +5 -2
  218. package/dist/xref.d.cts +1 -1
  219. package/dist/xref.d.ts +1 -1
  220. package/dist/xref.js +5 -2
  221. package/package.json +1 -1
@@ -4,9 +4,30 @@ const AES_BLOCK_BYTES = 16;
4
4
  const WORDS_PER_BLOCK = 4;
5
5
  const GF_MODULUS = 283;
6
6
  const AFFINE_CONSTANT = 99;
7
+ const BITS_PER_BYTE = 8;
8
+ const BYTE_MASK = 255;
9
+ const BYTE_RANGE = 256;
10
+ const WORD_BYTES = 4;
11
+ const BYTE_OVERFLOW_BIT = 256;
12
+ const ROUND_COUNT_OFFSET = 6;
13
+ const EXTRA_SUBWORD_NK_THRESHOLD = 6;
14
+ const EXTRA_SUBWORD_INTERVAL = 4;
15
+ const AES_128_KEY_BYTES = 16;
16
+ const AES_192_KEY_BYTES = 24;
17
+ const AES_256_KEY_BYTES = 32;
18
+ const AFFINE_ROTATE_1 = 1;
19
+ const AFFINE_ROTATE_2 = 2;
20
+ const AFFINE_ROTATE_3 = 3;
21
+ const AFFINE_ROTATE_4 = 4;
22
+ const MIX_COLUMNS_02 = 2;
23
+ const MIX_COLUMNS_03 = 3;
24
+ const INV_MIX_COLUMNS_0E = 14;
25
+ const INV_MIX_COLUMNS_09 = 9;
26
+ const INV_MIX_COLUMNS_0D = 13;
27
+ const INV_MIX_COLUMNS_0B = 11;
7
28
  function xtime(a) {
8
29
  const doubled = a << 1;
9
- return (doubled & 256) !== 0 ? (doubled ^ GF_MODULUS) & 255 : doubled;
30
+ return (doubled & BYTE_OVERFLOW_BIT) !== 0 ? (doubled ^ GF_MODULUS) & BYTE_MASK : doubled;
10
31
  }
11
32
  function gmul(a, b) {
12
33
  let product = 0;
@@ -17,22 +38,22 @@ function gmul(a, b) {
17
38
  left = xtime(left);
18
39
  right >>= 1;
19
40
  }
20
- return product & 255;
41
+ return product & BYTE_MASK;
21
42
  }
22
43
  function rotl8(value, bits) {
23
- return (value << bits | value >>> 8 - bits) & 255;
44
+ return (value << bits | value >>> BITS_PER_BYTE - bits) & BYTE_MASK;
24
45
  }
25
46
  const { SBOX, INV_SBOX } = (() => {
26
- const inverse = /* @__PURE__ */ new Uint8Array(256);
27
- for (let a = 1; a < 256; a++) for (let b = 1; b < 256; b++) if (gmul(a, b) === 1) {
47
+ const inverse = new Uint8Array(BYTE_RANGE);
48
+ for (let a = 1; a < BYTE_RANGE; a++) for (let b = 1; b < BYTE_RANGE; b++) if (gmul(a, b) === 1) {
28
49
  inverse[a] = b;
29
50
  break;
30
51
  }
31
- const sbox = /* @__PURE__ */ new Uint8Array(256);
32
- const invSbox = /* @__PURE__ */ new Uint8Array(256);
33
- for (let i = 0; i < 256; i++) {
52
+ const sbox = new Uint8Array(BYTE_RANGE);
53
+ const invSbox = new Uint8Array(BYTE_RANGE);
54
+ for (let i = 0; i < BYTE_RANGE; i++) {
34
55
  const b = inverse[i];
35
- const s = (b ^ rotl8(b, 1) ^ rotl8(b, 2) ^ rotl8(b, 3) ^ rotl8(b, 4) ^ AFFINE_CONSTANT) & 255;
56
+ const s = (b ^ rotl8(b, AFFINE_ROTATE_1) ^ rotl8(b, AFFINE_ROTATE_2) ^ rotl8(b, AFFINE_ROTATE_3) ^ rotl8(b, AFFINE_ROTATE_4) ^ AFFINE_CONSTANT) & BYTE_MASK;
36
57
  sbox[i] = s;
37
58
  invSbox[s] = i;
38
59
  }
@@ -42,17 +63,17 @@ const { SBOX, INV_SBOX } = (() => {
42
63
  };
43
64
  })();
44
65
  function subWord(word) {
45
- return (SBOX[word >>> 24 & 255] << 24 | SBOX[word >>> 16 & 255] << 16 | SBOX[word >>> 8 & 255] << 8 | SBOX[word & 255]) >>> 0;
66
+ return (SBOX[word >>> 24 & BYTE_MASK] << 24 | SBOX[word >>> 16 & BYTE_MASK] << 16 | SBOX[word >>> BITS_PER_BYTE & BYTE_MASK] << BITS_PER_BYTE | SBOX[word & BYTE_MASK]) >>> 0;
46
67
  }
47
68
  function rotWord(word) {
48
- return (word << 8 | word >>> 24) >>> 0;
69
+ return (word << BITS_PER_BYTE | word >>> 24) >>> 0;
49
70
  }
50
71
  function expandKey(key) {
51
- if (key.length !== 16 && key.length !== 24 && key.length !== 32) throw new Error(`AES key must be 16, 24, or 32 bytes; got ${String(key.length)}`);
52
- const nk = key.length / 4;
53
- const rounds = nk + 6;
72
+ if (key.length !== AES_128_KEY_BYTES && key.length !== AES_192_KEY_BYTES && key.length !== AES_256_KEY_BYTES) throw new Error(`AES key must be 16, 24, or 32 bytes; got ${String(key.length)}`);
73
+ const nk = key.length / WORD_BYTES;
74
+ const rounds = nk + ROUND_COUNT_OFFSET;
54
75
  const words = new Uint32Array(WORDS_PER_BLOCK * (rounds + 1));
55
- words.set(Array.from({ length: nk }, (_unused, i) => readWord(key, 4 * i)));
76
+ words.set(Array.from({ length: nk }, (_unused, i) => readWord(key, WORD_BYTES * i)));
56
77
  let rcon = 1;
57
78
  words.forEach((_word, i) => {
58
79
  if (i < nk) return;
@@ -60,7 +81,7 @@ function expandKey(key) {
60
81
  if (i % nk === 0) {
61
82
  temp = (subWord(rotWord(temp)) ^ rcon << 24) >>> 0;
62
83
  rcon = xtime(rcon);
63
- } else if (nk > 6 && i % nk === 4) temp = subWord(temp);
84
+ } else if (nk > EXTRA_SUBWORD_NK_THRESHOLD && i % nk === EXTRA_SUBWORD_INTERVAL) temp = subWord(temp);
64
85
  words[i] = (words[i - nk] ^ temp) >>> 0;
65
86
  });
66
87
  return {
@@ -69,28 +90,28 @@ function expandKey(key) {
69
90
  };
70
91
  }
71
92
  function packWord(row0, row1, row2, row3) {
72
- return (row0 << 24 | row1 << 16 | row2 << 8 | row3) >>> 0;
93
+ return (row0 << 24 | row1 << 16 | row2 << BITS_PER_BYTE | row3) >>> 0;
73
94
  }
74
95
  function rotateRight8(word) {
75
- return (word >>> 8 | word << 24) >>> 0;
96
+ return (word >>> BITS_PER_BYTE | word << 24) >>> 0;
76
97
  }
77
98
  const { TE0, TE1, TE2, TE3, TD0, TD1, TD2, TD3 } = (() => {
78
- const te0 = /* @__PURE__ */ new Uint32Array(256);
79
- const te1 = /* @__PURE__ */ new Uint32Array(256);
80
- const te2 = /* @__PURE__ */ new Uint32Array(256);
81
- const te3 = /* @__PURE__ */ new Uint32Array(256);
82
- const td0 = /* @__PURE__ */ new Uint32Array(256);
83
- const td1 = /* @__PURE__ */ new Uint32Array(256);
84
- const td2 = /* @__PURE__ */ new Uint32Array(256);
85
- const td3 = /* @__PURE__ */ new Uint32Array(256);
86
- for (let x = 0; x < 256; x++) {
99
+ const te0 = new Uint32Array(BYTE_RANGE);
100
+ const te1 = new Uint32Array(BYTE_RANGE);
101
+ const te2 = new Uint32Array(BYTE_RANGE);
102
+ const te3 = new Uint32Array(BYTE_RANGE);
103
+ const td0 = new Uint32Array(BYTE_RANGE);
104
+ const td1 = new Uint32Array(BYTE_RANGE);
105
+ const td2 = new Uint32Array(BYTE_RANGE);
106
+ const td3 = new Uint32Array(BYTE_RANGE);
107
+ for (let x = 0; x < BYTE_RANGE; x++) {
87
108
  const s = SBOX[x];
88
- te0[x] = packWord(gmul(s, 2), s, s, gmul(s, 3));
109
+ te0[x] = packWord(gmul(s, MIX_COLUMNS_02), s, s, gmul(s, MIX_COLUMNS_03));
89
110
  te1[x] = rotateRight8(te0[x]);
90
111
  te2[x] = rotateRight8(te1[x]);
91
112
  te3[x] = rotateRight8(te2[x]);
92
113
  const i = INV_SBOX[x];
93
- td0[x] = packWord(gmul(i, 14), gmul(i, 9), gmul(i, 13), gmul(i, 11));
114
+ td0[x] = packWord(gmul(i, INV_MIX_COLUMNS_0E), gmul(i, INV_MIX_COLUMNS_09), gmul(i, INV_MIX_COLUMNS_0D), gmul(i, INV_MIX_COLUMNS_0B));
94
115
  td1[x] = rotateRight8(td0[x]);
95
116
  td2[x] = rotateRight8(td1[x]);
96
117
  td3[x] = rotateRight8(td2[x]);
@@ -112,7 +133,7 @@ function inverseKeyWords(expanded) {
112
133
  const round = Math.floor(index / WORDS_PER_BLOCK);
113
134
  const column = index % WORDS_PER_BLOCK;
114
135
  const word = words[(rounds - round) * WORDS_PER_BLOCK + column];
115
- return round === 0 || round === rounds ? word : TD0[SBOX[word >>> 24]] ^ TD1[SBOX[word >>> 16 & 255]] ^ TD2[SBOX[word >>> 8 & 255]] ^ TD3[SBOX[word & 255]];
136
+ return round === 0 || round === rounds ? word : TD0[SBOX[word >>> 24]] ^ TD1[SBOX[word >>> 16 & BYTE_MASK]] ^ TD2[SBOX[word >>> BITS_PER_BYTE & BYTE_MASK]] ^ TD3[SBOX[word & BYTE_MASK]];
116
137
  });
117
138
  }
118
139
  function readWord(bytes, offset) {
@@ -120,9 +141,9 @@ function readWord(bytes, offset) {
120
141
  }
121
142
  function writeWord(bytes, offset, word) {
122
143
  bytes[offset] = word >>> 24;
123
- bytes[offset + 1] = word >>> 16 & 255;
124
- bytes[offset + 2] = word >>> 8 & 255;
125
- bytes[offset + 3] = word & 255;
144
+ bytes[offset + 1] = word >>> 16 & BYTE_MASK;
145
+ bytes[offset + 2] = word >>> BITS_PER_BYTE & BYTE_MASK;
146
+ bytes[offset + 3] = word & BYTE_MASK;
126
147
  }
127
148
  function encryptWords(key, a0, a1, a2, a3, out) {
128
149
  const rk = key.words;
@@ -132,20 +153,20 @@ function encryptWords(key, a0, a1, a2, a3, out) {
132
153
  let s3 = a3 ^ rk[3];
133
154
  for (let round = 1; round < key.rounds; round++) {
134
155
  const at = round * WORDS_PER_BLOCK;
135
- const t0 = TE0[s0 >>> 24] ^ TE1[s1 >>> 16 & 255] ^ TE2[s2 >>> 8 & 255] ^ TE3[s3 & 255] ^ rk[at];
136
- const t1 = TE0[s1 >>> 24] ^ TE1[s2 >>> 16 & 255] ^ TE2[s3 >>> 8 & 255] ^ TE3[s0 & 255] ^ rk[at + 1];
137
- const t2 = TE0[s2 >>> 24] ^ TE1[s3 >>> 16 & 255] ^ TE2[s0 >>> 8 & 255] ^ TE3[s1 & 255] ^ rk[at + 2];
138
- const t3 = TE0[s3 >>> 24] ^ TE1[s0 >>> 16 & 255] ^ TE2[s1 >>> 8 & 255] ^ TE3[s2 & 255] ^ rk[at + 3];
156
+ const t0 = TE0[s0 >>> 24] ^ TE1[s1 >>> 16 & BYTE_MASK] ^ TE2[s2 >>> BITS_PER_BYTE & BYTE_MASK] ^ TE3[s3 & BYTE_MASK] ^ rk[at];
157
+ const t1 = TE0[s1 >>> 24] ^ TE1[s2 >>> 16 & BYTE_MASK] ^ TE2[s3 >>> BITS_PER_BYTE & BYTE_MASK] ^ TE3[s0 & BYTE_MASK] ^ rk[at + 1];
158
+ const t2 = TE0[s2 >>> 24] ^ TE1[s3 >>> 16 & BYTE_MASK] ^ TE2[s0 >>> BITS_PER_BYTE & BYTE_MASK] ^ TE3[s1 & BYTE_MASK] ^ rk[at + 2];
159
+ const t3 = TE0[s3 >>> 24] ^ TE1[s0 >>> 16 & BYTE_MASK] ^ TE2[s1 >>> BITS_PER_BYTE & BYTE_MASK] ^ TE3[s2 & BYTE_MASK] ^ rk[at + 3];
139
160
  s0 = t0;
140
161
  s1 = t1;
141
162
  s2 = t2;
142
163
  s3 = t3;
143
164
  }
144
165
  const at = key.rounds * WORDS_PER_BLOCK;
145
- out[0] = packWord(SBOX[s0 >>> 24], SBOX[s1 >>> 16 & 255], SBOX[s2 >>> 8 & 255], SBOX[s3 & 255]) ^ rk[at];
146
- out[1] = packWord(SBOX[s1 >>> 24], SBOX[s2 >>> 16 & 255], SBOX[s3 >>> 8 & 255], SBOX[s0 & 255]) ^ rk[at + 1];
147
- out[2] = packWord(SBOX[s2 >>> 24], SBOX[s3 >>> 16 & 255], SBOX[s0 >>> 8 & 255], SBOX[s1 & 255]) ^ rk[at + 2];
148
- out[3] = packWord(SBOX[s3 >>> 24], SBOX[s0 >>> 16 & 255], SBOX[s1 >>> 8 & 255], SBOX[s2 & 255]) ^ rk[at + 3];
166
+ out[0] = packWord(SBOX[s0 >>> 24], SBOX[s1 >>> 16 & BYTE_MASK], SBOX[s2 >>> BITS_PER_BYTE & BYTE_MASK], SBOX[s3 & BYTE_MASK]) ^ rk[at];
167
+ out[1] = packWord(SBOX[s1 >>> 24], SBOX[s2 >>> 16 & BYTE_MASK], SBOX[s3 >>> BITS_PER_BYTE & BYTE_MASK], SBOX[s0 & BYTE_MASK]) ^ rk[at + 1];
168
+ out[2] = packWord(SBOX[s2 >>> 24], SBOX[s3 >>> 16 & BYTE_MASK], SBOX[s0 >>> BITS_PER_BYTE & BYTE_MASK], SBOX[s1 & BYTE_MASK]) ^ rk[at + 2];
169
+ out[3] = packWord(SBOX[s3 >>> 24], SBOX[s0 >>> 16 & BYTE_MASK], SBOX[s1 >>> BITS_PER_BYTE & BYTE_MASK], SBOX[s2 & BYTE_MASK]) ^ rk[at + 3];
149
170
  }
150
171
  function decryptWords(rk, rounds, a0, a1, a2, a3, out) {
151
172
  let s0 = a0 ^ rk[0];
@@ -154,20 +175,20 @@ function decryptWords(rk, rounds, a0, a1, a2, a3, out) {
154
175
  let s3 = a3 ^ rk[3];
155
176
  for (let round = 1; round < rounds; round++) {
156
177
  const at = round * WORDS_PER_BLOCK;
157
- const t0 = TD0[s0 >>> 24] ^ TD1[s3 >>> 16 & 255] ^ TD2[s2 >>> 8 & 255] ^ TD3[s1 & 255] ^ rk[at];
158
- const t1 = TD0[s1 >>> 24] ^ TD1[s0 >>> 16 & 255] ^ TD2[s3 >>> 8 & 255] ^ TD3[s2 & 255] ^ rk[at + 1];
159
- const t2 = TD0[s2 >>> 24] ^ TD1[s1 >>> 16 & 255] ^ TD2[s0 >>> 8 & 255] ^ TD3[s3 & 255] ^ rk[at + 2];
160
- const t3 = TD0[s3 >>> 24] ^ TD1[s2 >>> 16 & 255] ^ TD2[s1 >>> 8 & 255] ^ TD3[s0 & 255] ^ rk[at + 3];
178
+ const t0 = TD0[s0 >>> 24] ^ TD1[s3 >>> 16 & BYTE_MASK] ^ TD2[s2 >>> BITS_PER_BYTE & BYTE_MASK] ^ TD3[s1 & BYTE_MASK] ^ rk[at];
179
+ const t1 = TD0[s1 >>> 24] ^ TD1[s0 >>> 16 & BYTE_MASK] ^ TD2[s3 >>> BITS_PER_BYTE & BYTE_MASK] ^ TD3[s2 & BYTE_MASK] ^ rk[at + 1];
180
+ const t2 = TD0[s2 >>> 24] ^ TD1[s1 >>> 16 & BYTE_MASK] ^ TD2[s0 >>> BITS_PER_BYTE & BYTE_MASK] ^ TD3[s3 & BYTE_MASK] ^ rk[at + 2];
181
+ const t3 = TD0[s3 >>> 24] ^ TD1[s2 >>> 16 & BYTE_MASK] ^ TD2[s1 >>> BITS_PER_BYTE & BYTE_MASK] ^ TD3[s0 & BYTE_MASK] ^ rk[at + 3];
161
182
  s0 = t0;
162
183
  s1 = t1;
163
184
  s2 = t2;
164
185
  s3 = t3;
165
186
  }
166
187
  const at = rounds * WORDS_PER_BLOCK;
167
- out[0] = packWord(INV_SBOX[s0 >>> 24], INV_SBOX[s3 >>> 16 & 255], INV_SBOX[s2 >>> 8 & 255], INV_SBOX[s1 & 255]) ^ rk[at];
168
- out[1] = packWord(INV_SBOX[s1 >>> 24], INV_SBOX[s0 >>> 16 & 255], INV_SBOX[s3 >>> 8 & 255], INV_SBOX[s2 & 255]) ^ rk[at + 1];
169
- out[2] = packWord(INV_SBOX[s2 >>> 24], INV_SBOX[s1 >>> 16 & 255], INV_SBOX[s0 >>> 8 & 255], INV_SBOX[s3 & 255]) ^ rk[at + 2];
170
- out[3] = packWord(INV_SBOX[s3 >>> 24], INV_SBOX[s2 >>> 16 & 255], INV_SBOX[s1 >>> 8 & 255], INV_SBOX[s0 & 255]) ^ rk[at + 3];
188
+ out[0] = packWord(INV_SBOX[s0 >>> 24], INV_SBOX[s3 >>> 16 & BYTE_MASK], INV_SBOX[s2 >>> BITS_PER_BYTE & BYTE_MASK], INV_SBOX[s1 & BYTE_MASK]) ^ rk[at];
189
+ out[1] = packWord(INV_SBOX[s1 >>> 24], INV_SBOX[s0 >>> 16 & BYTE_MASK], INV_SBOX[s3 >>> BITS_PER_BYTE & BYTE_MASK], INV_SBOX[s2 & BYTE_MASK]) ^ rk[at + 1];
190
+ out[2] = packWord(INV_SBOX[s2 >>> 24], INV_SBOX[s1 >>> 16 & BYTE_MASK], INV_SBOX[s0 >>> BITS_PER_BYTE & BYTE_MASK], INV_SBOX[s3 & BYTE_MASK]) ^ rk[at + 2];
191
+ out[3] = packWord(INV_SBOX[s3 >>> 24], INV_SBOX[s2 >>> 16 & BYTE_MASK], INV_SBOX[s1 >>> BITS_PER_BYTE & BYTE_MASK], INV_SBOX[s0 & BYTE_MASK]) ^ rk[at + 3];
171
192
  }
172
193
  function wholeBlockCount(byteLength) {
173
194
  return Math.floor(byteLength / 16);
@@ -179,18 +200,18 @@ function aesCbcDecrypt(key, iv, data) {
179
200
  const out = new Uint8Array(blocks * 16);
180
201
  const plain = new Uint32Array(WORDS_PER_BLOCK);
181
202
  let c0 = readWord(iv, 0);
182
- let c1 = readWord(iv, 4);
203
+ let c1 = readWord(iv, WORD_BYTES);
183
204
  let c2 = readWord(iv, 8);
184
205
  let c3 = readWord(iv, 12);
185
206
  Array.from({ length: blocks }).forEach((_unused, b) => {
186
207
  const at = b * 16;
187
208
  const x0 = readWord(data, at);
188
- const x1 = readWord(data, at + 4);
209
+ const x1 = readWord(data, at + WORD_BYTES);
189
210
  const x2 = readWord(data, at + 8);
190
211
  const x3 = readWord(data, at + 12);
191
212
  decryptWords(inverseKey, expanded.rounds, x0, x1, x2, x3, plain);
192
213
  writeWord(out, at, plain[0] ^ c0);
193
- writeWord(out, at + 4, plain[1] ^ c1);
214
+ writeWord(out, at + WORD_BYTES, plain[1] ^ c1);
194
215
  writeWord(out, at + 8, plain[2] ^ c2);
195
216
  writeWord(out, at + 12, plain[3] ^ c3);
196
217
  c0 = x0;
@@ -206,18 +227,18 @@ function aesCbcEncrypt(key, iv, data) {
206
227
  const out = new Uint8Array(blocks * 16);
207
228
  const cipher = new Uint32Array(WORDS_PER_BLOCK);
208
229
  let c0 = readWord(iv, 0);
209
- let c1 = readWord(iv, 4);
230
+ let c1 = readWord(iv, WORD_BYTES);
210
231
  let c2 = readWord(iv, 8);
211
232
  let c3 = readWord(iv, 12);
212
233
  Array.from({ length: blocks }).forEach((_unused, b) => {
213
234
  const at = b * 16;
214
- encryptWords(expanded, readWord(data, at) ^ c0, readWord(data, at + 4) ^ c1, readWord(data, at + 8) ^ c2, readWord(data, at + 12) ^ c3, cipher);
235
+ encryptWords(expanded, readWord(data, at) ^ c0, readWord(data, at + WORD_BYTES) ^ c1, readWord(data, at + 8) ^ c2, readWord(data, at + 12) ^ c3, cipher);
215
236
  c0 = cipher[0];
216
237
  c1 = cipher[1];
217
238
  c2 = cipher[2];
218
239
  c3 = cipher[3];
219
240
  writeWord(out, at, c0);
220
- writeWord(out, at + 4, c1);
241
+ writeWord(out, at + WORD_BYTES, c1);
221
242
  writeWord(out, at + 8, c2);
222
243
  writeWord(out, at + 12, c3);
223
244
  });
@@ -3,9 +3,30 @@ const AES_BLOCK_BYTES = 16;
3
3
  const WORDS_PER_BLOCK = 4;
4
4
  const GF_MODULUS = 283;
5
5
  const AFFINE_CONSTANT = 99;
6
+ const BITS_PER_BYTE = 8;
7
+ const BYTE_MASK = 255;
8
+ const BYTE_RANGE = 256;
9
+ const WORD_BYTES = 4;
10
+ const BYTE_OVERFLOW_BIT = 256;
11
+ const ROUND_COUNT_OFFSET = 6;
12
+ const EXTRA_SUBWORD_NK_THRESHOLD = 6;
13
+ const EXTRA_SUBWORD_INTERVAL = 4;
14
+ const AES_128_KEY_BYTES = 16;
15
+ const AES_192_KEY_BYTES = 24;
16
+ const AES_256_KEY_BYTES = 32;
17
+ const AFFINE_ROTATE_1 = 1;
18
+ const AFFINE_ROTATE_2 = 2;
19
+ const AFFINE_ROTATE_3 = 3;
20
+ const AFFINE_ROTATE_4 = 4;
21
+ const MIX_COLUMNS_02 = 2;
22
+ const MIX_COLUMNS_03 = 3;
23
+ const INV_MIX_COLUMNS_0E = 14;
24
+ const INV_MIX_COLUMNS_09 = 9;
25
+ const INV_MIX_COLUMNS_0D = 13;
26
+ const INV_MIX_COLUMNS_0B = 11;
6
27
  function xtime(a) {
7
28
  const doubled = a << 1;
8
- return (doubled & 256) !== 0 ? (doubled ^ GF_MODULUS) & 255 : doubled;
29
+ return (doubled & BYTE_OVERFLOW_BIT) !== 0 ? (doubled ^ GF_MODULUS) & BYTE_MASK : doubled;
9
30
  }
10
31
  function gmul(a, b) {
11
32
  let product = 0;
@@ -16,22 +37,22 @@ function gmul(a, b) {
16
37
  left = xtime(left);
17
38
  right >>= 1;
18
39
  }
19
- return product & 255;
40
+ return product & BYTE_MASK;
20
41
  }
21
42
  function rotl8(value, bits) {
22
- return (value << bits | value >>> 8 - bits) & 255;
43
+ return (value << bits | value >>> BITS_PER_BYTE - bits) & BYTE_MASK;
23
44
  }
24
45
  const { SBOX, INV_SBOX } = (() => {
25
- const inverse = /* @__PURE__ */ new Uint8Array(256);
26
- for (let a = 1; a < 256; a++) for (let b = 1; b < 256; b++) if (gmul(a, b) === 1) {
46
+ const inverse = new Uint8Array(BYTE_RANGE);
47
+ for (let a = 1; a < BYTE_RANGE; a++) for (let b = 1; b < BYTE_RANGE; b++) if (gmul(a, b) === 1) {
27
48
  inverse[a] = b;
28
49
  break;
29
50
  }
30
- const sbox = /* @__PURE__ */ new Uint8Array(256);
31
- const invSbox = /* @__PURE__ */ new Uint8Array(256);
32
- for (let i = 0; i < 256; i++) {
51
+ const sbox = new Uint8Array(BYTE_RANGE);
52
+ const invSbox = new Uint8Array(BYTE_RANGE);
53
+ for (let i = 0; i < BYTE_RANGE; i++) {
33
54
  const b = inverse[i];
34
- const s = (b ^ rotl8(b, 1) ^ rotl8(b, 2) ^ rotl8(b, 3) ^ rotl8(b, 4) ^ AFFINE_CONSTANT) & 255;
55
+ const s = (b ^ rotl8(b, AFFINE_ROTATE_1) ^ rotl8(b, AFFINE_ROTATE_2) ^ rotl8(b, AFFINE_ROTATE_3) ^ rotl8(b, AFFINE_ROTATE_4) ^ AFFINE_CONSTANT) & BYTE_MASK;
35
56
  sbox[i] = s;
36
57
  invSbox[s] = i;
37
58
  }
@@ -41,17 +62,17 @@ const { SBOX, INV_SBOX } = (() => {
41
62
  };
42
63
  })();
43
64
  function subWord(word) {
44
- return (SBOX[word >>> 24 & 255] << 24 | SBOX[word >>> 16 & 255] << 16 | SBOX[word >>> 8 & 255] << 8 | SBOX[word & 255]) >>> 0;
65
+ return (SBOX[word >>> 24 & BYTE_MASK] << 24 | SBOX[word >>> 16 & BYTE_MASK] << 16 | SBOX[word >>> BITS_PER_BYTE & BYTE_MASK] << BITS_PER_BYTE | SBOX[word & BYTE_MASK]) >>> 0;
45
66
  }
46
67
  function rotWord(word) {
47
- return (word << 8 | word >>> 24) >>> 0;
68
+ return (word << BITS_PER_BYTE | word >>> 24) >>> 0;
48
69
  }
49
70
  function expandKey(key) {
50
- if (key.length !== 16 && key.length !== 24 && key.length !== 32) throw new Error(`AES key must be 16, 24, or 32 bytes; got ${String(key.length)}`);
51
- const nk = key.length / 4;
52
- const rounds = nk + 6;
71
+ if (key.length !== AES_128_KEY_BYTES && key.length !== AES_192_KEY_BYTES && key.length !== AES_256_KEY_BYTES) throw new Error(`AES key must be 16, 24, or 32 bytes; got ${String(key.length)}`);
72
+ const nk = key.length / WORD_BYTES;
73
+ const rounds = nk + ROUND_COUNT_OFFSET;
53
74
  const words = new Uint32Array(WORDS_PER_BLOCK * (rounds + 1));
54
- words.set(Array.from({ length: nk }, (_unused, i) => readWord(key, 4 * i)));
75
+ words.set(Array.from({ length: nk }, (_unused, i) => readWord(key, WORD_BYTES * i)));
55
76
  let rcon = 1;
56
77
  words.forEach((_word, i) => {
57
78
  if (i < nk) return;
@@ -59,7 +80,7 @@ function expandKey(key) {
59
80
  if (i % nk === 0) {
60
81
  temp = (subWord(rotWord(temp)) ^ rcon << 24) >>> 0;
61
82
  rcon = xtime(rcon);
62
- } else if (nk > 6 && i % nk === 4) temp = subWord(temp);
83
+ } else if (nk > EXTRA_SUBWORD_NK_THRESHOLD && i % nk === EXTRA_SUBWORD_INTERVAL) temp = subWord(temp);
63
84
  words[i] = (words[i - nk] ^ temp) >>> 0;
64
85
  });
65
86
  return {
@@ -68,28 +89,28 @@ function expandKey(key) {
68
89
  };
69
90
  }
70
91
  function packWord(row0, row1, row2, row3) {
71
- return (row0 << 24 | row1 << 16 | row2 << 8 | row3) >>> 0;
92
+ return (row0 << 24 | row1 << 16 | row2 << BITS_PER_BYTE | row3) >>> 0;
72
93
  }
73
94
  function rotateRight8(word) {
74
- return (word >>> 8 | word << 24) >>> 0;
95
+ return (word >>> BITS_PER_BYTE | word << 24) >>> 0;
75
96
  }
76
97
  const { TE0, TE1, TE2, TE3, TD0, TD1, TD2, TD3 } = (() => {
77
- const te0 = /* @__PURE__ */ new Uint32Array(256);
78
- const te1 = /* @__PURE__ */ new Uint32Array(256);
79
- const te2 = /* @__PURE__ */ new Uint32Array(256);
80
- const te3 = /* @__PURE__ */ new Uint32Array(256);
81
- const td0 = /* @__PURE__ */ new Uint32Array(256);
82
- const td1 = /* @__PURE__ */ new Uint32Array(256);
83
- const td2 = /* @__PURE__ */ new Uint32Array(256);
84
- const td3 = /* @__PURE__ */ new Uint32Array(256);
85
- for (let x = 0; x < 256; x++) {
98
+ const te0 = new Uint32Array(BYTE_RANGE);
99
+ const te1 = new Uint32Array(BYTE_RANGE);
100
+ const te2 = new Uint32Array(BYTE_RANGE);
101
+ const te3 = new Uint32Array(BYTE_RANGE);
102
+ const td0 = new Uint32Array(BYTE_RANGE);
103
+ const td1 = new Uint32Array(BYTE_RANGE);
104
+ const td2 = new Uint32Array(BYTE_RANGE);
105
+ const td3 = new Uint32Array(BYTE_RANGE);
106
+ for (let x = 0; x < BYTE_RANGE; x++) {
86
107
  const s = SBOX[x];
87
- te0[x] = packWord(gmul(s, 2), s, s, gmul(s, 3));
108
+ te0[x] = packWord(gmul(s, MIX_COLUMNS_02), s, s, gmul(s, MIX_COLUMNS_03));
88
109
  te1[x] = rotateRight8(te0[x]);
89
110
  te2[x] = rotateRight8(te1[x]);
90
111
  te3[x] = rotateRight8(te2[x]);
91
112
  const i = INV_SBOX[x];
92
- td0[x] = packWord(gmul(i, 14), gmul(i, 9), gmul(i, 13), gmul(i, 11));
113
+ td0[x] = packWord(gmul(i, INV_MIX_COLUMNS_0E), gmul(i, INV_MIX_COLUMNS_09), gmul(i, INV_MIX_COLUMNS_0D), gmul(i, INV_MIX_COLUMNS_0B));
93
114
  td1[x] = rotateRight8(td0[x]);
94
115
  td2[x] = rotateRight8(td1[x]);
95
116
  td3[x] = rotateRight8(td2[x]);
@@ -111,7 +132,7 @@ function inverseKeyWords(expanded) {
111
132
  const round = Math.floor(index / WORDS_PER_BLOCK);
112
133
  const column = index % WORDS_PER_BLOCK;
113
134
  const word = words[(rounds - round) * WORDS_PER_BLOCK + column];
114
- return round === 0 || round === rounds ? word : TD0[SBOX[word >>> 24]] ^ TD1[SBOX[word >>> 16 & 255]] ^ TD2[SBOX[word >>> 8 & 255]] ^ TD3[SBOX[word & 255]];
135
+ return round === 0 || round === rounds ? word : TD0[SBOX[word >>> 24]] ^ TD1[SBOX[word >>> 16 & BYTE_MASK]] ^ TD2[SBOX[word >>> BITS_PER_BYTE & BYTE_MASK]] ^ TD3[SBOX[word & BYTE_MASK]];
115
136
  });
116
137
  }
117
138
  function readWord(bytes, offset) {
@@ -119,9 +140,9 @@ function readWord(bytes, offset) {
119
140
  }
120
141
  function writeWord(bytes, offset, word) {
121
142
  bytes[offset] = word >>> 24;
122
- bytes[offset + 1] = word >>> 16 & 255;
123
- bytes[offset + 2] = word >>> 8 & 255;
124
- bytes[offset + 3] = word & 255;
143
+ bytes[offset + 1] = word >>> 16 & BYTE_MASK;
144
+ bytes[offset + 2] = word >>> BITS_PER_BYTE & BYTE_MASK;
145
+ bytes[offset + 3] = word & BYTE_MASK;
125
146
  }
126
147
  function encryptWords(key, a0, a1, a2, a3, out) {
127
148
  const rk = key.words;
@@ -131,20 +152,20 @@ function encryptWords(key, a0, a1, a2, a3, out) {
131
152
  let s3 = a3 ^ rk[3];
132
153
  for (let round = 1; round < key.rounds; round++) {
133
154
  const at = round * WORDS_PER_BLOCK;
134
- const t0 = TE0[s0 >>> 24] ^ TE1[s1 >>> 16 & 255] ^ TE2[s2 >>> 8 & 255] ^ TE3[s3 & 255] ^ rk[at];
135
- const t1 = TE0[s1 >>> 24] ^ TE1[s2 >>> 16 & 255] ^ TE2[s3 >>> 8 & 255] ^ TE3[s0 & 255] ^ rk[at + 1];
136
- const t2 = TE0[s2 >>> 24] ^ TE1[s3 >>> 16 & 255] ^ TE2[s0 >>> 8 & 255] ^ TE3[s1 & 255] ^ rk[at + 2];
137
- const t3 = TE0[s3 >>> 24] ^ TE1[s0 >>> 16 & 255] ^ TE2[s1 >>> 8 & 255] ^ TE3[s2 & 255] ^ rk[at + 3];
155
+ const t0 = TE0[s0 >>> 24] ^ TE1[s1 >>> 16 & BYTE_MASK] ^ TE2[s2 >>> BITS_PER_BYTE & BYTE_MASK] ^ TE3[s3 & BYTE_MASK] ^ rk[at];
156
+ const t1 = TE0[s1 >>> 24] ^ TE1[s2 >>> 16 & BYTE_MASK] ^ TE2[s3 >>> BITS_PER_BYTE & BYTE_MASK] ^ TE3[s0 & BYTE_MASK] ^ rk[at + 1];
157
+ const t2 = TE0[s2 >>> 24] ^ TE1[s3 >>> 16 & BYTE_MASK] ^ TE2[s0 >>> BITS_PER_BYTE & BYTE_MASK] ^ TE3[s1 & BYTE_MASK] ^ rk[at + 2];
158
+ const t3 = TE0[s3 >>> 24] ^ TE1[s0 >>> 16 & BYTE_MASK] ^ TE2[s1 >>> BITS_PER_BYTE & BYTE_MASK] ^ TE3[s2 & BYTE_MASK] ^ rk[at + 3];
138
159
  s0 = t0;
139
160
  s1 = t1;
140
161
  s2 = t2;
141
162
  s3 = t3;
142
163
  }
143
164
  const at = key.rounds * WORDS_PER_BLOCK;
144
- out[0] = packWord(SBOX[s0 >>> 24], SBOX[s1 >>> 16 & 255], SBOX[s2 >>> 8 & 255], SBOX[s3 & 255]) ^ rk[at];
145
- out[1] = packWord(SBOX[s1 >>> 24], SBOX[s2 >>> 16 & 255], SBOX[s3 >>> 8 & 255], SBOX[s0 & 255]) ^ rk[at + 1];
146
- out[2] = packWord(SBOX[s2 >>> 24], SBOX[s3 >>> 16 & 255], SBOX[s0 >>> 8 & 255], SBOX[s1 & 255]) ^ rk[at + 2];
147
- out[3] = packWord(SBOX[s3 >>> 24], SBOX[s0 >>> 16 & 255], SBOX[s1 >>> 8 & 255], SBOX[s2 & 255]) ^ rk[at + 3];
165
+ out[0] = packWord(SBOX[s0 >>> 24], SBOX[s1 >>> 16 & BYTE_MASK], SBOX[s2 >>> BITS_PER_BYTE & BYTE_MASK], SBOX[s3 & BYTE_MASK]) ^ rk[at];
166
+ out[1] = packWord(SBOX[s1 >>> 24], SBOX[s2 >>> 16 & BYTE_MASK], SBOX[s3 >>> BITS_PER_BYTE & BYTE_MASK], SBOX[s0 & BYTE_MASK]) ^ rk[at + 1];
167
+ out[2] = packWord(SBOX[s2 >>> 24], SBOX[s3 >>> 16 & BYTE_MASK], SBOX[s0 >>> BITS_PER_BYTE & BYTE_MASK], SBOX[s1 & BYTE_MASK]) ^ rk[at + 2];
168
+ out[3] = packWord(SBOX[s3 >>> 24], SBOX[s0 >>> 16 & BYTE_MASK], SBOX[s1 >>> BITS_PER_BYTE & BYTE_MASK], SBOX[s2 & BYTE_MASK]) ^ rk[at + 3];
148
169
  }
149
170
  function decryptWords(rk, rounds, a0, a1, a2, a3, out) {
150
171
  let s0 = a0 ^ rk[0];
@@ -153,20 +174,20 @@ function decryptWords(rk, rounds, a0, a1, a2, a3, out) {
153
174
  let s3 = a3 ^ rk[3];
154
175
  for (let round = 1; round < rounds; round++) {
155
176
  const at = round * WORDS_PER_BLOCK;
156
- const t0 = TD0[s0 >>> 24] ^ TD1[s3 >>> 16 & 255] ^ TD2[s2 >>> 8 & 255] ^ TD3[s1 & 255] ^ rk[at];
157
- const t1 = TD0[s1 >>> 24] ^ TD1[s0 >>> 16 & 255] ^ TD2[s3 >>> 8 & 255] ^ TD3[s2 & 255] ^ rk[at + 1];
158
- const t2 = TD0[s2 >>> 24] ^ TD1[s1 >>> 16 & 255] ^ TD2[s0 >>> 8 & 255] ^ TD3[s3 & 255] ^ rk[at + 2];
159
- const t3 = TD0[s3 >>> 24] ^ TD1[s2 >>> 16 & 255] ^ TD2[s1 >>> 8 & 255] ^ TD3[s0 & 255] ^ rk[at + 3];
177
+ const t0 = TD0[s0 >>> 24] ^ TD1[s3 >>> 16 & BYTE_MASK] ^ TD2[s2 >>> BITS_PER_BYTE & BYTE_MASK] ^ TD3[s1 & BYTE_MASK] ^ rk[at];
178
+ const t1 = TD0[s1 >>> 24] ^ TD1[s0 >>> 16 & BYTE_MASK] ^ TD2[s3 >>> BITS_PER_BYTE & BYTE_MASK] ^ TD3[s2 & BYTE_MASK] ^ rk[at + 1];
179
+ const t2 = TD0[s2 >>> 24] ^ TD1[s1 >>> 16 & BYTE_MASK] ^ TD2[s0 >>> BITS_PER_BYTE & BYTE_MASK] ^ TD3[s3 & BYTE_MASK] ^ rk[at + 2];
180
+ const t3 = TD0[s3 >>> 24] ^ TD1[s2 >>> 16 & BYTE_MASK] ^ TD2[s1 >>> BITS_PER_BYTE & BYTE_MASK] ^ TD3[s0 & BYTE_MASK] ^ rk[at + 3];
160
181
  s0 = t0;
161
182
  s1 = t1;
162
183
  s2 = t2;
163
184
  s3 = t3;
164
185
  }
165
186
  const at = rounds * WORDS_PER_BLOCK;
166
- out[0] = packWord(INV_SBOX[s0 >>> 24], INV_SBOX[s3 >>> 16 & 255], INV_SBOX[s2 >>> 8 & 255], INV_SBOX[s1 & 255]) ^ rk[at];
167
- out[1] = packWord(INV_SBOX[s1 >>> 24], INV_SBOX[s0 >>> 16 & 255], INV_SBOX[s3 >>> 8 & 255], INV_SBOX[s2 & 255]) ^ rk[at + 1];
168
- out[2] = packWord(INV_SBOX[s2 >>> 24], INV_SBOX[s1 >>> 16 & 255], INV_SBOX[s0 >>> 8 & 255], INV_SBOX[s3 & 255]) ^ rk[at + 2];
169
- out[3] = packWord(INV_SBOX[s3 >>> 24], INV_SBOX[s2 >>> 16 & 255], INV_SBOX[s1 >>> 8 & 255], INV_SBOX[s0 & 255]) ^ rk[at + 3];
187
+ out[0] = packWord(INV_SBOX[s0 >>> 24], INV_SBOX[s3 >>> 16 & BYTE_MASK], INV_SBOX[s2 >>> BITS_PER_BYTE & BYTE_MASK], INV_SBOX[s1 & BYTE_MASK]) ^ rk[at];
188
+ out[1] = packWord(INV_SBOX[s1 >>> 24], INV_SBOX[s0 >>> 16 & BYTE_MASK], INV_SBOX[s3 >>> BITS_PER_BYTE & BYTE_MASK], INV_SBOX[s2 & BYTE_MASK]) ^ rk[at + 1];
189
+ out[2] = packWord(INV_SBOX[s2 >>> 24], INV_SBOX[s1 >>> 16 & BYTE_MASK], INV_SBOX[s0 >>> BITS_PER_BYTE & BYTE_MASK], INV_SBOX[s3 & BYTE_MASK]) ^ rk[at + 2];
190
+ out[3] = packWord(INV_SBOX[s3 >>> 24], INV_SBOX[s2 >>> 16 & BYTE_MASK], INV_SBOX[s1 >>> BITS_PER_BYTE & BYTE_MASK], INV_SBOX[s0 & BYTE_MASK]) ^ rk[at + 3];
170
191
  }
171
192
  function wholeBlockCount(byteLength) {
172
193
  return Math.floor(byteLength / 16);
@@ -178,18 +199,18 @@ function aesCbcDecrypt(key, iv, data) {
178
199
  const out = new Uint8Array(blocks * 16);
179
200
  const plain = new Uint32Array(WORDS_PER_BLOCK);
180
201
  let c0 = readWord(iv, 0);
181
- let c1 = readWord(iv, 4);
202
+ let c1 = readWord(iv, WORD_BYTES);
182
203
  let c2 = readWord(iv, 8);
183
204
  let c3 = readWord(iv, 12);
184
205
  Array.from({ length: blocks }).forEach((_unused, b) => {
185
206
  const at = b * 16;
186
207
  const x0 = readWord(data, at);
187
- const x1 = readWord(data, at + 4);
208
+ const x1 = readWord(data, at + WORD_BYTES);
188
209
  const x2 = readWord(data, at + 8);
189
210
  const x3 = readWord(data, at + 12);
190
211
  decryptWords(inverseKey, expanded.rounds, x0, x1, x2, x3, plain);
191
212
  writeWord(out, at, plain[0] ^ c0);
192
- writeWord(out, at + 4, plain[1] ^ c1);
213
+ writeWord(out, at + WORD_BYTES, plain[1] ^ c1);
193
214
  writeWord(out, at + 8, plain[2] ^ c2);
194
215
  writeWord(out, at + 12, plain[3] ^ c3);
195
216
  c0 = x0;
@@ -205,18 +226,18 @@ function aesCbcEncrypt(key, iv, data) {
205
226
  const out = new Uint8Array(blocks * 16);
206
227
  const cipher = new Uint32Array(WORDS_PER_BLOCK);
207
228
  let c0 = readWord(iv, 0);
208
- let c1 = readWord(iv, 4);
229
+ let c1 = readWord(iv, WORD_BYTES);
209
230
  let c2 = readWord(iv, 8);
210
231
  let c3 = readWord(iv, 12);
211
232
  Array.from({ length: blocks }).forEach((_unused, b) => {
212
233
  const at = b * 16;
213
- encryptWords(expanded, readWord(data, at) ^ c0, readWord(data, at + 4) ^ c1, readWord(data, at + 8) ^ c2, readWord(data, at + 12) ^ c3, cipher);
234
+ encryptWords(expanded, readWord(data, at) ^ c0, readWord(data, at + WORD_BYTES) ^ c1, readWord(data, at + 8) ^ c2, readWord(data, at + 12) ^ c3, cipher);
214
235
  c0 = cipher[0];
215
236
  c1 = cipher[1];
216
237
  c2 = cipher[2];
217
238
  c3 = cipher[3];
218
239
  writeWord(out, at, c0);
219
- writeWord(out, at + 4, c1);
240
+ writeWord(out, at + WORD_BYTES, c1);
220
241
  writeWord(out, at + 8, c2);
221
242
  writeWord(out, at + 12, c3);
222
243
  });