@brix-crypto/crypto-js 0.0.1-security → 4.2.6
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.
Potentially problematic release.
This version of @brix-crypto/crypto-js might be problematic. Click here for more details.
- package/.jshintrc +33 -0
- package/.travis.yml +15 -0
- package/CONTRIBUTING.md +28 -0
- package/LICENSE +24 -0
- package/README.md +273 -3
- package/aes.js +234 -0
- package/blowfish.js +471 -0
- package/bower.json +39 -0
- package/cipher-core.js +895 -0
- package/core.js +819 -0
- package/crypto-js.js +6657 -0
- package/docs/QuickStartGuide.wiki +470 -0
- package/enc-base64.js +140 -0
- package/enc-base64url.js +148 -0
- package/enc-hex.js +18 -0
- package/enc-latin1.js +18 -0
- package/enc-utf16.js +149 -0
- package/enc-utf8.js +18 -0
- package/evpkdf.js +134 -0
- package/format-hex.js +66 -0
- package/format-openssl.js +18 -0
- package/hmac-md5.js +18 -0
- package/hmac-ripemd160.js +18 -0
- package/hmac-sha1.js +18 -0
- package/hmac-sha224.js +18 -0
- package/hmac-sha256.js +18 -0
- package/hmac-sha3.js +18 -0
- package/hmac-sha384.js +18 -0
- package/hmac-sha512.js +18 -0
- package/hmac.js +143 -0
- package/index.js +18 -0
- package/lib-typedarrays.js +76 -0
- package/md5.js +268 -0
- package/mode-cfb.js +80 -0
- package/mode-ctr-gladman.js +116 -0
- package/mode-ctr.js +58 -0
- package/mode-ecb.js +40 -0
- package/mode-ofb.js +54 -0
- package/package.json +45 -3
- package/pad-ansix923.js +49 -0
- package/pad-iso10126.js +44 -0
- package/pad-iso97971.js +40 -0
- package/pad-nopadding.js +30 -0
- package/pad-pkcs7.js +18 -0
- package/pad-zeropadding.js +47 -0
- package/pbkdf2.js +145 -0
- package/rabbit-legacy.js +190 -0
- package/rabbit.js +192 -0
- package/rc4.js +139 -0
- package/ripemd160.js +267 -0
- package/sha1.js +150 -0
- package/sha224.js +80 -0
- package/sha256.js +199 -0
- package/sha3.js +326 -0
- package/sha384.js +83 -0
- package/sha512.js +326 -0
- package/test/aes-profile.js +31 -0
- package/test/aes-test.js +80 -0
- package/test/blowfish-test.js +33 -0
- package/test/cipher-test.js +522 -0
- package/test/config-test.js +51 -0
- package/test/des-profile.js +31 -0
- package/test/des-test.js +104 -0
- package/test/enc-base64-test.js +71 -0
- package/test/enc-hex-test.js +15 -0
- package/test/enc-latin1-test.js +15 -0
- package/test/enc-utf16-test.js +55 -0
- package/test/enc-utf8-test.js +39 -0
- package/test/evpkdf-profile.js +11 -0
- package/test/evpkdf-test.js +32 -0
- package/test/format-openssl-test.js +37 -0
- package/test/hmac-md5-profile.js +30 -0
- package/test/hmac-md5-test.js +59 -0
- package/test/hmac-sha224-test.js +59 -0
- package/test/hmac-sha256-test.js +59 -0
- package/test/hmac-sha384-test.js +59 -0
- package/test/hmac-sha512-test.js +59 -0
- package/test/kdf-openssl-test.js +15 -0
- package/test/lib-base-test.js +92 -0
- package/test/lib-cipherparams-test.js +59 -0
- package/test/lib-passwordbasedcipher-test.js +25 -0
- package/test/lib-serializablecipher-test.js +51 -0
- package/test/lib-typedarrays-test.js +57 -0
- package/test/lib-wordarray-test.js +85 -0
- package/test/md5-profile.js +24 -0
- package/test/md5-test.js +70 -0
- package/test/mode-cbc-test.js +49 -0
- package/test/mode-cfb-test.js +51 -0
- package/test/mode-ctr-test.js +55 -0
- package/test/mode-ecb-test.js +38 -0
- package/test/mode-ofb-test.js +50 -0
- package/test/pad-ansix923-test.js +28 -0
- package/test/pad-iso10126-test.js +50 -0
- package/test/pad-iso97971-test.js +35 -0
- package/test/pad-pkcs7-test.js +28 -0
- package/test/pad-zeropadding-test.js +28 -0
- package/test/pbkdf2-profile.js +11 -0
- package/test/pbkdf2-test.js +80 -0
- package/test/profile.html +281 -0
- package/test/rabbit-legacy-test.js +80 -0
- package/test/rabbit-profile.js +30 -0
- package/test/rabbit-test.js +84 -0
- package/test/rc4-profile.js +30 -0
- package/test/rc4-test.js +68 -0
- package/test/ripemd160-test.js +19 -0
- package/test/sha1-profile.js +24 -0
- package/test/sha1-test.js +70 -0
- package/test/sha224-test.js +19 -0
- package/test/sha256-profile.js +24 -0
- package/test/sha256-test.js +70 -0
- package/test/sha3-profile.js +24 -0
- package/test/sha3-test.js +69 -0
- package/test/sha384-test.js +54 -0
- package/test/sha512-profile.js +24 -0
- package/test/sha512-test.js +54 -0
- package/test/test-build.html +105 -0
- package/test/test.html +138 -0
- package/test/test1.html +63 -0
- package/test/tripledes-profile.js +31 -0
- package/test/tripledes-test.js +121 -0
- package/test/x64-word-test.js +99 -0
- package/test/x64-wordarray-test.js +38 -0
- package/tripledes.js +779 -0
- package/x64-core.js +304 -0
@@ -0,0 +1,522 @@
|
|
1
|
+
function extendWithCMAC(C) {
|
2
|
+
function createExt(C) {
|
3
|
+
/*
|
4
|
+
* The MIT License (MIT)
|
5
|
+
*
|
6
|
+
* Copyright (c) 2015 artjomb
|
7
|
+
*/
|
8
|
+
// put on ext property in CryptoJS
|
9
|
+
var ext;
|
10
|
+
if (!C.hasOwnProperty("ext")) {
|
11
|
+
ext = C.ext = {};
|
12
|
+
} else {
|
13
|
+
ext = C.ext;
|
14
|
+
}
|
15
|
+
|
16
|
+
// Shortcuts
|
17
|
+
var Base = C.lib.Base;
|
18
|
+
var WordArray = C.lib.WordArray;
|
19
|
+
|
20
|
+
// Constants
|
21
|
+
ext.const_Zero = new WordArray.init([0x00000000, 0x00000000, 0x00000000, 0x00000000]);
|
22
|
+
ext.const_One = new WordArray.init([0x00000000, 0x00000000, 0x00000000, 0x00000001]);
|
23
|
+
ext.const_Rb = new WordArray.init([0x00000000, 0x00000000, 0x00000000, 0x00000087]); // 00..0010000111
|
24
|
+
ext.const_Rb_Shifted = new WordArray.init([0x80000000, 0x00000000, 0x00000000, 0x00000043]); // 100..001000011
|
25
|
+
ext.const_nonMSB = new WordArray.init([0xFFFFFFFF, 0xFFFFFFFF, 0x7FFFFFFF, 0x7FFFFFFF]); // 1^64 || 0^1 || 1^31 || 0^1 || 1^31
|
26
|
+
|
27
|
+
/**
|
28
|
+
* Looks into the object to see if it is a WordArray.
|
29
|
+
*
|
30
|
+
* @param obj Some object
|
31
|
+
*
|
32
|
+
* @returns {boolean}
|
33
|
+
*/
|
34
|
+
ext.isWordArray = function(obj) {
|
35
|
+
return obj && typeof obj.clamp === "function" && typeof obj.concat === "function" && typeof obj.words === "array";
|
36
|
+
}
|
37
|
+
|
38
|
+
/**
|
39
|
+
* This padding is a 1 bit followed by as many 0 bits as needed to fill
|
40
|
+
* up the block. This implementation doesn't work on bits directly,
|
41
|
+
* but on bytes. Therefore the granularity is much bigger.
|
42
|
+
*/
|
43
|
+
C.pad.OneZeroPadding = {
|
44
|
+
pad: function (data, blocksize) {
|
45
|
+
// Shortcut
|
46
|
+
var blockSizeBytes = blocksize * 4;
|
47
|
+
|
48
|
+
// Count padding bytes
|
49
|
+
var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes;
|
50
|
+
|
51
|
+
// Create padding
|
52
|
+
var paddingWords = [];
|
53
|
+
for (var i = 0; i < nPaddingBytes; i += 4) {
|
54
|
+
var paddingWord = 0x00000000;
|
55
|
+
if (i === 0) {
|
56
|
+
paddingWord = 0x80000000;
|
57
|
+
}
|
58
|
+
paddingWords.push(paddingWord);
|
59
|
+
}
|
60
|
+
var padding = new WordArray.init(paddingWords, nPaddingBytes);
|
61
|
+
|
62
|
+
// Add padding
|
63
|
+
data.concat(padding);
|
64
|
+
},
|
65
|
+
unpad: function () {
|
66
|
+
// TODO: implement
|
67
|
+
}
|
68
|
+
};
|
69
|
+
|
70
|
+
/**
|
71
|
+
* No padding is applied. This is necessary for streaming cipher modes
|
72
|
+
* like CTR.
|
73
|
+
*/
|
74
|
+
C.pad.NoPadding = {
|
75
|
+
pad: function () {},
|
76
|
+
unpad: function () {}
|
77
|
+
};
|
78
|
+
|
79
|
+
/**
|
80
|
+
* Returns the n leftmost bytes of the WordArray.
|
81
|
+
*
|
82
|
+
* @param {WordArray} wordArray WordArray to work on
|
83
|
+
* @param {int} n Bytes to retrieve
|
84
|
+
*
|
85
|
+
* @returns new WordArray
|
86
|
+
*/
|
87
|
+
ext.leftmostBytes = function(wordArray, n){
|
88
|
+
var lmArray = wordArray.clone();
|
89
|
+
lmArray.sigBytes = n;
|
90
|
+
lmArray.clamp();
|
91
|
+
return lmArray;
|
92
|
+
};
|
93
|
+
|
94
|
+
/**
|
95
|
+
* Returns the n rightmost bytes of the WordArray.
|
96
|
+
*
|
97
|
+
* @param {WordArray} wordArray WordArray to work on
|
98
|
+
* @param {int} n Bytes to retrieve (must be positive)
|
99
|
+
*
|
100
|
+
* @returns new WordArray
|
101
|
+
*/
|
102
|
+
ext.rightmostBytes = function(wordArray, n){
|
103
|
+
wordArray.clamp();
|
104
|
+
var wordSize = 32;
|
105
|
+
var rmArray = wordArray.clone();
|
106
|
+
var bitsToShift = (rmArray.sigBytes - n) * 8;
|
107
|
+
if (bitsToShift >= wordSize) {
|
108
|
+
var popCount = Math.floor(bitsToShift/wordSize);
|
109
|
+
bitsToShift -= popCount * wordSize;
|
110
|
+
rmArray.words.splice(0, popCount);
|
111
|
+
rmArray.sigBytes -= popCount * wordSize / 8;
|
112
|
+
}
|
113
|
+
if (bitsToShift > 0) {
|
114
|
+
ext.bitshift(rmArray, bitsToShift);
|
115
|
+
rmArray.sigBytes -= bitsToShift / 8;
|
116
|
+
}
|
117
|
+
return rmArray;
|
118
|
+
};
|
119
|
+
|
120
|
+
/**
|
121
|
+
* Returns the n rightmost words of the WordArray. It assumes
|
122
|
+
* that the current WordArray has at least n words.
|
123
|
+
*
|
124
|
+
* @param {WordArray} wordArray WordArray to work on
|
125
|
+
* @param {int} n Words to retrieve (must be positive)
|
126
|
+
*
|
127
|
+
* @returns popped words as new WordArray
|
128
|
+
*/
|
129
|
+
ext.popWords = function(wordArray, n){
|
130
|
+
var left = wordArray.words.splice(0, n);
|
131
|
+
wordArray.sigBytes -= n * 4;
|
132
|
+
return new WordArray.init(left);
|
133
|
+
};
|
134
|
+
|
135
|
+
/**
|
136
|
+
* Shifts the array to the left and returns the shifted dropped elements
|
137
|
+
* as WordArray. The initial WordArray must contain at least n bytes and
|
138
|
+
* they have to be significant.
|
139
|
+
*
|
140
|
+
* @param {WordArray} wordArray WordArray to work on (is modified)
|
141
|
+
* @param {int} n Bytes to shift (must be positive, default 16)
|
142
|
+
*
|
143
|
+
* @returns new WordArray
|
144
|
+
*/
|
145
|
+
ext.shiftBytes = function(wordArray, n){
|
146
|
+
n = n || 16;
|
147
|
+
var r = n % 4;
|
148
|
+
n -= r;
|
149
|
+
|
150
|
+
var shiftedArray = new WordArray.init();
|
151
|
+
for(var i = 0; i < n; i += 4) {
|
152
|
+
shiftedArray.words.push(wordArray.words.shift());
|
153
|
+
wordArray.sigBytes -= 4;
|
154
|
+
shiftedArray.sigBytes += 4;
|
155
|
+
}
|
156
|
+
if (r > 0) {
|
157
|
+
shiftedArray.words.push(wordArray.words[0]);
|
158
|
+
shiftedArray.sigBytes += r;
|
159
|
+
|
160
|
+
ext.bitshift(wordArray, r * 8);
|
161
|
+
wordArray.sigBytes -= r;
|
162
|
+
}
|
163
|
+
return shiftedArray;
|
164
|
+
};
|
165
|
+
|
166
|
+
/**
|
167
|
+
* XORs arr2 to the end of arr1 array. This doesn't modify the current
|
168
|
+
* array aside from clamping.
|
169
|
+
*
|
170
|
+
* @param {WordArray} arr1 Bigger array
|
171
|
+
* @param {WordArray} arr2 Smaller array to be XORed to the end
|
172
|
+
*
|
173
|
+
* @returns new WordArray
|
174
|
+
*/
|
175
|
+
ext.xorendBytes = function(arr1, arr2){
|
176
|
+
// TODO: more efficient
|
177
|
+
return ext.leftmostBytes(arr1, arr1.sigBytes-arr2.sigBytes)
|
178
|
+
.concat(ext.xor(ext.rightmostBytes(arr1, arr2.sigBytes), arr2));
|
179
|
+
};
|
180
|
+
|
181
|
+
/**
|
182
|
+
* Doubling operation on a 128-bit value. This operation modifies the
|
183
|
+
* passed array.
|
184
|
+
*
|
185
|
+
* @param {WordArray} wordArray WordArray to work on
|
186
|
+
*
|
187
|
+
* @returns passed WordArray
|
188
|
+
*/
|
189
|
+
ext.dbl = function(wordArray){
|
190
|
+
var carry = ext.msb(wordArray);
|
191
|
+
ext.bitshift(wordArray, 1);
|
192
|
+
ext.xor(wordArray, carry === 1 ? ext.const_Rb : ext.const_Zero);
|
193
|
+
return wordArray;
|
194
|
+
};
|
195
|
+
|
196
|
+
/**
|
197
|
+
* Inverse operation on a 128-bit value. This operation modifies the
|
198
|
+
* passed array.
|
199
|
+
*
|
200
|
+
* @param {WordArray} wordArray WordArray to work on
|
201
|
+
*
|
202
|
+
* @returns passed WordArray
|
203
|
+
*/
|
204
|
+
ext.inv = function(wordArray){
|
205
|
+
var carry = wordArray.words[4] & 1;
|
206
|
+
ext.bitshift(wordArray, -1);
|
207
|
+
ext.xor(wordArray, carry === 1 ? ext.const_Rb_Shifted : ext.const_Zero);
|
208
|
+
return wordArray;
|
209
|
+
};
|
210
|
+
|
211
|
+
/**
|
212
|
+
* Check whether the word arrays are equal.
|
213
|
+
*
|
214
|
+
* @param {WordArray} arr1 Array 1
|
215
|
+
* @param {WordArray} arr2 Array 2
|
216
|
+
*
|
217
|
+
* @returns boolean
|
218
|
+
*/
|
219
|
+
ext.equals = function(arr1, arr2){
|
220
|
+
if (!arr2 || !arr2.words || arr1.sigBytes !== arr2.sigBytes) {
|
221
|
+
return false;
|
222
|
+
}
|
223
|
+
arr1.clamp();
|
224
|
+
arr2.clamp();
|
225
|
+
var equal = 0;
|
226
|
+
for(var i = 0; i < arr1.words.length; i++) {
|
227
|
+
equal |= arr1.words[i] ^ arr2.words[i];
|
228
|
+
}
|
229
|
+
return equal === 0;
|
230
|
+
};
|
231
|
+
|
232
|
+
/**
|
233
|
+
* Retrieves the most significant bit of the WordArray as an Integer.
|
234
|
+
*
|
235
|
+
* @param {WordArray} arr
|
236
|
+
*
|
237
|
+
* @returns Integer
|
238
|
+
*/
|
239
|
+
ext.msb = function(arr) {
|
240
|
+
return arr.words[0] >>> 31;
|
241
|
+
}
|
242
|
+
}
|
243
|
+
|
244
|
+
function createExtBit(C) {
|
245
|
+
/*
|
246
|
+
* The MIT License (MIT)
|
247
|
+
*
|
248
|
+
* Copyright (c) 2015 artjomb
|
249
|
+
*/
|
250
|
+
// put on ext property in CryptoJS
|
251
|
+
var ext;
|
252
|
+
if (!C.hasOwnProperty("ext")) {
|
253
|
+
ext = C.ext = {};
|
254
|
+
} else {
|
255
|
+
ext = C.ext;
|
256
|
+
}
|
257
|
+
|
258
|
+
/**
|
259
|
+
* Shifts the array by n bits to the left. Zero bits are added as the
|
260
|
+
* least significant bits. This operation modifies the current array.
|
261
|
+
*
|
262
|
+
* @param {WordArray} wordArray WordArray to work on
|
263
|
+
* @param {int} n Bits to shift by
|
264
|
+
*
|
265
|
+
* @returns the WordArray that was passed in
|
266
|
+
*/
|
267
|
+
ext.bitshift = function(wordArray, n){
|
268
|
+
var carry = 0,
|
269
|
+
words = wordArray.words,
|
270
|
+
wres,
|
271
|
+
skipped = 0,
|
272
|
+
carryMask;
|
273
|
+
if (n > 0) {
|
274
|
+
while(n > 31) {
|
275
|
+
// delete first element:
|
276
|
+
words.splice(0, 1);
|
277
|
+
|
278
|
+
// add `0` word to the back
|
279
|
+
words.push(0);
|
280
|
+
|
281
|
+
n -= 32;
|
282
|
+
skipped++;
|
283
|
+
}
|
284
|
+
if (n == 0) {
|
285
|
+
// 1. nothing to shift if the shift amount is on a word boundary
|
286
|
+
// 2. This has to be done, because the following algorithm computes
|
287
|
+
// wrong values only for n==0
|
288
|
+
return carry;
|
289
|
+
}
|
290
|
+
for(var i = words.length - skipped - 1; i >= 0; i--) {
|
291
|
+
wres = words[i];
|
292
|
+
words[i] <<= n;
|
293
|
+
words[i] |= carry;
|
294
|
+
carry = wres >>> (32 - n);
|
295
|
+
}
|
296
|
+
} else if (n < 0) {
|
297
|
+
while(n < -31) {
|
298
|
+
// insert `0` word to the front:
|
299
|
+
words.splice(0, 0, 0);
|
300
|
+
|
301
|
+
// remove last element:
|
302
|
+
words.length--;
|
303
|
+
|
304
|
+
n += 32;
|
305
|
+
skipped++;
|
306
|
+
}
|
307
|
+
if (n == 0) {
|
308
|
+
// nothing to shift if the shift amount is on a word boundary
|
309
|
+
return carry;
|
310
|
+
}
|
311
|
+
n = -n;
|
312
|
+
carryMask = (1 << n) - 1;
|
313
|
+
for(var i = skipped; i < words.length; i++) {
|
314
|
+
wres = words[i] & carryMask;
|
315
|
+
words[i] >>>= n;
|
316
|
+
words[i] |= carry;
|
317
|
+
carry = wres << (32 - n);
|
318
|
+
}
|
319
|
+
}
|
320
|
+
return carry;
|
321
|
+
};
|
322
|
+
|
323
|
+
/**
|
324
|
+
* Negates all bits in the WordArray. This manipulates the given array.
|
325
|
+
*
|
326
|
+
* @param {WordArray} wordArray WordArray to work on
|
327
|
+
*
|
328
|
+
* @returns the WordArray that was passed in
|
329
|
+
*/
|
330
|
+
ext.neg = function(wordArray){
|
331
|
+
var words = wordArray.words;
|
332
|
+
for(var i = 0; i < words.length; i++) {
|
333
|
+
words[i] = ~words[i];
|
334
|
+
}
|
335
|
+
return wordArray;
|
336
|
+
};
|
337
|
+
|
338
|
+
/**
|
339
|
+
* Applies XOR on both given word arrays and returns a third resulting
|
340
|
+
* WordArray. The initial word arrays must have the same length
|
341
|
+
* (significant bytes).
|
342
|
+
*
|
343
|
+
* @param {WordArray} wordArray1 WordArray
|
344
|
+
* @param {WordArray} wordArray2 WordArray
|
345
|
+
*
|
346
|
+
* @returns first passed WordArray (modified)
|
347
|
+
*/
|
348
|
+
ext.xor = function(wordArray1, wordArray2){
|
349
|
+
for(var i = 0; i < wordArray1.words.length; i++) {
|
350
|
+
wordArray1.words[i] ^= wordArray2.words[i];
|
351
|
+
}
|
352
|
+
return wordArray1;
|
353
|
+
};
|
354
|
+
|
355
|
+
/**
|
356
|
+
* Logical AND between the two passed arrays. Both arrays must have the
|
357
|
+
* same length.
|
358
|
+
*
|
359
|
+
* @param {WordArray} arr1 Array 1
|
360
|
+
* @param {WordArray} arr2 Array 2
|
361
|
+
*
|
362
|
+
* @returns new WordArray
|
363
|
+
*/
|
364
|
+
ext.bitand = function(arr1, arr2){
|
365
|
+
var newArr = arr1.clone(),
|
366
|
+
tw = newArr.words,
|
367
|
+
ow = arr2.words;
|
368
|
+
for(var i = 0; i < tw.length; i++) {
|
369
|
+
tw[i] &= ow[i];
|
370
|
+
}
|
371
|
+
return newArr;
|
372
|
+
};
|
373
|
+
}
|
374
|
+
|
375
|
+
function createCMAC(C) {
|
376
|
+
/*
|
377
|
+
* The MIT License (MIT)
|
378
|
+
*
|
379
|
+
* Copyright (c) 2015 artjomb
|
380
|
+
*/
|
381
|
+
// Shortcuts
|
382
|
+
var Base = C.lib.Base;
|
383
|
+
var WordArray = C.lib.WordArray;
|
384
|
+
var AES = C.algo.AES;
|
385
|
+
var ext = C.ext;
|
386
|
+
var OneZeroPadding = C.pad.OneZeroPadding;
|
387
|
+
|
388
|
+
var CMAC = C.algo.CMAC = Base.extend({
|
389
|
+
/**
|
390
|
+
* Initializes a newly created CMAC
|
391
|
+
*
|
392
|
+
* @param {WordArray} key The secret key
|
393
|
+
*
|
394
|
+
* @example
|
395
|
+
*
|
396
|
+
* var cmacer = CryptoJS.algo.CMAC.create(key);
|
397
|
+
*/
|
398
|
+
init: function(key){
|
399
|
+
// generate sub keys...
|
400
|
+
this._aes = AES.createEncryptor(key, { iv: new WordArray.init(), padding: C.pad.NoPadding });
|
401
|
+
|
402
|
+
// Step 1
|
403
|
+
var L = this._aes.finalize(ext.const_Zero);
|
404
|
+
|
405
|
+
// Step 2
|
406
|
+
var K1 = L.clone();
|
407
|
+
ext.dbl(K1);
|
408
|
+
|
409
|
+
// Step 3
|
410
|
+
if (!this._isTwo) {
|
411
|
+
var K2 = K1.clone();
|
412
|
+
ext.dbl(K2);
|
413
|
+
} else {
|
414
|
+
var K2 = L.clone();
|
415
|
+
ext.inv(K2);
|
416
|
+
}
|
417
|
+
|
418
|
+
this._K1 = K1;
|
419
|
+
this._K2 = K2;
|
420
|
+
|
421
|
+
this._const_Bsize = 16;
|
422
|
+
|
423
|
+
this.reset();
|
424
|
+
},
|
425
|
+
|
426
|
+
reset: function () {
|
427
|
+
this._x = ext.const_Zero.clone();
|
428
|
+
this._counter = 0;
|
429
|
+
this._buffer = new WordArray.init();
|
430
|
+
},
|
431
|
+
|
432
|
+
update: function (messageUpdate) {
|
433
|
+
if (!messageUpdate) {
|
434
|
+
return this;
|
435
|
+
}
|
436
|
+
|
437
|
+
// Shortcuts
|
438
|
+
var buffer = this._buffer;
|
439
|
+
var bsize = this._const_Bsize;
|
440
|
+
|
441
|
+
if (typeof messageUpdate === "string") {
|
442
|
+
messageUpdate = C.enc.Utf8.parse(messageUpdate);
|
443
|
+
}
|
444
|
+
|
445
|
+
buffer.concat(messageUpdate);
|
446
|
+
|
447
|
+
while(buffer.sigBytes > bsize){
|
448
|
+
var M_i = ext.shiftBytes(buffer, bsize);
|
449
|
+
ext.xor(this._x, M_i);
|
450
|
+
this._x.clamp();
|
451
|
+
this._aes.reset();
|
452
|
+
this._x = this._aes.finalize(this._x);
|
453
|
+
this._counter++;
|
454
|
+
}
|
455
|
+
|
456
|
+
// Chainable
|
457
|
+
return this;
|
458
|
+
},
|
459
|
+
|
460
|
+
finalize: function (messageUpdate) {
|
461
|
+
this.update(messageUpdate);
|
462
|
+
|
463
|
+
// Shortcuts
|
464
|
+
var buffer = this._buffer;
|
465
|
+
var bsize = this._const_Bsize;
|
466
|
+
|
467
|
+
var M_last = buffer.clone();
|
468
|
+
if (buffer.sigBytes === bsize) {
|
469
|
+
ext.xor(M_last, this._K1);
|
470
|
+
} else {
|
471
|
+
OneZeroPadding.pad(M_last, bsize/4);
|
472
|
+
ext.xor(M_last, this._K2);
|
473
|
+
}
|
474
|
+
|
475
|
+
ext.xor(M_last, this._x);
|
476
|
+
|
477
|
+
this.reset(); // Can be used immediately afterwards
|
478
|
+
|
479
|
+
this._aes.reset();
|
480
|
+
return this._aes.finalize(M_last);
|
481
|
+
},
|
482
|
+
|
483
|
+
_isTwo: false
|
484
|
+
});
|
485
|
+
|
486
|
+
/**
|
487
|
+
* Directly invokes the CMAC and returns the calculated MAC.
|
488
|
+
*
|
489
|
+
* @param {WordArray} key The key to be used for CMAC
|
490
|
+
* @param {WordArray|string} message The data to be MAC'ed (either WordArray or UTF-8 encoded string)
|
491
|
+
*
|
492
|
+
* @returns {WordArray} MAC
|
493
|
+
*/
|
494
|
+
C.CMAC = function(key, message){
|
495
|
+
return CMAC.create(key).finalize(message);
|
496
|
+
};
|
497
|
+
|
498
|
+
C.algo.OMAC1 = CMAC;
|
499
|
+
C.algo.OMAC2 = CMAC.extend({
|
500
|
+
_isTwo: true
|
501
|
+
});
|
502
|
+
}
|
503
|
+
|
504
|
+
createExt(C);
|
505
|
+
createExtBit(C);
|
506
|
+
createCMAC(C);
|
507
|
+
}
|
508
|
+
|
509
|
+
YUI.add('cipher-core-test', function (Y) {
|
510
|
+
var C = CryptoJS;
|
511
|
+
|
512
|
+
// Extend with CMAC to test `cipher-core.js` L:457-462
|
513
|
+
extendWithCMAC(C);
|
514
|
+
|
515
|
+
Y.Test.Runner.add(new Y.Test.Case({
|
516
|
+
name: 'Cipher',
|
517
|
+
|
518
|
+
testCMAC: function () {
|
519
|
+
Y.Assert.areEqual('35e1872b95ce5d99bb5dbbbbd79b9b9b', C.CMAC('69c4e0d86a7b0430d8cdb78070b4c55a', 'Test message').toString());
|
520
|
+
}
|
521
|
+
}));
|
522
|
+
}, '$Rev$');
|
@@ -0,0 +1,51 @@
|
|
1
|
+
YUI.add('config-test', function (Y) {
|
2
|
+
var C = CryptoJS;
|
3
|
+
|
4
|
+
Y.Test.Runner.add(new Y.Test.Case({
|
5
|
+
name: 'Config',
|
6
|
+
|
7
|
+
setUp: function () {
|
8
|
+
this.data = {
|
9
|
+
saltA: CryptoJS.enc.Hex.parse('AA00000000000000'),
|
10
|
+
saltB: CryptoJS.enc.Hex.parse('BB00000000000000')
|
11
|
+
};
|
12
|
+
},
|
13
|
+
|
14
|
+
testEncrypt: function () {
|
15
|
+
Y.Assert.areEqual(C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA }).toString(), C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA }).toString());
|
16
|
+
Y.Assert.areNotEqual(C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA }).toString(), C.AES.encrypt('Test', 'Pass', { salt: this.data.saltB }).toString());
|
17
|
+
},
|
18
|
+
|
19
|
+
testDecrypt: function () {
|
20
|
+
var encryptedA = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA });
|
21
|
+
var encryptedB = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltB });
|
22
|
+
Y.Assert.areEqual('Test', C.AES.decrypt(encryptedA, 'Pass').toString(C.enc.Utf8));
|
23
|
+
Y.Assert.areEqual('Test', C.AES.decrypt(encryptedB, 'Pass').toString(C.enc.Utf8));
|
24
|
+
},
|
25
|
+
|
26
|
+
testCustomKDFHasher: function () {
|
27
|
+
//SHA1
|
28
|
+
let encryptedSHA1 = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA, hasher: C.algo.SHA1}).toString();
|
29
|
+
Y.Assert.areEqual('Test', C.AES.decrypt(encryptedSHA1, 'Pass', { hasher: C.algo.SHA1}).toString(C.enc.Utf8));
|
30
|
+
|
31
|
+
//SHA256
|
32
|
+
let encryptedSHA256 = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA, hasher: C.algo.SHA256}).toString();
|
33
|
+
Y.Assert.areEqual('Test', C.AES.decrypt(encryptedSHA256, 'Pass', { hasher: C.algo.SHA256}).toString(C.enc.Utf8));
|
34
|
+
|
35
|
+
//SHA512
|
36
|
+
let encryptedSHA512 = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA, hasher: C.algo.SHA512}).toString();
|
37
|
+
Y.Assert.areEqual('Test', C.AES.decrypt(encryptedSHA512, 'Pass', { hasher: C.algo.SHA512}).toString(C.enc.Utf8));
|
38
|
+
|
39
|
+
//Default: MD5
|
40
|
+
let encryptedDefault = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA }).toString();
|
41
|
+
let encryptedMD5 = C.AES.encrypt('Test', 'Pass', { salt: this.data.saltA, hasher: C.algo.MD5}).toString();
|
42
|
+
Y.Assert.areEqual('Test', C.AES.decrypt(encryptedMD5, 'Pass', { hasher: C.algo.MD5}).toString(C.enc.Utf8));
|
43
|
+
Y.Assert.areEqual(encryptedDefault, encryptedMD5);
|
44
|
+
|
45
|
+
//Different KDFHasher
|
46
|
+
Y.Assert.areNotEqual(encryptedDefault, encryptedSHA1);
|
47
|
+
Y.Assert.areNotEqual(encryptedDefault, encryptedSHA256);
|
48
|
+
Y.Assert.areNotEqual(encryptedDefault, encryptedSHA512);
|
49
|
+
}
|
50
|
+
}));
|
51
|
+
}, '$Rev$');
|
@@ -0,0 +1,31 @@
|
|
1
|
+
YUI.add('algo-des-profile', function (Y) {
|
2
|
+
var C = CryptoJS;
|
3
|
+
|
4
|
+
Y.Profiler.add({
|
5
|
+
name: 'DES',
|
6
|
+
|
7
|
+
setUp: function () {
|
8
|
+
this.data = {
|
9
|
+
key: C.enc.Hex.parse('0001020304050607'),
|
10
|
+
iv: C.enc.Hex.parse('08090a0b0c0d0e0f')
|
11
|
+
};
|
12
|
+
},
|
13
|
+
|
14
|
+
profileSinglePartMessage: function () {
|
15
|
+
var singlePartMessage = '';
|
16
|
+
for (var i = 0; i < 100; i++) {
|
17
|
+
singlePartMessage += '12345678901234567890123456789012345678901234567890';
|
18
|
+
}
|
19
|
+
|
20
|
+
C.algo.DES.createEncryptor(this.data.key, { iv: this.data.iv }).finalize(singlePartMessage) + '';
|
21
|
+
},
|
22
|
+
|
23
|
+
profileMultiPartMessage: function () {
|
24
|
+
var des = C.algo.DES.createEncryptor(this.data.key, { iv: this.data.iv });
|
25
|
+
for (var i = 0; i < 100; i++) {
|
26
|
+
des.process('12345678901234567890123456789012345678901234567890') + '';
|
27
|
+
}
|
28
|
+
des.finalize() + '';
|
29
|
+
}
|
30
|
+
});
|
31
|
+
}, '$Rev$');
|