File Coverage

inc/CryptX_PK_RSA.xs.inc
Criterion Covered Total %
statement 307 378 81.2
branch 159 302 52.6
condition n/a
subroutine n/a
pod n/a
total 466 680 68.5


line stmt bran cond sub pod time code
1             MODULE = CryptX PACKAGE = Crypt::PK::RSA
2              
3             PROTOTYPES: DISABLE
4              
5             Crypt::PK::RSA
6             _new(Class)
7             CODE:
8             {
9             int rv;
10 116           Newz(0, RETVAL, 1, struct rsa_struct);
11 116 50         if (!RETVAL) croak("FATAL: Newz failed");
12 116           RETVAL->key.type = -1;
13 116           RETVAL->pindex = find_prng("chacha20");
14 116           RETVAL->last_pid = (IV)PerlProc_getpid();
15 116 50         if (RETVAL->pindex == -1) {
16 0           zeromem(RETVAL, sizeof(*RETVAL));
17 0           Safefree(RETVAL);
18 0           croak("FATAL: find_prng('chacha20') failed");
19             }
20 116           rv = rng_make_prng(320, RETVAL->pindex, &RETVAL->pstate, NULL); /* 320bits = 40bytes */
21 116 50         if (rv != CRYPT_OK) {
22 0           zeromem(RETVAL, sizeof(*RETVAL));
23 0           Safefree(RETVAL);
24 0           croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
25             }
26             }
27             OUTPUT:
28             RETVAL
29              
30             void
31             generate_key(Crypt::PK::RSA self, int key_size=256, long key_e=65537)
32             PPCODE:
33             {
34             /* key_size is in octets */
35             int rv;
36 4           cryptx_internal_pk_prng_reseed(&self->pstate, self->pindex, &self->last_pid);
37 4 100         if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; }
38             /* gen the key */
39 4           rv = rsa_make_key(&self->pstate, self->pindex, key_size, key_e, &self->key);
40 4 50         if (rv != CRYPT_OK) croak("FATAL: rsa_make_key failed: %s", error_to_string(rv));
41 4 50         XPUSHs(ST(0)); /* return self */
42             }
43              
44             void
45             _import(Crypt::PK::RSA self, SV * key_data)
46             PPCODE:
47             {
48             int rv;
49 93           unsigned char *data=NULL;
50 93           STRLEN data_len=0;
51              
52 93           data = (unsigned char *)SvPVbyte(key_data, data_len);
53 93 100         if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; }
54 93           rv = rsa_import(data, (unsigned long)data_len, &self->key);
55 93 100         if (rv != CRYPT_OK) croak("FATAL: rsa_import failed: %s", error_to_string(rv));
56 91 50         XPUSHs(ST(0)); /* return self */
57             }
58              
59             void
60             _import_pkcs8(Crypt::PK::RSA self, SV * key_data, SV * passwd)
61             PPCODE:
62             {
63             int rv;
64 2           unsigned char *data = NULL;
65 2           STRLEN data_len = 0;
66 2           password_ctx pw_ctx = { cryptx_internal_password_cb_getpw, cryptx_internal_password_cb_free, passwd };
67              
68 2           data = (unsigned char *)SvPVbyte(key_data, data_len);
69 2 50         if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; }
70 2 100         if (SvOK(passwd)) {
71 1           rv = rsa_import_pkcs8(data, (unsigned long)data_len, &pw_ctx, &self->key);
72             }
73             else {
74 1           rv = rsa_import_pkcs8(data, (unsigned long)data_len, NULL, &self->key);
75             }
76 2 50         if (rv != CRYPT_OK) croak("FATAL: rsa_import_pkcs8 failed: %s", error_to_string(rv));
77 2 50         XPUSHs(ST(0)); /* return self */
78             }
79              
80             void
81             _import_pem(Crypt::PK::RSA self, SV * key_data, SV * passwd)
82             PPCODE:
83             {
84             int rv;
85 56           unsigned char *data = NULL;
86 56           STRLEN data_len = 0;
87 56           password_ctx pw_ctx = { cryptx_internal_password_cb_getpw, cryptx_internal_password_cb_free, passwd };
88             ltc_pka_key key_from_pem;
89              
90 56           data = (unsigned char *)SvPVbyte(key_data, data_len);
91 56 100         if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; }
92 56 100         if (SvOK(passwd)) {
93 22           rv = pem_decode_pkcs(data, (unsigned long)data_len, &key_from_pem, &pw_ctx);
94             }
95             else {
96 34           rv = pem_decode_pkcs(data, (unsigned long)data_len, &key_from_pem, NULL);
97             }
98 56 50         if (rv != CRYPT_OK) croak("FATAL: pem_decode_pkcs failed: %s", error_to_string(rv));
99 56 50         if (key_from_pem.id != LTC_PKA_RSA && key_from_pem.id != LTC_PKA_RSA_PSS) croak("FATAL: pem_decode_pkcs decoded non-RSA key");
    0          
