argon2.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #pragma once
  2. #include "common.h"
  3. #include <util/generic/ptr.h>
  4. #include <util/system/defaults.h>
  5. namespace NArgonish {
  6. /**
  7. * Type of Argon2 algorithm
  8. */
  9. enum class EArgon2Type : ui32 {
  10. Argon2d = 0, /// Data dependent version of Argon2
  11. Argon2i = 1, /// Data independent version of Argon2
  12. Argon2id = 2 /// Mixed version of Argon2
  13. };
  14. /**
  15. * Interface of all Argon2 instances
  16. */
  17. class IArgon2Base {
  18. public:
  19. virtual ~IArgon2Base() {
  20. }
  21. /**
  22. * Applies Argon2 algorithm
  23. * @param pwd password
  24. * @param pwdlen password length
  25. * @param salt salt
  26. * @param saltlen salt length
  27. * @param out output
  28. * @param outlen output length
  29. * @param aad additional authenticated data (optional)
  30. * @param aadlen additional authenticated data length (optional)
  31. */
  32. virtual void Hash(const ui8* pwd, ui32 pwdlen, const ui8* salt, ui32 saltlen,
  33. ui8* out, ui32 outlen, const ui8* aad = nullptr, ui32 aadlen = 0) const = 0;
  34. /**
  35. * Applies Argon2 algorithm to a password and compares the result with the hash data
  36. * @param pwd password
  37. * @param pwdlen password length
  38. * @param salt salt
  39. * @param saltlen salt length
  40. * @param hash hash value to compare with the result
  41. * @param hashlen hash value length
  42. * @param aad additional authenticated data (optional)
  43. * @param adadlen additional authenticated data length (optional)
  44. * @return true if the Argon2 result equals to the value in hash
  45. */
  46. virtual bool Verify(const ui8* pwd, ui32 pwdlen, const ui8* salt, ui32 saltlen,
  47. const ui8* hash, ui32 hashlen, const ui8* aad = nullptr, ui32 adadlen = 0) const = 0;
  48. /**
  49. * Applies Argon2 algorithms but allows to pass memory buffer for work.
  50. * This allows to use external memory allocator or reuse already allocated memory buffer.
  51. * @param memory memory buffer for Argon2 calculations
  52. * @param mlen memory buffer len (must be at least the value returned by the GetMemorySize method)
  53. * @param pwd password to hash
  54. * @param pwdlen password length
  55. * @param salt salt
  56. * @param saltlen salt length
  57. * @param out output buffer
  58. * @param outlen output length
  59. * @param aad additional authenticated data (optional)
  60. * @param aadlen additional authenticated data length (optional)
  61. */
  62. virtual void HashWithCustomMemory(ui8* memory, size_t mlen, const ui8* pwd, ui32 pwdlen,
  63. const ui8* salt, ui32 saltlen, ui8* out, ui32 outlen,
  64. const ui8* aad = nullptr, ui32 aadlen = 0) const = 0;
  65. /**
  66. * Applies Argon2 algorithm to a password and compares the result with the hash data.
  67. * This method allows to use a custom memory allocator or reuse already allocated memory buffer.
  68. * @param memory memory buffer for Argon2 calculations
  69. * @param mlen memory buffer length
  70. * @param pwd password to hash
  71. * @param pwdlen password length
  72. * @param salt salt
  73. * @param saltlen salt length
  74. * @param hash hash value to compare with the result
  75. * @param hashlen hash value length
  76. * @param aad additional authenticated data (optional)
  77. * @param aadlen additional authenticated data length (optional)
  78. * @return true if the Argon2 result equals to the value in hash
  79. */
  80. virtual bool VerifyWithCustomMemory(ui8* memory, size_t mlen, const ui8* pwd, ui32 pwdlen,
  81. const ui8* salt, ui32 saltlen, const ui8* hash, ui32 hashlen,
  82. const ui8* aad = nullptr, ui32 aadlen = 0) const = 0;
  83. /**
  84. * The function calculates the size of memory required by Argon2 algorithm
  85. * @return memory buffer size
  86. */
  87. virtual size_t GetMemorySize() const = 0;
  88. };
  89. /**
  90. * A factory to create Argon2 instances depending on instruction set, tcost, mcost, the number of threads etc.
  91. */
  92. class TArgon2Factory {
  93. public:
  94. /**
  95. * Constructs a factory object
  96. * @param skipTest if true then a simple runtime test will be skipped in the constructor (optional)
  97. */
  98. TArgon2Factory(bool skipTest = false);
  99. /**
  100. * Creates an instance of Argon2 algorithm.
  101. * The particular optimization is chosen automatically based on the cpuid instruction output.
  102. * @param atype the type of Argon2 algorithm
  103. * @param tcost the number of passes over memory block, must be at least 1
  104. * @param mcost the size in kilobytes of memory block used by Argon2
  105. * @param threads the number of threads for parallel version of Argon2 (must be 1,2 or 4)
  106. * @param key a secret key to use for password hashing (optional)
  107. * @param keylen the length of the key (optional)
  108. * @return unique_ptr to Argon2 instance. In case of error std::runtime_excetion is thrown
  109. */
  110. THolder<IArgon2Base> Create(EArgon2Type atype = EArgon2Type::Argon2d, ui32 tcost = 1, ui32 mcost = 1024,
  111. ui32 threads = 1, const ui8* key = nullptr, ui32 keylen = 0) const;
  112. /**
  113. * Creates an instance of Argon2 algorithm optimized for the provided instruction set
  114. * @param instructionSet instruction set
  115. * @param atype the type of Argon2 algorithm
  116. * @param tcost the number of passes over memory block, must be at least 1
  117. * @param mcost the size in kilobytes of memory block used by Argon2
  118. * @param threads the number of threads for parallel version of Argon2 (must be 1,2 or 4)
  119. * @param key a secret key to use for password hashing (optional)
  120. * @param keylen the length of the key (optional)
  121. * @return unique_ptr to Argon2 instance. In case of error std::runtime_excetion is thrown
  122. */
  123. THolder<IArgon2Base> Create(EInstructionSet instructionSet, EArgon2Type atype = EArgon2Type::Argon2d, ui32 tcost = 1,
  124. ui32 mcost = 1024, ui32 threads = 1, const ui8* key = nullptr,
  125. ui32 keylen = 0) const;
  126. /**
  127. * The function returns the best instruction set available on the current CPU
  128. * @return InstructionSet value
  129. */
  130. EInstructionSet GetInstructionSet() const;
  131. protected:
  132. EInstructionSet InstructionSet_ = EInstructionSet::REF;
  133. void QuickTest_() const;
  134. };
  135. }