]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_keyexpimp.c
Merge pull request #455 from arx11/magma_ctracpkm_omac
[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
9 #include <string.h>
10 #include <openssl/evp.h>
11 #include <openssl/hmac.h>
12 #include <openssl/buffer.h>
13
14 #include "gost_lcl.h"
15 #include "e_gost_err.h"
16
17 static uint32_t be32(uint32_t host)
18 {
19 #ifdef L_ENDIAN
20     return (host & 0xff000000) >> 24 |
21            (host & 0x00ff0000) >> 8  |
22            (host & 0x0000ff00) << 8  |
23            (host & 0x000000ff) << 24;
24 #else
25     return host;
26 #endif
27 }
28
29 int omac_imit_ctrl(EVP_MD_CTX *ctx, int type, int arg, void *ptr);
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 = be32(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 = be32(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)
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     int ret;
273
274     switch (cipher_nid) {
275     case NID_magma_cbc:
276         c1 = mg_c1;
277         c2 = mg_c2;
278         c3 = mg_c3;
279         break;
280     case NID_grasshopper_cbc:
281         c1 = gh_c1;
282         c2 = gh_c2;
283         c3 = gh_c3;
284         break;
285     default:
286         return 0;
287     }
288 #ifndef L_ENDIAN
289     BUF_reverse((unsigned char *)&seq, tlsseq, 8);
290 #else
291     memcpy(&seq, tlsseq, 8);
292 #endif
293     seed1 = seq & c1;
294     seed2 = seq & c2;
295     seed3 = seq & c3;
296
297     ret = !(gost_kdftree2012_256(ko1, 32, in, 32, (const unsigned char *)"level1", 6,
298                          (const unsigned char *)&seed1, 8, 1) <= 0
299                           || gost_kdftree2012_256(ko2, 32, ko1, 32, (const unsigned char *)"level2", 6,
300                          (const unsigned char *)&seed2, 8, 1) <= 0
301         || gost_kdftree2012_256(out, 32, ko2, 32, (const unsigned char *)"level3", 6,
302                          (const unsigned char *)&seed3, 8, 1) <= 0);
303
304     OPENSSL_cleanse(ko1, sizeof(ko1));
305     OPENSSL_cleanse(ko2, sizeof(ko2));
306     return ret;
307 }
308
309 #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
310
311 #define MAGMA_MAC_WRAP_LEN 8
312 #define KUZNYECHIK_MAC_WRAP_LEN 16
313 #define MAX_MAC_WRAP_LEN KUZNYECHIK_MAC_WRAP_LEN
314 #define GOSTKEYLEN 32
315 #define MAGMA_WRAPPED_KEY_LEN GOSTKEYLEN + MAGMA_MAC_WRAP_LEN
316 #define KUZNYECHIK_WRAPPED_KEY_LEN GOSTKEYLEN + KUZNYECHIK_MAC_WRAP_LEN
317 #define MAX_WRAPPED_KEY_LEN KUZNYECHIK_WRAPPED_KEY_LEN
318
319 typedef struct {
320         unsigned char iv[8];   /* Max IV size is half of base cipher block length */
321         unsigned char key[GOSTKEYLEN*2]; /* Combined cipher and mac keys */
322         unsigned char wrapped[MAX_WRAPPED_KEY_LEN]; /* Max size */
323         size_t wrap_count;
324 } GOST_WRAP_CTX;
325
326 static int magma_wrap_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
327         const unsigned char *iv, int enc)
328 {
329         GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
330         memset(cctx->wrapped, 0, MAX_WRAPPED_KEY_LEN);
331         cctx->wrap_count = 0;
332
333         if (iv) {
334                 memset(cctx->iv, 0, 8);
335                 memcpy(cctx->iv, iv, 4);
336         }
337
338         if (key) {
339                 memcpy(cctx->key, key, GOSTKEYLEN*2);
340         }
341         return 1;
342 }
343
344 static int magma_wrap_do(EVP_CIPHER_CTX *ctx, unsigned char *out,
345         const unsigned char *in, size_t inl)
346 {
347         GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
348         int enc = EVP_CIPHER_CTX_encrypting(ctx) ? 1 : 0;
349
350         if (out == NULL)
351                 return GOSTKEYLEN;
352
353         if (inl <= MAGMA_WRAPPED_KEY_LEN) {
354                 if (cctx->wrap_count + inl > MAGMA_WRAPPED_KEY_LEN)
355                         return -1;
356
357                 if (cctx->wrap_count + inl <= MAGMA_WRAPPED_KEY_LEN)
358                 {
359                         memcpy(cctx->wrapped+cctx->wrap_count, in, inl);
360                         cctx->wrap_count += inl;
361                 }
362         }
363
364         if (cctx->wrap_count < MAGMA_WRAPPED_KEY_LEN)
365                 return 0;
366
367         if (enc) {
368 #if 0
369                 return gost_kexp15(cctx->key, 32, NID_magma_ctr, in, NID_magma_mac,
370                         cctx->key, /* FIXME mac_key, */ cctx->iv, 4, out, &outl);
371 #endif
372                 return -1;
373         } else {
374                 return gost_kimp15(cctx->wrapped, cctx->wrap_count, NID_magma_ctr,
375                 cctx->key+GOSTKEYLEN, NID_magma_mac, cctx->key, cctx->iv, 4, out) > 0 ? GOSTKEYLEN : 0;
376         }
377 }
378
379 static int kuznyechik_wrap_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
380         const unsigned char *iv, int enc)
381 {
382         GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
383         memset(cctx->wrapped, 0, KUZNYECHIK_WRAPPED_KEY_LEN);
384         cctx->wrap_count = 0;
385
386         if (iv) {
387                 memset(cctx->iv, 0, 8);
388                 memcpy(cctx->iv, iv, 8);
389         }
390
391         if (key) {
392                 memcpy(cctx->key, key, GOSTKEYLEN*2);
393         }
394         return 1;
395 }
396
397 static int kuznyechik_wrap_do(EVP_CIPHER_CTX *ctx, unsigned char *out,
398         const unsigned char *in, size_t inl)
399 {
400         GOST_WRAP_CTX *cctx = EVP_CIPHER_CTX_get_cipher_data(ctx);
401         int enc = EVP_CIPHER_CTX_encrypting(ctx) ? 1 : 0;
402
403         if (out == NULL)
404                 return GOSTKEYLEN;
405
406         if (inl <= KUZNYECHIK_WRAPPED_KEY_LEN) {
407                 if (cctx->wrap_count + inl > KUZNYECHIK_WRAPPED_KEY_LEN)
408                         return -1;
409
410                 if (cctx->wrap_count + inl <= KUZNYECHIK_WRAPPED_KEY_LEN)
411                 {
412                         memcpy(cctx->wrapped+cctx->wrap_count, in, inl);
413                         cctx->wrap_count += inl;
414                 }
415         }
416
417         if (cctx->wrap_count < KUZNYECHIK_WRAPPED_KEY_LEN)
418                 return 0;
419
420         if (enc) {
421 #if 0
422                 return gost_kexp15(cctx->key, 32, NID_magma_ctr, in, NID_magma_mac,
423                         cctx->key, /* FIXME mac_key, */ cctx->iv, 4, out, &outl);
424 #endif
425                 return -1;
426         } else {
427                 return gost_kimp15(cctx->wrapped, cctx->wrap_count, NID_kuznyechik_ctr,
428                 cctx->key+GOSTKEYLEN, NID_kuznyechik_mac, cctx->key, cctx->iv, 8, out) > 0 ? GOSTKEYLEN : 0;
429         }
430 }
431
432 static int wrap_ctrl (EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
433 {
434         switch(type)
435         {
436                 case EVP_CTRL_INIT:
437                         EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
438                         return 1;
439                 default:
440                         return -2;
441         }
442 }
443
444 static GOST_cipher wrap_template_cipher = {
445     .key_len = GOSTKEYLEN * 2,
446     .flags = GOST_WRAP_FLAGS,
447     .ctx_size = sizeof(GOST_WRAP_CTX),
448     .ctrl = wrap_ctrl,
449 };
450
451 GOST_cipher magma_kexp15_cipher = {
452     .template = &wrap_template_cipher,
453     .nid = NID_magma_kexp15,
454     .block_size = 8,
455     .iv_len = 4,
456     .init = magma_wrap_init,
457     .do_cipher = magma_wrap_do,
458 };
459
460 GOST_cipher kuznyechik_kexp15_cipher = {
461     .template = &wrap_template_cipher,
462     .nid = NID_kuznyechik_kexp15,
463     .block_size = 16,
464     .iv_len = 8,
465     .init = kuznyechik_wrap_init,
466     .do_cipher = kuznyechik_wrap_do,
467 };
468 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */