pwhash_argon2id.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. #include <errno.h>
  2. #include <limits.h>
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include <string.h>
  6. #include "argon2-core.h"
  7. #include "argon2.h"
  8. #include "crypto_pwhash_argon2id.h"
  9. #include "private/common.h"
  10. #include "randombytes.h"
  11. #include "utils.h"
  12. #define STR_HASHBYTES 32U
  13. int
  14. crypto_pwhash_argon2id_alg_argon2id13(void)
  15. {
  16. return crypto_pwhash_argon2id_ALG_ARGON2ID13;
  17. }
  18. size_t
  19. crypto_pwhash_argon2id_bytes_min(void)
  20. {
  21. COMPILER_ASSERT(crypto_pwhash_argon2id_BYTES_MIN >= ARGON2_MIN_OUTLEN);
  22. return crypto_pwhash_argon2id_BYTES_MIN;
  23. }
  24. size_t
  25. crypto_pwhash_argon2id_bytes_max(void)
  26. {
  27. COMPILER_ASSERT(crypto_pwhash_argon2id_BYTES_MAX <= ARGON2_MAX_OUTLEN);
  28. return crypto_pwhash_argon2id_BYTES_MAX;
  29. }
  30. size_t
  31. crypto_pwhash_argon2id_passwd_min(void)
  32. {
  33. COMPILER_ASSERT(crypto_pwhash_argon2id_PASSWD_MIN >= ARGON2_MIN_PWD_LENGTH);
  34. return crypto_pwhash_argon2id_PASSWD_MIN;
  35. }
  36. size_t
  37. crypto_pwhash_argon2id_passwd_max(void)
  38. {
  39. COMPILER_ASSERT(crypto_pwhash_argon2id_PASSWD_MAX <= ARGON2_MAX_PWD_LENGTH);
  40. return crypto_pwhash_argon2id_PASSWD_MAX;
  41. }
  42. size_t
  43. crypto_pwhash_argon2id_saltbytes(void)
  44. {
  45. COMPILER_ASSERT(crypto_pwhash_argon2id_SALTBYTES >= ARGON2_MIN_SALT_LENGTH);
  46. COMPILER_ASSERT(crypto_pwhash_argon2id_SALTBYTES <= ARGON2_MAX_SALT_LENGTH);
  47. return crypto_pwhash_argon2id_SALTBYTES;
  48. }
  49. size_t
  50. crypto_pwhash_argon2id_strbytes(void)
  51. {
  52. return crypto_pwhash_argon2id_STRBYTES;
  53. }
  54. const char*
  55. crypto_pwhash_argon2id_strprefix(void)
  56. {
  57. return crypto_pwhash_argon2id_STRPREFIX;
  58. }
  59. size_t
  60. crypto_pwhash_argon2id_opslimit_min(void)
  61. {
  62. COMPILER_ASSERT(crypto_pwhash_argon2id_OPSLIMIT_MIN >= ARGON2_MIN_TIME);
  63. return crypto_pwhash_argon2id_OPSLIMIT_MIN;
  64. }
  65. size_t
  66. crypto_pwhash_argon2id_opslimit_max(void)
  67. {
  68. COMPILER_ASSERT(crypto_pwhash_argon2id_OPSLIMIT_MAX <= ARGON2_MAX_TIME);
  69. return crypto_pwhash_argon2id_OPSLIMIT_MAX;
  70. }
  71. size_t
  72. crypto_pwhash_argon2id_memlimit_min(void)
  73. {
  74. COMPILER_ASSERT((crypto_pwhash_argon2id_MEMLIMIT_MIN / 1024U) >= ARGON2_MIN_MEMORY);
  75. return crypto_pwhash_argon2id_MEMLIMIT_MIN;
  76. }
  77. size_t
  78. crypto_pwhash_argon2id_memlimit_max(void)
  79. {
  80. COMPILER_ASSERT((crypto_pwhash_argon2id_MEMLIMIT_MAX / 1024U) <= ARGON2_MAX_MEMORY);
  81. return crypto_pwhash_argon2id_MEMLIMIT_MAX;
  82. }
  83. size_t
  84. crypto_pwhash_argon2id_opslimit_interactive(void)
  85. {
  86. return crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE;
  87. }
  88. size_t
  89. crypto_pwhash_argon2id_memlimit_interactive(void)
  90. {
  91. return crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE;
  92. }
  93. size_t
  94. crypto_pwhash_argon2id_opslimit_moderate(void)
  95. {
  96. return crypto_pwhash_argon2id_OPSLIMIT_MODERATE;
  97. }
  98. size_t
  99. crypto_pwhash_argon2id_memlimit_moderate(void)
  100. {
  101. return crypto_pwhash_argon2id_MEMLIMIT_MODERATE;
  102. }
  103. size_t
  104. crypto_pwhash_argon2id_opslimit_sensitive(void)
  105. {
  106. return crypto_pwhash_argon2id_OPSLIMIT_SENSITIVE;
  107. }
  108. size_t
  109. crypto_pwhash_argon2id_memlimit_sensitive(void)
  110. {
  111. return crypto_pwhash_argon2id_MEMLIMIT_SENSITIVE;
  112. }
  113. int
  114. crypto_pwhash_argon2id(unsigned char *const out, unsigned long long outlen,
  115. const char *const passwd, unsigned long long passwdlen,
  116. const unsigned char *const salt,
  117. unsigned long long opslimit, size_t memlimit, int alg)
  118. {
  119. memset(out, 0, outlen);
  120. if (outlen > crypto_pwhash_argon2id_BYTES_MAX) {
  121. errno = EFBIG;
  122. return -1;
  123. }
  124. if (outlen < crypto_pwhash_argon2id_BYTES_MIN) {
  125. errno = EINVAL;
  126. return -1;
  127. }
  128. if (passwdlen > crypto_pwhash_argon2id_PASSWD_MAX ||
  129. opslimit > crypto_pwhash_argon2id_OPSLIMIT_MAX ||
  130. memlimit > crypto_pwhash_argon2id_MEMLIMIT_MAX) {
  131. errno = EFBIG;
  132. return -1;
  133. }
  134. if (passwdlen < crypto_pwhash_argon2id_PASSWD_MIN ||
  135. opslimit < crypto_pwhash_argon2id_OPSLIMIT_MIN ||
  136. memlimit < crypto_pwhash_argon2id_MEMLIMIT_MIN) {
  137. errno = EINVAL;
  138. return -1;
  139. }
  140. if ((const void *) out == (const void *) passwd) {
  141. errno = EINVAL;
  142. return -1;
  143. }
  144. switch (alg) {
  145. case crypto_pwhash_argon2id_ALG_ARGON2ID13:
  146. if (argon2id_hash_raw((uint32_t) opslimit, (uint32_t) (memlimit / 1024U),
  147. (uint32_t) 1U, passwd, (size_t) passwdlen, salt,
  148. (size_t) crypto_pwhash_argon2id_SALTBYTES, out,
  149. (size_t) outlen) != ARGON2_OK) {
  150. return -1; /* LCOV_EXCL_LINE */
  151. }
  152. return 0;
  153. default:
  154. errno = EINVAL;
  155. return -1;
  156. }
  157. }
  158. int
  159. crypto_pwhash_argon2id_str(char out[crypto_pwhash_argon2id_STRBYTES],
  160. const char *const passwd,
  161. unsigned long long passwdlen,
  162. unsigned long long opslimit, size_t memlimit)
  163. {
  164. unsigned char salt[crypto_pwhash_argon2id_SALTBYTES];
  165. memset(out, 0, crypto_pwhash_argon2id_STRBYTES);
  166. if (passwdlen > crypto_pwhash_argon2id_PASSWD_MAX ||
  167. opslimit > crypto_pwhash_argon2id_OPSLIMIT_MAX ||
  168. memlimit > crypto_pwhash_argon2id_MEMLIMIT_MAX) {
  169. errno = EFBIG;
  170. return -1;
  171. }
  172. if (passwdlen < crypto_pwhash_argon2id_PASSWD_MIN ||
  173. opslimit < crypto_pwhash_argon2id_OPSLIMIT_MIN ||
  174. memlimit < crypto_pwhash_argon2id_MEMLIMIT_MIN) {
  175. errno = EINVAL;
  176. return -1;
  177. }
  178. randombytes_buf(salt, sizeof salt);
  179. if (argon2id_hash_encoded((uint32_t) opslimit, (uint32_t) (memlimit / 1024U),
  180. (uint32_t) 1U, passwd, (size_t) passwdlen, salt,
  181. sizeof salt, STR_HASHBYTES, out,
  182. crypto_pwhash_argon2id_STRBYTES) != ARGON2_OK) {
  183. return -1; /* LCOV_EXCL_LINE */
  184. }
  185. return 0;
  186. }
  187. int
  188. crypto_pwhash_argon2id_str_verify(const char str[crypto_pwhash_argon2id_STRBYTES],
  189. const char *const passwd,
  190. unsigned long long passwdlen)
  191. {
  192. int verify_ret;
  193. if (passwdlen > crypto_pwhash_argon2id_PASSWD_MAX) {
  194. errno = EFBIG;
  195. return -1;
  196. }
  197. /* LCOV_EXCL_START */
  198. if (passwdlen < crypto_pwhash_argon2id_PASSWD_MIN) {
  199. errno = EINVAL;
  200. return -1;
  201. }
  202. /* LCOV_EXCL_STOP */
  203. verify_ret = argon2id_verify(str, passwd, (size_t) passwdlen);
  204. if (verify_ret == ARGON2_OK) {
  205. return 0;
  206. }
  207. if (verify_ret == ARGON2_VERIFY_MISMATCH) {
  208. errno = EINVAL;
  209. }
  210. return -1;
  211. }