argon2.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Argon2 reference source code package - reference C implementations
  3. *
  4. * Copyright 2015
  5. * Daniel Dinu, Dmitry Khovratovich, Jean-Philippe Aumasson, and Samuel Neves
  6. *
  7. * You may use this work under the terms of a Creative Commons CC0 1.0
  8. * License/Waiver or the Apache Public License 2.0, at your option. The terms of
  9. * these licenses can be found at:
  10. *
  11. * - CC0 1.0 Universal : http://creativecommons.org/publicdomain/zero/1.0
  12. * - Apache 2.0 : http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * You should have received a copy of both of these licenses along with this
  15. * software. If not, they may be obtained at the above URLs.
  16. */
  17. #ifndef ARGON2_H
  18. #define ARGON2_H
  19. #include <stdint.h>
  20. #include <stddef.h>
  21. #include <limits.h>
  22. #if defined(__cplusplus)
  23. extern "C" {
  24. #endif
  25. /* Symbols visibility control */
  26. #ifdef A2_VISCTL
  27. #define ARGON2_PUBLIC __attribute__((visibility("default")))
  28. #define ARGON2_LOCAL __attribute__ ((visibility ("hidden")))
  29. #elif _MSC_VER
  30. #define ARGON2_PUBLIC __declspec(dllexport)
  31. #define ARGON2_LOCAL
  32. #else
  33. #define ARGON2_PUBLIC
  34. #define ARGON2_LOCAL
  35. #endif
  36. /*
  37. * Argon2 input parameter restrictions
  38. */
  39. /* Minimum and maximum number of lanes (degree of parallelism) */
  40. #define ARGON2_MIN_LANES UINT32_C(1)
  41. #define ARGON2_MAX_LANES UINT32_C(0xFFFFFF)
  42. /* Minimum and maximum number of threads */
  43. #define ARGON2_MIN_THREADS UINT32_C(1)
  44. #define ARGON2_MAX_THREADS UINT32_C(0xFFFFFF)
  45. /* Number of synchronization points between lanes per pass */
  46. #define ARGON2_SYNC_POINTS UINT32_C(4)
  47. /* Minimum and maximum digest size in bytes */
  48. #define ARGON2_MIN_OUTLEN UINT32_C(4)
  49. #define ARGON2_MAX_OUTLEN UINT32_C(0xFFFFFFFF)
  50. /* Minimum and maximum number of memory blocks (each of BLOCK_SIZE bytes) */
  51. #define ARGON2_MIN_MEMORY (2 * ARGON2_SYNC_POINTS) /* 2 blocks per slice */
  52. #define ARGON2_MIN(a, b) ((a) < (b) ? (a) : (b))
  53. /* Max memory size is addressing-space/2, topping at 2^32 blocks (4 TB) */
  54. #define ARGON2_MAX_MEMORY_BITS \
  55. ARGON2_MIN(UINT32_C(32), (sizeof(void *) * CHAR_BIT - 10 - 1))
  56. #define ARGON2_MAX_MEMORY \
  57. ARGON2_MIN(UINT32_C(0xFFFFFFFF), UINT64_C(1) << ARGON2_MAX_MEMORY_BITS)
  58. /* Minimum and maximum number of passes */
  59. #define ARGON2_MIN_TIME UINT32_C(1)
  60. #define ARGON2_MAX_TIME UINT32_C(0xFFFFFFFF)
  61. /* Minimum and maximum password length in bytes */
  62. #define ARGON2_MIN_PWD_LENGTH UINT32_C(0)
  63. #define ARGON2_MAX_PWD_LENGTH UINT32_C(0xFFFFFFFF)
  64. /* Minimum and maximum associated data length in bytes */
  65. #define ARGON2_MIN_AD_LENGTH UINT32_C(0)
  66. #define ARGON2_MAX_AD_LENGTH UINT32_C(0xFFFFFFFF)
  67. /* Minimum and maximum salt length in bytes */
  68. #define ARGON2_MIN_SALT_LENGTH UINT32_C(8)
  69. #define ARGON2_MAX_SALT_LENGTH UINT32_C(0xFFFFFFFF)
  70. /* Minimum and maximum key length in bytes */
  71. #define ARGON2_MIN_SECRET UINT32_C(0)
  72. #define ARGON2_MAX_SECRET UINT32_C(0xFFFFFFFF)
  73. /* Flags to determine which fields are securely wiped (default = no wipe). */
  74. #define ARGON2_DEFAULT_FLAGS UINT32_C(0)
  75. #define ARGON2_FLAG_CLEAR_PASSWORD (UINT32_C(1) << 0)
  76. #define ARGON2_FLAG_CLEAR_SECRET (UINT32_C(1) << 1)
  77. /* Global flag to determine if we are wiping internal memory buffers. This flag
  78. * is defined in core.c and defaults to 1 (wipe internal memory). */
  79. extern int FLAG_clear_internal_memory;
  80. /* Error codes */
  81. typedef enum Argon2_ErrorCodes {
  82. ARGON2_OK = 0,
  83. ARGON2_OUTPUT_PTR_NULL = -1,
  84. ARGON2_OUTPUT_TOO_SHORT = -2,
  85. ARGON2_OUTPUT_TOO_LONG = -3,
  86. ARGON2_PWD_TOO_SHORT = -4,
  87. ARGON2_PWD_TOO_LONG = -5,
  88. ARGON2_SALT_TOO_SHORT = -6,
  89. ARGON2_SALT_TOO_LONG = -7,
  90. ARGON2_AD_TOO_SHORT = -8,
  91. ARGON2_AD_TOO_LONG = -9,
  92. ARGON2_SECRET_TOO_SHORT = -10,
  93. ARGON2_SECRET_TOO_LONG = -11,
  94. ARGON2_TIME_TOO_SMALL = -12,
  95. ARGON2_TIME_TOO_LARGE = -13,
  96. ARGON2_MEMORY_TOO_LITTLE = -14,
  97. ARGON2_MEMORY_TOO_MUCH = -15,
  98. ARGON2_LANES_TOO_FEW = -16,
  99. ARGON2_LANES_TOO_MANY = -17,
  100. ARGON2_PWD_PTR_MISMATCH = -18, /* NULL ptr with non-zero length */
  101. ARGON2_SALT_PTR_MISMATCH = -19, /* NULL ptr with non-zero length */
  102. ARGON2_SECRET_PTR_MISMATCH = -20, /* NULL ptr with non-zero length */
  103. ARGON2_AD_PTR_MISMATCH = -21, /* NULL ptr with non-zero length */
  104. ARGON2_MEMORY_ALLOCATION_ERROR = -22,
  105. ARGON2_FREE_MEMORY_CBK_NULL = -23,
  106. ARGON2_ALLOCATE_MEMORY_CBK_NULL = -24,
  107. ARGON2_INCORRECT_PARAMETER = -25,
  108. ARGON2_INCORRECT_TYPE = -26,
  109. ARGON2_OUT_PTR_MISMATCH = -27,
  110. ARGON2_THREADS_TOO_FEW = -28,
  111. ARGON2_THREADS_TOO_MANY = -29,
  112. ARGON2_MISSING_ARGS = -30,
  113. ARGON2_ENCODING_FAIL = -31,
  114. ARGON2_DECODING_FAIL = -32,
  115. ARGON2_THREAD_FAIL = -33,
  116. ARGON2_DECODING_LENGTH_FAIL = -34,
  117. ARGON2_VERIFY_MISMATCH = -35
  118. } argon2_error_codes;
  119. /* Memory allocator types --- for external allocation */
  120. typedef int (*allocate_fptr)(uint8_t **memory, size_t bytes_to_allocate);
  121. typedef void (*deallocate_fptr)(uint8_t *memory, size_t bytes_to_allocate);
  122. /* Argon2 external data structures */
  123. /*
  124. *****
  125. * Context: structure to hold Argon2 inputs:
  126. * output array and its length,
  127. * password and its length,
  128. * salt and its length,
  129. * secret and its length,
  130. * associated data and its length,
  131. * number of passes, amount of used memory (in KBytes, can be rounded up a bit)
  132. * number of parallel threads that will be run.
  133. * All the parameters above affect the output hash value.
  134. * Additionally, two function pointers can be provided to allocate and
  135. * deallocate the memory (if NULL, memory will be allocated internally).
  136. * Also, three flags indicate whether to erase password, secret as soon as they
  137. * are pre-hashed (and thus not needed anymore), and the entire memory
  138. *****
  139. * Simplest situation: you have output array out[8], password is stored in
  140. * pwd[32], salt is stored in salt[16], you do not have keys nor associated
  141. * data. You need to spend 1 GB of RAM and you run 5 passes of Argon2d with
  142. * 4 parallel lanes.
  143. * You want to erase the password, but you're OK with last pass not being
  144. * erased. You want to use the default memory allocator.
  145. * Then you initialize:
  146. Argon2_Context(out,8,pwd,32,salt,16,NULL,0,NULL,0,5,1<<20,4,4,NULL,NULL,true,false,false,false)
  147. */
  148. typedef struct Argon2_Context {
  149. uint8_t *out; /* output array */
  150. uint32_t outlen; /* digest length */
  151. uint8_t *pwd; /* password array */
  152. uint32_t pwdlen; /* password length */
  153. uint8_t *salt; /* salt array */
  154. uint32_t saltlen; /* salt length */
  155. uint8_t *secret; /* key array */
  156. uint32_t secretlen; /* key length */
  157. uint8_t *ad; /* associated data array */
  158. uint32_t adlen; /* associated data length */
  159. uint32_t t_cost; /* number of passes */
  160. uint32_t m_cost; /* amount of memory requested (KB) */
  161. uint32_t lanes; /* number of lanes */
  162. uint32_t threads; /* maximum number of threads */
  163. uint32_t version; /* version number */
  164. allocate_fptr allocate_cbk; /* pointer to memory allocator */
  165. deallocate_fptr free_cbk; /* pointer to memory deallocator */
  166. uint32_t flags; /* array of bool options */
  167. } argon2_context;
  168. /* Argon2 primitive type */
  169. typedef enum Argon2_type {
  170. Argon2_d = 0,
  171. Argon2_i = 1,
  172. Argon2_id = 2
  173. } argon2_type;
  174. /* Version of the algorithm */
  175. typedef enum Argon2_version {
  176. ARGON2_VERSION_10 = 0x10,
  177. ARGON2_VERSION_13 = 0x13,
  178. ARGON2_VERSION_NUMBER = ARGON2_VERSION_13
  179. } argon2_version;
  180. /*
  181. * Function that gives the string representation of an argon2_type.
  182. * @param type The argon2_type that we want the string for
  183. * @param uppercase Whether the string should have the first letter uppercase
  184. * @return NULL if invalid type, otherwise the string representation.
  185. */
  186. ARGON2_PUBLIC const char *argon2_type2string(argon2_type type, int uppercase);
  187. /*
  188. * Function that performs memory-hard hashing with certain degree of parallelism
  189. * @param context Pointer to the Argon2 internal structure
  190. * @return Error code if smth is wrong, ARGON2_OK otherwise
  191. */
  192. ARGON2_PUBLIC int argon2_ctx(argon2_context *context, argon2_type type);
  193. /**
  194. * Hashes a password with Argon2i, producing an encoded hash
  195. * @param t_cost Number of iterations
  196. * @param m_cost Sets memory usage to m_cost kibibytes
  197. * @param parallelism Number of threads and compute lanes
  198. * @param pwd Pointer to password
  199. * @param pwdlen Password size in bytes
  200. * @param salt Pointer to salt
  201. * @param saltlen Salt size in bytes
  202. * @param hashlen Desired length of the hash in bytes
  203. * @param encoded Buffer where to write the encoded hash
  204. * @param encodedlen Size of the buffer (thus max size of the encoded hash)
  205. * @pre Different parallelism levels will give different results
  206. * @pre Returns ARGON2_OK if successful
  207. */
  208. ARGON2_PUBLIC int argon2i_hash_encoded(const uint32_t t_cost,
  209. const uint32_t m_cost,
  210. const uint32_t parallelism,
  211. const void *pwd, const size_t pwdlen,
  212. const void *salt, const size_t saltlen,
  213. const size_t hashlen, char *encoded,
  214. const size_t encodedlen);
  215. /**
  216. * Hashes a password with Argon2i, producing a raw hash at @hash
  217. * @param t_cost Number of iterations
  218. * @param m_cost Sets memory usage to m_cost kibibytes
  219. * @param parallelism Number of threads and compute lanes
  220. * @param pwd Pointer to password
  221. * @param pwdlen Password size in bytes
  222. * @param salt Pointer to salt
  223. * @param saltlen Salt size in bytes
  224. * @param hash Buffer where to write the raw hash - updated by the function
  225. * @param hashlen Desired length of the hash in bytes
  226. * @pre Different parallelism levels will give different results
  227. * @pre Returns ARGON2_OK if successful
  228. */
  229. ARGON2_PUBLIC int argon2i_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
  230. const uint32_t parallelism, const void *pwd,
  231. const size_t pwdlen, const void *salt,
  232. const size_t saltlen, void *hash,
  233. const size_t hashlen);
  234. ARGON2_PUBLIC int argon2d_hash_encoded(const uint32_t t_cost,
  235. const uint32_t m_cost,
  236. const uint32_t parallelism,
  237. const void *pwd, const size_t pwdlen,
  238. const void *salt, const size_t saltlen,
  239. const size_t hashlen, char *encoded,
  240. const size_t encodedlen);
  241. ARGON2_PUBLIC int argon2d_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
  242. const uint32_t parallelism, const void *pwd,
  243. const size_t pwdlen, const void *salt,
  244. const size_t saltlen, void *hash,
  245. const size_t hashlen);
  246. ARGON2_PUBLIC int argon2id_hash_encoded(const uint32_t t_cost,
  247. const uint32_t m_cost,
  248. const uint32_t parallelism,
  249. const void *pwd, const size_t pwdlen,
  250. const void *salt, const size_t saltlen,
  251. const size_t hashlen, char *encoded,
  252. const size_t encodedlen);
  253. ARGON2_PUBLIC int argon2id_hash_raw(const uint32_t t_cost,
  254. const uint32_t m_cost,
  255. const uint32_t parallelism, const void *pwd,
  256. const size_t pwdlen, const void *salt,
  257. const size_t saltlen, void *hash,
  258. const size_t hashlen);
  259. /* generic function underlying the above ones */
  260. ARGON2_PUBLIC int argon2_hash(const uint32_t t_cost, const uint32_t m_cost,
  261. const uint32_t parallelism, const void *pwd,
  262. const size_t pwdlen, const void *salt,
  263. const size_t saltlen, void *hash,
  264. const size_t hashlen, char *encoded,
  265. const size_t encodedlen, argon2_type type,
  266. const uint32_t version);
  267. /**
  268. * Verifies a password against an encoded string
  269. * Encoded string is restricted as in validate_inputs()
  270. * @param encoded String encoding parameters, salt, hash
  271. * @param pwd Pointer to password
  272. * @pre Returns ARGON2_OK if successful
  273. */
  274. ARGON2_PUBLIC int argon2i_verify(const char *encoded, const void *pwd,
  275. const size_t pwdlen);
  276. ARGON2_PUBLIC int argon2d_verify(const char *encoded, const void *pwd,
  277. const size_t pwdlen);
  278. ARGON2_PUBLIC int argon2id_verify(const char *encoded, const void *pwd,
  279. const size_t pwdlen);
  280. /* generic function underlying the above ones */
  281. ARGON2_PUBLIC int argon2_verify(const char *encoded, const void *pwd,
  282. const size_t pwdlen, argon2_type type);
  283. /**
  284. * Argon2d: Version of Argon2 that picks memory blocks depending
  285. * on the password and salt. Only for side-channel-free
  286. * environment!!
  287. *****
  288. * @param context Pointer to current Argon2 context
  289. * @return Zero if successful, a non zero error code otherwise
  290. */
  291. ARGON2_PUBLIC int argon2d_ctx(argon2_context *context);
  292. /**
  293. * Argon2i: Version of Argon2 that picks memory blocks
  294. * independent on the password and salt. Good for side-channels,
  295. * but worse w.r.t. tradeoff attacks if only one pass is used.
  296. *****
  297. * @param context Pointer to current Argon2 context
  298. * @return Zero if successful, a non zero error code otherwise
  299. */
  300. ARGON2_PUBLIC int argon2i_ctx(argon2_context *context);
  301. /**
  302. * Argon2id: Version of Argon2 where the first half-pass over memory is
  303. * password-independent, the rest are password-dependent (on the password and
  304. * salt). OK against side channels (they reduce to 1/2-pass Argon2i), and
  305. * better with w.r.t. tradeoff attacks (similar to Argon2d).
  306. *****
  307. * @param context Pointer to current Argon2 context
  308. * @return Zero if successful, a non zero error code otherwise
  309. */
  310. ARGON2_PUBLIC int argon2id_ctx(argon2_context *context);
  311. /**
  312. * Verify if a given password is correct for Argon2d hashing
  313. * @param context Pointer to current Argon2 context
  314. * @param hash The password hash to verify. The length of the hash is
  315. * specified by the context outlen member
  316. * @return Zero if successful, a non zero error code otherwise
  317. */
  318. ARGON2_PUBLIC int argon2d_verify_ctx(argon2_context *context, const char *hash);
  319. /**
  320. * Verify if a given password is correct for Argon2i hashing
  321. * @param context Pointer to current Argon2 context
  322. * @param hash The password hash to verify. The length of the hash is
  323. * specified by the context outlen member
  324. * @return Zero if successful, a non zero error code otherwise
  325. */
  326. ARGON2_PUBLIC int argon2i_verify_ctx(argon2_context *context, const char *hash);
  327. /**
  328. * Verify if a given password is correct for Argon2id hashing
  329. * @param context Pointer to current Argon2 context
  330. * @param hash The password hash to verify. The length of the hash is
  331. * specified by the context outlen member
  332. * @return Zero if successful, a non zero error code otherwise
  333. */
  334. ARGON2_PUBLIC int argon2id_verify_ctx(argon2_context *context,
  335. const char *hash);
  336. /* generic function underlying the above ones */
  337. ARGON2_PUBLIC int argon2_verify_ctx(argon2_context *context, const char *hash,
  338. argon2_type type);
  339. /**
  340. * Get the associated error message for given error code
  341. * @return The error message associated with the given error code
  342. */
  343. ARGON2_PUBLIC const char *argon2_error_message(int error_code);
  344. /**
  345. * Returns the encoded hash length for the given input parameters
  346. * @param t_cost Number of iterations
  347. * @param m_cost Memory usage in kibibytes
  348. * @param parallelism Number of threads; used to compute lanes
  349. * @param saltlen Salt size in bytes
  350. * @param hashlen Hash size in bytes
  351. * @param type The argon2_type that we want the encoded length for
  352. * @return The encoded hash length in bytes
  353. */
  354. ARGON2_PUBLIC size_t argon2_encodedlen(uint32_t t_cost, uint32_t m_cost,
  355. uint32_t parallelism, uint32_t saltlen,
  356. uint32_t hashlen, argon2_type type);
  357. #if defined(__cplusplus)
  358. }
  359. #endif
  360. #endif