1 /* $OpenBSD: gost.h,v 1.3 2016/09/04 17:02:31 jsing 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 GOST_CIPHER_PARAMS *GOST_CIPHER_PARAMS_new(void); 93 void GOST_CIPHER_PARAMS_free(GOST_CIPHER_PARAMS *a); 94 GOST_CIPHER_PARAMS *d2i_GOST_CIPHER_PARAMS(GOST_CIPHER_PARAMS **a, const unsigned char **in, long len); 95 int i2d_GOST_CIPHER_PARAMS(GOST_CIPHER_PARAMS *a, unsigned char **out); 96 extern const ASN1_ITEM GOST_CIPHER_PARAMS_it; 97 98 #define GOST2814789IMIT_LENGTH 4 99 #define GOST2814789IMIT_CBLOCK 8 100 #define GOST2814789IMIT_LONG unsigned int 101 102 typedef struct GOST2814789IMITstate_st { 103 GOST2814789IMIT_LONG Nl, Nh; 104 unsigned char data[GOST2814789IMIT_CBLOCK]; 105 unsigned int num; 106 107 GOST2814789_KEY cipher; 108 unsigned char mac[GOST2814789IMIT_CBLOCK]; 109 } GOST2814789IMIT_CTX; 110 111 /* Note, also removed second parameter and removed dctx->cipher setting */ 112 int GOST2814789IMIT_Init(GOST2814789IMIT_CTX *c, int nid); 113 int GOST2814789IMIT_Update(GOST2814789IMIT_CTX *c, const void *data, size_t len); 114 int GOST2814789IMIT_Final(unsigned char *md, GOST2814789IMIT_CTX *c); 115 void GOST2814789IMIT_Transform(GOST2814789IMIT_CTX *c, const unsigned char *data); 116 unsigned char *GOST2814789IMIT(const unsigned char *d, size_t n, 117 unsigned char *md, int nid, 118 const unsigned char *key, const unsigned char *iv); 119 120 #define GOSTR341194_LONG unsigned int 121 122 #define GOSTR341194_LENGTH 32 123 #define GOSTR341194_CBLOCK 32 124 #define GOSTR341194_LBLOCK (GOSTR341194_CBLOCK/4) 125 126 typedef struct GOSTR341194state_st { 127 GOSTR341194_LONG Nl, Nh; 128 GOSTR341194_LONG data[GOSTR341194_LBLOCK]; 129 unsigned int num; 130 131 GOST2814789_KEY cipher; 132 unsigned char H[GOSTR341194_CBLOCK]; 133 unsigned char S[GOSTR341194_CBLOCK]; 134 } GOSTR341194_CTX; 135 136 /* Note, also removed second parameter and removed dctx->cipher setting */ 137 int GOSTR341194_Init(GOSTR341194_CTX *c, int nid); 138 int GOSTR341194_Update(GOSTR341194_CTX *c, const void *data, size_t len); 139 int GOSTR341194_Final(unsigned char *md, GOSTR341194_CTX *c); 140 void GOSTR341194_Transform(GOSTR341194_CTX *c, const unsigned char *data); 141 unsigned char *GOSTR341194(const unsigned char *d, size_t n,unsigned char *md, int nid); 142 143 #if defined(_LP64) 144 #define STREEBOG_LONG64 unsigned long 145 #define U64(C) C##UL 146 #else 147 #define STREEBOG_LONG64 unsigned long long 148 #define U64(C) C##ULL 149 #endif 150 151 #define STREEBOG_LBLOCK 8 152 #define STREEBOG_CBLOCK 64 153 #define STREEBOG256_LENGTH 32 154 #define STREEBOG512_LENGTH 64 155 156 typedef struct STREEBOGstate_st { 157 STREEBOG_LONG64 data[STREEBOG_LBLOCK]; 158 unsigned int num; 159 unsigned int md_len; 160 STREEBOG_LONG64 h[STREEBOG_LBLOCK]; 161 STREEBOG_LONG64 N[STREEBOG_LBLOCK]; 162 STREEBOG_LONG64 Sigma[STREEBOG_LBLOCK]; 163 } STREEBOG_CTX; 164 165 int STREEBOG256_Init(STREEBOG_CTX *c); 166 int STREEBOG256_Update(STREEBOG_CTX *c, const void *data, size_t len); 167 int STREEBOG256_Final(unsigned char *md, STREEBOG_CTX *c); 168 void STREEBOG256_Transform(STREEBOG_CTX *c, const unsigned char *data); 169 unsigned char *STREEBOG256(const unsigned char *d, size_t n,unsigned char *md); 170 171 int STREEBOG512_Init(STREEBOG_CTX *c); 172 int STREEBOG512_Update(STREEBOG_CTX *c, const void *data, size_t len); 173 int STREEBOG512_Final(unsigned char *md, STREEBOG_CTX *c); 174 void STREEBOG512_Transform(STREEBOG_CTX *c, const unsigned char *data); 175 unsigned char *STREEBOG512(const unsigned char *d, size_t n,unsigned char *md); 176 177 typedef struct gost_key_st GOST_KEY; 178 GOST_KEY *GOST_KEY_new(void); 179 void GOST_KEY_free(GOST_KEY * r); 180 int GOST_KEY_check_key(const GOST_KEY * eckey); 181 int GOST_KEY_set_public_key_affine_coordinates(GOST_KEY * key, BIGNUM * x, BIGNUM * y); 182 const EC_GROUP * GOST_KEY_get0_group(const GOST_KEY * key); 183 int GOST_KEY_set_group(GOST_KEY * key, const EC_GROUP * group); 184 int GOST_KEY_get_digest(const GOST_KEY * key); 185 int GOST_KEY_set_digest(GOST_KEY * key, int digest_nid); 186 const BIGNUM * GOST_KEY_get0_private_key(const GOST_KEY * key); 187 int GOST_KEY_set_private_key(GOST_KEY * key, const BIGNUM * priv_key); 188 const EC_POINT * GOST_KEY_get0_public_key(const GOST_KEY * key); 189 int GOST_KEY_set_public_key(GOST_KEY * key, const EC_POINT * pub_key); 190 size_t GOST_KEY_get_size(const GOST_KEY * r); 191 192 /* Gost-specific pmeth control-function parameters */ 193 /* For GOST R34.10 parameters */ 194 #define EVP_PKEY_CTRL_GOST_PARAMSET (EVP_PKEY_ALG_CTRL+1) 195 #define EVP_PKEY_CTRL_GOST_SIG_FORMAT (EVP_PKEY_ALG_CTRL+2) 196 #define EVP_PKEY_CTRL_GOST_SET_DIGEST (EVP_PKEY_ALG_CTRL+3) 197 #define EVP_PKEY_CTRL_GOST_GET_DIGEST (EVP_PKEY_ALG_CTRL+4) 198 199 #define GOST_SIG_FORMAT_SR_BE 0 200 #define GOST_SIG_FORMAT_RS_LE 1 201 202 /* BEGIN ERROR CODES */ 203 /* The following lines are auto generated by the script mkerr.pl. Any changes 204 * made after this point may be overwritten when the script is next run. 205 */ 206 void ERR_load_GOST_strings(void); 207 208 /* Error codes for the GOST functions. */ 209 210 /* Function codes. */ 211 #define GOST_F_DECODE_GOST01_ALGOR_PARAMS 104 212 #define GOST_F_ENCODE_GOST01_ALGOR_PARAMS 105 213 #define GOST_F_GOST2001_COMPUTE_PUBLIC 106 214 #define GOST_F_GOST2001_DO_SIGN 107 215 #define GOST_F_GOST2001_DO_VERIFY 108 216 #define GOST_F_GOST2001_KEYGEN 109 217 #define GOST_F_GOST89_GET_ASN1_PARAMETERS 102 218 #define GOST_F_GOST89_SET_ASN1_PARAMETERS 103 219 #define GOST_F_GOST_KEY_CHECK_KEY 124 220 #define GOST_F_GOST_KEY_NEW 125 221 #define GOST_F_GOST_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES 126 222 #define GOST_F_PARAM_COPY_GOST01 110 223 #define GOST_F_PARAM_DECODE_GOST01 111 224 #define GOST_F_PKEY_GOST01_CTRL 116 225 #define GOST_F_PKEY_GOST01_DECRYPT 112 226 #define GOST_F_PKEY_GOST01_DERIVE 113 227 #define GOST_F_PKEY_GOST01_ENCRYPT 114 228 #define GOST_F_PKEY_GOST01_PARAMGEN 115 229 #define GOST_F_PKEY_GOST01_SIGN 123 230 #define GOST_F_PKEY_GOST_MAC_CTRL 100 231 #define GOST_F_PKEY_GOST_MAC_KEYGEN 101 232 #define GOST_F_PRIV_DECODE_GOST01 117 233 #define GOST_F_PUB_DECODE_GOST01 118 234 #define GOST_F_PUB_ENCODE_GOST01 119 235 #define GOST_F_PUB_PRINT_GOST01 120 236 #define GOST_F_UNPACK_SIGNATURE_CP 121 237 #define GOST_F_UNPACK_SIGNATURE_LE 122 238 239 /* Reason codes. */ 240 #define GOST_R_BAD_KEY_PARAMETERS_FORMAT 104 241 #define GOST_R_BAD_PKEY_PARAMETERS_FORMAT 105 242 #define GOST_R_CANNOT_PACK_EPHEMERAL_KEY 106 243 #define GOST_R_CTRL_CALL_FAILED 107 244 #define GOST_R_ERROR_COMPUTING_SHARED_KEY 108 245 #define GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO 109 246 #define GOST_R_INCOMPATIBLE_ALGORITHMS 110 247 #define GOST_R_INCOMPATIBLE_PEER_KEY 111 248 #define GOST_R_INVALID_DIGEST_TYPE 100 249 #define GOST_R_INVALID_IV_LENGTH 103 250 #define GOST_R_INVALID_MAC_KEY_LENGTH 101 251 #define GOST_R_KEY_IS_NOT_INITIALIZED 112 252 #define GOST_R_KEY_PARAMETERS_MISSING 113 253 #define GOST_R_MAC_KEY_NOT_SET 102 254 #define GOST_R_NO_PARAMETERS_SET 115 255 #define GOST_R_NO_PEER_KEY 116 256 #define GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR 117 257 #define GOST_R_PUBLIC_KEY_UNDEFINED 118 258 #define GOST_R_RANDOM_NUMBER_GENERATOR_FAILED 120 259 #define GOST_R_SIGNATURE_MISMATCH 121 260 #define GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q 122 261 #define GOST_R_UKM_NOT_SET 123 262 263 #ifdef __cplusplus 264 } 265 #endif 266 #endif 267