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/dsa.h>
15 #include <openssl/asn1t.h>
16 #include <openssl/x509.h>
17 #include <openssl/engine.h>
18 #include <openssl/ec.h>
22 /* Control commands */
23 #define GOST_PARAM_CRYPT_PARAMS 0
24 #define GOST_PARAM_MAX 0
25 #define GOST_CTRL_CRYPT_PARAMS (ENGINE_CMD_BASE+GOST_PARAM_CRYPT_PARAMS)
27 extern const ENGINE_CMD_DEFN gost_cmds[];
28 int gost_control_func(ENGINE *e,int cmd, long i, void *p, void (*f)(void));
29 const char *get_gost_engine_param(int param);
30 int gost_set_default_param(int param, const char *value);
32 /* method registration */
34 int register_ameth_gost (int nid, EVP_PKEY_ASN1_METHOD **ameth, const char* pemstr, const char* info);
35 int register_pmeth_gost (int id, EVP_PKEY_METHOD **pmeth, int flags);
37 /* Gost-specific pmeth control-function parameters */
38 #define param_ctrl_string "paramset"
39 #define EVP_PKEY_CTRL_GOST_PARAMSET (EVP_PKEY_ALG_CTRL+1)
40 /* Pmeth internal representation */
41 struct gost_pmeth_data {
42 int sign_param_nid; /* Should be set whenever parameters are filled */
46 /* GOST-specific ASN1 structures */
50 ASN1_OCTET_STRING *encrypted_key;
51 ASN1_OCTET_STRING *imit;
54 DECLARE_ASN1_FUNCTIONS(GOST_KEY_INFO)
58 X509_PUBKEY *ephem_key;
59 ASN1_OCTET_STRING *eph_iv;
60 } GOST_KEY_AGREEMENT_INFO;
62 DECLARE_ASN1_FUNCTIONS(GOST_KEY_AGREEMENT_INFO)
65 GOST_KEY_INFO *key_info;
66 GOST_KEY_AGREEMENT_INFO *key_agreement_info;
69 DECLARE_ASN1_FUNCTIONS(GOST_KEY_TRANSPORT)
71 typedef struct { //FIXME incomplete
72 GOST_KEY_TRANSPORT *gkt;
73 } GOST_CLIENT_KEY_EXCHANGE_PARAMS;
75 DECLARE_ASN1_FUNCTIONS(GOST_CLIENT_KEY_EXCHANGE_PARAMS)
77 ASN1_OBJECT *key_params;
78 ASN1_OBJECT *hash_params;
79 ASN1_OBJECT *cipher_params;
82 DECLARE_ASN1_FUNCTIONS(GOST_KEY_PARAMS)
85 ASN1_OCTET_STRING *iv;
86 ASN1_OBJECT *enc_param_set;
89 DECLARE_ASN1_FUNCTIONS(GOST_CIPHER_PARAMS)
90 /*============== Message digest and cipher related structures ==========*/
91 /* Structure used as EVP_MD_CTX-md_data.
92 * It allows to avoid storing in the md-data pointers to
93 * dynamically allocated memory.
95 * I cannot invent better way to avoid memory leaks, because
96 * openssl insist on invoking Init on Final-ed digests, and there
97 * is no reliable way to find out whether pointer in the passed
98 * md_data is valid or not.
100 struct ossl_gost_digest_ctx {
104 /* EVP_MD structure for GOST R 34.11 */
105 extern EVP_MD digest_gost;
107 /* Cipher context used for EVP_CIPHER operation */
108 struct ossl_gost_cipher_ctx {
114 /* Structure to map parameter NID to S-block */
115 struct gost_cipher_info {
117 gost_subst_block *sblock;
121 /* Context for MAC */
122 struct ossl_gost_imit_ctx {
124 unsigned char buffer[8];
125 unsigned char partial_block[8];
132 /* Table which maps parameter NID to S-blocks */
133 extern struct gost_cipher_info gost_cipher_list[];
134 /* Find encryption params from ASN1_OBJECT */
135 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj);
136 /* Implementation of GOST 28147-89 cipher in CFB and CNT modes */
137 extern EVP_CIPHER cipher_gost;
139 #define EVP_MD_FLAG_NEEDS_KEY 0x20
140 #define EVP_MD_CTRL_GET_TLS_MAC_KEY_LENGTH (EVP_MD_CTRL_ALG_CTRL+1)
141 #define EVP_MD_CTRL_SET_KEY (EVP_MD_CTRL_ALG_CTRL+2)
142 /* Ciphers and MACs specific for GOST TLS draft */
143 extern EVP_CIPHER cipher_gost_vizircfb;
144 extern EVP_CIPHER cipher_gost_cpacnt;
145 extern EVP_MD imit_gost_vizir;
146 extern EVP_MD imit_gost_cpa;
148 /* EVP_PKEY_METHOD key encryption callbacks */
149 /* From gost94_keyx.c */
150 int pkey_GOST94cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* key, size_t key_len );
151 int pkey_GOST94cc_encrypt (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char * key,size_t key_len);
153 int pkey_GOST94cp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* in, size_t in_len );
154 int pkey_GOST94cc_decrypt (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char * in,size_t in_len);
155 /* From gost2001_keyx.c */
156 int pkey_GOST01cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* key, size_t key_len );
157 int pkey_GOST01cc_encrypt (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char * key,size_t key_len);
159 int pkey_GOST01cp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* in, size_t in_len );
160 int pkey_GOST01cc_decrypt (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char * in,size_t in_len);
162 /* Internal functions to make error processing happy */
163 int decrypt_cryptocom_key(unsigned char *sess_key,int max_key_len,
164 const unsigned char *crypted_key,int crypted_key_len, gost_ctx *ctx);
165 int encrypt_cryptocom_key(const unsigned char *sess_key,int key_len,
166 unsigned char *crypted_key, gost_ctx *ctx);
167 /* Internal functions for signature algorithms */
168 int fill_GOST94_params(DSA *dsa,int nid);
169 int fill_GOST2001_params(EC_KEY *eckey, int nid);
170 int gost_sign_keygen(DSA *dsa) ;
171 int gost2001_keygen(EC_KEY *ec) ;
173 DSA_SIG *gost_do_sign(const unsigned char *dgst,int dlen, DSA *dsa) ;
174 DSA_SIG *gost2001_do_sign(const unsigned char *dgst,int dlen, EC_KEY *eckey);
176 int gost_do_verify(const unsigned char *dgst, int dgst_len,
177 DSA_SIG *sig, DSA *dsa) ;
178 int gost2001_do_verify(const unsigned char *dgst,int dgst_len,
179 DSA_SIG *sig, EC_KEY *ec);
180 int gost2001_compute_public(EC_KEY *ec) ;
181 int gost94_compute_public(DSA *dsa) ;
182 /*============== miscellaneous functions============================= */
183 /* from gost_sign.c */
184 /* Convert GOST R 34.11 hash sum to bignum according to standard */
185 BIGNUM *hashsum2bn(const unsigned char *dgst) ;
186 /* Store bignum in byte array of given length, prepending by zeros
188 int store_bignum(BIGNUM *bn, unsigned char *buf,int len);
189 /* Read bignum, which can have few MSB all-zeros from buffer*/
190 BIGNUM *getbnfrombuf(const unsigned char *buf,size_t len);
191 /* Pack GOST R 34.10 signature according to CryptoCom rules */
192 int pack_sign_cc(DSA_SIG *s,int order,unsigned char *sig, unsigned int *siglen);
193 /* Pack GOST R 34.10 signature according to CryptoPro rules */
194 int pack_sign_cp(DSA_SIG *s,int order,unsigned char *sig, unsigned int *siglen);
195 /* Unpack GOST R 34.10 signature according to CryptoCom rules */
196 DSA_SIG *unpack_cc_signature(const unsigned char *sig,size_t siglen) ;
197 /* Unpack GOST R 34.10 signature according to CryptoPro rules */
198 DSA_SIG *unpack_cp_signature(const unsigned char *sig,size_t siglen) ;
200 /* Get private key as BIGNUM from both R 34.10-94 and R 34.10-2001 keys*/
201 BIGNUM* gost_get_priv_key(const EVP_PKEY *pkey) ;
202 /* Find NID by GOST 94 parameters */
203 int gost94_nid_by_params(DSA *p) ;