]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_lcl.h
Delete .travis.yml
[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  *             Copyright (c) 2020 Vitaly Chikunov <vt@altlinux.org>   *
7  *       This file is distributed under the same license as OpenSSL   *
8  *                                                                    *
9  *         Internal declarations  used in GOST engine                 *
10  *         OpenSSL 0.9.9 libraries required to compile and use        *
11  *                              this code                             *
12  **********************************************************************/
13 # include <openssl/bn.h>
14 # include <openssl/evp.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 3
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 ukm_ctrl_string "ukmhex"
60 # define vko_ctrl_string "vko"
61 # define EVP_PKEY_CTRL_GOST_PARAMSET (EVP_PKEY_ALG_CTRL+1)
62 /* For GOST 28147 MAC */
63 # define key_ctrl_string "key"
64 # define hexkey_ctrl_string "hexkey"
65 # define maclen_ctrl_string "size"
66 # define EVP_PKEY_CTRL_GOST_MAC_HEXKEY (EVP_PKEY_ALG_CTRL+3)
67 # define EVP_PKEY_CTRL_MAC_LEN (EVP_PKEY_ALG_CTRL+5)
68 # define EVP_PKEY_CTRL_SET_VKO (EVP_PKEY_ALG_CTRL+11)
69 # define TLSTREE_MODE_NONE                                  0
70 # define TLSTREE_MODE_S                                     1
71 # define TLSTREE_MODE_L                                     2
72 /* Pmeth internal representation */
73 struct gost_pmeth_data {
74     int sign_param_nid;         /* Should be set whenever parameters are
75                                  * filled */
76     EVP_MD *md;
77     unsigned char shared_ukm[32];
78     size_t shared_ukm_size;
79     int peer_key_used;
80     int cipher_nid;             /* KExp15/KImp15 algs */
81     int vko_dgst_nid;
82     char derive_mode;
83 };
84
85 struct gost_mac_pmeth_data {
86     short int key_set;
87     short int mac_size;
88     int mac_param_nid;
89     EVP_MD *md;
90     unsigned char key[32];
91 };
92
93 struct gost_mac_key {
94     int mac_param_nid;
95     unsigned char key[32];
96     short int mac_size;
97 };
98 /* GOST-specific ASN1 structures */
99
100 typedef struct {
101     ASN1_OCTET_STRING *encrypted_key;
102     ASN1_OCTET_STRING *imit;
103 } GOST_KEY_INFO;
104
105 DECLARE_ASN1_FUNCTIONS(GOST_KEY_INFO)
106
107 typedef struct {
108     ASN1_OBJECT *cipher;
109     X509_PUBKEY *ephem_key;
110     ASN1_OCTET_STRING *eph_iv;
111 } GOST_KEY_AGREEMENT_INFO;
112
113 DECLARE_ASN1_FUNCTIONS(GOST_KEY_AGREEMENT_INFO)
114
115 typedef struct {
116     GOST_KEY_INFO *key_info;
117     GOST_KEY_AGREEMENT_INFO *key_agreement_info;
118 } GOST_KEY_TRANSPORT;
119
120 DECLARE_ASN1_FUNCTIONS(GOST_KEY_TRANSPORT)
121
122 typedef struct {                /* FIXME incomplete */
123     GOST_KEY_TRANSPORT *gkt;
124 } GOST_CLIENT_KEY_EXCHANGE_PARAMS;
125
126 /*   PSKeyTransport ::= SEQUENCE {
127        PSEXP OCTET STRING,
128        ephemeralPublicKey SubjectPublicKeyInfo
129    }
130    SubjectPublicKeyInfo ::= SEQUENCE {
131        algorithm AlgorithmIdentifier,
132        subjectPublicKey BITSTRING
133    }
134    AlgorithmIdentifier ::= SEQUENCE {
135        algorithm OBJECT IDENTIFIER,
136        parameters ANY OPTIONAL
137    }*/
138 typedef struct PSKeyTransport_st {
139     ASN1_OCTET_STRING *psexp;
140     X509_PUBKEY       *ephem_key;
141     ASN1_OCTET_STRING *ukm;
142 } PSKeyTransport_gost;
143
144 DECLARE_ASN1_FUNCTIONS(PSKeyTransport_gost)
145 /*
146  * Hacks to shorten symbols to 31 characters or less, or OpenVMS. This mimics
147  * what's done in symhacks.h, but since this is a very local header file, I
148  * prefered to put this hack directly here. -- Richard Levitte
149  */
150 # ifdef OPENSSL_SYS_VMS
151 #  undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_it
152 #  define GOST_CLIENT_KEY_EXCHANGE_PARAMS_it      GOST_CLIENT_KEY_EXC_PARAMS_it
153 #  undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_new
154 #  define GOST_CLIENT_KEY_EXCHANGE_PARAMS_new     GOST_CLIENT_KEY_EXC_PARAMS_new
155 #  undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_free
156 #  define GOST_CLIENT_KEY_EXCHANGE_PARAMS_free    GOST_CLIENT_KEY_EXC_PARAMS_free
157 #  undef d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS
158 #  define d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS     d2i_GOST_CLIENT_KEY_EXC_PARAMS
159 #  undef i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS
160 #  define i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS     i2d_GOST_CLIENT_KEY_EXC_PARAMS
161 # endif                         /* End of hack */
162 DECLARE_ASN1_FUNCTIONS(GOST_CLIENT_KEY_EXCHANGE_PARAMS)
163 typedef struct {
164     ASN1_OBJECT *key_params;
165     ASN1_OBJECT *hash_params;
166     ASN1_OBJECT *cipher_params;
167 } GOST_KEY_PARAMS;
168
169 DECLARE_ASN1_FUNCTIONS(GOST_KEY_PARAMS)
170
171 typedef struct {
172     ASN1_OCTET_STRING *iv;
173     ASN1_OBJECT *enc_param_set;
174 } GOST_CIPHER_PARAMS;
175
176 DECLARE_ASN1_FUNCTIONS(GOST_CIPHER_PARAMS)
177
178 typedef struct {
179         ASN1_OCTET_STRING *ukm;
180         } GOST2015_CIPHER_PARAMS;
181
182 DECLARE_ASN1_FUNCTIONS(GOST2015_CIPHER_PARAMS)
183
184 typedef struct {
185     ASN1_OCTET_STRING *masked_priv_key;
186     ASN1_OCTET_STRING *public_key;
187 } MASKED_GOST_KEY;
188
189 DECLARE_ASN1_FUNCTIONS(MASKED_GOST_KEY)
190
191 /*============== Message digest  and cipher related structures  ==========*/
192     /*
193      * Structure used as EVP_MD_CTX-md_data. It allows to avoid storing
194      * in the md-data pointers to dynamically allocated memory. I
195      * cannot invent better way to avoid memory leaks, because openssl
196      * insist on invoking Init on Final-ed digests, and there is no
197      * reliable way to find out whether pointer in the passed md_data is
198      * valid or not.
199      */
200 struct ossl_gost_digest_ctx {
201     gost_hash_ctx dctx;
202     gost_ctx cctx;
203 };
204 /* Cipher context used for EVP_CIPHER operation */
205 struct ossl_gost_cipher_ctx {
206     int paramNID;
207     unsigned int count;
208     int key_meshing;
209     unsigned char kdf_seed[8];
210     unsigned char tag[8];
211     gost_ctx cctx;
212     EVP_MD_CTX *omac_ctx;
213 };
214 /* Structure to map parameter NID to S-block */
215 struct gost_cipher_info {
216     int nid;
217     gost_subst_block *sblock;
218     int key_meshing;
219 };
220 /* Context for MAC */
221 struct ossl_gost_imit_ctx {
222     gost_ctx cctx;
223     unsigned char buffer[8];
224     unsigned char partial_block[8];
225     unsigned int count;
226     int key_meshing;
227     int bytes_left;
228     int key_set;
229     int dgst_size;
230 };
231 /* Find encryption params from ASN1_OBJECT */
232 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj);
233
234 void inc_counter(unsigned char *counter, size_t counter_bytes);
235
236 # define EVP_MD_CTRL_KEY_LEN (EVP_MD_CTRL_ALG_CTRL+3)
237 # define EVP_MD_CTRL_SET_KEY (EVP_MD_CTRL_ALG_CTRL+4)
238 /* EVP_PKEY_METHOD key encryption callbacks */
239 /* From gost_ec_keyx.c */
240 int pkey_gost_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
241                            size_t *out_len, const unsigned char *key,
242                            size_t key_len);
243
244 int pkey_gost_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
245                            size_t *key_len, const unsigned char *in,
246                            size_t in_len);
247 /* derive functions */
248 /* From gost_ec_keyx.c */
249 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
250 int fill_GOST_EC_params(EC_KEY *eckey, int nid);
251 int gost_ec_keygen(EC_KEY *ec);
252
253 ECDSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey);
254 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
255                    ECDSA_SIG *sig, EC_KEY *ec);
256 int gost_ec_compute_public(EC_KEY *ec);
257 int gost_ec_point_mul(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n,
258                       const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx);
259 size_t gost_ec_key2buf(const EC_KEY *key, unsigned char **pbuf);
260 int gost_ec_oct2key(EC_KEY *key, const unsigned char *buf, size_t len);
261
262 #define CURVEDEF(a) \
263 int point_mul_##a(const EC_GROUP *group, EC_POINT *r, const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx);\
264 int point_mul_g_##a(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n, BN_CTX *ctx);\
265 int point_mul_two_##a(const EC_GROUP *group, EC_POINT *r, const BIGNUM *n, const EC_POINT *q, const BIGNUM *m, BN_CTX *ctx);
266
267 CURVEDEF(id_GostR3410_2001_CryptoPro_A_ParamSet)
268 CURVEDEF(id_GostR3410_2001_CryptoPro_B_ParamSet)
269 CURVEDEF(id_GostR3410_2001_CryptoPro_C_ParamSet)
270 CURVEDEF(id_GostR3410_2001_TestParamSet)
271 CURVEDEF(id_tc26_gost_3410_2012_256_paramSetA)
272 CURVEDEF(id_tc26_gost_3410_2012_512_paramSetA)
273 CURVEDEF(id_tc26_gost_3410_2012_512_paramSetB)
274 CURVEDEF(id_tc26_gost_3410_2012_512_paramSetC)
275
276 /* VKO */
277 int VKO_compute_key(unsigned char *shared_key,
278                     const EC_POINT *pub_key, const EC_KEY *priv_key,
279                     const unsigned char *ukm, const size_t ukm_size,
280                     const int vko_dgst_nid);
281
282 /* KDF TREE */
283 int gost_kdftree2012_256(unsigned char *keyout, size_t keyout_len,
284                          const unsigned char *key, size_t keylen,
285                          const unsigned char *label, size_t label_len,
286                          const unsigned char *seed, size_t seed_len,
287                          const size_t representation);
288
289 int gost_tlstree(int cipher_nid, const unsigned char *in, unsigned char *out,
290                  const unsigned char *tlsseq, int mode);
291 /* KExp/KImp */
292 int gost_kexp15(const unsigned char *shared_key, const int shared_len,
293                 int cipher_nid, const unsigned char *cipher_key,
294                 int mac_nid, unsigned char *mac_key,
295                 const unsigned char *iv, const size_t ivlen,
296                 unsigned char *out, int *out_len);
297 int gost_kimp15(const unsigned char *expkey, const size_t expkeylen,
298                 int cipher_nid, const unsigned char *cipher_key,
299                 int mac_nid, unsigned char *mac_key,
300                 const unsigned char *iv, const size_t ivlen,
301                 unsigned char *shared_key);
302 /*============== miscellaneous functions============================= */
303 /*
304  * Store bignum in byte array of given length, prepending by zeros if
305  * nesseccary
306  */
307 int store_bignum(const BIGNUM *bn, unsigned char *buf, int len);
308 /* Pack GOST R 34.10 signature according to CryptoPro rules */
309 int pack_sign_cp(ECDSA_SIG *s, int order, unsigned char *sig, size_t *siglen);
310 /* from ameth.c */
311 /* Get private key as BIGNUM from both 34.10-2001 keys*/
312 /* Returns pointer into EVP_PKEY structure */
313 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey);
314 /* from gost_crypt.c */
315 /* Decrements 8-byte sequence */ 
316 int decrement_sequence(unsigned char *seq, int decrement);
317
318 /* Struct describing cipher and used for init/deinit.*/
319 struct gost_cipher_st {
320     struct gost_cipher_st *template; /* template struct */
321     int nid;
322     EVP_CIPHER *cipher;
323     int block_size;     /* (bytes) */
324     int key_len;        /* (bytes) */
325     int iv_len;
326     int flags;
327     int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key,
328                  const unsigned char *iv, int enc);
329     int (*do_cipher)(EVP_CIPHER_CTX *ctx, unsigned char *out,
330                      const unsigned char *in, size_t inl);
331     int (*cleanup)(EVP_CIPHER_CTX *);
332     int ctx_size;
333     int (*set_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *);
334     int (*get_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *);
335     int (*ctrl)(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
336 };
337 typedef struct gost_cipher_st GOST_cipher;
338
339 EVP_CIPHER *GOST_init_cipher(GOST_cipher *c);
340 void GOST_deinit_cipher(GOST_cipher *c);
341
342 extern GOST_cipher Gost28147_89_cipher;
343 extern GOST_cipher Gost28147_89_cbc_cipher;
344 extern GOST_cipher Gost28147_89_cnt_cipher;
345 extern GOST_cipher Gost28147_89_cnt_12_cipher;
346 extern GOST_cipher magma_ctr_cipher;
347 extern GOST_cipher magma_ctr_acpkm_cipher;
348 extern GOST_cipher magma_ctr_acpkm_omac_cipher;
349 extern GOST_cipher magma_cbc_cipher;
350 extern GOST_cipher magma_mgm_cipher;
351 extern GOST_cipher grasshopper_ecb_cipher;
352 extern GOST_cipher grasshopper_cbc_cipher;
353 extern GOST_cipher grasshopper_cfb_cipher;
354 extern GOST_cipher grasshopper_ofb_cipher;
355 extern GOST_cipher grasshopper_ctr_cipher;
356 extern GOST_cipher grasshopper_mgm_cipher;
357 extern GOST_cipher grasshopper_ctr_acpkm_cipher;
358 extern GOST_cipher grasshopper_ctr_acpkm_omac_cipher;
359 extern GOST_cipher magma_kexp15_cipher;
360 extern GOST_cipher kuznyechik_kexp15_cipher;
361
362 struct gost_digest_st {
363     struct gost_digest_st *template;
364     int nid;
365     const char *alias;
366     EVP_MD *digest;
367     int result_size;
368     int input_blocksize;
369     int app_datasize;
370     int flags;
371     int (*init)(EVP_MD_CTX *ctx);
372     int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count);
373     int (*final)(EVP_MD_CTX *ctx, unsigned char *md);
374     int (*copy)(EVP_MD_CTX *to, const EVP_MD_CTX *from);
375     int (*cleanup)(EVP_MD_CTX *ctx);
376     int (*ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
377 };
378 typedef struct gost_digest_st GOST_digest;
379
380 EVP_MD *GOST_init_digest(GOST_digest *d);
381 void GOST_deinit_digest(GOST_digest *d);
382
383 extern GOST_digest GostR3411_94_digest;
384 extern GOST_digest Gost28147_89_MAC_digest;
385 extern GOST_digest Gost28147_89_mac_12_digest;
386 extern GOST_digest GostR3411_2012_256_digest;
387 extern GOST_digest GostR3411_2012_512_digest;
388 extern GOST_digest magma_mac_digest;
389 extern GOST_digest grasshopper_mac_digest;
390 extern GOST_digest kuznyechik_ctracpkm_omac_digest;
391
392 #endif
393 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */