forked from ANSSI-FR/libecc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec_key.h
181 lines (148 loc) · 5.41 KB
/
ec_key.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Copyright (C) 2017 - This file is part of libecc project
*
* Authors:
* Ryad BENADJILA <[email protected]>
* Arnaud EBALARD <[email protected]>
* Jean-Pierre FLORI <[email protected]>
*
* Contributors:
* Nicolas VIVET <[email protected]>
* Karim KHALFALLAH <[email protected]>
*
* This software is licensed under a dual BSD and GPL v2 license.
* See LICENSE file at the root folder of the project.
*/
#ifndef __EC_KEY_H__
#define __EC_KEY_H__
#include "../lib_ecc_config.h"
#include "../lib_ecc_types.h"
#include "../fp/fp.h"
#include "../curves/ec_params.h"
#include "../nn/nn_rand.h"
#include "../nn/nn_add.h"
#include "../curves/prj_pt_monty.h"
/* Enum for exported keys */
typedef enum {
EC_PUBKEY = 0,
EC_PRIVKEY = 1,
} ec_key_type;
/*
* Declarations for EC private keys
*/
#define PRIV_KEY_MAGIC ((word_t)(0x2feb91e938a4855dULL))
typedef struct {
/* A key type can only be used for a given sig alg */
ec_sig_alg_type key_type;
/* Elliptic curve parameters */
const ec_params *params;
/*
* Private key, an integer in ]0,q[, where q is
* the order of G, the generator of the group
* on the curve.
*/
nn x;
word_t magic;
} ec_priv_key;
#define EC_PRIV_KEY_MAX_SIZE (BYTECEIL(CURVES_MAX_Q_BIT_LEN))
#define EC_PRIV_KEY_EXPORT_SIZE(priv_key) \
((u8)(BYTECEIL((priv_key)->params->ec_gen_order_bitlen)))
#define EC_STRUCTURED_PRIV_KEY_MAX_EXPORT_SIZE (EC_PRIV_KEY_MAX_SIZE + 3)
#if (EC_STRUCTURED_PRIV_KEY_MAX_EXPORT_SIZE > 255)
#error "All structured priv keys size are expected to fit on an u8."
#endif
#define EC_STRUCTURED_PRIV_KEY_EXPORT_SIZE(priv_key) \
((u8)(EC_PRIV_KEY_EXPORT_SIZE(priv_key) + (3 * sizeof(u8))))
void priv_key_check_initialized(const ec_priv_key *A);
void priv_key_check_initialized_and_type(const ec_priv_key *A,
ec_sig_alg_type sig_type);
int priv_key_is_initialized(const ec_priv_key *A);
int priv_key_is_initialized_and_type(const ec_priv_key *A,
ec_sig_alg_type sig_type);
void ec_priv_key_import_from_buf(ec_priv_key *priv_key,
const ec_params *params,
const u8 *priv_key_buf, u8 priv_key_buf_len,
ec_sig_alg_type ec_key_alg);
int ec_priv_key_export_to_buf(const ec_priv_key *priv_key, u8 *priv_key_buf,
u8 priv_key_buf_len);
int ec_structured_priv_key_import_from_buf(ec_priv_key *priv_key,
const ec_params *params,
const u8 *priv_key_buf,
u8 priv_key_buf_len,
ec_sig_alg_type ec_key_alg);
int ec_structured_priv_key_export_to_buf(const ec_priv_key *priv_key,
u8 *priv_key_buf,
u8 priv_key_buf_len);
/*
* Declarations for EC public keys
*/
#define PUB_KEY_MAGIC ((word_t)(0x31327f37741ffb76ULL))
typedef struct {
/* A key type can only be used for a given sig alg */
ec_sig_alg_type key_type;
/* Elliptic curve parameters */
const ec_params *params;
/* Public key, i.e. y = xG mod p */
prj_pt y;
word_t magic;
} ec_pub_key;
#define EC_PUB_KEY_MAX_SIZE (3 * BYTECEIL(CURVES_MAX_P_BIT_LEN))
#define EC_PUB_KEY_EXPORT_SIZE(pub_key) \
(3 * BYTECEIL((pub_key)->params->ec_curve.a.ctx->p_bitlen))
#define EC_STRUCTURED_PUB_KEY_MAX_EXPORT_SIZE (EC_PUB_KEY_MAX_SIZE + 3)
#if (EC_STRUCTURED_PUB_KEY_MAX_EXPORT_SIZE > 255)
#error "All structured pub keys size are expected to fit on an u8."
#endif
#define EC_STRUCTURED_PUB_KEY_EXPORT_SIZE(pub_key) \
((u8)(EC_PUB_KEY_EXPORT_SIZE(pub_key) + (3 * sizeof(u8))))
void pub_key_check_initialized(const ec_pub_key *A);
void pub_key_check_initialized_and_type(const ec_pub_key *A,
ec_sig_alg_type sig_type);
int pub_key_is_initialized(const ec_pub_key *A);
int pub_key_is_initialized_and_type(const ec_pub_key *A,
ec_sig_alg_type sig_type);
int ec_pub_key_import_from_buf(ec_pub_key *pub_key, const ec_params *params,
const u8 *pub_key_buf, u8 pub_key_buf_len,
ec_sig_alg_type ec_key_alg);
int ec_pub_key_export_to_buf(const ec_pub_key *pub_key, u8 *pub_key_buf,
u8 pub_key_buf_len);
int ec_structured_pub_key_import_from_buf(ec_pub_key *pub_key,
const ec_params *params,
const u8 *pub_key_buf,
u8 pub_key_buf_len,
ec_sig_alg_type ec_key_alg);
int ec_structured_pub_key_export_to_buf(const ec_pub_key *pub_key,
u8 *pub_key_buf, u8 pub_key_buf_len);
/*
* Declarations for EC key pairs
*/
typedef struct {
ec_priv_key priv_key;
ec_pub_key pub_key;
} ec_key_pair;
void key_pair_check_initialized(const ec_key_pair *A);
int key_pair_is_initialized(const ec_key_pair *A);
void key_pair_check_initialized_and_type(const ec_key_pair *A,
ec_sig_alg_type sig_type);
int key_pair_is_initialized_and_type(const ec_key_pair *A,
ec_sig_alg_type sig_type);
int ec_key_pair_import_from_priv_key_buf(ec_key_pair *kp,
const ec_params *params,
const u8 *priv_key, u8 priv_key_len,
ec_sig_alg_type ec_key_alg);
int ec_key_pair_gen(ec_key_pair *kp, const ec_params *params,
ec_sig_alg_type ec_key_alg);
int ec_structured_key_pair_import_from_priv_key_buf(ec_key_pair *kp,
const ec_params *params,
const u8 *priv_key_buf,
u8 priv_key_buf_len,
ec_sig_alg_type
ec_key_alg);
int ec_structured_key_pair_import_from_buf(ec_key_pair *kp,
const ec_params *params,
const u8 *priv_key_buf,
u8 priv_key_buf_len,
const u8 *pub_key_buf,
u8 pub_key_buf_len,
ec_sig_alg_type ec_key_alg);
#endif /* __EC_KEY_H__ */