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