]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_lcl.h
Explicitely setting cofactor
[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     int peer_key_used;
73 };
74
75 struct gost_mac_pmeth_data {
76     short int key_set;
77     short int mac_size;
78     int mac_param_nid;
79     EVP_MD *md;
80     unsigned char key[32];
81 };
82
83 struct gost_mac_key {
84     int mac_param_nid;
85     unsigned char key[32];
86     short int mac_size;
87 };
88 /* GOST-specific ASN1 structures */
89
90 typedef struct {
91     ASN1_OCTET_STRING *encrypted_key;
92     ASN1_OCTET_STRING *imit;
93 } GOST_KEY_INFO;
94
95 DECLARE_ASN1_FUNCTIONS(GOST_KEY_INFO)
96
97 typedef struct {
98     ASN1_OBJECT *cipher;
99     X509_PUBKEY *ephem_key;
100     ASN1_OCTET_STRING *eph_iv;
101 } GOST_KEY_AGREEMENT_INFO;
102
103 DECLARE_ASN1_FUNCTIONS(GOST_KEY_AGREEMENT_INFO)
104
105 typedef struct {
106     GOST_KEY_INFO *key_info;
107     GOST_KEY_AGREEMENT_INFO *key_agreement_info;
108 } GOST_KEY_TRANSPORT;
109
110 DECLARE_ASN1_FUNCTIONS(GOST_KEY_TRANSPORT)
111
112 typedef struct {                /* FIXME incomplete */
113     GOST_KEY_TRANSPORT *gkt;
114 } GOST_CLIENT_KEY_EXCHANGE_PARAMS;
115
116 /*
117  * Hacks to shorten symbols to 31 characters or less, or OpenVMS. This mimics
118  * what's done in symhacks.h, but since this is a very local header file, I
119  * prefered to put this hack directly here. -- Richard Levitte
120  */
121 # ifdef OPENSSL_SYS_VMS
122 #  undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_it
123 #  define GOST_CLIENT_KEY_EXCHANGE_PARAMS_it      GOST_CLIENT_KEY_EXC_PARAMS_it
124 #  undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_new
125 #  define GOST_CLIENT_KEY_EXCHANGE_PARAMS_new     GOST_CLIENT_KEY_EXC_PARAMS_new
126 #  undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_free
127 #  define GOST_CLIENT_KEY_EXCHANGE_PARAMS_free    GOST_CLIENT_KEY_EXC_PARAMS_free
128 #  undef d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS
129 #  define d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS     d2i_GOST_CLIENT_KEY_EXC_PARAMS
130 #  undef i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS
131 #  define i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS     i2d_GOST_CLIENT_KEY_EXC_PARAMS
132 # endif                         /* End of hack */
133 DECLARE_ASN1_FUNCTIONS(GOST_CLIENT_KEY_EXCHANGE_PARAMS)
134 typedef struct {
135     ASN1_OBJECT *key_params;
136     ASN1_OBJECT *hash_params;
137     ASN1_OBJECT *cipher_params;
138 } GOST_KEY_PARAMS;
139
140 DECLARE_ASN1_FUNCTIONS(GOST_KEY_PARAMS)
141
142 typedef struct {
143     ASN1_OCTET_STRING *iv;
144     ASN1_OBJECT *enc_param_set;
145 } GOST_CIPHER_PARAMS;
146
147 DECLARE_ASN1_FUNCTIONS(GOST_CIPHER_PARAMS)
148
149 typedef struct {
150     ASN1_OCTET_STRING *masked_priv_key;
151     ASN1_OCTET_STRING *public_key;
152 } MASKED_GOST_KEY;
153
154 DECLARE_ASN1_FUNCTIONS(MASKED_GOST_KEY)
155
156 /*============== Message digest  and cipher related structures  ==========*/
157     /*
158      * Structure used as EVP_MD_CTX-md_data. It allows to avoid storing
159      * in the md-data pointers to dynamically allocated memory. I
160      * cannot invent better way to avoid memory leaks, because openssl
161      * insist on invoking Init on Final-ed digests, and there is no
162      * reliable way to find out whether pointer in the passed md_data is
163      * valid or not.
164      */
165 struct ossl_gost_digest_ctx {
166     gost_hash_ctx dctx;
167     gost_ctx cctx;
168 };
169 /* EVP_MD structure for GOST R 34.11 */
170 EVP_MD *digest_gost(void);
171 void digest_gost_destroy(void);
172 /* EVP MD structure for GOST R 34.11-2012 algorithms */
173 EVP_MD *digest_gost2012_256(void);
174 EVP_MD *digest_gost2012_512(void);
175 void digest_gost2012_256_destroy(void);
176 void digest_gost2012_512_destroy(void);
177 /* EVP_MD structure for GOST 28147 in MAC mode */
178 EVP_MD *imit_gost_cpa(void);
179 void imit_gost_cpa_destroy(void);
180 EVP_MD *imit_gost_cp_12(void);
181 void imit_gost_cp_12_destroy(void);
182 /* Cipher context used for EVP_CIPHER operation */
183 struct ossl_gost_cipher_ctx {
184     int paramNID;
185     unsigned int count;
186     int key_meshing;
187     gost_ctx cctx;
188 };
189 /* Structure to map parameter NID to S-block */
190 struct gost_cipher_info {
191     int nid;
192     gost_subst_block *sblock;
193     int key_meshing;
194 };
195 /* Context for MAC */
196 struct ossl_gost_imit_ctx {
197     gost_ctx cctx;
198     unsigned char buffer[8];
199     unsigned char partial_block[8];
200     unsigned int count;
201     int key_meshing;
202     int bytes_left;
203     int key_set;
204     int dgst_size;
205 };
206 /* Table which maps parameter NID to S-blocks */
207 extern struct gost_cipher_info gost_cipher_list[];
208 /* Find encryption params from ASN1_OBJECT */
209 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj);
210 /* Implementation of GOST 28147-89 cipher in CFB and CNT modes */
211 const EVP_CIPHER *cipher_gost();
212 const EVP_CIPHER *cipher_gost_cbc();
213 const EVP_CIPHER *cipher_gost_cpacnt();
214 const EVP_CIPHER *cipher_gost_cpcnt_12();
215 void cipher_gost_destroy();
216 # define EVP_MD_CTRL_KEY_LEN (EVP_MD_CTRL_ALG_CTRL+3)
217 # define EVP_MD_CTRL_SET_KEY (EVP_MD_CTRL_ALG_CTRL+4)
218 # define EVP_MD_CTRL_MAC_LEN (EVP_MD_CTRL_ALG_CTRL+5)
219 /* EVP_PKEY_METHOD key encryption callbacks */
220 /* From gost_ec_keyx.c */
221 int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
222                            size_t *outlen, const unsigned char *key,
223                            size_t key_len);
224
225 int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
226                            size_t *outlen, const unsigned char *in,
227                            size_t in_len);
228 /* derive functions */
229 /* From gost_ec_keyx.c */
230 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
231                         size_t *keylen);
232 int fill_GOST_EC_params(EC_KEY *eckey, int nid);
233 int gost_sign_keygen(DSA *dsa);
234 int gost_ec_keygen(EC_KEY *ec);
235
236 DSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey);
237
238 int gost_do_verify(const unsigned char *dgst, int dgst_len,
239                    DSA_SIG *sig, DSA *dsa);
240 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
241                    DSA_SIG *sig, EC_KEY *ec);
242 int gost_ec_compute_public(EC_KEY *ec);
243 /*============== miscellaneous functions============================= */
244 /* from gost_sign.c */
245 /* Convert GOST R 34.11 hash sum to bignum according to standard */
246 BIGNUM *hashsum2bn(const unsigned char *dgst, int len);
247 /*
248  * Store bignum in byte array of given length, prepending by zeros if
249  * nesseccary
250  */
251 int store_bignum(const BIGNUM *bn, unsigned char *buf, int len);
252 /* Pack GOST R 34.10 signature according to CryptoPro rules */
253 int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen);
254 /* from ameth.c */
255 /* Get private key as BIGNUM from both 34.10-2001 keys*/
256 /* Returns pointer into EVP_PKEY structure */
257 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey);
258
259 #endif