remove unused method declaration
[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     EVP_PKEY *mykey = EVP_PKEY_CTX_get0_peerkey(ctx);
109
110     /* Do not use vizir cipher parameters with cryptopro */
111     if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS)
112         && param == gost_cipher_list) {
113         param = gost_cipher_list + 1;
114     }
115
116     if (mykey) {
117         /* If key already set, it is not ephemeral */
118         key_is_ephemeral = 0;
119         if (!gost_get0_priv_key(mykey)) {
120             GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
121                     GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
122             goto err;
123         }
124     } else {
125         /* Otherwise generate ephemeral key */
126         key_is_ephemeral = 1;
127         if (out) {
128             mykey = EVP_PKEY_new();
129             EVP_PKEY_assign(mykey, EVP_PKEY_base_id(pubk), DSA_new());
130             EVP_PKEY_copy_parameters(mykey, pubk);
131             if (!gost_sign_keygen(EVP_PKEY_get0(mykey))) {
132                 goto err;
133             }
134         }
135     }
136     if (out)
137         make_cp_exchange_key(gost_get0_priv_key(mykey), pubk, shared_key);
138     if (data->shared_ukm) {
139         memcpy(ukm, data->shared_ukm, 8);
140     } else if (out) {
141         if (RAND_bytes(ukm, 8) <= 0) {
142             GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
143                     GOST_R_RANDOM_GENERATOR_FAILURE);
144             goto err;
145         }
146     }
147
148     if (out) {
149         gost_init(&cctx, param->sblock);
150         keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
151     }
152     gkt = GOST_KEY_TRANSPORT_new();
153     if (!gkt) {
154         goto memerr;
155     }
156     if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
157         goto memerr;
158     }
159     if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
160         goto memerr;
161     }
162     if (!ASN1_OCTET_STRING_set
163         (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
164         goto memerr;
165     }
166     if (key_is_ephemeral) {
167         if (!X509_PUBKEY_set
168             (&gkt->key_agreement_info->ephem_key, out ? mykey : pubk)) {
169             GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
170                     GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
171             goto err;
172         }
173         if (out)
174             EVP_PKEY_free(mykey);
175     }
176     ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
177     gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
178     *outlen = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL);
179     if (*outlen <= 0) {
180         GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
181                 GOST_R_ERROR_PACKING_KEY_TRANSPORT_INFO);
182         goto err;
183     }
184     if (!key_is_ephemeral) {
185         /* Set control "public key from client certificate used" */
186         if (EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL) <=
187             0) {
188             GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
189             goto err;
190         }
191     }
192     GOST_KEY_TRANSPORT_free(gkt);
193     return 1;
194  memerr:
195     if (key_is_ephemeral) {
196         EVP_PKEY_free(mykey);
197     }
198     GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT, ERR_R_MALLOC_FAILURE);
199  err:
200     GOST_KEY_TRANSPORT_free(gkt);
201     return -1;
202 }
203
204 /*
205  * EVP_PLEY_METHOD callback decrypt for GOST R 34.10-94 cryptopro
206  * modification
207  */
208 int pkey_GOST94cp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *key,
209                           size_t *key_len, const unsigned char *in,
210                           size_t in_len)
211 {
212     const unsigned char *p = in;
213     GOST_KEY_TRANSPORT *gkt = NULL;
214     unsigned char wrappedKey[44];
215     unsigned char sharedKey[32];
216     gost_ctx cctx;
217     const struct gost_cipher_info *param = NULL;
218     EVP_PKEY *eph_key = NULL, *peerkey = NULL;
219     EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(ctx);
220
221     if (!key) {
222         *key_len = 32;
223         return 1;
224     }
225
226     gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
227     if (!gkt) {
228         GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
229                 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
230         return 0;
231     }
232     eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
233     if (eph_key) {
234         if (EVP_PKEY_derive_set_peer(ctx, eph_key) <= 0) {
235             GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
236                     GOST_R_INCOMPATIBLE_PEER_KEY);
237             goto err;
238         }
239     } else {
240         /* Set control "public key from client certificate used" */
241         if (EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL) <=
242             0) {
243             GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
244             goto err;
245         }
246     }
247     peerkey = EVP_PKEY_CTX_get0_peerkey(ctx);
248     if (!peerkey) {
249         GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT, GOST_R_NO_PEER_KEY);
250         goto err;
251     }
252
253     param = get_encryption_params(gkt->key_agreement_info->cipher);
254     if (!param) {
255         goto err;
256     }
257
258     gost_init(&cctx, param->sblock);
259     OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
260     memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
261     OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
262     memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
263     OPENSSL_assert(gkt->key_info->imit->length == 4);
264     memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
265     make_cp_exchange_key(gost_get0_priv_key(priv), peerkey, sharedKey);
266     if (!keyUnwrapCryptoPro(&cctx, sharedKey, wrappedKey, key)) {
267         GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
268                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
269         goto err;
270     }
271
272     EVP_PKEY_free(eph_key);
273     GOST_KEY_TRANSPORT_free(gkt);
274     return 1;
275  err:
276     EVP_PKEY_free(eph_key);
277     GOST_KEY_TRANSPORT_free(gkt);
278     return -1;
279 }