gost89.h revision 279264
1/**********************************************************************
2 *                        gost89.h                                    *
3 *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4 *     This file is distributed under the same license as OpenSSL     *
5 *                                                                    *
6 *          Declarations for GOST 28147-89 encryption algorithm       *
7 *            No OpenSSL libraries required to compile and use        *
8 *                       this code                                    *
9 **********************************************************************/
10#ifndef GOST89_H
11#define GOST89_H
12
13/* Typedef for unsigned 32-bit integer */
14#if __LONG_MAX__ > 2147483647L
15typedef unsigned int u4;
16#else
17typedef unsigned long u4;
18#endif
19/* Typedef for unsigned 8-bit integer */
20typedef unsigned char byte;
21
22/* Internal representation of GOST substitution blocks */
23typedef struct {
24		byte k8[16];
25		byte k7[16];
26		byte k6[16];
27		byte k5[16];
28		byte k4[16];
29		byte k3[16];
30		byte k2[16];
31		byte k1[16];
32} gost_subst_block;
33
34
35/* Cipher context includes key and preprocessed  substitution block */
36typedef struct {
37		u4 k[8];
38		/* Constant s-boxes -- set up in gost_init(). */
39		u4 k87[256],k65[256],k43[256],k21[256];
40} gost_ctx;
41/* Note: encrypt and decrypt expect full blocks--padding blocks is
42         caller's responsibility. All bulk encryption is done in
43		 ECB mode by these calls. Other modes may be added easily
44		 enough.                                            */
45/* Encrypt several full blocks in ECB mode */
46void gost_enc(gost_ctx *ctx, const byte *clear,byte *cipher, int blocks);
47/* Decrypt several full blocks in ECB mode */
48void gost_dec(gost_ctx *ctx, const byte *cipher,byte *clear, int blocks);
49/* Encrypts several full blocks in CFB mode using 8byte IV */
50void gost_enc_cfb(gost_ctx *ctx,const byte *iv,const byte *clear,byte *cipher,int  blocks);
51/* Decrypts several full blocks in CFB mode using 8byte IV */
52void gost_dec_cfb(gost_ctx *ctx,const byte *iv,const byte *cipher,byte *clear,int  blocks);
53
54/* Encrypt one  block */
55void gostcrypt(gost_ctx *c, const byte *in, byte *out);
56/* Decrypt one  block */
57void gostdecrypt(gost_ctx *c, const byte *in,byte *out);
58/* Set key into context */
59void gost_key(gost_ctx *ctx, const byte *key);
60/* Get key from context */
61void gost_get_key(gost_ctx *ctx, byte *key);
62/* Set S-blocks into context */
63void gost_init(gost_ctx *ctx, const gost_subst_block *subst_block);
64/* Clean up context */
65void gost_destroy(gost_ctx *ctx);
66/* Intermediate function used for calculate hash */
67void gost_enc_with_key(gost_ctx *,byte *key,byte *inblock,byte *outblock);
68/* Compute MAC of given length in bits from data */
69int gost_mac(gost_ctx *ctx,int hmac_len,const unsigned char *data,
70		unsigned int data_len,unsigned char *hmac) ;
71/* Compute MAC of given length in bits from data, using non-zero 8-byte
72 * IV (non-standard, for use in CryptoPro key transport only */
73int gost_mac_iv(gost_ctx *ctx,int hmac_len,const unsigned char *iv,const unsigned char *data,
74		unsigned int data_len,unsigned char *hmac) ;
75/* Perform one step of MAC calculation like gostcrypt */
76void mac_block(gost_ctx *c,byte *buffer,const  byte *block);
77/* Extracts MAC value from mac state buffer */
78void get_mac(byte *buffer,int nbits,byte *out);
79/* Implements cryptopro key meshing algorithm. Expect IV to be 8-byte size*/
80void cryptopro_key_meshing(gost_ctx *ctx, unsigned char *iv);
81/* Parameter sets specified in RFC 4357 */
82extern gost_subst_block GostR3411_94_TestParamSet;
83extern gost_subst_block GostR3411_94_CryptoProParamSet;
84extern gost_subst_block Gost28147_TestParamSet;
85extern gost_subst_block Gost28147_CryptoProParamSetA;
86extern gost_subst_block Gost28147_CryptoProParamSetB;
87extern gost_subst_block Gost28147_CryptoProParamSetC;
88extern gost_subst_block Gost28147_CryptoProParamSetD;
89extern const byte CryptoProKeyMeshingKey[];
90typedef unsigned int word32;
91
92#endif
93