ectest.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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[]) { puts("Elliptic curves are disabled."); return 0; }
  82. #else
  83. #include <openssl/ec.h>
  84. #ifndef OPENSSL_NO_ENGINE
  85. #include <openssl/engine.h>
  86. #endif
  87. #include <openssl/err.h>
  88. #include <openssl/obj_mac.h>
  89. #include <openssl/objects.h>
  90. #include <openssl/rand.h>
  91. #include <openssl/bn.h>
  92. #include <openssl/opensslconf.h>
  93. #if defined(_MSC_VER) && defined(_MIPS_) && (_MSC_VER/100==12)
  94. /* suppress "too big too optimize" warning */
  95. #pragma warning(disable:4959)
  96. #endif
  97. #define ABORT do { \
  98. fflush(stdout); \
  99. fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
  100. ERR_print_errors_fp(stderr); \
  101. EXIT(1); \
  102. } while (0)
  103. #define TIMING_BASE_PT 0
  104. #define TIMING_RAND_PT 1
  105. #define TIMING_SIMUL 2
  106. #if 0
  107. static void timings(EC_GROUP *group, int type, BN_CTX *ctx)
  108. {
  109. clock_t clck;
  110. int i, j;
  111. BIGNUM *s;
  112. BIGNUM *r[10], *r0[10];
  113. EC_POINT *P;
  114. s = BN_new();
  115. if (s == NULL) ABORT;
  116. fprintf(stdout, "Timings for %d-bit field, ", EC_GROUP_get_degree(group));
  117. if (!EC_GROUP_get_order(group, s, ctx)) ABORT;
  118. fprintf(stdout, "%d-bit scalars ", (int)BN_num_bits(s));
  119. fflush(stdout);
  120. P = EC_POINT_new(group);
  121. if (P == NULL) ABORT;
  122. EC_POINT_copy(P, EC_GROUP_get0_generator(group));
  123. for (i = 0; i < 10; i++)
  124. {
  125. if ((r[i] = BN_new()) == NULL) ABORT;
  126. if (!BN_pseudo_rand(r[i], BN_num_bits(s), 0, 0)) ABORT;
  127. if (type != TIMING_BASE_PT)
  128. {
  129. if ((r0[i] = BN_new()) == NULL) ABORT;
  130. if (!BN_pseudo_rand(r0[i], BN_num_bits(s), 0, 0)) ABORT;
  131. }
  132. }
  133. clck = clock();
  134. for (i = 0; i < 10; i++)
  135. {
  136. for (j = 0; j < 10; j++)
  137. {
  138. if (!EC_POINT_mul(group, P, (type != TIMING_RAND_PT) ? r[i] : NULL,
  139. (type != TIMING_BASE_PT) ? P : NULL, (type != TIMING_BASE_PT) ? r0[i] : NULL, ctx)) ABORT;
  140. }
  141. }
  142. clck = clock() - clck;
  143. fprintf(stdout, "\n");
  144. #ifdef CLOCKS_PER_SEC
  145. /* "To determine the time in seconds, the value returned
  146. * by the clock function should be divided by the value
  147. * of the macro CLOCKS_PER_SEC."
  148. * -- ISO/IEC 9899 */
  149. # define UNIT "s"
  150. #else
  151. /* "`CLOCKS_PER_SEC' undeclared (first use this function)"
  152. * -- cc on NeXTstep/OpenStep */
  153. # define UNIT "units"
  154. # define CLOCKS_PER_SEC 1
  155. #endif
  156. if (type == TIMING_BASE_PT) {
  157. fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
  158. "base point multiplications", (double)clck/CLOCKS_PER_SEC);
  159. } else if (type == TIMING_RAND_PT) {
  160. fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
  161. "random point multiplications", (double)clck/CLOCKS_PER_SEC);
  162. } else if (type == TIMING_SIMUL) {
  163. fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j,
  164. "s*P+t*Q operations", (double)clck/CLOCKS_PER_SEC);
  165. }
  166. fprintf(stdout, "average: %.4f " UNIT "\n", (double)clck/(CLOCKS_PER_SEC*i*j));
  167. EC_POINT_free(P);
  168. BN_free(s);
  169. for (i = 0; i < 10; i++)
  170. {
  171. BN_free(r[i]);
  172. if (type != TIMING_BASE_PT) BN_free(r0[i]);
  173. }
  174. }
  175. #endif
  176. /* test multiplication with group order, long and negative scalars */
  177. static void group_order_tests(EC_GROUP *group)
  178. {
  179. BIGNUM *n1, *n2, *order;
  180. EC_POINT *P = EC_POINT_new(group);
  181. EC_POINT *Q = EC_POINT_new(group);
  182. BN_CTX *ctx = BN_CTX_new();
  183. n1 = BN_new(); n2 = BN_new(); order = BN_new();
  184. fprintf(stdout, "verify group order ...");
  185. fflush(stdout);
  186. if (!EC_GROUP_get_order(group, order, ctx)) ABORT;
  187. if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) ABORT;
  188. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  189. fprintf(stdout, ".");
  190. fflush(stdout);
  191. if (!EC_GROUP_precompute_mult(group, ctx)) ABORT;
  192. if (!EC_POINT_mul(group, Q, order, NULL, NULL, ctx)) ABORT;
  193. if (!EC_POINT_is_at_infinity(group, Q)) ABORT;
  194. fprintf(stdout, " ok\n");
  195. fprintf(stdout, "long/negative scalar tests ... ");
  196. if (!BN_one(n1)) ABORT;
  197. /* n1 = 1 - order */
  198. if (!BN_sub(n1, n1, order)) ABORT;
  199. if(!EC_POINT_mul(group, Q, NULL, P, n1, ctx)) ABORT;
  200. if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
  201. /* n2 = 1 + order */
  202. if (!BN_add(n2, order, BN_value_one())) ABORT;
  203. if(!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT;
  204. if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
  205. /* n2 = (1 - order) * (1 + order) */
  206. if (!BN_mul(n2, n1, n2, ctx)) ABORT;
  207. if(!EC_POINT_mul(group, Q, NULL, P, n2, ctx)) ABORT;
  208. if (0 != EC_POINT_cmp(group, Q, P, ctx)) ABORT;
  209. fprintf(stdout, "ok\n");
  210. EC_POINT_free(P);
  211. EC_POINT_free(Q);
  212. BN_free(n1);
  213. BN_free(n2);
  214. BN_free(order);
  215. BN_CTX_free(ctx);
  216. }
  217. static void prime_field_tests(void)
  218. {
  219. BN_CTX *ctx = NULL;
  220. BIGNUM *p, *a, *b;
  221. EC_GROUP *group;
  222. EC_GROUP *P_160 = NULL, *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL;
  223. EC_POINT *P, *Q, *R;
  224. BIGNUM *x, *y, *z;
  225. unsigned char buf[100];
  226. size_t i, len;
  227. int k;
  228. #if 1 /* optional */
  229. ctx = BN_CTX_new();
  230. if (!ctx) ABORT;
  231. #endif
  232. p = BN_new();
  233. a = BN_new();
  234. b = BN_new();
  235. if (!p || !a || !b) ABORT;
  236. if (!BN_hex2bn(&p, "17")) ABORT;
  237. if (!BN_hex2bn(&a, "1")) ABORT;
  238. if (!BN_hex2bn(&b, "1")) ABORT;
  239. group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp
  240. * so that the library gets to choose the EC_METHOD */
  241. if (!group) ABORT;
  242. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  243. {
  244. EC_GROUP *tmp;
  245. tmp = EC_GROUP_new(EC_GROUP_method_of(group));
  246. if (!tmp) ABORT;
  247. if (!EC_GROUP_copy(tmp, group)) ABORT;
  248. EC_GROUP_free(group);
  249. group = tmp;
  250. }
  251. if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT;
  252. fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x");
  253. BN_print_fp(stdout, p);
  254. fprintf(stdout, ")\n a = 0x");
  255. BN_print_fp(stdout, a);
  256. fprintf(stdout, "\n b = 0x");
  257. BN_print_fp(stdout, b);
  258. fprintf(stdout, "\n");
  259. P = EC_POINT_new(group);
  260. Q = EC_POINT_new(group);
  261. R = EC_POINT_new(group);
  262. if (!P || !Q || !R) ABORT;
  263. if (!EC_POINT_set_to_infinity(group, P)) ABORT;
  264. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  265. buf[0] = 0;
  266. if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
  267. if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
  268. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  269. x = BN_new();
  270. y = BN_new();
  271. z = BN_new();
  272. if (!x || !y || !z) ABORT;
  273. if (!BN_hex2bn(&x, "D")) ABORT;
  274. if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
  275. if (!EC_POINT_is_on_curve(group, Q, ctx))
  276. {
  277. if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT;
  278. fprintf(stderr, "Point is not on curve: x = 0x");
  279. BN_print_fp(stderr, x);
  280. fprintf(stderr, ", y = 0x");
  281. BN_print_fp(stderr, y);
  282. fprintf(stderr, "\n");
  283. ABORT;
  284. }
  285. fprintf(stdout, "A cyclic subgroup:\n");
  286. k = 100;
  287. do
  288. {
  289. if (k-- == 0) ABORT;
  290. if (EC_POINT_is_at_infinity(group, P))
  291. fprintf(stdout, " point at infinity\n");
  292. else
  293. {
  294. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  295. fprintf(stdout, " x = 0x");
  296. BN_print_fp(stdout, x);
  297. fprintf(stdout, ", y = 0x");
  298. BN_print_fp(stdout, y);
  299. fprintf(stdout, "\n");
  300. }
  301. if (!EC_POINT_copy(R, P)) ABORT;
  302. if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
  303. #if 0 /* optional */
  304. {
  305. EC_POINT *points[3];
  306. points[0] = R;
  307. points[1] = Q;
  308. points[2] = P;
  309. if (!EC_POINTs_make_affine(group, 2, points, ctx)) ABORT;
  310. }
  311. #endif
  312. }
  313. while (!EC_POINT_is_at_infinity(group, P));
  314. if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
  315. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  316. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
  317. if (len == 0) ABORT;
  318. if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
  319. if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
  320. fprintf(stdout, "Generator as octet string, compressed form:\n ");
  321. for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
  322. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
  323. if (len == 0) ABORT;
  324. if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
  325. if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
  326. fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
  327. for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
  328. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
  329. if (len == 0) ABORT;
  330. if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
  331. if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
  332. fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
  333. for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
  334. if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
  335. fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x");
  336. BN_print_fp(stdout, x);
  337. fprintf(stdout, ", Y = 0x");
  338. BN_print_fp(stdout, y);
  339. fprintf(stdout, ", Z = 0x");
  340. BN_print_fp(stdout, z);
  341. fprintf(stdout, "\n");
  342. if (!EC_POINT_invert(group, P, ctx)) ABORT;
  343. if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
  344. /* Curve secp160r1 (Certicom Research SEC 2 Version 1.0, section 2.4.2, 2000)
  345. * -- not a NIST curve, but commonly used */
  346. if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF")) ABORT;
  347. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  348. if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC")) ABORT;
  349. if (!BN_hex2bn(&b, "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45")) ABORT;
  350. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  351. if (!BN_hex2bn(&x, "4A96B5688EF573284664698968C38BB913CBFC82")) ABORT;
  352. if (!BN_hex2bn(&y, "23a628553168947d59dcc912042351377ac5fb32")) ABORT;
  353. if (!EC_POINT_set_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  354. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  355. if (!BN_hex2bn(&z, "0100000000000000000001F4C8F927AED3CA752257")) ABORT;
  356. if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
  357. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  358. fprintf(stdout, "\nSEC2 curve secp160r1 -- Generator:\n x = 0x");
  359. BN_print_fp(stdout, x);
  360. fprintf(stdout, "\n y = 0x");
  361. BN_print_fp(stdout, y);
  362. fprintf(stdout, "\n");
  363. /* G_y value taken from the standard: */
  364. if (!BN_hex2bn(&z, "23a628553168947d59dcc912042351377ac5fb32")) ABORT;
  365. if (0 != BN_cmp(y, z)) ABORT;
  366. fprintf(stdout, "verify degree ...");
  367. if (EC_GROUP_get_degree(group) != 160) ABORT;
  368. fprintf(stdout, " ok\n");
  369. group_order_tests(group);
  370. if (!(P_160 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
  371. if (!EC_GROUP_copy(P_160, group)) ABORT;
  372. /* Curve P-192 (FIPS PUB 186-2, App. 6) */
  373. if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT;
  374. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  375. if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT;
  376. if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT;
  377. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  378. if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT;
  379. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
  380. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  381. if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT;
  382. if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
  383. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  384. fprintf(stdout, "\nNIST curve P-192 -- Generator:\n x = 0x");
  385. BN_print_fp(stdout, x);
  386. fprintf(stdout, "\n y = 0x");
  387. BN_print_fp(stdout, y);
  388. fprintf(stdout, "\n");
  389. /* G_y value taken from the standard: */
  390. if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT;
  391. if (0 != BN_cmp(y, z)) ABORT;
  392. fprintf(stdout, "verify degree ...");
  393. if (EC_GROUP_get_degree(group) != 192) ABORT;
  394. fprintf(stdout, " ok\n");
  395. group_order_tests(group);
  396. if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
  397. if (!EC_GROUP_copy(P_192, group)) ABORT;
  398. /* Curve P-224 (FIPS PUB 186-2, App. 6) */
  399. if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT;
  400. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  401. if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT;
  402. if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT;
  403. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  404. if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT;
  405. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
  406. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  407. if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT;
  408. if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
  409. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  410. fprintf(stdout, "\nNIST curve P-224 -- Generator:\n x = 0x");
  411. BN_print_fp(stdout, x);
  412. fprintf(stdout, "\n y = 0x");
  413. BN_print_fp(stdout, y);
  414. fprintf(stdout, "\n");
  415. /* G_y value taken from the standard: */
  416. if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT;
  417. if (0 != BN_cmp(y, z)) ABORT;
  418. fprintf(stdout, "verify degree ...");
  419. if (EC_GROUP_get_degree(group) != 224) ABORT;
  420. fprintf(stdout, " ok\n");
  421. group_order_tests(group);
  422. if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
  423. if (!EC_GROUP_copy(P_224, group)) ABORT;
  424. /* Curve P-256 (FIPS PUB 186-2, App. 6) */
  425. if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
  426. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  427. if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
  428. if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT;
  429. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  430. if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT;
  431. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
  432. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  433. if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E"
  434. "84F3B9CAC2FC632551")) ABORT;
  435. if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
  436. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  437. fprintf(stdout, "\nNIST curve P-256 -- Generator:\n x = 0x");
  438. BN_print_fp(stdout, x);
  439. fprintf(stdout, "\n y = 0x");
  440. BN_print_fp(stdout, y);
  441. fprintf(stdout, "\n");
  442. /* G_y value taken from the standard: */
  443. if (!BN_hex2bn(&z, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT;
  444. if (0 != BN_cmp(y, z)) ABORT;
  445. fprintf(stdout, "verify degree ...");
  446. if (EC_GROUP_get_degree(group) != 256) ABORT;
  447. fprintf(stdout, " ok\n");
  448. group_order_tests(group);
  449. if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
  450. if (!EC_GROUP_copy(P_256, group)) ABORT;
  451. /* Curve P-384 (FIPS PUB 186-2, App. 6) */
  452. if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  453. "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT;
  454. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  455. if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  456. "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT;
  457. if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141"
  458. "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT;
  459. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  460. if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B"
  461. "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT;
  462. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT;
  463. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  464. if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  465. "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT;
  466. if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
  467. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  468. fprintf(stdout, "\nNIST curve P-384 -- Generator:\n x = 0x");
  469. BN_print_fp(stdout, x);
  470. fprintf(stdout, "\n y = 0x");
  471. BN_print_fp(stdout, y);
  472. fprintf(stdout, "\n");
  473. /* G_y value taken from the standard: */
  474. if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14"
  475. "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT;
  476. if (0 != BN_cmp(y, z)) ABORT;
  477. fprintf(stdout, "verify degree ...");
  478. if (EC_GROUP_get_degree(group) != 384) ABORT;
  479. fprintf(stdout, " ok\n");
  480. group_order_tests(group);
  481. if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
  482. if (!EC_GROUP_copy(P_384, group)) ABORT;
  483. /* Curve P-521 (FIPS PUB 186-2, App. 6) */
  484. if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  485. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  486. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT;
  487. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  488. if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  489. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  490. "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT;
  491. if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B"
  492. "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573"
  493. "DF883D2C34F1EF451FD46B503F00")) ABORT;
  494. if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
  495. if (!BN_hex2bn(&x, "C6858E06B70404E9CD9E3ECB662395B4429C648139053F"
  496. "B521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B"
  497. "3C1856A429BF97E7E31C2E5BD66")) ABORT;
  498. if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT;
  499. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  500. if (!BN_hex2bn(&z, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF"
  501. "FFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5"
  502. "C9B8899C47AEBB6FB71E91386409")) ABORT;
  503. if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT;
  504. if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
  505. fprintf(stdout, "\nNIST curve P-521 -- Generator:\n x = 0x");
  506. BN_print_fp(stdout, x);
  507. fprintf(stdout, "\n y = 0x");
  508. BN_print_fp(stdout, y);
  509. fprintf(stdout, "\n");
  510. /* G_y value taken from the standard: */
  511. if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579"
  512. "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C"
  513. "7086A272C24088BE94769FD16650")) ABORT;
  514. if (0 != BN_cmp(y, z)) ABORT;
  515. fprintf(stdout, "verify degree ...");
  516. if (EC_GROUP_get_degree(group) != 521) ABORT;
  517. fprintf(stdout, " ok\n");
  518. group_order_tests(group);
  519. if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT;
  520. if (!EC_GROUP_copy(P_521, group)) ABORT;
  521. /* more tests using the last curve */
  522. if (!EC_POINT_copy(Q, P)) ABORT;
  523. if (EC_POINT_is_at_infinity(group, Q)) ABORT;
  524. if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
  525. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  526. if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
  527. if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
  528. if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
  529. if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
  530. {
  531. const EC_POINT *points[4];
  532. const BIGNUM *scalars[4];
  533. BIGNUM scalar3;
  534. if (EC_POINT_is_at_infinity(group, Q)) ABORT;
  535. points[0] = Q;
  536. points[1] = Q;
  537. points[2] = Q;
  538. points[3] = Q;
  539. if (!EC_GROUP_get_order(group, z, ctx)) ABORT;
  540. if (!BN_add(y, z, BN_value_one())) ABORT;
  541. if (BN_is_odd(y)) ABORT;
  542. if (!BN_rshift1(y, y)) ABORT;
  543. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  544. scalars[1] = y;
  545. fprintf(stdout, "combined multiplication ...");
  546. fflush(stdout);
  547. /* z is still the group order */
  548. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
  549. if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
  550. if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
  551. if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
  552. fprintf(stdout, ".");
  553. fflush(stdout);
  554. if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
  555. if (!BN_add(z, z, y)) ABORT;
  556. BN_set_negative(z, 1);
  557. scalars[0] = y;
  558. scalars[1] = z; /* z = -(order + y) */
  559. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
  560. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  561. fprintf(stdout, ".");
  562. fflush(stdout);
  563. if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
  564. if (!BN_add(z, x, y)) ABORT;
  565. BN_set_negative(z, 1);
  566. scalars[0] = x;
  567. scalars[1] = y;
  568. scalars[2] = z; /* z = -(x+y) */
  569. BN_init(&scalar3);
  570. BN_zero(&scalar3);
  571. scalars[3] = &scalar3;
  572. if (!EC_POINTs_mul(group, P, NULL, 4, points, scalars, ctx)) ABORT;
  573. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  574. fprintf(stdout, " ok\n\n");
  575. BN_free(&scalar3);
  576. }
  577. #if 0
  578. timings(P_160, TIMING_BASE_PT, ctx);
  579. timings(P_160, TIMING_RAND_PT, ctx);
  580. timings(P_160, TIMING_SIMUL, ctx);
  581. timings(P_192, TIMING_BASE_PT, ctx);
  582. timings(P_192, TIMING_RAND_PT, ctx);
  583. timings(P_192, TIMING_SIMUL, ctx);
  584. timings(P_224, TIMING_BASE_PT, ctx);
  585. timings(P_224, TIMING_RAND_PT, ctx);
  586. timings(P_224, TIMING_SIMUL, ctx);
  587. timings(P_256, TIMING_BASE_PT, ctx);
  588. timings(P_256, TIMING_RAND_PT, ctx);
  589. timings(P_256, TIMING_SIMUL, ctx);
  590. timings(P_384, TIMING_BASE_PT, ctx);
  591. timings(P_384, TIMING_RAND_PT, ctx);
  592. timings(P_384, TIMING_SIMUL, ctx);
  593. timings(P_521, TIMING_BASE_PT, ctx);
  594. timings(P_521, TIMING_RAND_PT, ctx);
  595. timings(P_521, TIMING_SIMUL, ctx);
  596. #endif
  597. if (ctx)
  598. BN_CTX_free(ctx);
  599. BN_free(p); BN_free(a); BN_free(b);
  600. EC_GROUP_free(group);
  601. EC_POINT_free(P);
  602. EC_POINT_free(Q);
  603. EC_POINT_free(R);
  604. BN_free(x); BN_free(y); BN_free(z);
  605. if (P_160) EC_GROUP_free(P_160);
  606. if (P_192) EC_GROUP_free(P_192);
  607. if (P_224) EC_GROUP_free(P_224);
  608. if (P_256) EC_GROUP_free(P_256);
  609. if (P_384) EC_GROUP_free(P_384);
  610. if (P_521) EC_GROUP_free(P_521);
  611. }
  612. /* Change test based on whether binary point compression is enabled or not. */
  613. #ifdef OPENSSL_EC_BIN_PT_COMP
  614. #define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  615. if (!BN_hex2bn(&x, _x)) ABORT; \
  616. if (!EC_POINT_set_compressed_coordinates_GF2m(group, P, x, _y_bit, ctx)) ABORT; \
  617. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
  618. if (!BN_hex2bn(&z, _order)) ABORT; \
  619. if (!BN_hex2bn(&cof, _cof)) ABORT; \
  620. if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
  621. if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
  622. fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
  623. BN_print_fp(stdout, x); \
  624. fprintf(stdout, "\n y = 0x"); \
  625. BN_print_fp(stdout, y); \
  626. fprintf(stdout, "\n"); \
  627. /* G_y value taken from the standard: */ \
  628. if (!BN_hex2bn(&z, _y)) ABORT; \
  629. if (0 != BN_cmp(y, z)) ABORT;
  630. #else
  631. #define CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  632. if (!BN_hex2bn(&x, _x)) ABORT; \
  633. if (!BN_hex2bn(&y, _y)) ABORT; \
  634. if (!EC_POINT_set_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT; \
  635. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; \
  636. if (!BN_hex2bn(&z, _order)) ABORT; \
  637. if (!BN_hex2bn(&cof, _cof)) ABORT; \
  638. if (!EC_GROUP_set_generator(group, P, z, cof)) ABORT; \
  639. fprintf(stdout, "\n%s -- Generator:\n x = 0x", _name); \
  640. BN_print_fp(stdout, x); \
  641. fprintf(stdout, "\n y = 0x"); \
  642. BN_print_fp(stdout, y); \
  643. fprintf(stdout, "\n");
  644. #endif
  645. #define CHAR2_CURVE_TEST(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  646. if (!BN_hex2bn(&p, _p)) ABORT; \
  647. if (!BN_hex2bn(&a, _a)) ABORT; \
  648. if (!BN_hex2bn(&b, _b)) ABORT; \
  649. if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT; \
  650. CHAR2_CURVE_TEST_INTERNAL(_name, _p, _a, _b, _x, _y, _y_bit, _order, _cof, _degree, _variable) \
  651. fprintf(stdout, "verify degree ..."); \
  652. if (EC_GROUP_get_degree(group) != _degree) ABORT; \
  653. fprintf(stdout, " ok\n"); \
  654. group_order_tests(group); \
  655. if (!(_variable = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; \
  656. if (!EC_GROUP_copy(_variable, group)) ABORT; \
  657. #ifndef OPENSSL_NO_EC2M
  658. static void char2_field_tests(void)
  659. {
  660. BN_CTX *ctx = NULL;
  661. BIGNUM *p, *a, *b;
  662. EC_GROUP *group;
  663. EC_GROUP *C2_K163 = NULL, *C2_K233 = NULL, *C2_K283 = NULL, *C2_K409 = NULL, *C2_K571 = NULL;
  664. EC_GROUP *C2_B163 = NULL, *C2_B233 = NULL, *C2_B283 = NULL, *C2_B409 = NULL, *C2_B571 = NULL;
  665. EC_POINT *P, *Q, *R;
  666. BIGNUM *x, *y, *z, *cof;
  667. unsigned char buf[100];
  668. size_t i, len;
  669. int k;
  670. #if 1 /* optional */
  671. ctx = BN_CTX_new();
  672. if (!ctx) ABORT;
  673. #endif
  674. p = BN_new();
  675. a = BN_new();
  676. b = BN_new();
  677. if (!p || !a || !b) ABORT;
  678. if (!BN_hex2bn(&p, "13")) ABORT;
  679. if (!BN_hex2bn(&a, "3")) ABORT;
  680. if (!BN_hex2bn(&b, "1")) ABORT;
  681. group = EC_GROUP_new(EC_GF2m_simple_method()); /* applications should use EC_GROUP_new_curve_GF2m
  682. * so that the library gets to choose the EC_METHOD */
  683. if (!group) ABORT;
  684. if (!EC_GROUP_set_curve_GF2m(group, p, a, b, ctx)) ABORT;
  685. {
  686. EC_GROUP *tmp;
  687. tmp = EC_GROUP_new(EC_GROUP_method_of(group));
  688. if (!tmp) ABORT;
  689. if (!EC_GROUP_copy(tmp, group)) ABORT;
  690. EC_GROUP_free(group);
  691. group = tmp;
  692. }
  693. if (!EC_GROUP_get_curve_GF2m(group, p, a, b, ctx)) ABORT;
  694. fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 + x*y = x^3 + a*x^2 + b (mod 0x");
  695. BN_print_fp(stdout, p);
  696. fprintf(stdout, ")\n a = 0x");
  697. BN_print_fp(stdout, a);
  698. fprintf(stdout, "\n b = 0x");
  699. BN_print_fp(stdout, b);
  700. fprintf(stdout, "\n(0x... means binary polynomial)\n");
  701. P = EC_POINT_new(group);
  702. Q = EC_POINT_new(group);
  703. R = EC_POINT_new(group);
  704. if (!P || !Q || !R) ABORT;
  705. if (!EC_POINT_set_to_infinity(group, P)) ABORT;
  706. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  707. buf[0] = 0;
  708. if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
  709. if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
  710. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  711. x = BN_new();
  712. y = BN_new();
  713. z = BN_new();
  714. cof = BN_new();
  715. if (!x || !y || !z || !cof) ABORT;
  716. if (!BN_hex2bn(&x, "6")) ABORT;
  717. /* Change test based on whether binary point compression is enabled or not. */
  718. #ifdef OPENSSL_EC_BIN_PT_COMP
  719. if (!EC_POINT_set_compressed_coordinates_GF2m(group, Q, x, 1, ctx)) ABORT;
  720. #else
  721. if (!BN_hex2bn(&y, "8")) ABORT;
  722. if (!EC_POINT_set_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
  723. #endif
  724. if (!EC_POINT_is_on_curve(group, Q, ctx))
  725. {
  726. /* Change test based on whether binary point compression is enabled or not. */
  727. #ifdef OPENSSL_EC_BIN_PT_COMP
  728. if (!EC_POINT_get_affine_coordinates_GF2m(group, Q, x, y, ctx)) ABORT;
  729. #endif
  730. fprintf(stderr, "Point is not on curve: x = 0x");
  731. BN_print_fp(stderr, x);
  732. fprintf(stderr, ", y = 0x");
  733. BN_print_fp(stderr, y);
  734. fprintf(stderr, "\n");
  735. ABORT;
  736. }
  737. fprintf(stdout, "A cyclic subgroup:\n");
  738. k = 100;
  739. do
  740. {
  741. if (k-- == 0) ABORT;
  742. if (EC_POINT_is_at_infinity(group, P))
  743. fprintf(stdout, " point at infinity\n");
  744. else
  745. {
  746. if (!EC_POINT_get_affine_coordinates_GF2m(group, P, x, y, ctx)) ABORT;
  747. fprintf(stdout, " x = 0x");
  748. BN_print_fp(stdout, x);
  749. fprintf(stdout, ", y = 0x");
  750. BN_print_fp(stdout, y);
  751. fprintf(stdout, "\n");
  752. }
  753. if (!EC_POINT_copy(R, P)) ABORT;
  754. if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
  755. }
  756. while (!EC_POINT_is_at_infinity(group, P));
  757. if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
  758. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  759. /* Change test based on whether binary point compression is enabled or not. */
  760. #ifdef OPENSSL_EC_BIN_PT_COMP
  761. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
  762. if (len == 0) ABORT;
  763. if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
  764. if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
  765. fprintf(stdout, "Generator as octet string, compressed form:\n ");
  766. for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
  767. #endif
  768. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
  769. if (len == 0) ABORT;
  770. if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
  771. if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
  772. fprintf(stdout, "\nGenerator as octet string, uncompressed form:\n ");
  773. for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
  774. /* Change test based on whether binary point compression is enabled or not. */
  775. #ifdef OPENSSL_EC_BIN_PT_COMP
  776. len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
  777. if (len == 0) ABORT;
  778. if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
  779. if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
  780. fprintf(stdout, "\nGenerator as octet string, hybrid form:\n ");
  781. for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
  782. #endif
  783. fprintf(stdout, "\n");
  784. if (!EC_POINT_invert(group, P, ctx)) ABORT;
  785. if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
  786. /* Curve K-163 (FIPS PUB 186-2, App. 6) */
  787. CHAR2_CURVE_TEST
  788. (
  789. "NIST curve K-163",
  790. "0800000000000000000000000000000000000000C9",
  791. "1",
  792. "1",
  793. "02FE13C0537BBC11ACAA07D793DE4E6D5E5C94EEE8",
  794. "0289070FB05D38FF58321F2E800536D538CCDAA3D9",
  795. 1,
  796. "04000000000000000000020108A2E0CC0D99F8A5EF",
  797. "2",
  798. 163,
  799. C2_K163
  800. );
  801. /* Curve B-163 (FIPS PUB 186-2, App. 6) */
  802. CHAR2_CURVE_TEST
  803. (
  804. "NIST curve B-163",
  805. "0800000000000000000000000000000000000000C9",
  806. "1",
  807. "020A601907B8C953CA1481EB10512F78744A3205FD",
  808. "03F0EBA16286A2D57EA0991168D4994637E8343E36",
  809. "00D51FBC6C71A0094FA2CDD545B11C5C0C797324F1",
  810. 1,
  811. "040000000000000000000292FE77E70C12A4234C33",
  812. "2",
  813. 163,
  814. C2_B163
  815. );
  816. /* Curve K-233 (FIPS PUB 186-2, App. 6) */
  817. CHAR2_CURVE_TEST
  818. (
  819. "NIST curve K-233",
  820. "020000000000000000000000000000000000000004000000000000000001",
  821. "0",
  822. "1",
  823. "017232BA853A7E731AF129F22FF4149563A419C26BF50A4C9D6EEFAD6126",
  824. "01DB537DECE819B7F70F555A67C427A8CD9BF18AEB9B56E0C11056FAE6A3",
  825. 0,
  826. "008000000000000000000000000000069D5BB915BCD46EFB1AD5F173ABDF",
  827. "4",
  828. 233,
  829. C2_K233
  830. );
  831. /* Curve B-233 (FIPS PUB 186-2, App. 6) */
  832. CHAR2_CURVE_TEST
  833. (
  834. "NIST curve B-233",
  835. "020000000000000000000000000000000000000004000000000000000001",
  836. "000000000000000000000000000000000000000000000000000000000001",
  837. "0066647EDE6C332C7F8C0923BB58213B333B20E9CE4281FE115F7D8F90AD",
  838. "00FAC9DFCBAC8313BB2139F1BB755FEF65BC391F8B36F8F8EB7371FD558B",
  839. "01006A08A41903350678E58528BEBF8A0BEFF867A7CA36716F7E01F81052",
  840. 1,
  841. "01000000000000000000000000000013E974E72F8A6922031D2603CFE0D7",
  842. "2",
  843. 233,
  844. C2_B233
  845. );
  846. /* Curve K-283 (FIPS PUB 186-2, App. 6) */
  847. CHAR2_CURVE_TEST
  848. (
  849. "NIST curve K-283",
  850. "0800000000000000000000000000000000000000000000000000000000000000000010A1",
  851. "0",
  852. "1",
  853. "0503213F78CA44883F1A3B8162F188E553CD265F23C1567A16876913B0C2AC2458492836",
  854. "01CCDA380F1C9E318D90F95D07E5426FE87E45C0E8184698E45962364E34116177DD2259",
  855. 0,
  856. "01FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE9AE2ED07577265DFF7F94451E061E163C61",
  857. "4",
  858. 283,
  859. C2_K283
  860. );
  861. /* Curve B-283 (FIPS PUB 186-2, App. 6) */
  862. CHAR2_CURVE_TEST
  863. (
  864. "NIST curve B-283",
  865. "0800000000000000000000000000000000000000000000000000000000000000000010A1",
  866. "000000000000000000000000000000000000000000000000000000000000000000000001",
  867. "027B680AC8B8596DA5A4AF8A19A0303FCA97FD7645309FA2A581485AF6263E313B79A2F5",
  868. "05F939258DB7DD90E1934F8C70B0DFEC2EED25B8557EAC9C80E2E198F8CDBECD86B12053",
  869. "03676854FE24141CB98FE6D4B20D02B4516FF702350EDDB0826779C813F0DF45BE8112F4",
  870. 1,
  871. "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEF90399660FC938A90165B042A7CEFADB307",
  872. "2",
  873. 283,
  874. C2_B283
  875. );
  876. /* Curve K-409 (FIPS PUB 186-2, App. 6) */
  877. CHAR2_CURVE_TEST
  878. (
  879. "NIST curve K-409",
  880. "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
  881. "0",
  882. "1",
  883. "0060F05F658F49C1AD3AB1890F7184210EFD0987E307C84C27ACCFB8F9F67CC2C460189EB5AAAA62EE222EB1B35540CFE9023746",
  884. "01E369050B7C4E42ACBA1DACBF04299C3460782F918EA427E6325165E9EA10E3DA5F6C42E9C55215AA9CA27A5863EC48D8E0286B",
  885. 1,
  886. "007FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE5F83B2D4EA20400EC4557D5ED3E3E7CA5B4B5C83B8E01E5FCF",
  887. "4",
  888. 409,
  889. C2_K409
  890. );
  891. /* Curve B-409 (FIPS PUB 186-2, App. 6) */
  892. CHAR2_CURVE_TEST
  893. (
  894. "NIST curve B-409",
  895. "02000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000001",
  896. "00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
  897. "0021A5C2C8EE9FEB5C4B9A753B7B476B7FD6422EF1F3DD674761FA99D6AC27C8A9A197B272822F6CD57A55AA4F50AE317B13545F",
  898. "015D4860D088DDB3496B0C6064756260441CDE4AF1771D4DB01FFE5B34E59703DC255A868A1180515603AEAB60794E54BB7996A7",
  899. "0061B1CFAB6BE5F32BBFA78324ED106A7636B9C5A7BD198D0158AA4F5488D08F38514F1FDF4B4F40D2181B3681C364BA0273C706",
  900. 1,
  901. "010000000000000000000000000000000000000000000000000001E2AAD6A612F33307BE5FA47C3C9E052F838164CD37D9A21173",
  902. "2",
  903. 409,
  904. C2_B409
  905. );
  906. /* Curve K-571 (FIPS PUB 186-2, App. 6) */
  907. CHAR2_CURVE_TEST
  908. (
  909. "NIST curve K-571",
  910. "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
  911. "0",
  912. "1",
  913. "026EB7A859923FBC82189631F8103FE4AC9CA2970012D5D46024804801841CA44370958493B205E647DA304DB4CEB08CBBD1BA39494776FB988B47174DCA88C7E2945283A01C8972",
  914. "0349DC807F4FBF374F4AEADE3BCA95314DD58CEC9F307A54FFC61EFC006D8A2C9D4979C0AC44AEA74FBEBBB9F772AEDCB620B01A7BA7AF1B320430C8591984F601CD4C143EF1C7A3",
  915. 0,
  916. "020000000000000000000000000000000000000000000000000000000000000000000000131850E1F19A63E4B391A8DB917F4138B630D84BE5D639381E91DEB45CFE778F637C1001",
  917. "4",
  918. 571,
  919. C2_K571
  920. );
  921. /* Curve B-571 (FIPS PUB 186-2, App. 6) */
  922. CHAR2_CURVE_TEST
  923. (
  924. "NIST curve B-571",
  925. "80000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000425",
  926. "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
  927. "02F40E7E2221F295DE297117B7F3D62F5C6A97FFCB8CEFF1CD6BA8CE4A9A18AD84FFABBD8EFA59332BE7AD6756A66E294AFD185A78FF12AA520E4DE739BACA0C7FFEFF7F2955727A",
  928. "0303001D34B856296C16C0D40D3CD7750A93D1D2955FA80AA5F40FC8DB7B2ABDBDE53950F4C0D293CDD711A35B67FB1499AE60038614F1394ABFA3B4C850D927E1E7769C8EEC2D19",
  929. "037BF27342DA639B6DCCFFFEB73D69D78C6C27A6009CBBCA1980F8533921E8A684423E43BAB08A576291AF8F461BB2A8B3531D2F0485C19B16E2F1516E23DD3C1A4827AF1B8AC15B",
  930. 1,
  931. "03FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE661CE18FF55987308059B186823851EC7DD9CA1161DE93D5174D66E8382E9BB2FE84E47",
  932. "2",
  933. 571,
  934. C2_B571
  935. );
  936. /* more tests using the last curve */
  937. if (!EC_POINT_copy(Q, P)) ABORT;
  938. if (EC_POINT_is_at_infinity(group, Q)) ABORT;
  939. if (!EC_POINT_dbl(group, P, P, ctx)) ABORT;
  940. if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT;
  941. if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */
  942. if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT;
  943. if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT;
  944. if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */
  945. {
  946. const EC_POINT *points[3];
  947. const BIGNUM *scalars[3];
  948. if (EC_POINT_is_at_infinity(group, Q)) ABORT;
  949. points[0] = Q;
  950. points[1] = Q;
  951. points[2] = Q;
  952. if (!BN_add(y, z, BN_value_one())) ABORT;
  953. if (BN_is_odd(y)) ABORT;
  954. if (!BN_rshift1(y, y)) ABORT;
  955. scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */
  956. scalars[1] = y;
  957. fprintf(stdout, "combined multiplication ...");
  958. fflush(stdout);
  959. /* z is still the group order */
  960. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
  961. if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT;
  962. if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
  963. if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT;
  964. fprintf(stdout, ".");
  965. fflush(stdout);
  966. if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT;
  967. if (!BN_add(z, z, y)) ABORT;
  968. BN_set_negative(z, 1);
  969. scalars[0] = y;
  970. scalars[1] = z; /* z = -(order + y) */
  971. if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT;
  972. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  973. fprintf(stdout, ".");
  974. fflush(stdout);
  975. if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT;
  976. if (!BN_add(z, x, y)) ABORT;
  977. BN_set_negative(z, 1);
  978. scalars[0] = x;
  979. scalars[1] = y;
  980. scalars[2] = z; /* z = -(x+y) */
  981. if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT;
  982. if (!EC_POINT_is_at_infinity(group, P)) ABORT;
  983. fprintf(stdout, " ok\n\n");
  984. }
  985. #if 0
  986. timings(C2_K163, TIMING_BASE_PT, ctx);
  987. timings(C2_K163, TIMING_RAND_PT, ctx);
  988. timings(C2_K163, TIMING_SIMUL, ctx);
  989. timings(C2_B163, TIMING_BASE_PT, ctx);
  990. timings(C2_B163, TIMING_RAND_PT, ctx);
  991. timings(C2_B163, TIMING_SIMUL, ctx);
  992. timings(C2_K233, TIMING_BASE_PT, ctx);
  993. timings(C2_K233, TIMING_RAND_PT, ctx);
  994. timings(C2_K233, TIMING_SIMUL, ctx);
  995. timings(C2_B233, TIMING_BASE_PT, ctx);
  996. timings(C2_B233, TIMING_RAND_PT, ctx);
  997. timings(C2_B233, TIMING_SIMUL, ctx);
  998. timings(C2_K283, TIMING_BASE_PT, ctx);
  999. timings(C2_K283, TIMING_RAND_PT, ctx);
  1000. timings(C2_K283, TIMING_SIMUL, ctx);
  1001. timings(C2_B283, TIMING_BASE_PT, ctx);
  1002. timings(C2_B283, TIMING_RAND_PT, ctx);
  1003. timings(C2_B283, TIMING_SIMUL, ctx);
  1004. timings(C2_K409, TIMING_BASE_PT, ctx);
  1005. timings(C2_K409, TIMING_RAND_PT, ctx);
  1006. timings(C2_K409, TIMING_SIMUL, ctx);
  1007. timings(C2_B409, TIMING_BASE_PT, ctx);
  1008. timings(C2_B409, TIMING_RAND_PT, ctx);
  1009. timings(C2_B409, TIMING_SIMUL, ctx);
  1010. timings(C2_K571, TIMING_BASE_PT, ctx);
  1011. timings(C2_K571, TIMING_RAND_PT, ctx);
  1012. timings(C2_K571, TIMING_SIMUL, ctx);
  1013. timings(C2_B571, TIMING_BASE_PT, ctx);
  1014. timings(C2_B571, TIMING_RAND_PT, ctx);
  1015. timings(C2_B571, TIMING_SIMUL, ctx);
  1016. #endif
  1017. if (ctx)
  1018. BN_CTX_free(ctx);
  1019. BN_free(p); BN_free(a); BN_free(b);
  1020. EC_GROUP_free(group);
  1021. EC_POINT_free(P);
  1022. EC_POINT_free(Q);
  1023. EC_POINT_free(R);
  1024. BN_free(x); BN_free(y); BN_free(z); BN_free(cof);
  1025. if (C2_K163) EC_GROUP_free(C2_K163);
  1026. if (C2_B163) EC_GROUP_free(C2_B163);
  1027. if (C2_K233) EC_GROUP_free(C2_K233);
  1028. if (C2_B233) EC_GROUP_free(C2_B233);
  1029. if (C2_K283) EC_GROUP_free(C2_K283);
  1030. if (C2_B283) EC_GROUP_free(C2_B283);
  1031. if (C2_K409) EC_GROUP_free(C2_K409);
  1032. if (C2_B409) EC_GROUP_free(C2_B409);
  1033. if (C2_K571) EC_GROUP_free(C2_K571);
  1034. if (C2_B571) EC_GROUP_free(C2_B571);
  1035. }
  1036. #endif
  1037. static void internal_curve_test(void)
  1038. {
  1039. EC_builtin_curve *curves = NULL;
  1040. size_t crv_len = 0, n = 0;
  1041. int ok = 1;
  1042. crv_len = EC_get_builtin_curves(NULL, 0);
  1043. curves = OPENSSL_malloc(sizeof(EC_builtin_curve) * crv_len);
  1044. if (curves == NULL)
  1045. return;
  1046. if (!EC_get_builtin_curves(curves, crv_len))
  1047. {
  1048. OPENSSL_free(curves);
  1049. return;
  1050. }
  1051. fprintf(stdout, "testing internal curves: ");
  1052. for (n = 0; n < crv_len; n++)
  1053. {
  1054. EC_GROUP *group = NULL;
  1055. int nid = curves[n].nid;
  1056. if ((group = EC_GROUP_new_by_curve_name(nid)) == NULL)
  1057. {
  1058. ok = 0;
  1059. fprintf(stdout, "\nEC_GROUP_new_curve_name() failed with"
  1060. " curve %s\n", OBJ_nid2sn(nid));
  1061. /* try next curve */
  1062. continue;
  1063. }
  1064. if (!EC_GROUP_check(group, NULL))
  1065. {
  1066. ok = 0;
  1067. fprintf(stdout, "\nEC_GROUP_check() failed with"
  1068. " curve %s\n", OBJ_nid2sn(nid));
  1069. EC_GROUP_free(group);
  1070. /* try the next curve */
  1071. continue;
  1072. }
  1073. fprintf(stdout, ".");
  1074. fflush(stdout);
  1075. EC_GROUP_free(group);
  1076. }
  1077. if (ok)
  1078. fprintf(stdout, " ok\n\n");
  1079. else
  1080. {
  1081. fprintf(stdout, " failed\n\n");
  1082. ABORT;
  1083. }
  1084. OPENSSL_free(curves);
  1085. return;
  1086. }
  1087. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1088. /* nistp_test_params contains magic numbers for testing our optimized
  1089. * implementations of several NIST curves with characteristic > 3. */
  1090. struct nistp_test_params
  1091. {
  1092. const EC_METHOD* (*meth) ();
  1093. int degree;
  1094. /* Qx, Qy and D are taken from
  1095. * http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/ECDSA_Prime.pdf
  1096. * Otherwise, values are standard curve parameters from FIPS 180-3 */
  1097. const char *p, *a, *b, *Qx, *Qy, *Gx, *Gy, *order, *d;
  1098. };
  1099. static const struct nistp_test_params nistp_tests_params[] =
  1100. {
  1101. {
  1102. /* P-224 */
  1103. EC_GFp_nistp224_method,
  1104. 224,
  1105. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001", /* p */
  1106. "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE", /* a */
  1107. "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4", /* b */
  1108. "E84FB0B8E7000CB657D7973CF6B42ED78B301674276DF744AF130B3E", /* Qx */
  1109. "4376675C6FC5612C21A0FF2D2A89D2987DF7A2BC52183B5982298555", /* Qy */
  1110. "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21", /* Gx */
  1111. "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34", /* Gy */
  1112. "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D", /* order */
  1113. "3F0C488E987C80BE0FEE521F8D90BE6034EC69AE11CA72AA777481E8", /* d */
  1114. },
  1115. {
  1116. /* P-256 */
  1117. EC_GFp_nistp256_method,
  1118. 256,
  1119. "ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", /* p */
  1120. "ffffffff00000001000000000000000000000000fffffffffffffffffffffffc", /* a */
  1121. "5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b", /* b */
  1122. "b7e08afdfe94bad3f1dc8c734798ba1c62b3a0ad1e9ea2a38201cd0889bc7a19", /* Qx */
  1123. "3603f747959dbf7a4bb226e41928729063adc7ae43529e61b563bbc606cc5e09", /* Qy */
  1124. "6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296", /* Gx */
  1125. "4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5", /* Gy */
  1126. "ffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551", /* order */
  1127. "c477f9f65c22cce20657faa5b2d1d8122336f851a508a1ed04e479c34985bf96", /* d */
  1128. },
  1129. {
  1130. /* P-521 */
  1131. EC_GFp_nistp521_method,
  1132. 521,
  1133. "1ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", /* p */
  1134. "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffc", /* a */
  1135. "051953eb9618e1c9a1f929a21a0b68540eea2da725b99b315f3b8b489918ef109e156193951ec7e937b1652c0bd3bb1bf073573df883d2c34f1ef451fd46b503f00", /* b */
  1136. "0098e91eef9a68452822309c52fab453f5f117c1da8ed796b255e9ab8f6410cca16e59df403a6bdc6ca467a37056b1e54b3005d8ac030decfeb68df18b171885d5c4", /* Qx */
  1137. "0164350c321aecfc1cca1ba4364c9b15656150b4b78d6a48d7d28e7f31985ef17be8554376b72900712c4b83ad668327231526e313f5f092999a4632fd50d946bc2e", /* Qy */
  1138. "c6858e06b70404e9cd9e3ecb662395b4429c648139053fb521f828af606b4d3dbaa14b5e77efe75928fe1dc127a2ffa8de3348b3c1856a429bf97e7e31c2e5bd66", /* Gx */
  1139. "11839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650", /* Gy */
  1140. "1fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffa51868783bf2f966b7fcc0148f709a5d03bb5c9b8899c47aebb6fb71e91386409", /* order */
  1141. "0100085f47b8e1b8b11b7eb33028c0b2888e304bfc98501955b45bba1478dc184eeedf09b86a5f7c21994406072787205e69a63709fe35aa93ba333514b24f961722", /* d */
  1142. },
  1143. };
  1144. void nistp_single_test(const struct nistp_test_params *test)
  1145. {
  1146. BN_CTX *ctx;
  1147. BIGNUM *p, *a, *b, *x, *y, *n, *m, *order;
  1148. EC_GROUP *NISTP;
  1149. EC_POINT *G, *P, *Q, *Q_CHECK;
  1150. fprintf(stdout, "\nNIST curve P-%d (optimised implementation):\n", test->degree);
  1151. ctx = BN_CTX_new();
  1152. p = BN_new();
  1153. a = BN_new();
  1154. b = BN_new();
  1155. x = BN_new(); y = BN_new();
  1156. m = BN_new(); n = BN_new(); order = BN_new();
  1157. NISTP = EC_GROUP_new(test->meth());
  1158. if(!NISTP) ABORT;
  1159. if (!BN_hex2bn(&p, test->p)) ABORT;
  1160. if (1 != BN_is_prime_ex(p, BN_prime_checks, ctx, NULL)) ABORT;
  1161. if (!BN_hex2bn(&a, test->a)) ABORT;
  1162. if (!BN_hex2bn(&b, test->b)) ABORT;
  1163. if (!EC_GROUP_set_curve_GFp(NISTP, p, a, b, ctx)) ABORT;
  1164. G = EC_POINT_new(NISTP);
  1165. P = EC_POINT_new(NISTP);
  1166. Q = EC_POINT_new(NISTP);
  1167. Q_CHECK = EC_POINT_new(NISTP);
  1168. if(!BN_hex2bn(&x, test->Qx)) ABORT;
  1169. if(!BN_hex2bn(&y, test->Qy)) ABORT;
  1170. if(!EC_POINT_set_affine_coordinates_GFp(NISTP, Q_CHECK, x, y, ctx)) ABORT;
  1171. if (!BN_hex2bn(&x, test->Gx)) ABORT;
  1172. if (!BN_hex2bn(&y, test->Gy)) ABORT;
  1173. if (!EC_POINT_set_affine_coordinates_GFp(NISTP, G, x, y, ctx)) ABORT;
  1174. if (!BN_hex2bn(&order, test->order)) ABORT;
  1175. if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one())) ABORT;
  1176. fprintf(stdout, "verify degree ... ");
  1177. if (EC_GROUP_get_degree(NISTP) != test->degree) ABORT;
  1178. fprintf(stdout, "ok\n");
  1179. fprintf(stdout, "NIST test vectors ... ");
  1180. if (!BN_hex2bn(&n, test->d)) ABORT;
  1181. /* fixed point multiplication */
  1182. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1183. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
  1184. /* random point multiplication */
  1185. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1186. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
  1187. /* set generator to P = 2*G, where G is the standard generator */
  1188. if (!EC_POINT_dbl(NISTP, P, G, ctx)) ABORT;
  1189. if (!EC_GROUP_set_generator(NISTP, P, order, BN_value_one())) ABORT;
  1190. /* set the scalar to m=n/2, where n is the NIST test scalar */
  1191. if (!BN_rshift(m, n, 1)) ABORT;
  1192. /* test the non-standard generator */
  1193. /* fixed point multiplication */
  1194. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1195. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
  1196. /* random point multiplication */
  1197. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1198. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
  1199. /* now repeat all tests with precomputation */
  1200. if (!EC_GROUP_precompute_mult(NISTP, ctx)) ABORT;
  1201. /* fixed point multiplication */
  1202. EC_POINT_mul(NISTP, Q, m, NULL, NULL, ctx);
  1203. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
  1204. /* random point multiplication */
  1205. EC_POINT_mul(NISTP, Q, NULL, P, m, ctx);
  1206. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
  1207. /* reset generator */
  1208. if (!EC_GROUP_set_generator(NISTP, G, order, BN_value_one())) ABORT;
  1209. /* fixed point multiplication */
  1210. EC_POINT_mul(NISTP, Q, n, NULL, NULL, ctx);
  1211. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
  1212. /* random point multiplication */
  1213. EC_POINT_mul(NISTP, Q, NULL, G, n, ctx);
  1214. if (0 != EC_POINT_cmp(NISTP, Q, Q_CHECK, ctx)) ABORT;
  1215. fprintf(stdout, "ok\n");
  1216. group_order_tests(NISTP);
  1217. #if 0
  1218. timings(NISTP, TIMING_BASE_PT, ctx);
  1219. timings(NISTP, TIMING_RAND_PT, ctx);
  1220. #endif
  1221. EC_GROUP_free(NISTP);
  1222. EC_POINT_free(G);
  1223. EC_POINT_free(P);
  1224. EC_POINT_free(Q);
  1225. EC_POINT_free(Q_CHECK);
  1226. BN_free(n);
  1227. BN_free(m);
  1228. BN_free(p);
  1229. BN_free(a);
  1230. BN_free(b);
  1231. BN_free(x);
  1232. BN_free(y);
  1233. BN_free(order);
  1234. BN_CTX_free(ctx);
  1235. }
  1236. void nistp_tests()
  1237. {
  1238. unsigned i;
  1239. for (i = 0; i < sizeof(nistp_tests_params) / sizeof(struct nistp_test_params); i++)
  1240. {
  1241. nistp_single_test(&nistp_tests_params[i]);
  1242. }
  1243. }
  1244. #endif
  1245. static const char rnd_seed[] = "string to make the random number generator think it has entropy";
  1246. int main(int argc, char *argv[])
  1247. {
  1248. /* enable memory leak checking unless explicitly disabled */
  1249. if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off"))))
  1250. {
  1251. CRYPTO_malloc_debug_init();
  1252. CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
  1253. }
  1254. else
  1255. {
  1256. /* OPENSSL_DEBUG_MEMORY=off */
  1257. CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
  1258. }
  1259. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  1260. ERR_load_crypto_strings();
  1261. RAND_seed(rnd_seed, sizeof rnd_seed); /* or BN_generate_prime may fail */
  1262. prime_field_tests();
  1263. puts("");
  1264. #ifndef OPENSSL_NO_EC2M
  1265. char2_field_tests();
  1266. #endif
  1267. #ifndef OPENSSL_NO_EC_NISTP_64_GCC_128
  1268. nistp_tests();
  1269. #endif
  1270. /* test the internal curves */
  1271. internal_curve_test();
  1272. #ifndef OPENSSL_NO_ENGINE
  1273. ENGINE_cleanup();
  1274. #endif
  1275. CRYPTO_cleanup_all_ex_data();
  1276. ERR_free_strings();
  1277. ERR_remove_thread_state(NULL);
  1278. CRYPTO_mem_leaks_fp(stderr);
  1279. return 0;
  1280. }
  1281. #endif