ectest.c 126 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157
  1. /*
  2. * Copyright 2001-2023 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. static int hybrid_point_encoding_test(void)
  997. {
  998. BIGNUM *x = NULL, *y = NULL;
  999. EC_GROUP *group = NULL;
  1000. EC_POINT *point = NULL;
  1001. unsigned char *buf = NULL;
  1002. size_t len;
  1003. int r = 0;
  1004. if (!TEST_true(BN_dec2bn(&x, "0"))
  1005. || !TEST_true(BN_dec2bn(&y, "1"))
  1006. || !TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_sect571k1))
  1007. || !TEST_ptr(point = EC_POINT_new(group))
  1008. || !TEST_true(EC_POINT_set_affine_coordinates(group, point, x, y, NULL))
  1009. || !TEST_size_t_ne(0, (len = EC_POINT_point2oct(group,
  1010. point,
  1011. POINT_CONVERSION_HYBRID,
  1012. NULL,
  1013. 0,
  1014. NULL)))
  1015. || !TEST_ptr(buf = OPENSSL_malloc(len))
  1016. || !TEST_size_t_eq(len, EC_POINT_point2oct(group,
  1017. point,
  1018. POINT_CONVERSION_HYBRID,
  1019. buf,
  1020. len,
  1021. NULL)))
  1022. goto err;
  1023. r = 1;
  1024. /* buf contains a valid hybrid point, check that we can decode it. */
  1025. if (!TEST_true(EC_POINT_oct2point(group, point, buf, len, NULL)))
  1026. r = 0;
  1027. /* Flip the y_bit and verify that the invalid encoding is rejected. */
  1028. buf[0] ^= 1;
  1029. if (!TEST_false(EC_POINT_oct2point(group, point, buf, len, NULL)))
  1030. r = 0;
  1031. err:
  1032. BN_free(x);
  1033. BN_free(y);
  1034. EC_GROUP_free(group);
  1035. EC_POINT_free(point);
  1036. OPENSSL_free(buf);
  1037. return r;
  1038. }
  1039. #endif
  1040. static int internal_curve_test(int n)
  1041. {
  1042. EC_GROUP *group = NULL;
  1043. int nid = curves[n].nid;
  1044. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) {
  1045. TEST_info("EC_GROUP_new_curve_name() failed with curve %s\n",
  1046. OBJ_nid2sn(nid));
  1047. return 0;
  1048. }
  1049. if (!TEST_true(EC_GROUP_check(group, NULL))) {
  1050. TEST_info("EC_GROUP_check() failed with curve %s\n", OBJ_nid2sn(nid));
  1051. EC_GROUP_free(group);
  1052. return 0;
  1053. }
  1054. EC_GROUP_free(group);
  1055. return 1;
  1056. }
  1057. static int internal_curve_test_method(int n)
  1058. {
  1059. int r, nid = curves[n].nid;
  1060. EC_GROUP *group;
  1061. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))) {
  1062. TEST_info("Curve %s failed\n", OBJ_nid2sn(nid));
  1063. return 0;
  1064. }
  1065. r = group_order_tests(group);
  1066. EC_GROUP_free(group);
  1067. return r;
  1068. }
  1069. static int group_field_test(void)
  1070. {
  1071. int r = 1;
  1072. BIGNUM *secp521r1_field = NULL;
  1073. BIGNUM *sect163r2_field = NULL;
  1074. EC_GROUP *secp521r1_group = NULL;
  1075. EC_GROUP *sect163r2_group = NULL;
  1076. BN_hex2bn(&secp521r1_field,
  1077. "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  1078. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  1079. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  1080. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  1081. "FFFF");
  1082. BN_hex2bn(&sect163r2_field,
  1083. "08000000000000000000000000000000"
  1084. "00000000C9");
  1085. secp521r1_group = EC_GROUP_new_by_curve_name(NID_secp521r1);
  1086. if (BN_cmp(secp521r1_field, EC_GROUP_get0_field(secp521r1_group)))
  1087. r = 0;
  1088. # ifndef OPENSSL_NO_EC2M
  1089. sect163r2_group = EC_GROUP_new_by_curve_name(NID_sect163r2);
  1090. if (BN_cmp(sect163r2_field, EC_GROUP_get0_field(sect163r2_group)))
  1091. r = 0;
  1092. # endif
  1093. EC_GROUP_free(secp521r1_group);
  1094. EC_GROUP_free(sect163r2_group);
  1095. BN_free(secp521r1_field);
  1096. BN_free(sect163r2_field);
  1097. return r;
  1098. }
  1099. /*
  1100. * nistp_test_params contains magic numbers for testing
  1101. * several NIST curves with characteristic > 3.
  1102. */
  1103. struct nistp_test_params {
  1104. const int nid;
  1105. int degree;
  1106. /*
  1107. * Qx, Qy and D are taken from
  1108. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
  1109. * Otherwise, values are standard curve parameters from FIPS 180-3
  1110. */
  1111. const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d;
  1112. };
  1113. static const struct nistp_test_params nistp_tests_params[] = {
  1114. {
  1115. /* P-224 */
  1116. NID_secp224r1,
  1117. 224,
  1118. /* p */
  1119. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
  1120. /* a */
  1121. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
  1122. /* b */
  1123. "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
  1124. /* Qx */
  1125. "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E",
  1126. /* Qy */
  1127. "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555",
  1128. /* Gx */
  1129. "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
  1130. /* Gy */
  1131. "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
  1132. /* order */
  1133. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
  1134. /* d */
  1135. "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8",
  1136. },
  1137. {
  1138. /* P-256 */
  1139. NID_X9_62_prime256v1,
  1140. 256,
  1141. /* p */
  1142. "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
  1143. /* a */
  1144. "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
  1145. /* b */
  1146. "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
  1147. /* Qx */
  1148. "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19",
  1149. /* Qy */
  1150. "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09",
  1151. /* Gx */
  1152. "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
  1153. /* Gy */
  1154. "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
  1155. /* order */
  1156. "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
  1157. /* d */
  1158. "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96",
  1159. },
  1160. {
  1161. /* P-521 */
  1162. NID_secp521r1,
  1163. 521,
  1164. /* p */
  1165. "1ff"
  1166. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  1167. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  1168. /* a */
  1169. "1ff"
  1170. "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
  1171. "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc",
  1172. /* b */
  1173. "051"
  1174. "953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e1"
  1175. "56193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
  1176. /* Qx */
  1177. "0098"
  1178. "e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e"
  1179. "59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4",
  1180. /* Qy */
  1181. "0164"
  1182. "350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8"
  1183. "554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e",
  1184. /* Gx */
  1185. "c6"
  1186. "858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dba"
  1187. "a14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
  1188. /* Gy */
  1189. "118"
  1190. "39296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c"
  1191. "97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
  1192. /* order */
  1193. "1ff"
  1194. "fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa"
  1195. "51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
  1196. /* d */
  1197. "0100"
  1198. "085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eee"
  1199. "df09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722",
  1200. },
  1201. };
  1202. static int nistp_single_test(int idx)
  1203. {
  1204. const struct nistp_test_params *test = nistp_tests_params + idx;
  1205. BN_CTX *ctx = NULL;
  1206. BIGNUM *p = NULL, *a = NULL, *b = NULL, *x = NULL, *y = NULL;
  1207. BIGNUM *n = NULL, *m = NULL, *order = NULL, *yplusone = NULL;
  1208. EC_GROUP *NISTP = NULL;
  1209. EC_POINT *G = NULL, *P = NULL, *Q = NULL, *Q_CHECK = NULL;
  1210. int r = 0;
  1211. TEST_note("NIST curve P-%d (optimised implementation):",
  1212. test->degree);
  1213. if (!TEST_ptr(ctx = BN_CTX_new())
  1214. || !TEST_ptr(p = BN_new())
  1215. || !TEST_ptr(a = BN_new())
  1216. || !TEST_ptr(b = BN_new())
  1217. || !TEST_ptr(x = BN_new())
  1218. || !TEST_ptr(y = BN_new())
  1219. || !TEST_ptr(m = BN_new())
  1220. || !TEST_ptr(n = BN_new())
  1221. || !TEST_ptr(order = BN_new())
  1222. || !TEST_ptr(yplusone = BN_new())
  1223. || !TEST_ptr(NISTP = EC_GROUP_new_by_curve_name(test->nid))
  1224. || !TEST_true(BN_hex2bn(&p, test->p))
  1225. || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL))
  1226. || !TEST_true(BN_hex2bn(&a, test->a))
  1227. || !TEST_true(BN_hex2bn(&b, test->b))
  1228. || !TEST_true(EC_GROUP_set_curve(NISTP, p, a, b, ctx))
  1229. || !TEST_ptr(G = EC_POINT_new(NISTP))
  1230. || !TEST_ptr(P = EC_POINT_new(NISTP))
  1231. || !TEST_ptr(Q = EC_POINT_new(NISTP))
  1232. || !TEST_ptr(Q_CHECK = EC_POINT_new(NISTP))
  1233. || !TEST_true(BN_hex2bn(&x, test->Qx))
  1234. || !TEST_true(BN_hex2bn(&y, test->Qy))
  1235. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  1236. /*
  1237. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  1238. * and therefore setting the coordinates should fail.
  1239. */
  1240. || !TEST_false(EC_POINT_set_affine_coordinates(NISTP, Q_CHECK, x,
  1241. yplusone, ctx))
  1242. || !TEST_true(EC_POINT_set_affine_coordinates(NISTP, Q_CHECK, x, y,
  1243. ctx))
  1244. || !TEST_true(BN_hex2bn(&x, test->Gx))
  1245. || !TEST_true(BN_hex2bn(&y, test->Gy))
  1246. || !TEST_true(EC_POINT_set_affine_coordinates(NISTP, G, x, y, ctx))
  1247. || !TEST_true(BN_hex2bn(&order, test->order))
  1248. || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))
  1249. || !TEST_int_eq(EC_GROUP_get_degree(NISTP), test->degree))
  1250. goto err;
  1251. TEST_note("NIST test vectors ... ");
  1252. if (!TEST_true(BN_hex2bn(&n, test->d)))
  1253. goto err;
  1254. /* fixed point multiplication */
  1255. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1256. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1257. goto err;
  1258. /* random point multiplication */
  1259. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1260. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1261. /* set generator to P = 2*G, where G is the standard generator */
  1262. || !TEST_true(EC_POINT_dbl(NISTP, P, G, ctx))
  1263. || !TEST_true(EC_GROUP_set_generator(NISTP, P, order, BN_value_one()))
  1264. /* set the scalar to m=n/2, where n is the NIST test scalar */
  1265. || !TEST_true(BN_rshift(m, n, 1)))
  1266. goto err;
  1267. /* test the non-standard generator */
  1268. /* fixed point multiplication */
  1269. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1270. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1271. goto err;
  1272. /* random point multiplication */
  1273. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1274. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1275. #ifndef OPENSSL_NO_DEPRECATED_3_0
  1276. /* We have not performed precomp so this should be false */
  1277. || !TEST_false(EC_GROUP_have_precompute_mult(NISTP))
  1278. /* now repeat all tests with precomputation */
  1279. || !TEST_true(EC_GROUP_precompute_mult(NISTP, ctx))
  1280. #endif
  1281. )
  1282. goto err;
  1283. /* fixed point multiplication */
  1284. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1285. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1286. goto err;
  1287. /* random point multiplication */
  1288. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1289. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1290. /* reset generator */
  1291. || !TEST_true(EC_GROUP_set_generator(NISTP, G, order, BN_value_one())))
  1292. goto err;
  1293. /* fixed point multiplication */
  1294. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1295. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1296. goto err;
  1297. /* random point multiplication */
  1298. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1299. if (!TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)))
  1300. goto err;
  1301. /* regression test for felem_neg bug */
  1302. if (!TEST_true(BN_set_word(m, 32))
  1303. || !TEST_true(BN_set_word(n, 31))
  1304. || !TEST_true(EC_POINT_copy(P, G))
  1305. || !TEST_true(EC_POINT_invert(NISTP, P, ctx))
  1306. || !TEST_true(EC_POINT_mul(NISTP, Q, m, P, n, ctx))
  1307. || !TEST_int_eq(0, EC_POINT_cmp(NISTP, Q, G, ctx)))
  1308. goto err;
  1309. r = 1;
  1310. err:
  1311. EC_GROUP_free(NISTP);
  1312. EC_POINT_free(G);
  1313. EC_POINT_free(P);
  1314. EC_POINT_free(Q);
  1315. EC_POINT_free(Q_CHECK);
  1316. BN_free(n);
  1317. BN_free(m);
  1318. BN_free(p);
  1319. BN_free(a);
  1320. BN_free(b);
  1321. BN_free(x);
  1322. BN_free(y);
  1323. BN_free(order);
  1324. BN_free(yplusone);
  1325. BN_CTX_free(ctx);
  1326. return r;
  1327. }
  1328. static const unsigned char p521_named[] = {
  1329. 0x06, 0x05, 0x2b, 0x81, 0x04, 0x00, 0x23,
  1330. };
  1331. static const unsigned char p521_explicit[] = {
  1332. 0x30, 0x82, 0x01, 0xc3, 0x02, 0x01, 0x01, 0x30, 0x4d, 0x06, 0x07, 0x2a,
  1333. 0x86, 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x42, 0x01, 0xff, 0xff, 0xff,
  1334. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1335. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1336. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1337. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1338. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1339. 0xff, 0xff, 0x30, 0x81, 0x9f, 0x04, 0x42, 0x01, 0xff, 0xff, 0xff, 0xff,
  1340. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1341. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1342. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1343. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1344. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1345. 0xfc, 0x04, 0x42, 0x00, 0x51, 0x95, 0x3e, 0xb9, 0x61, 0x8e, 0x1c, 0x9a,
  1346. 0x1f, 0x92, 0x9a, 0x21, 0xa0, 0xb6, 0x85, 0x40, 0xee, 0xa2, 0xda, 0x72,
  1347. 0x5b, 0x99, 0xb3, 0x15, 0xf3, 0xb8, 0xb4, 0x89, 0x91, 0x8e, 0xf1, 0x09,
  1348. 0xe1, 0x56, 0x19, 0x39, 0x51, 0xec, 0x7e, 0x93, 0x7b, 0x16, 0x52, 0xc0,
  1349. 0xbd, 0x3b, 0xb1, 0xbf, 0x07, 0x35, 0x73, 0xdf, 0x88, 0x3d, 0x2c, 0x34,
  1350. 0xf1, 0xef, 0x45, 0x1f, 0xd4, 0x6b, 0x50, 0x3f, 0x00, 0x03, 0x15, 0x00,
  1351. 0xd0, 0x9e, 0x88, 0x00, 0x29, 0x1c, 0xb8, 0x53, 0x96, 0xcc, 0x67, 0x17,
  1352. 0x39, 0x32, 0x84, 0xaa, 0xa0, 0xda, 0x64, 0xba, 0x04, 0x81, 0x85, 0x04,
  1353. 0x00, 0xc6, 0x85, 0x8e, 0x06, 0xb7, 0x04, 0x04, 0xe9, 0xcd, 0x9e, 0x3e,
  1354. 0xcb, 0x66, 0x23, 0x95, 0xb4, 0x42, 0x9c, 0x64, 0x81, 0x39, 0x05, 0x3f,
  1355. 0xb5, 0x21, 0xf8, 0x28, 0xaf, 0x60, 0x6b, 0x4d, 0x3d, 0xba, 0xa1, 0x4b,
  1356. 0x5e, 0x77, 0xef, 0xe7, 0x59, 0x28, 0xfe, 0x1d, 0xc1, 0x27, 0xa2, 0xff,
  1357. 0xa8, 0xde, 0x33, 0x48, 0xb3, 0xc1, 0x85, 0x6a, 0x42, 0x9b, 0xf9, 0x7e,
  1358. 0x7e, 0x31, 0xc2, 0xe5, 0xbd, 0x66, 0x01, 0x18, 0x39, 0x29, 0x6a, 0x78,
  1359. 0x9a, 0x3b, 0xc0, 0x04, 0x5c, 0x8a, 0x5f, 0xb4, 0x2c, 0x7d, 0x1b, 0xd9,
  1360. 0x98, 0xf5, 0x44, 0x49, 0x57, 0x9b, 0x44, 0x68, 0x17, 0xaf, 0xbd, 0x17,
  1361. 0x27, 0x3e, 0x66, 0x2c, 0x97, 0xee, 0x72, 0x99, 0x5e, 0xf4, 0x26, 0x40,
  1362. 0xc5, 0x50, 0xb9, 0x01, 0x3f, 0xad, 0x07, 0x61, 0x35, 0x3c, 0x70, 0x86,
  1363. 0xa2, 0x72, 0xc2, 0x40, 0x88, 0xbe, 0x94, 0x76, 0x9f, 0xd1, 0x66, 0x50,
  1364. 0x02, 0x42, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1365. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1366. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfa,
  1367. 0x51, 0x86, 0x87, 0x83, 0xbf, 0x2f, 0x96, 0x6b, 0x7f, 0xcc, 0x01, 0x48,
  1368. 0xf7, 0x09, 0xa5, 0xd0, 0x3b, 0xb5, 0xc9, 0xb8, 0x89, 0x9c, 0x47, 0xae,
  1369. 0xbb, 0x6f, 0xb7, 0x1e, 0x91, 0x38, 0x64, 0x09, 0x02, 0x01, 0x01,
  1370. };
  1371. /*
  1372. * This test validates a named curve's group parameters using
  1373. * EC_GROUP_check_named_curve(). It also checks that modifying any of the
  1374. * group parameters results in the curve not being valid.
  1375. */
  1376. static int check_named_curve_test(int id)
  1377. {
  1378. int ret = 0, nid, field_nid, has_seed;
  1379. EC_GROUP *group = NULL, *gtest = NULL;
  1380. const EC_POINT *group_gen = NULL;
  1381. EC_POINT *other_gen = NULL;
  1382. BIGNUM *group_p = NULL, *group_a = NULL, *group_b = NULL;
  1383. BIGNUM *other_p = NULL, *other_a = NULL, *other_b = NULL;
  1384. BIGNUM *group_cofactor = NULL, *other_cofactor = NULL;
  1385. BIGNUM *other_order = NULL;
  1386. const BIGNUM *group_order = NULL;
  1387. BN_CTX *bn_ctx = NULL;
  1388. static const unsigned char invalid_seed[] = "THIS IS NOT A VALID SEED";
  1389. static size_t invalid_seed_len = sizeof(invalid_seed);
  1390. /* Do some setup */
  1391. nid = curves[id].nid;
  1392. if (!TEST_ptr(bn_ctx = BN_CTX_new())
  1393. || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
  1394. || !TEST_ptr(gtest = EC_GROUP_dup(group))
  1395. || !TEST_ptr(group_p = BN_new())
  1396. || !TEST_ptr(group_a = BN_new())
  1397. || !TEST_ptr(group_b = BN_new())
  1398. || !TEST_ptr(group_cofactor = BN_new())
  1399. || !TEST_ptr(group_gen = EC_GROUP_get0_generator(group))
  1400. || !TEST_ptr(group_order = EC_GROUP_get0_order(group))
  1401. || !TEST_true(EC_GROUP_get_cofactor(group, group_cofactor, NULL))
  1402. || !TEST_true(EC_GROUP_get_curve(group, group_p, group_a, group_b, NULL))
  1403. || !TEST_ptr(other_gen = EC_POINT_dup(group_gen, group))
  1404. || !TEST_true(EC_POINT_add(group, other_gen, group_gen, group_gen, NULL))
  1405. || !TEST_ptr(other_order = BN_dup(group_order))
  1406. || !TEST_true(BN_add_word(other_order, 1))
  1407. || !TEST_ptr(other_a = BN_dup(group_a))
  1408. || !TEST_true(BN_add_word(other_a, 1))
  1409. || !TEST_ptr(other_b = BN_dup(group_b))
  1410. || !TEST_true(BN_add_word(other_b, 1))
  1411. || !TEST_ptr(other_cofactor = BN_dup(group_cofactor))
  1412. || !TEST_true(BN_add_word(other_cofactor, 1)))
  1413. goto err;
  1414. /* Determine if the built-in curve has a seed field set */
  1415. has_seed = (EC_GROUP_get_seed_len(group) > 0);
  1416. field_nid = EC_GROUP_get_field_type(group);
  1417. if (field_nid == NID_X9_62_characteristic_two_field) {
  1418. if (!TEST_ptr(other_p = BN_dup(group_p))
  1419. || !TEST_true(BN_lshift1(other_p, other_p)))
  1420. goto err;
  1421. } else {
  1422. if (!TEST_ptr(other_p = BN_dup(group_p)))
  1423. goto err;
  1424. /*
  1425. * Just choosing any arbitrary prime does not work..
  1426. * Setting p via ec_GFp_nist_group_set_curve() needs the prime to be a
  1427. * nist prime. So only select one of these as an alternate prime.
  1428. */
  1429. if (!TEST_ptr(BN_copy(other_p,
  1430. BN_ucmp(BN_get0_nist_prime_192(), other_p) == 0 ?
  1431. BN_get0_nist_prime_256() :
  1432. BN_get0_nist_prime_192())))
  1433. goto err;
  1434. }
  1435. /* Passes because this is a valid curve */
  1436. if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid)
  1437. /* Only NIST curves pass */
  1438. || !TEST_int_eq(EC_GROUP_check_named_curve(group, 1, NULL),
  1439. EC_curve_nid2nist(nid) != NULL ? nid : NID_undef))
  1440. goto err;
  1441. /* Fail if the curve name doesn't match the parameters */
  1442. EC_GROUP_set_curve_name(group, nid + 1);
  1443. ERR_set_mark();
  1444. if (!TEST_int_le(EC_GROUP_check_named_curve(group, 0, NULL), 0))
  1445. goto err;
  1446. ERR_pop_to_mark();
  1447. /* Restore curve name and ensure it's passing */
  1448. EC_GROUP_set_curve_name(group, nid);
  1449. if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid))
  1450. goto err;
  1451. if (!TEST_int_eq(EC_GROUP_set_seed(group, invalid_seed, invalid_seed_len),
  1452. invalid_seed_len))
  1453. goto err;
  1454. if (has_seed) {
  1455. /*
  1456. * If the built-in curve has a seed and we set the seed to another value
  1457. * then it will fail the check.
  1458. */
  1459. if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), 0))
  1460. goto err;
  1461. } else {
  1462. /*
  1463. * If the built-in curve does not have a seed then setting the seed will
  1464. * pass the check (as the seed is optional).
  1465. */
  1466. if (!TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid))
  1467. goto err;
  1468. }
  1469. /* Pass if the seed is unknown (as it is optional) */
  1470. if (!TEST_int_eq(EC_GROUP_set_seed(group, NULL, 0), 1)
  1471. || !TEST_int_eq(EC_GROUP_check_named_curve(group, 0, NULL), nid))
  1472. goto err;
  1473. /* Check that a duped group passes */
  1474. if (!TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid))
  1475. goto err;
  1476. /* check that changing any generator parameter fails */
  1477. if (!TEST_true(EC_GROUP_set_generator(gtest, other_gen, group_order,
  1478. group_cofactor))
  1479. || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), 0)
  1480. || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, other_order,
  1481. group_cofactor))
  1482. || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), 0)
  1483. /* The order is not an optional field, so this should fail */
  1484. || !TEST_false(EC_GROUP_set_generator(gtest, group_gen, NULL,
  1485. group_cofactor))
  1486. || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, group_order,
  1487. other_cofactor))
  1488. || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), 0)
  1489. /* Check that if the cofactor is not set then it still passes */
  1490. || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, group_order,
  1491. NULL))
  1492. || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid)
  1493. /* check that restoring the generator passes */
  1494. || !TEST_true(EC_GROUP_set_generator(gtest, group_gen, group_order,
  1495. group_cofactor))
  1496. || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid))
  1497. goto err;
  1498. /*
  1499. * check that changing any curve parameter fails
  1500. *
  1501. * Setting arbitrary p, a or b might fail for some EC_GROUPs
  1502. * depending on the internal EC_METHOD implementation, hence run
  1503. * these tests conditionally to the success of EC_GROUP_set_curve().
  1504. */
  1505. ERR_set_mark();
  1506. if (EC_GROUP_set_curve(gtest, other_p, group_a, group_b, NULL)) {
  1507. if (!TEST_int_le(EC_GROUP_check_named_curve(gtest, 0, NULL), 0))
  1508. goto err;
  1509. } else {
  1510. /* clear the error stack if EC_GROUP_set_curve() failed */
  1511. ERR_pop_to_mark();
  1512. ERR_set_mark();
  1513. }
  1514. if (EC_GROUP_set_curve(gtest, group_p, other_a, group_b, NULL)) {
  1515. if (!TEST_int_le(EC_GROUP_check_named_curve(gtest, 0, NULL), 0))
  1516. goto err;
  1517. } else {
  1518. /* clear the error stack if EC_GROUP_set_curve() failed */
  1519. ERR_pop_to_mark();
  1520. ERR_set_mark();
  1521. }
  1522. if (EC_GROUP_set_curve(gtest, group_p, group_a, other_b, NULL)) {
  1523. if (!TEST_int_le(EC_GROUP_check_named_curve(gtest, 0, NULL), 0))
  1524. goto err;
  1525. } else {
  1526. /* clear the error stack if EC_GROUP_set_curve() failed */
  1527. ERR_pop_to_mark();
  1528. ERR_set_mark();
  1529. }
  1530. ERR_pop_to_mark();
  1531. /* Check that restoring the curve parameters passes */
  1532. if (!TEST_true(EC_GROUP_set_curve(gtest, group_p, group_a, group_b, NULL))
  1533. || !TEST_int_eq(EC_GROUP_check_named_curve(gtest, 0, NULL), nid))
  1534. goto err;
  1535. ret = 1;
  1536. err:
  1537. BN_free(group_p);
  1538. BN_free(other_p);
  1539. BN_free(group_a);
  1540. BN_free(other_a);
  1541. BN_free(group_b);
  1542. BN_free(other_b);
  1543. BN_free(group_cofactor);
  1544. BN_free(other_cofactor);
  1545. BN_free(other_order);
  1546. EC_POINT_free(other_gen);
  1547. EC_GROUP_free(gtest);
  1548. EC_GROUP_free(group);
  1549. BN_CTX_free(bn_ctx);
  1550. return ret;
  1551. }
  1552. /*
  1553. * This checks the lookup capability of EC_GROUP_check_named_curve()
  1554. * when the given group was created with explicit parameters.
  1555. *
  1556. * It is possible to retrieve an alternative alias that does not match
  1557. * the original nid in this case.
  1558. */
  1559. static int check_named_curve_lookup_test(int id)
  1560. {
  1561. int ret = 0, nid, rv = 0;
  1562. EC_GROUP *g = NULL , *ga = NULL;
  1563. ECPARAMETERS *p = NULL, *pa = NULL;
  1564. BN_CTX *ctx = NULL;
  1565. /* Do some setup */
  1566. nid = curves[id].nid;
  1567. if (!TEST_ptr(ctx = BN_CTX_new())
  1568. || !TEST_ptr(g = EC_GROUP_new_by_curve_name(nid))
  1569. || !TEST_ptr(p = EC_GROUP_get_ecparameters(g, NULL)))
  1570. goto err;
  1571. /* replace with group from explicit parameters */
  1572. EC_GROUP_free(g);
  1573. if (!TEST_ptr(g = EC_GROUP_new_from_ecparameters(p)))
  1574. goto err;
  1575. if (!TEST_int_gt(rv = EC_GROUP_check_named_curve(g, 0, NULL), 0))
  1576. goto err;
  1577. if (rv != nid) {
  1578. /*
  1579. * Found an alias:
  1580. * fail if the returned nid is not an alias of the original group.
  1581. *
  1582. * The comparison here is done by comparing two explicit
  1583. * parameter EC_GROUPs with EC_GROUP_cmp(), to ensure the
  1584. * comparison happens with unnamed EC_GROUPs using the same
  1585. * EC_METHODs.
  1586. */
  1587. if (!TEST_ptr(ga = EC_GROUP_new_by_curve_name(rv))
  1588. || !TEST_ptr(pa = EC_GROUP_get_ecparameters(ga, NULL)))
  1589. goto err;
  1590. /* replace with group from explicit parameters, then compare */
  1591. EC_GROUP_free(ga);
  1592. if (!TEST_ptr(ga = EC_GROUP_new_from_ecparameters(pa))
  1593. || !TEST_int_eq(EC_GROUP_cmp(g, ga, ctx), 0))
  1594. goto err;
  1595. }
  1596. ret = 1;
  1597. err:
  1598. EC_GROUP_free(g);
  1599. EC_GROUP_free(ga);
  1600. ECPARAMETERS_free(p);
  1601. ECPARAMETERS_free(pa);
  1602. BN_CTX_free(ctx);
  1603. return ret;
  1604. }
  1605. /*
  1606. * Sometime we cannot compare nids for equality, as the built-in curve table
  1607. * includes aliases with different names for the same curve.
  1608. *
  1609. * This function returns TRUE (1) if the checked nids are identical, or if they
  1610. * alias to the same curve. FALSE (0) otherwise.
  1611. */
  1612. static ossl_inline
  1613. int are_ec_nids_compatible(int n1d, int n2d)
  1614. {
  1615. int ret = 0;
  1616. switch (n1d) {
  1617. #ifndef OPENSSL_NO_EC2M
  1618. case NID_sect113r1:
  1619. case NID_wap_wsg_idm_ecid_wtls4:
  1620. ret = (n2d == NID_sect113r1 || n2d == NID_wap_wsg_idm_ecid_wtls4);
  1621. break;
  1622. case NID_sect163k1:
  1623. case NID_wap_wsg_idm_ecid_wtls3:
  1624. ret = (n2d == NID_sect163k1 || n2d == NID_wap_wsg_idm_ecid_wtls3);
  1625. break;
  1626. case NID_sect233k1:
  1627. case NID_wap_wsg_idm_ecid_wtls10:
  1628. ret = (n2d == NID_sect233k1 || n2d == NID_wap_wsg_idm_ecid_wtls10);
  1629. break;
  1630. case NID_sect233r1:
  1631. case NID_wap_wsg_idm_ecid_wtls11:
  1632. ret = (n2d == NID_sect233r1 || n2d == NID_wap_wsg_idm_ecid_wtls11);
  1633. break;
  1634. case NID_X9_62_c2pnb163v1:
  1635. case NID_wap_wsg_idm_ecid_wtls5:
  1636. ret = (n2d == NID_X9_62_c2pnb163v1
  1637. || n2d == NID_wap_wsg_idm_ecid_wtls5);
  1638. break;
  1639. #endif /* OPENSSL_NO_EC2M */
  1640. case NID_secp112r1:
  1641. case NID_wap_wsg_idm_ecid_wtls6:
  1642. ret = (n2d == NID_secp112r1 || n2d == NID_wap_wsg_idm_ecid_wtls6);
  1643. break;
  1644. case NID_secp160r2:
  1645. case NID_wap_wsg_idm_ecid_wtls7:
  1646. ret = (n2d == NID_secp160r2 || n2d == NID_wap_wsg_idm_ecid_wtls7);
  1647. break;
  1648. #ifdef OPENSSL_NO_EC_NISTP_64_GCC_128
  1649. case NID_secp224r1:
  1650. case NID_wap_wsg_idm_ecid_wtls12:
  1651. ret = (n2d == NID_secp224r1 || n2d == NID_wap_wsg_idm_ecid_wtls12);
  1652. break;
  1653. #else
  1654. /*
  1655. * For SEC P-224 we want to ensure that the SECP nid is returned, as
  1656. * that is associated with a specialized method.
  1657. */
  1658. case NID_wap_wsg_idm_ecid_wtls12:
  1659. ret = (n2d == NID_secp224r1);
  1660. break;
  1661. #endif /* def(OPENSSL_NO_EC_NISTP_64_GCC_128) */
  1662. default:
  1663. ret = (n1d == n2d);
  1664. }
  1665. return ret;
  1666. }
  1667. /*
  1668. * This checks that EC_GROUP_bew_from_ecparameters() returns a "named"
  1669. * EC_GROUP for built-in curves.
  1670. *
  1671. * Note that it is possible to retrieve an alternative alias that does not match
  1672. * the original nid.
  1673. *
  1674. * Ensure that the OPENSSL_EC_EXPLICIT_CURVE ASN1 flag is set.
  1675. */
  1676. static int check_named_curve_from_ecparameters(int id)
  1677. {
  1678. int ret = 0, nid, tnid;
  1679. EC_GROUP *group = NULL, *tgroup = NULL, *tmpg = NULL;
  1680. const EC_POINT *group_gen = NULL;
  1681. EC_POINT *other_gen = NULL;
  1682. BIGNUM *group_cofactor = NULL, *other_cofactor = NULL;
  1683. BIGNUM *other_gen_x = NULL, *other_gen_y = NULL;
  1684. const BIGNUM *group_order = NULL;
  1685. BIGNUM *other_order = NULL;
  1686. BN_CTX *bn_ctx = NULL;
  1687. static const unsigned char invalid_seed[] = "THIS IS NOT A VALID SEED";
  1688. static size_t invalid_seed_len = sizeof(invalid_seed);
  1689. ECPARAMETERS *params = NULL, *other_params = NULL;
  1690. EC_GROUP *g_ary[8] = {NULL};
  1691. EC_GROUP **g_next = &g_ary[0];
  1692. ECPARAMETERS *p_ary[8] = {NULL};
  1693. ECPARAMETERS **p_next = &p_ary[0];
  1694. /* Do some setup */
  1695. nid = curves[id].nid;
  1696. TEST_note("Curve %s", OBJ_nid2sn(nid));
  1697. if (!TEST_ptr(bn_ctx = BN_CTX_new()))
  1698. return ret;
  1699. BN_CTX_start(bn_ctx);
  1700. if (/* Allocations */
  1701. !TEST_ptr(group_cofactor = BN_CTX_get(bn_ctx))
  1702. || !TEST_ptr(other_gen_x = BN_CTX_get(bn_ctx))
  1703. || !TEST_ptr(other_gen_y = BN_CTX_get(bn_ctx))
  1704. || !TEST_ptr(other_order = BN_CTX_get(bn_ctx))
  1705. || !TEST_ptr(other_cofactor = BN_CTX_get(bn_ctx))
  1706. /* Generate reference group and params */
  1707. || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
  1708. || !TEST_ptr(params = EC_GROUP_get_ecparameters(group, NULL))
  1709. || !TEST_ptr(group_gen = EC_GROUP_get0_generator(group))
  1710. || !TEST_ptr(group_order = EC_GROUP_get0_order(group))
  1711. || !TEST_true(EC_GROUP_get_cofactor(group, group_cofactor, NULL))
  1712. /* compute `other_*` values */
  1713. || !TEST_ptr(tmpg = EC_GROUP_dup(group))
  1714. || !TEST_ptr(other_gen = EC_POINT_dup(group_gen, group))
  1715. || !TEST_true(EC_POINT_add(group, other_gen, group_gen, group_gen, NULL))
  1716. || !TEST_true(EC_POINT_get_affine_coordinates(group, other_gen,
  1717. other_gen_x, other_gen_y, bn_ctx))
  1718. || !TEST_true(BN_copy(other_order, group_order))
  1719. || !TEST_true(BN_add_word(other_order, 1))
  1720. || !TEST_true(BN_copy(other_cofactor, group_cofactor))
  1721. || !TEST_true(BN_add_word(other_cofactor, 1)))
  1722. goto err;
  1723. EC_POINT_free(other_gen);
  1724. other_gen = NULL;
  1725. if (!TEST_ptr(other_gen = EC_POINT_new(tmpg))
  1726. || !TEST_true(EC_POINT_set_affine_coordinates(tmpg, other_gen,
  1727. other_gen_x, other_gen_y,
  1728. bn_ctx)))
  1729. goto err;
  1730. /*
  1731. * ###########################
  1732. * # Actual tests start here #
  1733. * ###########################
  1734. */
  1735. /*
  1736. * Creating a group from built-in explicit parameters returns a
  1737. * "named" EC_GROUP
  1738. */
  1739. if (!TEST_ptr(tgroup = *g_next++ = EC_GROUP_new_from_ecparameters(params))
  1740. || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef))
  1741. goto err;
  1742. /*
  1743. * We cannot always guarantee the names match, as the built-in table
  1744. * contains aliases for the same curve with different names.
  1745. */
  1746. if (!TEST_true(are_ec_nids_compatible(nid, tnid))) {
  1747. TEST_info("nid = %s, tnid = %s", OBJ_nid2sn(nid), OBJ_nid2sn(tnid));
  1748. goto err;
  1749. }
  1750. /* Ensure that the OPENSSL_EC_EXPLICIT_CURVE ASN1 flag is set. */
  1751. if (!TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup), OPENSSL_EC_EXPLICIT_CURVE))
  1752. goto err;
  1753. /*
  1754. * An invalid seed in the parameters should be ignored: expect a "named"
  1755. * group.
  1756. */
  1757. if (!TEST_int_eq(EC_GROUP_set_seed(tmpg, invalid_seed, invalid_seed_len),
  1758. invalid_seed_len)
  1759. || !TEST_ptr(other_params = *p_next++ =
  1760. EC_GROUP_get_ecparameters(tmpg, NULL))
  1761. || !TEST_ptr(tgroup = *g_next++ =
  1762. EC_GROUP_new_from_ecparameters(other_params))
  1763. || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
  1764. || !TEST_true(are_ec_nids_compatible(nid, tnid))
  1765. || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
  1766. OPENSSL_EC_EXPLICIT_CURVE)) {
  1767. TEST_info("nid = %s, tnid = %s", OBJ_nid2sn(nid), OBJ_nid2sn(tnid));
  1768. goto err;
  1769. }
  1770. /*
  1771. * A null seed in the parameters should be ignored, as it is optional:
  1772. * expect a "named" group.
  1773. */
  1774. if (!TEST_int_eq(EC_GROUP_set_seed(tmpg, NULL, 0), 1)
  1775. || !TEST_ptr(other_params = *p_next++ =
  1776. EC_GROUP_get_ecparameters(tmpg, NULL))
  1777. || !TEST_ptr(tgroup = *g_next++ =
  1778. EC_GROUP_new_from_ecparameters(other_params))
  1779. || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
  1780. || !TEST_true(are_ec_nids_compatible(nid, tnid))
  1781. || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
  1782. OPENSSL_EC_EXPLICIT_CURVE)) {
  1783. TEST_info("nid = %s, tnid = %s", OBJ_nid2sn(nid), OBJ_nid2sn(tnid));
  1784. goto err;
  1785. }
  1786. /*
  1787. * Check that changing any of the generator parameters does not yield a
  1788. * match with the built-in curves
  1789. */
  1790. if (/* Other gen, same group order & cofactor */
  1791. !TEST_true(EC_GROUP_set_generator(tmpg, other_gen, group_order,
  1792. group_cofactor))
  1793. || !TEST_ptr(other_params = *p_next++ =
  1794. EC_GROUP_get_ecparameters(tmpg, NULL))
  1795. || !TEST_ptr(tgroup = *g_next++ =
  1796. EC_GROUP_new_from_ecparameters(other_params))
  1797. || !TEST_int_eq((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
  1798. /* Same gen & cofactor, different order */
  1799. || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, other_order,
  1800. group_cofactor))
  1801. || !TEST_ptr(other_params = *p_next++ =
  1802. EC_GROUP_get_ecparameters(tmpg, NULL))
  1803. || !TEST_ptr(tgroup = *g_next++ =
  1804. EC_GROUP_new_from_ecparameters(other_params))
  1805. || !TEST_int_eq((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
  1806. /* The order is not an optional field, so this should fail */
  1807. || !TEST_false(EC_GROUP_set_generator(tmpg, group_gen, NULL,
  1808. group_cofactor))
  1809. /* Check that a wrong cofactor is ignored, and we still match */
  1810. || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, group_order,
  1811. other_cofactor))
  1812. || !TEST_ptr(other_params = *p_next++ =
  1813. EC_GROUP_get_ecparameters(tmpg, NULL))
  1814. || !TEST_ptr(tgroup = *g_next++ =
  1815. EC_GROUP_new_from_ecparameters(other_params))
  1816. || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
  1817. || !TEST_true(are_ec_nids_compatible(nid, tnid))
  1818. || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
  1819. OPENSSL_EC_EXPLICIT_CURVE)
  1820. /* Check that if the cofactor is not set then it still matches */
  1821. || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, group_order,
  1822. NULL))
  1823. || !TEST_ptr(other_params = *p_next++ =
  1824. EC_GROUP_get_ecparameters(tmpg, NULL))
  1825. || !TEST_ptr(tgroup = *g_next++ =
  1826. EC_GROUP_new_from_ecparameters(other_params))
  1827. || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
  1828. || !TEST_true(are_ec_nids_compatible(nid, tnid))
  1829. || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
  1830. OPENSSL_EC_EXPLICIT_CURVE)
  1831. /* check that restoring the generator passes */
  1832. || !TEST_true(EC_GROUP_set_generator(tmpg, group_gen, group_order,
  1833. group_cofactor))
  1834. || !TEST_ptr(other_params = *p_next++ =
  1835. EC_GROUP_get_ecparameters(tmpg, NULL))
  1836. || !TEST_ptr(tgroup = *g_next++ =
  1837. EC_GROUP_new_from_ecparameters(other_params))
  1838. || !TEST_int_ne((tnid = EC_GROUP_get_curve_name(tgroup)), NID_undef)
  1839. || !TEST_true(are_ec_nids_compatible(nid, tnid))
  1840. || !TEST_int_eq(EC_GROUP_get_asn1_flag(tgroup),
  1841. OPENSSL_EC_EXPLICIT_CURVE))
  1842. goto err;
  1843. ret = 1;
  1844. err:
  1845. for (g_next = &g_ary[0]; g_next < g_ary + OSSL_NELEM(g_ary); g_next++)
  1846. EC_GROUP_free(*g_next);
  1847. for (p_next = &p_ary[0]; p_next < p_ary + OSSL_NELEM(g_ary); p_next++)
  1848. ECPARAMETERS_free(*p_next);
  1849. ECPARAMETERS_free(params);
  1850. EC_POINT_free(other_gen);
  1851. EC_GROUP_free(tmpg);
  1852. EC_GROUP_free(group);
  1853. BN_CTX_end(bn_ctx);
  1854. BN_CTX_free(bn_ctx);
  1855. return ret;
  1856. }
  1857. static int parameter_test(void)
  1858. {
  1859. EC_GROUP *group = NULL, *group2 = NULL;
  1860. ECPARAMETERS *ecparameters = NULL;
  1861. unsigned char *buf = NULL;
  1862. int r = 0, len;
  1863. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp384r1))
  1864. || !TEST_ptr(ecparameters = EC_GROUP_get_ecparameters(group, NULL))
  1865. || !TEST_ptr(group2 = EC_GROUP_new_from_ecparameters(ecparameters))
  1866. || !TEST_int_eq(EC_GROUP_cmp(group, group2, NULL), 0))
  1867. goto err;
  1868. EC_GROUP_free(group);
  1869. group = NULL;
  1870. /* Test the named curve encoding, which should be default. */
  1871. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(NID_secp521r1))
  1872. || !TEST_true((len = i2d_ECPKParameters(group, &buf)) >= 0)
  1873. || !TEST_mem_eq(buf, len, p521_named, sizeof(p521_named)))
  1874. goto err;
  1875. OPENSSL_free(buf);
  1876. buf = NULL;
  1877. /*
  1878. * Test the explicit encoding. P-521 requires correctly zero-padding the
  1879. * curve coefficients.
  1880. */
  1881. EC_GROUP_set_asn1_flag(group, OPENSSL_EC_EXPLICIT_CURVE);
  1882. if (!TEST_true((len = i2d_ECPKParameters(group, &buf)) >= 0)
  1883. || !TEST_mem_eq(buf, len, p521_explicit, sizeof(p521_explicit)))
  1884. goto err;
  1885. r = 1;
  1886. err:
  1887. EC_GROUP_free(group);
  1888. EC_GROUP_free(group2);
  1889. ECPARAMETERS_free(ecparameters);
  1890. OPENSSL_free(buf);
  1891. return r;
  1892. }
  1893. /*
  1894. * This test validates converting an EC_GROUP to an OSSL_PARAM array
  1895. * using EC_GROUP_to_params(). A named and an explicit curve are tested.
  1896. */
  1897. static int ossl_parameter_test(void)
  1898. {
  1899. EC_GROUP *group_nmd = NULL, *group_nmd2 = NULL, *group_nmd3 = NULL;
  1900. EC_GROUP *group_exp = NULL, *group_exp2 = NULL;
  1901. OSSL_PARAM *params_nmd = NULL, *params_nmd2 = NULL;
  1902. OSSL_PARAM *params_exp = NULL, *params_exp2 = NULL;
  1903. unsigned char *buf = NULL, *buf2 = NULL;
  1904. BN_CTX *bn_ctx = NULL;
  1905. OSSL_PARAM_BLD *bld = NULL;
  1906. BIGNUM *p, *a, *b;
  1907. const EC_POINT *group_gen = NULL;
  1908. size_t bsize;
  1909. int r = 0;
  1910. if (!TEST_ptr(bn_ctx = BN_CTX_new()))
  1911. goto err;
  1912. /* test named curve */
  1913. if (!TEST_ptr(group_nmd = EC_GROUP_new_by_curve_name(NID_secp384r1))
  1914. /* test with null BN_CTX */
  1915. || !TEST_ptr(params_nmd = EC_GROUP_to_params(
  1916. group_nmd, NULL, NULL, NULL))
  1917. || !TEST_ptr(group_nmd2 = EC_GROUP_new_from_params(
  1918. params_nmd, NULL, NULL))
  1919. || !TEST_int_eq(EC_GROUP_cmp(group_nmd, group_nmd2, NULL), 0)
  1920. /* test with BN_CTX set */
  1921. || !TEST_ptr(params_nmd2 = EC_GROUP_to_params(
  1922. group_nmd, NULL, NULL, bn_ctx))
  1923. || !TEST_ptr(group_nmd3 = EC_GROUP_new_from_params(
  1924. params_nmd2, NULL, NULL))
  1925. || !TEST_int_eq(EC_GROUP_cmp(group_nmd, group_nmd3, NULL), 0))
  1926. goto err;
  1927. /* test explicit curve */
  1928. if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()))
  1929. goto err;
  1930. BN_CTX_start(bn_ctx);
  1931. p = BN_CTX_get(bn_ctx);
  1932. a = BN_CTX_get(bn_ctx);
  1933. b = BN_CTX_get(bn_ctx);
  1934. if (!TEST_true(EC_GROUP_get_curve(group_nmd, p, a, b, bn_ctx))
  1935. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(
  1936. bld, OSSL_PKEY_PARAM_EC_FIELD_TYPE, SN_X9_62_prime_field, 0))
  1937. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, p))
  1938. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, a))
  1939. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, b)))
  1940. goto err;
  1941. if (EC_GROUP_get0_seed(group_nmd) != NULL) {
  1942. if (!TEST_true(OSSL_PARAM_BLD_push_octet_string(
  1943. bld, OSSL_PKEY_PARAM_EC_SEED, EC_GROUP_get0_seed(group_nmd),
  1944. EC_GROUP_get_seed_len(group_nmd))))
  1945. goto err;
  1946. }
  1947. if (EC_GROUP_get0_cofactor(group_nmd) != NULL) {
  1948. if (!TEST_true(OSSL_PARAM_BLD_push_BN(
  1949. bld, OSSL_PKEY_PARAM_EC_COFACTOR,
  1950. EC_GROUP_get0_cofactor(group_nmd))))
  1951. goto err;
  1952. }
  1953. if (!TEST_ptr(group_gen = EC_GROUP_get0_generator(group_nmd))
  1954. || !TEST_size_t_gt(bsize = EC_POINT_point2oct(
  1955. group_nmd, EC_GROUP_get0_generator(group_nmd),
  1956. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, bn_ctx), 0)
  1957. || !TEST_ptr(buf2 = OPENSSL_malloc(bsize))
  1958. || !TEST_size_t_eq(EC_POINT_point2oct(
  1959. group_nmd, EC_GROUP_get0_generator(group_nmd),
  1960. POINT_CONVERSION_UNCOMPRESSED, buf2, bsize, bn_ctx), bsize)
  1961. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(
  1962. bld, OSSL_PKEY_PARAM_EC_GENERATOR, buf2, bsize))
  1963. || !TEST_true(OSSL_PARAM_BLD_push_BN(
  1964. bld, OSSL_PKEY_PARAM_EC_ORDER, EC_GROUP_get0_order(group_nmd))))
  1965. goto err;
  1966. if (!TEST_ptr(params_exp = OSSL_PARAM_BLD_to_param(bld))
  1967. || !TEST_ptr(group_exp =
  1968. EC_GROUP_new_from_params(params_exp, NULL, NULL))
  1969. || !TEST_ptr(params_exp2 =
  1970. EC_GROUP_to_params(group_exp, NULL, NULL, NULL))
  1971. || !TEST_ptr(group_exp2 =
  1972. EC_GROUP_new_from_params(params_exp2, NULL, NULL))
  1973. || !TEST_int_eq(EC_GROUP_cmp(group_exp, group_exp2, NULL), 0))
  1974. goto err;
  1975. r = 1;
  1976. err:
  1977. EC_GROUP_free(group_nmd);
  1978. EC_GROUP_free(group_nmd2);
  1979. EC_GROUP_free(group_nmd3);
  1980. OSSL_PARAM_free(params_nmd);
  1981. OSSL_PARAM_free(params_nmd2);
  1982. OPENSSL_free(buf);
  1983. EC_GROUP_free(group_exp);
  1984. EC_GROUP_free(group_exp2);
  1985. BN_CTX_end(bn_ctx);
  1986. BN_CTX_free(bn_ctx);
  1987. OPENSSL_free(buf2);
  1988. OSSL_PARAM_BLD_free(bld);
  1989. OSSL_PARAM_free(params_exp);
  1990. OSSL_PARAM_free(params_exp2);
  1991. return r;
  1992. }
  1993. /*-
  1994. * random 256-bit explicit parameters curve, cofactor absent
  1995. * order: 0x0c38d96a9f892b88772ec2e39614a82f4f (132 bit)
  1996. * cofactor: 0x12bc94785251297abfafddf1565100da (125 bit)
  1997. */
  1998. static const unsigned char params_cf_pass[] = {
  1999. 0x30, 0x81, 0xcd, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86,
  2000. 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xe5, 0x00, 0x1f, 0xc5,
  2001. 0xca, 0x71, 0x9d, 0x8e, 0xf7, 0x07, 0x4b, 0x48, 0x37, 0xf9, 0x33, 0x2d,
  2002. 0x71, 0xbf, 0x79, 0xe7, 0xdc, 0x91, 0xc2, 0xff, 0xb6, 0x7b, 0xc3, 0x93,
  2003. 0x44, 0x88, 0xe6, 0x91, 0x30, 0x44, 0x04, 0x20, 0xe5, 0x00, 0x1f, 0xc5,
  2004. 0xca, 0x71, 0x9d, 0x8e, 0xf7, 0x07, 0x4b, 0x48, 0x37, 0xf9, 0x33, 0x2d,
  2005. 0x71, 0xbf, 0x79, 0xe7, 0xdc, 0x91, 0xc2, 0xff, 0xb6, 0x7b, 0xc3, 0x93,
  2006. 0x44, 0x88, 0xe6, 0x8e, 0x04, 0x20, 0x18, 0x8c, 0x59, 0x57, 0xc4, 0xbc,
  2007. 0x85, 0x57, 0xc3, 0x66, 0x9f, 0x89, 0xd5, 0x92, 0x0d, 0x7e, 0x42, 0x27,
  2008. 0x07, 0x64, 0xaa, 0x26, 0xed, 0x89, 0xc4, 0x09, 0x05, 0x4d, 0xc7, 0x23,
  2009. 0x47, 0xda, 0x04, 0x41, 0x04, 0x1b, 0x6b, 0x41, 0x0b, 0xf9, 0xfb, 0x77,
  2010. 0xfd, 0x50, 0xb7, 0x3e, 0x23, 0xa3, 0xec, 0x9a, 0x3b, 0x09, 0x31, 0x6b,
  2011. 0xfa, 0xf6, 0xce, 0x1f, 0xff, 0xeb, 0x57, 0x93, 0x24, 0x70, 0xf3, 0xf4,
  2012. 0xba, 0x7e, 0xfa, 0x86, 0x6e, 0x19, 0x89, 0xe3, 0x55, 0x6d, 0x5a, 0xe9,
  2013. 0xc0, 0x3d, 0xbc, 0xfb, 0xaf, 0xad, 0xd4, 0x7e, 0xa6, 0xe5, 0xfa, 0x1a,
  2014. 0x58, 0x07, 0x9e, 0x8f, 0x0d, 0x3b, 0xf7, 0x38, 0xca, 0x02, 0x11, 0x0c,
  2015. 0x38, 0xd9, 0x6a, 0x9f, 0x89, 0x2b, 0x88, 0x77, 0x2e, 0xc2, 0xe3, 0x96,
  2016. 0x14, 0xa8, 0x2f, 0x4f
  2017. };
  2018. /*-
  2019. * random 256-bit explicit parameters curve, cofactor absent
  2020. * order: 0x045a75c0c17228ebd9b169a10e34a22101 (131 bit)
  2021. * cofactor: 0x2e134b4ede82649f67a2e559d361e5fe (126 bit)
  2022. */
  2023. static const unsigned char params_cf_fail[] = {
  2024. 0x30, 0x81, 0xcd, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86,
  2025. 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xc8, 0x95, 0x27, 0x37,
  2026. 0xe8, 0xe1, 0xfd, 0xcc, 0xf9, 0x6e, 0x0c, 0xa6, 0x21, 0xc1, 0x7d, 0x6b,
  2027. 0x9d, 0x44, 0x42, 0xea, 0x73, 0x4e, 0x04, 0xb6, 0xac, 0x62, 0x50, 0xd0,
  2028. 0x33, 0xc2, 0xea, 0x13, 0x30, 0x44, 0x04, 0x20, 0xc8, 0x95, 0x27, 0x37,
  2029. 0xe8, 0xe1, 0xfd, 0xcc, 0xf9, 0x6e, 0x0c, 0xa6, 0x21, 0xc1, 0x7d, 0x6b,
  2030. 0x9d, 0x44, 0x42, 0xea, 0x73, 0x4e, 0x04, 0xb6, 0xac, 0x62, 0x50, 0xd0,
  2031. 0x33, 0xc2, 0xea, 0x10, 0x04, 0x20, 0xbf, 0xa6, 0xa8, 0x05, 0x1d, 0x09,
  2032. 0xac, 0x70, 0x39, 0xbb, 0x4d, 0xb2, 0x90, 0x8a, 0x15, 0x41, 0x14, 0x1d,
  2033. 0x11, 0x86, 0x9f, 0x13, 0xa2, 0x63, 0x1a, 0xda, 0x95, 0x22, 0x4d, 0x02,
  2034. 0x15, 0x0a, 0x04, 0x41, 0x04, 0xaf, 0x16, 0x71, 0xf9, 0xc4, 0xc8, 0x59,
  2035. 0x1d, 0xa3, 0x6f, 0xe7, 0xc3, 0x57, 0xa1, 0xfa, 0x9f, 0x49, 0x7c, 0x11,
  2036. 0x27, 0x05, 0xa0, 0x7f, 0xff, 0xf9, 0xe0, 0xe7, 0x92, 0xdd, 0x9c, 0x24,
  2037. 0x8e, 0xc7, 0xb9, 0x52, 0x71, 0x3f, 0xbc, 0x7f, 0x6a, 0x9f, 0x35, 0x70,
  2038. 0xe1, 0x27, 0xd5, 0x35, 0x8a, 0x13, 0xfa, 0xa8, 0x33, 0x3e, 0xd4, 0x73,
  2039. 0x1c, 0x14, 0x58, 0x9e, 0xc7, 0x0a, 0x87, 0x65, 0x8d, 0x02, 0x11, 0x04,
  2040. 0x5a, 0x75, 0xc0, 0xc1, 0x72, 0x28, 0xeb, 0xd9, 0xb1, 0x69, 0xa1, 0x0e,
  2041. 0x34, 0xa2, 0x21, 0x01
  2042. };
  2043. /*-
  2044. * Test two random 256-bit explicit parameters curves with absent cofactor.
  2045. * The two curves are chosen to roughly straddle the bounds at which the lib
  2046. * can compute the cofactor automatically, roughly 4*sqrt(p). So test that:
  2047. *
  2048. * - params_cf_pass: order is sufficiently close to p to compute cofactor
  2049. * - params_cf_fail: order is too far away from p to compute cofactor
  2050. *
  2051. * For standards-compliant curves, cofactor is chosen as small as possible.
  2052. * So you can see neither of these curves are fit for cryptographic use.
  2053. *
  2054. * Some standards even mandate an upper bound on the cofactor, e.g. SECG1 v2:
  2055. * h <= 2**(t/8) where t is the security level of the curve, for which the lib
  2056. * will always succeed in computing the cofactor. Neither of these curves
  2057. * conform to that -- this is just robustness testing.
  2058. */
  2059. static int cofactor_range_test(void)
  2060. {
  2061. EC_GROUP *group = NULL;
  2062. BIGNUM *cf = NULL;
  2063. int ret = 0;
  2064. const unsigned char *b1 = (const unsigned char *)params_cf_fail;
  2065. const unsigned char *b2 = (const unsigned char *)params_cf_pass;
  2066. if (!TEST_ptr(group = d2i_ECPKParameters(NULL, &b1, sizeof(params_cf_fail)))
  2067. || !TEST_BN_eq_zero(EC_GROUP_get0_cofactor(group))
  2068. || !TEST_ptr(group = d2i_ECPKParameters(&group, &b2,
  2069. sizeof(params_cf_pass)))
  2070. || !TEST_int_gt(BN_hex2bn(&cf, "12bc94785251297abfafddf1565100da"), 0)
  2071. || !TEST_BN_eq(cf, EC_GROUP_get0_cofactor(group)))
  2072. goto err;
  2073. ret = 1;
  2074. err:
  2075. BN_free(cf);
  2076. EC_GROUP_free(group);
  2077. return ret;
  2078. }
  2079. /*-
  2080. * For named curves, test that:
  2081. * - the lib correctly computes the cofactor if passed a NULL or zero cofactor
  2082. * - a nonsensical cofactor throws an error (negative test)
  2083. * - nonsensical orders throw errors (negative tests)
  2084. */
  2085. static int cardinality_test(int n)
  2086. {
  2087. int ret = 0, is_binary = 0;
  2088. int nid = curves[n].nid;
  2089. BN_CTX *ctx = NULL;
  2090. EC_GROUP *g1 = NULL, *g2 = NULL;
  2091. EC_POINT *g2_gen = NULL;
  2092. BIGNUM *g1_p = NULL, *g1_a = NULL, *g1_b = NULL, *g1_x = NULL, *g1_y = NULL,
  2093. *g1_order = NULL, *g1_cf = NULL, *g2_cf = NULL;
  2094. TEST_info("Curve %s cardinality test", OBJ_nid2sn(nid));
  2095. if (!TEST_ptr(ctx = BN_CTX_new())
  2096. || !TEST_ptr(g1 = EC_GROUP_new_by_curve_name(nid))) {
  2097. BN_CTX_free(ctx);
  2098. return 0;
  2099. }
  2100. is_binary = (EC_GROUP_get_field_type(g1) == NID_X9_62_characteristic_two_field);
  2101. BN_CTX_start(ctx);
  2102. g1_p = BN_CTX_get(ctx);
  2103. g1_a = BN_CTX_get(ctx);
  2104. g1_b = BN_CTX_get(ctx);
  2105. g1_x = BN_CTX_get(ctx);
  2106. g1_y = BN_CTX_get(ctx);
  2107. g1_order = BN_CTX_get(ctx);
  2108. g1_cf = BN_CTX_get(ctx);
  2109. if (!TEST_ptr(g2_cf = BN_CTX_get(ctx))
  2110. /* pull out the explicit curve parameters */
  2111. || !TEST_true(EC_GROUP_get_curve(g1, g1_p, g1_a, g1_b, ctx))
  2112. || !TEST_true(EC_POINT_get_affine_coordinates(g1,
  2113. EC_GROUP_get0_generator(g1), g1_x, g1_y, ctx))
  2114. || !TEST_true(BN_copy(g1_order, EC_GROUP_get0_order(g1)))
  2115. || !TEST_true(EC_GROUP_get_cofactor(g1, g1_cf, ctx))
  2116. /* construct g2 manually with g1 parameters */
  2117. #ifndef OPENSSL_NO_EC2M
  2118. || !TEST_ptr(g2 = (is_binary) ?
  2119. EC_GROUP_new_curve_GF2m(g1_p, g1_a, g1_b, ctx) :
  2120. EC_GROUP_new_curve_GFp(g1_p, g1_a, g1_b, ctx))
  2121. #else
  2122. || !TEST_int_eq(0, is_binary)
  2123. || !TEST_ptr(g2 = EC_GROUP_new_curve_GFp(g1_p, g1_a, g1_b, ctx))
  2124. #endif
  2125. || !TEST_ptr(g2_gen = EC_POINT_new(g2))
  2126. || !TEST_true(EC_POINT_set_affine_coordinates(g2, g2_gen, g1_x, g1_y, ctx))
  2127. /* pass NULL cofactor: lib should compute it */
  2128. || !TEST_true(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL))
  2129. || !TEST_true(EC_GROUP_get_cofactor(g2, g2_cf, ctx))
  2130. || !TEST_BN_eq(g1_cf, g2_cf)
  2131. /* pass zero cofactor: lib should compute it */
  2132. || !TEST_true(BN_set_word(g2_cf, 0))
  2133. || !TEST_true(EC_GROUP_set_generator(g2, g2_gen, g1_order, g2_cf))
  2134. || !TEST_true(EC_GROUP_get_cofactor(g2, g2_cf, ctx))
  2135. || !TEST_BN_eq(g1_cf, g2_cf)
  2136. /* negative test for invalid cofactor */
  2137. || !TEST_true(BN_set_word(g2_cf, 0))
  2138. || !TEST_true(BN_sub(g2_cf, g2_cf, BN_value_one()))
  2139. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, g2_cf))
  2140. /* negative test for NULL order */
  2141. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, NULL, NULL))
  2142. /* negative test for zero order */
  2143. || !TEST_true(BN_set_word(g1_order, 0))
  2144. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL))
  2145. /* negative test for negative order */
  2146. || !TEST_true(BN_set_word(g2_cf, 0))
  2147. || !TEST_true(BN_sub(g2_cf, g2_cf, BN_value_one()))
  2148. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL))
  2149. /* negative test for too large order */
  2150. || !TEST_true(BN_lshift(g1_order, g1_p, 2))
  2151. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL)))
  2152. goto err;
  2153. ret = 1;
  2154. err:
  2155. EC_POINT_free(g2_gen);
  2156. EC_GROUP_free(g1);
  2157. EC_GROUP_free(g2);
  2158. BN_CTX_end(ctx);
  2159. BN_CTX_free(ctx);
  2160. return ret;
  2161. }
  2162. static int check_ec_key_field_public_range_test(int id)
  2163. {
  2164. int ret = 0, type = 0;
  2165. const EC_POINT *pub = NULL;
  2166. const EC_GROUP *group = NULL;
  2167. const BIGNUM *field = NULL;
  2168. BIGNUM *x = NULL, *y = NULL;
  2169. EC_KEY *key = NULL;
  2170. if (!TEST_ptr(x = BN_new())
  2171. || !TEST_ptr(y = BN_new())
  2172. || !TEST_ptr(key = EC_KEY_new_by_curve_name(curves[id].nid))
  2173. || !TEST_ptr(group = EC_KEY_get0_group(key))
  2174. || !TEST_ptr(field = EC_GROUP_get0_field(group))
  2175. || !TEST_int_gt(EC_KEY_generate_key(key), 0)
  2176. || !TEST_int_gt(EC_KEY_check_key(key), 0)
  2177. || !TEST_ptr(pub = EC_KEY_get0_public_key(key))
  2178. || !TEST_int_gt(EC_POINT_get_affine_coordinates(group, pub, x, y,
  2179. NULL), 0))
  2180. goto err;
  2181. /*
  2182. * Make the public point out of range by adding the field (which will still
  2183. * be the same point on the curve). The add is different for char2 fields.
  2184. */
  2185. type = EC_GROUP_get_field_type(group);
  2186. #ifndef OPENSSL_NO_EC2M
  2187. if (type == NID_X9_62_characteristic_two_field) {
  2188. /* test for binary curves */
  2189. if (!TEST_true(BN_GF2m_add(x, x, field)))
  2190. goto err;
  2191. } else
  2192. #endif
  2193. if (type == NID_X9_62_prime_field) {
  2194. /* test for prime curves */
  2195. if (!TEST_true(BN_add(x, x, field)))
  2196. goto err;
  2197. } else {
  2198. /* this should never happen */
  2199. TEST_error("Unsupported EC_METHOD field_type");
  2200. goto err;
  2201. }
  2202. if (!TEST_int_le(EC_KEY_set_public_key_affine_coordinates(key, x, y), 0))
  2203. goto err;
  2204. ret = 1;
  2205. err:
  2206. BN_free(x);
  2207. BN_free(y);
  2208. EC_KEY_free(key);
  2209. return ret;
  2210. }
  2211. /*
  2212. * Helper for ec_point_hex2point_test
  2213. *
  2214. * Self-tests EC_POINT_point2hex() against EC_POINT_hex2point() for the given
  2215. * (group,P) pair.
  2216. *
  2217. * If P is NULL use point at infinity.
  2218. */
  2219. static ossl_inline
  2220. int ec_point_hex2point_test_helper(const EC_GROUP *group, const EC_POINT *P,
  2221. point_conversion_form_t form,
  2222. BN_CTX *bnctx)
  2223. {
  2224. int ret = 0;
  2225. EC_POINT *Q = NULL, *Pinf = NULL;
  2226. char *hex = NULL;
  2227. if (P == NULL) {
  2228. /* If P is NULL use point at infinity. */
  2229. if (!TEST_ptr(Pinf = EC_POINT_new(group))
  2230. || !TEST_true(EC_POINT_set_to_infinity(group, Pinf)))
  2231. goto err;
  2232. P = Pinf;
  2233. }
  2234. if (!TEST_ptr(hex = EC_POINT_point2hex(group, P, form, bnctx))
  2235. || !TEST_ptr(Q = EC_POINT_hex2point(group, hex, NULL, bnctx))
  2236. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, bnctx)))
  2237. goto err;
  2238. /*
  2239. * The next check is most likely superfluous, as EC_POINT_cmp should already
  2240. * cover this.
  2241. * Nonetheless it increases the test coverage for EC_POINT_is_at_infinity,
  2242. * so we include it anyway!
  2243. */
  2244. if (Pinf != NULL
  2245. && !TEST_true(EC_POINT_is_at_infinity(group, Q)))
  2246. goto err;
  2247. ret = 1;
  2248. err:
  2249. EC_POINT_free(Pinf);
  2250. OPENSSL_free(hex);
  2251. EC_POINT_free(Q);
  2252. return ret;
  2253. }
  2254. /*
  2255. * This test self-validates EC_POINT_hex2point() and EC_POINT_point2hex()
  2256. */
  2257. static int ec_point_hex2point_test(int id)
  2258. {
  2259. int ret = 0, nid;
  2260. EC_GROUP *group = NULL;
  2261. const EC_POINT *G = NULL;
  2262. EC_POINT *P = NULL;
  2263. BN_CTX *bnctx = NULL;
  2264. /* Do some setup */
  2265. nid = curves[id].nid;
  2266. if (!TEST_ptr(bnctx = BN_CTX_new())
  2267. || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
  2268. || !TEST_ptr(G = EC_GROUP_get0_generator(group))
  2269. || !TEST_ptr(P = EC_POINT_dup(G, group)))
  2270. goto err;
  2271. if (!TEST_true(ec_point_hex2point_test_helper(group, P,
  2272. POINT_CONVERSION_COMPRESSED,
  2273. bnctx))
  2274. || !TEST_true(ec_point_hex2point_test_helper(group, NULL,
  2275. POINT_CONVERSION_COMPRESSED,
  2276. bnctx))
  2277. || !TEST_true(ec_point_hex2point_test_helper(group, P,
  2278. POINT_CONVERSION_UNCOMPRESSED,
  2279. bnctx))
  2280. || !TEST_true(ec_point_hex2point_test_helper(group, NULL,
  2281. POINT_CONVERSION_UNCOMPRESSED,
  2282. bnctx))
  2283. || !TEST_true(ec_point_hex2point_test_helper(group, P,
  2284. POINT_CONVERSION_HYBRID,
  2285. bnctx))
  2286. || !TEST_true(ec_point_hex2point_test_helper(group, NULL,
  2287. POINT_CONVERSION_HYBRID,
  2288. bnctx)))
  2289. goto err;
  2290. ret = 1;
  2291. err:
  2292. EC_POINT_free(P);
  2293. EC_GROUP_free(group);
  2294. BN_CTX_free(bnctx);
  2295. return ret;
  2296. }
  2297. static int do_test_custom_explicit_fromdata(EC_GROUP *group, BN_CTX *ctx,
  2298. unsigned char *gen, int gen_size)
  2299. {
  2300. int ret = 0, i_out;
  2301. EVP_PKEY_CTX *pctx = NULL;
  2302. EVP_PKEY *pkeyparam = NULL;
  2303. OSSL_PARAM_BLD *bld = NULL;
  2304. const char *field_name;
  2305. OSSL_PARAM *params = NULL;
  2306. const OSSL_PARAM *gettable;
  2307. BIGNUM *p, *a, *b;
  2308. BIGNUM *p_out = NULL, *a_out = NULL, *b_out = NULL;
  2309. BIGNUM *order_out = NULL, *cofactor_out = NULL;
  2310. char name[80];
  2311. unsigned char buf[1024];
  2312. size_t buf_len, name_len;
  2313. #ifndef OPENSSL_NO_EC2M
  2314. unsigned int k1 = 0, k2 = 0, k3 = 0;
  2315. const char *basis_name = NULL;
  2316. #endif
  2317. p = BN_CTX_get(ctx);
  2318. a = BN_CTX_get(ctx);
  2319. b = BN_CTX_get(ctx);
  2320. if (!TEST_ptr(b)
  2321. || !TEST_ptr(bld = OSSL_PARAM_BLD_new()))
  2322. goto err;
  2323. if (EC_GROUP_get_field_type(group) == NID_X9_62_prime_field) {
  2324. field_name = SN_X9_62_prime_field;
  2325. } else {
  2326. field_name = SN_X9_62_characteristic_two_field;
  2327. #ifndef OPENSSL_NO_EC2M
  2328. if (EC_GROUP_get_basis_type(group) == NID_X9_62_tpBasis) {
  2329. basis_name = SN_X9_62_tpBasis;
  2330. if (!TEST_true(EC_GROUP_get_trinomial_basis(group, &k1)))
  2331. goto err;
  2332. } else {
  2333. basis_name = SN_X9_62_ppBasis;
  2334. if (!TEST_true(EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)))
  2335. goto err;
  2336. }
  2337. #endif /* OPENSSL_NO_EC2M */
  2338. }
  2339. if (!TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx))
  2340. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  2341. OSSL_PKEY_PARAM_EC_FIELD_TYPE, field_name, 0))
  2342. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, p))
  2343. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, a))
  2344. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, b)))
  2345. goto err;
  2346. if (EC_GROUP_get0_seed(group) != NULL) {
  2347. if (!TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  2348. OSSL_PKEY_PARAM_EC_SEED, EC_GROUP_get0_seed(group),
  2349. EC_GROUP_get_seed_len(group))))
  2350. goto err;
  2351. }
  2352. if (EC_GROUP_get0_cofactor(group) != NULL) {
  2353. if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR,
  2354. EC_GROUP_get0_cofactor(group))))
  2355. goto err;
  2356. }
  2357. if (!TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  2358. OSSL_PKEY_PARAM_EC_GENERATOR, gen, gen_size))
  2359. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_ORDER,
  2360. EC_GROUP_get0_order(group))))
  2361. goto err;
  2362. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  2363. || !TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL))
  2364. || !TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
  2365. || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkeyparam,
  2366. EVP_PKEY_KEY_PARAMETERS, params), 0))
  2367. goto err;
  2368. /*- Check that all the set values are retrievable -*/
  2369. /* There should be no match to a group name since the generator changed */
  2370. if (!TEST_false(EVP_PKEY_get_utf8_string_param(pkeyparam,
  2371. OSSL_PKEY_PARAM_GROUP_NAME, name, sizeof(name),
  2372. &name_len)))
  2373. goto err;
  2374. /* The encoding should be explicit as it has no group */
  2375. if (!TEST_true(EVP_PKEY_get_utf8_string_param(pkeyparam,
  2376. OSSL_PKEY_PARAM_EC_ENCODING,
  2377. name, sizeof(name), &name_len))
  2378. || !TEST_str_eq(name, OSSL_PKEY_EC_ENCODING_EXPLICIT))
  2379. goto err;
  2380. if (!TEST_true(EVP_PKEY_get_utf8_string_param(pkeyparam,
  2381. OSSL_PKEY_PARAM_EC_FIELD_TYPE, name, sizeof(name),
  2382. &name_len))
  2383. || !TEST_str_eq(name, field_name))
  2384. goto err;
  2385. if (!TEST_true(EVP_PKEY_get_octet_string_param(pkeyparam,
  2386. OSSL_PKEY_PARAM_EC_GENERATOR, buf, sizeof(buf), &buf_len))
  2387. || !TEST_mem_eq(buf, (int)buf_len, gen, gen_size))
  2388. goto err;
  2389. if (!TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_P, &p_out))
  2390. || !TEST_BN_eq(p_out, p)
  2391. || !TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_A,
  2392. &a_out))
  2393. || !TEST_BN_eq(a_out, a)
  2394. || !TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_B,
  2395. &b_out))
  2396. || !TEST_BN_eq(b_out, b)
  2397. || !TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_ORDER,
  2398. &order_out))
  2399. || !TEST_BN_eq(order_out, EC_GROUP_get0_order(group)))
  2400. goto err;
  2401. if (EC_GROUP_get0_cofactor(group) != NULL) {
  2402. if (!TEST_true(EVP_PKEY_get_bn_param(pkeyparam,
  2403. OSSL_PKEY_PARAM_EC_COFACTOR, &cofactor_out))
  2404. || !TEST_BN_eq(cofactor_out, EC_GROUP_get0_cofactor(group)))
  2405. goto err;
  2406. }
  2407. if (EC_GROUP_get0_seed(group) != NULL) {
  2408. if (!TEST_true(EVP_PKEY_get_octet_string_param(pkeyparam,
  2409. OSSL_PKEY_PARAM_EC_SEED, buf, sizeof(buf), &buf_len))
  2410. || !TEST_mem_eq(buf, buf_len, EC_GROUP_get0_seed(group),
  2411. EC_GROUP_get_seed_len(group)))
  2412. goto err;
  2413. }
  2414. if (EC_GROUP_get_field_type(group) == NID_X9_62_prime_field) {
  2415. /* No extra fields should be set for a prime field */
  2416. if (!TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2417. OSSL_PKEY_PARAM_EC_CHAR2_M, &i_out))
  2418. || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2419. OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, &i_out))
  2420. || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2421. OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, &i_out))
  2422. || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2423. OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, &i_out))
  2424. || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2425. OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, &i_out))
  2426. || !TEST_false(EVP_PKEY_get_utf8_string_param(pkeyparam,
  2427. OSSL_PKEY_PARAM_EC_CHAR2_TYPE, name, sizeof(name),
  2428. &name_len)))
  2429. goto err;
  2430. } else {
  2431. #ifndef OPENSSL_NO_EC2M
  2432. if (!TEST_true(EVP_PKEY_get_int_param(pkeyparam,
  2433. OSSL_PKEY_PARAM_EC_CHAR2_M, &i_out))
  2434. || !TEST_int_eq(EC_GROUP_get_degree(group), i_out)
  2435. || !TEST_true(EVP_PKEY_get_utf8_string_param(pkeyparam,
  2436. OSSL_PKEY_PARAM_EC_CHAR2_TYPE, name, sizeof(name),
  2437. &name_len))
  2438. || !TEST_str_eq(name, basis_name))
  2439. goto err;
  2440. if (EC_GROUP_get_basis_type(group) == NID_X9_62_tpBasis) {
  2441. if (!TEST_true(EVP_PKEY_get_int_param(pkeyparam,
  2442. OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, &i_out))
  2443. || !TEST_int_eq(k1, i_out)
  2444. || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2445. OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, &i_out))
  2446. || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2447. OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, &i_out))
  2448. || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2449. OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, &i_out)))
  2450. goto err;
  2451. } else {
  2452. if (!TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2453. OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, &i_out))
  2454. || !TEST_true(EVP_PKEY_get_int_param(pkeyparam,
  2455. OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, &i_out))
  2456. || !TEST_int_eq(k1, i_out)
  2457. || !TEST_true(EVP_PKEY_get_int_param(pkeyparam,
  2458. OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, &i_out))
  2459. || !TEST_int_eq(k2, i_out)
  2460. || !TEST_true(EVP_PKEY_get_int_param(pkeyparam,
  2461. OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, &i_out))
  2462. || !TEST_int_eq(k3, i_out))
  2463. goto err;
  2464. }
  2465. #endif /* OPENSSL_NO_EC2M */
  2466. }
  2467. if (!TEST_ptr(gettable = EVP_PKEY_gettable_params(pkeyparam))
  2468. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_GROUP_NAME))
  2469. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_ENCODING))
  2470. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_FIELD_TYPE))
  2471. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_P))
  2472. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_A))
  2473. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_B))
  2474. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_GENERATOR))
  2475. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_ORDER))
  2476. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_COFACTOR))
  2477. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_SEED))
  2478. #ifndef OPENSSL_NO_EC2M
  2479. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_M))
  2480. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_TYPE))
  2481. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS))
  2482. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_PP_K1))
  2483. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_PP_K2))
  2484. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_PP_K3))
  2485. #endif
  2486. )
  2487. goto err;
  2488. ret = 1;
  2489. err:
  2490. BN_free(order_out);
  2491. BN_free(cofactor_out);
  2492. BN_free(a_out);
  2493. BN_free(b_out);
  2494. BN_free(p_out);
  2495. OSSL_PARAM_free(params);
  2496. OSSL_PARAM_BLD_free(bld);
  2497. EVP_PKEY_free(pkeyparam);
  2498. EVP_PKEY_CTX_free(pctx);
  2499. return ret;
  2500. }
  2501. /*
  2502. * check the EC_METHOD respects the supplied EC_GROUP_set_generator G
  2503. */
  2504. static int custom_generator_test(int id)
  2505. {
  2506. int ret = 0, nid, bsize;
  2507. EC_GROUP *group = NULL;
  2508. EC_POINT *G2 = NULL, *Q1 = NULL, *Q2 = NULL;
  2509. BN_CTX *ctx = NULL;
  2510. BIGNUM *k = NULL;
  2511. unsigned char *b1 = NULL, *b2 = NULL;
  2512. /* Do some setup */
  2513. nid = curves[id].nid;
  2514. TEST_note("Curve %s", OBJ_nid2sn(nid));
  2515. if (!TEST_ptr(ctx = BN_CTX_new()))
  2516. return 0;
  2517. BN_CTX_start(ctx);
  2518. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid)))
  2519. goto err;
  2520. /* expected byte length of encoded points */
  2521. bsize = (EC_GROUP_get_degree(group) + 7) / 8;
  2522. bsize = 1 + 2 * bsize; /* UNCOMPRESSED_POINT format */
  2523. if (!TEST_ptr(k = BN_CTX_get(ctx))
  2524. /* fetch a testing scalar k != 0,1 */
  2525. || !TEST_true(BN_rand(k, EC_GROUP_order_bits(group) - 1,
  2526. BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
  2527. /* make k even */
  2528. || !TEST_true(BN_clear_bit(k, 0))
  2529. || !TEST_ptr(G2 = EC_POINT_new(group))
  2530. || !TEST_ptr(Q1 = EC_POINT_new(group))
  2531. /* Q1 := kG */
  2532. || !TEST_true(EC_POINT_mul(group, Q1, k, NULL, NULL, ctx))
  2533. /* pull out the bytes of that */
  2534. || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
  2535. POINT_CONVERSION_UNCOMPRESSED, NULL,
  2536. 0, ctx), bsize)
  2537. || !TEST_ptr(b1 = OPENSSL_malloc(bsize))
  2538. || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
  2539. POINT_CONVERSION_UNCOMPRESSED, b1,
  2540. bsize, ctx), bsize)
  2541. /* new generator is G2 := 2G */
  2542. || !TEST_true(EC_POINT_dbl(group, G2, EC_GROUP_get0_generator(group),
  2543. ctx))
  2544. || !TEST_true(EC_GROUP_set_generator(group, G2,
  2545. EC_GROUP_get0_order(group),
  2546. EC_GROUP_get0_cofactor(group)))
  2547. || !TEST_ptr(Q2 = EC_POINT_new(group))
  2548. || !TEST_true(BN_rshift1(k, k))
  2549. /* Q2 := k/2 G2 */
  2550. || !TEST_true(EC_POINT_mul(group, Q2, k, NULL, NULL, ctx))
  2551. || !TEST_int_eq(EC_POINT_point2oct(group, Q2,
  2552. POINT_CONVERSION_UNCOMPRESSED, NULL,
  2553. 0, ctx), bsize)
  2554. || !TEST_ptr(b2 = OPENSSL_malloc(bsize))
  2555. || !TEST_int_eq(EC_POINT_point2oct(group, Q2,
  2556. POINT_CONVERSION_UNCOMPRESSED, b2,
  2557. bsize, ctx), bsize)
  2558. /* Q1 = kG = k/2 G2 = Q2 should hold */
  2559. || !TEST_mem_eq(b1, bsize, b2, bsize))
  2560. goto err;
  2561. if (!do_test_custom_explicit_fromdata(group, ctx, b1, bsize))
  2562. goto err;
  2563. ret = 1;
  2564. err:
  2565. EC_POINT_free(Q1);
  2566. EC_POINT_free(Q2);
  2567. EC_POINT_free(G2);
  2568. EC_GROUP_free(group);
  2569. BN_CTX_end(ctx);
  2570. BN_CTX_free(ctx);
  2571. OPENSSL_free(b1);
  2572. OPENSSL_free(b2);
  2573. return ret;
  2574. }
  2575. /*
  2576. * check creation of curves from explicit params through the public API
  2577. */
  2578. static int custom_params_test(int id)
  2579. {
  2580. int ret = 0, nid, bsize;
  2581. const char *curve_name = NULL;
  2582. EC_GROUP *group = NULL, *altgroup = NULL;
  2583. EC_POINT *G2 = NULL, *Q1 = NULL, *Q2 = NULL;
  2584. const EC_POINT *Q = NULL;
  2585. BN_CTX *ctx = NULL;
  2586. BIGNUM *k = NULL;
  2587. unsigned char *buf1 = NULL, *buf2 = NULL;
  2588. const BIGNUM *z = NULL, *cof = NULL, *priv1 = NULL;
  2589. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  2590. int is_prime = 0;
  2591. EC_KEY *eckey1 = NULL, *eckey2 = NULL;
  2592. EVP_PKEY *pkey1 = NULL, *pkey2 = NULL;
  2593. EVP_PKEY_CTX *pctx1 = NULL, *pctx2 = NULL;
  2594. size_t sslen, t;
  2595. unsigned char *pub1 = NULL , *pub2 = NULL;
  2596. OSSL_PARAM_BLD *param_bld = NULL;
  2597. OSSL_PARAM *params1 = NULL, *params2 = NULL;
  2598. /* Do some setup */
  2599. nid = curves[id].nid;
  2600. curve_name = OBJ_nid2sn(nid);
  2601. TEST_note("Curve %s", curve_name);
  2602. if (nid == NID_sm2)
  2603. return TEST_skip("custom params not supported with SM2");
  2604. if (!TEST_ptr(ctx = BN_CTX_new()))
  2605. return 0;
  2606. BN_CTX_start(ctx);
  2607. if (!TEST_ptr(p = BN_CTX_get(ctx))
  2608. || !TEST_ptr(a = BN_CTX_get(ctx))
  2609. || !TEST_ptr(b = BN_CTX_get(ctx))
  2610. || !TEST_ptr(k = BN_CTX_get(ctx)))
  2611. goto err;
  2612. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid)))
  2613. goto err;
  2614. is_prime = EC_GROUP_get_field_type(group) == NID_X9_62_prime_field;
  2615. #ifdef OPENSSL_NO_EC2M
  2616. if (!is_prime) {
  2617. ret = TEST_skip("binary curves not supported in this build");
  2618. goto err;
  2619. }
  2620. #endif
  2621. /* expected byte length of encoded points */
  2622. bsize = (EC_GROUP_get_degree(group) + 7) / 8;
  2623. bsize = 1 + 2 * bsize; /* UNCOMPRESSED_POINT format */
  2624. /* extract parameters from built-in curve */
  2625. if (!TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx))
  2626. || !TEST_ptr(G2 = EC_POINT_new(group))
  2627. /* new generator is G2 := 2G */
  2628. || !TEST_true(EC_POINT_dbl(group, G2,
  2629. EC_GROUP_get0_generator(group), ctx))
  2630. /* pull out the bytes of that */
  2631. || !TEST_int_eq(EC_POINT_point2oct(group, G2,
  2632. POINT_CONVERSION_UNCOMPRESSED,
  2633. NULL, 0, ctx), bsize)
  2634. || !TEST_ptr(buf1 = OPENSSL_malloc(bsize))
  2635. || !TEST_int_eq(EC_POINT_point2oct(group, G2,
  2636. POINT_CONVERSION_UNCOMPRESSED,
  2637. buf1, bsize, ctx), bsize)
  2638. || !TEST_ptr(z = EC_GROUP_get0_order(group))
  2639. || !TEST_ptr(cof = EC_GROUP_get0_cofactor(group))
  2640. )
  2641. goto err;
  2642. /* create a new group using same params (but different generator) */
  2643. if (is_prime) {
  2644. if (!TEST_ptr(altgroup = EC_GROUP_new_curve_GFp(p, a, b, ctx)))
  2645. goto err;
  2646. }
  2647. #ifndef OPENSSL_NO_EC2M
  2648. else {
  2649. if (!TEST_ptr(altgroup = EC_GROUP_new_curve_GF2m(p, a, b, ctx)))
  2650. goto err;
  2651. }
  2652. #endif
  2653. /* set 2*G as the generator of altgroup */
  2654. EC_POINT_free(G2); /* discard G2 as it refers to the original group */
  2655. if (!TEST_ptr(G2 = EC_POINT_new(altgroup))
  2656. || !TEST_true(EC_POINT_oct2point(altgroup, G2, buf1, bsize, ctx))
  2657. || !TEST_int_eq(EC_POINT_is_on_curve(altgroup, G2, ctx), 1)
  2658. || !TEST_true(EC_GROUP_set_generator(altgroup, G2, z, cof))
  2659. )
  2660. goto err;
  2661. /* verify math checks out */
  2662. if (/* allocate temporary points on group and altgroup */
  2663. !TEST_ptr(Q1 = EC_POINT_new(group))
  2664. || !TEST_ptr(Q2 = EC_POINT_new(altgroup))
  2665. /* fetch a testing scalar k != 0,1 */
  2666. || !TEST_true(BN_rand(k, EC_GROUP_order_bits(group) - 1,
  2667. BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
  2668. /* make k even */
  2669. || !TEST_true(BN_clear_bit(k, 0))
  2670. /* Q1 := kG on group */
  2671. || !TEST_true(EC_POINT_mul(group, Q1, k, NULL, NULL, ctx))
  2672. /* pull out the bytes of that */
  2673. || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
  2674. POINT_CONVERSION_UNCOMPRESSED,
  2675. NULL, 0, ctx), bsize)
  2676. || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
  2677. POINT_CONVERSION_UNCOMPRESSED,
  2678. buf1, bsize, ctx), bsize)
  2679. /* k := k/2 */
  2680. || !TEST_true(BN_rshift1(k, k))
  2681. /* Q2 := k/2 G2 on altgroup */
  2682. || !TEST_true(EC_POINT_mul(altgroup, Q2, k, NULL, NULL, ctx))
  2683. /* pull out the bytes of that */
  2684. || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q2,
  2685. POINT_CONVERSION_UNCOMPRESSED,
  2686. NULL, 0, ctx), bsize)
  2687. || !TEST_ptr(buf2 = OPENSSL_malloc(bsize))
  2688. || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q2,
  2689. POINT_CONVERSION_UNCOMPRESSED,
  2690. buf2, bsize, ctx), bsize)
  2691. /* Q1 = kG = k/2 G2 = Q2 should hold */
  2692. || !TEST_mem_eq(buf1, bsize, buf2, bsize))
  2693. goto err;
  2694. /* create two `EC_KEY`s on altgroup */
  2695. if (!TEST_ptr(eckey1 = EC_KEY_new())
  2696. || !TEST_true(EC_KEY_set_group(eckey1, altgroup))
  2697. || !TEST_true(EC_KEY_generate_key(eckey1))
  2698. || !TEST_ptr(eckey2 = EC_KEY_new())
  2699. || !TEST_true(EC_KEY_set_group(eckey2, altgroup))
  2700. || !TEST_true(EC_KEY_generate_key(eckey2)))
  2701. goto err;
  2702. /* retrieve priv1 for later */
  2703. if (!TEST_ptr(priv1 = EC_KEY_get0_private_key(eckey1)))
  2704. goto err;
  2705. /*
  2706. * retrieve bytes for pub1 for later
  2707. *
  2708. * We compute the pub key in the original group as we will later use it to
  2709. * define a provider key in the built-in group.
  2710. */
  2711. if (!TEST_true(EC_POINT_mul(group, Q1, priv1, NULL, NULL, ctx))
  2712. || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
  2713. POINT_CONVERSION_UNCOMPRESSED,
  2714. NULL, 0, ctx), bsize)
  2715. || !TEST_ptr(pub1 = OPENSSL_malloc(bsize))
  2716. || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
  2717. POINT_CONVERSION_UNCOMPRESSED,
  2718. pub1, bsize, ctx), bsize))
  2719. goto err;
  2720. /* retrieve bytes for pub2 for later */
  2721. if (!TEST_ptr(Q = EC_KEY_get0_public_key(eckey2))
  2722. || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q,
  2723. POINT_CONVERSION_UNCOMPRESSED,
  2724. NULL, 0, ctx), bsize)
  2725. || !TEST_ptr(pub2 = OPENSSL_malloc(bsize))
  2726. || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q,
  2727. POINT_CONVERSION_UNCOMPRESSED,
  2728. pub2, bsize, ctx), bsize))
  2729. goto err;
  2730. /* create two `EVP_PKEY`s from the `EC_KEY`s */
  2731. if (!TEST_ptr(pkey1 = EVP_PKEY_new())
  2732. || !TEST_int_eq(EVP_PKEY_assign_EC_KEY(pkey1, eckey1), 1))
  2733. goto err;
  2734. eckey1 = NULL; /* ownership passed to pkey1 */
  2735. if (!TEST_ptr(pkey2 = EVP_PKEY_new())
  2736. || !TEST_int_eq(EVP_PKEY_assign_EC_KEY(pkey2, eckey2), 1))
  2737. goto err;
  2738. eckey2 = NULL; /* ownership passed to pkey2 */
  2739. /* Compute keyexchange in both directions */
  2740. if (!TEST_ptr(pctx1 = EVP_PKEY_CTX_new(pkey1, NULL))
  2741. || !TEST_int_eq(EVP_PKEY_derive_init(pctx1), 1)
  2742. || !TEST_int_eq(EVP_PKEY_derive_set_peer(pctx1, pkey2), 1)
  2743. || !TEST_int_eq(EVP_PKEY_derive(pctx1, NULL, &sslen), 1)
  2744. || !TEST_int_gt(bsize, sslen)
  2745. || !TEST_int_eq(EVP_PKEY_derive(pctx1, buf1, &sslen), 1))
  2746. goto err;
  2747. if (!TEST_ptr(pctx2 = EVP_PKEY_CTX_new(pkey2, NULL))
  2748. || !TEST_int_eq(EVP_PKEY_derive_init(pctx2), 1)
  2749. || !TEST_int_eq(EVP_PKEY_derive_set_peer(pctx2, pkey1), 1)
  2750. || !TEST_int_eq(EVP_PKEY_derive(pctx2, NULL, &t), 1)
  2751. || !TEST_int_gt(bsize, t)
  2752. || !TEST_int_le(sslen, t)
  2753. || !TEST_int_eq(EVP_PKEY_derive(pctx2, buf2, &t), 1))
  2754. goto err;
  2755. /* Both sides should expect the same shared secret */
  2756. if (!TEST_mem_eq(buf1, sslen, buf2, t))
  2757. goto err;
  2758. /* Build parameters for provider-native keys */
  2759. if (!TEST_ptr(param_bld = OSSL_PARAM_BLD_new())
  2760. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(param_bld,
  2761. OSSL_PKEY_PARAM_GROUP_NAME,
  2762. curve_name, 0))
  2763. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(param_bld,
  2764. OSSL_PKEY_PARAM_PUB_KEY,
  2765. pub1, bsize))
  2766. || !TEST_true(OSSL_PARAM_BLD_push_BN(param_bld,
  2767. OSSL_PKEY_PARAM_PRIV_KEY,
  2768. priv1))
  2769. || !TEST_ptr(params1 = OSSL_PARAM_BLD_to_param(param_bld)))
  2770. goto err;
  2771. OSSL_PARAM_BLD_free(param_bld);
  2772. if (!TEST_ptr(param_bld = OSSL_PARAM_BLD_new())
  2773. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(param_bld,
  2774. OSSL_PKEY_PARAM_GROUP_NAME,
  2775. curve_name, 0))
  2776. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(param_bld,
  2777. OSSL_PKEY_PARAM_PUB_KEY,
  2778. pub2, bsize))
  2779. || !TEST_ptr(params2 = OSSL_PARAM_BLD_to_param(param_bld)))
  2780. goto err;
  2781. /* create two new provider-native `EVP_PKEY`s */
  2782. EVP_PKEY_CTX_free(pctx2);
  2783. if (!TEST_ptr(pctx2 = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL))
  2784. || !TEST_int_eq(EVP_PKEY_fromdata_init(pctx2), 1)
  2785. || !TEST_int_eq(EVP_PKEY_fromdata(pctx2, &pkey1, EVP_PKEY_KEYPAIR,
  2786. params1), 1)
  2787. || !TEST_int_eq(EVP_PKEY_fromdata(pctx2, &pkey2, EVP_PKEY_PUBLIC_KEY,
  2788. params2), 1))
  2789. goto err;
  2790. /* compute keyexchange once more using the provider keys */
  2791. EVP_PKEY_CTX_free(pctx1);
  2792. if (!TEST_ptr(pctx1 = EVP_PKEY_CTX_new(pkey1, NULL))
  2793. || !TEST_int_eq(EVP_PKEY_derive_init(pctx1), 1)
  2794. || !TEST_int_eq(EVP_PKEY_derive_set_peer(pctx1, pkey2), 1)
  2795. || !TEST_int_eq(EVP_PKEY_derive(pctx1, NULL, &t), 1)
  2796. || !TEST_int_gt(bsize, t)
  2797. || !TEST_int_le(sslen, t)
  2798. || !TEST_int_eq(EVP_PKEY_derive(pctx1, buf1, &t), 1)
  2799. /* compare with previous result */
  2800. || !TEST_mem_eq(buf1, t, buf2, sslen))
  2801. goto err;
  2802. ret = 1;
  2803. err:
  2804. BN_CTX_end(ctx);
  2805. BN_CTX_free(ctx);
  2806. OSSL_PARAM_BLD_free(param_bld);
  2807. OSSL_PARAM_free(params1);
  2808. OSSL_PARAM_free(params2);
  2809. EC_POINT_free(Q1);
  2810. EC_POINT_free(Q2);
  2811. EC_POINT_free(G2);
  2812. EC_GROUP_free(group);
  2813. EC_GROUP_free(altgroup);
  2814. OPENSSL_free(buf1);
  2815. OPENSSL_free(buf2);
  2816. OPENSSL_free(pub1);
  2817. OPENSSL_free(pub2);
  2818. EC_KEY_free(eckey1);
  2819. EC_KEY_free(eckey2);
  2820. EVP_PKEY_free(pkey1);
  2821. EVP_PKEY_free(pkey2);
  2822. EVP_PKEY_CTX_free(pctx1);
  2823. EVP_PKEY_CTX_free(pctx2);
  2824. return ret;
  2825. }
  2826. static int ec_d2i_publickey_test(void)
  2827. {
  2828. unsigned char buf[1000];
  2829. unsigned char *pubkey_enc = buf;
  2830. const unsigned char *pk_enc = pubkey_enc;
  2831. EVP_PKEY *gen_key = NULL, *decoded_key = NULL;
  2832. EVP_PKEY_CTX *pctx = NULL;
  2833. int pklen, ret = 0;
  2834. OSSL_PARAM params[2];
  2835. if (!TEST_ptr(gen_key = EVP_EC_gen("P-256")))
  2836. goto err;
  2837. if (!TEST_int_gt(pklen = i2d_PublicKey(gen_key, &pubkey_enc), 0))
  2838. goto err;
  2839. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  2840. "P-256", 0);
  2841. params[1] = OSSL_PARAM_construct_end();
  2842. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL))
  2843. || !TEST_true(EVP_PKEY_fromdata_init(pctx))
  2844. || !TEST_true(EVP_PKEY_fromdata(pctx, &decoded_key,
  2845. OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
  2846. params))
  2847. || !TEST_ptr(decoded_key)
  2848. || !TEST_ptr(decoded_key = d2i_PublicKey(EVP_PKEY_EC, &decoded_key,
  2849. &pk_enc, pklen)))
  2850. goto err;
  2851. if (!TEST_true(EVP_PKEY_eq(gen_key, decoded_key)))
  2852. goto err;
  2853. ret = 1;
  2854. err:
  2855. EVP_PKEY_CTX_free(pctx);
  2856. EVP_PKEY_free(gen_key);
  2857. EVP_PKEY_free(decoded_key);
  2858. return ret;
  2859. }
  2860. int setup_tests(void)
  2861. {
  2862. crv_len = EC_get_builtin_curves(NULL, 0);
  2863. if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
  2864. || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
  2865. return 0;
  2866. ADD_TEST(parameter_test);
  2867. ADD_TEST(ossl_parameter_test);
  2868. ADD_TEST(cofactor_range_test);
  2869. ADD_ALL_TESTS(cardinality_test, crv_len);
  2870. ADD_TEST(prime_field_tests);
  2871. #ifndef OPENSSL_NO_EC2M
  2872. ADD_TEST(hybrid_point_encoding_test);
  2873. ADD_TEST(char2_field_tests);
  2874. ADD_ALL_TESTS(char2_curve_test, OSSL_NELEM(char2_curve_tests));
  2875. #endif
  2876. ADD_ALL_TESTS(nistp_single_test, OSSL_NELEM(nistp_tests_params));
  2877. ADD_ALL_TESTS(internal_curve_test, crv_len);
  2878. ADD_ALL_TESTS(internal_curve_test_method, crv_len);
  2879. ADD_TEST(group_field_test);
  2880. ADD_ALL_TESTS(check_named_curve_test, crv_len);
  2881. ADD_ALL_TESTS(check_named_curve_lookup_test, crv_len);
  2882. ADD_ALL_TESTS(check_ec_key_field_public_range_test, crv_len);
  2883. ADD_ALL_TESTS(check_named_curve_from_ecparameters, crv_len);
  2884. ADD_ALL_TESTS(ec_point_hex2point_test, crv_len);
  2885. ADD_ALL_TESTS(custom_generator_test, crv_len);
  2886. ADD_ALL_TESTS(custom_params_test, crv_len);
  2887. ADD_TEST(ec_d2i_publickey_test);
  2888. return 1;
  2889. }
  2890. void cleanup_tests(void)
  2891. {
  2892. OPENSSL_free(curves);
  2893. }