1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3
4 #include "tomcrypt_private.h"
5
6 #ifdef LTC_MECC
7
ltc_ecc_export_point(unsigned char * out,unsigned long * outlen,void * x,void * y,unsigned long size,int compressed)8 int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, void *y, unsigned long size, int compressed)
9 {
10 int err;
11 unsigned char buf[ECC_BUF_SIZE];
12 unsigned long xsize, ysize;
13
14 if (size > sizeof(buf)) return CRYPT_BUFFER_OVERFLOW;
15 if ((xsize = mp_unsigned_bin_size(x)) > size) return CRYPT_BUFFER_OVERFLOW;
16 if ((ysize = mp_unsigned_bin_size(y)) > size) return CRYPT_BUFFER_OVERFLOW;
17
18 if(compressed) {
19 if (*outlen < (1 + size)) {
20 *outlen = 1 + size;
21 return CRYPT_BUFFER_OVERFLOW;
22 }
23 /* store first byte */
24 out[0] = mp_isodd(y) ? 0x03 : 0x02;
25 /* pad and store x */
26 zeromem(buf, sizeof(buf));
27 if ((err = mp_to_unsigned_bin(x, buf + (size - xsize))) != CRYPT_OK) return err;
28 XMEMCPY(out+1, buf, size);
29 /* adjust outlen */
30 *outlen = 1 + size;
31 }
32 else {
33 if (*outlen < (1 + 2*size)) {
34 *outlen = 1 + 2*size;
35 return CRYPT_BUFFER_OVERFLOW;
36 }
37 /* store byte 0x04 */
38 out[0] = 0x04;
39 /* pad and store x */
40 zeromem(buf, sizeof(buf));
41 if ((err = mp_to_unsigned_bin(x, buf + (size - xsize))) != CRYPT_OK) return err;
42 XMEMCPY(out+1, buf, size);
43 /* pad and store y */
44 zeromem(buf, sizeof(buf));
45 if ((err = mp_to_unsigned_bin(y, buf + (size - ysize))) != CRYPT_OK) return err;
46 XMEMCPY(out+1+size, buf, size);
47 /* adjust outlen */
48 *outlen = 1 + 2*size;
49 }
50 return CRYPT_OK;
51 }
52
53 #endif
54