2
0

ectest.c 44 KB

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