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