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