xs/pwhash.xs

filter: Statementstmt 70.5% 242 / 343 Branchbran 33.6% 39 / 116 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 61.2% 281 / 459 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/onetimeauth.xs ↑ Summary xs/auth.xs »
1 MODULE = Crypt::Sodium::XS PACKAGE = Crypt::Sodium::XS::pwhash
2  
3 void _define_constants()
4 PREINIT:
5 3 HV *stash = gv_stashpv("Crypt::Sodium::XS::pwhash", 0);
6  
7 PPCODE:
8 3 newCONSTSUB(stash, "pwhash_BYTES_MAX", newSVuv(crypto_pwhash_BYTES_MAX));
9 3 newCONSTSUB(stash, "pwhash_argon2i_BYTES_MAX",
10 newSVuv(crypto_pwhash_argon2i_BYTES_MAX));
11 3 newCONSTSUB(stash, "pwhash_argon2id_BYTES_MAX",
12 newSVuv(crypto_pwhash_argon2id_BYTES_MAX));
13 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_BYTES_MAX",
14 newSVuv(crypto_pwhash_scryptsalsa208sha256_BYTES_MAX));
15 3 newCONSTSUB(stash, "pwhash_BYTES_MIN", newSVuv(crypto_pwhash_BYTES_MIN));
16 3 newCONSTSUB(stash, "pwhash_argon2i_BYTES_MIN",
17 newSVuv(crypto_pwhash_argon2i_BYTES_MIN));
18 3 newCONSTSUB(stash, "pwhash_argon2id_BYTES_MIN",
19 newSVuv(crypto_pwhash_argon2id_BYTES_MIN));
20 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_BYTES_MIN",
21 newSVuv(crypto_pwhash_scryptsalsa208sha256_BYTES_MIN));
22 3 newCONSTSUB(stash, "pwhash_MEMLIMIT_INTERACTIVE",
23 newSVuv(crypto_pwhash_MEMLIMIT_INTERACTIVE));
24 3 newCONSTSUB(stash, "pwhash_argon2i_MEMLIMIT_INTERACTIVE",
25 newSVuv(crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE));
26 3 newCONSTSUB(stash, "pwhash_argon2id_MEMLIMIT_INTERACTIVE",
27 newSVuv(crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE));
28 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE",
29 newSVuv(crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE));
30 3 newCONSTSUB(stash, "pwhash_MEMLIMIT_MAX",
31 newSVuv(crypto_pwhash_MEMLIMIT_MAX));
32 3 newCONSTSUB(stash, "pwhash_argon2i_MEMLIMIT_MAX",
33 newSVuv(crypto_pwhash_argon2i_MEMLIMIT_MAX));
34 3 newCONSTSUB(stash, "pwhash_argon2id_MEMLIMIT_MAX",
35 newSVuv(crypto_pwhash_argon2id_MEMLIMIT_MAX));
36 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_MEMLIMIT_MAX",
37 newSVuv(crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX));
38 3 newCONSTSUB(stash, "pwhash_MEMLIMIT_MIN",
39 newSVuv(crypto_pwhash_MEMLIMIT_MIN));
40 3 newCONSTSUB(stash, "pwhash_argon2i_MEMLIMIT_MIN",
41 newSVuv(crypto_pwhash_argon2i_MEMLIMIT_MIN));
42 3 newCONSTSUB(stash, "pwhash_argon2id_MEMLIMIT_MIN",
43 newSVuv(crypto_pwhash_argon2id_MEMLIMIT_MIN));
44 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_MEMLIMIT_MIN",
45 newSVuv(crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN));
46 3 newCONSTSUB(stash, "pwhash_MEMLIMIT_MODERATE",
47 newSVuv(crypto_pwhash_MEMLIMIT_MODERATE));
48 3 newCONSTSUB(stash, "pwhash_argon2i_MEMLIMIT_MODERATE",
49 newSVuv(crypto_pwhash_argon2i_MEMLIMIT_MODERATE));
50 3 newCONSTSUB(stash, "pwhash_argon2id_MEMLIMIT_MODERATE",
51 newSVuv(crypto_pwhash_argon2id_MEMLIMIT_MODERATE));
52 3 newCONSTSUB(stash, "pwhash_MEMLIMIT_SENSITIVE",
53 newSVuv(crypto_pwhash_MEMLIMIT_SENSITIVE));
54 3 newCONSTSUB(stash, "pwhash_argon2i_MEMLIMIT_SENSITIVE",
55 newSVuv(crypto_pwhash_argon2i_MEMLIMIT_SENSITIVE));
56 3 newCONSTSUB(stash, "pwhash_argon2id_MEMLIMIT_SENSITIVE",
57 newSVuv(crypto_pwhash_argon2id_MEMLIMIT_SENSITIVE));
58 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE",
59 newSVuv(crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_SENSITIVE));
60 3 newCONSTSUB(stash, "pwhash_OPSLIMIT_INTERACTIVE",
61 newSVuv(crypto_pwhash_OPSLIMIT_INTERACTIVE));
62 3 newCONSTSUB(stash, "pwhash_argon2i_OPSLIMIT_INTERACTIVE",
63 newSVuv(crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE));
64 3 newCONSTSUB(stash, "pwhash_argon2id_OPSLIMIT_INTERACTIVE",
65 newSVuv(crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE));
66 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE",
67 newSVuv(crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE));
68 3 newCONSTSUB(stash, "pwhash_OPSLIMIT_MAX",
69 newSVuv(crypto_pwhash_OPSLIMIT_MAX));
70 3 newCONSTSUB(stash, "pwhash_argon2i_OPSLIMIT_MAX",
71 newSVuv(crypto_pwhash_argon2i_OPSLIMIT_MAX));
72 3 newCONSTSUB(stash, "pwhash_argon2id_OPSLIMIT_MAX",
73 newSVuv(crypto_pwhash_argon2id_OPSLIMIT_MAX));
74 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_OPSLIMIT_MAX",
75 newSVuv(crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX));
76 3 newCONSTSUB(stash, "pwhash_OPSLIMIT_MIN",
77 newSVuv(crypto_pwhash_OPSLIMIT_MIN));
78 3 newCONSTSUB(stash, "pwhash_argon2i_OPSLIMIT_MIN",
79 newSVuv(crypto_pwhash_argon2i_OPSLIMIT_MIN));
80 3 newCONSTSUB(stash, "pwhash_argon2id_OPSLIMIT_MIN",
81 newSVuv(crypto_pwhash_argon2id_OPSLIMIT_MIN));
82 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_OPSLIMIT_MIN",
83 newSVuv(crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN));
84 3 newCONSTSUB(stash, "pwhash_OPSLIMIT_MODERATE",
85 newSVuv(crypto_pwhash_OPSLIMIT_MODERATE));
86 3 newCONSTSUB(stash, "pwhash_argon2i_OPSLIMIT_MODERATE",
87 newSVuv(crypto_pwhash_argon2i_OPSLIMIT_MODERATE));
88 3 newCONSTSUB(stash, "pwhash_argon2id_OPSLIMIT_MODERATE",
89 newSVuv(crypto_pwhash_argon2id_OPSLIMIT_MODERATE));
90 3 newCONSTSUB(stash, "pwhash_OPSLIMIT_SENSITIVE",
91 newSVuv(crypto_pwhash_OPSLIMIT_SENSITIVE));
92 3 newCONSTSUB(stash, "pwhash_argon2i_OPSLIMIT_SENSITIVE",
93 newSVuv(crypto_pwhash_argon2i_OPSLIMIT_SENSITIVE));
94 3 newCONSTSUB(stash, "pwhash_argon2id_OPSLIMIT_SENSITIVE",
95 newSVuv(crypto_pwhash_argon2id_OPSLIMIT_SENSITIVE));
96 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE",
97 newSVuv(crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_SENSITIVE));
98 3 newCONSTSUB(stash, "pwhash_PASSWD_MAX", newSVuv(crypto_pwhash_PASSWD_MAX));
99 3 newCONSTSUB(stash, "pwhash_argon2i_PASSWD_MAX",
100 newSVuv(crypto_pwhash_argon2i_PASSWD_MAX));
101 3 newCONSTSUB(stash, "pwhash_argon2id_PASSWD_MAX",
102 newSVuv(crypto_pwhash_argon2id_PASSWD_MAX));
103 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_PASSWD_MAX",
104 newSVuv(crypto_pwhash_scryptsalsa208sha256_PASSWD_MAX));
105 3 newCONSTSUB(stash, "pwhash_PASSWD_MIN", newSVuv(crypto_pwhash_PASSWD_MIN));
106 3 newCONSTSUB(stash, "pwhash_argon2i_PASSWD_MIN",
107 newSVuv(crypto_pwhash_argon2i_PASSWD_MIN));
108 3 newCONSTSUB(stash, "pwhash_argon2id_PASSWD_MIN",
109 newSVuv(crypto_pwhash_argon2id_PASSWD_MIN));
110 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_PASSWD_MIN",
111 newSVuv(crypto_pwhash_scryptsalsa208sha256_PASSWD_MIN));
112 3 newCONSTSUB(stash, "pwhash_SALTBYTES", newSVuv(crypto_pwhash_SALTBYTES));
113 3 newCONSTSUB(stash, "pwhash_argon2i_SALTBYTES",
114 newSVuv(crypto_pwhash_argon2i_SALTBYTES));
115 3 newCONSTSUB(stash, "pwhash_argon2id_SALTBYTES",
116 newSVuv(crypto_pwhash_argon2id_SALTBYTES));
117 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_SALTBYTES",
118 newSVuv(crypto_pwhash_scryptsalsa208sha256_SALTBYTES));
119 3 newCONSTSUB(stash, "pwhash_STRBYTES", newSVuv(crypto_pwhash_STRBYTES));
120 3 newCONSTSUB(stash, "pwhash_argon2i_STRBYTES",
121 newSVuv(crypto_pwhash_argon2i_STRBYTES));
122 3 newCONSTSUB(stash, "pwhash_argon2id_STRBYTES",
123 newSVuv(crypto_pwhash_argon2id_STRBYTES));
124 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_STRBYTES",
125 newSVuv(crypto_pwhash_scryptsalsa208sha256_STRBYTES));
126 3 newCONSTSUB(stash, "pwhash_STRPREFIX", newSVpvs(crypto_pwhash_STRPREFIX));
127 3 newCONSTSUB(stash, "pwhash_argon2i_STRPREFIX",
128 newSVpvs(crypto_pwhash_argon2i_STRPREFIX));
129 3 newCONSTSUB(stash, "pwhash_argon2id_STRPREFIX",
130 newSVpvs(crypto_pwhash_argon2id_STRPREFIX));
131 3 newCONSTSUB(stash, "pwhash_scryptsalsa208sha256_STRPREFIX",
132 newSVpvs(crypto_pwhash_scryptsalsa208sha256_STRPREFIX));
133 3 newCONSTSUB(stash, "pwhash_PRIMITIVE", newSVpvs(crypto_pwhash_PRIMITIVE));
134  
135 void pwhash_scryptsalsa208sha256_MEMLIMIT_MODERATE()
136 PPCODE:
137 1 croak("This primitive does not support MEMLIMIT_MODERATE");
138  
139 void pwhash_scryptsalsa208sha256_OPSLIMIT_MODERATE()
140 PPCODE:
141 1 croak("This primitive does not support OPSLIMIT_MODERATE");
142  
143 SV * pwhash( \
144 SV * passphrase, \
145 SV * salt, \
146 STRLEN out_len = 0, \
147 STRLEN opslimit = 0, \
148 STRLEN memlimit = 0, \
149 U32 flags = 0 \
150 )
151  
152 ALIAS:
153 pwhash_argon2i = 1
154 pwhash_argon2id = 2
155 pwhash_scryptsalsa208sha256 = 3
156  
157 PREINIT:
158 32 protmem *out_pm, *pw_pm = NULL;
159 unsigned char *salt_buf, *pw_buf;
160 STRLEN salt_len, pw_len, salt_req_len;
161 32 int alg = 0, ret;
162 32 ▶ U32 new_flags = g_protmem_default_flags_key;
Branch: 1 branch
BranchTrueFalseTotal
U32 new_flags = g_protmem_default_flags_key; 0 32 32
163 size_t out_min, out_max;
164 size_t opslimit_def, opslimit_min, opslimit_max;
165 size_t memlimit_def, memlimit_min, memlimit_max;
166  
167 CODE:
168 32 switch(ix) {
169 8 case 1:
170 8 alg = crypto_pwhash_ALG_ARGON2I13;
171 8 out_min = crypto_pwhash_argon2i_BYTES_MIN;
172 8 out_max = crypto_pwhash_argon2i_BYTES_MAX;
173 8 salt_req_len = crypto_pwhash_argon2i_SALTBYTES;
174 8 opslimit_def = crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE;
175 8 opslimit_min = crypto_pwhash_argon2i_OPSLIMIT_MIN;
176 8 opslimit_max = crypto_pwhash_argon2i_OPSLIMIT_MAX;
177 8 memlimit_def = crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE;
178 8 memlimit_min = crypto_pwhash_argon2i_MEMLIMIT_MIN;
179 8 memlimit_max = crypto_pwhash_argon2i_MEMLIMIT_MAX;
180 8 break;
181 8 case 2:
182 8 alg = crypto_pwhash_ALG_ARGON2ID13;
183 8 out_min = crypto_pwhash_argon2id_BYTES_MIN;
184 8 out_max = crypto_pwhash_argon2id_BYTES_MAX;
185 8 salt_req_len = crypto_pwhash_argon2id_SALTBYTES;
186 8 opslimit_def = crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE;
187 8 opslimit_min = crypto_pwhash_argon2id_OPSLIMIT_MIN;
188 8 opslimit_max = crypto_pwhash_argon2id_OPSLIMIT_MAX;
189 8 memlimit_def = crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE;
190 8 memlimit_min = crypto_pwhash_argon2id_MEMLIMIT_MIN;
191 8 memlimit_max = crypto_pwhash_argon2id_MEMLIMIT_MAX;
192 8 break;
193 8 case 3:
194 8 out_min = crypto_pwhash_scryptsalsa208sha256_BYTES_MIN;
195 8 out_max = crypto_pwhash_scryptsalsa208sha256_BYTES_MAX;
196 8 salt_req_len = crypto_pwhash_scryptsalsa208sha256_SALTBYTES;
197 8 opslimit_def = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE;
198 8 opslimit_min = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN;
199 8 opslimit_max = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX;
200 8 memlimit_def = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE;
201 8 memlimit_min = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN;
202 8 memlimit_max = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX;
203 8 break;
204 8 default:
205 8 alg = crypto_pwhash_ALG_DEFAULT;
206 8 out_min = crypto_pwhash_BYTES_MIN;
207 8 out_max = crypto_pwhash_BYTES_MAX;
208 8 salt_req_len = crypto_pwhash_SALTBYTES;
209 8 opslimit_def = crypto_pwhash_OPSLIMIT_INTERACTIVE;
210 8 opslimit_min = crypto_pwhash_OPSLIMIT_MIN;
211 8 opslimit_max = crypto_pwhash_OPSLIMIT_MAX;
212 8 memlimit_def = crypto_pwhash_MEMLIMIT_INTERACTIVE;
213 8 memlimit_min = crypto_pwhash_MEMLIMIT_MIN;
214 8 memlimit_max = crypto_pwhash_MEMLIMIT_MAX;
215 }
216  
217 32 ▶ if (flags)
Branch: 1 branch
BranchTrueFalseTotal
if (flags) 0 32 32
218 0 new_flags = flags;
219  
220 32 ▶ if (out_len == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (out_len == 0) 0 32 32
221 /* default out length identical to what the current string default uses */
222 0 out_len = 32;
223 else {
224 32 ▶ if (out_len < out_min || out_len > out_max)
Branch: 2 branches
BranchTrueFalseTotal
if (out_len < out_min || out_len > out_max) 32 0 32
if (out_len < out_min || out_len > out_max) 0 32 32
225 0 croak("pwhash: Invalid output length %lu", out_len);
226 }
227 32 ▶ if (opslimit == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (opslimit == 0) 32 0 32
228 32 opslimit = opslimit_def;
229 else {
230 0 ▶ if (opslimit < opslimit_min || opslimit > opslimit_max)
Branch: 2 branches
BranchTrueFalseTotal
if (opslimit < opslimit_min || opslimit > opslimit_max) 0 0 0
if (opslimit < opslimit_min || opslimit > opslimit_max) 0 0 0
231 0 croak("pwhash: Invalid opslimit %lu", opslimit);
232 }
233 32 ▶ if (memlimit == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memlimit == 0) 32 0 32
234 32 memlimit = memlimit_def;
235 else {
236 0 ▶ if (memlimit < memlimit_min || memlimit > memlimit_max)
Branch: 2 branches
BranchTrueFalseTotal
if (memlimit < memlimit_min || memlimit > memlimit_max) 0 0 0
if (memlimit < memlimit_min || memlimit > memlimit_max) 0 0 0
237 0 croak("pwhash: Invalid memlimit %lu", memlimit);
238 }
239  
240 32 salt_buf = (unsigned char *)SvPVbyte(salt, salt_len);
241 32 ▶ if (salt_len != salt_req_len)
Branch: 1 branch
BranchTrueFalseTotal
if (salt_len != salt_req_len) 0 32 32
242 0 croak("pwhash: Invalid salt length %lu", salt_len);
243  
244 32 ▶ if (sv_derived_from(passphrase, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(passphrase, MEMVAULT_CLASS)) { 0 32 32
245 0 pw_pm = protmem_get(aTHX_ passphrase, MEMVAULT_CLASS);
246 0 pw_buf = pw_pm->pm_ptr;
247 0 pw_len = pw_pm->size;
248 }
249 else
250 32 pw_buf = (unsigned char *)SvPVbyte(passphrase, pw_len);
251  
252 32 out_pm = protmem_init(aTHX_ out_len, new_flags);
253 32 ▶ if (out_pm == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (out_pm == NULL) 0 32 32
254 0 croak("pwhash: Failed to allocate protmem");
255  
256 32 ▶ if (pw_pm && protmem_grant(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (pw_pm && protmem_grant(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 32 32
if (pw_pm && protmem_grant(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
257 0 protmem_free(aTHX_ out_pm);
258 0 croak("pwhash: Failed to grant passphrase protmem RO");
259 }
260  
261 32 ▶ if (ix == 3)
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 3) 8 24 32
262 8 ret = crypto_pwhash_scryptsalsa208sha256(out_pm->pm_ptr, out_len, (char *)pw_buf,
263 pw_len, salt_buf, opslimit, memlimit);
264 else
265 24 ret = crypto_pwhash(out_pm->pm_ptr, out_len, (char *)pw_buf, pw_len,
266 salt_buf, opslimit, memlimit, alg);
267  
268 32 ▶ if (pw_pm && protmem_release(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (pw_pm && protmem_release(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 32 32
if (pw_pm && protmem_release(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
269 0 protmem_free(aTHX_ out_pm);
270 0 croak("pwhash: Failed to release passphrase protmem RO");
271 }
272 32 ▶ if (protmem_release(aTHX_ out_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ out_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 32 32
273 0 protmem_free(aTHX_ out_pm);
274 0 croak("pwhash: Failed to release passphrase protmem RO");
275 }
276  
277 32 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 0 32 32
278 0 protmem_free(aTHX_ out_pm);
279 0 croak("pwhash: pwhash failed (out of memory?)");
280 }
281  
282 32 RETVAL = protmem_to_sv(aTHX_ out_pm, MEMVAULT_CLASS);
283  
284 OUTPUT:
285 RETVAL
286  
287 SV * pwhash_salt()
288  
289 ALIAS:
290 pwhash_argon2i_salt = 1
291 pwhash_argon2id_salt = 2
292 pwhash_scryptsalsa208sha256_salt = 3
293  
294 PREINIT:
295 unsigned char *out_buf;
296 unsigned int out_len;
297  
298 CODE:
299 8 switch(ix) {
300 2 case 1:
301 2 out_len = crypto_pwhash_argon2i_SALTBYTES;
302 2 break;
303 2 case 2:
304 2 out_len = crypto_pwhash_argon2id_SALTBYTES;
305 2 break;
306 2 case 3:
307 2 out_len = crypto_pwhash_scryptsalsa208sha256_SALTBYTES;
308 2 break;
309 2 default:
310 2 out_len = crypto_pwhash_SALTBYTES;
311 }
312  
313 8 Newx(out_buf, out_len + 1, unsigned char);
314 8 ▶ if (out_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf == NULL) 0 8 8
315 0 croak("Failed to allocate memory");
316 8 out_buf[out_len] = '\0';
317  
318 8 randombytes_buf(out_buf, out_len);
319  
320 8 RETVAL = newSV(0);
321 8 sv_usepvn_flags(RETVAL, (char *)out_buf, out_len, SV_HAS_TRAILING_NUL);
322  
323 OUTPUT:
324 RETVAL
325  
326 SV * pwhash_str(SV * passphrase, STRLEN opslimit = 0, STRLEN memlimit = 0)
327  
328 ALIAS:
329 pwhash_argon2i_str = 1
330 pwhash_argon2id_str = 2
331 pwhash_scryptsalsa208sha256_str = 3
332  
333 PREINIT:
334 16 protmem *pw_pm = NULL;
335 unsigned char *pw_buf, *out_buf;
336 STRLEN pw_len;
337 16 ▶ int alg = crypto_pwhash_ALG_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
int alg = crypto_pwhash_ALG_DEFAULT; 8 8 16
338 size_t out_len;
339 size_t opslimit_def, opslimit_min, opslimit_max;
340 size_t memlimit_def, memlimit_min, memlimit_max;
341 int ret;
342  
343 CODE:
344 16 switch(ix) {
345 4 case 1:
346 4 alg = crypto_pwhash_ALG_ARGON2I13;
347 4 out_len = crypto_pwhash_argon2i_STRBYTES;
348 4 opslimit_def = crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE;
349 4 opslimit_min = crypto_pwhash_argon2i_OPSLIMIT_MIN;
350 4 opslimit_max = crypto_pwhash_argon2i_OPSLIMIT_MAX;
351 4 memlimit_def = crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE;
352 4 memlimit_min = crypto_pwhash_argon2i_MEMLIMIT_MIN;
353 4 memlimit_max = crypto_pwhash_argon2i_MEMLIMIT_MAX;
354 4 break;
355 4 case 2:
356 4 alg = crypto_pwhash_ALG_ARGON2ID13;
357 4 out_len = crypto_pwhash_argon2id_STRBYTES;
358 4 opslimit_def = crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE;
359 4 opslimit_min = crypto_pwhash_argon2id_OPSLIMIT_MIN;
360 4 opslimit_max = crypto_pwhash_argon2id_OPSLIMIT_MAX;
361 4 memlimit_def = crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE;
362 4 memlimit_min = crypto_pwhash_argon2id_MEMLIMIT_MIN;
363 4 memlimit_max = crypto_pwhash_argon2id_MEMLIMIT_MAX;
364 4 break;
365 4 case 3:
366 4 out_len = crypto_pwhash_scryptsalsa208sha256_STRBYTES;
367 4 opslimit_def = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE;
368 4 opslimit_min = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN;
369 4 opslimit_max = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX;
370 4 memlimit_def = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE;
371 4 memlimit_min = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN;
372 4 memlimit_max = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX;
373 4 break;
374 4 default:
375 4 out_len = crypto_pwhash_STRBYTES;
376 4 opslimit_def = crypto_pwhash_OPSLIMIT_INTERACTIVE;
377 4 opslimit_min = crypto_pwhash_OPSLIMIT_MIN;
378 4 opslimit_max = crypto_pwhash_OPSLIMIT_MAX;
379 4 memlimit_def = crypto_pwhash_MEMLIMIT_INTERACTIVE;
380 4 memlimit_min = crypto_pwhash_MEMLIMIT_MIN;
381 4 memlimit_max = crypto_pwhash_MEMLIMIT_MAX;
382 }
383  
384 16 ▶ if (opslimit == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (opslimit == 0) 8 8 16
385 8 opslimit = opslimit_def;
386 else {
387 8 ▶ if (opslimit < opslimit_min || opslimit > opslimit_max)
Branch: 2 branches
BranchTrueFalseTotal
if (opslimit < opslimit_min || opslimit > opslimit_max) 8 0 8
if (opslimit < opslimit_min || opslimit > opslimit_max) 0 8 8
388 0 croak("pwhash_str: Invalid opslimit %lu", opslimit);
389 }
390 16 ▶ if (memlimit == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memlimit == 0) 8 8 16
391 8 memlimit = memlimit_def;
392 else {
393 8 ▶ if (memlimit < memlimit_min || memlimit > memlimit_max)
Branch: 2 branches
BranchTrueFalseTotal
if (memlimit < memlimit_min || memlimit > memlimit_max) 8 0 8
if (memlimit < memlimit_min || memlimit > memlimit_max) 0 8 8
394 0 croak("pwhash_str: Invalid memlimit %lu", memlimit);
395 }
396  
397 16 ▶ if (sv_derived_from(passphrase, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(passphrase, MEMVAULT_CLASS)) { 0 16 16
398 0 pw_pm = protmem_get(aTHX_ passphrase, MEMVAULT_CLASS);
399 0 pw_buf = pw_pm->pm_ptr;
400 0 pw_len = pw_pm->size;
401 }
402 else
403 16 pw_buf = (unsigned char *)SvPVbyte(passphrase, pw_len);
404  
405 16 Newx(out_buf, out_len + 1, unsigned char);
406 16 ▶ if (out_buf == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (out_buf == NULL) 0 16 16
407 0 croak("pwhash_str: Failed to allocate protmem");
408 16 out_buf[out_len] = '\0';
409  
410 16 ▶ if (pw_pm && protmem_grant(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (pw_pm && protmem_grant(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 16 16
if (pw_pm && protmem_grant(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
411 0 Safefree(out_buf);
412 0 croak("pwhash_str: Failed to grant passphrase protmem RO");
413 }
414  
415 16 ▶ if (ix == 3)
Branch: 1 branch
BranchTrueFalseTotal
if (ix == 3) 4 12 16
416 4 ret = crypto_pwhash_scryptsalsa208sha256_str((char *)out_buf, (char *)pw_buf,
417 pw_len, opslimit, memlimit);
418 else
419 12 ret = crypto_pwhash_str_alg((char *)out_buf, (char *)pw_buf, pw_len,
420 opslimit, memlimit, alg);
421  
422 /* unlike the rest of the api, no argument for actual output length. >:| */
423 16 out_len = strlen((char *)out_buf);
424  
425 16 ▶ if (pw_pm && protmem_release(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (pw_pm && protmem_release(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 16 16
if (pw_pm && protmem_release(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) { 0 0 0
426 0 Safefree(out_buf);
427 0 croak("pwhash_str: Failed to release passphrase protmem RO");
428 }
429  
430 16 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 0 16 16
431 0 Safefree(out_buf);
432 0 croak("pwhash_str: pwhash_str failed (out of memory?)");
433 }
434  
435 16 RETVAL = newSV(0);
436 16 sv_usepvn_flags(RETVAL, (char *)out_buf, out_len, SV_HAS_TRAILING_NUL);
437  
438 OUTPUT:
439 RETVAL
440  
441 void pwhash_str_needs_rehash(SV * str, STRLEN opslimit = 0, STRLEN memlimit = 0)
442  
443 ALIAS:
444 pwhash_argon2i_str_needs_rehash = 1
445 pwhash_argon2id_str_needs_rehash = 2
446 pwhash_scryptsalsa208sha256_str_needs_rehash = 3
447  
448 PREINIT:
449 0 ▶ protmem *str_pm = NULL;
Branch: 1 branch
BranchTrueFalseTotal
protmem *str_pm = NULL; 0 0 0
450 char *str_buf;
451 size_t opslimit_def, opslimit_min, opslimit_max;
452 size_t memlimit_def, memlimit_min, memlimit_max;
453 STRLEN str_len;
454 int ret;
455 int (*func)(const char *, unsigned long long, size_t);
456  
457 PPCODE:
458 0 switch(ix) {
459 0 case 1:
460 0 opslimit_def = crypto_pwhash_argon2i_OPSLIMIT_INTERACTIVE;
461 0 opslimit_min = crypto_pwhash_argon2i_OPSLIMIT_MIN;
462 0 opslimit_max = crypto_pwhash_argon2i_OPSLIMIT_MAX;
463 0 memlimit_def = crypto_pwhash_argon2i_MEMLIMIT_INTERACTIVE;
464 0 memlimit_min = crypto_pwhash_argon2i_MEMLIMIT_MIN;
465 0 memlimit_max = crypto_pwhash_argon2i_MEMLIMIT_MAX;
466 0 func = crypto_pwhash_argon2i_str_needs_rehash;
467 0 break;
468 0 case 2:
469 0 opslimit_def = crypto_pwhash_argon2id_OPSLIMIT_INTERACTIVE;
470 0 opslimit_min = crypto_pwhash_argon2id_OPSLIMIT_MIN;
471 0 opslimit_max = crypto_pwhash_argon2id_OPSLIMIT_MAX;
472 0 memlimit_def = crypto_pwhash_argon2id_MEMLIMIT_INTERACTIVE;
473 0 memlimit_min = crypto_pwhash_argon2id_MEMLIMIT_MIN;
474 0 memlimit_max = crypto_pwhash_argon2id_MEMLIMIT_MAX;
475 0 func = crypto_pwhash_argon2id_str_needs_rehash;
476 0 break;
477 0 case 3:
478 0 opslimit_def = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_INTERACTIVE;
479 0 opslimit_min = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MIN;
480 0 opslimit_max = crypto_pwhash_scryptsalsa208sha256_OPSLIMIT_MAX;
481 0 memlimit_def = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_INTERACTIVE;
482 0 memlimit_min = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MIN;
483 0 memlimit_max = crypto_pwhash_scryptsalsa208sha256_MEMLIMIT_MAX;
484 0 func = crypto_pwhash_scryptsalsa208sha256_str_needs_rehash;
485 0 break;
486 0 default:
487 0 opslimit_def = crypto_pwhash_OPSLIMIT_INTERACTIVE;
488 0 opslimit_min = crypto_pwhash_OPSLIMIT_MIN;
489 0 opslimit_max = crypto_pwhash_OPSLIMIT_MAX;
490 0 memlimit_def = crypto_pwhash_MEMLIMIT_INTERACTIVE;
491 0 memlimit_min = crypto_pwhash_MEMLIMIT_MIN;
492 0 memlimit_max = crypto_pwhash_MEMLIMIT_MAX;
493 0 func = crypto_pwhash_str_needs_rehash;
494 }
495  
496 0 ▶ if (opslimit == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (opslimit == 0) 0 0 0
497 0 opslimit = opslimit_def;
498 else {
499 0 ▶ if (opslimit < opslimit_min || opslimit > opslimit_max)
Branch: 2 branches
BranchTrueFalseTotal
if (opslimit < opslimit_min || opslimit > opslimit_max) 0 0 0
if (opslimit < opslimit_min || opslimit > opslimit_max) 0 0 0
500 0 croak("pwhash_str_needs_rehash: Invalid opslimit %lu", opslimit);
501 }
502 0 ▶ if (memlimit == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (memlimit == 0) 0 0 0
503 0 memlimit = memlimit_def;
504 else {
505 0 ▶ if (memlimit < memlimit_min || memlimit > memlimit_max)
Branch: 2 branches
BranchTrueFalseTotal
if (memlimit < memlimit_min || memlimit > memlimit_max) 0 0 0
if (memlimit < memlimit_min || memlimit > memlimit_max) 0 0 0
506 0 croak("pwhash_str_needs_rehash: Invalid memlimit %lu", memlimit);
507 }
508  
509 0 ▶ if (sv_derived_from(str, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(str, MEMVAULT_CLASS)) { 0 0 0
510 0 str_pm = protmem_get(aTHX_ str, MEMVAULT_CLASS);
511 0 str_buf = str_pm->pm_ptr;
512 0 str_len = str_pm->size;
513 }
514 else
515 0 str_buf = SvPVbyte(str, str_len);
516 0 ▶ if (str_buf[str_len] != '\0')
Branch: 1 branch
BranchTrueFalseTotal
if (str_buf[str_len] != '\0') 0 0 0
517 0 croak("pwhash_str_needs_rehash: Invalid hash string");
518  
519 0 ▶ if (str_pm && protmem_grant(aTHX_ str_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (str_pm && protmem_grant(aTHX_ str_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 0 0
if (str_pm && protmem_grant(aTHX_ str_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 0 0
520 0 croak("pwhash_str_needs_rehash: Failed to grant str protmem RO");
521  
522 0 ret = func(str_buf, opslimit, memlimit);
523  
524 0 ▶ if (str_pm)
Branch: 1 branch
BranchTrueFalseTotal
if (str_pm) 0 0 0
525 0 ▶ if (protmem_release(aTHX_ str_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (protmem_release(aTHX_ str_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 0 0
526 0 croak("pwhash_str_needs_rehash: Failed to release str protmem RO");
527  
528 0 ▶ if (ret == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 0) 0 0 0
529 0 XSRETURN_NO;
530  
531 0 XSRETURN_YES;
532  
533 void pwhash_verify(SV * str, SV * passphrase)
534  
535 ALIAS:
536 pwhash_argon2i_verify = 1
537 pwhash_argon2id_verify = 2
538 pwhash_scryptsalsa208sha256_verify = 3
539  
540 PREINIT:
541 16 protmem *pw_pm = NULL;
542 unsigned char *pw_buf, *str_buf;
543 STRLEN pw_len, str_len;
544 16 int ret = 0;
545 int (*func)(const char *, const char * const, unsigned long long);
546  
547 CODE:
548 16 switch(ix) {
549 4 case 1:
550 4 func = crypto_pwhash_argon2i_str_verify;
551 4 break;
552 4 case 2:
553 4 func = crypto_pwhash_argon2id_str_verify;
554 4 break;
555 4 case 3:
556 4 func = crypto_pwhash_scryptsalsa208sha256_str_verify;
557 4 break;
558 4 default:
559 4 func = crypto_pwhash_str_verify;
560 }
561  
562 16 ▶ if (sv_derived_from(passphrase, MEMVAULT_CLASS)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sv_derived_from(passphrase, MEMVAULT_CLASS)) { 0 16 16
563 0 pw_pm = protmem_get(aTHX_ passphrase, MEMVAULT_CLASS);
564 0 pw_buf = pw_pm->pm_ptr;
565 0 pw_len = pw_pm->size;
566 }
567 else
568 16 pw_buf = (unsigned char *)SvPVbyte(passphrase, pw_len);
569  
570 16 str_buf = (unsigned char *)SvPVbyte(str, str_len);
571 16 ▶ if (str_buf[str_len] != '\0')
Branch: 1 branch
BranchTrueFalseTotal
if (str_buf[str_len] != '\0') 0 16 16
572 0 croak("pwhash_verify: Invalid hash string");
573  
574 16 ▶ if (pw_pm && protmem_grant(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (pw_pm && protmem_grant(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 16 16
if (pw_pm && protmem_grant(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 0 0
575 0 croak("pwhash_verify: Failed to grant passphrase protmem RO");
576  
577 16 ret = func((char *)str_buf, (char *)pw_buf, pw_len);
578  
579 16 ▶ if (pw_pm && protmem_release(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0)
Branch: 2 branches
BranchTrueFalseTotal
if (pw_pm && protmem_release(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 16 16
if (pw_pm && protmem_release(aTHX_ pw_pm, PROTMEM_FLAG_MPROTECT_RO) != 0) 0 0 0
580 0 croak("pwhash_verify: Failed to release passphrase protmem RO");
581  
582 16 ▶ if (ret == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ret == 0) 16 0 16
583 16 XSRETURN_YES;
584  
585 0 XSRETURN_NO;
« xs/onetimeauth.xs ↑ Summary xs/auth.xs »