]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - crypto/skcipher.c
perf tools: Fix build with ARCH=x86_64
[karo-tx-linux.git] / crypto / skcipher.c
index 0e1e6c35188e24185423e91a93ef2ffea29902e0..4faa0fd53b0c120d39022ad726dbbe2c74f787bd 100644 (file)
@@ -19,6 +19,7 @@
 #include <crypto/scatterwalk.h>
 #include <linux/bug.h>
 #include <linux/cryptouser.h>
+#include <linux/compiler.h>
 #include <linux/list.h>
 #include <linux/module.h>
 #include <linux/rtnetlink.h>
@@ -185,12 +186,12 @@ void skcipher_walk_complete(struct skcipher_walk *walk, int err)
                data = p->data;
                if (!data) {
                        data = PTR_ALIGN(&p->buffer[0], walk->alignmask + 1);
-                       data = skcipher_get_spot(data, walk->chunksize);
+                       data = skcipher_get_spot(data, walk->stride);
                }
 
                scatterwalk_copychunks(data, &p->dst, p->len, 1);
 
-               if (offset_in_page(p->data) + p->len + walk->chunksize >
+               if (offset_in_page(p->data) + p->len + walk->stride >
                    PAGE_SIZE)
                        free_page((unsigned long)p->data);
 
@@ -299,7 +300,7 @@ static int skcipher_next_copy(struct skcipher_walk *walk)
        p->len = walk->nbytes;
        skcipher_queue_write(walk, p);
 
-       if (offset_in_page(walk->page) + walk->nbytes + walk->chunksize >
+       if (offset_in_page(walk->page) + walk->nbytes + walk->stride >
            PAGE_SIZE)
                walk->page = NULL;
        else
@@ -344,7 +345,7 @@ static int skcipher_walk_next(struct skcipher_walk *walk)
                         SKCIPHER_WALK_DIFF);
 
        n = walk->total;
-       bsize = min(walk->chunksize, max(n, walk->blocksize));
+       bsize = min(walk->stride, max(n, walk->blocksize));
        n = scatterwalk_clamp(&walk->in, n);
        n = scatterwalk_clamp(&walk->out, n);
 
@@ -393,7 +394,7 @@ static int skcipher_copy_iv(struct skcipher_walk *walk)
        unsigned a = crypto_tfm_ctx_alignment() - 1;
        unsigned alignmask = walk->alignmask;
        unsigned ivsize = walk->ivsize;
-       unsigned bs = walk->chunksize;
+       unsigned bs = walk->stride;
        unsigned aligned_bs;
        unsigned size;
        u8 *iv;
@@ -463,7 +464,7 @@ static int skcipher_walk_skcipher(struct skcipher_walk *walk,
                       SKCIPHER_WALK_SLEEP : 0;
 
        walk->blocksize = crypto_skcipher_blocksize(tfm);
-       walk->chunksize = crypto_skcipher_chunksize(tfm);
+       walk->stride = crypto_skcipher_walksize(tfm);
        walk->ivsize = crypto_skcipher_ivsize(tfm);
        walk->alignmask = crypto_skcipher_alignmask(tfm);
 
@@ -525,7 +526,7 @@ static int skcipher_walk_aead_common(struct skcipher_walk *walk,
                walk->flags &= ~SKCIPHER_WALK_SLEEP;
 
        walk->blocksize = crypto_aead_blocksize(tfm);
-       walk->chunksize = crypto_aead_chunksize(tfm);
+       walk->stride = crypto_aead_chunksize(tfm);
        walk->ivsize = crypto_aead_ivsize(tfm);
        walk->alignmask = crypto_aead_alignmask(tfm);
 
@@ -763,6 +764,44 @@ static int crypto_init_skcipher_ops_ablkcipher(struct crypto_tfm *tfm)
        return 0;
 }
 
+static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
+                                    const u8 *key, unsigned int keylen)
+{
+       unsigned long alignmask = crypto_skcipher_alignmask(tfm);
+       struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
+       u8 *buffer, *alignbuffer;
+       unsigned long absize;
+       int ret;
+
+       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 = cipher->setkey(tfm, alignbuffer, keylen);
+       kzfree(buffer);
+       return ret;
+}
+
+static int skcipher_setkey(struct crypto_skcipher *tfm, const u8 *key,
+                          unsigned int keylen)
+{
+       struct skcipher_alg *cipher = crypto_skcipher_alg(tfm);
+       unsigned long alignmask = crypto_skcipher_alignmask(tfm);
+
+       if (keylen < cipher->min_keysize || keylen > cipher->max_keysize) {
+               crypto_skcipher_set_flags(tfm, CRYPTO_TFM_RES_BAD_KEY_LEN);
+               return -EINVAL;
+       }
+
+       if ((unsigned long)key & alignmask)
+               return skcipher_setkey_unaligned(tfm, key, keylen);
+
+       return cipher->setkey(tfm, key, keylen);
+}
+
 static void crypto_skcipher_exit_tfm(struct crypto_tfm *tfm)
 {
        struct crypto_skcipher *skcipher = __crypto_skcipher_cast(tfm);
@@ -783,7 +822,7 @@ static int crypto_skcipher_init_tfm(struct crypto_tfm *tfm)
            tfm->__crt_alg->cra_type == &crypto_givcipher_type)
                return crypto_init_skcipher_ops_ablkcipher(tfm);
 
-       skcipher->setkey = alg->setkey;
+       skcipher->setkey = skcipher_setkey;
        skcipher->encrypt = alg->encrypt;
        skcipher->decrypt = alg->decrypt;
        skcipher->ivsize = alg->ivsize;
@@ -807,7 +846,7 @@ static void crypto_skcipher_free_instance(struct crypto_instance *inst)
 }
 
 static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
-       __attribute__ ((unused));
+       __maybe_unused;
 static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
 {
        struct skcipher_alg *skcipher = container_of(alg, struct skcipher_alg,
@@ -821,6 +860,7 @@ static void crypto_skcipher_show(struct seq_file *m, struct crypto_alg *alg)
        seq_printf(m, "max keysize  : %u\n", skcipher->max_keysize);
        seq_printf(m, "ivsize       : %u\n", skcipher->ivsize);
        seq_printf(m, "chunksize    : %u\n", skcipher->chunksize);
+       seq_printf(m, "walksize     : %u\n", skcipher->walksize);
 }
 
 #ifdef CONFIG_NET
@@ -893,11 +933,14 @@ static int skcipher_prepare_alg(struct skcipher_alg *alg)
 {
        struct crypto_alg *base = &alg->base;
 
-       if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8)
+       if (alg->ivsize > PAGE_SIZE / 8 || alg->chunksize > PAGE_SIZE / 8 ||
+           alg->walksize > PAGE_SIZE / 8)
                return -EINVAL;
 
        if (!alg->chunksize)
                alg->chunksize = base->cra_blocksize;
+       if (!alg->walksize)
+               alg->walksize = alg->chunksize;
 
        base->cra_type = &crypto_skcipher_type2;
        base->cra_flags &= ~CRYPTO_ALG_TYPE_MASK;