Debian Codegraph @

external/boringssl/crypto/ec/ec_test.cc

raw
1 /* Copyright (c) 2014, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15 #include <stdio.h>
16 #include <string.h>
17
18 #include <vector>
19
20 #include <openssl/bytestring.h>
21 #include <openssl/crypto.h>
22 #include <openssl/ec_key.h>
23 #include <openssl/err.h>
24 #include <openssl/mem.h>
25
26 #include "../test/scoped_types.h"
27
28
29 // kECKeyWithoutPublic is an ECPrivateKey with the optional publicKey field
30 // omitted.
31 static const uint8_t kECKeyWithoutPublic[] = {
32 0x30, 0x31, 0x02, 0x01, 0x01, 0x04, 0x20, 0xc6, 0xc1, 0xaa, 0xda, 0x15, 0xb0,
33 0x76, 0x61, 0xf8, 0x14, 0x2c, 0x6c, 0xaf, 0x0f, 0xdb, 0x24, 0x1a, 0xff, 0x2e,
34 0xfe, 0x46, 0xc0, 0x93, 0x8b, 0x74, 0xf2, 0xbc, 0xc5, 0x30, 0x52, 0xb0, 0x77,
35 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07,
36 };
37
38 // kECKeySpecifiedCurve is the above key with P-256's parameters explicitly
39 // spelled out rather than using a named curve.
40 static const uint8_t kECKeySpecifiedCurve[] = {
41 0x30, 0x82, 0x01, 0x22, 0x02, 0x01, 0x01, 0x04, 0x20, 0xc6, 0xc1, 0xaa,
42 0xda, 0x15, 0xb0, 0x76, 0x61, 0xf8, 0x14, 0x2c, 0x6c, 0xaf, 0x0f, 0xdb,
43 0x24, 0x1a, 0xff, 0x2e, 0xfe, 0x46, 0xc0, 0x93, 0x8b, 0x74, 0xf2, 0xbc,
44 0xc5, 0x30, 0x52, 0xb0, 0x77, 0xa0, 0x81, 0xfa, 0x30, 0x81, 0xf7, 0x02,
45 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x01,
46 0x01, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
47 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
48 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
49 0x30, 0x5b, 0x04, 0x20, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01,
50 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
51 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
52 0x04, 0x20, 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 0xb3, 0xeb,
53 0xbd, 0x55, 0x76, 0x98, 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53,
54 0xb0, 0xf6, 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b, 0x03, 0x15,
55 0x00, 0xc4, 0x9d, 0x36, 0x08, 0x86, 0xe7, 0x04, 0x93, 0x6a, 0x66, 0x78,
56 0xe1, 0x13, 0x9d, 0x26, 0xb7, 0x81, 0x9f, 0x7e, 0x90, 0x04, 0x41, 0x04,
57 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6, 0xe5,
58 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0,
59 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3, 0x42, 0xe2,
60 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16,
61 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68,
62 0x37, 0xbf, 0x51, 0xf5, 0x02, 0x21, 0x00, 0xff, 0xff, 0xff, 0xff, 0x00,
63 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc,
64 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc,
65 0x63, 0x25, 0x51, 0x02, 0x01, 0x01,
66 };
67
68 // kECKeyMissingZeros is an ECPrivateKey containing a degenerate P-256 key where
69 // the private key is one. The private key is incorrectly encoded without zero
70 // padding.
71 static const uint8_t kECKeyMissingZeros[] = {
72 0x30, 0x58, 0x02, 0x01, 0x01, 0x04, 0x01, 0x01, 0xa0, 0x0a, 0x06, 0x08, 0x2a,
73 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04,
74 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6, 0xe5, 0x63,
75 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0, 0xf4, 0xa1,
76 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f,
77 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57,
78 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
79 };
80
81 // kECKeyMissingZeros is an ECPrivateKey containing a degenerate P-256 key where
82 // the private key is one. The private key is encoded with the required zero
83 // padding.
84 static const uint8_t kECKeyWithZeros[] = {
85 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
88 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0xa1,
89 0x44, 0x03, 0x42, 0x00, 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47,
90 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
91 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, 0x4f, 0xe3,
92 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e,
93 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68,
94 0x37, 0xbf, 0x51, 0xf5,
95 };
96
97 // DecodeECPrivateKey decodes |in| as an ECPrivateKey structure and returns the
98 // result or nullptr on error.
99 static ScopedEC_KEY DecodeECPrivateKey(const uint8_t *in, size_t in_len) {
100 CBS cbs;
101 CBS_init(&cbs, in, in_len);
102 ScopedEC_KEY ret(EC_KEY_parse_private_key(&cbs, NULL));
103 if (!ret || CBS_len(&cbs) != 0) {
104 return nullptr;
105 }
106 return ret;
107 }
108
109 // EncodeECPrivateKey encodes |key| as an ECPrivateKey structure into |*out|. It
110 // returns true on success or false on error.
111 static bool EncodeECPrivateKey(std::vector<uint8_t> *out, const EC_KEY *key) {
112 ScopedCBB cbb;
113 uint8_t *der;
114 size_t der_len;
115 if (!CBB_init(cbb.get(), 0) ||
116 !EC_KEY_marshal_private_key(cbb.get(), key, EC_KEY_get_enc_flags(key)) ||
117 !CBB_finish(cbb.get(), &der, &der_len)) {
118 return false;
119 }
120 out->assign(der, der + der_len);
121 OPENSSL_free(der);
122 return true;
123 }
124
125 static bool Testd2i_ECPrivateKey() {
126 ScopedEC_KEY key = DecodeECPrivateKey(kECKeyWithoutPublic,
127 sizeof(kECKeyWithoutPublic));
128 if (!key) {
129 fprintf(stderr, "Failed to parse private key.\n");
130 ERR_print_errors_fp(stderr);
131 return false;
132 }
133
134 std::vector<uint8_t> out;
135 if (!EncodeECPrivateKey(&out, key.get())) {
136 fprintf(stderr, "Failed to serialize private key.\n");
137 ERR_print_errors_fp(stderr);
138 return false;
139 }
140
141 if (std::vector<uint8_t>(kECKeyWithoutPublic,
142 kECKeyWithoutPublic + sizeof(kECKeyWithoutPublic)) !=
143 out) {
144 fprintf(stderr, "Serialisation of key doesn't match original.\n");
145 return false;
146 }
147
148 const EC_POINT *pub_key = EC_KEY_get0_public_key(key.get());
149 if (pub_key == NULL) {
150 fprintf(stderr, "Public key missing.\n");
151 return false;
152 }
153
154 ScopedBIGNUM x(BN_new());
155 ScopedBIGNUM y(BN_new());
156 if (!x || !y) {
157 return false;
158 }
159 if (!EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(key.get()),
160 pub_key, x.get(), y.get(), NULL)) {
161 fprintf(stderr, "Failed to get public key in affine coordinates.\n");
162 return false;
163 }
164 ScopedOpenSSLString x_hex(BN_bn2hex(x.get()));
165 ScopedOpenSSLString y_hex(BN_bn2hex(y.get()));
166 if (!x_hex || !y_hex) {
167 return false;
168 }
169 if (0 != strcmp(
170 x_hex.get(),
171 "c81561ecf2e54edefe6617db1c7a34a70744ddb261f269b83dacfcd2ade5a681") ||
172 0 != strcmp(
173 y_hex.get(),
174 "e0e2afa3f9b6abe4c698ef6495f1be49a3196c5056acb3763fe4507eec596e88")) {
175 fprintf(stderr, "Incorrect public key: %s %s\n", x_hex.get(), y_hex.get());
176 return false;
177 }
178
179 return true;
180 }
181
182 static bool TestZeroPadding() {
183 // Check that the correct encoding round-trips.
184 ScopedEC_KEY key = DecodeECPrivateKey(kECKeyWithZeros,
185 sizeof(kECKeyWithZeros));
186 std::vector<uint8_t> out;
187 if (!key || !EncodeECPrivateKey(&out, key.get())) {
188 ERR_print_errors_fp(stderr);
189 return false;
190 }
191
192 if (std::vector<uint8_t>(kECKeyWithZeros,
193 kECKeyWithZeros + sizeof(kECKeyWithZeros)) != out) {
194 fprintf(stderr, "Serialisation of key was incorrect.\n");
195 return false;
196 }
197
198 // Keys without leading zeros also parse, but they encode correctly.
199 key = DecodeECPrivateKey(kECKeyMissingZeros, sizeof(kECKeyMissingZeros));
200 if (!key || !EncodeECPrivateKey(&out, key.get())) {
201 ERR_print_errors_fp(stderr);
202 return false;
203 }
204
205 if (std::vector<uint8_t>(kECKeyWithZeros,
206 kECKeyWithZeros + sizeof(kECKeyWithZeros)) != out) {
207 fprintf(stderr, "Serialisation of key was incorrect.\n");
208 return false;
209 }
210
211 return true;
212 }
213
214 static bool TestSpecifiedCurve() {
215 // Test keys with specified curves may be decoded.
216 ScopedEC_KEY key =
217 DecodeECPrivateKey(kECKeySpecifiedCurve, sizeof(kECKeySpecifiedCurve));
218 if (!key) {
219 ERR_print_errors_fp(stderr);
220 return false;
221 }
222
223 // The group should have been interpreted as P-256.
224 if (EC_GROUP_get_curve_name(EC_KEY_get0_group(key.get())) !=
225 NID_X9_62_prime256v1) {
226 fprintf(stderr, "Curve name incorrect.\n");
227 return false;
228 }
229
230 // Encoding the key should still use named form.
231 std::vector<uint8_t> out;
232 if (!EncodeECPrivateKey(&out, key.get())) {
233 ERR_print_errors_fp(stderr);
234 return false;
235 }
236 if (std::vector<uint8_t>(kECKeyWithoutPublic,
237 kECKeyWithoutPublic + sizeof(kECKeyWithoutPublic)) !=
238 out) {
239 fprintf(stderr, "Serialisation of key was incorrect.\n");
240 return false;
241 }
242
243 return true;
244 }
245
246 static bool TestSetAffine(const int nid) {
247 ScopedEC_KEY key(EC_KEY_new_by_curve_name(nid));
248 if (!key) {
249 return false;
250 }
251
252 const EC_GROUP *const group = EC_KEY_get0_group(key.get());
253
254 if (!EC_KEY_generate_key(key.get())) {
255 fprintf(stderr, "EC_KEY_generate_key failed with nid %d\n", nid);
256 ERR_print_errors_fp(stderr);
257 return false;
258 }
259
260 if (!EC_POINT_is_on_curve(group, EC_KEY_get0_public_key(key.get()),
261 nullptr)) {
262 fprintf(stderr, "generated point is not on curve with nid %d", nid);
263 ERR_print_errors_fp(stderr);
264 return false;
265 }
266
267 ScopedBIGNUM x(BN_new());
268 ScopedBIGNUM y(BN_new());
269 if (!EC_POINT_get_affine_coordinates_GFp(group,
270 EC_KEY_get0_public_key(key.get()),
271 x.get(), y.get(), nullptr)) {
272 fprintf(stderr, "EC_POINT_get_affine_coordinates_GFp failed with nid %d\n",
273 nid);
274 ERR_print_errors_fp(stderr);
275 return false;
276 }
277
278 ScopedEC_POINT point(EC_POINT_new(group));
279 if (!point) {
280 return false;
281 }
282
283 if (!EC_POINT_set_affine_coordinates_GFp(group, point.get(), x.get(), y.get(),
284 nullptr)) {
285 fprintf(stderr, "EC_POINT_set_affine_coordinates_GFp failed with nid %d\n",
286 nid);
287 ERR_print_errors_fp(stderr);
288 return false;
289 }
290
291 // Subtract one from |y| to make the point no longer on the curve.
292 if (!BN_sub(y.get(), y.get(), BN_value_one())) {
293 return false;
294 }
295
296 ScopedEC_POINT invalid_point(EC_POINT_new(group));
297 if (!invalid_point) {
298 return false;
299 }
300
301 if (EC_POINT_set_affine_coordinates_GFp(group, invalid_point.get(), x.get(),
302 y.get(), nullptr)) {
303 fprintf(stderr,
304 "EC_POINT_set_affine_coordinates_GFp succeeded with invalid "
305 "coordinates with nid %d\n",
306 nid);
307 ERR_print_errors_fp(stderr);
308 return false;
309 }
310
311 return true;
312 }
313
314 static bool TestArbitraryCurve() {
315 // Make a P-256 key and extract the affine coordinates.
316 ScopedEC_KEY key(EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
317 if (!key || !EC_KEY_generate_key(key.get())) {
318 return false;
319 }
320
321 // Make an arbitrary curve which is identical to P-256.
322 static const uint8_t kP[] = {
323 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
324 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
325 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
326 };
327 static const uint8_t kA[] = {
328 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
329 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
330 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfc,
331 };
332 static const uint8_t kB[] = {
333 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, 0xb3, 0xeb, 0xbd,
334 0x55, 0x76, 0x98, 0x86, 0xbc, 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53,
335 0xb0, 0xf6, 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b,
336 };
337 static const uint8_t kX[] = {
338 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc, 0xe6,
339 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb,
340 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
341 };
342 static const uint8_t kY[] = {
343 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
344 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
345 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
346 };
347 static const uint8_t kOrder[] = {
348 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
349 0xff, 0xff, 0xff, 0xff, 0xff, 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17,
350 0x9e, 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51,
351 };
352 ScopedBN_CTX ctx(BN_CTX_new());
353 ScopedBIGNUM p(BN_bin2bn(kP, sizeof(kP), nullptr));
354 ScopedBIGNUM a(BN_bin2bn(kA, sizeof(kA), nullptr));
355 ScopedBIGNUM b(BN_bin2bn(kB, sizeof(kB), nullptr));
356 ScopedBIGNUM gx(BN_bin2bn(kX, sizeof(kX), nullptr));
357 ScopedBIGNUM gy(BN_bin2bn(kY, sizeof(kY), nullptr));
358 ScopedBIGNUM order(BN_bin2bn(kOrder, sizeof(kOrder), nullptr));
359 ScopedBIGNUM cofactor(BN_new());
360 if (!ctx || !p || !a || !b || !gx || !gy || !order || !cofactor ||
361 !BN_set_word(cofactor.get(), 1)) {
362 return false;
363 }
364
365 ScopedEC_GROUP group(
366 EC_GROUP_new_curve_GFp(p.get(), a.get(), b.get(), ctx.get()));
367 if (!group) {
368 return false;
369 }
370 ScopedEC_POINT generator(EC_POINT_new(group.get()));
371 if (!generator ||
372 !EC_POINT_set_affine_coordinates_GFp(group.get(), generator.get(),
373 gx.get(), gy.get(), ctx.get()) ||
374 !EC_GROUP_set_generator(group.get(), generator.get(), order.get(),
375 cofactor.get())) {
376 return false;
377 }
378
379 // |group| should not have a curve name.
380 if (EC_GROUP_get_curve_name(group.get()) != NID_undef) {
381 return false;
382 }
383
384 // Copy |key| to |key2| using |group|.
385 ScopedEC_KEY key2(EC_KEY_new());
386 ScopedEC_POINT point(EC_POINT_new(group.get()));
387 ScopedBIGNUM x(BN_new()), y(BN_new());
388 if (!key2 || !point || !x || !y ||
389 !EC_KEY_set_group(key2.get(), group.get()) ||
390 !EC_KEY_set_private_key(key2.get(), EC_KEY_get0_private_key(key.get())) ||
391 !EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(key.get()),
392 EC_KEY_get0_public_key(key.get()),
393 x.get(), y.get(), nullptr) ||
394 !EC_POINT_set_affine_coordinates_GFp(group.get(), point.get(), x.get(),
395 y.get(), nullptr) ||
396 !EC_KEY_set_public_key(key2.get(), point.get())) {
397 fprintf(stderr, "Could not copy key.\n");
398 return false;
399 }
400
401 // The key must be valid according to the new group too.
402 if (!EC_KEY_check_key(key2.get())) {
403 fprintf(stderr, "Copied key is not valid.\n");
404 return false;
405 }
406
407 // Repeat the process for |EC_GROUP_new_arbitrary|.
408 group.reset(EC_GROUP_new_arbitrary(p.get(), a.get(), b.get(), gx.get(),
409 gy.get(), order.get(), cofactor.get()));
410 if (!group) {
411 return false;
412 }
413
414 // |group| should not have a curve name.
415 if (EC_GROUP_get_curve_name(group.get()) != NID_undef) {
416 return false;
417 }
418
419 // Copy |key| to |key2| using |group|.
420 key2.reset(EC_KEY_new());
421 point.reset(EC_POINT_new(group.get()));
422 if (!key2 || !point ||
423 !EC_KEY_set_group(key2.get(), group.get()) ||
424 !EC_KEY_set_private_key(key2.get(), EC_KEY_get0_private_key(key.get())) ||
425 !EC_POINT_set_affine_coordinates_GFp(group.get(), point.get(), x.get(),
426 y.get(), nullptr) ||
427 !EC_KEY_set_public_key(key2.get(), point.get())) {
428 fprintf(stderr, "Could not copy key.\n");
429 return false;
430 }
431
432 // The key must be valid according to the new group too.
433 if (!EC_KEY_check_key(key2.get())) {
434 fprintf(stderr, "Copied key is not valid.\n");
435 return false;
436 }
437
438 return true;
439 }
440
441 static bool TestAddingEqualPoints(int nid) {
442 ScopedEC_KEY key(EC_KEY_new_by_curve_name(nid));
443 if (!key) {
444 return false;
445 }
446
447 const EC_GROUP *const group = EC_KEY_get0_group(key.get());
448
449 if (!EC_KEY_generate_key(key.get())) {
450 fprintf(stderr, "EC_KEY_generate_key failed with nid %d\n", nid);
451 ERR_print_errors_fp(stderr);
452 return false;
453 }
454
455 ScopedEC_POINT p1(EC_POINT_new(group));
456 ScopedEC_POINT p2(EC_POINT_new(group));
457 ScopedEC_POINT double_p1(EC_POINT_new(group));
458 ScopedEC_POINT p1_plus_p2(EC_POINT_new(group));
459 if (!p1 || !p2 || !double_p1 || !p1_plus_p2) {
460 return false;
461 }
462
463 if (!EC_POINT_copy(p1.get(), EC_KEY_get0_public_key(key.get())) ||
464 !EC_POINT_copy(p2.get(), EC_KEY_get0_public_key(key.get()))) {
465 fprintf(stderr, "EC_POINT_COPY failed with nid %d\n", nid);
466 ERR_print_errors_fp(stderr);
467 return false;
468 }
469
470 ScopedBN_CTX ctx(BN_CTX_new());
471 if (!ctx) {
472 return false;
473 }
474
475 if (!EC_POINT_dbl(group, double_p1.get(), p1.get(), ctx.get()) ||
476 !EC_POINT_add(group, p1_plus_p2.get(), p1.get(), p2.get(), ctx.get())) {
477 fprintf(stderr, "Point operation failed with nid %d\n", nid);
478 ERR_print_errors_fp(stderr);
479 return false;
480 }
481
482 if (EC_POINT_cmp(group, double_p1.get(), p1_plus_p2.get(), ctx.get()) != 0) {
483 fprintf(stderr, "A+A != 2A for nid %d", nid);
484 return false;
485 }
486
487 return true;
488 }
489
490 static bool ForEachCurve(bool (*test_func)(int nid)) {
491 const size_t num_curves = EC_get_builtin_curves(nullptr, 0);
492 std::vector<EC_builtin_curve> curves(num_curves);
493 EC_get_builtin_curves(curves.data(), num_curves);
494
495 for (const auto& curve : curves) {
496 if (!test_func(curve.nid)) {
497 fprintf(stderr, "Test failed for %s\n", curve.comment);
498 return false;
499 }
500 }
501
502 return true;
503 }
504
505 int main(void) {
506 CRYPTO_library_init();
507
508 if (!Testd2i_ECPrivateKey() ||
509 !TestZeroPadding() ||
510 !TestSpecifiedCurve() ||
511 !ForEachCurve(TestSetAffine) ||
512 !ForEachCurve(TestAddingEqualPoints) ||
513 !TestArbitraryCurve()) {
514 fprintf(stderr, "failed\n");
515 return 1;
516 }
517
518 printf("PASS\n");
519 return 0;
520 }