summaryrefslogtreecommitdiff
path: root/crypto/cipher.c
diff options
context:
space:
mode:
authorSebastian Siewior <linux-crypto@ml.breakpoint.cc>2007-05-19 19:51:21 +1000
committerHerbert Xu <herbert@gondor.apana.org.au>2007-07-11 20:58:54 +0800
commitca7c39385ce1a7b44894a4b225a4608624e90730 (patch)
tree107948d1bd8010ccb5185f34e2c2ef93098586cb /crypto/cipher.c
parentfe3c5206adc5d7395828185ab73e9a522655b984 (diff)
downloadlinux-3.10-ca7c39385ce1a7b44894a4b225a4608624e90730.tar.gz
linux-3.10-ca7c39385ce1a7b44894a4b225a4608624e90730.tar.bz2
linux-3.10-ca7c39385ce1a7b44894a4b225a4608624e90730.zip
[CRYPTO] api: Handle unaligned keys in setkey
setkey() in {cipher,blkcipher,ablkcipher,hash}.c does not respect the requested alignment by the algorithm. This patch fixes it. The extra memory is allocated by kmalloc() with GFP_ATOMIC flag. Signed-off-by: Sebastian Siewior <linux-crypto@ml.breakpoint.cc> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto/cipher.c')
-rw-r--r--crypto/cipher.c33
1 files changed, 30 insertions, 3 deletions
diff --git a/crypto/cipher.c b/crypto/cipher.c
index 333aab2f027..0b2650c2014 100644
--- a/crypto/cipher.c
+++ b/crypto/cipher.c
@@ -20,16 +20,43 @@
#include <linux/string.h>
#include "internal.h"
+static int setkey_unaligned(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
+{
+ struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
+ unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
+ int ret;
+ u8 *buffer, *alignbuffer;
+ unsigned long absize;
+
+ absize = keylen + alignmask;
+ buffer = kmalloc(absize, GFP_ATOMIC);
+ if (!buffer)
+ return -ENOMEM;
+
+ alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
+ memcpy(alignbuffer, key, keylen);
+ ret = cia->cia_setkey(tfm, alignbuffer, keylen);
+ memset(alignbuffer, 0, absize);
+ kfree(buffer);
+ return ret;
+
+}
+
static int setkey(struct crypto_tfm *tfm, const u8 *key, unsigned int keylen)
{
struct cipher_alg *cia = &tfm->__crt_alg->cra_cipher;
-
+ unsigned long alignmask = crypto_tfm_alg_alignmask(tfm);
+
tfm->crt_flags &= ~CRYPTO_TFM_RES_MASK;
if (keylen < cia->cia_min_keysize || keylen > cia->cia_max_keysize) {
tfm->crt_flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
return -EINVAL;
- } else
- return cia->cia_setkey(tfm, key, keylen);
+ }
+
+ if ((unsigned long)key & alignmask)
+ return setkey_unaligned(tfm, key, keylen);
+
+ return cia->cia_setkey(tfm, key, keylen);
}
static void cipher_crypt_unaligned(void (*fn)(struct crypto_tfm *, u8 *,