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