2 * Test GOST 34.10 Sign/Verify operation for every curve parameter
4 * Copyright (C) 2019 vt@altlinux.org. All Rights Reserved.
6 * Contents licensed under the terms of the OpenSSL license
7 * See https://www.openssl.org/source/license.html for details
10 #include "e_gost_err.h"
12 #include <openssl/evp.h>
13 #include <openssl/rand.h>
14 #include <openssl/err.h>
15 #include <openssl/asn1.h>
16 #include <openssl/obj_mac.h>
17 #include <openssl/ec.h>
18 #include <openssl/bn.h>
19 #include <openssl/store.h>
23 #define T(e) ({ if (!(e)) { \
24 ERR_print_errors_fp(stderr); \
25 OpenSSLDie(__FILE__, __LINE__, #e); \
28 #define TE(e) ({ if (!(e)) { \
29 ERR_print_errors_fp(stderr); \
30 fprintf(stderr, "Error at %s:%d %s\n", __FILE__, __LINE__, #e); \
35 #define cRED "\033[1;31m"
36 #define cDRED "\033[0;31m"
37 #define cGREEN "\033[1;32m"
38 #define cDGREEN "\033[0;32m"
39 #define cBLUE "\033[1;34m"
40 #define cDBLUE "\033[0;34m"
41 #define cCYAN "\033[1;36m"
42 #define cNORM "\033[m"
43 #define TEST_ASSERT(e) {if ((test = (e))) \
44 printf(cRED " Test FAILED\n" cNORM); \
46 printf(cGREEN " Test passed\n" cNORM);}
55 #define D(x,y,z) { .name = #x, .nid = x, .bits = y, .paramset = z }
56 static struct test_sign test_signs[] = {
57 D(NID_id_GostR3410_2001_CryptoPro_A_ParamSet, 256, "A"),
58 D(NID_id_GostR3410_2001_CryptoPro_B_ParamSet, 256, "B"),
59 D(NID_id_GostR3410_2001_CryptoPro_C_ParamSet, 256, "C"),
60 D(NID_id_tc26_gost_3410_2012_256_paramSetA, 256, "TCA"),
61 D(NID_id_tc26_gost_3410_2012_256_paramSetB, 256, "TCB"),
62 D(NID_id_tc26_gost_3410_2012_256_paramSetC, 256, "TCC"),
63 D(NID_id_tc26_gost_3410_2012_256_paramSetD, 256, "TCD"),
64 D(NID_id_tc26_gost_3410_2012_512_paramSetA, 512, "A"),
65 D(NID_id_tc26_gost_3410_2012_512_paramSetB, 512, "B"),
66 D(NID_id_tc26_gost_3410_2012_512_paramSetC, 512, "C"),
71 static void hexdump(const void *ptr, size_t len)
73 const unsigned char *p = ptr;
76 for (i = 0; i < len; i += j) {
77 for (j = 0; j < 16 && i + j < len; j++)
78 printf("%s %02x", j? "" : "\n", p[i + j]);
83 static void print_test_tf(int err, int val, const char *t, const char *f)
86 printf(cGREEN "%s\n" cNORM, t);
88 printf(cRED "%s [%d]\n" cNORM, f, val);
91 static void print_test_result(int err)
94 printf(cGREEN "success\n" cNORM);
96 printf(cRED "failure\n" cNORM);
98 ERR_print_errors_fp(stderr);
101 static int test_sign(struct test_sign *t)
104 size_t len = t->bits / 8;
106 printf(cBLUE "Test %s:\n" cNORM, t->name);
108 /* Signature type from size. */
110 const char *algname = NULL;
113 type = NID_id_GostR3410_2012_256;
114 algname = "gost2012_256";
117 type = NID_id_GostR3410_2012_512;
118 algname = "gost2012_512";
123 T(pkey = EVP_PKEY_new());
124 TE(EVP_PKEY_set_type(pkey, type));
126 T(ctx = EVP_PKEY_CTX_new(pkey, NULL));
127 T(EVP_PKEY_keygen_init(ctx));
128 T(EVP_PKEY_CTX_ctrl(ctx, type, -1, EVP_PKEY_CTRL_GOST_PARAMSET, t->nid, NULL));
129 EVP_PKEY *priv_key = NULL;
130 err = EVP_PKEY_keygen(ctx, &priv_key);
131 printf("\tEVP_PKEY_keygen:\t");
132 print_test_result(err);
133 EVP_PKEY_CTX_free(ctx);
138 /* Convert to PEM and back. */
140 T(bp = BIO_new(BIO_s_secmem()));
141 T(PEM_write_bio_PrivateKey(bp, priv_key, NULL, NULL, 0, NULL, NULL));
143 T(PEM_read_bio_PrivateKey(bp, &pkey, NULL, NULL));
144 printf("\tPEM_read_bio_PrivateKey:");
145 /* Yes, it compares only public part. */
146 err = !EVP_PKEY_cmp(priv_key, pkey);
147 print_test_result(!err);
151 /* Convert to DER and back, using _PrivateKey_bio API. */
153 T(i2d_PrivateKey_bio(bp, priv_key));
154 T(d2i_PrivateKey_bio(bp, &pkey));
155 printf("\td2i_PrivateKey_bio:\t");
156 err = !EVP_PKEY_cmp(priv_key, pkey);
157 print_test_result(!err);
161 #if OPENSSL_VERSION_MAJOR >= 3
162 /* Try d2i_PrivateKey_ex_bio, added in 3.0. */
164 T(i2d_PrivateKey_bio(bp, priv_key));
165 T(d2i_PrivateKey_ex_bio(bp, &pkey, NULL, NULL));
166 printf("\td2i_PrivateKey_ex_bio:\t");
167 err = !EVP_PKEY_cmp(priv_key, pkey);
168 print_test_result(!err);
173 /* Convert to DER and back, using OSSL_STORE API. */
175 T(i2d_PrivateKey_bio(bp, priv_key));
176 printf("\tOSSL_STORE_attach:\t");
180 T(cts = OSSL_STORE_attach(bp, "file", NULL, NULL, NULL, NULL, NULL, NULL));
182 OSSL_STORE_INFO *info = OSSL_STORE_load(cts);
184 ERR_print_errors_fp(stderr);
185 T(OSSL_STORE_eof(cts));
188 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PKEY) {
189 T((pkey = OSSL_STORE_INFO_get1_PKEY(info)));
191 OSSL_STORE_INFO_free(info);
193 OSSL_STORE_close(cts);
195 err = !EVP_PKEY_cmp(priv_key, pkey);
196 print_test_result(!err);
200 printf(cCYAN "skipped\n" cNORM);
203 /* Convert to DER and back, using memory API. */
204 unsigned char *kptr = NULL;
206 T(klen = i2d_PrivateKey(priv_key, &kptr));
207 const unsigned char *tptr = kptr; /* will be moved by d2i_PrivateKey */
209 T(d2i_PrivateKey(type, &pkey, &tptr, klen));
210 printf("\td2i_PrivateKey:\t\t");
211 err = !EVP_PKEY_cmp(priv_key, pkey);
212 print_test_result(!err);
217 /* Create another key using string interface. */
219 T(key1 = EVP_PKEY_new());
220 T(EVP_PKEY_set_type_str(key1, algname, strlen(algname)));
222 T(ctx1 = EVP_PKEY_CTX_new(key1, NULL));
223 T(EVP_PKEY_keygen_init(ctx1));
224 T(EVP_PKEY_CTX_ctrl_str(ctx1, "paramset", t->paramset));
225 EVP_PKEY *key2 = NULL;
226 err = EVP_PKEY_keygen(ctx1, &key2);
227 printf("\tEVP_PKEY_*_str:\t\t");
228 print_test_result(err);
231 /* Check if key type and curve_name match expected values. */
232 int id = EVP_PKEY_id(key2);
234 printf("\tEVP_PKEY_id (%d):\t", type);
235 print_test_tf(err, id, "match", "mismatch");
238 const EC_KEY *ec = EVP_PKEY_get0(key2);
239 const EC_GROUP *group = EC_KEY_get0_group(ec);
240 int curve_name = EC_GROUP_get_curve_name(group);
241 err = curve_name == t->nid;
242 printf("\tcurve_name (%u):\t", t->nid);
243 print_test_tf(err, curve_name, "match", "mismatch");
246 /* Compare both keys.
247 * Parameters should match, public keys should mismatch.
249 err = EVP_PKEY_cmp_parameters(priv_key, key2);
250 printf("\tEVP_PKEY_cmp_parameters:");
251 print_test_tf(err, err, "success", "failure");
254 err = EVP_PKEY_cmp(priv_key, key2);
255 err = (err < 0) ? err : !err;
256 printf("\tEVP_PKEY_cmp:\t\t");
257 print_test_tf(err, err, "differ (good)", "equal (error)");
259 EVP_PKEY_CTX_free(ctx1);
263 * Prepare for sign testing.
265 size_t siglen = EVP_PKEY_size(priv_key);
267 T(sig = OPENSSL_malloc(siglen));
269 T(hash = OPENSSL_zalloc(len));
270 T(ctx = EVP_PKEY_CTX_new(priv_key, NULL));
273 T(EVP_PKEY_sign_init(ctx));
274 err = EVP_PKEY_sign(ctx, sig, &siglen, hash, len);
275 printf("\tEVP_PKEY_sign:\t\t");
276 print_test_result(err);
279 /* Non-determinism test.
280 * Check that different signatures for the same data
283 T(sig2 = OPENSSL_malloc(siglen));
284 TE(EVP_PKEY_sign(ctx, sig2, &siglen, hash, len) == 1);
285 printf("\tNon-determinism:\t");
286 err = !!memcmp(sig, sig2, siglen);
287 print_test_result(err);
292 T(EVP_PKEY_verify_init(ctx));
294 err = EVP_PKEY_verify(ctx, sig, siglen, hash, len);
295 printf("\tEVP_PKEY_verify:\t");
296 print_test_result(err);
299 /* False positive Verify. */
300 T(EVP_PKEY_verify_init(ctx));
302 err = EVP_PKEY_verify(ctx, sig, siglen, hash, len);
303 err = (err < 0) ? err : !err;
304 printf("\tFalse positive test:\t");
305 print_test_result(err);
308 EVP_PKEY_CTX_free(ctx);
311 EVP_PKEY_free(priv_key);
317 int main(int argc, char **argv)
321 setenv("OPENSSL_ENGINES", ENGINE_DIR, 0);
322 OPENSSL_add_all_algorithms_conf();
323 ERR_load_crypto_strings();
325 T(eng = ENGINE_by_id("gost"));
327 T(ENGINE_set_default(eng, ENGINE_METHOD_ALL));
329 struct test_sign *sp;
330 for (sp = test_signs; sp->name; sp++)
331 ret |= test_sign(sp);
337 printf(cDRED "= Some tests FAILED!\n" cNORM);
339 printf(cDGREEN "= All tests passed!\n" cNORM);