]> git.ipfire.org Git - thirdparty/gnutls.git/commitdiff
nettle/gost: add Magma code
authorDmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Fri, 21 Sep 2018 19:09:11 +0000 (22:09 +0300)
committerDmitry Baryshkov <dbaryshkov@gmail.com>
Sat, 6 Jun 2020 21:58:59 +0000 (00:58 +0300)
Signed-off-by: Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
configure.ac
lib/nettle/Makefile.am
lib/nettle/gost/magma.c [new file with mode: 0644]
lib/nettle/gost/magma.h [new file with mode: 0644]

index d59553b6a180ed066aabe7654d8f541e23c1025c..82a04e9fe2e837cb54b28a561330e8d143f07eba 100644 (file)
@@ -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"
index aae87e09023a2ff4881c7e0eaac5293f203a806e..a97303a4913b65bcf0b001fb256be91336917335 100644 (file)
@@ -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 (file)
index 0000000..60d3e44
--- /dev/null
@@ -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 (file)
index 0000000..31c6b26
--- /dev/null
@@ -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 */