Codebase list libcryptx-perl / 4a2b9bf
xs tuning Karel Miko 6 years ago
37 changed file(s) with 436 addition(s) and 249 deletion(s). Raw diff Collapse all Expand all
2020 int id;
2121
2222 id = find_hash(hash_name);
23 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
23 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
2424 [%-ELSIF lc_name == 'blake2s' || lc_name == 'blake2b' %]
2525 [%-ELSIF lc_name == 'pelican' || lc_name == 'poly1305' %]
2626 [%-ELSE%]
2727 int id;
2828
2929 id = find_cipher(cipher_name);
30 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
30 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
3131 [%-END%]
3232
3333 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
4343 [%-ELSE%]
4444 rv = [%lc_name%]_init(&RETVAL->state, id, k, (unsigned long)k_len);
4545 [%-END%]
46 if (rv != CRYPT_OK) croak("FATAL: [%lc_name%]_init failed: %s", error_to_string(rv));
46 if (rv != CRYPT_OK) {
47 Safefree(RETVAL);
48 croak("FATAL: [%lc_name%]_init failed: %s", error_to_string(rv));
49 }
4750 }
4851 OUTPUT:
4952 RETVAL
1414 RETVAL->direction = 0;
1515 RETVAL->cipher_rounds = rounds;
1616 RETVAL->cipher_id = find_cipher(cipher_name);
17 if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
17 if (RETVAL->cipher_id == -1) {
18 Safefree(RETVAL);
19 croak("FATAL: find_cipfer failed for '%s'", cipher_name);
20 }
1821 [%-IF lc_name == 'ctr' %]
1922 if (ctr_mode == 0) RETVAL->ctr_mode_param = CTR_COUNTER_LITTLE_ENDIAN;
2023 if (ctr_mode == 1) RETVAL->ctr_mode_param = CTR_COUNTER_BIG_ENDIAN;
1212 RETVAL->direction = 0;
1313 RETVAL->cipher_rounds = rounds;
1414 RETVAL->cipher_id = find_cipher(cipher_name);
15 if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
15 if (RETVAL->cipher_id == -1) {
16 Safefree(RETVAL);
17 croak("FATAL: find_cipfer failed for '%s'", cipher_name);
18 }
1619 }
1720 OUTPUT:
1821 RETVAL
8487 in_data_start = 0;
8588 has_tmp_block = 0;
8689 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
87 if (in_data_len==0) {
90 if (in_data_len == 0) {
8891 RETVAL = newSVpvn("", 0);
8992 }
9093 else {
91 if(self->padlen > 0) {
94 if (self->padlen > 0) {
9295 i = (blen - self->padlen);
9396 if (in_data_len >= i) { /* enough data to fill pad */
9497 Copy(in_data, self->pad+self->padlen, i, unsigned char);
113116 in_data_len -= i;
114117 }
115118
116 if (in_data_len>0) {
119 if (in_data_len > 0) {
117120 i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len);
118121 RETVAL = NEWSV(0, i);
119122 SvPOK_only(RETVAL);
124127 out_data += blen;
125128 }
126129 rv = [%lc_name%]_encrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state);
127 if (rv != CRYPT_OK) croak("FATAL: [%lc_name%]_encrypt failed: %s", error_to_string(rv));
130 if (rv != CRYPT_OK) {
131 SvREFCNT_dec(RETVAL);
132 croak("FATAL: [%lc_name%]_encrypt failed: %s", error_to_string(rv));
133 }
128134 } /* in_data_len>0 */
129135 else if (has_tmp_block) {
130136 RETVAL = newSVpvn((char*)tmp_block, blen);
147153 blen = (&self->state)->blocklen;
148154 if (self->padlen<0 || self->padlen>=blen) croak("FATAL: invalid padlen");
149155
150 if(self->padding_mode == 1) { /* pkcs5|7 padding */
156 if (self->padding_mode == 1) { /* pkcs5|7 padding */
151157 i = blen - self->padlen;
152158 if (i == 0) i = blen;
153159 for(j=self->padlen; j<blen; j++) self->pad[j] = (unsigned char)i;
154160 rv = [%lc_name%]_encrypt(self->pad, tmp_block, blen, &self->state);
155161 if (rv != CRYPT_OK) croak("FATAL: [%lc_name%]_encrypt failed: %s", error_to_string(rv));
156162 }
157 else if(self->padding_mode == 2) { /* oneandzeroes padding */
163 else if (self->padding_mode == 2) { /* oneandzeroes padding */
158164 self->pad[self->padlen] = 0x80;
159165 for(j=self->padlen+1; j<blen; j++) self->pad[j] = 0;
160166 rv = [%lc_name%]_encrypt(self->pad, tmp_block, blen, &self->state);
186192 in_data_start = 0;
187193 has_tmp_block = 0;
188194 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
189 if (in_data_len==0) {
195 if (in_data_len == 0) {
190196 RETVAL = newSVpvn("", 0);
191197 }
192198 else {
193199
194 if(self->padlen == blen) {
200 if (self->padlen == blen) {
195201 rv = [%lc_name%]_decrypt(self->pad, tmp_block, blen, &self->state);
196202 if (rv != CRYPT_OK) croak("FATAL: [%lc_name%]_decrypt failed: %s", error_to_string(rv));
197203 self->padlen = 0;
198204 has_tmp_block = 1;
199205 } /* padlen == blen */
200 else if(self->padlen > 0) {
206 else if (self->padlen > 0) {
201207 i = (blen - self->padlen); /* remaining bytes in padding buffer */
202208 if (in_data_len >= i) { /* enough data to fill pad */
203209 Copy(in_data, self->pad+self->padlen, i, unsigned char);
218224 }
219225 } /* padlen > 0 */
220226
221 /* here: a/ padlen==1..16 && in_data_len==0; b/ padlen==0 && in_data_len>0 */
227 /* here: a/ padlen == 1..16 && in_data_len == 0; b/ padlen == 0 && in_data_len > 0 */
222228 if (in_data_len>0) {
223229 i = (unsigned long)(in_data_len % blen);
224230 if (i>0) { /* save tail of data into pad */
229235 }
230236
231237 if (in_data_len>0) {
232 if(self->padlen == 0 && self->padding_mode !=0) {
238 if (self->padlen == 0 && self->padding_mode !=0) {
233239 /* in case of padding keep full pad if no more data */
234240 Copy(in_data+in_data_start+in_data_len-blen, self->pad, blen, unsigned char);
235241 self->padlen = blen;
249255 out_data += blen;
250256 }
251257 rv = [%lc_name%]_decrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state);
252 if (rv != CRYPT_OK) croak("FATAL: [%lc_name%]_decrypt failed: %s", error_to_string(rv));
258 if (rv != CRYPT_OK) {
259 SvREFCNT_dec(RETVAL);
260 croak("FATAL: [%lc_name%]_decrypt failed: %s", error_to_string(rv));
261 }
253262 }
254263 } /* in_data_len>0 */
255264 else if (has_tmp_block) {
278287 if (self->padlen != blen) croak("FATAL: cipher text length has to be multiple of %d (%d)", blen, self->padlen);
279288 rv = [%lc_name%]_decrypt(self->pad, tmp_block, blen, &self->state);
280289 if (rv != CRYPT_OK) croak("FATAL: [%lc_name%]_decrypt failed: %s", error_to_string(rv));
281 if(self->padding_mode == 0) { /* no padding */
290 if (self->padding_mode == 0) { /* no padding */
282291 rv_len = blen;
283292 }
284 else if(self->padding_mode == 1) { /* pkcs5|7 padding */
293 else if (self->padding_mode == 1) { /* pkcs5|7 padding */
285294 i = tmp_block[blen-1];
286295 rv_len = blen - (i>blen ? blen : i);
287296 }
288 else if(self->padding_mode == 2) { /* oneandzeroes padding */
297 else if (self->padding_mode == 2) { /* oneandzeroes padding */
289298 rv_len = blen;
290299 while ((unsigned char)tmp_block[rv_len-1] == 0x00) rv_len--;
291300 if ((unsigned char)tmp_block[rv_len-1] == 0x80) rv_len--;
1919 pt = (unsigned char *) SvPVbyte(plaintext, pt_len);
2020
2121 id = find_cipher(cipher_name);
22 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
22 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
2323
2424 ct = NEWSV(0, pt_len);
2525 SvPOK_only(ct);
2626 SvCUR_set(ct, pt_len);
2727
28 if(tag_len<4 || tag_len>16) tag_len = 16;
28 if (tag_len < 4 || tag_len > 16) tag_len = 16;
2929
3030 rv = ccm_memory(id, k, (unsigned long)k_len, NULL, n, (unsigned long)n_len, h, (unsigned long)h_len,
3131 pt, (unsigned long)pt_len, (unsigned char *)SvPVX(ct), tag, &tag_len, CCM_ENCRYPT);
32 if (rv != CRYPT_OK) croak("FATAL: ccm_memory failed: %s", error_to_string(rv));
32 if (rv != CRYPT_OK) {
33 SvREFCNT_dec(ct);
34 croak("FATAL: ccm_memory failed: %s", error_to_string(rv));
35 }
3336
3437 XPUSHs(sv_2mortal(ct));
3538 XPUSHs(sv_2mortal(newSVpvn((char*)tag,tag_len)));
6972 t = (unsigned char *) SvPVbyte(tag, t_len);
7073
7174 id = find_cipher(cipher_name);
72 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
75 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
7376
7477 pt = NEWSV(0, ct_len);
7578 SvPOK_only(pt);
8184 rv = ccm_memory(id, k, (unsigned long)k_len, NULL, n, (unsigned long)n_len, h, (unsigned long)h_len,
8285 (unsigned char *)SvPV_nolen(pt), (unsigned long)ct_len, ct, xtag, &xtag_len, CCM_DECRYPT);
8386 if (rv != CRYPT_OK) {
87 SvREFCNT_dec(pt);
8488 XPUSHs(sv_2mortal(newSVpvn(NULL,0))); /* undef */
8589 }
8690 else {
1818 if (!RETVAL) croak("FATAL: Newz failed");
1919
2020 rv = chacha20poly1305_init(&RETVAL->state, k, (unsigned long)k_len);
21 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_init failed: %s", error_to_string(rv));
21 if (rv != CRYPT_OK) {
22 Safefree(RETVAL);
23 croak("FATAL: chacha20poly1305_init failed: %s", error_to_string(rv));
24 }
2225
2326 if (iv && iv_len > 0) {
2427 rv = chacha20poly1305_setiv(&RETVAL->state, iv, (unsigned long)iv_len);
25 if (rv != CRYPT_OK) croak("FATAL: chacha20poly1305_setiv failed: %s", error_to_string(rv));
28 if (rv != CRYPT_OK) {
29 Safefree(RETVAL);
30 croak("FATAL: chacha20poly1305_setiv failed: %s", error_to_string(rv));
31 }
2632 }
2733 }
2834 OUTPUT:
169175 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
170176 }
171177 else {
172 if(!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
178 if (!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
173179 expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len);
174180 if (expected_tag_len!=tag_len) {
175181 XPUSHs(sv_2mortal(newSViv(0))); /* false */
99 STRLEN n_len=0;
1010 unsigned char *h=NULL;
1111 STRLEN h_len=0;
12 int id;
12 int rv, id;
1313
1414 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1515 k = (unsigned char *) SvPVbyte(key, k_len);
1616 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
1717 n = (unsigned char *) SvPVbyte(nonce, n_len);
18 if(SvOK(adata)) { /* adata is optional param */
19 if(!SvPOK(adata)) croak("FATAL: adata must be string/buffer scalar");
18 if (SvOK(adata)) { /* adata is optional param */
19 if (!SvPOK(adata)) croak("FATAL: adata must be string/buffer scalar");
2020 h = (unsigned char *) SvPVbyte(adata, h_len);
2121 }
2222
2323 id = find_cipher(cipher_name);
24 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
24 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
2525
2626 Newz(0, RETVAL, 1, struct eax_struct);
2727 if (!RETVAL) croak("FATAL: Newz failed");
2828
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");
29 rv = eax_init(&RETVAL->state, id, k, (unsigned long)k_len, n, (unsigned long)n_len, h, (unsigned long)h_len);
30 if (rv != CRYPT_OK) {
31 Safefree(RETVAL);
32 croak("FATAL: eax setup failed: %s", error_to_string(rv));
3133 }
3234 }
3335 OUTPUT:
130132 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
131133 }
132134 else {
133 if(!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
135 if (!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
134136 expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len);
135137 if (expected_tag_len!=tag_len) {
136138 XPUSHs(sv_2mortal(newSViv(0))); /* false */
2121 if (!RETVAL) croak("FATAL: Newz failed");
2222
2323 rv = gcm_init(&RETVAL->state, id, k, (unsigned long)k_len);
24 if (rv != CRYPT_OK) croak("FATAL: gcm_init failed: %s", error_to_string(rv));
24 if (rv != CRYPT_OK) {
25 Safefree(RETVAL);
26 croak("FATAL: gcm_init failed: %s", error_to_string(rv));
27 }
2528
2629 if (iv && iv_len > 0) {
2730 rv = gcm_add_iv(&RETVAL->state, iv, (unsigned long)iv_len);
28 if (rv != CRYPT_OK) croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv));
31 if (rv != CRYPT_OK) {
32 Safefree(RETVAL);
33 croak("FATAL: gcm_add_iv failed: %s", error_to_string(rv));
34 }
2935 }
3036 }
3137 OUTPUT:
168174 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
169175 }
170176 else {
171 if(!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
177 if (!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
172178 expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len);
173179 if (expected_tag_len!=tag_len) {
174180 XPUSHs(sv_2mortal(newSViv(0))); /* false */
77 unsigned char *k=NULL;
88 unsigned char *n=NULL;
99 STRLEN n_len=0;
10 int id;
10 int rv, id;
1111
1212 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1313 k = (unsigned char *) SvPVbyte(key, k_len);
1515 n = (unsigned char *) SvPVbyte(nonce, n_len);
1616
1717 id = find_cipher(cipher_name);
18 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
18 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1919
2020 Newz(0, RETVAL, 1, struct ocb_struct);
2121 if (!RETVAL) croak("FATAL: Newz failed");
2222
23 if (ocb3_init(&RETVAL->state, id, k, (unsigned long)k_len, n, (unsigned long)n_len, taglen) != CRYPT_OK) {
24 croak("FATAL: ocb setup failed");
23 rv = ocb3_init(&RETVAL->state, id, k, (unsigned long)k_len, n, (unsigned long)n_len, taglen);
24 if (rv != CRYPT_OK) {
25 Safefree(RETVAL);
26 croak("FATAL: ocb setup failed: %s", error_to_string(rv));
2527 }
2628 }
2729 OUTPUT:
9799 unsigned char *in_data, *out_data;
98100
99101 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
100 if (in_data_len > 0) {
101 RETVAL = NEWSV(0, in_data_len);
102 SvPOK_only(RETVAL);
103 SvCUR_set(RETVAL, in_data_len);
104 out_data = (unsigned char *)SvPVX(RETVAL);
105 }
106 else {
107 RETVAL = newSVpvn("", 0);
108 out_data = NULL;
109 }
110 rv = ocb3_encrypt_last(&self->state, in_data, (unsigned long)in_data_len, out_data);
111 if (rv != CRYPT_OK) {
112 SvREFCNT_dec(RETVAL);
113 croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
102 if (in_data_len == 0) {
103 rv = ocb3_encrypt_last(&self->state, in_data, 0, NULL);
104 if (rv != CRYPT_OK) {
105 croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
106 }
107 RETVAL = newSVpvn("", 0);
108 }
109 else {
110 RETVAL = NEWSV(0, in_data_len);
111 SvPOK_only(RETVAL);
112 SvCUR_set(RETVAL, in_data_len);
113 out_data = (unsigned char *)SvPVX(RETVAL);
114 rv = ocb3_encrypt_last(&self->state, in_data, (unsigned long)in_data_len, out_data);
115 if (rv != CRYPT_OK) {
116 SvREFCNT_dec(RETVAL);
117 croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
118 }
114119 }
115120 }
116121 OUTPUT:
125130 unsigned char *in_data, *out_data;
126131
127132 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
128 if (in_data_len==0) {
133 if (in_data_len == 0) {
129134 RETVAL = newSVpvn("", 0);
130135 }
131136 else {
155160 unsigned char *in_data, *out_data;
156161
157162 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
158 if (in_data_len > 0) {
159 RETVAL = NEWSV(0, in_data_len);
160 SvPOK_only(RETVAL);
161 SvCUR_set(RETVAL, in_data_len);
162 out_data = (unsigned char *)SvPVX(RETVAL);
163 }
164 else {
165 RETVAL = newSVpvn("", 0);
166 out_data = NULL;
167 }
168 rv = ocb3_decrypt_last(&self->state, in_data, (unsigned long)in_data_len, out_data);
169 if (rv != CRYPT_OK) croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
163 if (in_data_len == 0) {
164 rv = ocb3_decrypt_last(&self->state, in_data, 0, NULL);
165 if (rv != CRYPT_OK) {
166 croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
167 }
168 RETVAL = newSVpvn("", 0);
169 }
170 else {
171 RETVAL = NEWSV(0, in_data_len);
172 SvPOK_only(RETVAL);
173 SvCUR_set(RETVAL, in_data_len);
174 out_data = (unsigned char *)SvPVX(RETVAL);
175 rv = ocb3_decrypt_last(&self->state, in_data, (unsigned long)in_data_len, out_data);
176 if (rv != CRYPT_OK) {
177 SvREFCNT_dec(RETVAL);
178 croak("FATAL: ocb3_encrypt_last failed: %s", error_to_string(rv));
179 }
180 }
170181 }
171182 OUTPUT:
172183 RETVAL
201212 XPUSHs(sv_2mortal(newSVpvn((char*)tag, tag_len)));
202213 }
203214 else {
204 if(!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
215 if (!SvPOK(ST(1))) croak("FATAL: expected_tag must be string/buffer scalar");
205216 expected_tag = (unsigned char *) SvPVbyte(ST(1), expected_tag_len);
206217 if (expected_tag_len!=tag_len) {
207218 XPUSHs(sv_2mortal(newSViv(0))); /* false */
22 Crypt::Checksum::Adler32
33 new(Class)
44 CODE:
5 {
56 Newz(0, RETVAL, 1, adler32_state);
67 if (!RETVAL) croak("FATAL: Newz failed");
7 adler32_init(RETVAL);
8 adler32_init(RETVAL); /* returns void */
9 }
810 OUTPUT:
911 RETVAL
1012
1719 reset(Crypt::Checksum::Adler32 self)
1820 PPCODE:
1921 {
20 adler32_init(self);
22 adler32_init(self); /* returns void */
2123 XPUSHs(ST(0)); /* return self */
2224 }
2325
3941 unsigned char *in;
4042 for(i=1; i<items; i++) {
4143 in = (unsigned char *)SvPVbyte(ST(i), inlen);
42 if (inlen>0) adler32_update(self, in, (unsigned long)inlen);
44 if (inlen > 0) {
45 adler32_update(self, in, (unsigned long)inlen); /* returns void */
46 }
4347 }
4448 XPUSHs(ST(0)); /* return self */
4549 }
4953 CODE:
5054 {
5155 unsigned char hash[4];
52 adler32_finish(self, hash, 4);
56 adler32_finish(self, hash, 4); /* returns void */
5357 RETVAL = newSVpvn((char *) hash, 4);
5458 }
5559 OUTPUT:
6266 unsigned long i;
6367 unsigned char hash[4];
6468 char hash_hex[4*2 + 1];
65 adler32_finish(self, hash, 4);
69 adler32_finish(self, hash, 4); /* returns void */
6670 hash_hex[0] = '\0';
6771 for(i=0; i<4; i++) sprintf(&hash_hex[2*i], "%02x", hash[i]);
6872 RETVAL = newSVpvn(hash_hex, strlen(hash_hex));
22 Crypt::Checksum::CRC32
33 new(Class)
44 CODE:
5 {
6 int rv;
57 Newz(0, RETVAL, 1, crc32_state);
68 if (!RETVAL) croak("FATAL: Newz failed");
7 crc32_init(RETVAL);
9 crc32_init(RETVAL); /* returns void */
10 }
811 OUTPUT:
912 RETVAL
1013
1720 reset(Crypt::Checksum::CRC32 self)
1821 PPCODE:
1922 {
20 crc32_init(self);
23 crc32_init(self); /* returns void */
2124 XPUSHs(ST(0)); /* return self */
2225 }
2326
3942 unsigned char *in;
4043 for(i=1; i<items; i++) {
4144 in = (unsigned char *)SvPVbyte(ST(i), inlen);
42 if (inlen>0) crc32_update(self, in, (unsigned long)inlen);
45 if (inlen > 0) {
46 crc32_update(self, in, (unsigned long)inlen); /* returns void */
47 }
4348 }
4449 XPUSHs(ST(0)); /* return self */
4550 }
4954 CODE:
5055 {
5156 unsigned char hash[4];
52 crc32_finish(self, hash, 4);
57 crc32_finish(self, hash, 4); /* returns void */
5358 RETVAL = newSVpvn((char *) hash, 4);
5459 }
5560 OUTPUT:
6267 unsigned long i;
6368 unsigned char hash[4];
6469 char hash_hex[4*2 + 1];
65 crc32_finish(self, hash, 4);
70 crc32_finish(self, hash, 4); /* returns void */
6671 hash_hex[0] = '\0';
6772 for(i=0; i<4; i++) sprintf(&hash_hex[2*i], "%02x", hash[i]);
6873 RETVAL = newSVpvn(hash_hex, strlen(hash_hex));
1515 key_data = (unsigned char *)SvPVbyte(key, key_len);
1616
1717 id = find_cipher(cipher_name);
18 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
18 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1919
2020 Newz(0, RETVAL, 1, struct cipher_struct);
2121 if (!RETVAL) croak("FATAL: Newz failed");
2323 RETVAL->id = id;
2424 RETVAL->desc = &cipher_descriptor[id];
2525 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));
26 if (rv != CRYPT_OK) {
27 Safefree(RETVAL);
28 croak("FATAL: cipher setup failed: %s", error_to_string(rv));
29 }
2730 }
2831 OUTPUT:
2932 RETVAL
134137 int rv, id;
135138
136139 id = find_cipher(cipher_name);
137 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
140 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
138141
139142 rv = cipher_descriptor[id].block_length;
140143 if (!rv) XSRETURN_UNDEF;
151154 int rv, id;
152155
153156 id = find_cipher(cipher_name);
154 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
157 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
155158
156159 rv = cipher_descriptor[id].min_key_length;
157160 if (!rv) XSRETURN_UNDEF;
168171 int rv, id;
169172
170173 id = find_cipher(cipher_name);
171 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
174 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
172175
173176 rv = cipher_descriptor[id].max_key_length;
174177 if (!rv) XSRETURN_UNDEF;
185188 int rv, id;
186189
187190 id = find_cipher(cipher_name);
188 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
191 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
189192
190193 rv = cipher_descriptor[id].default_rounds;
191194 if (!rv) XSRETURN_UNDEF;
88 int id;
99
1010 id = find_hash(digest_name);
11 if(id==-1) croak("FATAL: find_hash failed for '%s'", digest_name);
11 if (id == -1) croak("FATAL: find_hash failed for '%s'", digest_name);
1212
1313 Newz(0, RETVAL, 1, struct digest_struct);
1414 if (!RETVAL) croak("FATAL: Newz failed");
1616 RETVAL->id = id;
1717 RETVAL->desc = &hash_descriptor[id];
1818 rv = RETVAL->desc->init(&RETVAL->state);
19 if(rv!=CRYPT_OK) croak("FATAL: digest setup failed: %s", error_to_string(rv));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: digest setup failed: %s", error_to_string(rv));
22 }
2023 }
2124 OUTPUT:
2225 RETVAL
4346 Crypt::Digest self
4447 CODE:
4548 Newz(0, RETVAL, 1, struct digest_struct);
49 if (!RETVAL) croak("FATAL: Newz failed");
4650 Copy(&self->state, &RETVAL->state, 1, struct digest_struct);
4751 OUTPUT:
4852 RETVAL
159163 int rv, id;
160164
161165 id = find_hash(digest_name);
162 if(id==-1) croak("FATAL: find_digest failed for '%s'", digest_name);
166 if (id == -1) croak("FATAL: find_digest failed for '%s'", digest_name);
163167
164168 rv = hash_descriptor[id].hashsize;
165169 if (!rv) croak("FATAL: invalid hashsize for '%s'", digest_name);;
1010
1111 RETVAL->num = num;
1212 rv = sha3_shake_init(&RETVAL->state, RETVAL->num);
13 if (rv != CRYPT_OK) croak("FATAL: sha3_shake_init failed: %s", error_to_string(rv));
13 if (rv != CRYPT_OK) {
14 Safefree(RETVAL);
15 croak("FATAL: sha3_shake_init failed: %s", error_to_string(rv));
16 }
1417 }
1518 OUTPUT:
1619 RETVAL
3437 clone(Crypt::Digest::SHAKE self)
3538 CODE:
3639 Newz(0, RETVAL, 1, struct digest_shake_struct);
40 if (!RETVAL) croak("FATAL: Newz failed");
3741 Copy(&self->state, &RETVAL->state, 1, struct digest_shake_struct);
3842 OUTPUT:
3943 RETVAL
00 MODULE = CryptX PACKAGE = Crypt::KeyDerivation
11
22 SV *
3 _pkcs_5_alg1(SV * password, SV * salt, int iteration_count, char * hash_name, int len)
3 _pkcs_5_alg1(SV * password, SV * salt, int iteration_count, char * hash_name, unsigned long output_len)
44 CODE:
55 {
66 /*
1010 unsigned char *out, unsigned long *outlen)
1111 */
1212 int rv, id;
13 unsigned long output_len;
1413 unsigned char *output;
1514 unsigned char *password_ptr=NULL;
1615 STRLEN password_len=0;
1817 STRLEN salt_len=0;
1918
2019 id = find_hash(hash_name);
21 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
20 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
2221
2322 password_ptr = (unsigned char *)SvPVbyte(password, password_len);
2423 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
2524 if (salt_len < 8) croak("FATAL: salt_len has to be 8");
2625
27 output_len = len;
28 Newz(0, output, output_len, unsigned char);
29 if (!output) croak("FATAL: Newz failed [%ld]", output_len);
26 RETVAL = NEWSV(0, output_len);
27 SvPOK_only(RETVAL);
28 SvCUR_set(RETVAL, output_len);
29 output = (unsigned char *)SvPVX(RETVAL);
3030
3131 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);
32 if (rv != CRYPT_OK) {
33 SvREFCNT_dec(RETVAL);
34 croak("FATAL: pkcs_5_alg1 process failed: %s", error_to_string(rv));
35 }
36 SvCUR_set(RETVAL, output_len);
3637 }
3738 OUTPUT:
3839 RETVAL
3940
4041 SV *
41 _pkcs_5_alg2(SV * password, SV * salt, int iteration_count, char * hash_name, int len)
42 _pkcs_5_alg2(SV * password, SV * salt, int iteration_count, char * hash_name, unsigned long output_len)
4243 CODE:
4344 {
4445 /*
4849 unsigned char *out, unsigned long *outlen)
4950 */
5051 int rv, id;
51 unsigned long output_len;
5252 unsigned char *output;
5353 unsigned char *password_ptr=NULL;
5454 STRLEN password_len=0;
5656 STRLEN salt_len=0;
5757
5858 id = find_hash(hash_name);
59 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
59 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
6060
6161 password_ptr = (unsigned char *)SvPVbyte(password, password_len);
6262 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
6363
64 output_len = len;
65 Newz(0, output, output_len, unsigned char);
66 if (!output) croak("FATAL: Newz failed [%ld]", output_len);
64 RETVAL = NEWSV(0, output_len);
65 SvPOK_only(RETVAL);
66 SvCUR_set(RETVAL, output_len);
67 output = (unsigned char *)SvPVX(RETVAL);
6768
6869 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);
70 if (rv != CRYPT_OK) {
71 SvREFCNT_dec(RETVAL);
72 croak("FATAL: pkcs_5_alg2 process failed: %s", error_to_string(rv));
73 }
74 SvCUR_set(RETVAL, output_len);
7375 }
7476 OUTPUT:
7577 RETVAL
9294 STRLEN salt_len=0;
9395
9496 id = find_hash(hash_name);
95 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
97 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
9698
9799 in_ptr = (unsigned char *)SvPVbyte(in, in_len);
98100 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
123125 STRLEN info_len=0;
124126
125127 id = find_hash(hash_name);
126 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
128 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
127129
128130 in_ptr = (unsigned char *)SvPVbyte(in, in_len);
129131 info_ptr = (unsigned char *)SvPVbyte(info, info_len);
130132
131 Newz(0, output, output_len, unsigned char);
132 if (!output) croak("FATAL: Newz failed [%ld]", output_len);
133 RETVAL = NEWSV(0, output_len);
134 SvPOK_only(RETVAL);
135 SvCUR_set(RETVAL, output_len);
136 output = (unsigned char *)SvPVX(RETVAL);
133137
134138 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 if (rv != CRYPT_OK) {
140 SvREFCNT_dec(RETVAL);
141 croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
142 }
143 SvCUR_set(RETVAL, output_len);
139144 }
140145 OUTPUT:
141146 RETVAL
160165 STRLEN salt_len=0;
161166
162167 id = find_hash(hash_name);
163 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
168 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
164169
165 in_ptr = (unsigned char *)SvPVbyte(in, in_len);
170 in_ptr = (unsigned char *)SvPVbyte(in, in_len);
166171 info_ptr = (unsigned char *)SvPVbyte(info, info_len);
167172 salt_ptr = (unsigned char *)SvPVbyte(salt, salt_len);
168173
169 Newz(0, output, output_len, unsigned char);
170 if (!output) croak("FATAL: Newz failed [%ld]", output_len);
174 RETVAL = NEWSV(0, output_len);
175 SvPOK_only(RETVAL);
176 SvCUR_set(RETVAL, output_len);
177 output = (unsigned char *)SvPVX(RETVAL);
171178
172179 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);
180 if (rv != CRYPT_OK) {
181 SvREFCNT_dec(RETVAL);
182 croak("FATAL: hkdf_expand process failed: %s", error_to_string(rv));
183 }
184 SvCUR_set(RETVAL, output_len);
177185 }
178186 OUTPUT:
179187 RETVAL
180
1616 if (!RETVAL) croak("FATAL: Newz failed");
1717
1818 rv = blake2bmac_init(&RETVAL->state, size, k, (unsigned long)k_len);
19 if (rv != CRYPT_OK) croak("FATAL: blake2b_init failed: %s", error_to_string(rv));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: blake2b_init failed: %s", error_to_string(rv));
22 }
2023 }
2124 OUTPUT:
2225 RETVAL
1616 if (!RETVAL) croak("FATAL: Newz failed");
1717
1818 rv = blake2smac_init(&RETVAL->state, size, k, (unsigned long)k_len);
19 if (rv != CRYPT_OK) croak("FATAL: blake2s_init failed: %s", error_to_string(rv));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: blake2s_init failed: %s", error_to_string(rv));
22 }
2023 }
2124 OUTPUT:
2225 RETVAL
1111 int id;
1212
1313 id = find_cipher(cipher_name);
14 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
14 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1515
1616 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1717 k = (unsigned char *) SvPVbyte(key, k_len);
2020 if (!RETVAL) croak("FATAL: Newz failed");
2121
2222 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));
23 if (rv != CRYPT_OK) {
24 Safefree(RETVAL);
25 croak("FATAL: f9_init failed: %s", error_to_string(rv));
26 }
2427 }
2528 OUTPUT:
2629 RETVAL
1111 int id;
1212
1313 id = find_hash(hash_name);
14 if(id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
14 if (id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
1515
1616 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1717 k = (unsigned char *) SvPVbyte(key, k_len);
2020 if (!RETVAL) croak("FATAL: Newz failed");
2121
2222 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));
23 if (rv != CRYPT_OK) {
24 Safefree(RETVAL);
25 croak("FATAL: hmac_init failed: %s", error_to_string(rv));
26 }
2427 }
2528 OUTPUT:
2629 RETVAL
1111 int id;
1212
1313 id = find_cipher(cipher_name);
14 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
14 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1515
1616 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1717 k = (unsigned char *) SvPVbyte(key, k_len);
2020 if (!RETVAL) croak("FATAL: Newz failed");
2121
2222 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));
23 if (rv != CRYPT_OK) {
24 Safefree(RETVAL);
25 croak("FATAL: omac_init failed: %s", error_to_string(rv));
26 }
2427 }
2528 OUTPUT:
2629 RETVAL
1111 int id;
1212
1313 id = find_cipher(cipher_name);
14 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
14 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1515
1616 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1717 k = (unsigned char *) SvPVbyte(key, k_len);
2020 if (!RETVAL) croak("FATAL: Newz failed");
2121
2222 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));
23 if (rv != CRYPT_OK) {
24 Safefree(RETVAL);
25 croak("FATAL: pmac_init failed: %s", error_to_string(rv));
26 }
2427 }
2528 OUTPUT:
2629 RETVAL
1616 if (!RETVAL) croak("FATAL: Newz failed");
1717
1818 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));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: pelican_init failed: %s", error_to_string(rv));
22 }
2023 }
2124 OUTPUT:
2225 RETVAL
1616 if (!RETVAL) croak("FATAL: Newz failed");
1717
1818 rv = poly1305_init(&RETVAL->state, k, (unsigned long)k_len);
19 if (rv != CRYPT_OK) croak("FATAL: poly1305_init failed: %s", error_to_string(rv));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: poly1305_init failed: %s", error_to_string(rv));
22 }
2023 }
2124 OUTPUT:
2225 RETVAL
1111 int id;
1212
1313 id = find_cipher(cipher_name);
14 if(id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
14 if (id == -1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
1515
1616 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1717 k = (unsigned char *) SvPVbyte(key, k_len);
2020 if (!RETVAL) croak("FATAL: Newz failed");
2121
2222 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));
23 if (rv != CRYPT_OK) {
24 Safefree(RETVAL);
25 croak("FATAL: xcbc_init failed: %s", error_to_string(rv));
26 }
2427 }
2528 OUTPUT:
2629 RETVAL
1212 RETVAL->direction = 0;
1313 RETVAL->cipher_rounds = rounds;
1414 RETVAL->cipher_id = find_cipher(cipher_name);
15 if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
15 if (RETVAL->cipher_id == -1) {
16 Safefree(RETVAL);
17 croak("FATAL: find_cipfer failed for '%s'", cipher_name);
18 }
1619 }
1720 OUTPUT:
1821 RETVAL
7275 in_data_start = 0;
7376 has_tmp_block = 0;
7477 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
75 if (in_data_len==0) {
78 if (in_data_len == 0) {
7679 RETVAL = newSVpvn("", 0);
7780 }
7881 else {
79 if(self->padlen > 0) {
82 if (self->padlen > 0) {
8083 i = (blen - self->padlen);
8184 if (in_data_len >= i) { /* enough data to fill pad */
8285 Copy(in_data, self->pad+self->padlen, i, unsigned char);
101104 in_data_len -= i;
102105 }
103106
104 if (in_data_len>0) {
107 if (in_data_len > 0) {
105108 i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len);
106109 RETVAL = NEWSV(0, i);
107110 SvPOK_only(RETVAL);
112115 out_data += blen;
113116 }
114117 rv = cbc_encrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state);
115 if (rv != CRYPT_OK) croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv));
118 if (rv != CRYPT_OK) {
119 SvREFCNT_dec(RETVAL);
120 croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv));
121 }
116122 } /* in_data_len>0 */
117123 else if (has_tmp_block) {
118124 RETVAL = newSVpvn((char*)tmp_block, blen);
135141 blen = (&self->state)->blocklen;
136142 if (self->padlen<0 || self->padlen>=blen) croak("FATAL: invalid padlen");
137143
138 if(self->padding_mode == 1) { /* pkcs5|7 padding */
144 if (self->padding_mode == 1) { /* pkcs5|7 padding */
139145 i = blen - self->padlen;
140146 if (i == 0) i = blen;
141147 for(j=self->padlen; j<blen; j++) self->pad[j] = (unsigned char)i;
142148 rv = cbc_encrypt(self->pad, tmp_block, blen, &self->state);
143149 if (rv != CRYPT_OK) croak("FATAL: cbc_encrypt failed: %s", error_to_string(rv));
144150 }
145 else if(self->padding_mode == 2) { /* oneandzeroes padding */
151 else if (self->padding_mode == 2) { /* oneandzeroes padding */
146152 self->pad[self->padlen] = 0x80;
147153 for(j=self->padlen+1; j<blen; j++) self->pad[j] = 0;
148154 rv = cbc_encrypt(self->pad, tmp_block, blen, &self->state);
174180 in_data_start = 0;
175181 has_tmp_block = 0;
176182 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
177 if (in_data_len==0) {
183 if (in_data_len == 0) {
178184 RETVAL = newSVpvn("", 0);
179185 }
180186 else {
181187
182 if(self->padlen == blen) {
188 if (self->padlen == blen) {
183189 rv = cbc_decrypt(self->pad, tmp_block, blen, &self->state);
184190 if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv));
185191 self->padlen = 0;
186192 has_tmp_block = 1;
187193 } /* padlen == blen */
188 else if(self->padlen > 0) {
194 else if (self->padlen > 0) {
189195 i = (blen - self->padlen); /* remaining bytes in padding buffer */
190196 if (in_data_len >= i) { /* enough data to fill pad */
191197 Copy(in_data, self->pad+self->padlen, i, unsigned char);
206212 }
207213 } /* padlen > 0 */
208214
209 /* here: a/ padlen==1..16 && in_data_len==0; b/ padlen==0 && in_data_len>0 */
215 /* here: a/ padlen == 1..16 && in_data_len == 0; b/ padlen == 0 && in_data_len > 0 */
210216 if (in_data_len>0) {
211217 i = (unsigned long)(in_data_len % blen);
212218 if (i>0) { /* save tail of data into pad */
217223 }
218224
219225 if (in_data_len>0) {
220 if(self->padlen == 0 && self->padding_mode !=0) {
226 if (self->padlen == 0 && self->padding_mode !=0) {
221227 /* in case of padding keep full pad if no more data */
222228 Copy(in_data+in_data_start+in_data_len-blen, self->pad, blen, unsigned char);
223229 self->padlen = blen;
237243 out_data += blen;
238244 }
239245 rv = cbc_decrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state);
240 if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv));
246 if (rv != CRYPT_OK) {
247 SvREFCNT_dec(RETVAL);
248 croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv));
249 }
241250 }
242251 } /* in_data_len>0 */
243252 else if (has_tmp_block) {
266275 if (self->padlen != blen) croak("FATAL: cipher text length has to be multiple of %d (%d)", blen, self->padlen);
267276 rv = cbc_decrypt(self->pad, tmp_block, blen, &self->state);
268277 if (rv != CRYPT_OK) croak("FATAL: cbc_decrypt failed: %s", error_to_string(rv));
269 if(self->padding_mode == 0) { /* no padding */
278 if (self->padding_mode == 0) { /* no padding */
270279 rv_len = blen;
271280 }
272 else if(self->padding_mode == 1) { /* pkcs5|7 padding */
281 else if (self->padding_mode == 1) { /* pkcs5|7 padding */
273282 i = tmp_block[blen-1];
274283 rv_len = blen - (i>blen ? blen : i);
275284 }
276 else if(self->padding_mode == 2) { /* oneandzeroes padding */
285 else if (self->padding_mode == 2) { /* oneandzeroes padding */
277286 rv_len = blen;
278287 while ((unsigned char)tmp_block[rv_len-1] == 0x00) rv_len--;
279288 if ((unsigned char)tmp_block[rv_len-1] == 0x80) rv_len--;
1010 RETVAL->direction = 0;
1111 RETVAL->cipher_rounds = rounds;
1212 RETVAL->cipher_id = find_cipher(cipher_name);
13 if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
13 if (RETVAL->cipher_id == -1) {
14 Safefree(RETVAL);
15 croak("FATAL: find_cipfer failed for '%s'", cipher_name);
16 }
1417 }
1518 OUTPUT:
1619 RETVAL
1010 RETVAL->direction = 0;
1111 RETVAL->cipher_rounds = rounds;
1212 RETVAL->cipher_id = find_cipher(cipher_name);
13 if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
13 if (RETVAL->cipher_id == -1) {
14 Safefree(RETVAL);
15 croak("FATAL: find_cipfer failed for '%s'", cipher_name);
16 }
1417 if (ctr_mode == 0) RETVAL->ctr_mode_param = CTR_COUNTER_LITTLE_ENDIAN;
1518 if (ctr_mode == 1) RETVAL->ctr_mode_param = CTR_COUNTER_BIG_ENDIAN;
1619 if (ctr_mode == 2) RETVAL->ctr_mode_param = CTR_COUNTER_LITTLE_ENDIAN|LTC_CTR_RFC3686;
1212 RETVAL->direction = 0;
1313 RETVAL->cipher_rounds = rounds;
1414 RETVAL->cipher_id = find_cipher(cipher_name);
15 if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
15 if (RETVAL->cipher_id == -1) {
16 Safefree(RETVAL);
17 croak("FATAL: find_cipfer failed for '%s'", cipher_name);
18 }
1619 }
1720 OUTPUT:
1821 RETVAL
6568 in_data_start = 0;
6669 has_tmp_block = 0;
6770 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
68 if (in_data_len==0) {
71 if (in_data_len == 0) {
6972 RETVAL = newSVpvn("", 0);
7073 }
7174 else {
72 if(self->padlen > 0) {
75 if (self->padlen > 0) {
7376 i = (blen - self->padlen);
7477 if (in_data_len >= i) { /* enough data to fill pad */
7578 Copy(in_data, self->pad+self->padlen, i, unsigned char);
9497 in_data_len -= i;
9598 }
9699
97 if (in_data_len>0) {
100 if (in_data_len > 0) {
98101 i = (unsigned long)(has_tmp_block ? in_data_len + blen : in_data_len);
99102 RETVAL = NEWSV(0, i);
100103 SvPOK_only(RETVAL);
105108 out_data += blen;
106109 }
107110 rv = ecb_encrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state);
108 if (rv != CRYPT_OK) croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv));
111 if (rv != CRYPT_OK) {
112 SvREFCNT_dec(RETVAL);
113 croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv));
114 }
109115 } /* in_data_len>0 */
110116 else if (has_tmp_block) {
111117 RETVAL = newSVpvn((char*)tmp_block, blen);
128134 blen = (&self->state)->blocklen;
129135 if (self->padlen<0 || self->padlen>=blen) croak("FATAL: invalid padlen");
130136
131 if(self->padding_mode == 1) { /* pkcs5|7 padding */
137 if (self->padding_mode == 1) { /* pkcs5|7 padding */
132138 i = blen - self->padlen;
133139 if (i == 0) i = blen;
134140 for(j=self->padlen; j<blen; j++) self->pad[j] = (unsigned char)i;
135141 rv = ecb_encrypt(self->pad, tmp_block, blen, &self->state);
136142 if (rv != CRYPT_OK) croak("FATAL: ecb_encrypt failed: %s", error_to_string(rv));
137143 }
138 else if(self->padding_mode == 2) { /* oneandzeroes padding */
144 else if (self->padding_mode == 2) { /* oneandzeroes padding */
139145 self->pad[self->padlen] = 0x80;
140146 for(j=self->padlen+1; j<blen; j++) self->pad[j] = 0;
141147 rv = ecb_encrypt(self->pad, tmp_block, blen, &self->state);
167173 in_data_start = 0;
168174 has_tmp_block = 0;
169175 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
170 if (in_data_len==0) {
176 if (in_data_len == 0) {
171177 RETVAL = newSVpvn("", 0);
172178 }
173179 else {
174180
175 if(self->padlen == blen) {
181 if (self->padlen == blen) {
176182 rv = ecb_decrypt(self->pad, tmp_block, blen, &self->state);
177183 if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv));
178184 self->padlen = 0;
179185 has_tmp_block = 1;
180186 } /* padlen == blen */
181 else if(self->padlen > 0) {
187 else if (self->padlen > 0) {
182188 i = (blen - self->padlen); /* remaining bytes in padding buffer */
183189 if (in_data_len >= i) { /* enough data to fill pad */
184190 Copy(in_data, self->pad+self->padlen, i, unsigned char);
199205 }
200206 } /* padlen > 0 */
201207
202 /* here: a/ padlen==1..16 && in_data_len==0; b/ padlen==0 && in_data_len>0 */
208 /* here: a/ padlen == 1..16 && in_data_len == 0; b/ padlen == 0 && in_data_len > 0 */
203209 if (in_data_len>0) {
204210 i = (unsigned long)(in_data_len % blen);
205211 if (i>0) { /* save tail of data into pad */
210216 }
211217
212218 if (in_data_len>0) {
213 if(self->padlen == 0 && self->padding_mode !=0) {
219 if (self->padlen == 0 && self->padding_mode !=0) {
214220 /* in case of padding keep full pad if no more data */
215221 Copy(in_data+in_data_start+in_data_len-blen, self->pad, blen, unsigned char);
216222 self->padlen = blen;
230236 out_data += blen;
231237 }
232238 rv = ecb_decrypt(in_data+in_data_start, out_data, (unsigned long)in_data_len, &self->state);
233 if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv));
239 if (rv != CRYPT_OK) {
240 SvREFCNT_dec(RETVAL);
241 croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv));
242 }
234243 }
235244 } /* in_data_len>0 */
236245 else if (has_tmp_block) {
259268 if (self->padlen != blen) croak("FATAL: cipher text length has to be multiple of %d (%d)", blen, self->padlen);
260269 rv = ecb_decrypt(self->pad, tmp_block, blen, &self->state);
261270 if (rv != CRYPT_OK) croak("FATAL: ecb_decrypt failed: %s", error_to_string(rv));
262 if(self->padding_mode == 0) { /* no padding */
271 if (self->padding_mode == 0) { /* no padding */
263272 rv_len = blen;
264273 }
265 else if(self->padding_mode == 1) { /* pkcs5|7 padding */
274 else if (self->padding_mode == 1) { /* pkcs5|7 padding */
266275 i = tmp_block[blen-1];
267276 rv_len = blen - (i>blen ? blen : i);
268277 }
269 else if(self->padding_mode == 2) { /* oneandzeroes padding */
278 else if (self->padding_mode == 2) { /* oneandzeroes padding */
270279 rv_len = blen;
271280 while ((unsigned char)tmp_block[rv_len-1] == 0x00) rv_len--;
272281 if ((unsigned char)tmp_block[rv_len-1] == 0x80) rv_len--;
1010 RETVAL->direction = 0;
1111 RETVAL->cipher_rounds = rounds;
1212 RETVAL->cipher_id = find_cipher(cipher_name);
13 if(RETVAL->cipher_id==-1) croak("FATAL: find_cipfer failed for '%s'", cipher_name);
13 if (RETVAL->cipher_id == -1) {
14 Safefree(RETVAL);
15 croak("FATAL: find_cipfer failed for '%s'", cipher_name);
16 }
1417 }
1518 OUTPUT:
1619 RETVAL
88 if (!RETVAL) croak("FATAL: Newz failed");
99 RETVAL->key.type = -1;
1010 RETVAL->pindex = find_prng("chacha20");
11 if(RETVAL->pindex==-1) croak("FATAL: find_prng('chacha20') failed");
11 if (RETVAL->pindex == -1) {
12 Safefree(RETVAL);
13 croak("FATAL: find_prng('chacha20') failed");
14 }
1215 rv = rng_make_prng(320, RETVAL->pindex, &RETVAL->pstate, NULL); /* 320bits = 40bytes */
13 if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
16 if (rv != CRYPT_OK) {
17 Safefree(RETVAL);
18 croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
19 }
1420 }
1521 OUTPUT:
1622 RETVAL
148154 CODE:
149155 if (self->key.type == -1) XSRETURN_UNDEF;
150156 rv_hash = newHV();
151 /* =====> x */
157 /* x */
152158 siz = (self->key.x) ? mp_unsigned_bin_size(self->key.x) : 0;
153159 if (siz>10000) {
154160 croak("FATAL: key2hash failed - 'x' too big number");
160166 else{
161167 not_used = hv_store(rv_hash, "x", 1, newSVpv("", 0), 0);
162168 }
163 /* =====> y */
169 /* y */
164170 siz = (self->key.y) ? mp_unsigned_bin_size(self->key.y) : 0;
165171 if (siz>10000) {
166172 croak("FATAL: key2hash failed - 'y' too big number");
172178 else{
173179 not_used = hv_store(rv_hash, "y", 1, newSVpv("", 0), 0);
174180 }
175 /* =====> p */
181 /* p */
176182 siz = (self->key.prime) ? mp_unsigned_bin_size(self->key.prime) : 0;
177183 if (siz>10000) {
178184 croak("FATAL: key2hash failed - 'p' too big number");
185191 not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0);
186192 }
187193
188 /* =====> g */
194 /* g */
189195 siz = (self->key.base) ? mp_unsigned_bin_size(self->key.base) : 0;
190196 if (siz>10000) {
191197 croak("FATAL: key2hash failed - 'g' too big number");
197203 else {
198204 not_used = hv_store(rv_hash, "g", 1, newSVpv("", 0), 0);
199205 }
200 /* =====> size */
206 /* size */
201207 not_used = hv_store(rv_hash, "size", 4, newSViv(dh_get_groupsize(&self->key)), 0);
202 /* =====> type */
208 /* type */
203209 not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0);
204210 if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */
205211 RETVAL = newRV_noinc((SV*)rv_hash);
216222 CODE:
217223 if (self->key.type == -1) XSRETURN_UNDEF;
218224 rv_hash = newHV();
219 /* =====> p */
225 /* p */
220226 siz = (self->key.prime) ? mp_unsigned_bin_size(self->key.prime) : 0;
221227 if (siz>10000) {
222228 croak("FATAL: key2hash failed - 'p' too big number");
229235 not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0);
230236 }
231237
232 /* =====> g */
238 /* g */
233239 siz = (self->key.base) ? mp_unsigned_bin_size(self->key.base) : 0;
234240 if (siz>10000) {
235241 croak("FATAL: key2hash failed - 'g' too big number");
88 if (!RETVAL) croak("FATAL: Newz failed");
99 RETVAL->key.type = -1;
1010 RETVAL->pindex = find_prng("chacha20");
11 if(RETVAL->pindex==-1) croak("FATAL: find_prng('chacha20') failed");
11 if (RETVAL->pindex == -1) {
12 Safefree(RETVAL);
13 croak("FATAL: find_prng('chacha20') failed");
14 }
1215 rv = rng_make_prng(320, RETVAL->pindex, &RETVAL->pstate, NULL); /* 320bits = 40bytes */
13 if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
16 if (rv != CRYPT_OK) {
17 Safefree(RETVAL);
18 croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
19 }
1420 }
1521 OUTPUT:
1622 RETVAL
158164 qsize = mp_unsigned_bin_size(self->key.q);
159165 psize = mp_unsigned_bin_size(self->key.p);
160166 rv_hash = newHV();
161 /* =====> g */
167 /* g */
162168 siz = (self->key.g) ? mp_unsigned_bin_size(self->key.g) : 0;
163169 if (siz>10000) {
164170 croak("FATAL: key2hash failed - 'g' too big number");
170176 else{
171177 not_used = hv_store(rv_hash, "g", 1, newSVpv("", 0), 0);
172178 }
173 /* =====> q */
179 /* q */
174180 siz = (self->key.q) ? mp_unsigned_bin_size(self->key.q) : 0;
175181 if (siz>10000) {
176182 croak("FATAL: key2hash failed - 'q' too big number");
182188 else{
183189 not_used = hv_store(rv_hash, "q", 1, newSVpv("", 0), 0);
184190 }
185 /* =====> p */
191 /* p */
186192 siz = (self->key.p) ? mp_unsigned_bin_size(self->key.p) : 0;
187193 if (siz>10000) {
188194 croak("FATAL: key2hash failed - 'p' too big number");
194200 else{
195201 not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0);
196202 }
197 /* =====> x */
203 /* x */
198204 siz = (self->key.x) ? mp_unsigned_bin_size(self->key.x) : 0;
199205 if (siz>10000) {
200206 croak("FATAL: key2hash failed - 'x' too big number");
206212 else{
207213 not_used = hv_store(rv_hash, "x", 1, newSVpv("", 0), 0);
208214 }
209 /* =====> y */
215 /* y */
210216 siz = (self->key.y) ? mp_unsigned_bin_size(self->key.y) : 0;
211217 if (siz>10000) {
212218 croak("FATAL: key2hash failed - 'y' too big number");
218224 else{
219225 not_used = hv_store(rv_hash, "y", 1, newSVpv("", 0), 0);
220226 }
221 /* =====> size */
227 /* size */
222228 not_used = hv_store(rv_hash, "size", 4, newSViv(qsize), 0);
223 /* =====> type */
229 /* type */
224230 not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0);
225231 if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */
226232 RETVAL = newRV_noinc((SV*)rv_hash);
266272 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
267273
268274 hash_id = find_hash(hash_name);
269 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
275 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
270276 rv = dsa_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
271277 &self->pstate, self->pindex,
272278 hash_id, &self->key);
99 RETVAL->pindex = find_prng("chacha20");
1010 RETVAL->key.type = -1;
1111 ecc_dp_init(&RETVAL->dp);
12 if(RETVAL->pindex==-1) croak("FATAL: find_prng('chacha20') failed");
12 if (RETVAL->pindex == -1) {
13 Safefree(RETVAL);
14 croak("FATAL: find_prng('chacha20') failed");
15 }
1316 rv = rng_make_prng(320, RETVAL->pindex, &RETVAL->pstate, NULL); /* 320bits = 40bytes */
14 if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
17 if (rv != CRYPT_OK) {
18 Safefree(RETVAL);
19 croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
20 }
1521 }
1622 OUTPUT:
1723 RETVAL
107113 if (self->key.type == -1) XSRETURN_UNDEF;
108114 esize = ecc_get_size(&self->key);
109115 rv_hash = newHV();
110 /* =====> k */
116 /* k */
111117 siz = (self->key.k) ? mp_unsigned_bin_size(self->key.k) : 0;
112118 if (siz>10000) {
113119 croak("FATAL: key2hash failed - 'k' too big number");
119125 else{
120126 not_used = hv_store(rv_hash, "k", 1, newSVpv("", 0), 0);
121127 }
122 /* =====> pub_x */
128 /* pub_x */
123129 siz = (self->key.pubkey.x) ? mp_unsigned_bin_size(self->key.pubkey.x) : 0;
124130 if (siz>10000) {
125131 croak("FATAL: key2hash failed - 'pub_x' too big number");
131137 else{
132138 not_used = hv_store(rv_hash, "pub_x", 5, newSVpv("", 0), 0);
133139 }
134 /* =====> pub_y */
140 /* pub_y */
135141 siz = (self->key.pubkey.y) ? mp_unsigned_bin_size(self->key.pubkey.y) : 0;
136142 if (siz>10000) {
137143 croak("FATAL: key2hash failed - 'pub_y' too big number");
143149 else{
144150 not_used = hv_store(rv_hash, "pub_y", 5, newSVpv("", 0), 0);
145151 }
146 /* =====> curve_... */
152 /* curve_... */
147153 if (self->key.dp) {
148154 not_used = hv_store(rv_hash, "curve_cofactor", 14, newSViv(self->key.dp->cofactor), 0);
149155 /* prepend leading zero if we have odd number of hexadecimal digits */
194200 not_used = hv_store(rv_hash, "curve_oid", 9, oid, 0);
195201 }
196202 }
197 /* =====> size */
203 /* size */
198204 not_used = hv_store(rv_hash, "size", 4, newSViv(esize), 0);
199 /* =====> type */
205 /* type */
200206 not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0);
201207 if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */
202208 RETVAL = newRV_noinc((SV*)rv_hash);
283289 data_ptr = (unsigned char *)SvPVbyte(data, data_len);
284290
285291 hash_id = find_hash(hash_name);
286 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
292 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
287293 rv = ecc_encrypt_key(data_ptr, (unsigned long)data_len, buffer, &buffer_len,
288294 &self->pstate, self->pindex,
289295 hash_id, &self->key);
88 if (!RETVAL) croak("FATAL: Newz failed");
99 RETVAL->key.type = -1;
1010 RETVAL->pindex = find_prng("chacha20");
11 if(RETVAL->pindex==-1) croak("FATAL: find_prng('chacha20') failed");
11 if (RETVAL->pindex == -1) {
12 Safefree(RETVAL);
13 croak("FATAL: find_prng('chacha20') failed");
14 }
1215 rv = rng_make_prng(320, RETVAL->pindex, &RETVAL->pstate, NULL); /* 320bits = 40bytes */
13 if (rv != CRYPT_OK) croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
16 if (rv != CRYPT_OK) {
17 Safefree(RETVAL);
18 croak("FATAL: rng_make_prng failed: %s", error_to_string(rv));
19 }
1420 }
1521 OUTPUT:
1622 RETVAL
154160 if (self->key.type == -1 || self->key.N == NULL) XSRETURN_UNDEF;
155161 nsize = mp_unsigned_bin_size(self->key.N);
156162 rv_hash = newHV();
157 /* =====> e */
163 /* e */
158164 siz = (self->key.e) ? mp_unsigned_bin_size(self->key.e) : 0;
159165 if (siz>10000) {
160166 croak("FATAL: key2hash failed - 'e' too big number");
166172 else{
167173 not_used = hv_store(rv_hash, "e", 1, newSVpv("", 0), 0);
168174 }
169 /* =====> d */
175 /* d */
170176 siz = (self->key.d) ? mp_unsigned_bin_size(self->key.d) : 0;
171177 if (siz>10000) {
172178 croak("FATAL: key2hash failed - 'd' too big number");
178184 else{
179185 not_used = hv_store(rv_hash, "d", 1, newSVpv("", 0), 0);
180186 }
181 /* =====> N */
187 /* N */
182188 siz = (self->key.N) ? nsize : 0;
183189 if (siz>10000) {
184190 croak("FATAL: key2hash failed - 'N' too big number");
190196 else{
191197 not_used = hv_store(rv_hash, "N", 1, newSVpv("", 0), 0);
192198 }
193 /* =====> q */
199 /* q */
194200 siz = (self->key.q) ? mp_unsigned_bin_size(self->key.q) : 0;
195201 if (siz>10000) {
196202 croak("FATAL: key2hash failed - 'q' too big number");
202208 else{
203209 not_used = hv_store(rv_hash, "q", 1, newSVpv("", 0), 0);
204210 }
205 /* =====> p */
211 /* p */
206212 siz = (self->key.p) ? mp_unsigned_bin_size(self->key.p) : 0;
207213 if (siz>10000) {
208214 croak("FATAL: key2hash failed - 'p' too big number");
214220 else{
215221 not_used = hv_store(rv_hash, "p", 1, newSVpv("", 0), 0);
216222 }
217 /* =====> qP */
223 /* qP */
218224 siz = (self->key.qP) ? mp_unsigned_bin_size(self->key.qP) : 0;
219225 if (siz>10000) {
220226 croak("FATAL: key2hash failed - 'qP' too big number");
226232 else{
227233 not_used = hv_store(rv_hash, "qP", 2, newSVpv("", 0), 0);
228234 }
229 /* =====> dP */
235 /* dP */
230236 siz = (self->key.dP) ? mp_unsigned_bin_size(self->key.dP) : 0;
231237 if (siz>10000) {
232238 croak("FATAL: key2hash failed - 'dP' too big number");
238244 else{
239245 not_used = hv_store(rv_hash, "dP", 2, newSVpv("", 0), 0);
240246 }
241 /* =====> dQ */
247 /* dQ */
242248 siz = (self->key.dQ) ? mp_unsigned_bin_size(self->key.dQ) : 0;
243249 if (siz>10000) {
244250 croak("FATAL: key2hash failed - 'dQ' too big number");
250256 else{
251257 not_used = hv_store(rv_hash, "dQ", 2, newSVpv("", 0), 0);
252258 }
253 /* =====> size */
259 /* size */
254260 not_used = hv_store(rv_hash, "size", 4, newSViv(nsize), 0);
255 /* =====> type */
261 /* type */
256262 not_used = hv_store(rv_hash, "type", 4, newSViv(self->key.type), 0);
257263 if (not_used) not_used = NULL; /* just silence the warning: variable 'not_used' set but not used */
258264 RETVAL = newRV_noinc((SV*)rv_hash);
302308 RETVAL = newSVpvn(NULL, 0); /* undef */
303309 if (strnEQ(padding, "oaep", 4)) {
304310 hash_id = find_hash(oaep_hash);
305 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", oaep_hash);
311 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", oaep_hash);
306312 lparam_ptr = (unsigned char *)SvPVbyte(oaep_lparam, lparam_len);
307313 rv = rsa_encrypt_key_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, lparam_ptr, (unsigned long)lparam_len,
308314 &self->pstate, self->pindex,
347353 RETVAL = newSVpvn(NULL, 0); /* undef */
348354 if (strnEQ(padding, "oaep", 4)) {
349355 hash_id = find_hash(oaep_hash);
350 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", oaep_hash);
356 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", oaep_hash);
351357 lparam_ptr = (unsigned char *)SvPVbyte(oaep_lparam, lparam_len);
352358 rv = rsa_decrypt_key_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, lparam_ptr, (unsigned long)lparam_len,
353359 hash_id, LTC_PKCS_1_OAEP, &stat, &self->key);
390396 RETVAL = newSVpvn(NULL, 0); /* undef */
391397 if (strnEQ(padding, "pss", 3)) {
392398 hash_id = find_hash(hash_name);
393 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
399 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
394400 rv = rsa_sign_hash_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, LTC_PKCS_1_PSS,
395401 &self->pstate, self->pindex,
396402 hash_id, saltlen, &self->key);
399405 }
400406 else if (strnEQ(padding, "v1.5", 4)) {
401407 hash_id = find_hash(hash_name);
402 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
408 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
403409 rv = rsa_sign_hash_ex(data_ptr, (unsigned long)data_len, buffer, &buffer_len, LTC_PKCS_1_V1_5,
404410 &self->pstate, self->pindex,
405411 hash_id, 0, &self->key);
438444 stat = 0;
439445 if (strnEQ(padding, "pss", 3)) {
440446 hash_id = find_hash(hash_name);
441 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
447 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
442448 rv = rsa_verify_hash_ex(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, LTC_PKCS_1_PSS,
443449 hash_id, saltlen, &stat, &self->key);
444450 if (rv != CRYPT_OK || stat != 1) RETVAL = 0;
445451 }
446452 else if (strnEQ(padding, "v1.5", 4)) {
447453 hash_id = find_hash(hash_name);
448 if(hash_id==-1) croak("FATAL: find_hash failed for '%s'", hash_name);
454 if (hash_id == -1) croak("FATAL: find_hash failed for '%s'", hash_name);
449455 rv = rsa_verify_hash_ex(sig_ptr, (unsigned long)sig_len, data_ptr, (unsigned long)data_len, LTC_PKCS_1_V1_5,
450456 hash_id, 0, &stat, &self->key);
451457 if (rv != CRYPT_OK || stat != 1) RETVAL = 0;
1212 if (!RETVAL) croak("FATAL: Newz failed");
1313
1414 id = find_prng(prng_name);
15 if(id==-1) croak("FATAL: find_prng failed for '%s'", prng_name);
15 if (id == -1) {
16 Safefree(RETVAL);
17 croak("FATAL: find_prng failed for '%s'", prng_name);
18 }
1619 RETVAL->id = id;
1720 RETVAL->last_pid = curpid;
1821 RETVAL->desc = &prng_descriptor[id];
1922
2023 rv = RETVAL->desc->start(&RETVAL->state);
21 if (rv != CRYPT_OK) croak("FATAL: PRNG_start failed: %s", error_to_string(rv));
24 if (rv != CRYPT_OK) {
25 Safefree(RETVAL);
26 croak("FATAL: PRNG_start failed: %s", error_to_string(rv));
27 }
2228
23 if(SvOK(entropy)) {
29 if (SvOK(entropy)) {
2430 ent = (unsigned char *) SvPVbyte(entropy, ent_len);
2531 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));
32 if (rv != CRYPT_OK) {
33 Safefree(RETVAL);
34 croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv));
35 }
2736 }
2837 else {
29 if (rng_get_bytes(entropy_buf, 40, NULL) != 40) croak("FATAL: rng_get_bytes failed: %s", error_to_string(rv));
38 if (rng_get_bytes(entropy_buf, 40, NULL) != 40) {
39 Safefree(RETVAL);
40 croak("FATAL: rng_get_bytes failed: %s", error_to_string(rv));
41 }
3042 rv = RETVAL->desc->add_entropy(entropy_buf, 40, &RETVAL->state);
31 if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv));
43 if (rv != CRYPT_OK) {
44 Safefree(RETVAL);
45 croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv));
46 }
3247 }
3348 rv = RETVAL->desc->ready(&RETVAL->state);
34 if (rv != CRYPT_OK) croak("FATAL: PRNG_ready failed: %s", error_to_string(rv));
49 if (rv != CRYPT_OK) {
50 Safefree(RETVAL);
51 croak("FATAL: PRNG_ready failed: %s", error_to_string(rv));
52 }
3553 }
3654 OUTPUT:
3755 RETVAL
5169 unsigned char *in_buffer=NULL;
5270 unsigned char entropy_buf[32];
5371 int rv;
54 if(SvOK(entropy)) {
72 if (SvOK(entropy)) {
5573 in_buffer = (unsigned char *) SvPVbyte(entropy, in_len);
5674 rv = self->desc->add_entropy(in_buffer, (unsigned long)in_len, &self->state);
5775 if (rv != CRYPT_OK) croak("FATAL: PRNG_add_entropy failed: %s", error_to_string(rv));
1616 if (!RETVAL) croak("FATAL: Newz failed");
1717
1818 rv = chacha_setup(&RETVAL->state, k, (unsigned long)k_len, rounds);
19 if (rv != CRYPT_OK) croak("FATAL: chacha_setup failed: %s", error_to_string(rv));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: chacha_setup failed: %s", error_to_string(rv));
22 }
2023
2124 if (iv_len == 12) {
2225 rv = chacha_ivctr32(&RETVAL->state, iv, (unsigned long)iv_len, (ulong32)counter);
23 if (rv != CRYPT_OK) croak("FATAL: chacha_ivctr32 failed: %s", error_to_string(rv));
26 if (rv != CRYPT_OK) {
27 Safefree(RETVAL);
28 croak("FATAL: chacha_ivctr32 failed: %s", error_to_string(rv));
29 }
2430 }
2531 else if (iv_len == 8) {
2632 rv = chacha_ivctr64(&RETVAL->state, iv, (unsigned long)iv_len, (ulong64)counter);
27 if (rv != CRYPT_OK) croak("FATAL: chacha_ivctr64 failed: %s", error_to_string(rv));
33 if (rv != CRYPT_OK) {
34 Safefree(RETVAL);
35 croak("FATAL: chacha_ivctr64 failed: %s", error_to_string(rv));
36 }
2837 }
2938 else {
39 Safefree(RETVAL);
3040 croak("FATAL: chacha IV length must be 8 or 12 bytes");
3141 }
3242 }
7787 unsigned char *in_data, *out_data;
7888
7989 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
80 if (in_data_len==0) {
90 if (in_data_len == 0) {
8191 RETVAL = newSVpvn("", 0);
8292 }
8393 else {
1414 if (!RETVAL) croak("FATAL: Newz failed");
1515
1616 rv = rc4_stream_setup(&RETVAL->state, k, (unsigned long)k_len);
17 if (rv != CRYPT_OK) croak("FATAL: rc4_stream_setup failed: %s", error_to_string(rv));
17 if (rv != CRYPT_OK) {
18 Safefree(RETVAL);
19 croak("FATAL: rc4_stream_setup failed: %s", error_to_string(rv));
20 }
1821 }
1922 OUTPUT:
2023 RETVAL
6366 unsigned char *in_data, *out_data;
6467
6568 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
66 if (in_data_len==0) {
69 if (in_data_len == 0) {
6770 RETVAL = newSVpvn("", 0);
6871 }
6972 else {
99
1010 if (!SvPOK(key)) croak("FATAL: key must be string/buffer scalar");
1111 if (!SvPOK(nonce)) croak("FATAL: nonce must be string/buffer scalar");
12 k = (unsigned char *) SvPVbyte(key, k_len);
13 iv = (unsigned char *) SvPVbyte(nonce, iv_len);
12 k = (unsigned char *)SvPVbyte(key, k_len);
13 iv = (unsigned char *)SvPVbyte(nonce, iv_len);
1414
1515 Newz(0, RETVAL, 1, struct salsa20_struct);
1616 if (!RETVAL) croak("FATAL: Newz failed");
1717
1818 rv = salsa20_setup(&RETVAL->state, k, (unsigned long)k_len, rounds);
19 if (rv != CRYPT_OK) croak("FATAL: salsa20_setup failed: %s", error_to_string(rv));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: salsa20_setup failed: %s", error_to_string(rv));
22 }
2023
21 if (iv_len == 8) {
22 rv = salsa20_ivctr64(&RETVAL->state, iv, (unsigned long)iv_len, (ulong64)counter);
23 if (rv != CRYPT_OK) croak("FATAL: salsa20_ivctr64 failed: %s", error_to_string(rv));
24 }
25 else {
26 croak("FATAL: chacha IV length must be 8 or 12 bytes");
24 rv = salsa20_ivctr64(&RETVAL->state, iv, (unsigned long)iv_len, (ulong64)counter);
25 if (rv != CRYPT_OK) {
26 Safefree(RETVAL);
27 croak("FATAL: salsa20_ivctr64 failed: %s", error_to_string(rv));
2728 }
2829 }
2930 OUTPUT:
7374 unsigned char *in_data, *out_data;
7475
7576 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
76 if (in_data_len==0) {
77 if (in_data_len == 0) {
7778 RETVAL = newSVpvn("", 0);
7879 }
7980 else {
1616 if (!RETVAL) croak("FATAL: Newz failed");
1717
1818 rv = sober128_stream_setup(&RETVAL->state, k, (unsigned long)k_len);
19 if (rv != CRYPT_OK) croak("FATAL: sober128_stream_setup failed: %s", error_to_string(rv));
19 if (rv != CRYPT_OK) {
20 Safefree(RETVAL);
21 croak("FATAL: sober128_stream_setup failed: %s", error_to_string(rv));
22 }
2023
2124 rv = sober128_stream_setiv(&RETVAL->state, iv, (unsigned long)iv_len);
22 if (rv != CRYPT_OK) croak("FATAL: sober128_stream_setiv failed: %s", error_to_string(rv));
25 if (rv != CRYPT_OK) {
26 Safefree(RETVAL);
27 croak("FATAL: sober128_stream_setiv failed: %s", error_to_string(rv));
28 }
2329 }
2430 OUTPUT:
2531 RETVAL
6874 unsigned char *in_data, *out_data;
6975
7076 in_data = (unsigned char *)SvPVbyte(data, in_data_len);
71 if (in_data_len==0) {
77 if (in_data_len == 0) {
7278 RETVAL = newSVpvn("", 0);
7379 }
7480 else {