Merge branches 'pm-sleep', 'pm-cpufreq' and 'pm-cpuidle'
[cascardo/linux.git] / crypto / testmgr.c
index 498649a..ac2b631 100644 (file)
@@ -27,6 +27,7 @@
 #include <linux/slab.h>
 #include <linux/string.h>
 #include <crypto/rng.h>
+#include <crypto/drbg.h>
 
 #include "internal.h"
 
@@ -108,6 +109,11 @@ struct cprng_test_suite {
        unsigned int count;
 };
 
+struct drbg_test_suite {
+       struct drbg_testvec *vecs;
+       unsigned int count;
+};
+
 struct alg_test_desc {
        const char *alg;
        int (*test)(const struct alg_test_desc *desc, const char *driver,
@@ -121,6 +127,7 @@ struct alg_test_desc {
                struct pcomp_test_suite pcomp;
                struct hash_test_suite hash;
                struct cprng_test_suite cprng;
+               struct drbg_test_suite drbg;
        } suite;
 };
 
@@ -191,13 +198,20 @@ static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
        const char *algo = crypto_tfm_alg_driver_name(crypto_ahash_tfm(tfm));
        unsigned int i, j, k, temp;
        struct scatterlist sg[8];
-       char result[64];
+       char *result;
+       char *key;
        struct ahash_request *req;
        struct tcrypt_result tresult;
        void *hash_buff;
        char *xbuf[XBUFSIZE];
        int ret = -ENOMEM;
 
+       result = kmalloc(MAX_DIGEST_SIZE, GFP_KERNEL);
+       if (!result)
+               return ret;
+       key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
+       if (!key)
+               goto out_nobuf;
        if (testmgr_alloc_buf(xbuf))
                goto out_nobuf;
 
@@ -222,7 +236,7 @@ static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
                        goto out;
 
                j++;
-               memset(result, 0, 64);
+               memset(result, 0, MAX_DIGEST_SIZE);
 
                hash_buff = xbuf[0];
                hash_buff += align_offset;
@@ -232,8 +246,14 @@ static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
 
                if (template[i].ksize) {
                        crypto_ahash_clear_flags(tfm, ~0);
-                       ret = crypto_ahash_setkey(tfm, template[i].key,
-                                                 template[i].ksize);
+                       if (template[i].ksize > MAX_KEYLEN) {
+                               pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
+                                      j, algo, template[i].ksize, MAX_KEYLEN);
+                               ret = -EINVAL;
+                               goto out;
+                       }
+                       memcpy(key, template[i].key, template[i].ksize);
+                       ret = crypto_ahash_setkey(tfm, key, template[i].ksize);
                        if (ret) {
                                printk(KERN_ERR "alg: hash: setkey failed on "
                                       "test %d for %s: ret=%d\n", j, algo,
@@ -293,7 +313,7 @@ static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
 
                if (template[i].np) {
                        j++;
-                       memset(result, 0, 64);
+                       memset(result, 0, MAX_DIGEST_SIZE);
 
                        temp = 0;
                        sg_init_table(sg, template[i].np);
@@ -312,8 +332,16 @@ static int __test_hash(struct crypto_ahash *tfm, struct hash_testvec *template,
                        }
 
                        if (template[i].ksize) {
+                               if (template[i].ksize > MAX_KEYLEN) {
+                                       pr_err("alg: hash: setkey failed on test %d for %s: key size %d > %d\n",
+                                              j, algo, template[i].ksize,
+                                              MAX_KEYLEN);
+                                       ret = -EINVAL;
+                                       goto out;
+                               }
                                crypto_ahash_clear_flags(tfm, ~0);
-                               ret = crypto_ahash_setkey(tfm, template[i].key,
+                               memcpy(key, template[i].key, template[i].ksize);
+                               ret = crypto_ahash_setkey(tfm, key,
                                                          template[i].ksize);
 
                                if (ret) {
@@ -365,6 +393,8 @@ out:
 out_noreq:
        testmgr_free_buf(xbuf);
 out_nobuf:
+       kfree(key);
+       kfree(result);
        return ret;
 }
 
@@ -422,6 +452,9 @@ static int __test_aead(struct crypto_aead *tfm, int enc,
        iv = kzalloc(MAX_IVLEN, GFP_KERNEL);
        if (!iv)
                return ret;
+       key = kmalloc(MAX_KEYLEN, GFP_KERNEL);
+       if (!key)
+               goto out_noxbuf;
        if (testmgr_alloc_buf(xbuf))
                goto out_noxbuf;
        if (testmgr_alloc_buf(axbuf))
@@ -486,7 +519,14 @@ static int __test_aead(struct crypto_aead *tfm, int enc,
                                crypto_aead_set_flags(
                                        tfm, CRYPTO_TFM_REQ_WEAK_KEY);
 
-                       key = template[i].key;
+                       if (template[i].klen > MAX_KEYLEN) {
+                               pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
+                                      d, j, algo, template[i].klen,
+                                      MAX_KEYLEN);
+                               ret = -EINVAL;
+                               goto out;
+                       }
+                       memcpy(key, template[i].key, template[i].klen);
 
                        ret = crypto_aead_setkey(tfm, key,
                                                 template[i].klen);
@@ -587,7 +627,14 @@ static int __test_aead(struct crypto_aead *tfm, int enc,
                        if (template[i].wk)
                                crypto_aead_set_flags(
                                        tfm, CRYPTO_TFM_REQ_WEAK_KEY);
-                       key = template[i].key;
+                       if (template[i].klen > MAX_KEYLEN) {
+                               pr_err("alg: aead%s: setkey failed on test %d for %s: key size %d > %d\n",
+                                      d, j, algo, template[i].klen,
+                                      MAX_KEYLEN);
+                               ret = -EINVAL;
+                               goto out;
+                       }
+                       memcpy(key, template[i].key, template[i].klen);
 
                        ret = crypto_aead_setkey(tfm, key, template[i].klen);
                        if (!ret == template[i].fail) {
@@ -769,6 +816,7 @@ out_nooutbuf:
 out_noaxbuf:
        testmgr_free_buf(xbuf);
 out_noxbuf:
+       kfree(key);
        kfree(iv);
        return ret;
 }
@@ -1715,6 +1763,100 @@ static int alg_test_cprng(const struct alg_test_desc *desc, const char *driver,
        return err;
 }
 
+
+static int drbg_cavs_test(struct drbg_testvec *test, int pr,
+                         const char *driver, u32 type, u32 mask)
+{
+       int ret = -EAGAIN;
+       struct crypto_rng *drng;
+       struct drbg_test_data test_data;
+       struct drbg_string addtl, pers, testentropy;
+       unsigned char *buf = kzalloc(test->expectedlen, GFP_KERNEL);
+
+       if (!buf)
+               return -ENOMEM;
+
+       drng = crypto_alloc_rng(driver, type, mask);
+       if (IS_ERR(drng)) {
+               printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
+                      "%s\n", driver);
+               kzfree(buf);
+               return -ENOMEM;
+       }
+
+       test_data.testentropy = &testentropy;
+       drbg_string_fill(&testentropy, test->entropy, test->entropylen);
+       drbg_string_fill(&pers, test->pers, test->perslen);
+       ret = crypto_drbg_reset_test(drng, &pers, &test_data);
+       if (ret) {
+               printk(KERN_ERR "alg: drbg: Failed to reset rng\n");
+               goto outbuf;
+       }
+
+       drbg_string_fill(&addtl, test->addtla, test->addtllen);
+       if (pr) {
+               drbg_string_fill(&testentropy, test->entpra, test->entprlen);
+               ret = crypto_drbg_get_bytes_addtl_test(drng,
+                       buf, test->expectedlen, &addtl, &test_data);
+       } else {
+               ret = crypto_drbg_get_bytes_addtl(drng,
+                       buf, test->expectedlen, &addtl);
+       }
+       if (ret <= 0) {
+               printk(KERN_ERR "alg: drbg: could not obtain random data for "
+                      "driver %s\n", driver);
+               goto outbuf;
+       }
+
+       drbg_string_fill(&addtl, test->addtlb, test->addtllen);
+       if (pr) {
+               drbg_string_fill(&testentropy, test->entprb, test->entprlen);
+               ret = crypto_drbg_get_bytes_addtl_test(drng,
+                       buf, test->expectedlen, &addtl, &test_data);
+       } else {
+               ret = crypto_drbg_get_bytes_addtl(drng,
+                       buf, test->expectedlen, &addtl);
+       }
+       if (ret <= 0) {
+               printk(KERN_ERR "alg: drbg: could not obtain random data for "
+                      "driver %s\n", driver);
+               goto outbuf;
+       }
+
+       ret = memcmp(test->expected, buf, test->expectedlen);
+
+outbuf:
+       crypto_free_rng(drng);
+       kzfree(buf);
+       return ret;
+}
+
+
+static int alg_test_drbg(const struct alg_test_desc *desc, const char *driver,
+                        u32 type, u32 mask)
+{
+       int err = 0;
+       int pr = 0;
+       int i = 0;
+       struct drbg_testvec *template = desc->suite.drbg.vecs;
+       unsigned int tcount = desc->suite.drbg.count;
+
+       if (0 == memcmp(driver, "drbg_pr_", 8))
+               pr = 1;
+
+       for (i = 0; i < tcount; i++) {
+               err = drbg_cavs_test(&template[i], pr, driver, type, mask);
+               if (err) {
+                       printk(KERN_ERR "alg: drbg: Test %d failed for %s\n",
+                              i, driver);
+                       err = -EINVAL;
+                       break;
+               }
+       }
+       return err;
+
+}
+
 static int alg_test_null(const struct alg_test_desc *desc,
                             const char *driver, u32 type, u32 mask)
 {
@@ -2457,6 +2599,152 @@ static const struct alg_test_desc alg_test_descs[] = {
        }, {
                .alg = "digest_null",
                .test = alg_test_null,
+       }, {
+               .alg = "drbg_nopr_ctr_aes128",
+               .test = alg_test_drbg,
+               .fips_allowed = 1,
+               .suite = {
+                       .drbg = {
+                               .vecs = drbg_nopr_ctr_aes128_tv_template,
+                               .count = ARRAY_SIZE(drbg_nopr_ctr_aes128_tv_template)
+                       }
+               }
+       }, {
+               .alg = "drbg_nopr_ctr_aes192",
+               .test = alg_test_drbg,
+               .fips_allowed = 1,
+               .suite = {
+                       .drbg = {
+                               .vecs = drbg_nopr_ctr_aes192_tv_template,
+                               .count = ARRAY_SIZE(drbg_nopr_ctr_aes192_tv_template)
+                       }
+               }
+       }, {
+               .alg = "drbg_nopr_ctr_aes256",
+               .test = alg_test_drbg,
+               .fips_allowed = 1,
+               .suite = {
+                       .drbg = {
+                               .vecs = drbg_nopr_ctr_aes256_tv_template,
+                               .count = ARRAY_SIZE(drbg_nopr_ctr_aes256_tv_template)
+                       }
+               }
+       }, {
+               /*
+                * There is no need to specifically test the DRBG with every
+                * backend cipher -- covered by drbg_nopr_hmac_sha256 test
+                */
+               .alg = "drbg_nopr_hmac_sha1",
+               .fips_allowed = 1,
+               .test = alg_test_null,
+       }, {
+               .alg = "drbg_nopr_hmac_sha256",
+               .test = alg_test_drbg,
+               .fips_allowed = 1,
+               .suite = {
+                       .drbg = {
+                               .vecs = drbg_nopr_hmac_sha256_tv_template,
+                               .count =
+                               ARRAY_SIZE(drbg_nopr_hmac_sha256_tv_template)
+                       }
+               }
+       }, {
+               /* covered by drbg_nopr_hmac_sha256 test */
+               .alg = "drbg_nopr_hmac_sha384",
+               .fips_allowed = 1,
+               .test = alg_test_null,
+       }, {
+               .alg = "drbg_nopr_hmac_sha512",
+               .test = alg_test_null,
+               .fips_allowed = 1,
+       }, {
+               .alg = "drbg_nopr_sha1",
+               .fips_allowed = 1,
+               .test = alg_test_null,
+       }, {
+               .alg = "drbg_nopr_sha256",
+               .test = alg_test_drbg,
+               .fips_allowed = 1,
+               .suite = {
+                       .drbg = {
+                               .vecs = drbg_nopr_sha256_tv_template,
+                               .count = ARRAY_SIZE(drbg_nopr_sha256_tv_template)
+                       }
+               }
+       }, {
+               /* covered by drbg_nopr_sha256 test */
+               .alg = "drbg_nopr_sha384",
+               .fips_allowed = 1,
+               .test = alg_test_null,
+       }, {
+               .alg = "drbg_nopr_sha512",
+               .fips_allowed = 1,
+               .test = alg_test_null,
+       }, {
+               .alg = "drbg_pr_ctr_aes128",
+               .test = alg_test_drbg,
+               .fips_allowed = 1,
+               .suite = {
+                       .drbg = {
+                               .vecs = drbg_pr_ctr_aes128_tv_template,
+                               .count = ARRAY_SIZE(drbg_pr_ctr_aes128_tv_template)
+                       }
+               }
+       }, {
+               /* covered by drbg_pr_ctr_aes128 test */
+               .alg = "drbg_pr_ctr_aes192",
+               .fips_allowed = 1,
+               .test = alg_test_null,
+       }, {
+               .alg = "drbg_pr_ctr_aes256",
+               .fips_allowed = 1,
+               .test = alg_test_null,
+       }, {
+               .alg = "drbg_pr_hmac_sha1",
+               .fips_allowed = 1,
+               .test = alg_test_null,
+       }, {
+               .alg = "drbg_pr_hmac_sha256",
+               .test = alg_test_drbg,
+               .fips_allowed = 1,
+               .suite = {
+                       .drbg = {
+                               .vecs = drbg_pr_hmac_sha256_tv_template,
+                               .count = ARRAY_SIZE(drbg_pr_hmac_sha256_tv_template)
+                       }
+               }
+       }, {
+               /* covered by drbg_pr_hmac_sha256 test */
+               .alg = "drbg_pr_hmac_sha384",
+               .fips_allowed = 1,
+               .test = alg_test_null,
+       }, {
+               .alg = "drbg_pr_hmac_sha512",
+               .test = alg_test_null,
+               .fips_allowed = 1,
+       }, {
+               .alg = "drbg_pr_sha1",
+               .fips_allowed = 1,
+               .test = alg_test_null,
+       }, {
+               .alg = "drbg_pr_sha256",
+               .test = alg_test_drbg,
+               .fips_allowed = 1,
+               .suite = {
+                       .drbg = {
+                               .vecs = drbg_pr_sha256_tv_template,
+                               .count = ARRAY_SIZE(drbg_pr_sha256_tv_template)
+                       }
+               }
+       }, {
+               /* covered by drbg_pr_sha256 test */
+               .alg = "drbg_pr_sha384",
+               .fips_allowed = 1,
+               .test = alg_test_null,
+       }, {
+               .alg = "drbg_pr_sha512",
+               .fips_allowed = 1,
+               .test = alg_test_null,
        }, {
                .alg = "ecb(__aes-aesni)",
                .test = alg_test_null,