/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 magma_encrypt(const struct magma_ctx *ctx, size_t length, uint8_t *dst, |
51 | | const uint8_t *src) |
52 | 0 | { |
53 | 0 | uint32_t block[2]; |
54 | |
|
55 | 0 | assert(!(length % MAGMA_BLOCK_SIZE)); |
56 | | |
57 | 0 | while (length) { |
58 | 0 | block[1] = READ_UINT32(src); |
59 | 0 | src += 4; |
60 | 0 | block[0] = READ_UINT32(src); |
61 | 0 | src += 4; |
62 | 0 | gost28147_encrypt_simple(ctx->key, gost28147_param_TC26_Z.sbox, |
63 | 0 | block, block); |
64 | 0 | WRITE_UINT32(dst, block[1]); |
65 | 0 | dst += 4; |
66 | 0 | WRITE_UINT32(dst, block[0]); |
67 | 0 | dst += 4; |
68 | 0 | length -= MAGMA_BLOCK_SIZE; |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | | void magma_decrypt(const struct magma_ctx *ctx, size_t length, uint8_t *dst, |
73 | | const uint8_t *src) |
74 | 0 | { |
75 | 0 | uint32_t block[2]; |
76 | |
|
77 | 0 | assert(!(length % MAGMA_BLOCK_SIZE)); |
78 | | |
79 | 0 | while (length) { |
80 | 0 | block[1] = READ_UINT32(src); |
81 | 0 | src += 4; |
82 | 0 | block[0] = READ_UINT32(src); |
83 | 0 | src += 4; |
84 | 0 | gost28147_decrypt_simple(ctx->key, gost28147_param_TC26_Z.sbox, |
85 | 0 | block, block); |
86 | 0 | WRITE_UINT32(dst, block[1]); |
87 | 0 | dst += 4; |
88 | 0 | WRITE_UINT32(dst, block[0]); |
89 | 0 | dst += 4; |
90 | 0 | length -= MAGMA_BLOCK_SIZE; |
91 | 0 | } |
92 | 0 | } |
93 | | #endif /* HAVE_NETTLE_MAGMA_SET_KEY */ |