ectest.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  1. /*
  2. * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the Apache License 2.0 (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 "internal/nelem.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. static size_t crv_len = 0;
  24. static EC_builtin_curve *curves = NULL;
  25. /* test multiplication with group order, long and negative scalars */
  26. static int group_order_tests(EC_GROUP *group)
  27. {
  28. BIGNUM *n1 = NULL, *n2 = NULL, *order = NULL;
  29. EC_POINT *P = NULL, *Q = NULL, *R = NULL, *S = NULL;
  30. const EC_POINT *G = NULL;
  31. BN_CTX *ctx = NULL;
  32. int i = 0, r = 0;
  33. if (!TEST_ptr(n1 = BN_new())
  34. || !TEST_ptr(n2 = BN_new())
  35. || !TEST_ptr(order = BN_new())
  36. || !TEST_ptr(ctx = BN_CTX_new())
  37. || !TEST_ptr(G = EC_GROUP_get0_generator(group))
  38. || !TEST_ptr(P = EC_POINT_new(group))
  39. || !TEST_ptr(Q = EC_POINT_new(group))
  40. || !TEST_ptr(R = EC_POINT_new(group))
  41. || !TEST_ptr(S = EC_POINT_new(group)))
  42. goto err;
  43. if (!TEST_true(EC_GROUP_get_order(group, order, ctx))
  44. || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
  45. || !TEST_true(EC_POINT_is_at_infinity(group, Q))
  46. || !TEST_true(EC_GROUP_precompute_mult(group, ctx))
  47. || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
  48. || !TEST_true(EC_POINT_is_at_infinity(group, Q))
  49. || !TEST_true(EC_POINT_copy(P, G))
  50. || !TEST_true(BN_one(n1))
  51. || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))
  52. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))
  53. || !TEST_true(BN_sub(n1, order, n1))
  54. || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))
  55. || !TEST_true(EC_POINT_invert(group, Q, ctx))
  56. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))
  57. goto err;
  58. for (i = 1; i <= 2; i++) {
  59. const BIGNUM *scalars[6];
  60. const EC_POINT *points[6];
  61. if (!TEST_true(BN_set_word(n1, i))
  62. /*
  63. * If i == 1, P will be the predefined generator for which
  64. * EC_GROUP_precompute_mult has set up precomputation.
  65. */
  66. || !TEST_true(EC_POINT_mul(group, P, n1, NULL, NULL, ctx))
  67. || (i == 1 && !TEST_int_eq(0, EC_POINT_cmp(group, P, G, ctx)))
  68. || !TEST_true(BN_one(n1))
  69. /* n1 = 1 - order */
  70. || !TEST_true(BN_sub(n1, n1, order))
  71. || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n1, ctx))
  72. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))
  73. /* n2 = 1 + order */
  74. || !TEST_true(BN_add(n2, order, BN_value_one()))
  75. || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  76. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))
  77. /* n2 = (1 - order) * (1 + order) = 1 - order^2 */
  78. || !TEST_true(BN_mul(n2, n1, n2, ctx))
  79. || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  80. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))
  81. goto err;
  82. /* n2 = order^2 - 1 */
  83. BN_set_negative(n2, 0);
  84. if (!TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  85. /* Add P to verify the result. */
  86. || !TEST_true(EC_POINT_add(group, Q, Q, P, ctx))
  87. || !TEST_true(EC_POINT_is_at_infinity(group, Q))
  88. /* Exercise EC_POINTs_mul, including corner cases. */
  89. || !TEST_false(EC_POINT_is_at_infinity(group, P)))
  90. goto err;
  91. scalars[0] = scalars[1] = BN_value_one();
  92. points[0] = points[1] = P;
  93. if (!TEST_true(EC_POINTs_mul(group, R, NULL, 2, points, scalars, ctx))
  94. || !TEST_true(EC_POINT_dbl(group, S, points[0], ctx))
  95. || !TEST_int_eq(0, EC_POINT_cmp(group, R, S, ctx)))
  96. goto err;
  97. scalars[0] = n1;
  98. points[0] = Q; /* => infinity */
  99. scalars[1] = n2;
  100. points[1] = P; /* => -P */
  101. scalars[2] = n1;
  102. points[2] = Q; /* => infinity */
  103. scalars[3] = n2;
  104. points[3] = Q; /* => infinity */
  105. scalars[4] = n1;
  106. points[4] = P; /* => P */
  107. scalars[5] = n2;
  108. points[5] = Q; /* => infinity */
  109. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx))
  110. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  111. goto err;
  112. }
  113. r = 1;
  114. err:
  115. if (r == 0 && i != 0)
  116. TEST_info(i == 1 ? "allowing precomputation" :
  117. "without precomputation");
  118. EC_POINT_free(P);
  119. EC_POINT_free(Q);
  120. EC_POINT_free(R);
  121. EC_POINT_free(S);
  122. BN_free(n1);
  123. BN_free(n2);
  124. BN_free(order);
  125. BN_CTX_free(ctx);
  126. return r;
  127. }
  128. static int prime_field_tests(void)
  129. {
  130. BN_CTX *ctx = NULL;
  131. BIGNUM *p = NULL, *a = NULL, *b = NULL, *scalar3 = NULL;
  132. EC_GROUP *group = NULL, *tmp = NULL;
  133. EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL,
  134. *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
  135. EC_POINT *P = NULL, *Q = NULL, *R = NULL;
  136. BIGNUM *x = NULL, *y = NULL, *z = NULL, *yplusone = NULL;
  137. const EC_POINT *points[4];
  138. const BIGNUM *scalars[4];
  139. unsigned char buf[100];
  140. size_t len, r = 0;
  141. int k;
  142. if (!TEST_ptr(ctx = BN_CTX_new())
  143. || !TEST_ptr(p = BN_new())
  144. || !TEST_ptr(a = BN_new())
  145. || !TEST_ptr(b = BN_new())
  146. || !TEST_true(BN_hex2bn(&p, "17"))
  147. || !TEST_true(BN_hex2bn(&a, "1"))
  148. || !TEST_true(BN_hex2bn(&b, "1"))
  149. /*
  150. * applications should use EC_GROUP_new_curve_GFp so
  151. * that the library gets to choose the EC_METHOD
  152. */
  153. || !TEST_ptr(group = EC_GROUP_new(EC_GFp_mont_method()))
  154. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  155. || !TEST_ptr(tmp = EC_GROUP_new(EC_GROUP_method_of(group)))
  156. || !TEST_true(EC_GROUP_copy(tmp, group)))
  157. goto err;
  158. EC_GROUP_free(group);
  159. group = tmp;
  160. tmp = NULL;
  161. if (!TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx)))
  162. goto err;
  163. TEST_info("Curve defined by Weierstrass equation");
  164. TEST_note(" y^2 = x^3 + a*x + b (mod p)");
  165. test_output_bignum("a", a);
  166. test_output_bignum("b", b);
  167. test_output_bignum("p", p);
  168. buf[0] = 0;
  169. if (!TEST_ptr(P = EC_POINT_new(group))
  170. || !TEST_ptr(Q = EC_POINT_new(group))
  171. || !TEST_ptr(R = EC_POINT_new(group))
  172. || !TEST_true(EC_POINT_set_to_infinity(group, P))
  173. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  174. || !TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx))
  175. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))
  176. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  177. || !TEST_ptr(x = BN_new())
  178. || !TEST_ptr(y = BN_new())
  179. || !TEST_ptr(z = BN_new())
  180. || !TEST_ptr(yplusone = BN_new())
  181. || !TEST_true(BN_hex2bn(&x, "D"))
  182. || !TEST_true(EC_POINT_set_compressed_coordinates(group, Q, x, 1, ctx)))
  183. goto err;
  184. if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) {
  185. if (!TEST_true(EC_POINT_get_affine_coordinates(group, Q, x, y, ctx)))
  186. goto err;
  187. TEST_info("Point is not on curve");
  188. test_output_bignum("x", x);
  189. test_output_bignum("y", y);
  190. goto err;
  191. }
  192. TEST_note("A cyclic subgroup:");
  193. k = 100;
  194. do {
  195. if (!TEST_int_ne(k--, 0))
  196. goto err;
  197. if (EC_POINT_is_at_infinity(group, P)) {
  198. TEST_note(" point at infinity");
  199. } else {
  200. if (!TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y,
  201. ctx)))
  202. goto err;
  203. test_output_bignum("x", x);
  204. test_output_bignum("y", y);
  205. }
  206. if (!TEST_true(EC_POINT_copy(R, P))
  207. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)))
  208. goto err;
  209. } while (!EC_POINT_is_at_infinity(group, P));
  210. if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx))
  211. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  212. goto err;
  213. len =
  214. EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,
  215. sizeof(buf), ctx);
  216. if (!TEST_size_t_ne(len, 0)
  217. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  218. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  219. goto err;
  220. test_output_memory("Generator as octet string, compressed form:",
  221. buf, len);
  222. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
  223. buf, sizeof(buf), ctx);
  224. if (!TEST_size_t_ne(len, 0)
  225. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  226. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  227. goto err;
  228. test_output_memory("Generator as octet string, uncompressed form:",
  229. buf, len);
  230. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID,
  231. buf, sizeof(buf), ctx);
  232. if (!TEST_size_t_ne(len, 0)
  233. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  234. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  235. goto err;
  236. test_output_memory("Generator as octet string, hybrid form:",
  237. buf, len);
  238. if (!TEST_true(EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z,
  239. ctx)))
  240. goto err;
  241. TEST_info("A representation of the inverse of that generator in");
  242. TEST_note("Jacobian projective coordinates");
  243. test_output_bignum("x", x);
  244. test_output_bignum("y", y);
  245. test_output_bignum("z", z);
  246. if (!TEST_true(EC_POINT_invert(group, P, ctx))
  247. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
  248. /*
  249. * Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2,
  250. * 2000) -- not a NIST curve, but commonly used
  251. */
  252. || !TEST_true(BN_hex2bn(&p, "FFFFFFFF"
  253. "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"))
  254. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  255. || !TEST_true(BN_hex2bn(&a, "FFFFFFFF"
  256. "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"))
  257. || !TEST_true(BN_hex2bn(&b, "1C97BEFC"
  258. "54BD7A8B65ACF89F81D4D4ADC565FA45"))
  259. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  260. || !TEST_true(BN_hex2bn(&x, "4A96B568"
  261. "8EF573284664698968C38BB913CBFC82"))
  262. || !TEST_true(BN_hex2bn(&y, "23a62855"
  263. "3168947d59dcc912042351377ac5fb32"))
  264. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  265. /*
  266. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  267. * and therefore setting the coordinates should fail.
  268. */
  269. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  270. ctx))
  271. || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
  272. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  273. || !TEST_true(BN_hex2bn(&z, "0100000000"
  274. "000000000001F4C8F927AED3CA752257"))
  275. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  276. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  277. goto err;
  278. TEST_info("SEC2 curve secp160r1 -- Generator");
  279. test_output_bignum("x", x);
  280. test_output_bignum("y", y);
  281. /* G_y value taken from the standard: */
  282. if (!TEST_true(BN_hex2bn(&z, "23a62855"
  283. "3168947d59dcc912042351377ac5fb32"))
  284. || !TEST_BN_eq(y, z)
  285. || !TEST_int_eq(EC_GROUP_get_degree(group), 160)
  286. || !group_order_tests(group)
  287. || !TEST_ptr(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))
  288. || !TEST_true(EC_GROUP_copy(P_160, group))
  289. /* Curve P-192 (FIPS PUB 186-2, App. 6) */
  290. || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFF"
  291. "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"))
  292. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  293. || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFF"
  294. "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"))
  295. || !TEST_true(BN_hex2bn(&b, "64210519E59C80E7"
  296. "0FA7E9AB72243049FEB8DEECC146B9B1"))
  297. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  298. || !TEST_true(BN_hex2bn(&x, "188DA80EB03090F6"
  299. "7CBF20EB43A18800F4FF0AFD82FF1012"))
  300. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
  301. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  302. || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFF"
  303. "FFFFFFFF99DEF836146BC9B1B4D22831"))
  304. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  305. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  306. goto err;
  307. TEST_info("NIST curve P-192 -- Generator");
  308. test_output_bignum("x", x);
  309. test_output_bignum("y", y);
  310. /* G_y value taken from the standard: */
  311. if (!TEST_true(BN_hex2bn(&z, "07192B95FFC8DA78"
  312. "631011ED6B24CDD573F977A11E794811"))
  313. || !TEST_BN_eq(y, z)
  314. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  315. /*
  316. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  317. * and therefore setting the coordinates should fail.
  318. */
  319. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  320. ctx))
  321. || !TEST_int_eq(EC_GROUP_get_degree(group), 192)
  322. || !group_order_tests(group)
  323. || !TEST_ptr(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))
  324. || !TEST_true(EC_GROUP_copy(P_192, group))
  325. /* Curve P-224 (FIPS PUB 186-2, App. 6) */
  326. || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFF"
  327. "FFFFFFFF000000000000000000000001"))
  328. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  329. || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFF"
  330. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"))
  331. || !TEST_true(BN_hex2bn(&b, "B4050A850C04B3ABF5413256"
  332. "5044B0B7D7BFD8BA270B39432355FFB4"))
  333. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  334. || !TEST_true(BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B9"
  335. "4A03C1D356C21122343280D6115C1D21"))
  336. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx))
  337. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  338. || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF"
  339. "FFFF16A2E0B8F03E13DD29455C5C2A3D"))
  340. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  341. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  342. goto err;
  343. TEST_info("NIST curve P-224 -- Generator");
  344. test_output_bignum("x", x);
  345. test_output_bignum("y", y);
  346. /* G_y value taken from the standard: */
  347. if (!TEST_true(BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6"
  348. "CD4375A05A07476444D5819985007E34"))
  349. || !TEST_BN_eq(y, z)
  350. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  351. /*
  352. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  353. * and therefore setting the coordinates should fail.
  354. */
  355. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  356. ctx))
  357. || !TEST_int_eq(EC_GROUP_get_degree(group), 224)
  358. || !group_order_tests(group)
  359. || !TEST_ptr(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))
  360. || !TEST_true(EC_GROUP_copy(P_224, group))
  361. /* Curve P-256 (FIPS PUB 186-2, App. 6) */
  362. || !TEST_true(BN_hex2bn(&p, "FFFFFFFF000000010000000000000000"
  363. "00000000FFFFFFFFFFFFFFFFFFFFFFFF"))
  364. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  365. || !TEST_true(BN_hex2bn(&a, "FFFFFFFF000000010000000000000000"
  366. "00000000FFFFFFFFFFFFFFFFFFFFFFFC"))
  367. || !TEST_true(BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC"
  368. "651D06B0CC53B0F63BCE3C3E27D2604B"))
  369. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  370. || !TEST_true(BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F2"
  371. "77037D812DEB33A0F4A13945D898C296"))
  372. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
  373. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  374. || !TEST_true(BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFF"
  375. "BCE6FAADA7179E84F3B9CAC2FC632551"))
  376. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  377. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  378. goto err;
  379. TEST_info("NIST curve P-256 -- Generator");
  380. test_output_bignum("x", x);
  381. test_output_bignum("y", y);
  382. /* G_y value taken from the standard: */
  383. if (!TEST_true(BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  384. "2BCE33576B315ECECBB6406837BF51F5"))
  385. || !TEST_BN_eq(y, z)
  386. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  387. /*
  388. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  389. * and therefore setting the coordinates should fail.
  390. */
  391. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  392. ctx))
  393. || !TEST_int_eq(EC_GROUP_get_degree(group), 256)
  394. || !group_order_tests(group)
  395. || !TEST_ptr(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))
  396. || !TEST_true(EC_GROUP_copy(P_256, group))
  397. /* Curve P-384 (FIPS PUB 186-2, App. 6) */
  398. || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  399. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
  400. "FFFFFFFF0000000000000000FFFFFFFF"))
  401. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  402. || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  403. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
  404. "FFFFFFFF0000000000000000FFFFFFFC"))
  405. || !TEST_true(BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19"
  406. "181D9C6EFE8141120314088F5013875A"
  407. "C656398D8A2ED19D2A85C8EDD3EC2AEF"))
  408. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  409. || !TEST_true(BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD74"
  410. "6E1D3B628BA79B9859F741E082542A38"
  411. "5502F25DBF55296C3A545E3872760AB7"))
  412. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
  413. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  414. || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  415. "FFFFFFFFFFFFFFFFC7634D81F4372DDF"
  416. "581A0DB248B0A77AECEC196ACCC52973"))
  417. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  418. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  419. goto err;
  420. TEST_info("NIST curve P-384 -- Generator");
  421. test_output_bignum("x", x);
  422. test_output_bignum("y", y);
  423. /* G_y value taken from the standard: */
  424. if (!TEST_true(BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29"
  425. "F8F41DBD289A147CE9DA3113B5F0B8C0"
  426. "0A60B1CE1D7E819D7A431D7C90EA0E5F"))
  427. || !TEST_BN_eq(y, z)
  428. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  429. /*
  430. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  431. * and therefore setting the coordinates should fail.
  432. */
  433. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  434. ctx))
  435. || !TEST_int_eq(EC_GROUP_get_degree(group), 384)
  436. || !group_order_tests(group)
  437. || !TEST_ptr(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))
  438. || !TEST_true(EC_GROUP_copy(P_384, group))
  439. /* Curve P-521 (FIPS PUB 186-2, App. 6) */
  440. || !TEST_true(BN_hex2bn(&p, "1FF"
  441. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  442. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  443. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  444. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
  445. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  446. || !TEST_true(BN_hex2bn(&a, "1FF"
  447. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  448. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  449. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  450. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC"))
  451. || !TEST_true(BN_hex2bn(&b, "051"
  452. "953EB9618E1C9A1F929A21A0B68540EE"
  453. "A2DA725B99B315F3B8B489918EF109E1"
  454. "56193951EC7E937B1652C0BD3BB1BF07"
  455. "3573DF883D2C34F1EF451FD46B503F00"))
  456. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  457. || !TEST_true(BN_hex2bn(&x, "C6"
  458. "858E06B70404E9CD9E3ECB662395B442"
  459. "9C648139053FB521F828AF606B4D3DBA"
  460. "A14B5E77EFE75928FE1DC127A2FFA8DE"
  461. "3348B3C1856A429BF97E7E31C2E5BD66"))
  462. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 0, 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(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(group, P, x, yplusone,
  488. 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(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(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(group, P, x, yplusone, ctx))
  778. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x,
  779. test->y_bit,
  780. ctx))
  781. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  782. || !TEST_true(BN_hex2bn(&z, test->order))
  783. || !TEST_true(BN_hex2bn(&cof, test->cof))
  784. || !TEST_true(EC_GROUP_set_generator(group, P, z, cof))
  785. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  786. goto err;
  787. TEST_info("%s -- Generator", test->name);
  788. test_output_bignum("x", x);
  789. test_output_bignum("y", y);
  790. /* G_y value taken from the standard: */
  791. if (!TEST_true(BN_hex2bn(&z, test->y))
  792. || !TEST_BN_eq(y, z))
  793. goto err;
  794. # else
  795. /*
  796. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  797. * and therefore setting the coordinates should fail.
  798. */
  799. if (!TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx))
  800. || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
  801. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  802. || !TEST_true(BN_hex2bn(&z, test->order))
  803. || !TEST_true(BN_hex2bn(&cof, test->cof))
  804. || !TEST_true(EC_GROUP_set_generator(group, P, z, cof)))
  805. goto err;
  806. TEST_info("%s -- Generator:", test->name);
  807. test_output_bignum("x", x);
  808. test_output_bignum("y", y);
  809. # endif
  810. if (!TEST_int_eq(EC_GROUP_get_degree(group), test->degree)
  811. || !group_order_tests(group)
  812. || !TEST_ptr(variable = EC_GROUP_new(EC_GROUP_method_of(group)))
  813. || !TEST_true(EC_GROUP_copy(variable, group)))
  814. goto err;
  815. /* more tests using the last curve */
  816. if (n == OSSL_NELEM(char2_curve_tests) - 1) {
  817. if (!TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
  818. || !TEST_true(EC_POINT_copy(Q, P))
  819. || !TEST_false(EC_POINT_is_at_infinity(group, Q))
  820. || !TEST_true(EC_POINT_dbl(group, P, P, ctx))
  821. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  822. || !TEST_true(EC_POINT_invert(group, Q, ctx)) /* P = -2Q */
  823. || !TEST_true(EC_POINT_add(group, R, P, Q, ctx))
  824. || !TEST_true(EC_POINT_add(group, R, R, Q, ctx))
  825. || !TEST_true(EC_POINT_is_at_infinity(group, R)) /* R = P + 2Q */
  826. || !TEST_false(EC_POINT_is_at_infinity(group, Q)))
  827. goto err;
  828. points[0] = Q;
  829. points[1] = Q;
  830. points[2] = Q;
  831. if (!TEST_true(BN_add(y, z, BN_value_one()))
  832. || !TEST_BN_even(y)
  833. || !TEST_true(BN_rshift1(y, y)))
  834. goto err;
  835. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  836. scalars[1] = y;
  837. TEST_note("combined multiplication ...");
  838. /* z is still the group order */
  839. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  840. || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
  841. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
  842. || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx)))
  843. goto err;
  844. if (!TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))
  845. || !TEST_true(BN_add(z, z, y)))
  846. goto err;
  847. BN_set_negative(z, 1);
  848. scalars[0] = y;
  849. scalars[1] = z; /* z = -(order + y) */
  850. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  851. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  852. goto err;
  853. if (!TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))
  854. || !TEST_true(BN_add(z, x, y)))
  855. goto err;
  856. BN_set_negative(z, 1);
  857. scalars[0] = x;
  858. scalars[1] = y;
  859. scalars[2] = z; /* z = -(x+y) */
  860. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx))
  861. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  862. goto err;;
  863. }
  864. r = 1;
  865. err:
  866. BN_CTX_free(ctx);
  867. BN_free(p);
  868. BN_free(a);
  869. BN_free(b);
  870. BN_free(x);
  871. BN_free(y);
  872. BN_free(z);
  873. BN_free(yplusone);
  874. BN_free(cof);
  875. EC_POINT_free(P);
  876. EC_POINT_free(Q);
  877. EC_POINT_free(R);
  878. EC_GROUP_free(group);
  879. EC_GROUP_free(variable);
  880. return r;
  881. }
  882. static int char2_field_tests(void)
  883. {
  884. BN_CTX *ctx = NULL;
  885. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  886. EC_GROUP *group = NULL, *tmp = NULL;
  887. EC_POINT *P = NULL, *Q = NULL, *R = NULL;
  888. BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;
  889. unsigned char buf[100];
  890. size_t len;
  891. int k, r = 0;
  892. if (!TEST_ptr(ctx = BN_CTX_new())
  893. || !TEST_ptr(p = BN_new())
  894. || !TEST_ptr(a = BN_new())
  895. || !TEST_ptr(b = BN_new())
  896. || !TEST_true(BN_hex2bn(&p, "13"))
  897. || !TEST_true(BN_hex2bn(&a, "3"))
  898. || !TEST_true(BN_hex2bn(&b, "1")))
  899. goto err;
  900. group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use
  901. * EC_GROUP_new_curve_GF2m
  902. * so that the library gets
  903. * to choose the EC_METHOD */
  904. if (!TEST_ptr(group)
  905. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  906. || !TEST_ptr(tmp = EC_GROUP_new(EC_GROUP_method_of(group)))
  907. || !TEST_true(EC_GROUP_copy(tmp, group)))
  908. goto err;
  909. EC_GROUP_free(group);
  910. group = tmp;
  911. tmp = NULL;
  912. if (!TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx)))
  913. goto err;
  914. TEST_info("Curve defined by Weierstrass equation");
  915. TEST_note(" y^2 + x*y = x^3 + a*x^2 + b (mod p)");
  916. test_output_bignum("a", a);
  917. test_output_bignum("b", b);
  918. test_output_bignum("p", p);
  919. if (!TEST_ptr(P = EC_POINT_new(group))
  920. || !TEST_ptr(Q = EC_POINT_new(group))
  921. || !TEST_ptr(R = EC_POINT_new(group))
  922. || !TEST_true(EC_POINT_set_to_infinity(group, P))
  923. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  924. goto err;
  925. buf[0] = 0;
  926. if (!TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx))
  927. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))
  928. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  929. || !TEST_ptr(x = BN_new())
  930. || !TEST_ptr(y = BN_new())
  931. || !TEST_ptr(z = BN_new())
  932. || !TEST_ptr(cof = BN_new())
  933. || !TEST_ptr(yplusone = BN_new())
  934. || !TEST_true(BN_hex2bn(&x, "6"))
  935. /* Change test based on whether binary point compression is enabled or not. */
  936. # ifdef OPENSSL_EC_BIN_PT_COMP
  937. || !TEST_true(EC_POINT_set_compressed_coordinates(group, Q, x, 1, ctx))
  938. # else
  939. || !TEST_true(BN_hex2bn(&y, "8"))
  940. || !TEST_true(EC_POINT_set_affine_coordinates(group, Q, x, y, ctx))
  941. # endif
  942. )
  943. goto err;
  944. if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) {
  945. /* Change test based on whether binary point compression is enabled or not. */
  946. # ifdef OPENSSL_EC_BIN_PT_COMP
  947. if (!TEST_true(EC_POINT_get_affine_coordinates(group, Q, x, y, ctx)))
  948. goto err;
  949. # endif
  950. TEST_info("Point is not on curve");
  951. test_output_bignum("x", x);
  952. test_output_bignum("y", y);
  953. goto err;
  954. }
  955. TEST_note("A cyclic subgroup:");
  956. k = 100;
  957. do {
  958. if (!TEST_int_ne(k--, 0))
  959. goto err;
  960. if (EC_POINT_is_at_infinity(group, P))
  961. TEST_note(" point at infinity");
  962. else {
  963. if (!TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y,
  964. ctx)))
  965. goto err;
  966. test_output_bignum("x", x);
  967. test_output_bignum("y", y);
  968. }
  969. if (!TEST_true(EC_POINT_copy(R, P))
  970. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)))
  971. goto err;
  972. }
  973. while (!EC_POINT_is_at_infinity(group, P));
  974. if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx))
  975. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  976. goto err;
  977. /* Change test based on whether binary point compression is enabled or not. */
  978. # ifdef OPENSSL_EC_BIN_PT_COMP
  979. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED,
  980. buf, sizeof(buf), ctx);
  981. if (!TEST_size_t_ne(len, 0)
  982. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  983. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  984. goto err;
  985. test_output_memory("Generator as octet string, compressed form:",
  986. buf, len);
  987. # endif
  988. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
  989. buf, sizeof(buf), ctx);
  990. if (!TEST_size_t_ne(len, 0)
  991. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  992. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  993. goto err;
  994. test_output_memory("Generator as octet string, uncompressed form:",
  995. buf, len);
  996. /* Change test based on whether binary point compression is enabled or not. */
  997. # ifdef OPENSSL_EC_BIN_PT_COMP
  998. len =
  999. EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof(buf),
  1000. ctx);
  1001. if (!TEST_size_t_ne(len, 0)
  1002. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  1003. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  1004. goto err;
  1005. test_output_memory("Generator as octet string, hybrid form:",
  1006. buf, len);
  1007. # endif
  1008. if (!TEST_true(EC_POINT_invert(group, P, ctx))
  1009. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx)))
  1010. goto err;
  1011. TEST_note("\n");
  1012. r = 1;
  1013. err:
  1014. BN_CTX_free(ctx);
  1015. BN_free(p);
  1016. BN_free(a);
  1017. BN_free(b);
  1018. EC_GROUP_free(group);
  1019. EC_GROUP_free(tmp);
  1020. EC_POINT_free(P);
  1021. EC_POINT_free(Q);
  1022. EC_POINT_free(R);
  1023. BN_free(x);
  1024. BN_free(y);
  1025. BN_free(z);
  1026. BN_free(cof);
  1027. BN_free(yplusone);
  1028. return r;
  1029. }
  1030. # endif
  1031. static int internal_curve_test(int n)
  1032. {
  1033. EC_GROUP *group = NULL;
  1034. int nid = curves[n].nid;
  1035. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) {
  1036. TEST_info("EC_GROUP_new_curve_name() failed with curve %s\n",
  1037. OBJ_nid2sn(nid));
  1038. return 0;
  1039. }
  1040. if (!TEST_true(EC_GROUP_check(group, NULL))) {
  1041. TEST_info("EC_GROUP_check() failed with curve %s\n", OBJ_nid2sn(nid));
  1042. EC_GROUP_free(group);
  1043. return 0;
  1044. }
  1045. EC_GROUP_free(group);
  1046. return 1;
  1047. }
  1048. static int internal_curve_test_method(int n)
  1049. {
  1050. int r, nid = curves[n].nid;
  1051. EC_GROUP *group;
  1052. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) {
  1053. TEST_info("Curve %s failed\n", OBJ_nid2sn(nid));
  1054. return 0;
  1055. }
  1056. r = group_order_tests(group);
  1057. EC_GROUP_free(group);
  1058. return r;
  1059. }
  1060. static int group_field_test(void)
  1061. {
  1062. int r = 1;
  1063. BIGNUM *secp521r1_field = NULL;
  1064. BIGNUM *sect163r2_field = NULL;
  1065. EC_GROUP *secp521r1_group = NULL;
  1066. EC_GROUP *sect163r2_group = NULL;
  1067. BN_hex2bn(&secp521r1_field,
  1068. "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  1069. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  1070. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  1071. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  1072. "FFFF");
  1073. BN_hex2bn(&sect163r2_field,
  1074. "08000000000000000000000000000000"
  1075. "00000000C9");
  1076. secp521r1_group = EC_GROUP_new_by_curve_name(NID_secp521r1);
  1077. if (BN_cmp(secp521r1_field, EC_GROUP_get0_field(secp521r1_group)))
  1078. r = 0;
  1079. # ifndef OPENSSL_NO_EC2M
  1080. sect163r2_group = EC_GROUP_new_by_curve_name(NID_sect163r2);
  1081. if (BN_cmp(sect163r2_field, EC_GROUP_get0_field(sect163r2_group)))
  1082. r = 0;
  1083. # endif
  1084. EC_GROUP_free(secp521r1_group);
  1085. EC_GROUP_free(sect163r2_group);
  1086. BN_free(secp521r1_field);
  1087. BN_free(sect163r2_field);
  1088. return r;
  1089. }
  1090. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1091. /*
  1092. * nistp_test_params contains magic numbers for testing our optimized
  1093. * implementations of several NIST curves with characteristic > 3.
  1094. */
  1095. struct nistp_test_params {
  1096. const EC_METHOD *(*meth) (void);
  1097. int degree;
  1098. /*
  1099. * Qx, Qy and D are taken from
  1100. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
  1101. * Otherwise, values are standard curve parameters from FIPS 180-3
  1102. */
  1103. const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d;
  1104. };
  1105. static const struct nistp_test_params nistp_tests_params[] = {
  1106. {
  1107. /* P-224 */
  1108. EC_GFp_nistp224_method,
  1109. 224,
  1110. /* p */
  1111. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
  1112. /* a */
  1113. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
  1114. /* b */
  1115. "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
  1116. /* Qx */
  1117. "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E",
  1118. /* Qy */
  1119. "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555",
  1120. /* Gx */
  1121. "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
  1122. /* Gy */
  1123. "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
  1124. /* order */
  1125. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
  1126. /* d */
  1127. "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8",
  1128. },
  1129. {
  1130. /* P-256 */
  1131. EC_GFp_nistp256_method,
  1132. 256,
  1133. /* p */
  1134. "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
  1135. /* a */
  1136. "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
  1137. /* b */
  1138. "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
  1139. /* Qx */
  1140. "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19",
  1141. /* Qy */
  1142. "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09",
  1143. /* Gx */
  1144. "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
  1145. /* Gy */
  1146. "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
  1147. /* order */
  1148. "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
  1149. /* d */
  1150. "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96",
  1151. },
  1152. {
  1153. /* P-521 */
  1154. EC_GFp_nistp521_method,
  1155. 521,
  1156. /* p */
  1157. "1ff"
  1158. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  1159. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  1160. /* a */
  1161. "1ff"
  1162. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  1163. "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc",
  1164. /* b */
  1165. "051"
  1166. "953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e1"
  1167. "56193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
  1168. /* Qx */
  1169. "0098"
  1170. "e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e"
  1171. "59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4",
  1172. /* Qy */
  1173. "0164"
  1174. "350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8"
  1175. "554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e",
  1176. /* Gx */
  1177. "c6"
  1178. "858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dba"
  1179. "a14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
  1180. /* Gy */
  1181. "118"
  1182. "39296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c"
  1183. "97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
  1184. /* order */
  1185. "1ff"
  1186. "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa"
  1187. "51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
  1188. /* d */
  1189. "0100"
  1190. "085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eee"
  1191. "df09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722",
  1192. },
  1193. };
  1194. static int nistp_single_test(int idx)
  1195. {
  1196. const struct nistp_test_params *test = nistp_tests_params + idx;
  1197. BN_CTX *ctx = NULL;
  1198. BIGNUM *p = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL;
  1199. BIGNUM *n = NULL, *m = NULL, *order = NULL, *yplusone = NULL;
  1200. EC_GROUP *NISTP = NULL;
  1201. EC_POINT *G = NULL, *P = NULL, *Q = NULL, *Q_CHECK = NULL;
  1202. int r = 0;
  1203. TEST_note("NIST curve P-%d (optimised implementation):",
  1204. test->degree);
  1205. if (!TEST_ptr(ctx = BN_CTX_new())
  1206. || !TEST_ptr(p = BN_new())
  1207. || !TEST_ptr(a = BN_new())
  1208. || !TEST_ptr(b = BN_new())
  1209. || !TEST_ptr(x = BN_new())
  1210. || !TEST_ptr(y = BN_new())
  1211. || !TEST_ptr(m = BN_new())
  1212. || !TEST_ptr(n = BN_new())
  1213. || !TEST_ptr(order = BN_new())
  1214. || !TEST_ptr(yplusone = BN_new())
  1215. || !TEST_ptr(NISTP = EC_GROUP_new(test->meth()))
  1216. || !TEST_true(BN_hex2bn(&p, test->p))
  1217. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  1218. || !TEST_true(BN_hex2bn(&a, test->a))
  1219. || !TEST_true(BN_hex2bn(&b, test->b))
  1220. || !TEST_true(EC_GROUP_set_curve(NISTP, p, a, b, ctx))
  1221. || !TEST_ptr(G = EC_POINT_new(NISTP))
  1222. || !TEST_ptr(P = EC_POINT_new(NISTP))
  1223. || !TEST_ptr(Q = EC_POINT_new(NISTP))
  1224. || !TEST_ptr(Q_CHECK = EC_POINT_new(NISTP))
  1225. || !TEST_true(BN_hex2bn(&x, test->Qx))
  1226. || !TEST_true(BN_hex2bn(&y, test->Qy))
  1227. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  1228. /*
  1229. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  1230. * and therefore setting the coordinates should fail.
  1231. */
  1232. || !TEST_false(EC_POINT_set_affine_coordinates(NISTP, Q_CHECK, x,
  1233. yplusone, ctx))
  1234. || !TEST_true(EC_POINT_set_affine_coordinates(NISTP, Q_CHECK, x, y,
  1235. ctx))
  1236. || !TEST_true(BN_hex2bn(&x, test->Gx))
  1237. || !TEST_true(BN_hex2bn(&y, test->Gy))
  1238. || !TEST_true(EC_POINT_set_affine_coordinates(NISTP, G, x, y, ctx))
  1239. || !TEST_true(BN_hex2bn(&order, test->order))
  1240. || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))
  1241. || !TEST_int_eq(EC_GROUP_get_degree(NISTP), test->degree))
  1242. goto err;
  1243. TEST_note("NIST test vectors ... ");
  1244. if (!TEST_true(BN_hex2bn(&n, test->d)))
  1245. goto err;
  1246. /* fixed point multiplication */
  1247. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1248. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1249. goto err;
  1250. /* random point multiplication */
  1251. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1252. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1253. /* set generator to P = 2*G, where G is the standard generator */
  1254. || !TEST_true(EC_POINT_dbl(NISTP, P, G, ctx))
  1255. || !TEST_true(EC_GROUP_set_generator(NISTP, P, order, BN_value_one()))
  1256. /* set the scalar to m=n/2, where n is the NIST test scalar */
  1257. || !TEST_true(BN_rshift(m, n, 1)))
  1258. goto err;
  1259. /* test the non-standard generator */
  1260. /* fixed point multiplication */
  1261. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1262. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1263. goto err;
  1264. /* random point multiplication */
  1265. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1266. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1267. /*
  1268. * We have not performed precomputation so have_precompute mult should be
  1269. * false
  1270. */
  1271. || !TEST_false(EC_GROUP_have_precompute_mult(NISTP))
  1272. /* now repeat all tests with precomputation */
  1273. || !TEST_true(EC_GROUP_precompute_mult(NISTP, ctx))
  1274. || !TEST_true(EC_GROUP_have_precompute_mult(NISTP)))
  1275. goto err;
  1276. /* fixed point multiplication */
  1277. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1278. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1279. goto err;
  1280. /* random point multiplication */
  1281. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1282. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1283. /* reset generator */
  1284. || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one())))
  1285. goto err;
  1286. /* fixed point multiplication */
  1287. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1288. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1289. goto err;
  1290. /* random point multiplication */
  1291. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1292. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1293. goto err;
  1294. /* regression test for felem_neg bug */
  1295. if (!TEST_true(BN_set_word(m, 32))
  1296. || !TEST_true(BN_set_word(n, 31))
  1297. || !TEST_true(EC_POINT_copy(P, G))
  1298. || !TEST_true(EC_POINT_invert(NISTP, P, ctx))
  1299. || !TEST_true(EC_POINT_mul(NISTP, Q, m, P, n, ctx))
  1300. || !TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, G, ctx)))
  1301. goto err;
  1302. r = group_order_tests(NISTP);
  1303. err:
  1304. EC_GROUP_free(NISTP);
  1305. EC_POINT_free(G);
  1306. EC_POINT_free(P);
  1307. EC_POINT_free(Q);
  1308. EC_POINT_free(Q_CHECK);
  1309. BN_free(n);
  1310. BN_free(m);
  1311. BN_free(p);
  1312. BN_free(a);
  1313. BN_free(b);
  1314. BN_free(x);
  1315. BN_free(y);
  1316. BN_free(order);
  1317. BN_free(yplusone);
  1318. BN_CTX_free(ctx);
  1319. return r;
  1320. }
  1321. # endif
  1322. static const unsigned char p521_named[] = {
  1323. 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23,
  1324. };
  1325. static const unsigned char p521_explicit[] = {
  1326. 0x30, 0x82, 0x01, 0xc3, 0x02, 0x01, 0x01, 0x30, 0x4d, 0x06, 0x07, 0x2a,
  1327. 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x42, 0x01, 0xff, 0xff, 0xff,
  1328. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1329. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1330. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1331. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1332. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1333. 0xff, 0xff, 0x30, 0x81, 0x9f, 0x04, 0x42, 0x01, 0xff, 0xff, 0xff, 0xff,
  1334. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1335. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1336. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1337. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1338. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1339. 0xfc, 0x04, 0x42, 0x00, 0x51, 0x95, 0x3e, 0xb9, 0x61, 0x8e, 0x1c, 0x9a,
  1340. 0x1f, 0x92, 0x9a, 0x21, 0xa0, 0xb6, 0x85, 0x40, 0xee, 0xa2, 0xda, 0x72,
  1341. 0x5b, 0x99, 0xb3, 0x15, 0xf3, 0xb8, 0xb4, 0x89, 0x91, 0x8e, 0xf1, 0x09,
  1342. 0xe1, 0x56, 0x19, 0x39, 0x51, 0xec, 0x7e, 0x93, 0x7b, 0x16, 0x52, 0xc0,
  1343. 0xbd, 0x3b, 0xb1, 0xbf, 0x07, 0x35, 0x73, 0xdf, 0x88, 0x3d, 0x2c, 0x34,
  1344. 0xf1, 0xef, 0x45, 0x1f, 0xd4, 0x6b, 0x50, 0x3f, 0x00, 0x03, 0x15, 0x00,
  1345. 0xd0, 0x9e, 0x88, 0x00, 0x29, 0x1c, 0xb8, 0x53, 0x96, 0xcc, 0x67, 0x17,
  1346. 0x39, 0x32, 0x84, 0xaa, 0xa0, 0xda, 0x64, 0xba, 0x04, 0x81, 0x85, 0x04,
  1347. 0x00, 0xc6, 0x85, 0x8e, 0x06, 0xb7, 0x04, 0x04, 0xe9, 0xcd, 0x9e, 0x3e,
  1348. 0xcb, 0x66, 0x23, 0x95, 0xb4, 0x42, 0x9c, 0x64, 0x81, 0x39, 0x05, 0x3f,
  1349. 0xb5, 0x21, 0xf8, 0x28, 0xaf, 0x60, 0x6b, 0x4d, 0x3d, 0xba, 0xa1, 0x4b,
  1350. 0x5e, 0x77, 0xef, 0xe7, 0x59, 0x28, 0xfe, 0x1d, 0xc1, 0x27, 0xa2, 0xff,
  1351. 0xa8, 0xde, 0x33, 0x48, 0xb3, 0xc1, 0x85, 0x6a, 0x42, 0x9b, 0xf9, 0x7e,
  1352. 0x7e, 0x31, 0xc2, 0xe5, 0xbd, 0x66, 0x01, 0x18, 0x39, 0x29, 0x6a, 0x78,
  1353. 0x9a, 0x3b, 0xc0, 0x04, 0x5c, 0x8a, 0x5f, 0xb4, 0x2c, 0x7d, 0x1b, 0xd9,
  1354. 0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b, 0x44, 0x68, 0x17, 0xaf, 0xbd, 0x17,
  1355. 0x27, 0x3e, 0x66, 0x2c, 0x97, 0xee, 0x72, 0x99, 0x5e, 0xf4, 0x26, 0x40,
  1356. 0xc5, 0x50, 0xb9, 0x01, 0x3f, 0xad, 0x07, 0x61, 0x35, 0x3c, 0x70, 0x86,
  1357. 0xa2, 0x72, 0xc2, 0x40, 0x88, 0xbe, 0x94, 0x76, 0x9f, 0xd1, 0x66, 0x50,
  1358. 0x02, 0x42, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1359. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1360. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa,
  1361. 0x51, 0x86, 0x87, 0x83, 0xbf, 0x2f, 0x96, 0x6b, 0x7f, 0xcc, 0x01, 0x48,
  1362. 0xf7, 0x09, 0xa5, 0xd0, 0x3b, 0xb5, 0xc9, 0xb8, 0x89, 0x9c, 0x47, 0xae,
  1363. 0xbb, 0x6f, 0xb7, 0x1e, 0x91, 0x38, 0x64, 0x09, 0x02, 0x01, 0x01,
  1364. };
  1365. static int parameter_test(void)
  1366. {
  1367. EC_GROUP *group = NULL, *group2 = NULL;
  1368. ECPARAMETERS *ecparameters = NULL;
  1369. unsigned char *buf = NULL;
  1370. int r = 0, len;
  1371. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp112r1))
  1372. || !TEST_ptr(ecparameters = EC_GROUP_get_ecparameters(group, NULL))
  1373. || !TEST_ptr(group2 = EC_GROUP_new_from_ecparameters(ecparameters))
  1374. || !TEST_int_eq(EC_GROUP_cmp(group, group2, NULL), 0))
  1375. goto err;
  1376. EC_GROUP_free(group);
  1377. group = NULL;
  1378. /* Test the named curve encoding, which should be default. */
  1379. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp521r1))
  1380. || !TEST_true((len = i2d_ECPKParameters(group, &buf)) >= 0)
  1381. || !TEST_mem_eq(buf, len, p521_named, sizeof(p521_named)))
  1382. goto err;
  1383. OPENSSL_free(buf);
  1384. buf = NULL;
  1385. /*
  1386. * Test the explicit encoding. P-521 requires correctly zero-padding the
  1387. * curve coefficients.
  1388. */
  1389. EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
  1390. if (!TEST_true((len = i2d_ECPKParameters(group, &buf)) >= 0)
  1391. || !TEST_mem_eq(buf, len, p521_explicit, sizeof(p521_explicit)))
  1392. goto err;
  1393. r = 1;
  1394. err:
  1395. EC_GROUP_free(group);
  1396. EC_GROUP_free(group2);
  1397. ECPARAMETERS_free(ecparameters);
  1398. OPENSSL_free(buf);
  1399. return r;
  1400. }
  1401. #endif
  1402. int setup_tests(void)
  1403. {
  1404. #ifndef OPENSSL_NO_EC
  1405. crv_len = EC_get_builtin_curves(NULL, 0);
  1406. if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
  1407. || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
  1408. return 0;
  1409. ADD_TEST(parameter_test);
  1410. ADD_TEST(prime_field_tests);
  1411. # ifndef OPENSSL_NO_EC2M
  1412. ADD_TEST(char2_field_tests);
  1413. ADD_ALL_TESTS(char2_curve_test, OSSL_NELEM(char2_curve_tests));
  1414. # endif
  1415. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1416. ADD_ALL_TESTS(nistp_single_test, OSSL_NELEM(nistp_tests_params));
  1417. # endif
  1418. ADD_ALL_TESTS(internal_curve_test, crv_len);
  1419. ADD_ALL_TESTS(internal_curve_test_method, crv_len);
  1420. ADD_TEST(group_field_test);
  1421. #endif
  1422. return 1;
  1423. }
  1424. void cleanup_tests(void)
  1425. {
  1426. #ifndef OPENSSL_NO_EC
  1427. OPENSSL_free(curves);
  1428. #endif
  1429. }