1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
2 /* SPDX-License-Identifier: Unlicense */
3 
4 #include "tomcrypt_private.h"
5 
6 /**
7   @file ltc_ecc_points.c
8   ECC Crypto, Tom St Denis
9 */
10 
11 #ifdef LTC_MECC
12 
13 /**
14    Allocate a new ECC point
15    @return A newly allocated point or NULL on error
16 */
ltc_ecc_new_point(void)17 ecc_point *ltc_ecc_new_point(void)
18 {
19    ecc_point *p;
20    p = XCALLOC(1, sizeof(*p));
21    if (p == NULL) {
22       return NULL;
23    }
24    if (mp_init_multi_size(LTC_MAX_ECC * 2,
25 			  &p->x, &p->y, &p->z, LTC_NULL) != CRYPT_OK) {
26       XFREE(p);
27       return NULL;
28    }
29    return p;
30 }
31 
32 /** Free an ECC point from memory
33   @param p   The point to free
34 */
ltc_ecc_del_point(ecc_point * p)35 void ltc_ecc_del_point(ecc_point *p)
36 {
37    /* prevents free'ing null arguments */
38    if (p != NULL) {
39       mp_clear_multi(p->x, p->y, p->z, LTC_NULL); /* note: p->z may be NULL but that's ok with this function anyways */
40       XFREE(p);
41    }
42 }
43 
ltc_ecc_set_point_xyz(ltc_mp_digit x,ltc_mp_digit y,ltc_mp_digit z,ecc_point * p)44 int ltc_ecc_set_point_xyz(ltc_mp_digit x, ltc_mp_digit y, ltc_mp_digit z, ecc_point *p)
45 {
46    int err;
47    if ((err = ltc_mp.set_int(p->x, x)) != CRYPT_OK) return err;
48    if ((err = ltc_mp.set_int(p->y, y)) != CRYPT_OK) return err;
49    if ((err = ltc_mp.set_int(p->z, z)) != CRYPT_OK) return err;
50    return CRYPT_OK;
51 }
52 
ltc_ecc_copy_point(const ecc_point * src,ecc_point * dst)53 int ltc_ecc_copy_point(const ecc_point *src, ecc_point *dst)
54 {
55    int err;
56    if ((err = ltc_mp.copy(src->x, dst->x)) != CRYPT_OK) return err;
57    if ((err = ltc_mp.copy(src->y, dst->y)) != CRYPT_OK) return err;
58    if ((err = ltc_mp.copy(src->z, dst->z)) != CRYPT_OK) return err;
59    return CRYPT_OK;
60 }
61 
62 #endif
63