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) 2022 - Raw Material Software Limited
  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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. RSAKey::RSAKey()
  21. {
  22. }
  23. RSAKey::RSAKey (const String& s)
  24. {
  25. if (s.containsChar (','))
  26. {
  27. part1.parseString (s.upToFirstOccurrenceOf (",", false, false), 16);
  28. part2.parseString (s.fromFirstOccurrenceOf (",", false, false), 16);
  29. }
  30. else
  31. {
  32. // the string needs to be two hex numbers, comma-separated..
  33. jassertfalse;
  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 ? nullptr : (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. }
  104. } // namespace juce