x64-core.js (8277B)
1 (function (undefined) { 2 // Shortcuts 3 var C = CryptoJS; 4 var C_lib = C.lib; 5 var Base = C_lib.Base; 6 var X32WordArray = C_lib.WordArray; 7 8 /** 9 * x64 namespace. 10 */ 11 var C_x64 = C.x64 = {}; 12 13 /** 14 * A 64-bit word. 15 */ 16 var X64Word = C_x64.Word = Base.extend({ 17 /** 18 * Initializes a newly created 64-bit word. 19 * 20 * @param {number} high The high 32 bits. 21 * @param {number} low The low 32 bits. 22 * 23 * @example 24 * 25 * var x64Word = CryptoJS.x64.Word.create(0x00010203, 0x04050607); 26 */ 27 init: function (high, low) { 28 this.high = high; 29 this.low = low; 30 } 31 32 /** 33 * Bitwise NOTs this word. 34 * 35 * @return {X64Word} A new x64-Word object after negating. 36 * 37 * @example 38 * 39 * var negated = x64Word.not(); 40 */ 41 // not: function () { 42 // var high = ~this.high; 43 // var low = ~this.low; 44 45 // return X64Word.create(high, low); 46 // }, 47 48 /** 49 * Bitwise ANDs this word with the passed word. 50 * 51 * @param {X64Word} word The x64-Word to AND with this word. 52 * 53 * @return {X64Word} A new x64-Word object after ANDing. 54 * 55 * @example 56 * 57 * var anded = x64Word.and(anotherX64Word); 58 */ 59 // and: function (word) { 60 // var high = this.high & word.high; 61 // var low = this.low & word.low; 62 63 // return X64Word.create(high, low); 64 // }, 65 66 /** 67 * Bitwise ORs this word with the passed word. 68 * 69 * @param {X64Word} word The x64-Word to OR with this word. 70 * 71 * @return {X64Word} A new x64-Word object after ORing. 72 * 73 * @example 74 * 75 * var ored = x64Word.or(anotherX64Word); 76 */ 77 // or: function (word) { 78 // var high = this.high | word.high; 79 // var low = this.low | word.low; 80 81 // return X64Word.create(high, low); 82 // }, 83 84 /** 85 * Bitwise XORs this word with the passed word. 86 * 87 * @param {X64Word} word The x64-Word to XOR with this word. 88 * 89 * @return {X64Word} A new x64-Word object after XORing. 90 * 91 * @example 92 * 93 * var xored = x64Word.xor(anotherX64Word); 94 */ 95 // xor: function (word) { 96 // var high = this.high ^ word.high; 97 // var low = this.low ^ word.low; 98 99 // return X64Word.create(high, low); 100 // }, 101 102 /** 103 * Shifts this word n bits to the left. 104 * 105 * @param {number} n The number of bits to shift. 106 * 107 * @return {X64Word} A new x64-Word object after shifting. 108 * 109 * @example 110 * 111 * var shifted = x64Word.shiftL(25); 112 */ 113 // shiftL: function (n) { 114 // if (n < 32) { 115 // var high = (this.high << n) | (this.low >>> (32 - n)); 116 // var low = this.low << n; 117 // } else { 118 // var high = this.low << (n - 32); 119 // var low = 0; 120 // } 121 122 // return X64Word.create(high, low); 123 // }, 124 125 /** 126 * Shifts this word n bits to the right. 127 * 128 * @param {number} n The number of bits to shift. 129 * 130 * @return {X64Word} A new x64-Word object after shifting. 131 * 132 * @example 133 * 134 * var shifted = x64Word.shiftR(7); 135 */ 136 // shiftR: function (n) { 137 // if (n < 32) { 138 // var low = (this.low >>> n) | (this.high << (32 - n)); 139 // var high = this.high >>> n; 140 // } else { 141 // var low = this.high >>> (n - 32); 142 // var high = 0; 143 // } 144 145 // return X64Word.create(high, low); 146 // }, 147 148 /** 149 * Rotates this word n bits to the left. 150 * 151 * @param {number} n The number of bits to rotate. 152 * 153 * @return {X64Word} A new x64-Word object after rotating. 154 * 155 * @example 156 * 157 * var rotated = x64Word.rotL(25); 158 */ 159 // rotL: function (n) { 160 // return this.shiftL(n).or(this.shiftR(64 - n)); 161 // }, 162 163 /** 164 * Rotates this word n bits to the right. 165 * 166 * @param {number} n The number of bits to rotate. 167 * 168 * @return {X64Word} A new x64-Word object after rotating. 169 * 170 * @example 171 * 172 * var rotated = x64Word.rotR(7); 173 */ 174 // rotR: function (n) { 175 // return this.shiftR(n).or(this.shiftL(64 - n)); 176 // }, 177 178 /** 179 * Adds this word with the passed word. 180 * 181 * @param {X64Word} word The x64-Word to add with this word. 182 * 183 * @return {X64Word} A new x64-Word object after adding. 184 * 185 * @example 186 * 187 * var added = x64Word.add(anotherX64Word); 188 */ 189 // add: function (word) { 190 // var low = (this.low + word.low) | 0; 191 // var carry = (low >>> 0) < (this.low >>> 0) ? 1 : 0; 192 // var high = (this.high + word.high + carry) | 0; 193 194 // return X64Word.create(high, low); 195 // } 196 }); 197 198 /** 199 * An array of 64-bit words. 200 * 201 * @property {Array} words The array of CryptoJS.x64.Word objects. 202 * @property {number} sigBytes The number of significant bytes in this word array. 203 */ 204 var X64WordArray = C_x64.WordArray = Base.extend({ 205 /** 206 * Initializes a newly created word array. 207 * 208 * @param {Array} words (Optional) An array of CryptoJS.x64.Word objects. 209 * @param {number} sigBytes (Optional) The number of significant bytes in the words. 210 * 211 * @example 212 * 213 * var wordArray = CryptoJS.x64.WordArray.create(); 214 * 215 * var wordArray = CryptoJS.x64.WordArray.create([ 216 * CryptoJS.x64.Word.create(0x00010203, 0x04050607), 217 * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f) 218 * ]); 219 * 220 * var wordArray = CryptoJS.x64.WordArray.create([ 221 * CryptoJS.x64.Word.create(0x00010203, 0x04050607), 222 * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f) 223 * ], 10); 224 */ 225 init: function (words, sigBytes) { 226 words = this.words = words || []; 227 228 if (sigBytes != undefined) { 229 this.sigBytes = sigBytes; 230 } else { 231 this.sigBytes = words.length * 8; 232 } 233 }, 234 235 /** 236 * Converts this 64-bit word array to a 32-bit word array. 237 * 238 * @return {CryptoJS.lib.WordArray} This word array's data as a 32-bit word array. 239 * 240 * @example 241 * 242 * var x32WordArray = x64WordArray.toX32(); 243 */ 244 toX32: function () { 245 // Shortcuts 246 var x64Words = this.words; 247 var x64WordsLength = x64Words.length; 248 249 // Convert 250 var x32Words = []; 251 for (var i = 0; i < x64WordsLength; i++) { 252 var x64Word = x64Words[i]; 253 x32Words.push(x64Word.high); 254 x32Words.push(x64Word.low); 255 } 256 257 return X32WordArray.create(x32Words, this.sigBytes); 258 }, 259 260 /** 261 * Creates a copy of this word array. 262 * 263 * @return {X64WordArray} The clone. 264 * 265 * @example 266 * 267 * var clone = x64WordArray.clone(); 268 */ 269 clone: function () { 270 var clone = Base.clone.call(this); 271 272 // Clone "words" array 273 var words = clone.words = this.words.slice(0); 274 275 // Clone each X64Word object 276 var wordsLength = words.length; 277 for (var i = 0; i < wordsLength; i++) { 278 words[i] = words[i].clone(); 279 } 280 281 return clone; 282 } 283 }); 284 }());