ectest.c 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044
  1. /*
  2. * Copyright 2001-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
  4. *
  5. * Licensed under the Apache License 2.0 (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. */
  10. /*
  11. * EC_KEY low level APIs are deprecated for public use, but still ok for
  12. * internal use.
  13. */
  14. #include "internal/deprecated.h"
  15. #include <string.h>
  16. #include "internal/nelem.h"
  17. #include "testutil.h"
  18. #include <openssl/ec.h>
  19. #ifndef OPENSSL_NO_ENGINE
  20. # include <openssl/engine.h>
  21. #endif
  22. #include <openssl/err.h>
  23. #include <openssl/obj_mac.h>
  24. #include <openssl/objects.h>
  25. #include <openssl/rand.h>
  26. #include <openssl/bn.h>
  27. #include <openssl/opensslconf.h>
  28. #include <openssl/core_names.h>
  29. #include <openssl/param_build.h>
  30. #include <openssl/evp.h>
  31. static size_t crv_len = 0;
  32. static EC_builtin_curve *curves = NULL;
  33. /* test multiplication with group order, long and negative scalars */
  34. static int group_order_tests(EC_GROUP *group)
  35. {
  36. BIGNUM *n1 = NULL, *n2 = NULL, *order = NULL;
  37. EC_POINT *P = NULL, *Q = NULL, *R = NULL, *S = NULL;
  38. const EC_POINT *G = NULL;
  39. BN_CTX *ctx = NULL;
  40. int i = 0, r = 0;
  41. if (!TEST_ptr(n1 = BN_new())
  42. || !TEST_ptr(n2 = BN_new())
  43. || !TEST_ptr(order = BN_new())
  44. || !TEST_ptr(ctx = BN_CTX_new())
  45. || !TEST_ptr(G = EC_GROUP_get0_generator(group))
  46. || !TEST_ptr(P = EC_POINT_new(group))
  47. || !TEST_ptr(Q = EC_POINT_new(group))
  48. || !TEST_ptr(R = EC_POINT_new(group))
  49. || !TEST_ptr(S = EC_POINT_new(group)))
  50. goto err;
  51. if (!TEST_true(EC_GROUP_get_order(group, order, ctx))
  52. || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
  53. || !TEST_true(EC_POINT_is_at_infinity(group, Q))
  54. #ifndef OPENSSL_NO_DEPRECATED_3_0
  55. || !TEST_true(EC_GROUP_precompute_mult(group, ctx))
  56. #endif
  57. || !TEST_true(EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
  58. || !TEST_true(EC_POINT_is_at_infinity(group, Q))
  59. || !TEST_true(EC_POINT_copy(P, G))
  60. || !TEST_true(BN_one(n1))
  61. || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))
  62. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))
  63. || !TEST_true(BN_sub(n1, order, n1))
  64. || !TEST_true(EC_POINT_mul(group, Q, n1, NULL, NULL, ctx))
  65. || !TEST_true(EC_POINT_invert(group, Q, ctx))
  66. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))
  67. goto err;
  68. for (i = 1; i <= 2; i++) {
  69. #ifndef OPENSSL_NO_DEPRECATED_3_0
  70. const BIGNUM *scalars[6];
  71. const EC_POINT *points[6];
  72. #endif
  73. if (!TEST_true(BN_set_word(n1, i))
  74. /*
  75. * If i == 1, P will be the predefined generator for which
  76. * EC_GROUP_precompute_mult has set up precomputation.
  77. */
  78. || !TEST_true(EC_POINT_mul(group, P, n1, NULL, NULL, ctx))
  79. || (i == 1 && !TEST_int_eq(0, EC_POINT_cmp(group, P, G, ctx)))
  80. || !TEST_true(BN_one(n1))
  81. /* n1 = 1 - order */
  82. || !TEST_true(BN_sub(n1, n1, order))
  83. || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n1, ctx))
  84. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))
  85. /* n2 = 1 + order */
  86. || !TEST_true(BN_add(n2, order, BN_value_one()))
  87. || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  88. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx))
  89. /* n2 = (1 - order) * (1 + order) = 1 - order^2 */
  90. || !TEST_true(BN_mul(n2, n1, n2, ctx))
  91. || !TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  92. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, ctx)))
  93. goto err;
  94. /* n2 = order^2 - 1 */
  95. BN_set_negative(n2, 0);
  96. if (!TEST_true(EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  97. /* Add P to verify the result. */
  98. || !TEST_true(EC_POINT_add(group, Q, Q, P, ctx))
  99. || !TEST_true(EC_POINT_is_at_infinity(group, Q))
  100. || !TEST_false(EC_POINT_is_at_infinity(group, P)))
  101. goto err;
  102. #ifndef OPENSSL_NO_DEPRECATED_3_0
  103. /* Exercise EC_POINTs_mul, including corner cases. */
  104. scalars[0] = scalars[1] = BN_value_one();
  105. points[0] = points[1] = P;
  106. if (!TEST_true(EC_POINTs_mul(group, R, NULL, 2, points, scalars, ctx))
  107. || !TEST_true(EC_POINT_dbl(group, S, points[0], ctx))
  108. || !TEST_int_eq(0, EC_POINT_cmp(group, R, S, ctx)))
  109. goto err;
  110. scalars[0] = n1;
  111. points[0] = Q; /* => infinity */
  112. scalars[1] = n2;
  113. points[1] = P; /* => -P */
  114. scalars[2] = n1;
  115. points[2] = Q; /* => infinity */
  116. scalars[3] = n2;
  117. points[3] = Q; /* => infinity */
  118. scalars[4] = n1;
  119. points[4] = P; /* => P */
  120. scalars[5] = n2;
  121. points[5] = Q; /* => infinity */
  122. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx))
  123. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  124. goto err;
  125. #endif
  126. }
  127. r = 1;
  128. err:
  129. if (r == 0 && i != 0)
  130. TEST_info(i == 1 ? "allowing precomputation" :
  131. "without precomputation");
  132. EC_POINT_free(P);
  133. EC_POINT_free(Q);
  134. EC_POINT_free(R);
  135. EC_POINT_free(S);
  136. BN_free(n1);
  137. BN_free(n2);
  138. BN_free(order);
  139. BN_CTX_free(ctx);
  140. return r;
  141. }
  142. static int prime_field_tests(void)
  143. {
  144. BN_CTX *ctx = NULL;
  145. BIGNUM *p = NULL, *a = NULL, *b = NULL, *scalar3 = NULL;
  146. EC_GROUP *group = NULL;
  147. EC_POINT *P = NULL, *Q = NULL, *R = NULL;
  148. BIGNUM *x = NULL, *y = NULL, *z = NULL, *yplusone = NULL;
  149. #ifndef OPENSSL_NO_DEPRECATED_3_0
  150. const EC_POINT *points[4];
  151. const BIGNUM *scalars[4];
  152. #endif
  153. unsigned char buf[100];
  154. size_t len, r = 0;
  155. int k;
  156. if (!TEST_ptr(ctx = BN_CTX_new())
  157. || !TEST_ptr(p = BN_new())
  158. || !TEST_ptr(a = BN_new())
  159. || !TEST_ptr(b = BN_new())
  160. || !TEST_true(BN_hex2bn(&p, "17"))
  161. || !TEST_true(BN_hex2bn(&a, "1"))
  162. || !TEST_true(BN_hex2bn(&b, "1"))
  163. || !TEST_ptr(group = EC_GROUP_new_curve_GFp(p, a, b, ctx))
  164. || !TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx)))
  165. goto err;
  166. TEST_info("Curve defined by Weierstrass equation");
  167. TEST_note(" y^2 = x^3 + a*x + b (mod p)");
  168. test_output_bignum("a", a);
  169. test_output_bignum("b", b);
  170. test_output_bignum("p", p);
  171. buf[0] = 0;
  172. if (!TEST_ptr(P = EC_POINT_new(group))
  173. || !TEST_ptr(Q = EC_POINT_new(group))
  174. || !TEST_ptr(R = EC_POINT_new(group))
  175. || !TEST_true(EC_POINT_set_to_infinity(group, P))
  176. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  177. || !TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx))
  178. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))
  179. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  180. || !TEST_ptr(x = BN_new())
  181. || !TEST_ptr(y = BN_new())
  182. || !TEST_ptr(z = BN_new())
  183. || !TEST_ptr(yplusone = BN_new())
  184. || !TEST_true(BN_hex2bn(&x, "D"))
  185. || !TEST_true(EC_POINT_set_compressed_coordinates(group, Q, x, 1, ctx)))
  186. goto err;
  187. if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) {
  188. if (!TEST_true(EC_POINT_get_affine_coordinates(group, Q, x, y, ctx)))
  189. goto err;
  190. TEST_info("Point is not on curve");
  191. test_output_bignum("x", x);
  192. test_output_bignum("y", y);
  193. goto err;
  194. }
  195. TEST_note("A cyclic subgroup:");
  196. k = 100;
  197. do {
  198. if (!TEST_int_ne(k--, 0))
  199. goto err;
  200. if (EC_POINT_is_at_infinity(group, P)) {
  201. TEST_note(" point at infinity");
  202. } else {
  203. if (!TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y,
  204. ctx)))
  205. goto err;
  206. test_output_bignum("x", x);
  207. test_output_bignum("y", y);
  208. }
  209. if (!TEST_true(EC_POINT_copy(R, P))
  210. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)))
  211. goto err;
  212. } while (!EC_POINT_is_at_infinity(group, P));
  213. if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx))
  214. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  215. goto err;
  216. len =
  217. EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,
  218. sizeof(buf), ctx);
  219. if (!TEST_size_t_ne(len, 0)
  220. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  221. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  222. goto err;
  223. test_output_memory("Generator as octet string, compressed form:",
  224. buf, len);
  225. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
  226. buf, sizeof(buf), ctx);
  227. if (!TEST_size_t_ne(len, 0)
  228. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  229. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  230. goto err;
  231. test_output_memory("Generator as octet string, uncompressed form:",
  232. buf, len);
  233. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID,
  234. buf, sizeof(buf), ctx);
  235. if (!TEST_size_t_ne(len, 0)
  236. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  237. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  238. goto err;
  239. test_output_memory("Generator as octet string, hybrid form:",
  240. buf, len);
  241. if (!TEST_true(EC_POINT_invert(group, P, ctx))
  242. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
  243. /*
  244. * Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2,
  245. * 2000) -- not a NIST curve, but commonly used
  246. */
  247. || !TEST_true(BN_hex2bn(&p, "FFFFFFFF"
  248. "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"))
  249. || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL))
  250. || !TEST_true(BN_hex2bn(&a, "FFFFFFFF"
  251. "FFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"))
  252. || !TEST_true(BN_hex2bn(&b, "1C97BEFC"
  253. "54BD7A8B65ACF89F81D4D4ADC565FA45"))
  254. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  255. || !TEST_true(BN_hex2bn(&x, "4A96B568"
  256. "8EF573284664698968C38BB913CBFC82"))
  257. || !TEST_true(BN_hex2bn(&y, "23a62855"
  258. "3168947d59dcc912042351377ac5fb32"))
  259. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  260. /*
  261. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  262. * and therefore setting the coordinates should fail.
  263. */
  264. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  265. ctx))
  266. || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
  267. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  268. || !TEST_true(BN_hex2bn(&z, "0100000000"
  269. "000000000001F4C8F927AED3CA752257"))
  270. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  271. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  272. goto err;
  273. TEST_info("SEC2 curve secp160r1 -- Generator");
  274. test_output_bignum("x", x);
  275. test_output_bignum("y", y);
  276. /* G_y value taken from the standard: */
  277. if (!TEST_true(BN_hex2bn(&z, "23a62855"
  278. "3168947d59dcc912042351377ac5fb32"))
  279. || !TEST_BN_eq(y, z)
  280. || !TEST_int_eq(EC_GROUP_get_degree(group), 160)
  281. || !group_order_tests(group)
  282. /* Curve P-192 (FIPS PUB 186-2, App. 6) */
  283. || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFF"
  284. "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"))
  285. || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL))
  286. || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFF"
  287. "FFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"))
  288. || !TEST_true(BN_hex2bn(&b, "64210519E59C80E7"
  289. "0FA7E9AB72243049FEB8DEECC146B9B1"))
  290. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  291. || !TEST_true(BN_hex2bn(&x, "188DA80EB03090F6"
  292. "7CBF20EB43A18800F4FF0AFD82FF1012"))
  293. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
  294. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  295. || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFF"
  296. "FFFFFFFF99DEF836146BC9B1B4D22831"))
  297. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  298. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  299. goto err;
  300. TEST_info("NIST curve P-192 -- Generator");
  301. test_output_bignum("x", x);
  302. test_output_bignum("y", y);
  303. /* G_y value taken from the standard: */
  304. if (!TEST_true(BN_hex2bn(&z, "07192B95FFC8DA78"
  305. "631011ED6B24CDD573F977A11E794811"))
  306. || !TEST_BN_eq(y, z)
  307. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  308. /*
  309. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  310. * and therefore setting the coordinates should fail.
  311. */
  312. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  313. ctx))
  314. || !TEST_int_eq(EC_GROUP_get_degree(group), 192)
  315. || !group_order_tests(group)
  316. /* Curve P-224 (FIPS PUB 186-2, App. 6) */
  317. || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFF"
  318. "FFFFFFFF000000000000000000000001"))
  319. || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL))
  320. || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFF"
  321. "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"))
  322. || !TEST_true(BN_hex2bn(&b, "B4050A850C04B3ABF5413256"
  323. "5044B0B7D7BFD8BA270B39432355FFB4"))
  324. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  325. || !TEST_true(BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B9"
  326. "4A03C1D356C21122343280D6115C1D21"))
  327. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx))
  328. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  329. || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF"
  330. "FFFF16A2E0B8F03E13DD29455C5C2A3D"))
  331. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  332. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  333. goto err;
  334. TEST_info("NIST curve P-224 -- Generator");
  335. test_output_bignum("x", x);
  336. test_output_bignum("y", y);
  337. /* G_y value taken from the standard: */
  338. if (!TEST_true(BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6"
  339. "CD4375A05A07476444D5819985007E34"))
  340. || !TEST_BN_eq(y, z)
  341. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  342. /*
  343. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  344. * and therefore setting the coordinates should fail.
  345. */
  346. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  347. ctx))
  348. || !TEST_int_eq(EC_GROUP_get_degree(group), 224)
  349. || !group_order_tests(group)
  350. /* Curve P-256 (FIPS PUB 186-2, App. 6) */
  351. || !TEST_true(BN_hex2bn(&p, "FFFFFFFF000000010000000000000000"
  352. "00000000FFFFFFFFFFFFFFFFFFFFFFFF"))
  353. || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL))
  354. || !TEST_true(BN_hex2bn(&a, "FFFFFFFF000000010000000000000000"
  355. "00000000FFFFFFFFFFFFFFFFFFFFFFFC"))
  356. || !TEST_true(BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC"
  357. "651D06B0CC53B0F63BCE3C3E27D2604B"))
  358. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  359. || !TEST_true(BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F2"
  360. "77037D812DEB33A0F4A13945D898C296"))
  361. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
  362. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  363. || !TEST_true(BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFF"
  364. "BCE6FAADA7179E84F3B9CAC2FC632551"))
  365. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  366. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  367. goto err;
  368. TEST_info("NIST curve P-256 -- Generator");
  369. test_output_bignum("x", x);
  370. test_output_bignum("y", y);
  371. /* G_y value taken from the standard: */
  372. if (!TEST_true(BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  373. "2BCE33576B315ECECBB6406837BF51F5"))
  374. || !TEST_BN_eq(y, z)
  375. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  376. /*
  377. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  378. * and therefore setting the coordinates should fail.
  379. */
  380. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  381. ctx))
  382. || !TEST_int_eq(EC_GROUP_get_degree(group), 256)
  383. || !group_order_tests(group)
  384. /* Curve P-384 (FIPS PUB 186-2, App. 6) */
  385. || !TEST_true(BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  386. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
  387. "FFFFFFFF0000000000000000FFFFFFFF"))
  388. || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL))
  389. || !TEST_true(BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  390. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE"
  391. "FFFFFFFF0000000000000000FFFFFFFC"))
  392. || !TEST_true(BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19"
  393. "181D9C6EFE8141120314088F5013875A"
  394. "C656398D8A2ED19D2A85C8EDD3EC2AEF"))
  395. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  396. || !TEST_true(BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD74"
  397. "6E1D3B628BA79B9859F741E082542A38"
  398. "5502F25DBF55296C3A545E3872760AB7"))
  399. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 1, ctx))
  400. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  401. || !TEST_true(BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  402. "FFFFFFFFFFFFFFFFC7634D81F4372DDF"
  403. "581A0DB248B0A77AECEC196ACCC52973"))
  404. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  405. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  406. goto err;
  407. TEST_info("NIST curve P-384 -- Generator");
  408. test_output_bignum("x", x);
  409. test_output_bignum("y", y);
  410. /* G_y value taken from the standard: */
  411. if (!TEST_true(BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29"
  412. "F8F41DBD289A147CE9DA3113B5F0B8C0"
  413. "0A60B1CE1D7E819D7A431D7C90EA0E5F"))
  414. || !TEST_BN_eq(y, z)
  415. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  416. /*
  417. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  418. * and therefore setting the coordinates should fail.
  419. */
  420. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  421. ctx))
  422. || !TEST_int_eq(EC_GROUP_get_degree(group), 384)
  423. || !group_order_tests(group)
  424. /* Curve P-521 (FIPS PUB 186-2, App. 6) */
  425. || !TEST_true(BN_hex2bn(&p, "1FF"
  426. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  427. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  428. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  429. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
  430. || !TEST_int_eq(1, BN_check_prime(p, ctx, NULL))
  431. || !TEST_true(BN_hex2bn(&a, "1FF"
  432. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  433. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  434. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  435. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC"))
  436. || !TEST_true(BN_hex2bn(&b, "051"
  437. "953EB9618E1C9A1F929A21A0B68540EE"
  438. "A2DA725B99B315F3B8B489918EF109E1"
  439. "56193951EC7E937B1652C0BD3BB1BF07"
  440. "3573DF883D2C34F1EF451FD46B503F00"))
  441. || !TEST_true(EC_GROUP_set_curve(group, p, a, b, ctx))
  442. || !TEST_true(BN_hex2bn(&x, "C6"
  443. "858E06B70404E9CD9E3ECB662395B442"
  444. "9C648139053FB521F828AF606B4D3DBA"
  445. "A14B5E77EFE75928FE1DC127A2FFA8DE"
  446. "3348B3C1856A429BF97E7E31C2E5BD66"))
  447. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x, 0, ctx))
  448. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  449. || !TEST_true(BN_hex2bn(&z, "1FF"
  450. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  451. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA"
  452. "51868783BF2F966B7FCC0148F709A5D0"
  453. "3BB5C9B8899C47AEBB6FB71E91386409"))
  454. || !TEST_true(EC_GROUP_set_generator(group, P, z, BN_value_one()))
  455. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  456. goto err;
  457. TEST_info("NIST curve P-521 -- Generator");
  458. test_output_bignum("x", x);
  459. test_output_bignum("y", y);
  460. /* G_y value taken from the standard: */
  461. if (!TEST_true(BN_hex2bn(&z, "118"
  462. "39296A789A3BC0045C8A5FB42C7D1BD9"
  463. "98F54449579B446817AFBD17273E662C"
  464. "97EE72995EF42640C550B9013FAD0761"
  465. "353C7086A272C24088BE94769FD16650"))
  466. || !TEST_BN_eq(y, z)
  467. || !TEST_true(BN_add(yplusone, y, BN_value_one()))
  468. /*
  469. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  470. * and therefore setting the coordinates should fail.
  471. */
  472. || !TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone,
  473. ctx))
  474. || !TEST_int_eq(EC_GROUP_get_degree(group), 521)
  475. || !group_order_tests(group)
  476. /* more tests using the last curve */
  477. /* Restore the point that got mangled in the (x, y + 1) test. */
  478. || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
  479. || !TEST_true(EC_POINT_copy(Q, P))
  480. || !TEST_false(EC_POINT_is_at_infinity(group, Q))
  481. || !TEST_true(EC_POINT_dbl(group, P, P, ctx))
  482. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  483. || !TEST_true(EC_POINT_invert(group, Q, ctx)) /* P = -2Q */
  484. || !TEST_true(EC_POINT_add(group, R, P, Q, ctx))
  485. || !TEST_true(EC_POINT_add(group, R, R, Q, ctx))
  486. || !TEST_true(EC_POINT_is_at_infinity(group, R)) /* R = P + 2Q */
  487. || !TEST_false(EC_POINT_is_at_infinity(group, Q)))
  488. goto err;
  489. #ifndef OPENSSL_NO_DEPRECATED_3_0
  490. TEST_note("combined multiplication ...");
  491. points[0] = Q;
  492. points[1] = Q;
  493. points[2] = Q;
  494. points[3] = Q;
  495. if (!TEST_true(EC_GROUP_get_order(group, z, ctx))
  496. || !TEST_true(BN_add(y, z, BN_value_one()))
  497. || !TEST_BN_even(y)
  498. || !TEST_true(BN_rshift1(y, y)))
  499. goto err;
  500. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  501. scalars[1] = y;
  502. /* z is still the group order */
  503. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  504. || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
  505. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
  506. || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx))
  507. || !TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))
  508. || !TEST_true(BN_add(z, z, y)))
  509. goto err;
  510. BN_set_negative(z, 1);
  511. scalars[0] = y;
  512. scalars[1] = z; /* z = -(order + y) */
  513. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  514. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  515. || !TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))
  516. || !TEST_true(BN_add(z, x, y)))
  517. goto err;
  518. BN_set_negative(z, 1);
  519. scalars[0] = x;
  520. scalars[1] = y;
  521. scalars[2] = z; /* z = -(x+y) */
  522. if (!TEST_ptr(scalar3 = BN_new()))
  523. goto err;
  524. BN_zero(scalar3);
  525. scalars[3] = scalar3;
  526. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx))
  527. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  528. goto err;
  529. #endif
  530. TEST_note(" ok\n");
  531. r = 1;
  532. err:
  533. BN_CTX_free(ctx);
  534. BN_free(p);
  535. BN_free(a);
  536. BN_free(b);
  537. EC_GROUP_free(group);
  538. EC_POINT_free(P);
  539. EC_POINT_free(Q);
  540. EC_POINT_free(R);
  541. BN_free(x);
  542. BN_free(y);
  543. BN_free(z);
  544. BN_free(yplusone);
  545. BN_free(scalar3);
  546. return r;
  547. }
  548. #ifndef OPENSSL_NO_EC2M
  549. static struct c2_curve_test {
  550. const char *name;
  551. const char *p;
  552. const char *a;
  553. const char *b;
  554. const char *x;
  555. const char *y;
  556. int ybit;
  557. const char *order;
  558. const char *cof;
  559. int degree;
  560. } char2_curve_tests[] = {
  561. /* Curve K-163 (FIPS PUB 186-2, App. 6) */
  562. {
  563. "NIST curve K-163",
  564. "0800000000000000000000000000000000000000C9",
  565. "1",
  566. "1",
  567. "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
  568. "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
  569. 1, "04000000000000000000020108A2E0CC0D99F8A5EF", "2", 163
  570. },
  571. /* Curve B-163 (FIPS PUB 186-2, App. 6) */
  572. {
  573. "NIST curve B-163",
  574. "0800000000000000000000000000000000000000C9",
  575. "1",
  576. "020A601907B8C953CA1481EB10512F78744A3205FD",
  577. "03F0EBA16286A2D57EA0991168D4994637E8343E36",
  578. "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
  579. 1, "040000000000000000000292FE77E70C12A4234C33", "2", 163
  580. },
  581. /* Curve K-233 (FIPS PUB 186-2, App. 6) */
  582. {
  583. "NIST curve K-233",
  584. "020000000000000000000000000000000000000004000000000000000001",
  585. "0",
  586. "1",
  587. "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
  588. "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
  589. 0,
  590. "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
  591. "4", 233
  592. },
  593. /* Curve B-233 (FIPS PUB 186-2, App. 6) */
  594. {
  595. "NIST curve B-233",
  596. "020000000000000000000000000000000000000004000000000000000001",
  597. "000000000000000000000000000000000000000000000000000000000001",
  598. "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
  599. "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
  600. "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
  601. 1,
  602. "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
  603. "2", 233
  604. },
  605. /* Curve K-283 (FIPS PUB 186-2, App. 6) */
  606. {
  607. "NIST curve K-283",
  608. "08000000"
  609. "00000000000000000000000000000000000000000000000000000000000010A1",
  610. "0",
  611. "1",
  612. "0503213F"
  613. "78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
  614. "01CCDA38"
  615. "0F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
  616. 0,
  617. "01FFFFFF"
  618. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
  619. "4", 283
  620. },
  621. /* Curve B-283 (FIPS PUB 186-2, App. 6) */
  622. {
  623. "NIST curve B-283",
  624. "08000000"
  625. "00000000000000000000000000000000000000000000000000000000000010A1",
  626. "00000000"
  627. "0000000000000000000000000000000000000000000000000000000000000001",
  628. "027B680A"
  629. "C8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
  630. "05F93925"
  631. "8DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
  632. "03676854"
  633. "FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
  634. 1,
  635. "03FFFFFF"
  636. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
  637. "2", 283
  638. },
  639. /* Curve K-409 (FIPS PUB 186-2, App. 6) */
  640. {
  641. "NIST curve K-409",
  642. "0200000000000000000000000000000000000000"
  643. "0000000000000000000000000000000000000000008000000000000000000001",
  644. "0",
  645. "1",
  646. "0060F05F658F49C1AD3AB1890F7184210EFD0987"
  647. "E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
  648. "01E369050B7C4E42ACBA1DACBF04299C3460782F"
  649. "918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
  650. 1,
  651. "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  652. "FFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
  653. "4", 409
  654. },
  655. /* Curve B-409 (FIPS PUB 186-2, App. 6) */
  656. {
  657. "NIST curve B-409",
  658. "0200000000000000000000000000000000000000"
  659. "0000000000000000000000000000000000000000008000000000000000000001",
  660. "0000000000000000000000000000000000000000"
  661. "0000000000000000000000000000000000000000000000000000000000000001",
  662. "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422E"
  663. "F1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
  664. "015D4860D088DDB3496B0C6064756260441CDE4A"
  665. "F1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
  666. "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5"
  667. "A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
  668. 1,
  669. "0100000000000000000000000000000000000000"
  670. "00000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
  671. "2", 409
  672. },
  673. /* Curve K-571 (FIPS PUB 186-2, App. 6) */
  674. {
  675. "NIST curve K-571",
  676. "800000000000000"
  677. "0000000000000000000000000000000000000000000000000000000000000000"
  678. "0000000000000000000000000000000000000000000000000000000000000425",
  679. "0",
  680. "1",
  681. "026EB7A859923FBC"
  682. "82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E6"
  683. "47DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
  684. "0349DC807F4FBF37"
  685. "4F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA7"
  686. "4FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
  687. 0,
  688. "0200000000000000"
  689. "00000000000000000000000000000000000000000000000000000000131850E1"
  690. "F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
  691. "4", 571
  692. },
  693. /* Curve B-571 (FIPS PUB 186-2, App. 6) */
  694. {
  695. "NIST curve B-571",
  696. "800000000000000"
  697. "0000000000000000000000000000000000000000000000000000000000000000"
  698. "0000000000000000000000000000000000000000000000000000000000000425",
  699. "0000000000000000"
  700. "0000000000000000000000000000000000000000000000000000000000000000"
  701. "0000000000000000000000000000000000000000000000000000000000000001",
  702. "02F40E7E2221F295"
  703. "DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA5933"
  704. "2BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
  705. "0303001D34B85629"
  706. "6C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293"
  707. "CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
  708. "037BF27342DA639B"
  709. "6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A57"
  710. "6291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
  711. 1,
  712. "03FFFFFFFFFFFFFF"
  713. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18"
  714. "FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
  715. "2", 571
  716. }
  717. };
  718. static int char2_curve_test(int n)
  719. {
  720. int r = 0;
  721. BN_CTX *ctx = NULL;
  722. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  723. BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;
  724. EC_GROUP *group = NULL;
  725. EC_POINT *P = NULL, *Q = NULL, *R = NULL;
  726. # ifndef OPENSSL_NO_DEPRECATED_3_0
  727. const EC_POINT *points[3];
  728. const BIGNUM *scalars[3];
  729. # endif
  730. struct c2_curve_test *const test = char2_curve_tests + n;
  731. if (!TEST_ptr(ctx = BN_CTX_new())
  732. || !TEST_ptr(p = BN_new())
  733. || !TEST_ptr(a = BN_new())
  734. || !TEST_ptr(b = BN_new())
  735. || !TEST_ptr(x = BN_new())
  736. || !TEST_ptr(y = BN_new())
  737. || !TEST_ptr(z = BN_new())
  738. || !TEST_ptr(yplusone = BN_new())
  739. || !TEST_true(BN_hex2bn(&p, test->p))
  740. || !TEST_true(BN_hex2bn(&a, test->a))
  741. || !TEST_true(BN_hex2bn(&b, test->b))
  742. || !TEST_true(group = EC_GROUP_new_curve_GF2m(p, a, b, ctx))
  743. || !TEST_ptr(P = EC_POINT_new(group))
  744. || !TEST_ptr(Q = EC_POINT_new(group))
  745. || !TEST_ptr(R = EC_POINT_new(group))
  746. || !TEST_true(BN_hex2bn(&x, test->x))
  747. || !TEST_true(BN_hex2bn(&y, test->y))
  748. || !TEST_true(BN_add(yplusone, y, BN_value_one())))
  749. goto err;
  750. /* Change test based on whether binary point compression is enabled or not. */
  751. # ifdef OPENSSL_EC_BIN_PT_COMP
  752. /*
  753. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  754. * and therefore setting the coordinates should fail.
  755. */
  756. if (!TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx))
  757. || !TEST_true(EC_POINT_set_compressed_coordinates(group, P, x,
  758. test->y_bit,
  759. ctx))
  760. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  761. || !TEST_true(BN_hex2bn(&z, test->order))
  762. || !TEST_true(BN_hex2bn(&cof, test->cof))
  763. || !TEST_true(EC_GROUP_set_generator(group, P, z, cof))
  764. || !TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y, ctx)))
  765. goto err;
  766. TEST_info("%s -- Generator", test->name);
  767. test_output_bignum("x", x);
  768. test_output_bignum("y", y);
  769. /* G_y value taken from the standard: */
  770. if (!TEST_true(BN_hex2bn(&z, test->y))
  771. || !TEST_BN_eq(y, z))
  772. goto err;
  773. # else
  774. /*
  775. * When (x, y) is on the curve, (x, y + 1) is, as it happens, not,
  776. * and therefore setting the coordinates should fail.
  777. */
  778. if (!TEST_false(EC_POINT_set_affine_coordinates(group, P, x, yplusone, ctx))
  779. || !TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
  780. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  781. || !TEST_true(BN_hex2bn(&z, test->order))
  782. || !TEST_true(BN_hex2bn(&cof, test->cof))
  783. || !TEST_true(EC_GROUP_set_generator(group, P, z, cof)))
  784. goto err;
  785. TEST_info("%s -- Generator:", test->name);
  786. test_output_bignum("x", x);
  787. test_output_bignum("y", y);
  788. # endif
  789. if (!TEST_int_eq(EC_GROUP_get_degree(group), test->degree)
  790. || !group_order_tests(group))
  791. goto err;
  792. /* more tests using the last curve */
  793. if (n == OSSL_NELEM(char2_curve_tests) - 1) {
  794. if (!TEST_true(EC_POINT_set_affine_coordinates(group, P, x, y, ctx))
  795. || !TEST_true(EC_POINT_copy(Q, P))
  796. || !TEST_false(EC_POINT_is_at_infinity(group, Q))
  797. || !TEST_true(EC_POINT_dbl(group, P, P, ctx))
  798. || !TEST_int_gt(EC_POINT_is_on_curve(group, P, ctx), 0)
  799. || !TEST_true(EC_POINT_invert(group, Q, ctx)) /* P = -2Q */
  800. || !TEST_true(EC_POINT_add(group, R, P, Q, ctx))
  801. || !TEST_true(EC_POINT_add(group, R, R, Q, ctx))
  802. || !TEST_true(EC_POINT_is_at_infinity(group, R)) /* R = P + 2Q */
  803. || !TEST_false(EC_POINT_is_at_infinity(group, Q)))
  804. goto err;
  805. # ifndef OPENSSL_NO_DEPRECATED_3_0
  806. TEST_note("combined multiplication ...");
  807. points[0] = Q;
  808. points[1] = Q;
  809. points[2] = Q;
  810. if (!TEST_true(BN_add(y, z, BN_value_one()))
  811. || !TEST_BN_even(y)
  812. || !TEST_true(BN_rshift1(y, y)))
  813. goto err;
  814. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  815. scalars[1] = y;
  816. /* z is still the group order */
  817. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  818. || !TEST_true(EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
  819. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx))
  820. || !TEST_int_eq(0, EC_POINT_cmp(group, R, Q, ctx)))
  821. goto err;
  822. if (!TEST_true(BN_rand(y, BN_num_bits(y), 0, 0))
  823. || !TEST_true(BN_add(z, z, y)))
  824. goto err;
  825. BN_set_negative(z, 1);
  826. scalars[0] = y;
  827. scalars[1] = z; /* z = -(order + y) */
  828. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  829. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  830. goto err;
  831. if (!TEST_true(BN_rand(x, BN_num_bits(y) - 1, 0, 0))
  832. || !TEST_true(BN_add(z, x, y)))
  833. goto err;
  834. BN_set_negative(z, 1);
  835. scalars[0] = x;
  836. scalars[1] = y;
  837. scalars[2] = z; /* z = -(x+y) */
  838. if (!TEST_true(EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx))
  839. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  840. goto err;
  841. # endif
  842. }
  843. r = 1;
  844. err:
  845. BN_CTX_free(ctx);
  846. BN_free(p);
  847. BN_free(a);
  848. BN_free(b);
  849. BN_free(x);
  850. BN_free(y);
  851. BN_free(z);
  852. BN_free(yplusone);
  853. BN_free(cof);
  854. EC_POINT_free(P);
  855. EC_POINT_free(Q);
  856. EC_POINT_free(R);
  857. EC_GROUP_free(group);
  858. return r;
  859. }
  860. static int char2_field_tests(void)
  861. {
  862. BN_CTX *ctx = NULL;
  863. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  864. EC_GROUP *group = NULL;
  865. EC_POINT *P = NULL, *Q = NULL, *R = NULL;
  866. BIGNUM *x = NULL, *y = NULL, *z = NULL, *cof = NULL, *yplusone = NULL;
  867. unsigned char buf[100];
  868. size_t len;
  869. int k, r = 0;
  870. if (!TEST_ptr(ctx = BN_CTX_new())
  871. || !TEST_ptr(p = BN_new())
  872. || !TEST_ptr(a = BN_new())
  873. || !TEST_ptr(b = BN_new())
  874. || !TEST_true(BN_hex2bn(&p, "13"))
  875. || !TEST_true(BN_hex2bn(&a, "3"))
  876. || !TEST_true(BN_hex2bn(&b, "1")))
  877. goto err;
  878. if (!TEST_ptr(group = EC_GROUP_new_curve_GF2m(p, a, b, ctx))
  879. || !TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx)))
  880. goto err;
  881. TEST_info("Curve defined by Weierstrass equation");
  882. TEST_note(" y^2 + x*y = x^3 + a*x^2 + b (mod p)");
  883. test_output_bignum("a", a);
  884. test_output_bignum("b", b);
  885. test_output_bignum("p", p);
  886. if (!TEST_ptr(P = EC_POINT_new(group))
  887. || !TEST_ptr(Q = EC_POINT_new(group))
  888. || !TEST_ptr(R = EC_POINT_new(group))
  889. || !TEST_true(EC_POINT_set_to_infinity(group, P))
  890. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  891. goto err;
  892. buf[0] = 0;
  893. if (!TEST_true(EC_POINT_oct2point(group, Q, buf, 1, ctx))
  894. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx))
  895. || !TEST_true(EC_POINT_is_at_infinity(group, P))
  896. || !TEST_ptr(x = BN_new())
  897. || !TEST_ptr(y = BN_new())
  898. || !TEST_ptr(z = BN_new())
  899. || !TEST_ptr(cof = BN_new())
  900. || !TEST_ptr(yplusone = BN_new())
  901. || !TEST_true(BN_hex2bn(&x, "6"))
  902. /* Change test based on whether binary point compression is enabled or not. */
  903. # ifdef OPENSSL_EC_BIN_PT_COMP
  904. || !TEST_true(EC_POINT_set_compressed_coordinates(group, Q, x, 1, ctx))
  905. # else
  906. || !TEST_true(BN_hex2bn(&y, "8"))
  907. || !TEST_true(EC_POINT_set_affine_coordinates(group, Q, x, y, ctx))
  908. # endif
  909. )
  910. goto err;
  911. if (!TEST_int_gt(EC_POINT_is_on_curve(group, Q, ctx), 0)) {
  912. /* Change test based on whether binary point compression is enabled or not. */
  913. # ifdef OPENSSL_EC_BIN_PT_COMP
  914. if (!TEST_true(EC_POINT_get_affine_coordinates(group, Q, x, y, ctx)))
  915. goto err;
  916. # endif
  917. TEST_info("Point is not on curve");
  918. test_output_bignum("x", x);
  919. test_output_bignum("y", y);
  920. goto err;
  921. }
  922. TEST_note("A cyclic subgroup:");
  923. k = 100;
  924. do {
  925. if (!TEST_int_ne(k--, 0))
  926. goto err;
  927. if (EC_POINT_is_at_infinity(group, P))
  928. TEST_note(" point at infinity");
  929. else {
  930. if (!TEST_true(EC_POINT_get_affine_coordinates(group, P, x, y,
  931. ctx)))
  932. goto err;
  933. test_output_bignum("x", x);
  934. test_output_bignum("y", y);
  935. }
  936. if (!TEST_true(EC_POINT_copy(R, P))
  937. || !TEST_true(EC_POINT_add(group, P, P, Q, ctx)))
  938. goto err;
  939. }
  940. while (!EC_POINT_is_at_infinity(group, P));
  941. if (!TEST_true(EC_POINT_add(group, P, Q, R, ctx))
  942. || !TEST_true(EC_POINT_is_at_infinity(group, P)))
  943. goto err;
  944. /* Change test based on whether binary point compression is enabled or not. */
  945. # ifdef OPENSSL_EC_BIN_PT_COMP
  946. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED,
  947. buf, sizeof(buf), ctx);
  948. if (!TEST_size_t_ne(len, 0)
  949. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  950. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  951. goto err;
  952. test_output_memory("Generator as octet string, compressed form:",
  953. buf, len);
  954. # endif
  955. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED,
  956. buf, sizeof(buf), ctx);
  957. if (!TEST_size_t_ne(len, 0)
  958. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  959. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  960. goto err;
  961. test_output_memory("Generator as octet string, uncompressed form:",
  962. buf, len);
  963. /* Change test based on whether binary point compression is enabled or not. */
  964. # ifdef OPENSSL_EC_BIN_PT_COMP
  965. len =
  966. EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof(buf),
  967. ctx);
  968. if (!TEST_size_t_ne(len, 0)
  969. || !TEST_true(EC_POINT_oct2point(group, P, buf, len, ctx))
  970. || !TEST_int_eq(0, EC_POINT_cmp(group, P, Q, ctx)))
  971. goto err;
  972. test_output_memory("Generator as octet string, hybrid form:",
  973. buf, len);
  974. # endif
  975. if (!TEST_true(EC_POINT_invert(group, P, ctx))
  976. || !TEST_int_eq(0, EC_POINT_cmp(group, P, R, ctx)))
  977. goto err;
  978. TEST_note("\n");
  979. r = 1;
  980. err:
  981. BN_CTX_free(ctx);
  982. BN_free(p);
  983. BN_free(a);
  984. BN_free(b);
  985. EC_GROUP_free(group);
  986. EC_POINT_free(P);
  987. EC_POINT_free(Q);
  988. EC_POINT_free(R);
  989. BN_free(x);
  990. BN_free(y);
  991. BN_free(z);
  992. BN_free(cof);
  993. BN_free(yplusone);
  994. return r;
  995. }
  996. 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. * random 256-bit explicit parameters curve, cofactor absent
  1895. * order: 0x0c38d96a9f892b88772ec2e39614a82f4f (132 bit)
  1896. * cofactor: 0x12bc94785251297abfafddf1565100da (125 bit)
  1897. */
  1898. static const unsigned char params_cf_pass[] = {
  1899. 0x30, 0x81, 0xcd, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86,
  1900. 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xe5, 0x00, 0x1f, 0xc5,
  1901. 0xca, 0x71, 0x9d, 0x8e, 0xf7, 0x07, 0x4b, 0x48, 0x37, 0xf9, 0x33, 0x2d,
  1902. 0x71, 0xbf, 0x79, 0xe7, 0xdc, 0x91, 0xc2, 0xff, 0xb6, 0x7b, 0xc3, 0x93,
  1903. 0x44, 0x88, 0xe6, 0x91, 0x30, 0x44, 0x04, 0x20, 0xe5, 0x00, 0x1f, 0xc5,
  1904. 0xca, 0x71, 0x9d, 0x8e, 0xf7, 0x07, 0x4b, 0x48, 0x37, 0xf9, 0x33, 0x2d,
  1905. 0x71, 0xbf, 0x79, 0xe7, 0xdc, 0x91, 0xc2, 0xff, 0xb6, 0x7b, 0xc3, 0x93,
  1906. 0x44, 0x88, 0xe6, 0x8e, 0x04, 0x20, 0x18, 0x8c, 0x59, 0x57, 0xc4, 0xbc,
  1907. 0x85, 0x57, 0xc3, 0x66, 0x9f, 0x89, 0xd5, 0x92, 0x0d, 0x7e, 0x42, 0x27,
  1908. 0x07, 0x64, 0xaa, 0x26, 0xed, 0x89, 0xc4, 0x09, 0x05, 0x4d, 0xc7, 0x23,
  1909. 0x47, 0xda, 0x04, 0x41, 0x04, 0x1b, 0x6b, 0x41, 0x0b, 0xf9, 0xfb, 0x77,
  1910. 0xfd, 0x50, 0xb7, 0x3e, 0x23, 0xa3, 0xec, 0x9a, 0x3b, 0x09, 0x31, 0x6b,
  1911. 0xfa, 0xf6, 0xce, 0x1f, 0xff, 0xeb, 0x57, 0x93, 0x24, 0x70, 0xf3, 0xf4,
  1912. 0xba, 0x7e, 0xfa, 0x86, 0x6e, 0x19, 0x89, 0xe3, 0x55, 0x6d, 0x5a, 0xe9,
  1913. 0xc0, 0x3d, 0xbc, 0xfb, 0xaf, 0xad, 0xd4, 0x7e, 0xa6, 0xe5, 0xfa, 0x1a,
  1914. 0x58, 0x07, 0x9e, 0x8f, 0x0d, 0x3b, 0xf7, 0x38, 0xca, 0x02, 0x11, 0x0c,
  1915. 0x38, 0xd9, 0x6a, 0x9f, 0x89, 0x2b, 0x88, 0x77, 0x2e, 0xc2, 0xe3, 0x96,
  1916. 0x14, 0xa8, 0x2f, 0x4f
  1917. };
  1918. /*-
  1919. * random 256-bit explicit parameters curve, cofactor absent
  1920. * order: 0x045a75c0c17228ebd9b169a10e34a22101 (131 bit)
  1921. * cofactor: 0x2e134b4ede82649f67a2e559d361e5fe (126 bit)
  1922. */
  1923. static const unsigned char params_cf_fail[] = {
  1924. 0x30, 0x81, 0xcd, 0x02, 0x01, 0x01, 0x30, 0x2c, 0x06, 0x07, 0x2a, 0x86,
  1925. 0x48, 0xce, 0x3d, 0x01, 0x01, 0x02, 0x21, 0x00, 0xc8, 0x95, 0x27, 0x37,
  1926. 0xe8, 0xe1, 0xfd, 0xcc, 0xf9, 0x6e, 0x0c, 0xa6, 0x21, 0xc1, 0x7d, 0x6b,
  1927. 0x9d, 0x44, 0x42, 0xea, 0x73, 0x4e, 0x04, 0xb6, 0xac, 0x62, 0x50, 0xd0,
  1928. 0x33, 0xc2, 0xea, 0x13, 0x30, 0x44, 0x04, 0x20, 0xc8, 0x95, 0x27, 0x37,
  1929. 0xe8, 0xe1, 0xfd, 0xcc, 0xf9, 0x6e, 0x0c, 0xa6, 0x21, 0xc1, 0x7d, 0x6b,
  1930. 0x9d, 0x44, 0x42, 0xea, 0x73, 0x4e, 0x04, 0xb6, 0xac, 0x62, 0x50, 0xd0,
  1931. 0x33, 0xc2, 0xea, 0x10, 0x04, 0x20, 0xbf, 0xa6, 0xa8, 0x05, 0x1d, 0x09,
  1932. 0xac, 0x70, 0x39, 0xbb, 0x4d, 0xb2, 0x90, 0x8a, 0x15, 0x41, 0x14, 0x1d,
  1933. 0x11, 0x86, 0x9f, 0x13, 0xa2, 0x63, 0x1a, 0xda, 0x95, 0x22, 0x4d, 0x02,
  1934. 0x15, 0x0a, 0x04, 0x41, 0x04, 0xaf, 0x16, 0x71, 0xf9, 0xc4, 0xc8, 0x59,
  1935. 0x1d, 0xa3, 0x6f, 0xe7, 0xc3, 0x57, 0xa1, 0xfa, 0x9f, 0x49, 0x7c, 0x11,
  1936. 0x27, 0x05, 0xa0, 0x7f, 0xff, 0xf9, 0xe0, 0xe7, 0x92, 0xdd, 0x9c, 0x24,
  1937. 0x8e, 0xc7, 0xb9, 0x52, 0x71, 0x3f, 0xbc, 0x7f, 0x6a, 0x9f, 0x35, 0x70,
  1938. 0xe1, 0x27, 0xd5, 0x35, 0x8a, 0x13, 0xfa, 0xa8, 0x33, 0x3e, 0xd4, 0x73,
  1939. 0x1c, 0x14, 0x58, 0x9e, 0xc7, 0x0a, 0x87, 0x65, 0x8d, 0x02, 0x11, 0x04,
  1940. 0x5a, 0x75, 0xc0, 0xc1, 0x72, 0x28, 0xeb, 0xd9, 0xb1, 0x69, 0xa1, 0x0e,
  1941. 0x34, 0xa2, 0x21, 0x01
  1942. };
  1943. /*-
  1944. * Test two random 256-bit explicit parameters curves with absent cofactor.
  1945. * The two curves are chosen to roughly straddle the bounds at which the lib
  1946. * can compute the cofactor automatically, roughly 4*sqrt(p). So test that:
  1947. *
  1948. * - params_cf_pass: order is sufficiently close to p to compute cofactor
  1949. * - params_cf_fail: order is too far away from p to compute cofactor
  1950. *
  1951. * For standards-compliant curves, cofactor is chosen as small as possible.
  1952. * So you can see neither of these curves are fit for cryptographic use.
  1953. *
  1954. * Some standards even mandate an upper bound on the cofactor, e.g. SECG1 v2:
  1955. * h <= 2**(t/8) where t is the security level of the curve, for which the lib
  1956. * will always succeed in computing the cofactor. Neither of these curves
  1957. * conform to that -- this is just robustness testing.
  1958. */
  1959. static int cofactor_range_test(void)
  1960. {
  1961. EC_GROUP *group = NULL;
  1962. BIGNUM *cf = NULL;
  1963. int ret = 0;
  1964. const unsigned char *b1 = (const unsigned char *)params_cf_fail;
  1965. const unsigned char *b2 = (const unsigned char *)params_cf_pass;
  1966. if (!TEST_ptr(group = d2i_ECPKParameters(NULL, &b1, sizeof(params_cf_fail)))
  1967. || !TEST_BN_eq_zero(EC_GROUP_get0_cofactor(group))
  1968. || !TEST_ptr(group = d2i_ECPKParameters(&group, &b2,
  1969. sizeof(params_cf_pass)))
  1970. || !TEST_int_gt(BN_hex2bn(&cf, "12bc94785251297abfafddf1565100da"), 0)
  1971. || !TEST_BN_eq(cf, EC_GROUP_get0_cofactor(group)))
  1972. goto err;
  1973. ret = 1;
  1974. err:
  1975. BN_free(cf);
  1976. EC_GROUP_free(group);
  1977. return ret;
  1978. }
  1979. /*-
  1980. * For named curves, test that:
  1981. * - the lib correctly computes the cofactor if passed a NULL or zero cofactor
  1982. * - a nonsensical cofactor throws an error (negative test)
  1983. * - nonsensical orders throw errors (negative tests)
  1984. */
  1985. static int cardinality_test(int n)
  1986. {
  1987. int ret = 0, is_binary = 0;
  1988. int nid = curves[n].nid;
  1989. BN_CTX *ctx = NULL;
  1990. EC_GROUP *g1 = NULL, *g2 = NULL;
  1991. EC_POINT *g2_gen = NULL;
  1992. BIGNUM *g1_p = NULL, *g1_a = NULL, *g1_b = NULL, *g1_x = NULL, *g1_y = NULL,
  1993. *g1_order = NULL, *g1_cf = NULL, *g2_cf = NULL;
  1994. TEST_info("Curve %s cardinality test", OBJ_nid2sn(nid));
  1995. if (!TEST_ptr(ctx = BN_CTX_new())
  1996. || !TEST_ptr(g1 = EC_GROUP_new_by_curve_name(nid))) {
  1997. BN_CTX_free(ctx);
  1998. return 0;
  1999. }
  2000. is_binary = (EC_GROUP_get_field_type(g1) == NID_X9_62_characteristic_two_field);
  2001. BN_CTX_start(ctx);
  2002. g1_p = BN_CTX_get(ctx);
  2003. g1_a = BN_CTX_get(ctx);
  2004. g1_b = BN_CTX_get(ctx);
  2005. g1_x = BN_CTX_get(ctx);
  2006. g1_y = BN_CTX_get(ctx);
  2007. g1_order = BN_CTX_get(ctx);
  2008. g1_cf = BN_CTX_get(ctx);
  2009. if (!TEST_ptr(g2_cf = BN_CTX_get(ctx))
  2010. /* pull out the explicit curve parameters */
  2011. || !TEST_true(EC_GROUP_get_curve(g1, g1_p, g1_a, g1_b, ctx))
  2012. || !TEST_true(EC_POINT_get_affine_coordinates(g1,
  2013. EC_GROUP_get0_generator(g1), g1_x, g1_y, ctx))
  2014. || !TEST_true(BN_copy(g1_order, EC_GROUP_get0_order(g1)))
  2015. || !TEST_true(EC_GROUP_get_cofactor(g1, g1_cf, ctx))
  2016. /* construct g2 manually with g1 parameters */
  2017. #ifndef OPENSSL_NO_EC2M
  2018. || !TEST_ptr(g2 = (is_binary) ?
  2019. EC_GROUP_new_curve_GF2m(g1_p, g1_a, g1_b, ctx) :
  2020. EC_GROUP_new_curve_GFp(g1_p, g1_a, g1_b, ctx))
  2021. #else
  2022. || !TEST_int_eq(0, is_binary)
  2023. || !TEST_ptr(g2 = EC_GROUP_new_curve_GFp(g1_p, g1_a, g1_b, ctx))
  2024. #endif
  2025. || !TEST_ptr(g2_gen = EC_POINT_new(g2))
  2026. || !TEST_true(EC_POINT_set_affine_coordinates(g2, g2_gen, g1_x, g1_y, ctx))
  2027. /* pass NULL cofactor: lib should compute it */
  2028. || !TEST_true(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL))
  2029. || !TEST_true(EC_GROUP_get_cofactor(g2, g2_cf, ctx))
  2030. || !TEST_BN_eq(g1_cf, g2_cf)
  2031. /* pass zero cofactor: lib should compute it */
  2032. || !TEST_true(BN_set_word(g2_cf, 0))
  2033. || !TEST_true(EC_GROUP_set_generator(g2, g2_gen, g1_order, g2_cf))
  2034. || !TEST_true(EC_GROUP_get_cofactor(g2, g2_cf, ctx))
  2035. || !TEST_BN_eq(g1_cf, g2_cf)
  2036. /* negative test for invalid cofactor */
  2037. || !TEST_true(BN_set_word(g2_cf, 0))
  2038. || !TEST_true(BN_sub(g2_cf, g2_cf, BN_value_one()))
  2039. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, g2_cf))
  2040. /* negative test for NULL order */
  2041. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, NULL, NULL))
  2042. /* negative test for zero order */
  2043. || !TEST_true(BN_set_word(g1_order, 0))
  2044. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL))
  2045. /* negative test for negative order */
  2046. || !TEST_true(BN_set_word(g2_cf, 0))
  2047. || !TEST_true(BN_sub(g2_cf, g2_cf, BN_value_one()))
  2048. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL))
  2049. /* negative test for too large order */
  2050. || !TEST_true(BN_lshift(g1_order, g1_p, 2))
  2051. || !TEST_false(EC_GROUP_set_generator(g2, g2_gen, g1_order, NULL)))
  2052. goto err;
  2053. ret = 1;
  2054. err:
  2055. EC_POINT_free(g2_gen);
  2056. EC_GROUP_free(g1);
  2057. EC_GROUP_free(g2);
  2058. BN_CTX_end(ctx);
  2059. BN_CTX_free(ctx);
  2060. return ret;
  2061. }
  2062. static int check_ec_key_field_public_range_test(int id)
  2063. {
  2064. int ret = 0, type = 0;
  2065. const EC_POINT *pub = NULL;
  2066. const EC_GROUP *group = NULL;
  2067. const BIGNUM *field = NULL;
  2068. BIGNUM *x = NULL, *y = NULL;
  2069. EC_KEY *key = NULL;
  2070. if (!TEST_ptr(x = BN_new())
  2071. || !TEST_ptr(y = BN_new())
  2072. || !TEST_ptr(key = EC_KEY_new_by_curve_name(curves[id].nid))
  2073. || !TEST_ptr(group = EC_KEY_get0_group(key))
  2074. || !TEST_ptr(field = EC_GROUP_get0_field(group))
  2075. || !TEST_int_gt(EC_KEY_generate_key(key), 0)
  2076. || !TEST_int_gt(EC_KEY_check_key(key), 0)
  2077. || !TEST_ptr(pub = EC_KEY_get0_public_key(key))
  2078. || !TEST_int_gt(EC_POINT_get_affine_coordinates(group, pub, x, y,
  2079. NULL), 0))
  2080. goto err;
  2081. /*
  2082. * Make the public point out of range by adding the field (which will still
  2083. * be the same point on the curve). The add is different for char2 fields.
  2084. */
  2085. type = EC_GROUP_get_field_type(group);
  2086. #ifndef OPENSSL_NO_EC2M
  2087. if (type == NID_X9_62_characteristic_two_field) {
  2088. /* test for binary curves */
  2089. if (!TEST_true(BN_GF2m_add(x, x, field)))
  2090. goto err;
  2091. } else
  2092. #endif
  2093. if (type == NID_X9_62_prime_field) {
  2094. /* test for prime curves */
  2095. if (!TEST_true(BN_add(x, x, field)))
  2096. goto err;
  2097. } else {
  2098. /* this should never happen */
  2099. TEST_error("Unsupported EC_METHOD field_type");
  2100. goto err;
  2101. }
  2102. if (!TEST_int_le(EC_KEY_set_public_key_affine_coordinates(key, x, y), 0))
  2103. goto err;
  2104. ret = 1;
  2105. err:
  2106. BN_free(x);
  2107. BN_free(y);
  2108. EC_KEY_free(key);
  2109. return ret;
  2110. }
  2111. /*
  2112. * Helper for ec_point_hex2point_test
  2113. *
  2114. * Self-tests EC_POINT_point2hex() against EC_POINT_hex2point() for the given
  2115. * (group,P) pair.
  2116. *
  2117. * If P is NULL use point at infinity.
  2118. */
  2119. static ossl_inline
  2120. int ec_point_hex2point_test_helper(const EC_GROUP *group, const EC_POINT *P,
  2121. point_conversion_form_t form,
  2122. BN_CTX *bnctx)
  2123. {
  2124. int ret = 0;
  2125. EC_POINT *Q = NULL, *Pinf = NULL;
  2126. char *hex = NULL;
  2127. if (P == NULL) {
  2128. /* If P is NULL use point at infinity. */
  2129. if (!TEST_ptr(Pinf = EC_POINT_new(group))
  2130. || !TEST_true(EC_POINT_set_to_infinity(group, Pinf)))
  2131. goto err;
  2132. P = Pinf;
  2133. }
  2134. if (!TEST_ptr(hex = EC_POINT_point2hex(group, P, form, bnctx))
  2135. || !TEST_ptr(Q = EC_POINT_hex2point(group, hex, NULL, bnctx))
  2136. || !TEST_int_eq(0, EC_POINT_cmp(group, Q, P, bnctx)))
  2137. goto err;
  2138. /*
  2139. * The next check is most likely superfluous, as EC_POINT_cmp should already
  2140. * cover this.
  2141. * Nonetheless it increases the test coverage for EC_POINT_is_at_infinity,
  2142. * so we include it anyway!
  2143. */
  2144. if (Pinf != NULL
  2145. && !TEST_true(EC_POINT_is_at_infinity(group, Q)))
  2146. goto err;
  2147. ret = 1;
  2148. err:
  2149. EC_POINT_free(Pinf);
  2150. OPENSSL_free(hex);
  2151. EC_POINT_free(Q);
  2152. return ret;
  2153. }
  2154. /*
  2155. * This test self-validates EC_POINT_hex2point() and EC_POINT_point2hex()
  2156. */
  2157. static int ec_point_hex2point_test(int id)
  2158. {
  2159. int ret = 0, nid;
  2160. EC_GROUP *group = NULL;
  2161. const EC_POINT *G = NULL;
  2162. EC_POINT *P = NULL;
  2163. BN_CTX * bnctx = NULL;
  2164. /* Do some setup */
  2165. nid = curves[id].nid;
  2166. if (!TEST_ptr(bnctx = BN_CTX_new())
  2167. || !TEST_ptr(group = EC_GROUP_new_by_curve_name(nid))
  2168. || !TEST_ptr(G = EC_GROUP_get0_generator(group))
  2169. || !TEST_ptr(P = EC_POINT_dup(G, group)))
  2170. goto err;
  2171. if (!TEST_true(ec_point_hex2point_test_helper(group, P,
  2172. POINT_CONVERSION_COMPRESSED,
  2173. bnctx))
  2174. || !TEST_true(ec_point_hex2point_test_helper(group, NULL,
  2175. POINT_CONVERSION_COMPRESSED,
  2176. bnctx))
  2177. || !TEST_true(ec_point_hex2point_test_helper(group, P,
  2178. POINT_CONVERSION_UNCOMPRESSED,
  2179. bnctx))
  2180. || !TEST_true(ec_point_hex2point_test_helper(group, NULL,
  2181. POINT_CONVERSION_UNCOMPRESSED,
  2182. bnctx))
  2183. || !TEST_true(ec_point_hex2point_test_helper(group, P,
  2184. POINT_CONVERSION_HYBRID,
  2185. bnctx))
  2186. || !TEST_true(ec_point_hex2point_test_helper(group, NULL,
  2187. POINT_CONVERSION_HYBRID,
  2188. bnctx)))
  2189. goto err;
  2190. ret = 1;
  2191. err:
  2192. EC_POINT_free(P);
  2193. EC_GROUP_free(group);
  2194. BN_CTX_free(bnctx);
  2195. return ret;
  2196. }
  2197. static int do_test_custom_explicit_fromdata(EC_GROUP *group, BN_CTX *ctx,
  2198. unsigned char *gen, int gen_size)
  2199. {
  2200. int ret = 0, i_out;
  2201. EVP_PKEY_CTX *pctx = NULL;
  2202. EVP_PKEY *pkeyparam = NULL;
  2203. OSSL_PARAM_BLD *bld = NULL;
  2204. const char *field_name;
  2205. OSSL_PARAM *params = NULL;
  2206. const OSSL_PARAM *gettable;
  2207. BIGNUM *p, *a, *b;
  2208. BIGNUM *p_out = NULL, *a_out = NULL, *b_out = NULL;
  2209. BIGNUM *order_out = NULL, *cofactor_out = NULL;
  2210. char name[80];
  2211. unsigned char buf[1024];
  2212. size_t buf_len, name_len;
  2213. #ifndef OPENSSL_NO_EC2M
  2214. unsigned int k1 = 0, k2 = 0, k3 = 0;
  2215. const char *basis_name = NULL;
  2216. #endif
  2217. p = BN_CTX_get(ctx);
  2218. a = BN_CTX_get(ctx);
  2219. b = BN_CTX_get(ctx);
  2220. if (!TEST_ptr(b)
  2221. || !TEST_ptr(bld = OSSL_PARAM_BLD_new()))
  2222. goto err;
  2223. if (EC_GROUP_get_field_type(group) == NID_X9_62_prime_field) {
  2224. field_name = SN_X9_62_prime_field;
  2225. } else {
  2226. field_name = SN_X9_62_characteristic_two_field;
  2227. #ifndef OPENSSL_NO_EC2M
  2228. if (EC_GROUP_get_basis_type(group) == NID_X9_62_tpBasis) {
  2229. basis_name = SN_X9_62_tpBasis;
  2230. if (!TEST_true(EC_GROUP_get_trinomial_basis(group, &k1)))
  2231. goto err;
  2232. } else {
  2233. basis_name = SN_X9_62_ppBasis;
  2234. if (!TEST_true(EC_GROUP_get_pentanomial_basis(group, &k1, &k2, &k3)))
  2235. goto err;
  2236. }
  2237. #endif /* OPENSSL_NO_EC2M */
  2238. }
  2239. if (!TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx))
  2240. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
  2241. OSSL_PKEY_PARAM_EC_FIELD_TYPE, field_name, 0))
  2242. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, p))
  2243. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, a))
  2244. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, b)))
  2245. goto err;
  2246. if (EC_GROUP_get0_seed(group) != NULL) {
  2247. if (!TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  2248. OSSL_PKEY_PARAM_EC_SEED, EC_GROUP_get0_seed(group),
  2249. EC_GROUP_get_seed_len(group))))
  2250. goto err;
  2251. }
  2252. if (EC_GROUP_get0_cofactor(group) != NULL) {
  2253. if (!TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR,
  2254. EC_GROUP_get0_cofactor(group))))
  2255. goto err;
  2256. }
  2257. if (!TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
  2258. OSSL_PKEY_PARAM_EC_GENERATOR, gen, gen_size))
  2259. || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_ORDER,
  2260. EC_GROUP_get0_order(group))))
  2261. goto err;
  2262. if (!TEST_ptr(params = OSSL_PARAM_BLD_to_param(bld))
  2263. || !TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL))
  2264. || !TEST_int_gt(EVP_PKEY_fromdata_init(pctx), 0)
  2265. || !TEST_int_gt(EVP_PKEY_fromdata(pctx, &pkeyparam,
  2266. EVP_PKEY_KEY_PARAMETERS, params), 0))
  2267. goto err;
  2268. /*- Check that all the set values are retrievable -*/
  2269. /* There should be no match to a group name since the generator changed */
  2270. if (!TEST_false(EVP_PKEY_get_utf8_string_param(pkeyparam,
  2271. OSSL_PKEY_PARAM_GROUP_NAME, name, sizeof(name),
  2272. &name_len)))
  2273. goto err;
  2274. /* The encoding should be explicit as it has no group */
  2275. if (!TEST_true(EVP_PKEY_get_utf8_string_param(pkeyparam,
  2276. OSSL_PKEY_PARAM_EC_ENCODING,
  2277. name, sizeof(name), &name_len))
  2278. || !TEST_str_eq(name, OSSL_PKEY_EC_ENCODING_EXPLICIT))
  2279. goto err;
  2280. if (!TEST_true(EVP_PKEY_get_utf8_string_param(pkeyparam,
  2281. OSSL_PKEY_PARAM_EC_FIELD_TYPE, name, sizeof(name),
  2282. &name_len))
  2283. || !TEST_str_eq(name, field_name))
  2284. goto err;
  2285. if (!TEST_true(EVP_PKEY_get_octet_string_param(pkeyparam,
  2286. OSSL_PKEY_PARAM_EC_GENERATOR, buf, sizeof(buf), &buf_len))
  2287. || !TEST_mem_eq(buf, (int)buf_len, gen, gen_size))
  2288. goto err;
  2289. if (!TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_P, &p_out))
  2290. || !TEST_BN_eq(p_out, p)
  2291. || !TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_A,
  2292. &a_out))
  2293. || !TEST_BN_eq(a_out, a)
  2294. || !TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_B,
  2295. &b_out))
  2296. || !TEST_BN_eq(b_out, b)
  2297. || !TEST_true(EVP_PKEY_get_bn_param(pkeyparam, OSSL_PKEY_PARAM_EC_ORDER,
  2298. &order_out))
  2299. || !TEST_BN_eq(order_out, EC_GROUP_get0_order(group)))
  2300. goto err;
  2301. if (EC_GROUP_get0_cofactor(group) != NULL) {
  2302. if (!TEST_true(EVP_PKEY_get_bn_param(pkeyparam,
  2303. OSSL_PKEY_PARAM_EC_COFACTOR, &cofactor_out))
  2304. || !TEST_BN_eq(cofactor_out, EC_GROUP_get0_cofactor(group)))
  2305. goto err;
  2306. }
  2307. if (EC_GROUP_get0_seed(group) != NULL) {
  2308. if (!TEST_true(EVP_PKEY_get_octet_string_param(pkeyparam,
  2309. OSSL_PKEY_PARAM_EC_SEED, buf, sizeof(buf), &buf_len))
  2310. || !TEST_mem_eq(buf, buf_len, EC_GROUP_get0_seed(group),
  2311. EC_GROUP_get_seed_len(group)))
  2312. goto err;
  2313. }
  2314. if (EC_GROUP_get_field_type(group) == NID_X9_62_prime_field) {
  2315. /* No extra fields should be set for a prime field */
  2316. if (!TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2317. OSSL_PKEY_PARAM_EC_CHAR2_M, &i_out))
  2318. || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2319. OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, &i_out))
  2320. || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2321. OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, &i_out))
  2322. || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2323. OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, &i_out))
  2324. || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2325. OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, &i_out))
  2326. || !TEST_false(EVP_PKEY_get_utf8_string_param(pkeyparam,
  2327. OSSL_PKEY_PARAM_EC_CHAR2_TYPE, name, sizeof(name),
  2328. &name_len)))
  2329. goto err;
  2330. } else {
  2331. #ifndef OPENSSL_NO_EC2M
  2332. if (!TEST_true(EVP_PKEY_get_int_param(pkeyparam,
  2333. OSSL_PKEY_PARAM_EC_CHAR2_M, &i_out))
  2334. || !TEST_int_eq(EC_GROUP_get_degree(group), i_out)
  2335. || !TEST_true(EVP_PKEY_get_utf8_string_param(pkeyparam,
  2336. OSSL_PKEY_PARAM_EC_CHAR2_TYPE, name, sizeof(name),
  2337. &name_len))
  2338. || !TEST_str_eq(name, basis_name))
  2339. goto err;
  2340. if (EC_GROUP_get_basis_type(group) == NID_X9_62_tpBasis) {
  2341. if (!TEST_true(EVP_PKEY_get_int_param(pkeyparam,
  2342. OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, &i_out))
  2343. || !TEST_int_eq(k1, i_out)
  2344. || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2345. OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, &i_out))
  2346. || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2347. OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, &i_out))
  2348. || !TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2349. OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, &i_out)))
  2350. goto err;
  2351. } else {
  2352. if (!TEST_false(EVP_PKEY_get_int_param(pkeyparam,
  2353. OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS, &i_out))
  2354. || !TEST_true(EVP_PKEY_get_int_param(pkeyparam,
  2355. OSSL_PKEY_PARAM_EC_CHAR2_PP_K1, &i_out))
  2356. || !TEST_int_eq(k1, i_out)
  2357. || !TEST_true(EVP_PKEY_get_int_param(pkeyparam,
  2358. OSSL_PKEY_PARAM_EC_CHAR2_PP_K2, &i_out))
  2359. || !TEST_int_eq(k2, i_out)
  2360. || !TEST_true(EVP_PKEY_get_int_param(pkeyparam,
  2361. OSSL_PKEY_PARAM_EC_CHAR2_PP_K3, &i_out))
  2362. || !TEST_int_eq(k3, i_out))
  2363. goto err;
  2364. }
  2365. #endif /* OPENSSL_NO_EC2M */
  2366. }
  2367. if (!TEST_ptr(gettable = EVP_PKEY_gettable_params(pkeyparam))
  2368. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_GROUP_NAME))
  2369. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_ENCODING))
  2370. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_FIELD_TYPE))
  2371. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_P))
  2372. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_A))
  2373. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_B))
  2374. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_GENERATOR))
  2375. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_ORDER))
  2376. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_COFACTOR))
  2377. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_SEED))
  2378. #ifndef OPENSSL_NO_EC2M
  2379. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_M))
  2380. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_TYPE))
  2381. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_TP_BASIS))
  2382. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_PP_K1))
  2383. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_PP_K2))
  2384. || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_EC_CHAR2_PP_K3))
  2385. #endif
  2386. )
  2387. goto err;
  2388. ret = 1;
  2389. err:
  2390. BN_free(order_out);
  2391. BN_free(cofactor_out);
  2392. BN_free(a_out);
  2393. BN_free(b_out);
  2394. BN_free(p_out);
  2395. OSSL_PARAM_free(params);
  2396. OSSL_PARAM_BLD_free(bld);
  2397. EVP_PKEY_free(pkeyparam);
  2398. EVP_PKEY_CTX_free(pctx);
  2399. return ret;
  2400. }
  2401. /*
  2402. * check the EC_METHOD respects the supplied EC_GROUP_set_generator G
  2403. */
  2404. static int custom_generator_test(int id)
  2405. {
  2406. int ret = 0, nid, bsize;
  2407. EC_GROUP *group = NULL;
  2408. EC_POINT *G2 = NULL, *Q1 = NULL, *Q2 = NULL;
  2409. BN_CTX *ctx = NULL;
  2410. BIGNUM *k = NULL;
  2411. unsigned char *b1 = NULL, *b2 = NULL;
  2412. /* Do some setup */
  2413. nid = curves[id].nid;
  2414. TEST_note("Curve %s", OBJ_nid2sn(nid));
  2415. if (!TEST_ptr(ctx = BN_CTX_new()))
  2416. return 0;
  2417. BN_CTX_start(ctx);
  2418. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid)))
  2419. goto err;
  2420. /* expected byte length of encoded points */
  2421. bsize = (EC_GROUP_get_degree(group) + 7) / 8;
  2422. bsize = 1 + 2 * bsize; /* UNCOMPRESSED_POINT format */
  2423. if (!TEST_ptr(k = BN_CTX_get(ctx))
  2424. /* fetch a testing scalar k != 0,1 */
  2425. || !TEST_true(BN_rand(k, EC_GROUP_order_bits(group) - 1,
  2426. BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
  2427. /* make k even */
  2428. || !TEST_true(BN_clear_bit(k, 0))
  2429. || !TEST_ptr(G2 = EC_POINT_new(group))
  2430. || !TEST_ptr(Q1 = EC_POINT_new(group))
  2431. /* Q1 := kG */
  2432. || !TEST_true(EC_POINT_mul(group, Q1, k, NULL, NULL, ctx))
  2433. /* pull out the bytes of that */
  2434. || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
  2435. POINT_CONVERSION_UNCOMPRESSED, NULL,
  2436. 0, ctx), bsize)
  2437. || !TEST_ptr(b1 = OPENSSL_malloc(bsize))
  2438. || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
  2439. POINT_CONVERSION_UNCOMPRESSED, b1,
  2440. bsize, ctx), bsize)
  2441. /* new generator is G2 := 2G */
  2442. || !TEST_true(EC_POINT_dbl(group, G2, EC_GROUP_get0_generator(group),
  2443. ctx))
  2444. || !TEST_true(EC_GROUP_set_generator(group, G2,
  2445. EC_GROUP_get0_order(group),
  2446. EC_GROUP_get0_cofactor(group)))
  2447. || !TEST_ptr(Q2 = EC_POINT_new(group))
  2448. || !TEST_true(BN_rshift1(k, k))
  2449. /* Q2 := k/2 G2 */
  2450. || !TEST_true(EC_POINT_mul(group, Q2, k, NULL, NULL, ctx))
  2451. || !TEST_int_eq(EC_POINT_point2oct(group, Q2,
  2452. POINT_CONVERSION_UNCOMPRESSED, NULL,
  2453. 0, ctx), bsize)
  2454. || !TEST_ptr(b2 = OPENSSL_malloc(bsize))
  2455. || !TEST_int_eq(EC_POINT_point2oct(group, Q2,
  2456. POINT_CONVERSION_UNCOMPRESSED, b2,
  2457. bsize, ctx), bsize)
  2458. /* Q1 = kG = k/2 G2 = Q2 should hold */
  2459. || !TEST_mem_eq(b1, bsize, b2, bsize))
  2460. goto err;
  2461. if (!do_test_custom_explicit_fromdata(group, ctx, b1, bsize))
  2462. goto err;
  2463. ret = 1;
  2464. err:
  2465. EC_POINT_free(Q1);
  2466. EC_POINT_free(Q2);
  2467. EC_POINT_free(G2);
  2468. EC_GROUP_free(group);
  2469. BN_CTX_end(ctx);
  2470. BN_CTX_free(ctx);
  2471. OPENSSL_free(b1);
  2472. OPENSSL_free(b2);
  2473. return ret;
  2474. }
  2475. /*
  2476. * check creation of curves from explicit params through the public API
  2477. */
  2478. static int custom_params_test(int id)
  2479. {
  2480. int ret = 0, nid, bsize;
  2481. const char *curve_name = NULL;
  2482. EC_GROUP *group = NULL, *altgroup = NULL;
  2483. EC_POINT *G2 = NULL, *Q1 = NULL, *Q2 = NULL;
  2484. const EC_POINT *Q = NULL;
  2485. BN_CTX *ctx = NULL;
  2486. BIGNUM *k = NULL;
  2487. unsigned char *buf1 = NULL, *buf2 = NULL;
  2488. const BIGNUM *z = NULL, *cof = NULL, *priv1 = NULL;
  2489. BIGNUM *p = NULL, *a = NULL, *b = NULL;
  2490. int is_prime = 0;
  2491. EC_KEY *eckey1 = NULL, *eckey2 = NULL;
  2492. EVP_PKEY *pkey1 = NULL, *pkey2 = NULL;
  2493. EVP_PKEY_CTX *pctx1 = NULL, *pctx2 = NULL;
  2494. size_t sslen, t;
  2495. unsigned char *pub1 = NULL , *pub2 = NULL;
  2496. OSSL_PARAM_BLD *param_bld = NULL;
  2497. OSSL_PARAM *params1 = NULL, *params2 = NULL;
  2498. /* Do some setup */
  2499. nid = curves[id].nid;
  2500. curve_name = OBJ_nid2sn(nid);
  2501. TEST_note("Curve %s", curve_name);
  2502. if (nid == NID_sm2)
  2503. return TEST_skip("custom params not supported with SM2");
  2504. if (!TEST_ptr(ctx = BN_CTX_new()))
  2505. return 0;
  2506. BN_CTX_start(ctx);
  2507. if (!TEST_ptr(p = BN_CTX_get(ctx))
  2508. || !TEST_ptr(a = BN_CTX_get(ctx))
  2509. || !TEST_ptr(b = BN_CTX_get(ctx))
  2510. || !TEST_ptr(k = BN_CTX_get(ctx)))
  2511. goto err;
  2512. if (!TEST_ptr(group = EC_GROUP_new_by_curve_name(nid)))
  2513. goto err;
  2514. is_prime = EC_GROUP_get_field_type(group) == NID_X9_62_prime_field;
  2515. #ifdef OPENSSL_NO_EC2M
  2516. if (!is_prime) {
  2517. ret = TEST_skip("binary curves not supported in this build");
  2518. goto err;
  2519. }
  2520. #endif
  2521. /* expected byte length of encoded points */
  2522. bsize = (EC_GROUP_get_degree(group) + 7) / 8;
  2523. bsize = 1 + 2 * bsize; /* UNCOMPRESSED_POINT format */
  2524. /* extract parameters from built-in curve */
  2525. if (!TEST_true(EC_GROUP_get_curve(group, p, a, b, ctx))
  2526. || !TEST_ptr(G2 = EC_POINT_new(group))
  2527. /* new generator is G2 := 2G */
  2528. || !TEST_true(EC_POINT_dbl(group, G2,
  2529. EC_GROUP_get0_generator(group), ctx))
  2530. /* pull out the bytes of that */
  2531. || !TEST_int_eq(EC_POINT_point2oct(group, G2,
  2532. POINT_CONVERSION_UNCOMPRESSED,
  2533. NULL, 0, ctx), bsize)
  2534. || !TEST_ptr(buf1 = OPENSSL_malloc(bsize))
  2535. || !TEST_int_eq(EC_POINT_point2oct(group, G2,
  2536. POINT_CONVERSION_UNCOMPRESSED,
  2537. buf1, bsize, ctx), bsize)
  2538. || !TEST_ptr(z = EC_GROUP_get0_order(group))
  2539. || !TEST_ptr(cof = EC_GROUP_get0_cofactor(group))
  2540. )
  2541. goto err;
  2542. /* create a new group using same params (but different generator) */
  2543. if (is_prime) {
  2544. if (!TEST_ptr(altgroup = EC_GROUP_new_curve_GFp(p, a, b, ctx)))
  2545. goto err;
  2546. }
  2547. #ifndef OPENSSL_NO_EC2M
  2548. else {
  2549. if (!TEST_ptr(altgroup = EC_GROUP_new_curve_GF2m(p, a, b, ctx)))
  2550. goto err;
  2551. }
  2552. #endif
  2553. /* set 2*G as the generator of altgroup */
  2554. EC_POINT_free(G2); /* discard G2 as it refers to the original group */
  2555. if (!TEST_ptr(G2 = EC_POINT_new(altgroup))
  2556. || !TEST_true(EC_POINT_oct2point(altgroup, G2, buf1, bsize, ctx))
  2557. || !TEST_int_eq(EC_POINT_is_on_curve(altgroup, G2, ctx), 1)
  2558. || !TEST_true(EC_GROUP_set_generator(altgroup, G2, z, cof))
  2559. )
  2560. goto err;
  2561. /* verify math checks out */
  2562. if (/* allocate temporary points on group and altgroup */
  2563. !TEST_ptr(Q1 = EC_POINT_new(group))
  2564. || !TEST_ptr(Q2 = EC_POINT_new(altgroup))
  2565. /* fetch a testing scalar k != 0,1 */
  2566. || !TEST_true(BN_rand(k, EC_GROUP_order_bits(group) - 1,
  2567. BN_RAND_TOP_ONE, BN_RAND_BOTTOM_ANY))
  2568. /* make k even */
  2569. || !TEST_true(BN_clear_bit(k, 0))
  2570. /* Q1 := kG on group */
  2571. || !TEST_true(EC_POINT_mul(group, Q1, k, NULL, NULL, ctx))
  2572. /* pull out the bytes of that */
  2573. || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
  2574. POINT_CONVERSION_UNCOMPRESSED,
  2575. NULL, 0, ctx), bsize)
  2576. || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
  2577. POINT_CONVERSION_UNCOMPRESSED,
  2578. buf1, bsize, ctx), bsize)
  2579. /* k := k/2 */
  2580. || !TEST_true(BN_rshift1(k, k))
  2581. /* Q2 := k/2 G2 on altgroup */
  2582. || !TEST_true(EC_POINT_mul(altgroup, Q2, k, NULL, NULL, ctx))
  2583. /* pull out the bytes of that */
  2584. || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q2,
  2585. POINT_CONVERSION_UNCOMPRESSED,
  2586. NULL, 0, ctx), bsize)
  2587. || !TEST_ptr(buf2 = OPENSSL_malloc(bsize))
  2588. || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q2,
  2589. POINT_CONVERSION_UNCOMPRESSED,
  2590. buf2, bsize, ctx), bsize)
  2591. /* Q1 = kG = k/2 G2 = Q2 should hold */
  2592. || !TEST_mem_eq(buf1, bsize, buf2, bsize))
  2593. goto err;
  2594. /* create two `EC_KEY`s on altgroup */
  2595. if (!TEST_ptr(eckey1 = EC_KEY_new())
  2596. || !TEST_true(EC_KEY_set_group(eckey1, altgroup))
  2597. || !TEST_true(EC_KEY_generate_key(eckey1))
  2598. || !TEST_ptr(eckey2 = EC_KEY_new())
  2599. || !TEST_true(EC_KEY_set_group(eckey2, altgroup))
  2600. || !TEST_true(EC_KEY_generate_key(eckey2)))
  2601. goto err;
  2602. /* retrieve priv1 for later */
  2603. if (!TEST_ptr(priv1 = EC_KEY_get0_private_key(eckey1)))
  2604. goto err;
  2605. /*
  2606. * retrieve bytes for pub1 for later
  2607. *
  2608. * We compute the pub key in the original group as we will later use it to
  2609. * define a provider key in the built-in group.
  2610. */
  2611. if (!TEST_true(EC_POINT_mul(group, Q1, priv1, NULL, NULL, ctx))
  2612. || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
  2613. POINT_CONVERSION_UNCOMPRESSED,
  2614. NULL, 0, ctx), bsize)
  2615. || !TEST_ptr(pub1 = OPENSSL_malloc(bsize))
  2616. || !TEST_int_eq(EC_POINT_point2oct(group, Q1,
  2617. POINT_CONVERSION_UNCOMPRESSED,
  2618. pub1, bsize, ctx), bsize))
  2619. goto err;
  2620. /* retrieve bytes for pub2 for later */
  2621. if (!TEST_ptr(Q = EC_KEY_get0_public_key(eckey2))
  2622. || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q,
  2623. POINT_CONVERSION_UNCOMPRESSED,
  2624. NULL, 0, ctx), bsize)
  2625. || !TEST_ptr(pub2 = OPENSSL_malloc(bsize))
  2626. || !TEST_int_eq(EC_POINT_point2oct(altgroup, Q,
  2627. POINT_CONVERSION_UNCOMPRESSED,
  2628. pub2, bsize, ctx), bsize))
  2629. goto err;
  2630. /* create two `EVP_PKEY`s from the `EC_KEY`s */
  2631. if (!TEST_ptr(pkey1 = EVP_PKEY_new())
  2632. || !TEST_int_eq(EVP_PKEY_assign_EC_KEY(pkey1, eckey1), 1))
  2633. goto err;
  2634. eckey1 = NULL; /* ownership passed to pkey1 */
  2635. if (!TEST_ptr(pkey2 = EVP_PKEY_new())
  2636. || !TEST_int_eq(EVP_PKEY_assign_EC_KEY(pkey2, eckey2), 1))
  2637. goto err;
  2638. eckey2 = NULL; /* ownership passed to pkey2 */
  2639. /* Compute keyexchange in both directions */
  2640. if (!TEST_ptr(pctx1 = EVP_PKEY_CTX_new(pkey1, NULL))
  2641. || !TEST_int_eq(EVP_PKEY_derive_init(pctx1), 1)
  2642. || !TEST_int_eq(EVP_PKEY_derive_set_peer(pctx1, pkey2), 1)
  2643. || !TEST_int_eq(EVP_PKEY_derive(pctx1, NULL, &sslen), 1)
  2644. || !TEST_int_gt(bsize, sslen)
  2645. || !TEST_int_eq(EVP_PKEY_derive(pctx1, buf1, &sslen), 1))
  2646. goto err;
  2647. if (!TEST_ptr(pctx2 = EVP_PKEY_CTX_new(pkey2, NULL))
  2648. || !TEST_int_eq(EVP_PKEY_derive_init(pctx2), 1)
  2649. || !TEST_int_eq(EVP_PKEY_derive_set_peer(pctx2, pkey1), 1)
  2650. || !TEST_int_eq(EVP_PKEY_derive(pctx2, NULL, &t), 1)
  2651. || !TEST_int_gt(bsize, t)
  2652. || !TEST_int_le(sslen, t)
  2653. || !TEST_int_eq(EVP_PKEY_derive(pctx2, buf2, &t), 1))
  2654. goto err;
  2655. /* Both sides should expect the same shared secret */
  2656. if (!TEST_mem_eq(buf1, sslen, buf2, t))
  2657. goto err;
  2658. /* Build parameters for provider-native keys */
  2659. if (!TEST_ptr(param_bld = OSSL_PARAM_BLD_new())
  2660. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(param_bld,
  2661. OSSL_PKEY_PARAM_GROUP_NAME,
  2662. curve_name, 0))
  2663. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(param_bld,
  2664. OSSL_PKEY_PARAM_PUB_KEY,
  2665. pub1, bsize))
  2666. || !TEST_true(OSSL_PARAM_BLD_push_BN(param_bld,
  2667. OSSL_PKEY_PARAM_PRIV_KEY,
  2668. priv1))
  2669. || !TEST_ptr(params1 = OSSL_PARAM_BLD_to_param(param_bld)))
  2670. goto err;
  2671. OSSL_PARAM_BLD_free(param_bld);
  2672. if (!TEST_ptr(param_bld = OSSL_PARAM_BLD_new())
  2673. || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(param_bld,
  2674. OSSL_PKEY_PARAM_GROUP_NAME,
  2675. curve_name, 0))
  2676. || !TEST_true(OSSL_PARAM_BLD_push_octet_string(param_bld,
  2677. OSSL_PKEY_PARAM_PUB_KEY,
  2678. pub2, bsize))
  2679. || !TEST_ptr(params2 = OSSL_PARAM_BLD_to_param(param_bld)))
  2680. goto err;
  2681. /* create two new provider-native `EVP_PKEY`s */
  2682. EVP_PKEY_CTX_free(pctx2);
  2683. if (!TEST_ptr(pctx2 = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL))
  2684. || !TEST_int_eq(EVP_PKEY_fromdata_init(pctx2), 1)
  2685. || !TEST_int_eq(EVP_PKEY_fromdata(pctx2, &pkey1, EVP_PKEY_KEYPAIR,
  2686. params1), 1)
  2687. || !TEST_int_eq(EVP_PKEY_fromdata(pctx2, &pkey2, EVP_PKEY_PUBLIC_KEY,
  2688. params2), 1))
  2689. goto err;
  2690. /* compute keyexchange once more using the provider keys */
  2691. EVP_PKEY_CTX_free(pctx1);
  2692. if (!TEST_ptr(pctx1 = EVP_PKEY_CTX_new(pkey1, NULL))
  2693. || !TEST_int_eq(EVP_PKEY_derive_init(pctx1), 1)
  2694. || !TEST_int_eq(EVP_PKEY_derive_set_peer(pctx1, pkey2), 1)
  2695. || !TEST_int_eq(EVP_PKEY_derive(pctx1, NULL, &t), 1)
  2696. || !TEST_int_gt(bsize, t)
  2697. || !TEST_int_le(sslen, t)
  2698. || !TEST_int_eq(EVP_PKEY_derive(pctx1, buf1, &t), 1)
  2699. /* compare with previous result */
  2700. || !TEST_mem_eq(buf1, t, buf2, sslen))
  2701. goto err;
  2702. ret = 1;
  2703. err:
  2704. BN_CTX_end(ctx);
  2705. BN_CTX_free(ctx);
  2706. OSSL_PARAM_BLD_free(param_bld);
  2707. OSSL_PARAM_free(params1);
  2708. OSSL_PARAM_free(params2);
  2709. EC_POINT_free(Q1);
  2710. EC_POINT_free(Q2);
  2711. EC_POINT_free(G2);
  2712. EC_GROUP_free(group);
  2713. EC_GROUP_free(altgroup);
  2714. OPENSSL_free(buf1);
  2715. OPENSSL_free(buf2);
  2716. OPENSSL_free(pub1);
  2717. OPENSSL_free(pub2);
  2718. EC_KEY_free(eckey1);
  2719. EC_KEY_free(eckey2);
  2720. EVP_PKEY_free(pkey1);
  2721. EVP_PKEY_free(pkey2);
  2722. EVP_PKEY_CTX_free(pctx1);
  2723. EVP_PKEY_CTX_free(pctx2);
  2724. return ret;
  2725. }
  2726. static int ec_d2i_publickey_test(void)
  2727. {
  2728. unsigned char buf[1000];
  2729. unsigned char *pubkey_enc = buf;
  2730. const unsigned char *pk_enc = pubkey_enc;
  2731. EVP_PKEY *gen_key = NULL, *decoded_key = NULL;
  2732. EVP_PKEY_CTX *pctx = NULL;
  2733. int pklen, ret = 0;
  2734. OSSL_PARAM params[2];
  2735. if (!TEST_ptr(gen_key = EVP_EC_gen("P-256")))
  2736. goto err;
  2737. if (!TEST_int_gt(pklen = i2d_PublicKey(gen_key, &pubkey_enc), 0))
  2738. goto err;
  2739. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME,
  2740. "P-256", 0);
  2741. params[1] = OSSL_PARAM_construct_end();
  2742. if (!TEST_ptr(pctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL))
  2743. || !TEST_true(EVP_PKEY_fromdata_init(pctx))
  2744. || !TEST_true(EVP_PKEY_fromdata(pctx, &decoded_key,
  2745. OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
  2746. params))
  2747. || !TEST_ptr(decoded_key)
  2748. || !TEST_ptr(decoded_key = d2i_PublicKey(EVP_PKEY_EC, &decoded_key,
  2749. &pk_enc, pklen)))
  2750. goto err;
  2751. if (!TEST_true(EVP_PKEY_eq(gen_key, decoded_key)))
  2752. goto err;
  2753. ret = 1;
  2754. err:
  2755. EVP_PKEY_CTX_free(pctx);
  2756. EVP_PKEY_free(gen_key);
  2757. EVP_PKEY_free(decoded_key);
  2758. return ret;
  2759. }
  2760. int setup_tests(void)
  2761. {
  2762. crv_len = EC_get_builtin_curves(NULL, 0);
  2763. if (!TEST_ptr(curves = OPENSSL_malloc(sizeof(*curves) * crv_len))
  2764. || !TEST_true(EC_get_builtin_curves(curves, crv_len)))
  2765. return 0;
  2766. ADD_TEST(parameter_test);
  2767. ADD_TEST(cofactor_range_test);
  2768. ADD_ALL_TESTS(cardinality_test, crv_len);
  2769. ADD_TEST(prime_field_tests);
  2770. #ifndef OPENSSL_NO_EC2M
  2771. ADD_TEST(hybrid_point_encoding_test);
  2772. ADD_TEST(char2_field_tests);
  2773. ADD_ALL_TESTS(char2_curve_test, OSSL_NELEM(char2_curve_tests));
  2774. #endif
  2775. ADD_ALL_TESTS(nistp_single_test, OSSL_NELEM(nistp_tests_params));
  2776. ADD_ALL_TESTS(internal_curve_test, crv_len);
  2777. ADD_ALL_TESTS(internal_curve_test_method, crv_len);
  2778. ADD_TEST(group_field_test);
  2779. ADD_ALL_TESTS(check_named_curve_test, crv_len);
  2780. ADD_ALL_TESTS(check_named_curve_lookup_test, crv_len);
  2781. ADD_ALL_TESTS(check_ec_key_field_public_range_test, crv_len);
  2782. ADD_ALL_TESTS(check_named_curve_from_ecparameters, crv_len);
  2783. ADD_ALL_TESTS(ec_point_hex2point_test, crv_len);
  2784. ADD_ALL_TESTS(custom_generator_test, crv_len);
  2785. ADD_ALL_TESTS(custom_params_test, crv_len);
  2786. ADD_TEST(ec_d2i_publickey_test);
  2787. return 1;
  2788. }
  2789. void cleanup_tests(void)
  2790. {
  2791. OPENSSL_free(curves);
  2792. }