x64-core.js 8.2 KB

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