1 /* $OpenBSD: gost.h,v 1.1 2014/11/09 19:17:13 miod Exp $ */
2 /*
3  * Copyright (c) 2014 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
4  * Copyright (c) 2005-2006 Cryptocom LTD
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  *
18  * 3. All advertising materials mentioning features or use of this
19  *    software must display the following acknowledgment:
20  *    "This product includes software developed by the OpenSSL Project
21  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
22  *
23  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
24  *    endorse or promote products derived from this software without
25  *    prior written permission. For written permission, please contact
26  *    openssl-core@openssl.org.
27  *
28  * 5. Products derived from this software may not be called "OpenSSL"
29  *    nor may "OpenSSL" appear in their names without prior written
30  *    permission of the OpenSSL Project.
31  *
32  * 6. Redistributions of any form whatsoever must retain the following
33  *    acknowledgment:
34  *    "This product includes software developed by the OpenSSL Project
35  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
38  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
40  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
43  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
44  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
45  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
46  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
48  * OF THE POSSIBILITY OF SUCH DAMAGE.
49  * ====================================================================
50  */
51 
52 #ifndef HEADER_GOST_H
53 #define HEADER_GOST_H
54 
55 #include <openssl/opensslconf.h>
56 
57 #ifdef OPENSSL_NO_GOST
58 #error GOST is disabled.
59 #endif
60 
61 #include <openssl/asn1t.h>
62 #include <openssl/ec.h>
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 typedef struct gost2814789_key_st {
69 	unsigned int key[8];
70 	unsigned int k87[256],k65[256],k43[256],k21[256];
71 	unsigned int count;
72 	unsigned key_meshing : 1;
73 } GOST2814789_KEY;
74 
75 int Gost2814789_set_sbox(GOST2814789_KEY *key, int nid);
76 int Gost2814789_set_key(GOST2814789_KEY *key,
77 		const unsigned char *userKey, const int bits);
78 void Gost2814789_ecb_encrypt(const unsigned char *in, unsigned char *out,
79 	GOST2814789_KEY *key, const int enc);
80 void Gost2814789_cfb64_encrypt(const unsigned char *in, unsigned char *out,
81 	size_t length, GOST2814789_KEY *key,
82 	unsigned char *ivec, int *num, const int enc);
83 void Gost2814789_cnt_encrypt(const unsigned char *in, unsigned char *out,
84 	size_t length, GOST2814789_KEY *key,
85 	unsigned char *ivec, unsigned char *cnt_buf, int *num);
86 
87 typedef struct {
88 	ASN1_OCTET_STRING *iv;
89 	ASN1_OBJECT *enc_param_set;
90 } GOST_CIPHER_PARAMS;
91 
92 DECLARE_ASN1_FUNCTIONS(GOST_CIPHER_PARAMS)
93 
94 #define GOST2814789IMIT_LENGTH 4
95 #define GOST2814789IMIT_CBLOCK 8
96 #define GOST2814789IMIT_LONG unsigned int
97 
98 typedef struct GOST2814789IMITstate_st {
99 	GOST2814789IMIT_LONG	Nl, Nh;
100 	unsigned char		data[GOST2814789IMIT_CBLOCK];
101 	unsigned int		num;
102 
103 	GOST2814789_KEY		cipher;
104 	unsigned char		mac[GOST2814789IMIT_CBLOCK];
105 } GOST2814789IMIT_CTX;
106 
107 /* Note, also removed second parameter and removed dctx->cipher setting */
108 int GOST2814789IMIT_Init(GOST2814789IMIT_CTX *c, int nid);
109 int GOST2814789IMIT_Update(GOST2814789IMIT_CTX *c, const void *data, size_t len);
110 int GOST2814789IMIT_Final(unsigned char *md, GOST2814789IMIT_CTX *c);
111 void GOST2814789IMIT_Transform(GOST2814789IMIT_CTX *c, const unsigned char *data);
112 unsigned char *GOST2814789IMIT(const unsigned char *d, size_t n,
113 		unsigned char *md, int nid,
114 		const unsigned char *key, const unsigned char *iv);
115 
116 #define GOSTR341194_LONG unsigned int
117 
118 #define GOSTR341194_LENGTH	32
119 #define GOSTR341194_CBLOCK	32
120 #define GOSTR341194_LBLOCK	(GOSTR341194_CBLOCK/4)
121 
122 typedef struct GOSTR341194state_st {
123 	GOSTR341194_LONG	Nl, Nh;
124 	GOSTR341194_LONG	data[GOSTR341194_LBLOCK];
125 	unsigned int		num;
126 
127 	GOST2814789_KEY		cipher;
128 	unsigned char		H[GOSTR341194_CBLOCK];
129 	unsigned char		S[GOSTR341194_CBLOCK];
130 } GOSTR341194_CTX;
131 
132 /* Note, also removed second parameter and removed dctx->cipher setting */
133 int GOSTR341194_Init(GOSTR341194_CTX *c, int nid);
134 int GOSTR341194_Update(GOSTR341194_CTX *c, const void *data, size_t len);
135 int GOSTR341194_Final(unsigned char *md, GOSTR341194_CTX *c);
136 void GOSTR341194_Transform(GOSTR341194_CTX *c, const unsigned char *data);
137 unsigned char *GOSTR341194(const unsigned char *d, size_t n,unsigned char *md, int nid);
138 
139 #if defined(_LP64)
140 #define STREEBOG_LONG64 unsigned long
141 #define U64(C)     C##UL
142 #else
143 #define STREEBOG_LONG64 unsigned long long
144 #define U64(C)     C##ULL
145 #endif
146 
147 #define STREEBOG_LBLOCK 8
148 #define STREEBOG_CBLOCK 64
149 #define STREEBOG256_LENGTH 32
150 #define STREEBOG512_LENGTH 64
151 
152 typedef struct STREEBOGstate_st {
153 	STREEBOG_LONG64	data[STREEBOG_LBLOCK];
154 	unsigned int	num;
155 	unsigned int	md_len;
156 	STREEBOG_LONG64	h[STREEBOG_LBLOCK];
157 	STREEBOG_LONG64 N[STREEBOG_LBLOCK];
158 	STREEBOG_LONG64 Sigma[STREEBOG_LBLOCK];
159 } STREEBOG_CTX;
160 
161 int STREEBOG256_Init(STREEBOG_CTX *c);
162 int STREEBOG256_Update(STREEBOG_CTX *c, const void *data, size_t len);
163 int STREEBOG256_Final(unsigned char *md, STREEBOG_CTX *c);
164 void STREEBOG256_Transform(STREEBOG_CTX *c, const unsigned char *data);
165 unsigned char *STREEBOG256(const unsigned char *d, size_t n,unsigned char *md);
166 
167 int STREEBOG512_Init(STREEBOG_CTX *c);
168 int STREEBOG512_Update(STREEBOG_CTX *c, const void *data, size_t len);
169 int STREEBOG512_Final(unsigned char *md, STREEBOG_CTX *c);
170 void STREEBOG512_Transform(STREEBOG_CTX *c, const unsigned char *data);
171 unsigned char *STREEBOG512(const unsigned char *d, size_t n,unsigned char *md);
172 
173 typedef struct gost_key_st GOST_KEY;
174 GOST_KEY *GOST_KEY_new(void);
175 void GOST_KEY_free(GOST_KEY * r);
176 int GOST_KEY_check_key(const GOST_KEY * eckey);
177 int GOST_KEY_set_public_key_affine_coordinates(GOST_KEY * key, BIGNUM * x, BIGNUM * y);
178 const EC_GROUP * GOST_KEY_get0_group(const GOST_KEY * key);
179 int GOST_KEY_set_group(GOST_KEY * key, const EC_GROUP * group);
180 int GOST_KEY_get_digest(const GOST_KEY * key);
181 int GOST_KEY_set_digest(GOST_KEY * key, int digest_nid);
182 const BIGNUM * GOST_KEY_get0_private_key(const GOST_KEY * key);
183 int GOST_KEY_set_private_key(GOST_KEY * key, const BIGNUM * priv_key);
184 const EC_POINT * GOST_KEY_get0_public_key(const GOST_KEY * key);
185 int GOST_KEY_set_public_key(GOST_KEY * key, const EC_POINT * pub_key);
186 size_t GOST_KEY_get_size(const GOST_KEY * r);
187 
188 /* Gost-specific pmeth control-function parameters */
189 /* For GOST R34.10 parameters */
190 #define EVP_PKEY_CTRL_GOST_PARAMSET	(EVP_PKEY_ALG_CTRL+1)
191 #define EVP_PKEY_CTRL_GOST_SIG_FORMAT	(EVP_PKEY_ALG_CTRL+2)
192 #define EVP_PKEY_CTRL_GOST_SET_DIGEST	(EVP_PKEY_ALG_CTRL+3)
193 #define EVP_PKEY_CTRL_GOST_GET_DIGEST	(EVP_PKEY_ALG_CTRL+4)
194 
195 #define GOST_SIG_FORMAT_SR_BE	0
196 #define GOST_SIG_FORMAT_RS_LE	1
197 
198 /* BEGIN ERROR CODES */
199 /* The following lines are auto generated by the script mkerr.pl. Any changes
200  * made after this point may be overwritten when the script is next run.
201  */
202 void ERR_load_GOST_strings(void);
203 
204 /* Error codes for the GOST functions. */
205 
206 /* Function codes. */
207 #define GOST_F_DECODE_GOST01_ALGOR_PARAMS		 104
208 #define GOST_F_ENCODE_GOST01_ALGOR_PARAMS		 105
209 #define GOST_F_GOST2001_COMPUTE_PUBLIC			 106
210 #define GOST_F_GOST2001_DO_SIGN				 107
211 #define GOST_F_GOST2001_DO_VERIFY			 108
212 #define GOST_F_GOST2001_KEYGEN				 109
213 #define GOST_F_GOST89_GET_ASN1_PARAMETERS		 102
214 #define GOST_F_GOST89_SET_ASN1_PARAMETERS		 103
215 #define GOST_F_GOST_KEY_CHECK_KEY			 124
216 #define GOST_F_GOST_KEY_NEW				 125
217 #define GOST_F_GOST_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES 126
218 #define GOST_F_PARAM_COPY_GOST01			 110
219 #define GOST_F_PARAM_DECODE_GOST01			 111
220 #define GOST_F_PKEY_GOST01_CTRL				 116
221 #define GOST_F_PKEY_GOST01_DECRYPT			 112
222 #define GOST_F_PKEY_GOST01_DERIVE			 113
223 #define GOST_F_PKEY_GOST01_ENCRYPT			 114
224 #define GOST_F_PKEY_GOST01_PARAMGEN			 115
225 #define GOST_F_PKEY_GOST01_SIGN				 123
226 #define GOST_F_PKEY_GOST_MAC_CTRL			 100
227 #define GOST_F_PKEY_GOST_MAC_KEYGEN			 101
228 #define GOST_F_PRIV_DECODE_GOST01			 117
229 #define GOST_F_PUB_DECODE_GOST01			 118
230 #define GOST_F_PUB_ENCODE_GOST01			 119
231 #define GOST_F_PUB_PRINT_GOST01				 120
232 #define GOST_F_UNPACK_SIGNATURE_CP			 121
233 #define GOST_F_UNPACK_SIGNATURE_LE			 122
234 
235 /* Reason codes. */
236 #define GOST_R_BAD_KEY_PARAMETERS_FORMAT		 104
237 #define GOST_R_BAD_PKEY_PARAMETERS_FORMAT		 105
238 #define GOST_R_CANNOT_PACK_EPHEMERAL_KEY		 106
239 #define GOST_R_CTRL_CALL_FAILED				 107
240 #define GOST_R_ERROR_COMPUTING_SHARED_KEY		 108
241 #define GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO		 109
242 #define GOST_R_INCOMPATIBLE_ALGORITHMS			 110
243 #define GOST_R_INCOMPATIBLE_PEER_KEY			 111
244 #define GOST_R_INVALID_DIGEST_TYPE			 100
245 #define GOST_R_INVALID_IV_LENGTH			 103
246 #define GOST_R_INVALID_MAC_KEY_LENGTH			 101
247 #define GOST_R_KEY_IS_NOT_INITIALIZED			 112
248 #define GOST_R_KEY_PARAMETERS_MISSING			 113
249 #define GOST_R_MAC_KEY_NOT_SET				 102
250 #define GOST_R_NO_PARAMETERS_SET			 115
251 #define GOST_R_NO_PEER_KEY				 116
252 #define GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR	 117
253 #define GOST_R_PUBLIC_KEY_UNDEFINED			 118
254 #define GOST_R_RANDOM_NUMBER_GENERATOR_FAILED		 120
255 #define GOST_R_SIGNATURE_MISMATCH			 121
256 #define GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q		 122
257 #define GOST_R_UKM_NOT_SET				 123
258 
259 #ifdef  __cplusplus
260 }
261 #endif
262 #endif
263