]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_grasshopper_cipher.h
Merge branch 'mgm_impl' of https://github.com/gost-engine/engine into mgm_impl
[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 #if defined(__cplusplus)
10 extern "C" {
11 #endif
12
13 #include "gost_grasshopper_defines.h"
14
15 #include <openssl/evp.h>
16
17 // not thread safe
18 // because of buffers
19 typedef struct {
20     uint8_t type;
21     grasshopper_key_t master_key;
22     grasshopper_key_t key;
23     grasshopper_round_keys_t encrypt_round_keys;
24     grasshopper_round_keys_t decrypt_round_keys;
25     grasshopper_w128_t buffer;
26 } gost_grasshopper_cipher_ctx;
27
28 typedef struct {
29     gost_grasshopper_cipher_ctx c;
30     grasshopper_w128_t buffer1;
31 } gost_grasshopper_cipher_ctx_ofb;
32
33 typedef struct {
34     gost_grasshopper_cipher_ctx c;
35     grasshopper_w128_t partial_buffer;
36     unsigned int section_size;  /* After how much bytes mesh the key,
37                                    if 0 never mesh and work like plain ctr. */
38 } gost_grasshopper_cipher_ctx_ctr;
39
40 typedef enum {
41         mgm_associated_data = 0,
42         mgm_main_data,
43 } mgm_state;
44
45 typedef struct {
46     gost_grasshopper_cipher_ctx c;
47     grasshopper_w128_t partial_buffer;
48         
49                 mgm_state mgm_state; /* associated_data/plain text */
50                 grasshopper_w128_t mgm_iv[16]; /* nonce */
51                 grasshopper_w128_t mgm_partial_buffer; /* Rest of associated data */
52                 size_t ad_length;
53                 size_t taglen; /* MAC length*/
54                 unsigned char tag[16]; /* MAC - intermediate state */
55                 unsigned char final_tag[16]; /* MAC - final state*/
56 } gost_grasshopper_cipher_ctx_mgm;
57
58 typedef int (* grasshopper_init_cipher_func)(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv,
59                                              int enc);
60
61 typedef int (* grasshopper_do_cipher_func)(EVP_CIPHER_CTX* ctx, unsigned char* out, const unsigned char* in,
62                                            size_t inl);
63
64 typedef void (* grasshopper_destroy_cipher_func)(gost_grasshopper_cipher_ctx* c);
65
66 void gost_grasshopper_cipher_key(gost_grasshopper_cipher_ctx* c, const uint8_t* k);
67
68 void gost_grasshopper_cipher_destroy(gost_grasshopper_cipher_ctx* c);
69
70 int gost_grasshopper_cipher_init_ecb(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
71
72 int gost_grasshopper_cipher_init_cbc(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
73
74 int gost_grasshopper_cipher_init_ofb(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
75
76 int gost_grasshopper_cipher_init_cfb(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
77
78 int gost_grasshopper_cipher_init_ctr(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
79
80 int gost_grasshopper_cipher_init_ctracpkm(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
81
82 int gost_grasshopper_cipher_init_mgm(EVP_CIPHER_CTX* ctx, const unsigned char* key, const unsigned char* iv, int enc);
83
84 int gost_grasshopper_cipher_init(EVP_CIPHER_CTX* ctx, const unsigned char* key,
85                                  const unsigned char* iv, int enc);
86
87 int gost_grasshopper_cipher_do(EVP_CIPHER_CTX* ctx, unsigned char* out,
88                                const unsigned char* in, size_t inl);
89
90 int gost_grasshopper_cipher_do_ecb(EVP_CIPHER_CTX* ctx, unsigned char* out,
91                                    const unsigned char* in, size_t inl);
92
93 int gost_grasshopper_cipher_do_cbc(EVP_CIPHER_CTX* ctx, unsigned char* out,
94                                    const unsigned char* in, size_t inl);
95
96 int gost_grasshopper_cipher_do_ofb(EVP_CIPHER_CTX* ctx, unsigned char* out,
97                                    const unsigned char* in, size_t inl);
98
99 int gost_grasshopper_cipher_do_cfb(EVP_CIPHER_CTX* ctx, unsigned char* out,
100                                    const unsigned char* in, size_t inl);
101
102 int gost_grasshopper_cipher_do_ctr(EVP_CIPHER_CTX* ctx, unsigned char* out,
103                                    const unsigned char* in, size_t inl);
104 int gost_grasshopper_cipher_do_ctracpkm(EVP_CIPHER_CTX* ctx, unsigned char* out,
105                                    const unsigned char* in, size_t inl);
106 int gost_grasshopper_cipher_do_mgm(EVP_CIPHER_CTX* ctx, unsigned char* out,
107                                    const unsigned char* in, size_t inl);
108
109 int gost_grasshopper_cipher_cleanup(EVP_CIPHER_CTX* ctx);
110
111 int gost_grasshopper_set_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params);
112
113 int gost_grasshopper_get_asn1_parameters(EVP_CIPHER_CTX* ctx, ASN1_TYPE* params);
114
115 int gost_grasshopper_cipher_ctl(EVP_CIPHER_CTX* ctx, int type, int arg, void* ptr);
116
117 EVP_CIPHER* cipher_gost_grasshopper_create(int cipher_type, int block_size);
118
119 const int cipher_gost_grasshopper_setup(EVP_CIPHER* cipher, uint8_t mode, int iv_size, bool padding, int extra_flags);
120
121 const EVP_CIPHER* cipher_gost_grasshopper(uint8_t mode, uint8_t num);
122
123 extern const EVP_CIPHER* cipher_gost_grasshopper_ecb();
124 extern const EVP_CIPHER* cipher_gost_grasshopper_cbc();
125 extern const EVP_CIPHER* cipher_gost_grasshopper_ofb();
126 extern const EVP_CIPHER* cipher_gost_grasshopper_cfb();
127 extern const EVP_CIPHER* cipher_gost_grasshopper_ctr();
128 extern const EVP_CIPHER* cipher_gost_grasshopper_ctracpkm();
129 extern const EVP_CIPHER* cipher_gost_grasshopper_mgm();
130
131 void cipher_gost_grasshopper_destroy(void);
132
133 #if defined(__cplusplus)
134 }
135 #endif
136
137 #endif