Commit 08d6af8c160b6bd9b21a3177e2b1bebc72a21041

Authored by Jussi Kivilinna
Committed by Herbert Xu
1 parent 549595a0c7

crypto: testmgr - make test_skcipher also test 'dst != src' code paths

Currrently test_skcipher uses same buffer for destination and source. However
in any places, 'dst != src' take different path than 'dst == src' case.

Therefore make test_skcipher also run tests with destination buffer being
different than source buffer.

Signed-off-by: Jussi Kivilinna <jussi.kivilinna@mbnet.fi>
Acked-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Showing 1 changed file with 76 additions and 31 deletions Side-by-side Diff

... ... @@ -761,8 +761,9 @@
761 761 return ret;
762 762 }
763 763  
764   -static int test_skcipher(struct crypto_ablkcipher *tfm, int enc,
765   - struct cipher_testvec *template, unsigned int tcount)
  764 +static int __test_skcipher(struct crypto_ablkcipher *tfm, int enc,
  765 + struct cipher_testvec *template, unsigned int tcount,
  766 + const bool diff_dst)
766 767 {
767 768 const char *algo =
768 769 crypto_tfm_alg_driver_name(crypto_ablkcipher_tfm(tfm));
769 770  
770 771  
... ... @@ -770,16 +771,26 @@
770 771 char *q;
771 772 struct ablkcipher_request *req;
772 773 struct scatterlist sg[8];
773   - const char *e;
  774 + struct scatterlist sgout[8];
  775 + const char *e, *d;
774 776 struct tcrypt_result result;
775 777 void *data;
776 778 char iv[MAX_IVLEN];
777 779 char *xbuf[XBUFSIZE];
  780 + char *xoutbuf[XBUFSIZE];
778 781 int ret = -ENOMEM;
779 782  
780 783 if (testmgr_alloc_buf(xbuf))
781 784 goto out_nobuf;
782 785  
  786 + if (diff_dst && testmgr_alloc_buf(xoutbuf))
  787 + goto out_nooutbuf;
  788 +
  789 + if (diff_dst)
  790 + d = "-ddst";
  791 + else
  792 + d = "";
  793 +
783 794 if (enc == ENCRYPT)
784 795 e = "encryption";
785 796 else
... ... @@ -789,8 +800,8 @@
789 800  
790 801 req = ablkcipher_request_alloc(tfm, GFP_KERNEL);
791 802 if (!req) {
792   - printk(KERN_ERR "alg: skcipher: Failed to allocate request "
793   - "for %s\n", algo);
  803 + pr_err("alg: skcipher%s: Failed to allocate request for %s\n",
  804 + d, algo);
794 805 goto out;
795 806 }
796 807  
797 808  
798 809  
... ... @@ -822,16 +833,21 @@
822 833 ret = crypto_ablkcipher_setkey(tfm, template[i].key,
823 834 template[i].klen);
824 835 if (!ret == template[i].fail) {
825   - printk(KERN_ERR "alg: skcipher: setkey failed "
826   - "on test %d for %s: flags=%x\n", j,
827   - algo, crypto_ablkcipher_get_flags(tfm));
  836 + pr_err("alg: skcipher%s: setkey failed on test %d for %s: flags=%x\n",
  837 + d, j, algo,
  838 + crypto_ablkcipher_get_flags(tfm));
828 839 goto out;
829 840 } else if (ret)
830 841 continue;
831 842  
832 843 sg_init_one(&sg[0], data, template[i].ilen);
  844 + if (diff_dst) {
  845 + data = xoutbuf[0];
  846 + sg_init_one(&sgout[0], data, template[i].ilen);
  847 + }
833 848  
834   - ablkcipher_request_set_crypt(req, sg, sg,
  849 + ablkcipher_request_set_crypt(req, sg,
  850 + (diff_dst) ? sgout : sg,
835 851 template[i].ilen, iv);
836 852 ret = enc ?
837 853 crypto_ablkcipher_encrypt(req) :
838 854  
... ... @@ -850,16 +866,15 @@
850 866 }
851 867 /* fall through */
852 868 default:
853   - printk(KERN_ERR "alg: skcipher: %s failed on "
854   - "test %d for %s: ret=%d\n", e, j, algo,
855   - -ret);
  869 + pr_err("alg: skcipher%s: %s failed on test %d for %s: ret=%d\n",
  870 + d, e, j, algo, -ret);
856 871 goto out;
857 872 }
858 873  
859 874 q = data;
860 875 if (memcmp(q, template[i].result, template[i].rlen)) {
861   - printk(KERN_ERR "alg: skcipher: Test %d "
862   - "failed on %s for %s\n", j, e, algo);
  876 + pr_err("alg: skcipher%s: Test %d failed on %s for %s\n",
  877 + d, j, e, algo);
863 878 hexdump(q, template[i].rlen);
864 879 ret = -EINVAL;
865 880 goto out;
... ... @@ -886,9 +901,8 @@
886 901 ret = crypto_ablkcipher_setkey(tfm, template[i].key,
887 902 template[i].klen);
888 903 if (!ret == template[i].fail) {
889   - printk(KERN_ERR "alg: skcipher: setkey failed "
890   - "on chunk test %d for %s: flags=%x\n",
891   - j, algo,
  904 + pr_err("alg: skcipher%s: setkey failed on chunk test %d for %s: flags=%x\n",
  905 + d, j, algo,
892 906 crypto_ablkcipher_get_flags(tfm));
893 907 goto out;
894 908 } else if (ret)
... ... @@ -897,6 +911,8 @@
897 911 temp = 0;
898 912 ret = -EINVAL;
899 913 sg_init_table(sg, template[i].np);
  914 + if (diff_dst)
  915 + sg_init_table(sgout, template[i].np);
900 916 for (k = 0; k < template[i].np; k++) {
901 917 if (WARN_ON(offset_in_page(IDX[k]) +
902 918 template[i].tap[k] > PAGE_SIZE))
903 919  
904 920  
... ... @@ -913,11 +929,24 @@
913 929 q[template[i].tap[k]] = 0;
914 930  
915 931 sg_set_buf(&sg[k], q, template[i].tap[k]);
  932 + if (diff_dst) {
  933 + q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
  934 + offset_in_page(IDX[k]);
916 935  
  936 + sg_set_buf(&sgout[k], q,
  937 + template[i].tap[k]);
  938 +
  939 + memset(q, 0, template[i].tap[k]);
  940 + if (offset_in_page(q) +
  941 + template[i].tap[k] < PAGE_SIZE)
  942 + q[template[i].tap[k]] = 0;
  943 + }
  944 +
917 945 temp += template[i].tap[k];
918 946 }
919 947  
920   - ablkcipher_request_set_crypt(req, sg, sg,
  948 + ablkcipher_request_set_crypt(req, sg,
  949 + (diff_dst) ? sgout : sg,
921 950 template[i].ilen, iv);
922 951  
923 952 ret = enc ?
924 953  
925 954  
... ... @@ -937,23 +966,25 @@
937 966 }
938 967 /* fall through */
939 968 default:
940   - printk(KERN_ERR "alg: skcipher: %s failed on "
941   - "chunk test %d for %s: ret=%d\n", e, j,
942   - algo, -ret);
  969 + pr_err("alg: skcipher%s: %s failed on chunk test %d for %s: ret=%d\n",
  970 + d, e, j, algo, -ret);
943 971 goto out;
944 972 }
945 973  
946 974 temp = 0;
947 975 ret = -EINVAL;
948 976 for (k = 0; k < template[i].np; k++) {
949   - q = xbuf[IDX[k] >> PAGE_SHIFT] +
950   - offset_in_page(IDX[k]);
  977 + if (diff_dst)
  978 + q = xoutbuf[IDX[k] >> PAGE_SHIFT] +
  979 + offset_in_page(IDX[k]);
  980 + else
  981 + q = xbuf[IDX[k] >> PAGE_SHIFT] +
  982 + offset_in_page(IDX[k]);
951 983  
952 984 if (memcmp(q, template[i].result + temp,
953 985 template[i].tap[k])) {
954   - printk(KERN_ERR "alg: skcipher: Chunk "
955   - "test %d failed on %s at page "
956   - "%u for %s\n", j, e, k, algo);
  986 + pr_err("alg: skcipher%s: Chunk test %d failed on %s at page %u for %s\n",
  987 + d, j, e, k, algo);
957 988 hexdump(q, template[i].tap[k]);
958 989 goto out;
959 990 }
... ... @@ -962,11 +993,8 @@
962 993 for (n = 0; offset_in_page(q + n) && q[n]; n++)
963 994 ;
964 995 if (n) {
965   - printk(KERN_ERR "alg: skcipher: "
966   - "Result buffer corruption in "
967   - "chunk test %d on %s at page "
968   - "%u for %s: %u bytes:\n", j, e,
969   - k, algo, n);
  996 + pr_err("alg: skcipher%s: Result buffer corruption in chunk test %d on %s at page %u for %s: %u bytes:\n",
  997 + d, j, e, k, algo, n);
970 998 hexdump(q, n);
971 999 goto out;
972 1000 }
973 1001  
... ... @@ -979,9 +1007,26 @@
979 1007  
980 1008 out:
981 1009 ablkcipher_request_free(req);
  1010 + if (diff_dst)
  1011 + testmgr_free_buf(xoutbuf);
  1012 +out_nooutbuf:
982 1013 testmgr_free_buf(xbuf);
983 1014 out_nobuf:
984 1015 return ret;
  1016 +}
  1017 +
  1018 +static int test_skcipher(struct crypto_ablkcipher *tfm, int enc,
  1019 + struct cipher_testvec *template, unsigned int tcount)
  1020 +{
  1021 + int ret;
  1022 +
  1023 + /* test 'dst == src' case */
  1024 + ret = __test_skcipher(tfm, enc, template, tcount, false);
  1025 + if (ret)
  1026 + return ret;
  1027 +
  1028 + /* test 'dst != src' case */
  1029 + return __test_skcipher(tfm, enc, template, tcount, true);
985 1030 }
986 1031  
987 1032 static int test_comp(struct crypto_comp *tfm, struct comp_testvec *ctemplate,