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 module libressl_d.openssl.gost; 52 53 54 private static import core.stdc.config; 55 private static import libressl_d.openssl.asn1; 56 private static import libressl_d.openssl.evp; 57 private static import libressl_d.openssl.ossl_typ; 58 private static import std.bitmanip; 59 public import libressl_d.openssl.asn1t; 60 public import libressl_d.openssl.ec; 61 public import libressl_d.openssl.opensslconf; 62 63 version (OPENSSL_NO_GOST) { 64 static assert(false, "GOST is disabled."); 65 } 66 67 extern (C): 68 nothrow @nogc: 69 70 struct gost2814789_key_st 71 { 72 uint[8] key; 73 uint[256] k87; 74 uint[256] k65; 75 uint[256] k43; 76 uint[256] k21; 77 uint count; 78 mixin(std.bitmanip.bitfields!(ubyte, "key_meshing", 1, uint, "", 7)); 79 } 80 81 alias GOST2814789_KEY = .gost2814789_key_st; 82 83 int Gost2814789_set_sbox(.GOST2814789_KEY* key, int nid); 84 int Gost2814789_set_key(.GOST2814789_KEY* key, const (ubyte)* userKey, const int bits); 85 void Gost2814789_ecb_encrypt(const (ubyte)* in_, ubyte* out_, .GOST2814789_KEY* key, const int enc); 86 void Gost2814789_cfb64_encrypt(const (ubyte)* in_, ubyte* out_, size_t length_, .GOST2814789_KEY* key, ubyte* ivec, int* num, const int enc); 87 void Gost2814789_cnt_encrypt(const (ubyte)* in_, ubyte* out_, size_t length_, .GOST2814789_KEY* key, ubyte* ivec, ubyte* cnt_buf, int* num); 88 89 struct GOST_CIPHER_PARAMS 90 { 91 libressl_d.openssl.ossl_typ.ASN1_OCTET_STRING* iv; 92 libressl_d.openssl.asn1.ASN1_OBJECT* enc_param_set; 93 } 94 95 .GOST_CIPHER_PARAMS* GOST_CIPHER_PARAMS_new(); 96 void GOST_CIPHER_PARAMS_free(.GOST_CIPHER_PARAMS* a); 97 .GOST_CIPHER_PARAMS* d2i_GOST_CIPHER_PARAMS(.GOST_CIPHER_PARAMS** a, const (ubyte)** in_, core.stdc.config.c_long len); 98 int i2d_GOST_CIPHER_PARAMS(.GOST_CIPHER_PARAMS* a, ubyte** out_); 99 extern __gshared const libressl_d.openssl.ossl_typ.ASN1_ITEM GOST_CIPHER_PARAMS_it; 100 101 enum GOST2814789IMIT_LENGTH = 4; 102 enum GOST2814789IMIT_CBLOCK = 8; 103 alias GOST2814789IMIT_LONG = uint; 104 105 struct GOST2814789IMITstate_st 106 { 107 .GOST2814789IMIT_LONG Nl; 108 .GOST2814789IMIT_LONG Nh; 109 ubyte[.GOST2814789IMIT_CBLOCK] data; 110 uint num; 111 112 .GOST2814789_KEY cipher; 113 ubyte[.GOST2814789IMIT_CBLOCK] mac; 114 } 115 116 alias GOST2814789IMIT_CTX = .GOST2814789IMITstate_st; 117 118 /* Note, also removed second parameter and removed dctx.cipher setting */ 119 int GOST2814789IMIT_Init(.GOST2814789IMIT_CTX* c, int nid); 120 int GOST2814789IMIT_Update(.GOST2814789IMIT_CTX* c, const (void)* data, size_t len); 121 int GOST2814789IMIT_Final(ubyte* md, .GOST2814789IMIT_CTX* c); 122 void GOST2814789IMIT_Transform(.GOST2814789IMIT_CTX* c, const (ubyte)* data); 123 ubyte* GOST2814789IMIT(const (ubyte)* d, size_t n, ubyte* md, int nid, const (ubyte)* key, const (ubyte)* iv); 124 125 alias GOSTR341194_LONG = uint; 126 127 enum GOSTR341194_LENGTH = 32; 128 enum GOSTR341194_CBLOCK = 32; 129 enum GOSTR341194_LBLOCK = .GOSTR341194_CBLOCK / 4; 130 131 struct GOSTR341194state_st 132 { 133 .GOSTR341194_LONG Nl; 134 .GOSTR341194_LONG Nh; 135 .GOSTR341194_LONG[.GOSTR341194_LBLOCK] data; 136 uint num; 137 138 .GOST2814789_KEY cipher; 139 ubyte[.GOSTR341194_CBLOCK] H; 140 ubyte[.GOSTR341194_CBLOCK] S; 141 } 142 143 alias GOSTR341194_CTX = .GOSTR341194state_st; 144 145 /* Note, also removed second parameter and removed dctx.cipher setting */ 146 int GOSTR341194_Init(.GOSTR341194_CTX* c, int nid); 147 int GOSTR341194_Update(.GOSTR341194_CTX* c, const (void)* data, size_t len); 148 int GOSTR341194_Final(ubyte* md, .GOSTR341194_CTX* c); 149 void GOSTR341194_Transform(.GOSTR341194_CTX* c, const (ubyte)* data); 150 ubyte* GOSTR341194(const (ubyte)* d, size_t n, ubyte* md, int nid); 151 152 alias STREEBOG_LONG64 = ulong; 153 154 enum STREEBOG_LBLOCK = 8; 155 enum STREEBOG_CBLOCK = 64; 156 enum STREEBOG256_LENGTH = 32; 157 enum STREEBOG512_LENGTH = 64; 158 159 struct STREEBOGstate_st 160 { 161 .STREEBOG_LONG64[.STREEBOG_LBLOCK] data; 162 uint num; 163 uint md_len; 164 .STREEBOG_LONG64[.STREEBOG_LBLOCK] h; 165 .STREEBOG_LONG64[.STREEBOG_LBLOCK] N; 166 .STREEBOG_LONG64[.STREEBOG_LBLOCK] Sigma; 167 } 168 169 alias STREEBOG_CTX = .STREEBOGstate_st; 170 171 int STREEBOG256_Init(.STREEBOG_CTX* c); 172 int STREEBOG256_Update(.STREEBOG_CTX* c, const (void)* data, size_t len); 173 int STREEBOG256_Final(ubyte* md, .STREEBOG_CTX* c); 174 void STREEBOG256_Transform(.STREEBOG_CTX* c, const (ubyte)* data); 175 ubyte* STREEBOG256(const (ubyte)* d, size_t n, ubyte* md); 176 177 int STREEBOG512_Init(.STREEBOG_CTX* c); 178 int STREEBOG512_Update(.STREEBOG_CTX* c, const (void)* data, size_t len); 179 int STREEBOG512_Final(ubyte* md, .STREEBOG_CTX* c); 180 void STREEBOG512_Transform(.STREEBOG_CTX* c, const (ubyte)* data); 181 ubyte* STREEBOG512(const (ubyte)* d, size_t n, ubyte* md); 182 183 struct gost_key_st; 184 alias GOST_KEY = .gost_key_st; 185 .GOST_KEY* GOST_KEY_new(); 186 void GOST_KEY_free(.GOST_KEY* r); 187 int GOST_KEY_check_key(const (.GOST_KEY)* eckey); 188 int GOST_KEY_set_public_key_affine_coordinates(.GOST_KEY* key, libressl_d.openssl.ossl_typ.BIGNUM* x, libressl_d.openssl.ossl_typ.BIGNUM* y); 189 const (libressl_d.openssl.ec.EC_GROUP)* GOST_KEY_get0_group(const (.GOST_KEY)* key); 190 int GOST_KEY_set_group(.GOST_KEY* key, const (libressl_d.openssl.ec.EC_GROUP)* group); 191 int GOST_KEY_get_digest(const (.GOST_KEY)* key); 192 int GOST_KEY_set_digest(.GOST_KEY* key, int digest_nid); 193 const (libressl_d.openssl.ossl_typ.BIGNUM)* GOST_KEY_get0_private_key(const (.GOST_KEY)* key); 194 int GOST_KEY_set_private_key(.GOST_KEY* key, const (libressl_d.openssl.ossl_typ.BIGNUM)* priv_key); 195 const (libressl_d.openssl.ec.EC_POINT)* GOST_KEY_get0_public_key(const (.GOST_KEY)* key); 196 int GOST_KEY_set_public_key(.GOST_KEY* key, const (libressl_d.openssl.ec.EC_POINT)* pub_key); 197 size_t GOST_KEY_get_size(const (.GOST_KEY)* r); 198 199 /* Gost-specific pmeth control-function parameters */ 200 /* For GOST R34.10 parameters */ 201 enum EVP_PKEY_CTRL_GOST_PARAMSET = libressl_d.openssl.evp.EVP_PKEY_ALG_CTRL + 1; 202 enum EVP_PKEY_CTRL_GOST_SIG_FORMAT = libressl_d.openssl.evp.EVP_PKEY_ALG_CTRL + 2; 203 enum EVP_PKEY_CTRL_GOST_SET_DIGEST = libressl_d.openssl.evp.EVP_PKEY_ALG_CTRL + 3; 204 enum EVP_PKEY_CTRL_GOST_GET_DIGEST = libressl_d.openssl.evp.EVP_PKEY_ALG_CTRL + 4; 205 206 enum GOST_SIG_FORMAT_SR_BE = 0; 207 enum GOST_SIG_FORMAT_RS_LE = 1; 208 209 /* BEGIN ERROR CODES */ 210 /** 211 * The following lines are auto generated by the script mkerr.pl. Any changes 212 * made after this point may be overwritten when the script is next run. 213 */ 214 void ERR_load_GOST_strings(); 215 216 /* Error codes for the GOST functions. */ 217 218 /* Function codes. */ 219 enum GOST_F_DECODE_GOST01_ALGOR_PARAMS = 104; 220 enum GOST_F_ENCODE_GOST01_ALGOR_PARAMS = 105; 221 enum GOST_F_GOST2001_COMPUTE_PUBLIC = 106; 222 enum GOST_F_GOST2001_DO_SIGN = 107; 223 enum GOST_F_GOST2001_DO_VERIFY = 108; 224 enum GOST_F_GOST2001_KEYGEN = 109; 225 enum GOST_F_GOST89_GET_ASN1_PARAMETERS = 102; 226 enum GOST_F_GOST89_SET_ASN1_PARAMETERS = 103; 227 enum GOST_F_GOST_KEY_CHECK_KEY = 124; 228 enum GOST_F_GOST_KEY_NEW = 125; 229 enum GOST_F_GOST_KEY_SET_PUBLIC_KEY_AFFINE_COORDINATES = 126; 230 enum GOST_F_PARAM_COPY_GOST01 = 110; 231 enum GOST_F_PARAM_DECODE_GOST01 = 111; 232 enum GOST_F_PKEY_GOST01_CTRL = 116; 233 enum GOST_F_PKEY_GOST01_DECRYPT = 112; 234 enum GOST_F_PKEY_GOST01_DERIVE = 113; 235 enum GOST_F_PKEY_GOST01_ENCRYPT = 114; 236 enum GOST_F_PKEY_GOST01_PARAMGEN = 115; 237 enum GOST_F_PKEY_GOST01_SIGN = 123; 238 enum GOST_F_PKEY_GOST_MAC_CTRL = 100; 239 enum GOST_F_PKEY_GOST_MAC_KEYGEN = 101; 240 enum GOST_F_PRIV_DECODE_GOST01 = 117; 241 enum GOST_F_PUB_DECODE_GOST01 = 118; 242 enum GOST_F_PUB_ENCODE_GOST01 = 119; 243 enum GOST_F_PUB_PRINT_GOST01 = 120; 244 enum GOST_F_UNPACK_SIGNATURE_CP = 121; 245 enum GOST_F_UNPACK_SIGNATURE_LE = 122; 246 247 /* Reason codes. */ 248 enum GOST_R_BAD_KEY_PARAMETERS_FORMAT = 104; 249 enum GOST_R_BAD_PKEY_PARAMETERS_FORMAT = 105; 250 enum GOST_R_CANNOT_PACK_EPHEMERAL_KEY = 106; 251 enum GOST_R_CTRL_CALL_FAILED = 107; 252 enum GOST_R_ERROR_COMPUTING_SHARED_KEY = 108; 253 enum GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO = 109; 254 enum GOST_R_INCOMPATIBLE_ALGORITHMS = 110; 255 enum GOST_R_INCOMPATIBLE_PEER_KEY = 111; 256 enum GOST_R_INVALID_DIGEST_TYPE = 100; 257 enum GOST_R_INVALID_IV_LENGTH = 103; 258 enum GOST_R_INVALID_MAC_KEY_LENGTH = 101; 259 enum GOST_R_KEY_IS_NOT_INITIALIZED = 112; 260 enum GOST_R_KEY_PARAMETERS_MISSING = 113; 261 enum GOST_R_MAC_KEY_NOT_SET = 102; 262 enum GOST_R_NO_PARAMETERS_SET = 115; 263 enum GOST_R_NO_PEER_KEY = 116; 264 enum GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR = 117; 265 enum GOST_R_PUBLIC_KEY_UNDEFINED = 118; 266 enum GOST_R_RANDOM_NUMBER_GENERATOR_FAILED = 120; 267 enum GOST_R_SIGNATURE_MISMATCH = 121; 268 enum GOST_R_SIGNATURE_PARTS_GREATER_THAN_Q = 122; 269 enum GOST_R_UKM_NOT_SET = 123;