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.

138 lines
3.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. RSAKey::RSAKey()
  20. {
  21. }
  22. RSAKey::RSAKey (const String& s)
  23. {
  24. if (s.containsChar (','))
  25. {
  26. part1.parseString (s.upToFirstOccurrenceOf (",", false, false), 16);
  27. part2.parseString (s.fromFirstOccurrenceOf (",", false, false), 16);
  28. }
  29. else
  30. {
  31. // the string needs to be two hex numbers, comma-separated..
  32. jassertfalse;
  33. }
  34. }
  35. RSAKey::~RSAKey()
  36. {
  37. }
  38. bool RSAKey::operator== (const RSAKey& other) const noexcept
  39. {
  40. return part1 == other.part1 && part2 == other.part2;
  41. }
  42. bool RSAKey::operator!= (const RSAKey& other) const noexcept
  43. {
  44. return ! operator== (other);
  45. }
  46. bool RSAKey::isValid() const noexcept
  47. {
  48. return operator!= (RSAKey());
  49. }
  50. String RSAKey::toString() const
  51. {
  52. return part1.toString (16) + "," + part2.toString (16);
  53. }
  54. bool RSAKey::applyToValue (BigInteger& value) const
  55. {
  56. if (part1.isZero() || part2.isZero() || value <= 0)
  57. {
  58. jassertfalse; // using an uninitialised key
  59. value.clear();
  60. return false;
  61. }
  62. BigInteger result;
  63. while (! value.isZero())
  64. {
  65. result *= part2;
  66. BigInteger remainder;
  67. value.divideBy (part2, remainder);
  68. remainder.exponentModulo (part1, part2);
  69. result += remainder;
  70. }
  71. value.swapWith (result);
  72. return true;
  73. }
  74. BigInteger RSAKey::findBestCommonDivisor (const BigInteger& p, const BigInteger& q)
  75. {
  76. // try 3, 5, 9, 17, etc first because these only contain 2 bits and so
  77. // are fast to divide + multiply
  78. for (int i = 2; i <= 65536; i *= 2)
  79. {
  80. const BigInteger e (1 + i);
  81. if (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne())
  82. return e;
  83. }
  84. BigInteger e (4);
  85. while (! (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne()))
  86. ++e;
  87. return e;
  88. }
  89. void RSAKey::createKeyPair (RSAKey& publicKey, RSAKey& privateKey,
  90. const int numBits, const int* randomSeeds, const int numRandomSeeds)
  91. {
  92. jassert (numBits > 16); // not much point using less than this..
  93. jassert (numRandomSeeds == 0 || numRandomSeeds >= 2); // you need to provide plenty of seeds here!
  94. BigInteger p (Primes::createProbablePrime (numBits / 2, 30, randomSeeds, numRandomSeeds / 2));
  95. BigInteger q (Primes::createProbablePrime (numBits - numBits / 2, 30, randomSeeds == nullptr ? 0 : (randomSeeds + numRandomSeeds / 2), numRandomSeeds - numRandomSeeds / 2));
  96. const BigInteger n (p * q);
  97. const BigInteger m (--p * --q);
  98. const BigInteger e (findBestCommonDivisor (p, q));
  99. BigInteger d (e);
  100. d.inverseModulo (m);
  101. publicKey.part1 = e;
  102. publicKey.part2 = n;
  103. privateKey.part1 = d;
  104. privateKey.part2 = n;
  105. }