]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_keyexpimp.c
Delete .travis.yml
[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             break;
312         default:
313             return 0;
314         }
315         break;
316     default:
317         return 0;
318     }
319 #ifndef L_ENDIAN
320     BUF_reverse((unsigned char *)&seq, tlsseq, 8);
321 #else
322     memcpy(&seq, tlsseq, 8);
323 #endif
324     seed1 = seq & c1;
325     seed2 = seq & c2;
326     seed3 = seq & c3;
327
328     if (gost_kdftree2012_256(ko1, 32, in, 32, (const unsigned char *)"level1", 6,
329                          (const unsigned char *)&seed1, 8, 1) <= 0
330                           || gost_kdftree2012_256(ko2, 32, ko1, 32, (const unsigned char *)"level2", 6,
331                          (const unsigned char *)&seed2, 8, 1) <= 0
332         || gost_kdftree2012_256(out, 32, ko2, 32, (const unsigned char *)"level3", 6,
333                          (const unsigned char *)&seed3, 8, 1) <= 0)
334                         return 0;
335
336     return 1;
337 }
338
339 #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
340
341 #define MAGMA_MAC_WRAP_LEN 8
342 #define KUZNYECHIK_MAC_WRAP_LEN 16
343 #define MAX_MAC_WRAP_LEN KUZNYECHIK_MAC_WRAP_LEN
344 #define GOSTKEYLEN 32
345 #define MAGMA_WRAPPED_KEY_LEN GOSTKEYLEN + MAGMA_MAC_WRAP_LEN
346 #define KUZNYECHIK_WRAPPED_KEY_LEN GOSTKEYLEN + KUZNYECHIK_MAC_WRAP_LEN
347 #define MAX_WRAPPED_KEY_LEN KUZNYECHIK_WRAPPED_KEY_LEN
348
349 typedef struct {
350         unsigned char iv[8];   /* Max IV size is half of base cipher block length */
351         unsigned char key[GOSTKEYLEN*2]; /* Combined cipher and mac keys */
352         unsigned char wrapped[MAX_WRAPPED_KEY_LEN]; /* Max size */
353         size_t wrap_count;
354 } GOST_WRAP_CTX;
355
356 static int magma_wrap_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
357         const unsigned char *iv, int enc)
358 {
359         GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
360         memset(cctx->wrapped, 0, MAX_WRAPPED_KEY_LEN);
361         cctx->wrap_count = 0;
362
363         if (iv) {
364                 memset(cctx->iv, 0, 8);
365                 memcpy(cctx->iv, iv, 4);
366         }
367
368         if (key) {
369                 memcpy(cctx->key, key, GOSTKEYLEN*2);
370         }
371         return 1;
372 }
373
374 static int magma_wrap_do(EVP_CIPHER_CTX *ctx, unsigned char *out,
375         const unsigned char *in, size_t inl)
376 {
377         GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
378         int enc = EVP_CIPHER_CTX_encrypting(ctx) ? 1 : 0;
379
380         if (out == NULL)
381                 return GOSTKEYLEN;
382
383         if (inl <= MAGMA_WRAPPED_KEY_LEN) {
384                 if (cctx->wrap_count + inl > MAGMA_WRAPPED_KEY_LEN)
385                         return -1;
386
387                 if (cctx->wrap_count + inl <= MAGMA_WRAPPED_KEY_LEN)
388                 {
389                         memcpy(cctx->wrapped+cctx->wrap_count, in, inl);
390                         cctx->wrap_count += inl;
391                 }
392         }
393
394         if (cctx->wrap_count < MAGMA_WRAPPED_KEY_LEN)
395                 return 0;
396
397         if (enc) {
398 #if 0
399                 return gost_kexp15(cctx->key, 32, NID_magma_ctr, in, NID_magma_mac,
400                         cctx->key, /* FIXME mac_key, */ cctx->iv, 4, out, &outl);
401 #endif
402                 return -1;
403         } else {
404                 return gost_kimp15(cctx->wrapped, cctx->wrap_count, NID_magma_ctr,
405                 cctx->key+GOSTKEYLEN, NID_magma_mac, cctx->key, cctx->iv, 4, out) > 0 ? GOSTKEYLEN : 0;
406         }
407         return 1;
408 }
409
410 static int kuznyechik_wrap_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
411         const unsigned char *iv, int enc)
412 {
413         GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
414         memset(cctx->wrapped, 0, KUZNYECHIK_WRAPPED_KEY_LEN);
415         cctx->wrap_count = 0;
416
417         if (iv) {
418                 memset(cctx->iv, 0, 8);
419                 memcpy(cctx->iv, iv, 8);
420         }
421
422         if (key) {
423                 memcpy(cctx->key, key, GOSTKEYLEN*2);
424         }
425         return 1;
426 }
427
428 static int kuznyechik_wrap_do(EVP_CIPHER_CTX *ctx, unsigned char *out,
429         const unsigned char *in, size_t inl)
430 {
431         GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
432         int enc = EVP_CIPHER_CTX_encrypting(ctx) ? 1 : 0;
433
434         if (out == NULL)
435                 return GOSTKEYLEN;
436
437         if (inl <= KUZNYECHIK_WRAPPED_KEY_LEN) {
438                 if (cctx->wrap_count + inl > KUZNYECHIK_WRAPPED_KEY_LEN)
439                         return -1;
440
441                 if (cctx->wrap_count + inl <= KUZNYECHIK_WRAPPED_KEY_LEN)
442                 {
443                         memcpy(cctx->wrapped+cctx->wrap_count, in, inl);
444                         cctx->wrap_count += inl;
445                 }
446         }
447
448         if (cctx->wrap_count < KUZNYECHIK_WRAPPED_KEY_LEN)
449                 return 0;
450
451         if (enc) {
452 #if 0
453                 return gost_kexp15(cctx->key, 32, NID_magma_ctr, in, NID_magma_mac,
454                         cctx->key, /* FIXME mac_key, */ cctx->iv, 4, out, &outl);
455 #endif
456                 return -1;
457         } else {
458                 return gost_kimp15(cctx->wrapped, cctx->wrap_count, NID_kuznyechik_ctr,
459                 cctx->key+GOSTKEYLEN, NID_kuznyechik_mac, cctx->key, cctx->iv, 8, out) > 0 ? GOSTKEYLEN : 0;
460         }
461 }
462
463 static int wrap_ctrl (EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
464 {
465         switch(type)
466         {
467                 case EVP_CTRL_INIT:
468                         EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
469                         return 1;
470                 default:
471                         return -2;
472         }
473 }
474
475 static GOST_cipher wrap_template_cipher = {
476     .key_len = GOSTKEYLEN * 2,
477     .flags = GOST_WRAP_FLAGS,
478     .ctx_size = sizeof(GOST_WRAP_CTX),
479     .ctrl = wrap_ctrl,
480 };
481
482 GOST_cipher magma_kexp15_cipher = {
483     .template = &wrap_template_cipher,
484     .nid = NID_magma_kexp15,
485     .block_size = 8,
486     .iv_len = 4,
487     .init = magma_wrap_init,
488     .do_cipher = magma_wrap_do,
489 };
490
491 GOST_cipher kuznyechik_kexp15_cipher = {
492     .template = &wrap_template_cipher,
493     .nid = NID_kuznyechik_kexp15,
494     .block_size = 16,
495     .iv_len = 8,
496     .init = kuznyechik_wrap_init,
497     .do_cipher = kuznyechik_wrap_do,
498 };
499 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */