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