gost94_keyx.c 8.9 KB

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