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.

143 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. namespace juce
  20. {
  21. RSAKey::RSAKey()
  22. {
  23. }
  24. RSAKey::RSAKey (const String& s)
  25. {
  26. if (s.containsChar (','))
  27. {
  28. part1.parseString (s.upToFirstOccurrenceOf (",", false, false), 16);
  29. part2.parseString (s.fromFirstOccurrenceOf (",", false, false), 16);
  30. }
  31. else
  32. {
  33. // the string needs to be two hex numbers, comma-separated..
  34. jassertfalse;
  35. }
  36. }
  37. RSAKey::~RSAKey()
  38. {
  39. }
  40. bool RSAKey::operator== (const RSAKey& other) const noexcept
  41. {
  42. return part1 == other.part1 && part2 == other.part2;
  43. }
  44. bool RSAKey::operator!= (const RSAKey& other) const noexcept
  45. {
  46. return ! operator== (other);
  47. }
  48. bool RSAKey::isValid() const noexcept
  49. {
  50. return operator!= (RSAKey());
  51. }
  52. String RSAKey::toString() const
  53. {
  54. return part1.toString (16) + "," + part2.toString (16);
  55. }
  56. bool RSAKey::applyToValue (BigInteger& value) const
  57. {
  58. if (part1.isZero() || part2.isZero() || value <= 0)
  59. {
  60. jassertfalse; // using an uninitialised key
  61. value.clear();
  62. return false;
  63. }
  64. BigInteger result;
  65. while (! value.isZero())
  66. {
  67. result *= part2;
  68. BigInteger remainder;
  69. value.divideBy (part2, remainder);
  70. remainder.exponentModulo (part1, part2);
  71. result += remainder;
  72. }
  73. value.swapWith (result);
  74. return true;
  75. }
  76. BigInteger RSAKey::findBestCommonDivisor (const BigInteger& p, const BigInteger& q)
  77. {
  78. // try 3, 5, 9, 17, etc first because these only contain 2 bits and so
  79. // are fast to divide + multiply
  80. for (int i = 2; i <= 65536; i *= 2)
  81. {
  82. const BigInteger e (1 + i);
  83. if (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne())
  84. return e;
  85. }
  86. BigInteger e (4);
  87. while (! (e.findGreatestCommonDivisor (p).isOne() && e.findGreatestCommonDivisor (q).isOne()))
  88. ++e;
  89. return e;
  90. }
  91. void RSAKey::createKeyPair (RSAKey& publicKey, RSAKey& privateKey,
  92. const int numBits, const int* randomSeeds, const int numRandomSeeds)
  93. {
  94. jassert (numBits > 16); // not much point using less than this..
  95. jassert (numRandomSeeds == 0 || numRandomSeeds >= 2); // you need to provide plenty of seeds here!
  96. BigInteger p (Primes::createProbablePrime (numBits / 2, 30, randomSeeds, numRandomSeeds / 2));
  97. BigInteger q (Primes::createProbablePrime (numBits - numBits / 2, 30, randomSeeds == nullptr ? 0 : (randomSeeds + numRandomSeeds / 2), numRandomSeeds - numRandomSeeds / 2));
  98. const BigInteger n (p * q);
  99. const BigInteger m (--p * --q);
  100. const BigInteger e (findBestCommonDivisor (p, q));
  101. BigInteger d (e);
  102. d.inverseModulo (m);
  103. publicKey.part1 = e;
  104. publicKey.part2 = n;
  105. privateKey.part1 = d;
  106. privateKey.part2 = n;
  107. }
  108. } // namespace juce