File Coverage

inc/CryptX_PK_DSA.xs.inc
Criterion Covered Total %
statement 176 207 85.0
branch 97 196 49.4
condition n/a
subroutine n/a
pod n/a
total 273 403 67.7


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           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 52           unsigned char *data=NULL;
86 52           STRLEN data_len=0;
87              
88 52           data = (unsigned char *)SvPVbyte(key_data, data_len);
89 52 50         if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; }
90 52           rv = dsa_import(data, (unsigned long)data_len, &self->key);
91 52 50         if (rv != CRYPT_OK) croak("FATAL: dsa_import failed: %s", error_to_string(rv));
92 52 50         XPUSHs(ST(0)); /* return self */
93             }
94              
95             void
96             _import_pkcs8(Crypt::PK::DSA self, SV * key_data, SV * passwd)
97             PPCODE:
98             {
99             int rv;
100 0           unsigned char *data = NULL;
101 0           STRLEN data_len = 0;
102 0           password_ctx pw_ctx = { cryptx_internal_password_cb_getpw, cryptx_internal_password_cb_free, passwd };
103              
104 0           data = (unsigned char *)SvPVbyte(key_data, data_len);
105 0 0         if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; }
106 0 0         if (SvOK(passwd)) {
107 0           rv = dsa_import_pkcs8(data, (unsigned long)data_len, &pw_ctx, &self->key);
108             }
109             else {
110 0           rv = dsa_import_pkcs8(data, (unsigned long)data_len, NULL, &self->key);
111             }
112 0 0         if (rv != CRYPT_OK) croak("FATAL: dsa_import_pkcs8 failed: %s", error_to_string(rv));
113 0 0         XPUSHs(ST(0)); /* return self */
114             }
115              
116             void
117             _import_pem(Crypt::PK::DSA self, SV * key_data, SV * passwd)
118             PPCODE:
119             {
120             int rv;
121 20           unsigned char *data = NULL;
122 20           STRLEN data_len = 0;
123 20           password_ctx pw_ctx = { cryptx_internal_password_cb_getpw, cryptx_internal_password_cb_free, passwd };
124             ltc_pka_key key_from_pem;
125              
126 20           data = (unsigned char *)SvPVbyte(key_data, data_len);
127 20 100         if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; }
128 20 100         if (SvOK(passwd)) {
129 11           rv = pem_decode_pkcs(data, (unsigned long)data_len, &key_from_pem, &pw_ctx);
130             }
131             else {
132 9           rv = pem_decode_pkcs(data, (unsigned long)data_len, &key_from_pem, NULL);
133             }
134 20 50         if (rv != CRYPT_OK) croak("FATAL: pem_decode_pkcs failed: %s", error_to_string(rv));
135 20 50         if (key_from_pem.id != LTC_PKA_DSA) croak("FATAL: pem_decode_pkcs decoded non-DSA key");
136 20           self->key = key_from_pem.u.dsa;
137 20 50         XPUSHs(ST(0)); /* return self */
138             }
139              
140             void
141             _import_openssh(Crypt::PK::DSA self, SV * key_data, SV * passwd)
142             PPCODE:
143             {
144             int rv;
145 3           unsigned char *data = NULL;
146 3           STRLEN data_len = 0;
147 3           password_ctx pw_ctx = { cryptx_internal_password_cb_getpw, cryptx_internal_password_cb_free, passwd };
148             ltc_pka_key key_from_pem;
149              
150 3           data = (unsigned char *)SvPVbyte(key_data, data_len);
151 3 50         if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; }
152 3 100         if (SvOK(passwd)) {
153 1           rv = pem_decode_openssh(data, (unsigned long)data_len, &key_from_pem, &pw_ctx);
154             }
155             else {
156 2           rv = pem_decode_openssh(data, (unsigned long)data_len, &key_from_pem, NULL);
157             }
158 3 50         if (rv != CRYPT_OK) croak("FATAL: pem_decode_openssh failed: %s", error_to_string(rv));
159 3 50         if (key_from_pem.id != LTC_PKA_DSA) croak("FATAL: pem_decode_openssh decoded non-DSA key");
160 3           self->key = key_from_pem.u.dsa;
161 3 50         XPUSHs(ST(0)); /* return self */
162             }
163              
164             void
165             _import_hex(Crypt::PK::DSA self, char *p, char *q, char *g, char *x, char *y)
166             PPCODE:
167             {
168             int rv;
169             unsigned char pbin[512], qbin[512], gbin[512], xbin[512], ybin[512];
170 1           unsigned long plen=sizeof(pbin), qlen=sizeof(qbin), glen=sizeof(gbin), xlen=sizeof(xbin), ylen=sizeof(ybin);
171              
172 1 50         if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; }
173              
174 1 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          
175 1           rv = radix_to_bin(p, 16, pbin, &plen);
176 1 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(p) failed: %s", error_to_string(rv));
177 1           rv = radix_to_bin(q, 16, qbin, &qlen);
178 1 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(q) failed: %s", error_to_string(rv));
179 1           rv = radix_to_bin(g, 16, gbin, &glen);
180 1 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(g) failed: %s", error_to_string(rv));
181 1           rv = dsa_set_pqg(pbin, plen, qbin, qlen, gbin, glen, &self->key);
182 1 50         if (rv != CRYPT_OK) croak("FATAL: dsa_set_pqg failed: %s", error_to_string(rv));
183              
184 1           rv = radix_to_bin(y, 16, ybin, &ylen);
185 1 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(y) failed: %s", error_to_string(rv));
186 1 50         if (x && strlen(x) > 0) {
    0          
187             /* private */
188 0           rv = radix_to_bin(x, 16, xbin, &xlen);
189 0 0         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(x) failed: %s", error_to_string(rv));
190 0           rv = dsa_set_key(xbin, xlen, PK_PRIVATE, &self->key);
191 0 0         if (rv != CRYPT_OK) croak("FATAL: dsa_set_key failed: %s", error_to_string(rv));
192             }
193             else {
194             /* public */
195 1           rv = dsa_set_key(ybin, ylen, PK_PUBLIC, &self->key);
196 1 50         if (rv != CRYPT_OK) croak("FATAL: dsa_set_key failed: %s", error_to_string(rv));
197             }
198             }
199              
200 1 50         XPUSHs(ST(0)); /* return self */
201             }
202              
203             int
204             is_private(Crypt::PK::DSA self)
205             CODE:
206 34 50         if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF;
    50          
