v0.030
Karel Miko
8 years ago
13 | 13 | - maybe: switch yarrow > fortuna for Crypt::PK::* |
14 | 14 | - maybe: add enc_b64/dec_b64 + enc_b64u/dec_b64u + enc_b32/dec_b32 |
15 | 15 | - maybe: x509_rsa_pubkey + x509_rsa_pubkey_alg |
16 | ||
17 | 0.030 2016/04/13 | |
18 | - fix: 0.029 + 0.028 by mistake installed *.inc files to perl/(lib|site|vendor) | |
16 | 19 | |
17 | 20 | 0.029 2016/04/13 |
18 | 21 | - NEW module: Math::BigInt::LTM |
388 | 388 | |
389 | 389 | ############################################################################### |
390 | 390 | |
391 | INCLUDE: lib/CryptX_Digest.xs.inc | |
392 | INCLUDE: lib/CryptX_Cipher.xs.inc | |
393 | ||
394 | INCLUDE: lib/CryptX_AuthEnc_EAX.xs.inc | |
395 | INCLUDE: lib/CryptX_AuthEnc_GCM.xs.inc | |
396 | INCLUDE: lib/CryptX_AuthEnc_OCB.xs.inc | |
397 | INCLUDE: lib/CryptX_AuthEnc_CCM.xs.inc | |
398 | ||
399 | INCLUDE: lib/CryptX_Mac_F9.xs.inc | |
400 | INCLUDE: lib/CryptX_Mac_HMAC.xs.inc | |
401 | INCLUDE: lib/CryptX_Mac_OMAC.xs.inc | |
402 | INCLUDE: lib/CryptX_Mac_Pelican.xs.inc | |
403 | INCLUDE: lib/CryptX_Mac_PMAC.xs.inc | |
404 | INCLUDE: lib/CryptX_Mac_XCBC.xs.inc | |
405 | ||
406 | INCLUDE: lib/CryptX_Mode_CBC.xs.inc | |
407 | INCLUDE: lib/CryptX_Mode_ECB.xs.inc | |
408 | INCLUDE: lib/CryptX_Mode_CFB.xs.inc | |
409 | INCLUDE: lib/CryptX_Mode_OFB.xs.inc | |
410 | INCLUDE: lib/CryptX_Mode_CTR.xs.inc | |
411 | #INCLUDE: lib/CryptX_Mode_F8.xs.inc | |
412 | #INCLUDE: lib/CryptX_Mode_LRW.xs.inc | |
413 | #INCLUDE: lib/CryptX_Mode_XTS.xs.inc | |
414 | ||
415 | INCLUDE: lib/CryptX_PRNG.xs.inc | |
416 | ||
417 | INCLUDE: lib/CryptX_PK_RSA.xs.inc | |
418 | INCLUDE: lib/CryptX_PK_DSA.xs.inc | |
419 | INCLUDE: lib/CryptX_PK_DH.xs.inc | |
420 | INCLUDE: lib/CryptX_PK_ECC.xs.inc | |
421 | ||
422 | INCLUDE: lib/CryptX_KeyDerivation.xs.inc | |
423 | ||
424 | INCLUDE: lib/CryptX_BigInt_LTM.xs.inc⏎ | |
391 | INCLUDE: inc/CryptX_Digest.xs.inc | |
392 | INCLUDE: inc/CryptX_Cipher.xs.inc | |
393 | ||
394 | INCLUDE: inc/CryptX_AuthEnc_EAX.xs.inc | |
395 | INCLUDE: inc/CryptX_AuthEnc_GCM.xs.inc | |
396 | INCLUDE: inc/CryptX_AuthEnc_OCB.xs.inc | |
397 | INCLUDE: inc/CryptX_AuthEnc_CCM.xs.inc | |
398 | ||
399 | INCLUDE: inc/CryptX_Mac_F9.xs.inc | |
400 | INCLUDE: inc/CryptX_Mac_HMAC.xs.inc | |
401 | INCLUDE: inc/CryptX_Mac_OMAC.xs.inc | |
402 | INCLUDE: inc/CryptX_Mac_Pelican.xs.inc | |
403 | INCLUDE: inc/CryptX_Mac_PMAC.xs.inc | |
404 | INCLUDE: inc/CryptX_Mac_XCBC.xs.inc | |
405 | ||
406 | INCLUDE: inc/CryptX_Mode_CBC.xs.inc | |
407 | INCLUDE: inc/CryptX_Mode_ECB.xs.inc | |
408 | INCLUDE: inc/CryptX_Mode_CFB.xs.inc | |
409 | INCLUDE: inc/CryptX_Mode_OFB.xs.inc | |
410 | INCLUDE: inc/CryptX_Mode_CTR.xs.inc | |
411 | #INCLUDE: inc/CryptX_Mode_F8.xs.inc | |
412 | #INCLUDE: inc/CryptX_Mode_LRW.xs.inc | |
413 | #INCLUDE: inc/CryptX_Mode_XTS.xs.inc | |
414 | ||
415 | INCLUDE: inc/CryptX_PRNG.xs.inc | |
416 | ||
417 | INCLUDE: inc/CryptX_PK_RSA.xs.inc | |
418 | INCLUDE: inc/CryptX_PK_DSA.xs.inc | |
419 | INCLUDE: inc/CryptX_PK_DH.xs.inc | |
420 | INCLUDE: inc/CryptX_PK_ECC.xs.inc | |
421 | ||
422 | INCLUDE: inc/CryptX_KeyDerivation.xs.inc | |
423 | ||
424 | INCLUDE: inc/CryptX_BigInt_LTM.xs.inc⏎ |
0 | MODULE = CryptX PACKAGE = Crypt::AuthEnc::CCM | |
1 | ||
2 | void | |
3 | _memory_encrypt(char *cipher_name, SV *key, SV *nonce, SV *header, unsigned long tag_len, SV *plaintext) | |
4 | PPCODE: | |
5 | { | |
6 | STRLEN k_len, n_len, h_len, pt_len; | |
7 | unsigned char *k, *n, *h, *pt; | |
8 | int rv, id; | |
9 | unsigned char tag[MAXBLOCKSIZE]; | |
10 | SV *ct; | |
11 | ||
12 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
13 | if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar"); | |
14 | if (!SvPOK(header)) croak("FATAL: header must be string/buffer scalar"); | |
15 | if (!SvPOK(plaintext)) croak("FATAL: plaintext must be string/buffer scalar"); | |
16 | k = (unsigned char *) SvPVbyte(key, k_len); | |
17 | n = (unsigned char *) SvPVbyte(nonce, n_len); | |
18 | h = (unsigned char *) SvPVbyte(header, h_len); | |
19 | pt = (unsigned char *) SvPVbyte(plaintext, pt_len); | |
20 | ||
21 | id = find_cipher(cipher_name); | |
22 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
23 | ||
24 | ct = NEWSV(0, pt_len); | |
25 | SvPOK_only(ct); | |
26 | SvCUR_set(ct, pt_len); | |
27 | ||
28 | if(tag_len<4 || tag_len>16) tag_len = 16; | |
29 | ||
30 | rv = ccm_memory(id, k, (unsigned long)k_len, NULL, n, (unsigned long)n_len, h, (unsigned long)h_len, | |
31 | pt, (unsigned long)pt_len, (unsigned char *)SvPV_nolen(ct), tag, &tag_len, CCM_ENCRYPT); | |
32 | if (rv != CRYPT_OK) croak("FATAL: ccm_memory failed: %s", error_to_string(rv)); | |
33 | ||
34 | XPUSHs(sv_2mortal(ct)); | |
35 | XPUSHs(sv_2mortal(newSVpvn((char*)tag,tag_len))); | |
36 | ||
37 | /* int ccm_memory( int cipher, | |
38 | const unsigned char *key, unsigned long keylen, | |
39 | symmetric_key *uskey, | |
40 | const unsigned char *nonce, unsigned long noncelen, | |
41 | const unsigned char *header, unsigned long headerlen, | |
42 | unsigned char *pt, unsigned long ptlen, | |
43 | unsigned char *ct, | |
44 | unsigned char *tag, unsigned long *taglen, | |
45 | int direction); */ | |
46 | ||
47 | } | |
48 | ||
49 | void | |
50 | _memory_decrypt(char *cipher_name, SV *key, SV *nonce, SV *header, SV *ciphertext, SV *tag) | |
51 | PPCODE: | |
52 | { | |
53 | STRLEN k_len, n_len, h_len, ct_len, t_len; | |
54 | unsigned char *k, *n, *h, *ct, *t; | |
55 | int rv, id; | |
56 | unsigned char xtag[MAXBLOCKSIZE]; | |
57 | unsigned long xtag_len; | |
58 | SV *pt; | |
59 | ||
60 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
61 | if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar"); | |
62 | if (!SvPOK(header)) croak("FATAL: header must be string/buffer scalar"); | |
63 | if (!SvPOK(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); | |
64 | if (!SvPOK(tag)) croak("FATAL: tag must be string/buffer scalar"); | |
65 | k = (unsigned char *) SvPVbyte(key, k_len); | |
66 | n = (unsigned char *) SvPVbyte(nonce, n_len); | |
67 | h = (unsigned char *) SvPVbyte(header, h_len); | |
68 | ct = (unsigned char *) SvPVbyte(ciphertext, ct_len); | |
69 | t = (unsigned char *) SvPVbyte(tag, t_len); | |
70 | ||
71 | id = find_cipher(cipher_name); | |
72 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
73 | ||
74 | pt = NEWSV(0, ct_len); | |
75 | SvPOK_only(pt); | |
76 | SvCUR_set(pt, ct_len); | |
77 | ||
78 | xtag_len = (unsigned long)t_len; | |
79 | Copy(t, xtag, t_len, unsigned char); | |
80 | ||
81 | rv = ccm_memory(id, k, (unsigned long)k_len, NULL, n, (unsigned long)n_len, h, (unsigned long)h_len, | |
82 | (unsigned char *)SvPV_nolen(pt), (unsigned long)ct_len, ct, xtag, &xtag_len, CCM_DECRYPT); | |
83 | if (rv != CRYPT_OK) { | |
84 | XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */ | |
85 | } | |
86 | else { | |
87 | XPUSHs(sv_2mortal(pt)); | |
88 | } | |
89 | } |
0 | MODULE = CryptX PACKAGE = Crypt::AuthEnc::EAX | |
1 | ||
2 | Crypt::AuthEnc::EAX | |
3 | _new(char * cipher_name, SV * key, SV * nonce, SV * header=&PL_sv_undef) | |
4 | CODE: | |
5 | { | |
6 | STRLEN k_len=0; | |
7 | unsigned char *k=NULL; | |
8 | unsigned char *n=NULL; | |
9 | STRLEN n_len=0; | |
10 | unsigned char *h=NULL; | |
11 | STRLEN h_len=0; | |
12 | int id; | |
13 | ||
14 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
15 | k = (unsigned char *) SvPVbyte(key, k_len); | |
16 | if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar"); | |
17 | n = (unsigned char *) SvPVbyte(nonce, n_len); | |
18 | if(SvOK(header)) { /* header is optional param */ | |
19 | if(!SvPOK(header)) croak("FATAL: header must be string/buffer scalar"); | |
20 | h = (unsigned char *) SvPVbyte(header, h_len); | |
21 | } | |
22 | ||
23 | id = find_cipher(cipher_name); | |
24 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
25 | ||
26 | Newz(0, RETVAL, 1, struct eax_struct); | |
27 | if (!RETVAL) croak("FATAL: Newz failed"); | |
28 | ||
29 | if (eax_init(&RETVAL->state, id, k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len) != CRYPT_OK) { | |
30 | croak("FATAL: eax setup failed"); | |
31 | } | |
32 | } | |
33 | OUTPUT: | |
34 | RETVAL | |
35 | ||
36 | void | |
37 | DESTROY(Crypt::AuthEnc::EAX self) | |
38 | CODE: | |
39 | Safefree(self); | |
40 | ||
41 | Crypt::AuthEnc::EAX | |
42 | clone(Crypt::AuthEnc::EAX self) | |
43 | CODE: | |
44 | Newz(0, RETVAL, 1, struct eax_struct); | |
45 | if (!RETVAL) croak("FATAL: Newz failed"); | |
46 | Copy(&self->state, &RETVAL->state, 1, struct eax_struct); | |
47 | OUTPUT: | |
48 | RETVAL | |
49 | ||
50 | SV * | |
51 | encrypt_add(Crypt::AuthEnc::EAX self, SV * data) | |
52 | CODE: | |
53 | { | |
54 | int rv; | |
55 | STRLEN in_data_len; | |
56 | unsigned char *in_data, *out_data; | |
57 | ||
58 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
59 | if (in_data_len==0) { | |
60 | RETVAL = newSVpvn("", 0); | |
61 | } | |
62 | else { | |
63 | RETVAL = NEWSV(0, in_data_len); | |
64 | SvPOK_only(RETVAL); | |
65 | SvCUR_set(RETVAL, in_data_len); | |
66 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
67 | rv = eax_encrypt(&self->state, in_data, out_data, (unsigned long)in_data_len); | |
68 | if (rv != CRYPT_OK) croak("FATAL: eax_encrypt failed: %s", error_to_string(rv)); | |
69 | } | |
70 | } | |
71 | OUTPUT: | |
72 | RETVAL | |
73 | ||
74 | SV * | |
75 | decrypt_add(Crypt::AuthEnc::EAX self, SV * data) | |
76 | CODE: | |
77 | { | |
78 | int rv; | |
79 | STRLEN in_data_len; | |
80 | unsigned char *in_data, *out_data; | |
81 | ||
82 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
83 | if (in_data_len==0) { | |
84 | RETVAL = newSVpvn("", 0); | |
85 | } | |
86 | else { | |
87 | RETVAL = NEWSV(0, in_data_len); | |
88 | SvPOK_only(RETVAL); | |
89 | SvCUR_set(RETVAL, in_data_len); | |
90 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
91 | rv = eax_decrypt(&self->state, in_data, out_data, (unsigned long)in_data_len); | |
92 | if (rv != CRYPT_OK) croak("FATAL: eax_decrypt failed: %s", error_to_string(rv)); | |
93 | } | |
94 | } | |
95 | OUTPUT: | |
96 | RETVAL | |
97 | ||
98 | void | |
99 | encrypt_done(Crypt::AuthEnc::EAX self) | |
100 | PPCODE: | |
101 | { | |
102 | int rv; | |
103 | unsigned char tag[MAXBLOCKSIZE]; | |
104 | unsigned long tag_len = sizeof(tag); | |
105 | ||
106 | rv = eax_done(&self->state, tag, &tag_len); | |
107 | if (rv != CRYPT_OK) croak("FATAL: eax_done failed: %s", error_to_string(rv)); | |
108 | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); | |
109 | } | |
110 | ||
111 | void | |
112 | decrypt_done(Crypt::AuthEnc::EAX self, ...) | |
113 | PPCODE: | |
114 | { | |
115 | int rv; | |
116 | unsigned char tag[MAXBLOCKSIZE]; | |
117 | unsigned long tag_len = sizeof(tag); | |
118 | STRLEN expected_tag_len; | |
119 | unsigned char *expected_tag; | |
120 | ||
121 | rv = eax_done(&self->state, tag, &tag_len); | |
122 | if (rv != CRYPT_OK) croak("FATAL: eax_done failed: %s", error_to_string(rv)); | |
123 | if (items == 1) { | |
124 | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); | |
125 | } | |
126 | else { | |
127 | if(!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar"); | |
128 | expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len); | |
129 | if (expected_tag_len!=tag_len) { | |
130 | XPUSHs(sv_2mortal(newSViv(0))); /* false */ | |
131 | } | |
132 | else if (memNE(expected_tag, tag, tag_len)) { | |
133 | XPUSHs(sv_2mortal(newSViv(0))); /* false */ | |
134 | } | |
135 | else { | |
136 | XPUSHs(sv_2mortal(newSViv(1))); /* true */ | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
141 | int | |
142 | header_add(Crypt::AuthEnc::EAX self, SV * header) | |
143 | CODE: | |
144 | { | |
145 | STRLEN h_len; | |
146 | unsigned char *h; | |
147 | h = (unsigned char *)SvPVbyte(header, h_len); | |
148 | RETVAL = eax_addheader(&self->state, h, (unsigned long)h_len); | |
149 | } | |
150 | OUTPUT: | |
151 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::AuthEnc::GCM | |
1 | ||
2 | Crypt::AuthEnc::GCM | |
3 | _new(char * cipher_name, SV * key) | |
4 | CODE: | |
5 | { | |
6 | STRLEN k_len=0; | |
7 | unsigned char *k=NULL; | |
8 | int id; | |
9 | ||
10 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
11 | k = (unsigned char *) SvPVbyte(key, k_len); | |
12 | ||
13 | ||
14 | id = find_cipher(cipher_name); | |
15 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
16 | ||
17 | Newz(0, RETVAL, 1, struct gcm_struct); | |
18 | if (!RETVAL) croak("FATAL: Newz failed"); | |
19 | ||
20 | if (gcm_init(&RETVAL->state, id, k, (unsigned long)k_len) != CRYPT_OK) { | |
21 | croak("FATAL: gcm setup failed"); | |
22 | } | |
23 | } | |
24 | OUTPUT: | |
25 | RETVAL | |
26 | ||
27 | void | |
28 | DESTROY(Crypt::AuthEnc::GCM self) | |
29 | CODE: | |
30 | Safefree(self); | |
31 | ||
32 | Crypt::AuthEnc::GCM | |
33 | clone(Crypt::AuthEnc::GCM self) | |
34 | CODE: | |
35 | Newz(0, RETVAL, 1, struct gcm_struct); | |
36 | if (!RETVAL) croak("FATAL: Newz failed"); | |
37 | Copy(&self->state, &RETVAL->state, 1, struct gcm_struct); | |
38 | OUTPUT: | |
39 | RETVAL | |
40 | ||
41 | int | |
42 | reset(Crypt::AuthEnc::GCM self) | |
43 | CODE: | |
44 | { | |
45 | int rv; | |
46 | rv = gcm_reset(&self->state); | |
47 | if (rv != CRYPT_OK) croak("FATAL: gcm_reset failed: %s", error_to_string(rv)); | |
48 | RETVAL = rv; | |
49 | } | |
50 | OUTPUT: | |
51 | RETVAL | |
52 | ||
53 | SV * | |
54 | encrypt_add(Crypt::AuthEnc::GCM self, SV * data) | |
55 | CODE: | |
56 | { | |
57 | int rv; | |
58 | STRLEN in_data_len; | |
59 | unsigned char *in_data, *out_data; | |
60 | ||
61 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
62 | if (in_data_len==0) { | |
63 | RETVAL = newSVpvn("", 0); | |
64 | } | |
65 | else | |
66 | { | |
67 | RETVAL = NEWSV(0, in_data_len); | |
68 | SvPOK_only(RETVAL); | |
69 | SvCUR_set(RETVAL, in_data_len); | |
70 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
71 | rv = gcm_process(&self->state, in_data, (unsigned long)in_data_len, out_data, GCM_ENCRYPT); | |
72 | if (rv != CRYPT_OK) croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv)); | |
73 | } | |
74 | } | |
75 | OUTPUT: | |
76 | RETVAL | |
77 | ||
78 | int | |
79 | iv_add(Crypt::AuthEnc::GCM self, SV * data) | |
80 | CODE: | |
81 | { | |
82 | int rv; | |
83 | STRLEN in_data_len; | |
84 | unsigned char *in_data; | |
85 | ||
86 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
87 | rv = gcm_add_iv(&self->state, in_data, (unsigned long)in_data_len); | |
88 | if (rv != CRYPT_OK) croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv)); | |
89 | RETVAL = rv; | |
90 | } | |
91 | OUTPUT: | |
92 | RETVAL | |
93 | ||
94 | int | |
95 | adata_add(Crypt::AuthEnc::GCM self, SV * data) | |
96 | CODE: | |
97 | { | |
98 | int rv; | |
99 | STRLEN in_data_len; | |
100 | unsigned char *in_data; | |
101 | ||
102 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
103 | rv = gcm_add_aad(&self->state, in_data, (unsigned long)in_data_len); | |
104 | if (rv != CRYPT_OK) croak("FATAL: gcm_add_aad failed: %s", error_to_string(rv)); | |
105 | RETVAL = rv; | |
106 | } | |
107 | OUTPUT: | |
108 | RETVAL | |
109 | ||
110 | SV * | |
111 | decrypt_add(Crypt::AuthEnc::GCM self, SV * data) | |
112 | CODE: | |
113 | { | |
114 | int rv; | |
115 | STRLEN in_data_len; | |
116 | unsigned char *in_data, *out_data; | |
117 | ||
118 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
119 | if (in_data_len==0) { | |
120 | RETVAL = newSVpvn("", 0); | |
121 | } | |
122 | else { | |
123 | RETVAL = NEWSV(0, in_data_len); | |
124 | SvPOK_only(RETVAL); | |
125 | SvCUR_set(RETVAL, in_data_len); | |
126 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
127 | rv = gcm_process(&self->state, out_data, (unsigned long)in_data_len, in_data, GCM_DECRYPT); | |
128 | if (rv != CRYPT_OK) croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv)); | |
129 | } | |
130 | } | |
131 | OUTPUT: | |
132 | RETVAL | |
133 | ||
134 | ||
135 | void | |
136 | encrypt_done(Crypt::AuthEnc::GCM self) | |
137 | PPCODE: | |
138 | { | |
139 | int rv; | |
140 | unsigned char tag[MAXBLOCKSIZE]; | |
141 | unsigned long tag_len = sizeof(tag); | |
142 | ||
143 | rv = gcm_done(&self->state, tag, &tag_len); | |
144 | if (rv != CRYPT_OK) croak("FATAL: gcm_done failed: %s", error_to_string(rv)); | |
145 | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); | |
146 | } | |
147 | ||
148 | void | |
149 | decrypt_done(Crypt::AuthEnc::GCM self, ...) | |
150 | PPCODE: | |
151 | { | |
152 | int rv; | |
153 | unsigned char tag[MAXBLOCKSIZE]; | |
154 | unsigned long tag_len = sizeof(tag); | |
155 | STRLEN expected_tag_len; | |
156 | unsigned char *expected_tag; | |
157 | ||
158 | rv = gcm_done(&self->state, tag, &tag_len); | |
159 | if (rv != CRYPT_OK) croak("FATAL: gcm_done failed: %s", error_to_string(rv)); | |
160 | if (items == 1) { | |
161 | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); | |
162 | } | |
163 | else { | |
164 | if(!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar"); | |
165 | expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len); | |
166 | if (expected_tag_len!=tag_len) { | |
167 | XPUSHs(sv_2mortal(newSViv(0))); /* false */ | |
168 | } | |
169 | else if (memNE(expected_tag, tag, tag_len)) { | |
170 | XPUSHs(sv_2mortal(newSViv(0))); /* false */ | |
171 | } | |
172 | else { | |
173 | XPUSHs(sv_2mortal(newSViv(1))); /* true */ | |
174 | } | |
175 | } | |
176 | } |
0 | MODULE = CryptX PACKAGE = Crypt::AuthEnc::OCB | |
1 | ||
2 | Crypt::AuthEnc::OCB | |
3 | _new(char * cipher_name, SV * key, SV * nonce) | |
4 | CODE: | |
5 | { | |
6 | STRLEN k_len=0; | |
7 | unsigned char *k=NULL; | |
8 | unsigned char *n=NULL; | |
9 | STRLEN n_len=0; | |
10 | int id; | |
11 | ||
12 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
13 | k = (unsigned char *) SvPVbyte(key, k_len); | |
14 | if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar"); | |
15 | n = (unsigned char *) SvPVbyte(nonce, n_len); | |
16 | ||
17 | id = find_cipher(cipher_name); | |
18 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
19 | ||
20 | Newz(0, RETVAL, 1, struct ocb_struct); | |
21 | if (!RETVAL) croak("FATAL: Newz failed"); | |
22 | ||
23 | if (ocb3_init(&RETVAL->state, id, k, (unsigned long)k_len, n, (unsigned long)n_len) != CRYPT_OK) { | |
24 | croak("FATAL: ocb setup failed"); | |
25 | } | |
26 | } | |
27 | OUTPUT: | |
28 | RETVAL | |
29 | ||
30 | void | |
31 | DESTROY(Crypt::AuthEnc::OCB self) | |
32 | CODE: | |
33 | Safefree(self); | |
34 | ||
35 | Crypt::AuthEnc::OCB | |
36 | clone(Crypt::AuthEnc::OCB self) | |
37 | CODE: | |
38 | Newz(0, RETVAL, 1, struct ocb_struct); | |
39 | if (!RETVAL) croak("FATAL: Newz failed"); | |
40 | Copy(&self->state, &RETVAL->state, 1, struct ocb_struct); | |
41 | OUTPUT: | |
42 | RETVAL | |
43 | ||
44 | void | |
45 | adata_add(Crypt::AuthEnc::OCB self, SV * data) | |
46 | CODE: | |
47 | { | |
48 | int rv; | |
49 | STRLEN in_data_len; | |
50 | unsigned char *in_data; | |
51 | ||
52 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
53 | ||
54 | if (in_data_len>0) { | |
55 | rv = ocb3_add_aad(&self->state, in_data, (unsigned long)in_data_len); | |
56 | if (rv != CRYPT_OK) croak("FATAL: ocb3_add_aad failed: %s", error_to_string(rv)); | |
57 | } | |
58 | } | |
59 | ||
60 | SV * | |
61 | encrypt_add(Crypt::AuthEnc::OCB self, SV * data) | |
62 | CODE: | |
63 | { | |
64 | int rv; | |
65 | STRLEN in_data_len; | |
66 | unsigned char *in_data, *out_data; | |
67 | ||
68 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
69 | if (in_data_len==0) { | |
70 | RETVAL = newSVpvn("", 0); | |
71 | } | |
72 | else { | |
73 | RETVAL = NEWSV(0, in_data_len); | |
74 | SvPOK_only(RETVAL); | |
75 | SvCUR_set(RETVAL, in_data_len); | |
76 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
77 | ||
78 | if (in_data_len % (&self->state)->block_len) | |
79 | croak ("FATAL: sizeof(data) should be multiple of blocksize (%d)", (&self->state)->block_len); | |
80 | ||
81 | rv = ocb3_encrypt(&self->state, in_data, (unsigned long)in_data_len, out_data); | |
82 | if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt failed: %s", error_to_string(rv)); | |
83 | } | |
84 | } | |
85 | OUTPUT: | |
86 | RETVAL | |
87 | ||
88 | SV * | |
89 | encrypt_last(Crypt::AuthEnc::OCB self, SV * data) | |
90 | CODE: | |
91 | { | |
92 | int rv; | |
93 | STRLEN in_data_len; | |
94 | unsigned char *in_data, *out_data; | |
95 | ||
96 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
97 | if (in_data_len>0) { | |
98 | RETVAL = NEWSV(0, in_data_len); | |
99 | SvPOK_only(RETVAL); | |
100 | SvCUR_set(RETVAL, in_data_len); | |
101 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
102 | } | |
103 | else { | |
104 | RETVAL = newSVpvn("", 0); | |
105 | out_data = NULL; | |
106 | } | |
107 | rv = ocb3_encrypt_last(&self->state, in_data, (unsigned long)in_data_len, out_data); | |
108 | if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv)); | |
109 | } | |
110 | OUTPUT: | |
111 | RETVAL | |
112 | ||
113 | SV * | |
114 | decrypt_add(Crypt::AuthEnc::OCB self, SV * data) | |
115 | CODE: | |
116 | { | |
117 | int rv; | |
118 | STRLEN in_data_len; | |
119 | unsigned char *in_data, *out_data; | |
120 | ||
121 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
122 | if (in_data_len==0) { | |
123 | RETVAL = newSVpvn("", 0); | |
124 | } | |
125 | else { | |
126 | RETVAL = NEWSV(0, in_data_len); | |
127 | SvPOK_only(RETVAL); | |
128 | SvCUR_set(RETVAL, in_data_len); | |
129 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
130 | ||
131 | if (in_data_len % (&self->state)->block_len) | |
132 | croak ("FATAL: sizeof(data) should be multiple of blocksize (%d)", (&self->state)->block_len); | |
133 | ||
134 | rv = ocb3_decrypt(&self->state, in_data, (unsigned long)in_data_len, out_data); | |
135 | if (rv != CRYPT_OK) croak("FATAL: ocb3_decrypt failed: %s", error_to_string(rv)); | |
136 | } | |
137 | } | |
138 | OUTPUT: | |
139 | RETVAL | |
140 | ||
141 | SV * | |
142 | decrypt_last(Crypt::AuthEnc::OCB self, SV * data) | |
143 | CODE: | |
144 | { | |
145 | int rv; | |
146 | STRLEN in_data_len; | |
147 | unsigned char *in_data, *out_data; | |
148 | ||
149 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
150 | if (in_data_len>0) { | |
151 | RETVAL = NEWSV(0, in_data_len); | |
152 | SvPOK_only(RETVAL); | |
153 | SvCUR_set(RETVAL, in_data_len); | |
154 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
155 | } | |
156 | else { | |
157 | RETVAL = newSVpvn("", 0); | |
158 | out_data = NULL; | |
159 | } | |
160 | rv = ocb3_decrypt_last(&self->state, in_data, (unsigned long)in_data_len, out_data); | |
161 | if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv)); | |
162 | } | |
163 | OUTPUT: | |
164 | RETVAL | |
165 | ||
166 | void | |
167 | encrypt_done(Crypt::AuthEnc::OCB self) | |
168 | PPCODE: | |
169 | { | |
170 | int rv; | |
171 | unsigned char tag[MAXBLOCKSIZE]; | |
172 | unsigned long tag_len = sizeof(tag); | |
173 | ||
174 | rv = ocb3_done(&self->state, tag, &tag_len); | |
175 | if (rv != CRYPT_OK) croak("FATAL: ocb3_done_encrypt failed: %s", error_to_string(rv)); | |
176 | ||
177 | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); | |
178 | } | |
179 | ||
180 | void | |
181 | decrypt_done(Crypt::AuthEnc::OCB self, ...) | |
182 | PPCODE: | |
183 | { | |
184 | int rv; | |
185 | unsigned char tag[MAXBLOCKSIZE]; | |
186 | unsigned long tag_len = sizeof(tag); | |
187 | STRLEN expected_tag_len; | |
188 | unsigned char *expected_tag; | |
189 | ||
190 | rv = ocb3_done(&self->state, tag, &tag_len); | |
191 | if (rv != CRYPT_OK) croak("FATAL: ocb3_done_decrypt failed: %s", error_to_string(rv)); | |
192 | if (items == 1) { | |
193 | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); | |
194 | } | |
195 | else { | |
196 | if(!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar"); | |
197 | expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len); | |
198 | if (expected_tag_len!=tag_len) { | |
199 | XPUSHs(sv_2mortal(newSViv(0))); /* false */ | |
200 | } | |
201 | else if (memNE(expected_tag, tag, tag_len)) { | |
202 | XPUSHs(sv_2mortal(newSViv(0))); /* false */ | |
203 | } | |
204 | else { | |
205 | XPUSHs(sv_2mortal(newSViv(1))); /* true */ | |
206 | } | |
207 | } | |
208 | } | |
209 | ||
210 | int | |
211 | blocksize(Crypt::AuthEnc::OCB self) | |
212 | CODE: | |
213 | { | |
214 | RETVAL = (&self->state)->block_len; | |
215 | } | |
216 | OUTPUT: | |
217 | RETVAL |
0 | MODULE = CryptX PACKAGE = Math::BigInt::LTM | |
1 | ||
2 | ||
3 | ############################################################################## | |
4 | # _new() | |
5 | ||
6 | Math::BigInt::LTM | |
7 | _new(Class, SV *x) | |
8 | CODE: | |
9 | Newz(0, RETVAL, 1, mp_int); | |
10 | mp_init(RETVAL); | |
11 | if ((SvUOK(x) || SvIOK(x)) && (sizeof(UV) <= sizeof(unsigned long) || SvUV(x) == (unsigned long)SvUV(x))) { | |
12 | mp_set_int(RETVAL, (unsigned long)SvUV(x)); | |
13 | } | |
14 | else { | |
15 | mp_read_radix(RETVAL, SvPV_nolen(x), 10); | |
16 | } | |
17 | OUTPUT: | |
18 | RETVAL | |
19 | ||
20 | ############################################################################## | |
21 | # _from_bin() | |
22 | ||
23 | Math::BigInt::LTM | |
24 | _from_bin(Class, SV *x) | |
25 | PREINIT: | |
26 | char *str, *start; | |
27 | CODE: | |
28 | Newz(0, RETVAL, 1, mp_int); | |
29 | mp_init(RETVAL); | |
30 | str = SvPV_nolen(x); | |
31 | start = (strlen(str)>2 && str[0] == '0' && str[1] == 'b') ? str+2 : str; | |
32 | mp_read_radix(RETVAL, start, 2); | |
33 | OUTPUT: | |
34 | RETVAL | |
35 | ||
36 | ############################################################################## | |
37 | # _from_hex() | |
38 | ||
39 | Math::BigInt::LTM | |
40 | _from_hex(Class, SV *x) | |
41 | PREINIT: | |
42 | char *str, *start; | |
43 | CODE: | |
44 | Newz(0, RETVAL, 1, mp_int); | |
45 | mp_init(RETVAL); | |
46 | str = SvPV_nolen(x); | |
47 | start = (strlen(str)>2 && str[0] == '0' && str[1] == 'x') ? str+2 : str; | |
48 | mp_read_radix(RETVAL, start, 16); | |
49 | OUTPUT: | |
50 | RETVAL | |
51 | ||
52 | ############################################################################## | |
53 | # _from_oct() | |
54 | ||
55 | Math::BigInt::LTM | |
56 | _from_oct(Class, SV *x) | |
57 | CODE: | |
58 | Newz(0, RETVAL, 1, mp_int); | |
59 | mp_init(RETVAL); | |
60 | mp_read_radix(RETVAL, SvPV_nolen(x), 8); | |
61 | OUTPUT: | |
62 | RETVAL | |
63 | ||
64 | ############################################################################## | |
65 | # _set() - set an already existing object to the given scalar value | |
66 | ||
67 | void | |
68 | _set(Class, Math::BigInt::LTM n, SV *x) | |
69 | CODE: | |
70 | mp_set_int(n, SvIV(x)); | |
71 | ||
72 | ############################################################################## | |
73 | # _zero() | |
74 | ||
75 | Math::BigInt::LTM | |
76 | _zero(Class) | |
77 | CODE: | |
78 | Newz(0, RETVAL, 1, mp_int); | |
79 | mp_init(RETVAL); | |
80 | mp_set_int(RETVAL, 0); | |
81 | OUTPUT: | |
82 | RETVAL | |
83 | ||
84 | ############################################################################## | |
85 | # _one() | |
86 | ||
87 | Math::BigInt::LTM | |
88 | _one(Class) | |
89 | CODE: | |
90 | Newz(0, RETVAL, 1, mp_int); | |
91 | mp_init(RETVAL); | |
92 | mp_set_int(RETVAL, 1); | |
93 | OUTPUT: | |
94 | RETVAL | |
95 | ||
96 | ############################################################################## | |
97 | # _two() | |
98 | ||
99 | Math::BigInt::LTM | |
100 | _two(Class) | |
101 | CODE: | |
102 | Newz(0, RETVAL, 1, mp_int); | |
103 | mp_init(RETVAL); | |
104 | mp_set_int(RETVAL, 2); | |
105 | OUTPUT: | |
106 | RETVAL | |
107 | ||
108 | ############################################################################## | |
109 | # _ten() | |
110 | ||
111 | Math::BigInt::LTM | |
112 | _ten(Class) | |
113 | CODE: | |
114 | Newz(0, RETVAL, 1, mp_int); | |
115 | mp_init(RETVAL); | |
116 | mp_set_int(RETVAL, 10); | |
117 | OUTPUT: | |
118 | RETVAL | |
119 | ||
120 | ############################################################################## | |
121 | # _1ex() | |
122 | ||
123 | Math::BigInt::LTM | |
124 | _1ex(Class, int x) | |
125 | CODE: | |
126 | Newz(0, RETVAL, 1, mp_int); | |
127 | mp_init(RETVAL); | |
128 | mp_set_int(RETVAL, 10); | |
129 | mp_expt_d(RETVAL, x, RETVAL); | |
130 | OUTPUT: | |
131 | RETVAL | |
132 | ||
133 | ############################################################################## | |
134 | # DESTROY() - free memory of a GMP number | |
135 | ||
136 | void | |
137 | DESTROY(Math::BigInt::LTM n) | |
138 | PPCODE: | |
139 | if (n) { | |
140 | mp_clear(n); | |
141 | Safefree(n); | |
142 | } | |
143 | ||
144 | ############################################################################## | |
145 | # _str() - return string so that atof() and atoi() can use it | |
146 | ||
147 | SV * | |
148 | _str(Class, Math::BigInt::LTM n) | |
149 | PREINIT: | |
150 | int len; | |
151 | char *buf; | |
152 | CODE: | |
153 | if (mp_iszero(n) == MP_YES) { | |
154 | RETVAL = newSVpv("0", 0); | |
155 | } | |
156 | else { | |
157 | len = mp_unsigned_bin_size(n) * 3 + 2; /* decimal size ~ 3 * bin size +1 for sign +1 for NUL-byte */ | |
158 | Newz(0, buf, len, char); | |
159 | mp_toradix_n(n, buf, 10, len); | |
160 | RETVAL = newSVpv(buf, 0); | |
161 | Safefree(buf); | |
162 | } | |
163 | OUTPUT: | |
164 | RETVAL | |
165 | ||
166 | ############################################################################## | |
167 | # _len() - return the length of the number in base 10 (costly) | |
168 | ||
169 | int | |
170 | _len(Class, Math::BigInt::LTM n) | |
171 | PREINIT: | |
172 | int len; | |
173 | char *buf; | |
174 | CODE: | |
175 | if (mp_iszero(n) == MP_YES) { | |
176 | RETVAL = 1; | |
177 | } | |
178 | else { | |
179 | len = mp_unsigned_bin_size(n) * 3 + 2; /* decimal size ~ 3 * bin size +1 for sign +1 for NUL-byte */ | |
180 | Newz(0, buf, len, char); | |
181 | mp_toradix_n(n, buf, 10, len); | |
182 | RETVAL = strlen(buf); | |
183 | Safefree(buf); | |
184 | } | |
185 | OUTPUT: | |
186 | RETVAL | |
187 | ||
188 | ############################################################################## | |
189 | # _alen() - return the approx. length of the number in base 10 (fast) | |
190 | ||
191 | int | |
192 | _alen(Class, Math::BigInt::LTM n) | |
193 | CODE: | |
194 | RETVAL = mp_unsigned_bin_size(n) * 3 + 2; /* decimal size ~ 3 * bin size XXX-FIXME very bad estimate */ | |
195 | OUTPUT: | |
196 | RETVAL | |
197 | ||
198 | ############################################################################## | |
199 | # _zeros() - return number of trailing zeros (in decimal form) | |
200 | ||
201 | int | |
202 | _zeros(Class, Math::BigInt::LTM n) | |
203 | PREINIT: | |
204 | int len; | |
205 | char *buf; | |
206 | CODE: | |
207 | if (mp_iszero(n) == MP_YES) { | |
208 | RETVAL = 0; /* '0' has no trailing zeros! */ | |
209 | } | |
210 | else { | |
211 | len = mp_unsigned_bin_size(n) * 3 + 2; /* decimal size ~ 3 * bin size +1 for sign +1 for NUL-byte */ | |
212 | Newz(0, buf, len, char); | |
213 | mp_toradix_n(n, buf, 10, len); | |
214 | len = strlen(buf); | |
215 | RETVAL = 0; | |
216 | while (len > 0) { | |
217 | if (buf[len-1] != '0') break; | |
218 | RETVAL++; | |
219 | len--; | |
220 | } | |
221 | Safefree(buf); | |
222 | } | |
223 | OUTPUT: | |
224 | RETVAL | |
225 | ||
226 | ############################################################################## | |
227 | # _as_hex() - return ref to hexadecimal string (prefixed with 0x) | |
228 | ||
229 | SV * | |
230 | _as_hex(Class, Math::BigInt::LTM n) | |
231 | PREINIT: | |
232 | int i, len; | |
233 | char *buf; | |
234 | CODE: | |
235 | len = mp_unsigned_bin_size(n) * 2 + 3; | |
236 | RETVAL = newSV(len); | |
237 | SvPOK_on(RETVAL); | |
238 | buf = SvPVX(RETVAL); /* get ptr to storage */ | |
239 | *buf++ = '0'; *buf++ = 'x'; /* prepend '0x' */ | |
240 | mp_tohex(n, buf); | |
241 | for (i=0; i<len && buf[i]>0; i++) buf[i] = toLOWER(buf[i]); | |
242 | SvCUR_set(RETVAL, strlen(buf)+2); /* set real length */ | |
243 | OUTPUT: | |
244 | RETVAL | |
245 | ||
246 | ############################################################################## | |
247 | # _as_bin() - return ref to binary string (prefixed with 0b) | |
248 | ||
249 | SV * | |
250 | _as_bin(Class, Math::BigInt::LTM n) | |
251 | PREINIT: | |
252 | int len; | |
253 | char *buf; | |
254 | CODE: | |
255 | len = mp_unsigned_bin_size(n) * 8 + 3; | |
256 | RETVAL = newSV(len); | |
257 | SvPOK_on(RETVAL); | |
258 | buf = SvPVX(RETVAL); /* get ptr to storage */ | |
259 | *buf++ = '0'; *buf++ = 'b'; /* prepend '0b' */ | |
260 | mp_tobinary(n, buf); | |
261 | SvCUR_set(RETVAL, strlen(buf)+2); /* set real length */ | |
262 | OUTPUT: | |
263 | RETVAL | |
264 | ||
265 | ############################################################################## | |
266 | # _as_oct() - return ref to octal string (prefixed with 0) | |
267 | ||
268 | SV * | |
269 | _as_oct(Class, Math::BigInt::LTM n) | |
270 | PREINIT: | |
271 | int len; | |
272 | char *buf; | |
273 | CODE: | |
274 | len = mp_unsigned_bin_size(n) * 3 + 3; | |
275 | RETVAL = newSV(len); | |
276 | SvPOK_on(RETVAL); | |
277 | buf = SvPVX(RETVAL); | |
278 | *buf++ = '0'; /* prepend '0' */ | |
279 | mp_tooctal(n, buf); | |
280 | SvCUR_set(RETVAL, strlen(buf)+1); /* set real length */ | |
281 | OUTPUT: | |
282 | RETVAL | |
283 | ||
284 | ############################################################################## | |
285 | # _modpow() - ($n ** $exp) % $mod | |
286 | ||
287 | Math::BigInt::LTM | |
288 | _modpow(Class, Math::BigInt::LTM n, Math::BigInt::LTM exp, Math::BigInt::LTM mod) | |
289 | CODE: | |
290 | Newz(0, RETVAL, 1, mp_int); | |
291 | mp_init(RETVAL); | |
292 | if (mp_cmp_d(mod, 1) == MP_EQ) { | |
293 | mp_set_int(RETVAL, 0); | |
294 | } | |
295 | else { | |
296 | mp_exptmod(n, exp, mod, RETVAL); | |
297 | } | |
298 | OUTPUT: | |
299 | RETVAL | |
300 | ||
301 | ############################################################################## | |
302 | # _modinv() - compute the inverse of x % y | |
303 | ||
304 | void | |
305 | _modinv(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
306 | PREINIT: | |
307 | int rc, sign; | |
308 | SV* s; | |
309 | mp_int* RETVAL; | |
310 | PPCODE: | |
311 | Newz(0, RETVAL, 1, mp_int); | |
312 | mp_init(RETVAL); | |
313 | rc = mp_invmod(x, y, RETVAL); | |
314 | EXTEND(SP, 2); /* we return two values */ | |
315 | if (rc != MP_OKAY) { | |
316 | /* Inverse doesn't exist. Return both values undefined. */ | |
317 | PUSHs(&PL_sv_undef); | |
318 | PUSHs(&PL_sv_undef); | |
319 | } | |
320 | else { | |
321 | /* Inverse exists. When the modulus to mp_invert() is positive, | |
322 | * the returned value is also positive. */ | |
323 | PUSHs(sv_2mortal(sv_from_mpi(RETVAL))); | |
324 | s = sv_newmortal(); | |
325 | sv_setpvn(s, "+", 1); | |
326 | PUSHs(s); | |
327 | } | |
328 | ||
329 | ############################################################################## | |
330 | # _add() - add $y to $x in place | |
331 | ||
332 | void | |
333 | _add(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
334 | PPCODE: | |
335 | mp_add(x, y, x); | |
336 | XPUSHs(ST(1)); /* x */ | |
337 | ||
338 | ############################################################################## | |
339 | # _inc() - modify x inline by doing x++ | |
340 | ||
341 | void | |
342 | _inc(Class, Math::BigInt::LTM x) | |
343 | PPCODE: | |
344 | mp_add_d(x, 1, x); | |
345 | XPUSHs(ST(1)); /* x */ | |
346 | ||
347 | ############################################################################## | |
348 | # _dec() - modify x inline by doing x-- | |
349 | ||
350 | void | |
351 | _dec(Class, Math::BigInt::LTM x) | |
352 | PPCODE: | |
353 | mp_sub_d(x, 1, x); | |
354 | XPUSHs(ST(1)); /* x */ | |
355 | ||
356 | ############################################################################## | |
357 | # _sub() - $x - $y | |
358 | # $x is always larger than $y! So overflow/underflow can not happen here. | |
359 | ||
360 | void | |
361 | _sub(Class, Math::BigInt::LTM x, Math::BigInt::LTM y, ...) | |
362 | PPCODE: | |
363 | if ( items == 4 && SvTRUE(ST(3)) ) { | |
364 | /* y -= x */ | |
365 | mp_sub(x, y, y); | |
366 | XPUSHs(ST(2)); /* y */ | |
367 | } | |
368 | else { | |
369 | /* x -= y */ | |
370 | mp_sub(x, y, x); | |
371 | XPUSHs(ST(1)); /* x */ | |
372 | } | |
373 | ||
374 | ############################################################################## | |
375 | # _rsft() | |
376 | ||
377 | void | |
378 | _rsft(Class, Math::BigInt::LTM x, Math::BigInt::LTM y, unsigned long base_int) | |
379 | PREINIT: | |
380 | mp_int* BASE; | |
381 | PPCODE: | |
382 | Newz(0, BASE, 1, mp_int); | |
383 | mp_init_set_int(BASE, base_int); | |
384 | mp_expt_d(BASE, mp_get_long(y), BASE); | |
385 | mp_div(x, BASE, x, NULL); | |
386 | mp_clear(BASE); | |
387 | Safefree(BASE); | |
388 | XPUSHs(ST(1)); /* x */ | |
389 | ||
390 | ############################################################################## | |
391 | # _lsft() | |
392 | ||
393 | void | |
394 | _lsft(Class, Math::BigInt::LTM x, Math::BigInt::LTM y, unsigned long base_int) | |
395 | PREINIT: | |
396 | mp_int* BASE; | |
397 | PPCODE: | |
398 | Newz(0, BASE, 1, mp_int); | |
399 | mp_init_set_int(BASE, base_int); | |
400 | mp_expt_d(BASE, mp_get_long(y), BASE); | |
401 | mp_mul(x, BASE, x); | |
402 | mp_clear(BASE); | |
403 | Safefree(BASE); | |
404 | XPUSHs(ST(1)); /* x */ | |
405 | ||
406 | ############################################################################## | |
407 | # _mul() | |
408 | ||
409 | void | |
410 | _mul(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
411 | PPCODE: | |
412 | mp_mul(x, y, x); | |
413 | XPUSHs(ST(1)); /* x */ | |
414 | ||
415 | ############################################################################## | |
416 | # _div(): x /= y or (x,rem) = x / y | |
417 | ||
418 | void | |
419 | _div(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
420 | PREINIT: | |
421 | mp_int * rem; | |
422 | PPCODE: | |
423 | if (GIMME_V == G_ARRAY) { | |
424 | Newz(0, rem, 1, mp_int); | |
425 | mp_init(rem); | |
426 | mp_div(x, y, x, rem); | |
427 | EXTEND(SP, 2); | |
428 | PUSHs(ST(1)); /* x */ | |
429 | PUSHs(sv_2mortal(sv_from_mpi(rem))); | |
430 | } | |
431 | else { | |
432 | mp_div(x, y, x, NULL); | |
433 | XPUSHs(ST(1)); /* x */ | |
434 | } | |
435 | ||
436 | ############################################################################## | |
437 | # _mod() - x %= y | |
438 | ||
439 | void | |
440 | _mod(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
441 | PPCODE: | |
442 | mp_mod(x, y, x); | |
443 | XPUSHs(ST(1)); /* x */ | |
444 | ||
445 | ############################################################################## | |
446 | # _acmp() - cmp two numbers | |
447 | ||
448 | int | |
449 | _acmp(Class, Math::BigInt::LTM m, Math::BigInt::LTM n) | |
450 | CODE: | |
451 | RETVAL = mp_cmp(m, n); | |
452 | if ( RETVAL < 0) RETVAL = -1; | |
453 | if ( RETVAL > 0) RETVAL = 1; | |
454 | OUTPUT: | |
455 | RETVAL | |
456 | ||
457 | ############################################################################## | |
458 | # _is_zero() | |
459 | ||
460 | int | |
461 | _is_zero(Class, Math::BigInt::LTM x) | |
462 | CODE: | |
463 | RETVAL = (mp_iszero(x) == MP_YES) ? 1 : 0; | |
464 | OUTPUT: | |
465 | RETVAL | |
466 | ||
467 | ############################################################################## | |
468 | # _is_one() | |
469 | ||
470 | int | |
471 | _is_one(Class, Math::BigInt::LTM x) | |
472 | CODE: | |
473 | RETVAL = (mp_cmp_d(x, 1) == MP_EQ) ? 1 : 0; | |
474 | OUTPUT: | |
475 | RETVAL | |
476 | ||
477 | ############################################################################## | |
478 | # _is_two() | |
479 | ||
480 | int | |
481 | _is_two(Class, Math::BigInt::LTM x) | |
482 | CODE: | |
483 | RETVAL = (mp_cmp_d(x, 2) == MP_EQ) ? 1 : 0; | |
484 | OUTPUT: | |
485 | RETVAL | |
486 | ||
487 | ############################################################################## | |
488 | # _is_ten() | |
489 | ||
490 | int | |
491 | _is_ten(Class, Math::BigInt::LTM x) | |
492 | CODE: | |
493 | RETVAL = (mp_cmp_d(x, 10) == MP_EQ) ? 1 : 0; | |
494 | OUTPUT: | |
495 | RETVAL | |
496 | ||
497 | ############################################################################## | |
498 | # _pow() - x **= y | |
499 | ||
500 | void | |
501 | _pow(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
502 | PPCODE: | |
503 | mp_expt_d(x, mp_get_long(y), x); | |
504 | XPUSHs(ST(1)); /* x */ | |
505 | ||
506 | ############################################################################## | |
507 | # _gcd() - gcd(m,n) | |
508 | ||
509 | Math::BigInt::LTM | |
510 | _gcd(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
511 | CODE: | |
512 | Newz(0, RETVAL, 1, mp_int); | |
513 | mp_init(RETVAL); | |
514 | mp_gcd(x, y, RETVAL); | |
515 | OUTPUT: | |
516 | RETVAL | |
517 | ||
518 | ############################################################################## | |
519 | # _and() - m &= n | |
520 | ||
521 | void | |
522 | _and(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
523 | PPCODE: | |
524 | mp_and(x, y, x); | |
525 | XPUSHs(ST(1)); /* x */ | |
526 | ||
527 | ############################################################################## | |
528 | # _xor() - m =^ n | |
529 | ||
530 | void | |
531 | _xor(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
532 | PPCODE: | |
533 | mp_xor(x, y, x); | |
534 | XPUSHs(ST(1)); /* x */ | |
535 | ||
536 | ############################################################################## | |
537 | # _or() - m =| n | |
538 | ||
539 | void | |
540 | _or(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
541 | PPCODE: | |
542 | mp_or(x, y, x); | |
543 | XPUSHs(ST(1)); /* x */ | |
544 | ||
545 | ############################################################################## | |
546 | # _copy() | |
547 | ||
548 | Math::BigInt::LTM | |
549 | _copy(Class, Math::BigInt::LTM m) | |
550 | CODE: | |
551 | Newz(0, RETVAL, 1, mp_int); | |
552 | mp_init(RETVAL); | |
553 | mp_copy(m, RETVAL); | |
554 | OUTPUT: | |
555 | RETVAL | |
556 | ||
557 | ############################################################################## | |
558 | # _is_odd() - test for number being odd | |
559 | ||
560 | int | |
561 | _is_odd(Class, Math::BigInt::LTM n) | |
562 | CODE: | |
563 | RETVAL = (mp_isodd(n) == MP_YES) ? 1 : 0; | |
564 | OUTPUT: | |
565 | RETVAL | |
566 | ||
567 | ############################################################################## | |
568 | # _is_even() - test for number being even | |
569 | ||
570 | int | |
571 | _is_even(Class, Math::BigInt::LTM n) | |
572 | CODE: | |
573 | RETVAL = (mp_iseven(n) == MP_YES || mp_iszero(n) == MP_YES) ? 1 : 0; | |
574 | OUTPUT: | |
575 | RETVAL | |
576 | ||
577 | ############################################################################## | |
578 | # _sqrt() - square root | |
579 | ||
580 | void | |
581 | _sqrt(Class, Math::BigInt::LTM x) | |
582 | PPCODE: | |
583 | mp_sqrt(x, x); | |
584 | XPUSHs(ST(1)); /* x */ | |
585 | ||
586 | ############################################################################## | |
587 | # _root() - integer roots | |
588 | ||
589 | void | |
590 | _root(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
591 | PPCODE: | |
592 | mp_n_root(x, mp_get_long(y), x); | |
593 | XPUSHs(ST(1)); /* x */ | |
594 | ||
595 | ############################################################################## | |
596 | # Storable hooks | |
597 | ||
598 | void | |
599 | STORABLE_thaw(blank_obj, cloning, serialized, ...) | |
600 | SV *blank_obj | |
601 | SV *cloning = NO_INIT | |
602 | SV *serialized | |
603 | PREINIT: | |
604 | SV *target, *tmp; | |
605 | mp_int *mpi; | |
606 | mp_int *mpitmp; | |
607 | PPCODE: | |
608 | if (SvROK(blank_obj) && sv_isa(blank_obj, "Math::BigInt::LTM")) { | |
609 | Newz(0, mpi, 1, mp_int); | |
610 | mp_init(mpi); | |
611 | mp_read_radix(mpi, SvPV_nolen(serialized), 10); | |
612 | target = SvRV(blank_obj); | |
613 | SvIV_set(target, PTR2IV(mpi)); | |
614 | SvIOK_on(target); | |
615 | PUSHs(target); | |
616 | XSRETURN(1); | |
617 | } | |
618 | else | |
619 | croak("Bad object for Math::BigInt::LTM::STORABLE_thaw call"); | |
620 | ||
621 | SV * | |
622 | STORABLE_freeze(self, cloning = NULL) | |
623 | Math::BigInt::LTM self | |
624 | SV *cloning = NO_INIT | |
625 | PREINIT: | |
626 | unsigned long len; | |
627 | char *buf; | |
628 | CODE: | |
629 | if (mp_iszero(self) == MP_YES) { | |
630 | RETVAL = newSVpv("0", 0); | |
631 | } | |
632 | else { | |
633 | len = mp_unsigned_bin_size(self) * 3 + 2; /* decimal size ~ 3 * bin size +1 for sign +1 for NUL-byte */ | |
634 | Newz(0, buf, len, char); | |
635 | mp_toradix_n(self, buf, 10, len); | |
636 | RETVAL = newSVpv(buf, 0); | |
637 | Safefree(buf); | |
638 | } | |
639 | OUTPUT: | |
640 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Cipher | |
1 | ||
2 | Crypt::Cipher | |
3 | _new(cipher_name, key, rounds=0) | |
4 | char * cipher_name | |
5 | SV * key | |
6 | int rounds | |
7 | CODE: | |
8 | { | |
9 | STRLEN key_len; | |
10 | unsigned char *key_data=NULL; | |
11 | int rv; | |
12 | int id; | |
13 | ||
14 | if (!SvPOK (key)) croak("FATAL: key must be string scalar"); | |
15 | key_data = (unsigned char *)SvPVbyte(key, key_len); | |
16 | ||
17 | id = find_cipher(cipher_name); | |
18 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
19 | ||
20 | Newz(0, RETVAL, 1, struct cipher_struct); | |
21 | if (!RETVAL) croak("FATAL: Newz failed"); | |
22 | ||
23 | RETVAL->id = id; | |
24 | RETVAL->desc = &cipher_descriptor[id]; | |
25 | rv = RETVAL->desc->setup(key_data, (int)key_len, rounds, &RETVAL->skey); | |
26 | if(rv!=CRYPT_OK) croak("FATAL: cipher setup failed: %s", error_to_string(rv)); | |
27 | } | |
28 | OUTPUT: | |
29 | RETVAL | |
30 | ||
31 | void | |
32 | DESTROY(self) | |
33 | Crypt::Cipher self | |
34 | CODE: | |
35 | Safefree(self); | |
36 | ||
37 | int | |
38 | _max_keysize(self, ...) | |
39 | Crypt::Cipher self | |
40 | CODE: | |
41 | RETVAL = self->desc->max_key_length; | |
42 | OUTPUT: | |
43 | RETVAL | |
44 | ||
45 | int | |
46 | _min_keysize(self, ...) | |
47 | Crypt::Cipher self | |
48 | CODE: | |
49 | RETVAL = self->desc->min_key_length; | |
50 | OUTPUT: | |
51 | RETVAL | |
52 | ||
53 | int | |
54 | _blocksize(self, ...) | |
55 | Crypt::Cipher self | |
56 | CODE: | |
57 | RETVAL = self->desc->block_length; | |
58 | OUTPUT: | |
59 | RETVAL | |
60 | ||
61 | int | |
62 | _default_rounds(self, ...) | |
63 | Crypt::Cipher self | |
64 | CODE: | |
65 | RETVAL = self->desc->default_rounds; | |
66 | OUTPUT: | |
67 | RETVAL | |
68 | ||
69 | SV * | |
70 | encrypt(self, data) | |
71 | Crypt::Cipher self | |
72 | SV * data | |
73 | CODE: | |
74 | { | |
75 | int rv; | |
76 | STRLEN len; | |
77 | void *plaintext = SvPVbyte(data, len); | |
78 | ||
79 | if (len==0) | |
80 | RETVAL = newSVpvn("", 0); | |
81 | else if (len % self->desc->block_length) | |
82 | croak ("FATAL: sizeof(data) should be multiple of blocksize (%d)", self->desc->block_length); | |
83 | else { | |
84 | /* idea from Crypt::Rijndael */ | |
85 | RETVAL = NEWSV(0, len); | |
86 | SvPOK_only(RETVAL); | |
87 | SvCUR_set(RETVAL, len); | |
88 | rv = self->desc->ecb_encrypt((unsigned char *)plaintext, (unsigned char *)SvPV_nolen(RETVAL), &self->skey); | |
89 | if (rv!=CRYPT_OK) croak("FATAL: encrypt failed: %s", error_to_string(rv)); | |
90 | } | |
91 | } | |
92 | OUTPUT: | |
93 | RETVAL | |
94 | ||
95 | SV * | |
96 | decrypt(self, data) | |
97 | Crypt::Cipher self | |
98 | SV * data | |
99 | CODE: | |
100 | { | |
101 | int rv; | |
102 | STRLEN len; | |
103 | void *ciphertext = SvPVbyte(data, len); | |
104 | ||
105 | if (len==0) | |
106 | RETVAL = newSVpvn("", 0); | |
107 | else if (len % self->desc->block_length) | |
108 | croak ("FATAL: sizeof(data) should be multiple of blocksize (%d)", self->desc->block_length); | |
109 | else { | |
110 | /* idea from Crypt::Rijndael */ | |
111 | RETVAL = NEWSV(0, len); | |
112 | SvPOK_only(RETVAL); | |
113 | SvCUR_set(RETVAL, len); | |
114 | rv = self->desc->ecb_decrypt((unsigned char *)ciphertext, (unsigned char *)SvPV_nolen(RETVAL), &self->skey); | |
115 | if (rv!=CRYPT_OK) croak("FATAL: decrypt failed: %s", error_to_string(rv)); | |
116 | } | |
117 | } | |
118 | OUTPUT: | |
119 | RETVAL | |
120 | ||
121 | int | |
122 | _block_length_by_name(cipher_name) | |
123 | char * cipher_name | |
124 | CODE: | |
125 | { | |
126 | int rv, id; | |
127 | ||
128 | id = find_cipher(cipher_name); | |
129 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
130 | ||
131 | rv = cipher_descriptor[id].block_length; | |
132 | if (!rv) XSRETURN_UNDEF; | |
133 | RETVAL = rv; | |
134 | } | |
135 | OUTPUT: | |
136 | RETVAL | |
137 | ||
138 | int | |
139 | _min_key_length_by_name(cipher_name) | |
140 | char * cipher_name | |
141 | CODE: | |
142 | { | |
143 | int rv, id; | |
144 | ||
145 | id = find_cipher(cipher_name); | |
146 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
147 | ||
148 | rv = cipher_descriptor[id].min_key_length; | |
149 | if (!rv) XSRETURN_UNDEF; | |
150 | RETVAL = rv; | |
151 | } | |
152 | OUTPUT: | |
153 | RETVAL | |
154 | ||
155 | int | |
156 | _max_key_length_by_name(cipher_name) | |
157 | char * cipher_name | |
158 | CODE: | |
159 | { | |
160 | int rv, id; | |
161 | ||
162 | id = find_cipher(cipher_name); | |
163 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
164 | ||
165 | rv = cipher_descriptor[id].max_key_length; | |
166 | if (!rv) XSRETURN_UNDEF; | |
167 | RETVAL = rv; | |
168 | } | |
169 | OUTPUT: | |
170 | RETVAL | |
171 | ||
172 | int | |
173 | _default_rounds_by_name(cipher_name) | |
174 | char * cipher_name | |
175 | CODE: | |
176 | { | |
177 | int rv, id; | |
178 | ||
179 | id = find_cipher(cipher_name); | |
180 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
181 | ||
182 | rv = cipher_descriptor[id].default_rounds; | |
183 | if (!rv) XSRETURN_UNDEF; | |
184 | RETVAL = rv; | |
185 | } | |
186 | OUTPUT: | |
187 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Digest | |
1 | ||
2 | Crypt::Digest | |
3 | _new(digest_name) | |
4 | char * digest_name | |
5 | CODE: | |
6 | { | |
7 | int rv; | |
8 | int id; | |
9 | ||
10 | id = find_hash(digest_name); | |
11 | if(id==-1) croak("FATAL: find_hash failed for '%s'", digest_name); | |
12 | ||
13 | Newz(0, RETVAL, 1, struct digest_struct); | |
14 | if (!RETVAL) croak("FATAL: Newz failed"); | |
15 | ||
16 | RETVAL->id = id; | |
17 | RETVAL->desc = &hash_descriptor[id]; | |
18 | rv = RETVAL->desc->init(&RETVAL->state); | |
19 | if(rv!=CRYPT_OK) croak("FATAL: digest setup failed: %s", error_to_string(rv)); | |
20 | } | |
21 | OUTPUT: | |
22 | RETVAL | |
23 | ||
24 | void | |
25 | DESTROY(self) | |
26 | Crypt::Digest self | |
27 | CODE: | |
28 | Safefree(self); | |
29 | ||
30 | void | |
31 | reset(self) | |
32 | Crypt::Digest self | |
33 | CODE: | |
34 | { | |
35 | int rv; | |
36 | rv = self->desc->init(&self->state); | |
37 | if (rv != CRYPT_OK) croak("FATAL: digest init failed: %s", error_to_string(rv)); | |
38 | } | |
39 | ||
40 | Crypt::Digest | |
41 | clone(self) | |
42 | Crypt::Digest self | |
43 | CODE: | |
44 | Newz(0, RETVAL, 1, struct digest_struct); | |
45 | Copy(&self->state, &RETVAL->state, 1, struct digest_struct); | |
46 | OUTPUT: | |
47 | RETVAL | |
48 | ||
49 | void | |
50 | add(Crypt::Digest self, ...) | |
51 | PPCODE: | |
52 | { | |
53 | STRLEN inlen; | |
54 | int rv, i; | |
55 | unsigned char *in; | |
56 | ||
57 | for(i=1; i<items; i++) { | |
58 | in = (unsigned char *)SvPVbyte(ST(i), inlen); | |
59 | if (inlen>0) { | |
60 | rv = self->desc->process(&self->state, in, (unsigned long)inlen); | |
61 | if (rv != CRYPT_OK) croak("FATAL: digest process failed: %s", error_to_string(rv)); | |
62 | } | |
63 | } | |
64 | XPUSHs(ST(0)); /* return self */ | |
65 | } | |
66 | ||
67 | SV * | |
68 | digest(self) | |
69 | Crypt::Digest self | |
70 | CODE: | |
71 | { | |
72 | unsigned char hash[MAXBLOCKSIZE]; | |
73 | int rv; | |
74 | ||
75 | rv = self->desc->done(&self->state, hash); | |
76 | if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv)); | |
77 | RETVAL = newSVpvn((char *) hash, self->desc->hashsize); | |
78 | } | |
79 | OUTPUT: | |
80 | RETVAL | |
81 | ||
82 | SV * | |
83 | hexdigest(self) | |
84 | Crypt::Digest self | |
85 | CODE: | |
86 | { | |
87 | int rv; | |
88 | unsigned long i; | |
89 | unsigned char hash[MAXBLOCKSIZE]; | |
90 | char hash_hex[MAXBLOCKSIZE*2 + 1]; | |
91 | ||
92 | rv = self->desc->done(&self->state, hash); | |
93 | if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv)); | |
94 | ||
95 | hash_hex[0] = '\0'; | |
96 | for(i=0; i<self->desc->hashsize; i++) | |
97 | sprintf(&hash_hex[2*i], "%02x", hash[i]); | |
98 | RETVAL = newSVpvn(hash_hex, strlen(hash_hex)); | |
99 | } | |
100 | OUTPUT: | |
101 | RETVAL | |
102 | ||
103 | SV * | |
104 | b64digest(self) | |
105 | Crypt::Digest self | |
106 | CODE: | |
107 | { | |
108 | int rv; | |
109 | unsigned long outlen; | |
110 | unsigned char hash[MAXBLOCKSIZE]; | |
111 | char hash_base64[MAXBLOCKSIZE*2 + 1]; | |
112 | ||
113 | rv = self->desc->done(&self->state, hash); | |
114 | if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv)); | |
115 | ||
116 | outlen = sizeof(hash_base64); | |
117 | rv = base64_encode(hash, self->desc->hashsize, (unsigned char *)hash_base64, &outlen); | |
118 | if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); | |
119 | RETVAL = newSVpvn(hash_base64, outlen); | |
120 | } | |
121 | OUTPUT: | |
122 | RETVAL | |
123 | ||
124 | SV * | |
125 | b64udigest(self) | |
126 | Crypt::Digest self | |
127 | CODE: | |
128 | { | |
129 | int rv; | |
130 | unsigned long outlen; | |
131 | unsigned char hash[MAXBLOCKSIZE]; | |
132 | char hash_base64[MAXBLOCKSIZE*2 + 1]; | |
133 | ||
134 | rv = self->desc->done(&self->state, hash); | |
135 | if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv)); | |
136 | ||
137 | outlen = sizeof(hash_base64); | |
138 | rv = base64url_encode(hash, self->desc->hashsize, (unsigned char *)hash_base64, &outlen); | |
139 | if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); | |
140 | RETVAL = newSVpvn(hash_base64, outlen); | |
141 | } | |
142 | OUTPUT: | |
143 | RETVAL | |
144 | ||
145 | int | |
146 | _hashsize(self) | |
147 | Crypt::Digest self | |
148 | CODE: | |
149 | RETVAL = self->desc->hashsize; | |
150 | OUTPUT: | |
151 | RETVAL | |
152 | ||
153 | int | |
154 | _hashsize_by_name(digest_name) | |
155 | char * digest_name | |
156 | CODE: | |
157 | { | |
158 | int rv, id; | |
159 | ||
160 | id = find_hash(digest_name); | |
161 | if(id==-1) croak("FATAL: find_digest failed for '%s'", digest_name); | |
162 | ||
163 | rv = hash_descriptor[id].hashsize; | |
164 | if (!rv) croak("FATAL: invalid hashsize for '%s'", digest_name);; | |
165 | RETVAL = rv; | |
166 | } | |
167 | OUTPUT: | |
168 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::KeyDerivation | |
1 | ||
2 | SV * | |
3 | _pkcs_5_alg1(SV * password, SV * salt, int iteration_count, char * hash_name, int len) | |
4 | CODE: | |
5 | { | |
6 | /* | |
7 | int pkcs_5_alg1(const unsigned char *password, unsigned long password_len, | |
8 | const unsigned char *salt, | |
9 | int iteration_count, int hash_idx, | |
10 | unsigned char *out, unsigned long *outlen) | |
11 | */ | |
12 | int rv, id; | |
13 | unsigned long output_len; | |
14 | unsigned char *output; | |
15 | unsigned char *password_ptr=NULL; | |
16 | STRLEN password_len=0; | |
17 | unsigned char *salt_ptr=NULL; | |
18 | STRLEN salt_len=0; | |
19 | ||
20 | id = find_hash(hash_name); | |
21 | if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
22 | ||
23 | password_ptr = (unsigned char *)SvPVbyte(password, password_len); | |
24 | salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len); | |
25 | if (salt_len < 8) croak("FATAL: salt_len has to be 8"); | |
26 | ||
27 | output_len = len; | |
28 | Newz(0, output, output_len, unsigned char); | |
29 | if (!output) croak("FATAL: Newz failed [%ld]", output_len); | |
30 | ||
31 | rv = pkcs_5_alg1(password_ptr, (unsigned long)password_len, salt_ptr, iteration_count, id, output, &output_len); | |
32 | if (rv != CRYPT_OK) croak("FATAL: pkcs_5_alg1 process failed: %s", error_to_string(rv)); | |
33 | ||
34 | RETVAL = newSVpvn((char *)output, output_len); | |
35 | Safefree(output); | |
36 | } | |
37 | OUTPUT: | |
38 | RETVAL | |
39 | ||
40 | SV * | |
41 | _pkcs_5_alg2(SV * password, SV * salt, int iteration_count, char * hash_name, int len) | |
42 | CODE: | |
43 | { | |
44 | /* | |
45 | int pkcs_5_alg2(const unsigned char *password, unsigned long password_len, | |
46 | const unsigned char *salt, unsigned long salt_len, | |
47 | int iteration_count, int hash_idx, | |
48 | unsigned char *out, unsigned long *outlen) | |
49 | */ | |
50 | int rv, id; | |
51 | unsigned long output_len; | |
52 | unsigned char *output; | |
53 | unsigned char *password_ptr=NULL; | |
54 | STRLEN password_len=0; | |
55 | unsigned char *salt_ptr=NULL; | |
56 | STRLEN salt_len=0; | |
57 | ||
58 | id = find_hash(hash_name); | |
59 | if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
60 | ||
61 | password_ptr = (unsigned char *)SvPVbyte(password, password_len); | |
62 | salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len); | |
63 | ||
64 | output_len = len; | |
65 | Newz(0, output, output_len, unsigned char); | |
66 | if (!output) croak("FATAL: Newz failed [%ld]", output_len); | |
67 | ||
68 | rv = pkcs_5_alg2(password_ptr, (unsigned long)password_len, salt_ptr, (unsigned long)salt_len, iteration_count, id, output, &output_len); | |
69 | if (rv != CRYPT_OK) croak("FATAL: pkcs_5_alg2 process failed: %s", error_to_string(rv)); | |
70 | ||
71 | RETVAL = newSVpvn((char *)output, output_len); | |
72 | Safefree(output); | |
73 | } | |
74 | OUTPUT: | |
75 | RETVAL | |
76 | ||
77 | SV * | |
78 | _hkdf_extract(char * hash_name, SV * salt, SV * in) | |
79 | CODE: | |
80 | { | |
81 | /* | |
82 | int hkdf_extract(int hash_idx, const unsigned char *salt, unsigned long saltlen, | |
83 | const unsigned char *in, unsigned long inlen, | |
84 | unsigned char *out, unsigned long *outlen) | |
85 | */ | |
86 | int rv, id; | |
87 | unsigned char output[MAXBLOCKSIZE]; | |
88 | unsigned long output_len; | |
89 | unsigned char *in_ptr=NULL; | |
90 | STRLEN in_len=0; | |
91 | unsigned char *salt_ptr=NULL; | |
92 | STRLEN salt_len=0; | |
93 | ||
94 | id = find_hash(hash_name); | |
95 | if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
96 | ||
97 | in_ptr = (unsigned char *)SvPVbyte(in, in_len); | |
98 | salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len); | |
99 | ||
100 | output_len = sizeof(output); | |
101 | rv = hkdf_extract(id, salt_ptr, (unsigned long)salt_len, in_ptr, (unsigned long)in_len, output, &output_len); | |
102 | if (rv != CRYPT_OK) croak("FATAL: hkdf_extract process failed: %s", error_to_string(rv)); | |
103 | ||
104 | RETVAL = newSVpvn((char *)output, output_len); | |
105 | } | |
106 | OUTPUT: | |
107 | RETVAL | |
108 | ||
109 | SV * | |
110 | _hkdf_expand(char * hash_name, SV * info, SV * in, unsigned long output_len) | |
111 | CODE: | |
112 | { | |
113 | /* | |
114 | int hkdf_expand(int hash_idx, const unsigned char *info, unsigned long infolen, | |
115 | const unsigned char *in, unsigned long inlen, | |
116 | unsigned char *out, unsigned long outlen) | |
117 | */ | |
118 | int rv, id; | |
119 | unsigned char *output; | |
120 | unsigned char *in_ptr=NULL; | |
121 | STRLEN in_len=0; | |
122 | unsigned char *info_ptr=NULL; | |
123 | STRLEN info_len=0; | |
124 | ||
125 | id = find_hash(hash_name); | |
126 | if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
127 | ||
128 | in_ptr = (unsigned char *)SvPVbyte(in, in_len); | |
129 | info_ptr = (unsigned char *)SvPVbyte(info, info_len); | |
130 | ||
131 | Newz(0, output, output_len, unsigned char); | |
132 | if (!output) croak("FATAL: Newz failed [%ld]", output_len); | |
133 | ||
134 | rv = hkdf_expand(id, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len); | |
135 | if (rv != CRYPT_OK) croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv)); | |
136 | ||
137 | RETVAL = newSVpvn((char *)output, output_len); | |
138 | Safefree(output); | |
139 | } | |
140 | OUTPUT: | |
141 | RETVAL | |
142 | ||
143 | SV * | |
144 | _hkdf(char * hash_name, SV * salt, SV * info, SV * in, unsigned long output_len) | |
145 | CODE: | |
146 | { | |
147 | /* | |
148 | int hkdf(int hash_idx, const unsigned char *salt, unsigned long saltlen, | |
149 | const unsigned char *info, unsigned long infolen, | |
150 | const unsigned char *in, unsigned long inlen, | |
151 | unsigned char *out, unsigned long outlen) | |
152 | */ | |
153 | int rv, id; | |
154 | unsigned char *output; | |
155 | unsigned char *in_ptr=NULL; | |
156 | STRLEN in_len=0; | |
157 | unsigned char *info_ptr=NULL; | |
158 | STRLEN info_len=0; | |
159 | unsigned char *salt_ptr=NULL; | |
160 | STRLEN salt_len=0; | |
161 | ||
162 | id = find_hash(hash_name); | |
163 | if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
164 | ||
165 | in_ptr = (unsigned char *)SvPVbyte(in, in_len); | |
166 | info_ptr = (unsigned char *)SvPVbyte(info, info_len); | |
167 | salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len); | |
168 | ||
169 | Newz(0, output, output_len, unsigned char); | |
170 | if (!output) croak("FATAL: Newz failed [%ld]", output_len); | |
171 | ||
172 | rv = hkdf(id, salt_ptr, (unsigned long)salt_len, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len); | |
173 | if (rv != CRYPT_OK) croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv)); | |
174 | ||
175 | RETVAL = newSVpvn((char *)output, output_len); | |
176 | Safefree(output); | |
177 | } | |
178 | OUTPUT: | |
179 | RETVAL | |
180 |
0 | MODULE = CryptX PACKAGE = Crypt::Mac::F9 | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mac::F9 | |
5 | _new(char * cipher_name, SV * key) | |
6 | CODE: | |
7 | { | |
8 | STRLEN k_len=0; | |
9 | unsigned char *k=NULL; | |
10 | int rv; | |
11 | int id; | |
12 | ||
13 | id = find_cipher(cipher_name); | |
14 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
15 | ||
16 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
17 | k = (unsigned char *) SvPVbyte(key, k_len); | |
18 | ||
19 | Newz(0, RETVAL, 1, struct f9_struct); | |
20 | if (!RETVAL) croak("FATAL: Newz failed"); | |
21 | ||
22 | rv = f9_init(&RETVAL->state, id, k, (unsigned long)k_len); | |
23 | if (rv != CRYPT_OK) croak("FATAL: f9_init failed: %s", error_to_string(rv)); | |
24 | } | |
25 | OUTPUT: | |
26 | RETVAL | |
27 | ||
28 | void | |
29 | DESTROY(Crypt::Mac::F9 self) | |
30 | CODE: | |
31 | Safefree(self); | |
32 | ||
33 | Crypt::Mac::F9 | |
34 | clone(Crypt::Mac::F9 self) | |
35 | CODE: | |
36 | Newz(0, RETVAL, 1, struct f9_struct); | |
37 | if (!RETVAL) croak("FATAL: Newz failed"); | |
38 | Copy(&self->state, &RETVAL->state, 1, struct f9_struct); | |
39 | OUTPUT: | |
40 | RETVAL | |
41 | ||
42 | void | |
43 | _add_single(Crypt::Mac::F9 self, SV * data) | |
44 | CODE: | |
45 | { | |
46 | int rv; | |
47 | STRLEN in_data_len; | |
48 | unsigned char *in_data; | |
49 | ||
50 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
51 | if (in_data_len>0) { | |
52 | rv = f9_process(&self->state, in_data, (unsigned long)in_data_len); | |
53 | if (rv != CRYPT_OK) croak("FATAL: f9_process failed: %s", error_to_string(rv)); | |
54 | } | |
55 | } | |
56 | ||
57 | SV * | |
58 | mac(Crypt::Mac::F9 self) | |
59 | CODE: | |
60 | { | |
61 | char mac[MAXBLOCKSIZE]; | |
62 | unsigned long mac_len; | |
63 | int rv; | |
64 | ||
65 | mac_len = sizeof(mac); | |
66 | rv = f9_done(&self->state, (unsigned char*)mac, &mac_len); | |
67 | if (rv != CRYPT_OK) croak("FATAL: f9_done failed: %s", error_to_string(rv)); | |
68 | RETVAL = newSVpvn(mac, mac_len); | |
69 | } | |
70 | OUTPUT: | |
71 | RETVAL | |
72 | ||
73 | SV * | |
74 | b64mac(Crypt::Mac::F9 self) | |
75 | CODE: | |
76 | { | |
77 | unsigned char mac[MAXBLOCKSIZE]; | |
78 | unsigned long mac_len; | |
79 | int rv; | |
80 | unsigned long outlen; | |
81 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
82 | ||
83 | mac_len = sizeof(mac); | |
84 | rv = f9_done(&self->state, mac, &mac_len); | |
85 | if (rv != CRYPT_OK) croak("FATAL: f9_done failed: %s", error_to_string(rv)); | |
86 | outlen = sizeof(mac_base64); | |
87 | rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
88 | if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); | |
89 | RETVAL = newSVpvn(mac_base64, outlen); | |
90 | } | |
91 | OUTPUT: | |
92 | RETVAL | |
93 | ||
94 | SV * | |
95 | b64umac(Crypt::Mac::F9 self) | |
96 | CODE: | |
97 | { | |
98 | unsigned char mac[MAXBLOCKSIZE]; | |
99 | unsigned long mac_len; | |
100 | int rv; | |
101 | unsigned long outlen; | |
102 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
103 | ||
104 | mac_len = sizeof(mac); | |
105 | rv = f9_done(&self->state, mac, &mac_len); | |
106 | if (rv != CRYPT_OK) croak("FATAL: f9_done failed: %s", error_to_string(rv)); | |
107 | outlen = sizeof(mac_base64); | |
108 | rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
109 | if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); | |
110 | RETVAL = newSVpvn(mac_base64, outlen); | |
111 | } | |
112 | OUTPUT: | |
113 | RETVAL | |
114 | ||
115 | SV * | |
116 | hexmac(Crypt::Mac::F9 self) | |
117 | CODE: | |
118 | { | |
119 | unsigned char mac[MAXBLOCKSIZE]; | |
120 | unsigned long mac_len, i; | |
121 | int rv; | |
122 | char mac_hex[MAXBLOCKSIZE*2 + 1]; | |
123 | ||
124 | mac_len = sizeof(mac); | |
125 | rv = f9_done(&self->state, mac, &mac_len); | |
126 | if (rv != CRYPT_OK) croak("FATAL: f9_done failed: %s", error_to_string(rv)); | |
127 | mac_hex[0] = '\0'; | |
128 | for(i=0; i<mac_len; i++) | |
129 | sprintf(&mac_hex[2*i], "%02x", mac[i]); | |
130 | RETVAL = newSVpvn(mac_hex, strlen(mac_hex)); | |
131 | } | |
132 | OUTPUT: | |
133 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mac::HMAC | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mac::HMAC | |
5 | _new(char * hash_name, SV * key) | |
6 | CODE: | |
7 | { | |
8 | STRLEN k_len=0; | |
9 | unsigned char *k=NULL; | |
10 | int rv; | |
11 | int id; | |
12 | ||
13 | id = find_hash(hash_name); | |
14 | if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
15 | ||
16 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
17 | k = (unsigned char *) SvPVbyte(key, k_len); | |
18 | ||
19 | Newz(0, RETVAL, 1, struct hmac_struct); | |
20 | if (!RETVAL) croak("FATAL: Newz failed"); | |
21 | ||
22 | rv = hmac_init(&RETVAL->state, id, k, (unsigned long)k_len); | |
23 | if (rv != CRYPT_OK) croak("FATAL: hmac_init failed: %s", error_to_string(rv)); | |
24 | } | |
25 | OUTPUT: | |
26 | RETVAL | |
27 | ||
28 | void | |
29 | DESTROY(Crypt::Mac::HMAC self) | |
30 | CODE: | |
31 | Safefree(self); | |
32 | ||
33 | Crypt::Mac::HMAC | |
34 | clone(Crypt::Mac::HMAC self) | |
35 | CODE: | |
36 | Newz(0, RETVAL, 1, struct hmac_struct); | |
37 | if (!RETVAL) croak("FATAL: Newz failed"); | |
38 | Copy(&self->state, &RETVAL->state, 1, struct hmac_struct); | |
39 | OUTPUT: | |
40 | RETVAL | |
41 | ||
42 | void | |
43 | _add_single(Crypt::Mac::HMAC self, SV * data) | |
44 | CODE: | |
45 | { | |
46 | int rv; | |
47 | STRLEN in_data_len; | |
48 | unsigned char *in_data; | |
49 | ||
50 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
51 | if (in_data_len>0) { | |
52 | rv = hmac_process(&self->state, in_data, (unsigned long)in_data_len); | |
53 | if (rv != CRYPT_OK) croak("FATAL: hmac_process failed: %s", error_to_string(rv)); | |
54 | } | |
55 | } | |
56 | ||
57 | SV * | |
58 | mac(Crypt::Mac::HMAC self) | |
59 | CODE: | |
60 | { | |
61 | char mac[MAXBLOCKSIZE]; | |
62 | unsigned long mac_len; | |
63 | int rv; | |
64 | ||
65 | mac_len = sizeof(mac); | |
66 | rv = hmac_done(&self->state, (unsigned char*)mac, &mac_len); | |
67 | if (rv != CRYPT_OK) croak("FATAL: hmac_done failed: %s", error_to_string(rv)); | |
68 | RETVAL = newSVpvn(mac, mac_len); | |
69 | } | |
70 | OUTPUT: | |
71 | RETVAL | |
72 | ||
73 | SV * | |
74 | b64mac(Crypt::Mac::HMAC self) | |
75 | CODE: | |
76 | { | |
77 | unsigned char mac[MAXBLOCKSIZE]; | |
78 | unsigned long mac_len; | |
79 | int rv; | |
80 | unsigned long outlen; | |
81 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
82 | ||
83 | mac_len = sizeof(mac); | |
84 | rv = hmac_done(&self->state, mac, &mac_len); | |
85 | if (rv != CRYPT_OK) croak("FATAL: hmac_done failed: %s", error_to_string(rv)); | |
86 | outlen = sizeof(mac_base64); | |
87 | rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
88 | if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); | |
89 | RETVAL = newSVpvn(mac_base64, outlen); | |
90 | } | |
91 | OUTPUT: | |
92 | RETVAL | |
93 | ||
94 | SV * | |
95 | b64umac(Crypt::Mac::HMAC self) | |
96 | CODE: | |
97 | { | |
98 | unsigned char mac[MAXBLOCKSIZE]; | |
99 | unsigned long mac_len; | |
100 | int rv; | |
101 | unsigned long outlen; | |
102 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
103 | ||
104 | mac_len = sizeof(mac); | |
105 | rv = hmac_done(&self->state, mac, &mac_len); | |
106 | if (rv != CRYPT_OK) croak("FATAL: hmac_done failed: %s", error_to_string(rv)); | |
107 | outlen = sizeof(mac_base64); | |
108 | rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
109 | if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); | |
110 | RETVAL = newSVpvn(mac_base64, outlen); | |
111 | } | |
112 | OUTPUT: | |
113 | RETVAL | |
114 | ||
115 | SV * | |
116 | hexmac(Crypt::Mac::HMAC self) | |
117 | CODE: | |
118 | { | |
119 | unsigned char mac[MAXBLOCKSIZE]; | |
120 | unsigned long mac_len, i; | |
121 | int rv; | |
122 | char mac_hex[MAXBLOCKSIZE*2 + 1]; | |
123 | ||
124 | mac_len = sizeof(mac); | |
125 | rv = hmac_done(&self->state, mac, &mac_len); | |
126 | if (rv != CRYPT_OK) croak("FATAL: hmac_done failed: %s", error_to_string(rv)); | |
127 | mac_hex[0] = '\0'; | |
128 | for(i=0; i<mac_len; i++) | |
129 | sprintf(&mac_hex[2*i], "%02x", mac[i]); | |
130 | RETVAL = newSVpvn(mac_hex, strlen(mac_hex)); | |
131 | } | |
132 | OUTPUT: | |
133 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mac::OMAC | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mac::OMAC | |
5 | _new(char * cipher_name, SV * key) | |
6 | CODE: | |
7 | { | |
8 | STRLEN k_len=0; | |
9 | unsigned char *k=NULL; | |
10 | int rv; | |
11 | int id; | |
12 | ||
13 | id = find_cipher(cipher_name); | |
14 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
15 | ||
16 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
17 | k = (unsigned char *) SvPVbyte(key, k_len); | |
18 | ||
19 | Newz(0, RETVAL, 1, struct omac_struct); | |
20 | if (!RETVAL) croak("FATAL: Newz failed"); | |
21 | ||
22 | rv = omac_init(&RETVAL->state, id, k, (unsigned long)k_len); | |
23 | if (rv != CRYPT_OK) croak("FATAL: omac_init failed: %s", error_to_string(rv)); | |
24 | } | |
25 | OUTPUT: | |
26 | RETVAL | |
27 | ||
28 | void | |
29 | DESTROY(Crypt::Mac::OMAC self) | |
30 | CODE: | |
31 | Safefree(self); | |
32 | ||
33 | Crypt::Mac::OMAC | |
34 | clone(Crypt::Mac::OMAC self) | |
35 | CODE: | |
36 | Newz(0, RETVAL, 1, struct omac_struct); | |
37 | if (!RETVAL) croak("FATAL: Newz failed"); | |
38 | Copy(&self->state, &RETVAL->state, 1, struct omac_struct); | |
39 | OUTPUT: | |
40 | RETVAL | |
41 | ||
42 | void | |
43 | _add_single(Crypt::Mac::OMAC self, SV * data) | |
44 | CODE: | |
45 | { | |
46 | int rv; | |
47 | STRLEN in_data_len; | |
48 | unsigned char *in_data; | |
49 | ||
50 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
51 | if (in_data_len>0) { | |
52 | rv = omac_process(&self->state, in_data, (unsigned long)in_data_len); | |
53 | if (rv != CRYPT_OK) croak("FATAL: omac_process failed: %s", error_to_string(rv)); | |
54 | } | |
55 | } | |
56 | ||
57 | SV * | |
58 | mac(Crypt::Mac::OMAC self) | |
59 | CODE: | |
60 | { | |
61 | char mac[MAXBLOCKSIZE]; | |
62 | unsigned long mac_len; | |
63 | int rv; | |
64 | ||
65 | mac_len = sizeof(mac); | |
66 | rv = omac_done(&self->state, (unsigned char*)mac, &mac_len); | |
67 | if (rv != CRYPT_OK) croak("FATAL: omac_done failed: %s", error_to_string(rv)); | |
68 | RETVAL = newSVpvn(mac, mac_len); | |
69 | } | |
70 | OUTPUT: | |
71 | RETVAL | |
72 | ||
73 | SV * | |
74 | b64mac(Crypt::Mac::OMAC self) | |
75 | CODE: | |
76 | { | |
77 | unsigned char mac[MAXBLOCKSIZE]; | |
78 | unsigned long mac_len; | |
79 | int rv; | |
80 | unsigned long outlen; | |
81 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
82 | ||
83 | mac_len = sizeof(mac); | |
84 | rv = omac_done(&self->state, mac, &mac_len); | |
85 | if (rv != CRYPT_OK) croak("FATAL: omac_done failed: %s", error_to_string(rv)); | |
86 | outlen = sizeof(mac_base64); | |
87 | rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
88 | if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); | |
89 | RETVAL = newSVpvn(mac_base64, outlen); | |
90 | } | |
91 | OUTPUT: | |
92 | RETVAL | |
93 | ||
94 | SV * | |
95 | b64umac(Crypt::Mac::OMAC self) | |
96 | CODE: | |
97 | { | |
98 | unsigned char mac[MAXBLOCKSIZE]; | |
99 | unsigned long mac_len; | |
100 | int rv; | |
101 | unsigned long outlen; | |
102 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
103 | ||
104 | mac_len = sizeof(mac); | |
105 | rv = omac_done(&self->state, mac, &mac_len); | |
106 | if (rv != CRYPT_OK) croak("FATAL: omac_done failed: %s", error_to_string(rv)); | |
107 | outlen = sizeof(mac_base64); | |
108 | rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
109 | if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); | |
110 | RETVAL = newSVpvn(mac_base64, outlen); | |
111 | } | |
112 | OUTPUT: | |
113 | RETVAL | |
114 | ||
115 | SV * | |
116 | hexmac(Crypt::Mac::OMAC self) | |
117 | CODE: | |
118 | { | |
119 | unsigned char mac[MAXBLOCKSIZE]; | |
120 | unsigned long mac_len, i; | |
121 | int rv; | |
122 | char mac_hex[MAXBLOCKSIZE*2 + 1]; | |
123 | ||
124 | mac_len = sizeof(mac); | |
125 | rv = omac_done(&self->state, mac, &mac_len); | |
126 | if (rv != CRYPT_OK) croak("FATAL: omac_done failed: %s", error_to_string(rv)); | |
127 | mac_hex[0] = '\0'; | |
128 | for(i=0; i<mac_len; i++) | |
129 | sprintf(&mac_hex[2*i], "%02x", mac[i]); | |
130 | RETVAL = newSVpvn(mac_hex, strlen(mac_hex)); | |
131 | } | |
132 | OUTPUT: | |
133 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mac::PMAC | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mac::PMAC | |
5 | _new(char * cipher_name, SV * key) | |
6 | CODE: | |
7 | { | |
8 | STRLEN k_len=0; | |
9 | unsigned char *k=NULL; | |
10 | int rv; | |
11 | int id; | |
12 | ||
13 | id = find_cipher(cipher_name); | |
14 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
15 | ||
16 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
17 | k = (unsigned char *) SvPVbyte(key, k_len); | |
18 | ||
19 | Newz(0, RETVAL, 1, struct pmac_struct); | |
20 | if (!RETVAL) croak("FATAL: Newz failed"); | |
21 | ||
22 | rv = pmac_init(&RETVAL->state, id, k, (unsigned long)k_len); | |
23 | if (rv != CRYPT_OK) croak("FATAL: pmac_init failed: %s", error_to_string(rv)); | |
24 | } | |
25 | OUTPUT: | |
26 | RETVAL | |
27 | ||
28 | void | |
29 | DESTROY(Crypt::Mac::PMAC self) | |
30 | CODE: | |
31 | Safefree(self); | |
32 | ||
33 | Crypt::Mac::PMAC | |
34 | clone(Crypt::Mac::PMAC self) | |
35 | CODE: | |
36 | Newz(0, RETVAL, 1, struct pmac_struct); | |
37 | if (!RETVAL) croak("FATAL: Newz failed"); | |
38 | Copy(&self->state, &RETVAL->state, 1, struct pmac_struct); | |
39 | OUTPUT: | |
40 | RETVAL | |
41 | ||
42 | void | |
43 | _add_single(Crypt::Mac::PMAC self, SV * data) | |
44 | CODE: | |
45 | { | |
46 | int rv; | |
47 | STRLEN in_data_len; | |
48 | unsigned char *in_data; | |
49 | ||
50 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
51 | if (in_data_len>0) { | |
52 | rv = pmac_process(&self->state, in_data, (unsigned long)in_data_len); | |
53 | if (rv != CRYPT_OK) croak("FATAL: pmac_process failed: %s", error_to_string(rv)); | |
54 | } | |
55 | } | |
56 | ||
57 | SV * | |
58 | mac(Crypt::Mac::PMAC self) | |
59 | CODE: | |
60 | { | |
61 | char mac[MAXBLOCKSIZE]; | |
62 | unsigned long mac_len; | |
63 | int rv; | |
64 | ||
65 | mac_len = sizeof(mac); | |
66 | rv = pmac_done(&self->state, (unsigned char*)mac, &mac_len); | |
67 | if (rv != CRYPT_OK) croak("FATAL: pmac_done failed: %s", error_to_string(rv)); | |
68 | RETVAL = newSVpvn(mac, mac_len); | |
69 | } | |
70 | OUTPUT: | |
71 | RETVAL | |
72 | ||
73 | SV * | |
74 | b64mac(Crypt::Mac::PMAC self) | |
75 | CODE: | |
76 | { | |
77 | unsigned char mac[MAXBLOCKSIZE]; | |
78 | unsigned long mac_len; | |
79 | int rv; | |
80 | unsigned long outlen; | |
81 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
82 | ||
83 | mac_len = sizeof(mac); | |
84 | rv = pmac_done(&self->state, mac, &mac_len); | |
85 | if (rv != CRYPT_OK) croak("FATAL: pmac_done failed: %s", error_to_string(rv)); | |
86 | outlen = sizeof(mac_base64); | |
87 | rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
88 | if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); | |
89 | RETVAL = newSVpvn(mac_base64, outlen); | |
90 | } | |
91 | OUTPUT: | |
92 | RETVAL | |
93 | ||
94 | SV * | |
95 | b64umac(Crypt::Mac::PMAC self) | |
96 | CODE: | |
97 | { | |
98 | unsigned char mac[MAXBLOCKSIZE]; | |
99 | unsigned long mac_len; | |
100 | int rv; | |
101 | unsigned long outlen; | |
102 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
103 | ||
104 | mac_len = sizeof(mac); | |
105 | rv = pmac_done(&self->state, mac, &mac_len); | |
106 | if (rv != CRYPT_OK) croak("FATAL: pmac_done failed: %s", error_to_string(rv)); | |
107 | outlen = sizeof(mac_base64); | |
108 | rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
109 | if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); | |
110 | RETVAL = newSVpvn(mac_base64, outlen); | |
111 | } | |
112 | OUTPUT: | |
113 | RETVAL | |
114 | ||
115 | SV * | |
116 | hexmac(Crypt::Mac::PMAC self) | |
117 | CODE: | |
118 | { | |
119 | unsigned char mac[MAXBLOCKSIZE]; | |
120 | unsigned long mac_len, i; | |
121 | int rv; | |
122 | char mac_hex[MAXBLOCKSIZE*2 + 1]; | |
123 | ||
124 | mac_len = sizeof(mac); | |
125 | rv = pmac_done(&self->state, mac, &mac_len); | |
126 | if (rv != CRYPT_OK) croak("FATAL: pmac_done failed: %s", error_to_string(rv)); | |
127 | mac_hex[0] = '\0'; | |
128 | for(i=0; i<mac_len; i++) | |
129 | sprintf(&mac_hex[2*i], "%02x", mac[i]); | |
130 | RETVAL = newSVpvn(mac_hex, strlen(mac_hex)); | |
131 | } | |
132 | OUTPUT: | |
133 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mac::Pelican | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mac::Pelican | |
5 | _new(SV * key) | |
6 | CODE: | |
7 | { | |
8 | STRLEN k_len=0; | |
9 | unsigned char *k=NULL; | |
10 | int rv; | |
11 | ||
12 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
13 | k = (unsigned char *) SvPVbyte(key, k_len); | |
14 | ||
15 | Newz(0, RETVAL, 1, struct pelican_struct); | |
16 | if (!RETVAL) croak("FATAL: Newz failed"); | |
17 | ||
18 | rv = pelican_init(&RETVAL->state, k, (unsigned long)k_len); | |
19 | if (rv != CRYPT_OK) croak("FATAL: pelican_init failed: %s", error_to_string(rv)); | |
20 | } | |
21 | OUTPUT: | |
22 | RETVAL | |
23 | ||
24 | void | |
25 | DESTROY(Crypt::Mac::Pelican self) | |
26 | CODE: | |
27 | Safefree(self); | |
28 | ||
29 | Crypt::Mac::Pelican | |
30 | clone(Crypt::Mac::Pelican self) | |
31 | CODE: | |
32 | Newz(0, RETVAL, 1, struct pelican_struct); | |
33 | if (!RETVAL) croak("FATAL: Newz failed"); | |
34 | Copy(&self->state, &RETVAL->state, 1, struct pelican_struct); | |
35 | OUTPUT: | |
36 | RETVAL | |
37 | ||
38 | void | |
39 | _add_single(Crypt::Mac::Pelican self, SV * data) | |
40 | CODE: | |
41 | { | |
42 | int rv; | |
43 | STRLEN in_data_len; | |
44 | unsigned char *in_data; | |
45 | ||
46 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
47 | if (in_data_len>0) { | |
48 | rv = pelican_process(&self->state, in_data, (unsigned long)in_data_len); | |
49 | if (rv != CRYPT_OK) croak("FATAL: pelican_process failed: %s", error_to_string(rv)); | |
50 | } | |
51 | } | |
52 | ||
53 | SV * | |
54 | mac(Crypt::Mac::Pelican self) | |
55 | CODE: | |
56 | { | |
57 | char mac[MAXBLOCKSIZE]; | |
58 | unsigned long mac_len; | |
59 | int rv; | |
60 | ||
61 | mac_len = 16; | |
62 | rv = pelican_done(&self->state, (unsigned char*)mac); | |
63 | if (rv != CRYPT_OK) croak("FATAL: pelican_done failed: %s", error_to_string(rv)); | |
64 | RETVAL = newSVpvn(mac, mac_len); | |
65 | } | |
66 | OUTPUT: | |
67 | RETVAL | |
68 | ||
69 | SV * | |
70 | b64mac(Crypt::Mac::Pelican self) | |
71 | CODE: | |
72 | { | |
73 | unsigned char mac[MAXBLOCKSIZE]; | |
74 | unsigned long mac_len; | |
75 | int rv; | |
76 | unsigned long outlen; | |
77 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
78 | ||
79 | mac_len = 16; | |
80 | rv = pelican_done(&self->state, mac); | |
81 | if (rv != CRYPT_OK) croak("FATAL: pelican_done failed: %s", error_to_string(rv)); | |
82 | outlen = sizeof(mac_base64); | |
83 | rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
84 | if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); | |
85 | RETVAL = newSVpvn(mac_base64, outlen); | |
86 | } | |
87 | OUTPUT: | |
88 | RETVAL | |
89 | ||
90 | SV * | |
91 | b64umac(Crypt::Mac::Pelican self) | |
92 | CODE: | |
93 | { | |
94 | unsigned char mac[MAXBLOCKSIZE]; | |
95 | unsigned long mac_len; | |
96 | int rv; | |
97 | unsigned long outlen; | |
98 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
99 | ||
100 | mac_len = 16; | |
101 | rv = pelican_done(&self->state, mac); | |
102 | if (rv != CRYPT_OK) croak("FATAL: pelican_done failed: %s", error_to_string(rv)); | |
103 | outlen = sizeof(mac_base64); | |
104 | rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
105 | if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); | |
106 | RETVAL = newSVpvn(mac_base64, outlen); | |
107 | } | |
108 | OUTPUT: | |
109 | RETVAL | |
110 | ||
111 | SV * | |
112 | hexmac(Crypt::Mac::Pelican self) | |
113 | CODE: | |
114 | { | |
115 | unsigned char mac[MAXBLOCKSIZE]; | |
116 | unsigned long mac_len, i; | |
117 | int rv; | |
118 | char mac_hex[MAXBLOCKSIZE*2 + 1]; | |
119 | ||
120 | mac_len = 16; | |
121 | rv = pelican_done(&self->state, mac); | |
122 | if (rv != CRYPT_OK) croak("FATAL: pelican_done failed: %s", error_to_string(rv)); | |
123 | mac_hex[0] = '\0'; | |
124 | for(i=0; i<mac_len; i++) | |
125 | sprintf(&mac_hex[2*i], "%02x", mac[i]); | |
126 | RETVAL = newSVpvn(mac_hex, strlen(mac_hex)); | |
127 | } | |
128 | OUTPUT: | |
129 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mac::XCBC | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mac::XCBC | |
5 | _new(char * cipher_name, SV * key) | |
6 | CODE: | |
7 | { | |
8 | STRLEN k_len=0; | |
9 | unsigned char *k=NULL; | |
10 | int rv; | |
11 | int id; | |
12 | ||
13 | id = find_cipher(cipher_name); | |
14 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
15 | ||
16 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
17 | k = (unsigned char *) SvPVbyte(key, k_len); | |
18 | ||
19 | Newz(0, RETVAL, 1, struct xcbc_struct); | |
20 | if (!RETVAL) croak("FATAL: Newz failed"); | |
21 | ||
22 | rv = xcbc_init(&RETVAL->state, id, k, (unsigned long)k_len); | |
23 | if (rv != CRYPT_OK) croak("FATAL: xcbc_init failed: %s", error_to_string(rv)); | |
24 | } | |
25 | OUTPUT: | |
26 | RETVAL | |
27 | ||
28 | void | |
29 | DESTROY(Crypt::Mac::XCBC self) | |
30 | CODE: | |
31 | Safefree(self); | |
32 | ||
33 | Crypt::Mac::XCBC | |
34 | clone(Crypt::Mac::XCBC self) | |
35 | CODE: | |
36 | Newz(0, RETVAL, 1, struct xcbc_struct); | |
37 | if (!RETVAL) croak("FATAL: Newz failed"); | |
38 | Copy(&self->state, &RETVAL->state, 1, struct xcbc_struct); | |
39 | OUTPUT: | |
40 | RETVAL | |
41 | ||
42 | void | |
43 | _add_single(Crypt::Mac::XCBC self, SV * data) | |
44 | CODE: | |
45 | { | |
46 | int rv; | |
47 | STRLEN in_data_len; | |
48 | unsigned char *in_data; | |
49 | ||
50 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
51 | if (in_data_len>0) { | |
52 | rv = xcbc_process(&self->state, in_data, (unsigned long)in_data_len); | |
53 | if (rv != CRYPT_OK) croak("FATAL: xcbc_process failed: %s", error_to_string(rv)); | |
54 | } | |
55 | } | |
56 | ||
57 | SV * | |
58 | mac(Crypt::Mac::XCBC self) | |
59 | CODE: | |
60 | { | |
61 | char mac[MAXBLOCKSIZE]; | |
62 | unsigned long mac_len; | |
63 | int rv; | |
64 | ||
65 | mac_len = sizeof(mac); | |
66 | rv = xcbc_done(&self->state, (unsigned char*)mac, &mac_len); | |
67 | if (rv != CRYPT_OK) croak("FATAL: xcbc_done failed: %s", error_to_string(rv)); | |
68 | RETVAL = newSVpvn(mac, mac_len); | |
69 | } | |
70 | OUTPUT: | |
71 | RETVAL | |
72 | ||
73 | SV * | |
74 | b64mac(Crypt::Mac::XCBC self) | |
75 | CODE: | |
76 | { | |
77 | unsigned char mac[MAXBLOCKSIZE]; | |
78 | unsigned long mac_len; | |
79 | int rv; | |
80 | unsigned long outlen; | |
81 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
82 | ||
83 | mac_len = sizeof(mac); | |
84 | rv = xcbc_done(&self->state, mac, &mac_len); | |
85 | if (rv != CRYPT_OK) croak("FATAL: xcbc_done failed: %s", error_to_string(rv)); | |
86 | outlen = sizeof(mac_base64); | |
87 | rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
88 | if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); | |
89 | RETVAL = newSVpvn(mac_base64, outlen); | |
90 | } | |
91 | OUTPUT: | |
92 | RETVAL | |
93 | ||
94 | SV * | |
95 | b64umac(Crypt::Mac::XCBC self) | |
96 | CODE: | |
97 | { | |
98 | unsigned char mac[MAXBLOCKSIZE]; | |
99 | unsigned long mac_len; | |
100 | int rv; | |
101 | unsigned long outlen; | |
102 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
103 | ||
104 | mac_len = sizeof(mac); | |
105 | rv = xcbc_done(&self->state, mac, &mac_len); | |
106 | if (rv != CRYPT_OK) croak("FATAL: xcbc_done failed: %s", error_to_string(rv)); | |
107 | outlen = sizeof(mac_base64); | |
108 | rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
109 | if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); | |
110 | RETVAL = newSVpvn(mac_base64, outlen); | |
111 | } | |
112 | OUTPUT: | |
113 | RETVAL | |
114 | ||
115 | SV * | |
116 | hexmac(Crypt::Mac::XCBC self) | |
117 | CODE: | |
118 | { | |
119 | unsigned char mac[MAXBLOCKSIZE]; | |
120 | unsigned long mac_len, i; | |
121 | int rv; | |
122 | char mac_hex[MAXBLOCKSIZE*2 + 1]; | |
123 | ||
124 | mac_len = sizeof(mac); | |
125 | rv = xcbc_done(&self->state, mac, &mac_len); | |
126 | if (rv != CRYPT_OK) croak("FATAL: xcbc_done failed: %s", error_to_string(rv)); | |
127 | mac_hex[0] = '\0'; | |
128 | for(i=0; i<mac_len; i++) | |
129 | sprintf(&mac_hex[2*i], "%02x", mac[i]); | |
130 | RETVAL = newSVpvn(mac_hex, strlen(mac_hex)); | |
131 | } | |
132 | OUTPUT: | |
133 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mode::CBC | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mode::CBC | |
5 | _new(char * cipher_name, int padding=1, int rounds=0) | |
6 | CODE: | |
7 | { | |
8 | Newz(0, RETVAL, 1, struct cbc_struct); | |
9 | if (!RETVAL) croak("FATAL: Newz failed"); | |
10 | RETVAL->padding_mode = padding; | |
11 | RETVAL->padlen = 0; | |
12 | RETVAL->direction = 0; | |
13 | RETVAL->cipher_rounds = rounds; | |
14 | RETVAL->cipher_id = find_cipher(cipher_name); | |
15 | if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
16 | } | |
17 | OUTPUT: | |
18 | RETVAL | |
19 | ||
20 | void | |
21 | DESTROY(Crypt::Mode::CBC self) | |
22 | CODE: | |
23 | Safefree(self); | |
24 | ||
25 | int | |
26 | _get_dir(Crypt::Mode::CBC self) | |
27 | CODE: | |
28 | RETVAL = self->direction; | |
29 | OUTPUT: | |
30 | RETVAL | |
31 | ||
32 | void | |
33 | _start(Crypt::Mode::CBC self, int dir, SV * key, SV * iv) | |
34 | CODE: | |
35 | { | |
36 | int rv; | |
37 | STRLEN k_len=0; | |
38 | unsigned char *k=NULL; | |
39 | STRLEN i_len=0; | |
40 | unsigned char *i=NULL; | |
41 | ||
42 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
43 | k = (unsigned char *) SvPVbyte(key, k_len); | |
44 | ||
45 | if (!SvPOK(iv)) croak("FATAL: iv must be string/buffer scalar"); | |
46 | i = (unsigned char *) SvPVbyte(iv, i_len); | |
47 | if (i_len != (STRLEN)cipher_descriptor[self->cipher_id].block_length) { | |
48 | croak ("FATAL: sizeof(iv) should be equal to blocksize (%d)", cipher_descriptor[self->cipher_id].block_length); | |
49 | } | |
50 | rv = cbc_start(self->cipher_id, i, k, (unsigned long)k_len, self->cipher_rounds, &self->state); | |
51 | if (rv != CRYPT_OK) { | |
52 | croak("FATAL: cbc_start failed: %s", error_to_string(rv)); | |
53 | } | |
54 | ||
55 | self->direction = dir; | |
56 | self->padlen = 0; | |
57 | } | |
58 | ||
59 | SV * | |
60 | _encrypt(Crypt::Mode::CBC self, SV * data) | |
61 | CODE: | |
62 | { | |
63 | int rv, has_tmp_block, blen; | |
64 | unsigned long i; | |
65 | ||
66 | STRLEN in_data_len, in_data_start; | |
67 | unsigned char *in_data, *out_data, tmp_block[MAXBLOCKSIZE]; | |
68 | ||
69 | if (self->direction != 1) croak("FATAL: encrypt error, call start_encrypt first (%d)", self->direction); | |
70 | ||
71 | blen = (&self->state)->blocklen; | |
72 | in_data_start = 0; | |
73 | has_tmp_block = 0; | |
74 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
75 | if (in_data_len==0) { | |
76 | RETVAL = newSVpvn("", 0); | |
77 | } | |
78 | else { | |
79 | if(self->padlen > 0) { | |
80 | i = (blen - self->padlen); | |
81 | if (in_data_len >= i) { /* enough data to fill pad */ | |
82 | Copy(in_data, self->pad+self->padlen, i, unsigned char); | |
83 | in_data_len -= i; | |
84 | in_data_start = i; | |
85 | rv = cbc_encrypt(self->pad, tmp_block, blen, &self->state); | |
86 | if (rv != CRYPT_OK) croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv)); | |
87 | self->padlen = 0; | |
88 | has_tmp_block = 1; | |
89 | } | |
90 | else { /* not enough data to fill pad */ | |
91 | Copy(in_data, self->pad+self->padlen, in_data_len, unsigned char); | |
92 | self->padlen += (int)in_data_len; | |
93 | in_data_len = 0; | |
94 | } | |
95 | } /* padlen > 0 */ | |
96 | ||
97 | i = (unsigned long)(in_data_len % blen); | |
98 | if (in_data_len>0 && i>0) { /* save tail of data into pad */ | |
99 | Copy(in_data+in_data_start+in_data_len-i, self->pad, i, unsigned char); | |
100 | self->padlen = i; | |
101 | in_data_len -= i; | |
102 | } | |
103 | ||
104 | if (in_data_len>0) { | |
105 | i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len); | |
106 | RETVAL = NEWSV(0, i); | |
107 | SvPOK_only(RETVAL); | |
108 | SvCUR_set(RETVAL, i); | |
109 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
110 | ||
111 | if (has_tmp_block) { | |
112 | Copy(tmp_block, out_data, blen, unsigned char); | |
113 | out_data += blen; | |
114 | } | |
115 | rv = cbc_encrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state); | |
116 | if (rv != CRYPT_OK) croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv)); | |
117 | } /* in_data_len>0 */ | |
118 | else if (has_tmp_block) { | |
119 | RETVAL = newSVpvn((char*)tmp_block, blen); | |
120 | } | |
121 | else { | |
122 | RETVAL = newSVpvn("", 0); | |
123 | } | |
124 | } | |
125 | } | |
126 | OUTPUT: | |
127 | RETVAL | |
128 | ||
129 | SV * | |
130 | _finish_enc(Crypt::Mode::CBC self) | |
131 | CODE: | |
132 | { | |
133 | unsigned char tmp_block[MAXBLOCKSIZE]; | |
134 | int rv, blen, i, j; | |
135 | ||
136 | blen = (&self->state)->blocklen; | |
137 | if (self->padlen<0 || self->padlen>=blen) croak("FATAL: invalid padlen"); | |
138 | ||
139 | if(self->padding_mode == 1) { /* pkcs5|7 padding */ | |
140 | i = blen - self->padlen; | |
141 | if (i == 0) i = blen; | |
142 | for(j=self->padlen; j<blen; j++) self->pad[j] = (unsigned char)i; | |
143 | rv = cbc_encrypt(self->pad, tmp_block, blen, &self->state); | |
144 | if (rv != CRYPT_OK) croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv)); | |
145 | } | |
146 | else if(self->padding_mode == 2) { /* oneandzeroes padding */ | |
147 | self->pad[self->padlen] = 0x80; | |
148 | for(j=self->padlen+1; j<blen; j++) self->pad[j] = 0; | |
149 | rv = cbc_encrypt(self->pad, tmp_block, blen, &self->state); | |
150 | if (rv != CRYPT_OK) croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv)); | |
151 | } | |
152 | else { | |
153 | if (self->padlen>0) croak("FATAL: cbc_encrypt, input data length not multiple of %d", blen); | |
154 | blen = 0; | |
155 | } | |
156 | ||
157 | self->direction = 0; | |
158 | RETVAL = newSVpvn((char*)tmp_block, blen); | |
159 | } | |
160 | OUTPUT: | |
161 | RETVAL | |
162 | ||
163 | SV * | |
164 | _decrypt(Crypt::Mode::CBC self, SV * data) | |
165 | CODE: | |
166 | { | |
167 | int rv, has_tmp_block, blen; | |
168 | unsigned long i; | |
169 | STRLEN in_data_len, in_data_start; | |
170 | unsigned char *in_data, *out_data, tmp_block[MAXBLOCKSIZE]; | |
171 | ||
172 | if (self->direction != -1) croak("FATAL: decrypt error, call start_decryt first (%d)", self->direction); | |
173 | ||
174 | blen = (&self->state)->blocklen; | |
175 | in_data_start = 0; | |
176 | has_tmp_block = 0; | |
177 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
178 | if (in_data_len==0) { | |
179 | RETVAL = newSVpvn("", 0); | |
180 | } | |
181 | else { | |
182 | ||
183 | if(self->padlen == blen) { | |
184 | rv = cbc_decrypt(self->pad, tmp_block, blen, &self->state); | |
185 | if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv)); | |
186 | self->padlen = 0; | |
187 | has_tmp_block = 1; | |
188 | } /* padlen == blen */ | |
189 | else if(self->padlen > 0) { | |
190 | i = (blen - self->padlen); /* remaining bytes in padding buffer */ | |
191 | if (in_data_len >= i) { /* enough data to fill pad */ | |
192 | Copy(in_data, self->pad+self->padlen, i, unsigned char); | |
193 | self->padlen += i; | |
194 | in_data_len -= i; | |
195 | in_data_start = i; | |
196 | if (in_data_len>0 || self->padding_mode == 0) { | |
197 | rv = cbc_decrypt(self->pad, tmp_block, blen, &self->state); | |
198 | if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv)); | |
199 | self->padlen = 0; | |
200 | has_tmp_block = 1; | |
201 | } | |
202 | } | |
203 | else { /* not enough data to fill pad */ | |
204 | Copy(in_data, self->pad+self->padlen, in_data_len, unsigned char); | |
205 | self->padlen += (int)in_data_len; | |
206 | in_data_len = 0; | |
207 | } | |
208 | } /* padlen > 0 */ | |
209 | ||
210 | /* here: a/ padlen==1..16 && in_data_len==0; b/ padlen==0 && in_data_len>0 */ | |
211 | if (in_data_len>0) { | |
212 | i = (unsigned long)(in_data_len % blen); | |
213 | if (i>0) { /* save tail of data into pad */ | |
214 | Copy(in_data+in_data_start+in_data_len-i, self->pad, i, unsigned char); | |
215 | self->padlen = i; | |
216 | in_data_len -= i; | |
217 | } | |
218 | } | |
219 | ||
220 | if (in_data_len>0) { | |
221 | if(self->padlen == 0 && self->padding_mode !=0) { | |
222 | /* in case of padding keep full pad if no more data */ | |
223 | Copy(in_data+in_data_start+in_data_len-blen, self->pad, blen, unsigned char); | |
224 | self->padlen = blen; | |
225 | in_data_len -= blen; | |
226 | } | |
227 | i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len); | |
228 | if (i == 0) { | |
229 | RETVAL = newSVpvn("", 0); | |
230 | } | |
231 | else { | |
232 | RETVAL = NEWSV(0, i); | |
233 | SvPOK_only(RETVAL); | |
234 | SvCUR_set(RETVAL, i); | |
235 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
236 | if (has_tmp_block) { | |
237 | Copy(tmp_block, out_data, blen, unsigned char); | |
238 | out_data += blen; | |
239 | } | |
240 | rv = cbc_decrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state); | |
241 | if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv)); | |
242 | } | |
243 | } /* in_data_len>0 */ | |
244 | else if (has_tmp_block) { | |
245 | RETVAL = newSVpvn((char*)tmp_block, blen); | |
246 | } | |
247 | else { | |
248 | RETVAL = newSVpvn("", 0); | |
249 | } | |
250 | } | |
251 | ||
252 | } | |
253 | OUTPUT: | |
254 | RETVAL | |
255 | ||
256 | SV * | |
257 | _finish_dec(Crypt::Mode::CBC self) | |
258 | CODE: | |
259 | { | |
260 | unsigned char tmp_block[MAXBLOCKSIZE]; | |
261 | unsigned char i; | |
262 | int rv, rv_len, blen; | |
263 | ||
264 | rv_len = 0; | |
265 | if (self->padlen > 0) { | |
266 | blen = (&self->state)->blocklen; | |
267 | if (self->padlen != blen) croak("FATAL: cipher text length has to be multiple of %d (%d)", blen, self->padlen); | |
268 | rv = cbc_decrypt(self->pad, tmp_block, blen, &self->state); | |
269 | if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv)); | |
270 | if(self->padding_mode == 0) { /* no padding */ | |
271 | rv_len = blen; | |
272 | } | |
273 | else if(self->padding_mode == 1) { /* pkcs5|7 padding */ | |
274 | i = tmp_block[blen-1]; | |
275 | rv_len = blen - (i>blen ? blen : i); | |
276 | } | |
277 | else if(self->padding_mode == 2) { /* oneandzeroes padding */ | |
278 | rv_len = blen; | |
279 | while ((unsigned char)tmp_block[rv_len-1] == 0x00) rv_len--; | |
280 | if ((unsigned char)tmp_block[rv_len-1] == 0x80) rv_len--; | |
281 | if (rv_len<0) rv_len = 0; | |
282 | } | |
283 | } | |
284 | ||
285 | self->direction = 0; | |
286 | RETVAL = newSVpvn((char*)tmp_block, rv_len); | |
287 | } | |
288 | OUTPUT: | |
289 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mode::CFB | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mode::CFB | |
5 | _new(char * cipher_name, int rounds=0) | |
6 | CODE: | |
7 | { | |
8 | Newz(0, RETVAL, 1, struct cfb_struct); | |
9 | if (!RETVAL) croak("FATAL: Newz failed"); | |
10 | RETVAL->direction = 0; | |
11 | RETVAL->cipher_rounds = rounds; | |
12 | RETVAL->cipher_id = find_cipher(cipher_name); | |
13 | if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
14 | } | |
15 | OUTPUT: | |
16 | RETVAL | |
17 | ||
18 | void | |
19 | DESTROY(Crypt::Mode::CFB self) | |
20 | CODE: | |
21 | Safefree(self); | |
22 | ||
23 | int | |
24 | _get_dir(Crypt::Mode::CFB self) | |
25 | CODE: | |
26 | RETVAL = self->direction; | |
27 | OUTPUT: | |
28 | RETVAL | |
29 | ||
30 | void | |
31 | _start(Crypt::Mode::CFB self, int dir, SV * key, SV * iv) | |
32 | CODE: | |
33 | { | |
34 | STRLEN k_len=0; | |
35 | unsigned char *k=NULL; | |
36 | STRLEN i_len=0; | |
37 | unsigned char *i=NULL; | |
38 | int rv; | |
39 | ||
40 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
41 | k = (unsigned char *) SvPVbyte(key, k_len); | |
42 | ||
43 | if (!SvPOK(iv)) croak("FATAL: iv must be string/buffer scalar"); | |
44 | i = (unsigned char *) SvPVbyte(iv, i_len); | |
45 | if (i_len != (STRLEN)cipher_descriptor[self->cipher_id].block_length) { | |
46 | croak ("FATAL: sizeof(iv) should be equal to blocksize (%d)", cipher_descriptor[self->cipher_id].block_length); | |
47 | } | |
48 | ||
49 | rv = cfb_start(self->cipher_id, i, k, (int)k_len, self->cipher_rounds, &self->state); | |
50 | if (rv != CRYPT_OK) { | |
51 | croak("FATAL: cfb_start failed: %s", error_to_string(rv)); | |
52 | } | |
53 | ||
54 | self->direction = dir; | |
55 | } | |
56 | ||
57 | SV * | |
58 | _crypt(Crypt::Mode::CFB self, SV * data) | |
59 | CODE: | |
60 | { | |
61 | int rv; | |
62 | STRLEN in_data_len; | |
63 | unsigned char *in_data, *out_data; | |
64 | ||
65 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
66 | if (in_data_len==0) { | |
67 | RETVAL = newSVpvn("", 0); | |
68 | } | |
69 | else { | |
70 | RETVAL = NEWSV(0, in_data_len); | |
71 | SvPOK_only(RETVAL); | |
72 | SvCUR_set(RETVAL, in_data_len); | |
73 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
74 | ||
75 | if (self->direction == 1) { | |
76 | rv = cfb_encrypt(in_data, out_data, (unsigned long)in_data_len, &self->state); | |
77 | if (rv != CRYPT_OK) croak("FATAL: cfb_encrypt failed: %s", error_to_string(rv)); | |
78 | } | |
79 | else if (self->direction == -1) { | |
80 | rv = cfb_decrypt(in_data, out_data, (unsigned long)in_data_len, &self->state); | |
81 | if (rv != CRYPT_OK) croak("FATAL: cfb_decrypt failed: %s", error_to_string(rv)); | |
82 | } | |
83 | else { | |
84 | croak("FATAL: cfb_crypt failed: call start_encrypt or start_decrypt first"); | |
85 | } | |
86 | } | |
87 | } | |
88 | OUTPUT: | |
89 | RETVAL | |
90 | ||
91 | SV * | |
92 | _finish(Crypt::Mode::CFB self) | |
93 | CODE: | |
94 | self->direction = 0; | |
95 | RETVAL = newSVpvn("", 0); | |
96 | OUTPUT: | |
97 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mode::CTR | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mode::CTR | |
5 | _new(char * cipher_name, int ctr_mode=0, int ctr_width=0, int rounds=0) | |
6 | CODE: | |
7 | { | |
8 | Newz(0, RETVAL, 1, struct ctr_struct); | |
9 | if (!RETVAL) croak("FATAL: Newz failed"); | |
10 | RETVAL->direction = 0; | |
11 | RETVAL->cipher_rounds = rounds; | |
12 | RETVAL->cipher_id = find_cipher(cipher_name); | |
13 | if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
14 | if (ctr_mode == 0) RETVAL->ctr_mode_param = CTR_COUNTER_LITTLE_ENDIAN; | |
15 | if (ctr_mode == 1) RETVAL->ctr_mode_param = CTR_COUNTER_BIG_ENDIAN; | |
16 | if (ctr_mode == 2) RETVAL->ctr_mode_param = CTR_COUNTER_LITTLE_ENDIAN|LTC_CTR_RFC3686; | |
17 | if (ctr_mode == 3) RETVAL->ctr_mode_param = CTR_COUNTER_BIG_ENDIAN|LTC_CTR_RFC3686; | |
18 | if (ctr_width > 0 && ctr_width <= cipher_descriptor[RETVAL->cipher_id].block_length) RETVAL->ctr_mode_param |= ctr_width; | |
19 | } | |
20 | OUTPUT: | |
21 | RETVAL | |
22 | ||
23 | void | |
24 | DESTROY(Crypt::Mode::CTR self) | |
25 | CODE: | |
26 | Safefree(self); | |
27 | ||
28 | int | |
29 | _get_dir(Crypt::Mode::CTR self) | |
30 | CODE: | |
31 | RETVAL = self->direction; | |
32 | OUTPUT: | |
33 | RETVAL | |
34 | ||
35 | void | |
36 | _start(Crypt::Mode::CTR self, int dir, SV * key, SV * iv) | |
37 | CODE: | |
38 | { | |
39 | STRLEN k_len=0; | |
40 | unsigned char *k=NULL; | |
41 | STRLEN i_len=0; | |
42 | unsigned char *i=NULL; | |
43 | int rv; | |
44 | ||
45 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
46 | k = (unsigned char *) SvPVbyte(key, k_len); | |
47 | ||
48 | if (!SvPOK(iv)) croak("FATAL: iv must be string/buffer scalar"); | |
49 | i = (unsigned char *) SvPVbyte(iv, i_len); | |
50 | if (i_len != (STRLEN)cipher_descriptor[self->cipher_id].block_length) { | |
51 | croak ("FATAL: sizeof(iv) should be equal to blocksize (%d)", cipher_descriptor[self->cipher_id].block_length); | |
52 | } | |
53 | ||
54 | rv = ctr_start(self->cipher_id, i, k, (int)k_len, self->cipher_rounds, self->ctr_mode_param, &self->state); | |
55 | if (rv != CRYPT_OK) { | |
56 | croak("FATAL: ctr_start failed: %s", error_to_string(rv)); | |
57 | } | |
58 | ||
59 | self->direction = dir; | |
60 | } | |
61 | ||
62 | SV * | |
63 | _crypt(Crypt::Mode::CTR self, SV * data) | |
64 | CODE: | |
65 | { | |
66 | int rv; | |
67 | STRLEN in_data_len; | |
68 | unsigned char *in_data, *out_data; | |
69 | ||
70 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
71 | if (in_data_len==0) { | |
72 | RETVAL = newSVpvn("", 0); | |
73 | } | |
74 | else { | |
75 | RETVAL = NEWSV(0, in_data_len); | |
76 | SvPOK_only(RETVAL); | |
77 | SvCUR_set(RETVAL, in_data_len); | |
78 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
79 | ||
80 | if (self->direction == 1) { | |
81 | rv = ctr_encrypt(in_data, out_data, (unsigned long)in_data_len, &self->state); | |
82 | if (rv != CRYPT_OK) croak("FATAL: ctr_encrypt failed: %s", error_to_string(rv)); | |
83 | } | |
84 | else if (self->direction == -1) { | |
85 | rv = ctr_decrypt(in_data, out_data, (unsigned long)in_data_len, &self->state); | |
86 | if (rv != CRYPT_OK) croak("FATAL: ctr_decrypt failed: %s", error_to_string(rv)); | |
87 | } | |
88 | else { | |
89 | croak("FATAL: ctr_crypt failed: call start_encrypt or start_decrypt first"); | |
90 | } | |
91 | } | |
92 | } | |
93 | OUTPUT: | |
94 | RETVAL | |
95 | ||
96 | SV * | |
97 | _finish(Crypt::Mode::CTR self) | |
98 | CODE: | |
99 | self->direction = 0; | |
100 | RETVAL = newSVpvn("", 0); | |
101 | OUTPUT: | |
102 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mode::ECB | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mode::ECB | |
5 | _new(char * cipher_name, int padding=1, int rounds=0) | |
6 | CODE: | |
7 | { | |
8 | Newz(0, RETVAL, 1, struct ecb_struct); | |
9 | if (!RETVAL) croak("FATAL: Newz failed"); | |
10 | RETVAL->padding_mode = padding; | |
11 | RETVAL->padlen = 0; | |
12 | RETVAL->direction = 0; | |
13 | RETVAL->cipher_rounds = rounds; | |
14 | RETVAL->cipher_id = find_cipher(cipher_name); | |
15 | if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
16 | } | |
17 | OUTPUT: | |
18 | RETVAL | |
19 | ||
20 | void | |
21 | DESTROY(Crypt::Mode::ECB self) | |
22 | CODE: | |
23 | Safefree(self); | |
24 | ||
25 | int | |
26 | _get_dir(Crypt::Mode::ECB self) | |
27 | CODE: | |
28 | RETVAL = self->direction; | |
29 | OUTPUT: | |
30 | RETVAL | |
31 | ||
32 | void | |
33 | _start(Crypt::Mode::ECB self, int dir, SV * key) | |
34 | CODE: | |
35 | { | |
36 | int rv; | |
37 | STRLEN k_len=0; | |
38 | unsigned char *k=NULL; | |
39 | ||
40 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
41 | k = (unsigned char *) SvPVbyte(key, k_len); | |
42 | ||
43 | rv = ecb_start(self->cipher_id, k, (unsigned long)k_len, self->cipher_rounds, &self->state); | |
44 | if (rv != CRYPT_OK) { | |
45 | croak("FATAL: ecb_start failed: %s", error_to_string(rv)); | |
46 | } | |
47 | ||
48 | self->direction = dir; | |
49 | self->padlen = 0; | |
50 | } | |
51 | ||
52 | SV * | |
53 | _encrypt(Crypt::Mode::ECB self, SV * data) | |
54 | CODE: | |
55 | { | |
56 | int rv, has_tmp_block, blen; | |
57 | unsigned long i; | |
58 | ||
59 | STRLEN in_data_len, in_data_start; | |
60 | unsigned char *in_data, *out_data, tmp_block[MAXBLOCKSIZE]; | |
61 | ||
62 | if (self->direction != 1) croak("FATAL: encrypt error, call start_encrypt first (%d)", self->direction); | |
63 | ||
64 | blen = (&self->state)->blocklen; | |
65 | in_data_start = 0; | |
66 | has_tmp_block = 0; | |
67 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
68 | if (in_data_len==0) { | |
69 | RETVAL = newSVpvn("", 0); | |
70 | } | |
71 | else { | |
72 | if(self->padlen > 0) { | |
73 | i = (blen - self->padlen); | |
74 | if (in_data_len >= i) { /* enough data to fill pad */ | |
75 | Copy(in_data, self->pad+self->padlen, i, unsigned char); | |
76 | in_data_len -= i; | |
77 | in_data_start = i; | |
78 | rv = ecb_encrypt(self->pad, tmp_block, blen, &self->state); | |
79 | if (rv != CRYPT_OK) croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv)); | |
80 | self->padlen = 0; | |
81 | has_tmp_block = 1; | |
82 | } | |
83 | else { /* not enough data to fill pad */ | |
84 | Copy(in_data, self->pad+self->padlen, in_data_len, unsigned char); | |
85 | self->padlen += (int)in_data_len; | |
86 | in_data_len = 0; | |
87 | } | |
88 | } /* padlen > 0 */ | |
89 | ||
90 | i = (unsigned long)(in_data_len % blen); | |
91 | if (in_data_len>0 && i>0) { /* save tail of data into pad */ | |
92 | Copy(in_data+in_data_start+in_data_len-i, self->pad, i, unsigned char); | |
93 | self->padlen = i; | |
94 | in_data_len -= i; | |
95 | } | |
96 | ||
97 | if (in_data_len>0) { | |
98 | i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len); | |
99 | RETVAL = NEWSV(0, i); | |
100 | SvPOK_only(RETVAL); | |
101 | SvCUR_set(RETVAL, i); | |
102 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
103 | ||
104 | if (has_tmp_block) { | |
105 | Copy(tmp_block, out_data, blen, unsigned char); | |
106 | out_data += blen; | |
107 | } | |
108 | rv = ecb_encrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state); | |
109 | if (rv != CRYPT_OK) croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv)); | |
110 | } /* in_data_len>0 */ | |
111 | else if (has_tmp_block) { | |
112 | RETVAL = newSVpvn((char*)tmp_block, blen); | |
113 | } | |
114 | else { | |
115 | RETVAL = newSVpvn("", 0); | |
116 | } | |
117 | } | |
118 | } | |
119 | OUTPUT: | |
120 | RETVAL | |
121 | ||
122 | SV * | |
123 | _finish_enc(Crypt::Mode::ECB self) | |
124 | CODE: | |
125 | { | |
126 | unsigned char tmp_block[MAXBLOCKSIZE]; | |
127 | int rv, blen, i, j; | |
128 | ||
129 | blen = (&self->state)->blocklen; | |
130 | if (self->padlen<0 || self->padlen>=blen) croak("FATAL: invalid padlen"); | |
131 | ||
132 | if(self->padding_mode == 1) { /* pkcs5|7 padding */ | |
133 | i = blen - self->padlen; | |
134 | if (i == 0) i = blen; | |
135 | for(j=self->padlen; j<blen; j++) self->pad[j] = (unsigned char)i; | |
136 | rv = ecb_encrypt(self->pad, tmp_block, blen, &self->state); | |
137 | if (rv != CRYPT_OK) croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv)); | |
138 | } | |
139 | else if(self->padding_mode == 2) { /* oneandzeroes padding */ | |
140 | self->pad[self->padlen] = 0x80; | |
141 | for(j=self->padlen+1; j<blen; j++) self->pad[j] = 0; | |
142 | rv = ecb_encrypt(self->pad, tmp_block, blen, &self->state); | |
143 | if (rv != CRYPT_OK) croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv)); | |
144 | } | |
145 | else { | |
146 | if (self->padlen>0) croak("FATAL: ecb_encrypt, input data length not multiple of %d", blen); | |
147 | blen = 0; | |
148 | } | |
149 | ||
150 | self->direction = 0; | |
151 | RETVAL = newSVpvn((char*)tmp_block, blen); | |
152 | } | |
153 | OUTPUT: | |
154 | RETVAL | |
155 | ||
156 | SV * | |
157 | _decrypt(Crypt::Mode::ECB self, SV * data) | |
158 | CODE: | |
159 | { | |
160 | int rv, has_tmp_block, blen; | |
161 | unsigned long i; | |
162 | STRLEN in_data_len, in_data_start; | |
163 | unsigned char *in_data, *out_data, tmp_block[MAXBLOCKSIZE]; | |
164 | ||
165 | if (self->direction != -1) croak("FATAL: decrypt error, call start_decryt first (%d)", self->direction); | |
166 | ||
167 | blen = (&self->state)->blocklen; | |
168 | in_data_start = 0; | |
169 | has_tmp_block = 0; | |
170 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
171 | if (in_data_len==0) { | |
172 | RETVAL = newSVpvn("", 0); | |
173 | } | |
174 | else { | |
175 | ||
176 | if(self->padlen == blen) { | |
177 | rv = ecb_decrypt(self->pad, tmp_block, blen, &self->state); | |
178 | if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv)); | |
179 | self->padlen = 0; | |
180 | has_tmp_block = 1; | |
181 | } /* padlen == blen */ | |
182 | else if(self->padlen > 0) { | |
183 | i = (blen - self->padlen); /* remaining bytes in padding buffer */ | |
184 | if (in_data_len >= i) { /* enough data to fill pad */ | |
185 | Copy(in_data, self->pad+self->padlen, i, unsigned char); | |
186 | self->padlen += i; | |
187 | in_data_len -= i; | |
188 | in_data_start = i; | |
189 | if (in_data_len>0 || self->padding_mode == 0) { | |
190 | rv = ecb_decrypt(self->pad, tmp_block, blen, &self->state); | |
191 | if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv)); | |
192 | self->padlen = 0; | |
193 | has_tmp_block = 1; | |
194 | } | |
195 | } | |
196 | else { /* not enough data to fill pad */ | |
197 | Copy(in_data, self->pad+self->padlen, in_data_len, unsigned char); | |
198 | self->padlen += (int)in_data_len; | |
199 | in_data_len = 0; | |
200 | } | |
201 | } /* padlen > 0 */ | |
202 | ||
203 | /* here: a/ padlen==1..16 && in_data_len==0; b/ padlen==0 && in_data_len>0 */ | |
204 | if (in_data_len>0) { | |
205 | i = (unsigned long)(in_data_len % blen); | |
206 | if (i>0) { /* save tail of data into pad */ | |
207 | Copy(in_data+in_data_start+in_data_len-i, self->pad, i, unsigned char); | |
208 | self->padlen = i; | |
209 | in_data_len -= i; | |
210 | } | |
211 | } | |
212 | ||
213 | if (in_data_len>0) { | |
214 | if(self->padlen == 0 && self->padding_mode !=0) { | |
215 | /* in case of padding keep full pad if no more data */ | |
216 | Copy(in_data+in_data_start+in_data_len-blen, self->pad, blen, unsigned char); | |
217 | self->padlen = blen; | |
218 | in_data_len -= blen; | |
219 | } | |
220 | i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len); | |
221 | if (i == 0) { | |
222 | RETVAL = newSVpvn("", 0); | |
223 | } | |
224 | else { | |
225 | RETVAL = NEWSV(0, i); | |
226 | SvPOK_only(RETVAL); | |
227 | SvCUR_set(RETVAL, i); | |
228 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
229 | if (has_tmp_block) { | |
230 | Copy(tmp_block, out_data, blen, unsigned char); | |
231 | out_data += blen; | |
232 | } | |
233 | rv = ecb_decrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state); | |
234 | if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv)); | |
235 | } | |
236 | } /* in_data_len>0 */ | |
237 | else if (has_tmp_block) { | |
238 | RETVAL = newSVpvn((char*)tmp_block, blen); | |
239 | } | |
240 | else { | |
241 | RETVAL = newSVpvn("", 0); | |
242 | } | |
243 | } | |
244 | ||
245 | } | |
246 | OUTPUT: | |
247 | RETVAL | |
248 | ||
249 | SV * | |
250 | _finish_dec(Crypt::Mode::ECB self) | |
251 | CODE: | |
252 | { | |
253 | unsigned char tmp_block[MAXBLOCKSIZE]; | |
254 | unsigned char i; | |
255 | int rv, rv_len, blen; | |
256 | ||
257 | rv_len = 0; | |
258 | if (self->padlen > 0) { | |
259 | blen = (&self->state)->blocklen; | |
260 | if (self->padlen != blen) croak("FATAL: cipher text length has to be multiple of %d (%d)", blen, self->padlen); | |
261 | rv = ecb_decrypt(self->pad, tmp_block, blen, &self->state); | |
262 | if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv)); | |
263 | if(self->padding_mode == 0) { /* no padding */ | |
264 | rv_len = blen; | |
265 | } | |
266 | else if(self->padding_mode == 1) { /* pkcs5|7 padding */ | |
267 | i = tmp_block[blen-1]; | |
268 | rv_len = blen - (i>blen ? blen : i); | |
269 | } | |
270 | else if(self->padding_mode == 2) { /* oneandzeroes padding */ | |
271 | rv_len = blen; | |
272 | while ((unsigned char)tmp_block[rv_len-1] == 0x00) rv_len--; | |
273 | if ((unsigned char)tmp_block[rv_len-1] == 0x80) rv_len--; | |
274 | if (rv_len<0) rv_len = 0; | |
275 | } | |
276 | } | |
277 | ||
278 | self->direction = 0; | |
279 | RETVAL = newSVpvn((char*)tmp_block, rv_len); | |
280 | } | |
281 | OUTPUT: | |
282 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mode::OFB | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mode::OFB | |
5 | _new(char * cipher_name, int rounds=0) | |
6 | CODE: | |
7 | { | |
8 | Newz(0, RETVAL, 1, struct ofb_struct); | |
9 | if (!RETVAL) croak("FATAL: Newz failed"); | |
10 | RETVAL->direction = 0; | |
11 | RETVAL->cipher_rounds = rounds; | |
12 | RETVAL->cipher_id = find_cipher(cipher_name); | |
13 | if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
14 | } | |
15 | OUTPUT: | |
16 | RETVAL | |
17 | ||
18 | void | |
19 | DESTROY(Crypt::Mode::OFB self) | |
20 | CODE: | |
21 | Safefree(self); | |
22 | ||
23 | int | |
24 | _get_dir(Crypt::Mode::OFB self) | |
25 | CODE: | |
26 | RETVAL = self->direction; | |
27 | OUTPUT: | |
28 | RETVAL | |
29 | ||
30 | void | |
31 | _start(Crypt::Mode::OFB self, int dir, SV * key, SV * iv) | |
32 | CODE: | |
33 | { | |
34 | STRLEN k_len=0; | |
35 | unsigned char *k=NULL; | |
36 | STRLEN i_len=0; | |
37 | unsigned char *i=NULL; | |
38 | int rv; | |
39 | ||
40 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
41 | k = (unsigned char *) SvPVbyte(key, k_len); | |
42 | ||
43 | if (!SvPOK(iv)) croak("FATAL: iv must be string/buffer scalar"); | |
44 | i = (unsigned char *) SvPVbyte(iv, i_len); | |
45 | if (i_len != (STRLEN)cipher_descriptor[self->cipher_id].block_length) { | |
46 | croak ("FATAL: sizeof(iv) should be equal to blocksize (%d)", cipher_descriptor[self->cipher_id].block_length); | |
47 | } | |
48 | ||
49 | rv = ofb_start(self->cipher_id, i, k, (int)k_len, self->cipher_rounds, &self->state); | |
50 | if (rv != CRYPT_OK) { | |
51 | croak("FATAL: ofb_start failed: %s", error_to_string(rv)); | |
52 | } | |
53 | ||
54 | self->direction = dir; | |
55 | } | |
56 | ||
57 | SV * | |
58 | _crypt(Crypt::Mode::OFB self, SV * data) | |
59 | CODE: | |
60 | { | |
61 | int rv; | |
62 | STRLEN in_data_len; | |
63 | unsigned char *in_data, *out_data; | |
64 | ||
65 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
66 | if (in_data_len==0) { | |
67 | RETVAL = newSVpvn("", 0); | |
68 | } | |
69 | else { | |
70 | RETVAL = NEWSV(0, in_data_len); | |
71 | SvPOK_only(RETVAL); | |
72 | SvCUR_set(RETVAL, in_data_len); | |
73 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
74 | ||
75 | if (self->direction == 1) { | |
76 | rv = ofb_encrypt(in_data, out_data, (unsigned long)in_data_len, &self->state); | |
77 | if (rv != CRYPT_OK) croak("FATAL: ofb_encrypt failed: %s", error_to_string(rv)); | |
78 | } | |
79 | else if (self->direction == -1) { | |
80 | rv = ofb_decrypt(in_data, out_data, (unsigned long)in_data_len, &self->state); | |
81 | if (rv != CRYPT_OK) croak("FATAL: ofb_decrypt failed: %s", error_to_string(rv)); | |
82 | } | |
83 | else { | |
84 | croak("FATAL: ofb_crypt failed: call start_encrypt or start_decrypt first"); | |
85 | } | |
86 | } | |
87 | } | |
88 | OUTPUT: | |
89 | RETVAL | |
90 | ||
91 | SV * | |
92 | _finish(Crypt::Mode::OFB self) | |
93 | CODE: | |
94 | self->direction = 0; | |
95 | RETVAL = newSVpvn("", 0); | |
96 | OUTPUT: | |
97 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::PK::DH | |
1 | ||
2 | Crypt::PK::DH | |
3 | _new() | |
4 | CODE: | |
5 | { | |
6 | int rv; | |
7 | Newz(0, RETVAL, 1, struct dh_struct); | |
8 | if (!RETVAL) croak("FATAL: Newz failed"); | |
9 | RETVAL->key.type = -1; | |
10 | RETVAL->yarrow_prng_index = find_prng("yarrow"); | |
11 | if(RETVAL->yarrow_prng_index==-1) croak("FATAL: find_prng('yarrow') failed"); | |
12 | rv = rng_make_prng(256, RETVAL->yarrow_prng_index, &RETVAL->yarrow_prng_state, NULL); | |
13 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
14 | } | |
15 | OUTPUT: | |
16 | RETVAL | |
17 | ||
18 | void | |
19 | generate_key(Crypt::PK::DH self, int key_size=256) | |
20 | PPCODE: | |
21 | { | |
22 | int rv; | |
23 | /* add a small random entropy before generating key - not necessary as we have initialized prng with 256bit entropy in _new() */ | |
24 | rv = rng_make_prng(64, self->yarrow_prng_index, &self->yarrow_prng_state, NULL); | |
25 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
26 | /* gen the key */ | |
27 | rv = dh_make_key(&self->yarrow_prng_state, self->yarrow_prng_index, key_size, &self->key); | |
28 | if (rv != CRYPT_OK) croak("FATAL: dh_make_key failed: %s", error_to_string(rv)); | |
29 | XPUSHs(ST(0)); /* return self */ | |
30 | } | |
31 | ||
32 | void | |
33 | _import(Crypt::PK::DH self, SV * key_data) | |
34 | PPCODE: | |
35 | { | |
36 | int rv; | |
37 | unsigned char *data=NULL; | |
38 | STRLEN data_len=0; | |
39 | ||
40 | data = (unsigned char *)SvPVbyte(key_data, data_len); | |
41 | if (self->key.type != -1) { dh_free(&self->key); self->key.type = -1; } | |
42 | rv = dh_import(data, (unsigned long)data_len, &self->key); | |
43 | if (rv != CRYPT_OK) croak("FATAL: dh_import failed: %s", error_to_string(rv)); | |
44 | XPUSHs(ST(0)); /* return self */ | |
45 | } | |
46 | ||
47 | int | |
48 | is_private(Crypt::PK::DH self) | |
49 | CODE: | |
50 | if (self->key.type == -1) XSRETURN_UNDEF; | |
51 | RETVAL = (self->key.type == PK_PRIVATE) ? 1 : 0; | |
52 | OUTPUT: | |
53 | RETVAL | |
54 | ||
55 | int | |
56 | size(Crypt::PK::DH self) | |
57 | CODE: | |
58 | if (self->key.type == -1) XSRETURN_UNDEF; | |
59 | RETVAL = dh_get_size(&self->key); | |
60 | OUTPUT: | |
61 | RETVAL | |
62 | ||
63 | SV* | |
64 | key2hash(Crypt::PK::DH self) | |
65 | PREINIT: | |
66 | HV *rv_hash; | |
67 | long siz; | |
68 | char buf[20001]; | |
69 | SV **not_used; | |
70 | CODE: | |
71 | if (self->key.type == -1) XSRETURN_UNDEF; | |
72 | rv_hash = newHV(); | |
73 | /* =====> x */ | |
74 | siz = (self->key.x) ? mp_unsigned_bin_size(self->key.x) : 0; | |
75 | if (siz>10000) { | |
76 | croak("FATAL: key2hash failed - 'x' too big number"); | |
77 | } | |
78 | if (siz>0) { | |
79 | mp_tohex(self->key.x, buf); | |
80 | not_used = hv_store(rv_hash, "x", 1, newSVpv(buf, strlen(buf)), 0); | |
81 | } | |
82 | else{ | |
83 | not_used = hv_store(rv_hash, "x", 1, newSVpv("", 0), 0); | |
84 | } | |
85 | /* =====> y */ | |
86 | siz = (self->key.y) ? mp_unsigned_bin_size(self->key.y) : 0; | |
87 | if (siz>10000) { | |
88 | croak("FATAL: key2hash failed - 'y' too big number"); | |
89 | } | |
90 | if (siz>0) { | |
91 | mp_tohex(self->key.y, buf); | |
92 | not_used = hv_store(rv_hash, "y", 1, newSVpv(buf, strlen(buf)), 0); | |
93 | } | |
94 | else{ | |
95 | not_used = hv_store(rv_hash, "y", 1, newSVpv("", 0), 0); | |
96 | } | |
97 | /* =====> name */ | |
98 | snprintf(buf, sizeof(buf), "DH-%d", dh_get_size(&self->key)*8); | |
99 | not_used = hv_store(rv_hash, "name", 4, newSVpv(buf, strlen(buf)), 0); | |
100 | /* =====> size */ | |
101 | not_used = hv_store(rv_hash, "size", 4, newSViv(dh_get_size(&self->key)), 0); | |
102 | /* =====> type */ | |
103 | not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0); | |
104 | if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */ | |
105 | RETVAL = newRV_noinc((SV*)rv_hash); | |
106 | OUTPUT: | |
107 | RETVAL | |
108 | ||
109 | SV * | |
110 | export_key(Crypt::PK::DH self, char * type) | |
111 | CODE: | |
112 | { | |
113 | int rv; | |
114 | unsigned char out[4096]; | |
115 | unsigned long int out_len = 4096; | |
116 | ||
117 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
118 | if (strnEQ(type, "private", 7)) { | |
119 | rv = dh_export(out, &out_len, PK_PRIVATE, &self->key); | |
120 | if (rv != CRYPT_OK) croak("FATAL: dh_export(PK_PRIVATE) failed: %s", error_to_string(rv)); | |
121 | RETVAL = newSVpvn((char*)out, out_len); | |
122 | } | |
123 | else if (strnEQ(type, "public", 6)) { | |
124 | rv = dh_export(out, &out_len, PK_PUBLIC, &self->key); | |
125 | if (rv != CRYPT_OK) croak("FATAL: dh_export(PK_PUBLIC) failed: %s", error_to_string(rv)); | |
126 | RETVAL = newSVpvn((char*)out, out_len); | |
127 | } | |
128 | else { | |
129 | croak("FATAL: export_key_der invalid type '%s'", type); | |
130 | } | |
131 | } | |
132 | OUTPUT: | |
133 | RETVAL | |
134 | ||
135 | SV * | |
136 | _encrypt(Crypt::PK::DH self, SV * data, char * hash_name) | |
137 | CODE: | |
138 | { | |
139 | int rv, hash_id; | |
140 | unsigned char *data_ptr=NULL; | |
141 | STRLEN data_len=0; | |
142 | unsigned char buffer[1024]; | |
143 | unsigned long buffer_len = 1024; | |
144 | ||
145 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
146 | ||
147 | hash_id = find_hash(hash_name); | |
148 | if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
149 | rv = dh_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, | |
150 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
151 | hash_id, &self->key); | |
152 | if (rv != CRYPT_OK) croak("FATAL: dh_encrypt_key failed: %s", error_to_string(rv)); | |
153 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
154 | } | |
155 | OUTPUT: | |
156 | RETVAL | |
157 | ||
158 | SV * | |
159 | _decrypt(Crypt::PK::DH self, SV * data) | |
160 | CODE: | |
161 | { | |
162 | int rv; | |
163 | unsigned char *data_ptr=NULL; | |
164 | STRLEN data_len=0; | |
165 | unsigned char buffer[1024]; | |
166 | unsigned long buffer_len = 1024; | |
167 | ||
168 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
169 | ||
170 | rv = dh_decrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &self->key); | |
171 | if (rv != CRYPT_OK) croak("FATAL: dh_decrypt_key_ex failed: %s", error_to_string(rv)); | |
172 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
173 | } | |
174 | OUTPUT: | |
175 | RETVAL | |
176 | ||
177 | SV * | |
178 | _sign(Crypt::PK::DH self, SV * data) | |
179 | CODE: | |
180 | { | |
181 | int rv; | |
182 | unsigned char *data_ptr=NULL; | |
183 | STRLEN data_len=0; | |
184 | unsigned char buffer[1024]; | |
185 | unsigned long buffer_len = 1024; | |
186 | ||
187 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
188 | ||
189 | rv = dh_sign_hash(data_ptr, (unsigned long)data_len, buffer, &buffer_len, | |
190 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
191 | &self->key); | |
192 | if (rv != CRYPT_OK) croak("FATAL: dh_sign_hash_ex failed: %s", error_to_string(rv)); | |
193 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
194 | } | |
195 | OUTPUT: | |
196 | RETVAL | |
197 | ||
198 | int | |
199 | _verify(Crypt::PK::DH self, SV * sig, SV * data) | |
200 | CODE: | |
201 | { | |
202 | int rv, stat; | |
203 | unsigned char *data_ptr=NULL; | |
204 | STRLEN data_len=0; | |
205 | unsigned char *sig_ptr=NULL; | |
206 | STRLEN sig_len=0; | |
207 | ||
208 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
209 | sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len); | |
210 | ||
211 | RETVAL = 1; | |
212 | rv = dh_verify_hash(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key); | |
213 | if (rv != CRYPT_OK || stat != 1) RETVAL = 0; | |
214 | } | |
215 | OUTPUT: | |
216 | RETVAL | |
217 | ||
218 | SV * | |
219 | shared_secret(Crypt::PK::DH self, Crypt::PK::DH pubkey) | |
220 | CODE: | |
221 | { | |
222 | int rv; | |
223 | unsigned char buffer[1024]; | |
224 | unsigned long buffer_len = 1024; | |
225 | ||
226 | rv = dh_shared_secret(&self->key, &pubkey->key, buffer, &buffer_len); | |
227 | if (rv != CRYPT_OK) croak("FATAL: dh_shared_secret failed: %s", error_to_string(rv)); | |
228 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
229 | } | |
230 | OUTPUT: | |
231 | RETVAL | |
232 | ||
233 | void | |
234 | DESTROY(Crypt::PK::DH self) | |
235 | CODE: | |
236 | if (self->key.type != -1) { dh_free(&self->key); self->key.type = -1; } | |
237 | Safefree(self); | |
238 |
0 | MODULE = CryptX PACKAGE = Crypt::PK::DSA | |
1 | ||
2 | Crypt::PK::DSA | |
3 | _new() | |
4 | CODE: | |
5 | { | |
6 | int rv; | |
7 | Newz(0, RETVAL, 1, struct dsa_struct); | |
8 | if (!RETVAL) croak("FATAL: Newz failed"); | |
9 | RETVAL->key.type = -1; | |
10 | RETVAL->yarrow_prng_index = find_prng("yarrow"); | |
11 | if(RETVAL->yarrow_prng_index==-1) croak("FATAL: find_prng('yarrow') failed"); | |
12 | rv = rng_make_prng(256, RETVAL->yarrow_prng_index, &RETVAL->yarrow_prng_state, NULL); | |
13 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
14 | } | |
15 | OUTPUT: | |
16 | RETVAL | |
17 | ||
18 | void | |
19 | generate_key(Crypt::PK::DSA self, int group_size=30, int modulus_size=256) | |
20 | PPCODE: | |
21 | { | |
22 | int rv; | |
23 | /* add a small random entropy before generating key - not necessary as we have initialized prng with 256bit entropy in _new() */ | |
24 | rv = rng_make_prng(64, self->yarrow_prng_index, &self->yarrow_prng_state, NULL); | |
25 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
26 | /* gen the key */ | |
27 | rv = dsa_make_key(&self->yarrow_prng_state, self->yarrow_prng_index, group_size, modulus_size, &self->key); | |
28 | if (rv != CRYPT_OK) croak("FATAL: dsa_make_key failed: %s", error_to_string(rv)); | |
29 | XPUSHs(ST(0)); /* return self */ | |
30 | } | |
31 | ||
32 | void | |
33 | _import(Crypt::PK::DSA self, SV * key_data) | |
34 | PPCODE: | |
35 | { | |
36 | int rv; | |
37 | unsigned char *data=NULL; | |
38 | STRLEN data_len=0; | |
39 | ||
40 | data = (unsigned char *)SvPVbyte(key_data, data_len); | |
41 | if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; } | |
42 | rv = dsa_import(data, (unsigned long)data_len, &self->key); | |
43 | if (rv != CRYPT_OK) croak("FATAL: dsa_import failed: %s", error_to_string(rv)); | |
44 | XPUSHs(ST(0)); /* return self */ | |
45 | } | |
46 | ||
47 | void | |
48 | _import_hex(Crypt::PK::DSA self, char *p, char *q, char *g, char *x, char *y) | |
49 | PPCODE: | |
50 | { | |
51 | int rv; | |
52 | if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; } | |
53 | rv = dsa_import_hex(p, q, g, x, y, &self->key); | |
54 | if (rv != CRYPT_OK) croak("FATAL: dsa_import_hex failed: %s", error_to_string(rv)); | |
55 | XPUSHs(ST(0)); /* return self */ | |
56 | } | |
57 | ||
58 | int | |
59 | is_private(Crypt::PK::DSA self) | |
60 | CODE: | |
61 | if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF; | |
62 | RETVAL = (self->key.type == PK_PRIVATE) ? 1 : 0; | |
63 | OUTPUT: | |
64 | RETVAL | |
65 | ||
66 | int | |
67 | size(Crypt::PK::DSA self) | |
68 | CODE: | |
69 | if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF; | |
70 | RETVAL = mp_unsigned_bin_size(self->key.g); | |
71 | OUTPUT: | |
72 | RETVAL | |
73 | ||
74 | int | |
75 | size_q(Crypt::PK::DSA self) | |
76 | CODE: | |
77 | if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF; | |
78 | RETVAL = self->key.qord; | |
79 | OUTPUT: | |
80 | RETVAL | |
81 | ||
82 | SV* | |
83 | key2hash(Crypt::PK::DSA self) | |
84 | PREINIT: | |
85 | HV *rv_hash; | |
86 | long siz; | |
87 | char buf[20001]; | |
88 | SV **not_used; | |
89 | CODE: | |
90 | if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF; | |
91 | rv_hash = newHV(); | |
92 | /* =====> g */ | |
93 | siz = (self->key.g) ? mp_unsigned_bin_size(self->key.g) : 0; | |
94 | if (siz>10000) { | |
95 | croak("FATAL: key2hash failed - 'g' too big number"); | |
96 | } | |
97 | if (siz>0) { | |
98 | mp_tohex(self->key.g, buf); | |
99 | not_used = hv_store(rv_hash, "g", 1, newSVpv(buf, strlen(buf)), 0); | |
100 | } | |
101 | else{ | |
102 | not_used = hv_store(rv_hash, "g", 1, newSVpv("", 0), 0); | |
103 | } | |
104 | /* =====> q */ | |
105 | siz = (self->key.q) ? mp_unsigned_bin_size(self->key.q) : 0; | |
106 | if (siz>10000) { | |
107 | croak("FATAL: key2hash failed - 'q' too big number"); | |
108 | } | |
109 | if (siz>0) { | |
110 | mp_tohex(self->key.q, buf); | |
111 | not_used = hv_store(rv_hash, "q", 1, newSVpv(buf, strlen(buf)), 0); | |
112 | } | |
113 | else{ | |
114 | not_used = hv_store(rv_hash, "q", 1, newSVpv("", 0), 0); | |
115 | } | |
116 | /* =====> p */ | |
117 | siz = (self->key.p) ? mp_unsigned_bin_size(self->key.p) : 0; | |
118 | if (siz>10000) { | |
119 | croak("FATAL: key2hash failed - 'p' too big number"); | |
120 | } | |
121 | if (siz>0) { | |
122 | mp_tohex(self->key.p, buf); | |
123 | not_used = hv_store(rv_hash, "p", 1, newSVpv(buf, strlen(buf)), 0); | |
124 | } | |
125 | else{ | |
126 | not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0); | |
127 | } | |
128 | /* =====> x */ | |
129 | siz = (self->key.x) ? mp_unsigned_bin_size(self->key.x) : 0; | |
130 | if (siz>10000) { | |
131 | croak("FATAL: key2hash failed - 'x' too big number"); | |
132 | } | |
133 | if (siz>0) { | |
134 | mp_tohex(self->key.x, buf); | |
135 | not_used = hv_store(rv_hash, "x", 1, newSVpv(buf, strlen(buf)), 0); | |
136 | } | |
137 | else{ | |
138 | not_used = hv_store(rv_hash, "x", 1, newSVpv("", 0), 0); | |
139 | } | |
140 | /* =====> y */ | |
141 | siz = (self->key.y) ? mp_unsigned_bin_size(self->key.y) : 0; | |
142 | if (siz>10000) { | |
143 | croak("FATAL: key2hash failed - 'y' too big number"); | |
144 | } | |
145 | if (siz>0) { | |
146 | mp_tohex(self->key.y, buf); | |
147 | not_used = hv_store(rv_hash, "y", 1, newSVpv(buf, strlen(buf)), 0); | |
148 | } | |
149 | else{ | |
150 | not_used = hv_store(rv_hash, "y", 1, newSVpv("", 0), 0); | |
151 | } | |
152 | /* =====> size */ | |
153 | not_used = hv_store(rv_hash, "size", 4, newSViv(mp_unsigned_bin_size(self->key.q)), 0); | |
154 | /* =====> type */ | |
155 | not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0); | |
156 | if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */ | |
157 | RETVAL = newRV_noinc((SV*)rv_hash); | |
158 | OUTPUT: | |
159 | RETVAL | |
160 | ||
161 | SV * | |
162 | export_key_der(Crypt::PK::DSA self, char * type) | |
163 | CODE: | |
164 | { | |
165 | int rv; | |
166 | unsigned char out[4096]; | |
167 | unsigned long int out_len = 4096; | |
168 | ||
169 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
170 | if (strnEQ(type, "private", 7)) { | |
171 | rv = dsa_export(out, &out_len, PK_PRIVATE, &self->key); | |
172 | if (rv != CRYPT_OK) croak("FATAL: dsa_export(PK_PRIVATE) failed: %s", error_to_string(rv)); | |
173 | RETVAL = newSVpvn((char*)out, out_len); | |
174 | } | |
175 | else if (strnEQ(type, "public", 6)) { | |
176 | rv = dsa_export(out, &out_len, PK_PUBLIC, &self->key); | |
177 | if (rv != CRYPT_OK) croak("FATAL: dsa_export(PK_PUBLIC) failed: %s", error_to_string(rv)); | |
178 | RETVAL = newSVpvn((char*)out, out_len); | |
179 | } | |
180 | else { | |
181 | croak("FATAL: export_key_der invalid type '%s'", type); | |
182 | } | |
183 | } | |
184 | OUTPUT: | |
185 | RETVAL | |
186 | ||
187 | SV * | |
188 | _encrypt(Crypt::PK::DSA self, SV * data, char * hash_name) | |
189 | CODE: | |
190 | { | |
191 | int rv, hash_id; | |
192 | unsigned char *data_ptr=NULL; | |
193 | STRLEN data_len=0; | |
194 | unsigned char buffer[1024]; | |
195 | unsigned long buffer_len = 1024; | |
196 | ||
197 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
198 | ||
199 | hash_id = find_hash(hash_name); | |
200 | if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
201 | rv = dsa_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, | |
202 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
203 | hash_id, &self->key); | |
204 | if (rv != CRYPT_OK) croak("FATAL: dsa_encrypt_key failed: %s", error_to_string(rv)); | |
205 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
206 | } | |
207 | OUTPUT: | |
208 | RETVAL | |
209 | ||
210 | SV * | |
211 | _decrypt(Crypt::PK::DSA self, SV * data) | |
212 | CODE: | |
213 | { | |
214 | int rv; | |
215 | unsigned char *data_ptr=NULL; | |
216 | STRLEN data_len=0; | |
217 | unsigned char buffer[1024]; | |
218 | unsigned long buffer_len = 1024; | |
219 | ||
220 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
221 | ||
222 | rv = dsa_decrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &self->key); | |
223 | if (rv != CRYPT_OK) croak("FATAL: dsa_decrypt_key_ex failed: %s", error_to_string(rv)); | |
224 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
225 | } | |
226 | OUTPUT: | |
227 | RETVAL | |
228 | ||
229 | SV * | |
230 | _sign(Crypt::PK::DSA self, SV * data) | |
231 | CODE: | |
232 | { | |
233 | int rv; | |
234 | unsigned char *data_ptr=NULL; | |
235 | STRLEN data_len=0; | |
236 | unsigned char buffer[1024]; | |
237 | unsigned long buffer_len = 1024; | |
238 | ||
239 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
240 | ||
241 | rv = dsa_sign_hash(data_ptr, (unsigned long)data_len, buffer, &buffer_len, | |
242 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
243 | &self->key); | |
244 | if (rv != CRYPT_OK) croak("FATAL: dsa_sign_hash_ex failed: %s", error_to_string(rv)); | |
245 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
246 | } | |
247 | OUTPUT: | |
248 | RETVAL | |
249 | ||
250 | int | |
251 | _verify(Crypt::PK::DSA self, SV * sig, SV * data) | |
252 | CODE: | |
253 | { | |
254 | int rv, stat; | |
255 | unsigned char *data_ptr=NULL; | |
256 | STRLEN data_len=0; | |
257 | unsigned char *sig_ptr=NULL; | |
258 | STRLEN sig_len=0; | |
259 | ||
260 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
261 | sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len); | |
262 | ||
263 | RETVAL = 1; | |
264 | rv = dsa_verify_hash(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key); | |
265 | if (rv != CRYPT_OK || stat != 1) RETVAL = 0; | |
266 | } | |
267 | OUTPUT: | |
268 | RETVAL | |
269 | ||
270 | void | |
271 | DESTROY(Crypt::PK::DSA self) | |
272 | CODE: | |
273 | if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; } | |
274 | Safefree(self); | |
275 |
0 | MODULE = CryptX PACKAGE = Crypt::PK::ECC | |
1 | ||
2 | Crypt::PK::ECC | |
3 | _new() | |
4 | CODE: | |
5 | { | |
6 | int rv; | |
7 | Newz(0, RETVAL, 1, struct ecc_struct); | |
8 | if (!RETVAL) croak("FATAL: Newz failed"); | |
9 | RETVAL->yarrow_prng_index = find_prng("yarrow"); | |
10 | RETVAL->key.type = -1; | |
11 | ecc_dp_init(&RETVAL->dp); | |
12 | if(RETVAL->yarrow_prng_index==-1) croak("FATAL: find_prng('yarrow') failed"); | |
13 | rv = rng_make_prng(256, RETVAL->yarrow_prng_index, &RETVAL->yarrow_prng_state, NULL); | |
14 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
15 | } | |
16 | OUTPUT: | |
17 | RETVAL | |
18 | ||
19 | void | |
20 | generate_key(Crypt::PK::ECC self, SV *curve) | |
21 | PPCODE: | |
22 | { | |
23 | int rv; | |
24 | /* add a small random entropy before generating key - not necessary as we have initialized prng with 256bit entropy in _new() */ | |
25 | rv = rng_make_prng(64, self->yarrow_prng_index, &self->yarrow_prng_state, NULL); | |
26 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
27 | /* setup dp structure */ | |
28 | _ecc_set_dp_from_SV(&self->dp, curve); /* croaks on error */ | |
29 | /* gen the key */ | |
30 | rv = ecc_make_key_ex(&self->yarrow_prng_state, self->yarrow_prng_index, &self->key, &self->dp); | |
31 | if (rv != CRYPT_OK) croak("FATAL: ecc_make_key_ex failed: %s", error_to_string(rv)); | |
32 | XPUSHs(ST(0)); /* return self */ | |
33 | } | |
34 | ||
35 | void | |
36 | _import(Crypt::PK::ECC self, SV * key_data) | |
37 | PPCODE: | |
38 | { | |
39 | int rv; | |
40 | unsigned char *data=NULL; | |
41 | STRLEN data_len=0; | |
42 | ||
43 | data = (unsigned char *)SvPVbyte(key_data, data_len); | |
44 | _ecc_free_key(&self->key, &self->dp); | |
45 | rv = ecc_import_full(data, (unsigned long)data_len, &self->key, &self->dp); | |
46 | if (rv != CRYPT_OK) croak("FATAL: ecc_import_full failed: %s", error_to_string(rv)); | |
47 | XPUSHs(ST(0)); /* return self */ | |
48 | } | |
49 | ||
50 | void | |
51 | _import_pkcs8(Crypt::PK::ECC self, SV * key_data) | |
52 | PPCODE: | |
53 | { | |
54 | int rv; | |
55 | unsigned char *data=NULL; | |
56 | STRLEN data_len=0; | |
57 | ||
58 | data = (unsigned char *)SvPVbyte(key_data, data_len); | |
59 | _ecc_free_key(&self->key, &self->dp); | |
60 | rv = ecc_import_pkcs8(data, (unsigned long)data_len, &self->key, &self->dp); | |
61 | if (rv != CRYPT_OK) croak("FATAL: ecc_import_pkcs8 failed: %s", error_to_string(rv)); | |
62 | XPUSHs(ST(0)); /* return self */ | |
63 | } | |
64 | ||
65 | void | |
66 | import_key_raw(Crypt::PK::ECC self, SV * key_data, SV * curve) | |
67 | PPCODE: | |
68 | { | |
69 | int rv; | |
70 | unsigned char *data=NULL; | |
71 | STRLEN data_len=0; | |
72 | ||
73 | data = (unsigned char *)SvPVbyte(key_data, data_len); | |
74 | _ecc_free_key(&self->key, &self->dp); | |
75 | ||
76 | _ecc_set_dp_from_SV(&self->dp, curve); /* croaks on error */ | |
77 | ||
78 | rv = ecc_import_raw(data, (unsigned long)data_len, &self->key, &self->dp); | |
79 | if (rv != CRYPT_OK) croak("FATAL: ecc_import_raw failed: %s", error_to_string(rv)); | |
80 | XPUSHs(ST(0)); /* return self */ | |
81 | } | |
82 | ||
83 | int | |
84 | is_private(Crypt::PK::ECC self) | |
85 | CODE: | |
86 | if (self->key.type == -1) XSRETURN_UNDEF; | |
87 | RETVAL = (self->key.type == PK_PRIVATE) ? 1 : 0; | |
88 | OUTPUT: | |
89 | RETVAL | |
90 | ||
91 | int | |
92 | size(Crypt::PK::ECC self) | |
93 | CODE: | |
94 | if (self->key.type == -1) XSRETURN_UNDEF; | |
95 | RETVAL = ecc_get_size(&self->key); | |
96 | OUTPUT: | |
97 | RETVAL | |
98 | ||
99 | SV* | |
100 | key2hash(Crypt::PK::ECC self) | |
101 | PREINIT: | |
102 | HV *rv_hash; | |
103 | long siz; | |
104 | char buf[20001]; | |
105 | SV **not_used; | |
106 | CODE: | |
107 | if (self->key.type == -1) XSRETURN_UNDEF; | |
108 | rv_hash = newHV(); | |
109 | /* =====> k */ | |
110 | siz = (self->key.k) ? mp_unsigned_bin_size(self->key.k) : 0; | |
111 | if (siz>10000) { | |
112 | croak("FATAL: key2hash failed - 'k' too big number"); | |
113 | } | |
114 | if (siz>0) { | |
115 | mp_tohex(self->key.k, buf); | |
116 | not_used = hv_store(rv_hash, "k", 1, newSVpv(buf, strlen(buf)), 0); | |
117 | } | |
118 | else{ | |
119 | not_used = hv_store(rv_hash, "k", 1, newSVpv("", 0), 0); | |
120 | } | |
121 | /* =====> pub_x */ | |
122 | siz = (self->key.pubkey.x) ? mp_unsigned_bin_size(self->key.pubkey.x) : 0; | |
123 | if (siz>10000) { | |
124 | croak("FATAL: key2hash failed - 'pub_x' too big number"); | |
125 | } | |
126 | if (siz>0) { | |
127 | mp_tohex(self->key.pubkey.x, buf); | |
128 | not_used = hv_store(rv_hash, "pub_x", 5, newSVpv(buf, strlen(buf)), 0); | |
129 | } | |
130 | else{ | |
131 | not_used = hv_store(rv_hash, "pub_x", 5, newSVpv("", 0), 0); | |
132 | } | |
133 | /* =====> pub_y */ | |
134 | siz = (self->key.pubkey.y) ? mp_unsigned_bin_size(self->key.pubkey.y) : 0; | |
135 | if (siz>10000) { | |
136 | croak("FATAL: key2hash failed - 'pub_y' too big number"); | |
137 | } | |
138 | if (siz>0) { | |
139 | mp_tohex(self->key.pubkey.y, buf); | |
140 | not_used = hv_store(rv_hash, "pub_y", 5, newSVpv(buf, strlen(buf)), 0); | |
141 | } | |
142 | else{ | |
143 | not_used = hv_store(rv_hash, "pub_y", 5, newSVpv("", 0), 0); | |
144 | } | |
145 | /* =====> curve_... */ | |
146 | if (self->key.dp) { | |
147 | not_used = hv_store(rv_hash, "curve_name", 10, newSVpv(self->key.dp->name, strlen(self->key.dp->name)), 0); | |
148 | not_used = hv_store(rv_hash, "curve_prime", 11, newSVpv(self->key.dp->prime, strlen(self->key.dp->prime)), 0); | |
149 | not_used = hv_store(rv_hash, "curve_A", 7, newSVpv(self->key.dp->A, strlen(self->key.dp->A)), 0); | |
150 | not_used = hv_store(rv_hash, "curve_B", 7, newSVpv(self->key.dp->B, strlen(self->key.dp->B)), 0); | |
151 | not_used = hv_store(rv_hash, "curve_order", 11, newSVpv(self->key.dp->order, strlen(self->key.dp->order)), 0); | |
152 | not_used = hv_store(rv_hash, "curve_Gx", 8, newSVpv(self->key.dp->Gx, strlen(self->key.dp->Gx)), 0); | |
153 | not_used = hv_store(rv_hash, "curve_Gy", 8, newSVpv(self->key.dp->Gy, strlen(self->key.dp->Gy)), 0); | |
154 | not_used = hv_store(rv_hash, "curve_cofactor", 14, newSViv(self->key.dp->cofactor), 0); | |
155 | { | |
156 | mp_int p_num; | |
157 | mp_init(&p_num); | |
158 | mp_read_radix(&p_num, self->key.dp->prime, 16); | |
159 | not_used = hv_store(rv_hash, "curve_bytes", 11, newSViv(mp_unsigned_bin_size(&p_num)), 0); | |
160 | not_used = hv_store(rv_hash, "curve_bits", 10, newSViv(mp_count_bits(&p_num)), 0); | |
161 | mp_clear(&p_num); | |
162 | } | |
163 | } | |
164 | /* =====> size */ | |
165 | not_used = hv_store(rv_hash, "size", 4, newSViv(ecc_get_size(&self->key)), 0); | |
166 | /* =====> type */ | |
167 | not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0); | |
168 | if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */ | |
169 | RETVAL = newRV_noinc((SV*)rv_hash); | |
170 | OUTPUT: | |
171 | RETVAL | |
172 | ||
173 | SV * | |
174 | export_key_der(Crypt::PK::ECC self, char * type) | |
175 | CODE: | |
176 | { | |
177 | int rv; | |
178 | unsigned char out[4096]; | |
179 | unsigned long int out_len = 4096; | |
180 | ||
181 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
182 | if (strnEQ(type, "private", 7)) { | |
183 | rv = ecc_export_full(out, &out_len, PK_PRIVATE, &self->key); | |
184 | if (rv != CRYPT_OK) croak("FATAL: ecc_export(PK_PRIVATE) failed: %s", error_to_string(rv)); | |
185 | RETVAL = newSVpvn((char*)out, out_len); | |
186 | } | |
187 | else if (strnEQ(type, "public", 6)) { | |
188 | rv = ecc_export_full(out, &out_len, PK_PUBLIC, &self->key); | |
189 | if (rv != CRYPT_OK) croak("FATAL: ecc_export(PK_PUBLIC) failed: %s", error_to_string(rv)); | |
190 | RETVAL = newSVpvn((char*)out, out_len); | |
191 | } | |
192 | else { | |
193 | croak("FATAL: export_key_der invalid type '%s'", type); | |
194 | } | |
195 | } | |
196 | OUTPUT: | |
197 | RETVAL | |
198 | ||
199 | SV * | |
200 | export_key_raw(Crypt::PK::ECC self, char * type) | |
201 | CODE: | |
202 | { | |
203 | int rv; | |
204 | unsigned char out[4096]; | |
205 | unsigned long int out_len = sizeof(out); | |
206 | ||
207 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
208 | if (strnEQ(type, "private", 7)) { | |
209 | rv = ecc_export_raw(out, &out_len, PK_PRIVATE, &self->key); | |
210 | if (rv != CRYPT_OK) croak("FATAL: ecc_export_raw(private) failed: %s", error_to_string(rv)); | |
211 | RETVAL = newSVpvn((char*)out, out_len); | |
212 | } | |
213 | else if (strnEQ(type, "public_compressed", 17)) { | |
214 | rv = ecc_export_raw(out, &out_len, PK_PUBLIC_COMPRESSED, &self->key); | |
215 | if (rv != CRYPT_OK) croak("FATAL: ecc_export_raw(public_compressed) failed: %s", error_to_string(rv)); | |
216 | RETVAL = newSVpvn((char*)out, out_len); | |
217 | } | |
218 | else if (strnEQ(type, "public", 6)) { | |
219 | rv = ecc_export_raw(out, &out_len, PK_PUBLIC, &self->key); | |
220 | if (rv != CRYPT_OK) croak("FATAL: ecc_export_raw(public) failed: %s", error_to_string(rv)); | |
221 | RETVAL = newSVpvn((char*)out, out_len); | |
222 | } | |
223 | else { | |
224 | croak("FATAL: export_key_raw invalid type '%s'", type); | |
225 | } | |
226 | } | |
227 | OUTPUT: | |
228 | RETVAL | |
229 | ||
230 | SV * | |
231 | _encrypt(Crypt::PK::ECC self, SV * data, char * hash_name) | |
232 | CODE: | |
233 | { | |
234 | int rv, hash_id; | |
235 | unsigned char *data_ptr=NULL; | |
236 | STRLEN data_len=0; | |
237 | unsigned char buffer[1024]; | |
238 | unsigned long buffer_len = 1024; | |
239 | ||
240 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
241 | ||
242 | hash_id = find_hash(hash_name); | |
243 | if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
244 | rv = ecc_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, | |
245 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
246 | hash_id, &self->key); | |
247 | if (rv != CRYPT_OK) croak("FATAL: ecc_encrypt_key failed: %s", error_to_string(rv)); | |
248 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
249 | } | |
250 | OUTPUT: | |
251 | RETVAL | |
252 | ||
253 | SV * | |
254 | _decrypt(Crypt::PK::ECC self, SV * data) | |
255 | CODE: | |
256 | { | |
257 | int rv; | |
258 | unsigned char *data_ptr=NULL; | |
259 | STRLEN data_len=0; | |
260 | unsigned char buffer[1024]; | |
261 | unsigned long buffer_len = 1024; | |
262 | ||
263 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
264 | ||
265 | rv = ecc_decrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &self->key); | |
266 | if (rv != CRYPT_OK) croak("FATAL: ecc_decrypt_key_ex failed: %s", error_to_string(rv)); | |
267 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
268 | } | |
269 | OUTPUT: | |
270 | RETVAL | |
271 | ||
272 | SV * | |
273 | _sign(Crypt::PK::ECC self, SV * data) | |
274 | ALIAS: | |
275 | _sign_rfc7518 = 1 | |
276 | CODE: | |
277 | { | |
278 | int rv; | |
279 | unsigned char *data_ptr=NULL; | |
280 | STRLEN data_len=0; | |
281 | unsigned char buffer[1024]; | |
282 | unsigned long buffer_len = 1024; | |
283 | ||
284 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
285 | ||
286 | if (ix == 1) { | |
287 | rv = ecc_sign_hash_rfc7518(data_ptr, (unsigned long)data_len, buffer, &buffer_len, | |
288 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
289 | &self->key); | |
290 | } | |
291 | else { | |
292 | rv = ecc_sign_hash(data_ptr, (unsigned long)data_len, buffer, &buffer_len, | |
293 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
294 | &self->key); | |
295 | } | |
296 | if (rv != CRYPT_OK) croak("FATAL: ecc_sign_hash_ex failed: %s", error_to_string(rv)); | |
297 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
298 | } | |
299 | OUTPUT: | |
300 | RETVAL | |
301 | ||
302 | int | |
303 | _verify(Crypt::PK::ECC self, SV * sig, SV * data) | |
304 | ALIAS: | |
305 | _verify_rfc7518 = 1 | |
306 | CODE: | |
307 | { | |
308 | int rv, stat; | |
309 | unsigned char *data_ptr=NULL; | |
310 | STRLEN data_len=0; | |
311 | unsigned char *sig_ptr=NULL; | |
312 | STRLEN sig_len=0; | |
313 | ||
314 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
315 | sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len); | |
316 | ||
317 | RETVAL = 1; | |
318 | if (ix == 1) { | |
319 | rv = ecc_verify_hash_rfc7518(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key); | |
320 | } | |
321 | else { | |
322 | rv = ecc_verify_hash(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key); | |
323 | } | |
324 | if (rv != CRYPT_OK || stat != 1) RETVAL = 0; | |
325 | } | |
326 | OUTPUT: | |
327 | RETVAL | |
328 | ||
329 | SV * | |
330 | shared_secret(Crypt::PK::ECC self, Crypt::PK::ECC pubkey) | |
331 | CODE: | |
332 | { | |
333 | int rv; | |
334 | unsigned char buffer[1024]; | |
335 | unsigned long buffer_len = 1024; | |
336 | ||
337 | rv = ecc_shared_secret(&self->key, &pubkey->key, buffer, &buffer_len); | |
338 | if (rv != CRYPT_OK) croak("FATAL: ecc_shared_secret failed: %s", error_to_string(rv)); | |
339 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
340 | } | |
341 | OUTPUT: | |
342 | RETVAL | |
343 | ||
344 | void | |
345 | DESTROY(Crypt::PK::ECC self) | |
346 | CODE: | |
347 | _ecc_free_key(&self->key, &self->dp); | |
348 | Safefree(self); | |
349 |
0 | MODULE = CryptX PACKAGE = Crypt::PK::RSA | |
1 | ||
2 | Crypt::PK::RSA | |
3 | _new() | |
4 | CODE: | |
5 | { | |
6 | int rv; | |
7 | Newz(0, RETVAL, 1, struct rsa_struct); | |
8 | if (!RETVAL) croak("FATAL: Newz failed"); | |
9 | RETVAL->key.type = -1; | |
10 | RETVAL->yarrow_prng_index = find_prng("yarrow"); | |
11 | if(RETVAL->yarrow_prng_index==-1) croak("FATAL: find_prng('yarrow') failed"); | |
12 | rv = rng_make_prng(256, RETVAL->yarrow_prng_index, &RETVAL->yarrow_prng_state, NULL); | |
13 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
14 | } | |
15 | OUTPUT: | |
16 | RETVAL | |
17 | ||
18 | void | |
19 | generate_key(Crypt::PK::RSA self, int key_size=256, long key_e=65537) | |
20 | PPCODE: | |
21 | { | |
22 | /* key_size is in octets */ | |
23 | int rv; | |
24 | /* add a small random entropy before generating key - not necessary as we have initialized prng with 256bit entropy in _new() */ | |
25 | rv = rng_make_prng(64, self->yarrow_prng_index, &self->yarrow_prng_state, NULL); | |
26 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
27 | /* gen the key */ | |
28 | rv = rsa_make_key(&self->yarrow_prng_state, self->yarrow_prng_index, key_size, key_e, &self->key); | |
29 | if (rv != CRYPT_OK) croak("FATAL: rsa_make_key failed: %s", error_to_string(rv)); | |
30 | XPUSHs(ST(0)); /* return self */ | |
31 | } | |
32 | ||
33 | void | |
34 | _import(Crypt::PK::RSA self, SV * key_data) | |
35 | PPCODE: | |
36 | { | |
37 | int rv; | |
38 | unsigned char *data=NULL; | |
39 | STRLEN data_len=0; | |
40 | ||
41 | data = (unsigned char *)SvPVbyte(key_data, data_len); | |
42 | if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; } | |
43 | rv = rsa_import(data, (unsigned long)data_len, &self->key); | |
44 | if (rv != CRYPT_OK) croak("FATAL: rsa_import failed: %s", error_to_string(rv)); | |
45 | XPUSHs(ST(0)); /* return self */ | |
46 | } | |
47 | ||
48 | void | |
49 | _import_pkcs8(Crypt::PK::RSA self, SV * key_data) | |
50 | PPCODE: | |
51 | { | |
52 | int rv; | |
53 | unsigned char *data=NULL; | |
54 | STRLEN data_len=0; | |
55 | ||
56 | data = (unsigned char *)SvPVbyte(key_data, data_len); | |
57 | if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; } | |
58 | rv = rsa_import_pkcs8(data, (unsigned long)data_len, &self->key); | |
59 | if (rv != CRYPT_OK) croak("FATAL: rsa_import_pkcs8 failed: %s", error_to_string(rv)); | |
60 | XPUSHs(ST(0)); /* return self */ | |
61 | } | |
62 | ||
63 | void | |
64 | _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) | |
65 | PPCODE: | |
66 | { | |
67 | int rv; | |
68 | if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; } | |
69 | rv = rsa_import_hex(N, e, d, p, q, dP, dQ, qP, &self->key); | |
70 | if (rv != CRYPT_OK) croak("FATAL: rsa_import_hex failed: %s", error_to_string(rv)); | |
71 | XPUSHs(ST(0)); /* return self */ | |
72 | } | |
73 | ||
74 | int | |
75 | is_private(Crypt::PK::RSA self) | |
76 | CODE: | |
77 | if (self->key.type == -1 || self->key.N == NULL) XSRETURN_UNDEF; | |
78 | RETVAL = (self->key.type == PK_PRIVATE) ? 1 : 0; | |
79 | OUTPUT: | |
80 | RETVAL | |
81 | ||
82 | int | |
83 | size(Crypt::PK::RSA self) | |
84 | CODE: | |
85 | if (self->key.type == -1 || self->key.N == NULL) XSRETURN_UNDEF; | |
86 | RETVAL = mp_unsigned_bin_size(self->key.N); | |
87 | OUTPUT: | |
88 | RETVAL | |
89 | ||
90 | SV* | |
91 | key2hash(Crypt::PK::RSA self) | |
92 | PREINIT: | |
93 | HV *rv_hash; | |
94 | long siz; | |
95 | char buf[20001]; | |
96 | SV **not_used; | |
97 | CODE: | |
98 | if (self->key.type == -1 || self->key.N == NULL) XSRETURN_UNDEF; | |
99 | rv_hash = newHV(); | |
100 | /* =====> e */ | |
101 | siz = (self->key.e) ? mp_unsigned_bin_size(self->key.e) : 0; | |
102 | if (siz>10000) { | |
103 | croak("FATAL: key2hash failed - 'e' too big number"); | |
104 | } | |
105 | if (siz>0) { | |
106 | mp_tohex(self->key.e, buf); | |
107 | not_used = hv_store(rv_hash, "e", 1, newSVpv(buf, strlen(buf)), 0); | |
108 | } | |
109 | else{ | |
110 | not_used = hv_store(rv_hash, "e", 1, newSVpv("", 0), 0); | |
111 | } | |
112 | /* =====> d */ | |
113 | siz = (self->key.d) ? mp_unsigned_bin_size(self->key.d) : 0; | |
114 | if (siz>10000) { | |
115 | croak("FATAL: key2hash failed - 'd' too big number"); | |
116 | } | |
117 | if (siz>0) { | |
118 | mp_tohex(self->key.d, buf); | |
119 | not_used = hv_store(rv_hash, "d", 1, newSVpv(buf, strlen(buf)), 0); | |
120 | } | |
121 | else{ | |
122 | not_used = hv_store(rv_hash, "d", 1, newSVpv("", 0), 0); | |
123 | } | |
124 | /* =====> N */ | |
125 | siz = (self->key.N) ? mp_unsigned_bin_size(self->key.N) : 0; | |
126 | if (siz>10000) { | |
127 | croak("FATAL: key2hash failed - 'N' too big number"); | |
128 | } | |
129 | if (siz>0) { | |
130 | mp_tohex(self->key.N, buf); | |
131 | not_used = hv_store(rv_hash, "N", 1, newSVpv(buf, strlen(buf)), 0); | |
132 | } | |
133 | else{ | |
134 | not_used = hv_store(rv_hash, "N", 1, newSVpv("", 0), 0); | |
135 | } | |
136 | /* =====> q */ | |
137 | siz = (self->key.q) ? mp_unsigned_bin_size(self->key.q) : 0; | |
138 | if (siz>10000) { | |
139 | croak("FATAL: key2hash failed - 'q' too big number"); | |
140 | } | |
141 | if (siz>0) { | |
142 | mp_tohex(self->key.q, buf); | |
143 | not_used = hv_store(rv_hash, "q", 1, newSVpv(buf, strlen(buf)), 0); | |
144 | } | |
145 | else{ | |
146 | not_used = hv_store(rv_hash, "q", 1, newSVpv("", 0), 0); | |
147 | } | |
148 | /* =====> p */ | |
149 | siz = (self->key.p) ? mp_unsigned_bin_size(self->key.p) : 0; | |
150 | if (siz>10000) { | |
151 | croak("FATAL: key2hash failed - 'p' too big number"); | |
152 | } | |
153 | if (siz>0) { | |
154 | mp_tohex(self->key.p, buf); | |
155 | not_used = hv_store(rv_hash, "p", 1, newSVpv(buf, strlen(buf)), 0); | |
156 | } | |
157 | else{ | |
158 | not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0); | |
159 | } | |
160 | /* =====> qP */ | |
161 | siz = (self->key.qP) ? mp_unsigned_bin_size(self->key.qP) : 0; | |
162 | if (siz>10000) { | |
163 | croak("FATAL: key2hash failed - 'qP' too big number"); | |
164 | } | |
165 | if (siz>0) { | |
166 | mp_tohex(self->key.qP, buf); | |
167 | not_used = hv_store(rv_hash, "qP", 2, newSVpv(buf, strlen(buf)), 0); | |
168 | } | |
169 | else{ | |
170 | not_used = hv_store(rv_hash, "qP", 2, newSVpv("", 0), 0); | |
171 | } | |
172 | /* =====> dP */ | |
173 | siz = (self->key.dP) ? mp_unsigned_bin_size(self->key.dP) : 0; | |
174 | if (siz>10000) { | |
175 | croak("FATAL: key2hash failed - 'dP' too big number"); | |
176 | } | |
177 | if (siz>0) { | |
178 | mp_tohex(self->key.dP, buf); | |
179 | not_used = hv_store(rv_hash, "dP", 2, newSVpv(buf, strlen(buf)), 0); | |
180 | } | |
181 | else{ | |
182 | not_used = hv_store(rv_hash, "dP", 2, newSVpv("", 0), 0); | |
183 | } | |
184 | /* =====> dQ */ | |
185 | siz = (self->key.dQ) ? mp_unsigned_bin_size(self->key.dQ) : 0; | |
186 | if (siz>10000) { | |
187 | croak("FATAL: key2hash failed - 'dQ' too big number"); | |
188 | } | |
189 | if (siz>0) { | |
190 | mp_tohex(self->key.dQ, buf); | |
191 | not_used = hv_store(rv_hash, "dQ", 2, newSVpv(buf, strlen(buf)), 0); | |
192 | } | |
193 | else{ | |
194 | not_used = hv_store(rv_hash, "dQ", 2, newSVpv("", 0), 0); | |
195 | } | |
196 | /* =====> size */ | |
197 | not_used = hv_store(rv_hash, "size", 4, newSViv(mp_unsigned_bin_size(self->key.N)), 0); | |
198 | /* =====> type */ | |
199 | not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0); | |
200 | if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */ | |
201 | RETVAL = newRV_noinc((SV*)rv_hash); | |
202 | OUTPUT: | |
203 | RETVAL | |
204 | ||
205 | SV* | |
206 | export_key_der(Crypt::PK::RSA self, char * type) | |
207 | CODE: | |
208 | { | |
209 | int rv; | |
210 | unsigned char out[4096]; | |
211 | unsigned long int out_len = 4096; | |
212 | ||
213 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
214 | if (strnEQ(type, "private", 7)) { | |
215 | rv = rsa_export(out, &out_len, PK_PRIVATE, &self->key); | |
216 | if (rv != CRYPT_OK) croak("FATAL: rsa_export(PK_PRIVATE) failed: %s", error_to_string(rv)); | |
217 | RETVAL = newSVpvn((char*)out, out_len); | |
218 | } | |
219 | else if (strnEQ(type, "public", 6)) { | |
220 | rv = rsa_export(out, &out_len, PK_PUBLIC, &self->key); | |
221 | if (rv != CRYPT_OK) croak("FATAL: rsa_export(PK_PUBLIC) failed: %s", error_to_string(rv)); | |
222 | RETVAL = newSVpvn((char*)out, out_len); | |
223 | } | |
224 | else { | |
225 | croak("FATAL: export_key_der invalid type '%s'", type); | |
226 | } | |
227 | } | |
228 | OUTPUT: | |
229 | RETVAL | |
230 | ||
231 | SV * | |
232 | _encrypt(Crypt::PK::RSA self, SV * data, char * padding, char * oaep_hash, SV * oaep_lparam) | |
233 | CODE: | |
234 | { | |
235 | int rv, hash_id; | |
236 | unsigned char *lparam_ptr=NULL; | |
237 | STRLEN lparam_len=0; | |
238 | unsigned char *data_ptr=NULL; | |
239 | STRLEN data_len=0; | |
240 | unsigned char buffer[1024]; | |
241 | unsigned long buffer_len = 1024; | |
242 | ||
243 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
244 | ||
245 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
246 | if (strnEQ(padding, "oaep", 4)) { | |
247 | hash_id = find_hash(oaep_hash); | |
248 | if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", oaep_hash); | |
249 | lparam_ptr = (unsigned char *)SvPVbyte(oaep_lparam, lparam_len); | |
250 | rv = rsa_encrypt_key_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, lparam_ptr, (unsigned long)lparam_len, | |
251 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
252 | hash_id, LTC_PKCS_1_OAEP, &self->key); | |
253 | if (rv != CRYPT_OK) croak("FATAL: rsa_encrypt_key_ex failed: %s", error_to_string(rv)); | |
254 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
255 | } | |
256 | else if (strnEQ(padding, "v1.5", 4)) { | |
257 | rv = rsa_encrypt_key_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, NULL, 0, | |
258 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
259 | 0, LTC_PKCS_1_V1_5, &self->key); | |
260 | if (rv != CRYPT_OK) croak("FATAL: rsa_encrypt_key_ex failed: %s", error_to_string(rv)); | |
261 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
262 | } | |
263 | else if (strnEQ(padding, "none", 4)) { | |
264 | /* raw RSA */ | |
265 | rv = ltc_mp.rsa_me(data_ptr, (unsigned long)data_len, buffer, &buffer_len, PK_PUBLIC, &self->key); | |
266 | if (rv != CRYPT_OK) croak("FATAL: rsa_me failed: %s", error_to_string(rv)); | |
267 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
268 | } | |
269 | else { | |
270 | croak("FATAL: rsa_encrypt invalid padding '%s'", padding); | |
271 | } | |
272 | } | |
273 | OUTPUT: | |
274 | RETVAL | |
275 | ||
276 | SV * | |
277 | _decrypt(Crypt::PK::RSA self, SV * data, char * padding, char * oaep_hash, SV * oaep_lparam) | |
278 | CODE: | |
279 | { | |
280 | int rv, hash_id, stat; | |
281 | unsigned char *lparam_ptr=NULL; | |
282 | STRLEN lparam_len=0; | |
283 | unsigned char *data_ptr=NULL; | |
284 | STRLEN data_len=0; | |
285 | unsigned char buffer[1024]; | |
286 | unsigned long buffer_len = 1024; | |
287 | ||
288 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
289 | ||
290 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
291 | if (strnEQ(padding, "oaep", 4)) { | |
292 | hash_id = find_hash(oaep_hash); | |
293 | if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", oaep_hash); | |
294 | lparam_ptr = (unsigned char *)SvPVbyte(oaep_lparam, lparam_len); | |
295 | rv = rsa_decrypt_key_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, lparam_ptr, (unsigned long)lparam_len, | |
296 | hash_id, LTC_PKCS_1_OAEP, &stat, &self->key); | |
297 | if (rv != CRYPT_OK) croak("FATAL: rsa_decrypt_key_ex failed: %s", error_to_string(rv)); | |
298 | if (stat != 1) croak("FATAL: rsa_decrypt - not valid OAEP packet"); | |
299 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
300 | } | |
301 | else if (strnEQ(padding, "v1.5", 4)) { | |
302 | rv = rsa_decrypt_key_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, NULL, 0, | |
303 | 0, LTC_PKCS_1_V1_5, &stat, &self->key); | |
304 | if (rv != CRYPT_OK) croak("FATAL: rsa_decrypt_key_ex failed: %s", error_to_string(rv)); | |
305 | if (stat != 1) croak("FATAL: rsa_decrypt - invalid"); | |
306 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
307 | } | |
308 | else if (strnEQ(padding, "none", 4)) { | |
309 | /* raw RSA */ | |
310 | rv = ltc_mp.rsa_me(data_ptr, (unsigned long)data_len, buffer, &buffer_len, PK_PRIVATE, &self->key); | |
311 | if (rv != CRYPT_OK) croak("FATAL: rsa_me failed: %s", error_to_string(rv)); | |
312 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
313 | } | |
314 | else { | |
315 | croak("FATAL: rsa_encrypt invalid padding '%s'", padding); | |
316 | } | |
317 | } | |
318 | OUTPUT: | |
319 | RETVAL | |
320 | ||
321 | SV * | |
322 | _sign(Crypt::PK::RSA self, SV * data, char * padding, char * hash_name, unsigned long saltlen=12) | |
323 | CODE: | |
324 | { | |
325 | int rv, hash_id; | |
326 | unsigned char *data_ptr=NULL; | |
327 | STRLEN data_len=0; | |
328 | unsigned char buffer[1024]; | |
329 | unsigned long buffer_len = 1024; | |
330 | ||
331 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
332 | ||
333 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
334 | hash_id = find_hash(hash_name); | |
335 | if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
336 | if (strnEQ(padding, "pss", 3)) { | |
337 | rv = rsa_sign_hash_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, LTC_PKCS_1_PSS, | |
338 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
339 | hash_id, saltlen, &self->key); | |
340 | if (rv != CRYPT_OK) croak("FATAL: rsa_sign_hash_ex failed: %s", error_to_string(rv)); | |
341 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
342 | } | |
343 | else if (strnEQ(padding, "v1.5", 4)) { | |
344 | rv = rsa_sign_hash_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, LTC_PKCS_1_V1_5, | |
345 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
346 | hash_id, 0, &self->key); | |
347 | if (rv != CRYPT_OK) croak("FATAL: rsa_sign_hash_ex failed: %s", error_to_string(rv)); | |
348 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
349 | } | |
350 | else { | |
351 | croak("FATAL: rsa_sign invalid padding '%s'", padding); | |
352 | } | |
353 | } | |
354 | OUTPUT: | |
355 | RETVAL | |
356 | ||
357 | int | |
358 | _verify(Crypt::PK::RSA self, SV * sig, SV * data, char * padding, char * hash_name, unsigned long saltlen=12) | |
359 | CODE: | |
360 | { | |
361 | int rv, hash_id, stat; | |
362 | unsigned char *data_ptr=NULL; | |
363 | STRLEN data_len=0; | |
364 | unsigned char *sig_ptr=NULL; | |
365 | STRLEN sig_len=0; | |
366 | ||
367 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
368 | sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len); | |
369 | ||
370 | RETVAL = 1; | |
371 | hash_id = find_hash(hash_name); | |
372 | if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
373 | if (strnEQ(padding, "pss", 3)) { | |
374 | rv = rsa_verify_hash_ex(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, LTC_PKCS_1_PSS, | |
375 | hash_id, saltlen, &stat, &self->key); | |
376 | if (rv != CRYPT_OK || stat != 1) RETVAL = 0; | |
377 | } | |
378 | else if (strnEQ(padding, "v1.5", 4)) { | |
379 | rv = rsa_verify_hash_ex(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, LTC_PKCS_1_V1_5, | |
380 | hash_id, 0, &stat, &self->key); | |
381 | if (rv != CRYPT_OK || stat != 1) RETVAL = 0; | |
382 | } | |
383 | else { | |
384 | croak("FATAL: rsa_verify invalid padding '%s'", padding); | |
385 | } | |
386 | } | |
387 | OUTPUT: | |
388 | RETVAL | |
389 | ||
390 | void | |
391 | DESTROY(Crypt::PK::RSA self) | |
392 | CODE: | |
393 | if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; } | |
394 | Safefree(self); | |
395 |
0 | MODULE = CryptX PACKAGE = Crypt::PRNG | |
1 | ||
2 | Crypt::PRNG | |
3 | _new(IV curpid, char * prng_name, SV * entropy=&PL_sv_undef) | |
4 | CODE: | |
5 | { | |
6 | int rv, id; | |
7 | unsigned char *ent=NULL; | |
8 | STRLEN ent_len=0; | |
9 | unsigned char entropy_buf[32]; | |
10 | ||
11 | Newz(0, RETVAL, 1, struct prng_struct); | |
12 | if (!RETVAL) croak("FATAL: Newz failed"); | |
13 | ||
14 | id = find_prng(prng_name); | |
15 | if(id==-1) croak("FATAL: find_prng failed for '%s'", prng_name); | |
16 | RETVAL->id = id; | |
17 | RETVAL->last_pid = curpid; | |
18 | RETVAL->desc = &prng_descriptor[id]; | |
19 | ||
20 | rv = RETVAL->desc->start(&RETVAL->state); | |
21 | if (rv != CRYPT_OK) croak("FATAL: PRNG_start failed: %s", error_to_string(rv)); | |
22 | ||
23 | if(SvOK(entropy)) { | |
24 | ent = (unsigned char *) SvPVbyte(entropy, ent_len); | |
25 | rv = RETVAL->desc->add_entropy(ent, (unsigned long)ent_len, &RETVAL->state); | |
26 | if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv)); | |
27 | } | |
28 | else { | |
29 | if (rng_get_bytes(entropy_buf, 32, NULL) != 32) croak("FATAL: rng_get_bytes failed: %s", error_to_string(rv)); | |
30 | rv = RETVAL->desc->add_entropy(entropy_buf, 32, &RETVAL->state); | |
31 | if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv)); | |
32 | } | |
33 | rv = RETVAL->desc->ready(&RETVAL->state); | |
34 | if (rv != CRYPT_OK) croak("FATAL: PRNG_ready failed: %s", error_to_string(rv)); | |
35 | } | |
36 | OUTPUT: | |
37 | RETVAL | |
38 | ||
39 | void | |
40 | DESTROY(Crypt::PRNG self) | |
41 | CODE: | |
42 | { | |
43 | Safefree(self); | |
44 | } | |
45 | ||
46 | void | |
47 | add_entropy(Crypt::PRNG self, SV * entropy=&PL_sv_undef) | |
48 | CODE: | |
49 | { | |
50 | STRLEN in_len=0; | |
51 | unsigned char *in_buffer=NULL; | |
52 | unsigned char entropy_buf[32]; | |
53 | int rv; | |
54 | if(SvOK(entropy)) { | |
55 | in_buffer = (unsigned char *) SvPVbyte(entropy, in_len); | |
56 | rv = self->desc->add_entropy(in_buffer, (unsigned long)in_len, &self->state); | |
57 | if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv)); | |
58 | } | |
59 | else { | |
60 | if (rng_get_bytes(entropy_buf, 32, NULL) != 32) croak("FATAL: rng_get_bytes failed"); | |
61 | rv = self->desc->add_entropy(entropy_buf, 32, &self->state); | |
62 | if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv)); | |
63 | } | |
64 | rv = self->desc->ready(&self->state); | |
65 | if (rv != CRYPT_OK) croak("FATAL: PRNG_ready failed: %s", error_to_string(rv)); | |
66 | } | |
67 | ||
68 | SV * | |
69 | _bytes(Crypt::PRNG self, IV curpid, STRLEN output_len) | |
70 | CODE: | |
71 | { | |
72 | int rv_len; | |
73 | unsigned char *rdata; | |
74 | unsigned char entropy_buf[32]; | |
75 | ||
76 | if (self->last_pid != curpid) { | |
77 | rng_get_bytes(entropy_buf, 32, NULL); | |
78 | self->desc->add_entropy(entropy_buf, 32, &self->state); | |
79 | self->desc->ready(&self->state); | |
80 | self->last_pid = curpid; | |
81 | } | |
82 | ||
83 | RETVAL = NEWSV(0, output_len); | |
84 | SvPOK_only(RETVAL); | |
85 | SvCUR_set(RETVAL, output_len); | |
86 | rdata = (unsigned char *)SvPV_nolen(RETVAL); | |
87 | rv_len = (self->desc->read)(rdata, (unsigned long)output_len, &self->state); | |
88 | if ((UV)rv_len != output_len) croak("FATAL: PRNG_read failed"); | |
89 | } | |
90 | OUTPUT: | |
91 | RETVAL | |
92 | ||
93 | UV | |
94 | _int32(Crypt::PRNG self, IV curpid) | |
95 | CODE: | |
96 | { | |
97 | int i; | |
98 | unsigned char rdata[4]; | |
99 | unsigned char entropy_buf[32]; | |
100 | ||
101 | if (self->last_pid != curpid) { | |
102 | rng_get_bytes(entropy_buf, 32, NULL); | |
103 | self->desc->add_entropy(entropy_buf, 32, &self->state); | |
104 | self->desc->ready(&self->state); | |
105 | self->last_pid = curpid; | |
106 | } | |
107 | ||
108 | i = (self->desc->read)(rdata, 4, &self->state); | |
109 | if (i != 4) croak("FATAL: PRNG_read failed"); | |
110 | RETVAL = ((UV)(rdata[0])<<24) + ((UV)(rdata[1])<<16) + ((UV)(rdata[2])<<8) + ((UV)(rdata[3])); | |
111 | } | |
112 | OUTPUT: | |
113 | RETVAL | |
114 | ||
115 | NV | |
116 | _double(Crypt::PRNG self, IV curpid, ...) | |
117 | CODE: | |
118 | { | |
119 | int i; | |
120 | unsigned long a, b; /* 32bit is enough */ | |
121 | unsigned char rdata[7]; /* for double we need 53 bits */ | |
122 | unsigned char entropy_buf[32]; | |
123 | NV limit; | |
124 | ||
125 | if (self->last_pid != curpid) { | |
126 | rng_get_bytes(entropy_buf, 32, NULL); | |
127 | self->desc->add_entropy(entropy_buf, 32, &self->state); | |
128 | self->desc->ready(&self->state); | |
129 | self->last_pid = curpid; | |
130 | } | |
131 | ||
132 | i = (self->desc->read)(rdata, 7, &self->state); | |
133 | if (i != 7) croak("FATAL: PRNG_read failed"); | |
134 | a = (((unsigned long)(rdata[0])<<16) + ((unsigned long)(rdata[1])<<8) + ((unsigned long)(rdata[2]))) & 0x1FFFFF; /* 21 bits */ | |
135 | b = ((unsigned long)(rdata[3])<<24) + ((unsigned long)(rdata[4])<<16) + ((unsigned long)(rdata[5])<<8) + ((unsigned long)(rdata[6])); /* 32 bits */ | |
136 | RETVAL = ( (NV)a * 4294967296.0 + (NV)b ) / 9007199254740992.0; /* (a * 2^32 + b) / 2^53 */ | |
137 | if (items>2 && SvOK(ST(2))) { | |
138 | limit = SvNV(ST(2)); | |
139 | if (limit > 0 || limit < 0) RETVAL = RETVAL * limit; | |
140 | } | |
141 | } | |
142 | OUTPUT: | |
143 | RETVAL |
2 | 2 | use strict; |
3 | 3 | use warnings ; |
4 | 4 | |
5 | our $VERSION = '0.029'; | |
5 | our $VERSION = '0.030'; | |
6 | 6 | |
7 | 7 | use base qw(Exporter); |
8 | 8 | our @EXPORT_OK = qw( _decode_json _encode_json); |
0 | MODULE = CryptX PACKAGE = Crypt::AuthEnc::CCM | |
1 | ||
2 | void | |
3 | _memory_encrypt(char *cipher_name, SV *key, SV *nonce, SV *header, unsigned long tag_len, SV *plaintext) | |
4 | PPCODE: | |
5 | { | |
6 | STRLEN k_len, n_len, h_len, pt_len; | |
7 | unsigned char *k, *n, *h, *pt; | |
8 | int rv, id; | |
9 | unsigned char tag[MAXBLOCKSIZE]; | |
10 | SV *ct; | |
11 | ||
12 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
13 | if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar"); | |
14 | if (!SvPOK(header)) croak("FATAL: header must be string/buffer scalar"); | |
15 | if (!SvPOK(plaintext)) croak("FATAL: plaintext must be string/buffer scalar"); | |
16 | k = (unsigned char *) SvPVbyte(key, k_len); | |
17 | n = (unsigned char *) SvPVbyte(nonce, n_len); | |
18 | h = (unsigned char *) SvPVbyte(header, h_len); | |
19 | pt = (unsigned char *) SvPVbyte(plaintext, pt_len); | |
20 | ||
21 | id = find_cipher(cipher_name); | |
22 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
23 | ||
24 | ct = NEWSV(0, pt_len); | |
25 | SvPOK_only(ct); | |
26 | SvCUR_set(ct, pt_len); | |
27 | ||
28 | if(tag_len<4 || tag_len>16) tag_len = 16; | |
29 | ||
30 | rv = ccm_memory(id, k, (unsigned long)k_len, NULL, n, (unsigned long)n_len, h, (unsigned long)h_len, | |
31 | pt, (unsigned long)pt_len, (unsigned char *)SvPV_nolen(ct), tag, &tag_len, CCM_ENCRYPT); | |
32 | if (rv != CRYPT_OK) croak("FATAL: ccm_memory failed: %s", error_to_string(rv)); | |
33 | ||
34 | XPUSHs(sv_2mortal(ct)); | |
35 | XPUSHs(sv_2mortal(newSVpvn((char*)tag,tag_len))); | |
36 | ||
37 | /* int ccm_memory( int cipher, | |
38 | const unsigned char *key, unsigned long keylen, | |
39 | symmetric_key *uskey, | |
40 | const unsigned char *nonce, unsigned long noncelen, | |
41 | const unsigned char *header, unsigned long headerlen, | |
42 | unsigned char *pt, unsigned long ptlen, | |
43 | unsigned char *ct, | |
44 | unsigned char *tag, unsigned long *taglen, | |
45 | int direction); */ | |
46 | ||
47 | } | |
48 | ||
49 | void | |
50 | _memory_decrypt(char *cipher_name, SV *key, SV *nonce, SV *header, SV *ciphertext, SV *tag) | |
51 | PPCODE: | |
52 | { | |
53 | STRLEN k_len, n_len, h_len, ct_len, t_len; | |
54 | unsigned char *k, *n, *h, *ct, *t; | |
55 | int rv, id; | |
56 | unsigned char xtag[MAXBLOCKSIZE]; | |
57 | unsigned long xtag_len; | |
58 | SV *pt; | |
59 | ||
60 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
61 | if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar"); | |
62 | if (!SvPOK(header)) croak("FATAL: header must be string/buffer scalar"); | |
63 | if (!SvPOK(ciphertext)) croak("FATAL: ciphertext must be string/buffer scalar"); | |
64 | if (!SvPOK(tag)) croak("FATAL: tag must be string/buffer scalar"); | |
65 | k = (unsigned char *) SvPVbyte(key, k_len); | |
66 | n = (unsigned char *) SvPVbyte(nonce, n_len); | |
67 | h = (unsigned char *) SvPVbyte(header, h_len); | |
68 | ct = (unsigned char *) SvPVbyte(ciphertext, ct_len); | |
69 | t = (unsigned char *) SvPVbyte(tag, t_len); | |
70 | ||
71 | id = find_cipher(cipher_name); | |
72 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
73 | ||
74 | pt = NEWSV(0, ct_len); | |
75 | SvPOK_only(pt); | |
76 | SvCUR_set(pt, ct_len); | |
77 | ||
78 | xtag_len = (unsigned long)t_len; | |
79 | Copy(t, xtag, t_len, unsigned char); | |
80 | ||
81 | rv = ccm_memory(id, k, (unsigned long)k_len, NULL, n, (unsigned long)n_len, h, (unsigned long)h_len, | |
82 | (unsigned char *)SvPV_nolen(pt), (unsigned long)ct_len, ct, xtag, &xtag_len, CCM_DECRYPT); | |
83 | if (rv != CRYPT_OK) { | |
84 | XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */ | |
85 | } | |
86 | else { | |
87 | XPUSHs(sv_2mortal(pt)); | |
88 | } | |
89 | } |
0 | MODULE = CryptX PACKAGE = Crypt::AuthEnc::EAX | |
1 | ||
2 | Crypt::AuthEnc::EAX | |
3 | _new(char * cipher_name, SV * key, SV * nonce, SV * header=&PL_sv_undef) | |
4 | CODE: | |
5 | { | |
6 | STRLEN k_len=0; | |
7 | unsigned char *k=NULL; | |
8 | unsigned char *n=NULL; | |
9 | STRLEN n_len=0; | |
10 | unsigned char *h=NULL; | |
11 | STRLEN h_len=0; | |
12 | int id; | |
13 | ||
14 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
15 | k = (unsigned char *) SvPVbyte(key, k_len); | |
16 | if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar"); | |
17 | n = (unsigned char *) SvPVbyte(nonce, n_len); | |
18 | if(SvOK(header)) { /* header is optional param */ | |
19 | if(!SvPOK(header)) croak("FATAL: header must be string/buffer scalar"); | |
20 | h = (unsigned char *) SvPVbyte(header, h_len); | |
21 | } | |
22 | ||
23 | id = find_cipher(cipher_name); | |
24 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
25 | ||
26 | Newz(0, RETVAL, 1, struct eax_struct); | |
27 | if (!RETVAL) croak("FATAL: Newz failed"); | |
28 | ||
29 | if (eax_init(&RETVAL->state, id, k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len) != CRYPT_OK) { | |
30 | croak("FATAL: eax setup failed"); | |
31 | } | |
32 | } | |
33 | OUTPUT: | |
34 | RETVAL | |
35 | ||
36 | void | |
37 | DESTROY(Crypt::AuthEnc::EAX self) | |
38 | CODE: | |
39 | Safefree(self); | |
40 | ||
41 | Crypt::AuthEnc::EAX | |
42 | clone(Crypt::AuthEnc::EAX self) | |
43 | CODE: | |
44 | Newz(0, RETVAL, 1, struct eax_struct); | |
45 | if (!RETVAL) croak("FATAL: Newz failed"); | |
46 | Copy(&self->state, &RETVAL->state, 1, struct eax_struct); | |
47 | OUTPUT: | |
48 | RETVAL | |
49 | ||
50 | SV * | |
51 | encrypt_add(Crypt::AuthEnc::EAX self, SV * data) | |
52 | CODE: | |
53 | { | |
54 | int rv; | |
55 | STRLEN in_data_len; | |
56 | unsigned char *in_data, *out_data; | |
57 | ||
58 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
59 | if (in_data_len==0) { | |
60 | RETVAL = newSVpvn("", 0); | |
61 | } | |
62 | else { | |
63 | RETVAL = NEWSV(0, in_data_len); | |
64 | SvPOK_only(RETVAL); | |
65 | SvCUR_set(RETVAL, in_data_len); | |
66 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
67 | rv = eax_encrypt(&self->state, in_data, out_data, (unsigned long)in_data_len); | |
68 | if (rv != CRYPT_OK) croak("FATAL: eax_encrypt failed: %s", error_to_string(rv)); | |
69 | } | |
70 | } | |
71 | OUTPUT: | |
72 | RETVAL | |
73 | ||
74 | SV * | |
75 | decrypt_add(Crypt::AuthEnc::EAX self, SV * data) | |
76 | CODE: | |
77 | { | |
78 | int rv; | |
79 | STRLEN in_data_len; | |
80 | unsigned char *in_data, *out_data; | |
81 | ||
82 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
83 | if (in_data_len==0) { | |
84 | RETVAL = newSVpvn("", 0); | |
85 | } | |
86 | else { | |
87 | RETVAL = NEWSV(0, in_data_len); | |
88 | SvPOK_only(RETVAL); | |
89 | SvCUR_set(RETVAL, in_data_len); | |
90 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
91 | rv = eax_decrypt(&self->state, in_data, out_data, (unsigned long)in_data_len); | |
92 | if (rv != CRYPT_OK) croak("FATAL: eax_decrypt failed: %s", error_to_string(rv)); | |
93 | } | |
94 | } | |
95 | OUTPUT: | |
96 | RETVAL | |
97 | ||
98 | void | |
99 | encrypt_done(Crypt::AuthEnc::EAX self) | |
100 | PPCODE: | |
101 | { | |
102 | int rv; | |
103 | unsigned char tag[MAXBLOCKSIZE]; | |
104 | unsigned long tag_len = sizeof(tag); | |
105 | ||
106 | rv = eax_done(&self->state, tag, &tag_len); | |
107 | if (rv != CRYPT_OK) croak("FATAL: eax_done failed: %s", error_to_string(rv)); | |
108 | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); | |
109 | } | |
110 | ||
111 | void | |
112 | decrypt_done(Crypt::AuthEnc::EAX self, ...) | |
113 | PPCODE: | |
114 | { | |
115 | int rv; | |
116 | unsigned char tag[MAXBLOCKSIZE]; | |
117 | unsigned long tag_len = sizeof(tag); | |
118 | STRLEN expected_tag_len; | |
119 | unsigned char *expected_tag; | |
120 | ||
121 | rv = eax_done(&self->state, tag, &tag_len); | |
122 | if (rv != CRYPT_OK) croak("FATAL: eax_done failed: %s", error_to_string(rv)); | |
123 | if (items == 1) { | |
124 | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); | |
125 | } | |
126 | else { | |
127 | if(!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar"); | |
128 | expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len); | |
129 | if (expected_tag_len!=tag_len) { | |
130 | XPUSHs(sv_2mortal(newSViv(0))); /* false */ | |
131 | } | |
132 | else if (memNE(expected_tag, tag, tag_len)) { | |
133 | XPUSHs(sv_2mortal(newSViv(0))); /* false */ | |
134 | } | |
135 | else { | |
136 | XPUSHs(sv_2mortal(newSViv(1))); /* true */ | |
137 | } | |
138 | } | |
139 | } | |
140 | ||
141 | int | |
142 | header_add(Crypt::AuthEnc::EAX self, SV * header) | |
143 | CODE: | |
144 | { | |
145 | STRLEN h_len; | |
146 | unsigned char *h; | |
147 | h = (unsigned char *)SvPVbyte(header, h_len); | |
148 | RETVAL = eax_addheader(&self->state, h, (unsigned long)h_len); | |
149 | } | |
150 | OUTPUT: | |
151 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::AuthEnc::GCM | |
1 | ||
2 | Crypt::AuthEnc::GCM | |
3 | _new(char * cipher_name, SV * key) | |
4 | CODE: | |
5 | { | |
6 | STRLEN k_len=0; | |
7 | unsigned char *k=NULL; | |
8 | int id; | |
9 | ||
10 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
11 | k = (unsigned char *) SvPVbyte(key, k_len); | |
12 | ||
13 | ||
14 | id = find_cipher(cipher_name); | |
15 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
16 | ||
17 | Newz(0, RETVAL, 1, struct gcm_struct); | |
18 | if (!RETVAL) croak("FATAL: Newz failed"); | |
19 | ||
20 | if (gcm_init(&RETVAL->state, id, k, (unsigned long)k_len) != CRYPT_OK) { | |
21 | croak("FATAL: gcm setup failed"); | |
22 | } | |
23 | } | |
24 | OUTPUT: | |
25 | RETVAL | |
26 | ||
27 | void | |
28 | DESTROY(Crypt::AuthEnc::GCM self) | |
29 | CODE: | |
30 | Safefree(self); | |
31 | ||
32 | Crypt::AuthEnc::GCM | |
33 | clone(Crypt::AuthEnc::GCM self) | |
34 | CODE: | |
35 | Newz(0, RETVAL, 1, struct gcm_struct); | |
36 | if (!RETVAL) croak("FATAL: Newz failed"); | |
37 | Copy(&self->state, &RETVAL->state, 1, struct gcm_struct); | |
38 | OUTPUT: | |
39 | RETVAL | |
40 | ||
41 | int | |
42 | reset(Crypt::AuthEnc::GCM self) | |
43 | CODE: | |
44 | { | |
45 | int rv; | |
46 | rv = gcm_reset(&self->state); | |
47 | if (rv != CRYPT_OK) croak("FATAL: gcm_reset failed: %s", error_to_string(rv)); | |
48 | RETVAL = rv; | |
49 | } | |
50 | OUTPUT: | |
51 | RETVAL | |
52 | ||
53 | SV * | |
54 | encrypt_add(Crypt::AuthEnc::GCM self, SV * data) | |
55 | CODE: | |
56 | { | |
57 | int rv; | |
58 | STRLEN in_data_len; | |
59 | unsigned char *in_data, *out_data; | |
60 | ||
61 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
62 | if (in_data_len==0) { | |
63 | RETVAL = newSVpvn("", 0); | |
64 | } | |
65 | else | |
66 | { | |
67 | RETVAL = NEWSV(0, in_data_len); | |
68 | SvPOK_only(RETVAL); | |
69 | SvCUR_set(RETVAL, in_data_len); | |
70 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
71 | rv = gcm_process(&self->state, in_data, (unsigned long)in_data_len, out_data, GCM_ENCRYPT); | |
72 | if (rv != CRYPT_OK) croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv)); | |
73 | } | |
74 | } | |
75 | OUTPUT: | |
76 | RETVAL | |
77 | ||
78 | int | |
79 | iv_add(Crypt::AuthEnc::GCM self, SV * data) | |
80 | CODE: | |
81 | { | |
82 | int rv; | |
83 | STRLEN in_data_len; | |
84 | unsigned char *in_data; | |
85 | ||
86 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
87 | rv = gcm_add_iv(&self->state, in_data, (unsigned long)in_data_len); | |
88 | if (rv != CRYPT_OK) croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv)); | |
89 | RETVAL = rv; | |
90 | } | |
91 | OUTPUT: | |
92 | RETVAL | |
93 | ||
94 | int | |
95 | adata_add(Crypt::AuthEnc::GCM self, SV * data) | |
96 | CODE: | |
97 | { | |
98 | int rv; | |
99 | STRLEN in_data_len; | |
100 | unsigned char *in_data; | |
101 | ||
102 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
103 | rv = gcm_add_aad(&self->state, in_data, (unsigned long)in_data_len); | |
104 | if (rv != CRYPT_OK) croak("FATAL: gcm_add_aad failed: %s", error_to_string(rv)); | |
105 | RETVAL = rv; | |
106 | } | |
107 | OUTPUT: | |
108 | RETVAL | |
109 | ||
110 | SV * | |
111 | decrypt_add(Crypt::AuthEnc::GCM self, SV * data) | |
112 | CODE: | |
113 | { | |
114 | int rv; | |
115 | STRLEN in_data_len; | |
116 | unsigned char *in_data, *out_data; | |
117 | ||
118 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
119 | if (in_data_len==0) { | |
120 | RETVAL = newSVpvn("", 0); | |
121 | } | |
122 | else { | |
123 | RETVAL = NEWSV(0, in_data_len); | |
124 | SvPOK_only(RETVAL); | |
125 | SvCUR_set(RETVAL, in_data_len); | |
126 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
127 | rv = gcm_process(&self->state, out_data, (unsigned long)in_data_len, in_data, GCM_DECRYPT); | |
128 | if (rv != CRYPT_OK) croak("FATAL: encrypt_add/gcm_process failed: %s", error_to_string(rv)); | |
129 | } | |
130 | } | |
131 | OUTPUT: | |
132 | RETVAL | |
133 | ||
134 | ||
135 | void | |
136 | encrypt_done(Crypt::AuthEnc::GCM self) | |
137 | PPCODE: | |
138 | { | |
139 | int rv; | |
140 | unsigned char tag[MAXBLOCKSIZE]; | |
141 | unsigned long tag_len = sizeof(tag); | |
142 | ||
143 | rv = gcm_done(&self->state, tag, &tag_len); | |
144 | if (rv != CRYPT_OK) croak("FATAL: gcm_done failed: %s", error_to_string(rv)); | |
145 | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); | |
146 | } | |
147 | ||
148 | void | |
149 | decrypt_done(Crypt::AuthEnc::GCM self, ...) | |
150 | PPCODE: | |
151 | { | |
152 | int rv; | |
153 | unsigned char tag[MAXBLOCKSIZE]; | |
154 | unsigned long tag_len = sizeof(tag); | |
155 | STRLEN expected_tag_len; | |
156 | unsigned char *expected_tag; | |
157 | ||
158 | rv = gcm_done(&self->state, tag, &tag_len); | |
159 | if (rv != CRYPT_OK) croak("FATAL: gcm_done failed: %s", error_to_string(rv)); | |
160 | if (items == 1) { | |
161 | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); | |
162 | } | |
163 | else { | |
164 | if(!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar"); | |
165 | expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len); | |
166 | if (expected_tag_len!=tag_len) { | |
167 | XPUSHs(sv_2mortal(newSViv(0))); /* false */ | |
168 | } | |
169 | else if (memNE(expected_tag, tag, tag_len)) { | |
170 | XPUSHs(sv_2mortal(newSViv(0))); /* false */ | |
171 | } | |
172 | else { | |
173 | XPUSHs(sv_2mortal(newSViv(1))); /* true */ | |
174 | } | |
175 | } | |
176 | } |
0 | MODULE = CryptX PACKAGE = Crypt::AuthEnc::OCB | |
1 | ||
2 | Crypt::AuthEnc::OCB | |
3 | _new(char * cipher_name, SV * key, SV * nonce) | |
4 | CODE: | |
5 | { | |
6 | STRLEN k_len=0; | |
7 | unsigned char *k=NULL; | |
8 | unsigned char *n=NULL; | |
9 | STRLEN n_len=0; | |
10 | int id; | |
11 | ||
12 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
13 | k = (unsigned char *) SvPVbyte(key, k_len); | |
14 | if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar"); | |
15 | n = (unsigned char *) SvPVbyte(nonce, n_len); | |
16 | ||
17 | id = find_cipher(cipher_name); | |
18 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
19 | ||
20 | Newz(0, RETVAL, 1, struct ocb_struct); | |
21 | if (!RETVAL) croak("FATAL: Newz failed"); | |
22 | ||
23 | if (ocb3_init(&RETVAL->state, id, k, (unsigned long)k_len, n, (unsigned long)n_len) != CRYPT_OK) { | |
24 | croak("FATAL: ocb setup failed"); | |
25 | } | |
26 | } | |
27 | OUTPUT: | |
28 | RETVAL | |
29 | ||
30 | void | |
31 | DESTROY(Crypt::AuthEnc::OCB self) | |
32 | CODE: | |
33 | Safefree(self); | |
34 | ||
35 | Crypt::AuthEnc::OCB | |
36 | clone(Crypt::AuthEnc::OCB self) | |
37 | CODE: | |
38 | Newz(0, RETVAL, 1, struct ocb_struct); | |
39 | if (!RETVAL) croak("FATAL: Newz failed"); | |
40 | Copy(&self->state, &RETVAL->state, 1, struct ocb_struct); | |
41 | OUTPUT: | |
42 | RETVAL | |
43 | ||
44 | void | |
45 | adata_add(Crypt::AuthEnc::OCB self, SV * data) | |
46 | CODE: | |
47 | { | |
48 | int rv; | |
49 | STRLEN in_data_len; | |
50 | unsigned char *in_data; | |
51 | ||
52 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
53 | ||
54 | if (in_data_len>0) { | |
55 | rv = ocb3_add_aad(&self->state, in_data, (unsigned long)in_data_len); | |
56 | if (rv != CRYPT_OK) croak("FATAL: ocb3_add_aad failed: %s", error_to_string(rv)); | |
57 | } | |
58 | } | |
59 | ||
60 | SV * | |
61 | encrypt_add(Crypt::AuthEnc::OCB self, SV * data) | |
62 | CODE: | |
63 | { | |
64 | int rv; | |
65 | STRLEN in_data_len; | |
66 | unsigned char *in_data, *out_data; | |
67 | ||
68 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
69 | if (in_data_len==0) { | |
70 | RETVAL = newSVpvn("", 0); | |
71 | } | |
72 | else { | |
73 | RETVAL = NEWSV(0, in_data_len); | |
74 | SvPOK_only(RETVAL); | |
75 | SvCUR_set(RETVAL, in_data_len); | |
76 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
77 | ||
78 | if (in_data_len % (&self->state)->block_len) | |
79 | croak ("FATAL: sizeof(data) should be multiple of blocksize (%d)", (&self->state)->block_len); | |
80 | ||
81 | rv = ocb3_encrypt(&self->state, in_data, (unsigned long)in_data_len, out_data); | |
82 | if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt failed: %s", error_to_string(rv)); | |
83 | } | |
84 | } | |
85 | OUTPUT: | |
86 | RETVAL | |
87 | ||
88 | SV * | |
89 | encrypt_last(Crypt::AuthEnc::OCB self, SV * data) | |
90 | CODE: | |
91 | { | |
92 | int rv; | |
93 | STRLEN in_data_len; | |
94 | unsigned char *in_data, *out_data; | |
95 | ||
96 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
97 | if (in_data_len>0) { | |
98 | RETVAL = NEWSV(0, in_data_len); | |
99 | SvPOK_only(RETVAL); | |
100 | SvCUR_set(RETVAL, in_data_len); | |
101 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
102 | } | |
103 | else { | |
104 | RETVAL = newSVpvn("", 0); | |
105 | out_data = NULL; | |
106 | } | |
107 | rv = ocb3_encrypt_last(&self->state, in_data, (unsigned long)in_data_len, out_data); | |
108 | if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv)); | |
109 | } | |
110 | OUTPUT: | |
111 | RETVAL | |
112 | ||
113 | SV * | |
114 | decrypt_add(Crypt::AuthEnc::OCB self, SV * data) | |
115 | CODE: | |
116 | { | |
117 | int rv; | |
118 | STRLEN in_data_len; | |
119 | unsigned char *in_data, *out_data; | |
120 | ||
121 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
122 | if (in_data_len==0) { | |
123 | RETVAL = newSVpvn("", 0); | |
124 | } | |
125 | else { | |
126 | RETVAL = NEWSV(0, in_data_len); | |
127 | SvPOK_only(RETVAL); | |
128 | SvCUR_set(RETVAL, in_data_len); | |
129 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
130 | ||
131 | if (in_data_len % (&self->state)->block_len) | |
132 | croak ("FATAL: sizeof(data) should be multiple of blocksize (%d)", (&self->state)->block_len); | |
133 | ||
134 | rv = ocb3_decrypt(&self->state, in_data, (unsigned long)in_data_len, out_data); | |
135 | if (rv != CRYPT_OK) croak("FATAL: ocb3_decrypt failed: %s", error_to_string(rv)); | |
136 | } | |
137 | } | |
138 | OUTPUT: | |
139 | RETVAL | |
140 | ||
141 | SV * | |
142 | decrypt_last(Crypt::AuthEnc::OCB self, SV * data) | |
143 | CODE: | |
144 | { | |
145 | int rv; | |
146 | STRLEN in_data_len; | |
147 | unsigned char *in_data, *out_data; | |
148 | ||
149 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
150 | if (in_data_len>0) { | |
151 | RETVAL = NEWSV(0, in_data_len); | |
152 | SvPOK_only(RETVAL); | |
153 | SvCUR_set(RETVAL, in_data_len); | |
154 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
155 | } | |
156 | else { | |
157 | RETVAL = newSVpvn("", 0); | |
158 | out_data = NULL; | |
159 | } | |
160 | rv = ocb3_decrypt_last(&self->state, in_data, (unsigned long)in_data_len, out_data); | |
161 | if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv)); | |
162 | } | |
163 | OUTPUT: | |
164 | RETVAL | |
165 | ||
166 | void | |
167 | encrypt_done(Crypt::AuthEnc::OCB self) | |
168 | PPCODE: | |
169 | { | |
170 | int rv; | |
171 | unsigned char tag[MAXBLOCKSIZE]; | |
172 | unsigned long tag_len = sizeof(tag); | |
173 | ||
174 | rv = ocb3_done(&self->state, tag, &tag_len); | |
175 | if (rv != CRYPT_OK) croak("FATAL: ocb3_done_encrypt failed: %s", error_to_string(rv)); | |
176 | ||
177 | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); | |
178 | } | |
179 | ||
180 | void | |
181 | decrypt_done(Crypt::AuthEnc::OCB self, ...) | |
182 | PPCODE: | |
183 | { | |
184 | int rv; | |
185 | unsigned char tag[MAXBLOCKSIZE]; | |
186 | unsigned long tag_len = sizeof(tag); | |
187 | STRLEN expected_tag_len; | |
188 | unsigned char *expected_tag; | |
189 | ||
190 | rv = ocb3_done(&self->state, tag, &tag_len); | |
191 | if (rv != CRYPT_OK) croak("FATAL: ocb3_done_decrypt failed: %s", error_to_string(rv)); | |
192 | if (items == 1) { | |
193 | XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len))); | |
194 | } | |
195 | else { | |
196 | if(!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar"); | |
197 | expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len); | |
198 | if (expected_tag_len!=tag_len) { | |
199 | XPUSHs(sv_2mortal(newSViv(0))); /* false */ | |
200 | } | |
201 | else if (memNE(expected_tag, tag, tag_len)) { | |
202 | XPUSHs(sv_2mortal(newSViv(0))); /* false */ | |
203 | } | |
204 | else { | |
205 | XPUSHs(sv_2mortal(newSViv(1))); /* true */ | |
206 | } | |
207 | } | |
208 | } | |
209 | ||
210 | int | |
211 | blocksize(Crypt::AuthEnc::OCB self) | |
212 | CODE: | |
213 | { | |
214 | RETVAL = (&self->state)->block_len; | |
215 | } | |
216 | OUTPUT: | |
217 | RETVAL |
0 | MODULE = CryptX PACKAGE = Math::BigInt::LTM | |
1 | ||
2 | ||
3 | ############################################################################## | |
4 | # _new() | |
5 | ||
6 | Math::BigInt::LTM | |
7 | _new(Class, SV *x) | |
8 | CODE: | |
9 | Newz(0, RETVAL, 1, mp_int); | |
10 | mp_init(RETVAL); | |
11 | if ((SvUOK(x) || SvIOK(x)) && (sizeof(UV) <= sizeof(unsigned long) || SvUV(x) == (unsigned long)SvUV(x))) { | |
12 | mp_set_int(RETVAL, (unsigned long)SvUV(x)); | |
13 | } | |
14 | else { | |
15 | mp_read_radix(RETVAL, SvPV_nolen(x), 10); | |
16 | } | |
17 | OUTPUT: | |
18 | RETVAL | |
19 | ||
20 | ############################################################################## | |
21 | # _from_bin() | |
22 | ||
23 | Math::BigInt::LTM | |
24 | _from_bin(Class, SV *x) | |
25 | PREINIT: | |
26 | char *str, *start; | |
27 | CODE: | |
28 | Newz(0, RETVAL, 1, mp_int); | |
29 | mp_init(RETVAL); | |
30 | str = SvPV_nolen(x); | |
31 | start = (strlen(str)>2 && str[0] == '0' && str[1] == 'b') ? str+2 : str; | |
32 | mp_read_radix(RETVAL, start, 2); | |
33 | OUTPUT: | |
34 | RETVAL | |
35 | ||
36 | ############################################################################## | |
37 | # _from_hex() | |
38 | ||
39 | Math::BigInt::LTM | |
40 | _from_hex(Class, SV *x) | |
41 | PREINIT: | |
42 | char *str, *start; | |
43 | CODE: | |
44 | Newz(0, RETVAL, 1, mp_int); | |
45 | mp_init(RETVAL); | |
46 | str = SvPV_nolen(x); | |
47 | start = (strlen(str)>2 && str[0] == '0' && str[1] == 'x') ? str+2 : str; | |
48 | mp_read_radix(RETVAL, start, 16); | |
49 | OUTPUT: | |
50 | RETVAL | |
51 | ||
52 | ############################################################################## | |
53 | # _from_oct() | |
54 | ||
55 | Math::BigInt::LTM | |
56 | _from_oct(Class, SV *x) | |
57 | CODE: | |
58 | Newz(0, RETVAL, 1, mp_int); | |
59 | mp_init(RETVAL); | |
60 | mp_read_radix(RETVAL, SvPV_nolen(x), 8); | |
61 | OUTPUT: | |
62 | RETVAL | |
63 | ||
64 | ############################################################################## | |
65 | # _set() - set an already existing object to the given scalar value | |
66 | ||
67 | void | |
68 | _set(Class, Math::BigInt::LTM n, SV *x) | |
69 | CODE: | |
70 | mp_set_int(n, SvIV(x)); | |
71 | ||
72 | ############################################################################## | |
73 | # _zero() | |
74 | ||
75 | Math::BigInt::LTM | |
76 | _zero(Class) | |
77 | CODE: | |
78 | Newz(0, RETVAL, 1, mp_int); | |
79 | mp_init(RETVAL); | |
80 | mp_set_int(RETVAL, 0); | |
81 | OUTPUT: | |
82 | RETVAL | |
83 | ||
84 | ############################################################################## | |
85 | # _one() | |
86 | ||
87 | Math::BigInt::LTM | |
88 | _one(Class) | |
89 | CODE: | |
90 | Newz(0, RETVAL, 1, mp_int); | |
91 | mp_init(RETVAL); | |
92 | mp_set_int(RETVAL, 1); | |
93 | OUTPUT: | |
94 | RETVAL | |
95 | ||
96 | ############################################################################## | |
97 | # _two() | |
98 | ||
99 | Math::BigInt::LTM | |
100 | _two(Class) | |
101 | CODE: | |
102 | Newz(0, RETVAL, 1, mp_int); | |
103 | mp_init(RETVAL); | |
104 | mp_set_int(RETVAL, 2); | |
105 | OUTPUT: | |
106 | RETVAL | |
107 | ||
108 | ############################################################################## | |
109 | # _ten() | |
110 | ||
111 | Math::BigInt::LTM | |
112 | _ten(Class) | |
113 | CODE: | |
114 | Newz(0, RETVAL, 1, mp_int); | |
115 | mp_init(RETVAL); | |
116 | mp_set_int(RETVAL, 10); | |
117 | OUTPUT: | |
118 | RETVAL | |
119 | ||
120 | ############################################################################## | |
121 | # _1ex() | |
122 | ||
123 | Math::BigInt::LTM | |
124 | _1ex(Class, int x) | |
125 | CODE: | |
126 | Newz(0, RETVAL, 1, mp_int); | |
127 | mp_init(RETVAL); | |
128 | mp_set_int(RETVAL, 10); | |
129 | mp_expt_d(RETVAL, x, RETVAL); | |
130 | OUTPUT: | |
131 | RETVAL | |
132 | ||
133 | ############################################################################## | |
134 | # DESTROY() - free memory of a GMP number | |
135 | ||
136 | void | |
137 | DESTROY(Math::BigInt::LTM n) | |
138 | PPCODE: | |
139 | if (n) { | |
140 | mp_clear(n); | |
141 | Safefree(n); | |
142 | } | |
143 | ||
144 | ############################################################################## | |
145 | # _str() - return string so that atof() and atoi() can use it | |
146 | ||
147 | SV * | |
148 | _str(Class, Math::BigInt::LTM n) | |
149 | PREINIT: | |
150 | int len; | |
151 | char *buf; | |
152 | CODE: | |
153 | if (mp_iszero(n) == MP_YES) { | |
154 | RETVAL = newSVpv("0", 0); | |
155 | } | |
156 | else { | |
157 | len = mp_unsigned_bin_size(n) * 3 + 2; /* decimal size ~ 3 * bin size +1 for sign +1 for NUL-byte */ | |
158 | Newz(0, buf, len, char); | |
159 | mp_toradix_n(n, buf, 10, len); | |
160 | RETVAL = newSVpv(buf, 0); | |
161 | Safefree(buf); | |
162 | } | |
163 | OUTPUT: | |
164 | RETVAL | |
165 | ||
166 | ############################################################################## | |
167 | # _len() - return the length of the number in base 10 (costly) | |
168 | ||
169 | int | |
170 | _len(Class, Math::BigInt::LTM n) | |
171 | PREINIT: | |
172 | int len; | |
173 | char *buf; | |
174 | CODE: | |
175 | if (mp_iszero(n) == MP_YES) { | |
176 | RETVAL = 1; | |
177 | } | |
178 | else { | |
179 | len = mp_unsigned_bin_size(n) * 3 + 2; /* decimal size ~ 3 * bin size +1 for sign +1 for NUL-byte */ | |
180 | Newz(0, buf, len, char); | |
181 | mp_toradix_n(n, buf, 10, len); | |
182 | RETVAL = strlen(buf); | |
183 | Safefree(buf); | |
184 | } | |
185 | OUTPUT: | |
186 | RETVAL | |
187 | ||
188 | ############################################################################## | |
189 | # _alen() - return the approx. length of the number in base 10 (fast) | |
190 | ||
191 | int | |
192 | _alen(Class, Math::BigInt::LTM n) | |
193 | CODE: | |
194 | RETVAL = mp_unsigned_bin_size(n) * 3 + 2; /* decimal size ~ 3 * bin size XXX-FIXME very bad estimate */ | |
195 | OUTPUT: | |
196 | RETVAL | |
197 | ||
198 | ############################################################################## | |
199 | # _zeros() - return number of trailing zeros (in decimal form) | |
200 | ||
201 | int | |
202 | _zeros(Class, Math::BigInt::LTM n) | |
203 | PREINIT: | |
204 | int len; | |
205 | char *buf; | |
206 | CODE: | |
207 | if (mp_iszero(n) == MP_YES) { | |
208 | RETVAL = 0; /* '0' has no trailing zeros! */ | |
209 | } | |
210 | else { | |
211 | len = mp_unsigned_bin_size(n) * 3 + 2; /* decimal size ~ 3 * bin size +1 for sign +1 for NUL-byte */ | |
212 | Newz(0, buf, len, char); | |
213 | mp_toradix_n(n, buf, 10, len); | |
214 | len = strlen(buf); | |
215 | RETVAL = 0; | |
216 | while (len > 0) { | |
217 | if (buf[len-1] != '0') break; | |
218 | RETVAL++; | |
219 | len--; | |
220 | } | |
221 | Safefree(buf); | |
222 | } | |
223 | OUTPUT: | |
224 | RETVAL | |
225 | ||
226 | ############################################################################## | |
227 | # _as_hex() - return ref to hexadecimal string (prefixed with 0x) | |
228 | ||
229 | SV * | |
230 | _as_hex(Class, Math::BigInt::LTM n) | |
231 | PREINIT: | |
232 | int i, len; | |
233 | char *buf; | |
234 | CODE: | |
235 | len = mp_unsigned_bin_size(n) * 2 + 3; | |
236 | RETVAL = newSV(len); | |
237 | SvPOK_on(RETVAL); | |
238 | buf = SvPVX(RETVAL); /* get ptr to storage */ | |
239 | *buf++ = '0'; *buf++ = 'x'; /* prepend '0x' */ | |
240 | mp_tohex(n, buf); | |
241 | for (i=0; i<len && buf[i]>0; i++) buf[i] = toLOWER(buf[i]); | |
242 | SvCUR_set(RETVAL, strlen(buf)+2); /* set real length */ | |
243 | OUTPUT: | |
244 | RETVAL | |
245 | ||
246 | ############################################################################## | |
247 | # _as_bin() - return ref to binary string (prefixed with 0b) | |
248 | ||
249 | SV * | |
250 | _as_bin(Class, Math::BigInt::LTM n) | |
251 | PREINIT: | |
252 | int len; | |
253 | char *buf; | |
254 | CODE: | |
255 | len = mp_unsigned_bin_size(n) * 8 + 3; | |
256 | RETVAL = newSV(len); | |
257 | SvPOK_on(RETVAL); | |
258 | buf = SvPVX(RETVAL); /* get ptr to storage */ | |
259 | *buf++ = '0'; *buf++ = 'b'; /* prepend '0b' */ | |
260 | mp_tobinary(n, buf); | |
261 | SvCUR_set(RETVAL, strlen(buf)+2); /* set real length */ | |
262 | OUTPUT: | |
263 | RETVAL | |
264 | ||
265 | ############################################################################## | |
266 | # _as_oct() - return ref to octal string (prefixed with 0) | |
267 | ||
268 | SV * | |
269 | _as_oct(Class, Math::BigInt::LTM n) | |
270 | PREINIT: | |
271 | int len; | |
272 | char *buf; | |
273 | CODE: | |
274 | len = mp_unsigned_bin_size(n) * 3 + 3; | |
275 | RETVAL = newSV(len); | |
276 | SvPOK_on(RETVAL); | |
277 | buf = SvPVX(RETVAL); | |
278 | *buf++ = '0'; /* prepend '0' */ | |
279 | mp_tooctal(n, buf); | |
280 | SvCUR_set(RETVAL, strlen(buf)+1); /* set real length */ | |
281 | OUTPUT: | |
282 | RETVAL | |
283 | ||
284 | ############################################################################## | |
285 | # _modpow() - ($n ** $exp) % $mod | |
286 | ||
287 | Math::BigInt::LTM | |
288 | _modpow(Class, Math::BigInt::LTM n, Math::BigInt::LTM exp, Math::BigInt::LTM mod) | |
289 | CODE: | |
290 | Newz(0, RETVAL, 1, mp_int); | |
291 | mp_init(RETVAL); | |
292 | if (mp_cmp_d(mod, 1) == MP_EQ) { | |
293 | mp_set_int(RETVAL, 0); | |
294 | } | |
295 | else { | |
296 | mp_exptmod(n, exp, mod, RETVAL); | |
297 | } | |
298 | OUTPUT: | |
299 | RETVAL | |
300 | ||
301 | ############################################################################## | |
302 | # _modinv() - compute the inverse of x % y | |
303 | ||
304 | void | |
305 | _modinv(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
306 | PREINIT: | |
307 | int rc, sign; | |
308 | SV* s; | |
309 | mp_int* RETVAL; | |
310 | PPCODE: | |
311 | Newz(0, RETVAL, 1, mp_int); | |
312 | mp_init(RETVAL); | |
313 | rc = mp_invmod(x, y, RETVAL); | |
314 | EXTEND(SP, 2); /* we return two values */ | |
315 | if (rc != MP_OKAY) { | |
316 | /* Inverse doesn't exist. Return both values undefined. */ | |
317 | PUSHs(&PL_sv_undef); | |
318 | PUSHs(&PL_sv_undef); | |
319 | } | |
320 | else { | |
321 | /* Inverse exists. When the modulus to mp_invert() is positive, | |
322 | * the returned value is also positive. */ | |
323 | PUSHs(sv_2mortal(sv_from_mpi(RETVAL))); | |
324 | s = sv_newmortal(); | |
325 | sv_setpvn(s, "+", 1); | |
326 | PUSHs(s); | |
327 | } | |
328 | ||
329 | ############################################################################## | |
330 | # _add() - add $y to $x in place | |
331 | ||
332 | void | |
333 | _add(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
334 | PPCODE: | |
335 | mp_add(x, y, x); | |
336 | XPUSHs(ST(1)); /* x */ | |
337 | ||
338 | ############################################################################## | |
339 | # _inc() - modify x inline by doing x++ | |
340 | ||
341 | void | |
342 | _inc(Class, Math::BigInt::LTM x) | |
343 | PPCODE: | |
344 | mp_add_d(x, 1, x); | |
345 | XPUSHs(ST(1)); /* x */ | |
346 | ||
347 | ############################################################################## | |
348 | # _dec() - modify x inline by doing x-- | |
349 | ||
350 | void | |
351 | _dec(Class, Math::BigInt::LTM x) | |
352 | PPCODE: | |
353 | mp_sub_d(x, 1, x); | |
354 | XPUSHs(ST(1)); /* x */ | |
355 | ||
356 | ############################################################################## | |
357 | # _sub() - $x - $y | |
358 | # $x is always larger than $y! So overflow/underflow can not happen here. | |
359 | ||
360 | void | |
361 | _sub(Class, Math::BigInt::LTM x, Math::BigInt::LTM y, ...) | |
362 | PPCODE: | |
363 | if ( items == 4 && SvTRUE(ST(3)) ) { | |
364 | /* y -= x */ | |
365 | mp_sub(x, y, y); | |
366 | XPUSHs(ST(2)); /* y */ | |
367 | } | |
368 | else { | |
369 | /* x -= y */ | |
370 | mp_sub(x, y, x); | |
371 | XPUSHs(ST(1)); /* x */ | |
372 | } | |
373 | ||
374 | ############################################################################## | |
375 | # _rsft() | |
376 | ||
377 | void | |
378 | _rsft(Class, Math::BigInt::LTM x, Math::BigInt::LTM y, unsigned long base_int) | |
379 | PREINIT: | |
380 | mp_int* BASE; | |
381 | PPCODE: | |
382 | Newz(0, BASE, 1, mp_int); | |
383 | mp_init_set_int(BASE, base_int); | |
384 | mp_expt_d(BASE, mp_get_long(y), BASE); | |
385 | mp_div(x, BASE, x, NULL); | |
386 | mp_clear(BASE); | |
387 | Safefree(BASE); | |
388 | XPUSHs(ST(1)); /* x */ | |
389 | ||
390 | ############################################################################## | |
391 | # _lsft() | |
392 | ||
393 | void | |
394 | _lsft(Class, Math::BigInt::LTM x, Math::BigInt::LTM y, unsigned long base_int) | |
395 | PREINIT: | |
396 | mp_int* BASE; | |
397 | PPCODE: | |
398 | Newz(0, BASE, 1, mp_int); | |
399 | mp_init_set_int(BASE, base_int); | |
400 | mp_expt_d(BASE, mp_get_long(y), BASE); | |
401 | mp_mul(x, BASE, x); | |
402 | mp_clear(BASE); | |
403 | Safefree(BASE); | |
404 | XPUSHs(ST(1)); /* x */ | |
405 | ||
406 | ############################################################################## | |
407 | # _mul() | |
408 | ||
409 | void | |
410 | _mul(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
411 | PPCODE: | |
412 | mp_mul(x, y, x); | |
413 | XPUSHs(ST(1)); /* x */ | |
414 | ||
415 | ############################################################################## | |
416 | # _div(): x /= y or (x,rem) = x / y | |
417 | ||
418 | void | |
419 | _div(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
420 | PREINIT: | |
421 | mp_int * rem; | |
422 | PPCODE: | |
423 | if (GIMME_V == G_ARRAY) { | |
424 | Newz(0, rem, 1, mp_int); | |
425 | mp_init(rem); | |
426 | mp_div(x, y, x, rem); | |
427 | EXTEND(SP, 2); | |
428 | PUSHs(ST(1)); /* x */ | |
429 | PUSHs(sv_2mortal(sv_from_mpi(rem))); | |
430 | } | |
431 | else { | |
432 | mp_div(x, y, x, NULL); | |
433 | XPUSHs(ST(1)); /* x */ | |
434 | } | |
435 | ||
436 | ############################################################################## | |
437 | # _mod() - x %= y | |
438 | ||
439 | void | |
440 | _mod(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
441 | PPCODE: | |
442 | mp_mod(x, y, x); | |
443 | XPUSHs(ST(1)); /* x */ | |
444 | ||
445 | ############################################################################## | |
446 | # _acmp() - cmp two numbers | |
447 | ||
448 | int | |
449 | _acmp(Class, Math::BigInt::LTM m, Math::BigInt::LTM n) | |
450 | CODE: | |
451 | RETVAL = mp_cmp(m, n); | |
452 | if ( RETVAL < 0) RETVAL = -1; | |
453 | if ( RETVAL > 0) RETVAL = 1; | |
454 | OUTPUT: | |
455 | RETVAL | |
456 | ||
457 | ############################################################################## | |
458 | # _is_zero() | |
459 | ||
460 | int | |
461 | _is_zero(Class, Math::BigInt::LTM x) | |
462 | CODE: | |
463 | RETVAL = (mp_iszero(x) == MP_YES) ? 1 : 0; | |
464 | OUTPUT: | |
465 | RETVAL | |
466 | ||
467 | ############################################################################## | |
468 | # _is_one() | |
469 | ||
470 | int | |
471 | _is_one(Class, Math::BigInt::LTM x) | |
472 | CODE: | |
473 | RETVAL = (mp_cmp_d(x, 1) == MP_EQ) ? 1 : 0; | |
474 | OUTPUT: | |
475 | RETVAL | |
476 | ||
477 | ############################################################################## | |
478 | # _is_two() | |
479 | ||
480 | int | |
481 | _is_two(Class, Math::BigInt::LTM x) | |
482 | CODE: | |
483 | RETVAL = (mp_cmp_d(x, 2) == MP_EQ) ? 1 : 0; | |
484 | OUTPUT: | |
485 | RETVAL | |
486 | ||
487 | ############################################################################## | |
488 | # _is_ten() | |
489 | ||
490 | int | |
491 | _is_ten(Class, Math::BigInt::LTM x) | |
492 | CODE: | |
493 | RETVAL = (mp_cmp_d(x, 10) == MP_EQ) ? 1 : 0; | |
494 | OUTPUT: | |
495 | RETVAL | |
496 | ||
497 | ############################################################################## | |
498 | # _pow() - x **= y | |
499 | ||
500 | void | |
501 | _pow(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
502 | PPCODE: | |
503 | mp_expt_d(x, mp_get_long(y), x); | |
504 | XPUSHs(ST(1)); /* x */ | |
505 | ||
506 | ############################################################################## | |
507 | # _gcd() - gcd(m,n) | |
508 | ||
509 | Math::BigInt::LTM | |
510 | _gcd(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
511 | CODE: | |
512 | Newz(0, RETVAL, 1, mp_int); | |
513 | mp_init(RETVAL); | |
514 | mp_gcd(x, y, RETVAL); | |
515 | OUTPUT: | |
516 | RETVAL | |
517 | ||
518 | ############################################################################## | |
519 | # _and() - m &= n | |
520 | ||
521 | void | |
522 | _and(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
523 | PPCODE: | |
524 | mp_and(x, y, x); | |
525 | XPUSHs(ST(1)); /* x */ | |
526 | ||
527 | ############################################################################## | |
528 | # _xor() - m =^ n | |
529 | ||
530 | void | |
531 | _xor(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
532 | PPCODE: | |
533 | mp_xor(x, y, x); | |
534 | XPUSHs(ST(1)); /* x */ | |
535 | ||
536 | ############################################################################## | |
537 | # _or() - m =| n | |
538 | ||
539 | void | |
540 | _or(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
541 | PPCODE: | |
542 | mp_or(x, y, x); | |
543 | XPUSHs(ST(1)); /* x */ | |
544 | ||
545 | ############################################################################## | |
546 | # _copy() | |
547 | ||
548 | Math::BigInt::LTM | |
549 | _copy(Class, Math::BigInt::LTM m) | |
550 | CODE: | |
551 | Newz(0, RETVAL, 1, mp_int); | |
552 | mp_init(RETVAL); | |
553 | mp_copy(m, RETVAL); | |
554 | OUTPUT: | |
555 | RETVAL | |
556 | ||
557 | ############################################################################## | |
558 | # _is_odd() - test for number being odd | |
559 | ||
560 | int | |
561 | _is_odd(Class, Math::BigInt::LTM n) | |
562 | CODE: | |
563 | RETVAL = (mp_isodd(n) == MP_YES) ? 1 : 0; | |
564 | OUTPUT: | |
565 | RETVAL | |
566 | ||
567 | ############################################################################## | |
568 | # _is_even() - test for number being even | |
569 | ||
570 | int | |
571 | _is_even(Class, Math::BigInt::LTM n) | |
572 | CODE: | |
573 | RETVAL = (mp_iseven(n) == MP_YES || mp_iszero(n) == MP_YES) ? 1 : 0; | |
574 | OUTPUT: | |
575 | RETVAL | |
576 | ||
577 | ############################################################################## | |
578 | # _sqrt() - square root | |
579 | ||
580 | void | |
581 | _sqrt(Class, Math::BigInt::LTM x) | |
582 | PPCODE: | |
583 | mp_sqrt(x, x); | |
584 | XPUSHs(ST(1)); /* x */ | |
585 | ||
586 | ############################################################################## | |
587 | # _root() - integer roots | |
588 | ||
589 | void | |
590 | _root(Class, Math::BigInt::LTM x, Math::BigInt::LTM y) | |
591 | PPCODE: | |
592 | mp_n_root(x, mp_get_long(y), x); | |
593 | XPUSHs(ST(1)); /* x */ | |
594 | ||
595 | ############################################################################## | |
596 | # Storable hooks | |
597 | ||
598 | void | |
599 | STORABLE_thaw(blank_obj, cloning, serialized, ...) | |
600 | SV *blank_obj | |
601 | SV *cloning = NO_INIT | |
602 | SV *serialized | |
603 | PREINIT: | |
604 | SV *target, *tmp; | |
605 | mp_int *mpi; | |
606 | mp_int *mpitmp; | |
607 | PPCODE: | |
608 | if (SvROK(blank_obj) && sv_isa(blank_obj, "Math::BigInt::LTM")) { | |
609 | Newz(0, mpi, 1, mp_int); | |
610 | mp_init(mpi); | |
611 | mp_read_radix(mpi, SvPV_nolen(serialized), 10); | |
612 | target = SvRV(blank_obj); | |
613 | SvIV_set(target, PTR2IV(mpi)); | |
614 | SvIOK_on(target); | |
615 | PUSHs(target); | |
616 | XSRETURN(1); | |
617 | } | |
618 | else | |
619 | croak("Bad object for Math::BigInt::LTM::STORABLE_thaw call"); | |
620 | ||
621 | SV * | |
622 | STORABLE_freeze(self, cloning = NULL) | |
623 | Math::BigInt::LTM self | |
624 | SV *cloning = NO_INIT | |
625 | PREINIT: | |
626 | unsigned long len; | |
627 | char *buf; | |
628 | CODE: | |
629 | if (mp_iszero(self) == MP_YES) { | |
630 | RETVAL = newSVpv("0", 0); | |
631 | } | |
632 | else { | |
633 | len = mp_unsigned_bin_size(self) * 3 + 2; /* decimal size ~ 3 * bin size +1 for sign +1 for NUL-byte */ | |
634 | Newz(0, buf, len, char); | |
635 | mp_toradix_n(self, buf, 10, len); | |
636 | RETVAL = newSVpv(buf, 0); | |
637 | Safefree(buf); | |
638 | } | |
639 | OUTPUT: | |
640 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Cipher | |
1 | ||
2 | Crypt::Cipher | |
3 | _new(cipher_name, key, rounds=0) | |
4 | char * cipher_name | |
5 | SV * key | |
6 | int rounds | |
7 | CODE: | |
8 | { | |
9 | STRLEN key_len; | |
10 | unsigned char *key_data=NULL; | |
11 | int rv; | |
12 | int id; | |
13 | ||
14 | if (!SvPOK (key)) croak("FATAL: key must be string scalar"); | |
15 | key_data = (unsigned char *)SvPVbyte(key, key_len); | |
16 | ||
17 | id = find_cipher(cipher_name); | |
18 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
19 | ||
20 | Newz(0, RETVAL, 1, struct cipher_struct); | |
21 | if (!RETVAL) croak("FATAL: Newz failed"); | |
22 | ||
23 | RETVAL->id = id; | |
24 | RETVAL->desc = &cipher_descriptor[id]; | |
25 | rv = RETVAL->desc->setup(key_data, (int)key_len, rounds, &RETVAL->skey); | |
26 | if(rv!=CRYPT_OK) croak("FATAL: cipher setup failed: %s", error_to_string(rv)); | |
27 | } | |
28 | OUTPUT: | |
29 | RETVAL | |
30 | ||
31 | void | |
32 | DESTROY(self) | |
33 | Crypt::Cipher self | |
34 | CODE: | |
35 | Safefree(self); | |
36 | ||
37 | int | |
38 | _max_keysize(self, ...) | |
39 | Crypt::Cipher self | |
40 | CODE: | |
41 | RETVAL = self->desc->max_key_length; | |
42 | OUTPUT: | |
43 | RETVAL | |
44 | ||
45 | int | |
46 | _min_keysize(self, ...) | |
47 | Crypt::Cipher self | |
48 | CODE: | |
49 | RETVAL = self->desc->min_key_length; | |
50 | OUTPUT: | |
51 | RETVAL | |
52 | ||
53 | int | |
54 | _blocksize(self, ...) | |
55 | Crypt::Cipher self | |
56 | CODE: | |
57 | RETVAL = self->desc->block_length; | |
58 | OUTPUT: | |
59 | RETVAL | |
60 | ||
61 | int | |
62 | _default_rounds(self, ...) | |
63 | Crypt::Cipher self | |
64 | CODE: | |
65 | RETVAL = self->desc->default_rounds; | |
66 | OUTPUT: | |
67 | RETVAL | |
68 | ||
69 | SV * | |
70 | encrypt(self, data) | |
71 | Crypt::Cipher self | |
72 | SV * data | |
73 | CODE: | |
74 | { | |
75 | int rv; | |
76 | STRLEN len; | |
77 | void *plaintext = SvPVbyte(data, len); | |
78 | ||
79 | if (len==0) | |
80 | RETVAL = newSVpvn("", 0); | |
81 | else if (len % self->desc->block_length) | |
82 | croak ("FATAL: sizeof(data) should be multiple of blocksize (%d)", self->desc->block_length); | |
83 | else { | |
84 | /* idea from Crypt::Rijndael */ | |
85 | RETVAL = NEWSV(0, len); | |
86 | SvPOK_only(RETVAL); | |
87 | SvCUR_set(RETVAL, len); | |
88 | rv = self->desc->ecb_encrypt((unsigned char *)plaintext, (unsigned char *)SvPV_nolen(RETVAL), &self->skey); | |
89 | if (rv!=CRYPT_OK) croak("FATAL: encrypt failed: %s", error_to_string(rv)); | |
90 | } | |
91 | } | |
92 | OUTPUT: | |
93 | RETVAL | |
94 | ||
95 | SV * | |
96 | decrypt(self, data) | |
97 | Crypt::Cipher self | |
98 | SV * data | |
99 | CODE: | |
100 | { | |
101 | int rv; | |
102 | STRLEN len; | |
103 | void *ciphertext = SvPVbyte(data, len); | |
104 | ||
105 | if (len==0) | |
106 | RETVAL = newSVpvn("", 0); | |
107 | else if (len % self->desc->block_length) | |
108 | croak ("FATAL: sizeof(data) should be multiple of blocksize (%d)", self->desc->block_length); | |
109 | else { | |
110 | /* idea from Crypt::Rijndael */ | |
111 | RETVAL = NEWSV(0, len); | |
112 | SvPOK_only(RETVAL); | |
113 | SvCUR_set(RETVAL, len); | |
114 | rv = self->desc->ecb_decrypt((unsigned char *)ciphertext, (unsigned char *)SvPV_nolen(RETVAL), &self->skey); | |
115 | if (rv!=CRYPT_OK) croak("FATAL: decrypt failed: %s", error_to_string(rv)); | |
116 | } | |
117 | } | |
118 | OUTPUT: | |
119 | RETVAL | |
120 | ||
121 | int | |
122 | _block_length_by_name(cipher_name) | |
123 | char * cipher_name | |
124 | CODE: | |
125 | { | |
126 | int rv, id; | |
127 | ||
128 | id = find_cipher(cipher_name); | |
129 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
130 | ||
131 | rv = cipher_descriptor[id].block_length; | |
132 | if (!rv) XSRETURN_UNDEF; | |
133 | RETVAL = rv; | |
134 | } | |
135 | OUTPUT: | |
136 | RETVAL | |
137 | ||
138 | int | |
139 | _min_key_length_by_name(cipher_name) | |
140 | char * cipher_name | |
141 | CODE: | |
142 | { | |
143 | int rv, id; | |
144 | ||
145 | id = find_cipher(cipher_name); | |
146 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
147 | ||
148 | rv = cipher_descriptor[id].min_key_length; | |
149 | if (!rv) XSRETURN_UNDEF; | |
150 | RETVAL = rv; | |
151 | } | |
152 | OUTPUT: | |
153 | RETVAL | |
154 | ||
155 | int | |
156 | _max_key_length_by_name(cipher_name) | |
157 | char * cipher_name | |
158 | CODE: | |
159 | { | |
160 | int rv, id; | |
161 | ||
162 | id = find_cipher(cipher_name); | |
163 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
164 | ||
165 | rv = cipher_descriptor[id].max_key_length; | |
166 | if (!rv) XSRETURN_UNDEF; | |
167 | RETVAL = rv; | |
168 | } | |
169 | OUTPUT: | |
170 | RETVAL | |
171 | ||
172 | int | |
173 | _default_rounds_by_name(cipher_name) | |
174 | char * cipher_name | |
175 | CODE: | |
176 | { | |
177 | int rv, id; | |
178 | ||
179 | id = find_cipher(cipher_name); | |
180 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
181 | ||
182 | rv = cipher_descriptor[id].default_rounds; | |
183 | if (!rv) XSRETURN_UNDEF; | |
184 | RETVAL = rv; | |
185 | } | |
186 | OUTPUT: | |
187 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Digest | |
1 | ||
2 | Crypt::Digest | |
3 | _new(digest_name) | |
4 | char * digest_name | |
5 | CODE: | |
6 | { | |
7 | int rv; | |
8 | int id; | |
9 | ||
10 | id = find_hash(digest_name); | |
11 | if(id==-1) croak("FATAL: find_hash failed for '%s'", digest_name); | |
12 | ||
13 | Newz(0, RETVAL, 1, struct digest_struct); | |
14 | if (!RETVAL) croak("FATAL: Newz failed"); | |
15 | ||
16 | RETVAL->id = id; | |
17 | RETVAL->desc = &hash_descriptor[id]; | |
18 | rv = RETVAL->desc->init(&RETVAL->state); | |
19 | if(rv!=CRYPT_OK) croak("FATAL: digest setup failed: %s", error_to_string(rv)); | |
20 | } | |
21 | OUTPUT: | |
22 | RETVAL | |
23 | ||
24 | void | |
25 | DESTROY(self) | |
26 | Crypt::Digest self | |
27 | CODE: | |
28 | Safefree(self); | |
29 | ||
30 | void | |
31 | reset(self) | |
32 | Crypt::Digest self | |
33 | CODE: | |
34 | { | |
35 | int rv; | |
36 | rv = self->desc->init(&self->state); | |
37 | if (rv != CRYPT_OK) croak("FATAL: digest init failed: %s", error_to_string(rv)); | |
38 | } | |
39 | ||
40 | Crypt::Digest | |
41 | clone(self) | |
42 | Crypt::Digest self | |
43 | CODE: | |
44 | Newz(0, RETVAL, 1, struct digest_struct); | |
45 | Copy(&self->state, &RETVAL->state, 1, struct digest_struct); | |
46 | OUTPUT: | |
47 | RETVAL | |
48 | ||
49 | void | |
50 | add(Crypt::Digest self, ...) | |
51 | PPCODE: | |
52 | { | |
53 | STRLEN inlen; | |
54 | int rv, i; | |
55 | unsigned char *in; | |
56 | ||
57 | for(i=1; i<items; i++) { | |
58 | in = (unsigned char *)SvPVbyte(ST(i), inlen); | |
59 | if (inlen>0) { | |
60 | rv = self->desc->process(&self->state, in, (unsigned long)inlen); | |
61 | if (rv != CRYPT_OK) croak("FATAL: digest process failed: %s", error_to_string(rv)); | |
62 | } | |
63 | } | |
64 | XPUSHs(ST(0)); /* return self */ | |
65 | } | |
66 | ||
67 | SV * | |
68 | digest(self) | |
69 | Crypt::Digest self | |
70 | CODE: | |
71 | { | |
72 | unsigned char hash[MAXBLOCKSIZE]; | |
73 | int rv; | |
74 | ||
75 | rv = self->desc->done(&self->state, hash); | |
76 | if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv)); | |
77 | RETVAL = newSVpvn((char *) hash, self->desc->hashsize); | |
78 | } | |
79 | OUTPUT: | |
80 | RETVAL | |
81 | ||
82 | SV * | |
83 | hexdigest(self) | |
84 | Crypt::Digest self | |
85 | CODE: | |
86 | { | |
87 | int rv; | |
88 | unsigned long i; | |
89 | unsigned char hash[MAXBLOCKSIZE]; | |
90 | char hash_hex[MAXBLOCKSIZE*2 + 1]; | |
91 | ||
92 | rv = self->desc->done(&self->state, hash); | |
93 | if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv)); | |
94 | ||
95 | hash_hex[0] = '\0'; | |
96 | for(i=0; i<self->desc->hashsize; i++) | |
97 | sprintf(&hash_hex[2*i], "%02x", hash[i]); | |
98 | RETVAL = newSVpvn(hash_hex, strlen(hash_hex)); | |
99 | } | |
100 | OUTPUT: | |
101 | RETVAL | |
102 | ||
103 | SV * | |
104 | b64digest(self) | |
105 | Crypt::Digest self | |
106 | CODE: | |
107 | { | |
108 | int rv; | |
109 | unsigned long outlen; | |
110 | unsigned char hash[MAXBLOCKSIZE]; | |
111 | char hash_base64[MAXBLOCKSIZE*2 + 1]; | |
112 | ||
113 | rv = self->desc->done(&self->state, hash); | |
114 | if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv)); | |
115 | ||
116 | outlen = sizeof(hash_base64); | |
117 | rv = base64_encode(hash, self->desc->hashsize, (unsigned char *)hash_base64, &outlen); | |
118 | if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); | |
119 | RETVAL = newSVpvn(hash_base64, outlen); | |
120 | } | |
121 | OUTPUT: | |
122 | RETVAL | |
123 | ||
124 | SV * | |
125 | b64udigest(self) | |
126 | Crypt::Digest self | |
127 | CODE: | |
128 | { | |
129 | int rv; | |
130 | unsigned long outlen; | |
131 | unsigned char hash[MAXBLOCKSIZE]; | |
132 | char hash_base64[MAXBLOCKSIZE*2 + 1]; | |
133 | ||
134 | rv = self->desc->done(&self->state, hash); | |
135 | if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv)); | |
136 | ||
137 | outlen = sizeof(hash_base64); | |
138 | rv = base64url_encode(hash, self->desc->hashsize, (unsigned char *)hash_base64, &outlen); | |
139 | if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); | |
140 | RETVAL = newSVpvn(hash_base64, outlen); | |
141 | } | |
142 | OUTPUT: | |
143 | RETVAL | |
144 | ||
145 | int | |
146 | _hashsize(self) | |
147 | Crypt::Digest self | |
148 | CODE: | |
149 | RETVAL = self->desc->hashsize; | |
150 | OUTPUT: | |
151 | RETVAL | |
152 | ||
153 | int | |
154 | _hashsize_by_name(digest_name) | |
155 | char * digest_name | |
156 | CODE: | |
157 | { | |
158 | int rv, id; | |
159 | ||
160 | id = find_hash(digest_name); | |
161 | if(id==-1) croak("FATAL: find_digest failed for '%s'", digest_name); | |
162 | ||
163 | rv = hash_descriptor[id].hashsize; | |
164 | if (!rv) croak("FATAL: invalid hashsize for '%s'", digest_name);; | |
165 | RETVAL = rv; | |
166 | } | |
167 | OUTPUT: | |
168 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::KeyDerivation | |
1 | ||
2 | SV * | |
3 | _pkcs_5_alg1(SV * password, SV * salt, int iteration_count, char * hash_name, int len) | |
4 | CODE: | |
5 | { | |
6 | /* | |
7 | int pkcs_5_alg1(const unsigned char *password, unsigned long password_len, | |
8 | const unsigned char *salt, | |
9 | int iteration_count, int hash_idx, | |
10 | unsigned char *out, unsigned long *outlen) | |
11 | */ | |
12 | int rv, id; | |
13 | unsigned long output_len; | |
14 | unsigned char *output; | |
15 | unsigned char *password_ptr=NULL; | |
16 | STRLEN password_len=0; | |
17 | unsigned char *salt_ptr=NULL; | |
18 | STRLEN salt_len=0; | |
19 | ||
20 | id = find_hash(hash_name); | |
21 | if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
22 | ||
23 | password_ptr = (unsigned char *)SvPVbyte(password, password_len); | |
24 | salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len); | |
25 | if (salt_len < 8) croak("FATAL: salt_len has to be 8"); | |
26 | ||
27 | output_len = len; | |
28 | Newz(0, output, output_len, unsigned char); | |
29 | if (!output) croak("FATAL: Newz failed [%ld]", output_len); | |
30 | ||
31 | rv = pkcs_5_alg1(password_ptr, (unsigned long)password_len, salt_ptr, iteration_count, id, output, &output_len); | |
32 | if (rv != CRYPT_OK) croak("FATAL: pkcs_5_alg1 process failed: %s", error_to_string(rv)); | |
33 | ||
34 | RETVAL = newSVpvn((char *)output, output_len); | |
35 | Safefree(output); | |
36 | } | |
37 | OUTPUT: | |
38 | RETVAL | |
39 | ||
40 | SV * | |
41 | _pkcs_5_alg2(SV * password, SV * salt, int iteration_count, char * hash_name, int len) | |
42 | CODE: | |
43 | { | |
44 | /* | |
45 | int pkcs_5_alg2(const unsigned char *password, unsigned long password_len, | |
46 | const unsigned char *salt, unsigned long salt_len, | |
47 | int iteration_count, int hash_idx, | |
48 | unsigned char *out, unsigned long *outlen) | |
49 | */ | |
50 | int rv, id; | |
51 | unsigned long output_len; | |
52 | unsigned char *output; | |
53 | unsigned char *password_ptr=NULL; | |
54 | STRLEN password_len=0; | |
55 | unsigned char *salt_ptr=NULL; | |
56 | STRLEN salt_len=0; | |
57 | ||
58 | id = find_hash(hash_name); | |
59 | if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
60 | ||
61 | password_ptr = (unsigned char *)SvPVbyte(password, password_len); | |
62 | salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len); | |
63 | ||
64 | output_len = len; | |
65 | Newz(0, output, output_len, unsigned char); | |
66 | if (!output) croak("FATAL: Newz failed [%ld]", output_len); | |
67 | ||
68 | rv = pkcs_5_alg2(password_ptr, (unsigned long)password_len, salt_ptr, (unsigned long)salt_len, iteration_count, id, output, &output_len); | |
69 | if (rv != CRYPT_OK) croak("FATAL: pkcs_5_alg2 process failed: %s", error_to_string(rv)); | |
70 | ||
71 | RETVAL = newSVpvn((char *)output, output_len); | |
72 | Safefree(output); | |
73 | } | |
74 | OUTPUT: | |
75 | RETVAL | |
76 | ||
77 | SV * | |
78 | _hkdf_extract(char * hash_name, SV * salt, SV * in) | |
79 | CODE: | |
80 | { | |
81 | /* | |
82 | int hkdf_extract(int hash_idx, const unsigned char *salt, unsigned long saltlen, | |
83 | const unsigned char *in, unsigned long inlen, | |
84 | unsigned char *out, unsigned long *outlen) | |
85 | */ | |
86 | int rv, id; | |
87 | unsigned char output[MAXBLOCKSIZE]; | |
88 | unsigned long output_len; | |
89 | unsigned char *in_ptr=NULL; | |
90 | STRLEN in_len=0; | |
91 | unsigned char *salt_ptr=NULL; | |
92 | STRLEN salt_len=0; | |
93 | ||
94 | id = find_hash(hash_name); | |
95 | if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
96 | ||
97 | in_ptr = (unsigned char *)SvPVbyte(in, in_len); | |
98 | salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len); | |
99 | ||
100 | output_len = sizeof(output); | |
101 | rv = hkdf_extract(id, salt_ptr, (unsigned long)salt_len, in_ptr, (unsigned long)in_len, output, &output_len); | |
102 | if (rv != CRYPT_OK) croak("FATAL: hkdf_extract process failed: %s", error_to_string(rv)); | |
103 | ||
104 | RETVAL = newSVpvn((char *)output, output_len); | |
105 | } | |
106 | OUTPUT: | |
107 | RETVAL | |
108 | ||
109 | SV * | |
110 | _hkdf_expand(char * hash_name, SV * info, SV * in, unsigned long output_len) | |
111 | CODE: | |
112 | { | |
113 | /* | |
114 | int hkdf_expand(int hash_idx, const unsigned char *info, unsigned long infolen, | |
115 | const unsigned char *in, unsigned long inlen, | |
116 | unsigned char *out, unsigned long outlen) | |
117 | */ | |
118 | int rv, id; | |
119 | unsigned char *output; | |
120 | unsigned char *in_ptr=NULL; | |
121 | STRLEN in_len=0; | |
122 | unsigned char *info_ptr=NULL; | |
123 | STRLEN info_len=0; | |
124 | ||
125 | id = find_hash(hash_name); | |
126 | if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
127 | ||
128 | in_ptr = (unsigned char *)SvPVbyte(in, in_len); | |
129 | info_ptr = (unsigned char *)SvPVbyte(info, info_len); | |
130 | ||
131 | Newz(0, output, output_len, unsigned char); | |
132 | if (!output) croak("FATAL: Newz failed [%ld]", output_len); | |
133 | ||
134 | rv = hkdf_expand(id, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len); | |
135 | if (rv != CRYPT_OK) croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv)); | |
136 | ||
137 | RETVAL = newSVpvn((char *)output, output_len); | |
138 | Safefree(output); | |
139 | } | |
140 | OUTPUT: | |
141 | RETVAL | |
142 | ||
143 | SV * | |
144 | _hkdf(char * hash_name, SV * salt, SV * info, SV * in, unsigned long output_len) | |
145 | CODE: | |
146 | { | |
147 | /* | |
148 | int hkdf(int hash_idx, const unsigned char *salt, unsigned long saltlen, | |
149 | const unsigned char *info, unsigned long infolen, | |
150 | const unsigned char *in, unsigned long inlen, | |
151 | unsigned char *out, unsigned long outlen) | |
152 | */ | |
153 | int rv, id; | |
154 | unsigned char *output; | |
155 | unsigned char *in_ptr=NULL; | |
156 | STRLEN in_len=0; | |
157 | unsigned char *info_ptr=NULL; | |
158 | STRLEN info_len=0; | |
159 | unsigned char *salt_ptr=NULL; | |
160 | STRLEN salt_len=0; | |
161 | ||
162 | id = find_hash(hash_name); | |
163 | if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
164 | ||
165 | in_ptr = (unsigned char *)SvPVbyte(in, in_len); | |
166 | info_ptr = (unsigned char *)SvPVbyte(info, info_len); | |
167 | salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len); | |
168 | ||
169 | Newz(0, output, output_len, unsigned char); | |
170 | if (!output) croak("FATAL: Newz failed [%ld]", output_len); | |
171 | ||
172 | rv = hkdf(id, salt_ptr, (unsigned long)salt_len, info_ptr, (unsigned long)info_len, in_ptr, (unsigned long)in_len, output, output_len); | |
173 | if (rv != CRYPT_OK) croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv)); | |
174 | ||
175 | RETVAL = newSVpvn((char *)output, output_len); | |
176 | Safefree(output); | |
177 | } | |
178 | OUTPUT: | |
179 | RETVAL | |
180 |
0 | MODULE = CryptX PACKAGE = Crypt::Mac::F9 | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mac::F9 | |
5 | _new(char * cipher_name, SV * key) | |
6 | CODE: | |
7 | { | |
8 | STRLEN k_len=0; | |
9 | unsigned char *k=NULL; | |
10 | int rv; | |
11 | int id; | |
12 | ||
13 | id = find_cipher(cipher_name); | |
14 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
15 | ||
16 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
17 | k = (unsigned char *) SvPVbyte(key, k_len); | |
18 | ||
19 | Newz(0, RETVAL, 1, struct f9_struct); | |
20 | if (!RETVAL) croak("FATAL: Newz failed"); | |
21 | ||
22 | rv = f9_init(&RETVAL->state, id, k, (unsigned long)k_len); | |
23 | if (rv != CRYPT_OK) croak("FATAL: f9_init failed: %s", error_to_string(rv)); | |
24 | } | |
25 | OUTPUT: | |
26 | RETVAL | |
27 | ||
28 | void | |
29 | DESTROY(Crypt::Mac::F9 self) | |
30 | CODE: | |
31 | Safefree(self); | |
32 | ||
33 | Crypt::Mac::F9 | |
34 | clone(Crypt::Mac::F9 self) | |
35 | CODE: | |
36 | Newz(0, RETVAL, 1, struct f9_struct); | |
37 | if (!RETVAL) croak("FATAL: Newz failed"); | |
38 | Copy(&self->state, &RETVAL->state, 1, struct f9_struct); | |
39 | OUTPUT: | |
40 | RETVAL | |
41 | ||
42 | void | |
43 | _add_single(Crypt::Mac::F9 self, SV * data) | |
44 | CODE: | |
45 | { | |
46 | int rv; | |
47 | STRLEN in_data_len; | |
48 | unsigned char *in_data; | |
49 | ||
50 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
51 | if (in_data_len>0) { | |
52 | rv = f9_process(&self->state, in_data, (unsigned long)in_data_len); | |
53 | if (rv != CRYPT_OK) croak("FATAL: f9_process failed: %s", error_to_string(rv)); | |
54 | } | |
55 | } | |
56 | ||
57 | SV * | |
58 | mac(Crypt::Mac::F9 self) | |
59 | CODE: | |
60 | { | |
61 | char mac[MAXBLOCKSIZE]; | |
62 | unsigned long mac_len; | |
63 | int rv; | |
64 | ||
65 | mac_len = sizeof(mac); | |
66 | rv = f9_done(&self->state, (unsigned char*)mac, &mac_len); | |
67 | if (rv != CRYPT_OK) croak("FATAL: f9_done failed: %s", error_to_string(rv)); | |
68 | RETVAL = newSVpvn(mac, mac_len); | |
69 | } | |
70 | OUTPUT: | |
71 | RETVAL | |
72 | ||
73 | SV * | |
74 | b64mac(Crypt::Mac::F9 self) | |
75 | CODE: | |
76 | { | |
77 | unsigned char mac[MAXBLOCKSIZE]; | |
78 | unsigned long mac_len; | |
79 | int rv; | |
80 | unsigned long outlen; | |
81 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
82 | ||
83 | mac_len = sizeof(mac); | |
84 | rv = f9_done(&self->state, mac, &mac_len); | |
85 | if (rv != CRYPT_OK) croak("FATAL: f9_done failed: %s", error_to_string(rv)); | |
86 | outlen = sizeof(mac_base64); | |
87 | rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
88 | if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); | |
89 | RETVAL = newSVpvn(mac_base64, outlen); | |
90 | } | |
91 | OUTPUT: | |
92 | RETVAL | |
93 | ||
94 | SV * | |
95 | b64umac(Crypt::Mac::F9 self) | |
96 | CODE: | |
97 | { | |
98 | unsigned char mac[MAXBLOCKSIZE]; | |
99 | unsigned long mac_len; | |
100 | int rv; | |
101 | unsigned long outlen; | |
102 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
103 | ||
104 | mac_len = sizeof(mac); | |
105 | rv = f9_done(&self->state, mac, &mac_len); | |
106 | if (rv != CRYPT_OK) croak("FATAL: f9_done failed: %s", error_to_string(rv)); | |
107 | outlen = sizeof(mac_base64); | |
108 | rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
109 | if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); | |
110 | RETVAL = newSVpvn(mac_base64, outlen); | |
111 | } | |
112 | OUTPUT: | |
113 | RETVAL | |
114 | ||
115 | SV * | |
116 | hexmac(Crypt::Mac::F9 self) | |
117 | CODE: | |
118 | { | |
119 | unsigned char mac[MAXBLOCKSIZE]; | |
120 | unsigned long mac_len, i; | |
121 | int rv; | |
122 | char mac_hex[MAXBLOCKSIZE*2 + 1]; | |
123 | ||
124 | mac_len = sizeof(mac); | |
125 | rv = f9_done(&self->state, mac, &mac_len); | |
126 | if (rv != CRYPT_OK) croak("FATAL: f9_done failed: %s", error_to_string(rv)); | |
127 | mac_hex[0] = '\0'; | |
128 | for(i=0; i<mac_len; i++) | |
129 | sprintf(&mac_hex[2*i], "%02x", mac[i]); | |
130 | RETVAL = newSVpvn(mac_hex, strlen(mac_hex)); | |
131 | } | |
132 | OUTPUT: | |
133 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mac::HMAC | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mac::HMAC | |
5 | _new(char * hash_name, SV * key) | |
6 | CODE: | |
7 | { | |
8 | STRLEN k_len=0; | |
9 | unsigned char *k=NULL; | |
10 | int rv; | |
11 | int id; | |
12 | ||
13 | id = find_hash(hash_name); | |
14 | if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
15 | ||
16 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
17 | k = (unsigned char *) SvPVbyte(key, k_len); | |
18 | ||
19 | Newz(0, RETVAL, 1, struct hmac_struct); | |
20 | if (!RETVAL) croak("FATAL: Newz failed"); | |
21 | ||
22 | rv = hmac_init(&RETVAL->state, id, k, (unsigned long)k_len); | |
23 | if (rv != CRYPT_OK) croak("FATAL: hmac_init failed: %s", error_to_string(rv)); | |
24 | } | |
25 | OUTPUT: | |
26 | RETVAL | |
27 | ||
28 | void | |
29 | DESTROY(Crypt::Mac::HMAC self) | |
30 | CODE: | |
31 | Safefree(self); | |
32 | ||
33 | Crypt::Mac::HMAC | |
34 | clone(Crypt::Mac::HMAC self) | |
35 | CODE: | |
36 | Newz(0, RETVAL, 1, struct hmac_struct); | |
37 | if (!RETVAL) croak("FATAL: Newz failed"); | |
38 | Copy(&self->state, &RETVAL->state, 1, struct hmac_struct); | |
39 | OUTPUT: | |
40 | RETVAL | |
41 | ||
42 | void | |
43 | _add_single(Crypt::Mac::HMAC self, SV * data) | |
44 | CODE: | |
45 | { | |
46 | int rv; | |
47 | STRLEN in_data_len; | |
48 | unsigned char *in_data; | |
49 | ||
50 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
51 | if (in_data_len>0) { | |
52 | rv = hmac_process(&self->state, in_data, (unsigned long)in_data_len); | |
53 | if (rv != CRYPT_OK) croak("FATAL: hmac_process failed: %s", error_to_string(rv)); | |
54 | } | |
55 | } | |
56 | ||
57 | SV * | |
58 | mac(Crypt::Mac::HMAC self) | |
59 | CODE: | |
60 | { | |
61 | char mac[MAXBLOCKSIZE]; | |
62 | unsigned long mac_len; | |
63 | int rv; | |
64 | ||
65 | mac_len = sizeof(mac); | |
66 | rv = hmac_done(&self->state, (unsigned char*)mac, &mac_len); | |
67 | if (rv != CRYPT_OK) croak("FATAL: hmac_done failed: %s", error_to_string(rv)); | |
68 | RETVAL = newSVpvn(mac, mac_len); | |
69 | } | |
70 | OUTPUT: | |
71 | RETVAL | |
72 | ||
73 | SV * | |
74 | b64mac(Crypt::Mac::HMAC self) | |
75 | CODE: | |
76 | { | |
77 | unsigned char mac[MAXBLOCKSIZE]; | |
78 | unsigned long mac_len; | |
79 | int rv; | |
80 | unsigned long outlen; | |
81 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
82 | ||
83 | mac_len = sizeof(mac); | |
84 | rv = hmac_done(&self->state, mac, &mac_len); | |
85 | if (rv != CRYPT_OK) croak("FATAL: hmac_done failed: %s", error_to_string(rv)); | |
86 | outlen = sizeof(mac_base64); | |
87 | rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
88 | if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); | |
89 | RETVAL = newSVpvn(mac_base64, outlen); | |
90 | } | |
91 | OUTPUT: | |
92 | RETVAL | |
93 | ||
94 | SV * | |
95 | b64umac(Crypt::Mac::HMAC self) | |
96 | CODE: | |
97 | { | |
98 | unsigned char mac[MAXBLOCKSIZE]; | |
99 | unsigned long mac_len; | |
100 | int rv; | |
101 | unsigned long outlen; | |
102 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
103 | ||
104 | mac_len = sizeof(mac); | |
105 | rv = hmac_done(&self->state, mac, &mac_len); | |
106 | if (rv != CRYPT_OK) croak("FATAL: hmac_done failed: %s", error_to_string(rv)); | |
107 | outlen = sizeof(mac_base64); | |
108 | rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
109 | if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); | |
110 | RETVAL = newSVpvn(mac_base64, outlen); | |
111 | } | |
112 | OUTPUT: | |
113 | RETVAL | |
114 | ||
115 | SV * | |
116 | hexmac(Crypt::Mac::HMAC self) | |
117 | CODE: | |
118 | { | |
119 | unsigned char mac[MAXBLOCKSIZE]; | |
120 | unsigned long mac_len, i; | |
121 | int rv; | |
122 | char mac_hex[MAXBLOCKSIZE*2 + 1]; | |
123 | ||
124 | mac_len = sizeof(mac); | |
125 | rv = hmac_done(&self->state, mac, &mac_len); | |
126 | if (rv != CRYPT_OK) croak("FATAL: hmac_done failed: %s", error_to_string(rv)); | |
127 | mac_hex[0] = '\0'; | |
128 | for(i=0; i<mac_len; i++) | |
129 | sprintf(&mac_hex[2*i], "%02x", mac[i]); | |
130 | RETVAL = newSVpvn(mac_hex, strlen(mac_hex)); | |
131 | } | |
132 | OUTPUT: | |
133 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mac::OMAC | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mac::OMAC | |
5 | _new(char * cipher_name, SV * key) | |
6 | CODE: | |
7 | { | |
8 | STRLEN k_len=0; | |
9 | unsigned char *k=NULL; | |
10 | int rv; | |
11 | int id; | |
12 | ||
13 | id = find_cipher(cipher_name); | |
14 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
15 | ||
16 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
17 | k = (unsigned char *) SvPVbyte(key, k_len); | |
18 | ||
19 | Newz(0, RETVAL, 1, struct omac_struct); | |
20 | if (!RETVAL) croak("FATAL: Newz failed"); | |
21 | ||
22 | rv = omac_init(&RETVAL->state, id, k, (unsigned long)k_len); | |
23 | if (rv != CRYPT_OK) croak("FATAL: omac_init failed: %s", error_to_string(rv)); | |
24 | } | |
25 | OUTPUT: | |
26 | RETVAL | |
27 | ||
28 | void | |
29 | DESTROY(Crypt::Mac::OMAC self) | |
30 | CODE: | |
31 | Safefree(self); | |
32 | ||
33 | Crypt::Mac::OMAC | |
34 | clone(Crypt::Mac::OMAC self) | |
35 | CODE: | |
36 | Newz(0, RETVAL, 1, struct omac_struct); | |
37 | if (!RETVAL) croak("FATAL: Newz failed"); | |
38 | Copy(&self->state, &RETVAL->state, 1, struct omac_struct); | |
39 | OUTPUT: | |
40 | RETVAL | |
41 | ||
42 | void | |
43 | _add_single(Crypt::Mac::OMAC self, SV * data) | |
44 | CODE: | |
45 | { | |
46 | int rv; | |
47 | STRLEN in_data_len; | |
48 | unsigned char *in_data; | |
49 | ||
50 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
51 | if (in_data_len>0) { | |
52 | rv = omac_process(&self->state, in_data, (unsigned long)in_data_len); | |
53 | if (rv != CRYPT_OK) croak("FATAL: omac_process failed: %s", error_to_string(rv)); | |
54 | } | |
55 | } | |
56 | ||
57 | SV * | |
58 | mac(Crypt::Mac::OMAC self) | |
59 | CODE: | |
60 | { | |
61 | char mac[MAXBLOCKSIZE]; | |
62 | unsigned long mac_len; | |
63 | int rv; | |
64 | ||
65 | mac_len = sizeof(mac); | |
66 | rv = omac_done(&self->state, (unsigned char*)mac, &mac_len); | |
67 | if (rv != CRYPT_OK) croak("FATAL: omac_done failed: %s", error_to_string(rv)); | |
68 | RETVAL = newSVpvn(mac, mac_len); | |
69 | } | |
70 | OUTPUT: | |
71 | RETVAL | |
72 | ||
73 | SV * | |
74 | b64mac(Crypt::Mac::OMAC self) | |
75 | CODE: | |
76 | { | |
77 | unsigned char mac[MAXBLOCKSIZE]; | |
78 | unsigned long mac_len; | |
79 | int rv; | |
80 | unsigned long outlen; | |
81 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
82 | ||
83 | mac_len = sizeof(mac); | |
84 | rv = omac_done(&self->state, mac, &mac_len); | |
85 | if (rv != CRYPT_OK) croak("FATAL: omac_done failed: %s", error_to_string(rv)); | |
86 | outlen = sizeof(mac_base64); | |
87 | rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
88 | if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); | |
89 | RETVAL = newSVpvn(mac_base64, outlen); | |
90 | } | |
91 | OUTPUT: | |
92 | RETVAL | |
93 | ||
94 | SV * | |
95 | b64umac(Crypt::Mac::OMAC self) | |
96 | CODE: | |
97 | { | |
98 | unsigned char mac[MAXBLOCKSIZE]; | |
99 | unsigned long mac_len; | |
100 | int rv; | |
101 | unsigned long outlen; | |
102 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
103 | ||
104 | mac_len = sizeof(mac); | |
105 | rv = omac_done(&self->state, mac, &mac_len); | |
106 | if (rv != CRYPT_OK) croak("FATAL: omac_done failed: %s", error_to_string(rv)); | |
107 | outlen = sizeof(mac_base64); | |
108 | rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
109 | if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); | |
110 | RETVAL = newSVpvn(mac_base64, outlen); | |
111 | } | |
112 | OUTPUT: | |
113 | RETVAL | |
114 | ||
115 | SV * | |
116 | hexmac(Crypt::Mac::OMAC self) | |
117 | CODE: | |
118 | { | |
119 | unsigned char mac[MAXBLOCKSIZE]; | |
120 | unsigned long mac_len, i; | |
121 | int rv; | |
122 | char mac_hex[MAXBLOCKSIZE*2 + 1]; | |
123 | ||
124 | mac_len = sizeof(mac); | |
125 | rv = omac_done(&self->state, mac, &mac_len); | |
126 | if (rv != CRYPT_OK) croak("FATAL: omac_done failed: %s", error_to_string(rv)); | |
127 | mac_hex[0] = '\0'; | |
128 | for(i=0; i<mac_len; i++) | |
129 | sprintf(&mac_hex[2*i], "%02x", mac[i]); | |
130 | RETVAL = newSVpvn(mac_hex, strlen(mac_hex)); | |
131 | } | |
132 | OUTPUT: | |
133 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mac::PMAC | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mac::PMAC | |
5 | _new(char * cipher_name, SV * key) | |
6 | CODE: | |
7 | { | |
8 | STRLEN k_len=0; | |
9 | unsigned char *k=NULL; | |
10 | int rv; | |
11 | int id; | |
12 | ||
13 | id = find_cipher(cipher_name); | |
14 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
15 | ||
16 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
17 | k = (unsigned char *) SvPVbyte(key, k_len); | |
18 | ||
19 | Newz(0, RETVAL, 1, struct pmac_struct); | |
20 | if (!RETVAL) croak("FATAL: Newz failed"); | |
21 | ||
22 | rv = pmac_init(&RETVAL->state, id, k, (unsigned long)k_len); | |
23 | if (rv != CRYPT_OK) croak("FATAL: pmac_init failed: %s", error_to_string(rv)); | |
24 | } | |
25 | OUTPUT: | |
26 | RETVAL | |
27 | ||
28 | void | |
29 | DESTROY(Crypt::Mac::PMAC self) | |
30 | CODE: | |
31 | Safefree(self); | |
32 | ||
33 | Crypt::Mac::PMAC | |
34 | clone(Crypt::Mac::PMAC self) | |
35 | CODE: | |
36 | Newz(0, RETVAL, 1, struct pmac_struct); | |
37 | if (!RETVAL) croak("FATAL: Newz failed"); | |
38 | Copy(&self->state, &RETVAL->state, 1, struct pmac_struct); | |
39 | OUTPUT: | |
40 | RETVAL | |
41 | ||
42 | void | |
43 | _add_single(Crypt::Mac::PMAC self, SV * data) | |
44 | CODE: | |
45 | { | |
46 | int rv; | |
47 | STRLEN in_data_len; | |
48 | unsigned char *in_data; | |
49 | ||
50 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
51 | if (in_data_len>0) { | |
52 | rv = pmac_process(&self->state, in_data, (unsigned long)in_data_len); | |
53 | if (rv != CRYPT_OK) croak("FATAL: pmac_process failed: %s", error_to_string(rv)); | |
54 | } | |
55 | } | |
56 | ||
57 | SV * | |
58 | mac(Crypt::Mac::PMAC self) | |
59 | CODE: | |
60 | { | |
61 | char mac[MAXBLOCKSIZE]; | |
62 | unsigned long mac_len; | |
63 | int rv; | |
64 | ||
65 | mac_len = sizeof(mac); | |
66 | rv = pmac_done(&self->state, (unsigned char*)mac, &mac_len); | |
67 | if (rv != CRYPT_OK) croak("FATAL: pmac_done failed: %s", error_to_string(rv)); | |
68 | RETVAL = newSVpvn(mac, mac_len); | |
69 | } | |
70 | OUTPUT: | |
71 | RETVAL | |
72 | ||
73 | SV * | |
74 | b64mac(Crypt::Mac::PMAC self) | |
75 | CODE: | |
76 | { | |
77 | unsigned char mac[MAXBLOCKSIZE]; | |
78 | unsigned long mac_len; | |
79 | int rv; | |
80 | unsigned long outlen; | |
81 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
82 | ||
83 | mac_len = sizeof(mac); | |
84 | rv = pmac_done(&self->state, mac, &mac_len); | |
85 | if (rv != CRYPT_OK) croak("FATAL: pmac_done failed: %s", error_to_string(rv)); | |
86 | outlen = sizeof(mac_base64); | |
87 | rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
88 | if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); | |
89 | RETVAL = newSVpvn(mac_base64, outlen); | |
90 | } | |
91 | OUTPUT: | |
92 | RETVAL | |
93 | ||
94 | SV * | |
95 | b64umac(Crypt::Mac::PMAC self) | |
96 | CODE: | |
97 | { | |
98 | unsigned char mac[MAXBLOCKSIZE]; | |
99 | unsigned long mac_len; | |
100 | int rv; | |
101 | unsigned long outlen; | |
102 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
103 | ||
104 | mac_len = sizeof(mac); | |
105 | rv = pmac_done(&self->state, mac, &mac_len); | |
106 | if (rv != CRYPT_OK) croak("FATAL: pmac_done failed: %s", error_to_string(rv)); | |
107 | outlen = sizeof(mac_base64); | |
108 | rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
109 | if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); | |
110 | RETVAL = newSVpvn(mac_base64, outlen); | |
111 | } | |
112 | OUTPUT: | |
113 | RETVAL | |
114 | ||
115 | SV * | |
116 | hexmac(Crypt::Mac::PMAC self) | |
117 | CODE: | |
118 | { | |
119 | unsigned char mac[MAXBLOCKSIZE]; | |
120 | unsigned long mac_len, i; | |
121 | int rv; | |
122 | char mac_hex[MAXBLOCKSIZE*2 + 1]; | |
123 | ||
124 | mac_len = sizeof(mac); | |
125 | rv = pmac_done(&self->state, mac, &mac_len); | |
126 | if (rv != CRYPT_OK) croak("FATAL: pmac_done failed: %s", error_to_string(rv)); | |
127 | mac_hex[0] = '\0'; | |
128 | for(i=0; i<mac_len; i++) | |
129 | sprintf(&mac_hex[2*i], "%02x", mac[i]); | |
130 | RETVAL = newSVpvn(mac_hex, strlen(mac_hex)); | |
131 | } | |
132 | OUTPUT: | |
133 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mac::Pelican | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mac::Pelican | |
5 | _new(SV * key) | |
6 | CODE: | |
7 | { | |
8 | STRLEN k_len=0; | |
9 | unsigned char *k=NULL; | |
10 | int rv; | |
11 | ||
12 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
13 | k = (unsigned char *) SvPVbyte(key, k_len); | |
14 | ||
15 | Newz(0, RETVAL, 1, struct pelican_struct); | |
16 | if (!RETVAL) croak("FATAL: Newz failed"); | |
17 | ||
18 | rv = pelican_init(&RETVAL->state, k, (unsigned long)k_len); | |
19 | if (rv != CRYPT_OK) croak("FATAL: pelican_init failed: %s", error_to_string(rv)); | |
20 | } | |
21 | OUTPUT: | |
22 | RETVAL | |
23 | ||
24 | void | |
25 | DESTROY(Crypt::Mac::Pelican self) | |
26 | CODE: | |
27 | Safefree(self); | |
28 | ||
29 | Crypt::Mac::Pelican | |
30 | clone(Crypt::Mac::Pelican self) | |
31 | CODE: | |
32 | Newz(0, RETVAL, 1, struct pelican_struct); | |
33 | if (!RETVAL) croak("FATAL: Newz failed"); | |
34 | Copy(&self->state, &RETVAL->state, 1, struct pelican_struct); | |
35 | OUTPUT: | |
36 | RETVAL | |
37 | ||
38 | void | |
39 | _add_single(Crypt::Mac::Pelican self, SV * data) | |
40 | CODE: | |
41 | { | |
42 | int rv; | |
43 | STRLEN in_data_len; | |
44 | unsigned char *in_data; | |
45 | ||
46 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
47 | if (in_data_len>0) { | |
48 | rv = pelican_process(&self->state, in_data, (unsigned long)in_data_len); | |
49 | if (rv != CRYPT_OK) croak("FATAL: pelican_process failed: %s", error_to_string(rv)); | |
50 | } | |
51 | } | |
52 | ||
53 | SV * | |
54 | mac(Crypt::Mac::Pelican self) | |
55 | CODE: | |
56 | { | |
57 | char mac[MAXBLOCKSIZE]; | |
58 | unsigned long mac_len; | |
59 | int rv; | |
60 | ||
61 | mac_len = 16; | |
62 | rv = pelican_done(&self->state, (unsigned char*)mac); | |
63 | if (rv != CRYPT_OK) croak("FATAL: pelican_done failed: %s", error_to_string(rv)); | |
64 | RETVAL = newSVpvn(mac, mac_len); | |
65 | } | |
66 | OUTPUT: | |
67 | RETVAL | |
68 | ||
69 | SV * | |
70 | b64mac(Crypt::Mac::Pelican self) | |
71 | CODE: | |
72 | { | |
73 | unsigned char mac[MAXBLOCKSIZE]; | |
74 | unsigned long mac_len; | |
75 | int rv; | |
76 | unsigned long outlen; | |
77 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
78 | ||
79 | mac_len = 16; | |
80 | rv = pelican_done(&self->state, mac); | |
81 | if (rv != CRYPT_OK) croak("FATAL: pelican_done failed: %s", error_to_string(rv)); | |
82 | outlen = sizeof(mac_base64); | |
83 | rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
84 | if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); | |
85 | RETVAL = newSVpvn(mac_base64, outlen); | |
86 | } | |
87 | OUTPUT: | |
88 | RETVAL | |
89 | ||
90 | SV * | |
91 | b64umac(Crypt::Mac::Pelican self) | |
92 | CODE: | |
93 | { | |
94 | unsigned char mac[MAXBLOCKSIZE]; | |
95 | unsigned long mac_len; | |
96 | int rv; | |
97 | unsigned long outlen; | |
98 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
99 | ||
100 | mac_len = 16; | |
101 | rv = pelican_done(&self->state, mac); | |
102 | if (rv != CRYPT_OK) croak("FATAL: pelican_done failed: %s", error_to_string(rv)); | |
103 | outlen = sizeof(mac_base64); | |
104 | rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
105 | if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); | |
106 | RETVAL = newSVpvn(mac_base64, outlen); | |
107 | } | |
108 | OUTPUT: | |
109 | RETVAL | |
110 | ||
111 | SV * | |
112 | hexmac(Crypt::Mac::Pelican self) | |
113 | CODE: | |
114 | { | |
115 | unsigned char mac[MAXBLOCKSIZE]; | |
116 | unsigned long mac_len, i; | |
117 | int rv; | |
118 | char mac_hex[MAXBLOCKSIZE*2 + 1]; | |
119 | ||
120 | mac_len = 16; | |
121 | rv = pelican_done(&self->state, mac); | |
122 | if (rv != CRYPT_OK) croak("FATAL: pelican_done failed: %s", error_to_string(rv)); | |
123 | mac_hex[0] = '\0'; | |
124 | for(i=0; i<mac_len; i++) | |
125 | sprintf(&mac_hex[2*i], "%02x", mac[i]); | |
126 | RETVAL = newSVpvn(mac_hex, strlen(mac_hex)); | |
127 | } | |
128 | OUTPUT: | |
129 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mac::XCBC | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mac::XCBC | |
5 | _new(char * cipher_name, SV * key) | |
6 | CODE: | |
7 | { | |
8 | STRLEN k_len=0; | |
9 | unsigned char *k=NULL; | |
10 | int rv; | |
11 | int id; | |
12 | ||
13 | id = find_cipher(cipher_name); | |
14 | if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
15 | ||
16 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
17 | k = (unsigned char *) SvPVbyte(key, k_len); | |
18 | ||
19 | Newz(0, RETVAL, 1, struct xcbc_struct); | |
20 | if (!RETVAL) croak("FATAL: Newz failed"); | |
21 | ||
22 | rv = xcbc_init(&RETVAL->state, id, k, (unsigned long)k_len); | |
23 | if (rv != CRYPT_OK) croak("FATAL: xcbc_init failed: %s", error_to_string(rv)); | |
24 | } | |
25 | OUTPUT: | |
26 | RETVAL | |
27 | ||
28 | void | |
29 | DESTROY(Crypt::Mac::XCBC self) | |
30 | CODE: | |
31 | Safefree(self); | |
32 | ||
33 | Crypt::Mac::XCBC | |
34 | clone(Crypt::Mac::XCBC self) | |
35 | CODE: | |
36 | Newz(0, RETVAL, 1, struct xcbc_struct); | |
37 | if (!RETVAL) croak("FATAL: Newz failed"); | |
38 | Copy(&self->state, &RETVAL->state, 1, struct xcbc_struct); | |
39 | OUTPUT: | |
40 | RETVAL | |
41 | ||
42 | void | |
43 | _add_single(Crypt::Mac::XCBC self, SV * data) | |
44 | CODE: | |
45 | { | |
46 | int rv; | |
47 | STRLEN in_data_len; | |
48 | unsigned char *in_data; | |
49 | ||
50 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
51 | if (in_data_len>0) { | |
52 | rv = xcbc_process(&self->state, in_data, (unsigned long)in_data_len); | |
53 | if (rv != CRYPT_OK) croak("FATAL: xcbc_process failed: %s", error_to_string(rv)); | |
54 | } | |
55 | } | |
56 | ||
57 | SV * | |
58 | mac(Crypt::Mac::XCBC self) | |
59 | CODE: | |
60 | { | |
61 | char mac[MAXBLOCKSIZE]; | |
62 | unsigned long mac_len; | |
63 | int rv; | |
64 | ||
65 | mac_len = sizeof(mac); | |
66 | rv = xcbc_done(&self->state, (unsigned char*)mac, &mac_len); | |
67 | if (rv != CRYPT_OK) croak("FATAL: xcbc_done failed: %s", error_to_string(rv)); | |
68 | RETVAL = newSVpvn(mac, mac_len); | |
69 | } | |
70 | OUTPUT: | |
71 | RETVAL | |
72 | ||
73 | SV * | |
74 | b64mac(Crypt::Mac::XCBC self) | |
75 | CODE: | |
76 | { | |
77 | unsigned char mac[MAXBLOCKSIZE]; | |
78 | unsigned long mac_len; | |
79 | int rv; | |
80 | unsigned long outlen; | |
81 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
82 | ||
83 | mac_len = sizeof(mac); | |
84 | rv = xcbc_done(&self->state, mac, &mac_len); | |
85 | if (rv != CRYPT_OK) croak("FATAL: xcbc_done failed: %s", error_to_string(rv)); | |
86 | outlen = sizeof(mac_base64); | |
87 | rv = base64_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
88 | if (rv != CRYPT_OK) croak("FATAL: base64_encode failed: %s", error_to_string(rv)); | |
89 | RETVAL = newSVpvn(mac_base64, outlen); | |
90 | } | |
91 | OUTPUT: | |
92 | RETVAL | |
93 | ||
94 | SV * | |
95 | b64umac(Crypt::Mac::XCBC self) | |
96 | CODE: | |
97 | { | |
98 | unsigned char mac[MAXBLOCKSIZE]; | |
99 | unsigned long mac_len; | |
100 | int rv; | |
101 | unsigned long outlen; | |
102 | char mac_base64[MAXBLOCKSIZE*2 + 1]; | |
103 | ||
104 | mac_len = sizeof(mac); | |
105 | rv = xcbc_done(&self->state, mac, &mac_len); | |
106 | if (rv != CRYPT_OK) croak("FATAL: xcbc_done failed: %s", error_to_string(rv)); | |
107 | outlen = sizeof(mac_base64); | |
108 | rv = base64url_encode(mac, mac_len, (unsigned char*)mac_base64, &outlen); | |
109 | if (rv != CRYPT_OK) croak("FATAL: base64url_encode failed: %s", error_to_string(rv)); | |
110 | RETVAL = newSVpvn(mac_base64, outlen); | |
111 | } | |
112 | OUTPUT: | |
113 | RETVAL | |
114 | ||
115 | SV * | |
116 | hexmac(Crypt::Mac::XCBC self) | |
117 | CODE: | |
118 | { | |
119 | unsigned char mac[MAXBLOCKSIZE]; | |
120 | unsigned long mac_len, i; | |
121 | int rv; | |
122 | char mac_hex[MAXBLOCKSIZE*2 + 1]; | |
123 | ||
124 | mac_len = sizeof(mac); | |
125 | rv = xcbc_done(&self->state, mac, &mac_len); | |
126 | if (rv != CRYPT_OK) croak("FATAL: xcbc_done failed: %s", error_to_string(rv)); | |
127 | mac_hex[0] = '\0'; | |
128 | for(i=0; i<mac_len; i++) | |
129 | sprintf(&mac_hex[2*i], "%02x", mac[i]); | |
130 | RETVAL = newSVpvn(mac_hex, strlen(mac_hex)); | |
131 | } | |
132 | OUTPUT: | |
133 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mode::CBC | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mode::CBC | |
5 | _new(char * cipher_name, int padding=1, int rounds=0) | |
6 | CODE: | |
7 | { | |
8 | Newz(0, RETVAL, 1, struct cbc_struct); | |
9 | if (!RETVAL) croak("FATAL: Newz failed"); | |
10 | RETVAL->padding_mode = padding; | |
11 | RETVAL->padlen = 0; | |
12 | RETVAL->direction = 0; | |
13 | RETVAL->cipher_rounds = rounds; | |
14 | RETVAL->cipher_id = find_cipher(cipher_name); | |
15 | if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
16 | } | |
17 | OUTPUT: | |
18 | RETVAL | |
19 | ||
20 | void | |
21 | DESTROY(Crypt::Mode::CBC self) | |
22 | CODE: | |
23 | Safefree(self); | |
24 | ||
25 | int | |
26 | _get_dir(Crypt::Mode::CBC self) | |
27 | CODE: | |
28 | RETVAL = self->direction; | |
29 | OUTPUT: | |
30 | RETVAL | |
31 | ||
32 | void | |
33 | _start(Crypt::Mode::CBC self, int dir, SV * key, SV * iv) | |
34 | CODE: | |
35 | { | |
36 | int rv; | |
37 | STRLEN k_len=0; | |
38 | unsigned char *k=NULL; | |
39 | STRLEN i_len=0; | |
40 | unsigned char *i=NULL; | |
41 | ||
42 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
43 | k = (unsigned char *) SvPVbyte(key, k_len); | |
44 | ||
45 | if (!SvPOK(iv)) croak("FATAL: iv must be string/buffer scalar"); | |
46 | i = (unsigned char *) SvPVbyte(iv, i_len); | |
47 | if (i_len != (STRLEN)cipher_descriptor[self->cipher_id].block_length) { | |
48 | croak ("FATAL: sizeof(iv) should be equal to blocksize (%d)", cipher_descriptor[self->cipher_id].block_length); | |
49 | } | |
50 | rv = cbc_start(self->cipher_id, i, k, (unsigned long)k_len, self->cipher_rounds, &self->state); | |
51 | if (rv != CRYPT_OK) { | |
52 | croak("FATAL: cbc_start failed: %s", error_to_string(rv)); | |
53 | } | |
54 | ||
55 | self->direction = dir; | |
56 | self->padlen = 0; | |
57 | } | |
58 | ||
59 | SV * | |
60 | _encrypt(Crypt::Mode::CBC self, SV * data) | |
61 | CODE: | |
62 | { | |
63 | int rv, has_tmp_block, blen; | |
64 | unsigned long i; | |
65 | ||
66 | STRLEN in_data_len, in_data_start; | |
67 | unsigned char *in_data, *out_data, tmp_block[MAXBLOCKSIZE]; | |
68 | ||
69 | if (self->direction != 1) croak("FATAL: encrypt error, call start_encrypt first (%d)", self->direction); | |
70 | ||
71 | blen = (&self->state)->blocklen; | |
72 | in_data_start = 0; | |
73 | has_tmp_block = 0; | |
74 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
75 | if (in_data_len==0) { | |
76 | RETVAL = newSVpvn("", 0); | |
77 | } | |
78 | else { | |
79 | if(self->padlen > 0) { | |
80 | i = (blen - self->padlen); | |
81 | if (in_data_len >= i) { /* enough data to fill pad */ | |
82 | Copy(in_data, self->pad+self->padlen, i, unsigned char); | |
83 | in_data_len -= i; | |
84 | in_data_start = i; | |
85 | rv = cbc_encrypt(self->pad, tmp_block, blen, &self->state); | |
86 | if (rv != CRYPT_OK) croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv)); | |
87 | self->padlen = 0; | |
88 | has_tmp_block = 1; | |
89 | } | |
90 | else { /* not enough data to fill pad */ | |
91 | Copy(in_data, self->pad+self->padlen, in_data_len, unsigned char); | |
92 | self->padlen += (int)in_data_len; | |
93 | in_data_len = 0; | |
94 | } | |
95 | } /* padlen > 0 */ | |
96 | ||
97 | i = (unsigned long)(in_data_len % blen); | |
98 | if (in_data_len>0 && i>0) { /* save tail of data into pad */ | |
99 | Copy(in_data+in_data_start+in_data_len-i, self->pad, i, unsigned char); | |
100 | self->padlen = i; | |
101 | in_data_len -= i; | |
102 | } | |
103 | ||
104 | if (in_data_len>0) { | |
105 | i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len); | |
106 | RETVAL = NEWSV(0, i); | |
107 | SvPOK_only(RETVAL); | |
108 | SvCUR_set(RETVAL, i); | |
109 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
110 | ||
111 | if (has_tmp_block) { | |
112 | Copy(tmp_block, out_data, blen, unsigned char); | |
113 | out_data += blen; | |
114 | } | |
115 | rv = cbc_encrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state); | |
116 | if (rv != CRYPT_OK) croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv)); | |
117 | } /* in_data_len>0 */ | |
118 | else if (has_tmp_block) { | |
119 | RETVAL = newSVpvn((char*)tmp_block, blen); | |
120 | } | |
121 | else { | |
122 | RETVAL = newSVpvn("", 0); | |
123 | } | |
124 | } | |
125 | } | |
126 | OUTPUT: | |
127 | RETVAL | |
128 | ||
129 | SV * | |
130 | _finish_enc(Crypt::Mode::CBC self) | |
131 | CODE: | |
132 | { | |
133 | unsigned char tmp_block[MAXBLOCKSIZE]; | |
134 | int rv, blen, i, j; | |
135 | ||
136 | blen = (&self->state)->blocklen; | |
137 | if (self->padlen<0 || self->padlen>=blen) croak("FATAL: invalid padlen"); | |
138 | ||
139 | if(self->padding_mode == 1) { /* pkcs5|7 padding */ | |
140 | i = blen - self->padlen; | |
141 | if (i == 0) i = blen; | |
142 | for(j=self->padlen; j<blen; j++) self->pad[j] = (unsigned char)i; | |
143 | rv = cbc_encrypt(self->pad, tmp_block, blen, &self->state); | |
144 | if (rv != CRYPT_OK) croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv)); | |
145 | } | |
146 | else if(self->padding_mode == 2) { /* oneandzeroes padding */ | |
147 | self->pad[self->padlen] = 0x80; | |
148 | for(j=self->padlen+1; j<blen; j++) self->pad[j] = 0; | |
149 | rv = cbc_encrypt(self->pad, tmp_block, blen, &self->state); | |
150 | if (rv != CRYPT_OK) croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv)); | |
151 | } | |
152 | else { | |
153 | if (self->padlen>0) croak("FATAL: cbc_encrypt, input data length not multiple of %d", blen); | |
154 | blen = 0; | |
155 | } | |
156 | ||
157 | self->direction = 0; | |
158 | RETVAL = newSVpvn((char*)tmp_block, blen); | |
159 | } | |
160 | OUTPUT: | |
161 | RETVAL | |
162 | ||
163 | SV * | |
164 | _decrypt(Crypt::Mode::CBC self, SV * data) | |
165 | CODE: | |
166 | { | |
167 | int rv, has_tmp_block, blen; | |
168 | unsigned long i; | |
169 | STRLEN in_data_len, in_data_start; | |
170 | unsigned char *in_data, *out_data, tmp_block[MAXBLOCKSIZE]; | |
171 | ||
172 | if (self->direction != -1) croak("FATAL: decrypt error, call start_decryt first (%d)", self->direction); | |
173 | ||
174 | blen = (&self->state)->blocklen; | |
175 | in_data_start = 0; | |
176 | has_tmp_block = 0; | |
177 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
178 | if (in_data_len==0) { | |
179 | RETVAL = newSVpvn("", 0); | |
180 | } | |
181 | else { | |
182 | ||
183 | if(self->padlen == blen) { | |
184 | rv = cbc_decrypt(self->pad, tmp_block, blen, &self->state); | |
185 | if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv)); | |
186 | self->padlen = 0; | |
187 | has_tmp_block = 1; | |
188 | } /* padlen == blen */ | |
189 | else if(self->padlen > 0) { | |
190 | i = (blen - self->padlen); /* remaining bytes in padding buffer */ | |
191 | if (in_data_len >= i) { /* enough data to fill pad */ | |
192 | Copy(in_data, self->pad+self->padlen, i, unsigned char); | |
193 | self->padlen += i; | |
194 | in_data_len -= i; | |
195 | in_data_start = i; | |
196 | if (in_data_len>0 || self->padding_mode == 0) { | |
197 | rv = cbc_decrypt(self->pad, tmp_block, blen, &self->state); | |
198 | if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv)); | |
199 | self->padlen = 0; | |
200 | has_tmp_block = 1; | |
201 | } | |
202 | } | |
203 | else { /* not enough data to fill pad */ | |
204 | Copy(in_data, self->pad+self->padlen, in_data_len, unsigned char); | |
205 | self->padlen += (int)in_data_len; | |
206 | in_data_len = 0; | |
207 | } | |
208 | } /* padlen > 0 */ | |
209 | ||
210 | /* here: a/ padlen==1..16 && in_data_len==0; b/ padlen==0 && in_data_len>0 */ | |
211 | if (in_data_len>0) { | |
212 | i = (unsigned long)(in_data_len % blen); | |
213 | if (i>0) { /* save tail of data into pad */ | |
214 | Copy(in_data+in_data_start+in_data_len-i, self->pad, i, unsigned char); | |
215 | self->padlen = i; | |
216 | in_data_len -= i; | |
217 | } | |
218 | } | |
219 | ||
220 | if (in_data_len>0) { | |
221 | if(self->padlen == 0 && self->padding_mode !=0) { | |
222 | /* in case of padding keep full pad if no more data */ | |
223 | Copy(in_data+in_data_start+in_data_len-blen, self->pad, blen, unsigned char); | |
224 | self->padlen = blen; | |
225 | in_data_len -= blen; | |
226 | } | |
227 | i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len); | |
228 | if (i == 0) { | |
229 | RETVAL = newSVpvn("", 0); | |
230 | } | |
231 | else { | |
232 | RETVAL = NEWSV(0, i); | |
233 | SvPOK_only(RETVAL); | |
234 | SvCUR_set(RETVAL, i); | |
235 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
236 | if (has_tmp_block) { | |
237 | Copy(tmp_block, out_data, blen, unsigned char); | |
238 | out_data += blen; | |
239 | } | |
240 | rv = cbc_decrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state); | |
241 | if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv)); | |
242 | } | |
243 | } /* in_data_len>0 */ | |
244 | else if (has_tmp_block) { | |
245 | RETVAL = newSVpvn((char*)tmp_block, blen); | |
246 | } | |
247 | else { | |
248 | RETVAL = newSVpvn("", 0); | |
249 | } | |
250 | } | |
251 | ||
252 | } | |
253 | OUTPUT: | |
254 | RETVAL | |
255 | ||
256 | SV * | |
257 | _finish_dec(Crypt::Mode::CBC self) | |
258 | CODE: | |
259 | { | |
260 | unsigned char tmp_block[MAXBLOCKSIZE]; | |
261 | unsigned char i; | |
262 | int rv, rv_len, blen; | |
263 | ||
264 | rv_len = 0; | |
265 | if (self->padlen > 0) { | |
266 | blen = (&self->state)->blocklen; | |
267 | if (self->padlen != blen) croak("FATAL: cipher text length has to be multiple of %d (%d)", blen, self->padlen); | |
268 | rv = cbc_decrypt(self->pad, tmp_block, blen, &self->state); | |
269 | if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv)); | |
270 | if(self->padding_mode == 0) { /* no padding */ | |
271 | rv_len = blen; | |
272 | } | |
273 | else if(self->padding_mode == 1) { /* pkcs5|7 padding */ | |
274 | i = tmp_block[blen-1]; | |
275 | rv_len = blen - (i>blen ? blen : i); | |
276 | } | |
277 | else if(self->padding_mode == 2) { /* oneandzeroes padding */ | |
278 | rv_len = blen; | |
279 | while ((unsigned char)tmp_block[rv_len-1] == 0x00) rv_len--; | |
280 | if ((unsigned char)tmp_block[rv_len-1] == 0x80) rv_len--; | |
281 | if (rv_len<0) rv_len = 0; | |
282 | } | |
283 | } | |
284 | ||
285 | self->direction = 0; | |
286 | RETVAL = newSVpvn((char*)tmp_block, rv_len); | |
287 | } | |
288 | OUTPUT: | |
289 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mode::CFB | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mode::CFB | |
5 | _new(char * cipher_name, int rounds=0) | |
6 | CODE: | |
7 | { | |
8 | Newz(0, RETVAL, 1, struct cfb_struct); | |
9 | if (!RETVAL) croak("FATAL: Newz failed"); | |
10 | RETVAL->direction = 0; | |
11 | RETVAL->cipher_rounds = rounds; | |
12 | RETVAL->cipher_id = find_cipher(cipher_name); | |
13 | if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
14 | } | |
15 | OUTPUT: | |
16 | RETVAL | |
17 | ||
18 | void | |
19 | DESTROY(Crypt::Mode::CFB self) | |
20 | CODE: | |
21 | Safefree(self); | |
22 | ||
23 | int | |
24 | _get_dir(Crypt::Mode::CFB self) | |
25 | CODE: | |
26 | RETVAL = self->direction; | |
27 | OUTPUT: | |
28 | RETVAL | |
29 | ||
30 | void | |
31 | _start(Crypt::Mode::CFB self, int dir, SV * key, SV * iv) | |
32 | CODE: | |
33 | { | |
34 | STRLEN k_len=0; | |
35 | unsigned char *k=NULL; | |
36 | STRLEN i_len=0; | |
37 | unsigned char *i=NULL; | |
38 | int rv; | |
39 | ||
40 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
41 | k = (unsigned char *) SvPVbyte(key, k_len); | |
42 | ||
43 | if (!SvPOK(iv)) croak("FATAL: iv must be string/buffer scalar"); | |
44 | i = (unsigned char *) SvPVbyte(iv, i_len); | |
45 | if (i_len != (STRLEN)cipher_descriptor[self->cipher_id].block_length) { | |
46 | croak ("FATAL: sizeof(iv) should be equal to blocksize (%d)", cipher_descriptor[self->cipher_id].block_length); | |
47 | } | |
48 | ||
49 | rv = cfb_start(self->cipher_id, i, k, (int)k_len, self->cipher_rounds, &self->state); | |
50 | if (rv != CRYPT_OK) { | |
51 | croak("FATAL: cfb_start failed: %s", error_to_string(rv)); | |
52 | } | |
53 | ||
54 | self->direction = dir; | |
55 | } | |
56 | ||
57 | SV * | |
58 | _crypt(Crypt::Mode::CFB self, SV * data) | |
59 | CODE: | |
60 | { | |
61 | int rv; | |
62 | STRLEN in_data_len; | |
63 | unsigned char *in_data, *out_data; | |
64 | ||
65 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
66 | if (in_data_len==0) { | |
67 | RETVAL = newSVpvn("", 0); | |
68 | } | |
69 | else { | |
70 | RETVAL = NEWSV(0, in_data_len); | |
71 | SvPOK_only(RETVAL); | |
72 | SvCUR_set(RETVAL, in_data_len); | |
73 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
74 | ||
75 | if (self->direction == 1) { | |
76 | rv = cfb_encrypt(in_data, out_data, (unsigned long)in_data_len, &self->state); | |
77 | if (rv != CRYPT_OK) croak("FATAL: cfb_encrypt failed: %s", error_to_string(rv)); | |
78 | } | |
79 | else if (self->direction == -1) { | |
80 | rv = cfb_decrypt(in_data, out_data, (unsigned long)in_data_len, &self->state); | |
81 | if (rv != CRYPT_OK) croak("FATAL: cfb_decrypt failed: %s", error_to_string(rv)); | |
82 | } | |
83 | else { | |
84 | croak("FATAL: cfb_crypt failed: call start_encrypt or start_decrypt first"); | |
85 | } | |
86 | } | |
87 | } | |
88 | OUTPUT: | |
89 | RETVAL | |
90 | ||
91 | SV * | |
92 | _finish(Crypt::Mode::CFB self) | |
93 | CODE: | |
94 | self->direction = 0; | |
95 | RETVAL = newSVpvn("", 0); | |
96 | OUTPUT: | |
97 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mode::CTR | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mode::CTR | |
5 | _new(char * cipher_name, int ctr_mode=0, int ctr_width=0, int rounds=0) | |
6 | CODE: | |
7 | { | |
8 | Newz(0, RETVAL, 1, struct ctr_struct); | |
9 | if (!RETVAL) croak("FATAL: Newz failed"); | |
10 | RETVAL->direction = 0; | |
11 | RETVAL->cipher_rounds = rounds; | |
12 | RETVAL->cipher_id = find_cipher(cipher_name); | |
13 | if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
14 | if (ctr_mode == 0) RETVAL->ctr_mode_param = CTR_COUNTER_LITTLE_ENDIAN; | |
15 | if (ctr_mode == 1) RETVAL->ctr_mode_param = CTR_COUNTER_BIG_ENDIAN; | |
16 | if (ctr_mode == 2) RETVAL->ctr_mode_param = CTR_COUNTER_LITTLE_ENDIAN|LTC_CTR_RFC3686; | |
17 | if (ctr_mode == 3) RETVAL->ctr_mode_param = CTR_COUNTER_BIG_ENDIAN|LTC_CTR_RFC3686; | |
18 | if (ctr_width > 0 && ctr_width <= cipher_descriptor[RETVAL->cipher_id].block_length) RETVAL->ctr_mode_param |= ctr_width; | |
19 | } | |
20 | OUTPUT: | |
21 | RETVAL | |
22 | ||
23 | void | |
24 | DESTROY(Crypt::Mode::CTR self) | |
25 | CODE: | |
26 | Safefree(self); | |
27 | ||
28 | int | |
29 | _get_dir(Crypt::Mode::CTR self) | |
30 | CODE: | |
31 | RETVAL = self->direction; | |
32 | OUTPUT: | |
33 | RETVAL | |
34 | ||
35 | void | |
36 | _start(Crypt::Mode::CTR self, int dir, SV * key, SV * iv) | |
37 | CODE: | |
38 | { | |
39 | STRLEN k_len=0; | |
40 | unsigned char *k=NULL; | |
41 | STRLEN i_len=0; | |
42 | unsigned char *i=NULL; | |
43 | int rv; | |
44 | ||
45 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
46 | k = (unsigned char *) SvPVbyte(key, k_len); | |
47 | ||
48 | if (!SvPOK(iv)) croak("FATAL: iv must be string/buffer scalar"); | |
49 | i = (unsigned char *) SvPVbyte(iv, i_len); | |
50 | if (i_len != (STRLEN)cipher_descriptor[self->cipher_id].block_length) { | |
51 | croak ("FATAL: sizeof(iv) should be equal to blocksize (%d)", cipher_descriptor[self->cipher_id].block_length); | |
52 | } | |
53 | ||
54 | rv = ctr_start(self->cipher_id, i, k, (int)k_len, self->cipher_rounds, self->ctr_mode_param, &self->state); | |
55 | if (rv != CRYPT_OK) { | |
56 | croak("FATAL: ctr_start failed: %s", error_to_string(rv)); | |
57 | } | |
58 | ||
59 | self->direction = dir; | |
60 | } | |
61 | ||
62 | SV * | |
63 | _crypt(Crypt::Mode::CTR self, SV * data) | |
64 | CODE: | |
65 | { | |
66 | int rv; | |
67 | STRLEN in_data_len; | |
68 | unsigned char *in_data, *out_data; | |
69 | ||
70 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
71 | if (in_data_len==0) { | |
72 | RETVAL = newSVpvn("", 0); | |
73 | } | |
74 | else { | |
75 | RETVAL = NEWSV(0, in_data_len); | |
76 | SvPOK_only(RETVAL); | |
77 | SvCUR_set(RETVAL, in_data_len); | |
78 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
79 | ||
80 | if (self->direction == 1) { | |
81 | rv = ctr_encrypt(in_data, out_data, (unsigned long)in_data_len, &self->state); | |
82 | if (rv != CRYPT_OK) croak("FATAL: ctr_encrypt failed: %s", error_to_string(rv)); | |
83 | } | |
84 | else if (self->direction == -1) { | |
85 | rv = ctr_decrypt(in_data, out_data, (unsigned long)in_data_len, &self->state); | |
86 | if (rv != CRYPT_OK) croak("FATAL: ctr_decrypt failed: %s", error_to_string(rv)); | |
87 | } | |
88 | else { | |
89 | croak("FATAL: ctr_crypt failed: call start_encrypt or start_decrypt first"); | |
90 | } | |
91 | } | |
92 | } | |
93 | OUTPUT: | |
94 | RETVAL | |
95 | ||
96 | SV * | |
97 | _finish(Crypt::Mode::CTR self) | |
98 | CODE: | |
99 | self->direction = 0; | |
100 | RETVAL = newSVpvn("", 0); | |
101 | OUTPUT: | |
102 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mode::ECB | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mode::ECB | |
5 | _new(char * cipher_name, int padding=1, int rounds=0) | |
6 | CODE: | |
7 | { | |
8 | Newz(0, RETVAL, 1, struct ecb_struct); | |
9 | if (!RETVAL) croak("FATAL: Newz failed"); | |
10 | RETVAL->padding_mode = padding; | |
11 | RETVAL->padlen = 0; | |
12 | RETVAL->direction = 0; | |
13 | RETVAL->cipher_rounds = rounds; | |
14 | RETVAL->cipher_id = find_cipher(cipher_name); | |
15 | if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
16 | } | |
17 | OUTPUT: | |
18 | RETVAL | |
19 | ||
20 | void | |
21 | DESTROY(Crypt::Mode::ECB self) | |
22 | CODE: | |
23 | Safefree(self); | |
24 | ||
25 | int | |
26 | _get_dir(Crypt::Mode::ECB self) | |
27 | CODE: | |
28 | RETVAL = self->direction; | |
29 | OUTPUT: | |
30 | RETVAL | |
31 | ||
32 | void | |
33 | _start(Crypt::Mode::ECB self, int dir, SV * key) | |
34 | CODE: | |
35 | { | |
36 | int rv; | |
37 | STRLEN k_len=0; | |
38 | unsigned char *k=NULL; | |
39 | ||
40 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
41 | k = (unsigned char *) SvPVbyte(key, k_len); | |
42 | ||
43 | rv = ecb_start(self->cipher_id, k, (unsigned long)k_len, self->cipher_rounds, &self->state); | |
44 | if (rv != CRYPT_OK) { | |
45 | croak("FATAL: ecb_start failed: %s", error_to_string(rv)); | |
46 | } | |
47 | ||
48 | self->direction = dir; | |
49 | self->padlen = 0; | |
50 | } | |
51 | ||
52 | SV * | |
53 | _encrypt(Crypt::Mode::ECB self, SV * data) | |
54 | CODE: | |
55 | { | |
56 | int rv, has_tmp_block, blen; | |
57 | unsigned long i; | |
58 | ||
59 | STRLEN in_data_len, in_data_start; | |
60 | unsigned char *in_data, *out_data, tmp_block[MAXBLOCKSIZE]; | |
61 | ||
62 | if (self->direction != 1) croak("FATAL: encrypt error, call start_encrypt first (%d)", self->direction); | |
63 | ||
64 | blen = (&self->state)->blocklen; | |
65 | in_data_start = 0; | |
66 | has_tmp_block = 0; | |
67 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
68 | if (in_data_len==0) { | |
69 | RETVAL = newSVpvn("", 0); | |
70 | } | |
71 | else { | |
72 | if(self->padlen > 0) { | |
73 | i = (blen - self->padlen); | |
74 | if (in_data_len >= i) { /* enough data to fill pad */ | |
75 | Copy(in_data, self->pad+self->padlen, i, unsigned char); | |
76 | in_data_len -= i; | |
77 | in_data_start = i; | |
78 | rv = ecb_encrypt(self->pad, tmp_block, blen, &self->state); | |
79 | if (rv != CRYPT_OK) croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv)); | |
80 | self->padlen = 0; | |
81 | has_tmp_block = 1; | |
82 | } | |
83 | else { /* not enough data to fill pad */ | |
84 | Copy(in_data, self->pad+self->padlen, in_data_len, unsigned char); | |
85 | self->padlen += (int)in_data_len; | |
86 | in_data_len = 0; | |
87 | } | |
88 | } /* padlen > 0 */ | |
89 | ||
90 | i = (unsigned long)(in_data_len % blen); | |
91 | if (in_data_len>0 && i>0) { /* save tail of data into pad */ | |
92 | Copy(in_data+in_data_start+in_data_len-i, self->pad, i, unsigned char); | |
93 | self->padlen = i; | |
94 | in_data_len -= i; | |
95 | } | |
96 | ||
97 | if (in_data_len>0) { | |
98 | i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len); | |
99 | RETVAL = NEWSV(0, i); | |
100 | SvPOK_only(RETVAL); | |
101 | SvCUR_set(RETVAL, i); | |
102 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
103 | ||
104 | if (has_tmp_block) { | |
105 | Copy(tmp_block, out_data, blen, unsigned char); | |
106 | out_data += blen; | |
107 | } | |
108 | rv = ecb_encrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state); | |
109 | if (rv != CRYPT_OK) croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv)); | |
110 | } /* in_data_len>0 */ | |
111 | else if (has_tmp_block) { | |
112 | RETVAL = newSVpvn((char*)tmp_block, blen); | |
113 | } | |
114 | else { | |
115 | RETVAL = newSVpvn("", 0); | |
116 | } | |
117 | } | |
118 | } | |
119 | OUTPUT: | |
120 | RETVAL | |
121 | ||
122 | SV * | |
123 | _finish_enc(Crypt::Mode::ECB self) | |
124 | CODE: | |
125 | { | |
126 | unsigned char tmp_block[MAXBLOCKSIZE]; | |
127 | int rv, blen, i, j; | |
128 | ||
129 | blen = (&self->state)->blocklen; | |
130 | if (self->padlen<0 || self->padlen>=blen) croak("FATAL: invalid padlen"); | |
131 | ||
132 | if(self->padding_mode == 1) { /* pkcs5|7 padding */ | |
133 | i = blen - self->padlen; | |
134 | if (i == 0) i = blen; | |
135 | for(j=self->padlen; j<blen; j++) self->pad[j] = (unsigned char)i; | |
136 | rv = ecb_encrypt(self->pad, tmp_block, blen, &self->state); | |
137 | if (rv != CRYPT_OK) croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv)); | |
138 | } | |
139 | else if(self->padding_mode == 2) { /* oneandzeroes padding */ | |
140 | self->pad[self->padlen] = 0x80; | |
141 | for(j=self->padlen+1; j<blen; j++) self->pad[j] = 0; | |
142 | rv = ecb_encrypt(self->pad, tmp_block, blen, &self->state); | |
143 | if (rv != CRYPT_OK) croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv)); | |
144 | } | |
145 | else { | |
146 | if (self->padlen>0) croak("FATAL: ecb_encrypt, input data length not multiple of %d", blen); | |
147 | blen = 0; | |
148 | } | |
149 | ||
150 | self->direction = 0; | |
151 | RETVAL = newSVpvn((char*)tmp_block, blen); | |
152 | } | |
153 | OUTPUT: | |
154 | RETVAL | |
155 | ||
156 | SV * | |
157 | _decrypt(Crypt::Mode::ECB self, SV * data) | |
158 | CODE: | |
159 | { | |
160 | int rv, has_tmp_block, blen; | |
161 | unsigned long i; | |
162 | STRLEN in_data_len, in_data_start; | |
163 | unsigned char *in_data, *out_data, tmp_block[MAXBLOCKSIZE]; | |
164 | ||
165 | if (self->direction != -1) croak("FATAL: decrypt error, call start_decryt first (%d)", self->direction); | |
166 | ||
167 | blen = (&self->state)->blocklen; | |
168 | in_data_start = 0; | |
169 | has_tmp_block = 0; | |
170 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
171 | if (in_data_len==0) { | |
172 | RETVAL = newSVpvn("", 0); | |
173 | } | |
174 | else { | |
175 | ||
176 | if(self->padlen == blen) { | |
177 | rv = ecb_decrypt(self->pad, tmp_block, blen, &self->state); | |
178 | if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv)); | |
179 | self->padlen = 0; | |
180 | has_tmp_block = 1; | |
181 | } /* padlen == blen */ | |
182 | else if(self->padlen > 0) { | |
183 | i = (blen - self->padlen); /* remaining bytes in padding buffer */ | |
184 | if (in_data_len >= i) { /* enough data to fill pad */ | |
185 | Copy(in_data, self->pad+self->padlen, i, unsigned char); | |
186 | self->padlen += i; | |
187 | in_data_len -= i; | |
188 | in_data_start = i; | |
189 | if (in_data_len>0 || self->padding_mode == 0) { | |
190 | rv = ecb_decrypt(self->pad, tmp_block, blen, &self->state); | |
191 | if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv)); | |
192 | self->padlen = 0; | |
193 | has_tmp_block = 1; | |
194 | } | |
195 | } | |
196 | else { /* not enough data to fill pad */ | |
197 | Copy(in_data, self->pad+self->padlen, in_data_len, unsigned char); | |
198 | self->padlen += (int)in_data_len; | |
199 | in_data_len = 0; | |
200 | } | |
201 | } /* padlen > 0 */ | |
202 | ||
203 | /* here: a/ padlen==1..16 && in_data_len==0; b/ padlen==0 && in_data_len>0 */ | |
204 | if (in_data_len>0) { | |
205 | i = (unsigned long)(in_data_len % blen); | |
206 | if (i>0) { /* save tail of data into pad */ | |
207 | Copy(in_data+in_data_start+in_data_len-i, self->pad, i, unsigned char); | |
208 | self->padlen = i; | |
209 | in_data_len -= i; | |
210 | } | |
211 | } | |
212 | ||
213 | if (in_data_len>0) { | |
214 | if(self->padlen == 0 && self->padding_mode !=0) { | |
215 | /* in case of padding keep full pad if no more data */ | |
216 | Copy(in_data+in_data_start+in_data_len-blen, self->pad, blen, unsigned char); | |
217 | self->padlen = blen; | |
218 | in_data_len -= blen; | |
219 | } | |
220 | i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len); | |
221 | if (i == 0) { | |
222 | RETVAL = newSVpvn("", 0); | |
223 | } | |
224 | else { | |
225 | RETVAL = NEWSV(0, i); | |
226 | SvPOK_only(RETVAL); | |
227 | SvCUR_set(RETVAL, i); | |
228 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
229 | if (has_tmp_block) { | |
230 | Copy(tmp_block, out_data, blen, unsigned char); | |
231 | out_data += blen; | |
232 | } | |
233 | rv = ecb_decrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state); | |
234 | if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv)); | |
235 | } | |
236 | } /* in_data_len>0 */ | |
237 | else if (has_tmp_block) { | |
238 | RETVAL = newSVpvn((char*)tmp_block, blen); | |
239 | } | |
240 | else { | |
241 | RETVAL = newSVpvn("", 0); | |
242 | } | |
243 | } | |
244 | ||
245 | } | |
246 | OUTPUT: | |
247 | RETVAL | |
248 | ||
249 | SV * | |
250 | _finish_dec(Crypt::Mode::ECB self) | |
251 | CODE: | |
252 | { | |
253 | unsigned char tmp_block[MAXBLOCKSIZE]; | |
254 | unsigned char i; | |
255 | int rv, rv_len, blen; | |
256 | ||
257 | rv_len = 0; | |
258 | if (self->padlen > 0) { | |
259 | blen = (&self->state)->blocklen; | |
260 | if (self->padlen != blen) croak("FATAL: cipher text length has to be multiple of %d (%d)", blen, self->padlen); | |
261 | rv = ecb_decrypt(self->pad, tmp_block, blen, &self->state); | |
262 | if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv)); | |
263 | if(self->padding_mode == 0) { /* no padding */ | |
264 | rv_len = blen; | |
265 | } | |
266 | else if(self->padding_mode == 1) { /* pkcs5|7 padding */ | |
267 | i = tmp_block[blen-1]; | |
268 | rv_len = blen - (i>blen ? blen : i); | |
269 | } | |
270 | else if(self->padding_mode == 2) { /* oneandzeroes padding */ | |
271 | rv_len = blen; | |
272 | while ((unsigned char)tmp_block[rv_len-1] == 0x00) rv_len--; | |
273 | if ((unsigned char)tmp_block[rv_len-1] == 0x80) rv_len--; | |
274 | if (rv_len<0) rv_len = 0; | |
275 | } | |
276 | } | |
277 | ||
278 | self->direction = 0; | |
279 | RETVAL = newSVpvn((char*)tmp_block, rv_len); | |
280 | } | |
281 | OUTPUT: | |
282 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::Mode::OFB | |
1 | ||
2 | ### BEWARE - GENERATED FILE, DO NOT EDIT MANUALLY! | |
3 | ||
4 | Crypt::Mode::OFB | |
5 | _new(char * cipher_name, int rounds=0) | |
6 | CODE: | |
7 | { | |
8 | Newz(0, RETVAL, 1, struct ofb_struct); | |
9 | if (!RETVAL) croak("FATAL: Newz failed"); | |
10 | RETVAL->direction = 0; | |
11 | RETVAL->cipher_rounds = rounds; | |
12 | RETVAL->cipher_id = find_cipher(cipher_name); | |
13 | if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name); | |
14 | } | |
15 | OUTPUT: | |
16 | RETVAL | |
17 | ||
18 | void | |
19 | DESTROY(Crypt::Mode::OFB self) | |
20 | CODE: | |
21 | Safefree(self); | |
22 | ||
23 | int | |
24 | _get_dir(Crypt::Mode::OFB self) | |
25 | CODE: | |
26 | RETVAL = self->direction; | |
27 | OUTPUT: | |
28 | RETVAL | |
29 | ||
30 | void | |
31 | _start(Crypt::Mode::OFB self, int dir, SV * key, SV * iv) | |
32 | CODE: | |
33 | { | |
34 | STRLEN k_len=0; | |
35 | unsigned char *k=NULL; | |
36 | STRLEN i_len=0; | |
37 | unsigned char *i=NULL; | |
38 | int rv; | |
39 | ||
40 | if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar"); | |
41 | k = (unsigned char *) SvPVbyte(key, k_len); | |
42 | ||
43 | if (!SvPOK(iv)) croak("FATAL: iv must be string/buffer scalar"); | |
44 | i = (unsigned char *) SvPVbyte(iv, i_len); | |
45 | if (i_len != (STRLEN)cipher_descriptor[self->cipher_id].block_length) { | |
46 | croak ("FATAL: sizeof(iv) should be equal to blocksize (%d)", cipher_descriptor[self->cipher_id].block_length); | |
47 | } | |
48 | ||
49 | rv = ofb_start(self->cipher_id, i, k, (int)k_len, self->cipher_rounds, &self->state); | |
50 | if (rv != CRYPT_OK) { | |
51 | croak("FATAL: ofb_start failed: %s", error_to_string(rv)); | |
52 | } | |
53 | ||
54 | self->direction = dir; | |
55 | } | |
56 | ||
57 | SV * | |
58 | _crypt(Crypt::Mode::OFB self, SV * data) | |
59 | CODE: | |
60 | { | |
61 | int rv; | |
62 | STRLEN in_data_len; | |
63 | unsigned char *in_data, *out_data; | |
64 | ||
65 | in_data = (unsigned char *)SvPVbyte(data, in_data_len); | |
66 | if (in_data_len==0) { | |
67 | RETVAL = newSVpvn("", 0); | |
68 | } | |
69 | else { | |
70 | RETVAL = NEWSV(0, in_data_len); | |
71 | SvPOK_only(RETVAL); | |
72 | SvCUR_set(RETVAL, in_data_len); | |
73 | out_data = (unsigned char *)SvPV_nolen(RETVAL); | |
74 | ||
75 | if (self->direction == 1) { | |
76 | rv = ofb_encrypt(in_data, out_data, (unsigned long)in_data_len, &self->state); | |
77 | if (rv != CRYPT_OK) croak("FATAL: ofb_encrypt failed: %s", error_to_string(rv)); | |
78 | } | |
79 | else if (self->direction == -1) { | |
80 | rv = ofb_decrypt(in_data, out_data, (unsigned long)in_data_len, &self->state); | |
81 | if (rv != CRYPT_OK) croak("FATAL: ofb_decrypt failed: %s", error_to_string(rv)); | |
82 | } | |
83 | else { | |
84 | croak("FATAL: ofb_crypt failed: call start_encrypt or start_decrypt first"); | |
85 | } | |
86 | } | |
87 | } | |
88 | OUTPUT: | |
89 | RETVAL | |
90 | ||
91 | SV * | |
92 | _finish(Crypt::Mode::OFB self) | |
93 | CODE: | |
94 | self->direction = 0; | |
95 | RETVAL = newSVpvn("", 0); | |
96 | OUTPUT: | |
97 | RETVAL |
0 | MODULE = CryptX PACKAGE = Crypt::PK::DH | |
1 | ||
2 | Crypt::PK::DH | |
3 | _new() | |
4 | CODE: | |
5 | { | |
6 | int rv; | |
7 | Newz(0, RETVAL, 1, struct dh_struct); | |
8 | if (!RETVAL) croak("FATAL: Newz failed"); | |
9 | RETVAL->key.type = -1; | |
10 | RETVAL->yarrow_prng_index = find_prng("yarrow"); | |
11 | if(RETVAL->yarrow_prng_index==-1) croak("FATAL: find_prng('yarrow') failed"); | |
12 | rv = rng_make_prng(256, RETVAL->yarrow_prng_index, &RETVAL->yarrow_prng_state, NULL); | |
13 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
14 | } | |
15 | OUTPUT: | |
16 | RETVAL | |
17 | ||
18 | void | |
19 | generate_key(Crypt::PK::DH self, int key_size=256) | |
20 | PPCODE: | |
21 | { | |
22 | int rv; | |
23 | /* add a small random entropy before generating key - not necessary as we have initialized prng with 256bit entropy in _new() */ | |
24 | rv = rng_make_prng(64, self->yarrow_prng_index, &self->yarrow_prng_state, NULL); | |
25 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
26 | /* gen the key */ | |
27 | rv = dh_make_key(&self->yarrow_prng_state, self->yarrow_prng_index, key_size, &self->key); | |
28 | if (rv != CRYPT_OK) croak("FATAL: dh_make_key failed: %s", error_to_string(rv)); | |
29 | XPUSHs(ST(0)); /* return self */ | |
30 | } | |
31 | ||
32 | void | |
33 | _import(Crypt::PK::DH self, SV * key_data) | |
34 | PPCODE: | |
35 | { | |
36 | int rv; | |
37 | unsigned char *data=NULL; | |
38 | STRLEN data_len=0; | |
39 | ||
40 | data = (unsigned char *)SvPVbyte(key_data, data_len); | |
41 | if (self->key.type != -1) { dh_free(&self->key); self->key.type = -1; } | |
42 | rv = dh_import(data, (unsigned long)data_len, &self->key); | |
43 | if (rv != CRYPT_OK) croak("FATAL: dh_import failed: %s", error_to_string(rv)); | |
44 | XPUSHs(ST(0)); /* return self */ | |
45 | } | |
46 | ||
47 | int | |
48 | is_private(Crypt::PK::DH self) | |
49 | CODE: | |
50 | if (self->key.type == -1) XSRETURN_UNDEF; | |
51 | RETVAL = (self->key.type == PK_PRIVATE) ? 1 : 0; | |
52 | OUTPUT: | |
53 | RETVAL | |
54 | ||
55 | int | |
56 | size(Crypt::PK::DH self) | |
57 | CODE: | |
58 | if (self->key.type == -1) XSRETURN_UNDEF; | |
59 | RETVAL = dh_get_size(&self->key); | |
60 | OUTPUT: | |
61 | RETVAL | |
62 | ||
63 | SV* | |
64 | key2hash(Crypt::PK::DH self) | |
65 | PREINIT: | |
66 | HV *rv_hash; | |
67 | long siz; | |
68 | char buf[20001]; | |
69 | SV **not_used; | |
70 | CODE: | |
71 | if (self->key.type == -1) XSRETURN_UNDEF; | |
72 | rv_hash = newHV(); | |
73 | /* =====> x */ | |
74 | siz = (self->key.x) ? mp_unsigned_bin_size(self->key.x) : 0; | |
75 | if (siz>10000) { | |
76 | croak("FATAL: key2hash failed - 'x' too big number"); | |
77 | } | |
78 | if (siz>0) { | |
79 | mp_tohex(self->key.x, buf); | |
80 | not_used = hv_store(rv_hash, "x", 1, newSVpv(buf, strlen(buf)), 0); | |
81 | } | |
82 | else{ | |
83 | not_used = hv_store(rv_hash, "x", 1, newSVpv("", 0), 0); | |
84 | } | |
85 | /* =====> y */ | |
86 | siz = (self->key.y) ? mp_unsigned_bin_size(self->key.y) : 0; | |
87 | if (siz>10000) { | |
88 | croak("FATAL: key2hash failed - 'y' too big number"); | |
89 | } | |
90 | if (siz>0) { | |
91 | mp_tohex(self->key.y, buf); | |
92 | not_used = hv_store(rv_hash, "y", 1, newSVpv(buf, strlen(buf)), 0); | |
93 | } | |
94 | else{ | |
95 | not_used = hv_store(rv_hash, "y", 1, newSVpv("", 0), 0); | |
96 | } | |
97 | /* =====> name */ | |
98 | snprintf(buf, sizeof(buf), "DH-%d", dh_get_size(&self->key)*8); | |
99 | not_used = hv_store(rv_hash, "name", 4, newSVpv(buf, strlen(buf)), 0); | |
100 | /* =====> size */ | |
101 | not_used = hv_store(rv_hash, "size", 4, newSViv(dh_get_size(&self->key)), 0); | |
102 | /* =====> type */ | |
103 | not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0); | |
104 | if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */ | |
105 | RETVAL = newRV_noinc((SV*)rv_hash); | |
106 | OUTPUT: | |
107 | RETVAL | |
108 | ||
109 | SV * | |
110 | export_key(Crypt::PK::DH self, char * type) | |
111 | CODE: | |
112 | { | |
113 | int rv; | |
114 | unsigned char out[4096]; | |
115 | unsigned long int out_len = 4096; | |
116 | ||
117 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
118 | if (strnEQ(type, "private", 7)) { | |
119 | rv = dh_export(out, &out_len, PK_PRIVATE, &self->key); | |
120 | if (rv != CRYPT_OK) croak("FATAL: dh_export(PK_PRIVATE) failed: %s", error_to_string(rv)); | |
121 | RETVAL = newSVpvn((char*)out, out_len); | |
122 | } | |
123 | else if (strnEQ(type, "public", 6)) { | |
124 | rv = dh_export(out, &out_len, PK_PUBLIC, &self->key); | |
125 | if (rv != CRYPT_OK) croak("FATAL: dh_export(PK_PUBLIC) failed: %s", error_to_string(rv)); | |
126 | RETVAL = newSVpvn((char*)out, out_len); | |
127 | } | |
128 | else { | |
129 | croak("FATAL: export_key_der invalid type '%s'", type); | |
130 | } | |
131 | } | |
132 | OUTPUT: | |
133 | RETVAL | |
134 | ||
135 | SV * | |
136 | _encrypt(Crypt::PK::DH self, SV * data, char * hash_name) | |
137 | CODE: | |
138 | { | |
139 | int rv, hash_id; | |
140 | unsigned char *data_ptr=NULL; | |
141 | STRLEN data_len=0; | |
142 | unsigned char buffer[1024]; | |
143 | unsigned long buffer_len = 1024; | |
144 | ||
145 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
146 | ||
147 | hash_id = find_hash(hash_name); | |
148 | if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
149 | rv = dh_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, | |
150 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
151 | hash_id, &self->key); | |
152 | if (rv != CRYPT_OK) croak("FATAL: dh_encrypt_key failed: %s", error_to_string(rv)); | |
153 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
154 | } | |
155 | OUTPUT: | |
156 | RETVAL | |
157 | ||
158 | SV * | |
159 | _decrypt(Crypt::PK::DH self, SV * data) | |
160 | CODE: | |
161 | { | |
162 | int rv; | |
163 | unsigned char *data_ptr=NULL; | |
164 | STRLEN data_len=0; | |
165 | unsigned char buffer[1024]; | |
166 | unsigned long buffer_len = 1024; | |
167 | ||
168 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
169 | ||
170 | rv = dh_decrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &self->key); | |
171 | if (rv != CRYPT_OK) croak("FATAL: dh_decrypt_key_ex failed: %s", error_to_string(rv)); | |
172 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
173 | } | |
174 | OUTPUT: | |
175 | RETVAL | |
176 | ||
177 | SV * | |
178 | _sign(Crypt::PK::DH self, SV * data) | |
179 | CODE: | |
180 | { | |
181 | int rv; | |
182 | unsigned char *data_ptr=NULL; | |
183 | STRLEN data_len=0; | |
184 | unsigned char buffer[1024]; | |
185 | unsigned long buffer_len = 1024; | |
186 | ||
187 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
188 | ||
189 | rv = dh_sign_hash(data_ptr, (unsigned long)data_len, buffer, &buffer_len, | |
190 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
191 | &self->key); | |
192 | if (rv != CRYPT_OK) croak("FATAL: dh_sign_hash_ex failed: %s", error_to_string(rv)); | |
193 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
194 | } | |
195 | OUTPUT: | |
196 | RETVAL | |
197 | ||
198 | int | |
199 | _verify(Crypt::PK::DH self, SV * sig, SV * data) | |
200 | CODE: | |
201 | { | |
202 | int rv, stat; | |
203 | unsigned char *data_ptr=NULL; | |
204 | STRLEN data_len=0; | |
205 | unsigned char *sig_ptr=NULL; | |
206 | STRLEN sig_len=0; | |
207 | ||
208 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
209 | sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len); | |
210 | ||
211 | RETVAL = 1; | |
212 | rv = dh_verify_hash(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key); | |
213 | if (rv != CRYPT_OK || stat != 1) RETVAL = 0; | |
214 | } | |
215 | OUTPUT: | |
216 | RETVAL | |
217 | ||
218 | SV * | |
219 | shared_secret(Crypt::PK::DH self, Crypt::PK::DH pubkey) | |
220 | CODE: | |
221 | { | |
222 | int rv; | |
223 | unsigned char buffer[1024]; | |
224 | unsigned long buffer_len = 1024; | |
225 | ||
226 | rv = dh_shared_secret(&self->key, &pubkey->key, buffer, &buffer_len); | |
227 | if (rv != CRYPT_OK) croak("FATAL: dh_shared_secret failed: %s", error_to_string(rv)); | |
228 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
229 | } | |
230 | OUTPUT: | |
231 | RETVAL | |
232 | ||
233 | void | |
234 | DESTROY(Crypt::PK::DH self) | |
235 | CODE: | |
236 | if (self->key.type != -1) { dh_free(&self->key); self->key.type = -1; } | |
237 | Safefree(self); | |
238 |
0 | MODULE = CryptX PACKAGE = Crypt::PK::DSA | |
1 | ||
2 | Crypt::PK::DSA | |
3 | _new() | |
4 | CODE: | |
5 | { | |
6 | int rv; | |
7 | Newz(0, RETVAL, 1, struct dsa_struct); | |
8 | if (!RETVAL) croak("FATAL: Newz failed"); | |
9 | RETVAL->key.type = -1; | |
10 | RETVAL->yarrow_prng_index = find_prng("yarrow"); | |
11 | if(RETVAL->yarrow_prng_index==-1) croak("FATAL: find_prng('yarrow') failed"); | |
12 | rv = rng_make_prng(256, RETVAL->yarrow_prng_index, &RETVAL->yarrow_prng_state, NULL); | |
13 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
14 | } | |
15 | OUTPUT: | |
16 | RETVAL | |
17 | ||
18 | void | |
19 | generate_key(Crypt::PK::DSA self, int group_size=30, int modulus_size=256) | |
20 | PPCODE: | |
21 | { | |
22 | int rv; | |
23 | /* add a small random entropy before generating key - not necessary as we have initialized prng with 256bit entropy in _new() */ | |
24 | rv = rng_make_prng(64, self->yarrow_prng_index, &self->yarrow_prng_state, NULL); | |
25 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
26 | /* gen the key */ | |
27 | rv = dsa_make_key(&self->yarrow_prng_state, self->yarrow_prng_index, group_size, modulus_size, &self->key); | |
28 | if (rv != CRYPT_OK) croak("FATAL: dsa_make_key failed: %s", error_to_string(rv)); | |
29 | XPUSHs(ST(0)); /* return self */ | |
30 | } | |
31 | ||
32 | void | |
33 | _import(Crypt::PK::DSA self, SV * key_data) | |
34 | PPCODE: | |
35 | { | |
36 | int rv; | |
37 | unsigned char *data=NULL; | |
38 | STRLEN data_len=0; | |
39 | ||
40 | data = (unsigned char *)SvPVbyte(key_data, data_len); | |
41 | if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; } | |
42 | rv = dsa_import(data, (unsigned long)data_len, &self->key); | |
43 | if (rv != CRYPT_OK) croak("FATAL: dsa_import failed: %s", error_to_string(rv)); | |
44 | XPUSHs(ST(0)); /* return self */ | |
45 | } | |
46 | ||
47 | void | |
48 | _import_hex(Crypt::PK::DSA self, char *p, char *q, char *g, char *x, char *y) | |
49 | PPCODE: | |
50 | { | |
51 | int rv; | |
52 | if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; } | |
53 | rv = dsa_import_hex(p, q, g, x, y, &self->key); | |
54 | if (rv != CRYPT_OK) croak("FATAL: dsa_import_hex failed: %s", error_to_string(rv)); | |
55 | XPUSHs(ST(0)); /* return self */ | |
56 | } | |
57 | ||
58 | int | |
59 | is_private(Crypt::PK::DSA self) | |
60 | CODE: | |
61 | if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF; | |
62 | RETVAL = (self->key.type == PK_PRIVATE) ? 1 : 0; | |
63 | OUTPUT: | |
64 | RETVAL | |
65 | ||
66 | int | |
67 | size(Crypt::PK::DSA self) | |
68 | CODE: | |
69 | if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF; | |
70 | RETVAL = mp_unsigned_bin_size(self->key.g); | |
71 | OUTPUT: | |
72 | RETVAL | |
73 | ||
74 | int | |
75 | size_q(Crypt::PK::DSA self) | |
76 | CODE: | |
77 | if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF; | |
78 | RETVAL = self->key.qord; | |
79 | OUTPUT: | |
80 | RETVAL | |
81 | ||
82 | SV* | |
83 | key2hash(Crypt::PK::DSA self) | |
84 | PREINIT: | |
85 | HV *rv_hash; | |
86 | long siz; | |
87 | char buf[20001]; | |
88 | SV **not_used; | |
89 | CODE: | |
90 | if (self->key.type == -1 || self->key.qord <= 0) XSRETURN_UNDEF; | |
91 | rv_hash = newHV(); | |
92 | /* =====> g */ | |
93 | siz = (self->key.g) ? mp_unsigned_bin_size(self->key.g) : 0; | |
94 | if (siz>10000) { | |
95 | croak("FATAL: key2hash failed - 'g' too big number"); | |
96 | } | |
97 | if (siz>0) { | |
98 | mp_tohex(self->key.g, buf); | |
99 | not_used = hv_store(rv_hash, "g", 1, newSVpv(buf, strlen(buf)), 0); | |
100 | } | |
101 | else{ | |
102 | not_used = hv_store(rv_hash, "g", 1, newSVpv("", 0), 0); | |
103 | } | |
104 | /* =====> q */ | |
105 | siz = (self->key.q) ? mp_unsigned_bin_size(self->key.q) : 0; | |
106 | if (siz>10000) { | |
107 | croak("FATAL: key2hash failed - 'q' too big number"); | |
108 | } | |
109 | if (siz>0) { | |
110 | mp_tohex(self->key.q, buf); | |
111 | not_used = hv_store(rv_hash, "q", 1, newSVpv(buf, strlen(buf)), 0); | |
112 | } | |
113 | else{ | |
114 | not_used = hv_store(rv_hash, "q", 1, newSVpv("", 0), 0); | |
115 | } | |
116 | /* =====> p */ | |
117 | siz = (self->key.p) ? mp_unsigned_bin_size(self->key.p) : 0; | |
118 | if (siz>10000) { | |
119 | croak("FATAL: key2hash failed - 'p' too big number"); | |
120 | } | |
121 | if (siz>0) { | |
122 | mp_tohex(self->key.p, buf); | |
123 | not_used = hv_store(rv_hash, "p", 1, newSVpv(buf, strlen(buf)), 0); | |
124 | } | |
125 | else{ | |
126 | not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0); | |
127 | } | |
128 | /* =====> x */ | |
129 | siz = (self->key.x) ? mp_unsigned_bin_size(self->key.x) : 0; | |
130 | if (siz>10000) { | |
131 | croak("FATAL: key2hash failed - 'x' too big number"); | |
132 | } | |
133 | if (siz>0) { | |
134 | mp_tohex(self->key.x, buf); | |
135 | not_used = hv_store(rv_hash, "x", 1, newSVpv(buf, strlen(buf)), 0); | |
136 | } | |
137 | else{ | |
138 | not_used = hv_store(rv_hash, "x", 1, newSVpv("", 0), 0); | |
139 | } | |
140 | /* =====> y */ | |
141 | siz = (self->key.y) ? mp_unsigned_bin_size(self->key.y) : 0; | |
142 | if (siz>10000) { | |
143 | croak("FATAL: key2hash failed - 'y' too big number"); | |
144 | } | |
145 | if (siz>0) { | |
146 | mp_tohex(self->key.y, buf); | |
147 | not_used = hv_store(rv_hash, "y", 1, newSVpv(buf, strlen(buf)), 0); | |
148 | } | |
149 | else{ | |
150 | not_used = hv_store(rv_hash, "y", 1, newSVpv("", 0), 0); | |
151 | } | |
152 | /* =====> size */ | |
153 | not_used = hv_store(rv_hash, "size", 4, newSViv(mp_unsigned_bin_size(self->key.q)), 0); | |
154 | /* =====> type */ | |
155 | not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0); | |
156 | if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */ | |
157 | RETVAL = newRV_noinc((SV*)rv_hash); | |
158 | OUTPUT: | |
159 | RETVAL | |
160 | ||
161 | SV * | |
162 | export_key_der(Crypt::PK::DSA self, char * type) | |
163 | CODE: | |
164 | { | |
165 | int rv; | |
166 | unsigned char out[4096]; | |
167 | unsigned long int out_len = 4096; | |
168 | ||
169 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
170 | if (strnEQ(type, "private", 7)) { | |
171 | rv = dsa_export(out, &out_len, PK_PRIVATE, &self->key); | |
172 | if (rv != CRYPT_OK) croak("FATAL: dsa_export(PK_PRIVATE) failed: %s", error_to_string(rv)); | |
173 | RETVAL = newSVpvn((char*)out, out_len); | |
174 | } | |
175 | else if (strnEQ(type, "public", 6)) { | |
176 | rv = dsa_export(out, &out_len, PK_PUBLIC, &self->key); | |
177 | if (rv != CRYPT_OK) croak("FATAL: dsa_export(PK_PUBLIC) failed: %s", error_to_string(rv)); | |
178 | RETVAL = newSVpvn((char*)out, out_len); | |
179 | } | |
180 | else { | |
181 | croak("FATAL: export_key_der invalid type '%s'", type); | |
182 | } | |
183 | } | |
184 | OUTPUT: | |
185 | RETVAL | |
186 | ||
187 | SV * | |
188 | _encrypt(Crypt::PK::DSA self, SV * data, char * hash_name) | |
189 | CODE: | |
190 | { | |
191 | int rv, hash_id; | |
192 | unsigned char *data_ptr=NULL; | |
193 | STRLEN data_len=0; | |
194 | unsigned char buffer[1024]; | |
195 | unsigned long buffer_len = 1024; | |
196 | ||
197 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
198 | ||
199 | hash_id = find_hash(hash_name); | |
200 | if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
201 | rv = dsa_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, | |
202 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
203 | hash_id, &self->key); | |
204 | if (rv != CRYPT_OK) croak("FATAL: dsa_encrypt_key failed: %s", error_to_string(rv)); | |
205 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
206 | } | |
207 | OUTPUT: | |
208 | RETVAL | |
209 | ||
210 | SV * | |
211 | _decrypt(Crypt::PK::DSA self, SV * data) | |
212 | CODE: | |
213 | { | |
214 | int rv; | |
215 | unsigned char *data_ptr=NULL; | |
216 | STRLEN data_len=0; | |
217 | unsigned char buffer[1024]; | |
218 | unsigned long buffer_len = 1024; | |
219 | ||
220 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
221 | ||
222 | rv = dsa_decrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &self->key); | |
223 | if (rv != CRYPT_OK) croak("FATAL: dsa_decrypt_key_ex failed: %s", error_to_string(rv)); | |
224 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
225 | } | |
226 | OUTPUT: | |
227 | RETVAL | |
228 | ||
229 | SV * | |
230 | _sign(Crypt::PK::DSA self, SV * data) | |
231 | CODE: | |
232 | { | |
233 | int rv; | |
234 | unsigned char *data_ptr=NULL; | |
235 | STRLEN data_len=0; | |
236 | unsigned char buffer[1024]; | |
237 | unsigned long buffer_len = 1024; | |
238 | ||
239 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
240 | ||
241 | rv = dsa_sign_hash(data_ptr, (unsigned long)data_len, buffer, &buffer_len, | |
242 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
243 | &self->key); | |
244 | if (rv != CRYPT_OK) croak("FATAL: dsa_sign_hash_ex failed: %s", error_to_string(rv)); | |
245 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
246 | } | |
247 | OUTPUT: | |
248 | RETVAL | |
249 | ||
250 | int | |
251 | _verify(Crypt::PK::DSA self, SV * sig, SV * data) | |
252 | CODE: | |
253 | { | |
254 | int rv, stat; | |
255 | unsigned char *data_ptr=NULL; | |
256 | STRLEN data_len=0; | |
257 | unsigned char *sig_ptr=NULL; | |
258 | STRLEN sig_len=0; | |
259 | ||
260 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
261 | sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len); | |
262 | ||
263 | RETVAL = 1; | |
264 | rv = dsa_verify_hash(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key); | |
265 | if (rv != CRYPT_OK || stat != 1) RETVAL = 0; | |
266 | } | |
267 | OUTPUT: | |
268 | RETVAL | |
269 | ||
270 | void | |
271 | DESTROY(Crypt::PK::DSA self) | |
272 | CODE: | |
273 | if (self->key.type != -1) { dsa_free(&self->key); self->key.type = -1; } | |
274 | Safefree(self); | |
275 |
0 | MODULE = CryptX PACKAGE = Crypt::PK::ECC | |
1 | ||
2 | Crypt::PK::ECC | |
3 | _new() | |
4 | CODE: | |
5 | { | |
6 | int rv; | |
7 | Newz(0, RETVAL, 1, struct ecc_struct); | |
8 | if (!RETVAL) croak("FATAL: Newz failed"); | |
9 | RETVAL->yarrow_prng_index = find_prng("yarrow"); | |
10 | RETVAL->key.type = -1; | |
11 | ecc_dp_init(&RETVAL->dp); | |
12 | if(RETVAL->yarrow_prng_index==-1) croak("FATAL: find_prng('yarrow') failed"); | |
13 | rv = rng_make_prng(256, RETVAL->yarrow_prng_index, &RETVAL->yarrow_prng_state, NULL); | |
14 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
15 | } | |
16 | OUTPUT: | |
17 | RETVAL | |
18 | ||
19 | void | |
20 | generate_key(Crypt::PK::ECC self, SV *curve) | |
21 | PPCODE: | |
22 | { | |
23 | int rv; | |
24 | /* add a small random entropy before generating key - not necessary as we have initialized prng with 256bit entropy in _new() */ | |
25 | rv = rng_make_prng(64, self->yarrow_prng_index, &self->yarrow_prng_state, NULL); | |
26 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
27 | /* setup dp structure */ | |
28 | _ecc_set_dp_from_SV(&self->dp, curve); /* croaks on error */ | |
29 | /* gen the key */ | |
30 | rv = ecc_make_key_ex(&self->yarrow_prng_state, self->yarrow_prng_index, &self->key, &self->dp); | |
31 | if (rv != CRYPT_OK) croak("FATAL: ecc_make_key_ex failed: %s", error_to_string(rv)); | |
32 | XPUSHs(ST(0)); /* return self */ | |
33 | } | |
34 | ||
35 | void | |
36 | _import(Crypt::PK::ECC self, SV * key_data) | |
37 | PPCODE: | |
38 | { | |
39 | int rv; | |
40 | unsigned char *data=NULL; | |
41 | STRLEN data_len=0; | |
42 | ||
43 | data = (unsigned char *)SvPVbyte(key_data, data_len); | |
44 | _ecc_free_key(&self->key, &self->dp); | |
45 | rv = ecc_import_full(data, (unsigned long)data_len, &self->key, &self->dp); | |
46 | if (rv != CRYPT_OK) croak("FATAL: ecc_import_full failed: %s", error_to_string(rv)); | |
47 | XPUSHs(ST(0)); /* return self */ | |
48 | } | |
49 | ||
50 | void | |
51 | _import_pkcs8(Crypt::PK::ECC self, SV * key_data) | |
52 | PPCODE: | |
53 | { | |
54 | int rv; | |
55 | unsigned char *data=NULL; | |
56 | STRLEN data_len=0; | |
57 | ||
58 | data = (unsigned char *)SvPVbyte(key_data, data_len); | |
59 | _ecc_free_key(&self->key, &self->dp); | |
60 | rv = ecc_import_pkcs8(data, (unsigned long)data_len, &self->key, &self->dp); | |
61 | if (rv != CRYPT_OK) croak("FATAL: ecc_import_pkcs8 failed: %s", error_to_string(rv)); | |
62 | XPUSHs(ST(0)); /* return self */ | |
63 | } | |
64 | ||
65 | void | |
66 | import_key_raw(Crypt::PK::ECC self, SV * key_data, SV * curve) | |
67 | PPCODE: | |
68 | { | |
69 | int rv; | |
70 | unsigned char *data=NULL; | |
71 | STRLEN data_len=0; | |
72 | ||
73 | data = (unsigned char *)SvPVbyte(key_data, data_len); | |
74 | _ecc_free_key(&self->key, &self->dp); | |
75 | ||
76 | _ecc_set_dp_from_SV(&self->dp, curve); /* croaks on error */ | |
77 | ||
78 | rv = ecc_import_raw(data, (unsigned long)data_len, &self->key, &self->dp); | |
79 | if (rv != CRYPT_OK) croak("FATAL: ecc_import_raw failed: %s", error_to_string(rv)); | |
80 | XPUSHs(ST(0)); /* return self */ | |
81 | } | |
82 | ||
83 | int | |
84 | is_private(Crypt::PK::ECC self) | |
85 | CODE: | |
86 | if (self->key.type == -1) XSRETURN_UNDEF; | |
87 | RETVAL = (self->key.type == PK_PRIVATE) ? 1 : 0; | |
88 | OUTPUT: | |
89 | RETVAL | |
90 | ||
91 | int | |
92 | size(Crypt::PK::ECC self) | |
93 | CODE: | |
94 | if (self->key.type == -1) XSRETURN_UNDEF; | |
95 | RETVAL = ecc_get_size(&self->key); | |
96 | OUTPUT: | |
97 | RETVAL | |
98 | ||
99 | SV* | |
100 | key2hash(Crypt::PK::ECC self) | |
101 | PREINIT: | |
102 | HV *rv_hash; | |
103 | long siz; | |
104 | char buf[20001]; | |
105 | SV **not_used; | |
106 | CODE: | |
107 | if (self->key.type == -1) XSRETURN_UNDEF; | |
108 | rv_hash = newHV(); | |
109 | /* =====> k */ | |
110 | siz = (self->key.k) ? mp_unsigned_bin_size(self->key.k) : 0; | |
111 | if (siz>10000) { | |
112 | croak("FATAL: key2hash failed - 'k' too big number"); | |
113 | } | |
114 | if (siz>0) { | |
115 | mp_tohex(self->key.k, buf); | |
116 | not_used = hv_store(rv_hash, "k", 1, newSVpv(buf, strlen(buf)), 0); | |
117 | } | |
118 | else{ | |
119 | not_used = hv_store(rv_hash, "k", 1, newSVpv("", 0), 0); | |
120 | } | |
121 | /* =====> pub_x */ | |
122 | siz = (self->key.pubkey.x) ? mp_unsigned_bin_size(self->key.pubkey.x) : 0; | |
123 | if (siz>10000) { | |
124 | croak("FATAL: key2hash failed - 'pub_x' too big number"); | |
125 | } | |
126 | if (siz>0) { | |
127 | mp_tohex(self->key.pubkey.x, buf); | |
128 | not_used = hv_store(rv_hash, "pub_x", 5, newSVpv(buf, strlen(buf)), 0); | |
129 | } | |
130 | else{ | |
131 | not_used = hv_store(rv_hash, "pub_x", 5, newSVpv("", 0), 0); | |
132 | } | |
133 | /* =====> pub_y */ | |
134 | siz = (self->key.pubkey.y) ? mp_unsigned_bin_size(self->key.pubkey.y) : 0; | |
135 | if (siz>10000) { | |
136 | croak("FATAL: key2hash failed - 'pub_y' too big number"); | |
137 | } | |
138 | if (siz>0) { | |
139 | mp_tohex(self->key.pubkey.y, buf); | |
140 | not_used = hv_store(rv_hash, "pub_y", 5, newSVpv(buf, strlen(buf)), 0); | |
141 | } | |
142 | else{ | |
143 | not_used = hv_store(rv_hash, "pub_y", 5, newSVpv("", 0), 0); | |
144 | } | |
145 | /* =====> curve_... */ | |
146 | if (self->key.dp) { | |
147 | not_used = hv_store(rv_hash, "curve_name", 10, newSVpv(self->key.dp->name, strlen(self->key.dp->name)), 0); | |
148 | not_used = hv_store(rv_hash, "curve_prime", 11, newSVpv(self->key.dp->prime, strlen(self->key.dp->prime)), 0); | |
149 | not_used = hv_store(rv_hash, "curve_A", 7, newSVpv(self->key.dp->A, strlen(self->key.dp->A)), 0); | |
150 | not_used = hv_store(rv_hash, "curve_B", 7, newSVpv(self->key.dp->B, strlen(self->key.dp->B)), 0); | |
151 | not_used = hv_store(rv_hash, "curve_order", 11, newSVpv(self->key.dp->order, strlen(self->key.dp->order)), 0); | |
152 | not_used = hv_store(rv_hash, "curve_Gx", 8, newSVpv(self->key.dp->Gx, strlen(self->key.dp->Gx)), 0); | |
153 | not_used = hv_store(rv_hash, "curve_Gy", 8, newSVpv(self->key.dp->Gy, strlen(self->key.dp->Gy)), 0); | |
154 | not_used = hv_store(rv_hash, "curve_cofactor", 14, newSViv(self->key.dp->cofactor), 0); | |
155 | { | |
156 | mp_int p_num; | |
157 | mp_init(&p_num); | |
158 | mp_read_radix(&p_num, self->key.dp->prime, 16); | |
159 | not_used = hv_store(rv_hash, "curve_bytes", 11, newSViv(mp_unsigned_bin_size(&p_num)), 0); | |
160 | not_used = hv_store(rv_hash, "curve_bits", 10, newSViv(mp_count_bits(&p_num)), 0); | |
161 | mp_clear(&p_num); | |
162 | } | |
163 | } | |
164 | /* =====> size */ | |
165 | not_used = hv_store(rv_hash, "size", 4, newSViv(ecc_get_size(&self->key)), 0); | |
166 | /* =====> type */ | |
167 | not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0); | |
168 | if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */ | |
169 | RETVAL = newRV_noinc((SV*)rv_hash); | |
170 | OUTPUT: | |
171 | RETVAL | |
172 | ||
173 | SV * | |
174 | export_key_der(Crypt::PK::ECC self, char * type) | |
175 | CODE: | |
176 | { | |
177 | int rv; | |
178 | unsigned char out[4096]; | |
179 | unsigned long int out_len = 4096; | |
180 | ||
181 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
182 | if (strnEQ(type, "private", 7)) { | |
183 | rv = ecc_export_full(out, &out_len, PK_PRIVATE, &self->key); | |
184 | if (rv != CRYPT_OK) croak("FATAL: ecc_export(PK_PRIVATE) failed: %s", error_to_string(rv)); | |
185 | RETVAL = newSVpvn((char*)out, out_len); | |
186 | } | |
187 | else if (strnEQ(type, "public", 6)) { | |
188 | rv = ecc_export_full(out, &out_len, PK_PUBLIC, &self->key); | |
189 | if (rv != CRYPT_OK) croak("FATAL: ecc_export(PK_PUBLIC) failed: %s", error_to_string(rv)); | |
190 | RETVAL = newSVpvn((char*)out, out_len); | |
191 | } | |
192 | else { | |
193 | croak("FATAL: export_key_der invalid type '%s'", type); | |
194 | } | |
195 | } | |
196 | OUTPUT: | |
197 | RETVAL | |
198 | ||
199 | SV * | |
200 | export_key_raw(Crypt::PK::ECC self, char * type) | |
201 | CODE: | |
202 | { | |
203 | int rv; | |
204 | unsigned char out[4096]; | |
205 | unsigned long int out_len = sizeof(out); | |
206 | ||
207 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
208 | if (strnEQ(type, "private", 7)) { | |
209 | rv = ecc_export_raw(out, &out_len, PK_PRIVATE, &self->key); | |
210 | if (rv != CRYPT_OK) croak("FATAL: ecc_export_raw(private) failed: %s", error_to_string(rv)); | |
211 | RETVAL = newSVpvn((char*)out, out_len); | |
212 | } | |
213 | else if (strnEQ(type, "public_compressed", 17)) { | |
214 | rv = ecc_export_raw(out, &out_len, PK_PUBLIC_COMPRESSED, &self->key); | |
215 | if (rv != CRYPT_OK) croak("FATAL: ecc_export_raw(public_compressed) failed: %s", error_to_string(rv)); | |
216 | RETVAL = newSVpvn((char*)out, out_len); | |
217 | } | |
218 | else if (strnEQ(type, "public", 6)) { | |
219 | rv = ecc_export_raw(out, &out_len, PK_PUBLIC, &self->key); | |
220 | if (rv != CRYPT_OK) croak("FATAL: ecc_export_raw(public) failed: %s", error_to_string(rv)); | |
221 | RETVAL = newSVpvn((char*)out, out_len); | |
222 | } | |
223 | else { | |
224 | croak("FATAL: export_key_raw invalid type '%s'", type); | |
225 | } | |
226 | } | |
227 | OUTPUT: | |
228 | RETVAL | |
229 | ||
230 | SV * | |
231 | _encrypt(Crypt::PK::ECC self, SV * data, char * hash_name) | |
232 | CODE: | |
233 | { | |
234 | int rv, hash_id; | |
235 | unsigned char *data_ptr=NULL; | |
236 | STRLEN data_len=0; | |
237 | unsigned char buffer[1024]; | |
238 | unsigned long buffer_len = 1024; | |
239 | ||
240 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
241 | ||
242 | hash_id = find_hash(hash_name); | |
243 | if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
244 | rv = ecc_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, | |
245 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
246 | hash_id, &self->key); | |
247 | if (rv != CRYPT_OK) croak("FATAL: ecc_encrypt_key failed: %s", error_to_string(rv)); | |
248 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
249 | } | |
250 | OUTPUT: | |
251 | RETVAL | |
252 | ||
253 | SV * | |
254 | _decrypt(Crypt::PK::ECC self, SV * data) | |
255 | CODE: | |
256 | { | |
257 | int rv; | |
258 | unsigned char *data_ptr=NULL; | |
259 | STRLEN data_len=0; | |
260 | unsigned char buffer[1024]; | |
261 | unsigned long buffer_len = 1024; | |
262 | ||
263 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
264 | ||
265 | rv = ecc_decrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len, &self->key); | |
266 | if (rv != CRYPT_OK) croak("FATAL: ecc_decrypt_key_ex failed: %s", error_to_string(rv)); | |
267 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
268 | } | |
269 | OUTPUT: | |
270 | RETVAL | |
271 | ||
272 | SV * | |
273 | _sign(Crypt::PK::ECC self, SV * data) | |
274 | ALIAS: | |
275 | _sign_rfc7518 = 1 | |
276 | CODE: | |
277 | { | |
278 | int rv; | |
279 | unsigned char *data_ptr=NULL; | |
280 | STRLEN data_len=0; | |
281 | unsigned char buffer[1024]; | |
282 | unsigned long buffer_len = 1024; | |
283 | ||
284 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
285 | ||
286 | if (ix == 1) { | |
287 | rv = ecc_sign_hash_rfc7518(data_ptr, (unsigned long)data_len, buffer, &buffer_len, | |
288 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
289 | &self->key); | |
290 | } | |
291 | else { | |
292 | rv = ecc_sign_hash(data_ptr, (unsigned long)data_len, buffer, &buffer_len, | |
293 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
294 | &self->key); | |
295 | } | |
296 | if (rv != CRYPT_OK) croak("FATAL: ecc_sign_hash_ex failed: %s", error_to_string(rv)); | |
297 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
298 | } | |
299 | OUTPUT: | |
300 | RETVAL | |
301 | ||
302 | int | |
303 | _verify(Crypt::PK::ECC self, SV * sig, SV * data) | |
304 | ALIAS: | |
305 | _verify_rfc7518 = 1 | |
306 | CODE: | |
307 | { | |
308 | int rv, stat; | |
309 | unsigned char *data_ptr=NULL; | |
310 | STRLEN data_len=0; | |
311 | unsigned char *sig_ptr=NULL; | |
312 | STRLEN sig_len=0; | |
313 | ||
314 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
315 | sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len); | |
316 | ||
317 | RETVAL = 1; | |
318 | if (ix == 1) { | |
319 | rv = ecc_verify_hash_rfc7518(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key); | |
320 | } | |
321 | else { | |
322 | rv = ecc_verify_hash(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, &stat, &self->key); | |
323 | } | |
324 | if (rv != CRYPT_OK || stat != 1) RETVAL = 0; | |
325 | } | |
326 | OUTPUT: | |
327 | RETVAL | |
328 | ||
329 | SV * | |
330 | shared_secret(Crypt::PK::ECC self, Crypt::PK::ECC pubkey) | |
331 | CODE: | |
332 | { | |
333 | int rv; | |
334 | unsigned char buffer[1024]; | |
335 | unsigned long buffer_len = 1024; | |
336 | ||
337 | rv = ecc_shared_secret(&self->key, &pubkey->key, buffer, &buffer_len); | |
338 | if (rv != CRYPT_OK) croak("FATAL: ecc_shared_secret failed: %s", error_to_string(rv)); | |
339 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
340 | } | |
341 | OUTPUT: | |
342 | RETVAL | |
343 | ||
344 | void | |
345 | DESTROY(Crypt::PK::ECC self) | |
346 | CODE: | |
347 | _ecc_free_key(&self->key, &self->dp); | |
348 | Safefree(self); | |
349 |
0 | MODULE = CryptX PACKAGE = Crypt::PK::RSA | |
1 | ||
2 | Crypt::PK::RSA | |
3 | _new() | |
4 | CODE: | |
5 | { | |
6 | int rv; | |
7 | Newz(0, RETVAL, 1, struct rsa_struct); | |
8 | if (!RETVAL) croak("FATAL: Newz failed"); | |
9 | RETVAL->key.type = -1; | |
10 | RETVAL->yarrow_prng_index = find_prng("yarrow"); | |
11 | if(RETVAL->yarrow_prng_index==-1) croak("FATAL: find_prng('yarrow') failed"); | |
12 | rv = rng_make_prng(256, RETVAL->yarrow_prng_index, &RETVAL->yarrow_prng_state, NULL); | |
13 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
14 | } | |
15 | OUTPUT: | |
16 | RETVAL | |
17 | ||
18 | void | |
19 | generate_key(Crypt::PK::RSA self, int key_size=256, long key_e=65537) | |
20 | PPCODE: | |
21 | { | |
22 | /* key_size is in octets */ | |
23 | int rv; | |
24 | /* add a small random entropy before generating key - not necessary as we have initialized prng with 256bit entropy in _new() */ | |
25 | rv = rng_make_prng(64, self->yarrow_prng_index, &self->yarrow_prng_state, NULL); | |
26 | if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv)); | |
27 | /* gen the key */ | |
28 | rv = rsa_make_key(&self->yarrow_prng_state, self->yarrow_prng_index, key_size, key_e, &self->key); | |
29 | if (rv != CRYPT_OK) croak("FATAL: rsa_make_key failed: %s", error_to_string(rv)); | |
30 | XPUSHs(ST(0)); /* return self */ | |
31 | } | |
32 | ||
33 | void | |
34 | _import(Crypt::PK::RSA self, SV * key_data) | |
35 | PPCODE: | |
36 | { | |
37 | int rv; | |
38 | unsigned char *data=NULL; | |
39 | STRLEN data_len=0; | |
40 | ||
41 | data = (unsigned char *)SvPVbyte(key_data, data_len); | |
42 | if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; } | |
43 | rv = rsa_import(data, (unsigned long)data_len, &self->key); | |
44 | if (rv != CRYPT_OK) croak("FATAL: rsa_import failed: %s", error_to_string(rv)); | |
45 | XPUSHs(ST(0)); /* return self */ | |
46 | } | |
47 | ||
48 | void | |
49 | _import_pkcs8(Crypt::PK::RSA self, SV * key_data) | |
50 | PPCODE: | |
51 | { | |
52 | int rv; | |
53 | unsigned char *data=NULL; | |
54 | STRLEN data_len=0; | |
55 | ||
56 | data = (unsigned char *)SvPVbyte(key_data, data_len); | |
57 | if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; } | |
58 | rv = rsa_import_pkcs8(data, (unsigned long)data_len, &self->key); | |
59 | if (rv != CRYPT_OK) croak("FATAL: rsa_import_pkcs8 failed: %s", error_to_string(rv)); | |
60 | XPUSHs(ST(0)); /* return self */ | |
61 | } | |
62 | ||
63 | void | |
64 | _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) | |
65 | PPCODE: | |
66 | { | |
67 | int rv; | |
68 | if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; } | |
69 | rv = rsa_import_hex(N, e, d, p, q, dP, dQ, qP, &self->key); | |
70 | if (rv != CRYPT_OK) croak("FATAL: rsa_import_hex failed: %s", error_to_string(rv)); | |
71 | XPUSHs(ST(0)); /* return self */ | |
72 | } | |
73 | ||
74 | int | |
75 | is_private(Crypt::PK::RSA self) | |
76 | CODE: | |
77 | if (self->key.type == -1 || self->key.N == NULL) XSRETURN_UNDEF; | |
78 | RETVAL = (self->key.type == PK_PRIVATE) ? 1 : 0; | |
79 | OUTPUT: | |
80 | RETVAL | |
81 | ||
82 | int | |
83 | size(Crypt::PK::RSA self) | |
84 | CODE: | |
85 | if (self->key.type == -1 || self->key.N == NULL) XSRETURN_UNDEF; | |
86 | RETVAL = mp_unsigned_bin_size(self->key.N); | |
87 | OUTPUT: | |
88 | RETVAL | |
89 | ||
90 | SV* | |
91 | key2hash(Crypt::PK::RSA self) | |
92 | PREINIT: | |
93 | HV *rv_hash; | |
94 | long siz; | |
95 | char buf[20001]; | |
96 | SV **not_used; | |
97 | CODE: | |
98 | if (self->key.type == -1 || self->key.N == NULL) XSRETURN_UNDEF; | |
99 | rv_hash = newHV(); | |
100 | /* =====> e */ | |
101 | siz = (self->key.e) ? mp_unsigned_bin_size(self->key.e) : 0; | |
102 | if (siz>10000) { | |
103 | croak("FATAL: key2hash failed - 'e' too big number"); | |
104 | } | |
105 | if (siz>0) { | |
106 | mp_tohex(self->key.e, buf); | |
107 | not_used = hv_store(rv_hash, "e", 1, newSVpv(buf, strlen(buf)), 0); | |
108 | } | |
109 | else{ | |
110 | not_used = hv_store(rv_hash, "e", 1, newSVpv("", 0), 0); | |
111 | } | |
112 | /* =====> d */ | |
113 | siz = (self->key.d) ? mp_unsigned_bin_size(self->key.d) : 0; | |
114 | if (siz>10000) { | |
115 | croak("FATAL: key2hash failed - 'd' too big number"); | |
116 | } | |
117 | if (siz>0) { | |
118 | mp_tohex(self->key.d, buf); | |
119 | not_used = hv_store(rv_hash, "d", 1, newSVpv(buf, strlen(buf)), 0); | |
120 | } | |
121 | else{ | |
122 | not_used = hv_store(rv_hash, "d", 1, newSVpv("", 0), 0); | |
123 | } | |
124 | /* =====> N */ | |
125 | siz = (self->key.N) ? mp_unsigned_bin_size(self->key.N) : 0; | |
126 | if (siz>10000) { | |
127 | croak("FATAL: key2hash failed - 'N' too big number"); | |
128 | } | |
129 | if (siz>0) { | |
130 | mp_tohex(self->key.N, buf); | |
131 | not_used = hv_store(rv_hash, "N", 1, newSVpv(buf, strlen(buf)), 0); | |
132 | } | |
133 | else{ | |
134 | not_used = hv_store(rv_hash, "N", 1, newSVpv("", 0), 0); | |
135 | } | |
136 | /* =====> q */ | |
137 | siz = (self->key.q) ? mp_unsigned_bin_size(self->key.q) : 0; | |
138 | if (siz>10000) { | |
139 | croak("FATAL: key2hash failed - 'q' too big number"); | |
140 | } | |
141 | if (siz>0) { | |
142 | mp_tohex(self->key.q, buf); | |
143 | not_used = hv_store(rv_hash, "q", 1, newSVpv(buf, strlen(buf)), 0); | |
144 | } | |
145 | else{ | |
146 | not_used = hv_store(rv_hash, "q", 1, newSVpv("", 0), 0); | |
147 | } | |
148 | /* =====> p */ | |
149 | siz = (self->key.p) ? mp_unsigned_bin_size(self->key.p) : 0; | |
150 | if (siz>10000) { | |
151 | croak("FATAL: key2hash failed - 'p' too big number"); | |
152 | } | |
153 | if (siz>0) { | |
154 | mp_tohex(self->key.p, buf); | |
155 | not_used = hv_store(rv_hash, "p", 1, newSVpv(buf, strlen(buf)), 0); | |
156 | } | |
157 | else{ | |
158 | not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0); | |
159 | } | |
160 | /* =====> qP */ | |
161 | siz = (self->key.qP) ? mp_unsigned_bin_size(self->key.qP) : 0; | |
162 | if (siz>10000) { | |
163 | croak("FATAL: key2hash failed - 'qP' too big number"); | |
164 | } | |
165 | if (siz>0) { | |
166 | mp_tohex(self->key.qP, buf); | |
167 | not_used = hv_store(rv_hash, "qP", 2, newSVpv(buf, strlen(buf)), 0); | |
168 | } | |
169 | else{ | |
170 | not_used = hv_store(rv_hash, "qP", 2, newSVpv("", 0), 0); | |
171 | } | |
172 | /* =====> dP */ | |
173 | siz = (self->key.dP) ? mp_unsigned_bin_size(self->key.dP) : 0; | |
174 | if (siz>10000) { | |
175 | croak("FATAL: key2hash failed - 'dP' too big number"); | |
176 | } | |
177 | if (siz>0) { | |
178 | mp_tohex(self->key.dP, buf); | |
179 | not_used = hv_store(rv_hash, "dP", 2, newSVpv(buf, strlen(buf)), 0); | |
180 | } | |
181 | else{ | |
182 | not_used = hv_store(rv_hash, "dP", 2, newSVpv("", 0), 0); | |
183 | } | |
184 | /* =====> dQ */ | |
185 | siz = (self->key.dQ) ? mp_unsigned_bin_size(self->key.dQ) : 0; | |
186 | if (siz>10000) { | |
187 | croak("FATAL: key2hash failed - 'dQ' too big number"); | |
188 | } | |
189 | if (siz>0) { | |
190 | mp_tohex(self->key.dQ, buf); | |
191 | not_used = hv_store(rv_hash, "dQ", 2, newSVpv(buf, strlen(buf)), 0); | |
192 | } | |
193 | else{ | |
194 | not_used = hv_store(rv_hash, "dQ", 2, newSVpv("", 0), 0); | |
195 | } | |
196 | /* =====> size */ | |
197 | not_used = hv_store(rv_hash, "size", 4, newSViv(mp_unsigned_bin_size(self->key.N)), 0); | |
198 | /* =====> type */ | |
199 | not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0); | |
200 | if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */ | |
201 | RETVAL = newRV_noinc((SV*)rv_hash); | |
202 | OUTPUT: | |
203 | RETVAL | |
204 | ||
205 | SV* | |
206 | export_key_der(Crypt::PK::RSA self, char * type) | |
207 | CODE: | |
208 | { | |
209 | int rv; | |
210 | unsigned char out[4096]; | |
211 | unsigned long int out_len = 4096; | |
212 | ||
213 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
214 | if (strnEQ(type, "private", 7)) { | |
215 | rv = rsa_export(out, &out_len, PK_PRIVATE, &self->key); | |
216 | if (rv != CRYPT_OK) croak("FATAL: rsa_export(PK_PRIVATE) failed: %s", error_to_string(rv)); | |
217 | RETVAL = newSVpvn((char*)out, out_len); | |
218 | } | |
219 | else if (strnEQ(type, "public", 6)) { | |
220 | rv = rsa_export(out, &out_len, PK_PUBLIC, &self->key); | |
221 | if (rv != CRYPT_OK) croak("FATAL: rsa_export(PK_PUBLIC) failed: %s", error_to_string(rv)); | |
222 | RETVAL = newSVpvn((char*)out, out_len); | |
223 | } | |
224 | else { | |
225 | croak("FATAL: export_key_der invalid type '%s'", type); | |
226 | } | |
227 | } | |
228 | OUTPUT: | |
229 | RETVAL | |
230 | ||
231 | SV * | |
232 | _encrypt(Crypt::PK::RSA self, SV * data, char * padding, char * oaep_hash, SV * oaep_lparam) | |
233 | CODE: | |
234 | { | |
235 | int rv, hash_id; | |
236 | unsigned char *lparam_ptr=NULL; | |
237 | STRLEN lparam_len=0; | |
238 | unsigned char *data_ptr=NULL; | |
239 | STRLEN data_len=0; | |
240 | unsigned char buffer[1024]; | |
241 | unsigned long buffer_len = 1024; | |
242 | ||
243 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
244 | ||
245 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
246 | if (strnEQ(padding, "oaep", 4)) { | |
247 | hash_id = find_hash(oaep_hash); | |
248 | if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", oaep_hash); | |
249 | lparam_ptr = (unsigned char *)SvPVbyte(oaep_lparam, lparam_len); | |
250 | rv = rsa_encrypt_key_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, lparam_ptr, (unsigned long)lparam_len, | |
251 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
252 | hash_id, LTC_PKCS_1_OAEP, &self->key); | |
253 | if (rv != CRYPT_OK) croak("FATAL: rsa_encrypt_key_ex failed: %s", error_to_string(rv)); | |
254 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
255 | } | |
256 | else if (strnEQ(padding, "v1.5", 4)) { | |
257 | rv = rsa_encrypt_key_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, NULL, 0, | |
258 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
259 | 0, LTC_PKCS_1_V1_5, &self->key); | |
260 | if (rv != CRYPT_OK) croak("FATAL: rsa_encrypt_key_ex failed: %s", error_to_string(rv)); | |
261 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
262 | } | |
263 | else if (strnEQ(padding, "none", 4)) { | |
264 | /* raw RSA */ | |
265 | rv = ltc_mp.rsa_me(data_ptr, (unsigned long)data_len, buffer, &buffer_len, PK_PUBLIC, &self->key); | |
266 | if (rv != CRYPT_OK) croak("FATAL: rsa_me failed: %s", error_to_string(rv)); | |
267 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
268 | } | |
269 | else { | |
270 | croak("FATAL: rsa_encrypt invalid padding '%s'", padding); | |
271 | } | |
272 | } | |
273 | OUTPUT: | |
274 | RETVAL | |
275 | ||
276 | SV * | |
277 | _decrypt(Crypt::PK::RSA self, SV * data, char * padding, char * oaep_hash, SV * oaep_lparam) | |
278 | CODE: | |
279 | { | |
280 | int rv, hash_id, stat; | |
281 | unsigned char *lparam_ptr=NULL; | |
282 | STRLEN lparam_len=0; | |
283 | unsigned char *data_ptr=NULL; | |
284 | STRLEN data_len=0; | |
285 | unsigned char buffer[1024]; | |
286 | unsigned long buffer_len = 1024; | |
287 | ||
288 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
289 | ||
290 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
291 | if (strnEQ(padding, "oaep", 4)) { | |
292 | hash_id = find_hash(oaep_hash); | |
293 | if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", oaep_hash); | |
294 | lparam_ptr = (unsigned char *)SvPVbyte(oaep_lparam, lparam_len); | |
295 | rv = rsa_decrypt_key_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, lparam_ptr, (unsigned long)lparam_len, | |
296 | hash_id, LTC_PKCS_1_OAEP, &stat, &self->key); | |
297 | if (rv != CRYPT_OK) croak("FATAL: rsa_decrypt_key_ex failed: %s", error_to_string(rv)); | |
298 | if (stat != 1) croak("FATAL: rsa_decrypt - not valid OAEP packet"); | |
299 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
300 | } | |
301 | else if (strnEQ(padding, "v1.5", 4)) { | |
302 | rv = rsa_decrypt_key_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, NULL, 0, | |
303 | 0, LTC_PKCS_1_V1_5, &stat, &self->key); | |
304 | if (rv != CRYPT_OK) croak("FATAL: rsa_decrypt_key_ex failed: %s", error_to_string(rv)); | |
305 | if (stat != 1) croak("FATAL: rsa_decrypt - invalid"); | |
306 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
307 | } | |
308 | else if (strnEQ(padding, "none", 4)) { | |
309 | /* raw RSA */ | |
310 | rv = ltc_mp.rsa_me(data_ptr, (unsigned long)data_len, buffer, &buffer_len, PK_PRIVATE, &self->key); | |
311 | if (rv != CRYPT_OK) croak("FATAL: rsa_me failed: %s", error_to_string(rv)); | |
312 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
313 | } | |
314 | else { | |
315 | croak("FATAL: rsa_encrypt invalid padding '%s'", padding); | |
316 | } | |
317 | } | |
318 | OUTPUT: | |
319 | RETVAL | |
320 | ||
321 | SV * | |
322 | _sign(Crypt::PK::RSA self, SV * data, char * padding, char * hash_name, unsigned long saltlen=12) | |
323 | CODE: | |
324 | { | |
325 | int rv, hash_id; | |
326 | unsigned char *data_ptr=NULL; | |
327 | STRLEN data_len=0; | |
328 | unsigned char buffer[1024]; | |
329 | unsigned long buffer_len = 1024; | |
330 | ||
331 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
332 | ||
333 | RETVAL = newSVpvn(NULL, 0); /* undef */ | |
334 | hash_id = find_hash(hash_name); | |
335 | if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
336 | if (strnEQ(padding, "pss", 3)) { | |
337 | rv = rsa_sign_hash_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, LTC_PKCS_1_PSS, | |
338 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
339 | hash_id, saltlen, &self->key); | |
340 | if (rv != CRYPT_OK) croak("FATAL: rsa_sign_hash_ex failed: %s", error_to_string(rv)); | |
341 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
342 | } | |
343 | else if (strnEQ(padding, "v1.5", 4)) { | |
344 | rv = rsa_sign_hash_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, LTC_PKCS_1_V1_5, | |
345 | &self->yarrow_prng_state, self->yarrow_prng_index, | |
346 | hash_id, 0, &self->key); | |
347 | if (rv != CRYPT_OK) croak("FATAL: rsa_sign_hash_ex failed: %s", error_to_string(rv)); | |
348 | RETVAL = newSVpvn((char*)buffer, buffer_len); | |
349 | } | |
350 | else { | |
351 | croak("FATAL: rsa_sign invalid padding '%s'", padding); | |
352 | } | |
353 | } | |
354 | OUTPUT: | |
355 | RETVAL | |
356 | ||
357 | int | |
358 | _verify(Crypt::PK::RSA self, SV * sig, SV * data, char * padding, char * hash_name, unsigned long saltlen=12) | |
359 | CODE: | |
360 | { | |
361 | int rv, hash_id, stat; | |
362 | unsigned char *data_ptr=NULL; | |
363 | STRLEN data_len=0; | |
364 | unsigned char *sig_ptr=NULL; | |
365 | STRLEN sig_len=0; | |
366 | ||
367 | data_ptr = (unsigned char *)SvPVbyte(data, data_len); | |
368 | sig_ptr = (unsigned char *)SvPVbyte(sig, sig_len); | |
369 | ||
370 | RETVAL = 1; | |
371 | hash_id = find_hash(hash_name); | |
372 | if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name); | |
373 | if (strnEQ(padding, "pss", 3)) { | |
374 | rv = rsa_verify_hash_ex(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, LTC_PKCS_1_PSS, | |
375 | hash_id, saltlen, &stat, &self->key); | |
376 | if (rv != CRYPT_OK || stat != 1) RETVAL = 0; | |
377 | } | |
378 | else if (strnEQ(padding, "v1.5", 4)) { | |
379 | rv = rsa_verify_hash_ex(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, LTC_PKCS_1_V1_5, | |
380 | hash_id, 0, &stat, &self->key); | |
381 | if (rv != CRYPT_OK || stat != 1) RETVAL = 0; | |
382 | } | |
383 | else { | |
384 | croak("FATAL: rsa_verify invalid padding '%s'", padding); | |
385 | } | |
386 | } | |
387 | OUTPUT: | |
388 | RETVAL | |
389 | ||
390 | void | |
391 | DESTROY(Crypt::PK::RSA self) | |
392 | CODE: | |
393 | if (self->key.type != -1) { rsa_free(&self->key); self->key.type = -1; } | |
394 | Safefree(self); | |
395 |
0 | MODULE = CryptX PACKAGE = Crypt::PRNG | |
1 | ||
2 | Crypt::PRNG | |
3 | _new(IV curpid, char * prng_name, SV * entropy=&PL_sv_undef) | |
4 | CODE: | |
5 | { | |
6 | int rv, id; | |
7 | unsigned char *ent=NULL; | |
8 | STRLEN ent_len=0; | |
9 | unsigned char entropy_buf[32]; | |
10 | ||
11 | Newz(0, RETVAL, 1, struct prng_struct); | |
12 | if (!RETVAL) croak("FATAL: Newz failed"); | |
13 | ||
14 | id = find_prng(prng_name); | |
15 | if(id==-1) croak("FATAL: find_prng failed for '%s'", prng_name); | |
16 | RETVAL->id = id; | |
17 | RETVAL->last_pid = curpid; | |
18 | RETVAL->desc = &prng_descriptor[id]; | |
19 | ||
20 | rv = RETVAL->desc->start(&RETVAL->state); | |
21 | if (rv != CRYPT_OK) croak("FATAL: PRNG_start failed: %s", error_to_string(rv)); | |
22 | ||
23 | if(SvOK(entropy)) { | |
24 | ent = (unsigned char *) SvPVbyte(entropy, ent_len); | |
25 | rv = RETVAL->desc->add_entropy(ent, (unsigned long)ent_len, &RETVAL->state); | |
26 | if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv)); | |
27 | } | |
28 | else { | |
29 | if (rng_get_bytes(entropy_buf, 32, NULL) != 32) croak("FATAL: rng_get_bytes failed: %s", error_to_string(rv)); | |
30 | rv = RETVAL->desc->add_entropy(entropy_buf, 32, &RETVAL->state); | |
31 | if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv)); | |
32 | } | |
33 | rv = RETVAL->desc->ready(&RETVAL->state); | |
34 | if (rv != CRYPT_OK) croak("FATAL: PRNG_ready failed: %s", error_to_string(rv)); | |
35 | } | |
36 | OUTPUT: | |
37 | RETVAL | |
38 | ||
39 | void | |
40 | DESTROY(Crypt::PRNG self) | |
41 | CODE: | |
42 | { | |
43 | Safefree(self); | |
44 | } | |
45 | ||
46 | void | |
47 | add_entropy(Crypt::PRNG self, SV * entropy=&PL_sv_undef) | |
48 | CODE: | |
49 | { | |
50 | STRLEN in_len=0; | |
51 | unsigned char *in_buffer=NULL; | |
52 | unsigned char entropy_buf[32]; | |
53 | int rv; | |
54 | if(SvOK(entropy)) { | |
55 | in_buffer = (unsigned char *) SvPVbyte(entropy, in_len); | |
56 | rv = self->desc->add_entropy(in_buffer, (unsigned long)in_len, &self->state); | |
57 | if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv)); | |
58 | } | |
59 | else { | |
60 | if (rng_get_bytes(entropy_buf, 32, NULL) != 32) croak("FATAL: rng_get_bytes failed"); | |
61 | rv = self->desc->add_entropy(entropy_buf, 32, &self->state); | |
62 | if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv)); | |
63 | } | |
64 | rv = self->desc->ready(&self->state); | |
65 | if (rv != CRYPT_OK) croak("FATAL: PRNG_ready failed: %s", error_to_string(rv)); | |
66 | } | |
67 | ||
68 | SV * | |
69 | _bytes(Crypt::PRNG self, IV curpid, STRLEN output_len) | |
70 | CODE: | |
71 | { | |
72 | int rv_len; | |
73 | unsigned char *rdata; | |
74 | unsigned char entropy_buf[32]; | |
75 | ||
76 | if (self->last_pid != curpid) { | |
77 | rng_get_bytes(entropy_buf, 32, NULL); | |
78 | self->desc->add_entropy(entropy_buf, 32, &self->state); | |
79 | self->desc->ready(&self->state); | |
80 | self->last_pid = curpid; | |
81 | } | |
82 | ||
83 | RETVAL = NEWSV(0, output_len); | |
84 | SvPOK_only(RETVAL); | |
85 | SvCUR_set(RETVAL, output_len); | |
86 | rdata = (unsigned char *)SvPV_nolen(RETVAL); | |
87 | rv_len = (self->desc->read)(rdata, (unsigned long)output_len, &self->state); | |
88 | if ((UV)rv_len != output_len) croak("FATAL: PRNG_read failed"); | |
89 | } | |
90 | OUTPUT: | |
91 | RETVAL | |
92 | ||
93 | UV | |
94 | _int32(Crypt::PRNG self, IV curpid) | |
95 | CODE: | |
96 | { | |
97 | int i; | |
98 | unsigned char rdata[4]; | |
99 | unsigned char entropy_buf[32]; | |
100 | ||
101 | if (self->last_pid != curpid) { | |
102 | rng_get_bytes(entropy_buf, 32, NULL); | |
103 | self->desc->add_entropy(entropy_buf, 32, &self->state); | |
104 | self->desc->ready(&self->state); | |
105 | self->last_pid = curpid; | |
106 | } | |
107 | ||
108 | i = (self->desc->read)(rdata, 4, &self->state); | |
109 | if (i != 4) croak("FATAL: PRNG_read failed"); | |
110 | RETVAL = ((UV)(rdata[0])<<24) + ((UV)(rdata[1])<<16) + ((UV)(rdata[2])<<8) + ((UV)(rdata[3])); | |
111 | } | |
112 | OUTPUT: | |
113 | RETVAL | |
114 | ||
115 | NV | |
116 | _double(Crypt::PRNG self, IV curpid, ...) | |
117 | CODE: | |
118 | { | |
119 | int i; | |
120 | unsigned long a, b; /* 32bit is enough */ | |
121 | unsigned char rdata[7]; /* for double we need 53 bits */ | |
122 | unsigned char entropy_buf[32]; | |
123 | NV limit; | |
124 | ||
125 | if (self->last_pid != curpid) { | |
126 | rng_get_bytes(entropy_buf, 32, NULL); | |
127 | self->desc->add_entropy(entropy_buf, 32, &self->state); | |
128 | self->desc->ready(&self->state); | |
129 | self->last_pid = curpid; | |
130 | } | |
131 | ||
132 | i = (self->desc->read)(rdata, 7, &self->state); | |
133 | if (i != 7) croak("FATAL: PRNG_read failed"); | |
134 | a = (((unsigned long)(rdata[0])<<16) + ((unsigned long)(rdata[1])<<8) + ((unsigned long)(rdata[2]))) & 0x1FFFFF; /* 21 bits */ | |
135 | b = ((unsigned long)(rdata[3])<<24) + ((unsigned long)(rdata[4])<<16) + ((unsigned long)(rdata[5])<<8) + ((unsigned long)(rdata[6])); /* 32 bits */ | |
136 | RETVAL = ( (NV)a * 4294967296.0 + (NV)b ) / 9007199254740992.0; /* (a * 2^32 + b) / 2^53 */ | |
137 | if (items>2 && SvOK(ST(2))) { | |
138 | limit = SvNV(ST(2)); | |
139 | if (limit > 0 || limit < 0) RETVAL = RETVAL * limit; | |
140 | } | |
141 | } | |
142 | OUTPUT: | |
143 | RETVAL |