1 /**********************************************************************
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * Implements generation and parsing of GOST_KEY_TRANSPORT for *
7 * GOST R 34.10-94 algorithms *
9 * Requires OpenSSL 0.9.9 for compilation *
10 **********************************************************************/
12 #include <openssl/dh.h>
13 #include <openssl/rand.h>
14 #include <openssl/evp.h>
15 #include <openssl/objects.h>
19 #include "e_gost_err.h"
20 #include "gost_keywrap.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
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,
35 unsigned char be_key[128];
37 key_size = DH_compute_key(be_key, pub_key, dh);
40 memset(pair_key, 0, 128);
41 for (i = 0; i < key_size; i++) {
42 pair_key[i] = be_key[key_size - 1 - i];
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];
55 gost_hash_ctx hash_ctx;
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);
65 compute_pair_key_le(dh_key, ((DSA *)(EVP_PKEY_get0(pubk)))->pub_key,
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);
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);
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
96 int pkey_GOST94cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
97 size_t *outlen, const unsigned char *key,
100 GOST_KEY_TRANSPORT *gkt = NULL;
101 unsigned char shared_key[32], ukm[8], crypted_key[44];
102 const struct gost_cipher_info *param =
103 get_encryption_params(OBJ_nid2obj
104 (NID_id_Gost28147_89_CryptoPro_A_ParamSet));
105 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(ctx);
106 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
108 int key_is_ephemeral = 1;
110 EVP_PKEY *mykey = EVP_PKEY_CTX_get0_peerkey(ctx);
112 /* Do not use vizir cipher parameters with cryptopro */
113 if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS)
114 && param == gost_cipher_list) {
115 param = gost_cipher_list + 1;
119 /* If key already set, it is not ephemeral */
120 key_is_ephemeral = 0;
121 if (!gost_get0_priv_key(mykey)) {
122 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
123 GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
127 /* Otherwise generate ephemeral key */
128 key_is_ephemeral = 1;
130 mykey = EVP_PKEY_new();
131 EVP_PKEY_assign(mykey, EVP_PKEY_base_id(pubk), DSA_new());
132 EVP_PKEY_copy_parameters(mykey, pubk);
133 if (!gost_sign_keygen(EVP_PKEY_get0(mykey))) {
139 make_cp_exchange_key(gost_get0_priv_key(mykey), pubk, shared_key);
140 if (data->shared_ukm) {
141 memcpy(ukm, data->shared_ukm, 8);
143 if (RAND_bytes(ukm, 8) <= 0) {
144 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
145 GOST_R_RANDOM_GENERATOR_FAILURE);
151 gost_init(&cctx, param->sblock);
152 keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
154 gkt = GOST_KEY_TRANSPORT_new();
158 if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
161 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
164 if (!ASN1_OCTET_STRING_set
165 (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
168 if (key_is_ephemeral) {
170 (&gkt->key_agreement_info->ephem_key, out ? mykey : pubk)) {
171 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
172 GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
176 EVP_PKEY_free(mykey);
178 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
179 gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
180 tmp_outlen = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL);
181 if (tmp_outlen <= 0) {
182 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
183 GOST_R_ERROR_PACKING_KEY_TRANSPORT_INFO);
186 *outlen = tmp_outlen;
187 if (!key_is_ephemeral) {
188 /* Set control "public key from client certificate used" */
189 if (EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL) <=
191 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
195 GOST_KEY_TRANSPORT_free(gkt);
198 if (key_is_ephemeral) {
199 EVP_PKEY_free(mykey);
201 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT, GOST_R_MALLOC_FAILURE);
203 GOST_KEY_TRANSPORT_free(gkt);
208 * EVP_PLEY_METHOD callback decrypt for GOST R 34.10-94 cryptopro
211 int pkey_GOST94cp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *key,
212 size_t *key_len, const unsigned char *in,
215 const unsigned char *p = in;
216 GOST_KEY_TRANSPORT *gkt = NULL;
217 unsigned char wrappedKey[44];
218 unsigned char sharedKey[32];
220 const struct gost_cipher_info *param = NULL;
221 EVP_PKEY *eph_key = NULL, *peerkey = NULL;
222 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(ctx);
229 gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
231 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
232 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
235 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
237 if (EVP_PKEY_derive_set_peer(ctx, eph_key) <= 0) {
238 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
239 GOST_R_INCOMPATIBLE_PEER_KEY);
243 /* Set control "public key from client certificate used" */
244 if (EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL) <=
246 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
250 peerkey = EVP_PKEY_CTX_get0_peerkey(ctx);
252 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT, GOST_R_NO_PEER_KEY);
256 param = get_encryption_params(gkt->key_agreement_info->cipher);
261 gost_init(&cctx, param->sblock);
262 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
263 memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
264 OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
265 memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
266 OPENSSL_assert(gkt->key_info->imit->length == 4);
267 memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
268 make_cp_exchange_key(gost_get0_priv_key(priv), peerkey, sharedKey);
269 if (!keyUnwrapCryptoPro(&cctx, sharedKey, wrappedKey, key)) {
270 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
271 GOST_R_ERROR_COMPUTING_SHARED_KEY);
275 EVP_PKEY_free(eph_key);
276 GOST_KEY_TRANSPORT_free(gkt);
279 EVP_PKEY_free(eph_key);
280 GOST_KEY_TRANSPORT_free(gkt);