Botan  2.11.0
Crypto and TLS for C++11
argon2.h
Go to the documentation of this file.
1 /**
2 * (C) 2018,2019 Jack Lloyd
3 *
4 * Botan is released under the Simplified BSD License (see license.txt)
5 */
6 
7 #ifndef BOTAN_ARGON2_H_
8 #define BOTAN_ARGON2_H_
9 
10 #include <botan/pwdhash.h>
11 
12 namespace Botan {
13 
14 class RandomNumberGenerator;
15 
16 /**
17 * Argon2 key derivation function
18 */
20  {
21  public:
22  Argon2(uint8_t family, size_t M, size_t t, size_t p);
23 
24  Argon2(const Argon2& other) = default;
25  Argon2& operator=(const Argon2&) = default;
26 
27  /**
28  * Derive a new key under the current Argon2 parameter set
29  */
30  void derive_key(uint8_t out[], size_t out_len,
31  const char* password, size_t password_len,
32  const uint8_t salt[], size_t salt_len) const override;
33 
34  std::string to_string() const override;
35 
36  size_t M() const { return m_M; }
37  size_t t() const { return m_t; }
38  size_t p() const { return m_p; }
39 
40  size_t iterations() const override { return t(); }
41 
42  size_t parallelism() const override { return p(); }
43 
44  size_t memory_param() const override { return M(); }
45 
46  size_t total_memory_usage() const override { return M() * 1024; }
47 
48  private:
49  uint8_t m_family;
50  size_t m_M, m_t, m_p;
51  };
52 
54  {
55  public:
56  Argon2_Family(uint8_t family);
57 
58  std::string name() const override;
59 
60  std::unique_ptr<PasswordHash> tune(size_t output_length,
61  std::chrono::milliseconds msec,
62  size_t max_memory) const override;
63 
64  std::unique_ptr<PasswordHash> default_params() const override;
65 
66  std::unique_ptr<PasswordHash> from_iterations(size_t iter) const override;
67 
68  std::unique_ptr<PasswordHash> from_params(
69  size_t M, size_t t, size_t p) const override;
70  private:
71  const uint8_t m_family;
72  };
73 
74 /**
75 * Argon2 key derivation function
76 *
77 * @param output the output will be placed here
78 * @param output_len length of output
79 * @param password the user password
80 * @param salt the salt
81 * @param salt_len length of salt
82 * @param y the Argon2 variant (0 = Argon2d, 1 = Argon2i, 2 = Argon2id)
83 * @param p the parallelization parameter
84 * @param M the amount of memory to use in Kb
85 * @param t the number of iterations to use
86 */
87 void BOTAN_PUBLIC_API(2,11) argon2(uint8_t output[], size_t output_len,
88  const char* password, size_t password_len,
89  const uint8_t salt[], size_t salt_len,
90  const uint8_t key[], size_t key_len,
91  const uint8_t ad[], size_t ad_len,
92  uint8_t y, size_t p, size_t M, size_t t);
93 
94 std::string BOTAN_PUBLIC_API(2,11)
95  argon2_generate_pwhash(const char* password, size_t password_len,
97  size_t p, size_t M, size_t t,
98  uint8_t y = 2, size_t salt_len = 16, size_t output_len = 32);
99 
100 /**
101 * Check a previously created password hash
102 * @param password the password to check against
103 * @param hash the stored hash to check against
104 */
105 bool BOTAN_PUBLIC_API(2,11) argon2_check_pwhash(const char* password, size_t password_len,
106  const std::string& hash);
107 
108 }
109 
110 #endif
std::string argon2_generate_pwhash(const char *password, size_t password_len, RandomNumberGenerator &rng, size_t p, size_t M, size_t t, uint8_t y=2, size_t salt_len=16, size_t output_len=32)
Definition: argon2fmt.cpp:26
bool argon2_check_pwhash(const char *password, size_t password_len, const std::string &hash)
Definition: argon2fmt.cpp:57
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: bigint.h:1136
size_t parallelism() const override
Definition: argon2.h:42
size_t M() const
Definition: argon2.h:36
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11
std::string name
size_t p() const
Definition: argon2.h:38
size_t total_memory_usage() const override
Definition: argon2.h:46
size_t salt_len
Definition: x509_obj.cpp:26
Definition: alg_id.cpp:13
size_t iterations() const override
Definition: argon2.h:40
void argon2(uint8_t output[], size_t output_len, const char *password, size_t password_len, const uint8_t salt[], size_t salt_len, const uint8_t key[], size_t key_len, const uint8_t ad[], size_t ad_len, uint8_t mode, size_t threads, size_t M, size_t t)
Definition: argon2.cpp:381
size_t memory_param() const override
Definition: argon2.h:44
MechanismType hash
size_t t() const
Definition: argon2.h:37