argon2.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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
  10. * this software. If not, see
  11. * <http://creativecommons.org/publicdomain/zero/1.0/>.
  12. */
  13. #include <limits.h>
  14. #include <stdint.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include "utils.h"
  19. #include "argon2-core.h"
  20. #include "argon2-encoding.h"
  21. #include "argon2.h"
  22. int
  23. argon2_ctx(argon2_context *context, argon2_type type)
  24. {
  25. /* 1. Validate all inputs */
  26. int result = validate_inputs(context);
  27. uint32_t memory_blocks, segment_length;
  28. uint32_t pass;
  29. argon2_instance_t instance;
  30. if (ARGON2_OK != result) {
  31. return result;
  32. }
  33. if (type != Argon2_id && type != Argon2_i) {
  34. return ARGON2_INCORRECT_TYPE;
  35. }
  36. /* 2. Align memory size */
  37. /* Minimum memory_blocks = 8L blocks, where L is the number of lanes */
  38. memory_blocks = context->m_cost;
  39. if (memory_blocks < 2 * ARGON2_SYNC_POINTS * context->lanes) {
  40. memory_blocks = 2 * ARGON2_SYNC_POINTS * context->lanes;
  41. }
  42. segment_length = memory_blocks / (context->lanes * ARGON2_SYNC_POINTS);
  43. /* Ensure that all segments have equal length */
  44. memory_blocks = segment_length * (context->lanes * ARGON2_SYNC_POINTS);
  45. instance.region = NULL;
  46. instance.passes = context->t_cost;
  47. instance.current_pass = ~ 0U;
  48. instance.memory_blocks = memory_blocks;
  49. instance.segment_length = segment_length;
  50. instance.lane_length = segment_length * ARGON2_SYNC_POINTS;
  51. instance.lanes = context->lanes;
  52. instance.threads = context->threads;
  53. instance.type = type;
  54. /* 3. Initialization: Hashing inputs, allocating memory, filling first
  55. * blocks
  56. */
  57. result = initialize(&instance, context);
  58. if (ARGON2_OK != result) {
  59. return result;
  60. }
  61. /* 4. Filling memory */
  62. for (pass = 0; pass < instance.passes; pass++) {
  63. fill_memory_blocks(&instance, pass);
  64. }
  65. /* 5. Finalization */
  66. finalize(context, &instance);
  67. return ARGON2_OK;
  68. }
  69. int
  70. argon2_hash(const uint32_t t_cost, const uint32_t m_cost,
  71. const uint32_t parallelism, const void *pwd, const size_t pwdlen,
  72. const void *salt, const size_t saltlen, void *hash,
  73. const size_t hashlen, char *encoded, const size_t encodedlen,
  74. argon2_type type)
  75. {
  76. argon2_context context;
  77. int result;
  78. uint8_t *out;
  79. if (pwdlen > ARGON2_MAX_PWD_LENGTH) {
  80. return ARGON2_PWD_TOO_LONG;
  81. }
  82. if (hashlen > ARGON2_MAX_OUTLEN) {
  83. return ARGON2_OUTPUT_TOO_LONG;
  84. }
  85. if (saltlen > ARGON2_MAX_SALT_LENGTH) {
  86. return ARGON2_SALT_TOO_LONG;
  87. }
  88. out = (uint8_t *) malloc(hashlen);
  89. if (!out) {
  90. return ARGON2_MEMORY_ALLOCATION_ERROR;
  91. }
  92. context.out = (uint8_t *) out;
  93. context.outlen = (uint32_t) hashlen;
  94. context.pwd = (uint8_t *) pwd;
  95. context.pwdlen = (uint32_t) pwdlen;
  96. context.salt = (uint8_t *) salt;
  97. context.saltlen = (uint32_t) saltlen;
  98. context.secret = NULL;
  99. context.secretlen = 0;
  100. context.ad = NULL;
  101. context.adlen = 0;
  102. context.t_cost = t_cost;
  103. context.m_cost = m_cost;
  104. context.lanes = parallelism;
  105. context.threads = parallelism;
  106. context.flags = ARGON2_DEFAULT_FLAGS;
  107. result = argon2_ctx(&context, type);
  108. if (result != ARGON2_OK) {
  109. sodium_memzero(out, hashlen);
  110. free(out);
  111. return result;
  112. }
  113. /* if raw hash requested, write it */
  114. if (hash) {
  115. memcpy(hash, out, hashlen);
  116. }
  117. /* if encoding requested, write it */
  118. if (encoded && encodedlen) {
  119. if (encode_string(encoded, encodedlen, &context, type) != ARGON2_OK) {
  120. sodium_memzero(out, hashlen);
  121. sodium_memzero(encoded, encodedlen);
  122. free(out);
  123. return ARGON2_ENCODING_FAIL;
  124. }
  125. }
  126. sodium_memzero(out, hashlen);
  127. free(out);
  128. return ARGON2_OK;
  129. }
  130. int
  131. argon2i_hash_encoded(const uint32_t t_cost, const uint32_t m_cost,
  132. const uint32_t parallelism, const void *pwd,
  133. const size_t pwdlen, const void *salt,
  134. const size_t saltlen, const size_t hashlen, char *encoded,
  135. const size_t encodedlen)
  136. {
  137. return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
  138. NULL, hashlen, encoded, encodedlen, Argon2_i);
  139. }
  140. int
  141. argon2i_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
  142. const uint32_t parallelism, const void *pwd,
  143. const size_t pwdlen, const void *salt, const size_t saltlen,
  144. void *hash, const size_t hashlen)
  145. {
  146. return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
  147. hash, hashlen, NULL, 0, Argon2_i);
  148. }
  149. int
  150. argon2id_hash_encoded(const uint32_t t_cost, const uint32_t m_cost,
  151. const uint32_t parallelism, const void *pwd,
  152. const size_t pwdlen, const void *salt,
  153. const size_t saltlen, const size_t hashlen, char *encoded,
  154. const size_t encodedlen)
  155. {
  156. return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
  157. NULL, hashlen, encoded, encodedlen, Argon2_id);
  158. }
  159. int
  160. argon2id_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
  161. const uint32_t parallelism, const void *pwd,
  162. const size_t pwdlen, const void *salt, const size_t saltlen,
  163. void *hash, const size_t hashlen)
  164. {
  165. return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
  166. hash, hashlen, NULL, 0, Argon2_id);
  167. }
  168. int
  169. argon2_verify(const char *encoded, const void *pwd, const size_t pwdlen,
  170. argon2_type type)
  171. {
  172. argon2_context ctx;
  173. uint8_t *out;
  174. int decode_result;
  175. int ret;
  176. size_t encoded_len;
  177. memset(&ctx, 0, sizeof ctx);
  178. ctx.pwd = NULL;
  179. ctx.pwdlen = 0;
  180. ctx.secret = NULL;
  181. ctx.secretlen = 0;
  182. /* max values, to be updated in decode_string */
  183. encoded_len = strlen(encoded);
  184. if (encoded_len > UINT32_MAX) {
  185. return ARGON2_DECODING_LENGTH_FAIL;
  186. }
  187. ctx.adlen = (uint32_t) encoded_len;
  188. ctx.saltlen = (uint32_t) encoded_len;
  189. ctx.outlen = (uint32_t) encoded_len;
  190. ctx.ad = (uint8_t *) malloc(ctx.adlen);
  191. ctx.salt = (uint8_t *) malloc(ctx.saltlen);
  192. ctx.out = (uint8_t *) malloc(ctx.outlen);
  193. if (!ctx.out || !ctx.salt || !ctx.ad) {
  194. free(ctx.ad);
  195. free(ctx.salt);
  196. free(ctx.out);
  197. return ARGON2_MEMORY_ALLOCATION_ERROR;
  198. }
  199. out = (uint8_t *) malloc(ctx.outlen);
  200. if (!out) {
  201. free(ctx.ad);
  202. free(ctx.salt);
  203. free(ctx.out);
  204. return ARGON2_MEMORY_ALLOCATION_ERROR;
  205. }
  206. decode_result = decode_string(&ctx, encoded, type);
  207. if (decode_result != ARGON2_OK) {
  208. free(ctx.ad);
  209. free(ctx.salt);
  210. free(ctx.out);
  211. free(out);
  212. return decode_result;
  213. }
  214. ret = argon2_hash(ctx.t_cost, ctx.m_cost, ctx.threads, pwd, pwdlen,
  215. ctx.salt, ctx.saltlen, out, ctx.outlen, NULL, 0, type);
  216. free(ctx.ad);
  217. free(ctx.salt);
  218. if (ret != ARGON2_OK || sodium_memcmp(out, ctx.out, ctx.outlen) != 0) {
  219. ret = ARGON2_VERIFY_MISMATCH;
  220. }
  221. free(out);
  222. free(ctx.out);
  223. return ret;
  224. }
  225. int
  226. argon2i_verify(const char *encoded, const void *pwd, const size_t pwdlen)
  227. {
  228. return argon2_verify(encoded, pwd, pwdlen, Argon2_i);
  229. }
  230. int
  231. argon2id_verify(const char *encoded, const void *pwd, const size_t pwdlen)
  232. {
  233. return argon2_verify(encoded, pwd, pwdlen, Argon2_id);
  234. }