CipherKeyImpl.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. //
  2. // CipherKeyImpl.h
  3. //
  4. // Library: Crypto
  5. // Package: Cipher
  6. // Module: CipherKeyImpl
  7. //
  8. // Definition of the CipherKeyImpl class.
  9. //
  10. // Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Crypto_CipherKeyImpl_INCLUDED
  16. #define Crypto_CipherKeyImpl_INCLUDED
  17. #include "Poco/Crypto/Crypto.h"
  18. #include "Poco/Crypto/OpenSSLInitializer.h"
  19. #include "Poco/RefCountedObject.h"
  20. #include "Poco/AutoPtr.h"
  21. #include <vector>
  22. struct evp_cipher_st;
  23. typedef struct evp_cipher_st EVP_CIPHER;
  24. namespace Poco {
  25. namespace Crypto {
  26. class CipherKeyImpl: public RefCountedObject
  27. /// An implementation of the CipherKey class for OpenSSL's crypto library.
  28. {
  29. public:
  30. typedef std::vector<unsigned char> ByteVec;
  31. typedef Poco::AutoPtr<CipherKeyImpl> Ptr;
  32. enum Mode
  33. /// Cipher mode of operation. This mode determines how multiple blocks
  34. /// are connected; this is essential to improve security.
  35. {
  36. MODE_STREAM_CIPHER, /// Stream cipher
  37. MODE_ECB, /// Electronic codebook (plain concatenation)
  38. MODE_CBC, /// Cipher block chaining (default)
  39. MODE_CFB, /// Cipher feedback
  40. MODE_OFB, /// Output feedback
  41. MODE_CTR, /// Counter mode
  42. MODE_GCM, /// Galois/Counter mode
  43. MODE_CCM /// Counter with CBC-MAC
  44. };
  45. CipherKeyImpl(const std::string& name,
  46. const std::string& passphrase,
  47. const std::string& salt,
  48. int iterationCount,
  49. const std::string& digest);
  50. /// Creates a new CipherKeyImpl object, using
  51. /// the given cipher name, passphrase, salt value
  52. /// and iteration count.
  53. CipherKeyImpl(const std::string& name,
  54. const ByteVec& key,
  55. const ByteVec& iv);
  56. /// Creates a new CipherKeyImpl object, using the
  57. /// given cipher name, key and initialization vector.
  58. CipherKeyImpl(const std::string& name);
  59. /// Creates a new CipherKeyImpl object. Autoinitializes key
  60. /// and initialization vector.
  61. virtual ~CipherKeyImpl();
  62. /// Destroys the CipherKeyImpl.
  63. const std::string& name() const;
  64. /// Returns the name of the Cipher.
  65. int keySize() const;
  66. /// Returns the key size of the Cipher.
  67. int blockSize() const;
  68. /// Returns the block size of the Cipher.
  69. int ivSize() const;
  70. /// Returns the IV size of the Cipher.
  71. Mode mode() const;
  72. /// Returns the Cipher's mode of operation.
  73. const ByteVec& getKey() const;
  74. /// Returns the key for the Cipher.
  75. void setKey(const ByteVec& key);
  76. /// Sets the key for the Cipher.
  77. const ByteVec& getIV() const;
  78. /// Returns the initialization vector (IV) for the Cipher.
  79. void setIV(const ByteVec& iv);
  80. /// Sets the initialization vector (IV) for the Cipher.
  81. const EVP_CIPHER* cipher();
  82. /// Returns the cipher object
  83. private:
  84. void generateKey(const std::string& passphrase,
  85. const std::string& salt,
  86. int iterationCount);
  87. /// Generates key and IV from a password and optional salt string.
  88. void generateKey();
  89. /// Generates key and IV from random data.
  90. void getRandomBytes(ByteVec& vec, std::size_t count);
  91. /// Stores random bytes in vec.
  92. private:
  93. const EVP_CIPHER* _pCipher;
  94. const EVP_MD* _pDigest;
  95. std::string _name;
  96. ByteVec _key;
  97. ByteVec _iv;
  98. OpenSSLInitializer _openSSLInitializer;
  99. };
  100. //
  101. // Inlines
  102. //
  103. inline const std::string& CipherKeyImpl::name() const
  104. {
  105. return _name;
  106. }
  107. inline const CipherKeyImpl::ByteVec& CipherKeyImpl::getKey() const
  108. {
  109. return _key;
  110. }
  111. inline void CipherKeyImpl::setKey(const ByteVec& key)
  112. {
  113. poco_assert(key.size() == static_cast<ByteVec::size_type>(keySize()));
  114. _key = key;
  115. }
  116. inline const CipherKeyImpl::ByteVec& CipherKeyImpl::getIV() const
  117. {
  118. return _iv;
  119. }
  120. inline const EVP_CIPHER* CipherKeyImpl::cipher()
  121. {
  122. return _pCipher;
  123. }
  124. } } // namespace Poco::Crypto
  125. #endif // Crypto_CipherKeyImpl_INCLUDED