3 /**********************************************************************
5 * Copyright (c) 2006 Cryptocom LTD *
6 * This file is distributed under the same license as OpenSSL *
8 * Internal declarations used in GOST engine *
9 * OpenSSL 0.9.9 libraries required to compile and use *
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>
20 # include "gosthash.h"
22 /* Control commands */
23 # define GOST_PARAM_CRYPT_PARAMS 0
24 # define GOST_PARAM_PBE_PARAMS 1
25 # define GOST_PARAM_MAX 1
26 # define GOST_CTRL_CRYPT_PARAMS (ENGINE_CMD_BASE+GOST_PARAM_CRYPT_PARAMS)
27 # define GOST_CTRL_PBE_PARAMS (ENGINE_CMD_BASE+GOST_PARAM_PBE_PARAMS)
29 typedef struct R3410_ec {
39 extern R3410_ec_params R3410_2001_paramset[],
40 *R3410_2012_256_paramset,
41 R3410_2012_512_paramset[];
43 extern const ENGINE_CMD_DEFN gost_cmds[];
44 int gost_control_func(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
45 const char *get_gost_engine_param(int param);
46 int gost_set_default_param(int param, const char *value);
47 void gost_param_free(void);
49 /* method registration */
51 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
52 const char *pemstr, const char *info);
53 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags);
55 /* Gost-specific pmeth control-function parameters */
56 /* For GOST R34.10 parameters */
57 # define param_ctrl_string "paramset"
58 # define EVP_PKEY_CTRL_GOST_PARAMSET (EVP_PKEY_ALG_CTRL+1)
59 /* For GOST 28147 MAC */
60 # define key_ctrl_string "key"
61 # define hexkey_ctrl_string "hexkey"
62 # define maclen_ctrl_string "size"
63 # define EVP_PKEY_CTRL_GOST_MAC_HEXKEY (EVP_PKEY_ALG_CTRL+3)
64 # define EVP_PKEY_CTRL_MAC_LEN (EVP_PKEY_ALG_CTRL+5)
65 /* Pmeth internal representation */
66 struct gost_pmeth_data {
67 int sign_param_nid; /* Should be set whenever parameters are
70 unsigned char *shared_ukm;
74 struct gost_mac_pmeth_data {
78 unsigned char key[32];
80 /* GOST-specific ASN1 structures */
83 ASN1_OCTET_STRING *encrypted_key;
84 ASN1_OCTET_STRING *imit;
87 DECLARE_ASN1_FUNCTIONS(GOST_KEY_INFO)
91 X509_PUBKEY *ephem_key;
92 ASN1_OCTET_STRING *eph_iv;
93 } GOST_KEY_AGREEMENT_INFO;
95 DECLARE_ASN1_FUNCTIONS(GOST_KEY_AGREEMENT_INFO)
98 GOST_KEY_INFO *key_info;
99 GOST_KEY_AGREEMENT_INFO *key_agreement_info;
100 } GOST_KEY_TRANSPORT;
102 DECLARE_ASN1_FUNCTIONS(GOST_KEY_TRANSPORT)
104 typedef struct { /* FIXME incomplete */
105 GOST_KEY_TRANSPORT *gkt;
106 } GOST_CLIENT_KEY_EXCHANGE_PARAMS;
109 * Hacks to shorten symbols to 31 characters or less, or OpenVMS. This mimics
110 * what's done in symhacks.h, but since this is a very local header file, I
111 * prefered to put this hack directly here. -- Richard Levitte
113 # ifdef OPENSSL_SYS_VMS
114 # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_it
115 # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_it GOST_CLIENT_KEY_EXC_PARAMS_it
116 # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_new
117 # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_new GOST_CLIENT_KEY_EXC_PARAMS_new
118 # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_free
119 # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_free GOST_CLIENT_KEY_EXC_PARAMS_free
120 # undef d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS
121 # define d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS d2i_GOST_CLIENT_KEY_EXC_PARAMS
122 # undef i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS
123 # define i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS i2d_GOST_CLIENT_KEY_EXC_PARAMS
124 # endif /* End of hack */
125 DECLARE_ASN1_FUNCTIONS(GOST_CLIENT_KEY_EXCHANGE_PARAMS)
127 ASN1_OBJECT *key_params;
128 ASN1_OBJECT *hash_params;
129 ASN1_OBJECT *cipher_params;
132 DECLARE_ASN1_FUNCTIONS(GOST_KEY_PARAMS)
135 ASN1_OCTET_STRING *iv;
136 ASN1_OBJECT *enc_param_set;
137 } GOST_CIPHER_PARAMS;
139 DECLARE_ASN1_FUNCTIONS(GOST_CIPHER_PARAMS)
142 ASN1_OCTET_STRING *masked_priv_key;
143 ASN1_OCTET_STRING *public_key;
146 DECLARE_ASN1_FUNCTIONS(MASKED_GOST_KEY)
148 /*============== Message digest and cipher related structures ==========*/
150 * Structure used as EVP_MD_CTX-md_data. It allows to avoid storing
151 * in the md-data pointers to dynamically allocated memory. I
152 * cannot invent better way to avoid memory leaks, because openssl
153 * insist on invoking Init on Final-ed digests, and there is no
154 * reliable way to find out whether pointer in the passed md_data is
157 struct ossl_gost_digest_ctx {
161 /* EVP_MD structure for GOST R 34.11 */
162 extern EVP_MD digest_gost;
163 /* EVP MD structure for GOST R 34.11-2012 algorithms */
164 extern EVP_MD digest_gost2012_256;
165 extern EVP_MD digest_gost2012_512;
166 /* EVP_MD structure for GOST 28147 in MAC mode */
167 extern EVP_MD imit_gost_cpa;
168 extern EVP_MD imit_gost_cp_12;
169 /* Cipher context used for EVP_CIPHER operation */
170 struct ossl_gost_cipher_ctx {
176 /* Structure to map parameter NID to S-block */
177 struct gost_cipher_info {
179 gost_subst_block *sblock;
182 /* Context for MAC */
183 struct ossl_gost_imit_ctx {
185 unsigned char buffer[8];
186 unsigned char partial_block[8];
193 /* Table which maps parameter NID to S-blocks */
194 extern struct gost_cipher_info gost_cipher_list[];
195 /* Find encryption params from ASN1_OBJECT */
196 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj);
197 /* Implementation of GOST 28147-89 cipher in CFB and CNT modes */
198 extern EVP_CIPHER cipher_gost;
199 extern EVP_CIPHER cipher_gost_cbc;
200 extern EVP_CIPHER cipher_gost_cpacnt;
201 extern EVP_CIPHER cipher_gost_cpcnt_12;
202 # define EVP_MD_CTRL_KEY_LEN (EVP_MD_CTRL_ALG_CTRL+3)
203 # define EVP_MD_CTRL_SET_KEY (EVP_MD_CTRL_ALG_CTRL+4)
204 # define EVP_MD_CTRL_MAC_LEN (EVP_MD_CTRL_ALG_CTRL+5)
205 /* EVP_PKEY_METHOD key encryption callbacks */
206 /* From gost_ec_keyx.c */
207 int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
208 size_t *outlen, const unsigned char *key,
211 int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
212 size_t *outlen, const unsigned char *in,
214 /* derive functions */
215 /* From gost_ec_keyx.c */
216 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
218 int fill_GOST_EC_params(EC_KEY *eckey, int nid);
219 int gost_sign_keygen(DSA *dsa);
220 int gost_ec_keygen(EC_KEY *ec);
222 DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey);
224 int gost_do_verify(const unsigned char *dgst, int dgst_len,
225 DSA_SIG *sig, DSA *dsa);
226 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
227 DSA_SIG *sig, EC_KEY *ec);
228 int gost_ec_compute_public(EC_KEY *ec);
229 /*============== miscellaneous functions============================= */
230 /* from gost_sign.c */
231 /* Convert GOST R 34.11 hash sum to bignum according to standard */
232 BIGNUM *hashsum2bn(const unsigned char *dgst, int len);
234 * Store bignum in byte array of given length, prepending by zeros if
237 int store_bignum(BIGNUM *bn, unsigned char *buf, int len);
238 /* Read bignum, which can have few MSB all-zeros from buffer*/
239 BIGNUM *getbnfrombuf(const unsigned char *buf, size_t len);
240 /* Pack GOST R 34.10 signature according to CryptoPro rules */
241 int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen);
243 /* Get private key as BIGNUM from both 34.10-2001 keys*/
244 /* Returns pointer into EVP_PKEY structure */
245 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey);
247 int gost_add_obj(const char *oid, const char *sn, const char *ln);