gost2001_keyx.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /**********************************************************************
  2. * gost_keyx.c *
  3. * Copyright (c) 2005-2006 Cryptocom LTD *
  4. * This file is distributed under the same license as OpenSSL *
  5. * *
  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/rand.h>
  12. #include <string.h>
  13. #include <openssl/objects.h>
  14. #include "gost89.h"
  15. #include "gosthash.h"
  16. #include "e_gost_err.h"
  17. #include "gost_keywrap.h"
  18. #include "gost_lcl.h"
  19. #include "gost2001_keyx.h"
  20. /* Implementation of CryptoPro VKO 34.10-2001 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)
  24. {
  25. unsigned char ukm_be[8], databuf[64], hashbuf[64];
  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));
  29. int i;
  30. gost_hash_ctx hash_ctx;
  31. BN_CTX *ctx = BN_CTX_new();
  32. for (i = 0; i < 8; i++) {
  33. ukm_be[7 - i] = ukm[i];
  34. }
  35. BN_CTX_start(ctx);
  36. UKM = getbnfrombuf(ukm_be, 8);
  37. p = BN_CTX_get(ctx);
  38. order = BN_CTX_get(ctx);
  39. X = BN_CTX_get(ctx);
  40. Y = BN_CTX_get(ctx);
  41. EC_GROUP_get_order(EC_KEY_get0_group(priv_key), order, ctx);
  42. BN_mod_mul(p, key, UKM, order, ctx);
  43. EC_POINT_mul(EC_KEY_get0_group(priv_key), pnt, NULL, pub_key, p, ctx);
  44. EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(priv_key),
  45. pnt, X, Y, ctx);
  46. /*
  47. * Serialize elliptic curve point same way as we do it when saving key
  48. */
  49. store_bignum(Y, databuf, 32);
  50. store_bignum(X, databuf + 32, 32);
  51. /* And reverse byte order of whole buffer */
  52. for (i = 0; i < 64; i++) {
  53. hashbuf[63 - i] = databuf[i];
  54. }
  55. init_gost_hash_ctx(&hash_ctx, &GostR3411_94_CryptoProParamSet);
  56. start_hash(&hash_ctx);
  57. hash_block(&hash_ctx, hashbuf, 64);
  58. finish_hash(&hash_ctx, shared_key);
  59. done_gost_hash_ctx(&hash_ctx);
  60. BN_free(UKM);
  61. BN_CTX_end(ctx);
  62. BN_CTX_free(ctx);
  63. EC_POINT_free(pnt);
  64. return 32;
  65. }
  66. /*
  67. * EVP_PKEY_METHOD callback derive. Implements VKO R 34.10-2001
  68. * algorithm
  69. */
  70. int pkey_gost2001_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  71. size_t *keylen)
  72. {
  73. /*
  74. * Public key of peer in the ctx field peerkey Our private key in the ctx
  75. * pkey ukm is in the algorithm specific context data
  76. */
  77. EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx);
  78. EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx);
  79. struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
  80. if (!data->shared_ukm) {
  81. GOSTerr(GOST_F_PKEY_GOST2001_DERIVE, GOST_R_UKM_NOT_SET);
  82. return 0;
  83. }
  84. if (key == NULL) {
  85. *keylen = 32;
  86. return 32;
  87. }
  88. *keylen =
  89. VKO_compute_key(key, 32,
  90. EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
  91. (EC_KEY *)EVP_PKEY_get0(my_key), data->shared_ukm);
  92. return 1;
  93. }
  94. /*
  95. * EVP_PKEY_METHOD callback encrypt
  96. * Implementation of GOST2001 key transport, cryptocom variation
  97. */
  98. /*
  99. * Generates ephemeral key based on pubk algorithm computes shared key using
  100. * VKO and returns filled up GOST_KEY_TRANSPORT structure
  101. */
  102. /*
  103. * EVP_PKEY_METHOD callback encrypt
  104. * Implementation of GOST2001 key transport, cryptopo variation
  105. */
  106. int pkey_GOST01cp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
  107. size_t *out_len, const unsigned char *key,
  108. size_t key_len)
  109. {
  110. GOST_KEY_TRANSPORT *gkt = NULL;
  111. EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
  112. struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
  113. const struct gost_cipher_info *param = get_encryption_params(NULL);
  114. unsigned char ukm[8], shared_key[32], crypted_key[44];
  115. int ret = 0;
  116. int key_is_ephemeral = 1;
  117. gost_ctx cctx;
  118. EVP_PKEY *sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
  119. if (data->shared_ukm) {
  120. memcpy(ukm, data->shared_ukm, 8);
  121. } else if (out) {
  122. if (RAND_bytes(ukm, 8) <= 0) {
  123. GOSTerr(GOST_F_PKEY_GOST01CP_ENCRYPT,
  124. GOST_R_RANDOM_GENERATOR_FAILURE);
  125. return 0;
  126. }
  127. }
  128. /* Check for private key in the peer_key of context */
  129. if (sec_key) {
  130. key_is_ephemeral = 0;
  131. if (!gost_get0_priv_key(sec_key)) {
  132. GOSTerr(GOST_F_PKEY_GOST01CP_ENCRYPT,
  133. GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
  134. goto err;
  135. }
  136. } else {
  137. key_is_ephemeral = 1;
  138. if (out) {
  139. sec_key = EVP_PKEY_new();
  140. EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new());
  141. EVP_PKEY_copy_parameters(sec_key, pubk);
  142. if (!gost2001_keygen(EVP_PKEY_get0(sec_key))) {
  143. goto err;
  144. }
  145. }
  146. }
  147. if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS)
  148. && param == gost_cipher_list) {
  149. param = gost_cipher_list + 1;
  150. }
  151. if (out) {
  152. VKO_compute_key(shared_key, 32,
  153. EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
  154. EVP_PKEY_get0(sec_key), ukm);
  155. gost_init(&cctx, param->sblock);
  156. keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
  157. }
  158. gkt = GOST_KEY_TRANSPORT_new();
  159. if (!gkt) {
  160. goto err;
  161. }
  162. if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
  163. goto err;
  164. }
  165. if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
  166. goto err;
  167. }
  168. if (!ASN1_OCTET_STRING_set
  169. (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
  170. goto err;
  171. }
  172. if (key_is_ephemeral) {
  173. if (!X509_PUBKEY_set
  174. (&gkt->key_agreement_info->ephem_key, out ? sec_key : pubk)) {
  175. GOSTerr(GOST_F_PKEY_GOST01CP_ENCRYPT,
  176. GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
  177. goto err;
  178. }
  179. }
  180. ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
  181. gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
  182. if (key_is_ephemeral && sec_key)
  183. EVP_PKEY_free(sec_key);
  184. if (!key_is_ephemeral) {
  185. /* Set control "public key from client certificate used" */
  186. if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
  187. <= 0) {
  188. GOSTerr(GOST_F_PKEY_GOST01CP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
  189. goto err;
  190. }
  191. }
  192. if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL)) > 0)
  193. ret = 1;
  194. GOST_KEY_TRANSPORT_free(gkt);
  195. return ret;
  196. err:
  197. if (key_is_ephemeral && sec_key)
  198. EVP_PKEY_free(sec_key);
  199. GOST_KEY_TRANSPORT_free(gkt);
  200. return -1;
  201. }
  202. /*
  203. * EVP_PKEY_METHOD callback decrypt
  204. * Implementation of GOST2001 key transport, cryptopo variation
  205. */
  206. int pkey_GOST01cp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
  207. size_t *key_len, const unsigned char *in,
  208. size_t in_len)
  209. {
  210. const unsigned char *p = in;
  211. EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
  212. GOST_KEY_TRANSPORT *gkt = NULL;
  213. int ret = 0;
  214. unsigned char wrappedKey[44];
  215. unsigned char sharedKey[32];
  216. gost_ctx ctx;
  217. const struct gost_cipher_info *param = NULL;
  218. EVP_PKEY *eph_key = NULL, *peerkey = NULL;
  219. if (!key) {
  220. *key_len = 32;
  221. return 1;
  222. }
  223. gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
  224. if (!gkt) {
  225. GOSTerr(GOST_F_PKEY_GOST01CP_DECRYPT,
  226. GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
  227. return -1;
  228. }
  229. /* If key transport structure contains public key, use it */
  230. eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
  231. if (eph_key) {
  232. if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
  233. GOSTerr(GOST_F_PKEY_GOST01CP_DECRYPT,
  234. GOST_R_INCOMPATIBLE_PEER_KEY);
  235. goto err;
  236. }
  237. } else {
  238. /* Set control "public key from client certificate used" */
  239. if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
  240. <= 0) {
  241. GOSTerr(GOST_F_PKEY_GOST01CP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
  242. goto err;
  243. }
  244. }
  245. peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
  246. if (!peerkey) {
  247. GOSTerr(GOST_F_PKEY_GOST01CP_DECRYPT, GOST_R_NO_PEER_KEY);
  248. goto err;
  249. }
  250. param = get_encryption_params(gkt->key_agreement_info->cipher);
  251. if (!param) {
  252. goto err;
  253. }
  254. gost_init(&ctx, param->sblock);
  255. OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
  256. memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
  257. OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
  258. memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
  259. OPENSSL_assert(gkt->key_info->imit->length == 4);
  260. memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
  261. VKO_compute_key(sharedKey, 32,
  262. EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
  263. EVP_PKEY_get0(priv), wrappedKey);
  264. if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
  265. GOSTerr(GOST_F_PKEY_GOST01CP_DECRYPT,
  266. GOST_R_ERROR_COMPUTING_SHARED_KEY);
  267. goto err;
  268. }
  269. ret = 1;
  270. err:
  271. if (eph_key)
  272. EVP_PKEY_free(eph_key);
  273. if (gkt)
  274. GOST_KEY_TRANSPORT_free(gkt);
  275. return ret;
  276. }