]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_ec_keyx.c
openssl 1.1.0 compatibility
[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, int dgst_nid)
24 {
25     unsigned char *databuf = NULL, *hashbuf = NULL;
26     BIGNUM *UKM = NULL, *p = NULL, *order = NULL, *X = NULL, *Y = NULL;
27     const BIGNUM *key = EC_KEY_get0_private_key(priv_key);
28     EC_POINT *pnt = EC_POINT_new(EC_KEY_get0_group(priv_key));
29     int i;
30     BN_CTX *ctx = BN_CTX_new();
31     EVP_MD_CTX *mdctx = NULL;
32     const EVP_MD *md = NULL;
33     int effective_dgst_nid = (dgst_nid == NID_id_GostR3411_2012_512) ?
34         NID_id_GostR3411_2012_256 : dgst_nid;
35     int buf_len = (dgst_nid == NID_id_GostR3411_2012_512) ? 128 : 64,
36         half_len = buf_len >> 1;
37
38     if (!ctx) {
39         GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
40         return 0;
41     }
42     BN_CTX_start(ctx);
43
44     databuf = OPENSSL_malloc(buf_len);
45     hashbuf = OPENSSL_malloc(buf_len);
46     if (!databuf || !hashbuf) {
47         GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
48         goto err;
49     }
50
51     md = EVP_get_digestbynid(effective_dgst_nid);
52     if (!md) {
53         GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_INVALID_DIGEST_TYPE);
54         goto err;
55     }
56
57     UKM = hashsum2bn(ukm, 8);
58     p = BN_CTX_get(ctx);
59     order = BN_CTX_get(ctx);
60     X = BN_CTX_get(ctx);
61     Y = BN_CTX_get(ctx);
62     EC_GROUP_get_order(EC_KEY_get0_group(priv_key), order, ctx);
63     BN_mod_mul(p, key, UKM, order, ctx);
64     if (!EC_POINT_mul
65         (EC_KEY_get0_group(priv_key), pnt, NULL, pub_key, p, ctx)) {
66         GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_ERROR_POINT_MUL);
67         goto err;
68     }
69     EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(priv_key),
70                                         pnt, X, Y, ctx);
71     /*
72      * Serialize elliptic curve point same way as we do it when saving key
73      */
74     store_bignum(Y, databuf, half_len);
75     store_bignum(X, databuf + half_len, half_len);
76     /* And reverse byte order of whole buffer */
77     for (i = 0; i < buf_len; i++) {
78         hashbuf[buf_len - 1 - i] = databuf[i];
79     }
80     EVP_MD_CTX_init(mdctx);
81     EVP_DigestInit_ex(mdctx, md, NULL);
82     EVP_DigestUpdate(mdctx, hashbuf, buf_len);
83     EVP_DigestFinal_ex(mdctx, shared_key, NULL);
84     EVP_MD_CTX_free(mdctx);
85  err:
86     BN_free(UKM);
87     BN_CTX_end(ctx);
88     BN_CTX_free(ctx);
89     EC_POINT_free(pnt);
90     if (databuf)
91         OPENSSL_free(databuf);
92     if (hashbuf)
93         OPENSSL_free(hashbuf);
94
95     return 32;
96 }
97
98 /*
99  * EVP_PKEY_METHOD callback derive.
100  * Implements VKO R 34.10-2001/2012 algorithms
101  */
102 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
103 {
104     /*
105      * Public key of peer in the ctx field peerkey
106      * Our private key in the ctx pkey
107      * ukm is in the algorithm specific context data
108      */
109     EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx);
110     EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx);
111     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
112     int dgst_nid = NID_undef;
113
114     if (!data || !data->shared_ukm) {
115         GOSTerr(GOST_F_PKEY_GOST_EC_DERIVE, GOST_R_UKM_NOT_SET);
116         return 0;
117     }
118
119     if (key == NULL) {
120         *keylen = 32;
121         return 32;
122     }
123
124     EVP_PKEY_get_default_digest_nid(my_key, &dgst_nid);
125
126     *keylen =
127         VKO_compute_key(key, 32,
128                         EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
129                         (EC_KEY *)EVP_PKEY_get0(my_key), data->shared_ukm,
130                         dgst_nid);
131     return (*keylen) ? 1 : 0;
132 }
133
134 /*
135  * EVP_PKEY_METHOD callback encrypt
136  * Implementation of GOST2001 key transport, cryptocom variation
137  */
138 /*
139  * Generates ephemeral key based on pubk algorithm computes shared key using
140  * VKO and returns filled up GOST_KEY_TRANSPORT structure
141  */
142
143 /*
144  * EVP_PKEY_METHOD callback encrypt
145  * Implementation of GOST2001 key transport, cryptopo variation
146  */
147
148 int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
149                            size_t *out_len, const unsigned char *key,
150                            size_t key_len)
151 {
152     GOST_KEY_TRANSPORT *gkt = NULL;
153     EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
154     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
155     int pkey_nid = EVP_PKEY_base_id(pubk);
156     ASN1_OBJECT *crypt_params_obj = (pkey_nid == NID_id_GostR3410_2001) ?
157         OBJ_nid2obj(NID_id_Gost28147_89_CryptoPro_A_ParamSet) :
158         OBJ_nid2obj(NID_id_tc26_gost_28147_param_Z);
159     const struct gost_cipher_info *param =
160         get_encryption_params(crypt_params_obj);
161     unsigned char ukm[8], shared_key[32], crypted_key[44];
162     int ret = 0;
163     int key_is_ephemeral = 1;
164     gost_ctx cctx;
165     EVP_PKEY *sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
166     if (data->shared_ukm) {
167         memcpy(ukm, data->shared_ukm, 8);
168     } else if (out) {
169
170         if (RAND_bytes(ukm, 8) <= 0) {
171             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_RNG_ERROR);
172             return 0;
173         }
174     }
175     /* Check for private key in the peer_key of context */
176     if (sec_key) {
177         key_is_ephemeral = 0;
178         if (!gost_get0_priv_key(sec_key)) {
179             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
180                     GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
181             goto err;
182         }
183     } else {
184         key_is_ephemeral = 1;
185         if (out) {
186             sec_key = EVP_PKEY_new();
187             EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new());
188             EVP_PKEY_copy_parameters(sec_key, pubk);
189             if (!gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
190                 goto err;
191             }
192         }
193     }
194     if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS)
195         && param == gost_cipher_list) {
196         param = gost_cipher_list;
197     }
198     if (out) {
199         int dgst_nid = NID_undef;
200         EVP_PKEY_get_default_digest_nid(pubk, &dgst_nid);
201
202         if (!VKO_compute_key(shared_key, 32,
203                              EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
204                              EVP_PKEY_get0(sec_key), ukm, dgst_nid)) {
205             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
206                     GOST_R_ERROR_COMPUTING_SHARED_KEY);
207             goto err;
208         }
209         gost_init(&cctx, param->sblock);
210         keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
211     }
212     gkt = GOST_KEY_TRANSPORT_new();
213     if (!gkt) {
214         goto err;
215     }
216     if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
217         goto err;
218     }
219     if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
220         goto err;
221     }
222     if (!ASN1_OCTET_STRING_set
223         (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
224         goto err;
225     }
226     if (key_is_ephemeral) {
227         if (!X509_PUBKEY_set
228             (&gkt->key_agreement_info->ephem_key, out ? sec_key : pubk)) {
229             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
230                     GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
231             goto err;
232         }
233     }
234     ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
235     gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
236     if (key_is_ephemeral)
237         EVP_PKEY_free(sec_key);
238     if (!key_is_ephemeral) {
239         /* Set control "public key from client certificate used" */
240         if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
241             <= 0) {
242             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
243             goto err;
244         }
245     }
246     if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL)) > 0)
247         ret = 1;
248     GOST_KEY_TRANSPORT_free(gkt);
249     return ret;
250  err:
251     if (key_is_ephemeral)
252         EVP_PKEY_free(sec_key);
253     GOST_KEY_TRANSPORT_free(gkt);
254     return -1;
255 }
256
257 /*
258  * EVP_PKEY_METHOD callback decrypt
259  * Implementation of GOST2001 key transport, cryptopo variation
260  */
261 int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
262                            size_t *key_len, const unsigned char *in,
263                            size_t in_len)
264 {
265     const unsigned char *p = in;
266     EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
267     GOST_KEY_TRANSPORT *gkt = NULL;
268     int ret = 0;
269     unsigned char wrappedKey[44];
270     unsigned char sharedKey[32];
271     gost_ctx ctx;
272     const struct gost_cipher_info *param = NULL;
273     EVP_PKEY *eph_key = NULL, *peerkey = NULL;
274     int dgst_nid = NID_undef;
275
276     if (!key) {
277         *key_len = 32;
278         return 1;
279     }
280     gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
281     if (!gkt) {
282         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
283                 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
284         return -1;
285     }
286
287     /* If key transport structure contains public key, use it */
288     eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
289     if (eph_key) {
290         if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
291             GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
292                     GOST_R_INCOMPATIBLE_PEER_KEY);
293             goto err;
294         }
295     } else {
296         /* Set control "public key from client certificate used" */
297         if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
298             <= 0) {
299             GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
300             goto err;
301         }
302     }
303     peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
304     if (!peerkey) {
305         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_NO_PEER_KEY);
306         goto err;
307     }
308
309     param = get_encryption_params(gkt->key_agreement_info->cipher);
310     if (!param) {
311         goto err;
312     }
313
314     gost_init(&ctx, param->sblock);
315     OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
316     memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
317     OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
318     memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
319     OPENSSL_assert(gkt->key_info->imit->length == 4);
320     memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
321
322     EVP_PKEY_get_default_digest_nid(priv, &dgst_nid);
323     if (!VKO_compute_key(sharedKey, 32,
324                          EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
325                          EVP_PKEY_get0(priv), wrappedKey, dgst_nid)) {
326         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
327                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
328         goto err;
329     }
330     if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
331         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
332                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
333         goto err;
334     }
335
336     ret = 1;
337  err:
338     EVP_PKEY_free(eph_key);
339     GOST_KEY_TRANSPORT_free(gkt);
340     return ret;
341 }