]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_keyexpimp.c
gost_keyexpimp: Rework cipher registration
[openssl-gost/engine.git] / gost_keyexpimp.c
1 #ifdef _WIN32
2 #include <winsock.h>
3 #else
4 #include <arpa/inet.h>
5 #endif
6 #include <string.h>
7 #include <openssl/evp.h>
8 #include <openssl/hmac.h>
9
10 #include "gost_lcl.h"
11 #include "e_gost_err.h"
12
13 int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
14 /*
15  * Function expects that out is a preallocated buffer of length
16  * defined as sum of shared_len and mac length defined by mac_nid
17  * */
18 int gost_kexp15(const unsigned char *shared_key, const int shared_len,
19                 int cipher_nid, const unsigned char *cipher_key,
20                 int mac_nid, unsigned char *mac_key,
21                 const unsigned char *iv, const size_t ivlen,
22                 unsigned char *out, int *out_len)
23 {
24     unsigned char iv_full[16], mac_buf[16];
25     unsigned int mac_len;
26
27     EVP_CIPHER_CTX *ciph = NULL;
28     EVP_MD_CTX *mac = NULL;
29
30     int ret = 0;
31     int len;
32
33     mac_len = (cipher_nid == NID_magma_ctr) ? 8 :
34         (cipher_nid == NID_grasshopper_ctr) ? 16 : 0;
35
36     if (mac_len == 0) {
37         GOSTerr(GOST_F_GOST_KEXP15, GOST_R_INVALID_CIPHER);
38         goto err;
39     }
40
41     /* we expect IV of half length */
42     memset(iv_full, 0, 16);
43     memcpy(iv_full, iv, ivlen);
44
45     mac = EVP_MD_CTX_new();
46     if (mac == NULL) {
47         GOSTerr(GOST_F_GOST_KEXP15, ERR_R_MALLOC_FAILURE);
48         goto err;
49     }
50
51     if (EVP_DigestInit_ex(mac, EVP_get_digestbynid(mac_nid), NULL) <= 0
52         || omac_imit_ctrl(mac, EVP_MD_CTRL_SET_KEY, 32, mac_key) <= 0
53         || omac_imit_ctrl(mac, EVP_MD_CTRL_XOF_LEN, mac_len, NULL) <= 0
54         || EVP_DigestUpdate(mac, iv, ivlen) <= 0
55         || EVP_DigestUpdate(mac, shared_key, shared_len) <= 0
56         /* As we set MAC length directly, we should not allow overwriting it */
57         || EVP_DigestFinalXOF(mac, mac_buf, mac_len) <= 0) {
58         GOSTerr(GOST_F_GOST_KEXP15, ERR_R_INTERNAL_ERROR);
59         goto err;
60     }
61
62     ciph = EVP_CIPHER_CTX_new();
63     if (ciph == NULL) {
64         GOSTerr(GOST_F_GOST_KEXP15, ERR_R_MALLOC_FAILURE);
65         goto err;
66     }
67
68     if (EVP_CipherInit_ex
69         (ciph, EVP_get_cipherbynid(cipher_nid), NULL, NULL, NULL, 1) <= 0
70         || EVP_CipherInit_ex(ciph, NULL, NULL, cipher_key, iv_full, 1) <= 0
71         || EVP_CipherUpdate(ciph, out, &len, shared_key, shared_len) <= 0
72         || EVP_CipherUpdate(ciph, out + shared_len, &len, mac_buf, mac_len) <= 0
73         || EVP_CipherFinal_ex(ciph, out + shared_len + len, out_len) <= 0) {
74         GOSTerr(GOST_F_GOST_KEXP15, ERR_R_INTERNAL_ERROR);
75         goto err;
76     }
77
78     *out_len = shared_len + mac_len;
79
80     ret = 1;
81
82  err:
83     OPENSSL_cleanse(mac_buf, mac_len);
84     EVP_MD_CTX_free(mac);
85     EVP_CIPHER_CTX_free(ciph);
86
87     return ret;
88 }
89
90 /*
91  * Function expects that shared_key is a preallocated buffer
92  * with length defined as expkeylen + mac_len defined by mac_nid
93  * */
94 int gost_kimp15(const unsigned char *expkey, const size_t expkeylen,
95                 int cipher_nid, const unsigned char *cipher_key,
96                 int mac_nid, unsigned char *mac_key,
97                 const unsigned char *iv, const size_t ivlen,
98                 unsigned char *shared_key)
99 {
100     unsigned char iv_full[16], out[48], mac_buf[16];
101     unsigned int mac_len;
102     const size_t shared_len = 32;
103
104     EVP_CIPHER_CTX *ciph = NULL;
105     EVP_MD_CTX *mac = NULL;
106
107     int ret = 0;
108     int len;
109
110     mac_len = (cipher_nid == NID_magma_ctr) ? 8 :
111         (cipher_nid == NID_grasshopper_ctr) ? 16 : 0;
112
113     if (mac_len == 0) {
114         GOSTerr(GOST_F_GOST_KIMP15, GOST_R_INVALID_CIPHER);
115         goto err;
116     }
117
118     /* we expect IV of half length */
119     memset(iv_full, 0, 16);
120     memcpy(iv_full, iv, ivlen);
121
122     ciph = EVP_CIPHER_CTX_new();
123     if (ciph == NULL) {
124         GOSTerr(GOST_F_GOST_KIMP15, ERR_R_MALLOC_FAILURE);
125         goto err;
126     }
127
128     if (EVP_CipherInit_ex
129         (ciph, EVP_get_cipherbynid(cipher_nid), NULL, NULL, NULL, 0) <= 0
130         || EVP_CipherInit_ex(ciph, NULL, NULL, cipher_key, iv_full, 0) <= 0
131         || EVP_CipherUpdate(ciph, out, &len, expkey, expkeylen) <= 0
132         || EVP_CipherFinal_ex(ciph, out + len, &len) <= 0) {
133         GOSTerr(GOST_F_GOST_KIMP15, ERR_R_INTERNAL_ERROR);
134         goto err;
135     }
136     /*Now we have shared key and mac in out[] */
137
138     mac = EVP_MD_CTX_new();
139     if (mac == NULL) {
140         GOSTerr(GOST_F_GOST_KIMP15, ERR_R_MALLOC_FAILURE);
141         goto err;
142     }
143
144     if (EVP_DigestInit_ex(mac, EVP_get_digestbynid(mac_nid), NULL) <= 0
145         || omac_imit_ctrl(mac, EVP_MD_CTRL_SET_KEY, 32, mac_key) <= 0
146         || omac_imit_ctrl(mac, EVP_MD_CTRL_XOF_LEN, mac_len, NULL) <= 0
147         || EVP_DigestUpdate(mac, iv, ivlen) <= 0
148         || EVP_DigestUpdate(mac, out, shared_len) <= 0
149         /* As we set MAC length directly, we should not allow overwriting it */
150         || EVP_DigestFinalXOF(mac, mac_buf, mac_len) <= 0) {
151         GOSTerr(GOST_F_GOST_KIMP15, ERR_R_INTERNAL_ERROR);
152         goto err;
153     }
154
155     if (CRYPTO_memcmp(mac_buf, out + shared_len, mac_len) != 0) {
156         GOSTerr(GOST_F_GOST_KIMP15, GOST_R_BAD_MAC);
157         goto err;
158     }
159
160     memcpy(shared_key, out, shared_len);
161     ret = 1;
162
163  err:
164     OPENSSL_cleanse(out, sizeof(out));
165     EVP_MD_CTX_free(mac);
166     EVP_CIPHER_CTX_free(ciph);
167     return ret;
168 }
169
170 int gost_kdftree2012_256(unsigned char *keyout, size_t keyout_len,
171                          const unsigned char *key, size_t keylen,
172                          const unsigned char *label, size_t label_len,
173                          const unsigned char *seed, size_t seed_len,
174                          const size_t representation)
175 {
176     int iters, i = 0;
177     unsigned char zero = 0;
178     unsigned char *ptr = keyout;
179     HMAC_CTX *ctx;
180     unsigned char *len_ptr = NULL;
181     uint32_t len_repr = htonl(keyout_len * 8);
182     size_t len_repr_len = 4;
183
184     ctx = HMAC_CTX_new();
185     if (ctx == NULL) {
186         GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_MALLOC_FAILURE);
187         return 0;
188     }
189
190     if ((keyout_len == 0) || (keyout_len % 32 != 0)) {
191         GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_INTERNAL_ERROR);
192         return 0;
193     }
194     iters = keyout_len / 32;
195
196     len_ptr = (unsigned char *)&len_repr;
197     while (*len_ptr == 0) {
198         len_ptr++;
199         len_repr_len--;
200     }
201
202     for (i = 1; i <= iters; i++) {
203         uint32_t iter_net = htonl(i);
204         unsigned char *rep_ptr =
205             ((unsigned char *)&iter_net) + (4 - representation);
206
207         if (HMAC_Init_ex(ctx, key, keylen,
208                          EVP_get_digestbynid(NID_id_GostR3411_2012_256),
209                          NULL) <= 0
210             || HMAC_Update(ctx, rep_ptr, representation) <= 0
211             || HMAC_Update(ctx, label, label_len) <= 0
212             || HMAC_Update(ctx, &zero, 1) <= 0
213             || HMAC_Update(ctx, seed, seed_len) <= 0
214             || HMAC_Update(ctx, len_ptr, len_repr_len) <= 0
215             || HMAC_Final(ctx, ptr, NULL) <= 0) {
216             GOSTerr(GOST_F_GOST_KDFTREE2012_256, ERR_R_INTERNAL_ERROR);
217             HMAC_CTX_free(ctx);
218             return 0;
219         }
220
221         HMAC_CTX_reset(ctx);
222         ptr += 32;
223     }
224
225     HMAC_CTX_free(ctx);
226
227     return 1;
228 }
229
230 int gost_tlstree(int cipher_nid, const unsigned char *in, unsigned char *out,
231                  const unsigned char *tlsseq)
232 {
233 #ifndef L_ENDIAN
234     uint64_t gh_c1 = 0xFFFFFFFF00000000, gh_c2 = 0xFFFFFFFFFFF80000,
235         gh_c3 = 0xFFFFFFFFFFFFFFC0;
236     uint64_t mg_c1 = 0xFFFFFFC000000000, mg_c2 = 0xFFFFFFFFFE000000,
237         mg_c3 = 0xFFFFFFFFFFFFF000;
238 #else
239     uint64_t gh_c1 = 0x00000000FFFFFFFF, gh_c2 = 0x0000F8FFFFFFFFFF,
240         gh_c3 = 0xC0FFFFFFFFFFFFFF;
241     uint64_t mg_c1 = 0x00000000C0FFFFFF, mg_c2 = 0x000000FEFFFFFFFF,
242         mg_c3 = 0x00F0FFFFFFFFFFFF;
243 #endif
244     uint64_t c1, c2, c3;
245     uint64_t seed1, seed2, seed3;
246     uint64_t seq;
247     unsigned char ko1[32], ko2[32];
248
249     switch (cipher_nid) {
250     case NID_magma_cbc:
251         c1 = mg_c1;
252         c2 = mg_c2;
253         c3 = mg_c3;
254         break;
255     case NID_grasshopper_cbc:
256         c1 = gh_c1;
257         c2 = gh_c2;
258         c3 = gh_c3;
259         break;
260     default:
261         return 0;
262     }
263     memcpy(&seq, tlsseq, 8);
264     seed1 = seq & c1;
265     seed2 = seq & c2;
266     seed3 = seq & c3;
267
268     if (gost_kdftree2012_256(ko1, 32, in, 32, (const unsigned char *)"level1", 6,
269                          (const unsigned char *)&seed1, 8, 1) <= 0
270                           || gost_kdftree2012_256(ko2, 32, ko1, 32, (const unsigned char *)"level2", 6,
271                          (const unsigned char *)&seed2, 8, 1) <= 0
272         || gost_kdftree2012_256(out, 32, ko2, 32, (const unsigned char *)"level3", 6,
273                          (const unsigned char *)&seed3, 8, 1) <= 0)
274                         return 0;
275
276     return 1;
277 }
278
279 #define GOST_WRAP_FLAGS  EVP_CIPH_CTRL_INIT | EVP_CIPH_WRAP_MODE | EVP_CIPH_CUSTOM_IV | EVP_CIPH_FLAG_CUSTOM_CIPHER | EVP_CIPH_FLAG_DEFAULT_ASN1
280
281 #define MAGMA_MAC_WRAP_LEN 8
282 #define KUZNYECHIK_MAC_WRAP_LEN 16
283 #define MAX_MAC_WRAP_LEN KUZNYECHIK_MAC_WRAP_LEN
284 #define GOSTKEYLEN 32
285 #define MAGMA_WRAPPED_KEY_LEN GOSTKEYLEN + MAGMA_MAC_WRAP_LEN
286 #define KUZNYECHIK_WRAPPED_KEY_LEN GOSTKEYLEN + KUZNYECHIK_MAC_WRAP_LEN
287 #define MAX_WRAPPED_KEY_LEN KUZNYECHIK_WRAPPED_KEY_LEN
288
289 typedef struct {
290         unsigned char iv[8];   /* Max IV size is half of base cipher block length */
291         unsigned char key[GOSTKEYLEN*2]; /* Combined cipher and mac keys */
292         unsigned char wrapped[MAX_WRAPPED_KEY_LEN]; /* Max size */
293         size_t wrap_count;
294 } GOST_WRAP_CTX;
295
296 static int magma_wrap_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
297         const unsigned char *iv, int enc)
298 {
299         GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
300         memset(cctx->wrapped, 0, MAX_WRAPPED_KEY_LEN);
301         cctx->wrap_count = 0;
302
303         if (iv) {
304                 memset(cctx->iv, 0, 8);
305                 memcpy(cctx->iv, iv, 4);
306         }
307
308         if (key) {
309                 memcpy(cctx->key, key, GOSTKEYLEN*2);
310         }
311         return 1;
312 }
313
314 static int magma_wrap_do(EVP_CIPHER_CTX *ctx, unsigned char *out,
315         const unsigned char *in, size_t inl)
316 {
317         GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
318         int enc = EVP_CIPHER_CTX_encrypting(ctx) ? 1 : 0;
319
320         if (out == NULL)
321                 return GOSTKEYLEN;
322
323         if (inl <= MAGMA_WRAPPED_KEY_LEN) {
324                 if (cctx->wrap_count + inl > MAGMA_WRAPPED_KEY_LEN)
325                         return -1;
326
327                 if (cctx->wrap_count + inl <= MAGMA_WRAPPED_KEY_LEN)
328                 {
329                         memcpy(cctx->wrapped+cctx->wrap_count, in, inl);
330                         cctx->wrap_count += inl;
331                 }
332         }
333
334         if (cctx->wrap_count < MAGMA_WRAPPED_KEY_LEN)
335                 return 0;
336
337         if (enc) {
338 #if 0
339                 return gost_kexp15(cctx->key, 32, NID_magma_ctr, in, NID_magma_mac,
340                         cctx->key, /* FIXME mac_key, */ cctx->iv, 4, out, &outl);
341 #endif
342                 return -1;
343         } else {
344                 return gost_kimp15(cctx->wrapped, cctx->wrap_count, NID_magma_ctr,
345                 cctx->key+GOSTKEYLEN, NID_magma_mac, cctx->key, cctx->iv, 4, out) > 0 ? GOSTKEYLEN : 0;
346         }
347         return 1;
348 }
349
350 static int kuznyechik_wrap_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
351         const unsigned char *iv, int enc)
352 {
353         GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
354         memset(cctx->wrapped, 0, KUZNYECHIK_WRAPPED_KEY_LEN);
355         cctx->wrap_count = 0;
356
357         if (iv) {
358                 memset(cctx->iv, 0, 8);
359                 memcpy(cctx->iv, iv, 8);
360         }
361
362         if (key) {
363                 memcpy(cctx->key, key, GOSTKEYLEN*2);
364         }
365         return 1;
366 }
367
368 static int kuznyechik_wrap_do(EVP_CIPHER_CTX *ctx, unsigned char *out,
369         const unsigned char *in, size_t inl)
370 {
371         GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
372         int enc = EVP_CIPHER_CTX_encrypting(ctx) ? 1 : 0;
373
374         if (out == NULL)
375                 return GOSTKEYLEN;
376
377         if (inl <= KUZNYECHIK_WRAPPED_KEY_LEN) {
378                 if (cctx->wrap_count + inl > KUZNYECHIK_WRAPPED_KEY_LEN)
379                         return -1;
380
381                 if (cctx->wrap_count + inl <= KUZNYECHIK_WRAPPED_KEY_LEN)
382                 {
383                         memcpy(cctx->wrapped+cctx->wrap_count, in, inl);
384                         cctx->wrap_count += inl;
385                 }
386         }
387
388         if (cctx->wrap_count < KUZNYECHIK_WRAPPED_KEY_LEN)
389                 return 0;
390
391         if (enc) {
392 #if 0
393                 return gost_kexp15(cctx->key, 32, NID_magma_ctr, in, NID_magma_mac,
394                         cctx->key, /* FIXME mac_key, */ cctx->iv, 4, out, &outl);
395 #endif
396                 return -1;
397         } else {
398                 return gost_kimp15(cctx->wrapped, cctx->wrap_count, NID_kuznyechik_ctr,
399                 cctx->key+GOSTKEYLEN, NID_kuznyechik_mac, cctx->key, cctx->iv, 8, out) > 0 ? GOSTKEYLEN : 0;
400         }
401 }
402
403 int wrap_ctrl (EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
404 {
405         switch(type)
406         {
407                 case EVP_CTRL_INIT:
408                         EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
409                         return 1;
410                 default:
411                         return -2;
412         }
413 }
414
415 static GOST_cipher wrap_template_cipher = {
416     .key_len = GOSTKEYLEN * 2,
417     .flags = GOST_WRAP_FLAGS,
418     .ctx_size = sizeof(GOST_WRAP_CTX),
419     .ctrl = wrap_ctrl,
420 };
421
422 GOST_cipher magma_kexp15_cipher = {
423     .template = &wrap_template_cipher,
424     .nid = NID_magma_kexp15,
425     .block_size = 8,
426     .iv_len = 4,
427     .init = magma_wrap_init,
428     .do_cipher = magma_wrap_do,
429 };
430
431 GOST_cipher kuznyechik_kexp15_cipher = {
432     .template = &wrap_template_cipher,
433     .nid = NID_kuznyechik_kexp15,
434     .block_size = 16,
435     .iv_len = 8,
436     .init = kuznyechik_wrap_init,
437     .do_cipher = kuznyechik_wrap_do,
438 };
439 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */