[PATCH 4/4] rijndael: split Padlock part to separate file

Jussi Kivilinna jussi.kivilinna at iki.fi
Mon Dec 1 20:12:57 CET 2014


* cipher/Makefile.am: Add 'rijndael-padlock.c'.
* cipher/rijndael-padlock.c: New.
* cipher/rijndael.c (do_padlock, do_padlock_encrypt)
(do_padlock_decrypt): Move to 'rijndael-padlock.c'.
* configure.ac [mpi_cpu_arch=x86]: Add 'rijndael-padlock.lo'.
--

Signed-off-by: Jussi Kivilinna <jussi.kivilinna at iki.fi>
---
 cipher/Makefile.am        |    2 -
 cipher/rijndael-padlock.c |  102 +++++++++++++++++++++++++++++++++++++++++++++
 cipher/rijndael.c         |   86 ++++----------------------------------
 configure.ac              |    3 +
 4 files changed, 114 insertions(+), 79 deletions(-)
 create mode 100644 cipher/rijndael-padlock.c

diff --git a/cipher/Makefile.am b/cipher/Makefile.am
index 19b3692..d7e7773 100644
--- a/cipher/Makefile.am
+++ b/cipher/Makefile.am
@@ -75,7 +75,7 @@ md4.c \
 md5.c \
 poly1305-sse2-amd64.S poly1305-avx2-amd64.S poly1305-armv7-neon.S \
 rijndael.c rijndael-internal.h rijndael-tables.h rijndael-aesni.c \
-  rijndael-amd64.S rijndael-arm.S \
+  rijndael-padlock.c rijndael-amd64.S rijndael-arm.S \
 rmd160.c \
 rsa.c \
 salsa20.c salsa20-amd64.S salsa20-armv7-neon.S \
