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.

131 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software 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. String RSAKey::toString() const
  45. {
  46. return part1.toString (16) + "," + part2.toString (16);
  47. }
  48. bool RSAKey::applyToValue (BigInteger& value) const
  49. {
  50. if (part1.isZero() || part2.isZero() || value <= 0)
  51. {
  52. jassertfalse; // using an uninitialised key
  53. value.clear();
  54. return false;
  55. }
  56. BigInteger result;
  57. while (! value.isZero())
  58. {
  59. result *= part2;
  60. BigInteger remainder;
  61. value.divideBy (part2, remainder);
  62. remainder.exponentModulo (part1, part2);
  63. result += remainder;
  64. }
  65. value.swapWith (result);
  66. return true;
  67. }
  68. BigInteger RSAKey::findBestCommonDivisor (const BigInteger& p, const BigInteger& q)
  69. {
  70. // try 3, 5, 9, 17, etc first because these only contain 2 bits and so
  71. // are fast to divide + multiply
  72. for (int i = 2; i <= 65536; i *= 2)
  73. {
  74. const BigInteger e (1 + i);
  75. if (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne())
  76. return e;
  77. }
  78. BigInteger e (4);
  79. while (! (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne()))
  80. ++e;
  81. return e;
  82. }
  83. void RSAKey::createKeyPair (RSAKey& publicKey, RSAKey& privateKey,
  84. const int numBits, const int* randomSeeds, const int numRandomSeeds)
  85. {
  86. jassert (numBits > 16); // not much point using less than this..
  87. jassert (numRandomSeeds == 0 || numRandomSeeds >= 2); // you need to provide plenty of seeds here!
  88. BigInteger p (Primes::createProbablePrime (numBits / 2, 30, randomSeeds, numRandomSeeds / 2));
  89. BigInteger q (Primes::createProbablePrime (numBits - numBits / 2, 30, randomSeeds == nullptr ? 0 : (randomSeeds + numRandomSeeds / 2), numRandomSeeds - numRandomSeeds / 2));
  90. const BigInteger n (p * q);
  91. const BigInteger m (--p * --q);
  92. const BigInteger e (findBestCommonDivisor (p, q));
  93. BigInteger d (e);
  94. d.inverseModulo (m);
  95. publicKey.part1 = e;
  96. publicKey.part2 = n;
  97. privateKey.part1 = d;
  98. privateKey.part2 = n;
  99. }