Fix "make test" seg fault with SCTP enabled
[openssl.git] / engines / ccgost / gost94_keyx.c
1 /**********************************************************************
2  *                             gost94_keyx.c                          *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *     Implements generation and parsing of GOST_KEY_TRANSPORT for    *
7  *              GOST R 34.10-94 algorithms                            *
8  *                                                                    *
9  *          Requires OpenSSL 0.9.9 for compilation                    *
10  **********************************************************************/
11 #include <string.h>
12 #include <openssl/dh.h>
13 #include <openssl/rand.h>
14 #include <openssl/err.h>
15 #include <openssl/evp.h>
16 #include <openssl/objects.h>
17
18 #include "gost89.h"
19 #include "gosthash.h"
20 #include "e_gost_err.h"
21 #include "gost_keywrap.h"
22 #include "gost_lcl.h"
23 /* Common functions for both 94 and 2001 key exchange schemes */
24 /*
25  * Implementation of the Diffi-Hellman key agreement scheme based on GOST-94
26  * keys
27  */
28
29 /*
30  * Computes Diffie-Hellman key and stores it into buffer in little-endian
31  * byte order as expected by both versions of GOST 94 algorithm
32  */
33 static int compute_pair_key_le(unsigned char *pair_key, BIGNUM *pub_key,
34                                DH *dh)
35 {
36     unsigned char be_key[128];
37     int i, key_size;
38     key_size = DH_compute_key(be_key, pub_key, dh);
39     if (!key_size)
40         return 0;
41     memset(pair_key, 0, 128);
42     for (i = 0; i < key_size; i++) {
43         pair_key[i] = be_key[key_size - 1 - i];
44     }
45     return key_size;
46 }
47
48 /*
49  * Computes 256 bit Key exchange key as specified in RFC 4357
50  */
51 static int make_cp_exchange_key(BIGNUM *priv_key, EVP_PKEY *pubk,
52                                 unsigned char *shared_key)
53 {
54     unsigned char dh_key[128];
55     int ret;
56     gost_hash_ctx hash_ctx;
57     DH *dh = DH_new();
58
59     if (!dh)
60         return 0;
61     memset(dh_key, 0, 128);
62     dh->g = BN_dup(pubk->pkey.dsa->g);
63     dh->p = BN_dup(pubk->pkey.dsa->p);
64     dh->priv_key = BN_dup(priv_key);
65     ret =
66         compute_pair_key_le(dh_key, ((DSA *)(EVP_PKEY_get0(pubk)))->pub_key,
67                             dh);
68     DH_free(dh);
69     if (!ret)
70         return 0;
71     init_gost_hash_ctx(&hash_ctx, &GostR3411_94_CryptoProParamSet);
72     start_hash(&hash_ctx);
73     hash_block(&hash_ctx, dh_key, 128);
74     finish_hash(&hash_ctx, shared_key);
75     done_gost_hash_ctx(&hash_ctx);
76     return 1;
77 }
78
79 /* EVP_PKEY_METHOD callback derive. Implements VKO R 34.10-94 */
80
81 int pkey_gost94_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
82 {
83     EVP_PKEY *pubk = EVP_PKEY_CTX_get0_peerkey(ctx);
84     EVP_PKEY *mykey = EVP_PKEY_CTX_get0_pkey(ctx);
85     *keylen = 32;
86     if (key == NULL)
87         return 1;
88
89     return make_cp_exchange_key(gost_get0_priv_key(mykey), pubk, key);
90 }
91
92 /*
93  * EVP_PKEY_METHOD callback encrypt for GOST R 34.10-94 cryptopro
94  * modification
95  */
96
97 int pkey_GOST94cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
98                           size_t *outlen, const unsigned char *key,
99                           size_t key_len)
100 {
101     GOST_KEY_TRANSPORT *gkt = NULL;
102     unsigned char shared_key[32], ukm[8], crypted_key[44];
103     const struct gost_cipher_info *param = get_encryption_params(NULL);
104     EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(ctx);
105     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
106     gost_ctx cctx;
107     int key_is_ephemeral = 1;
108     int tmp_outlen;
109     EVP_PKEY *mykey = EVP_PKEY_CTX_get0_peerkey(ctx);
110
111     /* Do not use vizir cipher parameters with cryptopro */
112     if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS)
113         && param == gost_cipher_list) {
114         param = gost_cipher_list + 1;
115     }
116
117     if (mykey) {
118         /* If key already set, it is not ephemeral */
119         key_is_ephemeral = 0;
120         if (!gost_get0_priv_key(mykey)) {
121             GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
122                     GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
123             goto err;
124         }
125     } else {
126         /* Otherwise generate ephemeral key */
127         key_is_ephemeral = 1;
128         if (out) {
129             mykey = EVP_PKEY_new();
130             EVP_PKEY_assign(mykey, EVP_PKEY_base_id(pubk), DSA_new());
131             EVP_PKEY_copy_parameters(mykey, pubk);
132             if (!gost_sign_keygen(EVP_PKEY_get0(mykey))) {
133                 goto err;
134             }
135         }
136     }
137     if (out)
138         make_cp_exchange_key(gost_get0_priv_key(mykey), pubk, shared_key);
139     if (data->shared_ukm) {
140         memcpy(ukm, data->shared_ukm, 8);
141     } else if (out) {
142         if (RAND_bytes(ukm, 8) <= 0) {
143             GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
144                     GOST_R_RANDOM_GENERATOR_FAILURE);
145             goto err;
146         }
147     }
148
149     if (out) {
150         gost_init(&cctx, param->sblock);
151         keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
152     }
153     gkt = GOST_KEY_TRANSPORT_new();
154     if (!gkt) {
155         goto memerr;
156     }
157     if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
158         goto memerr;
159     }
160     if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
161         goto memerr;
162     }
163     if (!ASN1_OCTET_STRING_set
164         (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
165         goto memerr;
166     }
167     if (key_is_ephemeral) {
168         if (!X509_PUBKEY_set
169             (&gkt->key_agreement_info->ephem_key, out ? mykey : pubk)) {
170             GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
171                     GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
172             goto err;
173         }
174         if (out)
175             EVP_PKEY_free(mykey);
176     }
177     ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
178     gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
179     tmp_outlen = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL);
180     if (tmp_outlen <= 0) {
181         GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
182                 GOST_R_ERROR_PACKING_KEY_TRANSPORT_INFO);
183         goto err;
184     }
185     *outlen = tmp_outlen;
186     if (!key_is_ephemeral) {
187         /* Set control "public key from client certificate used" */
188         if (EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL) <=
189             0) {
190             GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
191             goto err;
192         }
193     }
194     GOST_KEY_TRANSPORT_free(gkt);
195     return 1;
196  memerr:
197     if (key_is_ephemeral) {
198         EVP_PKEY_free(mykey);
199     }
200     GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT, ERR_R_MALLOC_FAILURE);
201  err:
202     GOST_KEY_TRANSPORT_free(gkt);
203     return -1;
204 }
205
206 /*
207  * EVP_PLEY_METHOD callback decrypt for GOST R 34.10-94 cryptopro
208  * modification
209  */
210 int pkey_GOST94cp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *key,
211                           size_t *key_len, const unsigned char *in,
212                           size_t in_len)
213 {
214     const unsigned char *p = in;
215     GOST_KEY_TRANSPORT *gkt = NULL;
216     unsigned char wrappedKey[44];
217     unsigned char sharedKey[32];
218     gost_ctx cctx;
219     const struct gost_cipher_info *param = NULL;
220     EVP_PKEY *eph_key = NULL, *peerkey = NULL;
221     EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(ctx);
222
223     if (!key) {
224         *key_len = 32;
225         return 1;
226     }
227
228     gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
229     if (!gkt) {
230         GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
231                 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
232         return 0;
233     }
234     eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
235     if (eph_key) {
236         if (EVP_PKEY_derive_set_peer(ctx, eph_key) <= 0) {
237             GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
238                     GOST_R_INCOMPATIBLE_PEER_KEY);
239             goto err;
240         }
241     } else {
242         /* Set control "public key from client certificate used" */
243         if (EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL) <=
244             0) {
245             GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
246             goto err;
247         }
248     }
249     peerkey = EVP_PKEY_CTX_get0_peerkey(ctx);
250     if (!peerkey) {
251         GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT, GOST_R_NO_PEER_KEY);
252         goto err;
253     }
254
255     param = get_encryption_params(gkt->key_agreement_info->cipher);
256     if (!param) {
257         goto err;
258     }
259
260     gost_init(&cctx, param->sblock);
261     OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
262     memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
263     OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
264     memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
265     OPENSSL_assert(gkt->key_info->imit->length == 4);
266     memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
267     make_cp_exchange_key(gost_get0_priv_key(priv), peerkey, sharedKey);
268     if (!keyUnwrapCryptoPro(&cctx, sharedKey, wrappedKey, key)) {
269         GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
270                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
271         goto err;
272     }
273
274     EVP_PKEY_free(eph_key);
275     GOST_KEY_TRANSPORT_free(gkt);
276     return 1;
277  err:
278     EVP_PKEY_free(eph_key);
279     GOST_KEY_TRANSPORT_free(gkt);
280     return -1;
281 }