File Coverage

inc/CryptX_KeyDerivation.xs.inc
Criterion Covered Total %
statement 117 139 84.1
branch 38 68 55.8
condition n/a
subroutine n/a
pod n/a
total 155 207 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             pbkdf2(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 5           unsigned char *password_ptr=NULL;
50 5           STRLEN password_len=0;
51 5           unsigned char *salt_ptr=NULL;
52 5           STRLEN salt_len=0;
53              
54 5 50         if (output_len == 0) {
55 0           RETVAL = newSVpvn("", 0);
56             }
57             else {
58 5           id = cryptx_internal_find_hash(hash_name);
59 5 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
60              
61 5           password_ptr = (unsigned char *)SvPVbyte(password, password_len);
62 5           salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
63              
64 5           RETVAL = NEWSV(0, output_len); /* avoid zero! */
65 5           SvPOK_only(RETVAL);
66 5           SvCUR_set(RETVAL, output_len);
67 5           output = (unsigned char *)SvPVX(RETVAL);
68              
69 5           rv = pkcs_5_alg2(password_ptr, (unsigned long)password_len, salt_ptr, (unsigned long)salt_len, iteration_count, id, output, &output_len);
70 5 50         if (rv != CRYPT_OK) {
71 0           SvREFCNT_dec(RETVAL);
72 0           croak("FATAL: pkcs_5_alg2 process failed: %s", error_to_string(rv));
73             }
74 5           SvCUR_set(RETVAL, output_len);
75             }
76             }
77             OUTPUT:
78             RETVAL
79              
80             SV *
81             hkdf_extract(SV * in, SV * salt = &PL_sv_undef, const char * hash_name = "SHA256")
82             CODE:
83             {
84             int rv, id;
85             unsigned char output[MAXBLOCKSIZE];
86             unsigned long output_len;
87 7           unsigned char *in_ptr = NULL, *salt_ptr = NULL;
88 7           STRLEN in_len = 0, salt_len = 0;
89              
90 7           id = cryptx_internal_find_hash(hash_name);
91 7 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
92              
93 7 50         if (SvPOK(in)) in_ptr = (unsigned char *)SvPVbyte(in, in_len);
94 7 100         if (SvPOK(salt)) salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
95              
96 7           output_len = sizeof(output);
97 7           rv = hkdf_extract(id, salt_ptr, (unsigned long)salt_len, in_ptr, (unsigned long)in_len, output, &output_len);
98 7 50         if (rv != CRYPT_OK) croak("FATAL: hkdf_extract process failed: %s", error_to_string(rv));
99              
100 7           RETVAL = newSVpvn((char *)output, output_len);
101             }
102             OUTPUT:
103             RETVAL
104              
105             SV *
106             hkdf_expand(SV * in, const char * hash_name = "SHA256", unsigned long output_len = 32, SV * info = &PL_sv_undef)
107             CODE:
108             {
109             int rv, id;
110             unsigned char *output;
111 7           unsigned char *in_ptr = NULL, *info_ptr = NULL;
112 7           STRLEN in_len = 0, info_len = 0;
113              
114 7 50         if (output_len == 0) {
115 0           RETVAL = newSVpvn("", 0);
116             }
117             else {
118 7           id = cryptx_internal_find_hash(hash_name);
119 7 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
120              
121 7 50         if (SvPOK(in)) in_ptr = (unsigned char *)SvPVbyte(in, in_len);
122 7 50         if (SvPOK(info)) info_ptr = (unsigned char *)SvPVbyte(info, info_len);
123              
124 7           RETVAL = NEWSV(0, output_len); /* avoid zero! */
125 7           SvPOK_only(RETVAL);
126 7           SvCUR_set(RETVAL, output_len);
127 7           output = (unsigned char *)SvPVX(RETVAL);
128              
129 7           rv = hkdf_expand(id, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len);
130 7 50         if (rv != CRYPT_OK) {
131 0           SvREFCNT_dec(RETVAL);
132 0           croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
133             }
134 7           SvCUR_set(RETVAL, output_len);
135             }
136             }
137             OUTPUT:
138             RETVAL
139              
140             SV *
141             hkdf(SV * in, SV * salt, const char * hash_name = "SHA256", unsigned long output_len = 32, SV * info = &PL_sv_undef)
142             CODE:
143             {
144             int rv, id;
145             unsigned char *output;
146 7           unsigned char *in_ptr = NULL, *info_ptr = NULL, *salt_ptr = NULL;
147 7           STRLEN in_len = 0, info_len = 0, salt_len = 0;
148              
149 7 50         if (output_len == 0) {
150 0           RETVAL = newSVpvn("", 0);
151             }
152             else {
153 7           id = cryptx_internal_find_hash(hash_name);
154 7 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
155              
156 7 50         if (SvPOK(in)) in_ptr = (unsigned char *)SvPVbyte(in, in_len);
157 7 50         if (SvPOK(info)) info_ptr = (unsigned char *)SvPVbyte(info, info_len);
158 7 100         if (SvPOK(salt)) salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
159              
160 7           RETVAL = NEWSV(0, output_len); /* avoid zero! */
161 7           SvPOK_only(RETVAL);
162 7           SvCUR_set(RETVAL, output_len);
163 7           output = (unsigned char *)SvPVX(RETVAL);
164              
165 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);
166 7 50         if (rv != CRYPT_OK) {
167 0           SvREFCNT_dec(RETVAL);
168 0           croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
169             }
170 7           SvCUR_set(RETVAL, output_len);
171             }
172             }
173             OUTPUT:
174             RETVAL
175              
176             SV *
177             bcrypt_pbkdf(SV * password, SV * salt, unsigned int rounds = 16, const char * hash_name = "SHA512", unsigned long output_len = 32)
178             CODE:
179             {
180             int rv, id;
181             unsigned char *output;
182 3           unsigned char *password_ptr=NULL;
183 3           STRLEN password_len=0;
184 3           unsigned char *salt_ptr=NULL;
185 3           STRLEN salt_len=0;
186              
187 3 50         if (output_len == 0) {
188 0           RETVAL = newSVpvn("", 0);
189             }
190             else {
191 3           id = cryptx_internal_find_hash(hash_name);
192 3 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
193              
194 3           password_ptr = (unsigned char *)SvPVbyte(password, password_len);
195 3           salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
196              
197 3           RETVAL = NEWSV(0, output_len); /* avoid zero! */
198 3           SvPOK_only(RETVAL);
199 3           SvCUR_set(RETVAL, output_len);
200 3           output = (unsigned char *)SvPVX(RETVAL);
201              
202 3           rv = bcrypt_pbkdf_openbsd(password_ptr, (unsigned long)password_len, salt_ptr, (unsigned long)salt_len, rounds, id, output, &output_len);
203 3 50         if (rv != CRYPT_OK) {
204 0           SvREFCNT_dec(RETVAL);
205 0           croak("FATAL: bcrypt_pbkdf process failed: %s", error_to_string(rv));
206             }
207 3           SvCUR_set(RETVAL, output_len);
208             }
209             }
210             OUTPUT:
211             RETVAL
212              
213             SV *
214             scrypt_pbkdf(SV * password, SV * salt, unsigned long N = 1024, unsigned long r = 8, unsigned long p = 1, unsigned long output_len = 32)
215             CODE:
216             {
217             int rv;
218             unsigned char *output;
219 3           unsigned char *password_ptr=NULL;
220 3           STRLEN password_len=0;
221 3           unsigned char *salt_ptr=NULL;
222 3           STRLEN salt_len=0;
223              
224 3 50         if (output_len == 0) {
225 0           RETVAL = newSVpvn("", 0);
226             }
227             else {
228 3           password_ptr = (unsigned char *)SvPVbyte(password, password_len);
229 3           salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
230              
231 3           RETVAL = NEWSV(0, output_len); /* avoid zero! */
232 3           SvPOK_only(RETVAL);
233 3           SvCUR_set(RETVAL, output_len);
234 3           output = (unsigned char *)SvPVX(RETVAL);
235              
236 3           rv = scrypt_pbkdf(password_ptr, (unsigned long)password_len, salt_ptr, (unsigned long)salt_len, N, r, p, output, output_len);
237 3 50         if (rv != CRYPT_OK) {
238 0           SvREFCNT_dec(RETVAL);
239 0           croak("FATAL: scrypt_pbkdf process failed: %s", error_to_string(rv));
240             }
241             }
242             }
243             OUTPUT:
244             RETVAL
245              
246             SV *
247             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)
248             CODE:
249             {
250             int rv;
251             argon2_type argon2_t;
252             unsigned char *output;
253 3           unsigned char *password_ptr=NULL, *salt_ptr=NULL, *secret_ptr=NULL, *ad_ptr=NULL;
254 3           STRLEN password_len=0, salt_len=0, secret_len=0, ad_len=0;
255              
256 3 50         if (output_len == 0) {
257 0           RETVAL = newSVpvn("", 0);
258             }
259             else {
260 3 100         if (strcmp(type, "argon2d") == 0) argon2_t = ARGON2_D;
261 2 100         else if (strcmp(type, "argon2i") == 0) argon2_t = ARGON2_I;
262 1 50         else if (strcmp(type, "argon2id") == 0) argon2_t = ARGON2_ID;
263 0           else croak("FATAL: unknown argon2 type '%s'", type);
264              
265 3           password_ptr = (unsigned char *)SvPVbyte(password, password_len);
266 3           salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
267 3 50         if (SvPOK(secret)) secret_ptr = (unsigned char *)SvPVbyte(secret, secret_len);
268 3 50         if (SvPOK(ad)) ad_ptr = (unsigned char *)SvPVbyte(ad, ad_len);
269              
270 3           RETVAL = NEWSV(0, output_len); /* avoid zero! */
271 3           SvPOK_only(RETVAL);
272 3           SvCUR_set(RETVAL, output_len);
273 3           output = (unsigned char *)SvPVX(RETVAL);
274              
275 3           rv = argon2_hash(password_ptr, (unsigned long)password_len,
276             salt_ptr, (unsigned long)salt_len,
277             secret_ptr, (unsigned long)secret_len,
278             ad_ptr, (unsigned long)ad_len,
279             t_cost, m_factor, parallelism, argon2_t,
280             output, output_len);
281 3 50         if (rv != CRYPT_OK) {
282 0           SvREFCNT_dec(RETVAL);
283 0           croak("FATAL: argon2_pbkdf failed: %s", error_to_string(rv));
284             }
285             }
286             }
287             OUTPUT:
288             RETVAL