inc/CryptX_KeyDerivation.xs.inc

filter: Statementstmt 87.8% 174 / 198 Branchbran 45.5% 134 / 294 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 62.6% 308 / 492 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« inc/CryptX_Stream_XSalsa20.xs.inc ↑ Summary inc/CryptX_PK_DH.xs.inc »
1 MODULE = CryptX PACKAGE = Crypt::KeyDerivation
2  
3 PROTOTYPES: DISABLE
4  
5 SV *
6 pbkdf1(SV * password, SV * salt, int iteration_count = 5000, const char * hash_name = "SHA256", unsigned long output_len = 32)
7 CODE:
8 {
9 int rv, id;
10 unsigned long derived_len;
11 unsigned char *output;
12 8 unsigned char *password_ptr=NULL;
13 8 STRLEN password_len=0;
14 8 unsigned char *salt_ptr=NULL;
15 8 STRLEN salt_len=0;
16  
17 8 id = cryptx_internal_find_hash(hash_name);
18 8 ▶ if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name); 0 8 8
19 8 derived_len = hash_descriptor[id].hashsize;
20 8 ▶ if (output_len > derived_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (output_len > derived_len) { 1 7 8
21 1 croak("FATAL: pbkdf1 output_len cannot exceed hash size (%lu)", derived_len);
22 }
23 7 ▶ if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF;
Branch: 10 branches
BranchTrueFalseTotal
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 5 2 7
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 5 5
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 4 1 5
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 4 4
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
24  
25 4 ▶ password_ptr = (unsigned char *)SvPVbyte(password, password_len);
Branch: 1 branch
BranchTrueFalseTotal
password_ptr = (unsigned char *)SvPVbyte(password, password_len); 4 0 4
26 4 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
27 4 ▶ if (salt_len < 8) croak("FATAL: salt_len has to be 8");
Branch: 1 branch
BranchTrueFalseTotal
if (salt_len < 8) croak("FATAL: salt_len has to be 8"); 0 4 4
28  
29 4 ▶ if (output_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (output_len == 0) { 0 4 4
30 0 RETVAL = newSVpvn("", 0);
31 }
32 else {
33 4 RETVAL = NEWSV(0, derived_len); /* avoid zero! */
34 4 SvPOK_only(RETVAL);
35 4 SvCUR_set(RETVAL, derived_len);
36 4 output = (unsigned char *)SvPVX(RETVAL);
37  
38 4 rv = pkcs_5_alg1(password_ptr, (unsigned long)password_len, salt_ptr, iteration_count, id, output, &derived_len);
39 4 ▶ if (rv != CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { 0 4 4
40 0 SvREFCNT_dec(RETVAL);
41 0 croak("FATAL: pkcs_5_alg1 process failed: %s", error_to_string(rv));
42 }
43 4 SvCUR_set(RETVAL, output_len);
44 4 *SvEND(RETVAL) = '\0';
45 }
46 }
47 OUTPUT:
48 RETVAL
49  
50 SV *
51 pbkdf1_openssl(SV * password, SV * salt, int iteration_count = 5000, const char * hash_name = "SHA256", unsigned long output_len = 32)
52 CODE:
53 {
54 int rv, id;
55 unsigned char *output;
56 6 unsigned char *password_ptr=NULL;
57 6 STRLEN password_len=0;
58 6 unsigned char *salt_ptr=NULL;
59 6 STRLEN salt_len=0;
60  
61 6 id = cryptx_internal_find_hash(hash_name);
62 6 ▶ if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name); 0 6 6
63 6 ▶ if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF;
Branch: 10 branches
BranchTrueFalseTotal
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 4 2 6
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 4 4
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 3 1 4
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 3 3
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
64  
65 3 ▶ password_ptr = (unsigned char *)SvPVbyte(password, password_len);
Branch: 1 branch
BranchTrueFalseTotal
password_ptr = (unsigned char *)SvPVbyte(password, password_len); 3 0 3
66 3 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
67 3 ▶ if (salt_len < 8) croak("FATAL: salt_len has to be 8");
Branch: 1 branch
BranchTrueFalseTotal
if (salt_len < 8) croak("FATAL: salt_len has to be 8"); 0 3 3
68  
69 3 ▶ if (output_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (output_len == 0) { 0 3 3
70 0 RETVAL = newSVpvn("", 0);
71 }
72 else {
73 3 RETVAL = NEWSV(0, output_len); /* avoid zero! */
74 3 SvPOK_only(RETVAL);
75 3 SvCUR_set(RETVAL, output_len);
76 3 output = (unsigned char *)SvPVX(RETVAL);
77  
78 3 rv = pkcs_5_alg1_openssl(password_ptr, (unsigned long)password_len, salt_ptr, iteration_count, id, output, &output_len);
79 3 ▶ if (rv != CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { 0 3 3
80 0 SvREFCNT_dec(RETVAL);
81 0 croak("FATAL: pkcs_5_alg1_openssl process failed: %s", error_to_string(rv));
82 }
83 3 SvCUR_set(RETVAL, output_len);
84 3 *SvEND(RETVAL) = '\0';
85 }
86 }
87 OUTPUT:
88 RETVAL
89  
90 SV *
91 pbkdf2(SV * password, SV * salt, int iteration_count = 5000, const char * hash_name = "SHA256", unsigned long output_len = 32)
92 CODE:
93 {
94 int rv, id;
95 unsigned char *output;
96 20 unsigned char *password_ptr=NULL;
97 20 STRLEN password_len=0;
98 20 unsigned char *salt_ptr=NULL;
99 20 STRLEN salt_len=0;
100  
101 20 id = cryptx_internal_find_hash(hash_name);
102 20 ▶ if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name); 1 19 20
103 19 ▶ if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF;
Branch: 10 branches
BranchTrueFalseTotal
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 17 2 19
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 17 17
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 16 1 17
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 16 16
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
104  
105 16 ▶ password_ptr = (unsigned char *)SvPVbyte(password, password_len);
Branch: 1 branch
BranchTrueFalseTotal
password_ptr = (unsigned char *)SvPVbyte(password, password_len); 16 0 16
106 16 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
107  
108 16 ▶ if (output_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (output_len == 0) { 0 16 16
109 0 RETVAL = newSVpvn("", 0);
110 }
111 else {
112 16 RETVAL = NEWSV(0, output_len); /* avoid zero! */
113 16 SvPOK_only(RETVAL);
114 16 SvCUR_set(RETVAL, output_len);
115 16 output = (unsigned char *)SvPVX(RETVAL);
116  
117 16 rv = pkcs_5_alg2(password_ptr, (unsigned long)password_len, salt_ptr, (unsigned long)salt_len, iteration_count, id, output, &output_len);
118 16 ▶ if (rv != CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { 0 16 16
119 0 SvREFCNT_dec(RETVAL);
120 0 croak("FATAL: pkcs_5_alg2 process failed: %s", error_to_string(rv));
121 }
122 16 SvCUR_set(RETVAL, output_len);
123 16 *SvEND(RETVAL) = '\0';
124 }
125 }
126 OUTPUT:
127 RETVAL
128  
129 SV *
130 hkdf_extract(SV * in, SV * salt = &PL_sv_undef, const char * hash_name = "SHA256")
131 CODE:
132 {
133 int rv, id;
134 unsigned long output_len;
135 unsigned char *output;
136 12 unsigned char *in_ptr = NULL, *salt_ptr = NULL;
137 12 STRLEN in_len = 0, salt_len = 0;
138  
139 12 id = cryptx_internal_find_hash(hash_name);
140 12 ▶ if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name); 0 12 12
141  
142 12 ▶ if (!SvPOK_spec(in)) XSRETURN_UNDEF;
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 11 1 12
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 2 9 11
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 2 0 2
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 1 1 2
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 0 1 1
143 10 in_ptr = (unsigned char *)SvPVbyte(in, in_len);
144 10 ▶ if (SvOK(salt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(salt)) { 9 1 10
145 9 ▶ if (!SvPOK_spec(salt)) XSRETURN_UNDEF;
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(salt)) XSRETURN_UNDEF; 9 0 9
if (!SvPOK_spec(salt)) XSRETURN_UNDEF; 0 9 9
if (!SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
146 9 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
147 }
148  
149 10 output_len = hash_descriptor[id].hashsize;
150 10 RETVAL = NEWSV(0, output_len); /* avoid zero! */
151 10 SvPOK_only(RETVAL);
152 10 SvCUR_set(RETVAL, output_len);
153 10 output = (unsigned char *)SvPVX(RETVAL);
154  
155 10 rv = hkdf_extract(id, salt_ptr, (unsigned long)salt_len, in_ptr, (unsigned long)in_len, output, &output_len);
156 10 ▶ if (rv != CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { 0 10 10
157 0 SvREFCNT_dec(RETVAL);
158 0 croak("FATAL: hkdf_extract process failed: %s", error_to_string(rv));
159 }
160 10 SvCUR_set(RETVAL, output_len);
161 10 *SvEND(RETVAL) = '\0';
162 }
163 OUTPUT:
164 RETVAL
165  
166 SV *
167 hkdf_expand(SV * in, const char * hash_name = "SHA256", unsigned long output_len = 32, SV * info = &PL_sv_undef)
168 CODE:
169 {
170 int rv, id;
171 unsigned char *output;
172 13 unsigned char *in_ptr = NULL, *info_ptr = NULL;
173 13 STRLEN in_len = 0, info_len = 0;
174  
175 13 id = cryptx_internal_find_hash(hash_name);
176 13 ▶ if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name); 0 13 13
177  
178 13 ▶ if (!SvPOK_spec(in)) XSRETURN_UNDEF;
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 11 2 13
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 2 9 11
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 2 0 2
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 1 1 2
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 0 1 1
179 10 in_ptr = (unsigned char *)SvPVbyte(in, in_len);
180 10 ▶ if (SvOK(info)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(info)) { 10 0 10
181 10 ▶ if (!SvPOK_spec(info)) XSRETURN_UNDEF;
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(info)) XSRETURN_UNDEF; 10 0 10
if (!SvPOK_spec(info)) XSRETURN_UNDEF; 0 10 10
if (!SvPOK_spec(info)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(info)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(info)) XSRETURN_UNDEF; 0 0 0
182 10 info_ptr = (unsigned char *)SvPVbyte(info, info_len);
183 }
184  
185 10 ▶ if (output_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (output_len == 0) { 0 10 10
186 0 RETVAL = newSVpvn("", 0);
187 }
188 else {
189 10 RETVAL = NEWSV(0, output_len); /* avoid zero! */
190 10 SvPOK_only(RETVAL);
191 10 SvCUR_set(RETVAL, output_len);
192 10 output = (unsigned char *)SvPVX(RETVAL);
193  
194 10 rv = hkdf_expand(id, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len);
195 10 ▶ if (rv != CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { 0 10 10
196 0 SvREFCNT_dec(RETVAL);
197 0 croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
198 }
199 10 SvCUR_set(RETVAL, output_len);
200 10 *SvEND(RETVAL) = '\0';
201 }
202 }
203 OUTPUT:
204 RETVAL
205  
206 SV *
207 hkdf(SV * in, SV * salt, const char * hash_name = "SHA256", unsigned long output_len = 32, SV * info = &PL_sv_undef)
208 CODE:
209 {
210 int rv, id;
211 unsigned char *output;
212 23 unsigned char *in_ptr = NULL, *info_ptr = NULL, *salt_ptr = NULL;
213 23 STRLEN in_len = 0, info_len = 0, salt_len = 0;
214  
215 23 id = cryptx_internal_find_hash(hash_name);
216 23 ▶ if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name); 0 23 23
217  
218 23 ▶ if (!SvPOK_spec(in)) XSRETURN_UNDEF;
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 21 2 23
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 2 19 21
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 2 0 2
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 1 1 2
if (!SvPOK_spec(in)) XSRETURN_UNDEF; 0 1 1
219 20 in_ptr = (unsigned char *)SvPVbyte(in, in_len);
220 20 ▶ if (SvOK(info)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(info)) { 20 0 20
221 20 ▶ if (!SvPOK_spec(info)) XSRETURN_UNDEF;
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(info)) XSRETURN_UNDEF; 20 0 20
if (!SvPOK_spec(info)) XSRETURN_UNDEF; 0 20 20
if (!SvPOK_spec(info)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(info)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(info)) XSRETURN_UNDEF; 0 0 0
222 20 info_ptr = (unsigned char *)SvPVbyte(info, info_len);
223 }
224 20 ▶ if (SvOK(salt)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(salt)) { 19 1 20
225 19 ▶ if (!SvPOK_spec(salt)) XSRETURN_UNDEF;
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(salt)) XSRETURN_UNDEF; 19 0 19
if (!SvPOK_spec(salt)) XSRETURN_UNDEF; 0 19 19
if (!SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
226 19 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
227 }
228  
229 20 ▶ if (output_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (output_len == 0) { 0 20 20
230 0 RETVAL = newSVpvn("", 0);
231 }
232 else {
233 20 RETVAL = NEWSV(0, output_len); /* avoid zero! */
234 20 SvPOK_only(RETVAL);
235 20 SvCUR_set(RETVAL, output_len);
236 20 output = (unsigned char *)SvPVX(RETVAL);
237  
238 20 rv = hkdf(id, salt_ptr, (unsigned long)salt_len, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len);
239 20 ▶ if (rv != CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { 0 20 20
240 0 SvREFCNT_dec(RETVAL);
241 0 croak("FATAL: hkdf process failed: %s", error_to_string(rv));
242 }
243 20 SvCUR_set(RETVAL, output_len);
244 20 *SvEND(RETVAL) = '\0';
245 }
246 }
247 OUTPUT:
248 RETVAL
249  
250 SV *
251 bcrypt_pbkdf(SV * password, SV * salt, unsigned int rounds = 16, const char * hash_name = "SHA512", unsigned long output_len = 32)
252 CODE:
253 {
254 int rv, id;
255 unsigned char *output;
256 6 unsigned char *password_ptr=NULL;
257 6 STRLEN password_len=0;
258 6 unsigned char *salt_ptr=NULL;
259 6 STRLEN salt_len=0;
260  
261 6 id = cryptx_internal_find_hash(hash_name);
262 6 ▶ if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
Branch: 1 branch
BranchTrueFalseTotal
if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name); 0 6 6
263 6 ▶ if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF;
Branch: 10 branches
BranchTrueFalseTotal
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 4 2 6
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 4 4
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 3 1 4
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 3 3
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
264  
265 3 ▶ password_ptr = (unsigned char *)SvPVbyte(password, password_len);
Branch: 1 branch
BranchTrueFalseTotal
password_ptr = (unsigned char *)SvPVbyte(password, password_len); 3 0 3
266 3 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
267  
268 3 ▶ if (output_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (output_len == 0) { 0 3 3
269 0 RETVAL = newSVpvn("", 0);
270 }
271 else {
272 3 RETVAL = NEWSV(0, output_len); /* avoid zero! */
273 3 SvPOK_only(RETVAL);
274 3 SvCUR_set(RETVAL, output_len);
275 3 output = (unsigned char *)SvPVX(RETVAL);
276  
277 3 rv = bcrypt_pbkdf_openbsd(password_ptr, (unsigned long)password_len, salt_ptr, (unsigned long)salt_len, rounds, id, output, &output_len);
278 3 ▶ if (rv != CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { 0 3 3
279 0 SvREFCNT_dec(RETVAL);
280 0 croak("FATAL: bcrypt_pbkdf process failed: %s", error_to_string(rv));
281 }
282 3 SvCUR_set(RETVAL, output_len);
283 3 *SvEND(RETVAL) = '\0';
284 }
285 }
286 OUTPUT:
287 RETVAL
288  
289 SV *
290 scrypt_pbkdf(SV * password, SV * salt, unsigned long N = 1024, unsigned long r = 8, unsigned long p = 1, unsigned long output_len = 32)
291 CODE:
292 {
293 int rv;
294 unsigned char *output;
295 7 unsigned char *password_ptr=NULL;
296 7 STRLEN password_len=0;
297 7 unsigned char *salt_ptr=NULL;
298 7 STRLEN salt_len=0;
299  
300 7 ▶ if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF;
Branch: 10 branches
BranchTrueFalseTotal
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 5 2 7
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 5 5
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 4 1 5
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 4 4
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
301 4 ▶ password_ptr = (unsigned char *)SvPVbyte(password, password_len);
Branch: 1 branch
BranchTrueFalseTotal
password_ptr = (unsigned char *)SvPVbyte(password, password_len); 4 0 4
302 4 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
303  
304 4 ▶ if (output_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (output_len == 0) { 0 4 4
305 0 RETVAL = newSVpvn("", 0);
306 }
307 else {
308 4 RETVAL = NEWSV(0, output_len); /* avoid zero! */
309 4 SvPOK_only(RETVAL);
310 4 SvCUR_set(RETVAL, output_len);
311 4 *SvEND(RETVAL) = '\0';
312 4 output = (unsigned char *)SvPVX(RETVAL);
313  
314 4 rv = scrypt_pbkdf(password_ptr, (unsigned long)password_len, salt_ptr, (unsigned long)salt_len, N, r, p, output, output_len);
315 4 ▶ if (rv != CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { 0 4 4
316 0 SvREFCNT_dec(RETVAL);
317 0 croak("FATAL: scrypt_pbkdf process failed: %s", error_to_string(rv));
318 }
319 }
320 }
321 OUTPUT:
322 RETVAL
323  
324 SV *
325 argon2_pbkdf(const char * type, SV * password, SV * salt, unsigned int t_cost = 3, unsigned int m_factor = 65536, unsigned int parallelism = 1, unsigned long output_len = 32, SV * secret = &PL_sv_undef, SV * ad = &PL_sv_undef)
326 CODE:
327 {
328 int rv;
329 argon2_type argon2_t;
330 unsigned char *output;
331 16 unsigned char *password_ptr=NULL, *salt_ptr=NULL, *secret_ptr=NULL, *ad_ptr=NULL;
332 16 STRLEN password_len=0, salt_len=0, secret_len=0, ad_len=0;
333  
334 16 ▶ if (strcmp(type, "argon2d") == 0) argon2_t = ARGON2_D;
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(type, "argon2d") == 0) argon2_t = ARGON2_D; 1 15 16
335 15 ▶ else if (strcmp(type, "argon2i") == 0) argon2_t = ARGON2_I;
Branch: 1 branch
BranchTrueFalseTotal
else if (strcmp(type, "argon2i") == 0) argon2_t = ARGON2_I; 1 14 15
336 14 ▶ else if (strcmp(type, "argon2id") == 0) argon2_t = ARGON2_ID;
Branch: 1 branch
BranchTrueFalseTotal
else if (strcmp(type, "argon2id") == 0) argon2_t = ARGON2_ID; 13 1 14
337 1 else croak("FATAL: unknown argon2 type '%s'", type);
338 17 ▶ if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF;
Branch: 10 branches
BranchTrueFalseTotal
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 13 2 15
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 13 13
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 12 1 13
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 12 12
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
if (!SvPOK_spec(password) || !SvPOK_spec(salt)) XSRETURN_UNDEF; 0 0 0
339  
340 12 ▶ password_ptr = (unsigned char *)SvPVbyte(password, password_len);
Branch: 1 branch
BranchTrueFalseTotal
password_ptr = (unsigned char *)SvPVbyte(password, password_len); 12 0 12
341 12 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
342 12 ▶ if (SvOK(secret)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(secret)) { 9 3 12
343 9 ▶ if (!SvPOK_spec(secret)) XSRETURN_UNDEF;
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(secret)) XSRETURN_UNDEF; 9 0 9
if (!SvPOK_spec(secret)) XSRETURN_UNDEF; 2 7 9
if (!SvPOK_spec(secret)) XSRETURN_UNDEF; 2 0 2
if (!SvPOK_spec(secret)) XSRETURN_UNDEF; 1 1 2
if (!SvPOK_spec(secret)) XSRETURN_UNDEF; 0 1 1
344 8 secret_ptr = (unsigned char *)SvPVbyte(secret, secret_len);
345 }
346 11 ▶ if (SvOK(ad)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(ad)) { 8 3 11
347 8 ▶ if (!SvPOK_spec(ad)) XSRETURN_UNDEF;
Branch: 5 branches
BranchTrueFalseTotal
if (!SvPOK_spec(ad)) XSRETURN_UNDEF; 8 0 8
if (!SvPOK_spec(ad)) XSRETURN_UNDEF; 2 6 8
if (!SvPOK_spec(ad)) XSRETURN_UNDEF; 2 0 2
if (!SvPOK_spec(ad)) XSRETURN_UNDEF; 1 1 2
if (!SvPOK_spec(ad)) XSRETURN_UNDEF; 0 1 1
348 7 ad_ptr = (unsigned char *)SvPVbyte(ad, ad_len);
349 }
350  
351 10 ▶ if (output_len == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (output_len == 0) { 0 10 10
352 0 RETVAL = newSVpvn("", 0);
353 }
354 else {
355 10 RETVAL = NEWSV(0, output_len); /* avoid zero! */
356 10 SvPOK_only(RETVAL);
357 10 SvCUR_set(RETVAL, output_len);
358 10 *SvEND(RETVAL) = '\0';
359 10 output = (unsigned char *)SvPVX(RETVAL);
360  
361 10 rv = argon2_hash(password_ptr, (unsigned long)password_len,
362 salt_ptr, (unsigned long)salt_len,
363 secret_ptr, (unsigned long)secret_len,
364 ad_ptr, (unsigned long)ad_len,
365 t_cost, m_factor, parallelism, argon2_t,
366 output, output_len);
367 10 ▶ if (rv != CRYPT_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (rv != CRYPT_OK) { 2 8 10
368 2 SvREFCNT_dec(RETVAL);
369 2 croak("FATAL: argon2_pbkdf failed: %s", error_to_string(rv));
370 }
371 }
372 }
373 OUTPUT:
374 RETVAL
« inc/CryptX_Stream_XSalsa20.xs.inc ↑ Summary inc/CryptX_PK_DH.xs.inc »