]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_ec_keyx.c
KDF TREE + test
[openssl-gost/engine.git] / gost_ec_keyx.c
1 /**********************************************************************
2  *                        gost_ec_keyx.c                              *
3  *             Copyright (c) 2005-2013 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *   VK0 34.10-2001 key exchange and GOST R 34.10-2001                *
7  *   based PKCS7/SMIME support                                        *
8  *          Requires OpenSSL 0.9.9 for compilation                    *
9  **********************************************************************/
10 #include <openssl/evp.h>
11 #include <openssl/err.h>
12 #include <openssl/rand.h>
13 #include <string.h>
14 #include <openssl/objects.h>
15 #include "gost89.h"
16 #include "e_gost_err.h"
17 #include "gost_keywrap.h"
18 #include "gost_lcl.h"
19
20 /* Implementation of CryptoPro VKO 34.10-2001/2012 algorithm */
21 static int VKO_compute_key(unsigned char *shared_key, size_t shared_key_size,
22                            const EC_POINT *pub_key, EC_KEY *priv_key,
23                            const unsigned char *ukm, size_t ukm_size,
24                            int vko_dgst_nid)
25 {
26     unsigned char *databuf = NULL;
27     BIGNUM *UKM = NULL, *p = NULL, *order = NULL, *X = NULL, *Y = NULL;
28     const BIGNUM *key = EC_KEY_get0_private_key(priv_key);
29     EC_POINT *pnt = EC_POINT_new(EC_KEY_get0_group(priv_key));
30     BN_CTX *ctx = BN_CTX_new();
31     EVP_MD_CTX *mdctx = NULL;
32     const EVP_MD *md = NULL;
33     int buf_len, half_len;
34     int ret = 0;
35
36     if (!ctx) {
37         GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
38         return 0;
39     }
40     BN_CTX_start(ctx);
41
42     md = EVP_get_digestbynid(vko_dgst_nid);
43     if (!md) {
44         GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_INVALID_DIGEST_TYPE);
45         goto err;
46     }
47
48     UKM = hashsum2bn(ukm, ukm_size);
49     p = BN_CTX_get(ctx);
50     order = BN_CTX_get(ctx);
51     X = BN_CTX_get(ctx);
52     Y = BN_CTX_get(ctx);
53     EC_GROUP_get_order(EC_KEY_get0_group(priv_key), order, ctx);
54     BN_mod_mul(p, key, UKM, order, ctx);
55     if (!EC_POINT_mul(EC_KEY_get0_group(priv_key), pnt, NULL, pub_key, p, ctx)) {
56         GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_ERROR_POINT_MUL);
57         goto err;
58     }
59     EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(priv_key),
60                                         pnt, X, Y, ctx);
61
62     half_len = BN_num_bytes(order);
63     buf_len = 2 * half_len;
64     databuf = OPENSSL_zalloc(buf_len);
65     if (!databuf) {
66         GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
67         goto err;
68     }
69
70     /*
71      * Serialize elliptic curve point same way as we do it when saving key
72      */
73     store_bignum(Y, databuf, half_len);
74     store_bignum(X, databuf + half_len, half_len);
75     /* And reverse byte order of whole buffer */
76     BUF_reverse(databuf, NULL, buf_len);
77
78     mdctx = EVP_MD_CTX_new();
79     if (!mdctx) {
80         GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
81         goto err;
82     }
83     EVP_MD_CTX_init(mdctx);
84     EVP_DigestInit_ex(mdctx, md, NULL);
85     EVP_DigestUpdate(mdctx, databuf, buf_len);
86     EVP_DigestFinal_ex(mdctx, shared_key, NULL);
87     ret = (EVP_MD_size(md) > 0) ? EVP_MD_size(md) : 0;
88
89  err:
90     BN_free(UKM);
91     BN_CTX_end(ctx);
92     BN_CTX_free(ctx);
93
94     EC_POINT_free(pnt);
95
96     EVP_MD_CTX_free(mdctx);
97
98     OPENSSL_free(databuf);
99
100     return ret;
101 }
102
103 /*
104  * keyout expected to be 64 bytes
105  * */
106 static int gost_keg(const unsigned char *ukm_source, int pkey_nid,
107                     const EC_POINT *pub_key, EC_KEY *priv_key,
108                     unsigned char *keyout)
109 {
110 /* Adjust UKM */
111     unsigned char real_ukm[16];
112     size_t keylen;
113
114     memset(real_ukm, 0, 16);
115     if (memcmp(ukm_source, real_ukm, 16) == 0)
116         real_ukm[15] = 1;
117     else
118         memcpy(real_ukm, ukm_source, 16);
119
120     switch (pkey_nid) {
121     case NID_id_GostR3410_2012_512:
122         keylen =
123             VKO_compute_key(keyout, 64, pub_key, priv_key, real_ukm, 16,
124                             NID_id_GostR3411_2012_512);
125         return (keylen) ? keylen : 0;
126         break;
127
128     case NID_id_GostR3410_2012_256:
129         {
130             unsigned char tmpkey[32];
131             keylen =
132                 VKO_compute_key(tmpkey, 32, pub_key, priv_key, real_ukm, 16,
133                                 NID_id_GostR3411_2012_256);
134
135             if (keylen == 0)
136                 return 0;
137
138             if (gost_kdftree2012_256
139                 (keyout, 64, tmpkey, 32, (const unsigned char *)"kdf tree", 8,
140                  ukm_source + 16, 8, 1) > 0)
141                 keylen = 64;
142             else
143                 keylen = 0;
144
145             OPENSSL_cleanse(tmpkey, 32);
146             return (keylen) ? keylen : 0;
147             break;
148         }
149     default:
150         return 0;
151     }
152 }
153
154 /*
155  * EVP_PKEY_METHOD callback derive.
156  * Implements VKO R 34.10-2001/2012 algorithms
157  */
158 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
159 {
160     /*
161      * Public key of peer in the ctx field peerkey
162      * Our private key in the ctx pkey
163      * ukm is in the algorithm specific context data
164      */
165     EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx);
166     EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx);
167     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
168     int dgst_nid = NID_undef;
169
170     if (!data || !data->shared_ukm) {
171         GOSTerr(GOST_F_PKEY_GOST_EC_DERIVE, GOST_R_UKM_NOT_SET);
172         return 0;
173     }
174     /*
175      * shared_ukm_size = 8 stands for pre-2018 cipher suites
176      * It means 32 bytes of key length, 8 byte UKM, 32-bytes hash
177      *
178      * shared_ukm_size = 32 stands for pre-2018 cipher suites
179      * It means 64 bytes of shared_key, 16 bytes of UKM and either
180      * 64 bytes of hash or 64 bytes of TLSTREE output
181      * */
182
183     switch (data->shared_ukm_size) {
184     case 8:
185         {
186             if (key == NULL) {
187                 *keylen = 32;
188                 return 1;
189             }
190
191             EVP_PKEY_get_default_digest_nid(my_key, &dgst_nid);
192             if (dgst_nid == NID_id_GostR3411_2012_512)
193                 dgst_nid = NID_id_GostR3411_2012_256;
194
195             *keylen =
196                 VKO_compute_key(key, 32,
197                                 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
198                                 (EC_KEY *)EVP_PKEY_get0(my_key),
199                                 data->shared_ukm, 8, dgst_nid);
200             return (*keylen) ? 1 : 0;
201         }
202         break;
203     case 32:
204         {
205             if (key == NULL) {
206                 *keylen = 64;
207                 return 1;
208             }
209
210             *keylen = gost_keg(data->shared_ukm, EVP_PKEY_id(my_key),
211                                EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
212                                (EC_KEY *)EVP_PKEY_get0(my_key), key);
213             return (*keylen) ? 1 : 0;
214         }
215
216         break;
217     default:
218         return 0;
219     }
220 }
221
222 /*
223  * Generates ephemeral key based on pubk algorithm computes shared key using
224  * VKO and returns filled up GOST_KEY_TRANSPORT structure
225  */
226
227 /*
228  * EVP_PKEY_METHOD callback encrypt
229  * Implementation of GOST2001/12 key transport, cryptopro variation
230  */
231
232 int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
233                            size_t *out_len, const unsigned char *key,
234                            size_t key_len)
235 {
236     GOST_KEY_TRANSPORT *gkt = NULL;
237     EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
238     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
239     int pkey_nid = EVP_PKEY_base_id(pubk);
240     ASN1_OBJECT *crypt_params_obj = (pkey_nid == NID_id_GostR3410_2001) ?
241         OBJ_nid2obj(NID_id_Gost28147_89_CryptoPro_A_ParamSet) :
242         OBJ_nid2obj(NID_id_tc26_gost_28147_param_Z);
243     const struct gost_cipher_info *param =
244         get_encryption_params(crypt_params_obj);
245     unsigned char ukm[8], shared_key[32], crypted_key[44];
246     int ret = 0;
247     int key_is_ephemeral = 1;
248     gost_ctx cctx;
249     EVP_PKEY *sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
250     if (data->shared_ukm) {
251         memcpy(ukm, data->shared_ukm, 8);
252     } else if (out) {
253
254         if (RAND_bytes(ukm, 8) <= 0) {
255             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_RNG_ERROR);
256             return 0;
257         }
258     }
259     /* Check for private key in the peer_key of context */
260     if (sec_key) {
261         key_is_ephemeral = 0;
262         if (!gost_get0_priv_key(sec_key)) {
263             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
264                     GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
265             goto err;
266         }
267     } else {
268         key_is_ephemeral = 1;
269         if (out) {
270             sec_key = EVP_PKEY_new();
271             if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
272                 || !EVP_PKEY_copy_parameters(sec_key, pubk)
273                 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
274                 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
275                         GOST_R_ERROR_COMPUTING_SHARED_KEY);
276                 goto err;
277             }
278         }
279     }
280     if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS)
281         && param == gost_cipher_list) {
282         param = gost_cipher_list;
283     }
284     if (out) {
285         int dgst_nid = NID_undef;
286         EVP_PKEY_get_default_digest_nid(pubk, &dgst_nid);
287         if (dgst_nid == NID_id_GostR3411_2012_512)
288             dgst_nid = NID_id_GostR3411_2012_256;
289
290         if (!VKO_compute_key(shared_key, 32,
291                              EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
292                              EVP_PKEY_get0(sec_key), ukm, 8, dgst_nid)) {
293             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
294                     GOST_R_ERROR_COMPUTING_SHARED_KEY);
295             goto err;
296         }
297         gost_init(&cctx, param->sblock);
298         keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
299     }
300     gkt = GOST_KEY_TRANSPORT_new();
301     if (!gkt) {
302         goto err;
303     }
304     if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
305         goto err;
306     }
307     if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
308         goto err;
309     }
310     if (!ASN1_OCTET_STRING_set
311         (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
312         goto err;
313     }
314     if (key_is_ephemeral) {
315         if (!X509_PUBKEY_set
316             (&gkt->key_agreement_info->ephem_key, out ? sec_key : pubk)) {
317             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
318                     GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
319             goto err;
320         }
321     }
322     ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
323     gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
324     if (key_is_ephemeral)
325         EVP_PKEY_free(sec_key);
326     if (!key_is_ephemeral) {
327         /* Set control "public key from client certificate used" */
328         if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
329             <= 0) {
330             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
331             goto err;
332         }
333     }
334     if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL)) > 0)
335         ret = 1;
336     GOST_KEY_TRANSPORT_free(gkt);
337     return ret;
338  err:
339     if (key_is_ephemeral)
340         EVP_PKEY_free(sec_key);
341     GOST_KEY_TRANSPORT_free(gkt);
342     return -1;
343 }
344
345 /*
346  * EVP_PKEY_METHOD callback decrypt
347  * Implementation of GOST2001 key transport, cryptopo variation
348  */
349 int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
350                            size_t *key_len, const unsigned char *in,
351                            size_t in_len)
352 {
353     const unsigned char *p = in;
354     EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
355     GOST_KEY_TRANSPORT *gkt = NULL;
356     int ret = 0;
357     unsigned char wrappedKey[44];
358     unsigned char sharedKey[32];
359     gost_ctx ctx;
360     const struct gost_cipher_info *param = NULL;
361     EVP_PKEY *eph_key = NULL, *peerkey = NULL;
362     int dgst_nid = NID_undef;
363
364     if (!key) {
365         *key_len = 32;
366         return 1;
367     }
368     gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
369     if (!gkt) {
370         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
371                 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
372         return -1;
373     }
374
375     /* If key transport structure contains public key, use it */
376     eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
377     if (eph_key) {
378         if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
379             GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
380                     GOST_R_INCOMPATIBLE_PEER_KEY);
381             goto err;
382         }
383     } else {
384         /* Set control "public key from client certificate used" */
385         if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
386             <= 0) {
387             GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
388             goto err;
389         }
390     }
391     peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
392     if (!peerkey) {
393         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_NO_PEER_KEY);
394         goto err;
395     }
396
397     param = get_encryption_params(gkt->key_agreement_info->cipher);
398     if (!param) {
399         goto err;
400     }
401
402     gost_init(&ctx, param->sblock);
403     OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
404     memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
405     OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
406     memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
407     OPENSSL_assert(gkt->key_info->imit->length == 4);
408     memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
409
410     EVP_PKEY_get_default_digest_nid(priv, &dgst_nid);
411     if (dgst_nid == NID_id_GostR3411_2012_512)
412         dgst_nid = NID_id_GostR3411_2012_256;
413
414     if (!VKO_compute_key(sharedKey, 32,
415                          EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
416                          EVP_PKEY_get0(priv), wrappedKey, 8, dgst_nid)) {
417         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
418                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
419         goto err;
420     }
421     if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
422         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
423                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
424         goto err;
425     }
426
427     ret = 1;
428  err:
429     EVP_PKEY_free(eph_key);
430     GOST_KEY_TRANSPORT_free(gkt);
431     return ret;
432 }