PBKDF2Engine.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //
  2. // PBKDF2Engine.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/PBKDF2Engine.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Crypt
  8. // Module: PBKDF2Engine
  9. //
  10. // Definition of the PBKDF2Engine class.
  11. //
  12. // Copyright (c) 2014, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_PBKDF2Engine_INCLUDED
  18. #define Foundation_PBKDF2Engine_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/DigestEngine.h"
  21. #include "Poco/ByteOrder.h"
  22. #include <algorithm>
  23. namespace Poco {
  24. template <class PRF>
  25. class PBKDF2Engine: public DigestEngine
  26. /// This class implementes the Password-Based Key Derivation Function 2,
  27. /// as specified in RFC 2898. The underlying DigestEngine (HMACEngine, etc.),
  28. /// which must accept the passphrase as constructor argument (std::string),
  29. /// must be given as template argument.
  30. ///
  31. /// PBKDF2 (Password-Based Key Derivation Function 2) is a key derivation function
  32. /// that is part of RSA Laboratories' Public-Key Cryptography Standards (PKCS) series,
  33. /// specifically PKCS #5 v2.0, also published as Internet Engineering Task Force's
  34. /// RFC 2898. It replaces an earlier standard, PBKDF1, which could only produce
  35. /// derived keys up to 160 bits long.
  36. ///
  37. /// PBKDF2 applies a pseudorandom function, such as a cryptographic hash, cipher, or
  38. /// HMAC to the input password or passphrase along with a salt value and repeats the
  39. /// process many times to produce a derived key, which can then be used as a
  40. /// cryptographic key in subsequent operations. The added computational work makes
  41. /// password cracking much more difficult, and is known as key stretching.
  42. /// When the standard was written in 2000, the recommended minimum number of
  43. /// iterations was 1000, but the parameter is intended to be increased over time as
  44. /// CPU speeds increase. Having a salt added to the password reduces the ability to
  45. /// use precomputed hashes (rainbow tables) for attacks, and means that multiple
  46. /// passwords have to be tested individually, not all at once. The standard
  47. /// recommends a salt length of at least 64 bits. [Wikipedia]
  48. ///
  49. /// The PBKDF2 algorithm is implemented as a DigestEngine. The passphrase is specified
  50. /// by calling update().
  51. ///
  52. /// Example (WPA2):
  53. /// PBKDF2Engine<HMACEngine<SHA1Engine> > pbkdf2(ssid, 4096, 256);
  54. /// pbkdf2.update(passphrase);
  55. /// DigestEngine::Digest d = pbkdf2.digest();
  56. {
  57. public:
  58. enum
  59. {
  60. PRF_DIGEST_SIZE = PRF::DIGEST_SIZE
  61. };
  62. PBKDF2Engine(const std::string& salt, unsigned c = 4096, Poco::UInt32 dkLen = PRF_DIGEST_SIZE):
  63. _s(salt),
  64. _c(c),
  65. _dkLen(dkLen)
  66. {
  67. _result.reserve(_dkLen + PRF_DIGEST_SIZE);
  68. }
  69. ~PBKDF2Engine()
  70. {
  71. }
  72. std::size_t digestLength() const
  73. {
  74. return _dkLen;
  75. }
  76. void reset()
  77. {
  78. _p.clear();
  79. _result.clear();
  80. }
  81. const DigestEngine::Digest& digest()
  82. {
  83. Poco::UInt32 i = 1;
  84. while (_result.size() < _dkLen)
  85. {
  86. f(i++);
  87. }
  88. _result.resize(_dkLen);
  89. return _result;
  90. }
  91. protected:
  92. void updateImpl(const void* data, std::size_t length)
  93. {
  94. _p.append(reinterpret_cast<const char*>(data), length);
  95. }
  96. void f(Poco::UInt32 i)
  97. {
  98. PRF prf(_p);
  99. prf.update(_s);
  100. Poco::UInt32 iBE = Poco::ByteOrder::toBigEndian(i);
  101. prf.update(&iBE, sizeof(iBE));
  102. Poco::DigestEngine::Digest up = prf.digest();
  103. Poco::DigestEngine::Digest ux = up;
  104. poco_assert_dbg(ux.size() == PRF_DIGEST_SIZE);
  105. for (unsigned k = 1; k < _c; k++)
  106. {
  107. prf.reset();
  108. prf.update(&up[0], up.size());
  109. Poco::DigestEngine::Digest u = prf.digest();
  110. poco_assert_dbg(u.size() == PRF_DIGEST_SIZE);
  111. for (int ui = 0; ui < PRF_DIGEST_SIZE; ui++)
  112. {
  113. ux[ui] ^= u[ui];
  114. }
  115. std::swap(up, u);
  116. }
  117. _result.insert(_result.end(), ux.begin(), ux.end());
  118. }
  119. private:
  120. PBKDF2Engine();
  121. PBKDF2Engine(const PBKDF2Engine&);
  122. PBKDF2Engine& operator = (const PBKDF2Engine&);
  123. std::string _p;
  124. std::string _s;
  125. unsigned _c;
  126. Poco::UInt32 _dkLen;
  127. DigestEngine::Digest _result;
  128. };
  129. } // namespace Poco
  130. #endif // Foundation_PBKDF2Engine_INCLUDED