1 /* crypto/ec/ectest.c */
2 /* ====================================================================
3 * Copyright (c) 1998-2001 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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).
61 int main(int argc, char * argv[]) { puts("Elliptic curves are disabled."); return 0; }
65 #include <openssl/ec.h>
66 #include <openssl/err.h>
69 fprintf(stderr, "%s:%d: ABORT\n", __FILE__, __LINE__); \
70 ERR_print_errors_fp(stderr); \
74 int main(int argc, char *argv[])
81 unsigned char buf[100];
84 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
85 ERR_load_crypto_strings();
95 if (!p || !a || !b) ABORT;
97 if (!BN_hex2bn(&p, "D")) ABORT;
98 if (!BN_hex2bn(&a, "7")) ABORT;
99 if (!BN_hex2bn(&b, "C")) ABORT;
101 group = EC_GROUP_new_curve_GFp(p, a, b, NULL);
104 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x");
105 BN_print_fp(stdout, p);
106 fprintf(stdout, ")\n a = 0x");
107 BN_print_fp(stdout, a);
108 fprintf(stdout, "\n b = 0x");
109 BN_print_fp(stdout, b);
110 fprintf(stdout, "\n");
112 P = EC_POINT_new(group);
113 Q = EC_POINT_new(group);
114 R = EC_POINT_new(group);
115 if (!P || !Q || !R) ABORT;
117 if (!EC_POINT_set_to_infinity(group, P)) ABORT;
118 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
121 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
123 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
124 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
129 if (!x || !y || !z) ABORT;
131 if (!BN_hex2bn(&x, "C")) ABORT;
132 if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
133 if (!EC_POINT_is_on_curve(group, Q, ctx))
135 fprintf(stderr, "Point is not on curve, x = 0x");
136 BN_print_fp(stderr, x);
137 fprintf(stderr, "\n");
141 fprintf(stdout, "A cyclic subgroup:\n");
144 if (EC_POINT_is_at_infinity(group, P))
145 fprintf(stdout, " point at infinity\n");
148 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
150 fprintf(stdout, " x = 0x");
151 BN_print_fp(stdout, x);
152 fprintf(stdout, ", y = 0x");
153 BN_print_fp(stdout, y);
154 fprintf(stdout, "\n");
157 if (!EC_POINT_copy(R, P)) ABORT;
158 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
161 if (!EC_POINT_make_affine(group, P, ctx)) ABORT;
164 while (!EC_POINT_is_at_infinity(group, P));
166 if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
167 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
169 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
171 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
172 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
173 fprintf(stdout, "Generator as octect string, compressed form:\n ");
174 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
176 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
178 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
179 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
180 fprintf(stdout, "\nGenerator as octect string, uncompressed form:\n ");
181 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
183 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
185 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
186 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
187 fprintf(stdout, "\nGenerator as octect string, hybrid form:\n ");
188 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
190 if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
191 fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x");
192 BN_print_fp(stdout, x);
193 fprintf(stdout, ", Y = 0x");
194 BN_print_fp(stdout, y);
195 fprintf(stdout, ", Z = 0x");
196 BN_print_fp(stdout, z);
197 fprintf(stdout, "\n");
199 if (!EC_POINT_invert(group, P, ctx)) ABORT;
200 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
206 BN_free(p); BN_free(a); BN_free(b);
207 EC_GROUP_free(group);
211 BN_free(x); BN_free(y); BN_free(z);
215 CRYPTO_mem_leaks_fp(stderr);