scrypt.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // scrypt.h - written and placed in public domain by Jeffrey Walton.
  2. // Based on reference source code by Colin Percival.
  3. /// \file scrypt.h
  4. /// \brief Classes for Scrypt from RFC 7914
  5. /// \sa <A HREF="https://www.tarsnap.com/scrypt/scrypt.pdf">Stronger Key Derivation via
  6. /// Sequential Memory-Hard Functions</a>,
  7. /// <A HREF="https://www.tarsnap.com/scrypt.html">The scrypt key derivation function</A>
  8. /// and <A HREF="https://tools.ietf.org/html/rfc7914">RFC 7914, The scrypt Password-Based
  9. /// Key Derivation Function</A>
  10. /// \since Crypto++ 7.0
  11. #ifndef CRYPTOPP_SCRYPT_H
  12. #define CRYPTOPP_SCRYPT_H
  13. #include "cryptlib.h"
  14. #include "secblock.h"
  15. NAMESPACE_BEGIN(CryptoPP)
  16. /// \brief Scrypt key derivation function
  17. /// \details The Crypto++ implementation uses OpenMP to accelerate the derivation when
  18. /// available.
  19. /// \details The Crypto++ implementation of Scrypt is limited by C++ datatypes. For
  20. /// example, the library is limited to a derived key length of <tt>SIZE_MAX</tt>,
  21. /// and not <tt>(2^32 - 1) * 32</tt>.
  22. /// \sa <A HREF="https://www.tarsnap.com/scrypt/scrypt.pdf">Stronger Key Derivation via
  23. /// Sequential Memory-Hard Functions</A>,
  24. /// <A HREF="https://www.tarsnap.com/scrypt.html">The scrypt key derivation function</A>
  25. /// and <A HREF="https://tools.ietf.org/html/rfc7914">RFC 7914, The scrypt Password-Based
  26. /// Key Derivation Function</A>
  27. /// \since Crypto++ 7.0
  28. class Scrypt : public KeyDerivationFunction
  29. {
  30. public:
  31. virtual ~Scrypt() {}
  32. static std::string StaticAlgorithmName () {
  33. return "scrypt";
  34. }
  35. // KeyDerivationFunction interface
  36. std::string AlgorithmName() const {
  37. return StaticAlgorithmName();
  38. }
  39. // KeyDerivationFunction interface
  40. size_t MaxDerivedKeyLength() const {
  41. return static_cast<size_t>(0)-1;
  42. }
  43. // KeyDerivationFunction interface
  44. size_t GetValidDerivedLength(size_t keylength) const;
  45. // KeyDerivationFunction interface
  46. size_t DeriveKey(byte *derived, size_t derivedLen, const byte *secret, size_t secretLen,
  47. const NameValuePairs& params) const;
  48. /// \brief Derive a key from a seed
  49. /// \param derived the derived output buffer
  50. /// \param derivedLen the size of the derived buffer, in bytes
  51. /// \param secret the seed input buffer
  52. /// \param secretLen the size of the secret buffer, in bytes
  53. /// \param salt the salt input buffer
  54. /// \param saltLen the size of the salt buffer, in bytes
  55. /// \param cost the CPU/memory cost factor
  56. /// \param blockSize the block size
  57. /// \param parallelization the parallelization factor
  58. /// \return the number of iterations performed
  59. /// \throw InvalidDerivedKeyLength if <tt>derivedLen</tt> is invalid for the scheme
  60. /// \details DeriveKey() provides a standard interface to derive a key from
  61. /// a seed and other parameters. Each class that derives from KeyDerivationFunction
  62. /// provides an overload that accepts most parameters used by the derivation function.
  63. /// \details The CPU/Memory <tt>cost</tt> parameter ("N" in the documents) must be
  64. /// larger than 1, a power of 2, and less than <tt>2^(128 * r / 8)</tt>.
  65. /// \details The parameter <tt>blockSize</tt> ("r" in the documents) specifies the block
  66. /// size.
  67. /// \details The <tt>parallelization</tt> parameter ("p" in the documents) is a positive
  68. /// integer less than or equal to <tt>((2^32-1) * 32) / (128 * r)</tt>. Due to Microsoft
  69. /// and its OpenMP 2.0 implementation <tt>parallelization</tt> is limited to
  70. /// <tt>std::numeric_limits<int>::max()</tt>.
  71. /// \details Scrypt always returns 1 because it only performs 1 iteration. Other
  72. /// derivation functions, like PBKDF's, will return more interesting values.
  73. /// \details The Crypto++ implementation of Scrypt is limited by C++ datatypes. For
  74. /// example, the library is limited to a derived key length of <tt>SIZE_MAX</tt>,
  75. /// and not <tt>(2^32 - 1) * 32</tt>.
  76. size_t DeriveKey(byte *derived, size_t derivedLen, const byte *secret, size_t secretLen,
  77. const byte *salt, size_t saltLen, word64 cost=2, word64 blockSize=8, word64 parallelization=1) const;
  78. protected:
  79. enum {defaultCost=2, defaultBlockSize=8, defaultParallelization=1};
  80. // KeyDerivationFunction interface
  81. const Algorithm & GetAlgorithm() const {
  82. return *this;
  83. }
  84. inline void ValidateParameters(size_t derivedlen, word64 cost, word64 blockSize, word64 parallelization) const;
  85. };
  86. NAMESPACE_END
  87. #endif // CRYPTOPP_SCRYPT_H