summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsing <>2015-09-13 13:51:13 +0000
committerjsing <>2015-09-13 13:51:13 +0000
commitf9283b7125226e020af908825ee7ea347e976ad2 (patch)
tree85ba8e668076b788f39061b0402d225fb34c5441
parentc4d22677da5e8b9a9a2a2f27a61bc5dc8ad73029 (diff)
downloadopenbsd-f9283b7125226e020af908825ee7ea347e976ad2.tar.gz
openbsd-f9283b7125226e020af908825ee7ea347e976ad2.tar.bz2
openbsd-f9283b7125226e020af908825ee7ea347e976ad2.zip
style(9) grenade.
-rw-r--r--src/regress/lib/libcrypto/ecdh/ecdhtest.c331
1 files changed, 178 insertions, 153 deletions
diff --git a/src/regress/lib/libcrypto/ecdh/ecdhtest.c b/src/regress/lib/libcrypto/ecdh/ecdhtest.c
index 427957f709..eac7dab426 100644
--- a/src/regress/lib/libcrypto/ecdh/ecdhtest.c
+++ b/src/regress/lib/libcrypto/ecdh/ecdhtest.c
@@ -21,7 +21,7 @@
21 * are met: 21 * are met:
22 * 22 *
23 * 1. Redistributions of source code must retain the above copyright 23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer. 24 * notice, this list of conditions and the following disclaimer.
25 * 25 *
26 * 2. Redistributions in binary form must reproduce the above copyright 26 * 2. Redistributions in binary form must reproduce the above copyright
27 * notice, this list of conditions and the following disclaimer in 27 * notice, this list of conditions and the following disclaimer in
@@ -82,8 +82,9 @@
82#include <openssl/ecdh.h> 82#include <openssl/ecdh.h>
83 83
84static const int KDF1_SHA1_len = 20; 84static const int KDF1_SHA1_len = 20;
85static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen) 85static void *
86 { 86KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
87{
87#ifndef OPENSSL_NO_SHA 88#ifndef OPENSSL_NO_SHA
88 if (*outlen < SHA_DIGEST_LENGTH) 89 if (*outlen < SHA_DIGEST_LENGTH)
89 return NULL; 90 return NULL;
@@ -93,18 +94,19 @@ static void *KDF1_SHA1(const void *in, size_t inlen, void *out, size_t *outlen)
93#else 94#else
94 return NULL; 95 return NULL;
95#endif 96#endif
96 } 97}
97 98
98 99
99static int test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO *out) 100static int
100 { 101test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO *out)
101 EC_KEY *a=NULL; 102{
102 EC_KEY *b=NULL; 103 EC_KEY *a = NULL;
103 BIGNUM *x_a=NULL, *y_a=NULL, 104 EC_KEY *b = NULL;
104 *x_b=NULL, *y_b=NULL; 105 BIGNUM *x_a = NULL, *y_a = NULL,
106 *x_b = NULL, *y_b = NULL;
105 char buf[12]; 107 char buf[12];
106 unsigned char *abuf=NULL,*bbuf=NULL; 108 unsigned char *abuf = NULL, *bbuf = NULL;
107 int i,alen,blen,aout,bout,ret=0; 109 int i, alen, blen, aout, bout, ret = 0;
108 const EC_GROUP *group; 110 const EC_GROUP *group;
109 111
110 a = EC_KEY_new_by_curve_name(nid); 112 a = EC_KEY_new_by_curve_name(nid);
@@ -114,66 +116,67 @@ static int test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO *out)
114 116
115 group = EC_KEY_get0_group(a); 117 group = EC_KEY_get0_group(a);
116 118
117 if ((x_a=BN_new()) == NULL) goto err; 119 if ((x_a = BN_new()) == NULL)
118 if ((y_a=BN_new()) == NULL) goto err; 120 goto err;
119 if ((x_b=BN_new()) == NULL) goto err; 121 if ((y_a = BN_new()) == NULL)
120 if ((y_b=BN_new()) == NULL) goto err; 122 goto err;
123 if ((x_b = BN_new()) == NULL)
124 goto err;
125 if ((y_b = BN_new()) == NULL)
126 goto err;
121 127
122 BIO_puts(out,"Testing key generation with "); 128 BIO_puts(out, "Testing key generation with ");
123 BIO_puts(out,text); 129 BIO_puts(out, text);
124 (void)BIO_flush(out); 130 (void)BIO_flush(out);
125 131
126 if (!EC_KEY_generate_key(a)) goto err; 132 if (!EC_KEY_generate_key(a))
127 133 goto err;
128 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) 134
129 { 135 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
130 if (!EC_POINT_get_affine_coordinates_GFp(group, 136 if (!EC_POINT_get_affine_coordinates_GFp(group,
131 EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err; 137 EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
132 } 138 }
133#ifndef OPENSSL_NO_EC2M 139#ifndef OPENSSL_NO_EC2M
134 else 140 else {
135 {
136 if (!EC_POINT_get_affine_coordinates_GF2m(group, 141 if (!EC_POINT_get_affine_coordinates_GF2m(group,
137 EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err; 142 EC_KEY_get0_public_key(a), x_a, y_a, ctx)) goto err;
138 } 143 }
139#endif 144#endif
140 BIO_printf(out," ."); 145 BIO_printf(out, " .");
141 (void)BIO_flush(out); 146 (void)BIO_flush(out);
142 147
143 if (!EC_KEY_generate_key(b)) goto err; 148 if (!EC_KEY_generate_key(b))
149 goto err;
144 150
145 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) 151 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field) {
146 { 152 if (!EC_POINT_get_affine_coordinates_GFp(group,
147 if (!EC_POINT_get_affine_coordinates_GFp(group, 153 EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
148 EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err; 154 }
149 }
150#ifndef OPENSSL_NO_EC2M 155#ifndef OPENSSL_NO_EC2M
151 else 156 else {
152 { 157 if (!EC_POINT_get_affine_coordinates_GF2m(group,
153 if (!EC_POINT_get_affine_coordinates_GF2m(group, 158 EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err;
154 EC_KEY_get0_public_key(b), x_b, y_b, ctx)) goto err; 159 }
155 }
156#endif 160#endif
157 161
158 BIO_printf(out,"."); 162 BIO_printf(out, ".");
159 (void)BIO_flush(out); 163 (void)BIO_flush(out);
160 164
161 alen=KDF1_SHA1_len; 165 alen = KDF1_SHA1_len;
162 abuf=malloc(alen); 166 abuf = malloc(alen);
163 aout=ECDH_compute_key(abuf,alen,EC_KEY_get0_public_key(b),a,KDF1_SHA1); 167 aout = ECDH_compute_key(abuf, alen, EC_KEY_get0_public_key(b), a, KDF1_SHA1);
164 168
165 BIO_printf(out,"."); 169 BIO_printf(out, ".");
166 (void)BIO_flush(out); 170 (void)BIO_flush(out);
167 171
168 blen=KDF1_SHA1_len; 172 blen = KDF1_SHA1_len;
169 bbuf=malloc(blen); 173 bbuf = malloc(blen);
170 bout=ECDH_compute_key(bbuf,blen,EC_KEY_get0_public_key(a),b,KDF1_SHA1); 174 bout = ECDH_compute_key(bbuf, blen, EC_KEY_get0_public_key(a), b, KDF1_SHA1);
171 175
172 BIO_printf(out,"."); 176 BIO_printf(out, ".");
173 (void)BIO_flush(out); 177 (void)BIO_flush(out);
174 178
175 if ((aout < 4) || (bout != aout) || (memcmp(abuf,bbuf,aout) != 0)) 179 if ((aout < 4) || (bout != aout) || (memcmp(abuf, bbuf, aout) != 0)) {
176 {
177 BIO_printf(out, " failed\n\n"); 180 BIO_printf(out, " failed\n\n");
178 BIO_printf(out, "key a:\n"); 181 BIO_printf(out, "key a:\n");
179 BIO_printf(out, "private key: "); 182 BIO_printf(out, "private key: ");
@@ -193,114 +196,117 @@ static int test_ecdh_curve(int nid, const char *text, BN_CTX *ctx, BIO *out)
193 BN_print(out, y_b); 196 BN_print(out, y_b);
194 BIO_printf(out, "\n"); 197 BIO_printf(out, "\n");
195 BIO_printf(out, "generated key a: "); 198 BIO_printf(out, "generated key a: ");
196 for (i=0; i<bout; i++) 199 for (i = 0; i < bout; i++) {
197 {
198 snprintf(buf, sizeof buf, "%02X", bbuf[i]); 200 snprintf(buf, sizeof buf, "%02X", bbuf[i]);
199 BIO_puts(out, buf); 201 BIO_puts(out, buf);
200 } 202 }
201 BIO_printf(out, "\n"); 203 BIO_printf(out, "\n");
202 BIO_printf(out, "generated key b: "); 204 BIO_printf(out, "generated key b: ");
203 for (i=0; i<aout; i++) 205 for (i = 0; i < aout; i++) {
204 {
205 snprintf(buf, sizeof buf, "%02X", abuf[i]); 206 snprintf(buf, sizeof buf, "%02X", abuf[i]);
206 BIO_puts(out,buf); 207 BIO_puts(out, buf);
207 }
208 BIO_printf(out, "\n");
209 fprintf(stderr,"Error in ECDH routines\n");
210 ret=0;
211 } 208 }
212 else 209 BIO_printf(out, "\n");
213 { 210 fprintf(stderr, "Error in ECDH routines\n");
211 ret = 0;
212 } else {
214 BIO_printf(out, " ok\n"); 213 BIO_printf(out, " ok\n");
215 ret=1; 214 ret = 1;
216 } 215 }
217err: 216err:
218 ERR_print_errors_fp(stderr); 217 ERR_print_errors_fp(stderr);
219 218
220 free(abuf); 219 free(abuf);
221 free(bbuf); 220 free(bbuf);
222 if (x_a) BN_free(x_a); 221 if (x_a)
223 if (y_a) BN_free(y_a); 222 BN_free(x_a);
224 if (x_b) BN_free(x_b); 223 if (y_a)
225 if (y_b) BN_free(y_b); 224 BN_free(y_a);
226 if (b) EC_KEY_free(b); 225 if (x_b)
227 if (a) EC_KEY_free(a); 226 BN_free(x_b);
228 return(ret); 227 if (y_b)
229 } 228 BN_free(y_b);
229 if (b)
230 EC_KEY_free(b);
231 if (a)
232 EC_KEY_free(a);
233 return (ret);
234}
230 235
231/* Keys and shared secrets from RFC 7027 */ 236/* Keys and shared secrets from RFC 7027 */
232 237
233static const unsigned char bp256_da[] = { 238static const unsigned char bp256_da[] = {
234 0x81,0xDB,0x1E,0xE1,0x00,0x15,0x0F,0xF2,0xEA,0x33,0x8D,0x70, 239 0x81, 0xDB, 0x1E, 0xE1, 0x00, 0x15, 0x0F, 0xF2, 0xEA, 0x33, 0x8D, 0x70,
235 0x82,0x71,0xBE,0x38,0x30,0x0C,0xB5,0x42,0x41,0xD7,0x99,0x50, 240 0x82, 0x71, 0xBE, 0x38, 0x30, 0x0C, 0xB5, 0x42, 0x41, 0xD7, 0x99, 0x50,
236 0xF7,0x7B,0x06,0x30,0x39,0x80,0x4F,0x1D 241 0xF7, 0x7B, 0x06, 0x30, 0x39, 0x80, 0x4F, 0x1D
237}; 242};
238 243
239static const unsigned char bp256_db[] = { 244static const unsigned char bp256_db[] = {
240 0x55,0xE4,0x0B,0xC4,0x1E,0x37,0xE3,0xE2,0xAD,0x25,0xC3,0xC6, 245 0x55, 0xE4, 0x0B, 0xC4, 0x1E, 0x37, 0xE3, 0xE2, 0xAD, 0x25, 0xC3, 0xC6,
241 0x65,0x45,0x11,0xFF,0xA8,0x47,0x4A,0x91,0xA0,0x03,0x20,0x87, 246 0x65, 0x45, 0x11, 0xFF, 0xA8, 0x47, 0x4A, 0x91, 0xA0, 0x03, 0x20, 0x87,
242 0x59,0x38,0x52,0xD3,0xE7,0xD7,0x6B,0xD3 247 0x59, 0x38, 0x52, 0xD3, 0xE7, 0xD7, 0x6B, 0xD3
243}; 248};
244 249
245static const unsigned char bp256_Z[] = { 250static const unsigned char bp256_Z[] = {
246 0x89,0xAF,0xC3,0x9D,0x41,0xD3,0xB3,0x27,0x81,0x4B,0x80,0x94, 251 0x89, 0xAF, 0xC3, 0x9D, 0x41, 0xD3, 0xB3, 0x27, 0x81, 0x4B, 0x80, 0x94,
247 0x0B,0x04,0x25,0x90,0xF9,0x65,0x56,0xEC,0x91,0xE6,0xAE,0x79, 252 0x0B, 0x04, 0x25, 0x90, 0xF9, 0x65, 0x56, 0xEC, 0x91, 0xE6, 0xAE, 0x79,
248 0x39,0xBC,0xE3,0x1F,0x3A,0x18,0xBF,0x2B 253 0x39, 0xBC, 0xE3, 0x1F, 0x3A, 0x18, 0xBF, 0x2B
249}; 254};
250 255
251static const unsigned char bp384_da[] = { 256static const unsigned char bp384_da[] = {
252 0x1E,0x20,0xF5,0xE0,0x48,0xA5,0x88,0x6F,0x1F,0x15,0x7C,0x74, 257 0x1E, 0x20, 0xF5, 0xE0, 0x48, 0xA5, 0x88, 0x6F, 0x1F, 0x15, 0x7C, 0x74,
253 0xE9,0x1B,0xDE,0x2B,0x98,0xC8,0xB5,0x2D,0x58,0xE5,0x00,0x3D, 258 0xE9, 0x1B, 0xDE, 0x2B, 0x98, 0xC8, 0xB5, 0x2D, 0x58, 0xE5, 0x00, 0x3D,
254 0x57,0x05,0x3F,0xC4,0xB0,0xBD,0x65,0xD6,0xF1,0x5E,0xB5,0xD1, 259 0x57, 0x05, 0x3F, 0xC4, 0xB0, 0xBD, 0x65, 0xD6, 0xF1, 0x5E, 0xB5, 0xD1,
255 0xEE,0x16,0x10,0xDF,0x87,0x07,0x95,0x14,0x36,0x27,0xD0,0x42 260 0xEE, 0x16, 0x10, 0xDF, 0x87, 0x07, 0x95, 0x14, 0x36, 0x27, 0xD0, 0x42
256}; 261};
257 262
258static const unsigned char bp384_db[] = { 263static const unsigned char bp384_db[] = {
259 0x03,0x26,0x40,0xBC,0x60,0x03,0xC5,0x92,0x60,0xF7,0x25,0x0C, 264 0x03, 0x26, 0x40, 0xBC, 0x60, 0x03, 0xC5, 0x92, 0x60, 0xF7, 0x25, 0x0C,
260 0x3D,0xB5,0x8C,0xE6,0x47,0xF9,0x8E,0x12,0x60,0xAC,0xCE,0x4A, 265 0x3D, 0xB5, 0x8C, 0xE6, 0x47, 0xF9, 0x8E, 0x12, 0x60, 0xAC, 0xCE, 0x4A,
261 0xCD,0xA3,0xDD,0x86,0x9F,0x74,0xE0,0x1F,0x8B,0xA5,0xE0,0x32, 266 0xCD, 0xA3, 0xDD, 0x86, 0x9F, 0x74, 0xE0, 0x1F, 0x8B, 0xA5, 0xE0, 0x32,
262 0x43,0x09,0xDB,0x6A,0x98,0x31,0x49,0x7A,0xBA,0xC9,0x66,0x70 267 0x43, 0x09, 0xDB, 0x6A, 0x98, 0x31, 0x49, 0x7A, 0xBA, 0xC9, 0x66, 0x70
263}; 268};
264 269
265static const unsigned char bp384_Z[] = { 270static const unsigned char bp384_Z[] = {
266 0x0B,0xD9,0xD3,0xA7,0xEA,0x0B,0x3D,0x51,0x9D,0x09,0xD8,0xE4, 271 0x0B, 0xD9, 0xD3, 0xA7, 0xEA, 0x0B, 0x3D, 0x51, 0x9D, 0x09, 0xD8, 0xE4,
267 0x8D,0x07,0x85,0xFB,0x74,0x4A,0x6B,0x35,0x5E,0x63,0x04,0xBC, 272 0x8D, 0x07, 0x85, 0xFB, 0x74, 0x4A, 0x6B, 0x35, 0x5E, 0x63, 0x04, 0xBC,
268 0x51,0xC2,0x29,0xFB,0xBC,0xE2,0x39,0xBB,0xAD,0xF6,0x40,0x37, 273 0x51, 0xC2, 0x29, 0xFB, 0xBC, 0xE2, 0x39, 0xBB, 0xAD, 0xF6, 0x40, 0x37,
269 0x15,0xC3,0x5D,0x4F,0xB2,0xA5,0x44,0x4F,0x57,0x5D,0x4F,0x42 274 0x15, 0xC3, 0x5D, 0x4F, 0xB2, 0xA5, 0x44, 0x4F, 0x57, 0x5D, 0x4F, 0x42
270}; 275};
271 276
272static const unsigned char bp512_da[] = { 277static const unsigned char bp512_da[] = {
273 0x16,0x30,0x2F,0xF0,0xDB,0xBB,0x5A,0x8D,0x73,0x3D,0xAB,0x71, 278 0x16, 0x30, 0x2F, 0xF0, 0xDB, 0xBB, 0x5A, 0x8D, 0x73, 0x3D, 0xAB, 0x71,
274 0x41,0xC1,0xB4,0x5A,0xCB,0xC8,0x71,0x59,0x39,0x67,0x7F,0x6A, 279 0x41, 0xC1, 0xB4, 0x5A, 0xCB, 0xC8, 0x71, 0x59, 0x39, 0x67, 0x7F, 0x6A,
275 0x56,0x85,0x0A,0x38,0xBD,0x87,0xBD,0x59,0xB0,0x9E,0x80,0x27, 280 0x56, 0x85, 0x0A, 0x38, 0xBD, 0x87, 0xBD, 0x59, 0xB0, 0x9E, 0x80, 0x27,
276 0x96,0x09,0xFF,0x33,0x3E,0xB9,0xD4,0xC0,0x61,0x23,0x1F,0xB2, 281 0x96, 0x09, 0xFF, 0x33, 0x3E, 0xB9, 0xD4, 0xC0, 0x61, 0x23, 0x1F, 0xB2,
277 0x6F,0x92,0xEE,0xB0,0x49,0x82,0xA5,0xF1,0xD1,0x76,0x4C,0xAD, 282 0x6F, 0x92, 0xEE, 0xB0, 0x49, 0x82, 0xA5, 0xF1, 0xD1, 0x76, 0x4C, 0xAD,
278 0x57,0x66,0x54,0x22 283 0x57, 0x66, 0x54, 0x22
279}; 284};
280 285
281static const unsigned char bp512_db[] = { 286static const unsigned char bp512_db[] = {
282 0x23,0x0E,0x18,0xE1,0xBC,0xC8,0x8A,0x36,0x2F,0xA5,0x4E,0x4E, 287 0x23, 0x0E, 0x18, 0xE1, 0xBC, 0xC8, 0x8A, 0x36, 0x2F, 0xA5, 0x4E, 0x4E,
283 0xA3,0x90,0x20,0x09,0x29,0x2F,0x7F,0x80,0x33,0x62,0x4F,0xD4, 288 0xA3, 0x90, 0x20, 0x09, 0x29, 0x2F, 0x7F, 0x80, 0x33, 0x62, 0x4F, 0xD4,
284 0x71,0xB5,0xD8,0xAC,0xE4,0x9D,0x12,0xCF,0xAB,0xBC,0x19,0x96, 289 0x71, 0xB5, 0xD8, 0xAC, 0xE4, 0x9D, 0x12, 0xCF, 0xAB, 0xBC, 0x19, 0x96,
285 0x3D,0xAB,0x8E,0x2F,0x1E,0xBA,0x00,0xBF,0xFB,0x29,0xE4,0xD7, 290 0x3D, 0xAB, 0x8E, 0x2F, 0x1E, 0xBA, 0x00, 0xBF, 0xFB, 0x29, 0xE4, 0xD7,
286 0x2D,0x13,0xF2,0x22,0x45,0x62,0xF4,0x05,0xCB,0x80,0x50,0x36, 291 0x2D, 0x13, 0xF2, 0x22, 0x45, 0x62, 0xF4, 0x05, 0xCB, 0x80, 0x50, 0x36,
287 0x66,0xB2,0x54,0x29 292 0x66, 0xB2, 0x54, 0x29
288}; 293};
289 294
290 295
291static const unsigned char bp512_Z[] = { 296static const unsigned char bp512_Z[] = {
292 0xA7,0x92,0x70,0x98,0x65,0x5F,0x1F,0x99,0x76,0xFA,0x50,0xA9, 297 0xA7, 0x92, 0x70, 0x98, 0x65, 0x5F, 0x1F, 0x99, 0x76, 0xFA, 0x50, 0xA9,
293 0xD5,0x66,0x86,0x5D,0xC5,0x30,0x33,0x18,0x46,0x38,0x1C,0x87, 298 0xD5, 0x66, 0x86, 0x5D, 0xC5, 0x30, 0x33, 0x18, 0x46, 0x38, 0x1C, 0x87,
294 0x25,0x6B,0xAF,0x32,0x26,0x24,0x4B,0x76,0xD3,0x64,0x03,0xC0, 299 0x25, 0x6B, 0xAF, 0x32, 0x26, 0x24, 0x4B, 0x76, 0xD3, 0x64, 0x03, 0xC0,
295 0x24,0xD7,0xBB,0xF0,0xAA,0x08,0x03,0xEA,0xFF,0x40,0x5D,0x3D, 300 0x24, 0xD7, 0xBB, 0xF0, 0xAA, 0x08, 0x03, 0xEA, 0xFF, 0x40, 0x5D, 0x3D,
296 0x24,0xF1,0x1A,0x9B,0x5C,0x0B,0xEF,0x67,0x9F,0xE1,0x45,0x4B, 301 0x24, 0xF1, 0x1A, 0x9B, 0x5C, 0x0B, 0xEF, 0x67, 0x9F, 0xE1, 0x45, 0x4B,
297 0x21,0xC4,0xCD,0x1F 302 0x21, 0xC4, 0xCD, 0x1F
298}; 303};
299 304
300/* Given private value and NID, create EC_KEY structure */ 305/* Given private value and NID, create EC_KEY structure */
301 306
302static EC_KEY *mk_eckey(int nid, const unsigned char *p, size_t plen) 307static EC_KEY *
303 { 308mk_eckey(int nid, const unsigned char *p, size_t plen)
309{
304 int ok = 0; 310 int ok = 0;
305 EC_KEY *k = NULL; 311 EC_KEY *k = NULL;
306 BIGNUM *priv = NULL; 312 BIGNUM *priv = NULL;
@@ -323,7 +329,7 @@ static EC_KEY *mk_eckey(int nid, const unsigned char *p, size_t plen)
323 if (!EC_KEY_set_public_key(k, pub)) 329 if (!EC_KEY_set_public_key(k, pub))
324 goto err; 330 goto err;
325 ok = 1; 331 ok = 1;
326 err: 332err:
327 if (priv) 333 if (priv)
328 BN_clear_free(priv); 334 BN_clear_free(priv);
329 if (pub) 335 if (pub)
@@ -333,17 +339,18 @@ static EC_KEY *mk_eckey(int nid, const unsigned char *p, size_t plen)
333 else if (k) 339 else if (k)
334 EC_KEY_free(k); 340 EC_KEY_free(k);
335 return NULL; 341 return NULL;
336 } 342}
337 343
338/* Known answer test: compute shared secret and check it matches 344/* Known answer test: compute shared secret and check it matches
339 * expected value. 345 * expected value.
340 */ 346 */
341 347
342static int ecdh_kat(BIO *out, const char *cname, int nid, 348static int
343 const unsigned char *k1, size_t k1_len, 349ecdh_kat(BIO *out, const char *cname, int nid,
344 const unsigned char *k2, size_t k2_len, 350 const unsigned char *k1, size_t k1_len,
345 const unsigned char *Z, size_t Zlen) 351 const unsigned char *k2, size_t k2_len,
346 { 352 const unsigned char *Z, size_t Zlen)
353{
347 int rv = 0; 354 int rv = 0;
348 EC_KEY *key1 = NULL, *key2 = NULL; 355 EC_KEY *key1 = NULL, *key2 = NULL;
349 unsigned char *Ztmp = NULL; 356 unsigned char *Ztmp = NULL;
@@ -359,18 +366,18 @@ static int ecdh_kat(BIO *out, const char *cname, int nid,
359 goto err; 366 goto err;
360 Ztmp = malloc(Ztmplen); 367 Ztmp = malloc(Ztmplen);
361 if (!ECDH_compute_key(Ztmp, Ztmplen, 368 if (!ECDH_compute_key(Ztmp, Ztmplen,
362 EC_KEY_get0_public_key(key2), key1, 0)) 369 EC_KEY_get0_public_key(key2), key1, 0))
363 goto err; 370 goto err;
364 if (memcmp(Ztmp, Z, Zlen)) 371 if (memcmp(Ztmp, Z, Zlen))
365 goto err; 372 goto err;
366 memset(Ztmp, 0, Zlen); 373 memset(Ztmp, 0, Zlen);
367 if (!ECDH_compute_key(Ztmp, Ztmplen, 374 if (!ECDH_compute_key(Ztmp, Ztmplen,
368 EC_KEY_get0_public_key(key1), key2, 0)) 375 EC_KEY_get0_public_key(key1), key2, 0))
369 goto err; 376 goto err;
370 if (memcmp(Ztmp, Z, Zlen)) 377 if (memcmp(Ztmp, Z, Zlen))
371 goto err; 378 goto err;
372 rv = 1; 379 rv = 1;
373 err: 380err:
374 if (key1) 381 if (key1)
375 EC_KEY_free(key1); 382 EC_KEY_free(key1);
376 if (key2) 383 if (key2)
@@ -378,13 +385,12 @@ static int ecdh_kat(BIO *out, const char *cname, int nid,
378 free(Ztmp); 385 free(Ztmp);
379 if (rv) 386 if (rv)
380 BIO_puts(out, " ok\n"); 387 BIO_puts(out, " ok\n");
381 else 388 else {
382 {
383 fprintf(stderr, "Error in ECDH routines\n"); 389 fprintf(stderr, "Error in ECDH routines\n");
384 ERR_print_errors_fp(stderr); 390 ERR_print_errors_fp(stderr);
385 }
386 return rv;
387 } 391 }
392 return rv;
393}
388 394
389#define test_ecdh_kat(bio, curve, bits) \ 395#define test_ecdh_kat(bio, curve, bits) \
390 ecdh_kat(bio, curve, NID_brainpoolP##bits##r1, \ 396 ecdh_kat(bio, curve, NID_brainpoolP##bits##r1, \
@@ -392,36 +398,54 @@ static int ecdh_kat(BIO *out, const char *cname, int nid,
392 bp##bits##_db, sizeof(bp##bits##_db), \ 398 bp##bits##_db, sizeof(bp##bits##_db), \
393 bp##bits##_Z, sizeof(bp##bits##_Z)) 399 bp##bits##_Z, sizeof(bp##bits##_Z))
394 400
395int main(int argc, char *argv[]) 401int
396 { 402main(int argc, char *argv[])
397 BN_CTX *ctx=NULL; 403{
398 int ret=1; 404 BN_CTX *ctx = NULL;
405 int ret = 1;
399 BIO *out; 406 BIO *out;
400 407
401 out=BIO_new(BIO_s_file()); 408 out = BIO_new(BIO_s_file());
402 if (out == NULL) exit(1); 409 if (out == NULL)
403 BIO_set_fp(out,stdout,BIO_NOCLOSE); 410 exit(1);
411 BIO_set_fp(out, stdout, BIO_NOCLOSE);
404 412
405 if ((ctx=BN_CTX_new()) == NULL) goto err; 413 if ((ctx = BN_CTX_new()) == NULL)
414 goto err;
406 415
407 /* NIST PRIME CURVES TESTS */ 416 /* NIST PRIME CURVES TESTS */
408 if (!test_ecdh_curve(NID_X9_62_prime192v1, "NIST Prime-Curve P-192", ctx, out)) goto err; 417 if (!test_ecdh_curve(NID_X9_62_prime192v1, "NIST Prime-Curve P-192", ctx, out))
409 if (!test_ecdh_curve(NID_secp224r1, "NIST Prime-Curve P-224", ctx, out)) goto err; 418 goto err;
410 if (!test_ecdh_curve(NID_X9_62_prime256v1, "NIST Prime-Curve P-256", ctx, out)) goto err; 419 if (!test_ecdh_curve(NID_secp224r1, "NIST Prime-Curve P-224", ctx, out))
411 if (!test_ecdh_curve(NID_secp384r1, "NIST Prime-Curve P-384", ctx, out)) goto err; 420 goto err;
412 if (!test_ecdh_curve(NID_secp521r1, "NIST Prime-Curve P-521", ctx, out)) goto err; 421 if (!test_ecdh_curve(NID_X9_62_prime256v1, "NIST Prime-Curve P-256", ctx, out))
422 goto err;
423 if (!test_ecdh_curve(NID_secp384r1, "NIST Prime-Curve P-384", ctx, out))
424 goto err;
425 if (!test_ecdh_curve(NID_secp521r1, "NIST Prime-Curve P-521", ctx, out))
426 goto err;
413#ifndef OPENSSL_NO_EC2M 427#ifndef OPENSSL_NO_EC2M
414 /* NIST BINARY CURVES TESTS */ 428 /* NIST BINARY CURVES TESTS */
415 if (!test_ecdh_curve(NID_sect163k1, "NIST Binary-Curve K-163", ctx, out)) goto err; 429 if (!test_ecdh_curve(NID_sect163k1, "NIST Binary-Curve K-163", ctx, out))
416 if (!test_ecdh_curve(NID_sect163r2, "NIST Binary-Curve B-163", ctx, out)) goto err; 430 goto err;
417 if (!test_ecdh_curve(NID_sect233k1, "NIST Binary-Curve K-233", ctx, out)) goto err; 431 if (!test_ecdh_curve(NID_sect163r2, "NIST Binary-Curve B-163", ctx, out))
418 if (!test_ecdh_curve(NID_sect233r1, "NIST Binary-Curve B-233", ctx, out)) goto err; 432 goto err;
419 if (!test_ecdh_curve(NID_sect283k1, "NIST Binary-Curve K-283", ctx, out)) goto err; 433 if (!test_ecdh_curve(NID_sect233k1, "NIST Binary-Curve K-233", ctx, out))
420 if (!test_ecdh_curve(NID_sect283r1, "NIST Binary-Curve B-283", ctx, out)) goto err; 434 goto err;
421 if (!test_ecdh_curve(NID_sect409k1, "NIST Binary-Curve K-409", ctx, out)) goto err; 435 if (!test_ecdh_curve(NID_sect233r1, "NIST Binary-Curve B-233", ctx, out))
422 if (!test_ecdh_curve(NID_sect409r1, "NIST Binary-Curve B-409", ctx, out)) goto err; 436 goto err;
423 if (!test_ecdh_curve(NID_sect571k1, "NIST Binary-Curve K-571", ctx, out)) goto err; 437 if (!test_ecdh_curve(NID_sect283k1, "NIST Binary-Curve K-283", ctx, out))
424 if (!test_ecdh_curve(NID_sect571r1, "NIST Binary-Curve B-571", ctx, out)) goto err; 438 goto err;
439 if (!test_ecdh_curve(NID_sect283r1, "NIST Binary-Curve B-283", ctx, out))
440 goto err;
441 if (!test_ecdh_curve(NID_sect409k1, "NIST Binary-Curve K-409", ctx, out))
442 goto err;
443 if (!test_ecdh_curve(NID_sect409r1, "NIST Binary-Curve B-409", ctx, out))
444 goto err;
445 if (!test_ecdh_curve(NID_sect571k1, "NIST Binary-Curve K-571", ctx, out))
446 goto err;
447 if (!test_ecdh_curve(NID_sect571r1, "NIST Binary-Curve B-571", ctx, out))
448 goto err;
425#endif 449#endif
426 if (!test_ecdh_kat(out, "Brainpool Prime-Curve brainpoolP256r1", 256)) 450 if (!test_ecdh_kat(out, "Brainpool Prime-Curve brainpoolP256r1", 256))
427 goto err; 451 goto err;
@@ -434,10 +458,11 @@ int main(int argc, char *argv[])
434 458
435err: 459err:
436 ERR_print_errors_fp(stderr); 460 ERR_print_errors_fp(stderr);
437 if (ctx) BN_CTX_free(ctx); 461 if (ctx)
462 BN_CTX_free(ctx);
438 BIO_free(out); 463 BIO_free(out);
439 CRYPTO_cleanup_all_ex_data(); 464 CRYPTO_cleanup_all_ex_data();
440 ERR_remove_thread_state(NULL); 465 ERR_remove_thread_state(NULL);
441 CRYPTO_mem_leaks_fp(stderr); 466 CRYPTO_mem_leaks_fp(stderr);
442 exit(ret); 467 exit(ret);
443 } 468}