File Coverage

inc/CryptX_KeyDerivation.xs.inc
Criterion Covered Total %
statement 72 84 85.7
branch 35 66 53.0
condition n/a
subroutine n/a
pod n/a
total 107 150 71.3


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 50         password_ptr = (unsigned char *)SvPVbyte(password, password_len);
24 1 50         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 50         password_ptr = (unsigned char *)SvPVbyte(password, password_len);
62 5 50         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);
    50          
94 7 100         if (SvPOK(salt)) salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
    50          
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);
    50          
122 7 50         if (SvPOK(info)) info_ptr = (unsigned char *)SvPVbyte(info, info_len);
    50          
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);
    50          
157 7 50         if (SvPOK(info)) info_ptr = (unsigned char *)SvPVbyte(info, info_len);
    50          
158 7 100         if (SvPOK(salt)) salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
    50          
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