gost89.h 4.0 KB

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