Coverage Report

Created: 2023-03-26 08:33

/src/gnutls/lib/nettle/gost/magma.c
Line
Count
Source (jump to first uncovered line)
1
/* magma.c - GOST R 34.12-2015 (Magma) cipher implementation
2
 *
3
 * Copyright: 2017 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
4
 *
5
 * Permission is hereby granted, free of charge, to any person obtaining a
6
 * copy of this software and associated documentation files (the
7
 * "Software"), to deal in the Software without restriction, including
8
 * without limitation the rights to use, copy, modify, merge, publish,
9
 * distribute, sublicense, and/or sell copies of the Software, and to
10
 * permit persons to whom the Software is furnished to do so, subject to
11
 * the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included
14
 * in all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
 */
24
25
#if HAVE_CONFIG_H
26
# include "config.h"
27
#endif
28
29
#ifndef HAVE_NETTLE_MAGMA_SET_KEY
30
31
# include <assert.h>
32
33
# include <nettle/macros.h>
34
# include "nettle-write.h"
35
# include "magma.h"
36
# ifndef HAVE_NETTLE_GOST28147_SET_KEY
37
#  include "gost28147.h"
38
# else
39
#  include <nettle/gost28147.h>
40
# endif
41
42
void magma_set_key(struct magma_ctx *ctx, const uint8_t * key)
43
0
{
44
0
  unsigned i;
45
46
0
  for (i = 0; i < 8; i++, key += 4)
47
0
    ctx->key[i] = READ_UINT32(key);
48
0
}
49
50
void
51
magma_encrypt(const struct magma_ctx *ctx,
52
        size_t length, uint8_t * dst, const uint8_t * src)
53
0
{
54
0
  uint32_t block[2];
55
56
0
  assert(!(length % MAGMA_BLOCK_SIZE));
57
58
0
  while (length) {
59
0
    block[1] = READ_UINT32(src);
60
0
    src += 4;
61
0
    block[0] = READ_UINT32(src);
62
0
    src += 4;
63
0
    gost28147_encrypt_simple(ctx->key, gost28147_param_TC26_Z.sbox,
64
0
           block, block);
65
0
    WRITE_UINT32(dst, block[1]);
66
0
    dst += 4;
67
0
    WRITE_UINT32(dst, block[0]);
68
0
    dst += 4;
69
0
    length -= MAGMA_BLOCK_SIZE;
70
0
  }
71
0
}
72
73
void
74
magma_decrypt(const struct magma_ctx *ctx,
75
        size_t length, uint8_t * dst, const uint8_t * src)
76
0
{
77
0
  uint32_t block[2];
78
79
0
  assert(!(length % MAGMA_BLOCK_SIZE));
80
81
0
  while (length) {
82
0
    block[1] = READ_UINT32(src);
83
0
    src += 4;
84
0
    block[0] = READ_UINT32(src);
85
0
    src += 4;
86
0
    gost28147_decrypt_simple(ctx->key, gost28147_param_TC26_Z.sbox,
87
0
           block, block);
88
0
    WRITE_UINT32(dst, block[1]);
89
0
    dst += 4;
90
0
    WRITE_UINT32(dst, block[0]);
91
0
    dst += 4;
92
0
    length -= MAGMA_BLOCK_SIZE;
93
0
  }
94
0
}
95
#endif        /* HAVE_NETTLE_MAGMA_SET_KEY */