ectest.c 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  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 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 "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. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1061. /*
  1062. * nistp_test_params contains magic numbers for testing our optimized
  1063. * implementations of several NIST curves with characteristic > 3.
  1064. */
  1065. struct nistp_test_params {
  1066. const EC_METHOD *(*meth) (void);
  1067. int degree;
  1068. /*
  1069. * Qx, Qy and D are taken from
  1070. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
  1071. * Otherwise, values are standard curve parameters from FIPS 180-3
  1072. */
  1073. const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d;
  1074. };
  1075. static const struct nistp_test_params nistp_tests_params[] = {
  1076. {
  1077. /* P-224 */
  1078. EC_GFp_nistp224_method,
  1079. 224,
  1080. /* p */
  1081. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
  1082. /* a */
  1083. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
  1084. /* b */
  1085. "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
  1086. /* Qx */
  1087. "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E",
  1088. /* Qy */
  1089. "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555",
  1090. /* Gx */
  1091. "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
  1092. /* Gy */
  1093. "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
  1094. /* order */
  1095. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
  1096. /* d */
  1097. "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8",
  1098. },
  1099. {
  1100. /* P-256 */
  1101. EC_GFp_nistp256_method,
  1102. 256,
  1103. /* p */
  1104. "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
  1105. /* a */
  1106. "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
  1107. /* b */
  1108. "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
  1109. /* Qx */
  1110. "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19",
  1111. /* Qy */
  1112. "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09",
  1113. /* Gx */
  1114. "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
  1115. /* Gy */
  1116. "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
  1117. /* order */
  1118. "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
  1119. /* d */
  1120. "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96",
  1121. },
  1122. {
  1123. /* P-521 */
  1124. EC_GFp_nistp521_method,
  1125. 521,
  1126. /* p */
  1127. "1ff"
  1128. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  1129. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  1130. /* a */
  1131. "1ff"
  1132. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  1133. "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc",
  1134. /* b */
  1135. "051"
  1136. "953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e1"
  1137. "56193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
  1138. /* Qx */
  1139. "0098"
  1140. "e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e"
  1141. "59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4",
  1142. /* Qy */
  1143. "0164"
  1144. "350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8"
  1145. "554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e",
  1146. /* Gx */
  1147. "c6"
  1148. "858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dba"
  1149. "a14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
  1150. /* Gy */
  1151. "118"
  1152. "39296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c"
  1153. "97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
  1154. /* order */
  1155. "1ff"
  1156. "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa"
  1157. "51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
  1158. /* d */
  1159. "0100"
  1160. "085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eee"
  1161. "df09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722",
  1162. },
  1163. };
  1164. static int nistp_single_test(int idx)
  1165. {
  1166. const struct nistp_test_params *test = nistp_tests_params + idx;
  1167. BN_CTX *ctx = NULL;
  1168. BIGNUM *p = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL;
  1169. BIGNUM *n = NULL, *m = NULL, *order = NULL, *yplusone = NULL;
  1170. EC_GROUP *NISTP = NULL;
  1171. EC_POINT *G = NULL, *P = NULL, *Q = NULL, *Q_CHECK = NULL;
  1172. int r = 0;
  1173. TEST_note("NIST curve P-%d (optimised implementation):",
  1174. test->degree);
  1175. if (!TEST_ptr(ctx = BN_CTX_new())
  1176. || !TEST_ptr(p = BN_new())
  1177. || !TEST_ptr(a = BN_new())
  1178. || !TEST_ptr(b = BN_new())
  1179. || !TEST_ptr(x = BN_new())
  1180. || !TEST_ptr(y = BN_new())
  1181. || !TEST_ptr(m = BN_new())
  1182. || !TEST_ptr(n = BN_new())
  1183. || !TEST_ptr(order = BN_new())
  1184. || !TEST_ptr(yplusone = BN_new())
  1185. || !TEST_ptr(NISTP = EC_GROUP_new(test->meth()))
  1186. || !TEST_true(BN_hex2bn(&p, test->p))
  1187. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  1188. || !TEST_true(BN_hex2bn(&a, test->a))
  1189. || !TEST_true(BN_hex2bn(&b, test->b))
  1190. || !TEST_true(EC_GROUP_set_curve(NISTP, p, a, b, ctx))
  1191. || !TEST_ptr(G = EC_POINT_new(NISTP))
  1192. || !TEST_ptr(P = EC_POINT_new(NISTP))
  1193. || !TEST_ptr(Q = EC_POINT_new(NISTP))
  1194. || !TEST_ptr(Q_CHECK = EC_POINT_new(NISTP))
  1195. || !TEST_true(BN_hex2bn(&x, test->Qx))
  1196. || !TEST_true(BN_hex2bn(&y, test->Qy))
  1197. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  1198. /*
  1199. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  1200. * and therefore setting the coordinates should fail.
  1201. */
  1202. || !TEST_false(EC_POINT_set_affine_coordinates(NISTP, Q_CHECK, x,
  1203. yplusone, ctx))
  1204. || !TEST_true(EC_POINT_set_affine_coordinates(NISTP, Q_CHECK, x, y,
  1205. ctx))
  1206. || !TEST_true(BN_hex2bn(&x, test->Gx))
  1207. || !TEST_true(BN_hex2bn(&y, test->Gy))
  1208. || !TEST_true(EC_POINT_set_affine_coordinates(NISTP, G, x, y, ctx))
  1209. || !TEST_true(BN_hex2bn(&order, test->order))
  1210. || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))
  1211. || !TEST_int_eq(EC_GROUP_get_degree(NISTP), test->degree))
  1212. goto err;
  1213. TEST_note("NIST test vectors ... ");
  1214. if (!TEST_true(BN_hex2bn(&n, test->d)))
  1215. goto err;
  1216. /* fixed point multiplication */
  1217. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1218. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1219. goto err;
  1220. /* random point multiplication */
  1221. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1222. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1223. /* set generator to P = 2*G, where G is the standard generator */
  1224. || !TEST_true(EC_POINT_dbl(NISTP, P, G, ctx))
  1225. || !TEST_true(EC_GROUP_set_generator(NISTP, P, order, BN_value_one()))
  1226. /* set the scalar to m=n/2, where n is the NIST test scalar */
  1227. || !TEST_true(BN_rshift(m, n, 1)))
  1228. goto err;
  1229. /* test the non-standard generator */
  1230. /* fixed point multiplication */
  1231. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1232. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1233. goto err;
  1234. /* random point multiplication */
  1235. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1236. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1237. /*
  1238. * We have not performed precomputation so have_precompute mult should be
  1239. * false
  1240. */
  1241. || !TEST_false(EC_GROUP_have_precompute_mult(NISTP))
  1242. /* now repeat all tests with precomputation */
  1243. || !TEST_true(EC_GROUP_precompute_mult(NISTP, ctx))
  1244. || !TEST_true(EC_GROUP_have_precompute_mult(NISTP)))
  1245. goto err;
  1246. /* fixed point multiplication */
  1247. EC_POINT_mul(NISTP, Q, m, 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, P, m, ctx);
  1252. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1253. /* reset generator */
  1254. || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one())))
  1255. goto err;
  1256. /* fixed point multiplication */
  1257. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1258. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1259. goto err;
  1260. /* random point multiplication */
  1261. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1262. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1263. goto err;
  1264. /* regression test for felem_neg bug */
  1265. if (!TEST_true(BN_set_word(m, 32))
  1266. || !TEST_true(BN_set_word(n, 31))
  1267. || !TEST_true(EC_POINT_copy(P, G))
  1268. || !TEST_true(EC_POINT_invert(NISTP, P, ctx))
  1269. || !TEST_true(EC_POINT_mul(NISTP, Q, m, P, n, ctx))
  1270. || !TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, G, ctx)))
  1271. goto err;
  1272. r = group_order_tests(NISTP);
  1273. err:
  1274. EC_GROUP_free(NISTP);
  1275. EC_POINT_free(G);
  1276. EC_POINT_free(P);
  1277. EC_POINT_free(Q);
  1278. EC_POINT_free(Q_CHECK);
  1279. BN_free(n);
  1280. BN_free(m);
  1281. BN_free(p);
  1282. BN_free(a);
  1283. BN_free(b);
  1284. BN_free(x);
  1285. BN_free(y);
  1286. BN_free(order);
  1287. BN_free(yplusone);
  1288. BN_CTX_free(ctx);
  1289. return r;
  1290. }
  1291. # endif
  1292. static const unsigned char p521_named[] = {
  1293. 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23,
  1294. };
  1295. static const unsigned char p521_explicit[] = {
  1296. 0x30, 0x82, 0x01, 0xc3, 0x02, 0x01, 0x01, 0x30, 0x4d, 0x06, 0x07, 0x2a,
  1297. 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x42, 0x01, 0xff, 0xff, 0xff,
  1298. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1299. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1300. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1301. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1302. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1303. 0xff, 0xff, 0x30, 0x81, 0x9f, 0x04, 0x42, 0x01, 0xff, 0xff, 0xff, 0xff,
  1304. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1305. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1306. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1307. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1308. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1309. 0xfc, 0x04, 0x42, 0x00, 0x51, 0x95, 0x3e, 0xb9, 0x61, 0x8e, 0x1c, 0x9a,
  1310. 0x1f, 0x92, 0x9a, 0x21, 0xa0, 0xb6, 0x85, 0x40, 0xee, 0xa2, 0xda, 0x72,
  1311. 0x5b, 0x99, 0xb3, 0x15, 0xf3, 0xb8, 0xb4, 0x89, 0x91, 0x8e, 0xf1, 0x09,
  1312. 0xe1, 0x56, 0x19, 0x39, 0x51, 0xec, 0x7e, 0x93, 0x7b, 0x16, 0x52, 0xc0,
  1313. 0xbd, 0x3b, 0xb1, 0xbf, 0x07, 0x35, 0x73, 0xdf, 0x88, 0x3d, 0x2c, 0x34,
  1314. 0xf1, 0xef, 0x45, 0x1f, 0xd4, 0x6b, 0x50, 0x3f, 0x00, 0x03, 0x15, 0x00,
  1315. 0xd0, 0x9e, 0x88, 0x00, 0x29, 0x1c, 0xb8, 0x53, 0x96, 0xcc, 0x67, 0x17,
  1316. 0x39, 0x32, 0x84, 0xaa, 0xa0, 0xda, 0x64, 0xba, 0x04, 0x81, 0x85, 0x04,
  1317. 0x00, 0xc6, 0x85, 0x8e, 0x06, 0xb7, 0x04, 0x04, 0xe9, 0xcd, 0x9e, 0x3e,
  1318. 0xcb, 0x66, 0x23, 0x95, 0xb4, 0x42, 0x9c, 0x64, 0x81, 0x39, 0x05, 0x3f,
  1319. 0xb5, 0x21, 0xf8, 0x28, 0xaf, 0x60, 0x6b, 0x4d, 0x3d, 0xba, 0xa1, 0x4b,
  1320. 0x5e, 0x77, 0xef, 0xe7, 0x59, 0x28, 0xfe, 0x1d, 0xc1, 0x27, 0xa2, 0xff,
  1321. 0xa8, 0xde, 0x33, 0x48, 0xb3, 0xc1, 0x85, 0x6a, 0x42, 0x9b, 0xf9, 0x7e,
  1322. 0x7e, 0x31, 0xc2, 0xe5, 0xbd, 0x66, 0x01, 0x18, 0x39, 0x29, 0x6a, 0x78,
  1323. 0x9a, 0x3b, 0xc0, 0x04, 0x5c, 0x8a, 0x5f, 0xb4, 0x2c, 0x7d, 0x1b, 0xd9,
  1324. 0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b, 0x44, 0x68, 0x17, 0xaf, 0xbd, 0x17,
  1325. 0x27, 0x3e, 0x66, 0x2c, 0x97, 0xee, 0x72, 0x99, 0x5e, 0xf4, 0x26, 0x40,
  1326. 0xc5, 0x50, 0xb9, 0x01, 0x3f, 0xad, 0x07, 0x61, 0x35, 0x3c, 0x70, 0x86,
  1327. 0xa2, 0x72, 0xc2, 0x40, 0x88, 0xbe, 0x94, 0x76, 0x9f, 0xd1, 0x66, 0x50,
  1328. 0x02, 0x42, 0x01, 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, 0xfa,
  1331. 0x51, 0x86, 0x87, 0x83, 0xbf, 0x2f, 0x96, 0x6b, 0x7f, 0xcc, 0x01, 0x48,
  1332. 0xf7, 0x09, 0xa5, 0xd0, 0x3b, 0xb5, 0xc9, 0xb8, 0x89, 0x9c, 0x47, 0xae,
  1333. 0xbb, 0x6f, 0xb7, 0x1e, 0x91, 0x38, 0x64, 0x09, 0x02, 0x01, 0x01,
  1334. };
  1335. static int parameter_test(void)
  1336. {
  1337. EC_GROUP *group = NULL, *group2 = NULL;
  1338. ECPARAMETERS *ecparameters = NULL;
  1339. unsigned char *buf = NULL;
  1340. int r = 0, len;
  1341. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp112r1))
  1342. || !TEST_ptr(ecparameters = EC_GROUP_get_ecparameters(group, NULL))
  1343. || !TEST_ptr(group2 = EC_GROUP_new_from_ecparameters(ecparameters))
  1344. || !TEST_int_eq(EC_GROUP_cmp(group, group2, NULL), 0))
  1345. goto err;
  1346. EC_GROUP_free(group);
  1347. group = NULL;
  1348. /* Test the named curve encoding, which should be default. */
  1349. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp521r1))
  1350. || !TEST_true((len = i2d_ECPKParameters(group, &buf)) >= 0)
  1351. || !TEST_mem_eq(buf, len, p521_named, sizeof(p521_named)))
  1352. goto err;
  1353. OPENSSL_free(buf);
  1354. buf = NULL;
  1355. /*
  1356. * Test the explicit encoding. P-521 requires correctly zero-padding the
  1357. * curve coefficients.
  1358. */
  1359. EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
  1360. if (!TEST_true((len = i2d_ECPKParameters(group, &buf)) >= 0)
  1361. || !TEST_mem_eq(buf, len, p521_explicit, sizeof(p521_explicit)))
  1362. goto err;
  1363. r = 1;
  1364. err:
  1365. EC_GROUP_free(group);
  1366. EC_GROUP_free(group2);
  1367. ECPARAMETERS_free(ecparameters);
  1368. OPENSSL_free(buf);
  1369. return r;
  1370. }
  1371. #endif
  1372. int setup_tests(void)
  1373. {
  1374. #ifndef OPENSSL_NO_EC
  1375. crv_len = EC_get_builtin_curves(NULL, 0);
  1376. if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
  1377. || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
  1378. return 0;
  1379. ADD_TEST(parameter_test);
  1380. ADD_TEST(prime_field_tests);
  1381. # ifndef OPENSSL_NO_EC2M
  1382. ADD_TEST(char2_field_tests);
  1383. ADD_ALL_TESTS(char2_curve_test, OSSL_NELEM(char2_curve_tests));
  1384. # endif
  1385. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1386. ADD_ALL_TESTS(nistp_single_test, OSSL_NELEM(nistp_tests_params));
  1387. # endif
  1388. ADD_ALL_TESTS(internal_curve_test, crv_len);
  1389. ADD_ALL_TESTS(internal_curve_test_method, crv_len);
  1390. #endif
  1391. return 1;
  1392. }
  1393. void cleanup_tests(void)
  1394. {
  1395. #ifndef OPENSSL_NO_EC
  1396. OPENSSL_free(curves);
  1397. #endif
  1398. }