forked from contrun/libecc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsig_algs_internal.h
310 lines (290 loc) · 9.02 KB
/
sig_algs_internal.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* 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 __SIG_ALGS_INTERNAL_H__
#define __SIG_ALGS_INTERNAL_H__
#include "../hash/hash_algs.h"
#include "../curves/curves.h"
#include "ec_key.h"
#include "ecdsa.h"
#include "eckcdsa.h"
#include "ecsdsa.h"
#include "ecosdsa.h"
#include "ecfsdsa.h"
#include "ecgdsa.h"
#include "ecrdsa.h"
#if (EC_MAX_SIGLEN == 0)
#error "It seems you disabled all signature schemes in lib_ecc_config.h"
#endif
/* Sanity check to ensure our sig mapping does not contain
* NULL pointers
*/
#define SIG_MAPPING_SANITY_CHECK(A) \
MUST_HAVE(((A) != NULL) && \
((A)->name != NULL) && \
((A)->siglen != NULL) && \
((A)->init_pub_key != NULL) && \
((A)->sign_init != NULL) && \
((A)->sign_update != NULL) && \
((A)->sign_finalize != NULL) && \
((A)->verify_init != NULL) && \
((A)->verify_update != NULL) && \
((A)->verify_finalize != NULL)) \
/*
* All the signature algorithms we support are abstracted using the following
* structure (and following map) which provides for each hash alg its
* digest size, its block size and the associated scattered function.
*/
typedef struct {
ec_sig_alg_type type;
const char *name;
u8 (*siglen) (u16 p_bit_len, u16 q_bit_len, u8 hsize, u8 blocksize);
void (*init_pub_key) (ec_pub_key *pub_key, ec_priv_key *priv_key);
int (*sign_init) (struct ec_sign_context * ctx);
int (*sign_update) (struct ec_sign_context * ctx,
const u8 *chunk, u32 chunklen);
int (*sign_finalize) (struct ec_sign_context * ctx,
u8 *sig, u8 siglen);
int (*verify_init) (struct ec_verify_context * ctx,
const u8 *sig, u8 siglen);
int (*verify_update) (struct ec_verify_context * ctx,
const u8 *chunk, u32 chunklen);
int (*verify_finalize) (struct ec_verify_context * ctx);
} ec_sig_mapping;
/*
* Each specific signature scheme need to maintain some specific
* data between calls to init()/update()/finalize() functions.
*
* Each scheme provides a specific structure for that purpose
* (in its .h file) which we include in the union below. A field
* of that type (.sign_data) is then included in the generic
* struct ec_sign_context below.
*
* The purpose of that work is to allow static declaration and
* allocation of common struct ec_sign_context with enough room
* available for all supported signature types.
*/
typedef union {
#ifdef WITH_SIG_ECDSA /* ECDSA */
ecdsa_sign_data ecdsa;
#endif
#ifdef WITH_SIG_ECKCDSA /* ECKCDSA */
eckcdsa_sign_data eckcdsa;
#endif
#if (defined(WITH_SIG_ECSDSA) || defined(WITH_SIG_ECOSDSA)) /* EC[O]SDSA */
ecsdsa_sign_data ecsdsa;
#endif
#ifdef WITH_SIG_ECFSDSA /* ECFSDSA */
ecfsdsa_sign_data ecfsdsa;
#endif
#ifdef WITH_SIG_ECGDSA /* ECGDSA */
ecgdsa_sign_data ecgdsa;
#endif
#ifdef WITH_SIG_ECRDSA /* ECRDSA */
ecrdsa_sign_data ecrdsa;
#endif
} sig_sign_data;
/*
* The 'struct ec_sign_context' below provides a persistent state
* between successive calls to ec_sign_{init,update,finalize}().
*/
struct ec_sign_context {
word_t ctx_magic;
const ec_key_pair *key_pair;
int (*rand) (nn_t out, nn_src_t q);
const hash_mapping *h;
const ec_sig_mapping *sig;
sig_sign_data sign_data;
};
#define SIG_SIGN_MAGIC ((word_t)(0x4ed73cfe4594dfd3ULL))
#define SIG_SIGN_CHECK_INITIALIZED(A) \
MUST_HAVE(((A) != NULL) && ((A)->ctx_magic == SIG_SIGN_MAGIC))
typedef union {
#ifdef WITH_SIG_ECDSA /* ECDSA */
ecdsa_verify_data ecdsa;
#endif
#ifdef WITH_SIG_ECKCDSA /* ECKCDSA */
eckcdsa_verify_data eckcdsa;
#endif
#if (defined(WITH_SIG_ECSDSA) || defined(WITH_SIG_ECOSDSA)) /* EC[O]SDSA */
ecsdsa_verify_data ecsdsa;
#endif
#ifdef WITH_SIG_ECFSDSA /* ECFSDSA */
ecfsdsa_verify_data ecfsdsa;
#endif
#ifdef WITH_SIG_ECGDSA /* ECGDSA */
ecgdsa_verify_data ecgdsa;
#endif
#ifdef WITH_SIG_ECRDSA /* ECRDSA */
ecrdsa_verify_data ecrdsa;
#endif
} sig_verify_data;
/*
* The 'struct ec_verify_context' below provides a persistent state
* between successive calls to ec_verify_{init,update,finalize}().
*/
struct ec_verify_context {
word_t ctx_magic;
const ec_pub_key *pub_key;
const hash_mapping *h;
const ec_sig_mapping *sig;
sig_verify_data verify_data;
};
#define SIG_VERIFY_MAGIC ((word_t)(0x7e0d42d13e3159baULL))
#define SIG_VERIFY_CHECK_INITIALIZED(A) \
MUST_HAVE(((A) != NULL) && ((A)->ctx_magic == SIG_VERIFY_MAGIC))
/*
* Each signature algorithm supported by the library and implemented
* in ec{,ck,s,fs,g,r}dsa.{c,h} is referenced below.
*/
#define MAX_SIG_ALG_NAME_LEN 0
static const ec_sig_mapping ec_sig_maps[] = {
#ifdef WITH_SIG_ECDSA
{.type = ECDSA,
.name = "ECDSA",
.siglen = ecdsa_siglen,
.init_pub_key = ecdsa_init_pub_key,
.sign_init = _ecdsa_sign_init,
.sign_update = _ecdsa_sign_update,
.sign_finalize = _ecdsa_sign_finalize,
.verify_init = _ecdsa_verify_init,
.verify_update = _ecdsa_verify_update,
.verify_finalize = _ecdsa_verify_finalize,
},
#if (MAX_SIG_ALG_NAME_LEN < 6)
#undef MAX_SIG_ALG_NAME_LEN
#define MAX_SIG_ALG_NAME_LEN 6
#endif /* MAX_SIG_ALG_NAME_LEN */
#endif /* WITH_SIG_ECDSA */
#ifdef WITH_SIG_ECKCDSA
{.type = ECKCDSA,
.name = "ECKCDSA",
.siglen = eckcdsa_siglen,
.init_pub_key = eckcdsa_init_pub_key,
.sign_init = _eckcdsa_sign_init,
.sign_update = _eckcdsa_sign_update,
.sign_finalize = _eckcdsa_sign_finalize,
.verify_init = _eckcdsa_verify_init,
.verify_update = _eckcdsa_verify_update,
.verify_finalize = _eckcdsa_verify_finalize,
},
#if (MAX_SIG_ALG_NAME_LEN < 8)
#undef MAX_SIG_ALG_NAME_LEN
#define MAX_SIG_ALG_NAME_LEN 8
#endif /* MAX_SIG_ALG_NAME_LEN */
#endif /* WITH_SIG_ECKCDSA */
#ifdef WITH_SIG_ECSDSA
{.type = ECSDSA,
.name = "ECSDSA",
.siglen = ecsdsa_siglen,
.init_pub_key = ecsdsa_init_pub_key,
.sign_init = _ecsdsa_sign_init,
.sign_update = _ecsdsa_sign_update,
.sign_finalize = _ecsdsa_sign_finalize,
.verify_init = _ecsdsa_verify_init,
.verify_update = _ecsdsa_verify_update,
.verify_finalize = _ecsdsa_verify_finalize,
},
#if (MAX_SIG_ALG_NAME_LEN < 7)
#undef MAX_SIG_ALG_NAME_LEN
#define MAX_SIG_ALG_NAME_LEN 7
#endif /* MAX_SIG_ALG_NAME_LEN */
#endif /* WITH_SIG_ECSDSA */
#ifdef WITH_SIG_ECOSDSA
{.type = ECOSDSA,
.name = "ECOSDSA",
.siglen = ecosdsa_siglen,
.init_pub_key = ecosdsa_init_pub_key,
.sign_init = _ecosdsa_sign_init,
.sign_update = _ecosdsa_sign_update,
.sign_finalize = _ecosdsa_sign_finalize,
.verify_init = _ecosdsa_verify_init,
.verify_update = _ecosdsa_verify_update,
.verify_finalize = _ecosdsa_verify_finalize,
},
#if (MAX_SIG_ALG_NAME_LEN < 8)
#undef MAX_SIG_ALG_NAME_LEN
#define MAX_SIG_ALG_NAME_LEN 8
#endif /* MAX_SIG_ALG_NAME_LEN */
#endif /* WITH_SIG_ECOSDSA */
#ifdef WITH_SIG_ECFSDSA
{.type = ECFSDSA,
.name = "ECFSDSA",
.siglen = ecfsdsa_siglen,
.init_pub_key = ecfsdsa_init_pub_key,
.sign_init = _ecfsdsa_sign_init,
.sign_update = _ecfsdsa_sign_update,
.sign_finalize = _ecfsdsa_sign_finalize,
.verify_init = _ecfsdsa_verify_init,
.verify_update = _ecfsdsa_verify_update,
.verify_finalize = _ecfsdsa_verify_finalize,
},
#if (MAX_SIG_ALG_NAME_LEN < 8)
#undef MAX_SIG_ALG_NAME_LEN
#define MAX_SIG_ALG_NAME_LEN 8
#endif /* MAX_SIG_ALG_NAME_LEN */
#endif /* WITH_SIG_ECFSDSA */
#ifdef WITH_SIG_ECGDSA
{.type = ECGDSA,
.name = "ECGDSA",
.siglen = ecgdsa_siglen,
.init_pub_key = ecgdsa_init_pub_key,
.sign_init = _ecgdsa_sign_init,
.sign_update = _ecgdsa_sign_update,
.sign_finalize = _ecgdsa_sign_finalize,
.verify_init = _ecgdsa_verify_init,
.verify_update = _ecgdsa_verify_update,
.verify_finalize = _ecgdsa_verify_finalize,
},
#if (MAX_SIG_ALG_NAME_LEN < 7)
#undef MAX_SIG_ALG_NAME_LEN
#define MAX_SIG_ALG_NAME_LEN 7
#endif /* MAX_SIG_ALG_NAME_LEN */
#endif /* WITH_SIG_ECGDSA */
#ifdef WITH_SIG_ECRDSA
{.type = ECRDSA,
.name = "ECRDSA",
.siglen = ecrdsa_siglen,
.init_pub_key = ecrdsa_init_pub_key,
.sign_init = _ecrdsa_sign_init,
.sign_update = _ecrdsa_sign_update,
.sign_finalize = _ecrdsa_sign_finalize,
.verify_init = _ecrdsa_verify_init,
.verify_update = _ecrdsa_verify_update,
.verify_finalize = _ecrdsa_verify_finalize,
},
#if (MAX_SIG_ALG_NAME_LEN < 7)
#undef MAX_SIG_ALG_NAME_LEN
#define MAX_SIG_ALG_NAME_LEN 7
#endif /* MAX_SIG_ALG_NAME_LEN */
#endif /* WITH_SIG_ECRDSA */
{.type = UNKNOWN_SIG_ALG, /* Needs to be kept last */
.name = "UNKNOWN",
.siglen = 0,
.init_pub_key = NULL,
.sign_init = NULL,
.sign_update = NULL,
.sign_finalize = NULL,
.verify_init = NULL,
.verify_update = NULL,
.verify_finalize = NULL,
},
};
/*
* For a given raw signature, the structured version is produced by prepending
* three bytes providing specific sig alg, hash alg and curve.
*/
#define EC_STRUCTURED_SIG_EXPORT_SIZE(siglen) ((siglen) + (3 * sizeof(u8)))
#endif /* __SIG_ALGS_INTERNAL_H__ */