3 /**********************************************************************
5 * Copyright (c) 2006 Cryptocom LTD *
6 * This file is distributed under the same license as OpenSSL *
8 * Internal declarations used in GOST engine *
9 * OpenSSL 0.9.9 libraries required to compile and use *
11 **********************************************************************/
12 # include <openssl/bn.h>
13 # include <openssl/evp.h>
14 # include <openssl/asn1t.h>
15 # include <openssl/x509.h>
16 # include <openssl/engine.h>
17 # include <openssl/ec.h>
19 # include "gosthash.h"
20 /* Control commands */
21 # define GOST_PARAM_CRYPT_PARAMS 0
22 # define GOST_PARAM_PBE_PARAMS 1
23 # define GOST_PARAM_PK_FORMAT 2
24 # define GOST_PARAM_MAX 3
25 # define GOST_CTRL_CRYPT_PARAMS (ENGINE_CMD_BASE+GOST_PARAM_CRYPT_PARAMS)
26 # define GOST_CTRL_PBE_PARAMS (ENGINE_CMD_BASE+GOST_PARAM_PBE_PARAMS)
27 # define GOST_CTRL_PK_FORMAT (ENGINE_CMD_BASE+GOST_PARAM_PK_FORMAT)
29 typedef struct R3410_ec {
40 extern R3410_ec_params R3410_2001_paramset[],
41 *R3410_2012_256_paramset, R3410_2012_512_paramset[];
43 extern const ENGINE_CMD_DEFN gost_cmds[];
44 int gost_control_func(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
45 const char *get_gost_engine_param(int param);
46 int gost_set_default_param(int param, const char *value);
47 void gost_param_free(void);
49 /* method registration */
51 int register_ameth_gost(int nid, EVP_PKEY_ASN1_METHOD **ameth,
52 const char *pemstr, const char *info);
53 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags);
55 /* Gost-specific pmeth control-function parameters */
56 /* For GOST R34.10 parameters */
57 # define param_ctrl_string "paramset"
58 # define EVP_PKEY_CTRL_GOST_PARAMSET (EVP_PKEY_ALG_CTRL+1)
59 /* For GOST 28147 MAC */
60 # define key_ctrl_string "key"
61 # define hexkey_ctrl_string "hexkey"
62 # define maclen_ctrl_string "size"
63 # define EVP_PKEY_CTRL_GOST_MAC_HEXKEY (EVP_PKEY_ALG_CTRL+3)
64 # define EVP_PKEY_CTRL_MAC_LEN (EVP_PKEY_ALG_CTRL+5)
65 /* Pmeth internal representation */
66 struct gost_pmeth_data {
67 int sign_param_nid; /* Should be set whenever parameters are
70 unsigned char *shared_ukm;
71 size_t shared_ukm_size; /* XXX temporary use shared_ukm and hash for 2018 CKE */
73 int cipher_nid; /* KExp15/KImp15 algs */
76 struct gost_mac_pmeth_data {
81 unsigned char key[32];
86 unsigned char key[32];
89 /* GOST-specific ASN1 structures */
92 ASN1_OCTET_STRING *encrypted_key;
93 ASN1_OCTET_STRING *imit;
96 DECLARE_ASN1_FUNCTIONS(GOST_KEY_INFO)
100 X509_PUBKEY *ephem_key;
101 ASN1_OCTET_STRING *eph_iv;
102 } GOST_KEY_AGREEMENT_INFO;
104 DECLARE_ASN1_FUNCTIONS(GOST_KEY_AGREEMENT_INFO)
107 GOST_KEY_INFO *key_info;
108 GOST_KEY_AGREEMENT_INFO *key_agreement_info;
109 } GOST_KEY_TRANSPORT;
111 DECLARE_ASN1_FUNCTIONS(GOST_KEY_TRANSPORT)
113 typedef struct { /* FIXME incomplete */
114 GOST_KEY_TRANSPORT *gkt;
115 } GOST_CLIENT_KEY_EXCHANGE_PARAMS;
117 /* PSKeyTransport ::= SEQUENCE {
119 ephemeralPublicKey SubjectPublicKeyInfo
121 SubjectPublicKeyInfo ::= SEQUENCE {
122 algorithm AlgorithmIdentifier,
123 subjectPublicKey BITSTRING
125 AlgorithmIdentifier ::= SEQUENCE {
126 algorithm OBJECT IDENTIFIER,
127 parameters ANY OPTIONAL
129 typedef struct PSKeyTransport_st {
130 ASN1_OCTET_STRING *psexp;
131 X509_PUBKEY *ephem_key;
132 } PSKeyTransport_gost;
134 DECLARE_ASN1_FUNCTIONS(PSKeyTransport_gost)
136 * Hacks to shorten symbols to 31 characters or less, or OpenVMS. This mimics
137 * what's done in symhacks.h, but since this is a very local header file, I
138 * prefered to put this hack directly here. -- Richard Levitte
140 # ifdef OPENSSL_SYS_VMS
141 # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_it
142 # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_it GOST_CLIENT_KEY_EXC_PARAMS_it
143 # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_new
144 # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_new GOST_CLIENT_KEY_EXC_PARAMS_new
145 # undef GOST_CLIENT_KEY_EXCHANGE_PARAMS_free
146 # define GOST_CLIENT_KEY_EXCHANGE_PARAMS_free GOST_CLIENT_KEY_EXC_PARAMS_free
147 # undef d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS
148 # define d2i_GOST_CLIENT_KEY_EXCHANGE_PARAMS d2i_GOST_CLIENT_KEY_EXC_PARAMS
149 # undef i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS
150 # define i2d_GOST_CLIENT_KEY_EXCHANGE_PARAMS i2d_GOST_CLIENT_KEY_EXC_PARAMS
151 # endif /* End of hack */
152 DECLARE_ASN1_FUNCTIONS(GOST_CLIENT_KEY_EXCHANGE_PARAMS)
154 ASN1_OBJECT *key_params;
155 ASN1_OBJECT *hash_params;
156 ASN1_OBJECT *cipher_params;
159 DECLARE_ASN1_FUNCTIONS(GOST_KEY_PARAMS)
162 ASN1_OCTET_STRING *iv;
163 ASN1_OBJECT *enc_param_set;
164 } GOST_CIPHER_PARAMS;
166 DECLARE_ASN1_FUNCTIONS(GOST_CIPHER_PARAMS)
169 ASN1_OCTET_STRING *masked_priv_key;
170 ASN1_OCTET_STRING *public_key;
173 DECLARE_ASN1_FUNCTIONS(MASKED_GOST_KEY)
175 /*============== Message digest and cipher related structures ==========*/
177 * Structure used as EVP_MD_CTX-md_data. It allows to avoid storing
178 * in the md-data pointers to dynamically allocated memory. I
179 * cannot invent better way to avoid memory leaks, because openssl
180 * insist on invoking Init on Final-ed digests, and there is no
181 * reliable way to find out whether pointer in the passed md_data is
184 struct ossl_gost_digest_ctx {
188 /* EVP_MD structure for GOST R 34.11 */
189 EVP_MD *digest_gost(void);
190 void digest_gost_destroy(void);
191 /* EVP MD structure for GOST R 34.11-2012 algorithms */
192 EVP_MD *digest_gost2012_256(void);
193 EVP_MD *digest_gost2012_512(void);
194 void digest_gost2012_256_destroy(void);
195 void digest_gost2012_512_destroy(void);
196 /* EVP_MD structure for GOST 28147 in MAC mode */
197 EVP_MD *imit_gost_cpa(void);
198 void imit_gost_cpa_destroy(void);
199 EVP_MD *imit_gost_cp_12(void);
200 void imit_gost_cp_12_destroy(void);
201 EVP_MD *magma_omac(void);
202 void magma_omac_destroy(void);
203 EVP_MD *grasshopper_omac(void);
204 EVP_MD *grasshopper_omac_acpkm(void);
205 void grasshopper_omac_destroy(void);
206 void grasshopper_omac_acpkm_destroy(void);
207 /* Cipher context used for EVP_CIPHER operation */
208 struct ossl_gost_cipher_ctx {
214 /* Structure to map parameter NID to S-block */
215 struct gost_cipher_info {
217 gost_subst_block *sblock;
220 /* Context for MAC */
221 struct ossl_gost_imit_ctx {
223 unsigned char buffer[8];
224 unsigned char partial_block[8];
231 /* Table which maps parameter NID to S-blocks */
232 extern struct gost_cipher_info gost_cipher_list[];
233 /* Find encryption params from ASN1_OBJECT */
234 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj);
235 /* Implementation of GOST 28147-89 cipher in CFB and CNT modes */
236 const EVP_CIPHER *cipher_gost();
237 const EVP_CIPHER *cipher_gost_cbc();
238 const EVP_CIPHER *cipher_gost_cpacnt();
239 const EVP_CIPHER *cipher_gost_cpcnt_12();
240 const EVP_CIPHER *cipher_magma_cbc();
241 const EVP_CIPHER *cipher_magma_ctr();
242 void cipher_gost_destroy();
244 void inc_counter(unsigned char *buffer, size_t buf_len);
246 # define EVP_MD_CTRL_KEY_LEN (EVP_MD_CTRL_ALG_CTRL+3)
247 # define EVP_MD_CTRL_SET_KEY (EVP_MD_CTRL_ALG_CTRL+4)
248 # define EVP_MD_CTRL_MAC_LEN (EVP_MD_CTRL_ALG_CTRL+5)
249 /* EVP_PKEY_METHOD key encryption callbacks */
250 /* From gost_ec_keyx.c */
251 int pkey_gost_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
252 size_t *outlen, const unsigned char *key,
255 int pkey_gost_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out,
256 size_t *outlen, const unsigned char *in,
258 /* derive functions */
259 /* From gost_ec_keyx.c */
260 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
261 int fill_GOST_EC_params(EC_KEY *eckey, int nid);
262 int gost_ec_keygen(EC_KEY *ec);
264 ECDSA_SIG *gost_ec_sign(const unsigned char *dgst, int dlen, EC_KEY *eckey);
265 int gost_ec_verify(const unsigned char *dgst, int dgst_len,
266 ECDSA_SIG *sig, EC_KEY *ec);
267 int gost_ec_compute_public(EC_KEY *ec);
270 int gost_kdftree2012_256(unsigned char *keyout, size_t keyout_len,
271 const unsigned char *key, size_t keylen,
272 const unsigned char *label, size_t label_len,
273 const unsigned char *seed, size_t seed_len,
274 const size_t representation);
276 int gost_tlstree(int cipher_nid, const unsigned char* inkey,
277 unsigned char *outkey, const unsigned char *tlsseq);
279 int gost_kexp15(const unsigned char *shared_key, const int shared_len,
280 int cipher_nid, const unsigned char *cipher_key,
281 int mac_nid, unsigned char *mac_key,
282 const unsigned char *iv, const size_t ivlen,
283 unsigned char *out, int *out_len);
284 int gost_kimp15(const unsigned char *expkey, const size_t expkeylen,
285 int cipher_nid, const unsigned char *cipher_key,
286 int mac_nid, unsigned char *mac_key,
287 const unsigned char *iv, const size_t ivlen,
288 unsigned char *shared_key);
289 /*============== miscellaneous functions============================= */
290 /* from gost_sign.c */
291 /* Convert GOST R 34.11 hash sum to bignum according to standard */
292 BIGNUM *hashsum2bn(const unsigned char *dgst, int len);
294 * Store bignum in byte array of given length, prepending by zeros if
297 int store_bignum(const BIGNUM *bn, unsigned char *buf, int len);
298 /* Pack GOST R 34.10 signature according to CryptoPro rules */
299 int pack_sign_cp(ECDSA_SIG *s, int order, unsigned char *sig, size_t *siglen);
301 /* Get private key as BIGNUM from both 34.10-2001 keys*/
302 /* Returns pointer into EVP_PKEY structure */
303 BIGNUM *gost_get0_priv_key(const EVP_PKEY *pkey);