]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - test_curves.c
Don't load the gost engine in test_curves.c
[openssl-gost/engine.git] / test_curves.c
1 /*
2  * Copyright (C) 2018 vt@altlinux.org. All Rights Reserved.
3  *
4  * Contents licensed under the terms of the OpenSSL license
5  * See https://www.openssl.org/source/license.html for details
6  */
7
8 #include "gost_lcl.h"
9 #include <openssl/evp.h>
10 #include <openssl/rand.h>
11 #include <openssl/err.h>
12 #include <openssl/asn1.h>
13 #include <openssl/obj_mac.h>
14 #include <openssl/ec.h>
15 #include <openssl/bn.h>
16 #include <string.h>
17
18 #define T(e) ({ if (!(e)) { \
19                 ERR_print_errors_fp(stderr); \
20                 OpenSSLDie(__FILE__, __LINE__, #e); \
21             } \
22         })
23
24 #define cRED    "\033[1;31m"
25 #define cDRED   "\033[0;31m"
26 #define cGREEN  "\033[1;32m"
27 #define cDGREEN "\033[0;32m"
28 #define cBLUE   "\033[1;34m"
29 #define cDBLUE  "\033[0;34m"
30 #define cNORM   "\033[m"
31 #define TEST_ASSERT(e) { \
32         test = e; \
33         if (test) \
34                 printf(cRED "  Test FAILED" cNORM "\n"); \
35         else \
36                 printf(cGREEN "  Test passed" cNORM "\n"); \
37 }
38
39 struct test_curve {
40     int nid;
41     const char *name;
42     int listed;
43 };
44
45 static struct test_curve test_curves[] = {
46 #if 2001
47     { NID_id_GostR3410_2001_TestParamSet, },
48 #endif
49     { NID_id_GostR3410_2001_CryptoPro_A_ParamSet },
50     { NID_id_GostR3410_2001_CryptoPro_B_ParamSet },
51     { NID_id_GostR3410_2001_CryptoPro_C_ParamSet },
52     { NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet },
53     { NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet },
54     { NID_id_tc26_gost_3410_2012_512_paramSetA, "id-tc26-gost-3410-2012-512-paramSetA", },
55     { NID_id_tc26_gost_3410_2012_512_paramSetB, "id-tc26-gost-3410-2012-512-paramSetB", },
56     { NID_id_tc26_gost_3410_2012_512_paramSetC, "id-tc26-gost-3410-2012-512-paramSetC", },
57     { NID_id_tc26_gost_3410_2012_256_paramSetA, "id-tc26-gost-3410-2012-256-paramSetA", },
58     { NID_id_tc26_gost_3410_2012_256_paramSetB, "id-tc26-gost-3410-2012-256-paramSetB", },
59     { NID_id_tc26_gost_3410_2012_256_paramSetC, "id-tc26-gost-3410-2012-256-paramSetC", },
60     { NID_id_tc26_gost_3410_2012_256_paramSetD, "id-tc26-gost-3410-2012-256-paramSetD", },
61     0,
62 };
63
64 static struct test_curve *get_test_curve(int nid)
65 {
66     int i;
67
68     for (i = 0; test_curves[i].nid; i++)
69         if (test_curves[i].nid == nid)
70             return &test_curves[i];
71     return NULL;
72 }
73
74 static void print_bn(const char *name, const BIGNUM *n)
75 {
76     printf("%3s = ", name);
77     BN_print_fp(stdout, n);
78     printf("\n");
79 }
80
81 // https://wiki.openssl.org/index.php/Elliptic_Curve_Cryptography
82 static int parameter_test(struct test_curve *tc)
83 {
84     const int nid = tc->nid;
85     int test;
86
87     printf(cBLUE "Test curve NID %d" cNORM, nid);
88     if (tc->name)
89         printf(cBLUE ": %s" cNORM, tc->name);
90     else if (OBJ_nid2sn(nid))
91         printf(cBLUE ": %s" cNORM, OBJ_nid2sn(nid));
92     printf("\n");
93
94     if (!OBJ_nid2obj(nid)) {
95         printf(cRED "NID %d not found" cNORM "\n", nid);
96         return 1;
97     }
98
99     /* nid resolves in both directions */
100     const char *sn, *ln;
101     T(sn = OBJ_nid2sn(nid));
102     T(ln = OBJ_nid2ln(nid));
103     if (tc->name)
104         T(!strcmp(tc->name, OBJ_nid2sn(nid)));
105     T(nid == OBJ_sn2nid(sn));
106     T(nid == OBJ_ln2nid(ln));
107
108     EC_KEY *ec;
109     T(ec = EC_KEY_new());
110     if (!fill_GOST_EC_params(ec, nid)) {
111         printf(cRED "fill_GOST_EC_params FAIL" cNORM "\n");
112         ERR_print_errors_fp(stderr);
113         return 1;
114     }
115
116     const EC_GROUP *group;
117     T(group = EC_KEY_get0_group(ec));
118
119     BN_CTX *ctx;
120     T(ctx = BN_CTX_new());
121     BIGNUM *p, *a, *b;
122     T(p = BN_new());
123     T(a = BN_new());
124     T(b = BN_new());
125     EC_GROUP_get_curve(group, p, a, b, ctx);
126     print_bn("p", p);
127     print_bn("a", a);
128     print_bn("b", b);
129     T(!BN_is_zero(p));
130     T(BN_is_odd(p)); /* Should be odd for F_p */
131     T(!BN_is_zero(a));
132     T(!BN_is_zero(b));
133
134     /* Check generator */
135     const EC_POINT *generator;
136     T(generator = EC_GROUP_get0_generator(group));
137     BIGNUM *x, *y;
138     T(x = BN_new());
139     T(y = BN_new());
140     T(EC_POINT_get_affine_coordinates(group, generator, x, y, ctx));
141     print_bn("x", x);
142     print_bn("y", y);
143     T(!BN_is_zero(y));
144
145     /* Generator is not identity element 0 */
146     T(EC_POINT_is_at_infinity(group, generator) == 0);
147
148     /* x and y is in range [1 .. p-1] */
149     T(!BN_is_negative(x));
150     T(!BN_is_negative(y));
151     T(BN_cmp(x, p) < 0);
152     T(BN_cmp(y, p) < 0);
153
154     /* Generator should be on curve */
155     T(EC_POINT_is_on_curve(group, generator, ctx) == 1);
156
157     /* y^2 == (x^3 + ax + b) mod p
158      * Should be same as EC_POINT_is_on_curve(generator),
159      * but, let's calculate it manually. */
160     BIGNUM *yy  = BN_new();
161     BIGNUM *r   = BN_new();
162     BIGNUM *xxx = BN_new();
163     BIGNUM *ax  = BN_new();
164     T(yy && r && xxx && ax);
165     BN_set_word(r, 2);
166     BN_mod_exp(yy, y, r, p, ctx);
167     BN_set_word(r, 3);
168     BN_mod_exp(xxx, x, r, p, ctx);
169     BN_mod_mul(ax, a, x, p, ctx);
170     BN_mod_add(xxx, xxx, ax, p, ctx);
171     BN_mod_add(xxx, xxx, b, p, ctx);
172     T(BN_cmp(yy, xxx) == 0);
173     BN_free(yy);
174     BN_free(r);
175     BN_free(xxx);
176     BN_free(ax);
177     BN_free(p);
178     BN_free(a);
179     BN_free(b);
180     BN_free(x);
181     BN_free(y);
182
183     /* Check order */
184     const BIGNUM *order;
185     T(order = EC_GROUP_get0_order(group));
186     T(!BN_is_zero(order));
187     print_bn("q", order);
188     T(BN_is_odd(order));
189     EC_POINT *point;
190     T((point = EC_POINT_new(group)));
191     T(EC_POINT_mul(group, point, NULL, generator, order, ctx));
192     /* generator * order is the point at infinity? */
193     T(EC_POINT_is_at_infinity(group, point) == 1);
194     EC_POINT_free(point);
195
196     /* Check if order is cyclic */
197     BIGNUM *k1 = BN_new();
198     BIGNUM *k2 = BN_new();
199     EC_POINT *p1 = EC_POINT_new(group);
200     EC_POINT *p2 = EC_POINT_new(group);
201     BN_set_word(k1, 3);
202     BN_set_word(k2, 3);
203     BN_add(k2, k2, order);
204     T(EC_POINT_mul(group, p1, NULL, generator, k1, ctx));
205     T(EC_POINT_mul(group, p2, NULL, generator, k2, ctx));
206     T(EC_POINT_cmp(group, p1, p2, ctx) == 0);
207     BN_free(k1);
208     BN_free(k2);
209     EC_POINT_free(p1);
210     EC_POINT_free(p2);
211
212     /* Cofactor is 1 or 4 */
213     const BIGNUM *c;
214     T(c = EC_GROUP_get0_cofactor(group));
215     T(BN_is_word(c, 1) || BN_is_word(c, 4));
216
217     BN_CTX_free(ctx);
218     EC_KEY_free(ec);
219     TEST_ASSERT(0);
220     return test;
221 }
222
223 int main(int argc, char **argv)
224 {
225     int ret = 0;
226
227     struct test_curve *tc;
228     for (tc = test_curves; tc->nid; tc++) {
229         ret |= parameter_test(tc);
230     }
231
232     if (ret)
233         printf(cDRED "= Some tests FAILED!" cNORM "\n");
234     else
235         printf(cDGREEN "= All tests passed!" cNORM "\n");
236     return ret;
237 }