CryptoPP.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. This file is part of cpp-ethereum.
  3. cpp-ethereum is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. cpp-ethereum is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /** @file CryptoPP.h
  15. * @author Alex Leverington <nessence@gmail.com>
  16. * @date 2014
  17. *
  18. * CryptoPP headers and primitive helper methods
  19. */
  20. #pragma once
  21. #include <mutex>
  22. #if defined(_MSC_VER)
  23. #pragma warning(push)
  24. // Compiler Warning (level 4) C4100 - 'identifier' : unreferenced formal parameter
  25. #pragma warning(disable:4100)
  26. // Compiler Warning (levels 3 and 4) C4244 - 'conversion' conversion from 'type1' to 'type2', possible loss of data
  27. #pragma warning(disable:4244)
  28. // Compiler Warning (level 1) C4297 - 'function' : function assumed not to throw an exception but does
  29. #pragma warning(disable:4297)
  30. #endif // defined(_MSC_VER)
  31. #if defined(__GNUC__)
  32. #pragma GCC diagnostic push
  33. // Warn if a prototype causes a type conversion that is different from what would happen to the same argument in the absence of a prototype.
  34. #pragma GCC diagnostic ignored "-Wconversion"
  35. // Deleting object of polymorphic class type 'Base' which has non-virtual destructor might cause undefined behaviour
  36. #pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
  37. // This enables some extra warning flags that are not enabled by -Wall
  38. #pragma GCC diagnostic ignored "-Wextra"
  39. // Warn whenever a static function is declared but not defined or a non-inline static function is unused
  40. #pragma GCC diagnostic ignored "-Wunused-function"
  41. // Warn whenever a function parameter is unused aside from its declaration.
  42. #pragma GCC diagnostic ignored "-Wunused-parameter"
  43. // Warn whenever a local or static variable is unused aside from its declaration.
  44. #pragma GCC diagnostic ignored "-Wunused-variable"
  45. #endif // defined(__GNUC__)
  46. #include <cryptopp/sha.h>
  47. #include <cryptopp/sha3.h>
  48. #include <cryptopp/ripemd.h>
  49. #include <cryptopp/aes.h>
  50. #include <cryptopp/pwdbased.h>
  51. #include <cryptopp/modes.h>
  52. #include <cryptopp/filters.h>
  53. #include <cryptopp/eccrypto.h>
  54. #include <cryptopp/ecp.h>
  55. #include <cryptopp/files.h>
  56. #include <cryptopp/osrng.h>
  57. #include <cryptopp/oids.h>
  58. #include <cryptopp/dsa.h>
  59. #if defined(__GNUC__)
  60. #pragma GCC diagnostic pop
  61. #endif // defined(__GNUC__)
  62. #if defined(_MSC_VER)
  63. #pragma warning(pop)
  64. #endif // defined(_MSC_VER)
  65. #include <libdevcore/SHA3.h>
  66. #include "Common.h"
  67. namespace dev
  68. {
  69. namespace crypto
  70. {
  71. using namespace CryptoPP;
  72. inline ECP::Point publicToPoint(Public const& _p) { Integer x(_p.data(), 32); Integer y(_p.data() + 32, 32); return ECP::Point(x,y); }
  73. inline Integer secretToExponent(Secret const& _s) { return std::move(Integer(_s.data(), Secret::size)); }
  74. /// Amount of bytes added when encrypting with encryptECIES.
  75. static const unsigned c_eciesOverhead = 113;
  76. /**
  77. * CryptoPP secp256k1 algorithms.
  78. * @todo Collect ECIES methods into class.
  79. */
  80. class Secp256k1PP
  81. {
  82. public:
  83. static Secp256k1PP* get() { if (!s_this) s_this = new Secp256k1PP; return s_this; }
  84. void toPublic(Secret const& _s, Public& o_public) { exponentToPublic(Integer(_s.data(), sizeof(_s)), o_public); }
  85. /// Encrypts text (replace input). (ECIES w/XOR-SHA1)
  86. void encrypt(Public const& _k, bytes& io_cipher);
  87. /// Decrypts text (replace input). (ECIES w/XOR-SHA1)
  88. void decrypt(Secret const& _k, bytes& io_text);
  89. /// Encrypts text (replace input). (ECIES w/AES128-CTR-SHA256)
  90. void encryptECIES(Public const& _k, bytes& io_cipher);
  91. /// Encrypts text (replace input). (ECIES w/AES128-CTR-SHA256)
  92. void encryptECIES(Public const& _k, bytesConstRef _sharedMacData, bytes& io_cipher);
  93. /// Decrypts text (replace input). (ECIES w/AES128-CTR-SHA256)
  94. bool decryptECIES(Secret const& _k, bytes& io_text);
  95. /// Decrypts text (replace input). (ECIES w/AES128-CTR-SHA256)
  96. bool decryptECIES(Secret const& _k, bytesConstRef _sharedMacData, bytes& io_text);
  97. /// Key derivation function used by encryptECIES and decryptECIES.
  98. bytes eciesKDF(Secret const& _z, bytes _s1, unsigned kdBitLen = 256);
  99. /// @returns siganture of message.
  100. Signature sign(Secret const& _k, bytesConstRef _message);
  101. /// @returns compact siganture of provided hash.
  102. Signature sign(Secret const& _k, h256 const& _hash);
  103. /// Verify compact signature (public key is extracted from signature).
  104. bool verify(Signature const& _signature, bytesConstRef _message);
  105. /// Verify signature.
  106. bool verify(Public const& _p, Signature const& _sig, bytesConstRef _message, bool _hashed = false);
  107. /// Recovers public key from compact signature. Uses libsecp256k1.
  108. Public recover(Signature _signature, bytesConstRef _message);
  109. /// Verifies _s is a valid secret key and returns corresponding public key in o_p.
  110. bool verifySecret(Secret const& _s, Public& o_p);
  111. void agree(Secret const& _s, Public const& _r, Secret& o_s);
  112. protected:
  113. void exportPrivateKey(DL_PrivateKey_EC<ECP> const& _k, Secret& o_s) { _k.GetPrivateExponent().Encode(o_s.writable().data(), Secret::size); }
  114. void exportPublicKey(DL_PublicKey_EC<ECP> const& _k, Public& o_p);
  115. void exponentToPublic(Integer const& _e, Public& o_p);
  116. template <class T> void initializeDLScheme(Secret const& _s, T& io_operator) { std::lock_guard<std::mutex> l(x_params); io_operator.AccessKey().Initialize(m_params, secretToExponent(_s)); }
  117. template <class T> void initializeDLScheme(Public const& _p, T& io_operator) { std::lock_guard<std::mutex> l(x_params); io_operator.AccessKey().Initialize(m_params, publicToPoint(_p)); }
  118. private:
  119. Secp256k1PP(): m_oid(ASN1::secp256k1()), m_params(m_oid), m_curve(m_params.GetCurve()), m_q(m_params.GetGroupOrder()), m_qs(m_params.GetSubgroupOrder()) {}
  120. OID m_oid;
  121. std::mutex x_rng;
  122. AutoSeededRandomPool m_rng;
  123. std::mutex x_params;
  124. DL_GroupParameters_EC<ECP> m_params;
  125. std::mutex x_curve;
  126. DL_GroupParameters_EC<ECP>::EllipticCurve m_curve;
  127. Integer m_q;
  128. Integer m_qs;
  129. static Secp256k1PP* s_this;
  130. };
  131. }
  132. }