Codebase list libcryptx-perl / 1e9bd50
ltc update Karel Miko 6 years ago
18 changed file(s) with 63 addition(s) and 81 deletion(s). Raw diff Collapse all Expand all
174174 }
175175
176176 return MP_OKAY;
177 }
178
179 int _base16_encode(const unsigned char *in, unsigned long inlen, unsigned char *out, unsigned long *outlen)
180 {
181 unsigned long i;
182 const char alphabet[] = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
183
184 if (*outlen < inlen * 2) {
185 *outlen = inlen * 2;
186 return CRYPT_BUFFER_OVERFLOW;
187 }
188
189 for (i = 0; i < inlen; i++) {
190 out[i*2] = (unsigned char)alphabet[in[i] >> 4];
191 out[i*2+1] = (unsigned char)alphabet[in[i] & 0xF];
192 }
193
194 *outlen = inlen * 2;
195 return CRYPT_OK;
196177 }
197178
198179 size_t _find_start(const char *name, char *ltcname, size_t ltclen)
267248 if ((err = ltc_mp.init(&tmp)) != CRYPT_OK) return;
268249 for (cu = ltc_ecc_curves; cu->prime != NULL; cu++) {
269250 if ((err = mp_read_radix(tmp, cu->prime, 16)) != CRYPT_OK) continue;
270 if ((mp_cmp(tmp, key->dp.prime) != LTC_MP_EQ)) continue;
251 if ((mp_cmp(tmp, key->dp.prime) != LTC_MP_EQ)) continue;
271252 if ((err = mp_read_radix(tmp, cu->order, 16)) != CRYPT_OK) continue;
272 if ((mp_cmp(tmp, key->dp.order) != LTC_MP_EQ)) continue;
253 if ((mp_cmp(tmp, key->dp.order) != LTC_MP_EQ)) continue;
273254 if ((err = mp_read_radix(tmp, cu->A, 16)) != CRYPT_OK) continue;
274 if ((mp_cmp(tmp, key->dp.A) != LTC_MP_EQ)) continue;
255 if ((mp_cmp(tmp, key->dp.A) != LTC_MP_EQ)) continue;
275256 if ((err = mp_read_radix(tmp, cu->B, 16)) != CRYPT_OK) continue;
276 if ((mp_cmp(tmp, key->dp.B) != LTC_MP_EQ)) continue;
257 if ((mp_cmp(tmp, key->dp.B) != LTC_MP_EQ)) continue;
277258 if ((err = mp_read_radix(tmp, cu->Gx, 16)) != CRYPT_OK) continue;
278 if ((mp_cmp(tmp, key->dp.base.x) != LTC_MP_EQ)) continue;
259 if ((mp_cmp(tmp, key->dp.base.x) != LTC_MP_EQ)) continue;
279260 if ((err = mp_read_radix(tmp, cu->Gy, 16)) != CRYPT_OK) continue;
280 if ((mp_cmp(tmp, key->dp.base.y) != LTC_MP_EQ)) continue;
261 if ((mp_cmp(tmp, key->dp.base.y) != LTC_MP_EQ)) continue;
281262 if (key->dp.cofactor != cu->cofactor) continue;
282263 break; /* found */
283264 }
531512 SvREFCNT_dec(RETVAL);
532513 XSRETURN_UNDEF;
533514 }
534 SvCUR_set(RETVAL, strlen(out_data));
515 SvCUR_set(RETVAL, out_len);
535516 }
536517 }
537518 OUTPUT:
606587 SvREFCNT_dec(RETVAL);
607588 XSRETURN_UNDEF;
608589 }
609 SvCUR_set(RETVAL, strlen(out_data));
590 SvCUR_set(RETVAL, out_len);
610591 }
611592 }
612593 OUTPUT:
110110 unsigned char mac[MAXBLOCKSIZE];
111111 unsigned long maclen, outlen;
112112 int rv;
113 char out[MAXBLOCKSIZE*2];
113 char out[MAXBLOCKSIZE*2+1];
114114
115115 [%-IF lc_name == 'pelican' %]
116116 maclen = 16;
137137 RETVAL = newSVpvn(out, outlen);
138138 }
139139 if (ix == 1) {
140 rv = _base16_encode(mac, maclen, (unsigned char *)out, &outlen);
140 rv = base16_encode(mac, maclen, (unsigned char *)out, &outlen, 0);
141141 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
142142 RETVAL = newSVpvn(out, outlen);
143143 }
257257 RETVAL = newSVpvn((char *) out, outlen);
258258 }
259259 else if (ix == 1) {
260 rv = _base16_encode(mac, len, (unsigned char *)out, &outlen);
260 rv = base16_encode(mac, len, (unsigned char *)out, &outlen, 0);
261261 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
262262 RETVAL = newSVpvn((char *) out, outlen);
263263 }
5858 CODE:
5959 {
6060 int rv;
61 unsigned char hash[4], out[8];
62 unsigned long outlen = 8;
61 unsigned char hash[4], out[9];
62 unsigned long outlen = 9;
6363 unsigned int ui32;
6464
6565 adler32_finish(self, hash, 4); /* returns void */
6666 if (ix == 1) {
67 rv = _base16_encode(hash, 4, out, &outlen);
67 rv = base16_encode(hash, 4, out, &outlen, 0);
6868 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
6969 RETVAL = newSVpvn((char *)out, outlen);
7070 }
8888 {
8989 adler32_state st;
9090 int rv, j;
91 unsigned char hash[4], out[8], *in;
92 unsigned long outlen = 8;
91 unsigned char hash[4], out[9], *in;
92 unsigned long outlen = 9;
9393 unsigned int ui32;
9494 STRLEN inlen;
9595
102102 }
103103 adler32_finish(&st, hash, 4); /* returns void */
104104 if (ix == 1) {
105 rv = _base16_encode(hash, 4, out, &outlen);
105 rv = base16_encode(hash, 4, out, &outlen, 0);
106106 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
107107 RETVAL = newSVpvn((char *)out, outlen);
108108 }
5858 CODE:
5959 {
6060 int rv;
61 unsigned char hash[4], out[8];
62 unsigned long outlen = 8;
61 unsigned char hash[4], out[9];
62 unsigned long outlen = 9;
6363 unsigned int ui32;
6464
6565 crc32_finish(self, hash, 4); /* returns void */
6666 if (ix == 1) {
67 rv = _base16_encode(hash, 4, out, &outlen);
67 rv = base16_encode(hash, 4, out, &outlen, 0);
6868 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
6969 RETVAL = newSVpvn((char *)out, outlen);
7070 }
8888 {
8989 crc32_state st;
9090 int rv, j;
91 unsigned char hash[4], out[8], *in;
92 unsigned long outlen = 8;
91 unsigned char hash[4], out[9], *in;
92 unsigned long outlen = 9;
9393 unsigned int ui32;
9494 STRLEN inlen;
9595
102102 }
103103 crc32_finish(&st, hash, 4); /* returns void */
104104 if (ix == 1) {
105 rv = _base16_encode(hash, 4, out, &outlen);
105 rv = base16_encode(hash, 4, out, &outlen, 0);
106106 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
107107 RETVAL = newSVpvn((char *)out, outlen);
108108 }
7878 int rv;
7979 unsigned long outlen;
8080 unsigned char hash[MAXBLOCKSIZE];
81 char out[MAXBLOCKSIZE*2];
81 char out[MAXBLOCKSIZE*2+1];
8282
8383 rv = self->desc->done(&self->state, hash);
8484 if (rv != CRYPT_OK) croak("FATAL: digest done failed: %s", error_to_string(rv));
9595 RETVAL = newSVpvn(out, outlen);
9696 }
9797 else if (ix == 1) {
98 rv = _base16_encode(hash, self->desc->hashsize, (unsigned char *)out, &outlen);
98 rv = base16_encode(hash, self->desc->hashsize, (unsigned char *)out, &outlen, 0);
9999 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
100100 RETVAL = newSVpvn(out, outlen);
101101 }
118118 int rv, id, i;
119119 unsigned char *in, hash[MAXBLOCKSIZE];
120120 unsigned long len = sizeof(hash), outlen;
121 char out[MAXBLOCKSIZE*2];
121 char out[MAXBLOCKSIZE*2+1];
122122 hash_state md;
123123
124124 id = _find_hash(digest_name);
150150 RETVAL = newSVpvn((char *) out, outlen);
151151 }
152152 else if (ix == 1) {
153 rv = _base16_encode(hash, len, (unsigned char *)out, &outlen);
153 rv = base16_encode(hash, len, (unsigned char *)out, &outlen, 0);
154154 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
155155 RETVAL = newSVpvn((char *) out, outlen);
156156 }
6969 unsigned char mac[MAXBLOCKSIZE];
7070 unsigned long maclen, outlen;
7171 int rv;
72 char out[MAXBLOCKSIZE*2];
72 char out[MAXBLOCKSIZE*2+1];
7373
7474 maclen = sizeof(mac);
7575 rv = blake2bmac_done(self, mac, &maclen);
8686 RETVAL = newSVpvn(out, outlen);
8787 }
8888 if (ix == 1) {
89 rv = _base16_encode(mac, maclen, (unsigned char *)out, &outlen);
89 rv = base16_encode(mac, maclen, (unsigned char *)out, &outlen, 0);
9090 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
9191 RETVAL = newSVpvn(out, outlen);
9292 }
139139 RETVAL = newSVpvn((char *) out, outlen);
140140 }
141141 else if (ix == 1) {
142 rv = _base16_encode(mac, len, (unsigned char *)out, &outlen);
142 rv = base16_encode(mac, len, (unsigned char *)out, &outlen, 0);
143143 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
144144 RETVAL = newSVpvn((char *) out, outlen);
145145 }
6969 unsigned char mac[MAXBLOCKSIZE];
7070 unsigned long maclen, outlen;
7171 int rv;
72 char out[MAXBLOCKSIZE*2];
72 char out[MAXBLOCKSIZE*2+1];
7373
7474 maclen = sizeof(mac);
7575 rv = blake2smac_done(self, mac, &maclen);
8686 RETVAL = newSVpvn(out, outlen);
8787 }
8888 if (ix == 1) {
89 rv = _base16_encode(mac, maclen, (unsigned char *)out, &outlen);
89 rv = base16_encode(mac, maclen, (unsigned char *)out, &outlen, 0);
9090 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
9191 RETVAL = newSVpvn(out, outlen);
9292 }
139139 RETVAL = newSVpvn((char *) out, outlen);
140140 }
141141 else if (ix == 1) {
142 rv = _base16_encode(mac, len, (unsigned char *)out, &outlen);
142 rv = base16_encode(mac, len, (unsigned char *)out, &outlen, 0);
143143 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
144144 RETVAL = newSVpvn((char *) out, outlen);
145145 }
7373 unsigned char mac[MAXBLOCKSIZE];
7474 unsigned long maclen, outlen;
7575 int rv;
76 char out[MAXBLOCKSIZE*2];
76 char out[MAXBLOCKSIZE*2+1];
7777
7878 maclen = sizeof(mac);
7979 rv = f9_done(self, mac, &maclen);
9090 RETVAL = newSVpvn(out, outlen);
9191 }
9292 if (ix == 1) {
93 rv = _base16_encode(mac, maclen, (unsigned char *)out, &outlen);
93 rv = base16_encode(mac, maclen, (unsigned char *)out, &outlen, 0);
9494 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
9595 RETVAL = newSVpvn(out, outlen);
9696 }
144144 RETVAL = newSVpvn((char *) out, outlen);
145145 }
146146 else if (ix == 1) {
147 rv = _base16_encode(mac, len, (unsigned char *)out, &outlen);
147 rv = base16_encode(mac, len, (unsigned char *)out, &outlen, 0);
148148 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
149149 RETVAL = newSVpvn((char *) out, outlen);
150150 }
7373 unsigned char mac[MAXBLOCKSIZE];
7474 unsigned long maclen, outlen;
7575 int rv;
76 char out[MAXBLOCKSIZE*2];
76 char out[MAXBLOCKSIZE*2+1];
7777
7878 maclen = sizeof(mac);
7979 rv = hmac_done(self, mac, &maclen);
9090 RETVAL = newSVpvn(out, outlen);
9191 }
9292 if (ix == 1) {
93 rv = _base16_encode(mac, maclen, (unsigned char *)out, &outlen);
93 rv = base16_encode(mac, maclen, (unsigned char *)out, &outlen, 0);
9494 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
9595 RETVAL = newSVpvn(out, outlen);
9696 }
144144 RETVAL = newSVpvn((char *) out, outlen);
145145 }
146146 else if (ix == 1) {
147 rv = _base16_encode(mac, len, (unsigned char *)out, &outlen);
147 rv = base16_encode(mac, len, (unsigned char *)out, &outlen, 0);
148148 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
149149 RETVAL = newSVpvn((char *) out, outlen);
150150 }
7373 unsigned char mac[MAXBLOCKSIZE];
7474 unsigned long maclen, outlen;
7575 int rv;
76 char out[MAXBLOCKSIZE*2];
76 char out[MAXBLOCKSIZE*2+1];
7777
7878 maclen = sizeof(mac);
7979 rv = omac_done(self, mac, &maclen);
9090 RETVAL = newSVpvn(out, outlen);
9191 }
9292 if (ix == 1) {
93 rv = _base16_encode(mac, maclen, (unsigned char *)out, &outlen);
93 rv = base16_encode(mac, maclen, (unsigned char *)out, &outlen, 0);
9494 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
9595 RETVAL = newSVpvn(out, outlen);
9696 }
144144 RETVAL = newSVpvn((char *) out, outlen);
145145 }
146146 else if (ix == 1) {
147 rv = _base16_encode(mac, len, (unsigned char *)out, &outlen);
147 rv = base16_encode(mac, len, (unsigned char *)out, &outlen, 0);
148148 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
149149 RETVAL = newSVpvn((char *) out, outlen);
150150 }
7373 unsigned char mac[MAXBLOCKSIZE];
7474 unsigned long maclen, outlen;
7575 int rv;
76 char out[MAXBLOCKSIZE*2];
76 char out[MAXBLOCKSIZE*2+1];
7777
7878 maclen = sizeof(mac);
7979 rv = pmac_done(self, mac, &maclen);
9090 RETVAL = newSVpvn(out, outlen);
9191 }
9292 if (ix == 1) {
93 rv = _base16_encode(mac, maclen, (unsigned char *)out, &outlen);
93 rv = base16_encode(mac, maclen, (unsigned char *)out, &outlen, 0);
9494 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
9595 RETVAL = newSVpvn(out, outlen);
9696 }
144144 RETVAL = newSVpvn((char *) out, outlen);
145145 }
146146 else if (ix == 1) {
147 rv = _base16_encode(mac, len, (unsigned char *)out, &outlen);
147 rv = base16_encode(mac, len, (unsigned char *)out, &outlen, 0);
148148 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
149149 RETVAL = newSVpvn((char *) out, outlen);
150150 }
6969 unsigned char mac[MAXBLOCKSIZE];
7070 unsigned long maclen, outlen;
7171 int rv;
72 char out[MAXBLOCKSIZE*2];
72 char out[MAXBLOCKSIZE*2+1];
7373
7474 maclen = 16;
7575 rv = pelican_done(self, mac);
8686 RETVAL = newSVpvn(out, outlen);
8787 }
8888 if (ix == 1) {
89 rv = _base16_encode(mac, maclen, (unsigned char *)out, &outlen);
89 rv = base16_encode(mac, maclen, (unsigned char *)out, &outlen, 0);
9090 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
9191 RETVAL = newSVpvn(out, outlen);
9292 }
139139 RETVAL = newSVpvn((char *) out, outlen);
140140 }
141141 else if (ix == 1) {
142 rv = _base16_encode(mac, len, (unsigned char *)out, &outlen);
142 rv = base16_encode(mac, len, (unsigned char *)out, &outlen, 0);
143143 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
144144 RETVAL = newSVpvn((char *) out, outlen);
145145 }
6969 unsigned char mac[MAXBLOCKSIZE];
7070 unsigned long maclen, outlen;
7171 int rv;
72 char out[MAXBLOCKSIZE*2];
72 char out[MAXBLOCKSIZE*2+1];
7373
7474 maclen = sizeof(mac);
7575 rv = poly1305_done(self, mac, &maclen);
8686 RETVAL = newSVpvn(out, outlen);
8787 }
8888 if (ix == 1) {
89 rv = _base16_encode(mac, maclen, (unsigned char *)out, &outlen);
89 rv = base16_encode(mac, maclen, (unsigned char *)out, &outlen, 0);
9090 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
9191 RETVAL = newSVpvn(out, outlen);
9292 }
138138 RETVAL = newSVpvn((char *) out, outlen);
139139 }
140140 else if (ix == 1) {
141 rv = _base16_encode(mac, len, (unsigned char *)out, &outlen);
141 rv = base16_encode(mac, len, (unsigned char *)out, &outlen, 0);
142142 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
143143 RETVAL = newSVpvn((char *) out, outlen);
144144 }
7373 unsigned char mac[MAXBLOCKSIZE];
7474 unsigned long maclen, outlen;
7575 int rv;
76 char out[MAXBLOCKSIZE*2];
76 char out[MAXBLOCKSIZE*2+1];
7777
7878 maclen = sizeof(mac);
7979 rv = xcbc_done(self, mac, &maclen);
9090 RETVAL = newSVpvn(out, outlen);
9191 }
9292 if (ix == 1) {
93 rv = _base16_encode(mac, maclen, (unsigned char *)out, &outlen);
93 rv = base16_encode(mac, maclen, (unsigned char *)out, &outlen, 0);
9494 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
9595 RETVAL = newSVpvn(out, outlen);
9696 }
144144 RETVAL = newSVpvn((char *) out, outlen);
145145 }
146146 else if (ix == 1) {
147 rv = _base16_encode(mac, len, (unsigned char *)out, &outlen);
147 rv = base16_encode(mac, len, (unsigned char *)out, &outlen, 0);
148148 if (rv != CRYPT_OK) croak("FATAL: base16_encode failed: %s", error_to_string(rv));
149149 RETVAL = newSVpvn((char *) out, outlen);
150150 }
123123 if (tmp == NULL) croak("FATAL: Newz failed");
124124 rv_len = (self->desc->read)(tmp, (unsigned long)output_len, &self->state);
125125 if ((UV)rv_len != output_len) croak("FATAL: PRNG_read failed");
126 RETVAL = NEWSV(0, output_len * 2); /* avoid zero! */
126 RETVAL = NEWSV(0, output_len * 2 + 1); /* avoid zero! */
127127 SvPOK_only(RETVAL);
128 SvCUR_set(RETVAL, output_len * 2);
128 SvCUR_set(RETVAL, output_len * 2 + 1);
129129 rdata = (unsigned char *)SvPVX(RETVAL);
130 len = output_len * 2;
131 rv = _base16_encode(tmp, output_len, rdata, &len);
130 len = output_len * 2 + 1;
131 rv = base16_encode(tmp, output_len, rdata, &len, 0);
132 SvCUR_set(RETVAL, len);
132133 Safefree(tmp);
133134 if (rv != CRYPT_OK) {
134135 SvREFCNT_dec(RETVAL);
4848 *outlen = x;
4949 return CRYPT_BUFFER_OVERFLOW;
5050 }
51 *outlen = x;
51 x--;
52 *outlen = x; /* returning the length without terminating NUL */
5253
5354 if (caps == 0) alphabet = alphabets[0];
5455 else alphabet = alphabets[1];
5556
56 x -= 1;
5757 for (i = 0; i < x; i += 2) {
5858 out[i] = alphabet[(in[i/2] >> 4) & 0x0f];
5959 out[i+1] = alphabet[in[i/2] & 0x0f];
4444 *outlen = x;
4545 return CRYPT_BUFFER_OVERFLOW;
4646 }
47 *outlen = x;
47 *outlen = x - 1; /* returning the length without terminating NUL */
4848
4949 /* no input, nothing to do */
5050 if (inlen == 0) {
7272 *p = '\0';
7373
7474 /* return ok */
75 *outlen = (unsigned long)(p - out);
75 *outlen = (unsigned long)(p - out); /* the length without terminating NUL */
7676 return CRYPT_OK;
7777 }
7878