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