6a67f05123fc56e5cd344ff28af5f79fbbb84f11
[openssl.git] / engines / ccgost / 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  *       This file is distributed under the same license as OpenSSL   *
7  *                                                                    *
8  *         Internal declarations  used in GOST engine                *
9  *         OpenSSL 0.9.9 libraries required to compile and use        *
10  *                              this code                             *
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>
19 #include <unistd.h>
20 #include "gost89.h"
21 #include "gosthash.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)
26
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); 
31         void gost_param_free(void);
32
33 /* method registration */
34
35         int register_ameth_gost (int nid, EVP_PKEY_ASN1_METHOD **ameth, const char* pemstr, const char* info);
36         int register_pmeth_gost (int id, EVP_PKEY_METHOD **pmeth, int flags);
37
38 /* Gost-specific pmeth control-function parameters */
39 /* For GOST R34.10 parameters */
40 #define param_ctrl_string "paramset"
41 #define EVP_PKEY_CTRL_GOST_PARAMSET (EVP_PKEY_ALG_CTRL+1)
42 /* For GOST 28147 MAC */
43 #define key_ctrl_string "key"
44 #define hexkey_ctrl_string "hexkey"
45 #define EVP_PKEY_CTRL_GOST_MAC_HEXKEY (EVP_PKEY_ALG_CTRL+3)
46 /* Pmeth internal representation */
47         struct gost_pmeth_data {
48             int sign_param_nid; /* Should be set whenever parameters are filled */
49                 EVP_MD *md;
50                 unsigned char *shared_ukm;
51         };
52
53         struct gost_mac_pmeth_data {
54                 int key_set;
55                 EVP_MD *md;
56                 unsigned char key[32];
57         }       ;
58 /* GOST-specific ASN1 structures */
59
60
61 typedef struct {
62         ASN1_OCTET_STRING *encrypted_key;
63         ASN1_OCTET_STRING *imit;
64 } GOST_KEY_INFO;
65
66 DECLARE_ASN1_FUNCTIONS(GOST_KEY_INFO)
67
68 typedef struct {
69         ASN1_OBJECT *cipher;
70         X509_PUBKEY *ephem_key;
71         ASN1_OCTET_STRING *eph_iv;
72 } GOST_KEY_AGREEMENT_INFO;
73
74 DECLARE_ASN1_FUNCTIONS(GOST_KEY_AGREEMENT_INFO)
75         
76 typedef struct {
77         GOST_KEY_INFO *key_info;
78         GOST_KEY_AGREEMENT_INFO *key_agreement_info;
79 } GOST_KEY_TRANSPORT;
80
81 DECLARE_ASN1_FUNCTIONS(GOST_KEY_TRANSPORT)
82
83 typedef struct { /* FIXME incomplete */
84         GOST_KEY_TRANSPORT *gkt;
85 } GOST_CLIENT_KEY_EXCHANGE_PARAMS;
86
87 DECLARE_ASN1_FUNCTIONS(GOST_CLIENT_KEY_EXCHANGE_PARAMS)
88 typedef struct {
89         ASN1_OBJECT *key_params;
90         ASN1_OBJECT *hash_params;
91         ASN1_OBJECT *cipher_params;
92 } GOST_KEY_PARAMS;
93
94 DECLARE_ASN1_FUNCTIONS(GOST_KEY_PARAMS)
95
96 typedef struct {
97         ASN1_OCTET_STRING *iv;
98         ASN1_OBJECT *enc_param_set;
99 } GOST_CIPHER_PARAMS; 
100
101 DECLARE_ASN1_FUNCTIONS(GOST_CIPHER_PARAMS)
102 /*============== Message digest  and cipher related structures  ==========*/
103          /* Structure used as EVP_MD_CTX-md_data. 
104           * It allows to avoid storing in the md-data pointers to
105           * dynamically allocated memory.
106           *
107           * I cannot invent better way to avoid memory leaks, because
108           * openssl insist on invoking Init on Final-ed digests, and there
109           * is no reliable way to find out whether pointer in the passed
110           * md_data is valid or not.
111           * */
112 struct ossl_gost_digest_ctx {
113         gost_hash_ctx dctx;
114         gost_ctx cctx;
115 };      
116 /* EVP_MD structure for GOST R 34.11 */
117 extern EVP_MD digest_gost;
118 /* EVP_MD structure for GOST 28147 in MAC mode */
119 extern EVP_MD imit_gost_cpa;
120 /* Cipher context used for EVP_CIPHER operation */
121 struct ossl_gost_cipher_ctx {
122         int paramNID;
123         off_t count;
124         int key_meshing;
125         gost_ctx cctx;
126 };      
127 /* Structure to map parameter NID to S-block */
128 struct gost_cipher_info {
129         int nid;
130         gost_subst_block *sblock;
131         int key_meshing;
132 };
133 /* Context for MAC */
134 struct ossl_gost_imit_ctx {
135         gost_ctx cctx;
136         unsigned char buffer[8];
137         unsigned char partial_block[8];
138         off_t count;
139         int key_meshing;
140         int bytes_left;
141         int key_set;
142 };      
143 /* Table which maps parameter NID to S-blocks */
144 extern struct gost_cipher_info gost_cipher_list[];
145 /* Find encryption params from ASN1_OBJECT */
146 const struct gost_cipher_info *get_encryption_params(ASN1_OBJECT *obj);
147 /* Implementation of GOST 28147-89 cipher in CFB and CNT modes */
148 extern EVP_CIPHER cipher_gost;
149 extern EVP_CIPHER cipher_gost_cpacnt;
150 #define EVP_MD_CTRL_KEY_LEN (EVP_MD_CTRL_ALG_CTRL+3)
151 #define EVP_MD_CTRL_SET_KEY (EVP_MD_CTRL_ALG_CTRL+4)
152 /* EVP_PKEY_METHOD key encryption callbacks */
153 /* From gost94_keyx.c */
154 int pkey_GOST94cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* key, size_t key_len );
155
156 int pkey_GOST94cp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* in, size_t in_len );
157 /* From gost2001_keyx.c */
158 int pkey_GOST01cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* key, size_t key_len );
159
160 int pkey_GOST01cp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* in, size_t in_len );
161 /* derive functions */
162 /* From gost2001_keyx.c */
163 int pkey_gost2001_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
164 /* From gost94_keyx.c */
165 int pkey_gost94_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
166 /* Internal functions for signature algorithms */
167 int fill_GOST94_params(DSA *dsa,int nid);
168 int fill_GOST2001_params(EC_KEY *eckey, int nid);
169 int gost_sign_keygen(DSA *dsa) ;
170 int gost2001_keygen(EC_KEY *ec) ;
171
172 DSA_SIG *gost_do_sign(const unsigned char *dgst,int dlen, DSA *dsa) ;
173 DSA_SIG *gost2001_do_sign(const unsigned char *dgst,int dlen, EC_KEY *eckey);
174
175 int gost_do_verify(const unsigned char *dgst, int dgst_len,
176                 DSA_SIG *sig, DSA *dsa) ;
177 int gost2001_do_verify(const unsigned char *dgst,int dgst_len,
178                         DSA_SIG *sig, EC_KEY *ec);
179 int gost2001_compute_public(EC_KEY *ec) ;
180 int gost94_compute_public(DSA *dsa) ;
181 /*============== miscellaneous functions============================= */
182 /* from gost_sign.c */
183 /* Convert GOST R 34.11 hash sum to bignum according to standard */
184 BIGNUM *hashsum2bn(const unsigned char *dgst) ;
185 /* Store bignum in byte array of given length, prepending by zeros
186  * if nesseccary */
187 int store_bignum(BIGNUM *bn, unsigned char *buf,int len);
188 /* Read bignum, which can have few MSB all-zeros    from buffer*/ 
189 BIGNUM *getbnfrombuf(const unsigned char *buf,size_t len);
190 /* Pack GOST R 34.10 signature according to CryptoPro rules */
191 int pack_sign_cp(DSA_SIG *s,int order,unsigned char *sig, size_t *siglen); 
192 /* Unpack GOST R 34.10 signature according to CryptoPro rules */
193 DSA_SIG *unpack_cp_signature(const unsigned char *sig,size_t siglen) ;
194 /* from ameth.c */
195 /* Get private key as BIGNUM from both R 34.10-94 and R 34.10-2001  keys*/
196 /* Returns pointer into EVP_PKEY structure */
197 BIGNUM* gost_get0_priv_key(const EVP_PKEY *pkey) ;
198 /* Find NID by GOST 94 parameters */
199 int gost94_nid_by_params(DSA *p) ;
200
201
202 #endif