ectest.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320
  1. /* crypto/ec/ectest.c */
  2. /*
  3. * Originally written by Bodo Moeller for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * openssl-core@openssl.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. /* ====================================================================
  59. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  60. *
  61. * Portions of the attached software ("Contribution") are developed by
  62. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  63. *
  64. * The Contribution is licensed pursuant to the OpenSSL open source
  65. * license provided above.
  66. *
  67. * The elliptic curve binary polynomial software is originally written by
  68. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
  69. *
  70. */
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #ifdef FLAT_INC
  74. #include "e_os.h"
  75. #else
  76. #include "../e_os.h"
  77. #endif
  78. #include <string.h>
  79. #include <time.h>
  80. #ifdef OPENSSL_NO_EC
  81. int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
  82. #else
  83. #include <openssl/ec.h>
  84. #ifndef OPENSSL_NO_ENGINE
  85. #include <openssl/engine.h>
  86. #endif
  87. #include <openssl/err.h>
  88. #include <openssl/obj_mac.h>
  89. #include <openssl/objects.h>
  90. #include <openssl/rand.h>
  91. #define ABORT do { \
  92. fflush(stdout); \
  93. fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
  94. ERR_print_errors_fp(stderr); \
  95. EXIT(1); \
  96. } while (0)
  97. void prime_field_tests(void);
  98. void char2_field_tests(void);
  99. void internal_curve_test(void);
  100. #define TIMING_BASE_PT 0
  101. #define TIMING_RAND_PT 1
  102. #define TIMING_SIMUL 2
  103. #if 0
  104. static void timings(EC_GROUP *group, int type, BN_CTX *ctx)
  105. {
  106. clock_t clck;
  107. int i, j;
  108. BIGNUM *s;
  109. BIGNUM *r[10], *r0[10];
  110. EC_POINT *P;
  111. s = BN_new();
  112. if (s == NULL) ABORT;
  113. fprintf(stdout, "Timings for %d-bit field, ", EC_GROUP_get_degree(group));
  114. if (!EC_GROUP_get_order(group, s, ctx)) ABORT;
  115. fprintf(stdout, "%d-bit scalars ", (int)BN_num_bits(s));
  116. fflush(stdout);
  117. P = EC_POINT_new(group);
  118. if (P == NULL) ABORT;
  119. EC_POINT_copy(P, EC_GROUP_get0_generator(group));
  120. for (i = 0; i < 10; i++)
  121. {
  122. if ((r[i] = BN_new()) == NULL) ABORT;
  123. if (!BN_pseudo_rand(r[i], BN_num_bits(s), 0, 0)) ABORT;
  124. if (type != TIMING_BASE_PT)
  125. {
  126. if ((r0[i] = BN_new()) == NULL) ABORT;
  127. if (!BN_pseudo_rand(r0[i], BN_num_bits(s), 0, 0)) ABORT;
  128. }
  129. }
  130. clck = clock();
  131. for (i = 0; i < 10; i++)
  132. {
  133. for (j = 0; j < 10; j++)
  134. {
  135. if (!EC_POINT_mul(group, P, (type != TIMING_RAND_PT) ? r[i] : NULL,
  136. (type != TIMING_BASE_PT) ? P : NULL, (type != TIMING_BASE_PT) ? r0[i] : NULL, ctx)) ABORT;
  137. }
  138. }
  139. clck = clock() - clck;
  140. fprintf(stdout, "\n");
  141. #ifdef CLOCKS_PER_SEC
  142. /* "To determine the time in seconds, the value returned
  143. * by the clock function should be divided by the value
  144. * of the macro CLOCKS_PER_SEC."
  145. * -- ISO/IEC 9899 */
  146. # define UNIT "s"
  147. #else
  148. /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
  149. * -- cc on NeXTstep/OpenStep */
  150. # define UNIT "units"
  151. # define CLOCKS_PER_SEC 1
  152. #endif
  153. if (type == TIMING_BASE_PT) {
  154. fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
  155. "base point multiplications", (double)clck/CLOCKS_PER_SEC);
  156. } else if (type == TIMING_RAND_PT) {
  157. fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
  158. "random point multiplications", (double)clck/CLOCKS_PER_SEC);
  159. } else if (type == TIMING_SIMUL) {
  160. fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
  161. "s*P+t*Q operations", (double)clck/CLOCKS_PER_SEC);
  162. }
  163. fprintf(stdout, "average: %.4f " UNIT "\n", (double)clck/(CLOCKS_PER_SEC*i*j));
  164. EC_POINT_free(P);
  165. BN_free(s);
  166. for (i = 0; i < 10; i++)
  167. {
  168. BN_free(r[i]);
  169. if (type != TIMING_BASE_PT) BN_free(r0[i]);
  170. }
  171. }
  172. #endif
  173. void prime_field_tests()
  174. {
  175. BN_CTX *ctx = NULL;
  176. BIGNUM *p, *a, *b;
  177. EC_GROUP *group;
  178. EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
  179. EC_POINT *P, *Q, *R;
  180. BIGNUM *x, *y, *z;
  181. unsigned char buf[100];
  182. size_t i, len;
  183. int k;
  184. #if 1 /* optional */
  185. ctx = BN_CTX_new();
  186. if (!ctx) ABORT;
  187. #endif
  188. p = BN_new();
  189. a = BN_new();
  190. b = BN_new();
  191. if (!p || !a || !b) ABORT;
  192. if (!BN_hex2bn(&p, "17")) ABORT;
  193. if (!BN_hex2bn(&a, "1")) ABORT;
  194. if (!BN_hex2bn(&b, "1")) ABORT;
  195. group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
  196. * so that the library gets to choose the EC_METHOD */
  197. if (!group) ABORT;
  198. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  199. {
  200. EC_GROUP *tmp;
  201. tmp = EC_GROUP_new(EC_GROUP_method_of(group));
  202. if (!tmp) ABORT;
  203. if (!EC_GROUP_copy(tmp, group));
  204. EC_GROUP_free(group);
  205. group = tmp;
  206. }
  207. if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT;
  208. fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x");
  209. BN_print_fp(stdout, p);
  210. fprintf(stdout, ")\n a = 0x");
  211. BN_print_fp(stdout, a);
  212. fprintf(stdout, "\n b = 0x");
  213. BN_print_fp(stdout, b);
  214. fprintf(stdout, "\n");
  215. P = EC_POINT_new(group);
  216. Q = EC_POINT_new(group);
  217. R = EC_POINT_new(group);
  218. if (!P || !Q || !R) ABORT;
  219. if (!EC_POINT_set_to_infinity(group, P)) ABORT;
  220. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  221. buf[0] = 0;
  222. if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
  223. if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
  224. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  225. x = BN_new();
  226. y = BN_new();
  227. z = BN_new();
  228. if (!x || !y || !z) ABORT;
  229. if (!BN_hex2bn(&x, "D")) ABORT;
  230. if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
  231. if (!EC_POINT_is_on_curve(group, Q, ctx))
  232. {
  233. if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT;
  234. fprintf(stderr, "Point is not on curve: x = 0x");
  235. BN_print_fp(stderr, x);
  236. fprintf(stderr, ", y = 0x");
  237. BN_print_fp(stderr, y);
  238. fprintf(stderr, "\n");
  239. ABORT;
  240. }
  241. fprintf(stdout, "A cyclic subgroup:\n");
  242. k = 100;
  243. do
  244. {
  245. if (k-- == 0) ABORT;
  246. if (EC_POINT_is_at_infinity(group, P))
  247. fprintf(stdout, " point at infinity\n");
  248. else
  249. {
  250. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  251. fprintf(stdout, " x = 0x");
  252. BN_print_fp(stdout, x);
  253. fprintf(stdout, ", y = 0x");
  254. BN_print_fp(stdout, y);
  255. fprintf(stdout, "\n");
  256. }
  257. if (!EC_POINT_copy(R, P)) ABORT;
  258. if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
  259. #if 0 /* optional */
  260. {
  261. EC_POINT *points[3];
  262. points[0] = R;
  263. points[1] = Q;
  264. points[2] = P;
  265. if (!EC_POINTs_make_affine(group, 2, points, ctx)) ABORT;
  266. }
  267. #endif
  268. }
  269. while (!EC_POINT_is_at_infinity(group, P));
  270. if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
  271. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  272. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
  273. if (len == 0) ABORT;
  274. if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
  275. if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
  276. fprintf(stdout, "Generator as octect string, compressed form:\n ");
  277. for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
  278. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
  279. if (len == 0) ABORT;
  280. if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
  281. if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
  282. fprintf(stdout, "\nGenerator as octect string, uncompressed form:\n ");
  283. for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
  284. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
  285. if (len == 0) ABORT;
  286. if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
  287. if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
  288. fprintf(stdout, "\nGenerator as octect string, hybrid form:\n ");
  289. for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
  290. if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
  291. fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x");
  292. BN_print_fp(stdout, x);
  293. fprintf(stdout, ", Y = 0x");
  294. BN_print_fp(stdout, y);
  295. fprintf(stdout, ", Z = 0x");
  296. BN_print_fp(stdout, z);
  297. fprintf(stdout, "\n");
  298. if (!EC_POINT_invert(group, P, ctx)) ABORT;
  299. if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
  300. /* Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2, 2000)
  301. * -- not a NIST curve, but commonly used */
  302. if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF")) ABORT;
  303. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  304. if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC")) ABORT;
  305. if (!BN_hex2bn(&b, "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45")) ABORT;
  306. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  307. if (!BN_hex2bn(&x, "4A96B5688EF573284664698968C38BB913CBFC82")) ABORT;
  308. if (!BN_hex2bn(&y, "23a628553168947d59dcc912042351377ac5fb32")) ABORT;
  309. if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  310. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  311. if (!BN_hex2bn(&z, "0100000000000000000001F4C8F927AED3CA752257")) ABORT;
  312. if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
  313. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  314. fprintf(stdout, "\nSEC2 curve secp160r1 -- Generator:\n x = 0x");
  315. BN_print_fp(stdout, x);
  316. fprintf(stdout, "\n y = 0x");
  317. BN_print_fp(stdout, y);
  318. fprintf(stdout, "\n");
  319. /* G_y value taken from the standard: */
  320. if (!BN_hex2bn(&z, "23a628553168947d59dcc912042351377ac5fb32")) ABORT;
  321. if (0 != BN_cmp(y, z)) ABORT;
  322. fprintf(stdout, "verify degree ...");
  323. if (EC_GROUP_get_degree(group) != 160) ABORT;
  324. fprintf(stdout, " ok\n");
  325. fprintf(stdout, "verify group order ...");
  326. fflush(stdout);
  327. if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
  328. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
  329. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  330. fprintf(stdout, ".");
  331. fflush(stdout);
  332. if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
  333. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
  334. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  335. fprintf(stdout, " ok\n");
  336. if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
  337. if (!EC_GROUP_copy(P_160, group)) ABORT;
  338. /* Curve P-192 (FIPS PUB 186-2, App. 6) */
  339. if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT;
  340. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  341. if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT;
  342. if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT;
  343. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  344. if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT;
  345. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
  346. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  347. if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT;
  348. if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
  349. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  350. fprintf(stdout, "\nNIST curve P-192 -- Generator:\n x = 0x");
  351. BN_print_fp(stdout, x);
  352. fprintf(stdout, "\n y = 0x");
  353. BN_print_fp(stdout, y);
  354. fprintf(stdout, "\n");
  355. /* G_y value taken from the standard: */
  356. if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT;
  357. if (0 != BN_cmp(y, z)) ABORT;
  358. fprintf(stdout, "verify degree ...");
  359. if (EC_GROUP_get_degree(group) != 192) ABORT;
  360. fprintf(stdout, " ok\n");
  361. fprintf(stdout, "verify group order ...");
  362. fflush(stdout);
  363. if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
  364. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
  365. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  366. fprintf(stdout, ".");
  367. fflush(stdout);
  368. if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
  369. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
  370. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  371. fprintf(stdout, " ok\n");
  372. if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
  373. if (!EC_GROUP_copy(P_192, group)) ABORT;
  374. /* Curve P-224 (FIPS PUB 186-2, App. 6) */
  375. if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT;
  376. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  377. if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT;
  378. if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT;
  379. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  380. if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT;
  381. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
  382. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  383. if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT;
  384. if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
  385. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  386. fprintf(stdout, "\nNIST curve P-224 -- Generator:\n x = 0x");
  387. BN_print_fp(stdout, x);
  388. fprintf(stdout, "\n y = 0x");
  389. BN_print_fp(stdout, y);
  390. fprintf(stdout, "\n");
  391. /* G_y value taken from the standard: */
  392. if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT;
  393. if (0 != BN_cmp(y, z)) ABORT;
  394. fprintf(stdout, "verify degree ...");
  395. if (EC_GROUP_get_degree(group) != 224) ABORT;
  396. fprintf(stdout, " ok\n");
  397. fprintf(stdout, "verify group order ...");
  398. fflush(stdout);
  399. if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
  400. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
  401. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  402. fprintf(stdout, ".");
  403. fflush(stdout);
  404. if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
  405. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
  406. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  407. fprintf(stdout, " ok\n");
  408. if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
  409. if (!EC_GROUP_copy(P_224, group)) ABORT;
  410. /* Curve P-256 (FIPS PUB 186-2, App. 6) */
  411. if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
  412. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  413. if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
  414. if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT;
  415. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  416. if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT;
  417. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
  418. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  419. if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
  420. "84F3B9CAC2FC632551")) ABORT;
  421. if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
  422. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  423. fprintf(stdout, "\nNIST curve P-256 -- Generator:\n x = 0x");
  424. BN_print_fp(stdout, x);
  425. fprintf(stdout, "\n y = 0x");
  426. BN_print_fp(stdout, y);
  427. fprintf(stdout, "\n");
  428. /* G_y value taken from the standard: */
  429. if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT;
  430. if (0 != BN_cmp(y, z)) ABORT;
  431. fprintf(stdout, "verify degree ...");
  432. if (EC_GROUP_get_degree(group) != 256) ABORT;
  433. fprintf(stdout, " ok\n");
  434. fprintf(stdout, "verify group order ...");
  435. fflush(stdout);
  436. if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
  437. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
  438. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  439. fprintf(stdout, ".");
  440. fflush(stdout);
  441. if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
  442. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
  443. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  444. fprintf(stdout, " ok\n");
  445. if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
  446. if (!EC_GROUP_copy(P_256, group)) ABORT;
  447. /* Curve P-384 (FIPS PUB 186-2, App. 6) */
  448. if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  449. "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT;
  450. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  451. if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  452. "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT;
  453. if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
  454. "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT;
  455. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  456. if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
  457. "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT;
  458. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
  459. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  460. if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  461. "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT;
  462. if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
  463. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  464. fprintf(stdout, "\nNIST curve P-384 -- Generator:\n x = 0x");
  465. BN_print_fp(stdout, x);
  466. fprintf(stdout, "\n y = 0x");
  467. BN_print_fp(stdout, y);
  468. fprintf(stdout, "\n");
  469. /* G_y value taken from the standard: */
  470. if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
  471. "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT;
  472. if (0 != BN_cmp(y, z)) ABORT;
  473. fprintf(stdout, "verify degree ...");
  474. if (EC_GROUP_get_degree(group) != 384) ABORT;
  475. fprintf(stdout, " ok\n");
  476. fprintf(stdout, "verify group order ...");
  477. fflush(stdout);
  478. if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
  479. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
  480. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  481. fprintf(stdout, ".");
  482. fflush(stdout);
  483. if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
  484. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
  485. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  486. fprintf(stdout, " ok\n");
  487. if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
  488. if (!EC_GROUP_copy(P_384, group)) ABORT;
  489. /* Curve P-521 (FIPS PUB 186-2, App. 6) */
  490. if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  491. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  492. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
  493. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  494. if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  495. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  496. "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
  497. if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
  498. "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
  499. "DF883D2C34F1EF451FD46B503F00")) ABORT;
  500. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  501. if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
  502. "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
  503. "3C1856A429BF97E7E31C2E5BD66")) ABORT;
  504. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
  505. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  506. if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  507. "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
  508. "C9B8899C47AEBB6FB71E91386409")) ABORT;
  509. if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
  510. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  511. fprintf(stdout, "\nNIST curve P-521 -- Generator:\n x = 0x");
  512. BN_print_fp(stdout, x);
  513. fprintf(stdout, "\n y = 0x");
  514. BN_print_fp(stdout, y);
  515. fprintf(stdout, "\n");
  516. /* G_y value taken from the standard: */
  517. if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
  518. "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
  519. "7086A272C24088BE94769FD16650")) ABORT;
  520. if (0 != BN_cmp(y, z)) ABORT;
  521. fprintf(stdout, "verify degree ...");
  522. if (EC_GROUP_get_degree(group) != 521) ABORT;
  523. fprintf(stdout, " ok\n");
  524. fprintf(stdout, "verify group order ...");
  525. fflush(stdout);
  526. if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
  527. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
  528. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  529. fprintf(stdout, ".");
  530. fflush(stdout);
  531. if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
  532. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT;
  533. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  534. fprintf(stdout, " ok\n");
  535. if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
  536. if (!EC_GROUP_copy(P_521, group)) ABORT;
  537. /* more tests using the last curve */
  538. if (!EC_POINT_copy(Q, P)) ABORT;
  539. if (EC_POINT_is_at_infinity(group, Q)) ABORT;
  540. if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
  541. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  542. if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
  543. if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
  544. if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
  545. if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
  546. {
  547. const EC_POINT *points[3];
  548. const BIGNUM *scalars[3];
  549. if (EC_POINT_is_at_infinity(group, Q)) ABORT;
  550. points[0] = Q;
  551. points[1] = Q;
  552. points[2] = Q;
  553. if (!BN_add(y, z, BN_value_one())) ABORT;
  554. if (BN_is_odd(y)) ABORT;
  555. if (!BN_rshift1(y, y)) ABORT;
  556. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  557. scalars[1] = y;
  558. fprintf(stdout, "combined multiplication ...");
  559. fflush(stdout);
  560. /* z is still the group order */
  561. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
  562. if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
  563. if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
  564. if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
  565. fprintf(stdout, ".");
  566. fflush(stdout);
  567. if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
  568. if (!BN_add(z, z, y)) ABORT;
  569. BN_set_sign(z, 1);
  570. scalars[0] = y;
  571. scalars[1] = z; /* z = -(order + y) */
  572. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
  573. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  574. fprintf(stdout, ".");
  575. fflush(stdout);
  576. if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
  577. if (!BN_add(z, x, y)) ABORT;
  578. BN_set_sign(z, 1);
  579. scalars[0] = x;
  580. scalars[1] = y;
  581. scalars[2] = z; /* z = -(x+y) */
  582. if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
  583. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  584. fprintf(stdout, " ok\n\n");
  585. }
  586. #if 0
  587. timings(P_160, TIMING_BASE_PT, ctx);
  588. timings(P_160, TIMING_RAND_PT, ctx);
  589. timings(P_160, TIMING_SIMUL, ctx);
  590. timings(P_192, TIMING_BASE_PT, ctx);
  591. timings(P_192, TIMING_RAND_PT, ctx);
  592. timings(P_192, TIMING_SIMUL, ctx);
  593. timings(P_224, TIMING_BASE_PT, ctx);
  594. timings(P_224, TIMING_RAND_PT, ctx);
  595. timings(P_224, TIMING_SIMUL, ctx);
  596. timings(P_256, TIMING_BASE_PT, ctx);
  597. timings(P_256, TIMING_RAND_PT, ctx);
  598. timings(P_256, TIMING_SIMUL, ctx);
  599. timings(P_384, TIMING_BASE_PT, ctx);
  600. timings(P_384, TIMING_RAND_PT, ctx);
  601. timings(P_384, TIMING_SIMUL, ctx);
  602. timings(P_521, TIMING_BASE_PT, ctx);
  603. timings(P_521, TIMING_RAND_PT, ctx);
  604. timings(P_521, TIMING_SIMUL, ctx);
  605. #endif
  606. if (ctx)
  607. BN_CTX_free(ctx);
  608. BN_free(p); BN_free(a); BN_free(b);
  609. EC_GROUP_free(group);
  610. EC_POINT_free(P);
  611. EC_POINT_free(Q);
  612. EC_POINT_free(R);
  613. BN_free(x); BN_free(y); BN_free(z);
  614. if (P_160) EC_GROUP_free(P_160);
  615. if (P_192) EC_GROUP_free(P_192);
  616. if (P_224) EC_GROUP_free(P_224);
  617. if (P_256) EC_GROUP_free(P_256);
  618. if (P_384) EC_GROUP_free(P_384);
  619. if (P_521) EC_GROUP_free(P_521);
  620. }
  621. /* Change test based on whether binary point compression is enabled or not. */
  622. #ifdef OPENSSL_EC_BIN_PT_COMP
  623. #define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  624. if (!BN_hex2bn(&x, _x)) ABORT; \
  625. if (!EC_POINT_set_compressed_coordinates_GF2m(group, P, x, _y_bit, ctx)) ABORT; \
  626. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
  627. if (!BN_hex2bn(&z, _order)) ABORT; \
  628. if (!BN_hex2bn(&cof, _cof)) ABORT; \
  629. if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
  630. if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
  631. fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
  632. BN_print_fp(stdout, x); \
  633. fprintf(stdout, "\n y = 0x"); \
  634. BN_print_fp(stdout, y); \
  635. fprintf(stdout, "\n"); \
  636. /* G_y value taken from the standard: */ \
  637. if (!BN_hex2bn(&z, _y)) ABORT; \
  638. if (0 != BN_cmp(y, z)) ABORT;
  639. #else
  640. #define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  641. if (!BN_hex2bn(&x, _x)) ABORT; \
  642. if (!BN_hex2bn(&y, _y)) ABORT; \
  643. if (!EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
  644. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
  645. if (!BN_hex2bn(&z, _order)) ABORT; \
  646. if (!BN_hex2bn(&cof, _cof)) ABORT; \
  647. if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
  648. fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
  649. BN_print_fp(stdout, x); \
  650. fprintf(stdout, "\n y = 0x"); \
  651. BN_print_fp(stdout, y); \
  652. fprintf(stdout, "\n");
  653. #endif
  654. #define CHAR2_CURVE_TEST(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  655. if (!BN_hex2bn(&p, _p)) ABORT; \
  656. if (!BN_hex2bn(&a, _a)) ABORT; \
  657. if (!BN_hex2bn(&b, _b)) ABORT; \
  658. if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT; \
  659. CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  660. fprintf(stdout, "verify degree ..."); \
  661. if (EC_GROUP_get_degree(group) != _degree) ABORT; \
  662. fprintf(stdout, " ok\n"); \
  663. fprintf(stdout, "verify group order ..."); \
  664. fflush(stdout); \
  665. if (!EC_GROUP_get_order(group, z, ctx)) ABORT; \
  666. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; \
  667. if (!EC_POINT_is_at_infinity(group, Q)) ABORT; \
  668. fprintf(stdout, "."); \
  669. fflush(stdout); \
  670. if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; \
  671. if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; \
  672. if (!EC_POINT_is_at_infinity(group, Q)) ABORT; \
  673. fprintf(stdout, " ok\n"); \
  674. if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \
  675. if (!EC_GROUP_copy(_variable, group)) ABORT;
  676. void char2_field_tests()
  677. {
  678. BN_CTX *ctx = NULL;
  679. BIGNUM *p, *a, *b;
  680. EC_GROUP *group;
  681. EC_GROUP *C2_K163 = NULL, *C2_K233 = NULL, *C2_K283 = NULL, *C2_K409 = NULL, *C2_K571 = NULL;
  682. EC_GROUP *C2_B163 = NULL, *C2_B233 = NULL, *C2_B283 = NULL, *C2_B409 = NULL, *C2_B571 = NULL;
  683. EC_POINT *P, *Q, *R;
  684. BIGNUM *x, *y, *z, *cof;
  685. unsigned char buf[100];
  686. size_t i, len;
  687. int k;
  688. #if 1 /* optional */
  689. ctx = BN_CTX_new();
  690. if (!ctx) ABORT;
  691. #endif
  692. p = BN_new();
  693. a = BN_new();
  694. b = BN_new();
  695. if (!p || !a || !b) ABORT;
  696. if (!BN_hex2bn(&p, "13")) ABORT;
  697. if (!BN_hex2bn(&a, "3")) ABORT;
  698. if (!BN_hex2bn(&b, "1")) ABORT;
  699. group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use EC_GROUP_new_curve_GF2m
  700. * so that the library gets to choose the EC_METHOD */
  701. if (!group) ABORT;
  702. if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT;
  703. {
  704. EC_GROUP *tmp;
  705. tmp = EC_GROUP_new(EC_GROUP_method_of(group));
  706. if (!tmp) ABORT;
  707. if (!EC_GROUP_copy(tmp, group));
  708. EC_GROUP_free(group);
  709. group = tmp;
  710. }
  711. if (!EC_GROUP_get_curve_GF2m(group, p, a, b, ctx)) ABORT;
  712. fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 + x*y = x^3 + a*x^2 + b (mod 0x");
  713. BN_print_fp(stdout, p);
  714. fprintf(stdout, ")\n a = 0x");
  715. BN_print_fp(stdout, a);
  716. fprintf(stdout, "\n b = 0x");
  717. BN_print_fp(stdout, b);
  718. fprintf(stdout, "\n(0x... means binary polynomial)\n");
  719. P = EC_POINT_new(group);
  720. Q = EC_POINT_new(group);
  721. R = EC_POINT_new(group);
  722. if (!P || !Q || !R) ABORT;
  723. if (!EC_POINT_set_to_infinity(group, P)) ABORT;
  724. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  725. buf[0] = 0;
  726. if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
  727. if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
  728. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  729. x = BN_new();
  730. y = BN_new();
  731. z = BN_new();
  732. cof = BN_new();
  733. if (!x || !y || !z || !cof) ABORT;
  734. if (!BN_hex2bn(&x, "6")) ABORT;
  735. /* Change test based on whether binary point compression is enabled or not. */
  736. #ifdef OPENSSL_EC_BIN_PT_COMP
  737. if (!EC_POINT_set_compressed_coordinates_GF2m(group, Q, x, 1, ctx)) ABORT;
  738. #else
  739. if (!BN_hex2bn(&y, "8")) ABORT;
  740. if (!EC_POINT_set_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
  741. #endif
  742. if (!EC_POINT_is_on_curve(group, Q, ctx))
  743. {
  744. /* Change test based on whether binary point compression is enabled or not. */
  745. #ifdef OPENSSL_EC_BIN_PT_COMP
  746. if (!EC_POINT_get_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
  747. #endif
  748. fprintf(stderr, "Point is not on curve: x = 0x");
  749. BN_print_fp(stderr, x);
  750. fprintf(stderr, ", y = 0x");
  751. BN_print_fp(stderr, y);
  752. fprintf(stderr, "\n");
  753. ABORT;
  754. }
  755. fprintf(stdout, "A cyclic subgroup:\n");
  756. k = 100;
  757. do
  758. {
  759. if (k-- == 0) ABORT;
  760. if (EC_POINT_is_at_infinity(group, P))
  761. fprintf(stdout, " point at infinity\n");
  762. else
  763. {
  764. if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT;
  765. fprintf(stdout, " x = 0x");
  766. BN_print_fp(stdout, x);
  767. fprintf(stdout, ", y = 0x");
  768. BN_print_fp(stdout, y);
  769. fprintf(stdout, "\n");
  770. }
  771. if (!EC_POINT_copy(R, P)) ABORT;
  772. if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
  773. }
  774. while (!EC_POINT_is_at_infinity(group, P));
  775. if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
  776. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  777. /* Change test based on whether binary point compression is enabled or not. */
  778. #ifdef OPENSSL_EC_BIN_PT_COMP
  779. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
  780. if (len == 0) ABORT;
  781. if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
  782. if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
  783. fprintf(stdout, "Generator as octet string, compressed form:\n ");
  784. for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
  785. #endif
  786. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
  787. if (len == 0) ABORT;
  788. if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
  789. if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
  790. fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
  791. for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
  792. /* Change test based on whether binary point compression is enabled or not. */
  793. #ifdef OPENSSL_EC_BIN_PT_COMP
  794. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
  795. if (len == 0) ABORT;
  796. if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
  797. if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
  798. fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
  799. for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
  800. #endif
  801. fprintf(stdout, "\n");
  802. if (!EC_POINT_invert(group, P, ctx)) ABORT;
  803. if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
  804. /* Curve K-163 (FIPS PUB 186-2, App. 6) */
  805. CHAR2_CURVE_TEST
  806. (
  807. "NIST curve K-163",
  808. "0800000000000000000000000000000000000000C9",
  809. "1",
  810. "1",
  811. "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
  812. "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
  813. 1,
  814. "04000000000000000000020108A2E0CC0D99F8A5EF",
  815. "2",
  816. 163,
  817. C2_K163
  818. );
  819. /* Curve B-163 (FIPS PUB 186-2, App. 6) */
  820. CHAR2_CURVE_TEST
  821. (
  822. "NIST curve B-163",
  823. "0800000000000000000000000000000000000000C9",
  824. "1",
  825. "020A601907B8C953CA1481EB10512F78744A3205FD",
  826. "03F0EBA16286A2D57EA0991168D4994637E8343E36",
  827. "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
  828. 1,
  829. "040000000000000000000292FE77E70C12A4234C33",
  830. "2",
  831. 163,
  832. C2_B163
  833. );
  834. /* Curve K-233 (FIPS PUB 186-2, App. 6) */
  835. CHAR2_CURVE_TEST
  836. (
  837. "NIST curve K-233",
  838. "020000000000000000000000000000000000000004000000000000000001",
  839. "0",
  840. "1",
  841. "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
  842. "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
  843. 0,
  844. "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
  845. "4",
  846. 233,
  847. C2_K233
  848. );
  849. /* Curve B-233 (FIPS PUB 186-2, App. 6) */
  850. CHAR2_CURVE_TEST
  851. (
  852. "NIST curve B-233",
  853. "020000000000000000000000000000000000000004000000000000000001",
  854. "000000000000000000000000000000000000000000000000000000000001",
  855. "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
  856. "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
  857. "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
  858. 1,
  859. "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
  860. "2",
  861. 233,
  862. C2_B233
  863. );
  864. /* Curve K-283 (FIPS PUB 186-2, App. 6) */
  865. CHAR2_CURVE_TEST
  866. (
  867. "NIST curve K-283",
  868. "0800000000000000000000000000000000000000000000000000000000000000000010A1",
  869. "0",
  870. "1",
  871. "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
  872. "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
  873. 0,
  874. "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
  875. "4",
  876. 283,
  877. C2_K283
  878. );
  879. /* Curve B-283 (FIPS PUB 186-2, App. 6) */
  880. CHAR2_CURVE_TEST
  881. (
  882. "NIST curve B-283",
  883. "0800000000000000000000000000000000000000000000000000000000000000000010A1",
  884. "000000000000000000000000000000000000000000000000000000000000000000000001",
  885. "027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
  886. "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
  887. "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
  888. 1,
  889. "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
  890. "2",
  891. 283,
  892. C2_B283
  893. );
  894. /* Curve K-409 (FIPS PUB 186-2, App. 6) */
  895. CHAR2_CURVE_TEST
  896. (
  897. "NIST curve K-409",
  898. "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
  899. "0",
  900. "1",
  901. "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
  902. "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
  903. 1,
  904. "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
  905. "4",
  906. 409,
  907. C2_K409
  908. );
  909. /* Curve B-409 (FIPS PUB 186-2, App. 6) */
  910. CHAR2_CURVE_TEST
  911. (
  912. "NIST curve B-409",
  913. "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
  914. "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
  915. "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
  916. "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
  917. "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
  918. 1,
  919. "010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
  920. "2",
  921. 409,
  922. C2_B409
  923. );
  924. /* Curve K-571 (FIPS PUB 186-2, App. 6) */
  925. CHAR2_CURVE_TEST
  926. (
  927. "NIST curve K-571",
  928. "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
  929. "0",
  930. "1",
  931. "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
  932. "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
  933. 0,
  934. "020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
  935. "4",
  936. 571,
  937. C2_K571
  938. );
  939. /* Curve B-571 (FIPS PUB 186-2, App. 6) */
  940. CHAR2_CURVE_TEST
  941. (
  942. "NIST curve B-571",
  943. "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
  944. "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
  945. "02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
  946. "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
  947. "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
  948. 1,
  949. "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
  950. "2",
  951. 571,
  952. C2_B571
  953. );
  954. /* more tests using the last curve */
  955. if (!EC_POINT_copy(Q, P)) ABORT;
  956. if (EC_POINT_is_at_infinity(group, Q)) ABORT;
  957. if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
  958. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  959. if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
  960. if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
  961. if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
  962. if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
  963. {
  964. const EC_POINT *points[3];
  965. const BIGNUM *scalars[3];
  966. if (EC_POINT_is_at_infinity(group, Q)) ABORT;
  967. points[0] = Q;
  968. points[1] = Q;
  969. points[2] = Q;
  970. if (!BN_add(y, z, BN_value_one())) ABORT;
  971. if (BN_is_odd(y)) ABORT;
  972. if (!BN_rshift1(y, y)) ABORT;
  973. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  974. scalars[1] = y;
  975. fprintf(stdout, "combined multiplication ...");
  976. fflush(stdout);
  977. /* z is still the group order */
  978. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
  979. if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
  980. if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
  981. if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
  982. fprintf(stdout, ".");
  983. fflush(stdout);
  984. if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
  985. if (!BN_add(z, z, y)) ABORT;
  986. BN_set_sign(z, 1);
  987. scalars[0] = y;
  988. scalars[1] = z; /* z = -(order + y) */
  989. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
  990. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  991. fprintf(stdout, ".");
  992. fflush(stdout);
  993. if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
  994. if (!BN_add(z, x, y)) ABORT;
  995. BN_set_sign(z, 1);
  996. scalars[0] = x;
  997. scalars[1] = y;
  998. scalars[2] = z; /* z = -(x+y) */
  999. if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
  1000. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  1001. fprintf(stdout, " ok\n\n");
  1002. }
  1003. #if 0
  1004. timings(C2_K163, TIMING_BASE_PT, ctx);
  1005. timings(C2_K163, TIMING_RAND_PT, ctx);
  1006. timings(C2_K163, TIMING_SIMUL, ctx);
  1007. timings(C2_B163, TIMING_BASE_PT, ctx);
  1008. timings(C2_B163, TIMING_RAND_PT, ctx);
  1009. timings(C2_B163, TIMING_SIMUL, ctx);
  1010. timings(C2_K233, TIMING_BASE_PT, ctx);
  1011. timings(C2_K233, TIMING_RAND_PT, ctx);
  1012. timings(C2_K233, TIMING_SIMUL, ctx);
  1013. timings(C2_B233, TIMING_BASE_PT, ctx);
  1014. timings(C2_B233, TIMING_RAND_PT, ctx);
  1015. timings(C2_B233, TIMING_SIMUL, ctx);
  1016. timings(C2_K283, TIMING_BASE_PT, ctx);
  1017. timings(C2_K283, TIMING_RAND_PT, ctx);
  1018. timings(C2_K283, TIMING_SIMUL, ctx);
  1019. timings(C2_B283, TIMING_BASE_PT, ctx);
  1020. timings(C2_B283, TIMING_RAND_PT, ctx);
  1021. timings(C2_B283, TIMING_SIMUL, ctx);
  1022. timings(C2_K409, TIMING_BASE_PT, ctx);
  1023. timings(C2_K409, TIMING_RAND_PT, ctx);
  1024. timings(C2_K409, TIMING_SIMUL, ctx);
  1025. timings(C2_B409, TIMING_BASE_PT, ctx);
  1026. timings(C2_B409, TIMING_RAND_PT, ctx);
  1027. timings(C2_B409, TIMING_SIMUL, ctx);
  1028. timings(C2_K571, TIMING_BASE_PT, ctx);
  1029. timings(C2_K571, TIMING_RAND_PT, ctx);
  1030. timings(C2_K571, TIMING_SIMUL, ctx);
  1031. timings(C2_B571, TIMING_BASE_PT, ctx);
  1032. timings(C2_B571, TIMING_RAND_PT, ctx);
  1033. timings(C2_B571, TIMING_SIMUL, ctx);
  1034. #endif
  1035. if (ctx)
  1036. BN_CTX_free(ctx);
  1037. BN_free(p); BN_free(a); BN_free(b);
  1038. EC_GROUP_free(group);
  1039. EC_POINT_free(P);
  1040. EC_POINT_free(Q);
  1041. EC_POINT_free(R);
  1042. BN_free(x); BN_free(y); BN_free(z); BN_free(cof);
  1043. if (C2_K163) EC_GROUP_free(C2_K163);
  1044. if (C2_B163) EC_GROUP_free(C2_B163);
  1045. if (C2_K233) EC_GROUP_free(C2_K233);
  1046. if (C2_B233) EC_GROUP_free(C2_B233);
  1047. if (C2_K283) EC_GROUP_free(C2_K283);
  1048. if (C2_B283) EC_GROUP_free(C2_B283);
  1049. if (C2_K409) EC_GROUP_free(C2_K409);
  1050. if (C2_B409) EC_GROUP_free(C2_B409);
  1051. if (C2_K571) EC_GROUP_free(C2_K571);
  1052. if (C2_B571) EC_GROUP_free(C2_B571);
  1053. }
  1054. void internal_curve_test(void)
  1055. {
  1056. EC_builtin_curve *curves = NULL;
  1057. size_t crv_len = 0, n = 0;
  1058. int ok = 1;
  1059. crv_len = EC_get_builtin_curves(NULL, 0);
  1060. curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
  1061. if (curves == NULL)
  1062. return;
  1063. if (!EC_get_builtin_curves(curves, crv_len))
  1064. {
  1065. OPENSSL_free(curves);
  1066. return;
  1067. }
  1068. fprintf(stdout, "testing internal curves: ");
  1069. for (n = 0; n < crv_len; n++)
  1070. {
  1071. EC_GROUP *group = NULL;
  1072. int nid = curves[n].nid;
  1073. if ((group = EC_GROUP_new_by_nid(nid)) == NULL)
  1074. {
  1075. ok = 0;
  1076. fprintf(stdout, "\nEC_GROUP_new_by_nid() failed with"
  1077. " curve %s\n", OBJ_nid2sn(nid));
  1078. /* try next curve */
  1079. continue;
  1080. }
  1081. if (!EC_GROUP_check(group, NULL))
  1082. {
  1083. ok = 0;
  1084. fprintf(stdout, "\nEC_GROUP_check() failed with"
  1085. " curve %s\n", OBJ_nid2sn(nid));
  1086. EC_GROUP_free(group);
  1087. /* try the next curve */
  1088. continue;
  1089. }
  1090. fprintf(stdout, ".");
  1091. fflush(stdout);
  1092. EC_GROUP_free(group);
  1093. }
  1094. if (ok)
  1095. fprintf(stdout, " ok\n");
  1096. else
  1097. fprintf(stdout, " failed\n");
  1098. OPENSSL_free(curves);
  1099. return;
  1100. }
  1101. static const char rnd_seed[] = "string to make the random number generator think it has entropy";
  1102. int main(int argc, char *argv[])
  1103. {
  1104. /* enable memory leak checking unless explicitly disabled */
  1105. if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
  1106. {
  1107. CRYPTO_malloc_debug_init();
  1108. CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
  1109. }
  1110. else
  1111. {
  1112. /* OPENSSL_DEBUG_MEMORY=off */
  1113. CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
  1114. }
  1115. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  1116. ERR_load_crypto_strings();
  1117. RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
  1118. prime_field_tests();
  1119. puts("");
  1120. char2_field_tests();
  1121. /* test the internal curves */
  1122. internal_curve_test();
  1123. #ifndef OPENSSL_NO_ENGINE
  1124. ENGINE_cleanup();
  1125. #endif
  1126. CRYPTO_cleanup_all_ex_data();
  1127. ERR_free_strings();
  1128. ERR_remove_state(0);
  1129. CRYPTO_mem_leaks_fp(stderr);
  1130. return 0;
  1131. }
  1132. #endif