diff options
author | Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> | 2018-09-21 22:09:11 +0300 |
---|---|---|
committer | Dmitry Baryshkov <dbaryshkov@gmail.com> | 2020-06-07 00:58:59 +0300 |
commit | dcc719ba583237b5d87a5d21f1ce52bd036d5bd0 (patch) | |
tree | a9af62fd75d9e8733e6cbad594a231e2f6bc16d3 | |
parent | b8884d64c4ceb5dd493ace0d047c31cb49f08197 (diff) | |
download | gnutls-dcc719ba583237b5d87a5d21f1ce52bd036d5bd0.tar.gz |
nettle/gost: add Magma code
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
-rw-r--r-- | configure.ac | 6 | ||||
-rw-r--r-- | lib/nettle/Makefile.am | 3 | ||||
-rw-r--r-- | lib/nettle/gost/magma.c | 92 | ||||
-rw-r--r-- | lib/nettle/gost/magma.h | 78 |
4 files changed, 179 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac index d59553b6a1..82a04e9fe2 100644 --- a/configure.ac +++ b/configure.ac @@ -707,6 +707,12 @@ AC_CHECK_FUNCS(nettle_siv_cmac_aes128_set_key) LIBS=$save_LIBS AM_CONDITIONAL(NEED_SIV, [test "$ac_cv_func_nettle_siv_cmac_aes128_set_key" != "yes"]) +# Check for Magma +save_LIBS=$LIBS +LIBS="$LIBS $NETTLE_LIBS" +AC_CHECK_FUNCS(nettle_magma_set_key) +LIBS=$save_LIBS + # Check sonames of the linked libraries needed for FIPS selftests. save_LIBS=$LIBS LIBS="$LIBS $GMP_LIBS" diff --git a/lib/nettle/Makefile.am b/lib/nettle/Makefile.am index aae87e0902..a97303a491 100644 --- a/lib/nettle/Makefile.am +++ b/lib/nettle/Makefile.am @@ -88,6 +88,9 @@ libcrypto_la_SOURCES += \ gost/gostdsa-mask.c gost/gostdsa2.h libcrypto_la_SOURCES += gost_keywrap.c + +libcrypto_la_SOURCES += \ + gost/magma.c gost/magma.h endif if NEED_INT_ECC diff --git a/lib/nettle/gost/magma.c b/lib/nettle/gost/magma.c new file mode 100644 index 0000000000..60d3e44a8b --- /dev/null +++ b/lib/nettle/gost/magma.c @@ -0,0 +1,92 @@ +/* magma.c - GOST R 34.12-2015 (Magma) cipher implementation + * + * Copyright: 2017 Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY + * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#ifndef HAVE_NETTLE_MAGMA_SET_KEY + +#include <assert.h> + +#include <nettle/macros.h> +#include "nettle-write.h" +#include "magma.h" +#ifndef HAVE_NETTLE_GOST28147_SET_KEY +#include "gost28147.h" +#else +#include <nettle/gost28147.h> +#endif + +void +magma_set_key(struct magma_ctx *ctx, const uint8_t *key) +{ + unsigned i; + + for (i = 0; i < 8; i++, key += 4) + ctx->key[i] = READ_UINT32(key); +} + +void +magma_encrypt(const struct magma_ctx *ctx, + size_t length, uint8_t *dst, + const uint8_t *src) +{ + uint32_t block[2]; + + assert(!(length % MAGMA_BLOCK_SIZE)); + + while (length) + { + block[1] = READ_UINT32(src); src += 4; + block[0] = READ_UINT32(src); src += 4; + gost28147_encrypt_simple(ctx->key, gost28147_param_TC26_Z.sbox, + block, block); + WRITE_UINT32(dst, block[1]); dst += 4; + WRITE_UINT32(dst, block[0]); dst += 4; + length -= MAGMA_BLOCK_SIZE; + } +} + +void +magma_decrypt(const struct magma_ctx *ctx, + size_t length, uint8_t *dst, + const uint8_t *src) +{ + uint32_t block[2]; + + assert(!(length % MAGMA_BLOCK_SIZE)); + + while (length) + { + block[1] = READ_UINT32(src); src += 4; + block[0] = READ_UINT32(src); src += 4; + gost28147_decrypt_simple(ctx->key, gost28147_param_TC26_Z.sbox, + block, block); + WRITE_UINT32(dst, block[1]); dst += 4; + WRITE_UINT32(dst, block[0]); dst += 4; + length -= MAGMA_BLOCK_SIZE; + } +} +#endif /* HAVE_NETTLE_MAGMA_SET_KEY */ diff --git a/lib/nettle/gost/magma.h b/lib/nettle/gost/magma.h new file mode 100644 index 0000000000..31c6b262da --- /dev/null +++ b/lib/nettle/gost/magma.h @@ -0,0 +1,78 @@ +/* magma.h + + The GOST R 34.12-2015 (MAGMA) cipher function. + + Copyright (C) 2017 Dmitry Eremin-Solenikov + + This file is part of GNU Nettle. + + GNU Nettle is free software: you can redistribute it and/or + modify it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + + or + + * the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at your + option) any later version. + + or both in parallel, as here. + + GNU Nettle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received copies of the GNU General Public License and + the GNU Lesser General Public License along with this program. If + not, see http://www.gnu.org/licenses/. +*/ + +#ifndef GNUTLS_LIB_NETTLE_MAGMA_H_INCLUDED +#define GNUTLS_LIB_NETTLE_MAGMA_H_INCLUDED + +#include "config.h" + +#ifndef HAVE_NETTLE_MAGMA_SET_KEY + +#include <nettle/nettle-types.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#define magma_set_key _gnutls_magma_set_key +#define magma_set_param _gnutls_magma_set_param +#define magma_encrypt _gnutls_magma_encrypt +#define magma_decrypt _gnutls_magma_decrypt + +#define MAGMA_KEY_SIZE 32 +#define MAGMA_BLOCK_SIZE 8 + +struct magma_ctx +{ + uint32_t key[MAGMA_KEY_SIZE/4]; +}; + +void +magma_set_key(struct magma_ctx *ctx, const uint8_t *key); + +void +magma_encrypt(const struct magma_ctx *ctx, + size_t length, uint8_t *dst, + const uint8_t *src); +void +magma_decrypt(const struct magma_ctx *ctx, + size_t length, uint8_t *dst, + const uint8_t *src); + +#ifdef __cplusplus +} +#endif + +#endif + +#endif /* GNUTLS_LIB_NETTLE_MAGMA_H_INCLUDED */ |