ECTest.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. //
  2. // ECTest.cpp
  3. //
  4. //
  5. // Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
  6. // and Contributors.
  7. //
  8. // SPDX-License-Identifier: BSL-1.0
  9. //
  10. #include "ECTest.h"
  11. #include "CppUnit/TestCaller.h"
  12. #include "CppUnit/TestSuite.h"
  13. #include "Poco/Crypto/ECKey.h"
  14. #include "Poco/Crypto/ECDSADigestEngine.h"
  15. #include <openssl/pem.h>
  16. #include <iostream>
  17. #include <sstream>
  18. #include <cstring>
  19. using namespace Poco::Crypto;
  20. ECTest::ECTest(const std::string& name): CppUnit::TestCase(name)
  21. {
  22. }
  23. ECTest::~ECTest()
  24. {
  25. }
  26. void ECTest::testECNewKeys()
  27. {
  28. try
  29. {
  30. std::string curveName = ECKey::getCurveName();
  31. if (!curveName.empty())
  32. {
  33. ECKey key(curveName);
  34. std::ostringstream strPub;
  35. std::ostringstream strPriv;
  36. key.save(&strPub, &strPriv, "testpwd");
  37. std::string pubKey = strPub.str();
  38. std::string privKey = strPriv.str();
  39. // now do the round trip
  40. std::istringstream iPub(pubKey);
  41. std::istringstream iPriv(privKey);
  42. ECKey key2(&iPub, &iPriv, "testpwd");
  43. std::istringstream iPriv2(privKey);
  44. ECKey key3(0, &iPriv2, "testpwd");
  45. std::ostringstream strPub3;
  46. key3.save(&strPub3);
  47. std::string pubFromPrivate = strPub3.str();
  48. assertTrue (pubFromPrivate == pubKey);
  49. }
  50. else
  51. std::cerr << "No elliptic curves found!" << std::endl;
  52. }
  53. catch (Poco::Exception& ex)
  54. {
  55. std::cerr << ex.displayText() << std::endl;
  56. throw;
  57. }
  58. }
  59. void ECTest::testECNewKeysNoPassphrase()
  60. {
  61. try
  62. {
  63. std::string curveName = ECKey::getCurveName();
  64. if (!curveName.empty())
  65. {
  66. ECKey key(curveName);
  67. std::ostringstream strPub;
  68. std::ostringstream strPriv;
  69. key.save(&strPub, &strPriv);
  70. std::string pubKey = strPub.str();
  71. std::string privKey = strPriv.str();
  72. // now do the round trip
  73. std::istringstream iPub(pubKey);
  74. std::istringstream iPriv(privKey);
  75. ECKey key2(&iPub, &iPriv);
  76. std::istringstream iPriv2(privKey);
  77. ECKey key3(0, &iPriv2);
  78. std::ostringstream strPub3;
  79. key3.save(&strPub3);
  80. std::string pubFromPrivate = strPub3.str();
  81. assertTrue (pubFromPrivate == pubKey);
  82. }
  83. else
  84. std::cerr << "No elliptic curves found!" << std::endl;
  85. }
  86. catch (Poco::Exception& ex)
  87. {
  88. std::cerr << ex.displayText() << std::endl;
  89. throw;
  90. }
  91. }
  92. void ECTest::testECDSASignSha256()
  93. {
  94. try
  95. {
  96. std::string curveName = ECKey::getCurveName();
  97. if (!curveName.empty())
  98. {
  99. std::string msg("Test this sign message");
  100. ECKey key(curveName);
  101. ECDSADigestEngine eng(key, "SHA256");
  102. eng.update(msg.c_str(), static_cast<unsigned>(msg.length()));
  103. const Poco::DigestEngine::Digest& sig = eng.signature();
  104. // verify
  105. std::ostringstream strPub;
  106. key.save(&strPub);
  107. std::string pubKey = strPub.str();
  108. std::istringstream iPub(pubKey);
  109. ECKey keyPub(&iPub);
  110. ECDSADigestEngine eng2(keyPub, "SHA256");
  111. eng2.update(msg.c_str(), static_cast<unsigned>(msg.length()));
  112. assertTrue(eng2.verify(sig));
  113. }
  114. else
  115. std::cerr << "No elliptic curves found!" << std::endl;
  116. }
  117. catch (Poco::Exception& ex)
  118. {
  119. std::cerr << ex.displayText() << std::endl;
  120. throw;
  121. }
  122. }
  123. void ECTest::testECDSASignManipulated()
  124. {
  125. try
  126. {
  127. std::string curveName = ECKey::getCurveName();
  128. if (!curveName.empty())
  129. {
  130. std::string msg("Test this sign message");
  131. std::string msgManip("Test that sign message");
  132. ECKey key(curveName);
  133. ECDSADigestEngine eng(key, "SHA256");
  134. eng.update(msg.c_str(), static_cast<unsigned>(msg.length()));
  135. const Poco::DigestEngine::Digest& sig = eng.signature();
  136. std::string hexDig = Poco::DigestEngine::digestToHex(sig);
  137. // verify
  138. std::ostringstream strPub;
  139. key.save(&strPub);
  140. std::string pubKey = strPub.str();
  141. std::istringstream iPub(pubKey);
  142. ECKey keyPub(&iPub);
  143. ECDSADigestEngine eng2(keyPub, "SHA256");
  144. eng2.update(msgManip.c_str(), static_cast<unsigned>(msgManip.length()));
  145. assertTrue (!eng2.verify(sig));
  146. }
  147. else
  148. std::cerr << "No elliptic curves found!" << std::endl;
  149. }
  150. catch (Poco::Exception& ex)
  151. {
  152. std::cerr << ex.displayText() << std::endl;
  153. throw;
  154. }
  155. }
  156. void ECTest::setUp()
  157. {
  158. }
  159. void ECTest::tearDown()
  160. {
  161. }
  162. CppUnit::Test* ECTest::suite()
  163. {
  164. CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ECTest");
  165. CppUnit_addTest(pSuite, ECTest, testECNewKeys);
  166. CppUnit_addTest(pSuite, ECTest, testECNewKeysNoPassphrase);
  167. CppUnit_addTest(pSuite, ECTest, testECDSASignSha256);
  168. CppUnit_addTest(pSuite, ECTest, testECDSASignManipulated);
  169. return pSuite;
  170. }