ectest.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773
  1. /*
  2. * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /* ====================================================================
  10. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  11. *
  12. * Portions of the attached software ("Contribution") are developed by
  13. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  14. *
  15. * The Contribution is licensed pursuant to the OpenSSL open source
  16. * license provided above.
  17. *
  18. * The elliptic curve binary polynomial software is originally written by
  19. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
  20. *
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include "e_os.h"
  27. #ifdef OPENSSL_NO_EC
  28. int main(int argc, char *argv[])
  29. {
  30. puts("Elliptic curves are disabled.");
  31. return 0;
  32. }
  33. #else
  34. # include <openssl/ec.h>
  35. # ifndef OPENSSL_NO_ENGINE
  36. # include <openssl/engine.h>
  37. # endif
  38. # include <openssl/err.h>
  39. # include <openssl/obj_mac.h>
  40. # include <openssl/objects.h>
  41. # include <openssl/rand.h>
  42. # include <openssl/bn.h>
  43. # include <openssl/opensslconf.h>
  44. # if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12)
  45. /* suppress "too big too optimize" warning */
  46. # pragma warning(disable:4959)
  47. # endif
  48. # define ABORT do { \
  49. fflush(stdout); \
  50. fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
  51. ERR_print_errors_fp(stderr); \
  52. EXIT(1); \
  53. } while (0)
  54. # define TIMING_BASE_PT 0
  55. # define TIMING_RAND_PT 1
  56. # define TIMING_SIMUL 2
  57. /* test multiplication with group order, long and negative scalars */
  58. static void group_order_tests(EC_GROUP *group)
  59. {
  60. BIGNUM *n1, *n2, *order;
  61. EC_POINT *P = EC_POINT_new(group);
  62. EC_POINT *Q = EC_POINT_new(group);
  63. EC_POINT *R = EC_POINT_new(group);
  64. EC_POINT *S = EC_POINT_new(group);
  65. BN_CTX *ctx = BN_CTX_new();
  66. int i;
  67. n1 = BN_new();
  68. n2 = BN_new();
  69. order = BN_new();
  70. fprintf(stdout, "verify group order ...");
  71. fflush(stdout);
  72. if (!EC_GROUP_get_order(group, order, ctx))
  73. ABORT;
  74. if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
  75. ABORT;
  76. if (!EC_POINT_is_at_infinity(group, Q))
  77. ABORT;
  78. fprintf(stdout, ".");
  79. fflush(stdout);
  80. if (!EC_GROUP_precompute_mult(group, ctx))
  81. ABORT;
  82. if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
  83. ABORT;
  84. if (!EC_POINT_is_at_infinity(group, Q))
  85. ABORT;
  86. fprintf(stdout, " ok\n");
  87. fprintf(stdout, "long/negative scalar tests ");
  88. for (i = 1; i <= 2; i++) {
  89. const BIGNUM *scalars[6];
  90. const EC_POINT *points[6];
  91. fprintf(stdout, i == 1 ?
  92. "allowing precomputation ... " :
  93. "without precomputation ... ");
  94. if (!BN_set_word(n1, i))
  95. ABORT;
  96. /*
  97. * If i == 1, P will be the predefined generator for which
  98. * EC_GROUP_precompute_mult has set up precomputation.
  99. */
  100. if (!EC_POINT_mul(group, P, n1, NULL, NULL, ctx))
  101. ABORT;
  102. if (!BN_one(n1))
  103. ABORT;
  104. /* n1 = 1 - order */
  105. if (!BN_sub(n1, n1, order))
  106. ABORT;
  107. if (!EC_POINT_mul(group, Q, NULL, P, n1, ctx))
  108. ABORT;
  109. if (0 != EC_POINT_cmp(group, Q, P, ctx))
  110. ABORT;
  111. /* n2 = 1 + order */
  112. if (!BN_add(n2, order, BN_value_one()))
  113. ABORT;
  114. if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  115. ABORT;
  116. if (0 != EC_POINT_cmp(group, Q, P, ctx))
  117. ABORT;
  118. /* n2 = (1 - order) * (1 + order) = 1 - order^2 */
  119. if (!BN_mul(n2, n1, n2, ctx))
  120. ABORT;
  121. if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  122. ABORT;
  123. if (0 != EC_POINT_cmp(group, Q, P, ctx))
  124. ABORT;
  125. /* n2 = order^2 - 1 */
  126. BN_set_negative(n2, 0);
  127. if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  128. ABORT;
  129. /* Add P to verify the result. */
  130. if (!EC_POINT_add(group, Q, Q, P, ctx))
  131. ABORT;
  132. if (!EC_POINT_is_at_infinity(group, Q))
  133. ABORT;
  134. /* Exercise EC_POINTs_mul, including corner cases. */
  135. if (EC_POINT_is_at_infinity(group, P))
  136. ABORT;
  137. scalars[0] = scalars[1] = BN_value_one();
  138. points[0] = points[1] = P;
  139. if (!EC_POINTs_mul(group, R, NULL, 2, points, scalars, ctx))
  140. ABORT;
  141. if (!EC_POINT_dbl(group, S, points[0], ctx))
  142. ABORT;
  143. if (0 != EC_POINT_cmp(group, R, S, ctx))
  144. ABORT;
  145. scalars[0] = n1;
  146. points[0] = Q; /* => infinity */
  147. scalars[1] = n2;
  148. points[1] = P; /* => -P */
  149. scalars[2] = n1;
  150. points[2] = Q; /* => infinity */
  151. scalars[3] = n2;
  152. points[3] = Q; /* => infinity */
  153. scalars[4] = n1;
  154. points[4] = P; /* => P */
  155. scalars[5] = n2;
  156. points[5] = Q; /* => infinity */
  157. if (!EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx))
  158. ABORT;
  159. if (!EC_POINT_is_at_infinity(group, P))
  160. ABORT;
  161. }
  162. fprintf(stdout, "ok\n");
  163. EC_POINT_free(P);
  164. EC_POINT_free(Q);
  165. EC_POINT_free(R);
  166. EC_POINT_free(S);
  167. BN_free(n1);
  168. BN_free(n2);
  169. BN_free(order);
  170. BN_CTX_free(ctx);
  171. }
  172. static void prime_field_tests(void)
  173. {
  174. BN_CTX *ctx = NULL;
  175. BIGNUM *p, *a, *b;
  176. EC_GROUP *group;
  177. EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, *P_256 =
  178. NULL, *P_384 = NULL, *P_521 = NULL;
  179. EC_POINT *P, *Q, *R;
  180. BIGNUM *x, *y, *z, *yplusone;
  181. unsigned char buf[100];
  182. size_t i, len;
  183. int k;
  184. ctx = BN_CTX_new();
  185. if (!ctx)
  186. ABORT;
  187. p = BN_new();
  188. a = BN_new();
  189. b = BN_new();
  190. if (!p || !a || !b)
  191. ABORT;
  192. if (!BN_hex2bn(&p, "17"))
  193. ABORT;
  194. if (!BN_hex2bn(&a, "1"))
  195. ABORT;
  196. if (!BN_hex2bn(&b, "1"))
  197. ABORT;
  198. group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use
  199. * EC_GROUP_new_curve_GFp so
  200. * that the library gets to
  201. * choose the EC_METHOD */
  202. if (!group)
  203. ABORT;
  204. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  205. ABORT;
  206. {
  207. EC_GROUP *tmp;
  208. tmp = EC_GROUP_new(EC_GROUP_method_of(group));
  209. if (!tmp)
  210. ABORT;
  211. if (!EC_GROUP_copy(tmp, group))
  212. ABORT;
  213. EC_GROUP_free(group);
  214. group = tmp;
  215. }
  216. if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx))
  217. ABORT;
  218. fprintf(stdout,
  219. "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x");
  220. BN_print_fp(stdout, p);
  221. fprintf(stdout, ")\n a = 0x");
  222. BN_print_fp(stdout, a);
  223. fprintf(stdout, "\n b = 0x");
  224. BN_print_fp(stdout, b);
  225. fprintf(stdout, "\n");
  226. P = EC_POINT_new(group);
  227. Q = EC_POINT_new(group);
  228. R = EC_POINT_new(group);
  229. if (!P || !Q || !R)
  230. ABORT;
  231. if (!EC_POINT_set_to_infinity(group, P))
  232. ABORT;
  233. if (!EC_POINT_is_at_infinity(group, P))
  234. ABORT;
  235. buf[0] = 0;
  236. if (!EC_POINT_oct2point(group, Q, buf, 1, ctx))
  237. ABORT;
  238. if (!EC_POINT_add(group, P, P, Q, ctx))
  239. ABORT;
  240. if (!EC_POINT_is_at_infinity(group, P))
  241. ABORT;
  242. x = BN_new();
  243. y = BN_new();
  244. z = BN_new();
  245. yplusone = BN_new();
  246. if (x == NULL || y == NULL || z == NULL || yplusone == NULL)
  247. ABORT;
  248. if (!BN_hex2bn(&x, "D"))
  249. ABORT;
  250. if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx))
  251. ABORT;
  252. if (EC_POINT_is_on_curve(group, Q, ctx) <= 0) {
  253. if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx))
  254. ABORT;
  255. fprintf(stderr, "Point is not on curve: x = 0x");
  256. BN_print_fp(stderr, x);
  257. fprintf(stderr, ", y = 0x");
  258. BN_print_fp(stderr, y);
  259. fprintf(stderr, "\n");
  260. ABORT;
  261. }
  262. fprintf(stdout, "A cyclic subgroup:\n");
  263. k = 100;
  264. do {
  265. if (k-- == 0)
  266. ABORT;
  267. if (EC_POINT_is_at_infinity(group, P))
  268. fprintf(stdout, " point at infinity\n");
  269. else {
  270. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
  271. ABORT;
  272. fprintf(stdout, " x = 0x");
  273. BN_print_fp(stdout, x);
  274. fprintf(stdout, ", y = 0x");
  275. BN_print_fp(stdout, y);
  276. fprintf(stdout, "\n");
  277. }
  278. if (!EC_POINT_copy(R, P))
  279. ABORT;
  280. if (!EC_POINT_add(group, P, P, Q, ctx))
  281. ABORT;
  282. }
  283. while (!EC_POINT_is_at_infinity(group, P));
  284. if (!EC_POINT_add(group, P, Q, R, ctx))
  285. ABORT;
  286. if (!EC_POINT_is_at_infinity(group, P))
  287. ABORT;
  288. len =
  289. EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,
  290. sizeof buf, ctx);
  291. if (len == 0)
  292. ABORT;
  293. if (!EC_POINT_oct2point(group, P, buf, len, ctx))
  294. ABORT;
  295. if (0 != EC_POINT_cmp(group, P, Q, ctx))
  296. ABORT;
  297. fprintf(stdout, "Generator as octet string, compressed form:\n ");
  298. for (i = 0; i < len; i++)
  299. fprintf(stdout, "%02X", buf[i]);
  300. len =
  301. EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf,
  302. sizeof buf, ctx);
  303. if (len == 0)
  304. ABORT;
  305. if (!EC_POINT_oct2point(group, P, buf, len, ctx))
  306. ABORT;
  307. if (0 != EC_POINT_cmp(group, P, Q, ctx))
  308. ABORT;
  309. fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
  310. for (i = 0; i < len; i++)
  311. fprintf(stdout, "%02X", buf[i]);
  312. len =
  313. EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf,
  314. ctx);
  315. if (len == 0)
  316. ABORT;
  317. if (!EC_POINT_oct2point(group, P, buf, len, ctx))
  318. ABORT;
  319. if (0 != EC_POINT_cmp(group, P, Q, ctx))
  320. ABORT;
  321. fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
  322. for (i = 0; i < len; i++)
  323. fprintf(stdout, "%02X", buf[i]);
  324. if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx))
  325. ABORT;
  326. fprintf(stdout,
  327. "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x");
  328. BN_print_fp(stdout, x);
  329. fprintf(stdout, ", Y = 0x");
  330. BN_print_fp(stdout, y);
  331. fprintf(stdout, ", Z = 0x");
  332. BN_print_fp(stdout, z);
  333. fprintf(stdout, "\n");
  334. if (!EC_POINT_invert(group, P, ctx))
  335. ABORT;
  336. if (0 != EC_POINT_cmp(group, P, R, ctx))
  337. ABORT;
  338. /*
  339. * Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2,
  340. * 2000) -- not a NIST curve, but commonly used
  341. */
  342. if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"))
  343. ABORT;
  344. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  345. ABORT;
  346. if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"))
  347. ABORT;
  348. if (!BN_hex2bn(&b, "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45"))
  349. ABORT;
  350. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  351. ABORT;
  352. if (!BN_hex2bn(&x, "4A96B5688EF573284664698968C38BB913CBFC82"))
  353. ABORT;
  354. if (!BN_hex2bn(&y, "23a628553168947d59dcc912042351377ac5fb32"))
  355. ABORT;
  356. if (!BN_add(yplusone, y, BN_value_one()))
  357. ABORT;
  358. /*
  359. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  360. * and therefore setting the coordinates should fail.
  361. */
  362. if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))
  363. ABORT;
  364. if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx))
  365. ABORT;
  366. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  367. ABORT;
  368. if (!BN_hex2bn(&z, "0100000000000000000001F4C8F927AED3CA752257"))
  369. ABORT;
  370. if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
  371. ABORT;
  372. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
  373. ABORT;
  374. fprintf(stdout, "\nSEC2 curve secp160r1 -- Generator:\n x = 0x");
  375. BN_print_fp(stdout, x);
  376. fprintf(stdout, "\n y = 0x");
  377. BN_print_fp(stdout, y);
  378. fprintf(stdout, "\n");
  379. /* G_y value taken from the standard: */
  380. if (!BN_hex2bn(&z, "23a628553168947d59dcc912042351377ac5fb32"))
  381. ABORT;
  382. if (0 != BN_cmp(y, z))
  383. ABORT;
  384. fprintf(stdout, "verify degree ...");
  385. if (EC_GROUP_get_degree(group) != 160)
  386. ABORT;
  387. fprintf(stdout, " ok\n");
  388. group_order_tests(group);
  389. if ((P_160 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)
  390. ABORT;
  391. if (!EC_GROUP_copy(P_160, group))
  392. ABORT;
  393. /* Curve P-192 (FIPS PUB 186-2, App. 6) */
  394. if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"))
  395. ABORT;
  396. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  397. ABORT;
  398. if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"))
  399. ABORT;
  400. if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"))
  401. ABORT;
  402. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  403. ABORT;
  404. if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"))
  405. ABORT;
  406. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))
  407. ABORT;
  408. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  409. ABORT;
  410. if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"))
  411. ABORT;
  412. if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
  413. ABORT;
  414. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
  415. ABORT;
  416. fprintf(stdout, "\nNIST curve P-192 -- Generator:\n x = 0x");
  417. BN_print_fp(stdout, x);
  418. fprintf(stdout, "\n y = 0x");
  419. BN_print_fp(stdout, y);
  420. fprintf(stdout, "\n");
  421. /* G_y value taken from the standard: */
  422. if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"))
  423. ABORT;
  424. if (0 != BN_cmp(y, z))
  425. ABORT;
  426. if (!BN_add(yplusone, y, BN_value_one()))
  427. ABORT;
  428. /*
  429. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  430. * and therefore setting the coordinates should fail.
  431. */
  432. if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))
  433. ABORT;
  434. fprintf(stdout, "verify degree ...");
  435. if (EC_GROUP_get_degree(group) != 192)
  436. ABORT;
  437. fprintf(stdout, " ok\n");
  438. group_order_tests(group);
  439. if ((P_192 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)
  440. ABORT;
  441. if (!EC_GROUP_copy(P_192, group))
  442. ABORT;
  443. /* Curve P-224 (FIPS PUB 186-2, App. 6) */
  444. if (!BN_hex2bn
  445. (&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"))
  446. ABORT;
  447. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  448. ABORT;
  449. if (!BN_hex2bn
  450. (&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"))
  451. ABORT;
  452. if (!BN_hex2bn
  453. (&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"))
  454. ABORT;
  455. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  456. ABORT;
  457. if (!BN_hex2bn
  458. (&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"))
  459. ABORT;
  460. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx))
  461. ABORT;
  462. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  463. ABORT;
  464. if (!BN_hex2bn
  465. (&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"))
  466. ABORT;
  467. if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
  468. ABORT;
  469. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
  470. ABORT;
  471. fprintf(stdout, "\nNIST curve P-224 -- Generator:\n x = 0x");
  472. BN_print_fp(stdout, x);
  473. fprintf(stdout, "\n y = 0x");
  474. BN_print_fp(stdout, y);
  475. fprintf(stdout, "\n");
  476. /* G_y value taken from the standard: */
  477. if (!BN_hex2bn
  478. (&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"))
  479. ABORT;
  480. if (0 != BN_cmp(y, z))
  481. ABORT;
  482. if (!BN_add(yplusone, y, BN_value_one()))
  483. ABORT;
  484. /*
  485. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  486. * and therefore setting the coordinates should fail.
  487. */
  488. if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))
  489. ABORT;
  490. fprintf(stdout, "verify degree ...");
  491. if (EC_GROUP_get_degree(group) != 224)
  492. ABORT;
  493. fprintf(stdout, " ok\n");
  494. group_order_tests(group);
  495. if ((P_224 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)
  496. ABORT;
  497. if (!EC_GROUP_copy(P_224, group))
  498. ABORT;
  499. /* Curve P-256 (FIPS PUB 186-2, App. 6) */
  500. if (!BN_hex2bn
  501. (&p,
  502. "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"))
  503. ABORT;
  504. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  505. ABORT;
  506. if (!BN_hex2bn
  507. (&a,
  508. "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC"))
  509. ABORT;
  510. if (!BN_hex2bn
  511. (&b,
  512. "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"))
  513. ABORT;
  514. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  515. ABORT;
  516. if (!BN_hex2bn
  517. (&x,
  518. "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"))
  519. ABORT;
  520. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))
  521. ABORT;
  522. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  523. ABORT;
  524. if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
  525. "84F3B9CAC2FC632551"))
  526. ABORT;
  527. if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
  528. ABORT;
  529. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
  530. ABORT;
  531. fprintf(stdout, "\nNIST curve P-256 -- Generator:\n x = 0x");
  532. BN_print_fp(stdout, x);
  533. fprintf(stdout, "\n y = 0x");
  534. BN_print_fp(stdout, y);
  535. fprintf(stdout, "\n");
  536. /* G_y value taken from the standard: */
  537. if (!BN_hex2bn
  538. (&z,
  539. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"))
  540. ABORT;
  541. if (0 != BN_cmp(y, z))
  542. ABORT;
  543. if (!BN_add(yplusone, y, BN_value_one()))
  544. ABORT;
  545. /*
  546. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  547. * and therefore setting the coordinates should fail.
  548. */
  549. if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))
  550. ABORT;
  551. fprintf(stdout, "verify degree ...");
  552. if (EC_GROUP_get_degree(group) != 256)
  553. ABORT;
  554. fprintf(stdout, " ok\n");
  555. group_order_tests(group);
  556. if ((P_256 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)
  557. ABORT;
  558. if (!EC_GROUP_copy(P_256, group))
  559. ABORT;
  560. /* Curve P-384 (FIPS PUB 186-2, App. 6) */
  561. if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  562. "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"))
  563. ABORT;
  564. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  565. ABORT;
  566. if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  567. "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC"))
  568. ABORT;
  569. if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
  570. "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"))
  571. ABORT;
  572. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  573. ABORT;
  574. if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
  575. "9859F741E082542A385502F25DBF55296C3A545E3872760AB7"))
  576. ABORT;
  577. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))
  578. ABORT;
  579. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  580. ABORT;
  581. if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  582. "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"))
  583. ABORT;
  584. if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
  585. ABORT;
  586. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
  587. ABORT;
  588. fprintf(stdout, "\nNIST curve P-384 -- Generator:\n x = 0x");
  589. BN_print_fp(stdout, x);
  590. fprintf(stdout, "\n y = 0x");
  591. BN_print_fp(stdout, y);
  592. fprintf(stdout, "\n");
  593. /* G_y value taken from the standard: */
  594. if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
  595. "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"))
  596. ABORT;
  597. if (0 != BN_cmp(y, z))
  598. ABORT;
  599. if (!BN_add(yplusone, y, BN_value_one()))
  600. ABORT;
  601. /*
  602. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  603. * and therefore setting the coordinates should fail.
  604. */
  605. if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))
  606. ABORT;
  607. fprintf(stdout, "verify degree ...");
  608. if (EC_GROUP_get_degree(group) != 384)
  609. ABORT;
  610. fprintf(stdout, " ok\n");
  611. group_order_tests(group);
  612. if ((P_384 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)
  613. ABORT;
  614. if (!EC_GROUP_copy(P_384, group))
  615. ABORT;
  616. /* Curve P-521 (FIPS PUB 186-2, App. 6) */
  617. if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  618. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  619. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
  620. ABORT;
  621. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  622. ABORT;
  623. if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  624. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  625. "FFFFFFFFFFFFFFFFFFFFFFFFFFFC"))
  626. ABORT;
  627. if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
  628. "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
  629. "DF883D2C34F1EF451FD46B503F00"))
  630. ABORT;
  631. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  632. ABORT;
  633. if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
  634. "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
  635. "3C1856A429BF97E7E31C2E5BD66"))
  636. ABORT;
  637. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx))
  638. ABORT;
  639. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  640. ABORT;
  641. if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  642. "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
  643. "C9B8899C47AEBB6FB71E91386409"))
  644. ABORT;
  645. if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
  646. ABORT;
  647. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
  648. ABORT;
  649. fprintf(stdout, "\nNIST curve P-521 -- Generator:\n x = 0x");
  650. BN_print_fp(stdout, x);
  651. fprintf(stdout, "\n y = 0x");
  652. BN_print_fp(stdout, y);
  653. fprintf(stdout, "\n");
  654. /* G_y value taken from the standard: */
  655. if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
  656. "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
  657. "7086A272C24088BE94769FD16650"))
  658. ABORT;
  659. if (0 != BN_cmp(y, z))
  660. ABORT;
  661. if (!BN_add(yplusone, y, BN_value_one()))
  662. ABORT;
  663. /*
  664. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  665. * and therefore setting the coordinates should fail.
  666. */
  667. if (EC_POINT_set_affine_coordinates_GFp(group, P, x, yplusone, ctx))
  668. ABORT;
  669. fprintf(stdout, "verify degree ...");
  670. if (EC_GROUP_get_degree(group) != 521)
  671. ABORT;
  672. fprintf(stdout, " ok\n");
  673. group_order_tests(group);
  674. if ((P_521 = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL)
  675. ABORT;
  676. if (!EC_GROUP_copy(P_521, group))
  677. ABORT;
  678. /* more tests using the last curve */
  679. /* Restore the point that got mangled in the (x, y + 1) test. */
  680. if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx))
  681. ABORT;
  682. if (!EC_POINT_copy(Q, P))
  683. ABORT;
  684. if (EC_POINT_is_at_infinity(group, Q))
  685. ABORT;
  686. if (!EC_POINT_dbl(group, P, P, ctx))
  687. ABORT;
  688. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  689. ABORT;
  690. if (!EC_POINT_invert(group, Q, ctx))
  691. ABORT; /* P = -2Q */
  692. if (!EC_POINT_add(group, R, P, Q, ctx))
  693. ABORT;
  694. if (!EC_POINT_add(group, R, R, Q, ctx))
  695. ABORT;
  696. if (!EC_POINT_is_at_infinity(group, R))
  697. ABORT; /* R = P + 2Q */
  698. {
  699. const EC_POINT *points[4];
  700. const BIGNUM *scalars[4];
  701. BIGNUM *scalar3;
  702. if (EC_POINT_is_at_infinity(group, Q))
  703. ABORT;
  704. points[0] = Q;
  705. points[1] = Q;
  706. points[2] = Q;
  707. points[3] = Q;
  708. if (!EC_GROUP_get_order(group, z, ctx))
  709. ABORT;
  710. if (!BN_add(y, z, BN_value_one()))
  711. ABORT;
  712. if (BN_is_odd(y))
  713. ABORT;
  714. if (!BN_rshift1(y, y))
  715. ABORT;
  716. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  717. scalars[1] = y;
  718. fprintf(stdout, "combined multiplication ...");
  719. fflush(stdout);
  720. /* z is still the group order */
  721. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  722. ABORT;
  723. if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
  724. ABORT;
  725. if (0 != EC_POINT_cmp(group, P, R, ctx))
  726. ABORT;
  727. if (0 != EC_POINT_cmp(group, R, Q, ctx))
  728. ABORT;
  729. fprintf(stdout, ".");
  730. fflush(stdout);
  731. if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0))
  732. ABORT;
  733. if (!BN_add(z, z, y))
  734. ABORT;
  735. BN_set_negative(z, 1);
  736. scalars[0] = y;
  737. scalars[1] = z; /* z = -(order + y) */
  738. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  739. ABORT;
  740. if (!EC_POINT_is_at_infinity(group, P))
  741. ABORT;
  742. fprintf(stdout, ".");
  743. fflush(stdout);
  744. if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0))
  745. ABORT;
  746. if (!BN_add(z, x, y))
  747. ABORT;
  748. BN_set_negative(z, 1);
  749. scalars[0] = x;
  750. scalars[1] = y;
  751. scalars[2] = z; /* z = -(x+y) */
  752. scalar3 = BN_new();
  753. if (!scalar3)
  754. ABORT;
  755. BN_zero(scalar3);
  756. scalars[3] = scalar3;
  757. if (!EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx))
  758. ABORT;
  759. if (!EC_POINT_is_at_infinity(group, P))
  760. ABORT;
  761. fprintf(stdout, " ok\n\n");
  762. BN_free(scalar3);
  763. }
  764. BN_CTX_free(ctx);
  765. BN_free(p);
  766. BN_free(a);
  767. BN_free(b);
  768. EC_GROUP_free(group);
  769. EC_POINT_free(P);
  770. EC_POINT_free(Q);
  771. EC_POINT_free(R);
  772. BN_free(x);
  773. BN_free(y);
  774. BN_free(z);
  775. BN_free(yplusone);
  776. EC_GROUP_free(P_160);
  777. EC_GROUP_free(P_192);
  778. EC_GROUP_free(P_224);
  779. EC_GROUP_free(P_256);
  780. EC_GROUP_free(P_384);
  781. EC_GROUP_free(P_521);
  782. }
  783. /* Change test based on whether binary point compression is enabled or not. */
  784. # ifdef OPENSSL_EC_BIN_PT_COMP
  785. # define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  786. if (!BN_hex2bn(&x, _x)) ABORT; \
  787. if (!BN_hex2bn(&y, _y)) ABORT; \
  788. if (!BN_add(yplusone, y, BN_value_one())) ABORT; \
  789. /* \
  790. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not, \
  791. * and therefore setting the coordinates should fail. \
  792. */ \
  793. if (EC_POINT_set_affine_coordinates_GF2m(group, P, x, yplusone, ctx)) ABORT; \
  794. if (!EC_POINT_set_compressed_coordinates_GF2m(group, P, x, _y_bit, ctx)) ABORT; \
  795. if (EC_POINT_is_on_curve(group, P, ctx) <= 0) ABORT; \
  796. if (!BN_hex2bn(&z, _order)) ABORT; \
  797. if (!BN_hex2bn(&cof, _cof)) ABORT; \
  798. if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
  799. if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
  800. fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
  801. BN_print_fp(stdout, x); \
  802. fprintf(stdout, "\n y = 0x"); \
  803. BN_print_fp(stdout, y); \
  804. fprintf(stdout, "\n"); \
  805. /* G_y value taken from the standard: */ \
  806. if (!BN_hex2bn(&z, _y)) ABORT; \
  807. if (0 != BN_cmp(y, z)) ABORT;
  808. # else
  809. # define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  810. if (!BN_hex2bn(&x, _x)) ABORT; \
  811. if (!BN_hex2bn(&y, _y)) ABORT; \
  812. if (!BN_add(yplusone, y, BN_value_one())) ABORT; \
  813. /* \
  814. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not, \
  815. * and therefore setting the coordinates should fail. \
  816. */ \
  817. if (EC_POINT_set_affine_coordinates_GF2m(group, P, x, yplusone, ctx)) ABORT; \
  818. if (!EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
  819. if (EC_POINT_is_on_curve(group, P, ctx) <= 0) ABORT; \
  820. if (!BN_hex2bn(&z, _order)) ABORT; \
  821. if (!BN_hex2bn(&cof, _cof)) ABORT; \
  822. if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
  823. fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
  824. BN_print_fp(stdout, x); \
  825. fprintf(stdout, "\n y = 0x"); \
  826. BN_print_fp(stdout, y); \
  827. fprintf(stdout, "\n");
  828. # endif
  829. # define CHAR2_CURVE_TEST(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  830. if (!BN_hex2bn(&p, _p)) ABORT; \
  831. if (!BN_hex2bn(&a, _a)) ABORT; \
  832. if (!BN_hex2bn(&b, _b)) ABORT; \
  833. if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT; \
  834. CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  835. fprintf(stdout, "verify degree ..."); \
  836. if (EC_GROUP_get_degree(group) != _degree) ABORT; \
  837. fprintf(stdout, " ok\n"); \
  838. group_order_tests(group); \
  839. if ((_variable = EC_GROUP_new(EC_GROUP_method_of(group))) == NULL) ABORT; \
  840. if (!EC_GROUP_copy(_variable, group)) ABORT; \
  841. # ifndef OPENSSL_NO_EC2M
  842. static void char2_field_tests(void)
  843. {
  844. BN_CTX *ctx = NULL;
  845. BIGNUM *p, *a, *b;
  846. EC_GROUP *group;
  847. EC_GROUP *C2_K163 = NULL, *C2_K233 = NULL, *C2_K283 = NULL, *C2_K409 =
  848. NULL, *C2_K571 = NULL;
  849. EC_GROUP *C2_B163 = NULL, *C2_B233 = NULL, *C2_B283 = NULL, *C2_B409 =
  850. NULL, *C2_B571 = NULL;
  851. EC_POINT *P, *Q, *R;
  852. BIGNUM *x, *y, *z, *cof, *yplusone;
  853. unsigned char buf[100];
  854. size_t i, len;
  855. int k;
  856. ctx = BN_CTX_new();
  857. if (!ctx)
  858. ABORT;
  859. p = BN_new();
  860. a = BN_new();
  861. b = BN_new();
  862. if (p == NULL || a == NULL || b == NULL)
  863. ABORT;
  864. if (!BN_hex2bn(&p, "13"))
  865. ABORT;
  866. if (!BN_hex2bn(&a, "3"))
  867. ABORT;
  868. if (!BN_hex2bn(&b, "1"))
  869. ABORT;
  870. group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use
  871. * EC_GROUP_new_curve_GF2m
  872. * so that the library gets
  873. * to choose the EC_METHOD */
  874. if (!group)
  875. ABORT;
  876. if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx))
  877. ABORT;
  878. {
  879. EC_GROUP *tmp;
  880. tmp = EC_GROUP_new(EC_GROUP_method_of(group));
  881. if (!tmp)
  882. ABORT;
  883. if (!EC_GROUP_copy(tmp, group))
  884. ABORT;
  885. EC_GROUP_free(group);
  886. group = tmp;
  887. }
  888. if (!EC_GROUP_get_curve_GF2m(group, p, a, b, ctx))
  889. ABORT;
  890. fprintf(stdout,
  891. "Curve defined by Weierstrass equation\n y^2 + x*y = x^3 + a*x^2 + b (mod 0x");
  892. BN_print_fp(stdout, p);
  893. fprintf(stdout, ")\n a = 0x");
  894. BN_print_fp(stdout, a);
  895. fprintf(stdout, "\n b = 0x");
  896. BN_print_fp(stdout, b);
  897. fprintf(stdout, "\n(0x... means binary polynomial)\n");
  898. P = EC_POINT_new(group);
  899. Q = EC_POINT_new(group);
  900. R = EC_POINT_new(group);
  901. if (!P || !Q || !R)
  902. ABORT;
  903. if (!EC_POINT_set_to_infinity(group, P))
  904. ABORT;
  905. if (!EC_POINT_is_at_infinity(group, P))
  906. ABORT;
  907. buf[0] = 0;
  908. if (!EC_POINT_oct2point(group, Q, buf, 1, ctx))
  909. ABORT;
  910. if (!EC_POINT_add(group, P, P, Q, ctx))
  911. ABORT;
  912. if (!EC_POINT_is_at_infinity(group, P))
  913. ABORT;
  914. x = BN_new();
  915. y = BN_new();
  916. z = BN_new();
  917. cof = BN_new();
  918. yplusone = BN_new();
  919. if (x == NULL || y == NULL || z == NULL || cof == NULL || yplusone == NULL)
  920. ABORT;
  921. if (!BN_hex2bn(&x, "6"))
  922. ABORT;
  923. /* Change test based on whether binary point compression is enabled or not. */
  924. # ifdef OPENSSL_EC_BIN_PT_COMP
  925. if (!EC_POINT_set_compressed_coordinates_GF2m(group, Q, x, 1, ctx))
  926. ABORT;
  927. # else
  928. if (!BN_hex2bn(&y, "8"))
  929. ABORT;
  930. if (!EC_POINT_set_affine_coordinates_GF2m(group, Q, x, y, ctx))
  931. ABORT;
  932. # endif
  933. if (EC_POINT_is_on_curve(group, Q, ctx) <= 0) {
  934. /* Change test based on whether binary point compression is enabled or not. */
  935. # ifdef OPENSSL_EC_BIN_PT_COMP
  936. if (!EC_POINT_get_affine_coordinates_GF2m(group, Q, x, y, ctx))
  937. ABORT;
  938. # endif
  939. fprintf(stderr, "Point is not on curve: x = 0x");
  940. BN_print_fp(stderr, x);
  941. fprintf(stderr, ", y = 0x");
  942. BN_print_fp(stderr, y);
  943. fprintf(stderr, "\n");
  944. ABORT;
  945. }
  946. fprintf(stdout, "A cyclic subgroup:\n");
  947. k = 100;
  948. do {
  949. if (k-- == 0)
  950. ABORT;
  951. if (EC_POINT_is_at_infinity(group, P))
  952. fprintf(stdout, " point at infinity\n");
  953. else {
  954. if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx))
  955. ABORT;
  956. fprintf(stdout, " x = 0x");
  957. BN_print_fp(stdout, x);
  958. fprintf(stdout, ", y = 0x");
  959. BN_print_fp(stdout, y);
  960. fprintf(stdout, "\n");
  961. }
  962. if (!EC_POINT_copy(R, P))
  963. ABORT;
  964. if (!EC_POINT_add(group, P, P, Q, ctx))
  965. ABORT;
  966. }
  967. while (!EC_POINT_is_at_infinity(group, P));
  968. if (!EC_POINT_add(group, P, Q, R, ctx))
  969. ABORT;
  970. if (!EC_POINT_is_at_infinity(group, P))
  971. ABORT;
  972. /* Change test based on whether binary point compression is enabled or not. */
  973. # ifdef OPENSSL_EC_BIN_PT_COMP
  974. len =
  975. EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,
  976. sizeof buf, ctx);
  977. if (len == 0)
  978. ABORT;
  979. if (!EC_POINT_oct2point(group, P, buf, len, ctx))
  980. ABORT;
  981. if (0 != EC_POINT_cmp(group, P, Q, ctx))
  982. ABORT;
  983. fprintf(stdout, "Generator as octet string, compressed form:\n ");
  984. for (i = 0; i < len; i++)
  985. fprintf(stdout, "%02X", buf[i]);
  986. # endif
  987. len =
  988. EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf,
  989. sizeof buf, ctx);
  990. if (len == 0)
  991. ABORT;
  992. if (!EC_POINT_oct2point(group, P, buf, len, ctx))
  993. ABORT;
  994. if (0 != EC_POINT_cmp(group, P, Q, ctx))
  995. ABORT;
  996. fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
  997. for (i = 0; i < len; i++)
  998. fprintf(stdout, "%02X", buf[i]);
  999. /* Change test based on whether binary point compression is enabled or not. */
  1000. # ifdef OPENSSL_EC_BIN_PT_COMP
  1001. len =
  1002. EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf,
  1003. ctx);
  1004. if (len == 0)
  1005. ABORT;
  1006. if (!EC_POINT_oct2point(group, P, buf, len, ctx))
  1007. ABORT;
  1008. if (0 != EC_POINT_cmp(group, P, Q, ctx))
  1009. ABORT;
  1010. fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
  1011. for (i = 0; i < len; i++)
  1012. fprintf(stdout, "%02X", buf[i]);
  1013. # endif
  1014. fprintf(stdout, "\n");
  1015. if (!EC_POINT_invert(group, P, ctx))
  1016. ABORT;
  1017. if (0 != EC_POINT_cmp(group, P, R, ctx))
  1018. ABORT;
  1019. /* Curve K-163 (FIPS PUB 186-2, App. 6) */
  1020. CHAR2_CURVE_TEST
  1021. ("NIST curve K-163",
  1022. "0800000000000000000000000000000000000000C9",
  1023. "1",
  1024. "1",
  1025. "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
  1026. "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
  1027. 1, "04000000000000000000020108A2E0CC0D99F8A5EF", "2", 163, C2_K163);
  1028. /* Curve B-163 (FIPS PUB 186-2, App. 6) */
  1029. CHAR2_CURVE_TEST
  1030. ("NIST curve B-163",
  1031. "0800000000000000000000000000000000000000C9",
  1032. "1",
  1033. "020A601907B8C953CA1481EB10512F78744A3205FD",
  1034. "03F0EBA16286A2D57EA0991168D4994637E8343E36",
  1035. "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
  1036. 1, "040000000000000000000292FE77E70C12A4234C33", "2", 163, C2_B163);
  1037. /* Curve K-233 (FIPS PUB 186-2, App. 6) */
  1038. CHAR2_CURVE_TEST
  1039. ("NIST curve K-233",
  1040. "020000000000000000000000000000000000000004000000000000000001",
  1041. "0",
  1042. "1",
  1043. "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
  1044. "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
  1045. 0,
  1046. "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
  1047. "4", 233, C2_K233);
  1048. /* Curve B-233 (FIPS PUB 186-2, App. 6) */
  1049. CHAR2_CURVE_TEST
  1050. ("NIST curve B-233",
  1051. "020000000000000000000000000000000000000004000000000000000001",
  1052. "000000000000000000000000000000000000000000000000000000000001",
  1053. "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
  1054. "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
  1055. "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
  1056. 1,
  1057. "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
  1058. "2", 233, C2_B233);
  1059. /* Curve K-283 (FIPS PUB 186-2, App. 6) */
  1060. CHAR2_CURVE_TEST
  1061. ("NIST curve K-283",
  1062. "0800000000000000000000000000000000000000000000000000000000000000000010A1",
  1063. "0",
  1064. "1",
  1065. "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
  1066. "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
  1067. 0,
  1068. "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
  1069. "4", 283, C2_K283);
  1070. /* Curve B-283 (FIPS PUB 186-2, App. 6) */
  1071. CHAR2_CURVE_TEST
  1072. ("NIST curve B-283",
  1073. "0800000000000000000000000000000000000000000000000000000000000000000010A1",
  1074. "000000000000000000000000000000000000000000000000000000000000000000000001",
  1075. "027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
  1076. "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
  1077. "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
  1078. 1,
  1079. "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
  1080. "2", 283, C2_B283);
  1081. /* Curve K-409 (FIPS PUB 186-2, App. 6) */
  1082. CHAR2_CURVE_TEST
  1083. ("NIST curve K-409",
  1084. "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
  1085. "0",
  1086. "1",
  1087. "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
  1088. "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
  1089. 1,
  1090. "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
  1091. "4", 409, C2_K409);
  1092. /* Curve B-409 (FIPS PUB 186-2, App. 6) */
  1093. CHAR2_CURVE_TEST
  1094. ("NIST curve B-409",
  1095. "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
  1096. "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
  1097. "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
  1098. "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
  1099. "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
  1100. 1,
  1101. "010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
  1102. "2", 409, C2_B409);
  1103. /* Curve K-571 (FIPS PUB 186-2, App. 6) */
  1104. CHAR2_CURVE_TEST
  1105. ("NIST curve K-571",
  1106. "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
  1107. "0",
  1108. "1",
  1109. "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
  1110. "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
  1111. 0,
  1112. "020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
  1113. "4", 571, C2_K571);
  1114. /* Curve B-571 (FIPS PUB 186-2, App. 6) */
  1115. CHAR2_CURVE_TEST
  1116. ("NIST curve B-571",
  1117. "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
  1118. "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
  1119. "02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
  1120. "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
  1121. "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
  1122. 1,
  1123. "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
  1124. "2", 571, C2_B571);
  1125. /* more tests using the last curve */
  1126. if (!EC_POINT_copy(Q, P))
  1127. ABORT;
  1128. if (EC_POINT_is_at_infinity(group, Q))
  1129. ABORT;
  1130. if (!EC_POINT_dbl(group, P, P, ctx))
  1131. ABORT;
  1132. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  1133. ABORT;
  1134. if (!EC_POINT_invert(group, Q, ctx))
  1135. ABORT; /* P = -2Q */
  1136. if (!EC_POINT_add(group, R, P, Q, ctx))
  1137. ABORT;
  1138. if (!EC_POINT_add(group, R, R, Q, ctx))
  1139. ABORT;
  1140. if (!EC_POINT_is_at_infinity(group, R))
  1141. ABORT; /* R = P + 2Q */
  1142. {
  1143. const EC_POINT *points[3];
  1144. const BIGNUM *scalars[3];
  1145. if (EC_POINT_is_at_infinity(group, Q))
  1146. ABORT;
  1147. points[0] = Q;
  1148. points[1] = Q;
  1149. points[2] = Q;
  1150. if (!BN_add(y, z, BN_value_one()))
  1151. ABORT;
  1152. if (BN_is_odd(y))
  1153. ABORT;
  1154. if (!BN_rshift1(y, y))
  1155. ABORT;
  1156. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  1157. scalars[1] = y;
  1158. fprintf(stdout, "combined multiplication ...");
  1159. fflush(stdout);
  1160. /* z is still the group order */
  1161. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  1162. ABORT;
  1163. if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
  1164. ABORT;
  1165. if (0 != EC_POINT_cmp(group, P, R, ctx))
  1166. ABORT;
  1167. if (0 != EC_POINT_cmp(group, R, Q, ctx))
  1168. ABORT;
  1169. fprintf(stdout, ".");
  1170. fflush(stdout);
  1171. if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0))
  1172. ABORT;
  1173. if (!BN_add(z, z, y))
  1174. ABORT;
  1175. BN_set_negative(z, 1);
  1176. scalars[0] = y;
  1177. scalars[1] = z; /* z = -(order + y) */
  1178. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  1179. ABORT;
  1180. if (!EC_POINT_is_at_infinity(group, P))
  1181. ABORT;
  1182. fprintf(stdout, ".");
  1183. fflush(stdout);
  1184. if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0))
  1185. ABORT;
  1186. if (!BN_add(z, x, y))
  1187. ABORT;
  1188. BN_set_negative(z, 1);
  1189. scalars[0] = x;
  1190. scalars[1] = y;
  1191. scalars[2] = z; /* z = -(x+y) */
  1192. if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx))
  1193. ABORT;
  1194. if (!EC_POINT_is_at_infinity(group, P))
  1195. ABORT;
  1196. fprintf(stdout, " ok\n\n");
  1197. }
  1198. BN_CTX_free(ctx);
  1199. BN_free(p);
  1200. BN_free(a);
  1201. BN_free(b);
  1202. EC_GROUP_free(group);
  1203. EC_POINT_free(P);
  1204. EC_POINT_free(Q);
  1205. EC_POINT_free(R);
  1206. BN_free(x);
  1207. BN_free(y);
  1208. BN_free(z);
  1209. BN_free(cof);
  1210. BN_free(yplusone);
  1211. EC_GROUP_free(C2_K163);
  1212. EC_GROUP_free(C2_B163);
  1213. EC_GROUP_free(C2_K233);
  1214. EC_GROUP_free(C2_B233);
  1215. EC_GROUP_free(C2_K283);
  1216. EC_GROUP_free(C2_B283);
  1217. EC_GROUP_free(C2_K409);
  1218. EC_GROUP_free(C2_B409);
  1219. EC_GROUP_free(C2_K571);
  1220. EC_GROUP_free(C2_B571);
  1221. }
  1222. # endif
  1223. static void internal_curve_test(void)
  1224. {
  1225. EC_builtin_curve *curves = NULL;
  1226. size_t crv_len = 0, n = 0;
  1227. int ok = 1;
  1228. crv_len = EC_get_builtin_curves(NULL, 0);
  1229. curves = OPENSSL_malloc(sizeof(*curves) * crv_len);
  1230. if (curves == NULL)
  1231. return;
  1232. if (!EC_get_builtin_curves(curves, crv_len)) {
  1233. OPENSSL_free(curves);
  1234. return;
  1235. }
  1236. fprintf(stdout, "testing internal curves: ");
  1237. for (n = 0; n < crv_len; n++) {
  1238. EC_GROUP *group = NULL;
  1239. int nid = curves[n].nid;
  1240. if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL) {
  1241. ok = 0;
  1242. fprintf(stdout, "\nEC_GROUP_new_curve_name() failed with"
  1243. " curve %s\n", OBJ_nid2sn(nid));
  1244. /* try next curve */
  1245. continue;
  1246. }
  1247. if (!EC_GROUP_check(group, NULL)) {
  1248. ok = 0;
  1249. fprintf(stdout, "\nEC_GROUP_check() failed with"
  1250. " curve %s\n", OBJ_nid2sn(nid));
  1251. EC_GROUP_free(group);
  1252. /* try the next curve */
  1253. continue;
  1254. }
  1255. fprintf(stdout, ".");
  1256. fflush(stdout);
  1257. EC_GROUP_free(group);
  1258. }
  1259. if (ok)
  1260. fprintf(stdout, " ok\n\n");
  1261. else {
  1262. fprintf(stdout, " failed\n\n");
  1263. ABORT;
  1264. }
  1265. /* Test all built-in curves and let the library choose the EC_METHOD */
  1266. for (n = 0; n < crv_len; n++) {
  1267. EC_GROUP *group = NULL;
  1268. int nid = curves[n].nid;
  1269. /*
  1270. * Skip for X25519 because low level operations such as EC_POINT_mul()
  1271. * are not supported for this curve
  1272. */
  1273. if (nid == NID_X25519)
  1274. continue;
  1275. fprintf(stdout, "%s:\n", OBJ_nid2sn(nid));
  1276. fflush(stdout);
  1277. if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL) {
  1278. ABORT;
  1279. }
  1280. group_order_tests(group);
  1281. EC_GROUP_free(group);
  1282. }
  1283. OPENSSL_free(curves);
  1284. return;
  1285. }
  1286. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1287. /*
  1288. * nistp_test_params contains magic numbers for testing our optimized
  1289. * implementations of several NIST curves with characteristic > 3.
  1290. */
  1291. struct nistp_test_params {
  1292. const EC_METHOD *(*meth) ();
  1293. int degree;
  1294. /*
  1295. * Qx, Qy and D are taken from
  1296. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
  1297. * Otherwise, values are standard curve parameters from FIPS 180-3
  1298. */
  1299. const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d;
  1300. };
  1301. static const struct nistp_test_params nistp_tests_params[] = {
  1302. {
  1303. /* P-224 */
  1304. EC_GFp_nistp224_method,
  1305. 224,
  1306. /* p */
  1307. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
  1308. /* a */
  1309. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
  1310. /* b */
  1311. "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
  1312. /* Qx */
  1313. "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E",
  1314. /* Qy */
  1315. "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555",
  1316. /* Gx */
  1317. "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
  1318. /* Gy */
  1319. "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
  1320. /* order */
  1321. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
  1322. /* d */
  1323. "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8",
  1324. },
  1325. {
  1326. /* P-256 */
  1327. EC_GFp_nistp256_method,
  1328. 256,
  1329. /* p */
  1330. "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
  1331. /* a */
  1332. "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
  1333. /* b */
  1334. "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
  1335. /* Qx */
  1336. "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19",
  1337. /* Qy */
  1338. "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09",
  1339. /* Gx */
  1340. "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
  1341. /* Gy */
  1342. "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
  1343. /* order */
  1344. "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
  1345. /* d */
  1346. "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96",
  1347. },
  1348. {
  1349. /* P-521 */
  1350. EC_GFp_nistp521_method,
  1351. 521,
  1352. /* p */
  1353. "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  1354. /* a */
  1355. "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc",
  1356. /* b */
  1357. "051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
  1358. /* Qx */
  1359. "0098e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4",
  1360. /* Qy */
  1361. "0164350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e",
  1362. /* Gx */
  1363. "c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
  1364. /* Gy */
  1365. "11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
  1366. /* order */
  1367. "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
  1368. /* d */
  1369. "0100085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eeedf09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722",
  1370. },
  1371. };
  1372. static void nistp_single_test(const struct nistp_test_params *test)
  1373. {
  1374. BN_CTX *ctx;
  1375. BIGNUM *p, *a, *b, *x, *y, *n, *m, *order, *yplusone;
  1376. EC_GROUP *NISTP;
  1377. EC_POINT *G, *P, *Q, *Q_CHECK;
  1378. fprintf(stdout, "\nNIST curve P-%d (optimised implementation):\n",
  1379. test->degree);
  1380. ctx = BN_CTX_new();
  1381. p = BN_new();
  1382. a = BN_new();
  1383. b = BN_new();
  1384. x = BN_new();
  1385. y = BN_new();
  1386. m = BN_new();
  1387. n = BN_new();
  1388. order = BN_new();
  1389. yplusone = BN_new();
  1390. NISTP = EC_GROUP_new(test->meth());
  1391. if (!NISTP)
  1392. ABORT;
  1393. if (!BN_hex2bn(&p, test->p))
  1394. ABORT;
  1395. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  1396. ABORT;
  1397. if (!BN_hex2bn(&a, test->a))
  1398. ABORT;
  1399. if (!BN_hex2bn(&b, test->b))
  1400. ABORT;
  1401. if (!EC_GROUP_set_curve_GFp(NISTP, p, a, b, ctx))
  1402. ABORT;
  1403. G = EC_POINT_new(NISTP);
  1404. P = EC_POINT_new(NISTP);
  1405. Q = EC_POINT_new(NISTP);
  1406. Q_CHECK = EC_POINT_new(NISTP);
  1407. if (!BN_hex2bn(&x, test->Qx))
  1408. ABORT;
  1409. if (!BN_hex2bn(&y, test->Qy))
  1410. ABORT;
  1411. if (!BN_add(yplusone, y, BN_value_one()))
  1412. ABORT;
  1413. /*
  1414. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  1415. * and therefore setting the coordinates should fail.
  1416. */
  1417. if (EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x, yplusone, ctx))
  1418. ABORT;
  1419. if (!EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x, y, ctx))
  1420. ABORT;
  1421. if (!BN_hex2bn(&x, test->Gx))
  1422. ABORT;
  1423. if (!BN_hex2bn(&y, test->Gy))
  1424. ABORT;
  1425. if (!EC_POINT_set_affine_coordinates_GFp(NISTP, G, x, y, ctx))
  1426. ABORT;
  1427. if (!BN_hex2bn(&order, test->order))
  1428. ABORT;
  1429. if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))
  1430. ABORT;
  1431. fprintf(stdout, "verify degree ... ");
  1432. if (EC_GROUP_get_degree(NISTP) != test->degree)
  1433. ABORT;
  1434. fprintf(stdout, "ok\n");
  1435. fprintf(stdout, "NIST test vectors ... ");
  1436. if (!BN_hex2bn(&n, test->d))
  1437. ABORT;
  1438. /* fixed point multiplication */
  1439. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1440. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1441. ABORT;
  1442. /* random point multiplication */
  1443. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1444. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1445. ABORT;
  1446. /* set generator to P = 2*G, where G is the standard generator */
  1447. if (!EC_POINT_dbl(NISTP, P, G, ctx))
  1448. ABORT;
  1449. if (!EC_GROUP_set_generator(NISTP, P, order, BN_value_one()))
  1450. ABORT;
  1451. /* set the scalar to m=n/2, where n is the NIST test scalar */
  1452. if (!BN_rshift(m, n, 1))
  1453. ABORT;
  1454. /* test the non-standard generator */
  1455. /* fixed point multiplication */
  1456. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1457. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1458. ABORT;
  1459. /* random point multiplication */
  1460. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1461. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1462. ABORT;
  1463. /*
  1464. * We have not performed precomputation so have_precompute mult should be
  1465. * false
  1466. */
  1467. if (EC_GROUP_have_precompute_mult(NISTP))
  1468. ABORT;
  1469. /* now repeat all tests with precomputation */
  1470. if (!EC_GROUP_precompute_mult(NISTP, ctx))
  1471. ABORT;
  1472. if (!EC_GROUP_have_precompute_mult(NISTP))
  1473. ABORT;
  1474. /* fixed point multiplication */
  1475. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1476. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1477. ABORT;
  1478. /* random point multiplication */
  1479. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1480. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1481. ABORT;
  1482. /* reset generator */
  1483. if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))
  1484. ABORT;
  1485. /* fixed point multiplication */
  1486. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1487. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1488. ABORT;
  1489. /* random point multiplication */
  1490. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1491. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1492. ABORT;
  1493. fprintf(stdout, "ok\n");
  1494. group_order_tests(NISTP);
  1495. EC_GROUP_free(NISTP);
  1496. EC_POINT_free(G);
  1497. EC_POINT_free(P);
  1498. EC_POINT_free(Q);
  1499. EC_POINT_free(Q_CHECK);
  1500. BN_free(n);
  1501. BN_free(m);
  1502. BN_free(p);
  1503. BN_free(a);
  1504. BN_free(b);
  1505. BN_free(x);
  1506. BN_free(y);
  1507. BN_free(order);
  1508. BN_free(yplusone);
  1509. BN_CTX_free(ctx);
  1510. }
  1511. static void nistp_tests()
  1512. {
  1513. unsigned i;
  1514. for (i = 0; i < OSSL_NELEM(nistp_tests_params); i++) {
  1515. nistp_single_test(&nistp_tests_params[i]);
  1516. }
  1517. }
  1518. # endif
  1519. static void parameter_test(void)
  1520. {
  1521. EC_GROUP *group, *group2;
  1522. ECPARAMETERS *ecparameters;
  1523. fprintf(stderr, "\ntesting ecparameters conversion ...");
  1524. group = EC_GROUP_new_by_curve_name(NID_secp112r1);
  1525. if (!group)
  1526. ABORT;
  1527. ecparameters = EC_GROUP_get_ecparameters(group, NULL);
  1528. if (!ecparameters)
  1529. ABORT;
  1530. group2 = EC_GROUP_new_from_ecparameters(ecparameters);
  1531. if (!group2)
  1532. ABORT;
  1533. if (EC_GROUP_cmp(group, group2, NULL))
  1534. ABORT;
  1535. fprintf(stderr, " ok\n");
  1536. EC_GROUP_free(group);
  1537. EC_GROUP_free(group2);
  1538. ECPARAMETERS_free(ecparameters);
  1539. }
  1540. static const char rnd_seed[] =
  1541. "string to make the random number generator think it has entropy";
  1542. int main(int argc, char *argv[])
  1543. {
  1544. char *p;
  1545. p = getenv("OPENSSL_DEBUG_MEMORY");
  1546. if (p != NULL && strcmp(p, "on") == 0)
  1547. CRYPTO_set_mem_debug(1);
  1548. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  1549. RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
  1550. prime_field_tests();
  1551. puts("");
  1552. # ifndef OPENSSL_NO_EC2M
  1553. char2_field_tests();
  1554. # endif
  1555. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1556. nistp_tests();
  1557. # endif
  1558. /* test the internal curves */
  1559. internal_curve_test();
  1560. parameter_test();
  1561. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  1562. if (CRYPTO_mem_leaks_fp(stderr) <= 0)
  1563. return 1;
  1564. #endif
  1565. return 0;
  1566. }
  1567. #endif