OpenSSL: update to 1.0.2c
[tomato.git] / release / src / router / openssl / engines / ccgost / gost94_keyx.c
blobce57f17cbf32f111061c9aca7224565d3da23664
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/evp.h>
15 #include <openssl/objects.h>
17 #include "gost89.h"
18 #include "gosthash.h"
19 #include "e_gost_err.h"
20 #include "gost_keywrap.h"
21 #include "gost_lcl.h"
22 /* Common functions for both 94 and 2001 key exchange schemes */
24 * Implementation of the Diffi-Hellman key agreement scheme based on GOST-94
25 * keys
29 * Computes Diffie-Hellman key and stores it into buffer in little-endian
30 * byte order as expected by both versions of GOST 94 algorithm
32 static int compute_pair_key_le(unsigned char *pair_key, BIGNUM *pub_key,
33 DH *dh)
35 unsigned char be_key[128];
36 int i, key_size;
37 key_size = DH_compute_key(be_key, pub_key, dh);
38 if (!key_size)
39 return 0;
40 memset(pair_key, 0, 128);
41 for (i = 0; i < key_size; i++) {
42 pair_key[i] = be_key[key_size - 1 - i];
44 return key_size;
48 * Computes 256 bit Key exchange key as specified in RFC 4357
50 static int make_cp_exchange_key(BIGNUM *priv_key, EVP_PKEY *pubk,
51 unsigned char *shared_key)
53 unsigned char dh_key[128];
54 int ret;
55 gost_hash_ctx hash_ctx;
56 DH *dh = DH_new();
58 if (!dh)
59 return 0;
60 memset(dh_key, 0, 128);
61 dh->g = BN_dup(pubk->pkey.dsa->g);
62 dh->p = BN_dup(pubk->pkey.dsa->p);
63 dh->priv_key = BN_dup(priv_key);
64 ret =
65 compute_pair_key_le(dh_key, ((DSA *)(EVP_PKEY_get0(pubk)))->pub_key,
66 dh);
67 DH_free(dh);
68 if (!ret)
69 return 0;
70 init_gost_hash_ctx(&hash_ctx, &GostR3411_94_CryptoProParamSet);
71 start_hash(&hash_ctx);
72 hash_block(&hash_ctx, dh_key, 128);
73 finish_hash(&hash_ctx, shared_key);
74 done_gost_hash_ctx(&hash_ctx);
75 return 1;
78 /* EVP_PKEY_METHOD callback derive. Implements VKO R 34.10-94 */
80 int pkey_gost94_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
82 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_peerkey(ctx);
83 EVP_PKEY *mykey = EVP_PKEY_CTX_get0_pkey(ctx);
84 *keylen = 32;
85 if (key == NULL)
86 return 1;
88 return make_cp_exchange_key(gost_get0_priv_key(mykey), pubk, key);
92 * EVP_PKEY_METHOD callback encrypt for GOST R 34.10-94 cryptopro
93 * modification
96 int pkey_GOST94cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
97 size_t *outlen, const unsigned char *key,
98 size_t key_len)
100 GOST_KEY_TRANSPORT *gkt = NULL;
101 unsigned char shared_key[32], ukm[8], crypted_key[44];
102 const struct gost_cipher_info *param = get_encryption_params(NULL);
103 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(ctx);
104 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
105 gost_ctx cctx;
106 int key_is_ephemeral = 1;
107 int tmp_outlen;
108 EVP_PKEY *mykey = EVP_PKEY_CTX_get0_peerkey(ctx);
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;
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;
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;
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;
148 if (out) {
149 gost_init(&cctx, param->sblock);
150 keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
152 gkt = GOST_KEY_TRANSPORT_new();
153 if (!gkt) {
154 goto memerr;
156 if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
157 goto memerr;
159 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
160 goto memerr;
162 if (!ASN1_OCTET_STRING_set
163 (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
164 goto memerr;
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;
173 if (out)
174 EVP_PKEY_free(mykey);
176 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
177 gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
178 tmp_outlen = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL);
179 if (tmp_outlen <= 0) {
180 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
181 GOST_R_ERROR_PACKING_KEY_TRANSPORT_INFO);
182 goto err;
184 *outlen = tmp_outlen;
185 if (!key_is_ephemeral) {
186 /* Set control "public key from client certificate used" */
187 if (EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL) <=
188 0) {
189 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
190 goto err;
193 GOST_KEY_TRANSPORT_free(gkt);
194 return 1;
195 memerr:
196 if (key_is_ephemeral) {
197 EVP_PKEY_free(mykey);
199 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT, GOST_R_MALLOC_FAILURE);
200 err:
201 GOST_KEY_TRANSPORT_free(gkt);
202 return -1;
206 * EVP_PLEY_METHOD callback decrypt for GOST R 34.10-94 cryptopro
207 * modification
209 int pkey_GOST94cp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *key,
210 size_t *key_len, const unsigned char *in,
211 size_t in_len)
213 const unsigned char *p = in;
214 GOST_KEY_TRANSPORT *gkt = NULL;
215 unsigned char wrappedKey[44];
216 unsigned char sharedKey[32];
217 gost_ctx cctx;
218 const struct gost_cipher_info *param = NULL;
219 EVP_PKEY *eph_key = NULL, *peerkey = NULL;
220 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(ctx);
222 if (!key) {
223 *key_len = 32;
224 return 1;
227 gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
228 if (!gkt) {
229 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
230 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
231 return 0;
233 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
234 if (eph_key) {
235 if (EVP_PKEY_derive_set_peer(ctx, eph_key) <= 0) {
236 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
237 GOST_R_INCOMPATIBLE_PEER_KEY);
238 goto err;
240 } else {
241 /* Set control "public key from client certificate used" */
242 if (EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL) <=
243 0) {
244 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
245 goto err;
248 peerkey = EVP_PKEY_CTX_get0_peerkey(ctx);
249 if (!peerkey) {
250 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT, GOST_R_NO_PEER_KEY);
251 goto err;
254 param = get_encryption_params(gkt->key_agreement_info->cipher);
255 if (!param) {
256 goto err;
259 gost_init(&cctx, param->sblock);
260 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
261 memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
262 OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
263 memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
264 OPENSSL_assert(gkt->key_info->imit->length == 4);
265 memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
266 make_cp_exchange_key(gost_get0_priv_key(priv), peerkey, sharedKey);
267 if (!keyUnwrapCryptoPro(&cctx, sharedKey, wrappedKey, key)) {
268 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
269 GOST_R_ERROR_COMPUTING_SHARED_KEY);
270 goto err;
273 EVP_PKEY_free(eph_key);
274 GOST_KEY_TRANSPORT_free(gkt);
275 return 1;
276 err:
277 EVP_PKEY_free(eph_key);
278 GOST_KEY_TRANSPORT_free(gkt);
279 return -1;