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(EC_GFp_mont_method());
103 if (!EC_GROUP_set_curve_GFp(group, p, a, b, ctx)) ABORT;
104 if (!EC_GROUP_get_curve_GFp(group, p, a, b, ctx)) ABORT;
106 fprintf(stdout, "Curve defined by Weierstrass equation\n y^2 = x^3 + a*x + b (mod 0x");
107 BN_print_fp(stdout, p);
108 fprintf(stdout, ")\n a = 0x");
109 BN_print_fp(stdout, a);
110 fprintf(stdout, "\n b = 0x");
111 BN_print_fp(stdout, b);
112 fprintf(stdout, "\n");
114 P = EC_POINT_new(group);
115 Q = EC_POINT_new(group);
116 R = EC_POINT_new(group);
117 if (!P || !Q || !R) ABORT;
119 if (!EC_POINT_set_to_infinity(group, P)) ABORT;
120 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
123 if (!EC_POINT_oct2point(group, Q, buf, 1, ctx)) ABORT;
125 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
126 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
131 if (!x || !y || !z) ABORT;
133 if (!BN_hex2bn(&x, "C")) ABORT;
134 if (!EC_POINT_set_compressed_coordinates_GFp(group, Q, x, 1, ctx)) ABORT;
135 if (!EC_POINT_is_on_curve(group, Q, ctx))
137 if (!EC_POINT_get_affine_coordinates_GFp(group, Q, x, y, ctx)) ABORT;
138 fprintf(stderr, "Point is not on curve: x = 0x");
139 BN_print_fp(stderr, x);
140 fprintf(stderr, ", y = 0x");
141 BN_print_fp(stderr, y);
142 fprintf(stderr, "\n");
146 fprintf(stdout, "A cyclic subgroup:\n");
149 if (EC_POINT_is_at_infinity(group, P))
150 fprintf(stdout, " point at infinity\n");
153 if (!EC_POINT_get_affine_coordinates_GFp(group, P, x, y, ctx)) ABORT;
155 fprintf(stdout, " x = 0x");
156 BN_print_fp(stdout, x);
157 fprintf(stdout, ", y = 0x");
158 BN_print_fp(stdout, y);
159 fprintf(stdout, "\n");
162 if (!EC_POINT_copy(R, P)) ABORT;
163 if (!EC_POINT_add(group, P, P, Q, ctx)) ABORT;
166 if (!EC_POINT_make_affine(group, P, ctx)) ABORT;
169 while (!EC_POINT_is_at_infinity(group, P));
171 if (!EC_POINT_add(group, P, Q, R, ctx)) ABORT;
172 if (!EC_POINT_is_at_infinity(group, P)) ABORT;
174 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_COMPRESSED, buf, sizeof buf, ctx);
176 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
177 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
178 fprintf(stdout, "Generator as octect string, compressed form:\n ");
179 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
181 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_UNCOMPRESSED, buf, sizeof buf, ctx);
183 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
184 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
185 fprintf(stdout, "\nGenerator as octect string, uncompressed form:\n ");
186 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
188 len = EC_POINT_point2oct(group, Q, POINT_CONVERSION_HYBRID, buf, sizeof buf, ctx);
190 if (!EC_POINT_oct2point(group, P, buf, len, ctx)) ABORT;
191 if (0 != EC_POINT_cmp(group, P, Q, ctx)) ABORT;
192 fprintf(stdout, "\nGenerator as octect string, hybrid form:\n ");
193 for (i = 0; i < len; i++) fprintf(stdout, "%02X", buf[i]);
195 if (!EC_POINT_get_Jprojective_coordinates_GFp(group, R, x, y, z, ctx)) ABORT;
196 fprintf(stdout, "\nA representation of the inverse of that generator in\nJacobian projective coordinates:\n X = 0x");
197 BN_print_fp(stdout, x);
198 fprintf(stdout, ", Y = 0x");
199 BN_print_fp(stdout, y);
200 fprintf(stdout, ", Z = 0x");
201 BN_print_fp(stdout, z);
202 fprintf(stdout, "\n");
204 if (!EC_POINT_invert(group, P, ctx)) ABORT;
205 if (0 != EC_POINT_cmp(group, P, R, ctx)) ABORT;
211 BN_free(p); BN_free(a); BN_free(b);
212 EC_GROUP_free(group);
216 BN_free(x); BN_free(y); BN_free(z);
220 CRYPTO_mem_leaks_fp(stderr);