pixel-react 1.15.21 → 1.15.23

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 (208) hide show
  1. package/lib/_virtual/aesCipherSuites.js +4 -0
  2. package/lib/_virtual/aesCipherSuites.js.map +1 -0
  3. package/lib/_virtual/asn1-validator.js +4 -0
  4. package/lib/_virtual/asn1-validator.js.map +1 -0
  5. package/lib/_virtual/asn1.js +4 -0
  6. package/lib/_virtual/asn1.js.map +1 -0
  7. package/lib/_virtual/cipherModes.js +4 -0
  8. package/lib/_virtual/cipherModes.js.map +1 -0
  9. package/lib/_virtual/hmac.js +4 -0
  10. package/lib/_virtual/hmac.js.map +1 -0
  11. package/lib/_virtual/index11.js +2 -2
  12. package/lib/_virtual/index12.js +2 -2
  13. package/lib/_virtual/index13.js +2 -2
  14. package/lib/_virtual/index14.js +4 -0
  15. package/lib/_virtual/index14.js.map +1 -0
  16. package/lib/_virtual/index2.js +5 -3
  17. package/lib/_virtual/index2.js.map +1 -1
  18. package/lib/_virtual/index3.js +4 -2
  19. package/lib/_virtual/index3.js.map +1 -1
  20. package/lib/_virtual/index4.js +2 -4
  21. package/lib/_virtual/index4.js.map +1 -1
  22. package/lib/_virtual/index5.js +4 -2
  23. package/lib/_virtual/index5.js.map +1 -1
  24. package/lib/_virtual/index6.js +2 -2
  25. package/lib/_virtual/index7.js +2 -6
  26. package/lib/_virtual/index7.js.map +1 -1
  27. package/lib/_virtual/index8.js +6 -2
  28. package/lib/_virtual/index8.js.map +1 -1
  29. package/lib/_virtual/index9.js +2 -2
  30. package/lib/_virtual/md5.js +4 -0
  31. package/lib/_virtual/md5.js.map +1 -0
  32. package/lib/_virtual/mgf1.js +4 -0
  33. package/lib/_virtual/mgf1.js.map +1 -0
  34. package/lib/_virtual/oids.js +4 -0
  35. package/lib/_virtual/oids.js.map +1 -0
  36. package/lib/_virtual/pem.js +4 -0
  37. package/lib/_virtual/pem.js.map +1 -0
  38. package/lib/_virtual/pkcs1.js +4 -0
  39. package/lib/_virtual/pkcs1.js.map +1 -0
  40. package/lib/_virtual/pkcs12.js +4 -0
  41. package/lib/_virtual/pkcs12.js.map +1 -0
  42. package/lib/_virtual/pkcs7.js +4 -0
  43. package/lib/_virtual/pkcs7.js.map +1 -0
  44. package/lib/_virtual/pkcs7asn1.js +4 -0
  45. package/lib/_virtual/pkcs7asn1.js.map +1 -0
  46. package/lib/_virtual/pki.js +4 -0
  47. package/lib/_virtual/pki.js.map +1 -0
  48. package/lib/_virtual/prime.js +4 -0
  49. package/lib/_virtual/prime.js.map +1 -0
  50. package/lib/_virtual/prng.js +4 -0
  51. package/lib/_virtual/prng.js.map +1 -0
  52. package/lib/_virtual/pss.js +4 -0
  53. package/lib/_virtual/pss.js.map +1 -0
  54. package/lib/_virtual/random.js +4 -0
  55. package/lib/_virtual/random.js.map +1 -0
  56. package/lib/_virtual/sha1.js +4 -0
  57. package/lib/_virtual/sha1.js.map +1 -0
  58. package/lib/_virtual/sha256.js +4 -0
  59. package/lib/_virtual/sha256.js.map +1 -0
  60. package/lib/_virtual/sha512.js +4 -0
  61. package/lib/_virtual/sha512.js.map +1 -0
  62. package/lib/_virtual/ssh.js +4 -0
  63. package/lib/_virtual/ssh.js.map +1 -0
  64. package/lib/_virtual/util.js +4 -0
  65. package/lib/_virtual/util.js.map +1 -0
  66. package/lib/_virtual/x509.js +4 -0
  67. package/lib/_virtual/x509.js.map +1 -0
  68. package/lib/assets/icons/testify_icon.svg.js +6 -0
  69. package/lib/assets/icons/testify_icon.svg.js.map +1 -0
  70. package/lib/assets/icons/testify_loader.svg.js +6 -0
  71. package/lib/assets/icons/testify_loader.svg.js.map +1 -0
  72. package/lib/assets/icons/testify_logo_name.svg.js +6 -0
  73. package/lib/assets/icons/testify_logo_name.svg.js.map +1 -0
  74. package/lib/components/AllProjectsDropdown/AllProjectsDropdown.js +3 -1
  75. package/lib/components/AllProjectsDropdown/AllProjectsDropdown.js.map +1 -1
  76. package/lib/components/Charts/DashboardDonutChart/DashboardDonutChart.js +2 -9
  77. package/lib/components/Charts/DashboardDonutChart/DashboardDonutChart.js.map +1 -1
  78. package/lib/components/Charts/DashboardDonutChart/types.d.ts +1 -1
  79. package/lib/components/ConditionalDropdown/ConditionalDropdown.js +2 -9
  80. package/lib/components/ConditionalDropdown/ConditionalDropdown.js.map +1 -1
  81. package/lib/components/Editor/Editor.js +37 -17
  82. package/lib/components/Editor/Editor.js.map +1 -1
  83. package/lib/components/Icon/iconList.js +6 -0
  84. package/lib/components/Icon/iconList.js.map +1 -1
  85. package/lib/components/MultiSelect/MultiSelect.js +9 -10
  86. package/lib/components/MultiSelect/MultiSelect.js.map +1 -1
  87. package/lib/components/PhoneInput/PhoneInput.js +1 -0
  88. package/lib/components/PhoneInput/PhoneInput.js.map +1 -1
  89. package/lib/components/SessionDropdown/SessionDropdown.js +4 -2
  90. package/lib/components/SessionDropdown/SessionDropdown.js.map +1 -1
  91. package/lib/index.cjs +27423 -379
  92. package/lib/index.cjs.map +1 -1
  93. package/lib/index.d.ts +1 -1
  94. package/lib/node_modules/classnames/index.js +1 -1
  95. package/lib/node_modules/input-format/modules/react/Input.js +1 -1
  96. package/lib/node_modules/js-beautify/js/index.js +1 -1
  97. package/lib/node_modules/js-beautify/js/src/html/beautifier.js +1 -1
  98. package/lib/node_modules/js-beautify/js/src/html/index.js +1 -1
  99. package/lib/node_modules/js-beautify/js/src/html/options.js +1 -1
  100. package/lib/node_modules/js-beautify/js/src/html/tokenizer.js +1 -1
  101. package/lib/node_modules/js-beautify/js/src/index.js +1 -1
  102. package/lib/node_modules/js-beautify/js/src/javascript/beautifier.js +1 -1
  103. package/lib/node_modules/js-beautify/js/src/javascript/index.js +1 -1
  104. package/lib/node_modules/js-beautify/js/src/javascript/options.js +1 -1
  105. package/lib/node_modules/js-beautify/js/src/javascript/tokenizer.js +1 -1
  106. package/lib/node_modules/node-forge/lib/aes.js +1014 -0
  107. package/lib/node_modules/node-forge/lib/aes.js.map +1 -0
  108. package/lib/node_modules/node-forge/lib/aesCipherSuites.js +286 -0
  109. package/lib/node_modules/node-forge/lib/aesCipherSuites.js.map +1 -0
  110. package/lib/node_modules/node-forge/lib/asn1-validator.js +100 -0
  111. package/lib/node_modules/node-forge/lib/asn1-validator.js.map +1 -0
  112. package/lib/node_modules/node-forge/lib/asn1.js +1379 -0
  113. package/lib/node_modules/node-forge/lib/asn1.js.map +1 -0
  114. package/lib/node_modules/node-forge/lib/baseN.js +181 -0
  115. package/lib/node_modules/node-forge/lib/baseN.js.map +1 -0
  116. package/lib/node_modules/node-forge/lib/cipher.js +236 -0
  117. package/lib/node_modules/node-forge/lib/cipher.js.map +1 -0
  118. package/lib/node_modules/node-forge/lib/cipherModes.js +936 -0
  119. package/lib/node_modules/node-forge/lib/cipherModes.js.map +1 -0
  120. package/lib/node_modules/node-forge/lib/des.js +467 -0
  121. package/lib/node_modules/node-forge/lib/des.js.map +1 -0
  122. package/lib/node_modules/node-forge/lib/ed25519.js +1108 -0
  123. package/lib/node_modules/node-forge/lib/ed25519.js.map +1 -0
  124. package/lib/node_modules/node-forge/lib/forge.js +23 -0
  125. package/lib/node_modules/node-forge/lib/forge.js.map +1 -0
  126. package/lib/node_modules/node-forge/lib/hmac.js +158 -0
  127. package/lib/node_modules/node-forge/lib/hmac.js.map +1 -0
  128. package/lib/node_modules/node-forge/lib/index.js +70 -0
  129. package/lib/node_modules/node-forge/lib/index.js.map +1 -0
  130. package/lib/node_modules/node-forge/lib/jsbn.js +1481 -0
  131. package/lib/node_modules/node-forge/lib/jsbn.js.map +1 -0
  132. package/lib/node_modules/node-forge/lib/kem.js +178 -0
  133. package/lib/node_modules/node-forge/lib/kem.js.map +1 -0
  134. package/lib/node_modules/node-forge/lib/log.js +325 -0
  135. package/lib/node_modules/node-forge/lib/log.js.map +1 -0
  136. package/lib/node_modules/node-forge/lib/md.all.js +28 -0
  137. package/lib/node_modules/node-forge/lib/md.all.js.map +1 -0
  138. package/lib/node_modules/node-forge/lib/md.js +22 -0
  139. package/lib/node_modules/node-forge/lib/md.js.map +1 -0
  140. package/lib/node_modules/node-forge/lib/md5.js +288 -0
  141. package/lib/node_modules/node-forge/lib/md5.js.map +1 -0
  142. package/lib/node_modules/node-forge/lib/mgf.js +24 -0
  143. package/lib/node_modules/node-forge/lib/mgf.js.map +1 -0
  144. package/lib/node_modules/node-forge/lib/mgf1.js +68 -0
  145. package/lib/node_modules/node-forge/lib/mgf1.js.map +1 -0
  146. package/lib/node_modules/node-forge/lib/oids.js +185 -0
  147. package/lib/node_modules/node-forge/lib/oids.js.map +1 -0
  148. package/lib/node_modules/node-forge/lib/pbe.js +966 -0
  149. package/lib/node_modules/node-forge/lib/pbe.js.map +1 -0
  150. package/lib/node_modules/node-forge/lib/pbkdf2.js +209 -0
  151. package/lib/node_modules/node-forge/lib/pbkdf2.js.map +1 -0
  152. package/lib/node_modules/node-forge/lib/pem.js +250 -0
  153. package/lib/node_modules/node-forge/lib/pem.js.map +1 -0
  154. package/lib/node_modules/node-forge/lib/pkcs1.js +273 -0
  155. package/lib/node_modules/node-forge/lib/pkcs1.js.map +1 -0
  156. package/lib/node_modules/node-forge/lib/pkcs12.js +980 -0
  157. package/lib/node_modules/node-forge/lib/pkcs12.js.map +1 -0
  158. package/lib/node_modules/node-forge/lib/pkcs7.js +1073 -0
  159. package/lib/node_modules/node-forge/lib/pkcs7.js.map +1 -0
  160. package/lib/node_modules/node-forge/lib/pkcs7asn1.js +415 -0
  161. package/lib/node_modules/node-forge/lib/pkcs7asn1.js.map +1 -0
  162. package/lib/node_modules/node-forge/lib/pki.js +125 -0
  163. package/lib/node_modules/node-forge/lib/pki.js.map +1 -0
  164. package/lib/node_modules/node-forge/lib/prime.js +297 -0
  165. package/lib/node_modules/node-forge/lib/prime.js.map +1 -0
  166. package/lib/node_modules/node-forge/lib/prng.js +433 -0
  167. package/lib/node_modules/node-forge/lib/prng.js.map +1 -0
  168. package/lib/node_modules/node-forge/lib/pss.js +246 -0
  169. package/lib/node_modules/node-forge/lib/pss.js.map +1 -0
  170. package/lib/node_modules/node-forge/lib/random.js +191 -0
  171. package/lib/node_modules/node-forge/lib/random.js.map +1 -0
  172. package/lib/node_modules/node-forge/lib/rc2.js +382 -0
  173. package/lib/node_modules/node-forge/lib/rc2.js.map +1 -0
  174. package/lib/node_modules/node-forge/lib/rsa.js +1815 -0
  175. package/lib/node_modules/node-forge/lib/rsa.js.map +1 -0
  176. package/lib/node_modules/node-forge/lib/sha1.js +325 -0
  177. package/lib/node_modules/node-forge/lib/sha1.js.map +1 -0
  178. package/lib/node_modules/node-forge/lib/sha256.js +306 -0
  179. package/lib/node_modules/node-forge/lib/sha256.js.map +1 -0
  180. package/lib/node_modules/node-forge/lib/sha512.js +479 -0
  181. package/lib/node_modules/node-forge/lib/sha512.js.map +1 -0
  182. package/lib/node_modules/node-forge/lib/ssh.js +244 -0
  183. package/lib/node_modules/node-forge/lib/ssh.js.map +1 -0
  184. package/lib/node_modules/node-forge/lib/tls.js +4207 -0
  185. package/lib/node_modules/node-forge/lib/tls.js.map +1 -0
  186. package/lib/node_modules/node-forge/lib/util.js +2565 -0
  187. package/lib/node_modules/node-forge/lib/util.js.map +1 -0
  188. package/lib/node_modules/node-forge/lib/x509.js +2986 -0
  189. package/lib/node_modules/node-forge/lib/x509.js.map +1 -0
  190. package/lib/node_modules/prop-types/index.js +1 -1
  191. package/lib/node_modules/react-async-script/lib/esm/async-script-loader.js +1 -1
  192. package/lib/node_modules/react-google-recaptcha/lib/esm/recaptcha.js +1 -1
  193. package/lib/node_modules/react-is/index.js +1 -1
  194. package/lib/node_modules/react-phone-number-input/modules/CountryIcon.js +1 -1
  195. package/lib/node_modules/react-phone-number-input/modules/CountrySelect.js +1 -1
  196. package/lib/node_modules/react-phone-number-input/modules/Flag.js +1 -1
  197. package/lib/node_modules/react-phone-number-input/modules/InputBasic.js +1 -1
  198. package/lib/node_modules/react-phone-number-input/modules/InputSmart.js +1 -1
  199. package/lib/node_modules/react-phone-number-input/modules/InternationalIcon.js +1 -1
  200. package/lib/node_modules/react-phone-number-input/modules/PhoneInputWithCountry.js +1 -1
  201. package/lib/node_modules/react-phone-number-input/modules/PropTypes.js +1 -1
  202. package/lib/node_modules/scheduler/index.js +1 -1
  203. package/lib/node_modules/use-context-selector/dist/index.js +1 -1
  204. package/lib/styles.css +1 -1
  205. package/lib/styles.css.map +1 -1
  206. package/lib/utils/getEncryptedData/getEncryptedData.js +10 -13
  207. package/lib/utils/getEncryptedData/getEncryptedData.js.map +1 -1
  208. package/package.json +3 -1
