]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_lcl.h
Dealing with new KeyExchange
[openssl-gost/engine.git] / gost_lcl.h
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_PBE_PARAMS 1
24 # define GOST_PARAM_PK_FORMAT 2
25 # define GOST_PARAM_MAX 2
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)
28 # define GOST_CTRL_PK_FORMAT   (ENGINE_CMD_BASE+GOST_PARAM_PK_FORMAT)
29
30 typedef struct R3410_ec {
31     int nid;
32     char *a;
33     char *b;
34     char *p;
35     char *q;
36     char *x;
37     char *y;
38                 char *cofactor;
39 } R3410_ec_params;
40
41 extern R3410_ec_params R3410_2001_paramset[],
42     *R3410_2012_256_paramset, R3410_2012_512_paramset[];
43
44 extern const ENGINE_CMD_DEFN gost_cmds[];
45 int gost_control_func(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
46 const char *get_gost_engine_param(int param);
47 int gost_set_default_param(int param, const char *value);
48 void gost_param_free(void);
49
50 /* method registration */
51
52 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
53                         const char *pemstr, const char *info);
54 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags);
55
56 /* Gost-specific pmeth control-function parameters */
57 /* For GOST R34.10 parameters */
58 # define param_ctrl_string "paramset"
59 # define EVP_PKEY_CTRL_GOST_PARAMSET (EVP_PKEY_ALG_CTRL+1)
60 /* For GOST 28147 MAC */
61 # define key_ctrl_string "key"
62 # define hexkey_ctrl_string "hexkey"
63 # define maclen_ctrl_string "size"
64 # define EVP_PKEY_CTRL_GOST_MAC_HEXKEY (EVP_PKEY_ALG_CTRL+3)
65 # define EVP_PKEY_CTRL_MAC_LEN (EVP_PKEY_ALG_CTRL+5)
66 /* Pmeth internal representation */
67 struct gost_pmeth_data {
68     int sign_param_nid;         /* Should be set whenever parameters are
69                                  * filled */
70     EVP_MD *md;
71     unsigned char *shared_ukm;
72     size_t shared_ukm_size; /* XXX temporary use shared_ukm and hash for 2018 CKE*/
73     int peer_key_used;
74     int cipher_nid; /* KExp15/KImp15 algs*/
75 };
76
77 struct gost_mac_pmeth_data {
78     short int key_set;
79     short int mac_size;
80     int mac_param_nid;
81     EVP_MD *md;
82     unsigned char key[32];
83 };
84
85 struct gost_mac_key {
86     int mac_param_nid;
87     unsigned char key[32];
88     short int mac_size;
89 };
90 /* GOST-specific ASN1 structures */
91
92 typedef struct {
93     ASN1_OCTET_STRING *encrypted_key;
94     ASN1_OCTET_STRING *imit;
95 } GOST_KEY_INFO;
96
97 DECLARE_ASN1_FUNCTIONS(GOST_KEY_INFO)
98
99 typedef struct {
100     ASN1_OBJECT *cipher;
101     X509_PUBKEY *ephem_key;
102     ASN1_OCTET_STRING *eph_iv;
103 } GOST_KEY_AGREEMENT_INFO;
104
105 DECLARE_ASN1_FUNCTIONS(GOST_KEY_AGREEMENT_INFO)
106
107 typedef struct {
108     GOST_KEY_INFO *key_info;
109     GOST_KEY_AGREEMENT_INFO *key_agreement_info;
110 } GOST_KEY_TRANSPORT;
111
112 DECLARE_ASN1_FUNCTIONS(GOST_KEY_TRANSPORT)
113
114 typedef struct {                /* FIXME incomplete */
115     GOST_KEY_TRANSPORT *gkt;
116 } GOST_CLIENT_KEY_EXCHANGE_PARAMS;
117
118 /*
119  * Hacks to shorten symbols to 31 characters or less, or OpenVMS. This mimics
120  * what's done in symhacks.h, but since this is a very local header file, I
121  * prefered to put this hack directly here. -- Richard Levitte
122  */
123 # ifdef OPENSSL_SYS_VMS
124 #  undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_it
125 #  define GOST_CLIENT_KEY_EXCHANGE_PARAMS_it      GOST_CLIENT_KEY_EXC_PARAMS_it
126 #  undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_new
127 #  define GOST_CLIENT_KEY_EXCHANGE_PARAMS_new     GOST_CLIENT_KEY_EXC_PARAMS_new
128 #  undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_free
129 #  define GOST_CLIENT_KEY_EXCHANGE_PARAMS_free    GOST_CLIENT_KEY_EXC_PARAMS_free
130 #  undef d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS
131 #  define d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS     d2i_GOST_CLIENT_KEY_EXC_PARAMS
132 #  undef i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS
133 #  define i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS     i2d_GOST_CLIENT_KEY_EXC_PARAMS
134 # endif                         /* End of hack */
135 DECLARE_ASN1_FUNCTIONS(GOST_CLIENT_KEY_EXCHANGE_PARAMS)
136 typedef struct {
137     ASN1_OBJECT *key_params;
138     ASN1_OBJECT *hash_params;
139     ASN1_OBJECT *cipher_params;
140 } GOST_KEY_PARAMS;
141
142 DECLARE_ASN1_FUNCTIONS(GOST_KEY_PARAMS)
143
144 typedef struct {
145     ASN1_OCTET_STRING *iv;
146     ASN1_OBJECT *enc_param_set;
147 } GOST_CIPHER_PARAMS;
148
149 DECLARE_ASN1_FUNCTIONS(GOST_CIPHER_PARAMS)
150
151 typedef struct {
152     ASN1_OCTET_STRING *masked_priv_key;
153     ASN1_OCTET_STRING *public_key;
154 } MASKED_GOST_KEY;
155
156 DECLARE_ASN1_FUNCTIONS(MASKED_GOST_KEY)
157
158 /*============== Message digest  and cipher related structures  ==========*/
159     /*
160      * Structure used as EVP_MD_CTX-md_data. It allows to avoid storing
161      * in the md-data pointers to dynamically allocated memory. I
162      * cannot invent better way to avoid memory leaks, because openssl
163      * insist on invoking Init on Final-ed digests, and there is no
164      * reliable way to find out whether pointer in the passed md_data is
165      * valid or not.
166      */
167 struct ossl_gost_digest_ctx {
168     gost_hash_ctx dctx;
169     gost_ctx cctx;
170 };
171 /* EVP_MD structure for GOST R 34.11 */
172 EVP_MD *digest_gost(void);
173 void digest_gost_destroy(void);
174 /* EVP MD structure for GOST R 34.11-2012 algorithms */
175 EVP_MD *digest_gost2012_256(void);
176 EVP_MD *digest_gost2012_512(void);
177 void digest_gost2012_256_destroy(void);
178 void digest_gost2012_512_destroy(void);
179 /* EVP_MD structure for GOST 28147 in MAC mode */
180 EVP_MD *imit_gost_cpa(void);
181 void imit_gost_cpa_destroy(void);
182 EVP_MD *imit_gost_cp_12(void);
183 void imit_gost_cp_12_destroy(void);
184 EVP_MD *magma_omac(void);
185 void magma_omac_destroy(void);
186 EVP_MD *grasshopper_omac(void);
187 EVP_MD *grasshopper_omac_acpkm(void);
188 void grasshopper_omac_destroy(void);
189 void grasshopper_omac_acpkm_destroy(void);
190 /* Cipher context used for EVP_CIPHER operation */
191 struct ossl_gost_cipher_ctx {
192     int paramNID;
193     unsigned int count;
194     int key_meshing;
195     gost_ctx cctx;
196 };
197 /* Structure to map parameter NID to S-block */
198 struct gost_cipher_info {
199     int nid;
200     gost_subst_block *sblock;
201     int key_meshing;
202 };
203 /* Context for MAC */
204 struct ossl_gost_imit_ctx {
205     gost_ctx cctx;
206     unsigned char buffer[8];
207     unsigned char partial_block[8];
208     unsigned int count;
209     int key_meshing;
210     int bytes_left;
211     int key_set;
212     int dgst_size;
213 };
214 /* Table which maps parameter NID to S-blocks */
215 extern struct gost_cipher_info gost_cipher_list[];
216 /* Find encryption params from ASN1_OBJECT */
217 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj);
218 /* Implementation of GOST 28147-89 cipher in CFB and CNT modes */
219 const EVP_CIPHER *cipher_gost();
220 const EVP_CIPHER *cipher_gost_cbc();
221 const EVP_CIPHER *cipher_gost_cpacnt();
222 const EVP_CIPHER *cipher_gost_cpcnt_12();
223 const EVP_CIPHER *cipher_magma_cbc();
224 const EVP_CIPHER *cipher_magma_ctr();
225 void cipher_gost_destroy();
226
227 void inc_counter(unsigned char* buffer, size_t buf_len);
228
229 # define EVP_MD_CTRL_KEY_LEN (EVP_MD_CTRL_ALG_CTRL+3)
230 # define EVP_MD_CTRL_SET_KEY (EVP_MD_CTRL_ALG_CTRL+4)
231 # define EVP_MD_CTRL_MAC_LEN (EVP_MD_CTRL_ALG_CTRL+5)
232 /* EVP_PKEY_METHOD key encryption callbacks */
233 /* From gost_ec_keyx.c */
234 int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
235                            size_t *outlen, const unsigned char *key,
236                            size_t key_len);
237
238 int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
239                            size_t *outlen, const unsigned char *in,
240                            size_t in_len);
241 /* derive functions */
242 /* From gost_ec_keyx.c */
243 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
244                         size_t *keylen);
245 int fill_GOST_EC_params(EC_KEY *eckey, int nid);
246 int gost_sign_keygen(DSA *dsa);
247 int gost_ec_keygen(EC_KEY *ec);
248
249 DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey);
250
251 int gost_do_verify(const unsigned char *dgst, int dgst_len,
252                    DSA_SIG *sig, DSA *dsa);
253 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
254                    DSA_SIG *sig, EC_KEY *ec);
255 int gost_ec_compute_public(EC_KEY *ec);
256 /*============== miscellaneous functions============================= */
257 /* from gost_sign.c */
258 /* Convert GOST R 34.11 hash sum to bignum according to standard */
259 BIGNUM *hashsum2bn(const unsigned char *dgst, int len);
260 /*
261  * Store bignum in byte array of given length, prepending by zeros if
262  * nesseccary
263  */
264 int store_bignum(const BIGNUM *bn, unsigned char *buf, int len);
265 /* Pack GOST R 34.10 signature according to CryptoPro rules */
266 int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen);
267 /* from ameth.c */
268 /* Get private key as BIGNUM from both 34.10-2001 keys*/
269 /* Returns pointer into EVP_PKEY structure */
270 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey);
271
272 #endif