-
Notifications
You must be signed in to change notification settings - Fork 32
/
kex.c
251 lines (194 loc) · 9.63 KB
/
kex.c
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
/********************************************************************************
* FourQlib: a high-performance crypto library based on the elliptic curve FourQ
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* Abstract: Diffie-Hellman key exchange based on FourQ, including countermeasures
* against side-channel attacks
* option 1: co-factor ecdh using compressed 32-byte public keys,
* (see https://datatracker.ietf.org/doc/draft-ladd-cfrg-4q/).
* option 2: co-factor ecdh using uncompressed, 64-byte public keys.
*********************************************************************************/
#include "FourQ_internal.h"
#include "FourQ_params.h"
#include "../random/random.h"
#include <string.h>
static __inline bool is_neutral_point(point_t P)
{ // Is P the neutral point (0,1)?
// SECURITY NOTE: this function does not run in constant time (input point P is assumed to be public).
if (is_zero_ct((digit_t*)P->x, 2*NWORDS_FIELD) && is_zero_ct(&((digit_t*)P->y)[1], 2*NWORDS_FIELD-1) && is_digit_zero_ct(P->y[0][0] - 1)) {
return true;
}
return false;
}
/*************** ECDH USING COMPRESSED, 32-BYTE PUBLIC KEYS ***************/
ECCRYPTO_STATUS CompressedPublicKeyGeneration_SCA_secure(const unsigned char* SecretKey, unsigned char* PublicKey, unsigned char* BlindingPoint)
{ // Compressed public key generation for key exchange
// It produces a public key PublicKey, which is the encoding of P = SecretKey*G (G is the generator), and a blinding point BlindingPoint.
// Input: 32-byte SecretKey
// Output: 32-byte PublicKey and 64-byte BlindingPoint
point_t G, R;
point_extedwards_t S;
unsigned char SecretBlinding[32];
ECCRYPTO_STATUS Status = ECCRYPTO_ERROR_UNKNOWN;
Status = RandomBytesFunction(SecretBlinding, 32);
if (Status != ECCRYPTO_SUCCESS) {
goto cleanup;
}
// Set up an initial "weak" blinding point R
fp2copy1271((felm_t*)&GENERATOR_x[0], G->x);
fp2copy1271((felm_t*)&GENERATOR_y[0], G->y);
point_setup(G, S);
eccdouble(S);
eccnorm(S, R);
// Computing an initial blinding point. This computation itself is not protected with a secure point blinding
Status = ecc_mul_SCA_secure(G, R, (digit_t*)SecretBlinding, (point_affine*)BlindingPoint, false);
if (Status != ECCRYPTO_SUCCESS) {
goto cleanup;
}
Status = ecc_mul_SCA_secure(G, (point_affine*)BlindingPoint, (digit_t*)SecretKey, R, false); // Compute public key
if (Status != ECCRYPTO_SUCCESS) {
goto cleanup;
}
encode(R, PublicKey); // Encode public key
// Cleanup
clear_words((unsigned int*)SecretBlinding, 256/(sizeof(unsigned int)*8));
return ECCRYPTO_SUCCESS;
cleanup:
clear_words((unsigned int*)SecretBlinding, 256/(sizeof(unsigned int)*8));
clear_words((unsigned int*)BlindingPoint, 512/(sizeof(unsigned int)*8));
clear_words((unsigned int*)PublicKey, 256/(sizeof(unsigned int)*8));
return Status;
}
ECCRYPTO_STATUS CompressedKeyGeneration_SCA_secure(unsigned char* SecretKey, unsigned char* PublicKey, unsigned char* BlindingPoint)
{ // Keypair generation for key exchange. Public key is compressed to 32 bytes
// It produces a private key SecretKey, a public key PublicKey, which is the encoding of P = SecretKey*G (G is the generator), and a blinding point BlindingPoint.
// Outputs: 32-byte SecretKey, 32-byte PublicKey and 64-byte BlindingPoint
ECCRYPTO_STATUS Status = ECCRYPTO_ERROR_UNKNOWN;
Status = RandomBytesFunction(SecretKey, 32);
if (Status != ECCRYPTO_SUCCESS) {
goto cleanup;
}
Status = CompressedPublicKeyGeneration_SCA_secure(SecretKey, PublicKey, BlindingPoint);
if (Status != ECCRYPTO_SUCCESS) {
goto cleanup;
}
return ECCRYPTO_SUCCESS;
cleanup:
clear_words((unsigned int*)SecretKey, 256/(sizeof(unsigned int)*8));
clear_words((unsigned int*)PublicKey, 256/(sizeof(unsigned int)*8));
clear_words((unsigned int*)BlindingPoint, 512/(sizeof(unsigned int)*8));
return Status;
}
ECCRYPTO_STATUS CompressedSecretAgreement_SCA_secure(const unsigned char* SecretKey, const unsigned char* PublicKey, unsigned char* SharedSecret, unsigned char* BlindingPoint)
{ // Secret agreement computation for key exchange using a compressed, 32-byte public key
// The output is the y-coordinate of SecretKey*A, where A is the decoding of the public key PublicKey.
// Inputs: 32-byte SecretKey, 32-byte PublicKey and 64-byte BlindingPoint
// Output: 32-byte SharedSecret and updated BlindingPoint
point_t A;
ECCRYPTO_STATUS Status = ECCRYPTO_ERROR_UNKNOWN;
if ((PublicKey[15] & 0x80) != 0) { // Is bit128(PublicKey) = 0?
Status = ECCRYPTO_ERROR_INVALID_PARAMETER;
goto cleanup;
}
Status = decode(PublicKey, A); // Also verifies that A is on the curve. If it is not, it fails
if (Status != ECCRYPTO_SUCCESS) {
goto cleanup;
}
Status = ecc_mul_SCA_secure(A, (point_affine*)BlindingPoint, (digit_t*)SecretKey, A, true);
if (Status != ECCRYPTO_SUCCESS) {
goto cleanup;
}
if (is_neutral_point(A)) { // Is output = neutral point (0,1)?
Status = ECCRYPTO_ERROR_SHARED_KEY;
goto cleanup;
}
memmove(SharedSecret, (unsigned char*)A->y, 32);
return ECCRYPTO_SUCCESS;
cleanup:
clear_words((unsigned int*)SharedSecret, 256/(sizeof(unsigned int)*8));
return Status;
}
/*************** ECDH USING UNCOMPRESSED PUBLIC KEYS ***************/
ECCRYPTO_STATUS PublicKeyGeneration_SCA_secure(const unsigned char* SecretKey, unsigned char* PublicKey, unsigned char* BlindingPoint)
{ // Public key generation for key exchange
// It produces the public key PublicKey = SecretKey*G, where G is the generator, and a blinding point BlindingPoint.
// Input: 32-byte SecretKey
// Output: 64-byte PublicKey and 64-byte BlindingPoint
point_t G, R;
point_extedwards_t S;
unsigned char SecretBlinding[32];
ECCRYPTO_STATUS Status = ECCRYPTO_ERROR_UNKNOWN;
Status = RandomBytesFunction(SecretBlinding, 32);
if (Status != ECCRYPTO_SUCCESS) {
goto cleanup;
}
// Set up an initial "weak" blinding point R
fp2copy1271((felm_t*)&GENERATOR_x[0], G->x);
fp2copy1271((felm_t*)&GENERATOR_y[0], G->y);
point_setup(G, S);
eccdouble(S);
eccnorm(S, R);
// Computing an initial blinding point. This computation itself is not protected with a secure point blinding
Status = ecc_mul_SCA_secure(G, R, (digit_t*)SecretBlinding, (point_affine*)BlindingPoint, false);
if (Status != ECCRYPTO_SUCCESS) {
goto cleanup;
}
Status = ecc_mul_SCA_secure(G, (point_affine*)BlindingPoint, (digit_t*)SecretKey, (point_affine*)PublicKey, false); // Compute public key
if (Status != ECCRYPTO_SUCCESS) {
goto cleanup;
}
// Cleanup
clear_words((unsigned int*)SecretBlinding, 256/(sizeof(unsigned int)*8));
return ECCRYPTO_SUCCESS;
cleanup:
clear_words((unsigned int*)SecretBlinding, 256/(sizeof(unsigned int)*8));
clear_words((unsigned int*)BlindingPoint, 512/(sizeof(unsigned int)*8));
clear_words((unsigned int*)PublicKey, 512/(sizeof(unsigned int)*8));
return Status;
}
ECCRYPTO_STATUS KeyGeneration_SCA_secure(unsigned char* SecretKey, unsigned char* PublicKey, unsigned char* BlindingPoint)
{ // Keypair generation for key exchange
// It produces a private key SecretKey, the public key PublicKey = SecretKey*G, where G is the generator, and a blinding point BlindingPoint.
// Outputs: 32-byte SecretKey, 64-byte PublicKey and 64-byte BlindingPoint
ECCRYPTO_STATUS Status = ECCRYPTO_ERROR_UNKNOWN;
Status = RandomBytesFunction(SecretKey, 32);
if (Status != ECCRYPTO_SUCCESS) {
goto cleanup;
}
Status = PublicKeyGeneration_SCA_secure(SecretKey, PublicKey, BlindingPoint);
if (Status != ECCRYPTO_SUCCESS) {
goto cleanup;
}
return ECCRYPTO_SUCCESS;
cleanup:
clear_words((unsigned int*)SecretKey, 256/(sizeof(unsigned int)*8));
clear_words((unsigned int*)PublicKey, 512/(sizeof(unsigned int)*8));
clear_words((unsigned int*)BlindingPoint, 512/(sizeof(unsigned int)*8));
return Status;
}
ECCRYPTO_STATUS SecretAgreement_SCA_secure(const unsigned char* SecretKey, const unsigned char* PublicKey, unsigned char* SharedSecret, unsigned char* BlindingPoint)
{ // Secret agreement computation for key exchange
// The output is the y-coordinate of SecretKey*PublicKey.
// Inputs: 32-byte SecretKey, 64-byte PublicKey and 64-byte BlindingPoint
// Output: 32-byte SharedSecret and updated BlindingPoint
point_t A;
ECCRYPTO_STATUS Status = ECCRYPTO_ERROR_UNKNOWN;
if (((PublicKey[15] & 0x80) != 0) || ((PublicKey[31] & 0x80) != 0) || ((PublicKey[47] & 0x80) != 0) || ((PublicKey[63] & 0x80) != 0)) { // Are PublicKey_x[i] and PublicKey_y[i] < 2^127?
Status = ECCRYPTO_ERROR_INVALID_PARAMETER;
goto cleanup;
}
Status = ecc_mul_SCA_secure((point_affine*)PublicKey, (point_affine*)BlindingPoint, (digit_t*)SecretKey, A, true); // Also verifies that PublicKey and BlindingPoint are points on the curve. If not, it fails
if (Status != ECCRYPTO_SUCCESS) {
goto cleanup;
}
if (is_neutral_point(A)) { // Is output = neutral point (0,1)?
Status = ECCRYPTO_ERROR_SHARED_KEY;
goto cleanup;
}
memmove(SharedSecret, (unsigned char*)A->y, 32);
return ECCRYPTO_SUCCESS;
cleanup:
clear_words((unsigned int*)SharedSecret, 256/(sizeof(unsigned int)*8));
return Status;
}