File Coverage

inc/CryptX_PK_DSA.xs.inc
Criterion Covered Total %
statement 156 173 90.1
branch 95 180 52.7
condition n/a
subroutine n/a
pod n/a
total 251 353 71.1


line stmt bran cond sub pod time code
1             MODULE = CryptX PACKAGE = Crypt::PK::DSA
2              
3             PROTOTYPES: DISABLE
4              
5             Crypt::PK::DSA
6             _new(Class)
7             CODE:
8             {
9             int rv;
10 72           Newz(0, RETVAL, 1, struct dsa_struct);
11 72 50         if (!RETVAL) croak("FATAL: Newz failed");
12 72           RETVAL->key.type = -1;
13 72           RETVAL->pindex = find_prng("chacha20");
14 72 50         if (RETVAL->pindex == -1) {
15 0           Safefree(RETVAL);
16 0           croak("FATAL: find_prng('chacha20') failed");
17             }
18 72           rv = rng_make_prng(320, RETVAL->pindex, &RETVAL->pstate, NULL); /* 320bits = 40bytes */
19 72 50         if (rv != CRYPT_OK) {
20 0           Safefree(RETVAL);
21 0           croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
22             }
23             }
24             OUTPUT:
25             RETVAL
26              
27             void
28             _generate_key_size(Crypt::PK::DSA self, int group_size=30, int modulus_size=256)
29             PPCODE:
30             {
31             int rv;
32             /* gen the key */
33 1           rv = dsa_make_key(&self->pstate, self->pindex, group_size, modulus_size, &self->key);
34 1 50         if (rv != CRYPT_OK) croak("FATAL: dsa_make_key failed: %s", error_to_string(rv));
35 1 50         XPUSHs(ST(0)); /* return self */
36             }
37              
38             void
39             _generate_key_dsaparam(Crypt::PK::DSA self, SV * dsaparam)
40             PPCODE:
41             {
42             int rv;
43 2           unsigned char *data=NULL;
44 2           STRLEN data_len=0;
45 2 50         data = (unsigned char *)SvPVbyte(dsaparam, data_len);
46             /* load d p q */
47 2           rv = dsa_set_pqg_dsaparam(data, (unsigned long)data_len, &self->key);
48 2 50         if (rv != CRYPT_OK) croak("FATAL: dsa_set_pqg_dsaparam failed: %s", error_to_string(rv));
49             /* gen the key */
50 2           rv = dsa_generate_key(&self->pstate, self->pindex, &self->key);
51 2 50         if (rv != CRYPT_OK) croak("FATAL: dsa_generate_key failed: %s", error_to_string(rv));
52 2 50         XPUSHs(ST(0)); /* return self */
53             }
54              
55             void
56             _generate_key_pqg_hex(Crypt::PK::DSA self, char *p, char *q, char *g)
57             PPCODE:
58             {
59             int rv;
60             unsigned char pbin[512], qbin[512], gbin[512];
61 1           unsigned long plen=sizeof(pbin), qlen=sizeof(qbin), glen=sizeof(gbin);
62 1 50         if (!p || !strlen(p) || !q || !strlen(q) || !g || !strlen(g)) {
    50          
    50          
    50          
    50          
    50          
63 0           croak("FATAL: generate_key_pqg_hex incomplete args");
64             }
65             /* set p q g */
66 1           rv = radix_to_bin(p, 16, pbin, &plen);
67 1 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(p) failed: %s", error_to_string(rv));
68 1           rv = radix_to_bin(q, 16, qbin, &qlen);
69 1 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(q) failed: %s", error_to_string(rv));
70 1           rv = radix_to_bin(g, 16, gbin, &glen);
71 1 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(g) failed: %s", error_to_string(rv));
72 1           rv = dsa_set_pqg(pbin, plen, qbin, qlen, gbin, glen, &self->key);
73 1 50         if (rv != CRYPT_OK) croak("FATAL: dsa_set_pqg failed: %s", error_to_string(rv));
74             /* gen the key */
75 1           rv = dsa_generate_key(&self->pstate, self->pindex, &self->key);
76 1 50         if (rv != CRYPT_OK) croak("FATAL: dsa_generate_key failed: %s", error_to_string(rv));
77 1 50         XPUSHs(ST(0)); /* return self */
78             }
79              
80             void
81             _import(Crypt::PK::DSA self, SV * key_data)
82             PPCODE:
83             {
84             int rv;
85 69           unsigned char *data=NULL;
86 69           STRLEN data_len=0;
87              
88 69 50         data = (unsigned char *)SvPVbyte(key_data, data_len);
89 69 100         if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; }
90 69           rv = dsa_import(data, (unsigned long)data_len, &self->key);
91 69 50         if (rv != CRYPT_OK) croak("FATAL: dsa_import failed: %s", error_to_string(rv));
92 69 50         XPUSHs(ST(0)); /* return self */
93             }
94              
95             void
96             _import_hex(Crypt::PK::DSA self, char *p, char *q, char *g, char *x, char *y)
97             PPCODE:
98             {
99             int rv;
100             unsigned char pbin[512], qbin[512], gbin[512], xbin[512], ybin[512];
101 4           unsigned long plen=sizeof(pbin), qlen=sizeof(qbin), glen=sizeof(gbin), xlen=sizeof(xbin), ylen=sizeof(ybin);
102              
103 4 50         if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; }
104              
105 4 50         if (p && strlen(p) > 0 && q && strlen(q) > 0 && g && strlen(g) > 0 && y && strlen(y) > 0) {
    50          
    50          
    50          
    50          
    50          
    50          
    50          
106 4           rv = radix_to_bin(p, 16, pbin, &plen);
107 4 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(p) failed: %s", error_to_string(rv));
108 4           rv = radix_to_bin(q, 16, qbin, &qlen);
109 4 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(q) failed: %s", error_to_string(rv));
110 4           rv = radix_to_bin(g, 16, gbin, &glen);
111 4 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(g) failed: %s", error_to_string(rv));
112 4           rv = dsa_set_pqg(pbin, plen, qbin, qlen, gbin, glen, &self->key);
113 4 50         if (rv != CRYPT_OK) croak("FATAL: dsa_set_pqg failed: %s", error_to_string(rv));
114              
115 4           rv = radix_to_bin(y, 16, ybin, &ylen);
116 4 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(y) failed: %s", error_to_string(rv));
117 4 100         if (x && strlen(x) > 0) {
    100          
118             /* private */
119 1           rv = radix_to_bin(x, 16, xbin, &xlen);
120 1 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(x) failed: %s", error_to_string(rv));
121 1           rv = dsa_set_key(xbin, xlen, PK_PRIVATE, &self->key);
122 1 50         if (rv != CRYPT_OK) croak("FATAL: dsa_set_key failed: %s", error_to_string(rv));
123             }
124             else {
125             /* public */
126 3           rv = dsa_set_key(ybin, ylen, PK_PUBLIC, &self->key);
127 3 50         if (rv != CRYPT_OK) croak("FATAL: dsa_set_key failed: %s", error_to_string(rv));
128             }
129             }
130              
131 4 50         XPUSHs(ST(0)); /* return self */
132             }
133              
134             int
135             is_private(Crypt::PK::DSA self)
136             CODE:
137 32 50         if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF;
    50          
