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"
21 /* Control commands */
22 # define GOST_PARAM_CRYPT_PARAMS 0
23 # define GOST_PARAM_PBE_PARAMS 1
24 # define GOST_PARAM_MAX 1
25 # define GOST_CTRL_CRYPT_PARAMS (ENGINE_CMD_BASE+GOST_PARAM_CRYPT_PARAMS)
26 # define GOST_CTRL_PBE_PARAMS (ENGINE_CMD_BASE+GOST_PARAM_PBE_PARAMS)
28 typedef struct R3410_ec {
38 extern R3410_ec_params R3410_2001_paramset[],
39 *R3410_2012_256_paramset, R3410_2012_512_paramset[];
41 extern const ENGINE_CMD_DEFN gost_cmds[];
42 int gost_control_func(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
43 const char *get_gost_engine_param(int param);
44 int gost_set_default_param(int param, const char *value);
45 void gost_param_free(void);
47 /* method registration */
49 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
50 const char *pemstr, const char *info);
51 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags);
53 /* Gost-specific pmeth control-function parameters */
54 /* For GOST R34.10 parameters */
55 # define param_ctrl_string "paramset"
56 # define EVP_PKEY_CTRL_GOST_PARAMSET (EVP_PKEY_ALG_CTRL+1)
57 /* For GOST 28147 MAC */
58 # define key_ctrl_string "key"
59 # define hexkey_ctrl_string "hexkey"
60 # define maclen_ctrl_string "size"
61 # define EVP_PKEY_CTRL_GOST_MAC_HEXKEY (EVP_PKEY_ALG_CTRL+3)
62 # define EVP_PKEY_CTRL_MAC_LEN (EVP_PKEY_ALG_CTRL+5)
63 /* Pmeth internal representation */
64 struct gost_pmeth_data {
65 int sign_param_nid; /* Should be set whenever parameters are
68 unsigned char *shared_ukm;
72 struct gost_mac_pmeth_data {
77 unsigned char key[32];
82 unsigned char key[32];
85 /* GOST-specific ASN1 structures */
88 ASN1_OCTET_STRING *encrypted_key;
89 ASN1_OCTET_STRING *imit;
92 DECLARE_ASN1_FUNCTIONS(GOST_KEY_INFO)
96 X509_PUBKEY *ephem_key;
97 ASN1_OCTET_STRING *eph_iv;
98 } GOST_KEY_AGREEMENT_INFO;
100 DECLARE_ASN1_FUNCTIONS(GOST_KEY_AGREEMENT_INFO)
103 GOST_KEY_INFO *key_info;
104 GOST_KEY_AGREEMENT_INFO *key_agreement_info;
105 } GOST_KEY_TRANSPORT;
107 DECLARE_ASN1_FUNCTIONS(GOST_KEY_TRANSPORT)
109 typedef struct { /* FIXME incomplete */
110 GOST_KEY_TRANSPORT *gkt;
111 } GOST_CLIENT_KEY_EXCHANGE_PARAMS;
114 * Hacks to shorten symbols to 31 characters or less, or OpenVMS. This mimics
115 * what's done in symhacks.h, but since this is a very local header file, I
116 * prefered to put this hack directly here. -- Richard Levitte
118 # ifdef OPENSSL_SYS_VMS
119 # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_it
120 # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_it GOST_CLIENT_KEY_EXC_PARAMS_it
121 # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_new
122 # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_new GOST_CLIENT_KEY_EXC_PARAMS_new
123 # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_free
124 # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_free GOST_CLIENT_KEY_EXC_PARAMS_free
125 # undef d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS
126 # define d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS d2i_GOST_CLIENT_KEY_EXC_PARAMS
127 # undef i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS
128 # define i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS i2d_GOST_CLIENT_KEY_EXC_PARAMS
129 # endif /* End of hack */
130 DECLARE_ASN1_FUNCTIONS(GOST_CLIENT_KEY_EXCHANGE_PARAMS)
132 ASN1_OBJECT *key_params;
133 ASN1_OBJECT *hash_params;
134 ASN1_OBJECT *cipher_params;
137 DECLARE_ASN1_FUNCTIONS(GOST_KEY_PARAMS)
140 ASN1_OCTET_STRING *iv;
141 ASN1_OBJECT *enc_param_set;
142 } GOST_CIPHER_PARAMS;
144 DECLARE_ASN1_FUNCTIONS(GOST_CIPHER_PARAMS)
147 ASN1_OCTET_STRING *masked_priv_key;
148 ASN1_OCTET_STRING *public_key;
151 DECLARE_ASN1_FUNCTIONS(MASKED_GOST_KEY)
153 /*============== Message digest and cipher related structures ==========*/
155 * Structure used as EVP_MD_CTX-md_data. It allows to avoid storing
156 * in the md-data pointers to dynamically allocated memory. I
157 * cannot invent better way to avoid memory leaks, because openssl
158 * insist on invoking Init on Final-ed digests, and there is no
159 * reliable way to find out whether pointer in the passed md_data is
162 struct ossl_gost_digest_ctx {
166 /* EVP_MD structure for GOST R 34.11 */
167 EVP_MD *digest_gost(void);
168 void digest_gost_destroy(void);
169 /* EVP MD structure for GOST R 34.11-2012 algorithms */
170 EVP_MD *digest_gost2012_256(void);
171 EVP_MD *digest_gost2012_512(void);
172 void digest_gost2012_256_destroy(void);
173 void digest_gost2012_512_destroy(void);
174 /* EVP_MD structure for GOST 28147 in MAC mode */
175 EVP_MD *imit_gost_cpa(void);
176 void imit_gost_cpa_destroy(void);
177 EVP_MD *imit_gost_cp_12(void);
178 void imit_gost_cp_12_destroy(void);
179 /* Cipher context used for EVP_CIPHER operation */
180 struct ossl_gost_cipher_ctx {
186 /* Structure to map parameter NID to S-block */
187 struct gost_cipher_info {
189 gost_subst_block *sblock;
192 /* Context for MAC */
193 struct ossl_gost_imit_ctx {
195 unsigned char buffer[8];
196 unsigned char partial_block[8];
203 /* Table which maps parameter NID to S-blocks */
204 extern struct gost_cipher_info gost_cipher_list[];
205 /* Find encryption params from ASN1_OBJECT */
206 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj);
207 /* Implementation of GOST 28147-89 cipher in CFB and CNT modes */
208 const EVP_CIPHER *cipher_gost();
209 const EVP_CIPHER *cipher_gost_cbc();
210 const EVP_CIPHER *cipher_gost_cpacnt();
211 const EVP_CIPHER *cipher_gost_cpcnt_12();
212 void cipher_gost_destroy();
213 # define EVP_MD_CTRL_KEY_LEN (EVP_MD_CTRL_ALG_CTRL+3)
214 # define EVP_MD_CTRL_SET_KEY (EVP_MD_CTRL_ALG_CTRL+4)
215 # define EVP_MD_CTRL_MAC_LEN (EVP_MD_CTRL_ALG_CTRL+5)
216 /* EVP_PKEY_METHOD key encryption callbacks */
217 /* From gost_ec_keyx.c */
218 int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
219 size_t *outlen, const unsigned char *key,
222 int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
223 size_t *outlen, const unsigned char *in,
225 /* derive functions */
226 /* From gost_ec_keyx.c */
227 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
229 int fill_GOST_EC_params(EC_KEY *eckey, int nid);
230 int gost_sign_keygen(DSA *dsa);
231 int gost_ec_keygen(EC_KEY *ec);
233 DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey);
235 int gost_do_verify(const unsigned char *dgst, int dgst_len,
236 DSA_SIG *sig, DSA *dsa);
237 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
238 DSA_SIG *sig, EC_KEY *ec);
239 int gost_ec_compute_public(EC_KEY *ec);
240 /*============== miscellaneous functions============================= */
241 /* from gost_sign.c */
242 /* Convert GOST R 34.11 hash sum to bignum according to standard */
243 BIGNUM *hashsum2bn(const unsigned char *dgst, int len);
245 * Store bignum in byte array of given length, prepending by zeros if
248 int store_bignum(const BIGNUM *bn, unsigned char *buf, int len);
249 /* Pack GOST R 34.10 signature according to CryptoPro rules */
250 int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen);
252 /* Get private key as BIGNUM from both 34.10-2001 keys*/
253 /* Returns pointer into EVP_PKEY structure */
254 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey);