ectest.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. /*
  2. * Copyright 2001-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. #include "e_os.h"
  11. #include "testutil.h"
  12. #ifndef OPENSSL_NO_EC
  13. # include <openssl/ec.h>
  14. # ifndef OPENSSL_NO_ENGINE
  15. # include <openssl/engine.h>
  16. # endif
  17. # include <openssl/err.h>
  18. # include <openssl/obj_mac.h>
  19. # include <openssl/objects.h>
  20. # include <openssl/rand.h>
  21. # include <openssl/bn.h>
  22. # include <openssl/opensslconf.h>
  23. # if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12)
  24. /* suppress "too big too optimize" warning */
  25. # pragma warning(disable:4959)
  26. # endif
  27. static size_t crv_len = 0;
  28. static EC_builtin_curve *curves = NULL;
  29. /* test multiplication with group order, long and negative scalars */
  30. static int group_order_tests(EC_GROUP *group)
  31. {
  32. BIGNUM *n1 = NULL, *n2 = NULL, *order = NULL;
  33. EC_POINT *P = NULL, *Q = NULL, *R = NULL, *S = NULL;
  34. BN_CTX *ctx = NULL;
  35. int i = 0, r = 0;
  36. if (!TEST_ptr(n1 = BN_new())
  37. || !TEST_ptr(n2 = BN_new())
  38. || !TEST_ptr(order = BN_new())
  39. || !TEST_ptr(ctx = BN_CTX_new())
  40. || !TEST_ptr(P = EC_POINT_new(group))
  41. || !TEST_ptr(Q = EC_POINT_new(group))
  42. || !TEST_ptr(R = EC_POINT_new(group))
  43. || !TEST_ptr(S = EC_POINT_new(group)))
  44. goto err;
  45. if (!TEST_true(EC_GROUP_get_order(group, order, ctx))
  46. || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
  47. || !TEST_true(EC_POINT_is_at_infinity(group, Q))
  48. || !TEST_true(EC_GROUP_precompute_mult(group, ctx))
  49. || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
  50. || !TEST_true(EC_POINT_is_at_infinity(group, Q)))
  51. goto err;
  52. for (i = 1; i <= 2; i++) {
  53. const BIGNUM *scalars[6];
  54. const EC_POINT *points[6];
  55. if (!TEST_true(BN_set_word(n1, i))
  56. /*
  57. * If i == 1, P will be the predefined generator for which
  58. * EC_GROUP_precompute_mult has set up precomputation.
  59. */
  60. || !TEST_true(EC_POINT_mul(group, P, n1, NULL, NULL, ctx))
  61. || !TEST_true(BN_one(n1))
  62. /* n1 = 1 - order */
  63. || !TEST_true(BN_sub(n1, n1, order))
  64. || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n1, ctx))
  65. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))
  66. /* n2 = 1 + order */
  67. || !TEST_true(BN_add(n2, order, BN_value_one()))
  68. || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  69. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))
  70. /* n2 = (1 - order) * (1 + order) = 1 - order^2 */
  71. || !TEST_true(BN_mul(n2, n1, n2, ctx))
  72. || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  73. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))
  74. goto err;
  75. /* n2 = order^2 - 1 */
  76. BN_set_negative(n2, 0);
  77. if (!TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  78. /* Add P to verify the result. */
  79. || !TEST_true(EC_POINT_add(group, Q, Q, P, ctx))
  80. || !TEST_true(EC_POINT_is_at_infinity(group, Q))
  81. /* Exercise EC_POINTs_mul, including corner cases. */
  82. || !TEST_false(EC_POINT_is_at_infinity(group, P)))
  83. goto err;
  84. scalars[0] = scalars[1] = BN_value_one();
  85. points[0] = points[1] = P;
  86. if (!TEST_true(EC_POINTs_mul(group, R, NULL, 2, points, scalars, ctx))
  87. || !TEST_true(EC_POINT_dbl(group, S, points[0], ctx))
  88. || !TEST_int_eq(0, EC_POINT_cmp(group, R, S, ctx)))
  89. goto err;
  90. scalars[0] = n1;
  91. points[0] = Q; /* => infinity */
  92. scalars[1] = n2;
  93. points[1] = P; /* => -P */
  94. scalars[2] = n1;
  95. points[2] = Q; /* => infinity */
  96. scalars[3] = n2;
  97. points[3] = Q; /* => infinity */
  98. scalars[4] = n1;
  99. points[4] = P; /* => P */
  100. scalars[5] = n2;
  101. points[5] = Q; /* => infinity */
  102. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx))
  103. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  104. goto err;
  105. }
  106. r = 1;
  107. err:
  108. if (r == 0 && i != 0)
  109. TEST_info(i == 1 ? "allowing precomputation" :
  110. "without precomputation");
  111. EC_POINT_free(P);
  112. EC_POINT_free(Q);
  113. EC_POINT_free(R);
  114. EC_POINT_free(S);
  115. BN_free(n1);
  116. BN_free(n2);
  117. BN_free(order);
  118. BN_CTX_free(ctx);
  119. return r;
  120. }
  121. static int prime_field_tests(void)
  122. {
  123. BN_CTX *ctx = NULL;
  124. BIGNUM *p = NULL, *a = NULL, *b = NULL, *scalar3 = NULL;
  125. EC_GROUP *group = NULL, *tmp = NULL;
  126. EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL,
  127. *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
  128. EC_POINT *P = NULL, *Q = NULL, *R = NULL;
  129. BIGNUM *x = NULL, *y = NULL, *z = NULL, *yplusone = NULL;
  130. const EC_POINT *points[4];
  131. const BIGNUM *scalars[4];
  132. unsigned char buf[100];
  133. size_t len, r = 0;
  134. int k;
  135. if (!TEST_ptr(ctx = BN_CTX_new())
  136. || !TEST_ptr(p = BN_new())
  137. || !TEST_ptr(a = BN_new())
  138. || !TEST_ptr(b = BN_new())
  139. || !TEST_true(BN_hex2bn(&p, "17"))
  140. || !TEST_true(BN_hex2bn(&a, "1"))
  141. || !TEST_true(BN_hex2bn(&b, "1"))
  142. /*
  143. * applications should use EC_GROUP_new_curve_GFp so
  144. * that the library gets to choose the EC_METHOD
  145. */
  146. || !TEST_ptr(group = EC_GROUP_new(EC_GFp_mont_method()))
  147. || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  148. || !TEST_ptr(tmp = EC_GROUP_new(EC_GROUP_method_of(group)))
  149. || !TEST_true(EC_GROUP_copy(tmp, group)))
  150. goto err;
  151. EC_GROUP_free(group);
  152. group = tmp;
  153. tmp = NULL;
  154. if (!TEST_true(EC_GROUP_get_curve_GFp(group, p, a, b, ctx)))
  155. goto err;
  156. TEST_info("Curve defined by Weierstrass equation");
  157. TEST_note(" y^2 = x^3 + a*x + b (mod p)");
  158. test_output_bignum("a", a);
  159. test_output_bignum("b", b);
  160. test_output_bignum("p", p);
  161. buf[0] = 0;
  162. if (!TEST_ptr(P = EC_POINT_new(group))
  163. || !TEST_ptr(Q = EC_POINT_new(group))
  164. || !TEST_ptr(R = EC_POINT_new(group))
  165. || !TEST_true(EC_POINT_set_to_infinity(group, P))
  166. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  167. || !TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx))
  168. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))
  169. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  170. || !TEST_ptr(x = BN_new())
  171. || !TEST_ptr(y = BN_new())
  172. || !TEST_ptr(z = BN_new())
  173. || !TEST_ptr(yplusone = BN_new())
  174. || !TEST_true(BN_hex2bn(&x, "D"))
  175. || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1,
  176. ctx)))
  177. goto err;
  178. if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) {
  179. if (!TEST_true(EC_POINT_get_affine_coordinates_GFp(group, Q, x, y,
  180. ctx)))
  181. goto err;
  182. TEST_info("Point is not on curve");
  183. test_output_bignum("x", x);
  184. test_output_bignum("y", y);
  185. goto err;
  186. }
  187. TEST_note("A cyclic subgroup:");
  188. k = 100;
  189. do {
  190. if (!TEST_int_ne(k--, 0))
  191. goto err;
  192. if (EC_POINT_is_at_infinity(group, P)) {
  193. TEST_note(" point at infinity");
  194. } else {
  195. if (!TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y,
  196. ctx)))
  197. goto err;
  198. test_output_bignum("x", x);
  199. test_output_bignum("y", y);
  200. }
  201. if (!TEST_true(EC_POINT_copy(R, P))
  202. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)))
  203. goto err;
  204. } while (!EC_POINT_is_at_infinity(group, P));
  205. if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx))
  206. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  207. goto err;
  208. len =
  209. EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,
  210. sizeof buf, ctx);
  211. if (!TEST_size_t_ne(len, 0)
  212. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  213. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  214. goto err;
  215. test_output_memory("Generator as octet string, compressed form:",
  216. buf, len);
  217. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
  218. buf, sizeof buf, ctx);
  219. if (!TEST_size_t_ne(len, 0)
  220. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  221. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  222. goto err;
  223. test_output_memory("Generator as octet string, uncompressed form:",
  224. buf, len);
  225. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID,
  226. buf, sizeof buf, ctx);
  227. if (!TEST_size_t_ne(len, 0)
  228. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  229. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  230. goto err;
  231. test_output_memory("Generator as octet string, hybrid form:",
  232. buf, len);
  233. if (!TEST_true(EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z,
  234. ctx)))
  235. goto err;
  236. TEST_info("A representation of the inverse of that generator in");
  237. TEST_note("Jacobian projective coordinates");
  238. test_output_bignum("x", x);
  239. test_output_bignum("y", y);
  240. test_output_bignum("z", z);
  241. if (!TEST_true(EC_POINT_invert(group, P, ctx))
  242. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
  243. /*
  244. * Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2,
  245. * 2000) -- not a NIST curve, but commonly used
  246. */
  247. || !TEST_true(BN_hex2bn(&p, "FFFFFFFF"
  248. "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"))
  249. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  250. || !TEST_true(BN_hex2bn(&a, "FFFFFFFF"
  251. "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"))
  252. || !TEST_true(BN_hex2bn(&b, "1C97BEFC"
  253. "54BD7A8B65ACF89F81D4D4ADC565FA45"))
  254. || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  255. || !TEST_true(BN_hex2bn(&x, "4A96B568"
  256. "8EF573284664698968C38BB913CBFC82"))
  257. || !TEST_true(BN_hex2bn(&y, "23a62855"
  258. "3168947d59dcc912042351377ac5fb32"))
  259. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  260. /*
  261. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  262. * and therefore setting the coordinates should fail.
  263. */
  264. || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x,
  265. yplusone, ctx))
  266. || !TEST_true(EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx))
  267. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  268. || !TEST_true(BN_hex2bn(&z, "0100000000"
  269. "000000000001F4C8F927AED3CA752257"))
  270. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  271. || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
  272. goto err;
  273. TEST_info("SEC2 curve secp160r1 -- Generator");
  274. test_output_bignum("x", x);
  275. test_output_bignum("y", y);
  276. /* G_y value taken from the standard: */
  277. if (!TEST_true(BN_hex2bn(&z, "23a62855"
  278. "3168947d59dcc912042351377ac5fb32"))
  279. || !TEST_BN_eq(y, z)
  280. || !TEST_int_eq(EC_GROUP_get_degree(group), 160)
  281. || !group_order_tests(group)
  282. || !TEST_ptr(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))
  283. || !TEST_true(EC_GROUP_copy(P_160, group))
  284. /* Curve P-192 (FIPS PUB 186-2, App. 6) */
  285. || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFF"
  286. "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"))
  287. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  288. || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFF"
  289. "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"))
  290. || !TEST_true(BN_hex2bn(&b, "64210519E59C80E7"
  291. "0FA7E9AB72243049FEB8DEECC146B9B1"))
  292. || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  293. || !TEST_true(BN_hex2bn(&x, "188DA80EB03090F6"
  294. "7CBF20EB43A18800F4FF0AFD82FF1012"))
  295. || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1,
  296. ctx))
  297. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  298. || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFF"
  299. "FFFFFFFF99DEF836146BC9B1B4D22831"))
  300. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  301. || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
  302. goto err;
  303. TEST_info("NIST curve P-192 -- Generator");
  304. test_output_bignum("x", x);
  305. test_output_bignum("y", y);
  306. /* G_y value taken from the standard: */
  307. if (!TEST_true(BN_hex2bn(&z, "07192B95FFC8DA78"
  308. "631011ED6B24CDD573F977A11E794811"))
  309. || !TEST_BN_eq(y, z)
  310. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  311. /*
  312. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  313. * and therefore setting the coordinates should fail.
  314. */
  315. || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x,
  316. yplusone, ctx))
  317. || !TEST_int_eq(EC_GROUP_get_degree(group), 192)
  318. || !group_order_tests(group)
  319. || !TEST_ptr(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))
  320. || !TEST_true(EC_GROUP_copy(P_192, group))
  321. /* Curve P-224 (FIPS PUB 186-2, App. 6) */
  322. || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFF"
  323. "FFFFFFFF000000000000000000000001"))
  324. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  325. || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFF"
  326. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"))
  327. || !TEST_true(BN_hex2bn(&b, "B4050A850C04B3ABF5413256"
  328. "5044B0B7D7BFD8BA270B39432355FFB4"))
  329. || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  330. || !TEST_true(BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B9"
  331. "4A03C1D356C21122343280D6115C1D21"))
  332. || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0,
  333. ctx))
  334. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  335. || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF"
  336. "FFFF16A2E0B8F03E13DD29455C5C2A3D"))
  337. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  338. || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
  339. goto err;
  340. TEST_info("NIST curve P-224 -- Generator");
  341. test_output_bignum("x", x);
  342. test_output_bignum("y", y);
  343. /* G_y value taken from the standard: */
  344. if (!TEST_true(BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6"
  345. "CD4375A05A07476444D5819985007E34"))
  346. || !TEST_BN_eq(y, z)
  347. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  348. /*
  349. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  350. * and therefore setting the coordinates should fail.
  351. */
  352. || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x,
  353. yplusone, ctx))
  354. || !TEST_int_eq(EC_GROUP_get_degree(group), 224)
  355. || !group_order_tests(group)
  356. || !TEST_ptr(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))
  357. || !TEST_true(EC_GROUP_copy(P_224, group))
  358. /* Curve P-256 (FIPS PUB 186-2, App. 6) */
  359. || !TEST_true(BN_hex2bn(&p, "FFFFFFFF000000010000000000000000"
  360. "00000000FFFFFFFFFFFFFFFFFFFFFFFF"))
  361. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  362. || !TEST_true(BN_hex2bn(&a, "FFFFFFFF000000010000000000000000"
  363. "00000000FFFFFFFFFFFFFFFFFFFFFFFC"))
  364. || !TEST_true(BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC"
  365. "651D06B0CC53B0F63BCE3C3E27D2604B"))
  366. || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  367. || !TEST_true(BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F2"
  368. "77037D812DEB33A0F4A13945D898C296"))
  369. || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1,
  370. ctx))
  371. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  372. || !TEST_true(BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFF"
  373. "BCE6FAADA7179E84F3B9CAC2FC632551"))
  374. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  375. || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
  376. goto err;
  377. TEST_info("NIST curve P-256 -- Generator");
  378. test_output_bignum("x", x);
  379. test_output_bignum("y", y);
  380. /* G_y value taken from the standard: */
  381. if (!TEST_true(BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  382. "2BCE33576B315ECECBB6406837BF51F5"))
  383. || !TEST_BN_eq(y, z)
  384. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  385. /*
  386. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  387. * and therefore setting the coordinates should fail.
  388. */
  389. || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x,
  390. yplusone, ctx))
  391. || !TEST_int_eq(EC_GROUP_get_degree(group), 256)
  392. || !group_order_tests(group)
  393. || !TEST_ptr(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))
  394. || !TEST_true(EC_GROUP_copy(P_256, group))
  395. /* Curve P-384 (FIPS PUB 186-2, App. 6) */
  396. || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  397. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
  398. "FFFFFFFF0000000000000000FFFFFFFF"))
  399. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  400. || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  401. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
  402. "FFFFFFFF0000000000000000FFFFFFFC"))
  403. || !TEST_true(BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19"
  404. "181D9C6EFE8141120314088F5013875A"
  405. "C656398D8A2ED19D2A85C8EDD3EC2AEF"))
  406. || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  407. || !TEST_true(BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD74"
  408. "6E1D3B628BA79B9859F741E082542A38"
  409. "5502F25DBF55296C3A545E3872760AB7"))
  410. || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1,
  411. ctx))
  412. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  413. || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  414. "FFFFFFFFFFFFFFFFC7634D81F4372DDF"
  415. "581A0DB248B0A77AECEC196ACCC52973"))
  416. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  417. || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
  418. goto err;
  419. TEST_info("NIST curve P-384 -- Generator");
  420. test_output_bignum("x", x);
  421. test_output_bignum("y", y);
  422. /* G_y value taken from the standard: */
  423. if (!TEST_true(BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29"
  424. "F8F41DBD289A147CE9DA3113B5F0B8C0"
  425. "0A60B1CE1D7E819D7A431D7C90EA0E5F"))
  426. || !TEST_BN_eq(y, z)
  427. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  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. || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x,
  433. yplusone, ctx))
  434. || !TEST_int_eq(EC_GROUP_get_degree(group), 384)
  435. || !group_order_tests(group)
  436. || !TEST_ptr(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))
  437. || !TEST_true(EC_GROUP_copy(P_384, group))
  438. /* Curve P-521 (FIPS PUB 186-2, App. 6) */
  439. || !TEST_true(BN_hex2bn(&p, "1FF"
  440. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  441. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  442. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  443. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
  444. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  445. || !TEST_true(BN_hex2bn(&a, "1FF"
  446. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  447. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  448. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  449. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC"))
  450. || !TEST_true(BN_hex2bn(&b, "051"
  451. "953EB9618E1C9A1F929A21A0B68540EE"
  452. "A2DA725B99B315F3B8B489918EF109E1"
  453. "56193951EC7E937B1652C0BD3BB1BF07"
  454. "3573DF883D2C34F1EF451FD46B503F00"))
  455. || !TEST_true(EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  456. || !TEST_true(BN_hex2bn(&x, "C6"
  457. "858E06B70404E9CD9E3ECB662395B442"
  458. "9C648139053FB521F828AF606B4D3DBA"
  459. "A14B5E77EFE75928FE1DC127A2FFA8DE"
  460. "3348B3C1856A429BF97E7E31C2E5BD66"))
  461. || !TEST_true(EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0,
  462. ctx))
  463. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  464. || !TEST_true(BN_hex2bn(&z, "1FF"
  465. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  466. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA"
  467. "51868783BF2F966B7FCC0148F709A5D0"
  468. "3BB5C9B8899C47AEBB6FB71E91386409"))
  469. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  470. || !TEST_true(EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)))
  471. goto err;
  472. TEST_info("NIST curve P-521 -- Generator");
  473. test_output_bignum("x", x);
  474. test_output_bignum("y", y);
  475. /* G_y value taken from the standard: */
  476. if (!TEST_true(BN_hex2bn(&z, "118"
  477. "39296A789A3BC0045C8A5FB42C7D1BD9"
  478. "98F54449579B446817AFBD17273E662C"
  479. "97EE72995EF42640C550B9013FAD0761"
  480. "353C7086A272C24088BE94769FD16650"))
  481. || !TEST_BN_eq(y, z)
  482. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  483. /*
  484. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  485. * and therefore setting the coordinates should fail.
  486. */
  487. || !TEST_false(EC_POINT_set_affine_coordinates_GFp(group, P, x,
  488. yplusone, ctx))
  489. || !TEST_int_eq(EC_GROUP_get_degree(group), 521)
  490. || !group_order_tests(group)
  491. || !TEST_ptr(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))
  492. || !TEST_true(EC_GROUP_copy(P_521, group))
  493. /* more tests using the last curve */
  494. /* Restore the point that got mangled in the (x, y + 1) test. */
  495. || !TEST_true(EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx))
  496. || !TEST_true(EC_POINT_copy(Q, P))
  497. || !TEST_false(EC_POINT_is_at_infinity(group, Q))
  498. || !TEST_true(EC_POINT_dbl(group, P, P, ctx))
  499. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  500. || !TEST_true(EC_POINT_invert(group, Q, ctx)) /* P = -2Q */
  501. || !TEST_true(EC_POINT_add(group, R, P, Q, ctx))
  502. || !TEST_true(EC_POINT_add(group, R, R, Q, ctx))
  503. || !TEST_true(EC_POINT_is_at_infinity(group, R)) /* R = P + 2Q */
  504. || !TEST_false(EC_POINT_is_at_infinity(group, Q)))
  505. goto err;
  506. points[0] = Q;
  507. points[1] = Q;
  508. points[2] = Q;
  509. points[3] = Q;
  510. if (!TEST_true(EC_GROUP_get_order(group, z, ctx))
  511. || !TEST_true(BN_add(y, z, BN_value_one()))
  512. || !TEST_BN_even(y)
  513. || !TEST_true(BN_rshift1(y, y)))
  514. goto err;
  515. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  516. scalars[1] = y;
  517. TEST_note("combined multiplication ...");
  518. /* z is still the group order */
  519. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  520. || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
  521. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
  522. || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx))
  523. || !TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))
  524. || !TEST_true(BN_add(z, z, y)))
  525. goto err;
  526. BN_set_negative(z, 1);
  527. scalars[0] = y;
  528. scalars[1] = z; /* z = -(order + y) */
  529. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  530. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  531. || !TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))
  532. || !TEST_true(BN_add(z, x, y)))
  533. goto err;
  534. BN_set_negative(z, 1);
  535. scalars[0] = x;
  536. scalars[1] = y;
  537. scalars[2] = z; /* z = -(x+y) */
  538. if (!TEST_ptr(scalar3 = BN_new()))
  539. goto err;
  540. BN_zero(scalar3);
  541. scalars[3] = scalar3;
  542. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx))
  543. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  544. goto err;
  545. TEST_note(" ok\n");
  546. r = 1;
  547. err:
  548. BN_CTX_free(ctx);
  549. BN_free(p);
  550. BN_free(a);
  551. BN_free(b);
  552. EC_GROUP_free(group);
  553. EC_GROUP_free(tmp);
  554. EC_POINT_free(P);
  555. EC_POINT_free(Q);
  556. EC_POINT_free(R);
  557. BN_free(x);
  558. BN_free(y);
  559. BN_free(z);
  560. BN_free(yplusone);
  561. BN_free(scalar3);
  562. EC_GROUP_free(P_160);
  563. EC_GROUP_free(P_192);
  564. EC_GROUP_free(P_224);
  565. EC_GROUP_free(P_256);
  566. EC_GROUP_free(P_384);
  567. EC_GROUP_free(P_521);
  568. return r;
  569. }
  570. # ifndef OPENSSL_NO_EC2M
  571. static struct c2_curve_test {
  572. const char *name;
  573. const char *p;
  574. const char *a;
  575. const char *b;
  576. const char *x;
  577. const char *y;
  578. int ybit;
  579. const char *order;
  580. const char *cof;
  581. int degree;
  582. } char2_curve_tests[] = {
  583. /* Curve K-163 (FIPS PUB 186-2, App. 6) */
  584. {
  585. "NIST curve K-163",
  586. "0800000000000000000000000000000000000000C9",
  587. "1",
  588. "1",
  589. "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
  590. "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
  591. 1, "04000000000000000000020108A2E0CC0D99F8A5EF", "2", 163
  592. },
  593. /* Curve B-163 (FIPS PUB 186-2, App. 6) */
  594. {
  595. "NIST curve B-163",
  596. "0800000000000000000000000000000000000000C9",
  597. "1",
  598. "020A601907B8C953CA1481EB10512F78744A3205FD",
  599. "03F0EBA16286A2D57EA0991168D4994637E8343E36",
  600. "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
  601. 1, "040000000000000000000292FE77E70C12A4234C33", "2", 163
  602. },
  603. /* Curve K-233 (FIPS PUB 186-2, App. 6) */
  604. {
  605. "NIST curve K-233",
  606. "020000000000000000000000000000000000000004000000000000000001",
  607. "0",
  608. "1",
  609. "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
  610. "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
  611. 0,
  612. "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
  613. "4", 233
  614. },
  615. /* Curve B-233 (FIPS PUB 186-2, App. 6) */
  616. {
  617. "NIST curve B-233",
  618. "020000000000000000000000000000000000000004000000000000000001",
  619. "000000000000000000000000000000000000000000000000000000000001",
  620. "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
  621. "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
  622. "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
  623. 1,
  624. "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
  625. "2", 233
  626. },
  627. /* Curve K-283 (FIPS PUB 186-2, App. 6) */
  628. {
  629. "NIST curve K-283",
  630. "08000000"
  631. "00000000000000000000000000000000000000000000000000000000000010A1",
  632. "0",
  633. "1",
  634. "0503213F"
  635. "78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
  636. "01CCDA38"
  637. "0F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
  638. 0,
  639. "01FFFFFF"
  640. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
  641. "4", 283
  642. },
  643. /* Curve B-283 (FIPS PUB 186-2, App. 6) */
  644. {
  645. "NIST curve B-283",
  646. "08000000"
  647. "00000000000000000000000000000000000000000000000000000000000010A1",
  648. "00000000"
  649. "0000000000000000000000000000000000000000000000000000000000000001",
  650. "027B680A"
  651. "C8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
  652. "05F93925"
  653. "8DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
  654. "03676854"
  655. "FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
  656. 1,
  657. "03FFFFFF"
  658. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
  659. "2", 283
  660. },
  661. /* Curve K-409 (FIPS PUB 186-2, App. 6) */
  662. {
  663. "NIST curve K-409",
  664. "0200000000000000000000000000000000000000"
  665. "0000000000000000000000000000000000000000008000000000000000000001",
  666. "0",
  667. "1",
  668. "0060F05F658F49C1AD3AB1890F7184210EFD0987"
  669. "E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
  670. "01E369050B7C4E42ACBA1DACBF04299C3460782F"
  671. "918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
  672. 1,
  673. "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  674. "FFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
  675. "4", 409
  676. },
  677. /* Curve B-409 (FIPS PUB 186-2, App. 6) */
  678. {
  679. "NIST curve B-409",
  680. "0200000000000000000000000000000000000000"
  681. "0000000000000000000000000000000000000000008000000000000000000001",
  682. "0000000000000000000000000000000000000000"
  683. "0000000000000000000000000000000000000000000000000000000000000001",
  684. "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422E"
  685. "F1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
  686. "015D4860D088DDB3496B0C6064756260441CDE4A"
  687. "F1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
  688. "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5"
  689. "A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
  690. 1,
  691. "0100000000000000000000000000000000000000"
  692. "00000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
  693. "2", 409
  694. },
  695. /* Curve K-571 (FIPS PUB 186-2, App. 6) */
  696. {
  697. "NIST curve K-571",
  698. "800000000000000"
  699. "0000000000000000000000000000000000000000000000000000000000000000"
  700. "0000000000000000000000000000000000000000000000000000000000000425",
  701. "0",
  702. "1",
  703. "026EB7A859923FBC"
  704. "82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E6"
  705. "47DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
  706. "0349DC807F4FBF37"
  707. "4F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA7"
  708. "4FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
  709. 0,
  710. "0200000000000000"
  711. "00000000000000000000000000000000000000000000000000000000131850E1"
  712. "F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
  713. "4", 571
  714. },
  715. /* Curve B-571 (FIPS PUB 186-2, App. 6) */
  716. {
  717. "NIST curve B-571",
  718. "800000000000000"
  719. "0000000000000000000000000000000000000000000000000000000000000000"
  720. "0000000000000000000000000000000000000000000000000000000000000425",
  721. "0000000000000000"
  722. "0000000000000000000000000000000000000000000000000000000000000000"
  723. "0000000000000000000000000000000000000000000000000000000000000001",
  724. "02F40E7E2221F295"
  725. "DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA5933"
  726. "2BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
  727. "0303001D34B85629"
  728. "6C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293"
  729. "CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
  730. "037BF27342DA639B"
  731. "6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A57"
  732. "6291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
  733. 1,
  734. "03FFFFFFFFFFFFFF"
  735. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18"
  736. "FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
  737. "2", 571
  738. }
  739. };
  740. static int char2_curve_test(int n)
  741. {
  742. int r = 0;
  743. BN_CTX *ctx = NULL;
  744. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  745. BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;
  746. EC_GROUP *group = NULL, *variable = NULL;
  747. EC_POINT *P = NULL, *Q = NULL, *R = NULL;
  748. const EC_POINT *points[3];
  749. const BIGNUM *scalars[3];
  750. struct c2_curve_test *const test = char2_curve_tests + n;
  751. if (!TEST_ptr(ctx = BN_CTX_new())
  752. || !TEST_ptr(p = BN_new())
  753. || !TEST_ptr(a = BN_new())
  754. || !TEST_ptr(b = BN_new())
  755. || !TEST_ptr(x = BN_new())
  756. || !TEST_ptr(y = BN_new())
  757. || !TEST_ptr(z = BN_new())
  758. || !TEST_ptr(yplusone = BN_new())
  759. || !TEST_true(BN_hex2bn(&p, test->p))
  760. || !TEST_true(BN_hex2bn(&a, test->a))
  761. || !TEST_true(BN_hex2bn(&b, test->b))
  762. || !TEST_true(group = EC_GROUP_new(EC_GF2m_simple_method()))
  763. || !TEST_true(EC_GROUP_set_curve_GF2m(group, p, a, b, ctx))
  764. || !TEST_ptr(P = EC_POINT_new(group))
  765. || !TEST_ptr(Q = EC_POINT_new(group))
  766. || !TEST_ptr(R = EC_POINT_new(group))
  767. || !TEST_true(BN_hex2bn(&x, test->x))
  768. || !TEST_true(BN_hex2bn(&y, test->y))
  769. || !TEST_true(BN_add(yplusone, y, BN_value_one())))
  770. goto err;
  771. /* Change test based on whether binary point compression is enabled or not. */
  772. # ifdef OPENSSL_EC_BIN_PT_COMP
  773. /*
  774. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  775. * and therefore setting the coordinates should fail.
  776. */
  777. if (!TEST_false(EC_POINT_set_affine_coordinates_GF2m(group, P, x, yplusone,
  778. ctx))
  779. || !TEST_true(EC_POINT_set_compressed_coordinates_GF2m(group, P, x,
  780. test->y_bit,
  781. ctx))
  782. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  783. || !TEST_true(BN_hex2bn(&z, test->order))
  784. || !TEST_true(BN_hex2bn(&cof, test->cof))
  785. || !TEST_true(EC_GROUP_set_generator(group, P, z, cof))
  786. || !TEST_true(EC_POINT_get_affine_coordinates_GF2m(group, P, x, y,
  787. ctx)))
  788. goto err;
  789. TEST_info("%s -- Generator", test->name);
  790. test_output_bignum("x", x);
  791. test_output_bignum("y", y);
  792. /* G_y value taken from the standard: */
  793. if (!TEST_true(BN_hex2bn(&z, test->y))
  794. || !TEST_BN_eq(y, z))
  795. goto err;
  796. # else
  797. /*
  798. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  799. * and therefore setting the coordinates should fail.
  800. */
  801. if (!TEST_false(EC_POINT_set_affine_coordinates_GF2m(group, P, x, yplusone,
  802. ctx))
  803. || !TEST_true(EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx))
  804. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  805. || !TEST_true(BN_hex2bn(&z, test->order))
  806. || !TEST_true(BN_hex2bn(&cof, test->cof))
  807. || !TEST_true(EC_GROUP_set_generator(group, P, z, cof)))
  808. goto err;
  809. TEST_info("%s -- Generator:", test->name);
  810. test_output_bignum("x", x);
  811. test_output_bignum("y", y);
  812. # endif
  813. if (!TEST_int_eq(EC_GROUP_get_degree(group), test->degree)
  814. || !group_order_tests(group)
  815. || !TEST_ptr(variable = EC_GROUP_new(EC_GROUP_method_of(group)))
  816. || !TEST_true(EC_GROUP_copy(variable, group)))
  817. goto err;
  818. /* more tests using the last curve */
  819. if (n == OSSL_NELEM(char2_curve_tests) - 1) {
  820. if (!TEST_true(EC_POINT_set_affine_coordinates_GF2m(group, P, x, y,
  821. ctx))
  822. || !TEST_true(EC_POINT_copy(Q, P))
  823. || !TEST_false(EC_POINT_is_at_infinity(group, Q))
  824. || !TEST_true(EC_POINT_dbl(group, P, P, ctx))
  825. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  826. || !TEST_true(EC_POINT_invert(group, Q, ctx)) /* P = -2Q */
  827. || !TEST_true(EC_POINT_add(group, R, P, Q, ctx))
  828. || !TEST_true(EC_POINT_add(group, R, R, Q, ctx))
  829. || !TEST_true(EC_POINT_is_at_infinity(group, R)) /* R = P + 2Q */
  830. || !TEST_false(EC_POINT_is_at_infinity(group, Q)))
  831. goto err;
  832. points[0] = Q;
  833. points[1] = Q;
  834. points[2] = Q;
  835. if (!TEST_true(BN_add(y, z, BN_value_one()))
  836. || !TEST_BN_even(y)
  837. || !TEST_true(BN_rshift1(y, y)))
  838. goto err;
  839. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  840. scalars[1] = y;
  841. TEST_note("combined multiplication ...");
  842. /* z is still the group order */
  843. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  844. || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
  845. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
  846. || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx)))
  847. goto err;
  848. if (!TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))
  849. || !TEST_true(BN_add(z, z, y)))
  850. goto err;
  851. BN_set_negative(z, 1);
  852. scalars[0] = y;
  853. scalars[1] = z; /* z = -(order + y) */
  854. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  855. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  856. goto err;
  857. if (!TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))
  858. || !TEST_true(BN_add(z, x, y)))
  859. goto err;
  860. BN_set_negative(z, 1);
  861. scalars[0] = x;
  862. scalars[1] = y;
  863. scalars[2] = z; /* z = -(x+y) */
  864. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx))
  865. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  866. goto err;;
  867. }
  868. r = 1;
  869. err:
  870. BN_CTX_free(ctx);
  871. BN_free(p);
  872. BN_free(a);
  873. BN_free(b);
  874. BN_free(x);
  875. BN_free(y);
  876. BN_free(z);
  877. BN_free(yplusone);
  878. BN_free(cof);
  879. EC_POINT_free(P);
  880. EC_POINT_free(Q);
  881. EC_POINT_free(R);
  882. EC_GROUP_free(group);
  883. EC_GROUP_free(variable);
  884. return r;
  885. }
  886. static int char2_field_tests(void)
  887. {
  888. BN_CTX *ctx = NULL;
  889. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  890. EC_GROUP *group = NULL, *tmp = NULL;
  891. EC_POINT *P = NULL, *Q = NULL, *R = NULL;
  892. BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;
  893. unsigned char buf[100];
  894. size_t len;
  895. int k, r = 0;
  896. if (!TEST_ptr(ctx = BN_CTX_new())
  897. || !TEST_ptr(p = BN_new())
  898. || !TEST_ptr(a = BN_new())
  899. || !TEST_ptr(b = BN_new())
  900. || !TEST_true(BN_hex2bn(&p, "13"))
  901. || !TEST_true(BN_hex2bn(&a, "3"))
  902. || !TEST_true(BN_hex2bn(&b, "1")))
  903. goto err;
  904. group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use
  905. * EC_GROUP_new_curve_GF2m
  906. * so that the library gets
  907. * to choose the EC_METHOD */
  908. if (!TEST_ptr(group)
  909. || !TEST_true(EC_GROUP_set_curve_GF2m(group, p, a, b, ctx))
  910. || !TEST_ptr(tmp = EC_GROUP_new(EC_GROUP_method_of(group)))
  911. || !TEST_true(EC_GROUP_copy(tmp, group)))
  912. goto err;
  913. EC_GROUP_free(group);
  914. group = tmp;
  915. tmp = NULL;
  916. if (!TEST_true(EC_GROUP_get_curve_GF2m(group, p, a, b, ctx)))
  917. goto err;
  918. TEST_info("Curve defined by Weierstrass equation");
  919. TEST_note(" y^2 + x*y = x^3 + a*x^2 + b (mod p)");
  920. test_output_bignum("a", a);
  921. test_output_bignum("b", b);
  922. test_output_bignum("p", p);
  923. if (!TEST_ptr(P = EC_POINT_new(group))
  924. || !TEST_ptr(Q = EC_POINT_new(group))
  925. || !TEST_ptr(R = EC_POINT_new(group))
  926. || !TEST_true(EC_POINT_set_to_infinity(group, P))
  927. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  928. goto err;
  929. buf[0] = 0;
  930. if (!TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx))
  931. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))
  932. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  933. || !TEST_ptr(x = BN_new())
  934. || !TEST_ptr(y = BN_new())
  935. || !TEST_ptr(z = BN_new())
  936. || !TEST_ptr(cof = BN_new())
  937. || !TEST_ptr(yplusone = BN_new())
  938. || !TEST_true(BN_hex2bn(&x, "6"))
  939. /* Change test based on whether binary point compression is enabled or not. */
  940. # ifdef OPENSSL_EC_BIN_PT_COMP
  941. || !TEST_true(EC_POINT_set_compressed_coordinates_GF2m(group, Q, x, 1,
  942. ctx))
  943. # else
  944. || !TEST_true(BN_hex2bn(&y, "8"))
  945. || !TEST_true(EC_POINT_set_affine_coordinates_GF2m(group, Q, x, y, ctx))
  946. # endif
  947. )
  948. goto err;
  949. if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) {
  950. /* Change test based on whether binary point compression is enabled or not. */
  951. # ifdef OPENSSL_EC_BIN_PT_COMP
  952. if (!TEST_true(EC_POINT_get_affine_coordinates_GF2m(group, Q, x, y,
  953. ctx)))
  954. goto err;
  955. # endif
  956. TEST_info("Point is not on curve");
  957. test_output_bignum("x", x);
  958. test_output_bignum("y", y);
  959. goto err;
  960. }
  961. TEST_note("A cyclic subgroup:");
  962. k = 100;
  963. do {
  964. if (!TEST_int_ne(k--, 0))
  965. goto err;
  966. if (EC_POINT_is_at_infinity(group, P))
  967. TEST_note(" point at infinity");
  968. else {
  969. if (!TEST_true(EC_POINT_get_affine_coordinates_GF2m(group, P, x, y,
  970. ctx)))
  971. goto err;
  972. test_output_bignum("x", x);
  973. test_output_bignum("y", y);
  974. }
  975. if (!TEST_true(EC_POINT_copy(R, P))
  976. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)))
  977. goto err;
  978. }
  979. while (!EC_POINT_is_at_infinity(group, P));
  980. if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx))
  981. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  982. goto err;
  983. /* Change test based on whether binary point compression is enabled or not. */
  984. # ifdef OPENSSL_EC_BIN_PT_COMP
  985. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED,
  986. buf, sizeof buf, ctx);
  987. if (!TEST_size_t_ne(len, 0)
  988. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  989. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  990. goto err;
  991. test_output_memory("Generator as octet string, compressed form:",
  992. buf, len);
  993. # endif
  994. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
  995. buf, sizeof buf, ctx);
  996. if (!TEST_size_t_ne(len, 0)
  997. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  998. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  999. goto err;
  1000. test_output_memory("Generator as octet string, uncompressed form:",
  1001. buf, len);
  1002. /* Change test based on whether binary point compression is enabled or not. */
  1003. # ifdef OPENSSL_EC_BIN_PT_COMP
  1004. len =
  1005. EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf,
  1006. ctx);
  1007. if (!TEST_size_t_ne(len, 0)
  1008. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  1009. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  1010. goto err;
  1011. test_output_memory("Generator as octet string, hybrid form:",
  1012. buf, len);
  1013. # endif
  1014. if (!TEST_true(EC_POINT_invert(group, P, ctx))
  1015. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx)))
  1016. goto err;
  1017. TEST_note("\n");
  1018. r = 1;
  1019. err:
  1020. BN_CTX_free(ctx);
  1021. BN_free(p);
  1022. BN_free(a);
  1023. BN_free(b);
  1024. EC_GROUP_free(group);
  1025. EC_GROUP_free(tmp);
  1026. EC_POINT_free(P);
  1027. EC_POINT_free(Q);
  1028. EC_POINT_free(R);
  1029. BN_free(x);
  1030. BN_free(y);
  1031. BN_free(z);
  1032. BN_free(cof);
  1033. BN_free(yplusone);
  1034. return r;
  1035. }
  1036. # endif
  1037. static int internal_curve_test(int n)
  1038. {
  1039. EC_GROUP *group = NULL;
  1040. int nid = curves[n].nid;
  1041. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) {
  1042. TEST_info("EC_GROUP_new_curve_name() failed with curve %s\n",
  1043. OBJ_nid2sn(nid));
  1044. return 0;
  1045. }
  1046. if (!TEST_true(EC_GROUP_check(group, NULL))) {
  1047. TEST_info("EC_GROUP_check() failed with curve %s\n", OBJ_nid2sn(nid));
  1048. EC_GROUP_free(group);
  1049. return 0;
  1050. }
  1051. EC_GROUP_free(group);
  1052. return 1;
  1053. }
  1054. static int internal_curve_test_method(int n)
  1055. {
  1056. int r, nid = curves[n].nid;
  1057. EC_GROUP *group;
  1058. /*
  1059. * Skip for X25519 because low level operations such as EC_POINT_mul()
  1060. * are not supported for this curve
  1061. */
  1062. if (nid == NID_X25519)
  1063. return 1;
  1064. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) {
  1065. TEST_info("Curve %s failed\n", OBJ_nid2sn(nid));
  1066. return 0;
  1067. }
  1068. r = group_order_tests(group);
  1069. EC_GROUP_free(group);
  1070. return r;
  1071. }
  1072. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1073. /*
  1074. * nistp_test_params contains magic numbers for testing our optimized
  1075. * implementations of several NIST curves with characteristic > 3.
  1076. */
  1077. struct nistp_test_params {
  1078. const EC_METHOD *(*meth) ();
  1079. int degree;
  1080. /*
  1081. * Qx, Qy and D are taken from
  1082. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
  1083. * Otherwise, values are standard curve parameters from FIPS 180-3
  1084. */
  1085. const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d;
  1086. };
  1087. static const struct nistp_test_params nistp_tests_params[] = {
  1088. {
  1089. /* P-224 */
  1090. EC_GFp_nistp224_method,
  1091. 224,
  1092. /* p */
  1093. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
  1094. /* a */
  1095. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
  1096. /* b */
  1097. "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
  1098. /* Qx */
  1099. "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E",
  1100. /* Qy */
  1101. "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555",
  1102. /* Gx */
  1103. "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
  1104. /* Gy */
  1105. "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
  1106. /* order */
  1107. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
  1108. /* d */
  1109. "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8",
  1110. },
  1111. {
  1112. /* P-256 */
  1113. EC_GFp_nistp256_method,
  1114. 256,
  1115. /* p */
  1116. "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
  1117. /* a */
  1118. "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
  1119. /* b */
  1120. "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
  1121. /* Qx */
  1122. "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19",
  1123. /* Qy */
  1124. "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09",
  1125. /* Gx */
  1126. "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
  1127. /* Gy */
  1128. "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
  1129. /* order */
  1130. "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
  1131. /* d */
  1132. "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96",
  1133. },
  1134. {
  1135. /* P-521 */
  1136. EC_GFp_nistp521_method,
  1137. 521,
  1138. /* p */
  1139. "1ff"
  1140. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  1141. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  1142. /* a */
  1143. "1ff"
  1144. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  1145. "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc",
  1146. /* b */
  1147. "051"
  1148. "953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e1"
  1149. "56193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
  1150. /* Qx */
  1151. "0098"
  1152. "e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e"
  1153. "59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4",
  1154. /* Qy */
  1155. "0164"
  1156. "350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8"
  1157. "554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e",
  1158. /* Gx */
  1159. "c6"
  1160. "858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dba"
  1161. "a14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
  1162. /* Gy */
  1163. "118"
  1164. "39296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c"
  1165. "97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
  1166. /* order */
  1167. "1ff"
  1168. "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa"
  1169. "51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
  1170. /* d */
  1171. "0100"
  1172. "085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eee"
  1173. "df09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722",
  1174. },
  1175. };
  1176. static int nistp_single_test(int idx)
  1177. {
  1178. const struct nistp_test_params *test = nistp_tests_params + idx;
  1179. BN_CTX *ctx = NULL;
  1180. BIGNUM *p = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL;
  1181. BIGNUM *n = NULL, *m = NULL, *order = NULL, *yplusone = NULL;
  1182. EC_GROUP *NISTP = NULL;
  1183. EC_POINT *G = NULL, *P = NULL, *Q = NULL, *Q_CHECK = NULL;
  1184. int r = 0;
  1185. TEST_note("NIST curve P-%d (optimised implementation):",
  1186. test->degree);
  1187. if (!TEST_ptr(ctx = BN_CTX_new())
  1188. || !TEST_ptr(p = BN_new())
  1189. || !TEST_ptr(a = BN_new())
  1190. || !TEST_ptr(b = BN_new())
  1191. || !TEST_ptr(x = BN_new())
  1192. || !TEST_ptr(y = BN_new())
  1193. || !TEST_ptr(m = BN_new())
  1194. || !TEST_ptr(n = BN_new())
  1195. || !TEST_ptr(order = BN_new())
  1196. || !TEST_ptr(yplusone = BN_new())
  1197. || !TEST_ptr(NISTP = EC_GROUP_new(test->meth()))
  1198. || !TEST_true(BN_hex2bn(&p, test->p))
  1199. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  1200. || !TEST_true(BN_hex2bn(&a, test->a))
  1201. || !TEST_true(BN_hex2bn(&b, test->b))
  1202. || !TEST_true(EC_GROUP_set_curve_GFp(NISTP, p, a, b, ctx))
  1203. || !TEST_ptr(G = EC_POINT_new(NISTP))
  1204. || !TEST_ptr(P = EC_POINT_new(NISTP))
  1205. || !TEST_ptr(Q = EC_POINT_new(NISTP))
  1206. || !TEST_ptr(Q_CHECK = EC_POINT_new(NISTP))
  1207. || !TEST_true(BN_hex2bn(&x, test->Qx))
  1208. || !TEST_true(BN_hex2bn(&y, test->Qy))
  1209. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  1210. /*
  1211. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  1212. * and therefore setting the coordinates should fail.
  1213. */
  1214. || !TEST_false(EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x,
  1215. yplusone, ctx))
  1216. || !TEST_true(EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x, y,
  1217. ctx))
  1218. || !TEST_true(BN_hex2bn(&x, test->Gx))
  1219. || !TEST_true(BN_hex2bn(&y, test->Gy))
  1220. || !TEST_true(EC_POINT_set_affine_coordinates_GFp(NISTP, G, x, y, ctx))
  1221. || !TEST_true(BN_hex2bn(&order, test->order))
  1222. || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))
  1223. || !TEST_int_eq(EC_GROUP_get_degree(NISTP), test->degree))
  1224. goto err;
  1225. TEST_note("NIST test vectors ... ");
  1226. if (!TEST_true(BN_hex2bn(&n, test->d)))
  1227. goto err;
  1228. /* fixed point multiplication */
  1229. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1230. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1231. goto err;
  1232. /* random point multiplication */
  1233. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1234. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1235. /* set generator to P = 2*G, where G is the standard generator */
  1236. || !TEST_true(EC_POINT_dbl(NISTP, P, G, ctx))
  1237. || !TEST_true(EC_GROUP_set_generator(NISTP, P, order, BN_value_one()))
  1238. /* set the scalar to m=n/2, where n is the NIST test scalar */
  1239. || !TEST_true(BN_rshift(m, n, 1)))
  1240. goto err;
  1241. /* test the non-standard generator */
  1242. /* fixed point multiplication */
  1243. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1244. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1245. goto err;
  1246. /* random point multiplication */
  1247. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1248. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1249. /*
  1250. * We have not performed precomputation so have_precompute mult should be
  1251. * false
  1252. */
  1253. || !TEST_false(EC_GROUP_have_precompute_mult(NISTP))
  1254. /* now repeat all tests with precomputation */
  1255. || !TEST_true(EC_GROUP_precompute_mult(NISTP, ctx))
  1256. || !TEST_true(EC_GROUP_have_precompute_mult(NISTP)))
  1257. goto err;
  1258. /* fixed point multiplication */
  1259. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1260. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1261. goto err;
  1262. /* random point multiplication */
  1263. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1264. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1265. /* reset generator */
  1266. || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one())))
  1267. goto err;
  1268. /* fixed point multiplication */
  1269. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1270. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1271. goto err;
  1272. /* random point multiplication */
  1273. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1274. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1275. goto err;
  1276. r = group_order_tests(NISTP);
  1277. err:
  1278. EC_GROUP_free(NISTP);
  1279. EC_POINT_free(G);
  1280. EC_POINT_free(P);
  1281. EC_POINT_free(Q);
  1282. EC_POINT_free(Q_CHECK);
  1283. BN_free(n);
  1284. BN_free(m);
  1285. BN_free(p);
  1286. BN_free(a);
  1287. BN_free(b);
  1288. BN_free(x);
  1289. BN_free(y);
  1290. BN_free(order);
  1291. BN_free(yplusone);
  1292. BN_CTX_free(ctx);
  1293. return r;
  1294. }
  1295. # endif
  1296. static int parameter_test(void)
  1297. {
  1298. EC_GROUP *group = NULL, *group2 = NULL;
  1299. ECPARAMETERS *ecparameters = NULL;
  1300. int r;
  1301. r = TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp112r1))
  1302. && TEST_ptr(ecparameters = EC_GROUP_get_ecparameters(group, NULL))
  1303. && TEST_ptr(group2 = EC_GROUP_new_from_ecparameters(ecparameters))
  1304. && TEST_int_eq(EC_GROUP_cmp(group, group2, NULL), 0);
  1305. EC_GROUP_free(group);
  1306. EC_GROUP_free(group2);
  1307. ECPARAMETERS_free(ecparameters);
  1308. return r;
  1309. }
  1310. static const char rnd_seed[] =
  1311. "string to make the random number generator think it has randomness";
  1312. #endif
  1313. int test_main(int argc, char *argv[])
  1314. {
  1315. int result = EXIT_SUCCESS;
  1316. #ifndef OPENSSL_NO_EC
  1317. crv_len = EC_get_builtin_curves(NULL, 0);
  1318. if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
  1319. || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
  1320. return EXIT_FAILURE;
  1321. RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
  1322. ADD_TEST(parameter_test);
  1323. ADD_TEST(prime_field_tests);
  1324. # ifndef OPENSSL_NO_EC2M
  1325. ADD_TEST(char2_field_tests);
  1326. ADD_ALL_TESTS(char2_curve_test, OSSL_NELEM(char2_curve_tests));
  1327. # endif
  1328. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1329. ADD_ALL_TESTS(nistp_single_test, OSSL_NELEM(nistp_tests_params));
  1330. # endif
  1331. ADD_ALL_TESTS(internal_curve_test, crv_len);
  1332. ADD_ALL_TESTS(internal_curve_test_method, crv_len);
  1333. result = run_tests(argv[0]);
  1334. OPENSSL_free(curves);
  1335. #endif
  1336. return result;
  1337. }