gost94_keyx.c 7.7 KB

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