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