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