myrsa.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. (function($w) {
  2. if(typeof $w.RSAUtils === 'undefined')
  3. var RSAUtils = $w.RSAUtils = {};
  4. var biRadixBase = 2;
  5. var biRadixBits = 16;
  6. var bitsPerDigit = biRadixBits;
  7. var biRadix = 1 << 16; // = 2^16 = 65536
  8. var biHalfRadix = biRadix >>> 1;
  9. var biRadixSquared = biRadix * biRadix;
  10. var maxDigitVal = biRadix - 1;
  11. var maxInteger = 9999999999999998;
  12. //maxDigits:
  13. //Change this to accommodate your largest number size. Use setMaxDigits()
  14. //to change it!
  15. //
  16. //In general, if you're working with numbers of size N bits, you'll need 2*N
  17. //bits of storage. Each digit holds 16 bits. So, a 1024-bit key will need
  18. //
  19. //1024 * 2 / 16 = 128 digits of storage.
  20. //
  21. var maxDigits;
  22. var ZERO_ARRAY;
  23. var bigZero, bigOne;
  24. var BigInt = $w.BigInt = function(flag) {
  25. if (typeof flag == "boolean" && flag == true) {
  26. this.digits = null;
  27. } else {
  28. this.digits = ZERO_ARRAY.slice(0);
  29. }
  30. this.isNeg = false;
  31. };
  32. RSAUtils.setMaxDigits = function(value) {
  33. maxDigits = value;
  34. ZERO_ARRAY = new Array(maxDigits);
  35. for (var iza = 0; iza < ZERO_ARRAY.length; iza++) ZERO_ARRAY[iza] = 0;
  36. bigZero = new BigInt();
  37. bigOne = new BigInt();
  38. bigOne.digits[0] = 1;
  39. };
  40. RSAUtils.setMaxDigits(20);
  41. //The maximum number of digits in base 10 you can convert to an
  42. //integer without JavaScript throwing up on you.
  43. var dpl10 = 15;
  44. RSAUtils.biFromNumber = function(i) {
  45. var result = new BigInt();
  46. result.isNeg = i < 0;
  47. i = Math.abs(i);
  48. var j = 0;
  49. while (i > 0) {
  50. result.digits[j++] = i & maxDigitVal;
  51. i = Math.floor(i / biRadix);
  52. }
  53. return result;
  54. };
  55. //lr10 = 10 ^ dpl10
  56. var lr10 = RSAUtils.biFromNumber(1000000000000000);
  57. RSAUtils.biFromDecimal = function(s) {
  58. var isNeg = s.charAt(0) == '-';
  59. var i = isNeg ? 1 : 0;
  60. var result;
  61. // Skip leading zeros.
  62. while (i < s.length && s.charAt(i) == '0') ++i;
  63. if (i == s.length) {
  64. result = new BigInt();
  65. }
  66. else {
  67. var digitCount = s.length - i;
  68. var fgl = digitCount % dpl10;
  69. if (fgl == 0) fgl = dpl10;
  70. result = RSAUtils.biFromNumber(Number(s.substr(i, fgl)));
  71. i += fgl;
  72. while (i < s.length) {
  73. result = RSAUtils.biAdd(RSAUtils.biMultiply(result, lr10),
  74. RSAUtils.biFromNumber(Number(s.substr(i, dpl10))));
  75. i += dpl10;
  76. }
  77. result.isNeg = isNeg;
  78. }
  79. return result;
  80. };
  81. RSAUtils.biCopy = function(bi) {
  82. var result = new BigInt(true);
  83. result.digits = bi.digits.slice(0);
  84. result.isNeg = bi.isNeg;
  85. return result;
  86. };
  87. RSAUtils.reverseStr = function(s) {
  88. var result = "";
  89. for (var i = s.length - 1; i > -1; --i) {
  90. result += s.charAt(i);
  91. }
  92. return result;
  93. };
  94. var hexatrigesimalToChar = [
  95. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  96. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  97. 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  98. 'u', 'v', 'w', 'x', 'y', 'z'
  99. ];
  100. RSAUtils.biToString = function(x, radix) { // 2 <= radix <= 36
  101. var b = new BigInt();
  102. b.digits[0] = radix;
  103. var qr = RSAUtils.biDivideModulo(x, b);
  104. var result = hexatrigesimalToChar[qr[1].digits[0]];
  105. while (RSAUtils.biCompare(qr[0], bigZero) == 1) {
  106. qr = RSAUtils.biDivideModulo(qr[0], b);
  107. digit = qr[1].digits[0];
  108. result += hexatrigesimalToChar[qr[1].digits[0]];
  109. }
  110. return (x.isNeg ? "-" : "") + RSAUtils.reverseStr(result);
  111. };
  112. RSAUtils.biToDecimal = function(x) {
  113. var b = new BigInt();
  114. b.digits[0] = 10;
  115. var qr = RSAUtils.biDivideModulo(x, b);
  116. var result = String(qr[1].digits[0]);
  117. while (RSAUtils.biCompare(qr[0], bigZero) == 1) {
  118. qr = RSAUtils.biDivideModulo(qr[0], b);
  119. result += String(qr[1].digits[0]);
  120. }
  121. return (x.isNeg ? "-" : "") + RSAUtils.reverseStr(result);
  122. };
  123. var hexToChar = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  124. 'a', 'b', 'c', 'd', 'e', 'f'];
  125. RSAUtils.digitToHex = function(n) {
  126. var mask = 0xf;
  127. var result = "";
  128. for (i = 0; i < 4; ++i) {
  129. result += hexToChar[n & mask];
  130. n >>>= 4;
  131. }
  132. return RSAUtils.reverseStr(result);
  133. };
  134. RSAUtils.biToHex = function(x) {
  135. var result = "";
  136. var n = RSAUtils.biHighIndex(x);
  137. for (var i = RSAUtils.biHighIndex(x); i > -1; --i) {
  138. result += RSAUtils.digitToHex(x.digits[i]);
  139. }
  140. return result;
  141. };
  142. RSAUtils.charToHex = function(c) {
  143. var ZERO = 48;
  144. var NINE = ZERO + 9;
  145. var littleA = 97;
  146. var littleZ = littleA + 25;
  147. var bigA = 65;
  148. var bigZ = 65 + 25;
  149. var result;
  150. if (c >= ZERO && c <= NINE) {
  151. result = c - ZERO;
  152. } else if (c >= bigA && c <= bigZ) {
  153. result = 10 + c - bigA;
  154. } else if (c >= littleA && c <= littleZ) {
  155. result = 10 + c - littleA;
  156. } else {
  157. result = 0;
  158. }
  159. return result;
  160. };
  161. RSAUtils.hexToDigit = function(s) {
  162. var result = 0;
  163. var sl = Math.min(s.length, 4);
  164. for (var i = 0; i < sl; ++i) {
  165. result <<= 4;
  166. result |= RSAUtils.charToHex(s.charCodeAt(i));
  167. }
  168. return result;
  169. };
  170. RSAUtils.biFromHex = function(s) {
  171. var result = new BigInt();
  172. var sl = s.length;
  173. for (var i = sl, j = 0; i > 0; i -= 4, ++j) {
  174. result.digits[j] = RSAUtils.hexToDigit(s.substr(Math.max(i - 4, 0), Math.min(i, 4)));
  175. }
  176. return result;
  177. };
  178. RSAUtils.biFromString = function(s, radix) {
  179. var isNeg = s.charAt(0) == '-';
  180. var istop = isNeg ? 1 : 0;
  181. var result = new BigInt();
  182. var place = new BigInt();
  183. place.digits[0] = 1; // radix^0
  184. for (var i = s.length - 1; i >= istop; i--) {
  185. var c = s.charCodeAt(i);
  186. var digit = RSAUtils.charToHex(c);
  187. var biDigit = RSAUtils.biMultiplyDigit(place, digit);
  188. result = RSAUtils.biAdd(result, biDigit);
  189. place = RSAUtils.biMultiplyDigit(place, radix);
  190. }
  191. result.isNeg = isNeg;
  192. return result;
  193. };
  194. RSAUtils.biDump = function(b) {
  195. return (b.isNeg ? "-" : "") + b.digits.join(" ");
  196. };
  197. RSAUtils.biAdd = function(x, y) {
  198. var result;
  199. if (x.isNeg != y.isNeg) {
  200. y.isNeg = !y.isNeg;
  201. result = RSAUtils.biSubtract(x, y);
  202. y.isNeg = !y.isNeg;
  203. }
  204. else {
  205. result = new BigInt();
  206. var c = 0;
  207. var n;
  208. for (var i = 0; i < x.digits.length; ++i) {
  209. n = x.digits[i] + y.digits[i] + c;
  210. result.digits[i] = n % biRadix;
  211. c = Number(n >= biRadix);
  212. }
  213. result.isNeg = x.isNeg;
  214. }
  215. return result;
  216. };
  217. RSAUtils.biSubtract = function(x, y) {
  218. var result;
  219. if (x.isNeg != y.isNeg) {
  220. y.isNeg = !y.isNeg;
  221. result = RSAUtils.biAdd(x, y);
  222. y.isNeg = !y.isNeg;
  223. } else {
  224. result = new BigInt();
  225. var n, c;
  226. c = 0;
  227. for (var i = 0; i < x.digits.length; ++i) {
  228. n = x.digits[i] - y.digits[i] + c;
  229. result.digits[i] = n % biRadix;
  230. // Stupid non-conforming modulus operation.
  231. if (result.digits[i] < 0) result.digits[i] += biRadix;
  232. c = 0 - Number(n < 0);
  233. }
  234. // Fix up the negative sign, if any.
  235. if (c == -1) {
  236. c = 0;
  237. for (var i = 0; i < x.digits.length; ++i) {
  238. n = 0 - result.digits[i] + c;
  239. result.digits[i] = n % biRadix;
  240. // Stupid non-conforming modulus operation.
  241. if (result.digits[i] < 0) result.digits[i] += biRadix;
  242. c = 0 - Number(n < 0);
  243. }
  244. // Result is opposite sign of arguments.
  245. result.isNeg = !x.isNeg;
  246. } else {
  247. // Result is same sign.
  248. result.isNeg = x.isNeg;
  249. }
  250. }
  251. return result;
  252. };
  253. RSAUtils.biHighIndex = function(x) {
  254. var result = x.digits.length - 1;
  255. while (result > 0 && x.digits[result] == 0) --result;
  256. return result;
  257. };
  258. RSAUtils.biNumBits = function(x) {
  259. var n = RSAUtils.biHighIndex(x);
  260. var d = x.digits[n];
  261. var m = (n + 1) * bitsPerDigit;
  262. var result;
  263. for (result = m; result > m - bitsPerDigit; --result) {
  264. if ((d & 0x8000) != 0) break;
  265. d <<= 1;
  266. }
  267. return result;
  268. };
  269. RSAUtils.biMultiply = function(x, y) {
  270. var result = new BigInt();
  271. var c;
  272. var n = RSAUtils.biHighIndex(x);
  273. var t = RSAUtils.biHighIndex(y);
  274. var u, uv, k;
  275. for (var i = 0; i <= t; ++i) {
  276. c = 0;
  277. k = i;
  278. for (j = 0; j <= n; ++j, ++k) {
  279. uv = result.digits[k] + x.digits[j] * y.digits[i] + c;
  280. result.digits[k] = uv & maxDigitVal;
  281. c = uv >>> biRadixBits;
  282. //c = Math.floor(uv / biRadix);
  283. }
  284. result.digits[i + n + 1] = c;
  285. }
  286. // Someone give me a logical xor, please.
  287. result.isNeg = x.isNeg != y.isNeg;
  288. return result;
  289. };
  290. RSAUtils.biMultiplyDigit = function(x, y) {
  291. var n, c, uv;
  292. result = new BigInt();
  293. n = RSAUtils.biHighIndex(x);
  294. c = 0;
  295. for (var j = 0; j <= n; ++j) {
  296. uv = result.digits[j] + x.digits[j] * y + c;
  297. result.digits[j] = uv & maxDigitVal;
  298. c = uv >>> biRadixBits;
  299. //c = Math.floor(uv / biRadix);
  300. }
  301. result.digits[1 + n] = c;
  302. return result;
  303. };
  304. RSAUtils.arrayCopy = function(src, srcStart, dest, destStart, n) {
  305. var m = Math.min(srcStart + n, src.length);
  306. for (var i = srcStart, j = destStart; i < m; ++i, ++j) {
  307. dest[j] = src[i];
  308. }
  309. };
  310. var highBitMasks = [0x0000, 0x8000, 0xC000, 0xE000, 0xF000, 0xF800,
  311. 0xFC00, 0xFE00, 0xFF00, 0xFF80, 0xFFC0, 0xFFE0,
  312. 0xFFF0, 0xFFF8, 0xFFFC, 0xFFFE, 0xFFFF];
  313. RSAUtils.biShiftLeft = function(x, n) {
  314. var digitCount = Math.floor(n / bitsPerDigit);
  315. var result = new BigInt();
  316. RSAUtils.arrayCopy(x.digits, 0, result.digits, digitCount,
  317. result.digits.length - digitCount);
  318. var bits = n % bitsPerDigit;
  319. var rightBits = bitsPerDigit - bits;
  320. for (var i = result.digits.length - 1, i1 = i - 1; i > 0; --i, --i1) {
  321. result.digits[i] = ((result.digits[i] << bits) & maxDigitVal) |
  322. ((result.digits[i1] & highBitMasks[bits]) >>>
  323. (rightBits));
  324. }
  325. result.digits[0] = ((result.digits[i] << bits) & maxDigitVal);
  326. result.isNeg = x.isNeg;
  327. return result;
  328. };
  329. var lowBitMasks = [0x0000, 0x0001, 0x0003, 0x0007, 0x000F, 0x001F,
  330. 0x003F, 0x007F, 0x00FF, 0x01FF, 0x03FF, 0x07FF,
  331. 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF];
  332. RSAUtils.biShiftRight = function(x, n) {
  333. var digitCount = Math.floor(n / bitsPerDigit);
  334. var result = new BigInt();
  335. RSAUtils.arrayCopy(x.digits, digitCount, result.digits, 0,
  336. x.digits.length - digitCount);
  337. var bits = n % bitsPerDigit;
  338. var leftBits = bitsPerDigit - bits;
  339. for (var i = 0, i1 = i + 1; i < result.digits.length - 1; ++i, ++i1) {
  340. result.digits[i] = (result.digits[i] >>> bits) |
  341. ((result.digits[i1] & lowBitMasks[bits]) << leftBits);
  342. }
  343. result.digits[result.digits.length - 1] >>>= bits;
  344. result.isNeg = x.isNeg;
  345. return result;
  346. };
  347. RSAUtils.biMultiplyByRadixPower = function(x, n) {
  348. var result = new BigInt();
  349. RSAUtils.arrayCopy(x.digits, 0, result.digits, n, result.digits.length - n);
  350. return result;
  351. };
  352. RSAUtils.biDivideByRadixPower = function(x, n) {
  353. var result = new BigInt();
  354. RSAUtils.arrayCopy(x.digits, n, result.digits, 0, result.digits.length - n);
  355. return result;
  356. };
  357. RSAUtils.biModuloByRadixPower = function(x, n) {
  358. var result = new BigInt();
  359. RSAUtils.arrayCopy(x.digits, 0, result.digits, 0, n);
  360. return result;
  361. };
  362. RSAUtils.biCompare = function(x, y) {
  363. if (x.isNeg != y.isNeg) {
  364. return 1 - 2 * Number(x.isNeg);
  365. }
  366. for (var i = x.digits.length - 1; i >= 0; --i) {
  367. if (x.digits[i] != y.digits[i]) {
  368. if (x.isNeg) {
  369. return 1 - 2 * Number(x.digits[i] > y.digits[i]);
  370. } else {
  371. return 1 - 2 * Number(x.digits[i] < y.digits[i]);
  372. }
  373. }
  374. }
  375. return 0;
  376. };
  377. RSAUtils.biDivideModulo = function(x, y) {
  378. var nb = RSAUtils.biNumBits(x);
  379. var tb = RSAUtils.biNumBits(y);
  380. var origYIsNeg = y.isNeg;
  381. var q, r;
  382. if (nb < tb) {
  383. // |x| < |y|
  384. if (x.isNeg) {
  385. q = RSAUtils.biCopy(bigOne);
  386. q.isNeg = !y.isNeg;
  387. x.isNeg = false;
  388. y.isNeg = false;
  389. r = biSubtract(y, x);
  390. // Restore signs, 'cause they're references.
  391. x.isNeg = true;
  392. y.isNeg = origYIsNeg;
  393. } else {
  394. q = new BigInt();
  395. r = RSAUtils.biCopy(x);
  396. }
  397. return [q, r];
  398. }
  399. q = new BigInt();
  400. r = x;
  401. // Normalize Y.
  402. var t = Math.ceil(tb / bitsPerDigit) - 1;
  403. var lambda = 0;
  404. while (y.digits[t] < biHalfRadix) {
  405. y = RSAUtils.biShiftLeft(y, 1);
  406. ++lambda;
  407. ++tb;
  408. t = Math.ceil(tb / bitsPerDigit) - 1;
  409. }
  410. // Shift r over to keep the quotient constant. We'll shift the
  411. // remainder back at the end.
  412. r = RSAUtils.biShiftLeft(r, lambda);
  413. nb += lambda; // Update the bit count for x.
  414. var n = Math.ceil(nb / bitsPerDigit) - 1;
  415. var b = RSAUtils.biMultiplyByRadixPower(y, n - t);
  416. while (RSAUtils.biCompare(r, b) != -1) {
  417. ++q.digits[n - t];
  418. r = RSAUtils.biSubtract(r, b);
  419. }
  420. for (var i = n; i > t; --i) {
  421. var ri = (i >= r.digits.length) ? 0 : r.digits[i];
  422. var ri1 = (i - 1 >= r.digits.length) ? 0 : r.digits[i - 1];
  423. var ri2 = (i - 2 >= r.digits.length) ? 0 : r.digits[i - 2];
  424. var yt = (t >= y.digits.length) ? 0 : y.digits[t];
  425. var yt1 = (t - 1 >= y.digits.length) ? 0 : y.digits[t - 1];
  426. if (ri == yt) {
  427. q.digits[i - t - 1] = maxDigitVal;
  428. } else {
  429. q.digits[i - t - 1] = Math.floor((ri * biRadix + ri1) / yt);
  430. }
  431. var c1 = q.digits[i - t - 1] * ((yt * biRadix) + yt1);
  432. var c2 = (ri * biRadixSquared) + ((ri1 * biRadix) + ri2);
  433. while (c1 > c2) {
  434. --q.digits[i - t - 1];
  435. c1 = q.digits[i - t - 1] * ((yt * biRadix) | yt1);
  436. c2 = (ri * biRadix * biRadix) + ((ri1 * biRadix) + ri2);
  437. }
  438. b = RSAUtils.biMultiplyByRadixPower(y, i - t - 1);
  439. r = RSAUtils.biSubtract(r, RSAUtils.biMultiplyDigit(b, q.digits[i - t - 1]));
  440. if (r.isNeg) {
  441. r = RSAUtils.biAdd(r, b);
  442. --q.digits[i - t - 1];
  443. }
  444. }
  445. r = RSAUtils.biShiftRight(r, lambda);
  446. // Fiddle with the signs and stuff to make sure that 0 <= r < y.
  447. q.isNeg = x.isNeg != origYIsNeg;
  448. if (x.isNeg) {
  449. if (origYIsNeg) {
  450. q = RSAUtils.biAdd(q, bigOne);
  451. } else {
  452. q = RSAUtils.biSubtract(q, bigOne);
  453. }
  454. y = RSAUtils.biShiftRight(y, lambda);
  455. r = RSAUtils.biSubtract(y, r);
  456. }
  457. // Check for the unbelievably stupid degenerate case of r == -0.
  458. if (r.digits[0] == 0 && RSAUtils.biHighIndex(r) == 0) r.isNeg = false;
  459. return [q, r];
  460. };
  461. RSAUtils.biDivide = function(x, y) {
  462. return RSAUtils.biDivideModulo(x, y)[0];
  463. };
  464. RSAUtils.biModulo = function(x, y) {
  465. return RSAUtils.biDivideModulo(x, y)[1];
  466. };
  467. RSAUtils.biMultiplyMod = function(x, y, m) {
  468. return RSAUtils.biModulo(RSAUtils.biMultiply(x, y), m);
  469. };
  470. RSAUtils.biPow = function(x, y) {
  471. var result = bigOne;
  472. var a = x;
  473. while (true) {
  474. if ((y & 1) != 0) result = RSAUtils.biMultiply(result, a);
  475. y >>= 1;
  476. if (y == 0) break;
  477. a = RSAUtils.biMultiply(a, a);
  478. }
  479. return result;
  480. };
  481. RSAUtils.biPowMod = function(x, y, m) {
  482. var result = bigOne;
  483. var a = x;
  484. var k = y;
  485. while (true) {
  486. if ((k.digits[0] & 1) != 0) result = RSAUtils.biMultiplyMod(result, a, m);
  487. k = RSAUtils.biShiftRight(k, 1);
  488. if (k.digits[0] == 0 && RSAUtils.biHighIndex(k) == 0) break;
  489. a = RSAUtils.biMultiplyMod(a, a, m);
  490. }
  491. return result;
  492. };
  493. $w.BarrettMu = function(m) {
  494. this.modulus = RSAUtils.biCopy(m);
  495. this.k = RSAUtils.biHighIndex(this.modulus) + 1;
  496. var b2k = new BigInt();
  497. b2k.digits[2 * this.k] = 1; // b2k = b^(2k)
  498. this.mu = RSAUtils.biDivide(b2k, this.modulus);
  499. this.bkplus1 = new BigInt();
  500. this.bkplus1.digits[this.k + 1] = 1; // bkplus1 = b^(k+1)
  501. this.modulo = BarrettMu_modulo;
  502. this.multiplyMod = BarrettMu_multiplyMod;
  503. this.powMod = BarrettMu_powMod;
  504. };
  505. function BarrettMu_modulo(x) {
  506. var $dmath = RSAUtils;
  507. var q1 = $dmath.biDivideByRadixPower(x, this.k - 1);
  508. var q2 = $dmath.biMultiply(q1, this.mu);
  509. var q3 = $dmath.biDivideByRadixPower(q2, this.k + 1);
  510. var r1 = $dmath.biModuloByRadixPower(x, this.k + 1);
  511. var r2term = $dmath.biMultiply(q3, this.modulus);
  512. var r2 = $dmath.biModuloByRadixPower(r2term, this.k + 1);
  513. var r = $dmath.biSubtract(r1, r2);
  514. if (r.isNeg) {
  515. r = $dmath.biAdd(r, this.bkplus1);
  516. }
  517. var rgtem = $dmath.biCompare(r, this.modulus) >= 0;
  518. while (rgtem) {
  519. r = $dmath.biSubtract(r, this.modulus);
  520. rgtem = $dmath.biCompare(r, this.modulus) >= 0;
  521. }
  522. return r;
  523. }
  524. function BarrettMu_multiplyMod(x, y) {
  525. var xy = RSAUtils.biMultiply(x, y);
  526. return this.modulo(xy);
  527. }
  528. function BarrettMu_powMod(x, y) {
  529. var result = new BigInt();
  530. result.digits[0] = 1;
  531. var a = x;
  532. var k = y;
  533. while (true) {
  534. if ((k.digits[0] & 1) != 0) result = this.multiplyMod(result, a);
  535. k = RSAUtils.biShiftRight(k, 1);
  536. if (k.digits[0] == 0 && RSAUtils.biHighIndex(k) == 0) break;
  537. a = this.multiplyMod(a, a);
  538. }
  539. return result;
  540. }
  541. var RSAKeyPair = function(encryptionExponent, decryptionExponent, modulus) {
  542. var $dmath = RSAUtils;
  543. this.e = $dmath.biFromHex(encryptionExponent);
  544. this.d = $dmath.biFromHex(decryptionExponent);
  545. this.m = $dmath.biFromHex(modulus);
  546. // We can do two bytes per digit, so
  547. // chunkSize = 2 * (number of digits in modulus - 1).
  548. // Since biHighIndex returns the high index, not the number of digits, 1 has
  549. // already been subtracted.
  550. this.chunkSize = 2 * $dmath.biHighIndex(this.m);
  551. this.radix = 16;
  552. this.barrett = new $w.BarrettMu(this.m);
  553. };
  554. RSAUtils.getKeyPair = function(encryptionExponent, decryptionExponent, modulus) {
  555. return new RSAKeyPair(encryptionExponent, decryptionExponent, modulus);
  556. };
  557. if(typeof $w.twoDigit === 'undefined') {
  558. $w.twoDigit = function(n) {
  559. return (n < 10 ? "0" : "") + String(n);
  560. };
  561. }
  562. // Altered by Rob Saunders (rob@robsaunders.net). New routine pads the
  563. // string after it has been converted to an array. This fixes an
  564. // incompatibility with Flash MX's ActionScript.
  565. RSAUtils.encryptedString = function(key, s) {
  566. var a = [];
  567. var sl = s.length;
  568. var i = 0;
  569. while (i < sl) {
  570. a[i] = s.charCodeAt(i);
  571. i++;
  572. }
  573. while (a.length % key.chunkSize != 0) {
  574. a[i++] = 0;
  575. }
  576. var al = a.length;
  577. var result = "";
  578. var j, k, block;
  579. for (i = 0; i < al; i += key.chunkSize) {
  580. block = new BigInt();
  581. j = 0;
  582. for (k = i; k < i + key.chunkSize; ++j) {
  583. block.digits[j] = a[k++];
  584. block.digits[j] += a[k++] << 8;
  585. }
  586. var crypt = key.barrett.powMod(block, key.e);
  587. var text = key.radix == 16 ? RSAUtils.biToHex(crypt) : RSAUtils.biToString(crypt, key.radix);
  588. result += text + " ";
  589. }
  590. return result.substring(0, result.length - 1); // Remove last space.
  591. };
  592. RSAUtils.decryptedString = function(key, s) {
  593. var blocks = s.split(" ");
  594. var result = "";
  595. var i, j, block;
  596. for (i = 0; i < blocks.length; ++i) {
  597. var bi;
  598. if (key.radix == 16) {
  599. bi = RSAUtils.biFromHex(blocks[i]);
  600. }
  601. else {
  602. bi = RSAUtils.biFromString(blocks[i], key.radix);
  603. }
  604. block = key.barrett.powMod(bi, key.d);
  605. for (j = 0; j <= RSAUtils.biHighIndex(block); ++j) {
  606. result += String.fromCharCode(block.digits[j] & 255,
  607. block.digits[j] >> 8);
  608. }
  609. }
  610. // Remove trailing null, if any.
  611. if (result.charCodeAt(result.length - 1) == 0) {
  612. result = result.substring(0, result.length - 1);
  613. }
  614. return result;
  615. };
  616. RSAUtils.setMaxDigits(130);
  617. })(window);