You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.7 KiB

4 years ago
  1. /**
  2. * RSA Key Generation Worker.
  3. *
  4. * @author Dave Longley
  5. *
  6. * Copyright (c) 2013 Digital Bazaar, Inc.
  7. */
  8. // worker is built using CommonJS syntax to include all code in one worker file
  9. //importScripts('jsbn.js');
  10. var forge = require('./forge');
  11. require('./jsbn');
  12. // prime constants
  13. var LOW_PRIMES = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509,521,523,541,547,557,563,569,571,577,587,593,599,601,607,613,617,619,631,641,643,647,653,659,661,673,677,683,691,701,709,719,727,733,739,743,751,757,761,769,773,787,797,809,811,821,823,827,829,839,853,857,859,863,877,881,883,887,907,911,919,929,937,941,947,953,967,971,977,983,991,997];
  14. var LP_LIMIT = (1 << 26) / LOW_PRIMES[LOW_PRIMES.length - 1];
  15. var BigInteger = forge.jsbn.BigInteger;
  16. var BIG_TWO = new BigInteger(null);
  17. BIG_TWO.fromInt(2);
  18. self.addEventListener('message', function(e) {
  19. var result = findPrime(e.data);
  20. self.postMessage(result);
  21. });
  22. // start receiving ranges to check
  23. self.postMessage({found: false});
  24. // primes are 30k+i for i = 1, 7, 11, 13, 17, 19, 23, 29
  25. var GCD_30_DELTA = [6, 4, 2, 4, 2, 4, 6, 2];
  26. function findPrime(data) {
  27. // TODO: abstract based on data.algorithm (PRIMEINC vs. others)
  28. // create BigInteger from given random bytes
  29. var num = new BigInteger(data.hex, 16);
  30. /* Note: All primes are of the form 30k+i for i < 30 and gcd(30, i)=1. The
  31. number we are given is always aligned at 30k + 1. Each time the number is
  32. determined not to be prime we add to get to the next 'i', eg: if the number
  33. was at 30k + 1 we add 6. */
  34. var deltaIdx = 0;
  35. // find nearest prime
  36. var workLoad = data.workLoad;
  37. for(var i = 0; i < workLoad; ++i) {
  38. // do primality test
  39. if(isProbablePrime(num)) {
  40. return {found: true, prime: num.toString(16)};
  41. }
  42. // get next potential prime
  43. num.dAddOffset(GCD_30_DELTA[deltaIdx++ % 8], 0);
  44. }
  45. return {found: false};
  46. }
  47. function isProbablePrime(n) {
  48. // divide by low primes, ignore even checks, etc (n alread aligned properly)
  49. var i = 1;
  50. while(i < LOW_PRIMES.length) {
  51. var m = LOW_PRIMES[i];
  52. var j = i + 1;
  53. while(j < LOW_PRIMES.length && m < LP_LIMIT) {
  54. m *= LOW_PRIMES[j++];
  55. }
  56. m = n.modInt(m);
  57. while(i < j) {
  58. if(m % LOW_PRIMES[i++] === 0) {
  59. return false;
  60. }
  61. }
  62. }
  63. return runMillerRabin(n);
  64. }
  65. // HAC 4.24, Miller-Rabin
  66. function runMillerRabin(n) {
  67. // n1 = n - 1
  68. var n1 = n.subtract(BigInteger.ONE);
  69. // get s and d such that n1 = 2^s * d
  70. var s = n1.getLowestSetBit();
  71. if(s <= 0) {
  72. return false;
  73. }
  74. var d = n1.shiftRight(s);
  75. var k = _getMillerRabinTests(n.bitLength());
  76. var prng = getPrng();
  77. var a;
  78. for(var i = 0; i < k; ++i) {
  79. // select witness 'a' at random from between 1 and n - 1
  80. do {
  81. a = new BigInteger(n.bitLength(), prng);
  82. } while(a.compareTo(BigInteger.ONE) <= 0 || a.compareTo(n1) >= 0);
  83. /* See if 'a' is a composite witness. */
  84. // x = a^d mod n
  85. var x = a.modPow(d, n);
  86. // probably prime
  87. if(x.compareTo(BigInteger.ONE) === 0 || x.compareTo(n1) === 0) {
  88. continue;
  89. }
  90. var j = s;
  91. while(--j) {
  92. // x = x^2 mod a
  93. x = x.modPowInt(2, n);
  94. // 'n' is composite because no previous x == -1 mod n
  95. if(x.compareTo(BigInteger.ONE) === 0) {
  96. return false;
  97. }
  98. // x == -1 mod n, so probably prime
  99. if(x.compareTo(n1) === 0) {
  100. break;
  101. }
  102. }
  103. // 'x' is first_x^(n1/2) and is not +/- 1, so 'n' is not prime
  104. if(j === 0) {
  105. return false;
  106. }
  107. }
  108. return true;
  109. }
  110. // get pseudo random number generator
  111. function getPrng() {
  112. // create prng with api that matches BigInteger secure random
  113. return {
  114. // x is an array to fill with bytes
  115. nextBytes: function(x) {
  116. for(var i = 0; i < x.length; ++i) {
  117. x[i] = Math.floor(Math.random() * 0xFF);
  118. }
  119. }
  120. };
  121. }
  122. /**
  123. * Returns the required number of Miller-Rabin tests to generate a
  124. * prime with an error probability of (1/2)^80.
  125. *
  126. * See Handbook of Applied Cryptography Chapter 4, Table 4.4.
  127. *
  128. * @param bits the bit size.
  129. *
  130. * @return the required number of iterations.
  131. */
  132. function _getMillerRabinTests(bits) {
  133. if(bits <= 100) return 27;
  134. if(bits <= 150) return 18;
  135. if(bits <= 200) return 15;
  136. if(bits <= 250) return 12;
  137. if(bits <= 300) return 9;
  138. if(bits <= 350) return 8;
  139. if(bits <= 400) return 7;
  140. if(bits <= 500) return 6;
  141. if(bits <= 600) return 5;
  142. if(bits <= 800) return 4;
  143. if(bits <= 1250) return 3;
  144. return 2;
  145. }