diff --git a/cipher/rijndael-padlock.c b/cipher/rijndael-padlock.c
new file mode 100644
index 0000000..476772a
--- /dev/null
+++ b/cipher/rijndael-padlock.c
@@ -0,0 +1,102 @@
+/* Padlock accelerated AES for Libgcrypt
+ * Copyright (C) 2000, 2001, 2002, 2003, 2007,
+ *               2008, 2011, 2012 Free Software Foundation, Inc.
+ *
+ * This file is part of Libgcrypt.
+ *
+ * Libgcrypt is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * Libgcrypt 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 Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <config.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h> /* for memcmp() */
+
+#include "types.h"  /* for byte and u32 typedefs */
+#include "g10lib.h"
+#include "cipher.h"
+#include "bufhelp.h"
+#include "cipher-selftest.h"
+#include "rijndael-internal.h"
+
+#ifdef USE_PADLOCK
+
+/* Encrypt or decrypt one block using the padlock engine.  A and B may
+   be the same. */
+static unsigned int
+do_padlock (const RIJNDAEL_context *ctx, unsigned char *bx,
+            const unsigned char *ax, int decrypt_flag)
+{
+  /* BX and AX are not necessary correctly aligned.  Thus we need to
+     copy them here. */
+  unsigned char a[16] __attribute__ ((aligned (16)));
+  unsigned char b[16] __attribute__ ((aligned (16)));
+  unsigned int cword[4] __attribute__ ((aligned (16)));
+  int blocks;
+
+  /* The control word fields are:
+      127:12   11:10 9     8     7     6     5     4     3:0
+      RESERVED KSIZE CRYPT INTER KEYGN CIPHR ALIGN DGEST ROUND  */
+  cword[0] = (ctx->rounds & 15);  /* (The mask is just a safeguard.)  */
+  cword[1] = 0;
+  cword[2] = 0;
+  cword[3] = 0;
+  if (decrypt_flag)
+    cword[0] |= 0x00000200;
+
+  memcpy (a, ax, 16);
+
+  blocks = 1; /* Init counter for just one block.  */
+#ifdef __x86_64__
+  asm volatile
+    ("pushfq\n\t"          /* Force key reload.  */
+     "popfq\n\t"
+     ".byte 0xf3, 0x0f, 0xa7, 0xc8\n\t" /* REP XCRYPT ECB. */
+     : /* No output */
+     : "S" (a), "D" (b), "d" (cword), "b" (ctx->padlockkey), "c" (blocks)
+     : "cc", "memory"
+     );
+#else
+  asm volatile
+    ("pushfl\n\t"          /* Force key reload.  */
+     "popfl\n\t"
+     "xchg %3, %%ebx\n\t"  /* Load key.  */
+     ".byte 0xf3, 0x0f, 0xa7, 0xc8\n\t" /* REP XCRYPT ECB. */
+     "xchg %3, %%ebx\n"    /* Restore GOT register.  */
+     : /* No output */
+     : "S" (a), "D" (b), "d" (cword), "r" (ctx->padlockkey), "c" (blocks)
+     : "cc", "memory"
+     );
+#endif
+
+  memcpy (bx, b, 16);
+
+  return (48 + 15 /* possible padding for alignment */);
+}
+
+unsigned int
+_gcry_aes_padlock_encrypt (const RIJNDAEL_context *ctx,
+                           unsigned char *bx, const unsigned char *ax)
+{
+  return do_padlock(ctx, bx, ax, 0);
+}
+
+unsigned int
+_gcry_aes_padlock_decrypt (const RIJNDAEL_context *ctx,
+                           unsigned char *bx, const unsigned char *ax)
+{
+  return do_padlock(ctx, bx, ax, 1);
+}
+
+#endif /* USE_PADLOCK */
diff --git a/cipher/rijndael.c b/cipher/rijndael.c
index f45171a..9bdb27b 100644
--- a/cipher/rijndael.c
+++ b/cipher/rijndael.c
@@ -106,12 +106,12 @@ extern void _gcry_aes_aesni_cbc_dec (RIJNDAEL_context *ctx,
 #endif
 
 #ifdef USE_PADLOCK
-static unsigned int do_padlock_encrypt (const RIJNDAEL_context *ctx,
-                                        unsigned char *bx,
-                                        const unsigned char *ax);
-static unsigned int do_padlock_decrypt (const RIJNDAEL_context *ctx,
-                                        unsigned char *bx,
-                                        const unsigned char *ax);
+extern unsigned int _gcry_aes_padlock_encrypt (const RIJNDAEL_context *ctx,
+                                               unsigned char *bx,
+                                               const unsigned char *ax);
+extern unsigned int _gcry_aes_padlock_decrypt (const RIJNDAEL_context *ctx,
+                                               unsigned char *bx,
+                                               const unsigned char *ax);
 #endif
 
 #ifdef USE_ARM_ASM
@@ -222,8 +222,8 @@ do_setkey (RIJNDAEL_context *ctx, const byte *key, const unsigned keylen)
 #ifdef USE_PADLOCK
   else if (hwfeatures & HWF_PADLOCK_AES && keylen == 128/8)
     {
-      ctx->encrypt_fn = do_padlock_encrypt;
-      ctx->decrypt_fn = do_padlock_decrypt;
+      ctx->encrypt_fn = _gcry_aes_padlock_encrypt;
+      ctx->decrypt_fn = _gcry_aes_padlock_decrypt;
       ctx->use_padlock = 1;
       memcpy (ctx->padlockkey, key, keylen);
     }
@@ -532,76 +532,6 @@ do_encrypt (const RIJNDAEL_context *ctx,
 }
 
 
-/* Encrypt or decrypt one block using the padlock engine.  A and B may
-   be the same. */
-#ifdef USE_PADLOCK
-static unsigned int
-do_padlock (const RIJNDAEL_context *ctx, unsigned char *bx,
-            const unsigned char *ax, int decrypt_flag)
-{
-  /* BX and AX are not necessary correctly aligned.  Thus we need to
-     copy them here. */
-  unsigned char a[16] __attribute__ ((aligned (16)));
-  unsigned char b[16] __attribute__ ((aligned (16)));
-  unsigned int cword[4] __attribute__ ((aligned (16)));
-  int blocks;
-
-  /* The control word fields are:
-      127:12   11:10 9     8     7     6     5     4     3:0
-      RESERVED KSIZE CRYPT INTER KEYGN CIPHR ALIGN DGEST ROUND  */
-  cword[0] = (ctx->rounds & 15);  /* (The mask is just a safeguard.)  */
-  cword[1] = 0;
-  cword[2] = 0;
-  cword[3] = 0;
-  if (decrypt_flag)
-    cword[0] |= 0x00000200;
-
-  memcpy (a, ax, 16);
-
-  blocks = 1; /* Init counter for just one block.  */
-#ifdef __x86_64__
-  asm volatile
-    ("pushfq\n\t"          /* Force key reload.  */
-     "popfq\n\t"
-     ".byte 0xf3, 0x0f, 0xa7, 0xc8\n\t" /* REP XCRYPT ECB. */
-     : /* No output */
-     : "S" (a), "D" (b), "d" (cword), "b" (ctx->padlockkey), "c" (blocks)
-     : "cc", "memory"
-     );
-#else
-  asm volatile
-    ("pushfl\n\t"          /* Force key reload.  */
-     "popfl\n\t"
-     "xchg %3, %%ebx\n\t"  /* Load key.  */
-     ".byte 0xf3, 0x0f, 0xa7, 0xc8\n\t" /* REP XCRYPT ECB. */
-     "xchg %3, %%ebx\n"    /* Restore GOT register.  */
-     : /* No output */
-     : "S" (a), "D" (b), "d" (cword), "r" (ctx->padlockkey), "c" (blocks)
-     : "cc", "memory"
-     );
-#endif
-
-  memcpy (bx, b, 16);
-
-  return (48 + 15 /* possible padding for alignment */);
-}
-
-static unsigned int
-do_padlock_encrypt (const RIJNDAEL_context *ctx,
-                    unsigned char *bx, const unsigned char *ax)
-{
-  return do_padlock(ctx, bx, ax, 0);
-}
-
-static unsigned int
-do_padlock_decrypt (const RIJNDAEL_context *ctx,
-                    unsigned char *bx, const unsigned char *ax)
-{
-  return do_padlock(ctx, bx, ax, 1);
-}
-#endif /*USE_PADLOCK*/
-
-
 static unsigned int
 rijndael_encrypt (void *context, byte *b, const byte *a)
 {
diff --git a/configure.ac b/configure.ac
index 369be25..c979d57 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1703,6 +1703,9 @@ if test "$found" = "1" ; then
      x86)
          # Build with the AES-NI implementation
          GCRYPT_CIPHERS="$GCRYPT_CIPHERS rijndael-aesni.lo"
+
+         # Build with the Padlock implementation
+         GCRYPT_CIPHERS="$GCRYPT_CIPHERS rijndael-padlock.lo"
       ;;
    esac
 fi




More information about the Gcrypt-devel mailing list