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 /**
14   @file ltc_ecc_points.c
15   ECC Crypto, Tom St Denis
16 */
17 
18 #ifdef LTC_MECC
19 
20 /**
21    Allocate a new ECC point
22    @return A newly allocated point or NULL on error
23 */
ltc_ecc_new_point(void)24 ecc_point *ltc_ecc_new_point(void)
25 {
26    ecc_point *p;
27    p = XCALLOC(1, sizeof(*p));
28    if (p == NULL) {
29       return NULL;
30    }
31    if (mp_init_multi_size(LTC_MAX_ECC * 2,
32 			  &p->x, &p->y, &p->z, NULL) != CRYPT_OK) {
33       XFREE(p);
34       return NULL;
35    }
36    return p;
37 }
38 
39 /** Free an ECC point from memory
40   @param p   The point to free
41 */
ltc_ecc_del_point(ecc_point * p)42 void ltc_ecc_del_point(ecc_point *p)
43 {
44    /* prevents free'ing null arguments */
45    if (p != NULL) {
46       mp_clear_multi(p->x, p->y, p->z, NULL); /* note: p->z may be NULL but that's ok with this function anyways */
47       XFREE(p);
48    }
49 }
50 
ltc_ecc_set_point_xyz(ltc_mp_digit x,ltc_mp_digit y,ltc_mp_digit z,ecc_point * p)51 int ltc_ecc_set_point_xyz(ltc_mp_digit x, ltc_mp_digit y, ltc_mp_digit z, ecc_point *p)
52 {
53    int err;
54    if ((err = ltc_mp.set_int(p->x, x)) != CRYPT_OK) return err;
55    if ((err = ltc_mp.set_int(p->y, y)) != CRYPT_OK) return err;
56    if ((err = ltc_mp.set_int(p->z, z)) != CRYPT_OK) return err;
57    return CRYPT_OK;
58 }
59 
ltc_ecc_copy_point(const ecc_point * src,ecc_point * dst)60 int ltc_ecc_copy_point(const ecc_point *src, ecc_point *dst)
61 {
62    int err;
63    if ((err = ltc_mp.copy(src->x, dst->x)) != CRYPT_OK) return err;
64    if ((err = ltc_mp.copy(src->y, dst->y)) != CRYPT_OK) return err;
65    if ((err = ltc_mp.copy(src->z, dst->z)) != CRYPT_OK) return err;
66    return CRYPT_OK;
67 }
68 
69 #endif
70 /* ref:         $Format:%D$ */
71 /* git commit:  $Format:%H$ */
72 /* commit time: $Format:%ai$ */
73 
74