DomainParameters.cpp 3.53 KB
Newer Older
Chadwick Strange's avatar
Chadwick Strange committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
    Copyright (C) 2019-Present SKALE Labs

    This file is part of sgxwallet.

    sgxwallet is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published
    by the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    sgxwallet is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with sgxwallet.  If not, see <https://www.gnu.org/licenses/>.

    @file domain_parameters.c
kladko's avatar
kladko committed
20
    @author Stan Kladko
Chadwick Strange's avatar
Chadwick Strange committed
21 22 23
    @date 2019
*/

kladko's avatar
kladko committed
24 25 26 27
#define SAFE_FREE(__X__) if (__X__) {free(__X__); __X__ = NULL;}
#define SAFE_DELETE(__X__) if (__X__) {delete(__X__); __X__ = NULL;}
#define SAFE_CHAR_BUF(__X__, __Y__)  ;char __X__ [ __Y__ ]; memset(__X__, 0, __Y__);

kladko's avatar
kladko committed
28
#ifdef USER_SPACE
kladko's avatar
kladko committed
29 30
#include <gmp.h>
#else
31
#include <../tgmp-build/include/sgx_tgmp.h>
kladko's avatar
kladko committed
32 33
#endif

kladko's avatar
kladko committed
34
#include <stdint.h>
35 36 37
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
kladko's avatar
kladko committed
38
#include "EnclaveCommon.h"
39
#include "Point.h"
kladko's avatar
kladko committed
40

41
#include "DomainParameters.h"
42

kladko's avatar
kladko committed
43 44 45 46 47 48
#define CHECK_ARG_ABORT(_EXPRESSION_) \
    if (!(_EXPRESSION_)) {        \
        abort();                              \
        }


49 50 51
/*Initialize a curve*/
domain_parameters domain_parameters_init()
{
kladko's avatar
kladko committed
52 53 54



55
	domain_parameters curve;
kladko's avatar
kladko committed
56 57 58
	curve = (domain_parameters) calloc(sizeof(struct domain_parameters_s),1);

	CHECK_ARG_ABORT(curve);
59

kladko's avatar
kladko committed
60

61 62 63 64
	//Initialize all members
	mpz_init(curve->p);
	mpz_init(curve->a);
	mpz_init(curve->b);
kladko's avatar
kladko committed
65 66 67 68
    mpz_init(curve->n);
    mpz_init(curve->h);


69 70
	curve->G = point_init();

kladko's avatar
kladko committed
71 72 73
	CHECK_ARG_ABORT(curve->G);


74 75 76 77 78 79
	return curve;
}

/*Sets the name of a curve*/
void domain_parameters_set_name(domain_parameters curve, char* name)
{
kladko's avatar
kladko committed
80 81

    CHECK_ARG_ABORT(name);
82
	int len = strlen(name);
83
	curve->name = (char*)calloc( sizeof(char) * (len+1), 1 );
84 85
	curve->name[len] = '\0';
	strncpy(curve->name, name, len+1);
kladko's avatar
kladko committed
86

kladko's avatar
kladko committed
87

88 89 90 91 92 93 94 95 96 97 98 99 100
}

/*Set domain parameters from decimal unsigned long ints*/
void domain_parameters_set_ui(domain_parameters curve,
								char* name,
								unsigned long int p,
								unsigned long int a,
								unsigned long int b,
								unsigned long int Gx,
								unsigned long int Gy,
								unsigned long int n,
								unsigned long int h)
{
kladko's avatar
kladko committed
101

kladko's avatar
kladko committed
102
    CHECK_ARG_ABORT(name);
kladko's avatar
kladko committed
103

104 105 106 107 108 109 110
	domain_parameters_set_name(curve, name);
	mpz_set_ui(curve->p, p);
	mpz_set_ui(curve->a, a);
	mpz_set_ui(curve->b, b);
	point_set_ui(curve->G, Gx, Gy);
	mpz_set_ui(curve->n, n);
	mpz_set_ui(curve->h, h);
kladko's avatar
kladko committed
111

kladko's avatar
kladko committed
112

113 114 115 116 117
}

/*Set domain parameters from hexadecimal string*/
void domain_parameters_set_hex(domain_parameters curve, char* name, char* p, char* a, char* b, char* Gx, char* Gy, char* n, char* h)
{
kladko's avatar
kladko committed
118

kladko's avatar
kladko committed
119 120 121 122 123 124 125 126 127 128 129 130 131
    CHECK_ARG_ABORT(name);
    CHECK_ARG_ABORT(p);
    CHECK_ARG_ABORT(a);
    CHECK_ARG_ABORT(b);
    CHECK_ARG_ABORT(Gx);
    CHECK_ARG_ABORT(Gy);
    CHECK_ARG_ABORT(n);
    CHECK_ARG_ABORT(h);





132 133 134 135 136 137 138
	domain_parameters_set_name(curve, name);
	mpz_set_str(curve->p, p, 16);
	mpz_set_str(curve->a, a, 16);
	mpz_set_str(curve->b, b, 16);
	point_set_hex(curve->G, Gx, Gy);
	mpz_set_str(curve->n, n, 16);
	mpz_set_str(curve->h, h, 16);
kladko's avatar
kladko committed
139

kladko's avatar
kladko committed
140

141 142 143 144 145
}

/*Release memory*/
void domain_parameters_clear(domain_parameters curve)
{
kladko's avatar
kladko committed
146 147 148 149

    if (!curve)
        return;

150 151 152 153 154 155
	mpz_clear(curve->p);
	mpz_clear(curve->a);
	mpz_clear(curve->b);
	point_clear(curve->G);
	mpz_clear(curve->n);
	mpz_clear(curve->h);
kladko's avatar
kladko committed
156
	SAFE_FREE(curve->name);
157 158 159
	free(curve);
}