File Coverage

inc/CryptX_KeyDerivation.xs.inc
Criterion Covered Total %
statement 134 159 84.2
branch 42 76 55.2
condition n/a
subroutine n/a
pod n/a
total 176 235 74.8


line stmt bran cond sub pod time code
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 char *output;
11 1           unsigned char *password_ptr=NULL;
12 1           STRLEN password_len=0;
13 1           unsigned char *salt_ptr=NULL;
14 1           STRLEN salt_len=0;
15              
16 1 50         if (output_len == 0) {
17 0           RETVAL = newSVpvn("", 0);
18             }
19             else {
20 1           id = cryptx_internal_find_hash(hash_name);
21 1 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
22              
23 1           password_ptr = (unsigned char *)SvPVbyte(password, password_len);
24 1           salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
25 1 50         if (salt_len < 8) croak("FATAL: salt_len has to be 8");
26              
27 1           RETVAL = NEWSV(0, output_len); /* avoid zero! */
28 1           SvPOK_only(RETVAL);
29 1           SvCUR_set(RETVAL, output_len);
30 1           output = (unsigned char *)SvPVX(RETVAL);
31              
32 1           rv = pkcs_5_alg1(password_ptr, (unsigned long)password_len, salt_ptr, iteration_count, id, output, &output_len);
33 1 50         if (rv != CRYPT_OK) {
34 0           SvREFCNT_dec(RETVAL);
35 0           croak("FATAL: pkcs_5_alg1 process failed: %s", error_to_string(rv));
36             }
37 1           SvCUR_set(RETVAL, output_len);
38             }
39             }
40             OUTPUT:
41             RETVAL
42              
43             SV *
44             pbkdf1_openssl(SV * password, SV * salt, int iteration_count = 5000, const char * hash_name = "SHA256", unsigned long output_len = 32)
45             CODE:
46             {
47             int rv, id;
48             unsigned char *output;
49 2           unsigned char *password_ptr=NULL;
50 2           STRLEN password_len=0;
51 2           unsigned char *salt_ptr=NULL;
52 2           STRLEN salt_len=0;
53              
54 2 50         if (output_len == 0) {
55 0           RETVAL = newSVpvn("", 0);
56             }
57             else {
58 2           id = cryptx_internal_find_hash(hash_name);
59 2 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
60              
61 2           password_ptr = (unsigned char *)SvPVbyte(password, password_len);
62 2           salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
63 2 50         if (salt_len < 8) croak("FATAL: salt_len has to be 8");
64              
65 2           RETVAL = NEWSV(0, output_len); /* avoid zero! */
66 2           SvPOK_only(RETVAL);
67 2           SvCUR_set(RETVAL, output_len);
68 2           output = (unsigned char *)SvPVX(RETVAL);
69              
70 2           rv = pkcs_5_alg1_openssl(password_ptr, (unsigned long)password_len, salt_ptr, iteration_count, id, output, &output_len);
71 2 50         if (rv != CRYPT_OK) {
72 0           SvREFCNT_dec(RETVAL);
73 0           croak("FATAL: pkcs_5_alg1_openssl process failed: %s", error_to_string(rv));
74             }
75 2           SvCUR_set(RETVAL, output_len);
76             }
77             }
78             OUTPUT:
79             RETVAL
80              
81             SV *
82             pbkdf2(SV * password, SV * salt, int iteration_count = 5000, const char * hash_name = "SHA256", unsigned long output_len = 32)
83             CODE:
84             {
85             int rv, id;
86             unsigned char *output;
87 5           unsigned char *password_ptr=NULL;
88 5           STRLEN password_len=0;
89 5           unsigned char *salt_ptr=NULL;
90 5           STRLEN salt_len=0;
91              
92 5 50         if (output_len == 0) {
93 0           RETVAL = newSVpvn("", 0);
94             }
95             else {
96 5           id = cryptx_internal_find_hash(hash_name);
97 5 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
98              
99 5           password_ptr = (unsigned char *)SvPVbyte(password, password_len);
100 5           salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
101              
102 5           RETVAL = NEWSV(0, output_len); /* avoid zero! */
103 5           SvPOK_only(RETVAL);
104 5           SvCUR_set(RETVAL, output_len);
105 5           output = (unsigned char *)SvPVX(RETVAL);
106              
107 5           rv = pkcs_5_alg2(password_ptr, (unsigned long)password_len, salt_ptr, (unsigned long)salt_len, iteration_count, id, output, &output_len);
108 5 50         if (rv != CRYPT_OK) {
109 0           SvREFCNT_dec(RETVAL);
110 0           croak("FATAL: pkcs_5_alg2 process failed: %s", error_to_string(rv));
111             }
112 5           SvCUR_set(RETVAL, output_len);
113             }
114             }
115             OUTPUT:
116             RETVAL
117              
118             SV *
119             hkdf_extract(SV * in, SV * salt = &PL_sv_undef, const char * hash_name = "SHA256")
120             CODE:
121             {
122             int rv, id;
123             unsigned char output[MAXBLOCKSIZE];
124             unsigned long output_len;
125 7           unsigned char *in_ptr = NULL, *salt_ptr = NULL;
126 7           STRLEN in_len = 0, salt_len = 0;
127              
128 7           id = cryptx_internal_find_hash(hash_name);
129 7 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
130              
131 7 50         if (SvPOK(in)) in_ptr = (unsigned char *)SvPVbyte(in, in_len);
132 7 100         if (SvPOK(salt)) salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
133              
134 7           output_len = sizeof(output);
135 7           rv = hkdf_extract(id, salt_ptr, (unsigned long)salt_len, in_ptr, (unsigned long)in_len, output, &output_len);
136 7 50         if (rv != CRYPT_OK) croak("FATAL: hkdf_extract process failed: %s", error_to_string(rv));
137              
138 7           RETVAL = newSVpvn((char *)output, output_len);
139             }
140             OUTPUT:
141             RETVAL
142              
143             SV *
144             hkdf_expand(SV * in, const char * hash_name = "SHA256", unsigned long output_len = 32, SV * info = &PL_sv_undef)
145             CODE:
146             {
147             int rv, id;
148             unsigned char *output;
149 7           unsigned char *in_ptr = NULL, *info_ptr = NULL;
150 7           STRLEN in_len = 0, info_len = 0;
151              
152 7 50         if (output_len == 0) {
153 0           RETVAL = newSVpvn("", 0);
154             }
155             else {
156 7           id = cryptx_internal_find_hash(hash_name);
157 7 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
158              
159 7 50         if (SvPOK(in)) in_ptr = (unsigned char *)SvPVbyte(in, in_len);
160 7 50         if (SvPOK(info)) info_ptr = (unsigned char *)SvPVbyte(info, info_len);
161              
162 7           RETVAL = NEWSV(0, output_len); /* avoid zero! */
163 7           SvPOK_only(RETVAL);
164 7           SvCUR_set(RETVAL, output_len);
165 7           output = (unsigned char *)SvPVX(RETVAL);
166              
167 7           rv = hkdf_expand(id, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len);
168 7 50         if (rv != CRYPT_OK) {
169 0           SvREFCNT_dec(RETVAL);
170 0           croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
171             }
172 7           SvCUR_set(RETVAL, output_len);
173             }
174             }
175             OUTPUT:
176             RETVAL
177              
178             SV *
179             hkdf(SV * in, SV * salt, const char * hash_name = "SHA256", unsigned long output_len = 32, SV * info = &PL_sv_undef)
180             CODE:
181             {
182             int rv, id;
183             unsigned char *output;
184 7           unsigned char *in_ptr = NULL, *info_ptr = NULL, *salt_ptr = NULL;
185 7           STRLEN in_len = 0, info_len = 0, salt_len = 0;
186              
187 7 50         if (output_len == 0) {
188 0           RETVAL = newSVpvn("", 0);
189             }
190             else {
191 7           id = cryptx_internal_find_hash(hash_name);
192 7 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
193              
194 7 50         if (SvPOK(in)) in_ptr = (unsigned char *)SvPVbyte(in, in_len);
195 7 50         if (SvPOK(info)) info_ptr = (unsigned char *)SvPVbyte(info, info_len);
196 7 100         if (SvPOK(salt)) salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
197              
198 7           RETVAL = NEWSV(0, output_len); /* avoid zero! */
199 7           SvPOK_only(RETVAL);
200 7           SvCUR_set(RETVAL, output_len);
201 7           output = (unsigned char *)SvPVX(RETVAL);
202              
203 7           rv = hkdf(id, salt_ptr, (unsigned long)salt_len, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len);
204 7 50         if (rv != CRYPT_OK) {
205 0           SvREFCNT_dec(RETVAL);
206 0           croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
207             }
208 7           SvCUR_set(RETVAL, output_len);
209             }
210             }
211             OUTPUT:
212             RETVAL
213              
214             SV *
215             bcrypt_pbkdf(SV * password, SV * salt, unsigned int rounds = 16, const char * hash_name = "SHA512", unsigned long output_len = 32)
216             CODE:
217             {
218             int rv, id;
219             unsigned char *output;
220 3           unsigned char *password_ptr=NULL;
221 3           STRLEN password_len=0;
222 3           unsigned char *salt_ptr=NULL;
223 3           STRLEN salt_len=0;
224              
225 3 50         if (output_len == 0) {
226 0           RETVAL = newSVpvn("", 0);
227             }
228             else {
229 3           id = cryptx_internal_find_hash(hash_name);
230 3 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
231              
232 3           password_ptr = (unsigned char *)SvPVbyte(password, password_len);
233 3           salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
234              
235 3           RETVAL = NEWSV(0, output_len); /* avoid zero! */
236 3           SvPOK_only(RETVAL);
237 3           SvCUR_set(RETVAL, output_len);
238 3           output = (unsigned char *)SvPVX(RETVAL);
239              
240 3           rv = bcrypt_pbkdf_openbsd(password_ptr, (unsigned long)password_len, salt_ptr, (unsigned long)salt_len, rounds, id, output, &output_len);
241 3 50         if (rv != CRYPT_OK) {
242 0           SvREFCNT_dec(RETVAL);
243 0           croak("FATAL: bcrypt_pbkdf process failed: %s", error_to_string(rv));
244             }
245 3           SvCUR_set(RETVAL, output_len);
246             }
247             }
248             OUTPUT:
249             RETVAL
250              
251             SV *
252             scrypt_pbkdf(SV * password, SV * salt, unsigned long N = 1024, unsigned long r = 8, unsigned long p = 1, unsigned long output_len = 32)
253             CODE:
254             {
255             int rv;
256             unsigned char *output;
257 3           unsigned char *password_ptr=NULL;
258 3           STRLEN password_len=0;
259 3           unsigned char *salt_ptr=NULL;
260 3           STRLEN salt_len=0;
261              
262 3 50         if (output_len == 0) {
263 0           RETVAL = newSVpvn("", 0);
264             }
265             else {
266 3           password_ptr = (unsigned char *)SvPVbyte(password, password_len);
267 3           salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
268              
269 3           RETVAL = NEWSV(0, output_len); /* avoid zero! */
270 3           SvPOK_only(RETVAL);
271 3           SvCUR_set(RETVAL, output_len);
272 3           output = (unsigned char *)SvPVX(RETVAL);
273              
274 3           rv = scrypt_pbkdf(password_ptr, (unsigned long)password_len, salt_ptr, (unsigned long)salt_len, N, r, p, output, output_len);
275 3 50         if (rv != CRYPT_OK) {
276 0           SvREFCNT_dec(RETVAL);
277 0           croak("FATAL: scrypt_pbkdf process failed: %s", error_to_string(rv));
278             }
279             }
280             }
281             OUTPUT:
282             RETVAL
283              
284             SV *
285             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)
286             CODE:
287             {
288             int rv;
289             argon2_type argon2_t;
290             unsigned char *output;
291 3           unsigned char *password_ptr=NULL, *salt_ptr=NULL, *secret_ptr=NULL, *ad_ptr=NULL;
292 3           STRLEN password_len=0, salt_len=0, secret_len=0, ad_len=0;
293              
294 3 50         if (output_len == 0) {
295 0           RETVAL = newSVpvn("", 0);
296             }
297             else {
298 3 100         if (strcmp(type, "argon2d") == 0) argon2_t = ARGON2_D;
299 2 100         else if (strcmp(type, "argon2i") == 0) argon2_t = ARGON2_I;
300 1 50         else if (strcmp(type, "argon2id") == 0) argon2_t = ARGON2_ID;
301 0           else croak("FATAL: unknown argon2 type '%s'", type);
302              
303 3           password_ptr = (unsigned char *)SvPVbyte(password, password_len);
304 3           salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
305 3 50         if (SvPOK(secret)) secret_ptr = (unsigned char *)SvPVbyte(secret, secret_len);
306 3 50         if (SvPOK(ad)) ad_ptr = (unsigned char *)SvPVbyte(ad, ad_len);
307              
308 3           RETVAL = NEWSV(0, output_len); /* avoid zero! */
309 3           SvPOK_only(RETVAL);
310 3           SvCUR_set(RETVAL, output_len);
311 3           output = (unsigned char *)SvPVX(RETVAL);
312              
313 3           rv = argon2_hash(password_ptr, (unsigned long)password_len,
314             salt_ptr, (unsigned long)salt_len,
315             secret_ptr, (unsigned long)secret_len,
316             ad_ptr, (unsigned long)ad_len,
317             t_cost, m_factor, parallelism, argon2_t,
318             output, output_len);
319 3 50         if (rv != CRYPT_OK) {
320 0           SvREFCNT_dec(RETVAL);
321 0           croak("FATAL: argon2_pbkdf failed: %s", error_to_string(rv));
322             }
323             }
324             }
325             OUTPUT:
326             RETVAL