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