Merge tag 'metag-fixes-for-v3.11-1' of git://git.kernel.org/pub/scm/linux/kernel...
[firefly-linux-kernel-4.4.55.git] / crypto / testmgr.c
index 8bd185f068b6618a136ebd1ec922b035bf5477df..2f00607039e2ad7c808d273f08756a8e463732d4 100644 (file)
@@ -184,8 +184,9 @@ static int do_one_async_hash_op(struct ahash_request *req,
        return ret;
 }
 
-static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
-                    unsigned int tcount, bool use_digest)
+static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
+                      unsigned int tcount, bool use_digest,
+                      const int align_offset)
 {
        const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
        unsigned int i, j, k, temp;
@@ -216,10 +217,15 @@ static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
                if (template[i].np)
                        continue;
 
+               ret = -EINVAL;
+               if (WARN_ON(align_offset + template[i].psize > PAGE_SIZE))
+                       goto out;
+
                j++;
                memset(result, 0, 64);
 
                hash_buff = xbuf[0];
+               hash_buff += align_offset;
 
                memcpy(hash_buff, template[i].plaintext, template[i].psize);
                sg_init_one(&sg[0], hash_buff, template[i].psize);
@@ -281,6 +287,10 @@ static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
 
        j = 0;
        for (i = 0; i < tcount; i++) {
+               /* alignment tests are only done with continuous buffers */
+               if (align_offset != 0)
+                       break;
+
                if (template[i].np) {
                        j++;
                        memset(result, 0, 64);
@@ -358,9 +368,36 @@ out_nobuf:
        return ret;
 }
 
+static int test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
+                    unsigned int tcount, bool use_digest)
+{
+       unsigned int alignmask;
+       int ret;
+
+       ret = __test_hash(tfm, template, tcount, use_digest, 0);
+       if (ret)
+               return ret;
+
+       /* test unaligned buffers, check with one byte offset */
+       ret = __test_hash(tfm, template, tcount, use_digest, 1);
+       if (ret)
+               return ret;
+
+       alignmask = crypto_tfm_alg_alignmask(&tfm->base);
+       if (alignmask) {
+               /* Check if alignment mask for tfm is correctly set. */
+               ret = __test_hash(tfm, template, tcount, use_digest,
+                                 alignmask + 1);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
+}
+
 static int __test_aead(struct crypto_aead *tfm, int enc,
                       struct aead_testvec *template, unsigned int tcount,
-                      const bool diff_dst)
+                      const bool diff_dst, const int align_offset)
 {
        const char *algo = crypto_tfm_alg_driver_name(crypto_aead_tfm(tfm));
        unsigned int i, j, k, n, temp;
@@ -423,15 +460,16 @@ static int __test_aead(struct crypto_aead *tfm, int enc,
                if (!template[i].np) {
                        j++;
 
-                       /* some tepmplates have no input data but they will
+                       /* some templates have no input data but they will
                         * touch input
                         */
                        input = xbuf[0];
+                       input += align_offset;
                        assoc = axbuf[0];
 
                        ret = -EINVAL;
-                       if (WARN_ON(template[i].ilen > PAGE_SIZE ||
-                                   template[i].alen > PAGE_SIZE))
+                       if (WARN_ON(align_offset + template[i].ilen >
+                                   PAGE_SIZE || template[i].alen > PAGE_SIZE))
                                goto out;
 
                        memcpy(input, template[i].input, template[i].ilen);
@@ -470,6 +508,7 @@ static int __test_aead(struct crypto_aead *tfm, int enc,
 
                        if (diff_dst) {
                                output = xoutbuf[0];
+                               output += align_offset;
                                sg_init_one(&sgout[0], output,
                                            template[i].ilen +
                                                (enc ? authsize : 0));
@@ -530,6 +569,10 @@ static int __test_aead(struct crypto_aead *tfm, int enc,
        }
 
        for (i = 0, j = 0; i < tcount; i++) {
+               /* alignment tests are only done with continuous buffers */
+               if (align_offset != 0)
+                       break;
+
                if (template[i].np) {
                        j++;
 
@@ -732,15 +775,34 @@ out_noxbuf:
 static int test_aead(struct crypto_aead *tfm, int enc,
                     struct aead_testvec *template, unsigned int tcount)
 {
+       unsigned int alignmask;
        int ret;
 
        /* test 'dst == src' case */
-       ret = __test_aead(tfm, enc, template, tcount, false);
+       ret = __test_aead(tfm, enc, template, tcount, false, 0);
        if (ret)
                return ret;
 
        /* test 'dst != src' case */
-       return __test_aead(tfm, enc, template, tcount, true);
+       ret = __test_aead(tfm, enc, template, tcount, true, 0);
+       if (ret)
+               return ret;
+
+       /* test unaligned buffers, check with one byte offset */
+       ret = __test_aead(tfm, enc, template, tcount, true, 1);
+       if (ret)
+               return ret;
+
+       alignmask = crypto_tfm_alg_alignmask(&tfm->base);
+       if (alignmask) {
+               /* Check if alignment mask for tfm is correctly set. */
+               ret = __test_aead(tfm, enc, template, tcount, true,
+                                 alignmask + 1);
+               if (ret)
+                       return ret;
+       }
+
+       return 0;
 }
 
 static int test_cipher(struct crypto_cipher *tfm, int enc,