The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

136 lines
3.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. RSAKey::RSAKey()
  18. {
  19. }
  20. RSAKey::RSAKey (const String& s)
  21. {
  22. if (s.containsChar (','))
  23. {
  24. part1.parseString (s.upToFirstOccurrenceOf (",", false, false), 16);
  25. part2.parseString (s.fromFirstOccurrenceOf (",", false, false), 16);
  26. }
  27. else
  28. {
  29. // the string needs to be two hex numbers, comma-separated..
  30. jassertfalse;
  31. }
  32. }
  33. RSAKey::~RSAKey()
  34. {
  35. }
  36. bool RSAKey::operator== (const RSAKey& other) const noexcept
  37. {
  38. return part1 == other.part1 && part2 == other.part2;
  39. }
  40. bool RSAKey::operator!= (const RSAKey& other) const noexcept
  41. {
  42. return ! operator== (other);
  43. }
  44. bool RSAKey::isValid() const noexcept
  45. {
  46. return operator!= (RSAKey());
  47. }
  48. String RSAKey::toString() const
  49. {
  50. return part1.toString (16) + "," + part2.toString (16);
  51. }
  52. bool RSAKey::applyToValue (BigInteger& value) const
  53. {
  54. if (part1.isZero() || part2.isZero() || value <= 0)
  55. {
  56. jassertfalse; // using an uninitialised key
  57. value.clear();
  58. return false;
  59. }
  60. BigInteger result;
  61. while (! value.isZero())
  62. {
  63. result *= part2;
  64. BigInteger remainder;
  65. value.divideBy (part2, remainder);
  66. remainder.exponentModulo (part1, part2);
  67. result += remainder;
  68. }
  69. value.swapWith (result);
  70. return true;
  71. }
  72. BigInteger RSAKey::findBestCommonDivisor (const BigInteger& p, const BigInteger& q)
  73. {
  74. // try 3, 5, 9, 17, etc first because these only contain 2 bits and so
  75. // are fast to divide + multiply
  76. for (int i = 2; i <= 65536; i *= 2)
  77. {
  78. const BigInteger e (1 + i);
  79. if (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne())
  80. return e;
  81. }
  82. BigInteger e (4);
  83. while (! (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne()))
  84. ++e;
  85. return e;
  86. }
  87. void RSAKey::createKeyPair (RSAKey& publicKey, RSAKey& privateKey,
  88. const int numBits, const int* randomSeeds, const int numRandomSeeds)
  89. {
  90. jassert (numBits > 16); // not much point using less than this..
  91. jassert (numRandomSeeds == 0 || numRandomSeeds >= 2); // you need to provide plenty of seeds here!
  92. BigInteger p (Primes::createProbablePrime (numBits / 2, 30, randomSeeds, numRandomSeeds / 2));
  93. BigInteger q (Primes::createProbablePrime (numBits - numBits / 2, 30, randomSeeds == nullptr ? 0 : (randomSeeds + numRandomSeeds / 2), numRandomSeeds - numRandomSeeds / 2));
  94. const BigInteger n (p * q);
  95. const BigInteger m (--p * --q);
  96. const BigInteger e (findBestCommonDivisor (p, q));
  97. BigInteger d (e);
  98. d.inverseModulo (m);
  99. publicKey.part1 = e;
  100. publicKey.part2 = n;
  101. privateKey.part1 = d;
  102. privateKey.part2 = n;
  103. }