@@ -0,0 +1,1014 @@
1
+ import { __require as requireForge } from './forge.js';
2
+ import { __require as requireCipher } from './cipher.js';
3
+ import { __require as requireCipherModes } from './cipherModes.js';
4
+ import { __require as requireUtil } from './util.js';
5
+
6
+ /**
7
+ * Advanced Encryption Standard (AES) implementation.
8
+ *
9
+ * This implementation is based on the public domain library 'jscrypto' which
10
+ * was written by:
11
+ *
12
+ * Emily Stark (estark@stanford.edu)
13
+ * Mike Hamburg (mhamburg@stanford.edu)
14
+ * Dan Boneh (dabo@cs.stanford.edu)
15
+ *
16
+ * Parts of this code are based on the OpenSSL implementation of AES:
17
+ * http://www.openssl.org
18
+ *
19
+ * @author Dave Longley
20
+ *
21
+ * Copyright (c) 2010-2014 Digital Bazaar, Inc.
22
+ */
23
+ var aes;
24
+ var hasRequiredAes;
25
+ function requireAes() {
26
+ if (hasRequiredAes) return aes;
27
+ hasRequiredAes = 1;
28
+ var forge = requireForge();
29
+ requireCipher();
30
+ requireCipherModes();
31
+ requireUtil();
32
+
33
+ /* AES API */
34
+ aes = forge.aes = forge.aes || {};
35
+
36
+ /**
37
+ * Deprecated. Instead, use:
38
+ *
39
+ * var cipher = forge.cipher.createCipher('AES-<mode>', key);
40
+ * cipher.start({iv: iv});
41
+ *
42
+ * Creates an AES cipher object to encrypt data using the given symmetric key.
43
+ * The output will be stored in the 'output' member of the returned cipher.
44
+ *
45
+ * The key and iv may be given as a string of bytes, an array of bytes,
46
+ * a byte buffer, or an array of 32-bit words.
47
+ *
48
+ * @param key the symmetric key to use.
49
+ * @param iv the initialization vector to use.
50
+ * @param output the buffer to write to, null to create one.
51
+ * @param mode the cipher mode to use (default: 'CBC').
52
+ *
53
+ * @return the cipher.
54
+ */
55
+ forge.aes.startEncrypting = function (key, iv, output, mode) {
56
+ var cipher = _createCipher({
57
+ key: key,
58
+ output: output,
59
+ decrypt: false,
60
+ mode: mode
61
+ });
62
+ cipher.start(iv);
63
+ return cipher;
64
+ };
65
+
66
+ /**
67
+ * Deprecated. Instead, use:
68
+ *
69
+ * var cipher = forge.cipher.createCipher('AES-<mode>', key);
70
+ *
71
+ * Creates an AES cipher object to encrypt data using the given symmetric key.
72
+ *
73
+ * The key may be given as a string of bytes, an array of bytes, a
74
+ * byte buffer, or an array of 32-bit words.
75
+ *
76
+ * @param key the symmetric key to use.
77
+ * @param mode the cipher mode to use (default: 'CBC').
78
+ *
79
+ * @return the cipher.
80
+ */
81
+ forge.aes.createEncryptionCipher = function (key, mode) {
82
+ return _createCipher({
83
+ key: key,
84
+ output: null,
85
+ decrypt: false,
86
+ mode: mode
87
+ });
88
+ };
89
+
90
+ /**
91
+ * Deprecated. Instead, use:
92
+ *
93
+ * var decipher = forge.cipher.createDecipher('AES-<mode>', key);
94
+ * decipher.start({iv: iv});
95
+ *
96
+ * Creates an AES cipher object to decrypt data using the given symmetric key.
97
+ * The output will be stored in the 'output' member of the returned cipher.
98
+ *
99
+ * The key and iv may be given as a string of bytes, an array of bytes,
100
+ * a byte buffer, or an array of 32-bit words.
101
+ *
102
+ * @param key the symmetric key to use.
103
+ * @param iv the initialization vector to use.
104
+ * @param output the buffer to write to, null to create one.
105
+ * @param mode the cipher mode to use (default: 'CBC').
106
+ *
107
+ * @return the cipher.
108
+ */
109
+ forge.aes.startDecrypting = function (key, iv, output, mode) {
110
+ var cipher = _createCipher({
111
+ key: key,
112
+ output: output,
113
+ decrypt: true,
114
+ mode: mode
115
+ });
116
+ cipher.start(iv);
117
+ return cipher;
118
+ };
119
+
120
+ /**
121
+ * Deprecated. Instead, use:
122
+ *
123
+ * var decipher = forge.cipher.createDecipher('AES-<mode>', key);
124
+ *
125
+ * Creates an AES cipher object to decrypt data using the given symmetric key.
126
+ *
127
+ * The key may be given as a string of bytes, an array of bytes, a
128
+ * byte buffer, or an array of 32-bit words.
129
+ *
130
+ * @param key the symmetric key to use.
131
+ * @param mode the cipher mode to use (default: 'CBC').
132
+ *
133
+ * @return the cipher.
134
+ */
135
+ forge.aes.createDecryptionCipher = function (key, mode) {
136
+ return _createCipher({
137
+ key: key,
138
+ output: null,
139
+ decrypt: true,
140
+ mode: mode
141
+ });
142
+ };
143
+
144
+ /**
145
+ * Creates a new AES cipher algorithm object.
146
+ *
147
+ * @param name the name of the algorithm.
148
+ * @param mode the mode factory function.
149
+ *
150
+ * @return the AES algorithm object.
151
+ */
152
+ forge.aes.Algorithm = function (name, mode) {
153
+ if (!init) {
154
+ initialize();
155
+ }
156
+ var self = this;
157
+ self.name = name;
158
+ self.mode = new mode({
159
+ blockSize: 16,
160
+ cipher: {
161
+ encrypt: function (inBlock, outBlock) {
162
+ return _updateBlock(self._w, inBlock, outBlock, false);
163
+ },
164
+ decrypt: function (inBlock, outBlock) {
165
+ return _updateBlock(self._w, inBlock, outBlock, true);
166
+ }
167
+ }
168
+ });
169
+ self._init = false;
170
+ };
171
+
172
+ /**
173
+ * Initializes this AES algorithm by expanding its key.
174
+ *
175
+ * @param options the options to use.
176
+ * key the key to use with this algorithm.
177
+ * decrypt true if the algorithm should be initialized for decryption,
178
+ * false for encryption.
179
+ */
180
+ forge.aes.Algorithm.prototype.initialize = function (options) {
181
+ if (this._init) {
182
+ return;
183
+ }
184
+ var key = options.key;
185
+ var tmp;
186
+
187
+ /* Note: The key may be a string of bytes, an array of bytes, a byte
188
+ buffer, or an array of 32-bit integers. If the key is in bytes, then
189
+ it must be 16, 24, or 32 bytes in length. If it is in 32-bit
190
+ integers, it must be 4, 6, or 8 integers long. */
191
+
192
+ if (typeof key === 'string' && (key.length === 16 || key.length === 24 || key.length === 32)) {
193
+ // convert key string into byte buffer
194
+ key = forge.util.createBuffer(key);
195
+ } else if (forge.util.isArray(key) && (key.length === 16 || key.length === 24 || key.length === 32)) {
196
+ // convert key integer array into byte buffer
197
+ tmp = key;
198
+ key = forge.util.createBuffer();
199
+ for (var i = 0; i < tmp.length; ++i) {
200
+ key.putByte(tmp[i]);
201
+ }
202
+ }
203
+
204
+ // convert key byte buffer into 32-bit integer array
205
+ if (!forge.util.isArray(key)) {
206
+ tmp = key;
207
+ key = [];
208
+
209
+ // key lengths of 16, 24, 32 bytes allowed
210
+ var len = tmp.length();
211
+ if (len === 16 || len === 24 || len === 32) {
212
+ len = len >>> 2;
213
+ for (var i = 0; i < len; ++i) {
214
+ key.push(tmp.getInt32());
215
+ }
216
+ }
217
+ }
218
+
219
+ // key must be an array of 32-bit integers by now
220
+ if (!forge.util.isArray(key) || !(key.length === 4 || key.length === 6 || key.length === 8)) {
221
+ throw new Error('Invalid key parameter.');
222
+ }
223
+
224
+ // encryption operation is always used for these modes
225
+ var mode = this.mode.name;
226
+ var encryptOp = ['CFB', 'OFB', 'CTR', 'GCM'].indexOf(mode) !== -1;
227
+
228
+ // do key expansion
229
+ this._w = _expandKey(key, options.decrypt && !encryptOp);
230
+ this._init = true;
231
+ };
232
+
233
+ /**
234
+ * Expands a key. Typically only used for testing.
235
+ *
236
+ * @param key the symmetric key to expand, as an array of 32-bit words.
237
+ * @param decrypt true to expand for decryption, false for encryption.
238
+ *
239
+ * @return the expanded key.
240
+ */
241
+ forge.aes._expandKey = function (key, decrypt) {
242
+ if (!init) {
243
+ initialize();
244
+ }
245
+ return _expandKey(key, decrypt);
246
+ };
247
+
248
+ /**
249
+ * Updates a single block. Typically only used for testing.
250
+ *
251
+ * @param w the expanded key to use.
252
+ * @param input an array of block-size 32-bit words.
253
+ * @param output an array of block-size 32-bit words.
254
+ * @param decrypt true to decrypt, false to encrypt.
255
+ */
256
+ forge.aes._updateBlock = _updateBlock;
257
+
258
+ /** Register AES algorithms **/
259
+
260
+ registerAlgorithm('AES-ECB', forge.cipher.modes.ecb);
261
+ registerAlgorithm('AES-CBC', forge.cipher.modes.cbc);
262
+ registerAlgorithm('AES-CFB', forge.cipher.modes.cfb);
263
+ registerAlgorithm('AES-OFB', forge.cipher.modes.ofb);
264
+ registerAlgorithm('AES-CTR', forge.cipher.modes.ctr);
265
+ registerAlgorithm('AES-GCM', forge.cipher.modes.gcm);
266
+ function registerAlgorithm(name, mode) {
267
+ var factory = function () {
268
+ return new forge.aes.Algorithm(name, mode);
269
+ };
270
+ forge.cipher.registerAlgorithm(name, factory);
271
+ }
272
+
273
+ /** AES implementation **/
274
+
275
+ var init = false; // not yet initialized
276
+ var Nb = 4; // number of words comprising the state (AES = 4)
277
+ var sbox; // non-linear substitution table used in key expansion
278
+ var isbox; // inversion of sbox
279
+ var rcon; // round constant word array
280
+ var mix; // mix-columns table
281
+ var imix; // inverse mix-columns table
282
+
283
+ /**
284
+ * Performs initialization, ie: precomputes tables to optimize for speed.
285
+ *
286
+ * One way to understand how AES works is to imagine that 'addition' and
287
+ * 'multiplication' are interfaces that require certain mathematical
288
+ * properties to hold true (ie: they are associative) but they might have
289
+ * different implementations and produce different kinds of results ...
290
+ * provided that their mathematical properties remain true. AES defines
291
+ * its own methods of addition and multiplication but keeps some important
292
+ * properties the same, ie: associativity and distributivity. The
293
+ * explanation below tries to shed some light on how AES defines addition
294
+ * and multiplication of bytes and 32-bit words in order to perform its
295
+ * encryption and decryption algorithms.
296
+ *
297
+ * The basics:
298
+ *
299
+ * The AES algorithm views bytes as binary representations of polynomials
300
+ * that have either 1 or 0 as the coefficients. It defines the addition
301
+ * or subtraction of two bytes as the XOR operation. It also defines the
302
+ * multiplication of two bytes as a finite field referred to as GF(2^8)
303
+ * (Note: 'GF' means "Galois Field" which is a field that contains a finite
304
+ * number of elements so GF(2^8) has 256 elements).
305
+ *
306
+ * This means that any two bytes can be represented as binary polynomials;
307
+ * when they multiplied together and modularly reduced by an irreducible
308
+ * polynomial of the 8th degree, the results are the field GF(2^8). The
309
+ * specific irreducible polynomial that AES uses in hexadecimal is 0x11b.
310
+ * This multiplication is associative with 0x01 as the identity:
311
+ *
312
+ * (b * 0x01 = GF(b, 0x01) = b).
313
+ *
314
+ * The operation GF(b, 0x02) can be performed at the byte level by left
315
+ * shifting b once and then XOR'ing it (to perform the modular reduction)
316
+ * with 0x11b if b is >= 128. Repeated application of the multiplication
317
+ * of 0x02 can be used to implement the multiplication of any two bytes.
318
+ *
319
+ * For instance, multiplying 0x57 and 0x13, denoted as GF(0x57, 0x13), can
320
+ * be performed by factoring 0x13 into 0x01, 0x02, and 0x10. Then these
321
+ * factors can each be multiplied by 0x57 and then added together. To do
322
+ * the multiplication, values for 0x57 multiplied by each of these 3 factors
323
+ * can be precomputed and stored in a table. To add them, the values from
324
+ * the table are XOR'd together.
325
+ *
326
+ * AES also defines addition and multiplication of words, that is 4-byte
327
+ * numbers represented as polynomials of 3 degrees where the coefficients
328
+ * are the values of the bytes.
329
+ *
330
+ * The word [a0, a1, a2, a3] is a polynomial a3x^3 + a2x^2 + a1x + a0.
331
+ *
332
+ * Addition is performed by XOR'ing like powers of x. Multiplication
333
+ * is performed in two steps, the first is an algebriac expansion as
334
+ * you would do normally (where addition is XOR). But the result is
335
+ * a polynomial larger than 3 degrees and thus it cannot fit in a word. So
336
+ * next the result is modularly reduced by an AES-specific polynomial of
337
+ * degree 4 which will always produce a polynomial of less than 4 degrees
338
+ * such that it will fit in a word. In AES, this polynomial is x^4 + 1.
339
+ *
340
+ * The modular product of two polynomials 'a' and 'b' is thus:
341
+ *
342
+ * d(x) = d3x^3 + d2x^2 + d1x + d0
343
+ * with
344
+ * d0 = GF(a0, b0) ^ GF(a3, b1) ^ GF(a2, b2) ^ GF(a1, b3)
345
+ * d1 = GF(a1, b0) ^ GF(a0, b1) ^ GF(a3, b2) ^ GF(a2, b3)
346
+ * d2 = GF(a2, b0) ^ GF(a1, b1) ^ GF(a0, b2) ^ GF(a3, b3)
347
+ * d3 = GF(a3, b0) ^ GF(a2, b1) ^ GF(a1, b2) ^ GF(a0, b3)
348
+ *
349
+ * As a matrix:
350
+ *
351
+ * [d0] = [a0 a3 a2 a1][b0]
352
+ * [d1] [a1 a0 a3 a2][b1]
353
+ * [d2] [a2 a1 a0 a3][b2]
354
+ * [d3] [a3 a2 a1 a0][b3]
355
+ *
356
+ * Special polynomials defined by AES (0x02 == {02}):
357
+ * a(x) = {03}x^3 + {01}x^2 + {01}x + {02}
358
+ * a^-1(x) = {0b}x^3 + {0d}x^2 + {09}x + {0e}.
359
+ *
360
+ * These polynomials are used in the MixColumns() and InverseMixColumns()
361
+ * operations, respectively, to cause each element in the state to affect
362
+ * the output (referred to as diffusing).
363
+ *
364
+ * RotWord() uses: a0 = a1 = a2 = {00} and a3 = {01}, which is the
365
+ * polynomial x3.
366
+ *
367
+ * The ShiftRows() method modifies the last 3 rows in the state (where
368
+ * the state is 4 words with 4 bytes per word) by shifting bytes cyclically.
369
+ * The 1st byte in the second row is moved to the end of the row. The 1st
370
+ * and 2nd bytes in the third row are moved to the end of the row. The 1st,
371
+ * 2nd, and 3rd bytes are moved in the fourth row.
372
+ *
373
+ * More details on how AES arithmetic works:
374
+ *
375
+ * In the polynomial representation of binary numbers, XOR performs addition
376
+ * and subtraction and multiplication in GF(2^8) denoted as GF(a, b)
377
+ * corresponds with the multiplication of polynomials modulo an irreducible
378
+ * polynomial of degree 8. In other words, for AES, GF(a, b) will multiply
379
+ * polynomial 'a' with polynomial 'b' and then do a modular reduction by
380
+ * an AES-specific irreducible polynomial of degree 8.
381
+ *
382
+ * A polynomial is irreducible if its only divisors are one and itself. For
383
+ * the AES algorithm, this irreducible polynomial is:
384
+ *
385
+ * m(x) = x^8 + x^4 + x^3 + x + 1,
386
+ *
387
+ * or {01}{1b} in hexadecimal notation, where each coefficient is a bit:
388
+ * 100011011 = 283 = 0x11b.
389
+ *
390
+ * For example, GF(0x57, 0x83) = 0xc1 because
391
+ *
392
+ * 0x57 = 87 = 01010111 = x^6 + x^4 + x^2 + x + 1
393
+ * 0x85 = 131 = 10000101 = x^7 + x + 1
394
+ *
395
+ * (x^6 + x^4 + x^2 + x + 1) * (x^7 + x + 1)
396
+ * = x^13 + x^11 + x^9 + x^8 + x^7 +
397
+ * x^7 + x^5 + x^3 + x^2 + x +
398
+ * x^6 + x^4 + x^2 + x + 1
399
+ * = x^13 + x^11 + x^9 + x^8 + x^6 + x^5 + x^4 + x^3 + 1 = y
400
+ * y modulo (x^8 + x^4 + x^3 + x + 1)
401
+ * = x^7 + x^6 + 1.
402
+ *
403
+ * The modular reduction by m(x) guarantees the result will be a binary
404
+ * polynomial of less than degree 8, so that it can fit in a byte.
405
+ *
406
+ * The operation to multiply a binary polynomial b with x (the polynomial
407
+ * x in binary representation is 00000010) is:
408
+ *
409
+ * b_7x^8 + b_6x^7 + b_5x^6 + b_4x^5 + b_3x^4 + b_2x^3 + b_1x^2 + b_0x^1
410
+ *
411
+ * To get GF(b, x) we must reduce that by m(x). If b_7 is 0 (that is the
412
+ * most significant bit is 0 in b) then the result is already reduced. If
413
+ * it is 1, then we can reduce it by subtracting m(x) via an XOR.
414
+ *
415
+ * It follows that multiplication by x (00000010 or 0x02) can be implemented
416
+ * by performing a left shift followed by a conditional bitwise XOR with
417
+ * 0x1b. This operation on bytes is denoted by xtime(). Multiplication by
418
+ * higher powers of x can be implemented by repeated application of xtime().
419
+ *
420
+ * By adding intermediate results, multiplication by any constant can be
421
+ * implemented. For instance:
422
+ *
423
+ * GF(0x57, 0x13) = 0xfe because:
424
+ *
425
+ * xtime(b) = (b & 128) ? (b << 1 ^ 0x11b) : (b << 1)
426
+ *
427
+ * Note: We XOR with 0x11b instead of 0x1b because in javascript our
428
+ * datatype for b can be larger than 1 byte, so a left shift will not
429
+ * automatically eliminate bits that overflow a byte ... by XOR'ing the
430
+ * overflow bit with 1 (the extra one from 0x11b) we zero it out.
431
+ *
432
+ * GF(0x57, 0x02) = xtime(0x57) = 0xae
433
+ * GF(0x57, 0x04) = xtime(0xae) = 0x47
434
+ * GF(0x57, 0x08) = xtime(0x47) = 0x8e
435
+ * GF(0x57, 0x10) = xtime(0x8e) = 0x07
436
+ *
437
+ * GF(0x57, 0x13) = GF(0x57, (0x01 ^ 0x02 ^ 0x10))
438
+ *
439
+ * And by the distributive property (since XOR is addition and GF() is
440
+ * multiplication):
441
+ *
442
+ * = GF(0x57, 0x01) ^ GF(0x57, 0x02) ^ GF(0x57, 0x10)
443
+ * = 0x57 ^ 0xae ^ 0x07
444
+ * = 0xfe.
445
+ */
446
+ function initialize() {
447
+ init = true;
448
+
449
+ /* Populate the Rcon table. These are the values given by
450
+ [x^(i-1),{00},{00},{00}] where x^(i-1) are powers of x (and x = 0x02)
451
+ in the field of GF(2^8), where i starts at 1.
452
+ rcon[0] = [0x00, 0x00, 0x00, 0x00]
453
+ rcon[1] = [0x01, 0x00, 0x00, 0x00] 2^(1-1) = 2^0 = 1
454
+ rcon[2] = [0x02, 0x00, 0x00, 0x00] 2^(2-1) = 2^1 = 2
455
+ ...
456
+ rcon[9] = [0x1B, 0x00, 0x00, 0x00] 2^(9-1) = 2^8 = 0x1B
457
+ rcon[10] = [0x36, 0x00, 0x00, 0x00] 2^(10-1) = 2^9 = 0x36
458
+ We only store the first byte because it is the only one used.
459
+ */
460
+ rcon = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1B, 0x36];
461
+
462
+ // compute xtime table which maps i onto GF(i, 0x02)
463
+ var xtime = new Array(256);
464
+ for (var i = 0; i < 128; ++i) {
465
+ xtime[i] = i << 1;
466
+ xtime[i + 128] = i + 128 << 1 ^ 0x11B;
467
+ }
468
+
469
+ // compute all other tables
470
+ sbox = new Array(256);
471
+ isbox = new Array(256);
472
+ mix = new Array(4);
473
+ imix = new Array(4);
474
+ for (var i = 0; i < 4; ++i) {
475
+ mix[i] = new Array(256);
476
+ imix[i] = new Array(256);
477
+ }
478
+ var e = 0,
479
+ ei = 0,
480
+ e2,
481
+ e4,
482
+ e8,
483
+ sx,
484
+ sx2,
485
+ me,
486
+ ime;
487
+ for (var i = 0; i < 256; ++i) {
488
+ /* We need to generate the SubBytes() sbox and isbox tables so that
489
+ we can perform byte substitutions. This requires us to traverse
490
+ all of the elements in GF, find their multiplicative inverses,
491
+ and apply to each the following affine transformation:
492
+ bi' = bi ^ b(i + 4) mod 8 ^ b(i + 5) mod 8 ^ b(i + 6) mod 8 ^
493
+ b(i + 7) mod 8 ^ ci
494
+ for 0 <= i < 8, where bi is the ith bit of the byte, and ci is the
495
+ ith bit of a byte c with the value {63} or {01100011}.
496
+ It is possible to traverse every possible value in a Galois field
497
+ using what is referred to as a 'generator'. There are many
498
+ generators (128 out of 256): 3,5,6,9,11,82 to name a few. To fully
499
+ traverse GF we iterate 255 times, multiplying by our generator
500
+ each time.
501
+ On each iteration we can determine the multiplicative inverse for
502
+ the current element.
503
+ Suppose there is an element in GF 'e'. For a given generator 'g',
504
+ e = g^x. The multiplicative inverse of e is g^(255 - x). It turns
505
+ out that if use the inverse of a generator as another generator
506
+ it will produce all of the corresponding multiplicative inverses
507
+ at the same time. For this reason, we choose 5 as our inverse
508
+ generator because it only requires 2 multiplies and 1 add and its
509
+ inverse, 82, requires relatively few operations as well.
510
+ In order to apply the affine transformation, the multiplicative
511
+ inverse 'ei' of 'e' can be repeatedly XOR'd (4 times) with a
512
+ bit-cycling of 'ei'. To do this 'ei' is first stored in 's' and
513
+ 'x'. Then 's' is left shifted and the high bit of 's' is made the
514
+ low bit. The resulting value is stored in 's'. Then 'x' is XOR'd
515
+ with 's' and stored in 'x'. On each subsequent iteration the same
516
+ operation is performed. When 4 iterations are complete, 'x' is
517
+ XOR'd with 'c' (0x63) and the transformed value is stored in 'x'.
518
+ For example:
519
+ s = 01000001
520
+ x = 01000001
521
+ iteration 1: s = 10000010, x ^= s
522
+ iteration 2: s = 00000101, x ^= s
523
+ iteration 3: s = 00001010, x ^= s
524
+ iteration 4: s = 00010100, x ^= s
525
+ x ^= 0x63
526
+ This can be done with a loop where s = (s << 1) | (s >> 7). However,
527
+ it can also be done by using a single 16-bit (in this case 32-bit)
528
+ number 'sx'. Since XOR is an associative operation, we can set 'sx'
529
+ to 'ei' and then XOR it with 'sx' left-shifted 1,2,3, and 4 times.
530
+ The most significant bits will flow into the high 8 bit positions
531
+ and be correctly XOR'd with one another. All that remains will be
532
+ to cycle the high 8 bits by XOR'ing them all with the lower 8 bits
533
+ afterwards.
534
+ At the same time we're populating sbox and isbox we can precompute
535
+ the multiplication we'll need to do to do MixColumns() later.
536
+ */
537
+
538
+ // apply affine transformation
539
+ sx = ei ^ ei << 1 ^ ei << 2 ^ ei << 3 ^ ei << 4;
540
+ sx = sx >> 8 ^ sx & 255 ^ 0x63;
541
+
542
+ // update tables
543
+ sbox[e] = sx;
544
+ isbox[sx] = e;
545
+
546
+ /* Mixing columns is done using matrix multiplication. The columns
547
+ that are to be mixed are each a single word in the current state.
548
+ The state has Nb columns (4 columns). Therefore each column is a
549
+ 4 byte word. So to mix the columns in a single column 'c' where
550
+ its rows are r0, r1, r2, and r3, we use the following matrix
551
+ multiplication:
552
+ [2 3 1 1]*[r0,c]=[r'0,c]
553
+ [1 2 3 1] [r1,c] [r'1,c]
554
+ [1 1 2 3] [r2,c] [r'2,c]
555
+ [3 1 1 2] [r3,c] [r'3,c]
556
+ r0, r1, r2, and r3 are each 1 byte of one of the words in the
557
+ state (a column). To do matrix multiplication for each mixed
558
+ column c' we multiply the corresponding row from the left matrix
559
+ with the corresponding column from the right matrix. In total, we
560
+ get 4 equations:
561
+ r0,c' = 2*r0,c + 3*r1,c + 1*r2,c + 1*r3,c
562
+ r1,c' = 1*r0,c + 2*r1,c + 3*r2,c + 1*r3,c
563
+ r2,c' = 1*r0,c + 1*r1,c + 2*r2,c + 3*r3,c
564
+ r3,c' = 3*r0,c + 1*r1,c + 1*r2,c + 2*r3,c
565
+ As usual, the multiplication is as previously defined and the
566
+ addition is XOR. In order to optimize mixing columns we can store
567
+ the multiplication results in tables. If you think of the whole
568
+ column as a word (it might help to visualize by mentally rotating
569
+ the equations above by counterclockwise 90 degrees) then you can
570
+ see that it would be useful to map the multiplications performed on
571
+ each byte (r0, r1, r2, r3) onto a word as well. For instance, we
572
+ could map 2*r0,1*r0,1*r0,3*r0 onto a word by storing 2*r0 in the
573
+ highest 8 bits and 3*r0 in the lowest 8 bits (with the other two
574
+ respectively in the middle). This means that a table can be
575
+ constructed that uses r0 as an index to the word. We can do the
576
+ same with r1, r2, and r3, creating a total of 4 tables.
577
+ To construct a full c', we can just look up each byte of c in
578
+ their respective tables and XOR the results together.
579
+ Also, to build each table we only have to calculate the word
580
+ for 2,1,1,3 for every byte ... which we can do on each iteration
581
+ of this loop since we will iterate over every byte. After we have
582
+ calculated 2,1,1,3 we can get the results for the other tables
583
+ by cycling the byte at the end to the beginning. For instance
584
+ we can take the result of table 2,1,1,3 and produce table 3,2,1,1
585
+ by moving the right most byte to the left most position just like
586
+ how you can imagine the 3 moved out of 2,1,1,3 and to the front
587
+ to produce 3,2,1,1.
588
+ There is another optimization in that the same multiples of
589
+ the current element we need in order to advance our generator
590
+ to the next iteration can be reused in performing the 2,1,1,3
591
+ calculation. We also calculate the inverse mix column tables,
592
+ with e,9,d,b being the inverse of 2,1,1,3.
593
+ When we're done, and we need to actually mix columns, the first
594
+ byte of each state word should be put through mix[0] (2,1,1,3),
595
+ the second through mix[1] (3,2,1,1) and so forth. Then they should
596
+ be XOR'd together to produce the fully mixed column.
597
+ */
598
+
599
+ // calculate mix and imix table values
600
+ sx2 = xtime[sx];
601
+ e2 = xtime[e];
602
+ e4 = xtime[e2];
603
+ e8 = xtime[e4];
604
+ me = sx2 << 24 ^
605
+ // 2
606
+ sx << 16 ^
607
+ // 1
608
+ sx << 8 ^ (
609
+ // 1
610
+ sx ^ sx2); // 3
611
+ ime = (e2 ^ e4 ^ e8) << 24 ^
612
+ // E (14)
613
+ (e ^ e8) << 16 ^
614
+ // 9
615
+ (e ^ e4 ^ e8) << 8 ^ (
616
+ // D (13)
617
+ e ^ e2 ^ e8); // B (11)
618
+ // produce each of the mix tables by rotating the 2,1,1,3 value
619
+ for (var n = 0; n < 4; ++n) {
620
+ mix[n][e] = me;
621
+ imix[n][sx] = ime;
622
+ // cycle the right most byte to the left most position
623
+ // ie: 2,1,1,3 becomes 3,2,1,1
624
+ me = me << 24 | me >>> 8;
625
+ ime = ime << 24 | ime >>> 8;
626
+ }
627
+
628
+ // get next element and inverse
629
+ if (e === 0) {
630
+ // 1 is the inverse of 1
631
+ e = ei = 1;
632
+ } else {
633
+ // e = 2e + 2*2*2*(10e)) = multiply e by 82 (chosen generator)
634
+ // ei = ei + 2*2*ei = multiply ei by 5 (inverse generator)
635
+ e = e2 ^ xtime[xtime[xtime[e2 ^ e8]]];
636
+ ei ^= xtime[xtime[ei]];
637
+ }
638
+ }
639
+ }
640
+
641
+ /**
642
+ * Generates a key schedule using the AES key expansion algorithm.
643
+ *
644
+ * The AES algorithm takes the Cipher Key, K, and performs a Key Expansion
645
+ * routine to generate a key schedule. The Key Expansion generates a total
646
+ * of Nb*(Nr + 1) words: the algorithm requires an initial set of Nb words,
647
+ * and each of the Nr rounds requires Nb words of key data. The resulting
648
+ * key schedule consists of a linear array of 4-byte words, denoted [wi ],
649
+ * with i in the range 0 <= i < Nb(Nr + 1).
650
+ *
651
+ * KeyExpansion(byte key[4*Nk], word w[Nb*(Nr+1)], Nk)
652
+ * AES-128 (Nb=4, Nk=4, Nr=10)
653
+ * AES-192 (Nb=4, Nk=6, Nr=12)
654
+ * AES-256 (Nb=4, Nk=8, Nr=14)
655
+ * Note: Nr=Nk+6.
656
+ *
657
+ * Nb is the number of columns (32-bit words) comprising the State (or
658
+ * number of bytes in a block). For AES, Nb=4.
659
+ *
660
+ * @param key the key to schedule (as an array of 32-bit words).
661
+ * @param decrypt true to modify the key schedule to decrypt, false not to.
662
+ *
663
+ * @return the generated key schedule.
664
+ */
665
+ function _expandKey(key, decrypt) {
666
+ // copy the key's words to initialize the key schedule
667
+ var w = key.slice(0);
668
+
669
+ /* RotWord() will rotate a word, moving the first byte to the last
670
+ byte's position (shifting the other bytes left).
671
+ We will be getting the value of Rcon at i / Nk. 'i' will iterate
672
+ from Nk to (Nb * Nr+1). Nk = 4 (4 byte key), Nb = 4 (4 words in
673
+ a block), Nr = Nk + 6 (10). Therefore 'i' will iterate from
674
+ 4 to 44 (exclusive). Each time we iterate 4 times, i / Nk will
675
+ increase by 1. We use a counter iNk to keep track of this.
676
+ */
677
+
678
+ // go through the rounds expanding the key
679
+ var temp,
680
+ iNk = 1;
681
+ var Nk = w.length;
682
+ var Nr1 = Nk + 6 + 1;
683
+ var end = Nb * Nr1;
684
+ for (var i = Nk; i < end; ++i) {
685
+ temp = w[i - 1];
686
+ if (i % Nk === 0) {
687
+ // temp = SubWord(RotWord(temp)) ^ Rcon[i / Nk]
688
+ temp = sbox[temp >>> 16 & 255] << 24 ^ sbox[temp >>> 8 & 255] << 16 ^ sbox[temp & 255] << 8 ^ sbox[temp >>> 24] ^ rcon[iNk] << 24;
689
+ iNk++;
690
+ } else if (Nk > 6 && i % Nk === 4) {
691
+ // temp = SubWord(temp)
692
+ temp = sbox[temp >>> 24] << 24 ^ sbox[temp >>> 16 & 255] << 16 ^ sbox[temp >>> 8 & 255] << 8 ^ sbox[temp & 255];
693
+ }
694
+ w[i] = w[i - Nk] ^ temp;
695
+ }
696
+
697
+ /* When we are updating a cipher block we always use the code path for
698
+ encryption whether we are decrypting or not (to shorten code and
699
+ simplify the generation of look up tables). However, because there
700
+ are differences in the decryption algorithm, other than just swapping
701
+ in different look up tables, we must transform our key schedule to
702
+ account for these changes:
703
+ 1. The decryption algorithm gets its key rounds in reverse order.
704
+ 2. The decryption algorithm adds the round key before mixing columns
705
+ instead of afterwards.
706
+ We don't need to modify our key schedule to handle the first case,
707
+ we can just traverse the key schedule in reverse order when decrypting.
708
+ The second case requires a little work.
709
+ The tables we built for performing rounds will take an input and then
710
+ perform SubBytes() and MixColumns() or, for the decrypt version,
711
+ InvSubBytes() and InvMixColumns(). But the decrypt algorithm requires
712
+ us to AddRoundKey() before InvMixColumns(). This means we'll need to
713
+ apply some transformations to the round key to inverse-mix its columns
714
+ so they'll be correct for moving AddRoundKey() to after the state has
715
+ had its columns inverse-mixed.
716
+ To inverse-mix the columns of the state when we're decrypting we use a
717
+ lookup table that will apply InvSubBytes() and InvMixColumns() at the
718
+ same time. However, the round key's bytes are not inverse-substituted
719
+ in the decryption algorithm. To get around this problem, we can first
720
+ substitute the bytes in the round key so that when we apply the
721
+ transformation via the InvSubBytes()+InvMixColumns() table, it will
722
+ undo our substitution leaving us with the original value that we
723
+ want -- and then inverse-mix that value.
724
+ This change will correctly alter our key schedule so that we can XOR
725
+ each round key with our already transformed decryption state. This
726
+ allows us to use the same code path as the encryption algorithm.
727
+ We make one more change to the decryption key. Since the decryption
728
+ algorithm runs in reverse from the encryption algorithm, we reverse
729
+ the order of the round keys to avoid having to iterate over the key
730
+ schedule backwards when running the encryption algorithm later in
731
+ decryption mode. In addition to reversing the order of the round keys,
732
+ we also swap each round key's 2nd and 4th rows. See the comments
733
+ section where rounds are performed for more details about why this is
734
+ done. These changes are done inline with the other substitution
735
+ described above.
736
+ */
737
+ if (decrypt) {
738
+ var tmp;
739
+ var m0 = imix[0];
740
+ var m1 = imix[1];
741
+ var m2 = imix[2];
742
+ var m3 = imix[3];
743
+ var wnew = w.slice(0);
744
+ end = w.length;
745
+ for (var i = 0, wi = end - Nb; i < end; i += Nb, wi -= Nb) {
746
+ // do not sub the first or last round key (round keys are Nb
747
+ // words) as no column mixing is performed before they are added,
748
+ // but do change the key order
749
+ if (i === 0 || i === end - Nb) {
750
+ wnew[i] = w[wi];
751
+ wnew[i + 1] = w[wi + 3];
752
+ wnew[i + 2] = w[wi + 2];
753
+ wnew[i + 3] = w[wi + 1];
754
+ } else {
755
+ // substitute each round key byte because the inverse-mix
756
+ // table will inverse-substitute it (effectively cancel the
757
+ // substitution because round key bytes aren't sub'd in
758
+ // decryption mode) and swap indexes 3 and 1
759
+ for (var n = 0; n < Nb; ++n) {
760
+ tmp = w[wi + n];
761
+ wnew[i + (3 & -n)] = m0[sbox[tmp >>> 24]] ^ m1[sbox[tmp >>> 16 & 255]] ^ m2[sbox[tmp >>> 8 & 255]] ^ m3[sbox[tmp & 255]];
762
+ }
763
+ }
764
+ }
765
+ w = wnew;
766
+ }
767
+ return w;
768
+ }
769
+
770
+ /**
771
+ * Updates a single block (16 bytes) using AES. The update will either
772
+ * encrypt or decrypt the block.
773
+ *
774
+ * @param w the key schedule.
775
+ * @param input the input block (an array of 32-bit words).
776
+ * @param output the updated output block.
777
+ * @param decrypt true to decrypt the block, false to encrypt it.
778
+ */
779
+ function _updateBlock(w, input, output, decrypt) {
780
+ /*
781
+ Cipher(byte in[4*Nb], byte out[4*Nb], word w[Nb*(Nr+1)])
782
+ begin
783
+ byte state[4,Nb]
784
+ state = in
785
+ AddRoundKey(state, w[0, Nb-1])
786
+ for round = 1 step 1 to Nr-1
787
+ SubBytes(state)
788
+ ShiftRows(state)
789
+ MixColumns(state)
790
+ AddRoundKey(state, w[round*Nb, (round+1)*Nb-1])
791
+ end for
792
+ SubBytes(state)
793
+ ShiftRows(state)
794
+ AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1])
795
+ out = state
796
+ end
797
+ InvCipher(byte in[4*Nb], byte out[4*Nb], word w[Nb*(Nr+1)])
798
+ begin
799
+ byte state[4,Nb]
800
+ state = in
801
+ AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1])
802
+ for round = Nr-1 step -1 downto 1
803
+ InvShiftRows(state)
804
+ InvSubBytes(state)
805
+ AddRoundKey(state, w[round*Nb, (round+1)*Nb-1])
806
+ InvMixColumns(state)
807
+ end for
808
+ InvShiftRows(state)
809
+ InvSubBytes(state)
810
+ AddRoundKey(state, w[0, Nb-1])
811
+ out = state
812
+ end
813
+ */
814
+
815
+ // Encrypt: AddRoundKey(state, w[0, Nb-1])
816
+ // Decrypt: AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1])
817
+ var Nr = w.length / 4 - 1;
818
+ var m0, m1, m2, m3, sub;
819
+ if (decrypt) {
820
+ m0 = imix[0];
821
+ m1 = imix[1];
822
+ m2 = imix[2];
823
+ m3 = imix[3];
824
+ sub = isbox;
825
+ } else {
826
+ m0 = mix[0];
827
+ m1 = mix[1];
828
+ m2 = mix[2];
829
+ m3 = mix[3];
830
+ sub = sbox;
831
+ }
832
+ var a, b, c, d, a2, b2, c2;
833
+ a = input[0] ^ w[0];
834
+ b = input[decrypt ? 3 : 1] ^ w[1];
835
+ c = input[2] ^ w[2];
836
+ d = input[decrypt ? 1 : 3] ^ w[3];
837
+ var i = 3;
838
+
839
+ /* In order to share code we follow the encryption algorithm when both
840
+ encrypting and decrypting. To account for the changes required in the
841
+ decryption algorithm, we use different lookup tables when decrypting
842
+ and use a modified key schedule to account for the difference in the
843
+ order of transformations applied when performing rounds. We also get
844
+ key rounds in reverse order (relative to encryption). */
845
+ for (var round = 1; round < Nr; ++round) {
846
+ /* As described above, we'll be using table lookups to perform the
847
+ column mixing. Each column is stored as a word in the state (the
848
+ array 'input' has one column as a word at each index). In order to
849
+ mix a column, we perform these transformations on each row in c,
850
+ which is 1 byte in each word. The new column for c0 is c'0:
851
+ m0 m1 m2 m3
852
+ r0,c'0 = 2*r0,c0 + 3*r1,c0 + 1*r2,c0 + 1*r3,c0
853
+ r1,c'0 = 1*r0,c0 + 2*r1,c0 + 3*r2,c0 + 1*r3,c0
854
+ r2,c'0 = 1*r0,c0 + 1*r1,c0 + 2*r2,c0 + 3*r3,c0
855
+ r3,c'0 = 3*r0,c0 + 1*r1,c0 + 1*r2,c0 + 2*r3,c0
856
+ So using mix tables where c0 is a word with r0 being its upper
857
+ 8 bits and r3 being its lower 8 bits:
858
+ m0[c0 >> 24] will yield this word: [2*r0,1*r0,1*r0,3*r0]
859
+ ...
860
+ m3[c0 & 255] will yield this word: [1*r3,1*r3,3*r3,2*r3]
861
+ Therefore to mix the columns in each word in the state we
862
+ do the following (& 255 omitted for brevity):
863
+ c'0,r0 = m0[c0 >> 24] ^ m1[c1 >> 16] ^ m2[c2 >> 8] ^ m3[c3]
864
+ c'0,r1 = m0[c0 >> 24] ^ m1[c1 >> 16] ^ m2[c2 >> 8] ^ m3[c3]
865
+ c'0,r2 = m0[c0 >> 24] ^ m1[c1 >> 16] ^ m2[c2 >> 8] ^ m3[c3]
866
+ c'0,r3 = m0[c0 >> 24] ^ m1[c1 >> 16] ^ m2[c2 >> 8] ^ m3[c3]
867
+ However, before mixing, the algorithm requires us to perform
868
+ ShiftRows(). The ShiftRows() transformation cyclically shifts the
869
+ last 3 rows of the state over different offsets. The first row
870
+ (r = 0) is not shifted.
871
+ s'_r,c = s_r,(c + shift(r, Nb) mod Nb
872
+ for 0 < r < 4 and 0 <= c < Nb and
873
+ shift(1, 4) = 1
874
+ shift(2, 4) = 2
875
+ shift(3, 4) = 3.
876
+ This causes the first byte in r = 1 to be moved to the end of
877
+ the row, the first 2 bytes in r = 2 to be moved to the end of
878
+ the row, the first 3 bytes in r = 3 to be moved to the end of
879
+ the row:
880
+ r1: [c0 c1 c2 c3] => [c1 c2 c3 c0]
881
+ r2: [c0 c1 c2 c3] [c2 c3 c0 c1]
882
+ r3: [c0 c1 c2 c3] [c3 c0 c1 c2]
883
+ We can make these substitutions inline with our column mixing to
884
+ generate an updated set of equations to produce each word in the
885
+ state (note the columns have changed positions):
886
+ c0 c1 c2 c3 => c0 c1 c2 c3
887
+ c0 c1 c2 c3 c1 c2 c3 c0 (cycled 1 byte)
888
+ c0 c1 c2 c3 c2 c3 c0 c1 (cycled 2 bytes)
889
+ c0 c1 c2 c3 c3 c0 c1 c2 (cycled 3 bytes)
890
+ Therefore:
891
+ c'0 = 2*r0,c0 + 3*r1,c1 + 1*r2,c2 + 1*r3,c3
892
+ c'0 = 1*r0,c0 + 2*r1,c1 + 3*r2,c2 + 1*r3,c3
893
+ c'0 = 1*r0,c0 + 1*r1,c1 + 2*r2,c2 + 3*r3,c3
894
+ c'0 = 3*r0,c0 + 1*r1,c1 + 1*r2,c2 + 2*r3,c3
895
+ c'1 = 2*r0,c1 + 3*r1,c2 + 1*r2,c3 + 1*r3,c0
896
+ c'1 = 1*r0,c1 + 2*r1,c2 + 3*r2,c3 + 1*r3,c0
897
+ c'1 = 1*r0,c1 + 1*r1,c2 + 2*r2,c3 + 3*r3,c0
898
+ c'1 = 3*r0,c1 + 1*r1,c2 + 1*r2,c3 + 2*r3,c0
899
+ ... and so forth for c'2 and c'3. The important distinction is
900
+ that the columns are cycling, with c0 being used with the m0
901
+ map when calculating c0, but c1 being used with the m0 map when
902
+ calculating c1 ... and so forth.
903
+ When performing the inverse we transform the mirror image and
904
+ skip the bottom row, instead of the top one, and move upwards:
905
+ c3 c2 c1 c0 => c0 c3 c2 c1 (cycled 3 bytes) *same as encryption
906
+ c3 c2 c1 c0 c1 c0 c3 c2 (cycled 2 bytes)
907
+ c3 c2 c1 c0 c2 c1 c0 c3 (cycled 1 byte) *same as encryption
908
+ c3 c2 c1 c0 c3 c2 c1 c0
909
+ If you compare the resulting matrices for ShiftRows()+MixColumns()
910
+ and for InvShiftRows()+InvMixColumns() the 2nd and 4th columns are
911
+ different (in encrypt mode vs. decrypt mode). So in order to use
912
+ the same code to handle both encryption and decryption, we will
913
+ need to do some mapping.
914
+ If in encryption mode we let a=c0, b=c1, c=c2, d=c3, and r<N> be
915
+ a row number in the state, then the resulting matrix in encryption
916
+ mode for applying the above transformations would be:
917
+ r1: a b c d
918
+ r2: b c d a
919
+ r3: c d a b
920
+ r4: d a b c
921
+ If we did the same in decryption mode we would get:
922
+ r1: a d c b
923
+ r2: b a d c
924
+ r3: c b a d
925
+ r4: d c b a
926
+ If instead we swap d and b (set b=c3 and d=c1), then we get:
927
+ r1: a b c d
928
+ r2: d a b c
929
+ r3: c d a b
930
+ r4: b c d a
931
+ Now the 1st and 3rd rows are the same as the encryption matrix. All
932
+ we need to do then to make the mapping exactly the same is to swap
933
+ the 2nd and 4th rows when in decryption mode. To do this without
934
+ having to do it on each iteration, we swapped the 2nd and 4th rows
935
+ in the decryption key schedule. We also have to do the swap above
936
+ when we first pull in the input and when we set the final output. */
937
+ a2 = m0[a >>> 24] ^ m1[b >>> 16 & 255] ^ m2[c >>> 8 & 255] ^ m3[d & 255] ^ w[++i];
938
+ b2 = m0[b >>> 24] ^ m1[c >>> 16 & 255] ^ m2[d >>> 8 & 255] ^ m3[a & 255] ^ w[++i];
939
+ c2 = m0[c >>> 24] ^ m1[d >>> 16 & 255] ^ m2[a >>> 8 & 255] ^ m3[b & 255] ^ w[++i];
940
+ d = m0[d >>> 24] ^ m1[a >>> 16 & 255] ^ m2[b >>> 8 & 255] ^ m3[c & 255] ^ w[++i];
941
+ a = a2;
942
+ b = b2;
943
+ c = c2;
944
+ }
945
+
946
+ /*
947
+ Encrypt:
948
+ SubBytes(state)
949
+ ShiftRows(state)
950
+ AddRoundKey(state, w[Nr*Nb, (Nr+1)*Nb-1])
951
+ Decrypt:
952
+ InvShiftRows(state)
953
+ InvSubBytes(state)
954
+ AddRoundKey(state, w[0, Nb-1])
955
+ */
956
+ // Note: rows are shifted inline
957
+ output[0] = sub[a >>> 24] << 24 ^ sub[b >>> 16 & 255] << 16 ^ sub[c >>> 8 & 255] << 8 ^ sub[d & 255] ^ w[++i];
958
+ output[decrypt ? 3 : 1] = sub[b >>> 24] << 24 ^ sub[c >>> 16 & 255] << 16 ^ sub[d >>> 8 & 255] << 8 ^ sub[a & 255] ^ w[++i];
959
+ output[2] = sub[c >>> 24] << 24 ^ sub[d >>> 16 & 255] << 16 ^ sub[a >>> 8 & 255] << 8 ^ sub[b & 255] ^ w[++i];
960
+ output[decrypt ? 1 : 3] = sub[d >>> 24] << 24 ^ sub[a >>> 16 & 255] << 16 ^ sub[b >>> 8 & 255] << 8 ^ sub[c & 255] ^ w[++i];
961
+ }
962
+
963
+ /**
964
+ * Deprecated. Instead, use:
965
+ *
966
+ * forge.cipher.createCipher('AES-<mode>', key);
967
+ * forge.cipher.createDecipher('AES-<mode>', key);
968
+ *
969
+ * Creates a deprecated AES cipher object. This object's mode will default to
970
+ * CBC (cipher-block-chaining).
971
+ *
972
+ * The key and iv may be given as a string of bytes, an array of bytes, a
973
+ * byte buffer, or an array of 32-bit words.
974
+ *
975
+ * @param options the options to use.
976
+ * key the symmetric key to use.
977
+ * output the buffer to write to.
978
+ * decrypt true for decryption, false for encryption.
979
+ * mode the cipher mode to use (default: 'CBC').
980
+ *
981
+ * @return the cipher.
982
+ */
983
+ function _createCipher(options) {
984
+ options = options || {};
985
+ var mode = (options.mode || 'CBC').toUpperCase();
986
+ var algorithm = 'AES-' + mode;
987
+ var cipher;
988
+ if (options.decrypt) {
989
+ cipher = forge.cipher.createDecipher(algorithm, options.key);
990
+ } else {
991
+ cipher = forge.cipher.createCipher(algorithm, options.key);
992
+ }
993
+
994
+ // backwards compatible start API
995
+ var start = cipher.start;
996
+ cipher.start = function (iv, options) {
997
+ // backwards compatibility: support second arg as output buffer
998
+ var output = null;
999
+ if (options instanceof forge.util.ByteBuffer) {
1000
+ output = options;
1001
+ options = {};
1002
+ }
1003
+ options = options || {};
1004
+ options.output = output;
1005
+ options.iv = iv;
1006
+ start.call(cipher, options);
1007
+ };
1008
+ return cipher;
1009
+ }
1010
+ return aes;
1011
+ }
1012
+
1013
+ export { requireAes as __require };
1014
+ //# sourceMappingURL=aes.js.map