ectest.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870
  1. /* crypto/ec/ectest.c */
  2. /*
  3. * Originally written by Bodo Moeller for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * openssl-core@openssl.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. /* ====================================================================
  59. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  60. *
  61. * Portions of the attached software ("Contribution") are developed by
  62. * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
  63. *
  64. * The Contribution is licensed pursuant to the OpenSSL open source
  65. * license provided above.
  66. *
  67. * The elliptic curve binary polynomial software is originally written by
  68. * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
  69. *
  70. */
  71. #include <stdio.h>
  72. #include <stdlib.h>
  73. #ifdef FLAT_INC
  74. # include "e_os.h"
  75. #else
  76. # include "../e_os.h"
  77. #endif
  78. #include <string.h>
  79. #include <time.h>
  80. #ifdef OPENSSL_NO_EC
  81. int main(int argc, char *argv[])
  82. {
  83. puts("Elliptic curves are disabled.");
  84. return 0;
  85. }
  86. #else
  87. # include <openssl/ec.h>
  88. # ifndef OPENSSL_NO_ENGINE
  89. # include <openssl/engine.h>
  90. # endif
  91. # include <openssl/err.h>
  92. # include <openssl/obj_mac.h>
  93. # include <openssl/objects.h>
  94. # include <openssl/rand.h>
  95. # include <openssl/bn.h>
  96. # include <openssl/opensslconf.h>
  97. # if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12)
  98. /* suppress "too big too optimize" warning */
  99. # pragma warning(disable:4959)
  100. # endif
  101. # define ABORT do { \
  102. fflush(stdout); \
  103. fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
  104. ERR_print_errors_fp(stderr); \
  105. EXIT(1); \
  106. } while (0)
  107. # define TIMING_BASE_PT 0
  108. # define TIMING_RAND_PT 1
  109. # define TIMING_SIMUL 2
  110. # if 0
  111. static void timings(EC_GROUP *group, int type, BN_CTX *ctx)
  112. {
  113. clock_t clck;
  114. int i, j;
  115. BIGNUM *s;
  116. BIGNUM *r[10], *r0[10];
  117. EC_POINT *P;
  118. s = BN_new();
  119. if (s == NULL)
  120. ABORT;
  121. fprintf(stdout, "Timings for %d-bit field, ", EC_GROUP_get_degree(group));
  122. if (!EC_GROUP_get_order(group, s, ctx))
  123. ABORT;
  124. fprintf(stdout, "%d-bit scalars ", (int)BN_num_bits(s));
  125. fflush(stdout);
  126. P = EC_POINT_new(group);
  127. if (P == NULL)
  128. ABORT;
  129. EC_POINT_copy(P, EC_GROUP_get0_generator(group));
  130. for (i = 0; i < 10; i++) {
  131. if ((r[i] = BN_new()) == NULL)
  132. ABORT;
  133. if (!BN_pseudo_rand(r[i], BN_num_bits(s), 0, 0))
  134. ABORT;
  135. if (type != TIMING_BASE_PT) {
  136. if ((r0[i] = BN_new()) == NULL)
  137. ABORT;
  138. if (!BN_pseudo_rand(r0[i], BN_num_bits(s), 0, 0))
  139. ABORT;
  140. }
  141. }
  142. clck = clock();
  143. for (i = 0; i < 10; i++) {
  144. for (j = 0; j < 10; j++) {
  145. if (!EC_POINT_mul
  146. (group, P, (type != TIMING_RAND_PT) ? r[i] : NULL,
  147. (type != TIMING_BASE_PT) ? P : NULL,
  148. (type != TIMING_BASE_PT) ? r0[i] : NULL, ctx))
  149. ABORT;
  150. }
  151. }
  152. clck = clock() - clck;
  153. fprintf(stdout, "\n");
  154. # ifdef CLOCKS_PER_SEC
  155. /*
  156. * "To determine the time in seconds, the value returned by the clock
  157. * function should be divided by the value of the macro CLOCKS_PER_SEC."
  158. * -- ISO/IEC 9899
  159. */
  160. # define UNIT "s"
  161. # else
  162. /*
  163. * "`CLOCKS_PER_SEC' undeclared (first use this function)" -- cc on
  164. * NeXTstep/OpenStep
  165. */
  166. # define UNIT "units"
  167. # define CLOCKS_PER_SEC 1
  168. # endif
  169. if (type == TIMING_BASE_PT) {
  170. fprintf(stdout, "%i %s in %.2f " UNIT "\n", i * j,
  171. "base point multiplications", (double)clck / CLOCKS_PER_SEC);
  172. } else if (type == TIMING_RAND_PT) {
  173. fprintf(stdout, "%i %s in %.2f " UNIT "\n", i * j,
  174. "random point multiplications",
  175. (double)clck / CLOCKS_PER_SEC);
  176. } else if (type == TIMING_SIMUL) {
  177. fprintf(stdout, "%i %s in %.2f " UNIT "\n", i * j,
  178. "s*P+t*Q operations", (double)clck / CLOCKS_PER_SEC);
  179. }
  180. fprintf(stdout, "average: %.4f " UNIT "\n",
  181. (double)clck / (CLOCKS_PER_SEC * i * j));
  182. EC_POINT_free(P);
  183. BN_free(s);
  184. for (i = 0; i < 10; i++) {
  185. BN_free(r[i]);
  186. if (type != TIMING_BASE_PT)
  187. BN_free(r0[i]);
  188. }
  189. }
  190. # endif
  191. /* test multiplication with group order, long and negative scalars */
  192. static void group_order_tests(EC_GROUP *group)
  193. {
  194. BIGNUM *n1, *n2, *order;
  195. EC_POINT *P = EC_POINT_new(group);
  196. EC_POINT *Q = EC_POINT_new(group);
  197. BN_CTX *ctx = BN_CTX_new();
  198. int i;
  199. n1 = BN_new();
  200. n2 = BN_new();
  201. order = BN_new();
  202. fprintf(stdout, "verify group order ...");
  203. fflush(stdout);
  204. if (!EC_GROUP_get_order(group, order, ctx))
  205. ABORT;
  206. if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
  207. ABORT;
  208. if (!EC_POINT_is_at_infinity(group, Q))
  209. ABORT;
  210. fprintf(stdout, ".");
  211. fflush(stdout);
  212. if (!EC_GROUP_precompute_mult(group, ctx))
  213. ABORT;
  214. if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx))
  215. ABORT;
  216. if (!EC_POINT_is_at_infinity(group, Q))
  217. ABORT;
  218. fprintf(stdout, " ok\n");
  219. fprintf(stdout, "long/negative scalar tests ");
  220. for (i = 1; i <= 2; i++) {
  221. const BIGNUM *scalars[6];
  222. const EC_POINT *points[6];
  223. fprintf(stdout, i == 1 ?
  224. "allowing precomputation ... " :
  225. "without precomputation ... ");
  226. if (!BN_set_word(n1, i))
  227. ABORT;
  228. /*
  229. * If i == 1, P will be the predefined generator for which
  230. * EC_GROUP_precompute_mult has set up precomputation.
  231. */
  232. if (!EC_POINT_mul(group, P, n1, NULL, NULL, ctx))
  233. ABORT;
  234. if (!BN_one(n1))
  235. ABORT;
  236. /* n1 = 1 - order */
  237. if (!BN_sub(n1, n1, order))
  238. ABORT;
  239. if (!EC_POINT_mul(group, Q, NULL, P, n1, ctx))
  240. ABORT;
  241. if (0 != EC_POINT_cmp(group, Q, P, ctx))
  242. ABORT;
  243. /* n2 = 1 + order */
  244. if (!BN_add(n2, order, BN_value_one()))
  245. ABORT;
  246. if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  247. ABORT;
  248. if (0 != EC_POINT_cmp(group, Q, P, ctx))
  249. ABORT;
  250. /* n2 = (1 - order) * (1 + order) = 1 - order^2 */
  251. if (!BN_mul(n2, n1, n2, ctx))
  252. ABORT;
  253. if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  254. ABORT;
  255. if (0 != EC_POINT_cmp(group, Q, P, ctx))
  256. ABORT;
  257. /* n2 = order^2 - 1 */
  258. BN_set_negative(n2, 0);
  259. if (!EC_POINT_mul(group, Q, NULL, P, n2, ctx))
  260. ABORT;
  261. /* Add P to verify the result. */
  262. if (!EC_POINT_add(group, Q, Q, P, ctx))
  263. ABORT;
  264. if (!EC_POINT_is_at_infinity(group, Q))
  265. ABORT;
  266. /* Exercise EC_POINTs_mul, including corner cases. */
  267. if (EC_POINT_is_at_infinity(group, P))
  268. ABORT;
  269. scalars[0] = n1;
  270. points[0] = Q; /* => infinity */
  271. scalars[1] = n2;
  272. points[1] = P; /* => -P */
  273. scalars[2] = n1;
  274. points[2] = Q; /* => infinity */
  275. scalars[3] = n2;
  276. points[3] = Q; /* => infinity */
  277. scalars[4] = n1;
  278. points[4] = P; /* => P */
  279. scalars[5] = n2;
  280. points[5] = Q; /* => infinity */
  281. if (!EC_POINTs_mul(group, P, NULL, 6, points, scalars, ctx))
  282. ABORT;
  283. if (!EC_POINT_is_at_infinity(group, P))
  284. ABORT;
  285. }
  286. fprintf(stdout, "ok\n");
  287. EC_POINT_free(P);
  288. EC_POINT_free(Q);
  289. BN_free(n1);
  290. BN_free(n2);
  291. BN_free(order);
  292. BN_CTX_free(ctx);
  293. }
  294. static void prime_field_tests(void)
  295. {
  296. BN_CTX *ctx = NULL;
  297. BIGNUM *p, *a, *b;
  298. EC_GROUP *group;
  299. EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, *P_256 =
  300. NULL, *P_384 = NULL, *P_521 = NULL;
  301. EC_POINT *P, *Q, *R;
  302. BIGNUM *x, *y, *z;
  303. unsigned char buf[100];
  304. size_t i, len;
  305. int k;
  306. # if 1 /* optional */
  307. ctx = BN_CTX_new();
  308. if (!ctx)
  309. ABORT;
  310. # endif
  311. p = BN_new();
  312. a = BN_new();
  313. b = BN_new();
  314. if (!p || !a || !b)
  315. ABORT;
  316. if (!BN_hex2bn(&p, "17"))
  317. ABORT;
  318. if (!BN_hex2bn(&a, "1"))
  319. ABORT;
  320. if (!BN_hex2bn(&b, "1"))
  321. ABORT;
  322. group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use
  323. * EC_GROUP_new_curve_GFp so
  324. * that the library gets to
  325. * choose the EC_METHOD */
  326. if (!group)
  327. ABORT;
  328. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  329. ABORT;
  330. {
  331. EC_GROUP *tmp;
  332. tmp = EC_GROUP_new(EC_GROUP_method_of(group));
  333. if (!tmp)
  334. ABORT;
  335. if (!EC_GROUP_copy(tmp, group))
  336. ABORT;
  337. EC_GROUP_free(group);
  338. group = tmp;
  339. }
  340. if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx))
  341. ABORT;
  342. fprintf(stdout,
  343. "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x");
  344. BN_print_fp(stdout, p);
  345. fprintf(stdout, ")\n a = 0x");
  346. BN_print_fp(stdout, a);
  347. fprintf(stdout, "\n b = 0x");
  348. BN_print_fp(stdout, b);
  349. fprintf(stdout, "\n");
  350. P = EC_POINT_new(group);
  351. Q = EC_POINT_new(group);
  352. R = EC_POINT_new(group);
  353. if (!P || !Q || !R)
  354. ABORT;
  355. if (!EC_POINT_set_to_infinity(group, P))
  356. ABORT;
  357. if (!EC_POINT_is_at_infinity(group, P))
  358. ABORT;
  359. buf[0] = 0;
  360. if (!EC_POINT_oct2point(group, Q, buf, 1, ctx))
  361. ABORT;
  362. if (!EC_POINT_add(group, P, P, Q, ctx))
  363. ABORT;
  364. if (!EC_POINT_is_at_infinity(group, P))
  365. ABORT;
  366. x = BN_new();
  367. y = BN_new();
  368. z = BN_new();
  369. if (!x || !y || !z)
  370. ABORT;
  371. if (!BN_hex2bn(&x, "D"))
  372. ABORT;
  373. if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx))
  374. ABORT;
  375. if (EC_POINT_is_on_curve(group, Q, ctx) <= 0) {
  376. if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx))
  377. ABORT;
  378. fprintf(stderr, "Point is not on curve: x = 0x");
  379. BN_print_fp(stderr, x);
  380. fprintf(stderr, ", y = 0x");
  381. BN_print_fp(stderr, y);
  382. fprintf(stderr, "\n");
  383. ABORT;
  384. }
  385. fprintf(stdout, "A cyclic subgroup:\n");
  386. k = 100;
  387. do {
  388. if (k-- == 0)
  389. ABORT;
  390. if (EC_POINT_is_at_infinity(group, P))
  391. fprintf(stdout, " point at infinity\n");
  392. else {
  393. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
  394. ABORT;
  395. fprintf(stdout, " x = 0x");
  396. BN_print_fp(stdout, x);
  397. fprintf(stdout, ", y = 0x");
  398. BN_print_fp(stdout, y);
  399. fprintf(stdout, "\n");
  400. }
  401. if (!EC_POINT_copy(R, P))
  402. ABORT;
  403. if (!EC_POINT_add(group, P, P, Q, ctx))
  404. ABORT;
  405. # if 0 /* optional */
  406. {
  407. EC_POINT *points[3];
  408. points[0] = R;
  409. points[1] = Q;
  410. points[2] = P;
  411. if (!EC_POINTs_make_affine(group, 2, points, ctx))
  412. ABORT;
  413. }
  414. # endif
  415. }
  416. while (!EC_POINT_is_at_infinity(group, P));
  417. if (!EC_POINT_add(group, P, Q, R, ctx))
  418. ABORT;
  419. if (!EC_POINT_is_at_infinity(group, P))
  420. ABORT;
  421. len =
  422. EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,
  423. sizeof(buf), ctx);
  424. if (len == 0)
  425. ABORT;
  426. if (!EC_POINT_oct2point(group, P, buf, len, ctx))
  427. ABORT;
  428. if (0 != EC_POINT_cmp(group, P, Q, ctx))
  429. ABORT;
  430. fprintf(stdout, "Generator as octet string, compressed form:\n ");
  431. for (i = 0; i < len; i++)
  432. fprintf(stdout, "%02X", buf[i]);
  433. len =
  434. EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf,
  435. sizeof(buf), ctx);
  436. if (len == 0)
  437. ABORT;
  438. if (!EC_POINT_oct2point(group, P, buf, len, ctx))
  439. ABORT;
  440. if (0 != EC_POINT_cmp(group, P, Q, ctx))
  441. ABORT;
  442. fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
  443. for (i = 0; i < len; i++)
  444. fprintf(stdout, "%02X", buf[i]);
  445. len =
  446. EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof(buf),
  447. ctx);
  448. if (len == 0)
  449. ABORT;
  450. if (!EC_POINT_oct2point(group, P, buf, len, ctx))
  451. ABORT;
  452. if (0 != EC_POINT_cmp(group, P, Q, ctx))
  453. ABORT;
  454. fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
  455. for (i = 0; i < len; i++)
  456. fprintf(stdout, "%02X", buf[i]);
  457. if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx))
  458. ABORT;
  459. fprintf(stdout,
  460. "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x");
  461. BN_print_fp(stdout, x);
  462. fprintf(stdout, ", Y = 0x");
  463. BN_print_fp(stdout, y);
  464. fprintf(stdout, ", Z = 0x");
  465. BN_print_fp(stdout, z);
  466. fprintf(stdout, "\n");
  467. if (!EC_POINT_invert(group, P, ctx))
  468. ABORT;
  469. if (0 != EC_POINT_cmp(group, P, R, ctx))
  470. ABORT;
  471. /*
  472. * Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2,
  473. * 2000) -- not a NIST curve, but commonly used
  474. */
  475. if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF"))
  476. ABORT;
  477. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  478. ABORT;
  479. if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC"))
  480. ABORT;
  481. if (!BN_hex2bn(&b, "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45"))
  482. ABORT;
  483. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  484. ABORT;
  485. if (!BN_hex2bn(&x, "4A96B5688EF573284664698968C38BB913CBFC82"))
  486. ABORT;
  487. if (!BN_hex2bn(&y, "23a628553168947d59dcc912042351377ac5fb32"))
  488. ABORT;
  489. if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx))
  490. ABORT;
  491. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  492. ABORT;
  493. if (!BN_hex2bn(&z, "0100000000000000000001F4C8F927AED3CA752257"))
  494. ABORT;
  495. if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
  496. ABORT;
  497. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
  498. ABORT;
  499. fprintf(stdout, "\nSEC2 curve secp160r1 -- Generator:\n x = 0x");
  500. BN_print_fp(stdout, x);
  501. fprintf(stdout, "\n y = 0x");
  502. BN_print_fp(stdout, y);
  503. fprintf(stdout, "\n");
  504. /* G_y value taken from the standard: */
  505. if (!BN_hex2bn(&z, "23a628553168947d59dcc912042351377ac5fb32"))
  506. ABORT;
  507. if (0 != BN_cmp(y, z))
  508. ABORT;
  509. fprintf(stdout, "verify degree ...");
  510. if (EC_GROUP_get_degree(group) != 160)
  511. ABORT;
  512. fprintf(stdout, " ok\n");
  513. group_order_tests(group);
  514. if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group))))
  515. ABORT;
  516. if (!EC_GROUP_copy(P_160, group))
  517. ABORT;
  518. /* Curve P-192 (FIPS PUB 186-2, App. 6) */
  519. if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF"))
  520. ABORT;
  521. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  522. ABORT;
  523. if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC"))
  524. ABORT;
  525. if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1"))
  526. ABORT;
  527. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  528. ABORT;
  529. if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012"))
  530. ABORT;
  531. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))
  532. ABORT;
  533. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  534. ABORT;
  535. if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831"))
  536. ABORT;
  537. if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
  538. ABORT;
  539. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
  540. ABORT;
  541. fprintf(stdout, "\nNIST curve P-192 -- Generator:\n x = 0x");
  542. BN_print_fp(stdout, x);
  543. fprintf(stdout, "\n y = 0x");
  544. BN_print_fp(stdout, y);
  545. fprintf(stdout, "\n");
  546. /* G_y value taken from the standard: */
  547. if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811"))
  548. ABORT;
  549. if (0 != BN_cmp(y, z))
  550. ABORT;
  551. fprintf(stdout, "verify degree ...");
  552. if (EC_GROUP_get_degree(group) != 192)
  553. ABORT;
  554. fprintf(stdout, " ok\n");
  555. group_order_tests(group);
  556. if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group))))
  557. ABORT;
  558. if (!EC_GROUP_copy(P_192, group))
  559. ABORT;
  560. /* Curve P-224 (FIPS PUB 186-2, App. 6) */
  561. if (!BN_hex2bn
  562. (&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001"))
  563. ABORT;
  564. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  565. ABORT;
  566. if (!BN_hex2bn
  567. (&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE"))
  568. ABORT;
  569. if (!BN_hex2bn
  570. (&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4"))
  571. ABORT;
  572. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  573. ABORT;
  574. if (!BN_hex2bn
  575. (&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21"))
  576. ABORT;
  577. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx))
  578. ABORT;
  579. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  580. ABORT;
  581. if (!BN_hex2bn
  582. (&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D"))
  583. ABORT;
  584. if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
  585. ABORT;
  586. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
  587. ABORT;
  588. fprintf(stdout, "\nNIST curve P-224 -- Generator:\n x = 0x");
  589. BN_print_fp(stdout, x);
  590. fprintf(stdout, "\n y = 0x");
  591. BN_print_fp(stdout, y);
  592. fprintf(stdout, "\n");
  593. /* G_y value taken from the standard: */
  594. if (!BN_hex2bn
  595. (&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34"))
  596. ABORT;
  597. if (0 != BN_cmp(y, z))
  598. ABORT;
  599. fprintf(stdout, "verify degree ...");
  600. if (EC_GROUP_get_degree(group) != 224)
  601. ABORT;
  602. fprintf(stdout, " ok\n");
  603. group_order_tests(group);
  604. if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group))))
  605. ABORT;
  606. if (!EC_GROUP_copy(P_224, group))
  607. ABORT;
  608. /* Curve P-256 (FIPS PUB 186-2, App. 6) */
  609. if (!BN_hex2bn
  610. (&p,
  611. "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF"))
  612. ABORT;
  613. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  614. ABORT;
  615. if (!BN_hex2bn
  616. (&a,
  617. "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC"))
  618. ABORT;
  619. if (!BN_hex2bn
  620. (&b,
  621. "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B"))
  622. ABORT;
  623. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  624. ABORT;
  625. if (!BN_hex2bn
  626. (&x,
  627. "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296"))
  628. ABORT;
  629. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))
  630. ABORT;
  631. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  632. ABORT;
  633. if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
  634. "84F3B9CAC2FC632551"))
  635. ABORT;
  636. if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
  637. ABORT;
  638. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
  639. ABORT;
  640. fprintf(stdout, "\nNIST curve P-256 -- Generator:\n x = 0x");
  641. BN_print_fp(stdout, x);
  642. fprintf(stdout, "\n y = 0x");
  643. BN_print_fp(stdout, y);
  644. fprintf(stdout, "\n");
  645. /* G_y value taken from the standard: */
  646. if (!BN_hex2bn
  647. (&z,
  648. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5"))
  649. ABORT;
  650. if (0 != BN_cmp(y, z))
  651. ABORT;
  652. fprintf(stdout, "verify degree ...");
  653. if (EC_GROUP_get_degree(group) != 256)
  654. ABORT;
  655. fprintf(stdout, " ok\n");
  656. group_order_tests(group);
  657. if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group))))
  658. ABORT;
  659. if (!EC_GROUP_copy(P_256, group))
  660. ABORT;
  661. /* Curve P-384 (FIPS PUB 186-2, App. 6) */
  662. if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  663. "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF"))
  664. ABORT;
  665. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  666. ABORT;
  667. if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  668. "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC"))
  669. ABORT;
  670. if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
  671. "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF"))
  672. ABORT;
  673. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  674. ABORT;
  675. if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
  676. "9859F741E082542A385502F25DBF55296C3A545E3872760AB7"))
  677. ABORT;
  678. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx))
  679. ABORT;
  680. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  681. ABORT;
  682. if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  683. "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973"))
  684. ABORT;
  685. if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
  686. ABORT;
  687. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
  688. ABORT;
  689. fprintf(stdout, "\nNIST curve P-384 -- Generator:\n x = 0x");
  690. BN_print_fp(stdout, x);
  691. fprintf(stdout, "\n y = 0x");
  692. BN_print_fp(stdout, y);
  693. fprintf(stdout, "\n");
  694. /* G_y value taken from the standard: */
  695. if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
  696. "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F"))
  697. ABORT;
  698. if (0 != BN_cmp(y, z))
  699. ABORT;
  700. fprintf(stdout, "verify degree ...");
  701. if (EC_GROUP_get_degree(group) != 384)
  702. ABORT;
  703. fprintf(stdout, " ok\n");
  704. group_order_tests(group);
  705. if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group))))
  706. ABORT;
  707. if (!EC_GROUP_copy(P_384, group))
  708. ABORT;
  709. /* Curve P-521 (FIPS PUB 186-2, App. 6) */
  710. if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  711. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  712. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF"))
  713. ABORT;
  714. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  715. ABORT;
  716. if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  717. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  718. "FFFFFFFFFFFFFFFFFFFFFFFFFFFC"))
  719. ABORT;
  720. if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
  721. "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
  722. "DF883D2C34F1EF451FD46B503F00"))
  723. ABORT;
  724. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx))
  725. ABORT;
  726. if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
  727. "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
  728. "3C1856A429BF97E7E31C2E5BD66"))
  729. ABORT;
  730. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx))
  731. ABORT;
  732. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  733. ABORT;
  734. if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  735. "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
  736. "C9B8899C47AEBB6FB71E91386409"))
  737. ABORT;
  738. if (!EC_GROUP_set_generator(group, P, z, BN_value_one()))
  739. ABORT;
  740. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx))
  741. ABORT;
  742. fprintf(stdout, "\nNIST curve P-521 -- Generator:\n x = 0x");
  743. BN_print_fp(stdout, x);
  744. fprintf(stdout, "\n y = 0x");
  745. BN_print_fp(stdout, y);
  746. fprintf(stdout, "\n");
  747. /* G_y value taken from the standard: */
  748. if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
  749. "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
  750. "7086A272C24088BE94769FD16650"))
  751. ABORT;
  752. if (0 != BN_cmp(y, z))
  753. ABORT;
  754. fprintf(stdout, "verify degree ...");
  755. if (EC_GROUP_get_degree(group) != 521)
  756. ABORT;
  757. fprintf(stdout, " ok\n");
  758. group_order_tests(group);
  759. if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group))))
  760. ABORT;
  761. if (!EC_GROUP_copy(P_521, group))
  762. ABORT;
  763. /* more tests using the last curve */
  764. if (!EC_POINT_copy(Q, P))
  765. ABORT;
  766. if (EC_POINT_is_at_infinity(group, Q))
  767. ABORT;
  768. if (!EC_POINT_dbl(group, P, P, ctx))
  769. ABORT;
  770. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  771. ABORT;
  772. if (!EC_POINT_invert(group, Q, ctx))
  773. ABORT; /* P = -2Q */
  774. if (!EC_POINT_add(group, R, P, Q, ctx))
  775. ABORT;
  776. if (!EC_POINT_add(group, R, R, Q, ctx))
  777. ABORT;
  778. if (!EC_POINT_is_at_infinity(group, R))
  779. ABORT; /* R = P + 2Q */
  780. {
  781. const EC_POINT *points[4];
  782. const BIGNUM *scalars[4];
  783. BIGNUM scalar3;
  784. if (EC_POINT_is_at_infinity(group, Q))
  785. ABORT;
  786. points[0] = Q;
  787. points[1] = Q;
  788. points[2] = Q;
  789. points[3] = Q;
  790. if (!EC_GROUP_get_order(group, z, ctx))
  791. ABORT;
  792. if (!BN_add(y, z, BN_value_one()))
  793. ABORT;
  794. if (BN_is_odd(y))
  795. ABORT;
  796. if (!BN_rshift1(y, y))
  797. ABORT;
  798. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  799. scalars[1] = y;
  800. fprintf(stdout, "combined multiplication ...");
  801. fflush(stdout);
  802. /* z is still the group order */
  803. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  804. ABORT;
  805. if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
  806. ABORT;
  807. if (0 != EC_POINT_cmp(group, P, R, ctx))
  808. ABORT;
  809. if (0 != EC_POINT_cmp(group, R, Q, ctx))
  810. ABORT;
  811. fprintf(stdout, ".");
  812. fflush(stdout);
  813. if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0))
  814. ABORT;
  815. if (!BN_add(z, z, y))
  816. ABORT;
  817. BN_set_negative(z, 1);
  818. scalars[0] = y;
  819. scalars[1] = z; /* z = -(order + y) */
  820. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  821. ABORT;
  822. if (!EC_POINT_is_at_infinity(group, P))
  823. ABORT;
  824. fprintf(stdout, ".");
  825. fflush(stdout);
  826. if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0))
  827. ABORT;
  828. if (!BN_add(z, x, y))
  829. ABORT;
  830. BN_set_negative(z, 1);
  831. scalars[0] = x;
  832. scalars[1] = y;
  833. scalars[2] = z; /* z = -(x+y) */
  834. BN_init(&scalar3);
  835. BN_zero(&scalar3);
  836. scalars[3] = &scalar3;
  837. if (!EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx))
  838. ABORT;
  839. if (!EC_POINT_is_at_infinity(group, P))
  840. ABORT;
  841. fprintf(stdout, " ok\n\n");
  842. BN_free(&scalar3);
  843. }
  844. # if 0
  845. timings(P_160, TIMING_BASE_PT, ctx);
  846. timings(P_160, TIMING_RAND_PT, ctx);
  847. timings(P_160, TIMING_SIMUL, ctx);
  848. timings(P_192, TIMING_BASE_PT, ctx);
  849. timings(P_192, TIMING_RAND_PT, ctx);
  850. timings(P_192, TIMING_SIMUL, ctx);
  851. timings(P_224, TIMING_BASE_PT, ctx);
  852. timings(P_224, TIMING_RAND_PT, ctx);
  853. timings(P_224, TIMING_SIMUL, ctx);
  854. timings(P_256, TIMING_BASE_PT, ctx);
  855. timings(P_256, TIMING_RAND_PT, ctx);
  856. timings(P_256, TIMING_SIMUL, ctx);
  857. timings(P_384, TIMING_BASE_PT, ctx);
  858. timings(P_384, TIMING_RAND_PT, ctx);
  859. timings(P_384, TIMING_SIMUL, ctx);
  860. timings(P_521, TIMING_BASE_PT, ctx);
  861. timings(P_521, TIMING_RAND_PT, ctx);
  862. timings(P_521, TIMING_SIMUL, ctx);
  863. # endif
  864. if (ctx)
  865. BN_CTX_free(ctx);
  866. BN_free(p);
  867. BN_free(a);
  868. BN_free(b);
  869. EC_GROUP_free(group);
  870. EC_POINT_free(P);
  871. EC_POINT_free(Q);
  872. EC_POINT_free(R);
  873. BN_free(x);
  874. BN_free(y);
  875. BN_free(z);
  876. if (P_160)
  877. EC_GROUP_free(P_160);
  878. if (P_192)
  879. EC_GROUP_free(P_192);
  880. if (P_224)
  881. EC_GROUP_free(P_224);
  882. if (P_256)
  883. EC_GROUP_free(P_256);
  884. if (P_384)
  885. EC_GROUP_free(P_384);
  886. if (P_521)
  887. EC_GROUP_free(P_521);
  888. }
  889. /* Change test based on whether binary point compression is enabled or not. */
  890. # ifdef OPENSSL_EC_BIN_PT_COMP
  891. # define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  892. if (!BN_hex2bn(&x, _x)) ABORT; \
  893. if (!EC_POINT_set_compressed_coordinates_GF2m(group, P, x, _y_bit, ctx)) ABORT; \
  894. if (EC_POINT_is_on_curve(group, P, ctx) <= 0) ABORT; \
  895. if (!BN_hex2bn(&z, _order)) ABORT; \
  896. if (!BN_hex2bn(&cof, _cof)) ABORT; \
  897. if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
  898. if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
  899. fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
  900. BN_print_fp(stdout, x); \
  901. fprintf(stdout, "\n y = 0x"); \
  902. BN_print_fp(stdout, y); \
  903. fprintf(stdout, "\n"); \
  904. /* G_y value taken from the standard: */ \
  905. if (!BN_hex2bn(&z, _y)) ABORT; \
  906. if (0 != BN_cmp(y, z)) ABORT;
  907. # else
  908. # define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  909. if (!BN_hex2bn(&x, _x)) ABORT; \
  910. if (!BN_hex2bn(&y, _y)) ABORT; \
  911. if (!EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
  912. if (EC_POINT_is_on_curve(group, P, ctx) <= 0) ABORT; \
  913. if (!BN_hex2bn(&z, _order)) ABORT; \
  914. if (!BN_hex2bn(&cof, _cof)) ABORT; \
  915. if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
  916. fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
  917. BN_print_fp(stdout, x); \
  918. fprintf(stdout, "\n y = 0x"); \
  919. BN_print_fp(stdout, y); \
  920. fprintf(stdout, "\n");
  921. # endif
  922. # define CHAR2_CURVE_TEST(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  923. if (!BN_hex2bn(&p, _p)) ABORT; \
  924. if (!BN_hex2bn(&a, _a)) ABORT; \
  925. if (!BN_hex2bn(&b, _b)) ABORT; \
  926. if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT; \
  927. CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  928. fprintf(stdout, "verify degree ..."); \
  929. if (EC_GROUP_get_degree(group) != _degree) ABORT; \
  930. fprintf(stdout, " ok\n"); \
  931. group_order_tests(group); \
  932. if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \
  933. if (!EC_GROUP_copy(_variable, group)) ABORT; \
  934. # ifndef OPENSSL_NO_EC2M
  935. static void char2_field_tests(void)
  936. {
  937. BN_CTX *ctx = NULL;
  938. BIGNUM *p, *a, *b;
  939. EC_GROUP *group;
  940. EC_GROUP *C2_K163 = NULL, *C2_K233 = NULL, *C2_K283 = NULL, *C2_K409 =
  941. NULL, *C2_K571 = NULL;
  942. EC_GROUP *C2_B163 = NULL, *C2_B233 = NULL, *C2_B283 = NULL, *C2_B409 =
  943. NULL, *C2_B571 = NULL;
  944. EC_POINT *P, *Q, *R;
  945. BIGNUM *x, *y, *z, *cof;
  946. unsigned char buf[100];
  947. size_t i, len;
  948. int k;
  949. # if 1 /* optional */
  950. ctx = BN_CTX_new();
  951. if (!ctx)
  952. ABORT;
  953. # endif
  954. p = BN_new();
  955. a = BN_new();
  956. b = BN_new();
  957. if (!p || !a || !b)
  958. ABORT;
  959. if (!BN_hex2bn(&p, "13"))
  960. ABORT;
  961. if (!BN_hex2bn(&a, "3"))
  962. ABORT;
  963. if (!BN_hex2bn(&b, "1"))
  964. ABORT;
  965. group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use
  966. * EC_GROUP_new_curve_GF2m
  967. * so that the library gets
  968. * to choose the EC_METHOD */
  969. if (!group)
  970. ABORT;
  971. if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx))
  972. ABORT;
  973. {
  974. EC_GROUP *tmp;
  975. tmp = EC_GROUP_new(EC_GROUP_method_of(group));
  976. if (!tmp)
  977. ABORT;
  978. if (!EC_GROUP_copy(tmp, group))
  979. ABORT;
  980. EC_GROUP_free(group);
  981. group = tmp;
  982. }
  983. if (!EC_GROUP_get_curve_GF2m(group, p, a, b, ctx))
  984. ABORT;
  985. fprintf(stdout,
  986. "Curve defined by Weierstrass equation\n y^2 + x*y = x^3 + a*x^2 + b (mod 0x");
  987. BN_print_fp(stdout, p);
  988. fprintf(stdout, ")\n a = 0x");
  989. BN_print_fp(stdout, a);
  990. fprintf(stdout, "\n b = 0x");
  991. BN_print_fp(stdout, b);
  992. fprintf(stdout, "\n(0x... means binary polynomial)\n");
  993. P = EC_POINT_new(group);
  994. Q = EC_POINT_new(group);
  995. R = EC_POINT_new(group);
  996. if (!P || !Q || !R)
  997. ABORT;
  998. if (!EC_POINT_set_to_infinity(group, P))
  999. ABORT;
  1000. if (!EC_POINT_is_at_infinity(group, P))
  1001. ABORT;
  1002. buf[0] = 0;
  1003. if (!EC_POINT_oct2point(group, Q, buf, 1, ctx))
  1004. ABORT;
  1005. if (!EC_POINT_add(group, P, P, Q, ctx))
  1006. ABORT;
  1007. if (!EC_POINT_is_at_infinity(group, P))
  1008. ABORT;
  1009. x = BN_new();
  1010. y = BN_new();
  1011. z = BN_new();
  1012. cof = BN_new();
  1013. if (!x || !y || !z || !cof)
  1014. ABORT;
  1015. if (!BN_hex2bn(&x, "6"))
  1016. ABORT;
  1017. /* Change test based on whether binary point compression is enabled or not. */
  1018. # ifdef OPENSSL_EC_BIN_PT_COMP
  1019. if (!EC_POINT_set_compressed_coordinates_GF2m(group, Q, x, 1, ctx))
  1020. ABORT;
  1021. # else
  1022. if (!BN_hex2bn(&y, "8"))
  1023. ABORT;
  1024. if (!EC_POINT_set_affine_coordinates_GF2m(group, Q, x, y, ctx))
  1025. ABORT;
  1026. # endif
  1027. if (EC_POINT_is_on_curve(group, Q, ctx) <= 0) {
  1028. /* Change test based on whether binary point compression is enabled or not. */
  1029. # ifdef OPENSSL_EC_BIN_PT_COMP
  1030. if (!EC_POINT_get_affine_coordinates_GF2m(group, Q, x, y, ctx))
  1031. ABORT;
  1032. # endif
  1033. fprintf(stderr, "Point is not on curve: x = 0x");
  1034. BN_print_fp(stderr, x);
  1035. fprintf(stderr, ", y = 0x");
  1036. BN_print_fp(stderr, y);
  1037. fprintf(stderr, "\n");
  1038. ABORT;
  1039. }
  1040. fprintf(stdout, "A cyclic subgroup:\n");
  1041. k = 100;
  1042. do {
  1043. if (k-- == 0)
  1044. ABORT;
  1045. if (EC_POINT_is_at_infinity(group, P))
  1046. fprintf(stdout, " point at infinity\n");
  1047. else {
  1048. if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx))
  1049. ABORT;
  1050. fprintf(stdout, " x = 0x");
  1051. BN_print_fp(stdout, x);
  1052. fprintf(stdout, ", y = 0x");
  1053. BN_print_fp(stdout, y);
  1054. fprintf(stdout, "\n");
  1055. }
  1056. if (!EC_POINT_copy(R, P))
  1057. ABORT;
  1058. if (!EC_POINT_add(group, P, P, Q, ctx))
  1059. ABORT;
  1060. }
  1061. while (!EC_POINT_is_at_infinity(group, P));
  1062. if (!EC_POINT_add(group, P, Q, R, ctx))
  1063. ABORT;
  1064. if (!EC_POINT_is_at_infinity(group, P))
  1065. ABORT;
  1066. /* Change test based on whether binary point compression is enabled or not. */
  1067. # ifdef OPENSSL_EC_BIN_PT_COMP
  1068. len =
  1069. EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf,
  1070. sizeof(buf), ctx);
  1071. if (len == 0)
  1072. ABORT;
  1073. if (!EC_POINT_oct2point(group, P, buf, len, ctx))
  1074. ABORT;
  1075. if (0 != EC_POINT_cmp(group, P, Q, ctx))
  1076. ABORT;
  1077. fprintf(stdout, "Generator as octet string, compressed form:\n ");
  1078. for (i = 0; i < len; i++)
  1079. fprintf(stdout, "%02X", buf[i]);
  1080. # endif
  1081. len =
  1082. EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf,
  1083. sizeof(buf), ctx);
  1084. if (len == 0)
  1085. ABORT;
  1086. if (!EC_POINT_oct2point(group, P, buf, len, ctx))
  1087. ABORT;
  1088. if (0 != EC_POINT_cmp(group, P, Q, ctx))
  1089. ABORT;
  1090. fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
  1091. for (i = 0; i < len; i++)
  1092. fprintf(stdout, "%02X", buf[i]);
  1093. /* Change test based on whether binary point compression is enabled or not. */
  1094. # ifdef OPENSSL_EC_BIN_PT_COMP
  1095. len =
  1096. EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof(buf),
  1097. ctx);
  1098. if (len == 0)
  1099. ABORT;
  1100. if (!EC_POINT_oct2point(group, P, buf, len, ctx))
  1101. ABORT;
  1102. if (0 != EC_POINT_cmp(group, P, Q, ctx))
  1103. ABORT;
  1104. fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
  1105. for (i = 0; i < len; i++)
  1106. fprintf(stdout, "%02X", buf[i]);
  1107. # endif
  1108. fprintf(stdout, "\n");
  1109. if (!EC_POINT_invert(group, P, ctx))
  1110. ABORT;
  1111. if (0 != EC_POINT_cmp(group, P, R, ctx))
  1112. ABORT;
  1113. /* Curve K-163 (FIPS PUB 186-2, App. 6) */
  1114. CHAR2_CURVE_TEST
  1115. ("NIST curve K-163",
  1116. "0800000000000000000000000000000000000000C9",
  1117. "1",
  1118. "1",
  1119. "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
  1120. "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
  1121. 1, "04000000000000000000020108A2E0CC0D99F8A5EF", "2", 163, C2_K163);
  1122. /* Curve B-163 (FIPS PUB 186-2, App. 6) */
  1123. CHAR2_CURVE_TEST
  1124. ("NIST curve B-163",
  1125. "0800000000000000000000000000000000000000C9",
  1126. "1",
  1127. "020A601907B8C953CA1481EB10512F78744A3205FD",
  1128. "03F0EBA16286A2D57EA0991168D4994637E8343E36",
  1129. "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
  1130. 1, "040000000000000000000292FE77E70C12A4234C33", "2", 163, C2_B163);
  1131. /* Curve K-233 (FIPS PUB 186-2, App. 6) */
  1132. CHAR2_CURVE_TEST
  1133. ("NIST curve K-233",
  1134. "020000000000000000000000000000000000000004000000000000000001",
  1135. "0",
  1136. "1",
  1137. "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
  1138. "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
  1139. 0,
  1140. "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
  1141. "4", 233, C2_K233);
  1142. /* Curve B-233 (FIPS PUB 186-2, App. 6) */
  1143. CHAR2_CURVE_TEST
  1144. ("NIST curve B-233",
  1145. "020000000000000000000000000000000000000004000000000000000001",
  1146. "000000000000000000000000000000000000000000000000000000000001",
  1147. "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
  1148. "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
  1149. "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
  1150. 1,
  1151. "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
  1152. "2", 233, C2_B233);
  1153. /* Curve K-283 (FIPS PUB 186-2, App. 6) */
  1154. CHAR2_CURVE_TEST
  1155. ("NIST curve K-283",
  1156. "0800000000000000000000000000000000000000000000000000000000000000000010A1",
  1157. "0",
  1158. "1",
  1159. "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
  1160. "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
  1161. 0,
  1162. "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
  1163. "4", 283, C2_K283);
  1164. /* Curve B-283 (FIPS PUB 186-2, App. 6) */
  1165. CHAR2_CURVE_TEST
  1166. ("NIST curve B-283",
  1167. "0800000000000000000000000000000000000000000000000000000000000000000010A1",
  1168. "000000000000000000000000000000000000000000000000000000000000000000000001",
  1169. "027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
  1170. "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
  1171. "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
  1172. 1,
  1173. "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
  1174. "2", 283, C2_B283);
  1175. /* Curve K-409 (FIPS PUB 186-2, App. 6) */
  1176. CHAR2_CURVE_TEST
  1177. ("NIST curve K-409",
  1178. "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
  1179. "0",
  1180. "1",
  1181. "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
  1182. "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
  1183. 1,
  1184. "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
  1185. "4", 409, C2_K409);
  1186. /* Curve B-409 (FIPS PUB 186-2, App. 6) */
  1187. CHAR2_CURVE_TEST
  1188. ("NIST curve B-409",
  1189. "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
  1190. "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
  1191. "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
  1192. "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
  1193. "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
  1194. 1,
  1195. "010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
  1196. "2", 409, C2_B409);
  1197. /* Curve K-571 (FIPS PUB 186-2, App. 6) */
  1198. CHAR2_CURVE_TEST
  1199. ("NIST curve K-571",
  1200. "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
  1201. "0",
  1202. "1",
  1203. "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
  1204. "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
  1205. 0,
  1206. "020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
  1207. "4", 571, C2_K571);
  1208. /* Curve B-571 (FIPS PUB 186-2, App. 6) */
  1209. CHAR2_CURVE_TEST
  1210. ("NIST curve B-571",
  1211. "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
  1212. "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
  1213. "02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
  1214. "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
  1215. "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
  1216. 1,
  1217. "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
  1218. "2", 571, C2_B571);
  1219. /* more tests using the last curve */
  1220. if (!EC_POINT_copy(Q, P))
  1221. ABORT;
  1222. if (EC_POINT_is_at_infinity(group, Q))
  1223. ABORT;
  1224. if (!EC_POINT_dbl(group, P, P, ctx))
  1225. ABORT;
  1226. if (EC_POINT_is_on_curve(group, P, ctx) <= 0)
  1227. ABORT;
  1228. if (!EC_POINT_invert(group, Q, ctx))
  1229. ABORT; /* P = -2Q */
  1230. if (!EC_POINT_add(group, R, P, Q, ctx))
  1231. ABORT;
  1232. if (!EC_POINT_add(group, R, R, Q, ctx))
  1233. ABORT;
  1234. if (!EC_POINT_is_at_infinity(group, R))
  1235. ABORT; /* R = P + 2Q */
  1236. {
  1237. const EC_POINT *points[3];
  1238. const BIGNUM *scalars[3];
  1239. if (EC_POINT_is_at_infinity(group, Q))
  1240. ABORT;
  1241. points[0] = Q;
  1242. points[1] = Q;
  1243. points[2] = Q;
  1244. if (!BN_add(y, z, BN_value_one()))
  1245. ABORT;
  1246. if (BN_is_odd(y))
  1247. ABORT;
  1248. if (!BN_rshift1(y, y))
  1249. ABORT;
  1250. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  1251. scalars[1] = y;
  1252. fprintf(stdout, "combined multiplication ...");
  1253. fflush(stdout);
  1254. /* z is still the group order */
  1255. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  1256. ABORT;
  1257. if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx))
  1258. ABORT;
  1259. if (0 != EC_POINT_cmp(group, P, R, ctx))
  1260. ABORT;
  1261. if (0 != EC_POINT_cmp(group, R, Q, ctx))
  1262. ABORT;
  1263. fprintf(stdout, ".");
  1264. fflush(stdout);
  1265. if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0))
  1266. ABORT;
  1267. if (!BN_add(z, z, y))
  1268. ABORT;
  1269. BN_set_negative(z, 1);
  1270. scalars[0] = y;
  1271. scalars[1] = z; /* z = -(order + y) */
  1272. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx))
  1273. ABORT;
  1274. if (!EC_POINT_is_at_infinity(group, P))
  1275. ABORT;
  1276. fprintf(stdout, ".");
  1277. fflush(stdout);
  1278. if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0))
  1279. ABORT;
  1280. if (!BN_add(z, x, y))
  1281. ABORT;
  1282. BN_set_negative(z, 1);
  1283. scalars[0] = x;
  1284. scalars[1] = y;
  1285. scalars[2] = z; /* z = -(x+y) */
  1286. if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx))
  1287. ABORT;
  1288. if (!EC_POINT_is_at_infinity(group, P))
  1289. ABORT;
  1290. fprintf(stdout, " ok\n\n");
  1291. }
  1292. # if 0
  1293. timings(C2_K163, TIMING_BASE_PT, ctx);
  1294. timings(C2_K163, TIMING_RAND_PT, ctx);
  1295. timings(C2_K163, TIMING_SIMUL, ctx);
  1296. timings(C2_B163, TIMING_BASE_PT, ctx);
  1297. timings(C2_B163, TIMING_RAND_PT, ctx);
  1298. timings(C2_B163, TIMING_SIMUL, ctx);
  1299. timings(C2_K233, TIMING_BASE_PT, ctx);
  1300. timings(C2_K233, TIMING_RAND_PT, ctx);
  1301. timings(C2_K233, TIMING_SIMUL, ctx);
  1302. timings(C2_B233, TIMING_BASE_PT, ctx);
  1303. timings(C2_B233, TIMING_RAND_PT, ctx);
  1304. timings(C2_B233, TIMING_SIMUL, ctx);
  1305. timings(C2_K283, TIMING_BASE_PT, ctx);
  1306. timings(C2_K283, TIMING_RAND_PT, ctx);
  1307. timings(C2_K283, TIMING_SIMUL, ctx);
  1308. timings(C2_B283, TIMING_BASE_PT, ctx);
  1309. timings(C2_B283, TIMING_RAND_PT, ctx);
  1310. timings(C2_B283, TIMING_SIMUL, ctx);
  1311. timings(C2_K409, TIMING_BASE_PT, ctx);
  1312. timings(C2_K409, TIMING_RAND_PT, ctx);
  1313. timings(C2_K409, TIMING_SIMUL, ctx);
  1314. timings(C2_B409, TIMING_BASE_PT, ctx);
  1315. timings(C2_B409, TIMING_RAND_PT, ctx);
  1316. timings(C2_B409, TIMING_SIMUL, ctx);
  1317. timings(C2_K571, TIMING_BASE_PT, ctx);
  1318. timings(C2_K571, TIMING_RAND_PT, ctx);
  1319. timings(C2_K571, TIMING_SIMUL, ctx);
  1320. timings(C2_B571, TIMING_BASE_PT, ctx);
  1321. timings(C2_B571, TIMING_RAND_PT, ctx);
  1322. timings(C2_B571, TIMING_SIMUL, ctx);
  1323. # endif
  1324. if (ctx)
  1325. BN_CTX_free(ctx);
  1326. BN_free(p);
  1327. BN_free(a);
  1328. BN_free(b);
  1329. EC_GROUP_free(group);
  1330. EC_POINT_free(P);
  1331. EC_POINT_free(Q);
  1332. EC_POINT_free(R);
  1333. BN_free(x);
  1334. BN_free(y);
  1335. BN_free(z);
  1336. BN_free(cof);
  1337. if (C2_K163)
  1338. EC_GROUP_free(C2_K163);
  1339. if (C2_B163)
  1340. EC_GROUP_free(C2_B163);
  1341. if (C2_K233)
  1342. EC_GROUP_free(C2_K233);
  1343. if (C2_B233)
  1344. EC_GROUP_free(C2_B233);
  1345. if (C2_K283)
  1346. EC_GROUP_free(C2_K283);
  1347. if (C2_B283)
  1348. EC_GROUP_free(C2_B283);
  1349. if (C2_K409)
  1350. EC_GROUP_free(C2_K409);
  1351. if (C2_B409)
  1352. EC_GROUP_free(C2_B409);
  1353. if (C2_K571)
  1354. EC_GROUP_free(C2_K571);
  1355. if (C2_B571)
  1356. EC_GROUP_free(C2_B571);
  1357. }
  1358. # endif
  1359. static void internal_curve_test(void)
  1360. {
  1361. EC_builtin_curve *curves = NULL;
  1362. size_t crv_len = 0, n = 0;
  1363. int ok = 1;
  1364. crv_len = EC_get_builtin_curves(NULL, 0);
  1365. curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
  1366. if (curves == NULL)
  1367. return;
  1368. if (!EC_get_builtin_curves(curves, crv_len)) {
  1369. OPENSSL_free(curves);
  1370. return;
  1371. }
  1372. fprintf(stdout, "testing internal curves: ");
  1373. for (n = 0; n < crv_len; n++) {
  1374. EC_GROUP *group = NULL;
  1375. int nid = curves[n].nid;
  1376. if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL) {
  1377. ok = 0;
  1378. fprintf(stdout, "\nEC_GROUP_new_curve_name() failed with"
  1379. " curve %s\n", OBJ_nid2sn(nid));
  1380. /* try next curve */
  1381. continue;
  1382. }
  1383. if (!EC_GROUP_check(group, NULL)) {
  1384. ok = 0;
  1385. fprintf(stdout, "\nEC_GROUP_check() failed with"
  1386. " curve %s\n", OBJ_nid2sn(nid));
  1387. EC_GROUP_free(group);
  1388. /* try the next curve */
  1389. continue;
  1390. }
  1391. fprintf(stdout, ".");
  1392. fflush(stdout);
  1393. EC_GROUP_free(group);
  1394. }
  1395. if (ok)
  1396. fprintf(stdout, " ok\n\n");
  1397. else {
  1398. fprintf(stdout, " failed\n\n");
  1399. ABORT;
  1400. }
  1401. OPENSSL_free(curves);
  1402. return;
  1403. }
  1404. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1405. /*
  1406. * nistp_test_params contains magic numbers for testing our optimized
  1407. * implementations of several NIST curves with characteristic > 3.
  1408. */
  1409. struct nistp_test_params {
  1410. const EC_METHOD *(*meth) ();
  1411. int degree;
  1412. /*
  1413. * Qx, Qy and D are taken from
  1414. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
  1415. * Otherwise, values are standard curve parameters from FIPS 180-3
  1416. */
  1417. const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d;
  1418. };
  1419. static const struct nistp_test_params nistp_tests_params[] = {
  1420. {
  1421. /* P-224 */
  1422. EC_GFp_nistp224_method,
  1423. 224,
  1424. /* p */
  1425. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001",
  1426. /* a */
  1427. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE",
  1428. /* b */
  1429. "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4",
  1430. /* Qx */
  1431. "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E",
  1432. /* Qy */
  1433. "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555",
  1434. /* Gx */
  1435. "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21",
  1436. /* Gy */
  1437. "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34",
  1438. /* order */
  1439. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D",
  1440. /* d */
  1441. "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8",
  1442. },
  1443. {
  1444. /* P-256 */
  1445. EC_GFp_nistp256_method,
  1446. 256,
  1447. /* p */
  1448. "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
  1449. /* a */
  1450. "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc",
  1451. /* b */
  1452. "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b",
  1453. /* Qx */
  1454. "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19",
  1455. /* Qy */
  1456. "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09",
  1457. /* Gx */
  1458. "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296",
  1459. /* Gy */
  1460. "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5",
  1461. /* order */
  1462. "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
  1463. /* d */
  1464. "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96",
  1465. },
  1466. {
  1467. /* P-521 */
  1468. EC_GFp_nistp521_method,
  1469. 521,
  1470. /* p */
  1471. "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff",
  1472. /* a */
  1473. "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc",
  1474. /* b */
  1475. "051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00",
  1476. /* Qx */
  1477. "0098e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4",
  1478. /* Qy */
  1479. "0164350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e",
  1480. /* Gx */
  1481. "c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66",
  1482. /* Gy */
  1483. "11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650",
  1484. /* order */
  1485. "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409",
  1486. /* d */
  1487. "0100085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eeedf09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722",
  1488. },
  1489. };
  1490. static void nistp_single_test(const struct nistp_test_params *test)
  1491. {
  1492. BN_CTX *ctx;
  1493. BIGNUM *p, *a, *b, *x, *y, *n, *m, *order;
  1494. EC_GROUP *NISTP;
  1495. EC_POINT *G, *P, *Q, *Q_CHECK;
  1496. fprintf(stdout, "\nNIST curve P-%d (optimised implementation):\n",
  1497. test->degree);
  1498. ctx = BN_CTX_new();
  1499. p = BN_new();
  1500. a = BN_new();
  1501. b = BN_new();
  1502. x = BN_new();
  1503. y = BN_new();
  1504. m = BN_new();
  1505. n = BN_new();
  1506. order = BN_new();
  1507. NISTP = EC_GROUP_new(test->meth());
  1508. if (!NISTP)
  1509. ABORT;
  1510. if (!BN_hex2bn(&p, test->p))
  1511. ABORT;
  1512. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL))
  1513. ABORT;
  1514. if (!BN_hex2bn(&a, test->a))
  1515. ABORT;
  1516. if (!BN_hex2bn(&b, test->b))
  1517. ABORT;
  1518. if (!EC_GROUP_set_curve_GFp(NISTP, p, a, b, ctx))
  1519. ABORT;
  1520. G = EC_POINT_new(NISTP);
  1521. P = EC_POINT_new(NISTP);
  1522. Q = EC_POINT_new(NISTP);
  1523. Q_CHECK = EC_POINT_new(NISTP);
  1524. if (!BN_hex2bn(&x, test->Qx))
  1525. ABORT;
  1526. if (!BN_hex2bn(&y, test->Qy))
  1527. ABORT;
  1528. if (!EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x, y, ctx))
  1529. ABORT;
  1530. if (!BN_hex2bn(&x, test->Gx))
  1531. ABORT;
  1532. if (!BN_hex2bn(&y, test->Gy))
  1533. ABORT;
  1534. if (!EC_POINT_set_affine_coordinates_GFp(NISTP, G, x, y, ctx))
  1535. ABORT;
  1536. if (!BN_hex2bn(&order, test->order))
  1537. ABORT;
  1538. if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))
  1539. ABORT;
  1540. fprintf(stdout, "verify degree ... ");
  1541. if (EC_GROUP_get_degree(NISTP) != test->degree)
  1542. ABORT;
  1543. fprintf(stdout, "ok\n");
  1544. fprintf(stdout, "NIST test vectors ... ");
  1545. if (!BN_hex2bn(&n, test->d))
  1546. ABORT;
  1547. /* fixed point multiplication */
  1548. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1549. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1550. ABORT;
  1551. /* random point multiplication */
  1552. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1553. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1554. ABORT;
  1555. /* set generator to P = 2*G, where G is the standard generator */
  1556. if (!EC_POINT_dbl(NISTP, P, G, ctx))
  1557. ABORT;
  1558. if (!EC_GROUP_set_generator(NISTP, P, order, BN_value_one()))
  1559. ABORT;
  1560. /* set the scalar to m=n/2, where n is the NIST test scalar */
  1561. if (!BN_rshift(m, n, 1))
  1562. ABORT;
  1563. /* test the non-standard generator */
  1564. /* fixed point multiplication */
  1565. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1566. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1567. ABORT;
  1568. /* random point multiplication */
  1569. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1570. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1571. ABORT;
  1572. /*
  1573. * We have not performed precomputation so have_precompute mult should be
  1574. * false
  1575. */
  1576. if (EC_GROUP_have_precompute_mult(NISTP))
  1577. ABORT;
  1578. /* now repeat all tests with precomputation */
  1579. if (!EC_GROUP_precompute_mult(NISTP, ctx))
  1580. ABORT;
  1581. if (!EC_GROUP_have_precompute_mult(NISTP))
  1582. ABORT;
  1583. /* fixed point multiplication */
  1584. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1585. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1586. ABORT;
  1587. /* random point multiplication */
  1588. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1589. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1590. ABORT;
  1591. /* reset generator */
  1592. if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one()))
  1593. ABORT;
  1594. /* fixed point multiplication */
  1595. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1596. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1597. ABORT;
  1598. /* random point multiplication */
  1599. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1600. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx))
  1601. ABORT;
  1602. fprintf(stdout, "ok\n");
  1603. group_order_tests(NISTP);
  1604. # if 0
  1605. timings(NISTP, TIMING_BASE_PT, ctx);
  1606. timings(NISTP, TIMING_RAND_PT, ctx);
  1607. # endif
  1608. EC_GROUP_free(NISTP);
  1609. EC_POINT_free(G);
  1610. EC_POINT_free(P);
  1611. EC_POINT_free(Q);
  1612. EC_POINT_free(Q_CHECK);
  1613. BN_free(n);
  1614. BN_free(m);
  1615. BN_free(p);
  1616. BN_free(a);
  1617. BN_free(b);
  1618. BN_free(x);
  1619. BN_free(y);
  1620. BN_free(order);
  1621. BN_CTX_free(ctx);
  1622. }
  1623. static void nistp_tests()
  1624. {
  1625. unsigned i;
  1626. for (i = 0;
  1627. i < sizeof(nistp_tests_params) / sizeof(struct nistp_test_params);
  1628. i++) {
  1629. nistp_single_test(&nistp_tests_params[i]);
  1630. }
  1631. }
  1632. # endif
  1633. static const char rnd_seed[] =
  1634. "string to make the random number generator think it has entropy";
  1635. int main(int argc, char *argv[])
  1636. {
  1637. /* enable memory leak checking unless explicitly disabled */
  1638. if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL)
  1639. && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))) {
  1640. CRYPTO_malloc_debug_init();
  1641. CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
  1642. } else {
  1643. /* OPENSSL_DEBUG_MEMORY=off */
  1644. CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
  1645. }
  1646. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  1647. ERR_load_crypto_strings();
  1648. RAND_seed(rnd_seed, sizeof(rnd_seed)); /* or BN_generate_prime may fail */
  1649. prime_field_tests();
  1650. puts("");
  1651. # ifndef OPENSSL_NO_EC2M
  1652. char2_field_tests();
  1653. # endif
  1654. # ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1655. nistp_tests();
  1656. # endif
  1657. /* test the internal curves */
  1658. internal_curve_test();
  1659. # ifndef OPENSSL_NO_ENGINE
  1660. ENGINE_cleanup();
  1661. # endif
  1662. CRYPTO_cleanup_all_ex_data();
  1663. ERR_free_strings();
  1664. ERR_remove_thread_state(NULL);
  1665. CRYPTO_mem_leaks_fp(stderr);
  1666. return 0;
  1667. }
  1668. #endif