argon2.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Argon2 source code package
  3. *
  4. * Written by Daniel Dinu and Dmitry Khovratovich, 2015
  5. *
  6. * This work is licensed under a Creative Commons CC0 1.0 License/Waiver.
  7. *
  8. * You should have received a copy of the CC0 Public Domain Dedication along
  9. * with this software. If not, see
  10. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  11. */
  12. #ifndef argon2_H
  13. #define argon2_H
  14. #include <limits.h>
  15. #include <stddef.h>
  16. #include <stdint.h>
  17. /*
  18. * Argon2 input parameter restrictions
  19. */
  20. /* Minimum and maximum number of lanes (degree of parallelism) */
  21. #define ARGON2_MIN_LANES UINT32_C(1)
  22. #define ARGON2_MAX_LANES UINT32_C(0xFFFFFF)
  23. /* Minimum and maximum number of threads */
  24. #define ARGON2_MIN_THREADS UINT32_C(1)
  25. #define ARGON2_MAX_THREADS UINT32_C(0xFFFFFF)
  26. /* Number of synchronization points between lanes per pass */
  27. #define ARGON2_SYNC_POINTS UINT32_C(4)
  28. /* Minimum and maximum digest size in bytes */
  29. #define ARGON2_MIN_OUTLEN UINT32_C(16)
  30. #define ARGON2_MAX_OUTLEN UINT32_C(0xFFFFFFFF)
  31. /* Minimum and maximum number of memory blocks (each of BLOCK_SIZE bytes) */
  32. #define ARGON2_MIN_MEMORY (2 * ARGON2_SYNC_POINTS) /* 2 blocks per slice */
  33. #define ARGON2_MIN(a, b) ((a) < (b) ? (a) : (b))
  34. /* Max memory size is half the addressing space, topping at 2^32 blocks (4 TB)
  35. */
  36. #define ARGON2_MAX_MEMORY_BITS \
  37. ARGON2_MIN(UINT32_C(32), (sizeof(void *) * CHAR_BIT - 10 - 1))
  38. #define ARGON2_MAX_MEMORY \
  39. ARGON2_MIN(UINT32_C(0xFFFFFFFF), UINT64_C(1) << ARGON2_MAX_MEMORY_BITS)
  40. /* Minimum and maximum number of passes */
  41. #define ARGON2_MIN_TIME UINT32_C(1)
  42. #define ARGON2_MAX_TIME UINT32_C(0xFFFFFFFF)
  43. /* Minimum and maximum password length in bytes */
  44. #define ARGON2_MIN_PWD_LENGTH UINT32_C(0)
  45. #define ARGON2_MAX_PWD_LENGTH UINT32_C(0xFFFFFFFF)
  46. /* Minimum and maximum associated data length in bytes */
  47. #define ARGON2_MIN_AD_LENGTH UINT32_C(0)
  48. #define ARGON2_MAX_AD_LENGTH UINT32_C(0xFFFFFFFF)
  49. /* Minimum and maximum salt length in bytes */
  50. #define ARGON2_MIN_SALT_LENGTH UINT32_C(8)
  51. #define ARGON2_MAX_SALT_LENGTH UINT32_C(0xFFFFFFFF)
  52. /* Minimum and maximum key length in bytes */
  53. #define ARGON2_MIN_SECRET UINT32_C(0)
  54. #define ARGON2_MAX_SECRET UINT32_C(0xFFFFFFFF)
  55. #define ARGON2_FLAG_CLEAR_PASSWORD (UINT32_C(1) << 0)
  56. #define ARGON2_FLAG_CLEAR_SECRET (UINT32_C(1) << 1)
  57. #define ARGON2_FLAG_CLEAR_MEMORY (UINT32_C(1) << 2)
  58. #define ARGON2_DEFAULT_FLAGS (UINT32_C(0))
  59. /* Error codes */
  60. typedef enum Argon2_ErrorCodes {
  61. ARGON2_OK = 0,
  62. ARGON2_OUTPUT_PTR_NULL = -1,
  63. ARGON2_OUTPUT_TOO_SHORT = -2,
  64. ARGON2_OUTPUT_TOO_LONG = -3,
  65. ARGON2_PWD_TOO_SHORT = -4,
  66. ARGON2_PWD_TOO_LONG = -5,
  67. ARGON2_SALT_TOO_SHORT = -6,
  68. ARGON2_SALT_TOO_LONG = -7,
  69. ARGON2_AD_TOO_SHORT = -8,
  70. ARGON2_AD_TOO_LONG = -9,
  71. ARGON2_SECRET_TOO_SHORT = -10,
  72. ARGON2_SECRET_TOO_LONG = -11,
  73. ARGON2_TIME_TOO_SMALL = -12,
  74. ARGON2_TIME_TOO_LARGE = -13,
  75. ARGON2_MEMORY_TOO_LITTLE = -14,
  76. ARGON2_MEMORY_TOO_MUCH = -15,
  77. ARGON2_LANES_TOO_FEW = -16,
  78. ARGON2_LANES_TOO_MANY = -17,
  79. ARGON2_PWD_PTR_MISMATCH = -18, /* NULL ptr with non-zero length */
  80. ARGON2_SALT_PTR_MISMATCH = -19, /* NULL ptr with non-zero length */
  81. ARGON2_SECRET_PTR_MISMATCH = -20, /* NULL ptr with non-zero length */
  82. ARGON2_AD_PTR_MISMATCH = -21, /* NULL ptr with non-zero length */
  83. ARGON2_MEMORY_ALLOCATION_ERROR = -22,
  84. ARGON2_FREE_MEMORY_CBK_NULL = -23,
  85. ARGON2_ALLOCATE_MEMORY_CBK_NULL = -24,
  86. ARGON2_INCORRECT_PARAMETER = -25,
  87. ARGON2_INCORRECT_TYPE = -26,
  88. ARGON2_OUT_PTR_MISMATCH = -27,
  89. ARGON2_THREADS_TOO_FEW = -28,
  90. ARGON2_THREADS_TOO_MANY = -29,
  91. ARGON2_MISSING_ARGS = -30,
  92. ARGON2_ENCODING_FAIL = -31,
  93. ARGON2_DECODING_FAIL = -32,
  94. ARGON2_THREAD_FAIL = -33,
  95. ARGON2_DECODING_LENGTH_FAIL = -34,
  96. ARGON2_VERIFY_MISMATCH = -35
  97. } argon2_error_codes;
  98. /* Argon2 external data structures */
  99. /*
  100. * Context: structure to hold Argon2 inputs:
  101. * output array and its length,
  102. * password and its length,
  103. * salt and its length,
  104. * secret and its length,
  105. * associated data and its length,
  106. * number of passes, amount of used memory (in KBytes, can be rounded up a bit)
  107. * number of parallel threads that will be run.
  108. * All the parameters above affect the output hash value.
  109. * Additionally, two function pointers can be provided to allocate and
  110. * deallocate the memory (if NULL, memory will be allocated internally).
  111. * Also, three flags indicate whether to erase password, secret as soon as they
  112. * are pre-hashed (and thus not needed anymore), and the entire memory
  113. *****
  114. * Simplest situation: you have output array out[8], password is stored in
  115. * pwd[32], salt is stored in salt[16], you do not have keys nor associated
  116. *data.
  117. * You need to spend 1 GB of RAM and you run 5 passes of Argon2 with 4 parallel
  118. *lanes.
  119. * You want to erase the password, but you're OK with last pass not being
  120. *erased.
  121. * You want to use the default memory allocator.
  122. * Then you initialize:
  123. * Argon2_Context(out,8,pwd,32,salt,16,NULL,0,NULL,0,5,1<<20,4,4,NULL,NULL,true,false,false,false).
  124. */
  125. typedef struct Argon2_Context {
  126. uint8_t *out; /* output array */
  127. uint32_t outlen; /* digest length */
  128. uint8_t *pwd; /* password array */
  129. uint32_t pwdlen; /* password length */
  130. uint8_t *salt; /* salt array */
  131. uint32_t saltlen; /* salt length */
  132. uint8_t *secret; /* key array */
  133. uint32_t secretlen; /* key length */
  134. uint8_t *ad; /* associated data array */
  135. uint32_t adlen; /* associated data length */
  136. uint32_t t_cost; /* number of passes */
  137. uint32_t m_cost; /* amount of memory requested (KB) */
  138. uint32_t lanes; /* number of lanes */
  139. uint32_t threads; /* maximum number of threads */
  140. uint32_t flags; /* array of bool options */
  141. } argon2_context;
  142. /* Argon2 primitive type */
  143. typedef enum Argon2_type { Argon2_i = 1, Argon2_id = 2 } argon2_type;
  144. /*
  145. * Function that performs memory-hard hashing with certain degree of parallelism
  146. * @param context Pointer to the Argon2 internal structure
  147. * @return Error code if smth is wrong, ARGON2_OK otherwise
  148. */
  149. int argon2_ctx(argon2_context *context, argon2_type type);
  150. /**
  151. * Hashes a password with Argon2i, producing an encoded hash
  152. * @param t_cost Number of iterations
  153. * @param m_cost Sets memory usage to m_cost kibibytes
  154. * @param parallelism Number of threads and compute lanes
  155. * @param pwd Pointer to password
  156. * @param pwdlen Password size in bytes
  157. * @param salt Pointer to salt
  158. * @param saltlen Salt size in bytes
  159. * @param hashlen Desired length of the hash in bytes
  160. * @param encoded Buffer where to write the encoded hash
  161. * @param encodedlen Size of the buffer (thus max size of the encoded hash)
  162. * @pre Different parallelism levels will give different results
  163. * @pre Returns ARGON2_OK if successful
  164. */
  165. int argon2i_hash_encoded(const uint32_t t_cost, const uint32_t m_cost,
  166. const uint32_t parallelism, const void *pwd,
  167. const size_t pwdlen, const void *salt,
  168. const size_t saltlen, const size_t hashlen,
  169. char *encoded, const size_t encodedlen);
  170. /**
  171. * Hashes a password with Argon2id, producing an encoded hash
  172. * @param t_cost Number of iterations
  173. * @param m_cost Sets memory usage to m_cost kibibytes
  174. * @param parallelism Number of threads and compute lanes
  175. * @param pwd Pointer to password
  176. * @param pwdlen Password size in bytes
  177. * @param salt Pointer to salt
  178. * @param saltlen Salt size in bytes
  179. * @param hashlen Desired length of the hash in bytes
  180. * @param encoded Buffer where to write the encoded hash
  181. * @param encodedlen Size of the buffer (thus max size of the encoded hash)
  182. * @pre Different parallelism levels will give different results
  183. * @pre Returns ARGON2_OK if successful
  184. */
  185. int argon2id_hash_encoded(const uint32_t t_cost, const uint32_t m_cost,
  186. const uint32_t parallelism, const void *pwd,
  187. const size_t pwdlen, const void *salt,
  188. const size_t saltlen, const size_t hashlen,
  189. char *encoded, const size_t encodedlen);
  190. /**
  191. * Hashes a password with Argon2i, producing a raw hash
  192. * @param t_cost Number of iterations
  193. * @param m_cost Sets memory usage to m_cost kibibytes
  194. * @param parallelism Number of threads and compute lanes
  195. * @param pwd Pointer to password
  196. * @param pwdlen Password size in bytes
  197. * @param salt Pointer to salt
  198. * @param saltlen Salt size in bytes
  199. * @param hash Buffer where to write the raw hash
  200. * @param hashlen Desired length of the hash in bytes
  201. * @pre Different parallelism levels will give different results
  202. * @pre Returns ARGON2_OK if successful
  203. */
  204. int argon2i_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
  205. const uint32_t parallelism, const void *pwd,
  206. const size_t pwdlen, const void *salt,
  207. const size_t saltlen, void *hash, const size_t hashlen);
  208. /**
  209. * Hashes a password with Argon2id, producing a raw hash
  210. * @param t_cost Number of iterations
  211. * @param m_cost Sets memory usage to m_cost kibibytes
  212. * @param parallelism Number of threads and compute lanes
  213. * @param pwd Pointer to password
  214. * @param pwdlen Password size in bytes
  215. * @param salt Pointer to salt
  216. * @param saltlen Salt size in bytes
  217. * @param hash Buffer where to write the raw hash
  218. * @param hashlen Desired length of the hash in bytes
  219. * @pre Different parallelism levels will give different results
  220. * @pre Returns ARGON2_OK if successful
  221. */
  222. int argon2id_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
  223. const uint32_t parallelism, const void *pwd,
  224. const size_t pwdlen, const void *salt,
  225. const size_t saltlen, void *hash, const size_t hashlen);
  226. /* generic function underlying the above ones */
  227. int argon2_hash(const uint32_t t_cost, const uint32_t m_cost,
  228. const uint32_t parallelism, const void *pwd,
  229. const size_t pwdlen, const void *salt, const size_t saltlen,
  230. void *hash, const size_t hashlen, char *encoded,
  231. const size_t encodedlen, argon2_type type);
  232. /**
  233. * Verifies a password against an encoded string
  234. * Encoded string is restricted as in validate_inputs()
  235. * @param encoded String encoding parameters, salt, hash
  236. * @param pwd Pointer to password
  237. * @pre Returns ARGON2_OK if successful
  238. */
  239. int argon2i_verify(const char *encoded, const void *pwd, const size_t pwdlen);
  240. /**
  241. * Verifies a password against an encoded string
  242. * Encoded string is restricted as in validate_inputs()
  243. * @param encoded String encoding parameters, salt, hash
  244. * @param pwd Pointer to password
  245. * @pre Returns ARGON2_OK if successful
  246. */
  247. int argon2id_verify(const char *encoded, const void *pwd, const size_t pwdlen);
  248. /* generic function underlying the above ones */
  249. int argon2_verify(const char *encoded, const void *pwd, const size_t pwdlen,
  250. argon2_type type);
  251. #endif