100 56           self->key = key_from_pem.u.rsa;
101 56 50         XPUSHs(ST(0)); /* return self */
102             }
103              
104              
105             void
106             _import_openssh(Crypt::PK::RSA self, SV * key_data, SV * passwd)
107             PPCODE:
108             {
109             int rv;
110 18           unsigned char *data = NULL;
111 18           STRLEN data_len = 0;
112 18           password_ctx pw_ctx = { cryptx_internal_password_cb_getpw, cryptx_internal_password_cb_free, passwd };
113             ltc_pka_key key_from_pem;
114              
115 18           data = (unsigned char *)SvPVbyte(key_data, data_len);
116 18 50         if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; }
117 18 100         if (SvOK(passwd)) {
118 6           rv = pem_decode_openssh(data, (unsigned long)data_len, &key_from_pem, &pw_ctx);
119             }
120             else {
121 12           rv = pem_decode_openssh(data, (unsigned long)data_len, &key_from_pem, NULL);
122             }
123 18 50         if (rv != CRYPT_OK) croak("FATAL: pem_decode_openssh failed: %s", error_to_string(rv));
124 18 50         if (key_from_pem.id != LTC_PKA_RSA && key_from_pem.id != LTC_PKA_RSA_PSS) croak("FATAL: pem_decode_openssh decoded non-RSA key");
    0          
125 18           self->key = key_from_pem.u.rsa;
126 18 50         XPUSHs(ST(0)); /* return self */
127             }
128              
129             void
130             _import_x509(Crypt::PK::RSA self, SV * key_data)
131             PPCODE:
132             {
133             int rv;
134 0           unsigned char *data=NULL;
135 0           STRLEN data_len=0;
136              
137 0           data = (unsigned char *)SvPVbyte(key_data, data_len);
138 0 0         if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; }
139 0           rv = rsa_import_x509(data, (unsigned long)data_len, &self->key);
140 0 0         if (rv != CRYPT_OK) croak("FATAL: rsa_import_x509 failed: %s", error_to_string(rv));
141 0 0         XPUSHs(ST(0)); /* return self */
142             }
143              
144             void
145             _import_hex(Crypt::PK::RSA self, char *N, char *e, char *d=NULL, char *p=NULL, char *q=NULL, char *dP=NULL, char *dQ=NULL, char *qP=NULL)
146             PPCODE:
147             {
148             int rv;
149             unsigned char Nbin[1024], ebin[128], dbin[1024], pbin[512], qbin[512], dPbin[512], dQbin[512], qPbin[512];
150 9           unsigned long Nlen=sizeof(Nbin), elen=sizeof(ebin), dlen=sizeof(dbin), plen=sizeof(pbin),
151 9           qlen=sizeof(qbin), dPlen=sizeof(dPbin), dQlen=sizeof(dQbin), qPlen=sizeof(qPbin);
152              
153 9           rv = radix_to_bin(N, 16, Nbin, &Nlen);
154 9 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(N) failed: %s", error_to_string(rv));
155 9           rv = radix_to_bin(e, 16, ebin, &elen);
156 9 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(e) failed: %s", error_to_string(rv));
157 9 100         if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; }
158              
159 9 100         if (d && strlen(d) > 0) {
    50          
160             /* private */
161 2           rv = radix_to_bin(d, 16, dbin, &dlen);
162 2 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(d) failed: %s", error_to_string(rv));
163 2           rv = rsa_set_key(Nbin, Nlen, ebin, elen, dbin, dlen, &self->key);
164 2 50         if (rv != CRYPT_OK) croak("FATAL: rsa_set_key failed: %s", error_to_string(rv));
165             }
166             else {
167             /* public */
168 7           rv = rsa_set_key(Nbin, Nlen, ebin, elen, NULL, 0, &self->key);
169 7 50         if (rv != CRYPT_OK) croak("FATAL: rsa_set_key failed: %s", error_to_string(rv));
170             }
171              
172 9 100         if (p && strlen(p) > 0 && q && strlen(q) > 0) {
    50          
    50          
    50          
173             /* private only */
174 2           rv = radix_to_bin(p, 16, pbin, &plen);
175 2 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(p) failed: %s", error_to_string(rv));
176 2           rv = radix_to_bin(q, 16, qbin, &qlen);
177 2 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(q) failed: %s", error_to_string(rv));
178 2           rv = rsa_set_factors(pbin, plen, qbin, qlen, &self->key);
179 2 50         if (rv != CRYPT_OK) croak("FATAL: rsa_set_factors failed: %s", error_to_string(rv));
180             }
181              
182 9 100         if (dP && strlen(dP) > 0 && dQ && strlen(dQ) > 0 && qP && strlen(qP) > 0) {
    50          
    50          
    50          
    50          
    50          
183             /* private only */
184 2           rv = radix_to_bin(dP, 16, dPbin, &dPlen);
185 2 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(dP) failed: %s", error_to_string(rv));
186 2           rv = radix_to_bin(dQ, 16, dQbin, &dQlen);
187 2 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(dQ) failed: %s", error_to_string(rv));
188 2           rv = radix_to_bin(qP, 16, qPbin, &qPlen);
189 2 50         if (rv != CRYPT_OK) croak("FATAL: radix_to_bin(qP) failed: %s", error_to_string(rv));
190 2           rv = rsa_set_crt_params(dPbin, dPlen, dQbin, dQlen, qPbin, qPlen, &self->key);
191 2 50         if (rv != CRYPT_OK) croak("FATAL: rsa_set_crt_params failed: %s", error_to_string(rv));
192             }
193              
194 9 50         XPUSHs(ST(0)); /* return self */
195             }
196              
197             int
198             is_private(Crypt::PK::RSA self)
199             CODE:
200 89 50         if (self->key.type == -1 || self->key.N == NULL) XSRETURN_UNDEF;
    50          
