2006-09-17 13:00:18 +00:00
|
|
|
/**********************************************************************
|
|
|
|
* gost89.h *
|
|
|
|
* Copyright (c) 2005-2006 Cryptocom LTD *
|
|
|
|
* This file is distributed under the same license as OpenSSL *
|
|
|
|
* *
|
|
|
|
* Declarations for GOST 28147-89 encryption algorithm *
|
|
|
|
* No OpenSSL libraries required to compile and use *
|
|
|
|
* this code *
|
2015-01-21 23:55:44 +00:00
|
|
|
**********************************************************************/
|
2006-09-17 13:00:18 +00:00
|
|
|
#ifndef GOST89_H
|
2015-01-21 23:55:44 +00:00
|
|
|
# define GOST89_H
|
2006-09-17 13:00:18 +00:00
|
|
|
|
|
|
|
/* Typedef for unsigned 32-bit integer */
|
2015-01-21 23:55:44 +00:00
|
|
|
# if __LONG_MAX__ > 2147483647L
|
|
|
|
typedef unsigned int u4;
|
|
|
|
# else
|
|
|
|
typedef unsigned long u4;
|
|
|
|
# endif
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Typedef for unsigned 8-bit integer */
|
2015-01-21 23:55:44 +00:00
|
|
|
typedef unsigned char byte;
|
2006-09-17 13:00:18 +00:00
|
|
|
|
|
|
|
/* Internal representation of GOST substitution blocks */
|
|
|
|
typedef struct {
|
2015-01-21 23:55:44 +00:00
|
|
|
byte k8[16];
|
|
|
|
byte k7[16];
|
|
|
|
byte k6[16];
|
|
|
|
byte k5[16];
|
|
|
|
byte k4[16];
|
|
|
|
byte k3[16];
|
|
|
|
byte k2[16];
|
|
|
|
byte k1[16];
|
|
|
|
} gost_subst_block;
|
2006-09-17 13:00:18 +00:00
|
|
|
|
|
|
|
/* Cipher context includes key and preprocessed substitution block */
|
2015-01-21 23:55:44 +00:00
|
|
|
typedef struct {
|
|
|
|
u4 k[8];
|
|
|
|
/* Constant s-boxes -- set up in gost_init(). */
|
|
|
|
u4 k87[256], k65[256], k43[256], k21[256];
|
|
|
|
} gost_ctx;
|
|
|
|
/*
|
|
|
|
* Note: encrypt and decrypt expect full blocks--padding blocks is caller's
|
|
|
|
* responsibility. All bulk encryption is done in ECB mode by these calls.
|
|
|
|
* Other modes may be added easily enough.
|
|
|
|
*/
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Encrypt several full blocks in ECB mode */
|
2015-01-21 23:55:44 +00:00
|
|
|
void gost_enc(gost_ctx * ctx, const byte * clear, byte * cipher, int blocks);
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Decrypt several full blocks in ECB mode */
|
2015-01-21 23:55:44 +00:00
|
|
|
void gost_dec(gost_ctx * ctx, const byte * cipher, byte * clear, int blocks);
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Encrypts several full blocks in CFB mode using 8byte IV */
|
2015-01-21 23:55:44 +00:00
|
|
|
void gost_enc_cfb(gost_ctx * ctx, const byte * iv, const byte * clear,
|
|
|
|
byte * cipher, int blocks);
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Decrypts several full blocks in CFB mode using 8byte IV */
|
2015-01-21 23:55:44 +00:00
|
|
|
void gost_dec_cfb(gost_ctx * ctx, const byte * iv, const byte * cipher,
|
|
|
|
byte * clear, int blocks);
|
2006-09-17 13:00:18 +00:00
|
|
|
|
|
|
|
/* Encrypt one block */
|
2015-01-21 23:55:44 +00:00
|
|
|
void gostcrypt(gost_ctx * c, const byte * in, byte * out);
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Decrypt one block */
|
2015-01-21 23:55:44 +00:00
|
|
|
void gostdecrypt(gost_ctx * c, const byte * in, byte * out);
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Set key into context */
|
2015-01-21 23:55:44 +00:00
|
|
|
void gost_key(gost_ctx * ctx, const byte * key);
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Get key from context */
|
2015-01-21 23:55:44 +00:00
|
|
|
void gost_get_key(gost_ctx * ctx, byte * key);
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Set S-blocks into context */
|
2015-01-21 23:55:44 +00:00
|
|
|
void gost_init(gost_ctx * ctx, const gost_subst_block * subst_block);
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Clean up context */
|
2015-01-21 23:55:44 +00:00
|
|
|
void gost_destroy(gost_ctx * ctx);
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Intermediate function used for calculate hash */
|
2015-01-21 23:55:44 +00:00
|
|
|
void gost_enc_with_key(gost_ctx *, byte * key, byte * inblock,
|
|
|
|
byte * outblock);
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Compute MAC of given length in bits from data */
|
2015-01-21 23:55:44 +00:00
|
|
|
int gost_mac(gost_ctx * ctx, int hmac_len, const unsigned char *data,
|
|
|
|
unsigned int data_len, unsigned char *hmac);
|
|
|
|
/*
|
|
|
|
* Compute MAC of given length in bits from data, using non-zero 8-byte IV
|
|
|
|
* (non-standard, for use in CryptoPro key transport only
|
|
|
|
*/
|
|
|
|
int gost_mac_iv(gost_ctx * ctx, int hmac_len, const unsigned char *iv,
|
|
|
|
const unsigned char *data, unsigned int data_len,
|
|
|
|
unsigned char *hmac);
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Perform one step of MAC calculation like gostcrypt */
|
2015-01-21 23:55:44 +00:00
|
|
|
void mac_block(gost_ctx * c, byte * buffer, const byte * block);
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Extracts MAC value from mac state buffer */
|
2015-01-21 23:55:44 +00:00
|
|
|
void get_mac(byte * buffer, int nbits, byte * out);
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Implements cryptopro key meshing algorithm. Expect IV to be 8-byte size*/
|
2015-01-21 23:55:44 +00:00
|
|
|
void cryptopro_key_meshing(gost_ctx * ctx, unsigned char *iv);
|
2006-09-17 13:00:18 +00:00
|
|
|
/* Parameter sets specified in RFC 4357 */
|
|
|
|
extern gost_subst_block GostR3411_94_TestParamSet;
|
|
|
|
extern gost_subst_block GostR3411_94_CryptoProParamSet;
|
|
|
|
extern gost_subst_block Gost28147_TestParamSet;
|
|
|
|
extern gost_subst_block Gost28147_CryptoProParamSetA;
|
|
|
|
extern gost_subst_block Gost28147_CryptoProParamSetB;
|
|
|
|
extern gost_subst_block Gost28147_CryptoProParamSetC;
|
|
|
|
extern gost_subst_block Gost28147_CryptoProParamSetD;
|
2015-01-21 23:55:44 +00:00
|
|
|
extern const byte CryptoProKeyMeshingKey[];
|
|
|
|
typedef unsigned int word32;
|
2006-09-17 13:00:18 +00:00
|
|
|
|
|
|
|
#endif
|