ectest.c 53 KB

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