3 /**********************************************************************
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 *
9 * Internal declarations used in GOST engine *
10 * OpenSSL 0.9.9 libraries required to compile and use *
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>
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)
30 typedef struct R3410_ec {
41 extern R3410_ec_params R3410_2001_paramset[],
42 *R3410_2012_256_paramset, R3410_2012_512_paramset[];
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);
50 /* method registration */
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);
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 /* Pmeth internal representation */
68 struct gost_pmeth_data {
69 int sign_param_nid; /* Should be set whenever parameters are
72 unsigned char shared_ukm[32];
73 size_t shared_ukm_size;
75 int cipher_nid; /* KExp15/KImp15 algs */
78 struct gost_mac_pmeth_data {
83 unsigned char key[32];
88 unsigned char key[32];
91 /* GOST-specific ASN1 structures */
94 ASN1_OCTET_STRING *encrypted_key;
95 ASN1_OCTET_STRING *imit;
98 DECLARE_ASN1_FUNCTIONS(GOST_KEY_INFO)
102 X509_PUBKEY *ephem_key;
103 ASN1_OCTET_STRING *eph_iv;
104 } GOST_KEY_AGREEMENT_INFO;
106 DECLARE_ASN1_FUNCTIONS(GOST_KEY_AGREEMENT_INFO)
109 GOST_KEY_INFO *key_info;
110 GOST_KEY_AGREEMENT_INFO *key_agreement_info;
111 } GOST_KEY_TRANSPORT;
113 DECLARE_ASN1_FUNCTIONS(GOST_KEY_TRANSPORT)
115 typedef struct { /* FIXME incomplete */
116 GOST_KEY_TRANSPORT *gkt;
117 } GOST_CLIENT_KEY_EXCHANGE_PARAMS;
119 /* PSKeyTransport ::= SEQUENCE {
121 ephemeralPublicKey SubjectPublicKeyInfo
123 SubjectPublicKeyInfo ::= SEQUENCE {
124 algorithm AlgorithmIdentifier,
125 subjectPublicKey BITSTRING
127 AlgorithmIdentifier ::= SEQUENCE {
128 algorithm OBJECT IDENTIFIER,
129 parameters ANY OPTIONAL
131 typedef struct PSKeyTransport_st {
132 ASN1_OCTET_STRING *psexp;
133 X509_PUBKEY *ephem_key;
134 ASN1_OCTET_STRING *ukm;
135 } PSKeyTransport_gost;
137 DECLARE_ASN1_FUNCTIONS(PSKeyTransport_gost)
139 * Hacks to shorten symbols to 31 characters or less, or OpenVMS. This mimics
140 * what's done in symhacks.h, but since this is a very local header file, I
141 * prefered to put this hack directly here. -- Richard Levitte
143 # ifdef OPENSSL_SYS_VMS
144 # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_it
145 # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_it GOST_CLIENT_KEY_EXC_PARAMS_it
146 # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_new
147 # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_new GOST_CLIENT_KEY_EXC_PARAMS_new
148 # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_free
149 # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_free GOST_CLIENT_KEY_EXC_PARAMS_free
150 # undef d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS
151 # define d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS d2i_GOST_CLIENT_KEY_EXC_PARAMS
152 # undef i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS
153 # define i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS i2d_GOST_CLIENT_KEY_EXC_PARAMS
154 # endif /* End of hack */
155 DECLARE_ASN1_FUNCTIONS(GOST_CLIENT_KEY_EXCHANGE_PARAMS)
157 ASN1_OBJECT *key_params;
158 ASN1_OBJECT *hash_params;
159 ASN1_OBJECT *cipher_params;
162 DECLARE_ASN1_FUNCTIONS(GOST_KEY_PARAMS)
165 ASN1_OCTET_STRING *iv;
166 ASN1_OBJECT *enc_param_set;
167 } GOST_CIPHER_PARAMS;
169 DECLARE_ASN1_FUNCTIONS(GOST_CIPHER_PARAMS)
172 ASN1_OCTET_STRING *ukm;
173 } GOST2015_CIPHER_PARAMS;
175 DECLARE_ASN1_FUNCTIONS(GOST2015_CIPHER_PARAMS)
178 ASN1_OCTET_STRING *masked_priv_key;
179 ASN1_OCTET_STRING *public_key;
182 DECLARE_ASN1_FUNCTIONS(MASKED_GOST_KEY)
184 /*============== Message digest and cipher related structures ==========*/
186 * Structure used as EVP_MD_CTX-md_data. It allows to avoid storing
187 * in the md-data pointers to dynamically allocated memory. I
188 * cannot invent better way to avoid memory leaks, because openssl
189 * insist on invoking Init on Final-ed digests, and there is no
190 * reliable way to find out whether pointer in the passed md_data is
193 struct ossl_gost_digest_ctx {
197 /* EVP_MD structure for GOST R 34.11 */
198 EVP_MD *digest_gost(void);
199 void digest_gost_destroy(void);
200 /* EVP MD structure for GOST R 34.11-2012 algorithms */
201 EVP_MD *digest_gost2012_256(void);
202 EVP_MD *digest_gost2012_512(void);
203 void digest_gost2012_256_destroy(void);
204 void digest_gost2012_512_destroy(void);
205 /* EVP_MD structure for GOST 28147 in MAC mode */
206 EVP_MD *imit_gost_cpa(void);
207 void imit_gost_cpa_destroy(void);
208 EVP_MD *imit_gost_cp_12(void);
209 void imit_gost_cp_12_destroy(void);
210 EVP_MD *magma_omac(void);
211 void magma_omac_destroy(void);
212 EVP_MD *grasshopper_omac(void);
213 EVP_MD *grasshopper_omac_acpkm(void);
214 void grasshopper_omac_destroy(void);
215 void grasshopper_omac_acpkm_destroy(void);
216 /* Cipher context used for EVP_CIPHER operation */
217 struct ossl_gost_cipher_ctx {
221 unsigned char kdf_seed[8];
222 unsigned char tag[8];
224 EVP_MD_CTX *omac_ctx;
226 /* Structure to map parameter NID to S-block */
227 struct gost_cipher_info {
229 gost_subst_block *sblock;
232 /* Context for MAC */
233 struct ossl_gost_imit_ctx {
235 unsigned char buffer[8];
236 unsigned char partial_block[8];
243 /* Table which maps parameter NID to S-blocks */
244 extern struct gost_cipher_info gost_cipher_list[];
245 /* Find encryption params from ASN1_OBJECT */
246 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj);
247 /* Implementation of GOST 28147-89 cipher in CFB and CNT modes */
248 const EVP_CIPHER *cipher_gost_cpacnt();
249 const EVP_CIPHER *cipher_gost_cpcnt_12();
250 const EVP_CIPHER *cipher_magma_cbc();
251 const EVP_CIPHER *cipher_magma_ctr();
252 const EVP_CIPHER *cipher_magma_ctr_acpkm();
253 const EVP_CIPHER *cipher_magma_ctr_acpkm_omac();
254 const EVP_CIPHER *cipher_magma_wrap();
255 const EVP_CIPHER *cipher_kuznyechik_wrap();
256 void cipher_gost_destroy();
257 void wrap_ciphers_destroy();
259 void inc_counter(unsigned char *counter, size_t counter_bytes);
261 # define EVP_MD_CTRL_KEY_LEN (EVP_MD_CTRL_ALG_CTRL+3)
262 # define EVP_MD_CTRL_SET_KEY (EVP_MD_CTRL_ALG_CTRL+4)
263 /* EVP_PKEY_METHOD key encryption callbacks */
264 /* From gost_ec_keyx.c */
265 int pkey_gost_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
266 size_t *out_len, const unsigned char *key,
269 int pkey_gost_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
270 size_t *key_len, const unsigned char *in,
272 /* derive functions */
273 /* From gost_ec_keyx.c */
274 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
275 int fill_GOST_EC_params(EC_KEY *eckey, int nid);
276 int gost_ec_keygen(EC_KEY *ec);
278 ECDSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey);
279 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
280 ECDSA_SIG *sig, EC_KEY *ec);
281 int gost_ec_compute_public(EC_KEY *ec);
284 int VKO_compute_key(unsigned char *shared_key,
285 const EC_POINT *pub_key, const EC_KEY *priv_key,
286 const unsigned char *ukm, const size_t ukm_size,
287 const int vko_dgst_nid);
290 int gost_kdftree2012_256(unsigned char *keyout, size_t keyout_len,
291 const unsigned char *key, size_t keylen,
292 const unsigned char *label, size_t label_len,
293 const unsigned char *seed, size_t seed_len,
294 const size_t representation);
296 int gost_tlstree(int cipher_nid, const unsigned char *in, unsigned char *out,
297 const unsigned char *tlsseq);
299 int gost_kexp15(const unsigned char *shared_key, const int shared_len,
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 *out, int *out_len);
304 int gost_kimp15(const unsigned char *expkey, const size_t expkeylen,
305 int cipher_nid, const unsigned char *cipher_key,
306 int mac_nid, unsigned char *mac_key,
307 const unsigned char *iv, const size_t ivlen,
308 unsigned char *shared_key);
309 /*============== miscellaneous functions============================= */
311 * Store bignum in byte array of given length, prepending by zeros if
314 int store_bignum(const BIGNUM *bn, unsigned char *buf, int len);
315 /* Pack GOST R 34.10 signature according to CryptoPro rules */
316 int pack_sign_cp(ECDSA_SIG *s, int order, unsigned char *sig, size_t *siglen);
318 /* Get private key as BIGNUM from both 34.10-2001 keys*/
319 /* Returns pointer into EVP_PKEY structure */
320 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey);
322 /* Struct describing cipher and used for init/deinit.*/
323 struct gost_cipher_st {
324 struct gost_cipher_st *template; /* template struct */
327 int block_size; /* (bytes) */
328 int key_len; /* (bytes) */
331 int (*init) (EVP_CIPHER_CTX *ctx, const unsigned char *key,
332 const unsigned char *iv, int enc);
333 int (*do_cipher)(EVP_CIPHER_CTX *ctx, unsigned char *out,
334 const unsigned char *in, size_t inl);
335 int (*cleanup)(EVP_CIPHER_CTX *);
337 int (*set_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *);
338 int (*get_asn1_parameters)(EVP_CIPHER_CTX *, ASN1_TYPE *);
339 int (*ctrl)(EVP_CIPHER_CTX *, int type, int arg, void *ptr);
341 typedef struct gost_cipher_st GOST_cipher;
343 EVP_CIPHER *GOST_init_cipher(GOST_cipher *c);
344 void GOST_deinit_cipher(GOST_cipher *c);
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_cbc_cipher;
354 extern GOST_cipher grasshopper_ecb_cipher;
355 extern GOST_cipher grasshopper_cbc_cipher;
356 extern GOST_cipher grasshopper_cfb_cipher;
357 extern GOST_cipher grasshopper_ofb_cipher;
358 extern GOST_cipher grasshopper_ctr_cipher;
359 extern GOST_cipher grasshopper_ctr_acpkm_cipher;
360 extern GOST_cipher grasshopper_ctr_acpkm_omac_cipher;
361 extern GOST_cipher magma_kexp15_cipher;
362 extern GOST_cipher kuznyechik_kexp15_cipher;
364 struct gost_digest_st {
365 struct gost_digest_st *template;
373 int (*init)(EVP_MD_CTX *ctx);
374 int (*update)(EVP_MD_CTX *ctx, const void *data, size_t count);
375 int (*final)(EVP_MD_CTX *ctx, unsigned char *md);
376 int (*copy)(EVP_MD_CTX *to, const EVP_MD_CTX *from);
377 int (*cleanup)(EVP_MD_CTX *ctx);
378 int (*ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
380 typedef struct gost_digest_st GOST_digest;
382 EVP_MD *GOST_init_digest(GOST_digest *d);
383 void GOST_deinit_digest(GOST_digest *d);
385 extern GOST_digest GostR3411_94_digest;
386 extern GOST_digest Gost28147_89_MAC_digest;
387 extern GOST_digest Gost28147_89_mac_12_digest;
388 extern GOST_digest GostR3411_2012_256_digest;
389 extern GOST_digest GostR3411_2012_512_digest;
390 extern GOST_digest magma_mac_digest;
391 extern GOST_digest grasshopper_mac_digest;
392 extern GOST_digest kuznyechik_ctracpkm_omac_digest;
395 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */