argon2.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 = argon2_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 = argon2_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. argon2_fill_memory_blocks(&instance, pass);
  64. }
  65. /* 5. Finalization */
  66. argon2_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 encoding requested, write it */
  114. if (encoded && encodedlen) {
  115. if (argon2_encode_string(encoded, encodedlen,
  116. &context, type) != ARGON2_OK) {
  117. sodium_memzero(out, hashlen);
  118. sodium_memzero(encoded, encodedlen);
  119. free(out);
  120. return ARGON2_ENCODING_FAIL;
  121. }
  122. }
  123. /* if raw hash requested, write it */
  124. if (hash) {
  125. memcpy(hash, out, hashlen);
  126. }
  127. sodium_memzero(out, hashlen);
  128. free(out);
  129. return ARGON2_OK;
  130. }
  131. int
  132. argon2i_hash_encoded(const uint32_t t_cost, const uint32_t m_cost,
  133. const uint32_t parallelism, const void *pwd,
  134. const size_t pwdlen, const void *salt,
  135. const size_t saltlen, const size_t hashlen, char *encoded,
  136. const size_t encodedlen)
  137. {
  138. return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
  139. NULL, hashlen, encoded, encodedlen, Argon2_i);
  140. }
  141. int
  142. argon2i_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
  143. const uint32_t parallelism, const void *pwd,
  144. const size_t pwdlen, const void *salt, const size_t saltlen,
  145. void *hash, const size_t hashlen)
  146. {
  147. return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
  148. hash, hashlen, NULL, 0, Argon2_i);
  149. }
  150. int
  151. argon2id_hash_encoded(const uint32_t t_cost, const uint32_t m_cost,
  152. const uint32_t parallelism, const void *pwd,
  153. const size_t pwdlen, const void *salt,
  154. const size_t saltlen, const size_t hashlen, char *encoded,
  155. const size_t encodedlen)
  156. {
  157. return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
  158. NULL, hashlen, encoded, encodedlen, Argon2_id);
  159. }
  160. int
  161. argon2id_hash_raw(const uint32_t t_cost, const uint32_t m_cost,
  162. const uint32_t parallelism, const void *pwd,
  163. const size_t pwdlen, const void *salt, const size_t saltlen,
  164. void *hash, const size_t hashlen)
  165. {
  166. return argon2_hash(t_cost, m_cost, parallelism, pwd, pwdlen, salt, saltlen,
  167. hash, hashlen, NULL, 0, Argon2_id);
  168. }
  169. int
  170. argon2_verify(const char *encoded, const void *pwd, const size_t pwdlen,
  171. argon2_type type)
  172. {
  173. argon2_context ctx;
  174. uint8_t *out;
  175. int decode_result;
  176. int ret;
  177. size_t encoded_len;
  178. memset(&ctx, 0, sizeof ctx);
  179. ctx.pwd = NULL;
  180. ctx.pwdlen = 0;
  181. ctx.secret = NULL;
  182. ctx.secretlen = 0;
  183. /* max values, to be updated in argon2_decode_string */
  184. encoded_len = strlen(encoded);
  185. if (encoded_len > UINT32_MAX) {
  186. return ARGON2_DECODING_LENGTH_FAIL;
  187. }
  188. ctx.adlen = (uint32_t) encoded_len;
  189. ctx.saltlen = (uint32_t) encoded_len;
  190. ctx.outlen = (uint32_t) encoded_len;
  191. ctx.ad = (uint8_t *) malloc(ctx.adlen);
  192. ctx.salt = (uint8_t *) malloc(ctx.saltlen);
  193. ctx.out = (uint8_t *) malloc(ctx.outlen);
  194. if (!ctx.out || !ctx.salt || !ctx.ad) {
  195. free(ctx.ad);
  196. free(ctx.salt);
  197. free(ctx.out);
  198. return ARGON2_MEMORY_ALLOCATION_ERROR;
  199. }
  200. out = (uint8_t *) malloc(ctx.outlen);
  201. if (!out) {
  202. free(ctx.ad);
  203. free(ctx.salt);
  204. free(ctx.out);
  205. return ARGON2_MEMORY_ALLOCATION_ERROR;
  206. }
  207. decode_result = argon2_decode_string(&ctx, encoded, type);
  208. if (decode_result != ARGON2_OK) {
  209. free(ctx.ad);
  210. free(ctx.salt);
  211. free(ctx.out);
  212. free(out);
  213. return decode_result;
  214. }
  215. ret = argon2_hash(ctx.t_cost, ctx.m_cost, ctx.threads, pwd, pwdlen,
  216. ctx.salt, ctx.saltlen, out, ctx.outlen, NULL, 0, type);
  217. free(ctx.ad);
  218. free(ctx.salt);
  219. if (ret != ARGON2_OK || sodium_memcmp(out, ctx.out, ctx.outlen) != 0) {
  220. ret = ARGON2_VERIFY_MISMATCH;
  221. }
  222. free(out);
  223. free(ctx.out);
  224. return ret;
  225. }
  226. int
  227. argon2i_verify(const char *encoded, const void *pwd, const size_t pwdlen)
  228. {
  229. return argon2_verify(encoded, pwd, pwdlen, Argon2_i);
  230. }
  231. int
  232. argon2id_verify(const char *encoded, const void *pwd, const size_t pwdlen)
  233. {
  234. return argon2_verify(encoded, pwd, pwdlen, Argon2_id);
  235. }