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