crypto_pwhash.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <errno.h>
  2. #include <string.h>
  3. #include "core.h"
  4. #include "crypto_pwhash.h"
  5. int
  6. crypto_pwhash_alg_argon2i13(void)
  7. {
  8. return crypto_pwhash_ALG_ARGON2I13;
  9. }
  10. int
  11. crypto_pwhash_alg_argon2id13(void)
  12. {
  13. return crypto_pwhash_ALG_ARGON2ID13;
  14. }
  15. int
  16. crypto_pwhash_alg_default(void)
  17. {
  18. return crypto_pwhash_ALG_DEFAULT;
  19. }
  20. size_t
  21. crypto_pwhash_bytes_min(void)
  22. {
  23. return crypto_pwhash_BYTES_MIN;
  24. }
  25. size_t
  26. crypto_pwhash_bytes_max(void)
  27. {
  28. return crypto_pwhash_BYTES_MAX;
  29. }
  30. size_t
  31. crypto_pwhash_passwd_min(void)
  32. {
  33. return crypto_pwhash_PASSWD_MIN;
  34. }
  35. size_t
  36. crypto_pwhash_passwd_max(void)
  37. {
  38. return crypto_pwhash_PASSWD_MAX;
  39. }
  40. size_t
  41. crypto_pwhash_saltbytes(void)
  42. {
  43. return crypto_pwhash_SALTBYTES;
  44. }
  45. size_t
  46. crypto_pwhash_strbytes(void)
  47. {
  48. return crypto_pwhash_STRBYTES;
  49. }
  50. const char *
  51. crypto_pwhash_strprefix(void)
  52. {
  53. return crypto_pwhash_STRPREFIX;
  54. }
  55. size_t
  56. crypto_pwhash_opslimit_min(void)
  57. {
  58. return crypto_pwhash_OPSLIMIT_MIN;
  59. }
  60. size_t
  61. crypto_pwhash_opslimit_max(void)
  62. {
  63. return crypto_pwhash_OPSLIMIT_MAX;
  64. }
  65. size_t
  66. crypto_pwhash_memlimit_min(void)
  67. {
  68. return crypto_pwhash_MEMLIMIT_MIN;
  69. }
  70. size_t
  71. crypto_pwhash_memlimit_max(void)
  72. {
  73. return crypto_pwhash_MEMLIMIT_MAX;
  74. }
  75. size_t
  76. crypto_pwhash_opslimit_interactive(void)
  77. {
  78. return crypto_pwhash_OPSLIMIT_INTERACTIVE;
  79. }
  80. size_t
  81. crypto_pwhash_memlimit_interactive(void)
  82. {
  83. return crypto_pwhash_MEMLIMIT_INTERACTIVE;
  84. }
  85. size_t
  86. crypto_pwhash_opslimit_moderate(void)
  87. {
  88. return crypto_pwhash_OPSLIMIT_MODERATE;
  89. }
  90. size_t
  91. crypto_pwhash_memlimit_moderate(void)
  92. {
  93. return crypto_pwhash_MEMLIMIT_MODERATE;
  94. }
  95. size_t
  96. crypto_pwhash_opslimit_sensitive(void)
  97. {
  98. return crypto_pwhash_OPSLIMIT_SENSITIVE;
  99. }
  100. size_t
  101. crypto_pwhash_memlimit_sensitive(void)
  102. {
  103. return crypto_pwhash_MEMLIMIT_SENSITIVE;
  104. }
  105. int
  106. crypto_pwhash(unsigned char * const out, unsigned long long outlen,
  107. const char * const passwd, unsigned long long passwdlen,
  108. const unsigned char * const salt,
  109. unsigned long long opslimit, size_t memlimit, int alg)
  110. {
  111. switch (alg) {
  112. case crypto_pwhash_ALG_ARGON2I13:
  113. return crypto_pwhash_argon2i(out, outlen, passwd, passwdlen, salt,
  114. opslimit, memlimit, alg);
  115. case crypto_pwhash_ALG_ARGON2ID13:
  116. return crypto_pwhash_argon2id(out, outlen, passwd, passwdlen, salt,
  117. opslimit, memlimit, alg);
  118. default:
  119. errno = EINVAL;
  120. return -1;
  121. }
  122. }
  123. int
  124. crypto_pwhash_str(char out[crypto_pwhash_STRBYTES],
  125. const char * const passwd, unsigned long long passwdlen,
  126. unsigned long long opslimit, size_t memlimit)
  127. {
  128. return crypto_pwhash_argon2id_str(out, passwd, passwdlen,
  129. opslimit, memlimit);
  130. }
  131. int
  132. crypto_pwhash_str_alg(char out[crypto_pwhash_STRBYTES],
  133. const char * const passwd, unsigned long long passwdlen,
  134. unsigned long long opslimit, size_t memlimit, int alg)
  135. {
  136. switch (alg) {
  137. case crypto_pwhash_ALG_ARGON2I13:
  138. return crypto_pwhash_argon2i_str(out, passwd, passwdlen,
  139. opslimit, memlimit);
  140. case crypto_pwhash_ALG_ARGON2ID13:
  141. return crypto_pwhash_argon2id_str(out, passwd, passwdlen,
  142. opslimit, memlimit);
  143. }
  144. sodium_misuse();
  145. /* NOTREACHED */
  146. return -1;
  147. }
  148. int
  149. crypto_pwhash_str_verify(const char str[crypto_pwhash_STRBYTES],
  150. const char * const passwd,
  151. unsigned long long passwdlen)
  152. {
  153. if (strncmp(str, crypto_pwhash_argon2id_STRPREFIX,
  154. sizeof crypto_pwhash_argon2id_STRPREFIX - 1) == 0) {
  155. return crypto_pwhash_argon2id_str_verify(str, passwd, passwdlen);
  156. }
  157. if (strncmp(str, crypto_pwhash_argon2i_STRPREFIX,
  158. sizeof crypto_pwhash_argon2i_STRPREFIX - 1) == 0) {
  159. return crypto_pwhash_argon2i_str_verify(str, passwd, passwdlen);
  160. }
  161. errno = EINVAL;
  162. return -1;
  163. }
  164. int
  165. crypto_pwhash_str_needs_rehash(const char str[crypto_pwhash_STRBYTES],
  166. unsigned long long opslimit, size_t memlimit)
  167. {
  168. if (strncmp(str, crypto_pwhash_argon2id_STRPREFIX,
  169. sizeof crypto_pwhash_argon2id_STRPREFIX - 1) == 0) {
  170. return crypto_pwhash_argon2id_str_needs_rehash(str, opslimit, memlimit);
  171. }
  172. if (strncmp(str, crypto_pwhash_argon2i_STRPREFIX,
  173. sizeof crypto_pwhash_argon2i_STRPREFIX - 1) == 0) {
  174. return crypto_pwhash_argon2i_str_needs_rehash(str, opslimit, memlimit);
  175. }
  176. errno = EINVAL;
  177. return -1;
  178. }
  179. const char *
  180. crypto_pwhash_primitive(void) {
  181. return crypto_pwhash_PRIMITIVE;
  182. }