ectest.c 119 KB

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