-
Notifications
You must be signed in to change notification settings - Fork 8
/
ecdh.c
331 lines (284 loc) · 12.5 KB
/
ecdh.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
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Example Elliptic Curve Diffie-Hellman (ECDH) key exchange program
*
* This file is a modification of the ecdh_curve25519.c example which ships with mbedTLS to use different curves
*
* Curve25519 is very fast, but only uses 256 bits (128 bits of security) even though it is highly respected as being
* safe by pretty much everyone. This curve is suitable for an asymmetric ECDH key exchange used to derive a 128-bit
* key for use with a symmetric cipher such as AES-128. Python's Cryptography module doesn't have support for curve25519
* until version 2.0 and even then it only supports it with a bleeding-edge version of OpenSSL.
*
* Elliptic Curve SECP384R1 is a 384-bit NIST curve over a prime field. This is a curve with intermediate performance
* and intermediate security. It should be suitable for an asymmetric ECDH key exchange used to derive a 192-bit key
* for use with a symmetric cipher such as AES-192. Python's Cryptography module has support for this curve in all
* recent versions. The "SafeCurves" website specifically marks this curve as unsafe. NSA "Suite B" includes this
* curve in the list of recommended curves.
*
* Elliptic Curve SECP521R1 is a 521-bit NIST curve over a prime field. This is slower than most other recommended
* curves due to the larger bit size, but should be very secure and suitable for an asymmetric ECDH key exchange used to
* derive a 256-bit key for use with a symmetric cipher such as AES-256. Python's Cryptography module has support for
* this curve in all recent versions. The "SafeCurves" website doesn't comment on this particular NIST curve in any
* way shape or form. Notably, this particular NIST curve is not included in the "NSA Suite B" set of recommended
* curves, ostensibly because Suite B was only shooting for 192 bits of security and this curve would be overkill for
* that.
*/
#if !defined(MBEDTLS_CONFIG_FILE)
#include "mbedtls/config.h"
#else
#include MBEDTLS_CONFIG_FILE
#endif
#if defined(MBEDTLS_PLATFORM_C)
#include "mbedtls/platform.h"
#else
#include <stdio.h>
#define mbedtls_printf printf
#endif
// Define which Elliptic Curve we wish to use
//#define ELLIPTIC_CURVE MBEDTLS_ECP_DP_CURVE25519
//#define ELLIPTIC_CURVE MBEDTLS_ECP_DP_SECP384R1
#define ELLIPTIC_CURVE MBEDTLS_ECP_DP_SECP521R1
// Size of buffer used to store the public keys exchanged between the client and sever
// Buffer size should be the following:
// Curve Public Key Buffer Size
// ----- ----------------------
// 25519 32
// SECP384 48
// SECP521 66
#define BUF_BYTES 66 // Safe to use the largest buffer size
// Size of buffer used to translate mbed TLS error codes into a string representation
#define MBED_ERR_BUF 80
#if !defined(MBEDTLS_ECDH_C) || \
!defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || \
!defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_CTR_DRBG_C)
int main( void )
{
mbedtls_printf( "MBEDTLS_ECDH_C and/or "
"MBEDTLS_ECP_DP_SECP521R1_ENABLED and/or "
"MBEDTLS_ENTROPY_C and/or MBEDTLS_CTR_DRBG_C "
"not defined\n" );
return( 0 );
}
#else
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/ecdh.h"
#include "mbedtls/error.h"
int main( int argc, char *argv[] )
{
int ret;
mbedtls_ecdh_context ctx_cli, ctx_srv;
mbedtls_entropy_context entropy;
mbedtls_ctr_drbg_context ctr_drbg;
unsigned char cli_to_srv_x[BUF_BYTES];
unsigned char cli_to_srv_y[BUF_BYTES];
unsigned char srv_to_cli_x[BUF_BYTES];
unsigned char srv_to_cli_y[BUF_BYTES];
const char pers[] = "ecdh";
char mbed_err[MBED_ERR_BUF];
((void) argc);
((void) argv);
mbedtls_ecdh_init( &ctx_cli );
mbedtls_ecdh_init( &ctx_srv );
mbedtls_ctr_drbg_init( &ctr_drbg );
mbedtls_entropy_init( &entropy );
mbedtls_printf( "Using Elliptic Curve: ");
switch(ELLIPTIC_CURVE)
{
case MBEDTLS_ECP_DP_CURVE25519:
mbedtls_printf( "Curve25519 (offering 128 bits of security)\n" );
break;
case MBEDTLS_ECP_DP_SECP384R1:
mbedtls_printf( "SECP384R1 NIST P-384 (offering 192 bits of security)\n" );
break;
case MBEDTLS_ECP_DP_SECP521R1:
mbedtls_printf( "SECP521R1 NIST P-521 (offering 256 bits of security)\n" );
break;
default:
mbedtls_printf( "ERROR - Invalid Curve selected!\n" );
goto exit;
}
// Initialize random number generation
mbedtls_printf( " . Seeding the random number generator..." );
fflush( stdout );
ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, (const unsigned char *) pers, sizeof pers );
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
goto exit;
}
mbedtls_printf( " ok\n" );
// Client: initialize context and generate keypair
mbedtls_printf( " . Setting up client context..." );
fflush( stdout );
// Set a group (in the abstract algebra sense) using well-known domain parameters - configure elliptic curve used
ret = mbedtls_ecp_group_load( &ctx_cli.grp, // Destination group
ELLIPTIC_CURVE ); // Index in the list of well-known domain parameters
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
goto exit;
}
// Generate a public key
ret = mbedtls_ecdh_gen_public( &ctx_cli.grp, // ECP group
&ctx_cli.d, // Destination MPI (secret exponent, aka private key)
&ctx_cli.Q, // Destination point (public key)
mbedtls_ctr_drbg_random, // RNG function
&ctr_drbg ); //RNG parameter
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret );
goto exit;
}
// Export multi-precision integer (MPI) into unsigned binary data, big endian (X coordinate of ECP point)
ret = mbedtls_mpi_write_binary( &ctx_cli.Q.X, // Source MPI
cli_to_srv_x, // Output buffer
BUF_BYTES ); // Output buffer size
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
goto exit;
}
// Export multi-precision integer (MPI) into unsigned binary data, big endian (Y coordinate of ECP point)
ret = mbedtls_mpi_write_binary( &ctx_cli.Q.Y, // Source MPI
cli_to_srv_y, // Output buffer
BUF_BYTES ); // Output buffer size
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
goto exit;
}
mbedtls_printf( " ok\n" );
// Server: initialize context and generate keypair
mbedtls_printf( " . Setting up server context..." );
fflush( stdout );
// Set a group (in the abstract algebra sense) using well-known domain parameters - configure elliptic curve used
ret = mbedtls_ecp_group_load( &ctx_srv.grp, ELLIPTIC_CURVE );
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ecp_group_load returned %d\n", ret );
goto exit;
}
// Generate a public key
ret = mbedtls_ecdh_gen_public( &ctx_srv.grp, &ctx_srv.d, &ctx_srv.Q, mbedtls_ctr_drbg_random, &ctr_drbg );
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ecdh_gen_public returned %d\n", ret );
goto exit;
}
// Export multi-precision integer into unsigned binary data, big endian (X coordinate of ECP point)
ret = mbedtls_mpi_write_binary( &ctx_srv.Q.X, srv_to_cli_x, BUF_BYTES );
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
goto exit;
}
// Export multi-precision integer into unsigned binary data, big endian (Y coordinate of ECP point)
ret = mbedtls_mpi_write_binary( &ctx_srv.Q.Y, srv_to_cli_y, BUF_BYTES );
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_mpi_write_binary returned %d\n", ret );
goto exit;
}
mbedtls_printf( " ok\n" );
// Server: read peer's key and generate shared secret
mbedtls_printf( " . Server reading client key and computing secret..." );
fflush( stdout );
// Set the Z component of the peer's public value (public key) to 1
ret = mbedtls_mpi_lset( &ctx_srv.Qp.Z, // MPI to set
1 ); // Value to use
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret );
goto exit;
}
// Set the X component of the peer's public value based on what was passed from client in the buffer
ret = mbedtls_mpi_read_binary( &ctx_srv.Qp.X, // Destination MPI
cli_to_srv_x, // Input buffer
BUF_BYTES ); // Input buffer size
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
goto exit;
}
// Set the Y component of the peer's public value based on what was passed from client in the buffer
ret = mbedtls_mpi_read_binary( &ctx_srv.Qp.Y, // Destination MPI
cli_to_srv_y, // Input buffer
BUF_BYTES ); // Input buffer size
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
goto exit;
}
// Compute shared secret
ret = mbedtls_ecdh_compute_shared( &ctx_srv.grp, // ECP group
&ctx_srv.z, // Destination MPI (shared secret)
&ctx_srv.Qp, // Public key from other party
&ctx_srv.d, // Our secret exponent (private key)
mbedtls_ctr_drbg_random, // RNG function - countermeasure against timing attacks
&ctr_drbg ); // RNG parameter
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned -0x%04x\n", -ret );
goto exit;
}
mbedtls_printf( " ok\n" );
// Client: read peer's key and generate shared secret
mbedtls_printf( " . Client reading server key and computing secret..." );
fflush( stdout );
ret = mbedtls_mpi_lset( &ctx_cli.Qp.Z, 1 );
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_mpi_lset returned %d\n", ret );
goto exit;
}
ret = mbedtls_mpi_read_binary( &ctx_cli.Qp.X, srv_to_cli_x, BUF_BYTES );
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
goto exit;
}
ret = mbedtls_mpi_read_binary( &ctx_cli.Qp.Y, srv_to_cli_y, BUF_BYTES );
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_mpi_read_binary returned %d\n", ret );
goto exit;
}
ret = mbedtls_ecdh_compute_shared( &ctx_cli.grp, &ctx_cli.z, &ctx_cli.Qp, &ctx_cli.d,
mbedtls_ctr_drbg_random, &ctr_drbg );
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
goto exit;
}
mbedtls_printf( " ok\n" );
// Verification: are the computed secrets equal?
mbedtls_printf( " . Checking if both computed secrets are equal..." );
fflush( stdout );
// Compare two signed multi-precision integers
ret = mbedtls_mpi_cmp_mpi( &ctx_cli.z, &ctx_srv.z );
if( ret != 0 )
{
mbedtls_printf( " failed\n ! mbedtls_ecdh_compute_shared returned %d\n", ret );
goto exit;
}
mbedtls_printf( " ok\n" );
// TODO: Use a Key Derivation Function (KDF) to derive a 256-bit AES key and an IV from the 521-bit shared secret
exit:
// If there was an error, translate an mbed TLS error code into a string representation
if( ret != 0 )
{
mbedtls_strerror(ret, mbed_err, MBED_ERR_BUF);
mbedtls_printf( "mbedTLS ERROR: %s\n", mbed_err);
}
#if defined(_WIN32)
mbedtls_printf( " + Press Enter to exit this program.\n" );
fflush( stdout ); getchar();
#endif
mbedtls_ecdh_free( &ctx_srv );
mbedtls_ecdh_free( &ctx_cli );
mbedtls_ctr_drbg_free( &ctr_drbg );
mbedtls_entropy_free( &entropy );
return( ret != 0 );
}
#endif /* MBEDTLS_ECDH_C && MBEDTLS_ECP_DP_SECP521R1_ENABLED &&
MBEDTLS_ENTROPY_C && MBEDTLS_CTR_DRBG_C */