1238384Sjkim/**********************************************************************
2238384Sjkim *                        gost89.h                                    *
3238384Sjkim *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4238384Sjkim *     This file is distributed under the same license as OpenSSL     *
5238384Sjkim *                                                                    *
6238384Sjkim *          Declarations for GOST 28147-89 encryption algorithm       *
7238384Sjkim *            No OpenSSL libraries required to compile and use        *
8238384Sjkim *                       this code                                    *
9280297Sjkim **********************************************************************/
10238384Sjkim#ifndef GOST89_H
11280297Sjkim# define GOST89_H
12238384Sjkim
13238384Sjkim/* Typedef for unsigned 32-bit integer */
14280297Sjkim# if __LONG_MAX__ > 2147483647L
15280297Sjkimtypedef unsigned int u4;
16280297Sjkim# else
17280297Sjkimtypedef unsigned long u4;
18280297Sjkim# endif
19238384Sjkim/* Typedef for unsigned 8-bit integer */
20280297Sjkimtypedef unsigned char byte;
21238384Sjkim
22238384Sjkim/* Internal representation of GOST substitution blocks */
23238384Sjkimtypedef struct {
24280297Sjkim    byte k8[16];
25280297Sjkim    byte k7[16];
26280297Sjkim    byte k6[16];
27280297Sjkim    byte k5[16];
28280297Sjkim    byte k4[16];
29280297Sjkim    byte k3[16];
30280297Sjkim    byte k2[16];
31280297Sjkim    byte k1[16];
32280297Sjkim} gost_subst_block;
33238384Sjkim
34238384Sjkim/* Cipher context includes key and preprocessed  substitution block */
35280297Sjkimtypedef struct {
36280297Sjkim    u4 k[8];
37280297Sjkim    /* Constant s-boxes -- set up in gost_init(). */
38280297Sjkim    u4 k87[256], k65[256], k43[256], k21[256];
39280297Sjkim} gost_ctx;
40280297Sjkim/*
41280297Sjkim * Note: encrypt and decrypt expect full blocks--padding blocks is caller's
42280297Sjkim * responsibility. All bulk encryption is done in ECB mode by these calls.
43280297Sjkim * Other modes may be added easily enough.
44280297Sjkim */
45238384Sjkim/* Encrypt several full blocks in ECB mode */
46280297Sjkimvoid gost_enc(gost_ctx * ctx, const byte * clear, byte * cipher, int blocks);
47238384Sjkim/* Decrypt several full blocks in ECB mode */
48280297Sjkimvoid gost_dec(gost_ctx * ctx, const byte * cipher, byte * clear, int blocks);
49238384Sjkim/* Encrypts several full blocks in CFB mode using 8byte IV */
50280297Sjkimvoid gost_enc_cfb(gost_ctx * ctx, const byte * iv, const byte * clear,
51280297Sjkim                  byte * cipher, int blocks);
52238384Sjkim/* Decrypts several full blocks in CFB mode using 8byte IV */
53280297Sjkimvoid gost_dec_cfb(gost_ctx * ctx, const byte * iv, const byte * cipher,
54280297Sjkim                  byte * clear, int blocks);
55238384Sjkim
56238384Sjkim/* Encrypt one  block */
57280297Sjkimvoid gostcrypt(gost_ctx * c, const byte * in, byte * out);
58238384Sjkim/* Decrypt one  block */
59280297Sjkimvoid gostdecrypt(gost_ctx * c, const byte * in, byte * out);
60238384Sjkim/* Set key into context */
61280297Sjkimvoid gost_key(gost_ctx * ctx, const byte * key);
62238384Sjkim/* Get key from context */
63280297Sjkimvoid gost_get_key(gost_ctx * ctx, byte * key);
64238384Sjkim/* Set S-blocks into context */
65280297Sjkimvoid gost_init(gost_ctx * ctx, const gost_subst_block * subst_block);
66238384Sjkim/* Clean up context */
67280297Sjkimvoid gost_destroy(gost_ctx * ctx);
68238384Sjkim/* Intermediate function used for calculate hash */
69280297Sjkimvoid gost_enc_with_key(gost_ctx *, byte * key, byte * inblock,
70280297Sjkim                       byte * outblock);
71238384Sjkim/* Compute MAC of given length in bits from data */
72280297Sjkimint gost_mac(gost_ctx * ctx, int hmac_len, const unsigned char *data,
73280297Sjkim             unsigned int data_len, unsigned char *hmac);
74280297Sjkim/*
75280297Sjkim * Compute MAC of given length in bits from data, using non-zero 8-byte IV
76280297Sjkim * (non-standard, for use in CryptoPro key transport only
77280297Sjkim */
78280297Sjkimint gost_mac_iv(gost_ctx * ctx, int hmac_len, const unsigned char *iv,
79280297Sjkim                const unsigned char *data, unsigned int data_len,
80280297Sjkim                unsigned char *hmac);
81238384Sjkim/* Perform one step of MAC calculation like gostcrypt */
82280297Sjkimvoid mac_block(gost_ctx * c, byte * buffer, const byte * block);
83238384Sjkim/* Extracts MAC value from mac state buffer */
84280297Sjkimvoid get_mac(byte * buffer, int nbits, byte * out);
85238384Sjkim/* Implements cryptopro key meshing algorithm. Expect IV to be 8-byte size*/
86280297Sjkimvoid cryptopro_key_meshing(gost_ctx * ctx, unsigned char *iv);
87238384Sjkim/* Parameter sets specified in RFC 4357 */
88238384Sjkimextern gost_subst_block GostR3411_94_TestParamSet;
89238384Sjkimextern gost_subst_block GostR3411_94_CryptoProParamSet;
90238384Sjkimextern gost_subst_block Gost28147_TestParamSet;
91238384Sjkimextern gost_subst_block Gost28147_CryptoProParamSetA;
92238384Sjkimextern gost_subst_block Gost28147_CryptoProParamSetB;
93238384Sjkimextern gost_subst_block Gost28147_CryptoProParamSetC;
94238384Sjkimextern gost_subst_block Gost28147_CryptoProParamSetD;
95280297Sjkimextern const byte CryptoProKeyMeshingKey[];
96280297Sjkimtypedef unsigned int word32;
97238384Sjkim
98238384Sjkim#endif
99