201 89 100         RETVAL = (self->key.type == PK_PRIVATE) ? 1 : 0;
202             OUTPUT:
203             RETVAL
204              
205             size_t
206             size(Crypt::PK::RSA self)
207             CODE:
208 2 50         if (self->key.type == -1 || self->key.N == NULL) XSRETURN_UNDEF;
    50          
209 2           RETVAL = mp_ubin_size(self->key.N);
210             OUTPUT:
211             RETVAL
212              
213             SV*
214             key2hash(Crypt::PK::RSA self)
215             PREINIT:
216             HV *rv_hash;
217             size_t siz, nsize;
218             char buf[20001];
219             SV **not_used;
220             CODE:
221 141 50         if (self->key.type == -1 || self->key.N == NULL) XSRETURN_UNDEF;
    50          
222 141           nsize = mp_ubin_size(self->key.N);
223 141           rv_hash = newHV();
224             /* e */
225 141 50         siz = (self->key.e) ? mp_ubin_size(self->key.e) : 0;
226 141 50         if (siz>10000) {
227 0           croak("FATAL: key2hash failed - 'e' too big number");
228             }
229 141 50         if (siz>0) {
230 141           cryptx_internal_mp2hex_with_leading_zero(self->key.e, buf, 20000, 0);
231 141           not_used = hv_store(rv_hash, "e", 1, newSVpv(buf, strlen(buf)), 0);
232             }
233             else{
234 0           not_used = hv_store(rv_hash, "e", 1, newSVpv("", 0), 0);
235             }
236             /* d */
237 141 50         siz = (self->key.d) ? mp_ubin_size(self->key.d) : 0;
238 141 50         if (siz>10000) {
239 0           croak("FATAL: key2hash failed - 'd' too big number");
240             }
241 141 100         if (siz>0) {
242 81           cryptx_internal_mp2hex_with_leading_zero(self->key.d, buf, 20000, 0);
243 81           not_used = hv_store(rv_hash, "d", 1, newSVpv(buf, strlen(buf)), 0);
244             }
245             else{
246 60           not_used = hv_store(rv_hash, "d", 1, newSVpv("", 0), 0);
247             }
248             /* N */
249 141 50         siz = (self->key.N) ? nsize : 0;
250 141 50         if (siz>10000) {
251 0           croak("FATAL: key2hash failed - 'N' too big number");
252             }
253 141 50         if (siz>0) {
254 141           cryptx_internal_mp2hex_with_leading_zero(self->key.N, buf, 20000, 0);
255 141           not_used = hv_store(rv_hash, "N", 1, newSVpv(buf, strlen(buf)), 0);
256             }
257             else{
258 0           not_used = hv_store(rv_hash, "N", 1, newSVpv("", 0), 0);
259             }
260             /* q */
261 141 50         siz = (self->key.q) ? mp_ubin_size(self->key.q) : 0;
262 141 50         if (siz>10000) {
263 0           croak("FATAL: key2hash failed - 'q' too big number");
264             }
265 141 100         if (siz>0) {
266 81           cryptx_internal_mp2hex_with_leading_zero(self->key.q, buf, 20000, 0);
267 81           not_used = hv_store(rv_hash, "q", 1, newSVpv(buf, strlen(buf)), 0);
268             }
269             else{
270 60           not_used = hv_store(rv_hash, "q", 1, newSVpv("", 0), 0);
271             }
272             /* p */
273 141 50         siz = (self->key.p) ? mp_ubin_size(self->key.p) : 0;
274 141 50         if (siz>10000) {
275 0           croak("FATAL: key2hash failed - 'p' too big number");
276             }
277 141 100         if (siz>0) {
278 81           cryptx_internal_mp2hex_with_leading_zero(self->key.p, buf, 20000, 0);
279 81           not_used = hv_store(rv_hash, "p", 1, newSVpv(buf, strlen(buf)), 0);
280             }
281             else{
282 60           not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0);
283             }
284             /* qP */
285 141 50         siz = (self->key.qP) ? mp_ubin_size(self->key.qP) : 0;
286 141 50         if (siz>10000) {
287 0           croak("FATAL: key2hash failed - 'qP' too big number");
288             }
289 141 100         if (siz>0) {
290 81           cryptx_internal_mp2hex_with_leading_zero(self->key.qP, buf, 20000, 0);
291 81           not_used = hv_store(rv_hash, "qP", 2, newSVpv(buf, strlen(buf)), 0);
292             }
293             else{
294 60           not_used = hv_store(rv_hash, "qP", 2, newSVpv("", 0), 0);
295             }
296             /* dP */
297 141 50         siz = (self->key.dP) ? mp_ubin_size(self->key.dP) : 0;
298 141 50         if (siz>10000) {
299 0           croak("FATAL: key2hash failed - 'dP' too big number");
300             }
301 141 100         if (siz>0) {
302 81           cryptx_internal_mp2hex_with_leading_zero(self->key.dP, buf, 20000, 0);
303 81           not_used = hv_store(rv_hash, "dP", 2, newSVpv(buf, strlen(buf)), 0);
304             }
305             else{
306 60           not_used = hv_store(rv_hash, "dP", 2, newSVpv("", 0), 0);
307             }
308             /* dQ */
309 141 50         siz = (self->key.dQ) ? mp_ubin_size(self->key.dQ) : 0;
310 141 50         if (siz>10000) {
311 0           croak("FATAL: key2hash failed - 'dQ' too big number");
312             }
313 141 100         if (siz>0) {
314 81           cryptx_internal_mp2hex_with_leading_zero(self->key.dQ, buf, 20000, 0);
315 81           not_used = hv_store(rv_hash, "dQ", 2, newSVpv(buf, strlen(buf)), 0);
316             }
317             else{
318 60           not_used = hv_store(rv_hash, "dQ", 2, newSVpv("", 0), 0);
319             }
320             /* size */
321 141           not_used = hv_store(rv_hash, "size", 4, newSViv(nsize), 0);
322             /* type */
323 141           not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0);
324             LTC_UNUSED_PARAM(not_used);
325 141           RETVAL = newRV_noinc((SV*)rv_hash);
326             OUTPUT:
327             RETVAL
328              
329             SV*
330             export_key_der(Crypt::PK::RSA self, char * type)
331             CODE:
332             {
333             int rv;
334             unsigned char out[4096];
335 5           unsigned long out_len = 4096;
336              
337 5           RETVAL = newSVpvn(NULL, 0); /* undef */
338 5 100         if (strEQ(type, "private")) {
339 2           rv = rsa_export(out, &out_len, PK_PRIVATE, &self->key);
340 2 50         if (rv != CRYPT_OK) croak("FATAL: rsa_export(PK_PRIVATE) failed: %s", error_to_string(rv));
341 2           sv_setpvn(RETVAL, (char*)out, out_len);
342             }
343 3 50         else if (strEQ(type, "public")) {
344 3           rv = rsa_export(out, &out_len, PK_PUBLIC|PK_STD, &self->key);
345 3 50         if (rv != CRYPT_OK) croak("FATAL: rsa_export(PK_PUBLIC|PK_STD) failed: %s", error_to_string(rv));
346 3           sv_setpvn(RETVAL, (char*)out, out_len);
347             }
348             else {
349 0           croak("FATAL: export_key_der invalid type '%s'", type);
350             }
351 5           zeromem(out, sizeof(out));
352             }
353             OUTPUT:
354             RETVAL
355              
356             SV *
357             encrypt(Crypt::PK::RSA self, SV * data, const char * padding = "oaep", const char * mgf_hash = "SHA1", SV * oaep_lparam = NULL, const char * lparam_hash = NULL)
358             CODE:
359             {
360             int rv, mgf_hash_id, lparam_hash_id;
361 3           unsigned char *lparam_ptr=NULL;
362 3           STRLEN lparam_len=0;
363 3           unsigned char *data_ptr=NULL;
364 3           STRLEN data_len=0;
365             unsigned char buffer[1024];
366 3           unsigned long buffer_len = 1024;
367             ltc_rsa_op_parameters rsa_oparams;
368              
369 3           data_ptr = (unsigned char *)SvPVbyte(data, data_len);
370              
371 3           RETVAL = newSVpvn(NULL, 0); /* undef */
372 3           cryptx_internal_pk_prng_reseed(&self->pstate, self->pindex, &self->last_pid);
373 3 100         if (strnEQ(padding, "oaep", 4)) {
374 2           mgf_hash_id = cryptx_internal_find_hash(mgf_hash);
375 2 50         if (mgf_hash_id == -1) croak("FATAL: find_hash failed for '%s'", mgf_hash);
376 2 50         if (lparam_hash) {
377 0           lparam_hash_id = cryptx_internal_find_hash(lparam_hash);
378 0 0         if (lparam_hash_id == -1) croak("FATAL: find_hash failed for '%s'", lparam_hash);
379             }
380             else {
381 2           lparam_hash_id = mgf_hash_id;
382             }
383 2 50         if (oaep_lparam) lparam_ptr = (unsigned char *)SvPVbyte(oaep_lparam, lparam_len);
384 2           rsa_oparams.prng = &self->pstate;
385 2           rsa_oparams.wprng = self->pindex;
386 2           rsa_oparams.padding = LTC_PKCS_1_OAEP;
387 2           rsa_oparams.u.crypt.lparam = lparam_ptr;
388 2           rsa_oparams.u.crypt.lparamlen = (unsigned long)lparam_len;
389 2           rsa_oparams.params.hash_idx = lparam_hash_id;
390 2           rsa_oparams.params.mgf1_hash_idx = mgf_hash_id;
391 2           rsa_oparams.params.saltlen = 0;
392 2           rv = rsa_encrypt_key_v2(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &rsa_oparams, &self->key);
393 2 50         if (rv != CRYPT_OK) croak("FATAL: rsa_encrypt_key_v2/oaep failed: %s", error_to_string(rv));
394 2           sv_setpvn(RETVAL, (char*)buffer, buffer_len);
395             }
396 1 50         else if (strnEQ(padding, "v1.5", 4)) {
397 0           rsa_oparams.prng = &self->pstate;
398 0           rsa_oparams.wprng = self->pindex;
399 0           rsa_oparams.padding = LTC_PKCS_1_V1_5;
400 0           rsa_oparams.u.crypt.lparam = NULL; /* OAEP only */
401 0           rsa_oparams.u.crypt.lparamlen = 0; /* OAEP only */
402 0           rsa_oparams.params.hash_idx = -1; /* OAEP only */
403 0           rsa_oparams.params.mgf1_hash_idx = -1; /* OAEP only */
404 0           rsa_oparams.params.saltlen = 0; /* PSS only */
405 0           rv = rsa_encrypt_key_v2(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &rsa_oparams, &self->key);
406 0 0         if (rv != CRYPT_OK) croak("FATAL: rsa_encrypt_key_v2/v1.5 failed: %s", error_to_string(rv));
407 0           sv_setpvn(RETVAL, (char*)buffer, buffer_len);
408             }
409 1 50         else if (strnEQ(padding, "none", 4)) {
410             /* raw RSA */
411 1           rv = ltc_mp.rsa_me(data_ptr, (unsigned long)data_len, buffer, &buffer_len, PK_PUBLIC, &self->key);
412 1 50         if (rv != CRYPT_OK) croak("FATAL: rsa_me failed: %s", error_to_string(rv));
413 1           sv_setpvn(RETVAL, (char*)buffer, buffer_len);
414             }
415             else {
416 0           croak("FATAL: rsa_encrypt invalid padding '%s'", padding);
417             }
418 3           zeromem(buffer, sizeof(buffer));
419             }
420             OUTPUT:
421             RETVAL
422              
423             SV *
424             decrypt(Crypt::PK::RSA self, SV * data, const char * padding = "oaep", const char * mgf_hash = "SHA1", SV * oaep_lparam = NULL, const char * lparam_hash = NULL)
425             CODE:
426             {
427             int rv, lparam_hash_id, mgf_hash_id, stat;
428 40           unsigned char *lparam_ptr=NULL;
429 40           STRLEN lparam_len=0;
430 40           unsigned char *data_ptr=NULL;
431 40           STRLEN data_len=0;
432             unsigned char buffer[1024];
433 40           unsigned long buffer_len = 1024;
434             ltc_rsa_op_parameters rsa_oparams;
435              
436 40           data_ptr = (unsigned char *)SvPVbyte(data, data_len);
437              
438 40           RETVAL = newSVpvn(NULL, 0); /* undef */
439 40           cryptx_internal_pk_prng_reseed(&self->pstate, self->pindex, &self->last_pid);
440 40 100         if (strnEQ(padding, "oaep", 4)) {
441 3           mgf_hash_id = cryptx_internal_find_hash(mgf_hash);
442 3 50         if (mgf_hash_id == -1) croak("FATAL: find_hash failed for '%s'", mgf_hash);
443 3 100         if (lparam_hash) {
444 2           lparam_hash_id = cryptx_internal_find_hash(lparam_hash);
445 2 50         if (lparam_hash_id == -1) croak("FATAL: find_hash failed for '%s'", lparam_hash);
446             }
447             else {
448 1           lparam_hash_id = mgf_hash_id;
449             }
450 3 100         if (oaep_lparam) lparam_ptr = (unsigned char *)SvPVbyte(oaep_lparam, lparam_len);
451 3           rsa_oparams.prng = NULL;
452 3           rsa_oparams.wprng = 0;
453 3           rsa_oparams.padding = LTC_PKCS_1_OAEP;
454 3           rsa_oparams.u.crypt.lparam = lparam_ptr;
455 3           rsa_oparams.u.crypt.lparamlen = (unsigned long)lparam_len;
456 3           rsa_oparams.params.hash_idx = lparam_hash_id;
457 3           rsa_oparams.params.mgf1_hash_idx = mgf_hash_id;
458 3           rsa_oparams.params.saltlen = 0;
459 3           rv = rsa_decrypt_key_v2(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &rsa_oparams, &stat, &self->key);
460 3 100         if (rv != CRYPT_OK) croak("FATAL: rsa_decrypt_key_v2 failed: %s", error_to_string(rv));
461 1 50         if (stat != 1) croak("FATAL: rsa_decrypt - not valid OAEP packet");
462 1           sv_setpvn(RETVAL, (char*)buffer, buffer_len);
463             }
464 37 100         else if (strnEQ(padding, "v1.5", 4)) {
465 36           rsa_oparams.prng = &self->pstate;
466 36           rsa_oparams.wprng = self->pindex;
467 36           rsa_oparams.padding = LTC_PKCS_1_V1_5;
468 36           rsa_oparams.u.crypt.lparam = NULL; /* OAEP only */
469 36           rsa_oparams.u.crypt.lparamlen = 0; /* OAEP only */
470 36           rsa_oparams.params.hash_idx = -1; /* OAEP only */
471 36           rsa_oparams.params.mgf1_hash_idx = -1; /* OAEP only */
472 36           rsa_oparams.params.saltlen = 0; /* PSS only */
473 36           rv = rsa_decrypt_key_v2(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &rsa_oparams, &stat, &self->key);
474 36 50         if (rv != CRYPT_OK) croak("FATAL: rsa_decrypt_key_v2 failed: %s", error_to_string(rv));
475 36 50         if (stat != 1) croak("FATAL: rsa_decrypt - invalid");
476 36           sv_setpvn(RETVAL, (char*)buffer, buffer_len);
477             }
478 1 50         else if (strnEQ(padding, "none", 4)) {
479             /* raw RSA */
480 1           rv = ltc_mp.rsa_me(data_ptr, (unsigned long)data_len, buffer, &buffer_len, PK_PRIVATE, &self->key);
481 1 50         if (rv != CRYPT_OK) croak("FATAL: rsa_me failed: %s", error_to_string(rv));
482 1           sv_setpvn(RETVAL, (char*)buffer, buffer_len);
483             }
484             else {
485 0           croak("FATAL: rsa_decrypt invalid padding '%s'", padding);
486             }
487 38           zeromem(buffer, sizeof(buffer));
488             }
489             OUTPUT:
490             RETVAL
491              
492             SV *
493             sign_hash(Crypt::PK::RSA self, SV * data, const char * hash_name = "SHA1", const char * padding = "pss", unsigned long saltlen=12, const char * mgf_hash_name = NULL)
494             ALIAS:
495             sign_message = 1
496             CODE:
497             {
498             int rv, hash_id, mgf_hash_id;
499 9           unsigned char buffer[1024], tmp[MAXBLOCKSIZE], *data_ptr = NULL;
500 9           unsigned long tmp_len = MAXBLOCKSIZE, buffer_len = 1024;
501 9           STRLEN data_len = 0;
502             ltc_rsa_op_parameters rsa_oparams;
503              
504 9           data_ptr = (unsigned char *)SvPVbyte(data, data_len);
505 9           cryptx_internal_pk_prng_reseed(&self->pstate, self->pindex, &self->last_pid);
506 9 100         if (ix == 1) {
507 5           hash_id = cryptx_internal_find_hash(hash_name);
508 5 50         if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
509 5           rv = hash_memory(hash_id, data_ptr, (unsigned long)data_len, tmp, &tmp_len);
510 5 50         if (rv != CRYPT_OK) croak("FATAL: hash_memory failed: %s", error_to_string(rv));
511 5           data_ptr = tmp;
512 5           data_len = tmp_len;
513             }
514 9 50         if (strnEQ(padding, "pss", 3)) {
515 9           hash_id = cryptx_internal_find_hash(hash_name);
516 9 50         if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
517 9 50         mgf_hash_id = mgf_hash_name ? cryptx_internal_find_hash(mgf_hash_name) : hash_id;
518 9 50         if (mgf_hash_id == -1) croak("FATAL: find_hash failed for '%s'", mgf_hash_name);
519 9           rsa_oparams.prng = &self->pstate;
520 9           rsa_oparams.wprng = self->pindex;
521 9           rsa_oparams.padding = LTC_PKCS_1_PSS;
522 9           rsa_oparams.u.crypt.lparam = NULL;
523 9           rsa_oparams.u.crypt.lparamlen = 0;
524 9           rsa_oparams.params.hash_idx = hash_id;
525 9           rsa_oparams.params.mgf1_hash_idx = mgf_hash_id;
526 9           rsa_oparams.params.saltlen = saltlen;
527 9           rv = rsa_sign_hash_v2(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &rsa_oparams, &self->key);
528 9 50         if (rv != CRYPT_OK) croak("FATAL: rsa_sign_hash_v2 failed: %s", error_to_string(rv));
529 9           RETVAL = newSVpvn((char*)buffer, buffer_len);
530             }
531 0 0         else if (strnEQ(padding, "v1.5", 4)) {
532 0           hash_id = cryptx_internal_find_hash(hash_name);
533 0 0         if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
534 0           rsa_oparams.prng = &self->pstate;
535 0           rsa_oparams.wprng = self->pindex;
536 0           rsa_oparams.padding = LTC_PKCS_1_V1_5;
537 0           rsa_oparams.u.crypt.lparam = NULL;
538 0           rsa_oparams.u.crypt.lparamlen = 0;
539 0           rsa_oparams.params.hash_idx = hash_id;
540 0           rsa_oparams.params.mgf1_hash_idx = -1;
541 0           rsa_oparams.params.saltlen = 0;
542 0           rv = rsa_sign_hash_v2(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &rsa_oparams, &self->key);
543 0 0         if (rv != CRYPT_OK) croak("FATAL: rsa_sign_hash_v2 failed: %s", error_to_string(rv));
544 0           RETVAL = newSVpvn((char*)buffer, buffer_len);
545             }
546 0 0         else if (strnEQ(padding, "v1.5.na1", 8)) {
547 0           croak("FATAL: LTC_PKCS_1_V1_5_NA1 not implemented");
548             }
549 0 0         else if (strnEQ(padding, "none", 4)) {
550             /* raw RSA */
551 0           rv = ltc_mp.rsa_me(data_ptr, (unsigned long)data_len, buffer, &buffer_len, PK_PRIVATE, &self->key);
552 0 0         if (rv != CRYPT_OK) croak("FATAL: rsa_me failed: %s", error_to_string(rv));
553 0           RETVAL = newSVpvn((char*)buffer, buffer_len);
554             }
555             else {
556 0           croak("FATAL: rsa_sign invalid padding '%s'", padding);
557             }
558 9           zeromem(buffer, sizeof(buffer));
559 9           zeromem(tmp, sizeof(tmp));
560             }
561             OUTPUT:
562             RETVAL
563              
564             int
565             verify_hash(Crypt::PK::RSA self, SV * sig, SV * data, const char * hash_name = "SHA1", const char * padding = "pss", unsigned long saltlen = 12, const char * mgf_hash_name = NULL)
566             ALIAS:
567             verify_message = 1
568             CODE:
569             {
570             int rv, hash_id, mgf_hash_id, stat;
571 115           unsigned char tmp[MAXBLOCKSIZE], buffer[1024], *data_ptr = NULL, *sig_ptr = NULL;
572 115           unsigned long i, tmp_len = MAXBLOCKSIZE, buffer_len = 1024;
573 115           STRLEN data_len = 0, sig_len = 0;
574             ltc_rsa_op_parameters rsa_oparams;
575              
576 115           data_ptr = (unsigned char *)SvPVbyte(data, data_len);
577 115           sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len);
578 115           cryptx_internal_pk_prng_reseed(&self->pstate, self->pindex, &self->last_pid);
579 115 100         if (ix == 1) {
580 111           hash_id = cryptx_internal_find_hash(hash_name);
581 111 50         if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
582 111           rv = hash_memory(hash_id, data_ptr, (unsigned long)data_len, tmp, &tmp_len);
583 111 50         if (rv != CRYPT_OK) croak("FATAL: hash_memory failed: %s", error_to_string(rv));
584 111           data_ptr = tmp;
585 111           data_len = tmp_len;
586             }
587 115           RETVAL = 1;
588 115           stat = 0;
589 115 100         if (strnEQ(padding, "pss", 3)) {
590 10           hash_id = cryptx_internal_find_hash(hash_name);
591 10 50         if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
592 10 50         mgf_hash_id = mgf_hash_name ? cryptx_internal_find_hash(mgf_hash_name) : hash_id;
593 10 50         if (mgf_hash_id == -1) croak("FATAL: find_hash failed for '%s'", mgf_hash_name);
594 10           rsa_oparams.prng = &self->pstate;
595 10           rsa_oparams.wprng = self->pindex;
596 10           rsa_oparams.padding = LTC_PKCS_1_PSS;
597 10           rsa_oparams.u.crypt.lparam = NULL;
598 10           rsa_oparams.u.crypt.lparamlen = 0;
599 10           rsa_oparams.params.hash_idx = hash_id;
600 10           rsa_oparams.params.mgf1_hash_idx = mgf_hash_id;
601 10           rsa_oparams.params.saltlen = saltlen;
602 10           rv = rsa_verify_hash_v2(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &rsa_oparams, &stat, &self->key);
603 10 50         if (rv != CRYPT_OK || stat != 1) RETVAL = 0;
    100          
604             }
605 105 50         else if (strnEQ(padding, "v1.5", 4)) {
606 105           hash_id = cryptx_internal_find_hash(hash_name);
607 105 50         if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
608 105           rsa_oparams.prng = &self->pstate;
609 105           rsa_oparams.wprng = self->pindex;
610 105           rsa_oparams.padding = LTC_PKCS_1_V1_5;
611 105           rsa_oparams.u.crypt.lparam = NULL;
612 105           rsa_oparams.u.crypt.lparamlen = 0;
613 105           rsa_oparams.params.hash_idx = hash_id;
614 105           rsa_oparams.params.mgf1_hash_idx = -1;
615 105           rsa_oparams.params.saltlen = 0;
616 105           rv = rsa_verify_hash_v2(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &rsa_oparams, &stat, &self->key);
617 105 50         if (rv != CRYPT_OK || stat != 1) RETVAL = 0;
    100          
618             }
619 0 0         else if (strnEQ(padding, "v1.5.na1", 8)) {
620 0           croak("FATAL: LTC_PKCS_1_V1_5_NA1 not implemented");
621             }
622 0 0         else if (strnEQ(padding, "none", 4)) {
623             /* raw RSA */
624 0           Zero(buffer, buffer_len, unsigned char);
625 0           rv = ltc_mp.rsa_me(sig_ptr, (unsigned long)sig_len, buffer, &buffer_len, PK_PUBLIC, &self->key);
626 0 0         if (rv != CRYPT_OK) croak("FATAL: rsa_me failed: %s", error_to_string(rv));
627 0 0         if (data_len <= buffer_len && buffer_len > 0 && data_len > 0) {
    0          
    0          
628 0 0         for (i = 0; i < buffer_len - data_len; i++) if (buffer[i] != 0) RETVAL = 0;
    0          
629 0 0         if (memNE(data_ptr, buffer + buffer_len - data_len, data_len)) RETVAL = 0;
630             }
631             else {
632 0           RETVAL = 0;
633             }
634             }
635             else {
636 0           croak("FATAL: rsa_verify invalid padding '%s'", padding);
637             }
638             }
639             OUTPUT:
640             RETVAL
641              
642             void
643             DESTROY(Crypt::PK::RSA self)
644             CODE:
645 116 50         if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; }
646 116 50         if (self->pindex >= 0 && prng_is_valid(self->pindex) == CRYPT_OK && prng_descriptor[self->pindex].done) {
    50          
    50          
647 116           prng_descriptor[self->pindex].done(&self->pstate);
648             }
649 116           zeromem(self, sizeof(*self));
650 116           Safefree(self);