ectest.c 91 KB

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