Coverage Report

Created: 2022-11-30 06:20

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