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.

160 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #include "../basics/juce_StandardHeader.h"
  24. BEGIN_JUCE_NAMESPACE
  25. #include "juce_RSAKey.h"
  26. #include "juce_Primes.h"
  27. //==============================================================================
  28. RSAKey::RSAKey() throw()
  29. {
  30. }
  31. RSAKey::RSAKey (const String& s) throw()
  32. {
  33. if (s.containsChar (T(',')))
  34. {
  35. part1.parseString (s.upToFirstOccurrenceOf (T(","), false, false), 16);
  36. part2.parseString (s.fromFirstOccurrenceOf (T(","), false, false), 16);
  37. }
  38. else
  39. {
  40. // the string needs to be two hex numbers, comma-separated..
  41. jassertfalse;
  42. }
  43. }
  44. RSAKey::~RSAKey() throw()
  45. {
  46. }
  47. const String RSAKey::toString() const throw()
  48. {
  49. return part1.toString (16) + T(",") + part2.toString (16);
  50. }
  51. bool RSAKey::applyToValue (BitArray& value) const throw()
  52. {
  53. if (part1.isEmpty() || part2.isEmpty()
  54. || value.compare (0) <= 0)
  55. {
  56. jassertfalse // using an uninitialised key
  57. value.clear();
  58. return false;
  59. }
  60. BitArray result;
  61. while (! value.isEmpty())
  62. {
  63. result.multiplyBy (part2);
  64. BitArray remainder;
  65. value.divideBy (part2, remainder);
  66. remainder.exponentModulo (part1, part2);
  67. result.add (remainder);
  68. }
  69. value = result;
  70. return true;
  71. }
  72. static const BitArray findBestCommonDivisor (const BitArray& p,
  73. const BitArray& q) throw()
  74. {
  75. const BitArray one (1);
  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 BitArray e (1 + i);
  81. if (e.findGreatestCommonDivisor (p) == one
  82. && e.findGreatestCommonDivisor (q) == one)
  83. {
  84. return e;
  85. }
  86. }
  87. BitArray e (4);
  88. while (! (e.findGreatestCommonDivisor (p) == one
  89. && e.findGreatestCommonDivisor (q) == one))
  90. {
  91. e.add (one);
  92. }
  93. return e;
  94. }
  95. void RSAKey::createKeyPair (RSAKey& publicKey,
  96. RSAKey& privateKey,
  97. const int numBits) throw()
  98. {
  99. jassert (numBits > 16); // not much point using less than this..
  100. BitArray p (Primes::createProbablePrime (numBits / 2, 30));
  101. BitArray q (Primes::createProbablePrime (numBits - numBits / 2, 30));
  102. BitArray n (p);
  103. n.multiplyBy (q); // n = pq
  104. const BitArray one (1);
  105. p.subtract (one);
  106. q.subtract (one);
  107. BitArray m (p);
  108. m.multiplyBy (q); // m = (p - 1)(q - 1)
  109. const BitArray e (findBestCommonDivisor (p, q));
  110. BitArray d (e);
  111. d.inverseModulo (m);
  112. publicKey.part1 = e;
  113. publicKey.part2 = n;
  114. privateKey.part1 = d;
  115. privateKey.part2 = n;
  116. }
  117. END_JUCE_NAMESPACE