ectest.c 119 KB

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