diff options
| author | beck <> | 2002-05-15 02:29:21 +0000 |
|---|---|---|
| committer | beck <> | 2002-05-15 02:29:21 +0000 |
| commit | b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9 (patch) | |
| tree | fa27cf82a1250b64ed3bf5f4a18c7354d470bbcc /src/lib/libcrypto/ec/ectest.c | |
| parent | e471e1ea98d673597b182ea85f29e30c97cd08b5 (diff) | |
| download | openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.gz openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.tar.bz2 openbsd-b64270d1e45fe7f3241e4c9b6ce60d5ac89bc2e9.zip | |
OpenSSL 0.9.7 stable 2002 05 08 merge
Diffstat (limited to '')
| -rw-r--r-- | src/lib/libcrypto/ec/ectest.c | 634 |
1 files changed, 634 insertions, 0 deletions
diff --git a/src/lib/libcrypto/ec/ectest.c b/src/lib/libcrypto/ec/ectest.c new file mode 100644 index 0000000000..243cd83fb5 --- /dev/null +++ b/src/lib/libcrypto/ec/ectest.c | |||
| @@ -0,0 +1,634 @@ | |||
| 1 | /* crypto/ec/ectest.c */ | ||
| 2 | /* ==================================================================== | ||
| 3 | * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions | ||
| 7 | * are met: | ||
| 8 | * | ||
| 9 | * 1. Redistributions of source code must retain the above copyright | ||
| 10 | * notice, this list of conditions and the following disclaimer. | ||
| 11 | * | ||
| 12 | * 2. Redistributions in binary form must reproduce the above copyright | ||
| 13 | * notice, this list of conditions and the following disclaimer in | ||
| 14 | * the documentation and/or other materials provided with the | ||
| 15 | * distribution. | ||
| 16 | * | ||
| 17 | * 3. All advertising materials mentioning features or use of this | ||
| 18 | * software must display the following acknowledgment: | ||
| 19 | * "This product includes software developed by the OpenSSL Project | ||
| 20 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | ||
| 21 | * | ||
| 22 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | ||
| 23 | * endorse or promote products derived from this software without | ||
| 24 | * prior written permission. For written permission, please contact | ||
| 25 | * openssl-core@openssl.org. | ||
| 26 | * | ||
| 27 | * 5. Products derived from this software may not be called "OpenSSL" | ||
| 28 | * nor may "OpenSSL" appear in their names without prior written | ||
| 29 | * permission of the OpenSSL Project. | ||
| 30 | * | ||
| 31 | * 6. Redistributions of any form whatsoever must retain the following | ||
| 32 | * acknowledgment: | ||
| 33 | * "This product includes software developed by the OpenSSL Project | ||
| 34 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | ||
| 35 | * | ||
| 36 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | ||
| 37 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 38 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | ||
| 39 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | ||
| 40 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| 41 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | ||
| 42 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | ||
| 43 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
| 44 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
| 45 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 46 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
| 47 | * OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| 48 | * ==================================================================== | ||
| 49 | * | ||
| 50 | * This product includes cryptographic software written by Eric Young | ||
| 51 | * (eay@cryptsoft.com). This product includes software written by Tim | ||
| 52 | * Hudson (tjh@cryptsoft.com). | ||
| 53 | * | ||
| 54 | */ | ||
| 55 | |||
| 56 | #include <stdio.h> | ||
| 57 | #include <stdlib.h> | ||
| 58 | #include <string.h> | ||
| 59 | #include <time.h> | ||
| 60 | |||
| 61 | |||
| 62 | #ifdef OPENSSL_NO_EC | ||
| 63 | int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; } | ||
| 64 | #else | ||
| 65 | |||
| 66 | |||
| 67 | #include <openssl/ec.h> | ||
| 68 | #include <openssl/engine.h> | ||
| 69 | #include <openssl/err.h> | ||
| 70 | |||
| 71 | #define ABORT do { \ | ||
| 72 | fflush(stdout); \ | ||
| 73 | fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \ | ||
| 74 | ERR_print_errors_fp(stderr); \ | ||
| 75 | exit(1); \ | ||
| 76 | } while (0) | ||
| 77 | |||
| 78 | |||
| 79 | void timings(EC_GROUP *group, int multi, BN_CTX *ctx) | ||
| 80 | { | ||
| 81 | clock_t clck; | ||
| 82 | int i, j; | ||
| 83 | BIGNUM *s, *s0; | ||
| 84 | EC_POINT *P; | ||
| 85 | |||
| 86 | s = BN_new(); | ||
| 87 | s0 = BN_new(); | ||
| 88 | if (s == NULL || s0 == NULL) ABORT; | ||
| 89 | |||
| 90 | if (!EC_GROUP_get_curve_GFp(group, s, NULL, NULL, ctx)) ABORT; | ||
| 91 | fprintf(stdout, "Timings for %d bit prime, ", (int)BN_num_bits(s)); | ||
| 92 | if (!EC_GROUP_get_order(group, s, ctx)) ABORT; | ||
| 93 | fprintf(stdout, "%d bit scalars ", (int)BN_num_bits(s)); | ||
| 94 | fflush(stdout); | ||
| 95 | |||
| 96 | P = EC_POINT_new(group); | ||
| 97 | if (P == NULL) ABORT; | ||
| 98 | EC_POINT_copy(P, EC_GROUP_get0_generator(group)); | ||
| 99 | |||
| 100 | clck = clock(); | ||
| 101 | for (i = 0; i < 10; i++) | ||
| 102 | { | ||
| 103 | if (!BN_pseudo_rand(s, BN_num_bits(s), 0, 0)) ABORT; | ||
| 104 | if (multi) | ||
| 105 | { | ||
| 106 | if (!BN_pseudo_rand(s0, BN_num_bits(s), 0, 0)) ABORT; | ||
| 107 | } | ||
| 108 | for (j = 0; j < 10; j++) | ||
| 109 | { | ||
| 110 | if (!EC_POINT_mul(group, P, s, multi ? P : NULL, multi ? s0 : NULL, ctx)) ABORT; | ||
| 111 | } | ||
| 112 | fprintf(stdout, "."); | ||
| 113 | fflush(stdout); | ||
| 114 | } | ||
| 115 | fprintf(stdout, "\n"); | ||
| 116 | |||
| 117 | clck = clock() - clck; | ||
| 118 | |||
| 119 | #ifdef CLOCKS_PER_SEC | ||
| 120 | /* "To determine the time in seconds, the value returned | ||
| 121 | * by the clock function should be divided by the value | ||
| 122 | * of the macro CLOCKS_PER_SEC." | ||
| 123 | * -- ISO/IEC 9899 */ | ||
| 124 | # define UNIT "s" | ||
| 125 | #else | ||
| 126 | /* "`CLOCKS_PER_SEC' undeclared (first use this function)" | ||
| 127 | * -- cc on NeXTstep/OpenStep */ | ||
| 128 | # define UNIT "units" | ||
| 129 | # define CLOCKS_PER_SEC 1 | ||
| 130 | #endif | ||
| 131 | |||
| 132 | fprintf(stdout, "%i %s in %.2f " UNIT "\n", i*j, | ||
| 133 | multi ? "s*P+t*Q operations" : "point multiplications", | ||
| 134 | (double)clck/CLOCKS_PER_SEC); | ||
| 135 | fprintf(stdout, "average: %.4f " UNIT "\n", (double)clck/(CLOCKS_PER_SEC*i*j)); | ||
| 136 | |||
| 137 | EC_POINT_free(P); | ||
| 138 | BN_free(s); | ||
| 139 | BN_free(s0); | ||
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | int main(int argc, char *argv[]) | ||
| 144 | { | ||
| 145 | BN_CTX *ctx = NULL; | ||
| 146 | BIGNUM *p, *a, *b; | ||
| 147 | EC_GROUP *group; | ||
| 148 | EC_GROUP *P_192 = NULL, *P_224 = NULL, *P_256 = NULL, *P_384 = NULL, *P_521 = NULL; | ||
| 149 | EC_POINT *P, *Q, *R; | ||
| 150 | BIGNUM *x, *y, *z; | ||
| 151 | unsigned char buf[100]; | ||
| 152 | size_t i, len; | ||
| 153 | int k; | ||
| 154 | |||
| 155 | /* enable memory leak checking unless explicitly disabled */ | ||
| 156 | if (!((getenv("OPENSSL_DEBUG_MEMORY") != NULL) && (0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))) | ||
| 157 | { | ||
| 158 | CRYPTO_malloc_debug_init(); | ||
| 159 | CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL); | ||
| 160 | } | ||
| 161 | else | ||
| 162 | { | ||
| 163 | /* OPENSSL_DEBUG_MEMORY=off */ | ||
| 164 | CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0); | ||
| 165 | } | ||
| 166 | CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON); | ||
| 167 | ERR_load_crypto_strings(); | ||
| 168 | |||
| 169 | #if 1 /* optional */ | ||
| 170 | ctx = BN_CTX_new(); | ||
| 171 | if (!ctx) ABORT; | ||
| 172 | #endif | ||
| 173 | |||
| 174 | p = BN_new(); | ||
| 175 | a = BN_new(); | ||
| 176 | b = BN_new(); | ||
| 177 | if (!p || !a || !b) ABORT; | ||
| 178 | |||
| 179 | if (!BN_hex2bn(&p, "17")) ABORT; | ||
| 180 | if (!BN_hex2bn(&a, "1")) ABORT; | ||
| 181 | if (!BN_hex2bn(&b, "1")) ABORT; | ||
| 182 | |||
| 183 | group = EC_GROUP_new(EC_GFp_mont_method()); /* applications should use EC_GROUP_new_curve_GFp | ||
| 184 | * so that the library gets to choose the EC_METHOD */ | ||
| 185 | if (!group) ABORT; | ||
| 186 | |||
| 187 | if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT; | ||
| 188 | |||
| 189 | { | ||
| 190 | EC_GROUP *tmp; | ||
| 191 | tmp = EC_GROUP_new(EC_GROUP_method_of(group)); | ||
| 192 | if (!tmp) ABORT; | ||
| 193 | if (!EC_GROUP_copy(tmp, group)); | ||
| 194 | EC_GROUP_free(group); | ||
| 195 | group = tmp; | ||
| 196 | } | ||
| 197 | |||
| 198 | if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT; | ||
| 199 | |||
| 200 | fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x"); | ||
| 201 | BN_print_fp(stdout, p); | ||
| 202 | fprintf(stdout, ")\n a = 0x"); | ||
| 203 | BN_print_fp(stdout, a); | ||
| 204 | fprintf(stdout, "\n b = 0x"); | ||
| 205 | BN_print_fp(stdout, b); | ||
| 206 | fprintf(stdout, "\n"); | ||
| 207 | |||
| 208 | P = EC_POINT_new(group); | ||
| 209 | Q = EC_POINT_new(group); | ||
| 210 | R = EC_POINT_new(group); | ||
| 211 | if (!P || !Q || !R) ABORT; | ||
| 212 | |||
| 213 | if (!EC_POINT_set_to_infinity(group, P)) ABORT; | ||
| 214 | if (!EC_POINT_is_at_infinity(group, P)) ABORT; | ||
| 215 | |||
| 216 | buf[0] = 0; | ||
| 217 | if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT; | ||
| 218 | |||
| 219 | if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT; | ||
| 220 | if (!EC_POINT_is_at_infinity(group, P)) ABORT; | ||
| 221 | |||
| 222 | x = BN_new(); | ||
| 223 | y = BN_new(); | ||
| 224 | z = BN_new(); | ||
| 225 | if (!x || !y || !z) ABORT; | ||
| 226 | |||
| 227 | if (!BN_hex2bn(&x, "D")) ABORT; | ||
| 228 | if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT; | ||
| 229 | if (!EC_POINT_is_on_curve(group, Q, ctx)) | ||
| 230 | { | ||
| 231 | if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT; | ||
| 232 | fprintf(stderr, "Point is not on curve: x = 0x"); | ||
| 233 | BN_print_fp(stderr, x); | ||
| 234 | fprintf(stderr, ", y = 0x"); | ||
| 235 | BN_print_fp(stderr, y); | ||
| 236 | fprintf(stderr, "\n"); | ||
| 237 | ABORT; | ||
| 238 | } | ||
| 239 | |||
| 240 | fprintf(stdout, "A cyclic subgroup:\n"); | ||
| 241 | k = 100; | ||
| 242 | do | ||
| 243 | { | ||
| 244 | if (k-- == 0) ABORT; | ||
| 245 | |||
| 246 | if (EC_POINT_is_at_infinity(group, P)) | ||
| 247 | fprintf(stdout, " point at infinity\n"); | ||
| 248 | else | ||
| 249 | { | ||
| 250 | if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT; | ||
| 251 | |||
| 252 | fprintf(stdout, " x = 0x"); | ||
| 253 | BN_print_fp(stdout, x); | ||
| 254 | fprintf(stdout, ", y = 0x"); | ||
| 255 | BN_print_fp(stdout, y); | ||
| 256 | fprintf(stdout, "\n"); | ||
| 257 | } | ||
| 258 | |||
| 259 | if (!EC_POINT_copy(R, P)) ABORT; | ||
| 260 | if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT; | ||
| 261 | |||
| 262 | #if 0 /* optional */ | ||
| 263 | { | ||
| 264 | EC_POINT *points[3]; | ||
| 265 | |||
| 266 | points[0] = R; | ||
| 267 | points[1] = Q; | ||
| 268 | points[2] = P; | ||
| 269 | if (!EC_POINTs_make_affine(group, 2, points, ctx)) ABORT; | ||
| 270 | } | ||
| 271 | #endif | ||
| 272 | |||
| 273 | } | ||
| 274 | while (!EC_POINT_is_at_infinity(group, P)); | ||
| 275 | |||
| 276 | if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT; | ||
| 277 | if (!EC_POINT_is_at_infinity(group, P)) ABORT; | ||
| 278 | |||
| 279 | len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx); | ||
| 280 | if (len == 0) ABORT; | ||
| 281 | if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT; | ||
| 282 | if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT; | ||
| 283 | fprintf(stdout, "Generator as octect string, compressed form:\n "); | ||
| 284 | for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); | ||
| 285 | |||
| 286 | len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx); | ||
| 287 | if (len == 0) ABORT; | ||
| 288 | if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT; | ||
| 289 | if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT; | ||
| 290 | fprintf(stdout, "\nGenerator as octect string, uncompressed form:\n "); | ||
| 291 | for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); | ||
| 292 | |||
| 293 | len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx); | ||
| 294 | if (len == 0) ABORT; | ||
| 295 | if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT; | ||
| 296 | if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT; | ||
| 297 | fprintf(stdout, "\nGenerator as octect string, hybrid form:\n "); | ||
| 298 | for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]); | ||
| 299 | |||
| 300 | if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT; | ||
| 301 | fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x"); | ||
| 302 | BN_print_fp(stdout, x); | ||
| 303 | fprintf(stdout, ", Y = 0x"); | ||
| 304 | BN_print_fp(stdout, y); | ||
| 305 | fprintf(stdout, ", Z = 0x"); | ||
| 306 | BN_print_fp(stdout, z); | ||
| 307 | fprintf(stdout, "\n"); | ||
| 308 | |||
| 309 | if (!EC_POINT_invert(group, P, ctx)) ABORT; | ||
| 310 | if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT; | ||
| 311 | |||
| 312 | |||
| 313 | /* Curve P-192 (FIPS PUB 186-2, App. 6) */ | ||
| 314 | |||
| 315 | if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF")) ABORT; | ||
| 316 | if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT; | ||
| 317 | if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC")) ABORT; | ||
| 318 | if (!BN_hex2bn(&b, "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1")) ABORT; | ||
| 319 | if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT; | ||
| 320 | |||
| 321 | if (!BN_hex2bn(&x, "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012")) ABORT; | ||
| 322 | if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT; | ||
| 323 | if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; | ||
| 324 | if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831")) ABORT; | ||
| 325 | if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT; | ||
| 326 | |||
| 327 | if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT; | ||
| 328 | fprintf(stdout, "\nNIST curve P-192 -- Generator:\n x = 0x"); | ||
| 329 | BN_print_fp(stdout, x); | ||
| 330 | fprintf(stdout, "\n y = 0x"); | ||
| 331 | BN_print_fp(stdout, y); | ||
| 332 | fprintf(stdout, "\n"); | ||
| 333 | /* G_y value taken from the standard: */ | ||
| 334 | if (!BN_hex2bn(&z, "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811")) ABORT; | ||
| 335 | if (0 != BN_cmp(y, z)) ABORT; | ||
| 336 | |||
| 337 | fprintf(stdout, "verify group order ..."); | ||
| 338 | fflush(stdout); | ||
| 339 | if (!EC_GROUP_get_order(group, z, ctx)) ABORT; | ||
| 340 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 341 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 342 | fprintf(stdout, "."); | ||
| 343 | fflush(stdout); | ||
| 344 | if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; | ||
| 345 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 346 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 347 | fprintf(stdout, " ok\n"); | ||
| 348 | |||
| 349 | if (!(P_192 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; | ||
| 350 | if (!EC_GROUP_copy(P_192, group)) ABORT; | ||
| 351 | |||
| 352 | |||
| 353 | /* Curve P-224 (FIPS PUB 186-2, App. 6) */ | ||
| 354 | |||
| 355 | if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001")) ABORT; | ||
| 356 | if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT; | ||
| 357 | if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE")) ABORT; | ||
| 358 | if (!BN_hex2bn(&b, "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4")) ABORT; | ||
| 359 | if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT; | ||
| 360 | |||
| 361 | if (!BN_hex2bn(&x, "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21")) ABORT; | ||
| 362 | if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 0, ctx)) ABORT; | ||
| 363 | if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; | ||
| 364 | if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D")) ABORT; | ||
| 365 | if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT; | ||
| 366 | |||
| 367 | if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT; | ||
| 368 | fprintf(stdout, "\nNIST curve P-224 -- Generator:\n x = 0x"); | ||
| 369 | BN_print_fp(stdout, x); | ||
| 370 | fprintf(stdout, "\n y = 0x"); | ||
| 371 | BN_print_fp(stdout, y); | ||
| 372 | fprintf(stdout, "\n"); | ||
| 373 | /* G_y value taken from the standard: */ | ||
| 374 | if (!BN_hex2bn(&z, "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34")) ABORT; | ||
| 375 | if (0 != BN_cmp(y, z)) ABORT; | ||
| 376 | |||
| 377 | fprintf(stdout, "verify group order ..."); | ||
| 378 | fflush(stdout); | ||
| 379 | if (!EC_GROUP_get_order(group, z, ctx)) ABORT; | ||
| 380 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 381 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 382 | fprintf(stdout, "."); | ||
| 383 | fflush(stdout); | ||
| 384 | if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; | ||
| 385 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 386 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 387 | fprintf(stdout, " ok\n"); | ||
| 388 | |||
| 389 | if (!(P_224 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; | ||
| 390 | if (!EC_GROUP_copy(P_224, group)) ABORT; | ||
| 391 | |||
| 392 | |||
| 393 | /* Curve P-256 (FIPS PUB 186-2, App. 6) */ | ||
| 394 | |||
| 395 | if (!BN_hex2bn(&p, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF")) ABORT; | ||
| 396 | if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT; | ||
| 397 | if (!BN_hex2bn(&a, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC")) ABORT; | ||
| 398 | if (!BN_hex2bn(&b, "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B")) ABORT; | ||
| 399 | if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT; | ||
| 400 | |||
| 401 | if (!BN_hex2bn(&x, "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296")) ABORT; | ||
| 402 | if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT; | ||
| 403 | if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; | ||
| 404 | if (!BN_hex2bn(&z, "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E" | ||
| 405 | "84F3B9CAC2FC632551")) ABORT; | ||
| 406 | if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT; | ||
| 407 | |||
| 408 | if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT; | ||
| 409 | fprintf(stdout, "\nNIST curve P-256 -- 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, "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5")) ABORT; | ||
| 416 | if (0 != BN_cmp(y, z)) ABORT; | ||
| 417 | |||
| 418 | fprintf(stdout, "verify group order ..."); | ||
| 419 | fflush(stdout); | ||
| 420 | if (!EC_GROUP_get_order(group, z, ctx)) ABORT; | ||
| 421 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 422 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 423 | fprintf(stdout, "."); | ||
| 424 | fflush(stdout); | ||
| 425 | if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; | ||
| 426 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 427 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 428 | fprintf(stdout, " ok\n"); | ||
| 429 | |||
| 430 | if (!(P_256 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; | ||
| 431 | if (!EC_GROUP_copy(P_256, group)) ABORT; | ||
| 432 | |||
| 433 | |||
| 434 | /* Curve P-384 (FIPS PUB 186-2, App. 6) */ | ||
| 435 | |||
| 436 | if (!BN_hex2bn(&p, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | ||
| 437 | "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF")) ABORT; | ||
| 438 | if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT; | ||
| 439 | if (!BN_hex2bn(&a, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | ||
| 440 | "FFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC")) ABORT; | ||
| 441 | if (!BN_hex2bn(&b, "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141" | ||
| 442 | "120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF")) ABORT; | ||
| 443 | if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT; | ||
| 444 | |||
| 445 | if (!BN_hex2bn(&x, "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B" | ||
| 446 | "9859F741E082542A385502F25DBF55296C3A545E3872760AB7")) ABORT; | ||
| 447 | if (!EC_POINT_set_compressed_coordinates_GFp(group, P, x, 1, ctx)) ABORT; | ||
| 448 | if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; | ||
| 449 | if (!BN_hex2bn(&z, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | ||
| 450 | "FFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973")) ABORT; | ||
| 451 | if (!EC_GROUP_set_generator(group, P, z, BN_value_one())) ABORT; | ||
| 452 | |||
| 453 | if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT; | ||
| 454 | fprintf(stdout, "\nNIST curve P-384 -- Generator:\n x = 0x"); | ||
| 455 | BN_print_fp(stdout, x); | ||
| 456 | fprintf(stdout, "\n y = 0x"); | ||
| 457 | BN_print_fp(stdout, y); | ||
| 458 | fprintf(stdout, "\n"); | ||
| 459 | /* G_y value taken from the standard: */ | ||
| 460 | if (!BN_hex2bn(&z, "3617DE4A96262C6F5D9E98BF9292DC29F8F41DBD289A14" | ||
| 461 | "7CE9DA3113B5F0B8C00A60B1CE1D7E819D7A431D7C90EA0E5F")) ABORT; | ||
| 462 | if (0 != BN_cmp(y, z)) ABORT; | ||
| 463 | |||
| 464 | fprintf(stdout, "verify group order ..."); | ||
| 465 | fflush(stdout); | ||
| 466 | if (!EC_GROUP_get_order(group, z, ctx)) ABORT; | ||
| 467 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 468 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 469 | fprintf(stdout, "."); | ||
| 470 | fflush(stdout); | ||
| 471 | if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; | ||
| 472 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 473 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 474 | fprintf(stdout, " ok\n"); | ||
| 475 | |||
| 476 | if (!(P_384 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; | ||
| 477 | if (!EC_GROUP_copy(P_384, group)) ABORT; | ||
| 478 | |||
| 479 | |||
| 480 | /* Curve P-521 (FIPS PUB 186-2, App. 6) */ | ||
| 481 | |||
| 482 | if (!BN_hex2bn(&p, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | ||
| 483 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | ||
| 484 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFF")) ABORT; | ||
| 485 | if (1 != BN_is_prime(p, BN_prime_checks, 0, ctx, NULL)) ABORT; | ||
| 486 | if (!BN_hex2bn(&a, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | ||
| 487 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF" | ||
| 488 | "FFFFFFFFFFFFFFFFFFFFFFFFFFFC")) ABORT; | ||
| 489 | if (!BN_hex2bn(&b, "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B" | ||
| 490 | "315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573" | ||
| 491 | "DF883D2C34F1EF451FD46B503F00")) ABORT; | ||
| 492 | if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT; | ||
| 493 | |||
| 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 | |||
| 504 | if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT; | ||
| 505 | fprintf(stdout, "\nNIST curve P-521 -- Generator:\n x = 0x"); | ||
| 506 | BN_print_fp(stdout, x); | ||
| 507 | fprintf(stdout, "\n y = 0x"); | ||
| 508 | BN_print_fp(stdout, y); | ||
| 509 | fprintf(stdout, "\n"); | ||
| 510 | /* G_y value taken from the standard: */ | ||
| 511 | if (!BN_hex2bn(&z, "11839296A789A3BC0045C8A5FB42C7D1BD998F54449579" | ||
| 512 | "B446817AFBD17273E662C97EE72995EF42640C550B9013FAD0761353C" | ||
| 513 | "7086A272C24088BE94769FD16650")) ABORT; | ||
| 514 | if (0 != BN_cmp(y, z)) ABORT; | ||
| 515 | |||
| 516 | fprintf(stdout, "verify group order ..."); | ||
| 517 | fflush(stdout); | ||
| 518 | if (!EC_GROUP_get_order(group, z, ctx)) ABORT; | ||
| 519 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 520 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 521 | fprintf(stdout, "."); | ||
| 522 | fflush(stdout); | ||
| 523 | if (!EC_GROUP_precompute_mult(group, ctx)) ABORT; | ||
| 524 | if (!EC_POINT_mul(group, Q, z, NULL, NULL, ctx)) ABORT; | ||
| 525 | if (!EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 526 | fprintf(stdout, " ok\n"); | ||
| 527 | |||
| 528 | if (!(P_521 = EC_GROUP_new(EC_GROUP_method_of(group)))) ABORT; | ||
| 529 | if (!EC_GROUP_copy(P_521, group)) ABORT; | ||
| 530 | |||
| 531 | |||
| 532 | /* more tests using the last curve */ | ||
| 533 | |||
| 534 | if (!EC_POINT_copy(Q, P)) ABORT; | ||
| 535 | if (EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 536 | if (!EC_POINT_dbl(group, P, P, ctx)) ABORT; | ||
| 537 | if (!EC_POINT_is_on_curve(group, P, ctx)) ABORT; | ||
| 538 | if (!EC_POINT_invert(group, Q, ctx)) ABORT; /* P = -2Q */ | ||
| 539 | |||
| 540 | if (!EC_POINT_add(group, R, P, Q, ctx)) ABORT; | ||
| 541 | if (!EC_POINT_add(group, R, R, Q, ctx)) ABORT; | ||
| 542 | if (!EC_POINT_is_at_infinity(group, R)) ABORT; /* R = P + 2Q */ | ||
| 543 | |||
| 544 | { | ||
| 545 | const EC_POINT *points[3]; | ||
| 546 | const BIGNUM *scalars[3]; | ||
| 547 | |||
| 548 | if (EC_POINT_is_at_infinity(group, Q)) ABORT; | ||
| 549 | points[0] = Q; | ||
| 550 | points[1] = Q; | ||
| 551 | points[2] = Q; | ||
| 552 | |||
| 553 | if (!BN_add(y, z, BN_value_one())) ABORT; | ||
| 554 | if (BN_is_odd(y)) ABORT; | ||
| 555 | if (!BN_rshift1(y, y)) ABORT; | ||
| 556 | scalars[0] = y; /* (group order + 1)/2, so y*Q + y*Q = Q */ | ||
| 557 | scalars[1] = y; | ||
| 558 | |||
| 559 | fprintf(stdout, "combined multiplication ..."); | ||
| 560 | fflush(stdout); | ||
| 561 | |||
| 562 | /* z is still the group order */ | ||
| 563 | if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT; | ||
| 564 | if (!EC_POINTs_mul(group, R, z, 2, points, scalars, ctx)) ABORT; | ||
| 565 | if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT; | ||
| 566 | if (0 != EC_POINT_cmp(group, R, Q, ctx)) ABORT; | ||
| 567 | |||
| 568 | fprintf(stdout, "."); | ||
| 569 | fflush(stdout); | ||
| 570 | |||
| 571 | if (!BN_pseudo_rand(y, BN_num_bits(y), 0, 0)) ABORT; | ||
| 572 | if (!BN_add(z, z, y)) ABORT; | ||
| 573 | z->neg = 1; | ||
| 574 | scalars[0] = y; | ||
| 575 | scalars[1] = z; /* z = -(order + y) */ | ||
| 576 | |||
| 577 | if (!EC_POINTs_mul(group, P, NULL, 2, points, scalars, ctx)) ABORT; | ||
| 578 | if (!EC_POINT_is_at_infinity(group, P)) ABORT; | ||
| 579 | |||
| 580 | fprintf(stdout, "."); | ||
| 581 | fflush(stdout); | ||
| 582 | |||
| 583 | if (!BN_pseudo_rand(x, BN_num_bits(y) - 1, 0, 0)) ABORT; | ||
| 584 | if (!BN_add(z, x, y)) ABORT; | ||
| 585 | z->neg = 1; | ||
| 586 | scalars[0] = x; | ||
| 587 | scalars[1] = y; | ||
| 588 | scalars[2] = z; /* z = -(x+y) */ | ||
| 589 | |||
| 590 | if (!EC_POINTs_mul(group, P, NULL, 3, points, scalars, ctx)) ABORT; | ||
| 591 | if (!EC_POINT_is_at_infinity(group, P)) ABORT; | ||
| 592 | |||
| 593 | fprintf(stdout, " ok\n\n"); | ||
| 594 | } | ||
| 595 | |||
| 596 | |||
| 597 | #if 0 | ||
| 598 | timings(P_192, 0, ctx); | ||
| 599 | timings(P_192, 1, ctx); | ||
| 600 | timings(P_224, 0, ctx); | ||
| 601 | timings(P_224, 1, ctx); | ||
| 602 | timings(P_256, 0, ctx); | ||
| 603 | timings(P_256, 1, ctx); | ||
| 604 | timings(P_384, 0, ctx); | ||
| 605 | timings(P_384, 1, ctx); | ||
| 606 | timings(P_521, 0, ctx); | ||
| 607 | timings(P_521, 1, ctx); | ||
| 608 | #endif | ||
| 609 | |||
| 610 | |||
| 611 | if (ctx) | ||
| 612 | BN_CTX_free(ctx); | ||
| 613 | BN_free(p); BN_free(a); BN_free(b); | ||
| 614 | EC_GROUP_free(group); | ||
| 615 | EC_POINT_free(P); | ||
| 616 | EC_POINT_free(Q); | ||
| 617 | EC_POINT_free(R); | ||
| 618 | BN_free(x); BN_free(y); BN_free(z); | ||
| 619 | |||
| 620 | if (P_192) EC_GROUP_free(P_192); | ||
| 621 | if (P_224) EC_GROUP_free(P_224); | ||
| 622 | if (P_256) EC_GROUP_free(P_256); | ||
| 623 | if (P_384) EC_GROUP_free(P_384); | ||
| 624 | if (P_521) EC_GROUP_free(P_521); | ||
| 625 | |||
| 626 | ENGINE_cleanup(); | ||
| 627 | CRYPTO_cleanup_all_ex_data(); | ||
| 628 | ERR_free_strings(); | ||
| 629 | ERR_remove_state(0); | ||
| 630 | CRYPTO_mem_leaks_fp(stderr); | ||
| 631 | |||
| 632 | return 0; | ||
| 633 | } | ||
| 634 | #endif | ||
