1 /**********************************************************************
3 * Copyright (c) 2005-2013 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * VK0 34.10-2001 key exchange and GOST R 34.10-2001 *
7 * based PKCS7/SMIME support *
8 * Requires OpenSSL 0.9.9 for compilation *
9 **********************************************************************/
10 #include <openssl/evp.h>
11 #include <openssl/err.h>
12 #include <openssl/rand.h>
14 #include <openssl/objects.h>
16 #include "e_gost_err.h"
17 #include "gost_keywrap.h"
20 /* Implementation of CryptoPro VKO 34.10-2001/2012 algorithm */
21 static int VKO_compute_key(unsigned char *shared_key, size_t shared_key_size,
22 const EC_POINT *pub_key, EC_KEY *priv_key,
23 const unsigned char *ukm, int dgst_nid)
25 unsigned char *databuf = NULL, *hashbuf = NULL;
26 BIGNUM *UKM = NULL, *p = NULL, *order = NULL, *X = NULL, *Y = NULL;
27 const BIGNUM *key = EC_KEY_get0_private_key(priv_key);
28 EC_POINT *pnt = EC_POINT_new(EC_KEY_get0_group(priv_key));
30 BN_CTX *ctx = BN_CTX_new();
31 EVP_MD_CTX *mdctx = NULL;
32 const EVP_MD *md = NULL;
33 int effective_dgst_nid = (dgst_nid == NID_id_GostR3411_2012_512) ?
34 NID_id_GostR3411_2012_256 : dgst_nid;
35 int buf_len = (dgst_nid == NID_id_GostR3411_2012_512) ? 128 : 64,
36 half_len = buf_len >> 1;
39 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
44 databuf = OPENSSL_malloc(buf_len);
45 hashbuf = OPENSSL_malloc(buf_len);
46 if (!databuf || !hashbuf) {
47 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
51 md = EVP_get_digestbynid(effective_dgst_nid);
53 GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_INVALID_DIGEST_TYPE);
57 UKM = hashsum2bn(ukm, 8);
59 order = BN_CTX_get(ctx);
62 EC_GROUP_get_order(EC_KEY_get0_group(priv_key), order, ctx);
63 BN_mod_mul(p, key, UKM, order, ctx);
65 (EC_KEY_get0_group(priv_key), pnt, NULL, pub_key, p, ctx)) {
66 GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_ERROR_POINT_MUL);
69 EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(priv_key),
72 * Serialize elliptic curve point same way as we do it when saving key
74 store_bignum(Y, databuf, half_len);
75 store_bignum(X, databuf + half_len, half_len);
76 /* And reverse byte order of whole buffer */
77 for (i = 0; i < buf_len; i++) {
78 hashbuf[buf_len - 1 - i] = databuf[i];
80 mdctx = EVP_MD_CTX_new();
82 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
85 EVP_MD_CTX_init(mdctx);
86 EVP_DigestInit_ex(mdctx, md, NULL);
87 EVP_DigestUpdate(mdctx, hashbuf, buf_len);
88 EVP_DigestFinal_ex(mdctx, shared_key, NULL);
96 EVP_MD_CTX_free(mdctx);
98 OPENSSL_free(databuf);
99 OPENSSL_free(hashbuf);
105 * EVP_PKEY_METHOD callback derive.
106 * Implements VKO R 34.10-2001/2012 algorithms
108 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
111 * Public key of peer in the ctx field peerkey
112 * Our private key in the ctx pkey
113 * ukm is in the algorithm specific context data
115 EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx);
116 EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx);
117 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
118 int dgst_nid = NID_undef;
120 if (!data || !data->shared_ukm) {
121 GOSTerr(GOST_F_PKEY_GOST_EC_DERIVE, GOST_R_UKM_NOT_SET);
130 EVP_PKEY_get_default_digest_nid(my_key, &dgst_nid);
133 VKO_compute_key(key, 32,
134 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
135 (EC_KEY *)EVP_PKEY_get0(my_key), data->shared_ukm,
137 return (*keylen) ? 1 : 0;
141 * EVP_PKEY_METHOD callback encrypt
142 * Implementation of GOST2001 key transport, cryptocom variation
145 * Generates ephemeral key based on pubk algorithm computes shared key using
146 * VKO and returns filled up GOST_KEY_TRANSPORT structure
150 * EVP_PKEY_METHOD callback encrypt
151 * Implementation of GOST2001 key transport, cryptopo variation
154 int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
155 size_t *out_len, const unsigned char *key,
158 GOST_KEY_TRANSPORT *gkt = NULL;
159 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
160 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
161 int pkey_nid = EVP_PKEY_base_id(pubk);
162 ASN1_OBJECT *crypt_params_obj = (pkey_nid == NID_id_GostR3410_2001) ?
163 OBJ_nid2obj(NID_id_Gost28147_89_CryptoPro_A_ParamSet) :
164 OBJ_nid2obj(NID_id_tc26_gost_28147_param_Z);
165 const struct gost_cipher_info *param =
166 get_encryption_params(crypt_params_obj);
167 unsigned char ukm[8], shared_key[32], crypted_key[44];
169 int key_is_ephemeral = 1;
171 EVP_PKEY *sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
172 if (data->shared_ukm) {
173 memcpy(ukm, data->shared_ukm, 8);
176 if (RAND_bytes(ukm, 8) <= 0) {
177 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_RNG_ERROR);
181 /* Check for private key in the peer_key of context */
183 key_is_ephemeral = 0;
184 if (!gost_get0_priv_key(sec_key)) {
185 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
186 GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
190 key_is_ephemeral = 1;
192 sec_key = EVP_PKEY_new();
193 EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new());
194 EVP_PKEY_copy_parameters(sec_key, pubk);
195 if (!gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
200 if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS)
201 && param == gost_cipher_list) {
202 param = gost_cipher_list;
205 int dgst_nid = NID_undef;
206 EVP_PKEY_get_default_digest_nid(pubk, &dgst_nid);
208 if (!VKO_compute_key(shared_key, 32,
209 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
210 EVP_PKEY_get0(sec_key), ukm, dgst_nid)) {
211 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
212 GOST_R_ERROR_COMPUTING_SHARED_KEY);
215 gost_init(&cctx, param->sblock);
216 keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
218 gkt = GOST_KEY_TRANSPORT_new();
222 if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
225 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
228 if (!ASN1_OCTET_STRING_set
229 (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
232 if (key_is_ephemeral) {
234 (&gkt->key_agreement_info->ephem_key, out ? sec_key : pubk)) {
235 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
236 GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
240 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
241 gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
242 if (key_is_ephemeral)
243 EVP_PKEY_free(sec_key);
244 if (!key_is_ephemeral) {
245 /* Set control "public key from client certificate used" */
246 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
248 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
252 if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL)) > 0)
254 GOST_KEY_TRANSPORT_free(gkt);
257 if (key_is_ephemeral)
258 EVP_PKEY_free(sec_key);
259 GOST_KEY_TRANSPORT_free(gkt);
264 * EVP_PKEY_METHOD callback decrypt
265 * Implementation of GOST2001 key transport, cryptopo variation
267 int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
268 size_t *key_len, const unsigned char *in,
271 const unsigned char *p = in;
272 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
273 GOST_KEY_TRANSPORT *gkt = NULL;
275 unsigned char wrappedKey[44];
276 unsigned char sharedKey[32];
278 const struct gost_cipher_info *param = NULL;
279 EVP_PKEY *eph_key = NULL, *peerkey = NULL;
280 int dgst_nid = NID_undef;
286 gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
288 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
289 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
293 /* If key transport structure contains public key, use it */
294 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
296 if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
297 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
298 GOST_R_INCOMPATIBLE_PEER_KEY);
302 /* Set control "public key from client certificate used" */
303 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
305 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
309 peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
311 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_NO_PEER_KEY);
315 param = get_encryption_params(gkt->key_agreement_info->cipher);
320 gost_init(&ctx, param->sblock);
321 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
322 memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
323 OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
324 memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
325 OPENSSL_assert(gkt->key_info->imit->length == 4);
326 memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
328 EVP_PKEY_get_default_digest_nid(priv, &dgst_nid);
329 if (!VKO_compute_key(sharedKey, 32,
330 EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
331 EVP_PKEY_get0(priv), wrappedKey, dgst_nid)) {
332 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
333 GOST_R_ERROR_COMPUTING_SHARED_KEY);
336 if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
337 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
338 GOST_R_ERROR_COMPUTING_SHARED_KEY);
344 EVP_PKEY_free(eph_key);
345 GOST_KEY_TRANSPORT_free(gkt);