ectest.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389
  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 <string.h>
  11. #include "internal/nelem.h"
  12. #include "testutil.h"
  13. #ifndef OPENSSL_NO_EC
  14. # include <openssl/ec.h>
  15. # ifndef OPENSSL_NO_ENGINE
  16. # include <openssl/engine.h>
  17. # endif
  18. # include <openssl/err.h>
  19. # include <openssl/obj_mac.h>
  20. # include <openssl/objects.h>
  21. # include <openssl/rand.h>
  22. # include <openssl/bn.h>
  23. # include <openssl/opensslconf.h>
  24. static size_t crv_len = 0;
  25. static EC_builtin_curve *curves = NULL;
  26. /* test multiplication with group order, long and negative scalars */
  27. static int group_order_tests(EC_GROUP *group)
  28. {
  29. BIGNUM *n1 = NULL, *n2 = NULL, *order = NULL;
  30. EC_POINT *P = NULL, *Q = NULL, *R = NULL, *S = NULL;
  31. const EC_POINT *G = NULL;
  32. BN_CTX *ctx = NULL;
  33. int i = 0, r = 0;
  34. if (!TEST_ptr(n1 = BN_new())
  35. || !TEST_ptr(n2 = BN_new())
  36. || !TEST_ptr(order = BN_new())
  37. || !TEST_ptr(ctx = BN_CTX_new())
  38. || !TEST_ptr(G = EC_GROUP_get0_generator(group))
  39. || !TEST_ptr(P = EC_POINT_new(group))
  40. || !TEST_ptr(Q = EC_POINT_new(group))
  41. || !TEST_ptr(R = EC_POINT_new(group))
  42. || !TEST_ptr(S = EC_POINT_new(group)))
  43. goto err;
  44. if (!TEST_true(EC_GROUP_get_order(group, order, ctx))
  45. || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
  46. || !TEST_true(EC_POINT_is_at_infinity(group, Q))
  47. || !TEST_true(EC_GROUP_precompute_mult(group, ctx))
  48. || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
  49. || !TEST_true(EC_POINT_is_at_infinity(group, Q))
  50. || !TEST_true(EC_POINT_copy(P, G))
  51. || !TEST_true(BN_one(n1))
  52. || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))
  53. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))
  54. || !TEST_true(BN_sub(n1, order, n1))
  55. || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))
  56. || !TEST_true(EC_POINT_invert(group, Q, ctx))
  57. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))
  58. goto err;
  59. for (i = 1; i <= 2; i++) {
  60. const BIGNUM *scalars[6];
  61. const EC_POINT *points[6];
  62. if (!TEST_true(BN_set_word(n1, i))
  63. /*
  64. * If i == 1, P will be the predefined generator for which
  65. * EC_GROUP_precompute_mult has set up precomputation.
  66. */
  67. || !TEST_true(EC_POINT_mul(group, P, n1, NULL, NULL, ctx))
  68. || (i == 1 && !TEST_int_eq(0, EC_POINT_cmp(group, P, G, ctx)))
  69. || !TEST_true(BN_one(n1))
  70. /* n1 = 1 - order */
  71. || !TEST_true(BN_sub(n1, n1, order))
  72. || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n1, ctx))
  73. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))
  74. /* n2 = 1 + order */
  75. || !TEST_true(BN_add(n2, order, BN_value_one()))
  76. || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  77. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))
  78. /* n2 = (1 - order) * (1 + order) = 1 - order^2 */
  79. || !TEST_true(BN_mul(n2, n1, n2, ctx))
  80. || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  81. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))
  82. goto err;
  83. /* n2 = order^2 - 1 */
  84. BN_set_negative(n2, 0);
  85. if (!TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  86. /* Add P to verify the result. */
  87. || !TEST_true(EC_POINT_add(group, Q, Q, P, ctx))
  88. || !TEST_true(EC_POINT_is_at_infinity(group, Q))
  89. /* Exercise EC_POINTs_mul, including corner cases. */
  90. || !TEST_false(EC_POINT_is_at_infinity(group, P)))
  91. goto err;
  92. scalars[0] = scalars[1] = BN_value_one();
  93. points[0] = points[1] = P;
  94. if (!TEST_true(EC_POINTs_mul(group, R, NULL, 2, points, scalars, ctx))
  95. || !TEST_true(EC_POINT_dbl(group, S, points[0], ctx))
  96. || !TEST_int_eq(0, EC_POINT_cmp(group, R, S, ctx)))
  97. goto err;
  98. scalars[0] = n1;
  99. points[0] = Q; /* => infinity */
  100. scalars[1] = n2;
  101. points[1] = P; /* => -P */
  102. scalars[2] = n1;
  103. points[2] = Q; /* => infinity */
  104. scalars[3] = n2;
  105. points[3] = Q; /* => infinity */
  106. scalars[4] = n1;
  107. points[4] = P; /* => P */
  108. scalars[5] = n2;
  109. points[5] = Q; /* => infinity */
  110. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx))
  111. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  112. goto err;
  113. }
  114. r = 1;
  115. err:
  116. if (r == 0 && i != 0)
  117. TEST_info(i == 1 ? "allowing precomputation" :
  118. "without precomputation");
  119. EC_POINT_free(P);
  120. EC_POINT_free(Q);
  121. EC_POINT_free(R);
  122. EC_POINT_free(S);
  123. BN_free(n1);
  124. BN_free(n2);
  125. BN_free(order);
  126. BN_CTX_free(ctx);
  127. return r;
  128. }
  129. static int prime_field_tests(void)
  130. {
  131. BN_CTX *ctx = NULL;
  132. BIGNUM *p = NULL, *a = NULL, *b = NULL, *scalar3 = NULL;
  133. EC_GROUP *group = NULL, *tmp = NULL;
  134. EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL,
  135. *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
  136. EC_POINT *P = NULL, *Q = NULL, *R = NULL;
  137. BIGNUM *x = NULL, *y = NULL, *z = NULL, *yplusone = NULL;
  138. const EC_POINT *points[4];
  139. const BIGNUM *scalars[4];
  140. unsigned char buf[100];
  141. size_t len, r = 0;
  142. int k;
  143. if (!TEST_ptr(ctx = BN_CTX_new())
  144. || !TEST_ptr(p = BN_new())
  145. || !TEST_ptr(a = BN_new())
  146. || !TEST_ptr(b = BN_new())
  147. || !TEST_true(BN_hex2bn(&p, "17"))
  148. || !TEST_true(BN_hex2bn(&a, "1"))
  149. || !TEST_true(BN_hex2bn(&b, "1"))
  150. /*
  151. * applications should use EC_GROUP_new_curve_GFp so
  152. * that the library gets to choose the EC_METHOD
  153. */
  154. || !TEST_ptr(group = EC_GROUP_new(EC_GFp_mont_method()))
  155. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  156. || !TEST_ptr(tmp = EC_GROUP_new(EC_GROUP_method_of(group)))
  157. || !TEST_true(EC_GROUP_copy(tmp, group)))
  158. goto err;
  159. EC_GROUP_free(group);
  160. group = tmp;
  161. tmp = NULL;
  162. if (!TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx)))
  163. goto err;
  164. TEST_info("Curve defined by Weierstrass equation");
  165. TEST_note(" y^2 = x^3 + a*x + b (mod p)");
  166. test_output_bignum("a", a);
  167. test_output_bignum("b", b);
  168. test_output_bignum("p", p);
  169. buf[0] = 0;
  170. if (!TEST_ptr(P = EC_POINT_new(group))
  171. || !TEST_ptr(Q = EC_POINT_new(group))
  172. || !TEST_ptr(R = EC_POINT_new(group))
  173. || !TEST_true(EC_POINT_set_to_infinity(group, P))
  174. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  175. || !TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx))
  176. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))
  177. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  178. || !TEST_ptr(x = BN_new())
  179. || !TEST_ptr(y = BN_new())
  180. || !TEST_ptr(z = BN_new())
  181. || !TEST_ptr(yplusone = BN_new())
  182. || !TEST_true(BN_hex2bn(&x, "D"))
  183. || !TEST_true(EC_POINT_set_compressed_coordinates(group, Q, x, 1, ctx)))
  184. goto err;
  185. if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) {
  186. if (!TEST_true(EC_POINT_get_affine_coordinates(group, Q, x, y, ctx)))
  187. goto err;
  188. TEST_info("Point is not on curve");
  189. test_output_bignum("x", x);
  190. test_output_bignum("y", y);
  191. goto err;
  192. }
  193. TEST_note("A cyclic subgroup:");
  194. k = 100;
  195. do {
  196. if (!TEST_int_ne(k--, 0))
  197. goto err;
  198. if (EC_POINT_is_at_infinity(group, P)) {
  199. TEST_note(" point at infinity");
  200. } else {
  201. if (!TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y,
  202. ctx)))
  203. goto err;
  204. test_output_bignum("x", x);
  205. test_output_bignum("y", y);
  206. }
  207. if (!TEST_true(EC_POINT_copy(R, P))
  208. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)))
  209. goto err;
  210. } while (!EC_POINT_is_at_infinity(group, P));
  211. if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx))
  212. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  213. goto err;
  214. len =
  215. EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,
  216. sizeof(buf), ctx);
  217. if (!TEST_size_t_ne(len, 0)
  218. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  219. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  220. goto err;
  221. test_output_memory("Generator as octet string, compressed form:",
  222. buf, len);
  223. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
  224. buf, sizeof(buf), ctx);
  225. if (!TEST_size_t_ne(len, 0)
  226. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  227. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  228. goto err;
  229. test_output_memory("Generator as octet string, uncompressed form:",
  230. buf, len);
  231. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID,
  232. buf, sizeof(buf), ctx);
  233. if (!TEST_size_t_ne(len, 0)
  234. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  235. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  236. goto err;
  237. test_output_memory("Generator as octet string, hybrid form:",
  238. buf, len);
  239. if (!TEST_true(EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z,
  240. ctx)))
  241. goto err;
  242. TEST_info("A representation of the inverse of that generator in");
  243. TEST_note("Jacobian projective coordinates");
  244. test_output_bignum("x", x);
  245. test_output_bignum("y", y);
  246. test_output_bignum("z", z);
  247. if (!TEST_true(EC_POINT_invert(group, P, ctx))
  248. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
  249. /*
  250. * Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2,
  251. * 2000) -- not a NIST curve, but commonly used
  252. */
  253. || !TEST_true(BN_hex2bn(&p, "FFFFFFFF"
  254. "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"))
  255. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  256. || !TEST_true(BN_hex2bn(&a, "FFFFFFFF"
  257. "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"))
  258. || !TEST_true(BN_hex2bn(&b, "1C97BEFC"
  259. "54BD7A8B65ACF89F81D4D4ADC565FA45"))
  260. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  261. || !TEST_true(BN_hex2bn(&x, "4A96B568"
  262. "8EF573284664698968C38BB913CBFC82"))
  263. || !TEST_true(BN_hex2bn(&y, "23a62855"
  264. "3168947d59dcc912042351377ac5fb32"))
  265. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  266. /*
  267. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  268. * and therefore setting the coordinates should fail.
  269. */
  270. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  271. ctx))
  272. || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
  273. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  274. || !TEST_true(BN_hex2bn(&z, "0100000000"
  275. "000000000001F4C8F927AED3CA752257"))
  276. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  277. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  278. goto err;
  279. TEST_info("SEC2 curve secp160r1 -- Generator");
  280. test_output_bignum("x", x);
  281. test_output_bignum("y", y);
  282. /* G_y value taken from the standard: */
  283. if (!TEST_true(BN_hex2bn(&z, "23a62855"
  284. "3168947d59dcc912042351377ac5fb32"))
  285. || !TEST_BN_eq(y, z)
  286. || !TEST_int_eq(EC_GROUP_get_degree(group), 160)
  287. || !group_order_tests(group)
  288. || !TEST_ptr(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))
  289. || !TEST_true(EC_GROUP_copy(P_160, group))
  290. /* Curve P-192 (FIPS PUB 186-2, App. 6) */
  291. || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFF"
  292. "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"))
  293. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  294. || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFF"
  295. "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"))
  296. || !TEST_true(BN_hex2bn(&b, "64210519E59C80E7"
  297. "0FA7E9AB72243049FEB8DEECC146B9B1"))
  298. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  299. || !TEST_true(BN_hex2bn(&x, "188DA80EB03090F6"
  300. "7CBF20EB43A18800F4FF0AFD82FF1012"))
  301. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
  302. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  303. || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFF"
  304. "FFFFFFFF99DEF836146BC9B1B4D22831"))
  305. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  306. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  307. goto err;
  308. TEST_info("NIST curve P-192 -- Generator");
  309. test_output_bignum("x", x);
  310. test_output_bignum("y", y);
  311. /* G_y value taken from the standard: */
  312. if (!TEST_true(BN_hex2bn(&z, "07192B95FFC8DA78"
  313. "631011ED6B24CDD573F977A11E794811"))
  314. || !TEST_BN_eq(y, z)
  315. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  316. /*
  317. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  318. * and therefore setting the coordinates should fail.
  319. */
  320. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  321. ctx))
  322. || !TEST_int_eq(EC_GROUP_get_degree(group), 192)
  323. || !group_order_tests(group)
  324. || !TEST_ptr(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))
  325. || !TEST_true(EC_GROUP_copy(P_192, group))
  326. /* Curve P-224 (FIPS PUB 186-2, App. 6) */
  327. || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFF"
  328. "FFFFFFFF000000000000000000000001"))
  329. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  330. || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFF"
  331. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"))
  332. || !TEST_true(BN_hex2bn(&b, "B4050A850C04B3ABF5413256"
  333. "5044B0B7D7BFD8BA270B39432355FFB4"))
  334. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  335. || !TEST_true(BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B9"
  336. "4A03C1D356C21122343280D6115C1D21"))
  337. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx))
  338. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  339. || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF"
  340. "FFFF16A2E0B8F03E13DD29455C5C2A3D"))
  341. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  342. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  343. goto err;
  344. TEST_info("NIST curve P-224 -- Generator");
  345. test_output_bignum("x", x);
  346. test_output_bignum("y", y);
  347. /* G_y value taken from the standard: */
  348. if (!TEST_true(BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6"
  349. "CD4375A05A07476444D5819985007E34"))
  350. || !TEST_BN_eq(y, z)
  351. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  352. /*
  353. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  354. * and therefore setting the coordinates should fail.
  355. */
  356. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  357. ctx))
  358. || !TEST_int_eq(EC_GROUP_get_degree(group), 224)
  359. || !group_order_tests(group)
  360. || !TEST_ptr(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))
  361. || !TEST_true(EC_GROUP_copy(P_224, group))
  362. /* Curve P-256 (FIPS PUB 186-2, App. 6) */
  363. || !TEST_true(BN_hex2bn(&p, "FFFFFFFF000000010000000000000000"
  364. "00000000FFFFFFFFFFFFFFFFFFFFFFFF"))
  365. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  366. || !TEST_true(BN_hex2bn(&a, "FFFFFFFF000000010000000000000000"
  367. "00000000FFFFFFFFFFFFFFFFFFFFFFFC"))
  368. || !TEST_true(BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC"
  369. "651D06B0CC53B0F63BCE3C3E27D2604B"))
  370. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  371. || !TEST_true(BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F2"
  372. "77037D812DEB33A0F4A13945D898C296"))
  373. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
  374. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  375. || !TEST_true(BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFF"
  376. "BCE6FAADA7179E84F3B9CAC2FC632551"))
  377. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  378. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  379. goto err;
  380. TEST_info("NIST curve P-256 -- Generator");
  381. test_output_bignum("x", x);
  382. test_output_bignum("y", y);
  383. /* G_y value taken from the standard: */
  384. if (!TEST_true(BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  385. "2BCE33576B315ECECBB6406837BF51F5"))
  386. || !TEST_BN_eq(y, z)
  387. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  388. /*
  389. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  390. * and therefore setting the coordinates should fail.
  391. */
  392. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  393. ctx))
  394. || !TEST_int_eq(EC_GROUP_get_degree(group), 256)
  395. || !group_order_tests(group)
  396. || !TEST_ptr(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))
  397. || !TEST_true(EC_GROUP_copy(P_256, group))
  398. /* Curve P-384 (FIPS PUB 186-2, App. 6) */
  399. || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  400. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
  401. "FFFFFFFF0000000000000000FFFFFFFF"))
  402. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  403. || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  404. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
  405. "FFFFFFFF0000000000000000FFFFFFFC"))
  406. || !TEST_true(BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19"
  407. "181D9C6EFE8141120314088F5013875A"
  408. "C656398D8A2ED19D2A85C8EDD3EC2AEF"))
  409. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  410. || !TEST_true(BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD74"
  411. "6E1D3B628BA79B9859F741E082542A38"
  412. "5502F25DBF55296C3A545E3872760AB7"))
  413. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
  414. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  415. || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  416. "FFFFFFFFFFFFFFFFC7634D81F4372DDF"
  417. "581A0DB248B0A77AECEC196ACCC52973"))
  418. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  419. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  420. goto err;
  421. TEST_info("NIST curve P-384 -- Generator");
  422. test_output_bignum("x", x);
  423. test_output_bignum("y", y);
  424. /* G_y value taken from the standard: */
  425. if (!TEST_true(BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29"
  426. "F8F41DBD289A147CE9DA3113B5F0B8C0"
  427. "0A60B1CE1D7E819D7A431D7C90EA0E5F"))
  428. || !TEST_BN_eq(y, z)
  429. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  430. /*
  431. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  432. * and therefore setting the coordinates should fail.
  433. */
  434. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  435. ctx))
  436. || !TEST_int_eq(EC_GROUP_get_degree(group), 384)
  437. || !group_order_tests(group)
  438. || !TEST_ptr(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))
  439. || !TEST_true(EC_GROUP_copy(P_384, group))
  440. /* Curve P-521 (FIPS PUB 186-2, App. 6) */
  441. || !TEST_true(BN_hex2bn(&p, "1FF"
  442. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  443. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  444. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  445. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
  446. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  447. || !TEST_true(BN_hex2bn(&a, "1FF"
  448. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  449. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  450. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  451. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC"))
  452. || !TEST_true(BN_hex2bn(&b, "051"
  453. "953EB9618E1C9A1F929A21A0B68540EE"
  454. "A2DA725B99B315F3B8B489918EF109E1"
  455. "56193951EC7E937B1652C0BD3BB1BF07"
  456. "3573DF883D2C34F1EF451FD46B503F00"))
  457. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  458. || !TEST_true(BN_hex2bn(&x, "C6"
  459. "858E06B70404E9CD9E3ECB662395B442"
  460. "9C648139053FB521F828AF606B4D3DBA"
  461. "A14B5E77EFE75928FE1DC127A2FFA8DE"
  462. "3348B3C1856A429BF97E7E31C2E5BD66"))
  463. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx))
  464. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  465. || !TEST_true(BN_hex2bn(&z, "1FF"
  466. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  467. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA"
  468. "51868783BF2F966B7FCC0148F709A5D0"
  469. "3BB5C9B8899C47AEBB6FB71E91386409"))
  470. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  471. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  472. goto err;
  473. TEST_info("NIST curve P-521 -- Generator");
  474. test_output_bignum("x", x);
  475. test_output_bignum("y", y);
  476. /* G_y value taken from the standard: */
  477. if (!TEST_true(BN_hex2bn(&z, "118"
  478. "39296A789A3BC0045C8A5FB42C7D1BD9"
  479. "98F54449579B446817AFBD17273E662C"
  480. "97EE72995EF42640C550B9013FAD0761"
  481. "353C7086A272C24088BE94769FD16650"))
  482. || !TEST_BN_eq(y, z)
  483. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  484. /*
  485. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  486. * and therefore setting the coordinates should fail.
  487. */
  488. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  489. ctx))
  490. || !TEST_int_eq(EC_GROUP_get_degree(group), 521)
  491. || !group_order_tests(group)
  492. || !TEST_ptr(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))
  493. || !TEST_true(EC_GROUP_copy(P_521, group))
  494. /* more tests using the last curve */
  495. /* Restore the point that got mangled in the (x, y + 1) test. */
  496. || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
  497. || !TEST_true(EC_POINT_copy(Q, P))
  498. || !TEST_false(EC_POINT_is_at_infinity(group, Q))
  499. || !TEST_true(EC_POINT_dbl(group, P, P, ctx))
  500. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  501. || !TEST_true(EC_POINT_invert(group, Q, ctx)) /* P = -2Q */
  502. || !TEST_true(EC_POINT_add(group, R, P, Q, ctx))
  503. || !TEST_true(EC_POINT_add(group, R, R, Q, ctx))
  504. || !TEST_true(EC_POINT_is_at_infinity(group, R)) /* R = P + 2Q */
  505. || !TEST_false(EC_POINT_is_at_infinity(group, Q)))
  506. goto err;
  507. points[0] = Q;
  508. points[1] = Q;
  509. points[2] = Q;
  510. points[3] = Q;
  511. if (!TEST_true(EC_GROUP_get_order(group, z, ctx))
  512. || !TEST_true(BN_add(y, z, BN_value_one()))
  513. || !TEST_BN_even(y)
  514. || !TEST_true(BN_rshift1(y, y)))
  515. goto err;
  516. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  517. scalars[1] = y;
  518. TEST_note("combined multiplication ...");
  519. /* z is still the group order */
  520. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  521. || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
  522. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
  523. || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx))
  524. || !TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))
  525. || !TEST_true(BN_add(z, z, y)))
  526. goto err;
  527. BN_set_negative(z, 1);
  528. scalars[0] = y;
  529. scalars[1] = z; /* z = -(order + y) */
  530. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  531. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  532. || !TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))
  533. || !TEST_true(BN_add(z, x, y)))
  534. goto err;
  535. BN_set_negative(z, 1);
  536. scalars[0] = x;
  537. scalars[1] = y;
  538. scalars[2] = z; /* z = -(x+y) */
  539. if (!TEST_ptr(scalar3 = BN_new()))
  540. goto err;
  541. BN_zero(scalar3);
  542. scalars[3] = scalar3;
  543. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx))
  544. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  545. goto err;
  546. TEST_note(" ok\n");
  547. r = 1;
  548. err:
  549. BN_CTX_free(ctx);
  550. BN_free(p);
  551. BN_free(a);
  552. BN_free(b);
  553. EC_GROUP_free(group);
  554. EC_GROUP_free(tmp);
  555. EC_POINT_free(P);
  556. EC_POINT_free(Q);
  557. EC_POINT_free(R);
  558. BN_free(x);
  559. BN_free(y);
  560. BN_free(z);
  561. BN_free(yplusone);
  562. BN_free(scalar3);
  563. EC_GROUP_free(P_160);
  564. EC_GROUP_free(P_192);
  565. EC_GROUP_free(P_224);
  566. EC_GROUP_free(P_256);
  567. EC_GROUP_free(P_384);
  568. EC_GROUP_free(P_521);
  569. return r;
  570. }
  571. # ifndef OPENSSL_NO_EC2M
  572. static struct c2_curve_test {
  573. const char *name;
  574. const char *p;
  575. const char *a;
  576. const char *b;
  577. const char *x;
  578. const char *y;
  579. int ybit;
  580. const char *order;
  581. const char *cof;
  582. int degree;
  583. } char2_curve_tests[] = {
  584. /* Curve K-163 (FIPS PUB 186-2, App. 6) */
  585. {
  586. "NIST curve K-163",
  587. "0800000000000000000000000000000000000000C9",
  588. "1",
  589. "1",
  590. "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
  591. "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
  592. 1, "04000000000000000000020108A2E0CC0D99F8A5EF", "2", 163
  593. },
  594. /* Curve B-163 (FIPS PUB 186-2, App. 6) */
  595. {
  596. "NIST curve B-163",
  597. "0800000000000000000000000000000000000000C9",
  598. "1",
  599. "020A601907B8C953CA1481EB10512F78744A3205FD",
  600. "03F0EBA16286A2D57EA0991168D4994637E8343E36",
  601. "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
  602. 1, "040000000000000000000292FE77E70C12A4234C33", "2", 163
  603. },
  604. /* Curve K-233 (FIPS PUB 186-2, App. 6) */
  605. {
  606. "NIST curve K-233",
  607. "020000000000000000000000000000000000000004000000000000000001",
  608. "0",
  609. "1",
  610. "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
  611. "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
  612. 0,
  613. "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
  614. "4", 233
  615. },
  616. /* Curve B-233 (FIPS PUB 186-2, App. 6) */
  617. {
  618. "NIST curve B-233",
  619. "020000000000000000000000000000000000000004000000000000000001",
  620. "000000000000000000000000000000000000000000000000000000000001",
  621. "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
  622. "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
  623. "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
  624. 1,
  625. "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
  626. "2", 233
  627. },
  628. /* Curve K-283 (FIPS PUB 186-2, App. 6) */
  629. {
  630. "NIST curve K-283",
  631. "08000000"
  632. "00000000000000000000000000000000000000000000000000000000000010A1",
  633. "0",
  634. "1",
  635. "0503213F"
  636. "78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
  637. "01CCDA38"
  638. "0F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
  639. 0,
  640. "01FFFFFF"
  641. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
  642. "4", 283
  643. },
  644. /* Curve B-283 (FIPS PUB 186-2, App. 6) */
  645. {
  646. "NIST curve B-283",
  647. "08000000"
  648. "00000000000000000000000000000000000000000000000000000000000010A1",
  649. "00000000"
  650. "0000000000000000000000000000000000000000000000000000000000000001",
  651. "027B680A"
  652. "C8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
  653. "05F93925"
  654. "8DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
  655. "03676854"
  656. "FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
  657. 1,
  658. "03FFFFFF"
  659. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
  660. "2", 283
  661. },
  662. /* Curve K-409 (FIPS PUB 186-2, App. 6) */
  663. {
  664. "NIST curve K-409",
  665. "0200000000000000000000000000000000000000"
  666. "0000000000000000000000000000000000000000008000000000000000000001",
  667. "0",
  668. "1",
  669. "0060F05F658F49C1AD3AB1890F7184210EFD0987"
  670. "E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
  671. "01E369050B7C4E42ACBA1DACBF04299C3460782F"
  672. "918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
  673. 1,
  674. "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  675. "FFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
  676. "4", 409
  677. },
  678. /* Curve B-409 (FIPS PUB 186-2, App. 6) */
  679. {
  680. "NIST curve B-409",
  681. "0200000000000000000000000000000000000000"
  682. "0000000000000000000000000000000000000000008000000000000000000001",
  683. "0000000000000000000000000000000000000000"
  684. "0000000000000000000000000000000000000000000000000000000000000001",
  685. "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422E"
  686. "F1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
  687. "015D4860D088DDB3496B0C6064756260441CDE4A"
  688. "F1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
  689. "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5"
  690. "A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
  691. 1,
  692. "0100000000000000000000000000000000000000"
  693. "00000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
  694. "2", 409
  695. },
  696. /* Curve K-571 (FIPS PUB 186-2, App. 6) */
  697. {
  698. "NIST curve K-571",
  699. "800000000000000"
  700. "0000000000000000000000000000000000000000000000000000000000000000"
  701. "0000000000000000000000000000000000000000000000000000000000000425",
  702. "0",
  703. "1",
  704. "026EB7A859923FBC"
  705. "82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E6"
  706. "47DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
  707. "0349DC807F4FBF37"
  708. "4F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA7"
  709. "4FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
  710. 0,
  711. "0200000000000000"
  712. "00000000000000000000000000000000000000000000000000000000131850E1"
  713. "F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
  714. "4", 571
  715. },
  716. /* Curve B-571 (FIPS PUB 186-2, App. 6) */
  717. {
  718. "NIST curve B-571",
  719. "800000000000000"
  720. "0000000000000000000000000000000000000000000000000000000000000000"
  721. "0000000000000000000000000000000000000000000000000000000000000425",
  722. "0000000000000000"
  723. "0000000000000000000000000000000000000000000000000000000000000000"
  724. "0000000000000000000000000000000000000000000000000000000000000001",
  725. "02F40E7E2221F295"
  726. "DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA5933"
  727. "2BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
  728. "0303001D34B85629"
  729. "6C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293"
  730. "CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
  731. "037BF27342DA639B"
  732. "6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A57"
  733. "6291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
  734. 1,
  735. "03FFFFFFFFFFFFFF"
  736. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18"
  737. "FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
  738. "2", 571
  739. }
  740. };
  741. static int char2_curve_test(int n)
  742. {
  743. int r = 0;
  744. BN_CTX *ctx = NULL;
  745. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  746. BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;
  747. EC_GROUP *group = NULL, *variable = NULL;
  748. EC_POINT *P = NULL, *Q = NULL, *R = NULL;
  749. const EC_POINT *points[3];
  750. const BIGNUM *scalars[3];
  751. struct c2_curve_test *const test = char2_curve_tests + n;
  752. if (!TEST_ptr(ctx = BN_CTX_new())
  753. || !TEST_ptr(p = BN_new())
  754. || !TEST_ptr(a = BN_new())
  755. || !TEST_ptr(b = BN_new())
  756. || !TEST_ptr(x = BN_new())
  757. || !TEST_ptr(y = BN_new())
  758. || !TEST_ptr(z = BN_new())
  759. || !TEST_ptr(yplusone = BN_new())
  760. || !TEST_true(BN_hex2bn(&p, test->p))
  761. || !TEST_true(BN_hex2bn(&a, test->a))
  762. || !TEST_true(BN_hex2bn(&b, test->b))
  763. || !TEST_true(group = EC_GROUP_new(EC_GF2m_simple_method()))
  764. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  765. || !TEST_ptr(P = EC_POINT_new(group))
  766. || !TEST_ptr(Q = EC_POINT_new(group))
  767. || !TEST_ptr(R = EC_POINT_new(group))
  768. || !TEST_true(BN_hex2bn(&x, test->x))
  769. || !TEST_true(BN_hex2bn(&y, test->y))
  770. || !TEST_true(BN_add(yplusone, y, BN_value_one())))
  771. goto err;
  772. /* Change test based on whether binary point compression is enabled or not. */
  773. # ifdef OPENSSL_EC_BIN_PT_COMP
  774. /*
  775. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  776. * and therefore setting the coordinates should fail.
  777. */
  778. if (!TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx))
  779. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x,
  780. test->y_bit,
  781. ctx))
  782. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  783. || !TEST_true(BN_hex2bn(&z, test->order))
  784. || !TEST_true(BN_hex2bn(&cof, test->cof))
  785. || !TEST_true(EC_GROUP_set_generator(group, P, z, cof))
  786. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  787. goto err;
  788. TEST_info("%s -- Generator", test->name);
  789. test_output_bignum("x", x);
  790. test_output_bignum("y", y);
  791. /* G_y value taken from the standard: */
  792. if (!TEST_true(BN_hex2bn(&z, test->y))
  793. || !TEST_BN_eq(y, z))
  794. goto err;
  795. # else
  796. /*
  797. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  798. * and therefore setting the coordinates should fail.
  799. */
  800. if (!TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx))
  801. || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
  802. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  803. || !TEST_true(BN_hex2bn(&z, test->order))
  804. || !TEST_true(BN_hex2bn(&cof, test->cof))
  805. || !TEST_true(EC_GROUP_set_generator(group, P, z, cof)))
  806. goto err;
  807. TEST_info("%s -- Generator:", test->name);
  808. test_output_bignum("x", x);
  809. test_output_bignum("y", y);
  810. # endif
  811. if (!TEST_int_eq(EC_GROUP_get_degree(group), test->degree)
  812. || !group_order_tests(group)
  813. || !TEST_ptr(variable = EC_GROUP_new(EC_GROUP_method_of(group)))
  814. || !TEST_true(EC_GROUP_copy(variable, group)))
  815. goto err;
  816. /* more tests using the last curve */
  817. if (n == OSSL_NELEM(char2_curve_tests) - 1) {
  818. if (!TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
  819. || !TEST_true(EC_POINT_copy(Q, P))
  820. || !TEST_false(EC_POINT_is_at_infinity(group, Q))
  821. || !TEST_true(EC_POINT_dbl(group, P, P, ctx))
  822. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  823. || !TEST_true(EC_POINT_invert(group, Q, ctx)) /* P = -2Q */
  824. || !TEST_true(EC_POINT_add(group, R, P, Q, ctx))
  825. || !TEST_true(EC_POINT_add(group, R, R, Q, ctx))
  826. || !TEST_true(EC_POINT_is_at_infinity(group, R)) /* R = P + 2Q */
  827. || !TEST_false(EC_POINT_is_at_infinity(group, Q)))
  828. goto err;
  829. points[0] = Q;
  830. points[1] = Q;
  831. points[2] = Q;
  832. if (!TEST_true(BN_add(y, z, BN_value_one()))
  833. || !TEST_BN_even(y)
  834. || !TEST_true(BN_rshift1(y, y)))
  835. goto err;
  836. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  837. scalars[1] = y;
  838. TEST_note("combined multiplication ...");
  839. /* z is still the group order */
  840. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  841. || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
  842. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
  843. || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx)))
  844. goto err;
  845. if (!TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))
  846. || !TEST_true(BN_add(z, z, y)))
  847. goto err;
  848. BN_set_negative(z, 1);
  849. scalars[0] = y;
  850. scalars[1] = z; /* z = -(order + y) */
  851. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  852. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  853. goto err;
  854. if (!TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))
  855. || !TEST_true(BN_add(z, x, y)))
  856. goto err;
  857. BN_set_negative(z, 1);
  858. scalars[0] = x;
  859. scalars[1] = y;
  860. scalars[2] = z; /* z = -(x+y) */
  861. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx))
  862. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  863. goto err;;
  864. }
  865. r = 1;
  866. err:
  867. BN_CTX_free(ctx);
  868. BN_free(p);
  869. BN_free(a);
  870. BN_free(b);
  871. BN_free(x);
  872. BN_free(y);
  873. BN_free(z);
  874. BN_free(yplusone);
  875. BN_free(cof);
  876. EC_POINT_free(P);
  877. EC_POINT_free(Q);
  878. EC_POINT_free(R);
  879. EC_GROUP_free(group);
  880. EC_GROUP_free(variable);
  881. return r;
  882. }
  883. static int char2_field_tests(void)
  884. {
  885. BN_CTX *ctx = NULL;
  886. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  887. EC_GROUP *group = NULL, *tmp = NULL;
  888. EC_POINT *P = NULL, *Q = NULL, *R = NULL;
  889. BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;
  890. unsigned char buf[100];
  891. size_t len;
  892. int k, r = 0;
  893. if (!TEST_ptr(ctx = BN_CTX_new())
  894. || !TEST_ptr(p = BN_new())
  895. || !TEST_ptr(a = BN_new())
  896. || !TEST_ptr(b = BN_new())
  897. || !TEST_true(BN_hex2bn(&p, "13"))
  898. || !TEST_true(BN_hex2bn(&a, "3"))
  899. || !TEST_true(BN_hex2bn(&b, "1")))
  900. goto err;
  901. group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use
  902. * EC_GROUP_new_curve_GF2m
  903. * so that the library gets
  904. * to choose the EC_METHOD */
  905. if (!TEST_ptr(group)
  906. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  907. || !TEST_ptr(tmp = EC_GROUP_new(EC_GROUP_method_of(group)))
  908. || !TEST_true(EC_GROUP_copy(tmp, group)))
  909. goto err;
  910. EC_GROUP_free(group);
  911. group = tmp;
  912. tmp = NULL;
  913. if (!TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx)))
  914. goto err;
  915. TEST_info("Curve defined by Weierstrass equation");
  916. TEST_note(" y^2 + x*y = x^3 + a*x^2 + b (mod p)");
  917. test_output_bignum("a", a);
  918. test_output_bignum("b", b);
  919. test_output_bignum("p", p);
  920. if (!TEST_ptr(P = EC_POINT_new(group))
  921. || !TEST_ptr(Q = EC_POINT_new(group))
  922. || !TEST_ptr(R = EC_POINT_new(group))
  923. || !TEST_true(EC_POINT_set_to_infinity(group, P))
  924. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  925. goto err;
  926. buf[0] = 0;
  927. if (!TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx))
  928. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))
  929. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  930. || !TEST_ptr(x = BN_new())
  931. || !TEST_ptr(y = BN_new())
  932. || !TEST_ptr(z = BN_new())
  933. || !TEST_ptr(cof = BN_new())
  934. || !TEST_ptr(yplusone = BN_new())
  935. || !TEST_true(BN_hex2bn(&x, "6"))
  936. /* Change test based on whether binary point compression is enabled or not. */
  937. # ifdef OPENSSL_EC_BIN_PT_COMP
  938. || !TEST_true(EC_POINT_set_compressed_coordinates(group, Q, x, 1, ctx))
  939. # else
  940. || !TEST_true(BN_hex2bn(&y, "8"))
  941. || !TEST_true(EC_POINT_set_affine_coordinates(group, Q, x, y, ctx))
  942. # endif
  943. )
  944. goto err;
  945. if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) {
  946. /* Change test based on whether binary point compression is enabled or not. */
  947. # ifdef OPENSSL_EC_BIN_PT_COMP
  948. if (!TEST_true(EC_POINT_get_affine_coordinates(group, Q, x, y, ctx)))
  949. goto err;
  950. # endif
  951. TEST_info("Point is not on curve");
  952. test_output_bignum("x", x);
  953. test_output_bignum("y", y);
  954. goto err;
  955. }
  956. TEST_note("A cyclic subgroup:");
  957. k = 100;
  958. do {
  959. if (!TEST_int_ne(k--, 0))
  960. goto err;
  961. if (EC_POINT_is_at_infinity(group, P))
  962. TEST_note(" point at infinity");
  963. else {
  964. if (!TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y,
  965. ctx)))
  966. goto err;
  967. test_output_bignum("x", x);
  968. test_output_bignum("y", y);
  969. }
  970. if (!TEST_true(EC_POINT_copy(R, P))
  971. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)))
  972. goto err;
  973. }
  974. while (!EC_POINT_is_at_infinity(group, P));
  975. if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx))
  976. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  977. goto err;
  978. /* Change test based on whether binary point compression is enabled or not. */
  979. # ifdef OPENSSL_EC_BIN_PT_COMP
  980. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED,
  981. buf, sizeof(buf), ctx);
  982. if (!TEST_size_t_ne(len, 0)
  983. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  984. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  985. goto err;
  986. test_output_memory("Generator as octet string, compressed form:",
  987. buf, len);
  988. # endif
  989. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
  990. buf, sizeof(buf), ctx);
  991. if (!TEST_size_t_ne(len, 0)
  992. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  993. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  994. goto err;
  995. test_output_memory("Generator as octet string, uncompressed form:",
  996. buf, len);
  997. /* Change test based on whether binary point compression is enabled or not. */
  998. # ifdef OPENSSL_EC_BIN_PT_COMP
  999. len =
  1000. EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof(buf),
  1001. ctx);
  1002. if (!TEST_size_t_ne(len, 0)
  1003. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  1004. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  1005. goto err;
  1006. test_output_memory("Generator as octet string, hybrid form:",
  1007. buf, len);
  1008. # endif
  1009. if (!TEST_true(EC_POINT_invert(group, P, ctx))
  1010. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx)))
  1011. goto err;
  1012. TEST_note("\n");
  1013. r = 1;
  1014. err:
  1015. BN_CTX_free(ctx);
  1016. BN_free(p);
  1017. BN_free(a);
  1018. BN_free(b);
  1019. EC_GROUP_free(group);
  1020. EC_GROUP_free(tmp);
  1021. EC_POINT_free(P);
  1022. EC_POINT_free(Q);
  1023. EC_POINT_free(R);
  1024. BN_free(x);
  1025. BN_free(y);
  1026. BN_free(z);
  1027. BN_free(cof);
  1028. BN_free(yplusone);
  1029. return r;
  1030. }
  1031. # endif
  1032. static int internal_curve_test(int n)
  1033. {
  1034. EC_GROUP *group = NULL;
  1035. int nid = curves[n].nid;
  1036. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) {
  1037. TEST_info("EC_GROUP_new_curve_name() failed with curve %s\n",
  1038. OBJ_nid2sn(nid));
  1039. return 0;
  1040. }
  1041. if (!TEST_true(EC_GROUP_check(group, NULL))) {
  1042. TEST_info("EC_GROUP_check() failed with curve %s\n", OBJ_nid2sn(nid));
  1043. EC_GROUP_free(group);
  1044. return 0;
  1045. }
  1046. EC_GROUP_free(group);
  1047. return 1;
  1048. }
  1049. static int internal_curve_test_method(int n)
  1050. {
  1051. int r, nid = curves[n].nid;
  1052. EC_GROUP *group;
  1053. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) {
  1054. TEST_info("Curve %s failed\n", OBJ_nid2sn(nid));
  1055. return 0;
  1056. }
  1057. r = group_order_tests(group);
  1058. EC_GROUP_free(group);
  1059. return r;
  1060. }
  1061. static int group_field_test(void)
  1062. {
  1063. int r = 1;
  1064. BIGNUM *secp521r1_field = NULL;
  1065. BIGNUM *sect163r2_field = NULL;
  1066. EC_GROUP *secp521r1_group = NULL;
  1067. EC_GROUP *sect163r2_group = NULL;
  1068. BN_hex2bn(&secp521r1_field,
  1069. "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  1070. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  1071. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  1072. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  1073. "FFFF");
  1074. BN_hex2bn(&sect163r2_field,
  1075. "08000000000000000000000000000000"
  1076. "00000000C9");
  1077. secp521r1_group = EC_GROUP_new_by_curve_name(NID_secp521r1);
  1078. if (BN_cmp(secp521r1_field, EC_GROUP_get0_field(secp521r1_group)))
  1079. r = 0;
  1080. # ifndef OPENSSL_NO_EC2M
  1081. sect163r2_group = EC_GROUP_new_by_curve_name(NID_sect163r2);
  1082. if (BN_cmp(sect163r2_field, EC_GROUP_get0_field(sect163r2_group)))
  1083. r = 0;
  1084. # endif
  1085. EC_GROUP_free(secp521r1_group);
  1086. EC_GROUP_free(sect163r2_group);
  1087. BN_free(secp521r1_field);
  1088. BN_free(sect163r2_field);
  1089. return r;
  1090. }
  1091. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1092. /*
  1093. * nistp_test_params contains magic numbers for testing our optimized
  1094. * implementations of several NIST curves with characteristic > 3.
  1095. */
  1096. struct nistp_test_params {
  1097. const EC_METHOD *(*meth) (void);
  1098. int degree;
  1099. /*
  1100. * Qx, Qy and D are taken from
  1101. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
  1102. * Otherwise, values are standard curve parameters from FIPS 180-3
  1103. */
  1104. const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d;
  1105. };
  1106. static const struct nistp_test_params nistp_tests_params[] = {
  1107. {
  1108. /* P-224 */
  1109. EC_GFp_nistp224_method,
  1110. 224,
  1111. /* p */
  1112. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
  1113. /* a */
  1114. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
  1115. /* b */
  1116. "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
  1117. /* Qx */
  1118. "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E",
  1119. /* Qy */
  1120. "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555",
  1121. /* Gx */
  1122. "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
  1123. /* Gy */
  1124. "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
  1125. /* order */
  1126. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
  1127. /* d */
  1128. "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8",
  1129. },
  1130. {
  1131. /* P-256 */
  1132. EC_GFp_nistp256_method,
  1133. 256,
  1134. /* p */
  1135. "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
  1136. /* a */
  1137. "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
  1138. /* b */
  1139. "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
  1140. /* Qx */
  1141. "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19",
  1142. /* Qy */
  1143. "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09",
  1144. /* Gx */
  1145. "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
  1146. /* Gy */
  1147. "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
  1148. /* order */
  1149. "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
  1150. /* d */
  1151. "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96",
  1152. },
  1153. {
  1154. /* P-521 */
  1155. EC_GFp_nistp521_method,
  1156. 521,
  1157. /* p */
  1158. "1ff"
  1159. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  1160. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  1161. /* a */
  1162. "1ff"
  1163. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  1164. "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc",
  1165. /* b */
  1166. "051"
  1167. "953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e1"
  1168. "56193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
  1169. /* Qx */
  1170. "0098"
  1171. "e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e"
  1172. "59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4",
  1173. /* Qy */
  1174. "0164"
  1175. "350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8"
  1176. "554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e",
  1177. /* Gx */
  1178. "c6"
  1179. "858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dba"
  1180. "a14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
  1181. /* Gy */
  1182. "118"
  1183. "39296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c"
  1184. "97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
  1185. /* order */
  1186. "1ff"
  1187. "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa"
  1188. "51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
  1189. /* d */
  1190. "0100"
  1191. "085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eee"
  1192. "df09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722",
  1193. },
  1194. };
  1195. static int nistp_single_test(int idx)
  1196. {
  1197. const struct nistp_test_params *test = nistp_tests_params + idx;
  1198. BN_CTX *ctx = NULL;
  1199. BIGNUM *p = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL;
  1200. BIGNUM *n = NULL, *m = NULL, *order = NULL, *yplusone = NULL;
  1201. EC_GROUP *NISTP = NULL;
  1202. EC_POINT *G = NULL, *P = NULL, *Q = NULL, *Q_CHECK = NULL;
  1203. int r = 0;
  1204. TEST_note("NIST curve P-%d (optimised implementation):",
  1205. test->degree);
  1206. if (!TEST_ptr(ctx = BN_CTX_new())
  1207. || !TEST_ptr(p = BN_new())
  1208. || !TEST_ptr(a = BN_new())
  1209. || !TEST_ptr(b = BN_new())
  1210. || !TEST_ptr(x = BN_new())
  1211. || !TEST_ptr(y = BN_new())
  1212. || !TEST_ptr(m = BN_new())
  1213. || !TEST_ptr(n = BN_new())
  1214. || !TEST_ptr(order = BN_new())
  1215. || !TEST_ptr(yplusone = BN_new())
  1216. || !TEST_ptr(NISTP = EC_GROUP_new(test->meth()))
  1217. || !TEST_true(BN_hex2bn(&p, test->p))
  1218. || !TEST_int_eq(1, BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  1219. || !TEST_true(BN_hex2bn(&a, test->a))
  1220. || !TEST_true(BN_hex2bn(&b, test->b))
  1221. || !TEST_true(EC_GROUP_set_curve(NISTP, p, a, b, ctx))
  1222. || !TEST_ptr(G = EC_POINT_new(NISTP))
  1223. || !TEST_ptr(P = EC_POINT_new(NISTP))
  1224. || !TEST_ptr(Q = EC_POINT_new(NISTP))
  1225. || !TEST_ptr(Q_CHECK = EC_POINT_new(NISTP))
  1226. || !TEST_true(BN_hex2bn(&x, test->Qx))
  1227. || !TEST_true(BN_hex2bn(&y, test->Qy))
  1228. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  1229. /*
  1230. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  1231. * and therefore setting the coordinates should fail.
  1232. */
  1233. || !TEST_false(EC_POINT_set_affine_coordinates(NISTP, Q_CHECK, x,
  1234. yplusone, ctx))
  1235. || !TEST_true(EC_POINT_set_affine_coordinates(NISTP, Q_CHECK, x, y,
  1236. ctx))
  1237. || !TEST_true(BN_hex2bn(&x, test->Gx))
  1238. || !TEST_true(BN_hex2bn(&y, test->Gy))
  1239. || !TEST_true(EC_POINT_set_affine_coordinates(NISTP, G, x, y, ctx))
  1240. || !TEST_true(BN_hex2bn(&order, test->order))
  1241. || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))
  1242. || !TEST_int_eq(EC_GROUP_get_degree(NISTP), test->degree))
  1243. goto err;
  1244. TEST_note("NIST test vectors ... ");
  1245. if (!TEST_true(BN_hex2bn(&n, test->d)))
  1246. goto err;
  1247. /* fixed point multiplication */
  1248. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1249. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1250. goto err;
  1251. /* random point multiplication */
  1252. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1253. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1254. /* set generator to P = 2*G, where G is the standard generator */
  1255. || !TEST_true(EC_POINT_dbl(NISTP, P, G, ctx))
  1256. || !TEST_true(EC_GROUP_set_generator(NISTP, P, order, BN_value_one()))
  1257. /* set the scalar to m=n/2, where n is the NIST test scalar */
  1258. || !TEST_true(BN_rshift(m, n, 1)))
  1259. goto err;
  1260. /* test the non-standard generator */
  1261. /* fixed point multiplication */
  1262. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1263. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1264. goto err;
  1265. /* random point multiplication */
  1266. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1267. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1268. /*
  1269. * We have not performed precomputation so have_precompute mult should be
  1270. * false
  1271. */
  1272. || !TEST_false(EC_GROUP_have_precompute_mult(NISTP))
  1273. /* now repeat all tests with precomputation */
  1274. || !TEST_true(EC_GROUP_precompute_mult(NISTP, ctx))
  1275. || !TEST_true(EC_GROUP_have_precompute_mult(NISTP)))
  1276. goto err;
  1277. /* fixed point multiplication */
  1278. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1279. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1280. goto err;
  1281. /* random point multiplication */
  1282. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1283. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1284. /* reset generator */
  1285. || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one())))
  1286. goto err;
  1287. /* fixed point multiplication */
  1288. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1289. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1290. goto err;
  1291. /* random point multiplication */
  1292. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1293. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1294. goto err;
  1295. /* regression test for felem_neg bug */
  1296. if (!TEST_true(BN_set_word(m, 32))
  1297. || !TEST_true(BN_set_word(n, 31))
  1298. || !TEST_true(EC_POINT_copy(P, G))
  1299. || !TEST_true(EC_POINT_invert(NISTP, P, ctx))
  1300. || !TEST_true(EC_POINT_mul(NISTP, Q, m, P, n, ctx))
  1301. || !TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, G, ctx)))
  1302. goto err;
  1303. r = group_order_tests(NISTP);
  1304. err:
  1305. EC_GROUP_free(NISTP);
  1306. EC_POINT_free(G);
  1307. EC_POINT_free(P);
  1308. EC_POINT_free(Q);
  1309. EC_POINT_free(Q_CHECK);
  1310. BN_free(n);
  1311. BN_free(m);
  1312. BN_free(p);
  1313. BN_free(a);
  1314. BN_free(b);
  1315. BN_free(x);
  1316. BN_free(y);
  1317. BN_free(order);
  1318. BN_free(yplusone);
  1319. BN_CTX_free(ctx);
  1320. return r;
  1321. }
  1322. /*
  1323. * Tests a point known to cause an incorrect underflow in an old version of
  1324. * ecp_nist521.c
  1325. */
  1326. static int underflow_test(void)
  1327. {
  1328. BN_CTX *ctx = NULL;
  1329. EC_GROUP *grp = NULL;
  1330. EC_POINT *P = NULL, *Q = NULL, *R = NULL;
  1331. BIGNUM *x1 = NULL, *y1 = NULL, *z1 = NULL, *x2 = NULL, *y2 = NULL;
  1332. BIGNUM *k = NULL;
  1333. int testresult = 0;
  1334. const char *x1str =
  1335. "1534f0077fffffe87e9adcfe000000000000000000003e05a21d2400002e031b1f4"
  1336. "b80000c6fafa4f3c1288798d624a247b5e2ffffffffffffffefe099241900004";
  1337. const char *p521m1 =
  1338. "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  1339. "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffe";
  1340. ctx = BN_CTX_new();
  1341. if (!TEST_ptr(ctx))
  1342. return 0;
  1343. BN_CTX_start(ctx);
  1344. x1 = BN_CTX_get(ctx);
  1345. y1 = BN_CTX_get(ctx);
  1346. z1 = BN_CTX_get(ctx);
  1347. x2 = BN_CTX_get(ctx);
  1348. y2 = BN_CTX_get(ctx);
  1349. k = BN_CTX_get(ctx);
  1350. if (!TEST_ptr(k))
  1351. goto err;
  1352. grp = EC_GROUP_new_by_curve_name(NID_secp521r1);
  1353. P = EC_POINT_new(grp);
  1354. Q = EC_POINT_new(grp);
  1355. R = EC_POINT_new(grp);
  1356. if (!TEST_ptr(grp) || !TEST_ptr(P) || !TEST_ptr(Q) || !TEST_ptr(R))
  1357. goto err;
  1358. if (!TEST_int_gt(BN_hex2bn(&x1, x1str), 0)
  1359. || !TEST_int_gt(BN_hex2bn(&y1, p521m1), 0)
  1360. || !TEST_int_gt(BN_hex2bn(&z1, p521m1), 0)
  1361. || !TEST_int_gt(BN_hex2bn(&k, "02"), 0)
  1362. || !TEST_true(EC_POINT_set_Jprojective_coordinates_GFp(grp, P, x1,
  1363. y1, z1, ctx))
  1364. || !TEST_true(EC_POINT_mul(grp, Q, NULL, P, k, ctx))
  1365. || !TEST_true(EC_POINT_get_affine_coordinates(grp, Q, x1, y1, ctx))
  1366. || !TEST_true(EC_POINT_dbl(grp, R, P, ctx))
  1367. || !TEST_true(EC_POINT_get_affine_coordinates(grp, R, x2, y2, ctx)))
  1368. goto err;
  1369. if (!TEST_int_eq(BN_cmp(x1, x2), 0)
  1370. || !TEST_int_eq(BN_cmp(y1, y2), 0))
  1371. goto err;
  1372. testresult = 1;
  1373. err:
  1374. BN_CTX_end(ctx);
  1375. EC_POINT_free(P);
  1376. EC_POINT_free(Q);
  1377. EC_POINT_free(R);
  1378. EC_GROUP_free(grp);
  1379. BN_CTX_free(ctx);
  1380. return testresult;
  1381. }
  1382. # endif
  1383. static const unsigned char p521_named[] = {
  1384. 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23,
  1385. };
  1386. static const unsigned char p521_explicit[] = {
  1387. 0x30, 0x82, 0x01, 0xc3, 0x02, 0x01, 0x01, 0x30, 0x4d, 0x06, 0x07, 0x2a,
  1388. 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x42, 0x01, 0xff, 0xff, 0xff,
  1389. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1390. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1391. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1392. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1393. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1394. 0xff, 0xff, 0x30, 0x81, 0x9f, 0x04, 0x42, 0x01, 0xff, 0xff, 0xff, 0xff,
  1395. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1396. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1397. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1398. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1399. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1400. 0xfc, 0x04, 0x42, 0x00, 0x51, 0x95, 0x3e, 0xb9, 0x61, 0x8e, 0x1c, 0x9a,
  1401. 0x1f, 0x92, 0x9a, 0x21, 0xa0, 0xb6, 0x85, 0x40, 0xee, 0xa2, 0xda, 0x72,
  1402. 0x5b, 0x99, 0xb3, 0x15, 0xf3, 0xb8, 0xb4, 0x89, 0x91, 0x8e, 0xf1, 0x09,
  1403. 0xe1, 0x56, 0x19, 0x39, 0x51, 0xec, 0x7e, 0x93, 0x7b, 0x16, 0x52, 0xc0,
  1404. 0xbd, 0x3b, 0xb1, 0xbf, 0x07, 0x35, 0x73, 0xdf, 0x88, 0x3d, 0x2c, 0x34,
  1405. 0xf1, 0xef, 0x45, 0x1f, 0xd4, 0x6b, 0x50, 0x3f, 0x00, 0x03, 0x15, 0x00,
  1406. 0xd0, 0x9e, 0x88, 0x00, 0x29, 0x1c, 0xb8, 0x53, 0x96, 0xcc, 0x67, 0x17,
  1407. 0x39, 0x32, 0x84, 0xaa, 0xa0, 0xda, 0x64, 0xba, 0x04, 0x81, 0x85, 0x04,
  1408. 0x00, 0xc6, 0x85, 0x8e, 0x06, 0xb7, 0x04, 0x04, 0xe9, 0xcd, 0x9e, 0x3e,
  1409. 0xcb, 0x66, 0x23, 0x95, 0xb4, 0x42, 0x9c, 0x64, 0x81, 0x39, 0x05, 0x3f,
  1410. 0xb5, 0x21, 0xf8, 0x28, 0xaf, 0x60, 0x6b, 0x4d, 0x3d, 0xba, 0xa1, 0x4b,
  1411. 0x5e, 0x77, 0xef, 0xe7, 0x59, 0x28, 0xfe, 0x1d, 0xc1, 0x27, 0xa2, 0xff,
  1412. 0xa8, 0xde, 0x33, 0x48, 0xb3, 0xc1, 0x85, 0x6a, 0x42, 0x9b, 0xf9, 0x7e,
  1413. 0x7e, 0x31, 0xc2, 0xe5, 0xbd, 0x66, 0x01, 0x18, 0x39, 0x29, 0x6a, 0x78,
  1414. 0x9a, 0x3b, 0xc0, 0x04, 0x5c, 0x8a, 0x5f, 0xb4, 0x2c, 0x7d, 0x1b, 0xd9,
  1415. 0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b, 0x44, 0x68, 0x17, 0xaf, 0xbd, 0x17,
  1416. 0x27, 0x3e, 0x66, 0x2c, 0x97, 0xee, 0x72, 0x99, 0x5e, 0xf4, 0x26, 0x40,
  1417. 0xc5, 0x50, 0xb9, 0x01, 0x3f, 0xad, 0x07, 0x61, 0x35, 0x3c, 0x70, 0x86,
  1418. 0xa2, 0x72, 0xc2, 0x40, 0x88, 0xbe, 0x94, 0x76, 0x9f, 0xd1, 0x66, 0x50,
  1419. 0x02, 0x42, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1420. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1421. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa,
  1422. 0x51, 0x86, 0x87, 0x83, 0xbf, 0x2f, 0x96, 0x6b, 0x7f, 0xcc, 0x01, 0x48,
  1423. 0xf7, 0x09, 0xa5, 0xd0, 0x3b, 0xb5, 0xc9, 0xb8, 0x89, 0x9c, 0x47, 0xae,
  1424. 0xbb, 0x6f, 0xb7, 0x1e, 0x91, 0x38, 0x64, 0x09, 0x02, 0x01, 0x01,
  1425. };
  1426. /*
  1427. * This test validates a named curve's group parameters using
  1428. * EC_GROUP_check_named_curve(). It also checks that modifying any of the
  1429. * group parameters results in the curve not being valid.
  1430. */
  1431. static int check_named_curve_test(int id)
  1432. {
  1433. int ret = 0, nid, field_nid, has_seed;
  1434. EC_GROUP *group = NULL, *gtest = NULL;
  1435. const EC_POINT *group_gen = NULL;
  1436. EC_POINT *other_gen = NULL;
  1437. BIGNUM *group_p = NULL, *group_a = NULL, *group_b = NULL;
  1438. BIGNUM *other_p = NULL, *other_a = NULL, *other_b = NULL;
  1439. BIGNUM *group_cofactor = NULL, *other_cofactor = NULL;
  1440. BIGNUM *other_order = NULL;
  1441. const BIGNUM *group_order = NULL;
  1442. BN_CTX *bn_ctx = NULL;
  1443. static const unsigned char invalid_seed[] = "THIS IS NOT A VALID SEED";
  1444. static size_t invalid_seed_len = sizeof(invalid_seed);
  1445. /* Do some setup */
  1446. nid = curves[id].nid;
  1447. if (!TEST_ptr(bn_ctx = BN_CTX_new())
  1448. || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
  1449. || !TEST_ptr(gtest = EC_GROUP_dup(group))
  1450. || !TEST_ptr(group_p = BN_new())
  1451. || !TEST_ptr(group_a = BN_new())
  1452. || !TEST_ptr(group_b = BN_new())
  1453. || !TEST_ptr(group_cofactor = BN_new())
  1454. || !TEST_ptr(group_gen = EC_GROUP_get0_generator(group))
  1455. || !TEST_ptr(group_order = EC_GROUP_get0_order(group))
  1456. || !TEST_true(EC_GROUP_get_cofactor(group, group_cofactor, NULL))
  1457. || !TEST_true(EC_GROUP_get_curve(group, group_p, group_a, group_b, NULL))
  1458. || !TEST_ptr(other_gen = EC_POINT_dup(group_gen, group))
  1459. || !TEST_true(EC_POINT_add(group, other_gen, group_gen, group_gen, NULL))
  1460. || !TEST_ptr(other_order = BN_dup(group_order))
  1461. || !TEST_true(BN_add_word(other_order, 1))
  1462. || !TEST_ptr(other_a = BN_dup(group_a))
  1463. || !TEST_true(BN_add_word(other_a, 1))
  1464. || !TEST_ptr(other_b = BN_dup(group_b))
  1465. || !TEST_true(BN_add_word(other_b, 1))
  1466. || !TEST_ptr(other_cofactor = BN_dup(group_cofactor))
  1467. || !TEST_true(BN_add_word(other_cofactor, 1)))
  1468. goto err;
  1469. /* Determine if the built-in curve has a seed field set */
  1470. has_seed = (EC_GROUP_get_seed_len(group) > 0);
  1471. field_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group));
  1472. if (field_nid == NID_X9_62_characteristic_two_field) {
  1473. if (!TEST_ptr(other_p = BN_dup(group_p))
  1474. || !TEST_true(BN_lshift1(other_p, other_p)))
  1475. goto err;
  1476. } else {
  1477. if (!TEST_ptr(other_p = BN_dup(group_p)))
  1478. goto err;
  1479. /*
  1480. * Just choosing any arbitrary prime does not work..
  1481. * Setting p via ec_GFp_nist_group_set_curve() needs the prime to be a
  1482. * nist prime. So only select one of these as an alternate prime.
  1483. */
  1484. if (!TEST_ptr(BN_copy(other_p,
  1485. BN_ucmp(BN_get0_nist_prime_192(), other_p) == 0 ?
  1486. BN_get0_nist_prime_256() :
  1487. BN_get0_nist_prime_192())))
  1488. goto err;
  1489. }
  1490. /* Passes because this is a valid curve */
  1491. if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid)
  1492. /* Only NIST curves pass */
  1493. || !TEST_int_eq(EC_GROUP_check_named_curve(group, 1, NULL),
  1494. EC_curve_nid2nist(nid) != NULL ? nid : NID_undef))
  1495. goto err;
  1496. /* Fail if the curve name doesn't match the parameters */
  1497. EC_GROUP_set_curve_name(group, nid + 1);
  1498. ERR_set_mark();
  1499. if (!TEST_int_le(EC_GROUP_check_named_curve(group, 0, NULL), 0))
  1500. goto err;
  1501. ERR_pop_to_mark();
  1502. /* Restore curve name and ensure it's passing */
  1503. EC_GROUP_set_curve_name(group, nid);
  1504. if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid))
  1505. goto err;
  1506. if (!TEST_int_eq(EC_GROUP_set_seed(group, invalid_seed, invalid_seed_len),
  1507. invalid_seed_len))
  1508. goto err;
  1509. if (has_seed) {
  1510. /*
  1511. * If the built-in curve has a seed and we set the seed to another value
  1512. * then it will fail the check.
  1513. */
  1514. if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), 0))
  1515. goto err;
  1516. } else {
  1517. /*
  1518. * If the built-in curve does not have a seed then setting the seed will
  1519. * pass the check (as the seed is optional).
  1520. */
  1521. if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid))
  1522. goto err;
  1523. }
  1524. /* Pass if the seed is unknown (as it is optional) */
  1525. if (!TEST_int_eq(EC_GROUP_set_seed(group, NULL, 0), 1)
  1526. || !TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid))
  1527. goto err;
  1528. /* Check that a duped group passes */
  1529. if (!TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid))
  1530. goto err;
  1531. /* check that changing any generator parameter fails */
  1532. if (!TEST_true(EC_GROUP_set_generator(gtest, other_gen, group_order,
  1533. group_cofactor))
  1534. || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), 0)
  1535. || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, other_order,
  1536. group_cofactor))
  1537. || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), 0)
  1538. /* The order is not an optional field, so this should fail */
  1539. || !TEST_false(EC_GROUP_set_generator(gtest, group_gen, NULL,
  1540. group_cofactor))
  1541. || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, group_order,
  1542. other_cofactor))
  1543. || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), 0)
  1544. /* Check that if the cofactor is not set then it still passes */
  1545. || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, group_order,
  1546. NULL))
  1547. || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid)
  1548. /* check that restoring the generator passes */
  1549. || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, group_order,
  1550. group_cofactor))
  1551. || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid))
  1552. goto err;
  1553. /*
  1554. * check that changing any curve parameter fails
  1555. *
  1556. * Setting arbitrary p, a or b might fail for some EC_GROUPs
  1557. * depending on the internal EC_METHOD implementation, hence run
  1558. * these tests conditionally to the success of EC_GROUP_set_curve().
  1559. */
  1560. ERR_set_mark();
  1561. if (EC_GROUP_set_curve(gtest, other_p, group_a, group_b, NULL)) {
  1562. if (!TEST_int_le(EC_GROUP_check_named_curve(gtest, 0, NULL), 0))
  1563. goto err;
  1564. } else {
  1565. /* clear the error stack if EC_GROUP_set_curve() failed */
  1566. ERR_pop_to_mark();
  1567. ERR_set_mark();
  1568. }
  1569. if (EC_GROUP_set_curve(gtest, group_p, other_a, group_b, NULL)) {
  1570. if (!TEST_int_le(EC_GROUP_check_named_curve(gtest, 0, NULL), 0))
  1571. goto err;
  1572. } else {
  1573. /* clear the error stack if EC_GROUP_set_curve() failed */
  1574. ERR_pop_to_mark();
  1575. ERR_set_mark();
  1576. }
  1577. if (EC_GROUP_set_curve(gtest, group_p, group_a, other_b, NULL)) {
  1578. if (!TEST_int_le(EC_GROUP_check_named_curve(gtest, 0, NULL), 0))
  1579. goto err;
  1580. } else {
  1581. /* clear the error stack if EC_GROUP_set_curve() failed */
  1582. ERR_pop_to_mark();
  1583. ERR_set_mark();
  1584. }
  1585. ERR_pop_to_mark();
  1586. /* Check that restoring the curve parameters passes */
  1587. if (!TEST_true(EC_GROUP_set_curve(gtest, group_p, group_a, group_b, NULL))
  1588. || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid))
  1589. goto err;
  1590. ret = 1;
  1591. err:
  1592. BN_free(group_p);
  1593. BN_free(other_p);
  1594. BN_free(group_a);
  1595. BN_free(other_a);
  1596. BN_free(group_b);
  1597. BN_free(other_b);
  1598. BN_free(group_cofactor);
  1599. BN_free(other_cofactor);
  1600. BN_free(other_order);
  1601. EC_POINT_free(other_gen);
  1602. EC_GROUP_free(gtest);
  1603. EC_GROUP_free(group);
  1604. BN_CTX_free(bn_ctx);
  1605. return ret;
  1606. }
  1607. /*
  1608. * This checks the lookup capability of EC_GROUP_check_named_curve()
  1609. * when the given group was created with explicit parameters.
  1610. *
  1611. * It is possible to retrieve an alternative alias that does not match
  1612. * the original nid in this case.
  1613. */
  1614. static int check_named_curve_lookup_test(int id)
  1615. {
  1616. int ret = 0, nid, rv = 0;
  1617. EC_GROUP *g = NULL , *ga = NULL;
  1618. ECPARAMETERS *p = NULL, *pa = NULL;
  1619. BN_CTX *ctx = NULL;
  1620. /* Do some setup */
  1621. nid = curves[id].nid;
  1622. if (!TEST_ptr(ctx = BN_CTX_new())
  1623. || !TEST_ptr(g = EC_GROUP_new_by_curve_name(nid))
  1624. || !TEST_ptr(p = EC_GROUP_get_ecparameters(g, NULL)))
  1625. goto err;
  1626. /* replace with group from explicit parameters */
  1627. EC_GROUP_free(g);
  1628. if (!TEST_ptr(g = EC_GROUP_new_from_ecparameters(p)))
  1629. goto err;
  1630. if (!TEST_int_gt(rv = EC_GROUP_check_named_curve(g, 0, NULL), 0))
  1631. goto err;
  1632. if (rv != nid) {
  1633. /*
  1634. * Found an alias:
  1635. * fail if the returned nid is not an alias of the original group.
  1636. *
  1637. * The comparison here is done by comparing two explicit
  1638. * parameter EC_GROUPs with EC_GROUP_cmp(), to ensure the
  1639. * comparison happens with unnamed EC_GROUPs using the same
  1640. * EC_METHODs.
  1641. */
  1642. if (!TEST_ptr(ga = EC_GROUP_new_by_curve_name(rv))
  1643. || !TEST_ptr(pa = EC_GROUP_get_ecparameters(ga, NULL)))
  1644. goto err;
  1645. /* replace with group from explicit parameters, then compare */
  1646. EC_GROUP_free(ga);
  1647. if (!TEST_ptr(ga = EC_GROUP_new_from_ecparameters(pa))
  1648. || !TEST_int_eq(EC_GROUP_cmp(g, ga, ctx), 0))
  1649. goto err;
  1650. }
  1651. ret = 1;
  1652. err:
  1653. EC_GROUP_free(g);
  1654. EC_GROUP_free(ga);
  1655. ECPARAMETERS_free(p);
  1656. ECPARAMETERS_free(pa);
  1657. BN_CTX_free(ctx);
  1658. return ret;
  1659. }
  1660. /*
  1661. * Sometime we cannot compare nids for equality, as the built-in curve table
  1662. * includes aliases with different names for the same curve.
  1663. *
  1664. * This function returns TRUE (1) if the checked nids are identical, or if they
  1665. * alias to the same curve. FALSE (0) otherwise.
  1666. */
  1667. static ossl_inline
  1668. int are_ec_nids_compatible(int n1d, int n2d)
  1669. {
  1670. int ret = 0;
  1671. switch (n1d) {
  1672. # ifndef OPENSSL_NO_EC2M
  1673. case NID_sect113r1:
  1674. case NID_wap_wsg_idm_ecid_wtls4:
  1675. ret = (n2d == NID_sect113r1 || n2d == NID_wap_wsg_idm_ecid_wtls4);
  1676. break;
  1677. case NID_sect163k1:
  1678. case NID_wap_wsg_idm_ecid_wtls3:
  1679. ret = (n2d == NID_sect163k1 || n2d == NID_wap_wsg_idm_ecid_wtls3);
  1680. break;
  1681. case NID_sect233k1:
  1682. case NID_wap_wsg_idm_ecid_wtls10:
  1683. ret = (n2d == NID_sect233k1 || n2d == NID_wap_wsg_idm_ecid_wtls10);
  1684. break;
  1685. case NID_sect233r1:
  1686. case NID_wap_wsg_idm_ecid_wtls11:
  1687. ret = (n2d == NID_sect233r1 || n2d == NID_wap_wsg_idm_ecid_wtls11);
  1688. break;
  1689. case NID_X9_62_c2pnb163v1:
  1690. case NID_wap_wsg_idm_ecid_wtls5:
  1691. ret = (n2d == NID_X9_62_c2pnb163v1
  1692. || n2d == NID_wap_wsg_idm_ecid_wtls5);
  1693. break;
  1694. # endif /* OPENSSL_NO_EC2M */
  1695. case NID_secp112r1:
  1696. case NID_wap_wsg_idm_ecid_wtls6:
  1697. ret = (n2d == NID_secp112r1 || n2d == NID_wap_wsg_idm_ecid_wtls6);
  1698. break;
  1699. case NID_secp160r2:
  1700. case NID_wap_wsg_idm_ecid_wtls7:
  1701. ret = (n2d == NID_secp160r2 || n2d == NID_wap_wsg_idm_ecid_wtls7);
  1702. break;
  1703. # ifdef OPENSSL_NO_EC_NISTP_64_GCC_128
  1704. case NID_secp224r1:
  1705. case NID_wap_wsg_idm_ecid_wtls12:
  1706. ret = (n2d == NID_secp224r1 || n2d == NID_wap_wsg_idm_ecid_wtls12);
  1707. break;
  1708. # else
  1709. /*
  1710. * For SEC P-224 we want to ensure that the SECP nid is returned, as
  1711. * that is associated with a specialized method.
  1712. */
  1713. case NID_wap_wsg_idm_ecid_wtls12:
  1714. ret = (n2d == NID_secp224r1);
  1715. break;
  1716. # endif /* def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
  1717. default:
  1718. ret = (n1d == n2d);
  1719. }
  1720. return ret;
  1721. }
  1722. /*
  1723. * This checks that EC_GROUP_bew_from_ecparameters() returns a "named"
  1724. * EC_GROUP for built-in curves.
  1725. *
  1726. * Note that it is possible to retrieve an alternative alias that does not match
  1727. * the original nid.
  1728. *
  1729. * Ensure that the OPENSSL_EC_EXPLICIT_CURVE ASN1 flag is set.
  1730. */
  1731. static int check_named_curve_from_ecparameters(int id)
  1732. {
  1733. int ret = 0, nid, tnid;
  1734. EC_GROUP *group = NULL, *tgroup = NULL, *tmpg = NULL;
  1735. const EC_POINT *group_gen = NULL;
  1736. EC_POINT *other_gen = NULL;
  1737. BIGNUM *group_cofactor = NULL, *other_cofactor = NULL;
  1738. BIGNUM *other_gen_x = NULL, *other_gen_y = NULL;
  1739. const BIGNUM *group_order = NULL;
  1740. BIGNUM *other_order = NULL;
  1741. BN_CTX *bn_ctx = NULL;
  1742. static const unsigned char invalid_seed[] = "THIS IS NOT A VALID SEED";
  1743. static size_t invalid_seed_len = sizeof(invalid_seed);
  1744. ECPARAMETERS *params = NULL, *other_params = NULL;
  1745. EC_GROUP *g_ary[8] = {NULL};
  1746. EC_GROUP **g_next = &g_ary[0];
  1747. ECPARAMETERS *p_ary[8] = {NULL};
  1748. ECPARAMETERS **p_next = &p_ary[0];
  1749. /* Do some setup */
  1750. nid = curves[id].nid;
  1751. TEST_note("Curve %s", OBJ_nid2sn(nid));
  1752. if (!TEST_ptr(bn_ctx = BN_CTX_new()))
  1753. return ret;
  1754. BN_CTX_start(bn_ctx);
  1755. if (/* Allocations */
  1756. !TEST_ptr(group_cofactor = BN_CTX_get(bn_ctx))
  1757. || !TEST_ptr(other_gen_x = BN_CTX_get(bn_ctx))
  1758. || !TEST_ptr(other_gen_y = BN_CTX_get(bn_ctx))
  1759. || !TEST_ptr(other_order = BN_CTX_get(bn_ctx))
  1760. || !TEST_ptr(other_cofactor = BN_CTX_get(bn_ctx))
  1761. /* Generate reference group and params */
  1762. || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
  1763. || !TEST_ptr(params = EC_GROUP_get_ecparameters(group, NULL))
  1764. || !TEST_ptr(group_gen = EC_GROUP_get0_generator(group))
  1765. || !TEST_ptr(group_order = EC_GROUP_get0_order(group))
  1766. || !TEST_true(EC_GROUP_get_cofactor(group, group_cofactor, NULL))
  1767. /* compute `other_*` values */
  1768. || !TEST_ptr(tmpg = EC_GROUP_dup(group))
  1769. || !TEST_ptr(other_gen = EC_POINT_dup(group_gen, group))
  1770. || !TEST_true(EC_POINT_add(group, other_gen, group_gen, group_gen, NULL))
  1771. || !TEST_true(EC_POINT_get_affine_coordinates(group, other_gen,
  1772. other_gen_x, other_gen_y, bn_ctx))
  1773. || !TEST_true(BN_copy(other_order, group_order))
  1774. || !TEST_true(BN_add_word(other_order, 1))
  1775. || !TEST_true(BN_copy(other_cofactor, group_cofactor))
  1776. || !TEST_true(BN_add_word(other_cofactor, 1)))
  1777. goto err;
  1778. EC_POINT_free(other_gen);
  1779. other_gen = NULL;
  1780. if (!TEST_ptr(other_gen = EC_POINT_new(tmpg))
  1781. || !TEST_true(EC_POINT_set_affine_coordinates(tmpg, other_gen,
  1782. other_gen_x, other_gen_y,
  1783. bn_ctx)))
  1784. goto err;
  1785. /*
  1786. * ###########################
  1787. * # Actual tests start here #
  1788. * ###########################
  1789. */
  1790. /*
  1791. * Creating a group from built-in explicit parameters returns a
  1792. * "named" EC_GROUP
  1793. */
  1794. if (!TEST_ptr(tgroup = *g_next++ = EC_GROUP_new_from_ecparameters(params))
  1795. || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef))
  1796. goto err;
  1797. /*
  1798. * We cannot always guarantee the names match, as the built-in table
  1799. * contains aliases for the same curve with different names.
  1800. */
  1801. if (!TEST_true(are_ec_nids_compatible(nid, tnid))) {
  1802. TEST_info("nid = %s, tnid = %s", OBJ_nid2sn(nid), OBJ_nid2sn(tnid));
  1803. goto err;
  1804. }
  1805. /* Ensure that the OPENSSL_EC_EXPLICIT_CURVE ASN1 flag is set. */
  1806. if (!TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup), OPENSSL_EC_EXPLICIT_CURVE))
  1807. goto err;
  1808. /*
  1809. * An invalid seed in the parameters should be ignored: expect a "named"
  1810. * group.
  1811. */
  1812. if (!TEST_int_eq(EC_GROUP_set_seed(tmpg, invalid_seed, invalid_seed_len),
  1813. invalid_seed_len)
  1814. || !TEST_ptr(other_params = *p_next++ =
  1815. EC_GROUP_get_ecparameters(tmpg, NULL))
  1816. || !TEST_ptr(tgroup = *g_next++ =
  1817. EC_GROUP_new_from_ecparameters(other_params))
  1818. || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
  1819. || !TEST_true(are_ec_nids_compatible(nid, tnid))
  1820. || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
  1821. OPENSSL_EC_EXPLICIT_CURVE)) {
  1822. TEST_info("nid = %s, tnid = %s", OBJ_nid2sn(nid), OBJ_nid2sn(tnid));
  1823. goto err;
  1824. }
  1825. /*
  1826. * A null seed in the parameters should be ignored, as it is optional:
  1827. * expect a "named" group.
  1828. */
  1829. if (!TEST_int_eq(EC_GROUP_set_seed(tmpg, NULL, 0), 1)
  1830. || !TEST_ptr(other_params = *p_next++ =
  1831. EC_GROUP_get_ecparameters(tmpg, NULL))
  1832. || !TEST_ptr(tgroup = *g_next++ =
  1833. EC_GROUP_new_from_ecparameters(other_params))
  1834. || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
  1835. || !TEST_true(are_ec_nids_compatible(nid, tnid))
  1836. || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
  1837. OPENSSL_EC_EXPLICIT_CURVE)) {
  1838. TEST_info("nid = %s, tnid = %s", OBJ_nid2sn(nid), OBJ_nid2sn(tnid));
  1839. goto err;
  1840. }
  1841. /*
  1842. * Check that changing any of the generator parameters does not yield a
  1843. * match with the built-in curves
  1844. */
  1845. if (/* Other gen, same group order & cofactor */
  1846. !TEST_true(EC_GROUP_set_generator(tmpg, other_gen, group_order,
  1847. group_cofactor))
  1848. || !TEST_ptr(other_params = *p_next++ =
  1849. EC_GROUP_get_ecparameters(tmpg, NULL))
  1850. || !TEST_ptr(tgroup = *g_next++ =
  1851. EC_GROUP_new_from_ecparameters(other_params))
  1852. || !TEST_int_eq((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
  1853. /* Same gen & cofactor, different order */
  1854. || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, other_order,
  1855. group_cofactor))
  1856. || !TEST_ptr(other_params = *p_next++ =
  1857. EC_GROUP_get_ecparameters(tmpg, NULL))
  1858. || !TEST_ptr(tgroup = *g_next++ =
  1859. EC_GROUP_new_from_ecparameters(other_params))
  1860. || !TEST_int_eq((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
  1861. /* The order is not an optional field, so this should fail */
  1862. || !TEST_false(EC_GROUP_set_generator(tmpg, group_gen, NULL,
  1863. group_cofactor))
  1864. /* Check that a wrong cofactor is ignored, and we still match */
  1865. || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, group_order,
  1866. other_cofactor))
  1867. || !TEST_ptr(other_params = *p_next++ =
  1868. EC_GROUP_get_ecparameters(tmpg, NULL))
  1869. || !TEST_ptr(tgroup = *g_next++ =
  1870. EC_GROUP_new_from_ecparameters(other_params))
  1871. || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
  1872. || !TEST_true(are_ec_nids_compatible(nid, tnid))
  1873. || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
  1874. OPENSSL_EC_EXPLICIT_CURVE)
  1875. /* Check that if the cofactor is not set then it still matches */
  1876. || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, group_order,
  1877. NULL))
  1878. || !TEST_ptr(other_params = *p_next++ =
  1879. EC_GROUP_get_ecparameters(tmpg, NULL))
  1880. || !TEST_ptr(tgroup = *g_next++ =
  1881. EC_GROUP_new_from_ecparameters(other_params))
  1882. || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
  1883. || !TEST_true(are_ec_nids_compatible(nid, tnid))
  1884. || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
  1885. OPENSSL_EC_EXPLICIT_CURVE)
  1886. /* check that restoring the generator passes */
  1887. || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, group_order,
  1888. group_cofactor))
  1889. || !TEST_ptr(other_params = *p_next++ =
  1890. EC_GROUP_get_ecparameters(tmpg, NULL))
  1891. || !TEST_ptr(tgroup = *g_next++ =
  1892. EC_GROUP_new_from_ecparameters(other_params))
  1893. || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
  1894. || !TEST_true(are_ec_nids_compatible(nid, tnid))
  1895. || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
  1896. OPENSSL_EC_EXPLICIT_CURVE))
  1897. goto err;
  1898. ret = 1;
  1899. err:
  1900. for (g_next = &g_ary[0]; g_next < g_ary + OSSL_NELEM(g_ary); g_next++)
  1901. EC_GROUP_free(*g_next);
  1902. for (p_next = &p_ary[0]; p_next < p_ary + OSSL_NELEM(g_ary); p_next++)
  1903. ECPARAMETERS_free(*p_next);
  1904. ECPARAMETERS_free(params);
  1905. EC_POINT_free(other_gen);
  1906. EC_GROUP_free(tmpg);
  1907. EC_GROUP_free(group);
  1908. BN_CTX_end(bn_ctx);
  1909. BN_CTX_free(bn_ctx);
  1910. return ret;
  1911. }
  1912. static int parameter_test(void)
  1913. {
  1914. EC_GROUP *group = NULL, *group2 = NULL;
  1915. ECPARAMETERS *ecparameters = NULL;
  1916. unsigned char *buf = NULL;
  1917. int r = 0, len;
  1918. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp384r1))
  1919. || !TEST_ptr(ecparameters = EC_GROUP_get_ecparameters(group, NULL))
  1920. || !TEST_ptr(group2 = EC_GROUP_new_from_ecparameters(ecparameters))
  1921. || !TEST_int_eq(EC_GROUP_cmp(group, group2, NULL), 0))
  1922. goto err;
  1923. EC_GROUP_free(group);
  1924. group = NULL;
  1925. /* Test the named curve encoding, which should be default. */
  1926. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp521r1))
  1927. || !TEST_true((len = i2d_ECPKParameters(group, &buf)) >= 0)
  1928. || !TEST_mem_eq(buf, len, p521_named, sizeof(p521_named)))
  1929. goto err;
  1930. OPENSSL_free(buf);
  1931. buf = NULL;
  1932. /*
  1933. * Test the explicit encoding. P-521 requires correctly zero-padding the
  1934. * curve coefficients.
  1935. */
  1936. EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
  1937. if (!TEST_true((len = i2d_ECPKParameters(group, &buf)) >= 0)
  1938. || !TEST_mem_eq(buf, len, p521_explicit, sizeof(p521_explicit)))
  1939. goto err;
  1940. r = 1;
  1941. err:
  1942. EC_GROUP_free(group);
  1943. EC_GROUP_free(group2);
  1944. ECPARAMETERS_free(ecparameters);
  1945. OPENSSL_free(buf);
  1946. return r;
  1947. }
  1948. /*-
  1949. * random 256-bit explicit parameters curve, cofactor absent
  1950. * order: 0x0c38d96a9f892b88772ec2e39614a82f4f (132 bit)
  1951. * cofactor: 0x12bc94785251297abfafddf1565100da (125 bit)
  1952. */
  1953. static const unsigned char params_cf_pass[] = {
  1954. 0x30, 0x81, 0xcd, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86,
  1955. 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xe5, 0x00, 0x1f, 0xc5,
  1956. 0xca, 0x71, 0x9d, 0x8e, 0xf7, 0x07, 0x4b, 0x48, 0x37, 0xf9, 0x33, 0x2d,
  1957. 0x71, 0xbf, 0x79, 0xe7, 0xdc, 0x91, 0xc2, 0xff, 0xb6, 0x7b, 0xc3, 0x93,
  1958. 0x44, 0x88, 0xe6, 0x91, 0x30, 0x44, 0x04, 0x20, 0xe5, 0x00, 0x1f, 0xc5,
  1959. 0xca, 0x71, 0x9d, 0x8e, 0xf7, 0x07, 0x4b, 0x48, 0x37, 0xf9, 0x33, 0x2d,
  1960. 0x71, 0xbf, 0x79, 0xe7, 0xdc, 0x91, 0xc2, 0xff, 0xb6, 0x7b, 0xc3, 0x93,
  1961. 0x44, 0x88, 0xe6, 0x8e, 0x04, 0x20, 0x18, 0x8c, 0x59, 0x57, 0xc4, 0xbc,
  1962. 0x85, 0x57, 0xc3, 0x66, 0x9f, 0x89, 0xd5, 0x92, 0x0d, 0x7e, 0x42, 0x27,
  1963. 0x07, 0x64, 0xaa, 0x26, 0xed, 0x89, 0xc4, 0x09, 0x05, 0x4d, 0xc7, 0x23,
  1964. 0x47, 0xda, 0x04, 0x41, 0x04, 0x1b, 0x6b, 0x41, 0x0b, 0xf9, 0xfb, 0x77,
  1965. 0xfd, 0x50, 0xb7, 0x3e, 0x23, 0xa3, 0xec, 0x9a, 0x3b, 0x09, 0x31, 0x6b,
  1966. 0xfa, 0xf6, 0xce, 0x1f, 0xff, 0xeb, 0x57, 0x93, 0x24, 0x70, 0xf3, 0xf4,
  1967. 0xba, 0x7e, 0xfa, 0x86, 0x6e, 0x19, 0x89, 0xe3, 0x55, 0x6d, 0x5a, 0xe9,
  1968. 0xc0, 0x3d, 0xbc, 0xfb, 0xaf, 0xad, 0xd4, 0x7e, 0xa6, 0xe5, 0xfa, 0x1a,
  1969. 0x58, 0x07, 0x9e, 0x8f, 0x0d, 0x3b, 0xf7, 0x38, 0xca, 0x02, 0x11, 0x0c,
  1970. 0x38, 0xd9, 0x6a, 0x9f, 0x89, 0x2b, 0x88, 0x77, 0x2e, 0xc2, 0xe3, 0x96,
  1971. 0x14, 0xa8, 0x2f, 0x4f
  1972. };
  1973. /*-
  1974. * random 256-bit explicit parameters curve, cofactor absent
  1975. * order: 0x045a75c0c17228ebd9b169a10e34a22101 (131 bit)
  1976. * cofactor: 0x2e134b4ede82649f67a2e559d361e5fe (126 bit)
  1977. */
  1978. static const unsigned char params_cf_fail[] = {
  1979. 0x30, 0x81, 0xcd, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86,
  1980. 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xc8, 0x95, 0x27, 0x37,
  1981. 0xe8, 0xe1, 0xfd, 0xcc, 0xf9, 0x6e, 0x0c, 0xa6, 0x21, 0xc1, 0x7d, 0x6b,
  1982. 0x9d, 0x44, 0x42, 0xea, 0x73, 0x4e, 0x04, 0xb6, 0xac, 0x62, 0x50, 0xd0,
  1983. 0x33, 0xc2, 0xea, 0x13, 0x30, 0x44, 0x04, 0x20, 0xc8, 0x95, 0x27, 0x37,
  1984. 0xe8, 0xe1, 0xfd, 0xcc, 0xf9, 0x6e, 0x0c, 0xa6, 0x21, 0xc1, 0x7d, 0x6b,
  1985. 0x9d, 0x44, 0x42, 0xea, 0x73, 0x4e, 0x04, 0xb6, 0xac, 0x62, 0x50, 0xd0,
  1986. 0x33, 0xc2, 0xea, 0x10, 0x04, 0x20, 0xbf, 0xa6, 0xa8, 0x05, 0x1d, 0x09,
  1987. 0xac, 0x70, 0x39, 0xbb, 0x4d, 0xb2, 0x90, 0x8a, 0x15, 0x41, 0x14, 0x1d,
  1988. 0x11, 0x86, 0x9f, 0x13, 0xa2, 0x63, 0x1a, 0xda, 0x95, 0x22, 0x4d, 0x02,
  1989. 0x15, 0x0a, 0x04, 0x41, 0x04, 0xaf, 0x16, 0x71, 0xf9, 0xc4, 0xc8, 0x59,
  1990. 0x1d, 0xa3, 0x6f, 0xe7, 0xc3, 0x57, 0xa1, 0xfa, 0x9f, 0x49, 0x7c, 0x11,
  1991. 0x27, 0x05, 0xa0, 0x7f, 0xff, 0xf9, 0xe0, 0xe7, 0x92, 0xdd, 0x9c, 0x24,
  1992. 0x8e, 0xc7, 0xb9, 0x52, 0x71, 0x3f, 0xbc, 0x7f, 0x6a, 0x9f, 0x35, 0x70,
  1993. 0xe1, 0x27, 0xd5, 0x35, 0x8a, 0x13, 0xfa, 0xa8, 0x33, 0x3e, 0xd4, 0x73,
  1994. 0x1c, 0x14, 0x58, 0x9e, 0xc7, 0x0a, 0x87, 0x65, 0x8d, 0x02, 0x11, 0x04,
  1995. 0x5a, 0x75, 0xc0, 0xc1, 0x72, 0x28, 0xeb, 0xd9, 0xb1, 0x69, 0xa1, 0x0e,
  1996. 0x34, 0xa2, 0x21, 0x01
  1997. };
  1998. /*-
  1999. * Test two random 256-bit explicit parameters curves with absent cofactor.
  2000. * The two curves are chosen to roughly straddle the bounds at which the lib
  2001. * can compute the cofactor automatically, roughly 4*sqrt(p). So test that:
  2002. *
  2003. * - params_cf_pass: order is sufficiently close to p to compute cofactor
  2004. * - params_cf_fail: order is too far away from p to compute cofactor
  2005. *
  2006. * For standards-compliant curves, cofactor is chosen as small as possible.
  2007. * So you can see neither of these curves are fit for cryptographic use.
  2008. *
  2009. * Some standards even mandate an upper bound on the cofactor, e.g. SECG1 v2:
  2010. * h <= 2**(t/8) where t is the security level of the curve, for which the lib
  2011. * will always succeed in computing the cofactor. Neither of these curves
  2012. * conform to that -- this is just robustness testing.
  2013. */
  2014. static int cofactor_range_test(void)
  2015. {
  2016. EC_GROUP *group = NULL;
  2017. BIGNUM *cf = NULL;
  2018. int ret = 0;
  2019. const unsigned char *b1 = (const unsigned char *)params_cf_fail;
  2020. const unsigned char *b2 = (const unsigned char *)params_cf_pass;
  2021. if (!TEST_ptr(group = d2i_ECPKParameters(NULL, &b1, sizeof(params_cf_fail)))
  2022. || !TEST_BN_eq_zero(EC_GROUP_get0_cofactor(group))
  2023. || !TEST_ptr(group = d2i_ECPKParameters(&group, &b2,
  2024. sizeof(params_cf_pass)))
  2025. || !TEST_int_gt(BN_hex2bn(&cf, "12bc94785251297abfafddf1565100da"), 0)
  2026. || !TEST_BN_eq(cf, EC_GROUP_get0_cofactor(group)))
  2027. goto err;
  2028. ret = 1;
  2029. err:
  2030. BN_free(cf);
  2031. EC_GROUP_free(group);
  2032. return ret;
  2033. }
  2034. /*-
  2035. * For named curves, test that:
  2036. * - the lib correctly computes the cofactor if passed a NULL or zero cofactor
  2037. * - a nonsensical cofactor throws an error (negative test)
  2038. * - nonsensical orders throw errors (negative tests)
  2039. */
  2040. static int cardinality_test(int n)
  2041. {
  2042. int ret = 0;
  2043. int nid = curves[n].nid;
  2044. BN_CTX *ctx = NULL;
  2045. EC_GROUP *g1 = NULL, *g2 = NULL;
  2046. EC_POINT *g2_gen = NULL;
  2047. BIGNUM *g1_p = NULL, *g1_a = NULL, *g1_b = NULL, *g1_x = NULL, *g1_y = NULL,
  2048. *g1_order = NULL, *g1_cf = NULL, *g2_cf = NULL;
  2049. TEST_info("Curve %s cardinality test", OBJ_nid2sn(nid));
  2050. if (!TEST_ptr(ctx = BN_CTX_new())
  2051. || !TEST_ptr(g1 = EC_GROUP_new_by_curve_name(nid))
  2052. || !TEST_ptr(g2 = EC_GROUP_new(EC_GROUP_method_of(g1)))) {
  2053. EC_GROUP_free(g1);
  2054. EC_GROUP_free(g2);
  2055. BN_CTX_free(ctx);
  2056. return 0;
  2057. }
  2058. BN_CTX_start(ctx);
  2059. g1_p = BN_CTX_get(ctx);
  2060. g1_a = BN_CTX_get(ctx);
  2061. g1_b = BN_CTX_get(ctx);
  2062. g1_x = BN_CTX_get(ctx);
  2063. g1_y = BN_CTX_get(ctx);
  2064. g1_order = BN_CTX_get(ctx);
  2065. g1_cf = BN_CTX_get(ctx);
  2066. if (!TEST_ptr(g2_cf = BN_CTX_get(ctx))
  2067. /* pull out the explicit curve parameters */
  2068. || !TEST_true(EC_GROUP_get_curve(g1, g1_p, g1_a, g1_b, ctx))
  2069. || !TEST_true(EC_POINT_get_affine_coordinates(g1,
  2070. EC_GROUP_get0_generator(g1), g1_x, g1_y, ctx))
  2071. || !TEST_true(BN_copy(g1_order, EC_GROUP_get0_order(g1)))
  2072. || !TEST_true(EC_GROUP_get_cofactor(g1, g1_cf, ctx))
  2073. /* construct g2 manually with g1 parameters */
  2074. || !TEST_true(EC_GROUP_set_curve(g2, g1_p, g1_a, g1_b, ctx))
  2075. || !TEST_ptr(g2_gen = EC_POINT_new(g2))
  2076. || !TEST_true(EC_POINT_set_affine_coordinates(g2, g2_gen, g1_x, g1_y, ctx))
  2077. /* pass NULL cofactor: lib should compute it */
  2078. || !TEST_true(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL))
  2079. || !TEST_true(EC_GROUP_get_cofactor(g2, g2_cf, ctx))
  2080. || !TEST_BN_eq(g1_cf, g2_cf)
  2081. /* pass zero cofactor: lib should compute it */
  2082. || !TEST_true(BN_set_word(g2_cf, 0))
  2083. || !TEST_true(EC_GROUP_set_generator(g2, g2_gen, g1_order, g2_cf))
  2084. || !TEST_true(EC_GROUP_get_cofactor(g2, g2_cf, ctx))
  2085. || !TEST_BN_eq(g1_cf, g2_cf)
  2086. /* negative test for invalid cofactor */
  2087. || !TEST_true(BN_set_word(g2_cf, 0))
  2088. || !TEST_true(BN_sub(g2_cf, g2_cf, BN_value_one()))
  2089. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, g2_cf))
  2090. /* negative test for NULL order */
  2091. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, NULL, NULL))
  2092. /* negative test for zero order */
  2093. || !TEST_true(BN_set_word(g1_order, 0))
  2094. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL))
  2095. /* negative test for negative order */
  2096. || !TEST_true(BN_set_word(g2_cf, 0))
  2097. || !TEST_true(BN_sub(g2_cf, g2_cf, BN_value_one()))
  2098. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL))
  2099. /* negative test for too large order */
  2100. || !TEST_true(BN_lshift(g1_order, g1_p, 2))
  2101. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL)))
  2102. goto err;
  2103. ret = 1;
  2104. err:
  2105. EC_POINT_free(g2_gen);
  2106. EC_GROUP_free(g1);
  2107. EC_GROUP_free(g2);
  2108. BN_CTX_end(ctx);
  2109. BN_CTX_free(ctx);
  2110. return ret;
  2111. }
  2112. static int check_ec_key_field_public_range_test(int id)
  2113. {
  2114. int ret = 0, type = 0;
  2115. const EC_POINT *pub = NULL;
  2116. const EC_GROUP *group = NULL;
  2117. const EC_METHOD *meth = NULL;
  2118. const BIGNUM *field = NULL;
  2119. BIGNUM *x = NULL, *y = NULL;
  2120. EC_KEY *key = NULL;
  2121. if (!TEST_ptr(x = BN_new())
  2122. || !TEST_ptr(y = BN_new())
  2123. || !TEST_ptr(key = EC_KEY_new_by_curve_name(curves[id].nid))
  2124. || !TEST_ptr(group = EC_KEY_get0_group(key))
  2125. || !TEST_ptr(meth = EC_GROUP_method_of(group))
  2126. || !TEST_ptr(field = EC_GROUP_get0_field(group))
  2127. || !TEST_int_gt(EC_KEY_generate_key(key), 0)
  2128. || !TEST_int_gt(EC_KEY_check_key(key), 0)
  2129. || !TEST_ptr(pub = EC_KEY_get0_public_key(key))
  2130. || !TEST_int_gt(EC_POINT_get_affine_coordinates(group, pub, x, y,
  2131. NULL), 0))
  2132. goto err;
  2133. /*
  2134. * Make the public point out of range by adding the field (which will still
  2135. * be the same point on the curve). The add is different for char2 fields.
  2136. */
  2137. type = EC_METHOD_get_field_type(meth);
  2138. #ifndef OPENSSL_NO_EC2M
  2139. if (type == NID_X9_62_characteristic_two_field) {
  2140. /* test for binary curves */
  2141. if (!TEST_true(BN_GF2m_add(x, x, field)))
  2142. goto err;
  2143. } else
  2144. #endif
  2145. if (type == NID_X9_62_prime_field) {
  2146. /* test for prime curves */
  2147. if (!TEST_true(BN_add(x, x, field)))
  2148. goto err;
  2149. } else {
  2150. /* this should never happen */
  2151. TEST_error("Unsupported EC_METHOD field_type");
  2152. goto err;
  2153. }
  2154. if (!TEST_int_le(EC_KEY_set_public_key_affine_coordinates(key, x, y), 0))
  2155. goto err;
  2156. ret = 1;
  2157. err:
  2158. BN_free(x);
  2159. BN_free(y);
  2160. EC_KEY_free(key);
  2161. return ret;
  2162. }
  2163. #endif /* OPENSSL_NO_EC */
  2164. int setup_tests(void)
  2165. {
  2166. #ifndef OPENSSL_NO_EC
  2167. crv_len = EC_get_builtin_curves(NULL, 0);
  2168. if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
  2169. || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
  2170. return 0;
  2171. ADD_TEST(parameter_test);
  2172. ADD_TEST(cofactor_range_test);
  2173. ADD_ALL_TESTS(cardinality_test, crv_len);
  2174. ADD_TEST(prime_field_tests);
  2175. # ifndef OPENSSL_NO_EC2M
  2176. ADD_TEST(char2_field_tests);
  2177. ADD_ALL_TESTS(char2_curve_test, OSSL_NELEM(char2_curve_tests));
  2178. # endif
  2179. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  2180. ADD_ALL_TESTS(nistp_single_test, OSSL_NELEM(nistp_tests_params));
  2181. ADD_TEST(underflow_test);
  2182. # endif
  2183. ADD_ALL_TESTS(internal_curve_test, crv_len);
  2184. ADD_ALL_TESTS(internal_curve_test_method, crv_len);
  2185. ADD_TEST(group_field_test);
  2186. ADD_ALL_TESTS(check_named_curve_test, crv_len);
  2187. ADD_ALL_TESTS(check_named_curve_lookup_test, crv_len);
  2188. ADD_ALL_TESTS(check_ec_key_field_public_range_test, crv_len);
  2189. ADD_ALL_TESTS(check_named_curve_from_ecparameters, crv_len);
  2190. #endif /* OPENSSL_NO_EC */
  2191. return 1;
  2192. }
  2193. void cleanup_tests(void)
  2194. {
  2195. #ifndef OPENSSL_NO_EC
  2196. OPENSSL_free(curves);
  2197. #endif
  2198. }