]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_ec_keyx.c
Indentation
[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;
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     BN_CTX *ctx = BN_CTX_new();
30     EVP_MD_CTX *mdctx = NULL;
31     const EVP_MD *md = NULL;
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     int ret = 0;
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_zalloc(buf_len);
45     if (!databuf) {
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     BUF_reverse(databuf, NULL, buf_len);
76
77     mdctx = EVP_MD_CTX_new();
78     if (!mdctx) {
79         GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
80         goto err;
81     }
82     EVP_MD_CTX_init(mdctx);
83     EVP_DigestInit_ex(mdctx, md, NULL);
84     EVP_DigestUpdate(mdctx, databuf, buf_len);
85     EVP_DigestFinal_ex(mdctx, shared_key, NULL);
86     ret = 32;
87
88  err:
89     BN_free(UKM);
90     BN_CTX_end(ctx);
91     BN_CTX_free(ctx);
92
93     EC_POINT_free(pnt);
94
95     EVP_MD_CTX_free(mdctx);
96
97     OPENSSL_free(databuf);
98
99     return ret;
100 }
101
102 /*
103  * EVP_PKEY_METHOD callback derive.
104  * Implements VKO R 34.10-2001/2012 algorithms
105  */
106 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
107 {
108     /*
109      * Public key of peer in the ctx field peerkey
110      * Our private key in the ctx pkey
111      * ukm is in the algorithm specific context data
112      */
113     EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx);
114     EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx);
115     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
116     int dgst_nid = NID_undef;
117
118     if (!data || !data->shared_ukm) {
119         GOSTerr(GOST_F_PKEY_GOST_EC_DERIVE, GOST_R_UKM_NOT_SET);
120         return 0;
121     }
122
123     if (key == NULL) {
124         *keylen = 32;
125         return 1;
126     }
127
128     EVP_PKEY_get_default_digest_nid(my_key, &dgst_nid);
129
130     *keylen =
131         VKO_compute_key(key, 32,
132                         EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
133                         (EC_KEY *)EVP_PKEY_get0(my_key), data->shared_ukm,
134                         dgst_nid);
135     return (*keylen) ? 1 : 0;
136 }
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             if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
188                 || !EVP_PKEY_copy_parameters(sec_key, pubk)
189                 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
190                 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
191                         GOST_R_ERROR_COMPUTING_SHARED_KEY);
192                 goto err;
193             }
194         }
195     }
196     if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS)
197         && param == gost_cipher_list) {
198         param = gost_cipher_list;
199     }
200     if (out) {
201         int dgst_nid = NID_undef;
202         EVP_PKEY_get_default_digest_nid(pubk, &dgst_nid);
203
204         if (!VKO_compute_key(shared_key, 32,
205                              EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
206                              EVP_PKEY_get0(sec_key), ukm, dgst_nid)) {
207             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
208                     GOST_R_ERROR_COMPUTING_SHARED_KEY);
209             goto err;
210         }
211         gost_init(&cctx, param->sblock);
212         keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
213     }
214     gkt = GOST_KEY_TRANSPORT_new();
215     if (!gkt) {
216         goto err;
217     }
218     if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
219         goto err;
220     }
221     if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
222         goto err;
223     }
224     if (!ASN1_OCTET_STRING_set
225         (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
226         goto err;
227     }
228     if (key_is_ephemeral) {
229         if (!X509_PUBKEY_set
230             (&gkt->key_agreement_info->ephem_key, out ? sec_key : pubk)) {
231             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
232                     GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
233             goto err;
234         }
235     }
236     ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
237     gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
238     if (key_is_ephemeral)
239         EVP_PKEY_free(sec_key);
240     if (!key_is_ephemeral) {
241         /* Set control "public key from client certificate used" */
242         if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
243             <= 0) {
244             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
245             goto err;
246         }
247     }
248     if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL)) > 0)
249         ret = 1;
250     GOST_KEY_TRANSPORT_free(gkt);
251     return ret;
252  err:
253     if (key_is_ephemeral)
254         EVP_PKEY_free(sec_key);
255     GOST_KEY_TRANSPORT_free(gkt);
256     return -1;
257 }
258
259 /*
260  * EVP_PKEY_METHOD callback decrypt
261  * Implementation of GOST2001 key transport, cryptopo variation
262  */
263 int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
264                            size_t *key_len, const unsigned char *in,
265                            size_t in_len)
266 {
267     const unsigned char *p = in;
268     EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
269     GOST_KEY_TRANSPORT *gkt = NULL;
270     int ret = 0;
271     unsigned char wrappedKey[44];
272     unsigned char sharedKey[32];
273     gost_ctx ctx;
274     const struct gost_cipher_info *param = NULL;
275     EVP_PKEY *eph_key = NULL, *peerkey = NULL;
276     int dgst_nid = NID_undef;
277
278     if (!key) {
279         *key_len = 32;
280         return 1;
281     }
282     gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
283     if (!gkt) {
284         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
285                 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
286         return -1;
287     }
288
289     /* If key transport structure contains public key, use it */
290     eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
291     if (eph_key) {
292         if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
293             GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
294                     GOST_R_INCOMPATIBLE_PEER_KEY);
295             goto err;
296         }
297     } else {
298         /* Set control "public key from client certificate used" */
299         if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
300             <= 0) {
301             GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
302             goto err;
303         }
304     }
305     peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
306     if (!peerkey) {
307         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_NO_PEER_KEY);
308         goto err;
309     }
310
311     param = get_encryption_params(gkt->key_agreement_info->cipher);
312     if (!param) {
313         goto err;
314     }
315
316     gost_init(&ctx, param->sblock);
317     OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
318     memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
319     OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
320     memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
321     OPENSSL_assert(gkt->key_info->imit->length == 4);
322     memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
323
324     EVP_PKEY_get_default_digest_nid(priv, &dgst_nid);
325     if (!VKO_compute_key(sharedKey, 32,
326                          EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
327                          EVP_PKEY_get0(priv), wrappedKey, dgst_nid)) {
328         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
329                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
330         goto err;
331     }
332     if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
333         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
334                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
335         goto err;
336     }
337
338     ret = 1;
339  err:
340     EVP_PKEY_free(eph_key);
341     GOST_KEY_TRANSPORT_free(gkt);
342     return ret;
343 }