pwhash_argon2id.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. switch (alg) {
  141. case crypto_pwhash_argon2id_ALG_ARGON2ID13:
  142. if (argon2id_hash_raw((uint32_t) opslimit, (uint32_t) (memlimit / 1024U),
  143. (uint32_t) 1U, passwd, (size_t) passwdlen, salt,
  144. (size_t) crypto_pwhash_argon2id_SALTBYTES, out,
  145. (size_t) outlen) != ARGON2_OK) {
  146. return -1; /* LCOV_EXCL_LINE */
  147. }
  148. return 0;
  149. default:
  150. errno = EINVAL;
  151. return -1;
  152. }
  153. }
  154. int
  155. crypto_pwhash_argon2id_str(char out[crypto_pwhash_argon2id_STRBYTES],
  156. const char *const passwd,
  157. unsigned long long passwdlen,
  158. unsigned long long opslimit, size_t memlimit)
  159. {
  160. unsigned char salt[crypto_pwhash_argon2id_SALTBYTES];
  161. memset(out, 0, crypto_pwhash_argon2id_STRBYTES);
  162. if (passwdlen > crypto_pwhash_argon2id_PASSWD_MAX ||
  163. opslimit > crypto_pwhash_argon2id_OPSLIMIT_MAX ||
  164. memlimit > crypto_pwhash_argon2id_MEMLIMIT_MAX) {
  165. errno = EFBIG;
  166. return -1;
  167. }
  168. if (passwdlen < crypto_pwhash_argon2id_PASSWD_MIN ||
  169. opslimit < crypto_pwhash_argon2id_OPSLIMIT_MIN ||
  170. memlimit < crypto_pwhash_argon2id_MEMLIMIT_MIN) {
  171. errno = EINVAL;
  172. return -1;
  173. }
  174. randombytes_buf(salt, sizeof salt);
  175. if (argon2id_hash_encoded((uint32_t) opslimit, (uint32_t) (memlimit / 1024U),
  176. (uint32_t) 1U, passwd, (size_t) passwdlen, salt,
  177. sizeof salt, STR_HASHBYTES, out,
  178. crypto_pwhash_argon2id_STRBYTES) != ARGON2_OK) {
  179. return -1; /* LCOV_EXCL_LINE */
  180. }
  181. return 0;
  182. }
  183. int
  184. crypto_pwhash_argon2id_str_verify(const char str[crypto_pwhash_argon2id_STRBYTES],
  185. const char *const passwd,
  186. unsigned long long passwdlen)
  187. {
  188. int verify_ret;
  189. if (passwdlen > crypto_pwhash_argon2id_PASSWD_MAX) {
  190. errno = EFBIG;
  191. return -1;
  192. }
  193. /* LCOV_EXCL_START */
  194. if (passwdlen < crypto_pwhash_argon2id_PASSWD_MIN) {
  195. errno = EINVAL;
  196. return -1;
  197. }
  198. /* LCOV_EXCL_STOP */
  199. verify_ret = argon2id_verify(str, passwd, (size_t) passwdlen);
  200. if (verify_ret == ARGON2_OK) {
  201. return 0;
  202. }
  203. if (verify_ret == ARGON2_VERIFY_MISMATCH) {
  204. errno = EINVAL;
  205. }
  206. return -1;
  207. }