138 32           RETVAL = (self->key.type == PK_PRIVATE) ? 1 : 0;
139             OUTPUT:
140             RETVAL
141              
142             int
143             size(Crypt::PK::DSA self)
144             CODE:
145 1 50         if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF;
    50          
146 1           RETVAL = mp_unsigned_bin_size(self->key.p);
147             OUTPUT:
148             RETVAL
149              
150             int
151             size_q(Crypt::PK::DSA self)
152             CODE:
153 0 0         if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF;
    0          
154 0           RETVAL = mp_unsigned_bin_size(self->key.q);
155             OUTPUT:
156             RETVAL
157              
158             SV*
159             key2hash(Crypt::PK::DSA self)
160             PREINIT:
161             HV *rv_hash;
162             long siz, qsize, psize;
163             char buf[20001];
164             SV **not_used;
165             CODE:
166 45 50         if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF;
    50          
167 45           qsize = mp_unsigned_bin_size(self->key.q);
168 45           psize = mp_unsigned_bin_size(self->key.p);
169 45           rv_hash = newHV();
170             /* g */
171 45 50         siz = (self->key.g) ? mp_unsigned_bin_size(self->key.g) : 0;
172 45 50         if (siz>10000) {
173 0           croak("FATAL: key2hash failed - 'g' too big number");
174             }
175 45 50         if (siz>0) {
176 45           cryptx_internal_mp2hex_with_leading_zero(self->key.g, buf, 20000, 0);
177 45           not_used = hv_store(rv_hash, "g", 1, newSVpv(buf, strlen(buf)), 0);
178             }
179             else{
180 0           not_used = hv_store(rv_hash, "g", 1, newSVpv("", 0), 0);
181             }
182             /* q */
183 45 50         siz = (self->key.q) ? mp_unsigned_bin_size(self->key.q) : 0;
184 45 50         if (siz>10000) {
185 0           croak("FATAL: key2hash failed - 'q' too big number");
186             }
187 45 50         if (siz>0) {
188 45           cryptx_internal_mp2hex_with_leading_zero(self->key.q, buf, 20000, 0);
189 45           not_used = hv_store(rv_hash, "q", 1, newSVpv(buf, strlen(buf)), 0);
190             }
191             else{
192 0           not_used = hv_store(rv_hash, "q", 1, newSVpv("", 0), 0);
193             }
194             /* p */
195 45 50         siz = (self->key.p) ? mp_unsigned_bin_size(self->key.p) : 0;
196 45 50         if (siz>10000) {
197 0           croak("FATAL: key2hash failed - 'p' too big number");
198             }
199 45 50         if (siz>0) {
200 45           cryptx_internal_mp2hex_with_leading_zero(self->key.p, buf, 20000, 0);
201 45           not_used = hv_store(rv_hash, "p", 1, newSVpv(buf, strlen(buf)), 0);
202             }
203             else{
204 0           not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0);
205             }
206             /* x */
207 45 50         siz = (self->key.x) ? mp_unsigned_bin_size(self->key.x) : 0;
208 45 50         if (siz>10000) {
209 0           croak("FATAL: key2hash failed - 'x' too big number");
210             }
211 45 100         if (siz>0) {
212 22           cryptx_internal_mp2hex_with_leading_zero(self->key.x, buf, 20000, qsize*2);
213 22           not_used = hv_store(rv_hash, "x", 1, newSVpv(buf, strlen(buf)), 0);
214             }
215             else{
216 23           not_used = hv_store(rv_hash, "x", 1, newSVpv("", 0), 0);
217             }
218             /* y */
219 45 50         siz = (self->key.y) ? mp_unsigned_bin_size(self->key.y) : 0;
220 45 50         if (siz>10000) {
221 0           croak("FATAL: key2hash failed - 'y' too big number");
222             }
223 45 50         if (siz>0) {
224 45           cryptx_internal_mp2hex_with_leading_zero(self->key.y, buf, 20000, psize*2);
225 45           not_used = hv_store(rv_hash, "y", 1, newSVpv(buf, strlen(buf)), 0);
226             }
227             else{
228 0           not_used = hv_store(rv_hash, "y", 1, newSVpv("", 0), 0);
229             }
230             /* size */
231 45           not_used = hv_store(rv_hash, "size", 4, newSViv(qsize), 0);
232             /* type */
233 45           not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0);
234             LTC_UNUSED_PARAM(not_used);
235 45           RETVAL = newRV_noinc((SV*)rv_hash);
236             OUTPUT:
237             RETVAL
238              
239             SV *
240             export_key_der(Crypt::PK::DSA self, char * type)
241             CODE:
242             {
243             int rv;
244             unsigned char out[4096];
245 16           unsigned long int out_len = 4096;
246              
247 16           RETVAL = newSVpvn(NULL, 0); /* undef */
248 16 100         if (strnEQ(type, "private", 7)) {
249 8           rv = dsa_export(out, &out_len, PK_PRIVATE|PK_STD, &self->key);
250 8 50         if (rv != CRYPT_OK) croak("FATAL: dsa_export(PK_PRIVATE|PK_STD) failed: %s", error_to_string(rv));
251 8           RETVAL = newSVpvn((char*)out, out_len);
252             }
253 8 50         else if (strnEQ(type, "public", 6)) {
254 8           rv = dsa_export(out, &out_len, PK_PUBLIC|PK_STD, &self->key);
255 8 50         if (rv != CRYPT_OK) croak("FATAL: dsa_export(PK_PUBLIC|PK_STD) failed: %s", error_to_string(rv));
256 8           RETVAL = newSVpvn((char*)out, out_len);
257             }
258             else {
259 0           croak("FATAL: export_key_der invalid type '%s'", type);
260             }
261             }
262             OUTPUT:
263             RETVAL
264              
265             SV *
266             encrypt(Crypt::PK::DSA self, SV * data, const char * hash_name = "SHA1")
267             CODE:
268             {
269             int rv, hash_id;
270 2           unsigned char *data_ptr=NULL;
271 2           STRLEN data_len=0;
272             unsigned char buffer[1024];
273 2           unsigned long buffer_len = 1024;
274              
275 2 50         data_ptr = (unsigned char *)SvPVbyte(data, data_len);
276              
277 2           hash_id = cryptx_internal_find_hash(hash_name);
278 2 50         if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
279 2           rv = dsa_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
280             &self->pstate, self->pindex,
281 2           hash_id, &self->key);
282 2 50         if (rv != CRYPT_OK) croak("FATAL: dsa_encrypt_key failed: %s", error_to_string(rv));
283 2           RETVAL = newSVpvn((char*)buffer, buffer_len);
284             }
285             OUTPUT:
286             RETVAL
287              
288             SV *
289             decrypt(Crypt::PK::DSA self, SV * data)
290             CODE:
291             {
292             int rv;
293 2           unsigned char *data_ptr=NULL;
294 2           STRLEN data_len=0;
295             unsigned char buffer[1024];
296 2           unsigned long buffer_len = 1024;
297              
298 2 50         data_ptr = (unsigned char *)SvPVbyte(data, data_len);
299              
300 2           rv = dsa_decrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &self->key);
301 2 50         if (rv != CRYPT_OK) croak("FATAL: dsa_decrypt_key_ex failed: %s", error_to_string(rv));
302 2           RETVAL = newSVpvn((char*)buffer, buffer_len);
303             }
304             OUTPUT:
305             RETVAL
306              
307             SV *
308             sign_hash(Crypt::PK::DSA self, SV * data, const char * hash_name = "SHA1")
309             ALIAS:
310             sign_message = 1
311             CODE:
312             {
313             int rv, id;
314 4           unsigned char buffer[1024], tmp[MAXBLOCKSIZE], *data_ptr = NULL;
315 4           unsigned long tmp_len = MAXBLOCKSIZE, buffer_len = 1024;
316 4           STRLEN data_len = 0;
317              
318 4 50         data_ptr = (unsigned char *)SvPVbyte(data, data_len);
319 4 100         if (ix == 1) {
320 2           id = cryptx_internal_find_hash(hash_name);
321 2 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
322 2           rv = hash_memory(id, data_ptr, (unsigned long)data_len, tmp, &tmp_len);
323 2 50         if (rv != CRYPT_OK) croak("FATAL: hash_memory failed: %s", error_to_string(rv));
324 2           data_ptr = tmp;
325 2           data_len = tmp_len;
326             }
327 4           rv = dsa_sign_hash(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
328             &self->pstate, self->pindex,
329 4           &self->key);
330 4 50         if (rv != CRYPT_OK) croak("FATAL: dsa_sign_hash_ex failed: %s", error_to_string(rv));
331 4           RETVAL = newSVpvn((char*)buffer, buffer_len);
332             }
333             OUTPUT:
334             RETVAL
335              
336             int
337             verify_hash(Crypt::PK::DSA self, SV * sig, SV * data, const char * hash_name = "SHA1")
338             ALIAS:
339             verify_message = 1
340             CODE:
341             {
342             int rv, stat, id;
343 40           unsigned char tmp[MAXBLOCKSIZE], *data_ptr = NULL, *sig_ptr = NULL;
344 40           unsigned long tmp_len = MAXBLOCKSIZE;
345 40           STRLEN data_len = 0, sig_len = 0;
346              
347 40 50         data_ptr = (unsigned char *)SvPVbyte(data, data_len);
348 40 50         sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len);
349 40 100         if (ix == 1) {
350 38           id = cryptx_internal_find_hash(hash_name);
351 38 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
352 38           rv = hash_memory(id, data_ptr, (unsigned long)data_len, tmp, &tmp_len);
353 38 50         if (rv != CRYPT_OK) croak("FATAL: hash_memory failed: %s", error_to_string(rv));
354 38           data_ptr = tmp;
355 38           data_len = tmp_len;
356             }
357 40           RETVAL = 1;
358 40           stat = 0;
359 40           rv = dsa_verify_hash(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key);
360 40 50         if (rv != CRYPT_OK || stat != 1) RETVAL = 0;
    50          
361             }
362             OUTPUT:
363             RETVAL
364              
365             void
366             DESTROY(Crypt::PK::DSA self)
367             CODE:
368 72 50         if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; }
369 72           Safefree(self);
370