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