ectest.c 97 KB

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