]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_lcl.h
gost_crypt: Add magma_ctr_acpkm_cipher
[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/asn1t.h>
15 # include <openssl/x509.h>
16 # include <openssl/engine.h>
17 # include <openssl/ec.h>
18 # include "gost89.h"
19 # include "gosthash.h"
20 /* Control commands */
21 # define GOST_PARAM_CRYPT_PARAMS 0
22 # define GOST_PARAM_PBE_PARAMS 1
23 # define GOST_PARAM_PK_FORMAT 2
24 # define GOST_PARAM_MAX 3
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 # define GOST_CTRL_PK_FORMAT   (ENGINE_CMD_BASE+GOST_PARAM_PK_FORMAT)
28
29 typedef struct R3410_ec {
30     int nid;
31     char *a;
32     char *b;
33     char *p;
34     char *q;
35     char *x;
36     char *y;
37     char *cofactor;
38 } R3410_ec_params;
39
40 extern R3410_ec_params R3410_2001_paramset[],
41     *R3410_2012_256_paramset, R3410_2012_512_paramset[];
42
43 extern const ENGINE_CMD_DEFN gost_cmds[];
44 int gost_control_func(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
45 const char *get_gost_engine_param(int param);
46 int gost_set_default_param(int param, const char *value);
47 void gost_param_free(void);
48
49 /* method registration */
50
51 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
52                         const char *pemstr, const char *info);
53 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags);
54
55 /* Gost-specific pmeth control-function parameters */
56 /* For GOST R34.10 parameters */
57 # define param_ctrl_string "paramset"
58 # define ukm_ctrl_string "ukmhex"
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[32];
72     size_t shared_ukm_size;
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 /*   PSKeyTransport ::= SEQUENCE {
119        PSEXP OCTET STRING,
120        ephemeralPublicKey SubjectPublicKeyInfo
121    }
122    SubjectPublicKeyInfo ::= SEQUENCE {
123        algorithm AlgorithmIdentifier,
124        subjectPublicKey BITSTRING
125    }
126    AlgorithmIdentifier ::= SEQUENCE {
127        algorithm OBJECT IDENTIFIER,
128        parameters ANY OPTIONAL
129    }*/
130 typedef struct PSKeyTransport_st {
131     ASN1_OCTET_STRING *psexp;
132     X509_PUBKEY       *ephem_key;
133     ASN1_OCTET_STRING *ukm;
134 } PSKeyTransport_gost;
135
136 DECLARE_ASN1_FUNCTIONS(PSKeyTransport_gost)
137 /*
138  * Hacks to shorten symbols to 31 characters or less, or OpenVMS. This mimics
139  * what's done in symhacks.h, but since this is a very local header file, I
140  * prefered to put this hack directly here. -- Richard Levitte
141  */
142 # ifdef OPENSSL_SYS_VMS
143 #  undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_it
144 #  define GOST_CLIENT_KEY_EXCHANGE_PARAMS_it      GOST_CLIENT_KEY_EXC_PARAMS_it
145 #  undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_new
146 #  define GOST_CLIENT_KEY_EXCHANGE_PARAMS_new     GOST_CLIENT_KEY_EXC_PARAMS_new
147 #  undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_free
148 #  define GOST_CLIENT_KEY_EXCHANGE_PARAMS_free    GOST_CLIENT_KEY_EXC_PARAMS_free
149 #  undef d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS
150 #  define d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS     d2i_GOST_CLIENT_KEY_EXC_PARAMS
151 #  undef i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS
152 #  define i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS     i2d_GOST_CLIENT_KEY_EXC_PARAMS
153 # endif                         /* End of hack */
154 DECLARE_ASN1_FUNCTIONS(GOST_CLIENT_KEY_EXCHANGE_PARAMS)
155 typedef struct {
156     ASN1_OBJECT *key_params;
157     ASN1_OBJECT *hash_params;
158     ASN1_OBJECT *cipher_params;
159 } GOST_KEY_PARAMS;
160
161 DECLARE_ASN1_FUNCTIONS(GOST_KEY_PARAMS)
162
163 typedef struct {
164     ASN1_OCTET_STRING *iv;
165     ASN1_OBJECT *enc_param_set;
166 } GOST_CIPHER_PARAMS;
167
168 DECLARE_ASN1_FUNCTIONS(GOST_CIPHER_PARAMS)
169
170 typedef struct {
171         ASN1_OCTET_STRING *ukm;
172         } GOST2015_CIPHER_PARAMS;
173
174 DECLARE_ASN1_FUNCTIONS(GOST2015_CIPHER_PARAMS)
175
176 typedef struct {
177     ASN1_OCTET_STRING *masked_priv_key;
178     ASN1_OCTET_STRING *public_key;
179 } MASKED_GOST_KEY;
180
181 DECLARE_ASN1_FUNCTIONS(MASKED_GOST_KEY)
182
183 /*============== Message digest  and cipher related structures  ==========*/
184     /*
185      * Structure used as EVP_MD_CTX-md_data. It allows to avoid storing
186      * in the md-data pointers to dynamically allocated memory. I
187      * cannot invent better way to avoid memory leaks, because openssl
188      * insist on invoking Init on Final-ed digests, and there is no
189      * reliable way to find out whether pointer in the passed md_data is
190      * valid or not.
191      */
192 struct ossl_gost_digest_ctx {
193     gost_hash_ctx dctx;
194     gost_ctx cctx;
195 };
196 /* EVP_MD structure for GOST R 34.11 */
197 EVP_MD *digest_gost(void);
198 void digest_gost_destroy(void);
199 /* EVP MD structure for GOST R 34.11-2012 algorithms */
200 EVP_MD *digest_gost2012_256(void);
201 EVP_MD *digest_gost2012_512(void);
202 void digest_gost2012_256_destroy(void);
203 void digest_gost2012_512_destroy(void);
204 /* EVP_MD structure for GOST 28147 in MAC mode */
205 EVP_MD *imit_gost_cpa(void);
206 void imit_gost_cpa_destroy(void);
207 EVP_MD *imit_gost_cp_12(void);
208 void imit_gost_cp_12_destroy(void);
209 EVP_MD *magma_omac(void);
210 void magma_omac_destroy(void);
211 EVP_MD *grasshopper_omac(void);
212 EVP_MD *grasshopper_omac_acpkm(void);
213 void grasshopper_omac_destroy(void);
214 void grasshopper_omac_acpkm_destroy(void);
215 /* Cipher context used for EVP_CIPHER operation */
216 struct ossl_gost_cipher_ctx {
217     int paramNID;
218     unsigned int count;
219     int key_meshing;
220     unsigned char kdf_seed[8];
221     unsigned char tag[8];
222     gost_ctx cctx;
223     EVP_MD_CTX *omac_ctx;
224 };
225 /* Structure to map parameter NID to S-block */
226 struct gost_cipher_info {
227     int nid;
228     gost_subst_block *sblock;
229     int key_meshing;
230 };
231 /* Context for MAC */
232 struct ossl_gost_imit_ctx {
233     gost_ctx cctx;
234     unsigned char buffer[8];
235     unsigned char partial_block[8];
236     unsigned int count;
237     int key_meshing;
238     int bytes_left;
239     int key_set;
240     int dgst_size;
241 };
242 /* Table which maps parameter NID to S-blocks */
243 extern struct gost_cipher_info gost_cipher_list[];
244 /* Find encryption params from ASN1_OBJECT */
245 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj);
246 /* Implementation of GOST 28147-89 cipher in CFB and CNT modes */
247 const EVP_CIPHER *cipher_gost_cpacnt();
248 const EVP_CIPHER *cipher_gost_cpcnt_12();
249 const EVP_CIPHER *cipher_magma_cbc();
250 const EVP_CIPHER *cipher_magma_ctr();
251 const EVP_CIPHER *cipher_magma_ctr_acpkm();
252 const EVP_CIPHER *cipher_magma_ctr_acpkm_omac();
253 const EVP_CIPHER *cipher_magma_wrap();
254 const EVP_CIPHER *cipher_kuznyechik_wrap();
255 void cipher_gost_destroy();
256 void wrap_ciphers_destroy();
257
258 void inc_counter(unsigned char *counter, size_t counter_bytes);
259
260 # define EVP_MD_CTRL_KEY_LEN (EVP_MD_CTRL_ALG_CTRL+3)
261 # define EVP_MD_CTRL_SET_KEY (EVP_MD_CTRL_ALG_CTRL+4)
262 /* EVP_PKEY_METHOD key encryption callbacks */
263 /* From gost_ec_keyx.c */
264 int pkey_gost_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
265                            size_t *out_len, const unsigned char *key,
266                            size_t key_len);
267
268 int pkey_gost_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
269                            size_t *key_len, const unsigned char *in,
270                            size_t in_len);
271 /* derive functions */
272 /* From gost_ec_keyx.c */
273 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
274 int fill_GOST_EC_params(EC_KEY *eckey, int nid);
275 int gost_ec_keygen(EC_KEY *ec);
276
277 ECDSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey);
278 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
279                    ECDSA_SIG *sig, EC_KEY *ec);
280 int gost_ec_compute_public(EC_KEY *ec);
281
282 /* VKO */
283 int VKO_compute_key(unsigned char *shared_key,
284                     const EC_POINT *pub_key, const EC_KEY *priv_key,
285                     const unsigned char *ukm, const size_t ukm_size,
286                     const int vko_dgst_nid);
287
288 /* KDF TREE */
289 int gost_kdftree2012_256(unsigned char *keyout, size_t keyout_len,
290                          const unsigned char *key, size_t keylen,
291                          const unsigned char *label, size_t label_len,
292                          const unsigned char *seed, size_t seed_len,
293                          const size_t representation);
294
295 int gost_tlstree(int cipher_nid, const unsigned char *in, unsigned char *out,
296                  const unsigned char *tlsseq);
297 /* KExp/KImp */
298 int gost_kexp15(const unsigned char *shared_key, const int shared_len,
299                 int cipher_nid, const unsigned char *cipher_key,
300                 int mac_nid, unsigned char *mac_key,
301                 const unsigned char *iv, const size_t ivlen,
302                 unsigned char *out, int *out_len);
303 int gost_kimp15(const unsigned char *expkey, const size_t expkeylen,
304                 int cipher_nid, const unsigned char *cipher_key,
305                 int mac_nid, unsigned char *mac_key,
306                 const unsigned char *iv, const size_t ivlen,
307                 unsigned char *shared_key);
308 /*============== miscellaneous functions============================= */
309 /*
310  * Store bignum in byte array of given length, prepending by zeros if
311  * nesseccary
312  */
313 int store_bignum(const BIGNUM *bn, unsigned char *buf, int len);
314 /* Pack GOST R 34.10 signature according to CryptoPro rules */
315 int pack_sign_cp(ECDSA_SIG *s, int order, unsigned char *sig, size_t *siglen);
316 /* from ameth.c */
317 /* Get private key as BIGNUM from both 34.10-2001 keys*/
318 /* Returns pointer into EVP_PKEY structure */
319 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey);
320
321 /* Struct describing cipher and used for init/deinit.*/
322 struct gost_cipher_st {
323     int nid;
324     EVP_CIPHER *cipher;
325     int block_size;     /* (bytes) */
326     int key_len;        /* (bytes) */
327     int iv_len;
328     int flags;
329     int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key,
330                  const unsigned char *iv, int enc);
331     int (*do_cipher)(EVP_CIPHER_CTX *ctx, unsigned char *out,
332                      const unsigned char *in, size_t inl);
333     int (*cleanup)(EVP_CIPHER_CTX *);
334     int ctx_size;
335     int (*set_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *);
336     int (*get_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *);
337     int (*ctrl)(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
338 };
339 typedef struct gost_cipher_st GOST_cipher;
340
341 EVP_CIPHER *GOST_init_cipher(GOST_cipher *c);
342 void GOST_deinit_cipher(GOST_cipher *c);
343
344 extern GOST_cipher Gost28147_89_cipher;
345 extern GOST_cipher Gost28147_89_cbc_cipher;
346 extern GOST_cipher Gost28147_89_cnt_cipher;
347 extern GOST_cipher Gost28147_89_cnt_12_cipher;
348 extern GOST_cipher magma_ctr_cipher;
349 extern GOST_cipher magma_ctr_acpkm_cipher;
350 #endif
351 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */