1 // SPDX-License-Identifier: BSD-2-Clause
2 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3  *
4  * LibTomCrypt is a library that provides various cryptographic
5  * algorithms in a highly modular and flexible manner.
6  *
7  * The library is free for all purposes without any express
8  * guarantee it works.
9  */
10 
11 #include "tomcrypt_private.h"
12 
13 #ifdef LTC_MECC
14 
ltc_ecc_export_point(unsigned char * out,unsigned long * outlen,void * x,void * y,unsigned long size,int compressed)15 int ltc_ecc_export_point(unsigned char *out, unsigned long *outlen, void *x, void *y, unsigned long size, int compressed)
16 {
17    int err;
18    unsigned char buf[ECC_BUF_SIZE];
19    unsigned long xsize, ysize;
20 
21    if (size > sizeof(buf)) return CRYPT_BUFFER_OVERFLOW;
22    if ((xsize = mp_unsigned_bin_size(x)) > size) return CRYPT_BUFFER_OVERFLOW;
23    if ((ysize = mp_unsigned_bin_size(y)) > size) return CRYPT_BUFFER_OVERFLOW;
24 
25    if(compressed) {
26       if (*outlen < (1 + size)) {
27          *outlen = 1 + size;
28          return CRYPT_BUFFER_OVERFLOW;
29       }
30       /* store first byte */
31       out[0] = mp_isodd(y) ? 0x03 : 0x02;
32       /* pad and store x */
33       zeromem(buf, sizeof(buf));
34       if ((err = mp_to_unsigned_bin(x, buf + (size - xsize))) != CRYPT_OK) return err;
35       XMEMCPY(out+1, buf, size);
36       /* adjust outlen */
37       *outlen = 1 + size;
38    }
39    else {
40       if (*outlen < (1 + 2*size)) {
41          *outlen = 1 + 2*size;
42          return CRYPT_BUFFER_OVERFLOW;
43       }
44       /* store byte 0x04 */
45       out[0] = 0x04;
46       /* pad and store x */
47       zeromem(buf, sizeof(buf));
48       if ((err = mp_to_unsigned_bin(x, buf + (size - xsize))) != CRYPT_OK) return err;
49       XMEMCPY(out+1, buf, size);
50       /* pad and store y */
51       zeromem(buf, sizeof(buf));
52       if ((err = mp_to_unsigned_bin(y, buf + (size - ysize))) != CRYPT_OK) return err;
53       XMEMCPY(out+1+size, buf, size);
54       /* adjust outlen */
55       *outlen = 1 + 2*size;
56    }
57    return CRYPT_OK;
58 }
59 
60 #endif
61 
62 /* ref:         $Format:%D$ */
63 /* git commit:  $Format:%H$ */
64 /* commit time: $Format:%ai$ */
65