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