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