]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_grasshopper_cipher.h
Implementation of the MGM mode for magma/kuznyechik
[openssl-gost/engine.git] / gost_grasshopper_cipher.h
1 /*
2  * Maxim Tishkov 2016
3  * This file is distributed under the same license as OpenSSL
4  */
5
6 #ifndef GOST_GRASSHOPPER_CIPHER_H
7 #define GOST_GRASSHOPPER_CIPHER_H
8
9 #define SN_kuznyechik_mgm      "kuznyechik-mgm"
10
11 #if defined(__cplusplus)
12 extern "C" {
13 #endif
14
15 #include "gost_grasshopper_defines.h"
16
17 #include "gost_lcl.h"
18 #include <openssl/evp.h>
19
20 // not thread safe
21 // because of buffers
22 typedef struct {
23     uint8_t type;
24     grasshopper_key_t master_key;
25     grasshopper_key_t key;
26     grasshopper_round_keys_t encrypt_round_keys;
27     grasshopper_round_keys_t decrypt_round_keys;
28     grasshopper_w128_t buffer;
29 } gost_grasshopper_cipher_ctx;
30
31 typedef struct {
32     gost_grasshopper_cipher_ctx c;
33     grasshopper_w128_t partial_buffer;
34     unsigned int section_size;  /* After how much bytes mesh the key,
35                                    if 0 never mesh and work like plain ctr. */
36     unsigned char kdf_seed[8];
37                 unsigned char tag[16];
38                 EVP_MD_CTX *omac_ctx;
39 } gost_grasshopper_cipher_ctx_ctr;
40
41 static void gost_grasshopper_cipher_key(gost_grasshopper_cipher_ctx* c, const uint8_t* k);
42
43 static void gost_grasshopper_cipher_destroy(gost_grasshopper_cipher_ctx* c);
44
45 static int gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX* ctx,
46     const unsigned char* key, const unsigned char* iv, int enc);
47
48 static int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX* ctx,
49     const unsigned char* key, const unsigned char* iv, int enc);
50
51 static int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX* ctx,
52     const unsigned char* key, const unsigned char* iv, int enc);
53
54 static int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX* ctx,
55     const unsigned char* key, const unsigned char* iv, int enc);
56
57 static int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX* ctx,
58     const unsigned char* key, const unsigned char* iv, int enc);
59
60 static int gost_grasshopper_cipher_init_ctracpkm(EVP_CIPHER_CTX* ctx,
61     const unsigned char* key, const unsigned char* iv, int enc);
62
63 static int gost_grasshopper_cipher_init_ctracpkm_omac(EVP_CIPHER_CTX* ctx,
64     const unsigned char* key, const unsigned char* iv, int enc);
65
66 static int gost_grasshopper_cipher_init_mgm(EVP_CIPHER_CTX* ctx,
67     const unsigned char* key, const unsigned char* iv, int enc);
68
69 static int gost_grasshopper_cipher_init(EVP_CIPHER_CTX* ctx, const unsigned char* key,
70     const unsigned char* iv, int enc);
71
72 static int gost_grasshopper_cipher_do(EVP_CIPHER_CTX* ctx, unsigned char* out,
73     const unsigned char* in, size_t inl);
74
75 static int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX* ctx, unsigned char* out,
76     const unsigned char* in, size_t inl);
77
78 static int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX* ctx, unsigned char* out,
79     const unsigned char* in, size_t inl);
80
81 static int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX* ctx, unsigned char* out,
82     const unsigned char* in, size_t inl);
83
84 static int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX* ctx, unsigned char* out,
85     const unsigned char* in, size_t inl);
86
87 static int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX* ctx, unsigned char* out,
88     const unsigned char* in, size_t inl);
89
90 static int gost_grasshopper_cipher_do_ctracpkm(EVP_CIPHER_CTX* ctx, unsigned char* out,
91     const unsigned char* in, size_t inl);
92
93 static int gost_grasshopper_cipher_do_ctracpkm_omac(EVP_CIPHER_CTX* ctx, unsigned char* out,
94     const unsigned char* in, size_t inl);
95
96 static int gost_grasshopper_cipher_do_mgm(EVP_CIPHER_CTX* ctx, unsigned char* out,
97     const unsigned char* in, size_t inl);
98
99 static int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX* ctx);
100
101 static int gost_grasshopper_mgm_cleanup(EVP_CIPHER_CTX *c);
102
103 static int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params);
104
105 static int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params);
106
107 static int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX* ctx, int type, int arg, void* ptr);
108
109 static int gost_grasshopper_mgm_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr);
110
111 const EVP_CIPHER* cipher_gost_grasshopper_ctracpkm();
112
113 #if defined(__cplusplus)
114 }
115 #endif
116
117 #endif