2
0

gost_lcl.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #ifndef GOST_TOOLS_H
  2. # define GOST_TOOLS_H
  3. /**********************************************************************
  4. * gost_lcl.h *
  5. * Copyright (c) 2006 Cryptocom LTD *
  6. * This file is distributed under the same license as OpenSSL *
  7. * *
  8. * Internal declarations used in GOST engine *
  9. * OpenSSL 0.9.9 libraries required to compile and use *
  10. * this code *
  11. **********************************************************************/
  12. # include <openssl/bn.h>
  13. # include <openssl/evp.h>
  14. # include <openssl/dsa.h>
  15. # include <openssl/asn1t.h>
  16. # include <openssl/x509.h>
  17. # include <openssl/engine.h>
  18. # include <openssl/ec.h>
  19. # include "gost89.h"
  20. # include "gosthash.h"
  21. /* Control commands */
  22. # define GOST_PARAM_CRYPT_PARAMS 0
  23. # define GOST_PARAM_MAX 0
  24. # define GOST_CTRL_CRYPT_PARAMS (ENGINE_CMD_BASE+GOST_PARAM_CRYPT_PARAMS)
  25. extern const ENGINE_CMD_DEFN gost_cmds[];
  26. int gost_control_func(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
  27. const char *get_gost_engine_param(int param);
  28. int gost_set_default_param(int param, const char *value);
  29. void gost_param_free(void);
  30. /* method registration */
  31. int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
  32. const char *pemstr, const char *info);
  33. int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags);
  34. /* Gost-specific pmeth control-function parameters */
  35. /* For GOST R34.10 parameters */
  36. # define param_ctrl_string "paramset"
  37. # define EVP_PKEY_CTRL_GOST_PARAMSET (EVP_PKEY_ALG_CTRL+1)
  38. /* For GOST 28147 MAC */
  39. # define key_ctrl_string "key"
  40. # define hexkey_ctrl_string "hexkey"
  41. # define EVP_PKEY_CTRL_GOST_MAC_HEXKEY (EVP_PKEY_ALG_CTRL+3)
  42. /* Pmeth internal representation */
  43. struct gost_pmeth_data {
  44. int sign_param_nid; /* Should be set whenever parameters are
  45. * filled */
  46. EVP_MD *md;
  47. unsigned char *shared_ukm;
  48. int peer_key_used;
  49. };
  50. struct gost_mac_pmeth_data {
  51. int key_set;
  52. EVP_MD *md;
  53. unsigned char key[32];
  54. };
  55. /* GOST-specific ASN1 structures */
  56. typedef struct {
  57. ASN1_OCTET_STRING *encrypted_key;
  58. ASN1_OCTET_STRING *imit;
  59. } GOST_KEY_INFO;
  60. DECLARE_ASN1_FUNCTIONS(GOST_KEY_INFO)
  61. typedef struct {
  62. ASN1_OBJECT *cipher;
  63. X509_PUBKEY *ephem_key;
  64. ASN1_OCTET_STRING *eph_iv;
  65. } GOST_KEY_AGREEMENT_INFO;
  66. DECLARE_ASN1_FUNCTIONS(GOST_KEY_AGREEMENT_INFO)
  67. typedef struct {
  68. GOST_KEY_INFO *key_info;
  69. GOST_KEY_AGREEMENT_INFO *key_agreement_info;
  70. } GOST_KEY_TRANSPORT;
  71. DECLARE_ASN1_FUNCTIONS(GOST_KEY_TRANSPORT)
  72. typedef struct { /* FIXME incomplete */
  73. GOST_KEY_TRANSPORT *gkt;
  74. } GOST_CLIENT_KEY_EXCHANGE_PARAMS;
  75. /*
  76. * Hacks to shorten symbols to 31 characters or less, or OpenVMS. This mimics
  77. * what's done in symhacks.h, but since this is a very local header file, I
  78. * prefered to put this hack directly here. -- Richard Levitte
  79. */
  80. # ifdef OPENSSL_SYS_VMS
  81. # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_it
  82. # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_it GOST_CLIENT_KEY_EXC_PARAMS_it
  83. # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_new
  84. # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_new GOST_CLIENT_KEY_EXC_PARAMS_new
  85. # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_free
  86. # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_free GOST_CLIENT_KEY_EXC_PARAMS_free
  87. # undef d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS
  88. # define d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS d2i_GOST_CLIENT_KEY_EXC_PARAMS
  89. # undef i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS
  90. # define i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS i2d_GOST_CLIENT_KEY_EXC_PARAMS
  91. # endif /* End of hack */
  92. DECLARE_ASN1_FUNCTIONS(GOST_CLIENT_KEY_EXCHANGE_PARAMS)
  93. typedef struct {
  94. ASN1_OBJECT *key_params;
  95. ASN1_OBJECT *hash_params;
  96. ASN1_OBJECT *cipher_params;
  97. } GOST_KEY_PARAMS;
  98. DECLARE_ASN1_FUNCTIONS(GOST_KEY_PARAMS)
  99. typedef struct {
  100. ASN1_OCTET_STRING *iv;
  101. ASN1_OBJECT *enc_param_set;
  102. } GOST_CIPHER_PARAMS;
  103. DECLARE_ASN1_FUNCTIONS(GOST_CIPHER_PARAMS)
  104. /*============== Message digest and cipher related structures ==========*/
  105. /*
  106. * Structure used as EVP_MD_CTX-md_data. It allows to avoid storing
  107. * in the md-data pointers to dynamically allocated memory. I
  108. * cannot invent better way to avoid memory leaks, because openssl
  109. * insist on invoking Init on Final-ed digests, and there is no
  110. * reliable way to find out whether pointer in the passed md_data is
  111. * valid or not.
  112. */
  113. struct ossl_gost_digest_ctx {
  114. gost_hash_ctx dctx;
  115. gost_ctx cctx;
  116. };
  117. /* EVP_MD structure for GOST R 34.11 */
  118. extern EVP_MD digest_gost;
  119. /* EVP_MD structure for GOST 28147 in MAC mode */
  120. extern EVP_MD imit_gost_cpa;
  121. /* Cipher context used for EVP_CIPHER operation */
  122. struct ossl_gost_cipher_ctx {
  123. int paramNID;
  124. unsigned int count;
  125. int key_meshing;
  126. gost_ctx cctx;
  127. };
  128. /* Structure to map parameter NID to S-block */
  129. struct gost_cipher_info {
  130. int nid;
  131. gost_subst_block *sblock;
  132. int key_meshing;
  133. };
  134. /* Context for MAC */
  135. struct ossl_gost_imit_ctx {
  136. gost_ctx cctx;
  137. unsigned char buffer[8];
  138. unsigned char partial_block[8];
  139. unsigned int count;
  140. int key_meshing;
  141. int bytes_left;
  142. int key_set;
  143. };
  144. /* Table which maps parameter NID to S-blocks */
  145. extern struct gost_cipher_info gost_cipher_list[];
  146. /* Find encryption params from ASN1_OBJECT */
  147. const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj);
  148. /* Implementation of GOST 28147-89 cipher in CFB and CNT modes */
  149. extern EVP_CIPHER cipher_gost;
  150. extern EVP_CIPHER cipher_gost_cpacnt;
  151. # define EVP_MD_CTRL_KEY_LEN (EVP_MD_CTRL_ALG_CTRL+3)
  152. # define EVP_MD_CTRL_SET_KEY (EVP_MD_CTRL_ALG_CTRL+4)
  153. /* EVP_PKEY_METHOD key encryption callbacks */
  154. /* From gost94_keyx.c */
  155. int pkey_GOST94cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
  156. size_t *outlen, const unsigned char *key,
  157. size_t key_len);
  158. int pkey_GOST94cp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
  159. size_t *outlen, const unsigned char *in,
  160. size_t in_len);
  161. /* From gost2001_keyx.c */
  162. int pkey_GOST01cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
  163. size_t *outlen, const unsigned char *key,
  164. size_t key_len);
  165. int pkey_GOST01cp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
  166. size_t *outlen, const unsigned char *in,
  167. size_t in_len);
  168. /* derive functions */
  169. /* From gost2001_keyx.c */
  170. int pkey_gost2001_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  171. size_t *keylen);
  172. /* From gost94_keyx.c */
  173. int pkey_gost94_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
  174. /* Internal functions for signature algorithms */
  175. int fill_GOST94_params(DSA *dsa, int nid);
  176. int fill_GOST2001_params(EC_KEY *eckey, int nid);
  177. int gost_sign_keygen(DSA *dsa);
  178. int gost2001_keygen(EC_KEY *ec);
  179. DSA_SIG *gost_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
  180. DSA_SIG *gost2001_do_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey);
  181. int gost_do_verify(const unsigned char *dgst, int dgst_len,
  182. DSA_SIG *sig, DSA *dsa);
  183. int gost2001_do_verify(const unsigned char *dgst, int dgst_len,
  184. DSA_SIG *sig, EC_KEY *ec);
  185. int gost2001_compute_public(EC_KEY *ec);
  186. int gost94_compute_public(DSA *dsa);
  187. /*============== miscellaneous functions============================= */
  188. /* from gost_sign.c */
  189. /* Convert GOST R 34.11 hash sum to bignum according to standard */
  190. BIGNUM *hashsum2bn(const unsigned char *dgst);
  191. /*
  192. * Store bignum in byte array of given length, prepending by zeros if
  193. * nesseccary
  194. */
  195. int store_bignum(BIGNUM *bn, unsigned char *buf, int len);
  196. /* Read bignum, which can have few MSB all-zeros from buffer*/
  197. BIGNUM *getbnfrombuf(const unsigned char *buf, size_t len);
  198. /* Pack GOST R 34.10 signature according to CryptoPro rules */
  199. int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen);
  200. /* Unpack GOST R 34.10 signature according to CryptoPro rules */
  201. DSA_SIG *unpack_cp_signature(const unsigned char *sig, size_t siglen);
  202. /* from ameth.c */
  203. /* Get private key as BIGNUM from both R 34.10-94 and R 34.10-2001 keys*/
  204. /* Returns pointer into EVP_PKEY structure */
  205. BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey);
  206. /* Find NID by GOST 94 parameters */
  207. int gost94_nid_by_params(DSA *p);
  208. #endif