]> Pileus Git - ~andy/linux/blobdiff - drivers/crypto/padlock-aes.c
Merge branch 'upstream-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jgarzi...
[~andy/linux] / drivers / crypto / padlock-aes.c
index d4501dc7e65026d1fe87f5f1f751c8acff9e3810..2f3ad3f7dfea2229893864b7b31e9a3dfb5f61c6 100644 (file)
@@ -5,7 +5,7 @@
  *
  * Copyright (c) 2004  Michal Ludvig <michal@logix.cz>
  *
- * Key expansion routine taken from crypto/aes.c
+ * Key expansion routine taken from crypto/aes_generic.c
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -44,6 +44,7 @@
  */
 
 #include <crypto/algapi.h>
+#include <crypto/aes.h>
 #include <linux/module.h>
 #include <linux/init.h>
 #include <linux/types.h>
@@ -53,9 +54,6 @@
 #include <asm/byteorder.h>
 #include "padlock.h"
 
-#define AES_MIN_KEY_SIZE       16      /* in uint8_t units */
-#define AES_MAX_KEY_SIZE       32      /* ditto */
-#define AES_BLOCK_SIZE         16      /* ditto */
 #define AES_EXTENDED_KEY_SIZE  64      /* in uint32_t units */
 #define AES_EXTENDED_KEY_SIZE_B        (AES_EXTENDED_KEY_SIZE * sizeof(uint32_t))
 
@@ -419,20 +417,65 @@ static int aes_set_key(struct crypto_tfm *tfm, const u8 *in_key,
 /* ====== Encryption/decryption routines ====== */
 
 /* These are the real call to PadLock. */
+static inline void padlock_reset_key(void)
+{
+       asm volatile ("pushfl; popfl");
+}
+
+static inline void padlock_xcrypt(const u8 *input, u8 *output, void *key,
+                                 void *control_word)
+{
+       asm volatile (".byte 0xf3,0x0f,0xa7,0xc8"       /* rep xcryptecb */
+                     : "+S"(input), "+D"(output)
+                     : "d"(control_word), "b"(key), "c"(1));
+}
+
+static void aes_crypt_copy(const u8 *in, u8 *out, u32 *key, struct cword *cword)
+{
+       u8 buf[AES_BLOCK_SIZE * 2 + PADLOCK_ALIGNMENT - 1];
+       u8 *tmp = PTR_ALIGN(&buf[0], PADLOCK_ALIGNMENT);
+
+       memcpy(tmp, in, AES_BLOCK_SIZE);
+       padlock_xcrypt(tmp, out, key, cword);
+}
+
+static inline void aes_crypt(const u8 *in, u8 *out, u32 *key,
+                            struct cword *cword)
+{
+       /* padlock_xcrypt requires at least two blocks of data. */
+       if (unlikely(!(((unsigned long)in ^ (PAGE_SIZE - AES_BLOCK_SIZE)) &
+                      (PAGE_SIZE - 1)))) {
+               aes_crypt_copy(in, out, key, cword);
+               return;
+       }
+
+       padlock_xcrypt(in, out, key, cword);
+}
+
 static inline void padlock_xcrypt_ecb(const u8 *input, u8 *output, void *key,
                                      void *control_word, u32 count)
 {
-       asm volatile ("pushfl; popfl");         /* enforce key reload. */
-       asm volatile (".byte 0xf3,0x0f,0xa7,0xc8"       /* rep xcryptecb */
+       if (count == 1) {
+               aes_crypt(input, output, key, control_word);
+               return;
+       }
+
+       asm volatile ("test $1, %%cl;"
+                     "je 1f;"
+                     "lea -1(%%ecx), %%eax;"
+                     "mov $1, %%ecx;"
+                     ".byte 0xf3,0x0f,0xa7,0xc8;"      /* rep xcryptecb */
+                     "mov %%eax, %%ecx;"
+                     "1:"
+                     ".byte 0xf3,0x0f,0xa7,0xc8"       /* rep xcryptecb */
                      : "+S"(input), "+D"(output)
-                     : "d"(control_word), "b"(key), "c"(count));
+                     : "d"(control_word), "b"(key), "c"(count)
+                     : "ax");
 }
 
 static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
                                     u8 *iv, void *control_word, u32 count)
 {
-       /* Enforce key reload. */
-       asm volatile ("pushfl; popfl");
        /* rep xcryptcbc */
        asm volatile (".byte 0xf3,0x0f,0xa7,0xd0"
                      : "+S" (input), "+D" (output), "+a" (iv)
@@ -443,13 +486,15 @@ static inline u8 *padlock_xcrypt_cbc(const u8 *input, u8 *output, void *key,
 static void aes_encrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
 {
        struct aes_ctx *ctx = aes_ctx(tfm);
-       padlock_xcrypt_ecb(in, out, ctx->E, &ctx->cword.encrypt, 1);
+       padlock_reset_key();
+       aes_crypt(in, out, ctx->E, &ctx->cword.encrypt);
 }
 
 static void aes_decrypt(struct crypto_tfm *tfm, u8 *out, const u8 *in)
 {
        struct aes_ctx *ctx = aes_ctx(tfm);
-       padlock_xcrypt_ecb(in, out, ctx->D, &ctx->cword.decrypt, 1);
+       padlock_reset_key();
+       aes_crypt(in, out, ctx->D, &ctx->cword.decrypt);
 }
 
 static struct crypto_alg aes_alg = {
@@ -481,6 +526,8 @@ static int ecb_aes_encrypt(struct blkcipher_desc *desc,
        struct blkcipher_walk walk;
        int err;
 
+       padlock_reset_key();
+
        blkcipher_walk_init(&walk, dst, src, nbytes);
        err = blkcipher_walk_virt(desc, &walk);
 
@@ -503,6 +550,8 @@ static int ecb_aes_decrypt(struct blkcipher_desc *desc,
        struct blkcipher_walk walk;
        int err;
 
+       padlock_reset_key();
+
        blkcipher_walk_init(&walk, dst, src, nbytes);
        err = blkcipher_walk_virt(desc, &walk);
 
@@ -547,6 +596,8 @@ static int cbc_aes_encrypt(struct blkcipher_desc *desc,
        struct blkcipher_walk walk;
        int err;
 
+       padlock_reset_key();
+
        blkcipher_walk_init(&walk, dst, src, nbytes);
        err = blkcipher_walk_virt(desc, &walk);
 
@@ -571,6 +622,8 @@ static int cbc_aes_decrypt(struct blkcipher_desc *desc,
        struct blkcipher_walk walk;
        int err;
 
+       padlock_reset_key();
+
        blkcipher_walk_init(&walk, dst, src, nbytes);
        err = blkcipher_walk_virt(desc, &walk);
 
@@ -660,4 +713,4 @@ MODULE_DESCRIPTION("VIA PadLock AES algorithm support");
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("Michal Ludvig");
 
-MODULE_ALIAS("aes-padlock");
+MODULE_ALIAS("aes");