2
0

ectest.c 46 KB

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