207 34 100         RETVAL = (self->key.type == PK_PRIVATE) ? 1 : 0;
208             OUTPUT:
209             RETVAL
210              
211             size_t
212             size(Crypt::PK::DSA self)
213             CODE:
214 1 50         if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF;
    50          
215 1           RETVAL = mp_ubin_size(self->key.p);
216             OUTPUT:
217             RETVAL
218              
219             size_t
220             size_q(Crypt::PK::DSA self)
221             CODE:
222 0 0         if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF;
    0          
223 0           RETVAL = mp_ubin_size(self->key.q);
224             OUTPUT:
225             RETVAL
226              
227             SV*
228             key2hash(Crypt::PK::DSA self)
229             PREINIT:
230             HV *rv_hash;
231             size_t siz, qsize, psize;
232             char buf[20001];
233             SV **not_used;
234             CODE:
235 46 50         if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF;
    50          
236 46           qsize = mp_ubin_size(self->key.q);
237 46           psize = mp_ubin_size(self->key.p);
238 46           rv_hash = newHV();
239             /* g */
240 46 50         siz = (self->key.g) ? mp_ubin_size(self->key.g) : 0;
241 46 50         if (siz>10000) {
242 0           croak("FATAL: key2hash failed - 'g' too big number");
243             }
244 46 50         if (siz>0) {
245 46           cryptx_internal_mp2hex_with_leading_zero(self->key.g, buf, 20000, 0);
246 46           not_used = hv_store(rv_hash, "g", 1, newSVpv(buf, strlen(buf)), 0);
247             }
248             else{
249 0           not_used = hv_store(rv_hash, "g", 1, newSVpv("", 0), 0);
250             }
251             /* q */
252 46 50         siz = (self->key.q) ? mp_ubin_size(self->key.q) : 0;
253 46 50         if (siz>10000) {
254 0           croak("FATAL: key2hash failed - 'q' too big number");
255             }
256 46 50         if (siz>0) {
257 46           cryptx_internal_mp2hex_with_leading_zero(self->key.q, buf, 20000, 0);
258 46           not_used = hv_store(rv_hash, "q", 1, newSVpv(buf, strlen(buf)), 0);
259             }
260             else{
261 0           not_used = hv_store(rv_hash, "q", 1, newSVpv("", 0), 0);
262             }
263             /* p */
264 46 50         siz = (self->key.p) ? mp_ubin_size(self->key.p) : 0;
265 46 50         if (siz>10000) {
266 0           croak("FATAL: key2hash failed - 'p' too big number");
267             }
268 46 50         if (siz>0) {
269 46           cryptx_internal_mp2hex_with_leading_zero(self->key.p, buf, 20000, 0);
270 46           not_used = hv_store(rv_hash, "p", 1, newSVpv(buf, strlen(buf)), 0);
271             }
272             else{
273 0           not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0);
274             }
275             /* x */
276 46 50         siz = (self->key.x) ? mp_ubin_size(self->key.x) : 0;
277 46 50         if (siz>10000) {
278 0           croak("FATAL: key2hash failed - 'x' too big number");
279             }
280 46 100         if (siz>0) {
281 25           cryptx_internal_mp2hex_with_leading_zero(self->key.x, buf, 20000, qsize*2);
282 25           not_used = hv_store(rv_hash, "x", 1, newSVpv(buf, strlen(buf)), 0);
283             }
284             else{
285 21           not_used = hv_store(rv_hash, "x", 1, newSVpv("", 0), 0);
286             }
287             /* y */
288 46 50         siz = (self->key.y) ? mp_ubin_size(self->key.y) : 0;
289 46 50         if (siz>10000) {
290 0           croak("FATAL: key2hash failed - 'y' too big number");
291             }
292 46 50         if (siz>0) {
293 46           cryptx_internal_mp2hex_with_leading_zero(self->key.y, buf, 20000, psize*2);
294 46           not_used = hv_store(rv_hash, "y", 1, newSVpv(buf, strlen(buf)), 0);
295             }
296             else{
297 0           not_used = hv_store(rv_hash, "y", 1, newSVpv("", 0), 0);
298             }
299             /* size */
300 46           not_used = hv_store(rv_hash, "size", 4, newSViv(qsize), 0);
301             /* type */
302 46           not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0);
303             LTC_UNUSED_PARAM(not_used);
304 46           RETVAL = newRV_noinc((SV*)rv_hash);
305             OUTPUT:
306             RETVAL
307              
308             SV *
309             export_key_der(Crypt::PK::DSA self, char * type)
310             CODE:
311             {
312             int rv;
313             unsigned char out[4096];
314 20           unsigned long int out_len = 4096;
315              
316 20           RETVAL = newSVpvn(NULL, 0); /* undef */
317 20 100         if (strnEQ(type, "private", 7)) {
318 10           rv = dsa_export(out, &out_len, PK_PRIVATE|PK_STD, &self->key);
319 10 50         if (rv != CRYPT_OK) croak("FATAL: dsa_export(PK_PRIVATE|PK_STD) failed: %s", error_to_string(rv));
320 10           RETVAL = newSVpvn((char*)out, out_len);
321             }
322 10 50         else if (strnEQ(type, "public", 6)) {
323 10           rv = dsa_export(out, &out_len, PK_PUBLIC|PK_STD, &self->key);
324 10 50         if (rv != CRYPT_OK) croak("FATAL: dsa_export(PK_PUBLIC|PK_STD) failed: %s", error_to_string(rv));
325 10           RETVAL = newSVpvn((char*)out, out_len);
326             }
327             else {
328 0           croak("FATAL: export_key_der invalid type '%s'", type);
329             }
330             }
331             OUTPUT:
332             RETVAL
333              
334             SV *
335             encrypt(Crypt::PK::DSA self, SV * data, const char * hash_name = "SHA1")
336             CODE:
337             {
338             int rv, hash_id;
339 2           unsigned char *data_ptr=NULL;
340 2           STRLEN data_len=0;
341             unsigned char buffer[1024];
342 2           unsigned long buffer_len = 1024;
343              
344 2           data_ptr = (unsigned char *)SvPVbyte(data, data_len);
345              
346 2           hash_id = cryptx_internal_find_hash(hash_name);
347 2 50         if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
348 2           rv = dsa_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
349             &self->pstate, self->pindex,
350 2           hash_id, &self->key);
351 2 50         if (rv != CRYPT_OK) croak("FATAL: dsa_encrypt_key failed: %s", error_to_string(rv));
352 2           RETVAL = newSVpvn((char*)buffer, buffer_len);
353             }
354             OUTPUT:
355             RETVAL
356              
357             SV *
358             decrypt(Crypt::PK::DSA self, SV * data)
359             CODE:
360             {
361             int rv;
362 2           unsigned char *data_ptr=NULL;
363 2           STRLEN data_len=0;
364             unsigned char buffer[1024];
365 2           unsigned long buffer_len = 1024;
366              
367 2           data_ptr = (unsigned char *)SvPVbyte(data, data_len);
368              
369 2           rv = dsa_decrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &self->key);
370 2 50         if (rv != CRYPT_OK) croak("FATAL: dsa_decrypt_key_ex failed: %s", error_to_string(rv));
371 2           RETVAL = newSVpvn((char*)buffer, buffer_len);
372             }
373             OUTPUT:
374             RETVAL
375              
376             SV *
377             sign_hash(Crypt::PK::DSA self, SV * data, const char * hash_name = "SHA1")
378             ALIAS:
379             sign_message = 1
380             CODE:
381             {
382             int rv, id;
383 4           unsigned char buffer[1024], tmp[MAXBLOCKSIZE], *data_ptr = NULL;
384 4           unsigned long tmp_len = MAXBLOCKSIZE, buffer_len = 1024;
385 4           STRLEN data_len = 0;
386              
387 4           data_ptr = (unsigned char *)SvPVbyte(data, data_len);
388 4 100         if (ix == 1) {
389 2           id = cryptx_internal_find_hash(hash_name);
390 2 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
391 2           rv = hash_memory(id, data_ptr, (unsigned long)data_len, tmp, &tmp_len);
392 2 50         if (rv != CRYPT_OK) croak("FATAL: hash_memory failed: %s", error_to_string(rv));
393 2           data_ptr = tmp;
394 2           data_len = tmp_len;
395             }
396 4           rv = dsa_sign_hash(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
397             &self->pstate, self->pindex,
398 4           &self->key);
399 4 50         if (rv != CRYPT_OK) croak("FATAL: dsa_sign_hash_ex failed: %s", error_to_string(rv));
400 4           RETVAL = newSVpvn((char*)buffer, buffer_len);
401             }
402             OUTPUT:
403             RETVAL
404              
405             int
406             verify_hash(Crypt::PK::DSA self, SV * sig, SV * data, const char * hash_name = "SHA1")
407             ALIAS:
408             verify_message = 1
409             CODE:
410             {
411             int rv, stat, id;
412 40           unsigned char tmp[MAXBLOCKSIZE], *data_ptr = NULL, *sig_ptr = NULL;
413 40           unsigned long tmp_len = MAXBLOCKSIZE;
414 40           STRLEN data_len = 0, sig_len = 0;
415              
416 40           data_ptr = (unsigned char *)SvPVbyte(data, data_len);
417 40           sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len);
418 40 100         if (ix == 1) {
419 38           id = cryptx_internal_find_hash(hash_name);
420 38 50         if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
421 38           rv = hash_memory(id, data_ptr, (unsigned long)data_len, tmp, &tmp_len);
422 38 50         if (rv != CRYPT_OK) croak("FATAL: hash_memory failed: %s", error_to_string(rv));
423 38           data_ptr = tmp;
424 38           data_len = tmp_len;
425             }
426 40           RETVAL = 1;
427 40           stat = 0;
428 40           rv = dsa_verify_hash(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key);
429 40 50         if (rv != CRYPT_OK || stat != 1) RETVAL = 0;
    50          
430             }
431             OUTPUT:
432             RETVAL
433              
434             void
435             DESTROY(Crypt::PK::DSA self)
436             CODE:
437 72 50         if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; }
438 72           Safefree(self);
439