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