CipherKey.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // CipherKey.h
  3. //
  4. // Library: Crypto
  5. // Package: Cipher
  6. // Module: CipherKey
  7. //
  8. // Definition of the CipherKey class.
  9. //
  10. // Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Crypto_CipherKey_INCLUDED
  16. #define Crypto_CipherKey_INCLUDED
  17. #include "Poco/Crypto/Crypto.h"
  18. #include "Poco/Crypto/CipherKeyImpl.h"
  19. namespace Poco {
  20. namespace Crypto {
  21. class Crypto_API CipherKey
  22. /// CipherKey stores the key information for decryption/encryption of data.
  23. /// To create a random key, using the following code:
  24. ///
  25. /// CipherKey key("aes-256");
  26. ///
  27. /// Note that you won't be able to decrypt data encrypted with a random key
  28. /// once the Cipher is destroyed unless you persist the generated key and IV.
  29. /// An example usage for random keys is to encrypt data saved in a temporary
  30. /// file.
  31. ///
  32. /// To create a key using a human-readable password
  33. /// string, use the following code. We create a AES Cipher and
  34. /// use a salt value to make the key more robust:
  35. ///
  36. /// std::string password = "secret";
  37. /// std::string salt("asdff8723lasdf(**923412");
  38. /// CipherKey key("aes-256", password, salt);
  39. ///
  40. /// You may also control the digest and the number of iterations used to generate the key
  41. /// by specifying the specific values. Here we create a key with the same data as before,
  42. /// except that we use 100 iterations instead of DEFAULT_ITERATION_COUNT, and sha1 instead of
  43. /// the default md5:
  44. ///
  45. /// std::string password = "secret";
  46. /// std::string salt("asdff8723lasdf(**923412");
  47. /// std::string digest ("sha1");
  48. /// CipherKey key("aes-256", password, salt, 100, digest);
  49. ///
  50. {
  51. public:
  52. typedef CipherKeyImpl::Mode Mode;
  53. typedef CipherKeyImpl::ByteVec ByteVec;
  54. enum
  55. {
  56. DEFAULT_ITERATION_COUNT = 2000
  57. /// Default iteration count to use with
  58. /// generateKey(). RSA security recommends
  59. /// an iteration count of at least 1000.
  60. };
  61. CipherKey(const std::string& name,
  62. const std::string& passphrase,
  63. const std::string& salt = "",
  64. int iterationCount = DEFAULT_ITERATION_COUNT,
  65. const std::string& digest = "md5");
  66. /// Creates a new CipherKeyImpl object using the given
  67. /// cipher name, passphrase, salt value, iteration count and digest.
  68. CipherKey(const std::string& name,
  69. const ByteVec& key,
  70. const ByteVec& iv);
  71. /// Creates a new CipherKeyImpl object using the given cipher
  72. /// name, key and initialization vector (IV).
  73. ///
  74. /// The size of the IV must match the cipher's expected
  75. /// IV size (see ivSize()), except for GCM mode, which allows
  76. /// a custom IV size.
  77. CipherKey(const std::string& name);
  78. /// Creates a new CipherKeyImpl object. Autoinitializes key and
  79. /// initialization vector.
  80. ~CipherKey();
  81. /// Destroys the CipherKeyImpl.
  82. const std::string& name() const;
  83. /// Returns the name of the Cipher.
  84. int keySize() const;
  85. /// Returns the key size of the Cipher.
  86. int blockSize() const;
  87. /// Returns the block size of the Cipher.
  88. int ivSize() const;
  89. /// Returns the IV size of the Cipher.
  90. Mode mode() const;
  91. /// Returns the Cipher's mode of operation.
  92. const ByteVec& getKey() const;
  93. /// Returns the key for the Cipher.
  94. void setKey(const ByteVec& key);
  95. /// Sets the key for the Cipher.
  96. const ByteVec& getIV() const;
  97. /// Returns the initialization vector (IV) for the Cipher.
  98. void setIV(const ByteVec& iv);
  99. /// Sets the initialization vector (IV) for the Cipher.
  100. ///
  101. /// The size of the vector must match the cipher's expected
  102. /// IV size (see ivSize()), except for GCM mode, which allows
  103. /// a custom IV size.
  104. CipherKeyImpl::Ptr impl();
  105. /// Returns the impl object
  106. private:
  107. CipherKeyImpl::Ptr _pImpl;
  108. };
  109. //
  110. // inlines
  111. //
  112. inline const std::string& CipherKey::name() const
  113. {
  114. return _pImpl->name();
  115. }
  116. inline int CipherKey::keySize() const
  117. {
  118. return _pImpl->keySize();
  119. }
  120. inline int CipherKey::blockSize() const
  121. {
  122. return _pImpl->blockSize();
  123. }
  124. inline int CipherKey::ivSize() const
  125. {
  126. return _pImpl->ivSize();
  127. }
  128. inline CipherKey::Mode CipherKey::mode() const
  129. {
  130. return _pImpl->mode();
  131. }
  132. inline const CipherKey::ByteVec& CipherKey::getKey() const
  133. {
  134. return _pImpl->getKey();
  135. }
  136. inline void CipherKey::setKey(const CipherKey::ByteVec& key)
  137. {
  138. _pImpl->setKey(key);
  139. }
  140. inline const CipherKey::ByteVec& CipherKey::getIV() const
  141. {
  142. return _pImpl->getIV();
  143. }
  144. inline void CipherKey::setIV(const CipherKey::ByteVec& iv)
  145. {
  146. _pImpl->setIV(iv);
  147. }
  148. inline CipherKeyImpl::Ptr CipherKey::impl()
  149. {
  150. return _pImpl;
  151. }
  152. } } // namespace Poco::Crypto
  153. #endif // Crypto_CipherKey_INCLUDED