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