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