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