2 * Test derive operations
4 * Copyright (C) 2020 Vitaly Chikunov <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 <openssl/ec.h>
11 #include <openssl/engine.h>
12 #include <openssl/err.h>
13 #include <openssl/evp.h>
20 ERR_print_errors_fp(stderr); \
21 OpenSSLDie(__FILE__, __LINE__, #e); \
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 cCYAN "\033[1;36m"
32 #define cNORM "\033[m"
33 #define TEST_ASSERT(e) {if ((test = (e))) \
34 printf(cRED " Test FAILED\n" cNORM); \
36 printf(cGREEN " Test passed\n" cNORM);}
39 # define OSSL_NELEM(x) (sizeof(x)/sizeof((x)[0]))
42 const char party_a_priv[] =
43 "\xc9\x90\xec\xd9\x72\xfc\xe8\x4e\xc4\xdb\x02\x27\x78\xf5\x0f\xca"
44 "\xc7\x26\xf4\x67\x08\x38\x4b\x8d\x45\x83\x04\x96\x2d\x71\x47\xf8"
45 "\xc2\xdb\x41\xce\xf2\x2c\x90\xb1\x02\xf2\x96\x84\x04\xf9\xb9\xbe"
46 "\x6d\x47\xc7\x96\x92\xd8\x18\x26\xb3\x2b\x8d\xac\xa4\x3c\xb6\x67";
47 const char party_a_pub[] =
48 "\xaa\xb0\xed\xa4\xab\xff\x21\x20\x8d\x18\x79\x9f\xb9\xa8\x55\x66"
49 "\x54\xba\x78\x30\x70\xeb\xa1\x0c\xb9\xab\xb2\x53\xec\x56\xdc\xf5"
50 "\xd3\xcc\xba\x61\x92\xe4\x64\xe6\xe5\xbc\xb6\xde\xa1\x37\x79\x2f"
51 "\x24\x31\xf6\xc8\x97\xeb\x1b\x3c\x0c\xc1\x43\x27\xb1\xad\xc0\xa7"
52 "\x91\x46\x13\xa3\x07\x4e\x36\x3a\xed\xb2\x04\xd3\x8d\x35\x63\x97"
53 "\x1b\xd8\x75\x8e\x87\x8c\x9d\xb1\x14\x03\x72\x1b\x48\x00\x2d\x38"
54 "\x46\x1f\x92\x47\x2d\x40\xea\x92\xf9\x95\x8c\x0f\xfa\x4c\x93\x75"
55 "\x64\x01\xb9\x7f\x89\xfd\xbe\x0b\x5e\x46\xe4\xa4\x63\x1c\xdb\x5a";
56 const char party_b_priv[] =
57 "\x48\xc8\x59\xf7\xb6\xf1\x15\x85\x88\x7c\xc0\x5e\xc6\xef\x13\x90"
58 "\xcf\xea\x73\x9b\x1a\x18\xc0\xd4\x66\x22\x93\xef\x63\xb7\x9e\x3b"
59 "\x80\x14\x07\x0b\x44\x91\x85\x90\xb4\xb9\x96\xac\xfe\xa4\xed\xfb"
60 "\xbb\xcc\xcc\x8c\x06\xed\xd8\xbf\x5b\xda\x92\xa5\x13\x92\xd0\xdb";
61 const char party_b_pub[] =
62 "\x19\x2f\xe1\x83\xb9\x71\x3a\x07\x72\x53\xc7\x2c\x87\x35\xde\x2e"
63 "\xa4\x2a\x3d\xbc\x66\xea\x31\x78\x38\xb6\x5f\xa3\x25\x23\xcd\x5e"
64 "\xfc\xa9\x74\xed\xa7\xc8\x63\xf4\x95\x4d\x11\x47\xf1\xf2\xb2\x5c"
65 "\x39\x5f\xce\x1c\x12\x91\x75\xe8\x76\xd1\x32\xe9\x4e\xd5\xa6\x51"
66 "\x04\x88\x3b\x41\x4c\x9b\x59\x2e\xc4\xdc\x84\x82\x6f\x07\xd0\xb6"
67 "\xd9\x00\x6d\xda\x17\x6c\xe4\x8c\x39\x1e\x3f\x97\xd1\x02\xe0\x3b"
68 "\xb5\x98\xbf\x13\x2a\x22\x8a\x45\xf7\x20\x1a\xba\x08\xfc\x52\x4a"
69 "\x2d\x77\xe4\x3a\x36\x2a\xb0\x22\xad\x40\x28\xf7\x5b\xde\x3b\x79";
72 const char *descr; /* Source of test vector. */
73 int dgst_nid; /* VKO mode */
75 int param_nid; /* Curve id. */
76 const char *ukm; /* User Keying Material. */
78 const char *key_a_priv;
79 const char *key_a_pub;
80 const char *key_b_priv;
81 const char *key_b_pub;
82 const char *kek; /* Key Encryption Key. Output. */
86 .descr = "VKO_GOSTR3410_2012_256 from R 50.1.113-2016 A.9 (p.18)",
87 .dgst_nid = NID_id_GostR3411_2012_256,
88 .key_nid = NID_id_GostR3410_2012_512,
89 .param_nid = NID_id_tc26_gost_3410_2012_512_paramSetA,
90 .key_a_priv = party_a_priv,
91 .key_a_pub = party_a_pub,
92 .key_b_priv = party_b_priv,
93 .key_b_pub = party_b_pub,
94 .ukm = "\x1d\x80\x60\x3c\x85\x44\xc7\x27",
97 "\xc9\xa9\xa7\x73\x20\xe2\xcc\x55\x9e\xd7\x2d\xce\x6f\x47\xe2\x19"
98 "\x2c\xce\xa9\x5f\xa6\x48\x67\x05\x82\xc0\x54\xc0\xef\x36\xc2\x21",
102 .descr = "VKO_GOSTR3410_2012_512 from R 50.1.113-2016 A.10 (p.19)",
103 .dgst_nid = NID_id_GostR3411_2012_512,
104 .key_nid = NID_id_GostR3410_2012_512,
105 .param_nid = NID_id_tc26_gost_3410_2012_512_paramSetA,
106 .key_a_priv = party_a_priv,
107 .key_a_pub = party_a_pub,
108 .key_b_priv = party_b_priv,
109 .key_b_pub = party_b_pub,
110 .ukm = "\x1d\x80\x60\x3c\x85\x44\xc7\x27",
113 "\x79\xf0\x02\xa9\x69\x40\xce\x7b\xde\x32\x59\xa5\x2e\x01\x52\x97"
114 "\xad\xaa\xd8\x45\x97\xa0\xd2\x05\xb5\x0e\x3e\x17\x19\xf9\x7b\xfa"
115 "\x7e\xe1\xd2\x66\x1f\xa9\x97\x9a\x5a\xa2\x35\xb5\x58\xa7\xe6\xd9"
116 "\xf8\x8f\x98\x2d\xd6\x3f\xc3\x5a\x8e\xc0\xdd\x5e\x24\x2d\x3b\xdf",
121 static EVP_PKEY *load_private_key(int key_nid, int param_nid, const char *pk,
126 T(ctx = EVP_PKEY_CTX_new_id(key_nid, NULL));
127 T(EVP_PKEY_paramgen_init(ctx));
128 T(EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GOST_PARAMSET, param_nid,
130 EVP_PKEY *key = NULL;
131 T((EVP_PKEY_paramgen(ctx, &key)) == 1);
132 EVP_PKEY_CTX_free(ctx);
135 T(ec = EVP_PKEY_get0(key));
137 const int len = EVP_PKEY_bits(key) / 8;
139 T(bc = BN_CTX_secure_new());
141 const EC_GROUP *group = EC_KEY_get0_group(ec);
142 EC_POINT *pkey = NULL;
144 /* Read private key. */
146 T(d = BN_lebin2bn((const unsigned char *)pk, len, NULL));
147 T(EC_KEY_set_private_key(ec, d));
149 /* Compute public key. */
150 T(pkey = EC_POINT_new(group));
151 T(EC_POINT_mul(group, pkey, d, NULL, NULL, bc));
153 T(EC_KEY_set_public_key(ec, pkey));
155 /* Read public key. */
157 T(x = BN_lebin2bn((const unsigned char *)pub, len, NULL));
158 T(y = BN_lebin2bn((const unsigned char *)pub + len, len, NULL));
159 EC_POINT *xy = EC_POINT_new(group);
160 T(EC_POINT_set_affine_coordinates(group, xy, x, y, bc));
163 T(EC_KEY_set_public_key(ec, xy));
168 BIO *bp = BIO_new_fd(1, BIO_NOCLOSE);
170 PEM_write_bio_PrivateKey(bp, key, NULL, NULL, 0, NULL, NULL);
171 PEM_write_bio_PUBKEY(bp, key);
175 /* Verify public key. */
178 T(x = BN_lebin2bn((const unsigned char *)pub, len, NULL));
179 T(y = BN_lebin2bn((const unsigned char *)pub + len, len, NULL));
180 EC_POINT *xy = EC_POINT_new(group);
181 T(EC_POINT_set_affine_coordinates(group, xy, x, y, bc));
184 if (EC_POINT_cmp(group, pkey, xy, bc) == 0)
185 printf("Public key %08x matches private key %08x\n",
186 *(int *)pub, *(int *)pk);
188 printf(cRED "Public key mismatch!\n" cNORM);
200 int test_derive(struct test_derive *t, const char *name)
204 printf(cBLUE "Test %s" cNORM " for %s\n", t->descr, name);
206 EVP_PKEY *keyA = load_private_key(t->key_nid, t->param_nid,
207 t->key_a_priv, t->key_a_pub);
208 EVP_PKEY *keyB = load_private_key(t->key_nid, t->param_nid,
212 T(ctx = EVP_PKEY_CTX_new(keyA, NULL));
213 T(EVP_PKEY_derive_init(ctx));
214 T(EVP_PKEY_derive_set_peer(ctx, keyB));
216 T(EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_SET_VKO,
218 T(EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_SET_IV,
219 t->ukm_len, (unsigned char *)t->ukm));
223 T(EVP_PKEY_derive(ctx, NULL, &skeylen));
224 T(skey = OPENSSL_malloc(skeylen));
226 T(EVP_PKEY_derive(ctx, skey, &skeylen));
228 BIO_dump_fp(stdout, skey, skeylen);
231 EVP_PKEY_CTX_free(ctx);
235 if (t->kek_len == skeylen && memcmp(t->kek, skey, skeylen) == 0)
236 printf(cGREEN "KEK match" cNORM "\n");
238 printf(cRED "KEK mismatch" cNORM "\n");
245 int test_derive_pair(struct test_derive *t)
248 struct test_derive tt = *t;
250 tt.key_b_priv = NULL;
251 ret |= test_derive(&tt, "A");
252 /* Test swapped keys. */
253 tt.key_a_priv = t->key_b_priv;
254 tt.key_a_pub = t->key_b_pub;
255 tt.key_b_priv = NULL;
256 tt.key_b_pub = t->key_a_pub;
257 ret |= test_derive(&tt, "B");
261 int main(int argc, char **argv)
265 setenv("OPENSSL_ENGINES", ENGINE_DIR, 0);
266 OPENSSL_add_all_algorithms_conf();
267 ERR_load_crypto_strings();
269 T(eng = ENGINE_by_id("gost"));
271 T(ENGINE_set_default(eng, ENGINE_METHOD_ALL));
274 for (i = 0; i < OSSL_NELEM(derive_tests); i++)
275 ret |= test_derive_pair(&derive_tests[i]);
281 printf(cDRED "= Some tests FAILED!\n" cNORM);
283 printf(cDGREEN "= All tests passed!\n" cNORM);
286 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */