diff options
author | Herbert Xu <herbert@gondor.apana.org.au> | 2016-06-29 06:03:50 -0400 |
---|---|---|
committer | Herbert Xu <herbert@gondor.apana.org.au> | 2016-07-01 11:45:04 -0400 |
commit | 7166e589da5b681e1f8ff2d1c491d7d71e9f7671 (patch) | |
tree | 6852bcf48150b3a413dc59990292c9f54bb9022c /crypto/tcrypt.c | |
parent | 2495cf25f60e085b35beb9b215235dfe1ca4f200 (diff) |
crypto: tcrypt - Use skcipher
This patch converts tcrypt to use the new skcipher interface as
opposed to ablkcipher/blkcipher.
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
Diffstat (limited to 'crypto/tcrypt.c')
-rw-r--r-- | crypto/tcrypt.c | 241 |
1 files changed, 44 insertions, 197 deletions
diff --git a/crypto/tcrypt.c b/crypto/tcrypt.c index 8a91dc34610e..68064fc9c8ee 100644 --- a/crypto/tcrypt.c +++ b/crypto/tcrypt.c | |||
@@ -24,6 +24,7 @@ | |||
24 | 24 | ||
25 | #include <crypto/aead.h> | 25 | #include <crypto/aead.h> |
26 | #include <crypto/hash.h> | 26 | #include <crypto/hash.h> |
27 | #include <crypto/skcipher.h> | ||
27 | #include <linux/err.h> | 28 | #include <linux/err.h> |
28 | #include <linux/fips.h> | 29 | #include <linux/fips.h> |
29 | #include <linux/init.h> | 30 | #include <linux/init.h> |
@@ -92,76 +93,6 @@ static void tcrypt_complete(struct crypto_async_request *req, int err) | |||
92 | complete(&res->completion); | 93 | complete(&res->completion); |
93 | } | 94 | } |
94 | 95 | ||
95 | static int test_cipher_jiffies(struct blkcipher_desc *desc, int enc, | ||
96 | struct scatterlist *sg, int blen, int secs) | ||
97 | { | ||
98 | unsigned long start, end; | ||
99 | int bcount; | ||
100 | int ret; | ||
101 | |||
102 | for (start = jiffies, end = start + secs * HZ, bcount = 0; | ||
103 | time_before(jiffies, end); bcount++) { | ||
104 | if (enc) | ||
105 | ret = crypto_blkcipher_encrypt(desc, sg, sg, blen); | ||
106 | else | ||
107 | ret = crypto_blkcipher_decrypt(desc, sg, sg, blen); | ||
108 | |||
109 | if (ret) | ||
110 | return ret; | ||
111 | } | ||
112 | |||
113 | printk("%d operations in %d seconds (%ld bytes)\n", | ||
114 | bcount, secs, (long)bcount * blen); | ||
115 | return 0; | ||
116 | } | ||
117 | |||
118 | static int test_cipher_cycles(struct blkcipher_desc *desc, int enc, | ||
119 | struct scatterlist *sg, int blen) | ||
120 | { | ||
121 | unsigned long cycles = 0; | ||
122 | int ret = 0; | ||
123 | int i; | ||
124 | |||
125 | local_irq_disable(); | ||
126 | |||
127 | /* Warm-up run. */ | ||
128 | for (i = 0; i < 4; i++) { | ||
129 | if (enc) | ||
130 | ret = crypto_blkcipher_encrypt(desc, sg, sg, blen); | ||
131 | else | ||
132 | ret = crypto_blkcipher_decrypt(desc, sg, sg, blen); | ||
133 | |||
134 | if (ret) | ||
135 | goto out; | ||
136 | } | ||
137 | |||
138 | /* The real thing. */ | ||
139 | for (i = 0; i < 8; i++) { | ||
140 | cycles_t start, end; | ||
141 | |||
142 | start = get_cycles(); | ||
143 | if (enc) | ||
144 | ret = crypto_blkcipher_encrypt(desc, sg, sg, blen); | ||
145 | else | ||
146 | ret = crypto_blkcipher_decrypt(desc, sg, sg, blen); | ||
147 | end = get_cycles(); | ||
148 | |||
149 | if (ret) | ||
150 | goto out; | ||
151 | |||
152 | cycles += end - start; | ||
153 | } | ||
154 | |||
155 | out: | ||
156 | local_irq_enable(); | ||
157 | |||
158 | if (ret == 0) | ||
159 | printk("1 operation in %lu cycles (%d bytes)\n", | ||
160 | (cycles + 4) / 8, blen); | ||
161 | |||
162 | return ret; | ||
163 | } | ||
164 | |||
165 | static inline int do_one_aead_op(struct aead_request *req, int ret) | 96 | static inline int do_one_aead_op(struct aead_request *req, int ret) |
166 | { | 97 | { |
167 | if (ret == -EINPROGRESS || ret == -EBUSY) { | 98 | if (ret == -EINPROGRESS || ret == -EBUSY) { |
@@ -455,106 +386,6 @@ out_noxbuf: | |||
455 | return; | 386 | return; |
456 | } | 387 | } |
457 | 388 | ||
458 | static void test_cipher_speed(const char *algo, int enc, unsigned int secs, | ||
459 | struct cipher_speed_template *template, | ||
460 | unsigned int tcount, u8 *keysize) | ||
461 | { | ||
462 | unsigned int ret, i, j, iv_len; | ||
463 | const char *key; | ||
464 | char iv[128]; | ||
465 | struct crypto_blkcipher *tfm; | ||
466 | struct blkcipher_desc desc; | ||
467 | const char *e; | ||
468 | u32 *b_size; | ||
469 | |||
470 | if (enc == ENCRYPT) | ||
471 | e = "encryption"; | ||
472 | else | ||
473 | e = "decryption"; | ||
474 | |||
475 | tfm = crypto_alloc_blkcipher(algo, 0, CRYPTO_ALG_ASYNC); | ||
476 | |||
477 | if (IS_ERR(tfm)) { | ||
478 | printk("failed to load transform for %s: %ld\n", algo, | ||
479 | PTR_ERR(tfm)); | ||
480 | return; | ||
481 | } | ||
482 | desc.tfm = tfm; | ||
483 | desc.flags = 0; | ||
484 | |||
485 | printk(KERN_INFO "\ntesting speed of %s (%s) %s\n", algo, | ||
486 | get_driver_name(crypto_blkcipher, tfm), e); | ||
487 | |||
488 | i = 0; | ||
489 | do { | ||
490 | |||
491 | b_size = block_sizes; | ||
492 | do { | ||
493 | struct scatterlist sg[TVMEMSIZE]; | ||
494 | |||
495 | if ((*keysize + *b_size) > TVMEMSIZE * PAGE_SIZE) { | ||
496 | printk("template (%u) too big for " | ||
497 | "tvmem (%lu)\n", *keysize + *b_size, | ||
498 | TVMEMSIZE * PAGE_SIZE); | ||
499 | goto out; | ||
500 | } | ||
501 | |||
502 | printk("test %u (%d bit key, %d byte blocks): ", i, | ||
503 | *keysize * 8, *b_size); | ||
504 | |||
505 | memset(tvmem[0], 0xff, PAGE_SIZE); | ||
506 | |||
507 | /* set key, plain text and IV */ | ||
508 | key = tvmem[0]; | ||
509 | for (j = 0; j < tcount; j++) { | ||
510 | if (template[j].klen == *keysize) { | ||
511 | key = template[j].key; | ||
512 | break; | ||
513 | } | ||
514 | } | ||
515 | |||
516 | ret = crypto_blkcipher_setkey(tfm, key, *keysize); | ||
517 | if (ret) { | ||
518 | printk("setkey() failed flags=%x\n", | ||
519 | crypto_blkcipher_get_flags(tfm)); | ||
520 | goto out; | ||
521 | } | ||
522 | |||
523 | sg_init_table(sg, TVMEMSIZE); | ||
524 | sg_set_buf(sg, tvmem[0] + *keysize, | ||
525 | PAGE_SIZE - *keysize); | ||
526 | for (j = 1; j < TVMEMSIZE; j++) { | ||
527 | sg_set_buf(sg + j, tvmem[j], PAGE_SIZE); | ||
528 | memset (tvmem[j], 0xff, PAGE_SIZE); | ||
529 | } | ||
530 | |||
531 | iv_len = crypto_blkcipher_ivsize(tfm); | ||
532 | if (iv_len) { | ||
533 | memset(&iv, 0xff, iv_len); | ||
534 | crypto_blkcipher_set_iv(tfm, iv, iv_len); | ||
535 | } | ||
536 | |||
537 | if (secs) | ||
538 | ret = test_cipher_jiffies(&desc, enc, sg, | ||
539 | *b_size, secs); | ||
540 | else | ||
541 | ret = test_cipher_cycles(&desc, enc, sg, | ||
542 | *b_size); | ||
543 | |||
544 | if (ret) { | ||
545 | printk("%s() failed flags=%x\n", e, desc.flags); | ||
546 | break; | ||
547 | } | ||
548 | b_size++; | ||
549 | i++; | ||
550 | } while (*b_size); | ||
551 | keysize++; | ||
552 | } while (*keysize); | ||
553 | |||
554 | out: | ||
555 | crypto_free_blkcipher(tfm); | ||
556 | } | ||
557 | |||
558 | static void test_hash_sg_init(struct scatterlist *sg) | 389 | static void test_hash_sg_init(struct scatterlist *sg) |
559 | { | 390 | { |
560 | int i; | 391 | int i; |
@@ -932,7 +763,7 @@ static void test_hash_speed(const char *algo, unsigned int secs, | |||
932 | return test_ahash_speed_common(algo, secs, speed, CRYPTO_ALG_ASYNC); | 763 | return test_ahash_speed_common(algo, secs, speed, CRYPTO_ALG_ASYNC); |
933 | } | 764 | } |
934 | 765 | ||
935 | static inline int do_one_acipher_op(struct ablkcipher_request *req, int ret) | 766 | static inline int do_one_acipher_op(struct skcipher_request *req, int ret) |
936 | { | 767 | { |
937 | if (ret == -EINPROGRESS || ret == -EBUSY) { | 768 | if (ret == -EINPROGRESS || ret == -EBUSY) { |
938 | struct tcrypt_result *tr = req->base.data; | 769 | struct tcrypt_result *tr = req->base.data; |
@@ -945,7 +776,7 @@ static inline int do_one_acipher_op(struct ablkcipher_request *req, int ret) | |||
945 | return ret; | 776 | return ret; |
946 | } | 777 | } |
947 | 778 | ||
948 | static int test_acipher_jiffies(struct ablkcipher_request *req, int enc, | 779 | static int test_acipher_jiffies(struct skcipher_request *req, int enc, |
949 | int blen, int secs) | 780 | int blen, int secs) |
950 | { | 781 | { |
951 | unsigned long start, end; | 782 | unsigned long start, end; |
@@ -956,10 +787,10 @@ static int test_acipher_jiffies(struct ablkcipher_request *req, int enc, | |||
956 | time_before(jiffies, end); bcount++) { | 787 | time_before(jiffies, end); bcount++) { |
957 | if (enc) | 788 | if (enc) |
958 | ret = do_one_acipher_op(req, | 789 | ret = do_one_acipher_op(req, |
959 | crypto_ablkcipher_encrypt(req)); | 790 | crypto_skcipher_encrypt(req)); |
960 | else | 791 | else |
961 | ret = do_one_acipher_op(req, | 792 | ret = do_one_acipher_op(req, |
962 | crypto_ablkcipher_decrypt(req)); | 793 | crypto_skcipher_decrypt(req)); |
963 | 794 | ||
964 | if (ret) | 795 | if (ret) |
965 | return ret; | 796 | return ret; |
@@ -970,7 +801,7 @@ static int test_acipher_jiffies(struct ablkcipher_request *req, int enc, | |||
970 | return 0; | 801 | return 0; |
971 | } | 802 | } |
972 | 803 | ||
973 | static int test_acipher_cycles(struct ablkcipher_request *req, int enc, | 804 | static int test_acipher_cycles(struct skcipher_request *req, int enc, |
974 | int blen) | 805 | int blen) |
975 | { | 806 | { |
976 | unsigned long cycles = 0; | 807 | unsigned long cycles = 0; |
@@ -981,10 +812,10 @@ static int test_acipher_cycles(struct ablkcipher_request *req, int enc, | |||
981 | for (i = 0; i < 4; i++) { | 812 | for (i = 0; i < 4; i++) { |
982 | if (enc) | 813 | if (enc) |
983 | ret = do_one_acipher_op(req, | 814 | ret = do_one_acipher_op(req, |
984 | crypto_ablkcipher_encrypt(req)); | 815 | crypto_skcipher_encrypt(req)); |
985 | else | 816 | else |
986 | ret = do_one_acipher_op(req, | 817 | ret = do_one_acipher_op(req, |
987 | crypto_ablkcipher_decrypt(req)); | 818 | crypto_skcipher_decrypt(req)); |
988 | 819 | ||
989 | if (ret) | 820 | if (ret) |
990 | goto out; | 821 | goto out; |
@@ -997,10 +828,10 @@ static int test_acipher_cycles(struct ablkcipher_request *req, int enc, | |||
997 | start = get_cycles(); | 828 | start = get_cycles(); |
998 | if (enc) | 829 | if (enc) |
999 | ret = do_one_acipher_op(req, | 830 | ret = do_one_acipher_op(req, |
1000 | crypto_ablkcipher_encrypt(req)); | 831 | crypto_skcipher_encrypt(req)); |
1001 | else | 832 | else |
1002 | ret = do_one_acipher_op(req, | 833 | ret = do_one_acipher_op(req, |
1003 | crypto_ablkcipher_decrypt(req)); | 834 | crypto_skcipher_decrypt(req)); |
1004 | end = get_cycles(); | 835 | end = get_cycles(); |
1005 | 836 | ||
1006 | if (ret) | 837 | if (ret) |
@@ -1017,16 +848,16 @@ out: | |||
1017 | return ret; | 848 | return ret; |
1018 | } | 849 | } |
1019 | 850 | ||
1020 | static void test_acipher_speed(const char *algo, int enc, unsigned int secs, | 851 | static void test_skcipher_speed(const char *algo, int enc, unsigned int secs, |
1021 | struct cipher_speed_template *template, | 852 | struct cipher_speed_template *template, |
1022 | unsigned int tcount, u8 *keysize) | 853 | unsigned int tcount, u8 *keysize, bool async) |
1023 | { | 854 | { |
1024 | unsigned int ret, i, j, k, iv_len; | 855 | unsigned int ret, i, j, k, iv_len; |
1025 | struct tcrypt_result tresult; | 856 | struct tcrypt_result tresult; |
1026 | const char *key; | 857 | const char *key; |
1027 | char iv[128]; | 858 | char iv[128]; |
1028 | struct ablkcipher_request *req; | 859 | struct skcipher_request *req; |
1029 | struct crypto_ablkcipher *tfm; | 860 | struct crypto_skcipher *tfm; |
1030 | const char *e; | 861 | const char *e; |
1031 | u32 *b_size; | 862 | u32 *b_size; |
1032 | 863 | ||
@@ -1037,7 +868,7 @@ static void test_acipher_speed(const char *algo, int enc, unsigned int secs, | |||
1037 | 868 | ||
1038 | init_completion(&tresult.completion); | 869 | init_completion(&tresult.completion); |
1039 | 870 | ||
1040 | tfm = crypto_alloc_ablkcipher(algo, 0, 0); | 871 | tfm = crypto_alloc_skcipher(algo, 0, async ? 0 : CRYPTO_ALG_ASYNC); |
1041 | 872 | ||
1042 | if (IS_ERR(tfm)) { | 873 | if (IS_ERR(tfm)) { |
1043 | pr_err("failed to load transform for %s: %ld\n", algo, | 874 | pr_err("failed to load transform for %s: %ld\n", algo, |
@@ -1046,17 +877,17 @@ static void test_acipher_speed(const char *algo, int enc, unsigned int secs, | |||
1046 | } | 877 | } |
1047 | 878 | ||
1048 | pr_info("\ntesting speed of async %s (%s) %s\n", algo, | 879 | pr_info("\ntesting speed of async %s (%s) %s\n", algo, |
1049 | get_driver_name(crypto_ablkcipher, tfm), e); | 880 | get_driver_name(crypto_skcipher, tfm), e); |
1050 | 881 | ||
1051 | req = ablkcipher_request_alloc(tfm, GFP_KERNEL); | 882 | req = skcipher_request_alloc(tfm, GFP_KERNEL); |
1052 | if (!req) { | 883 | if (!req) { |
1053 | pr_err("tcrypt: skcipher: Failed to allocate request for %s\n", | 884 | pr_err("tcrypt: skcipher: Failed to allocate request for %s\n", |
1054 | algo); | 885 | algo); |
1055 | goto out; | 886 | goto out; |
1056 | } | 887 | } |
1057 | 888 | ||
1058 | ablkcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, | 889 | skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_BACKLOG, |
1059 | tcrypt_complete, &tresult); | 890 | tcrypt_complete, &tresult); |
1060 | 891 | ||
1061 | i = 0; | 892 | i = 0; |
1062 | do { | 893 | do { |
@@ -1086,12 +917,12 @@ static void test_acipher_speed(const char *algo, int enc, unsigned int secs, | |||
1086 | } | 917 | } |
1087 | } | 918 | } |
1088 | 919 | ||
1089 | crypto_ablkcipher_clear_flags(tfm, ~0); | 920 | crypto_skcipher_clear_flags(tfm, ~0); |
1090 | 921 | ||
1091 | ret = crypto_ablkcipher_setkey(tfm, key, *keysize); | 922 | ret = crypto_skcipher_setkey(tfm, key, *keysize); |
1092 | if (ret) { | 923 | if (ret) { |
1093 | pr_err("setkey() failed flags=%x\n", | 924 | pr_err("setkey() failed flags=%x\n", |
1094 | crypto_ablkcipher_get_flags(tfm)); | 925 | crypto_skcipher_get_flags(tfm)); |
1095 | goto out_free_req; | 926 | goto out_free_req; |
1096 | } | 927 | } |
1097 | 928 | ||
@@ -1115,11 +946,11 @@ static void test_acipher_speed(const char *algo, int enc, unsigned int secs, | |||
1115 | sg_set_buf(sg, tvmem[0] + *keysize, *b_size); | 946 | sg_set_buf(sg, tvmem[0] + *keysize, *b_size); |
1116 | } | 947 | } |
1117 | 948 | ||
1118 | iv_len = crypto_ablkcipher_ivsize(tfm); | 949 | iv_len = crypto_skcipher_ivsize(tfm); |
1119 | if (iv_len) | 950 | if (iv_len) |
1120 | memset(&iv, 0xff, iv_len); | 951 | memset(&iv, 0xff, iv_len); |
1121 | 952 | ||
1122 | ablkcipher_request_set_crypt(req, sg, sg, *b_size, iv); | 953 | skcipher_request_set_crypt(req, sg, sg, *b_size, iv); |
1123 | 954 | ||
1124 | if (secs) | 955 | if (secs) |
1125 | ret = test_acipher_jiffies(req, enc, | 956 | ret = test_acipher_jiffies(req, enc, |
@@ -1130,7 +961,7 @@ static void test_acipher_speed(const char *algo, int enc, unsigned int secs, | |||
1130 | 961 | ||
1131 | if (ret) { | 962 | if (ret) { |
1132 | pr_err("%s() failed flags=%x\n", e, | 963 | pr_err("%s() failed flags=%x\n", e, |
1133 | crypto_ablkcipher_get_flags(tfm)); | 964 | crypto_skcipher_get_flags(tfm)); |
1134 | break; | 965 | break; |
1135 | } | 966 | } |
1136 | b_size++; | 967 | b_size++; |
@@ -1140,9 +971,25 @@ static void test_acipher_speed(const char *algo, int enc, unsigned int secs, | |||
1140 | } while (*keysize); | 971 | } while (*keysize); |
1141 | 972 | ||
1142 | out_free_req: | 973 | out_free_req: |
1143 | ablkcipher_request_free(req); | 974 | skcipher_request_free(req); |
1144 | out: | 975 | out: |
1145 | crypto_free_ablkcipher(tfm); | 976 | crypto_free_skcipher(tfm); |
977 | } | ||
978 | |||
979 | static void test_acipher_speed(const char *algo, int enc, unsigned int secs, | ||
980 | struct cipher_speed_template *template, | ||
981 | unsigned int tcount, u8 *keysize) | ||
982 | { | ||
983 | return test_skcipher_speed(algo, enc, secs, template, tcount, keysize, | ||
984 | true); | ||
985 | } | ||
986 | |||
987 | static void test_cipher_speed(const char *algo, int enc, unsigned int secs, | ||
988 | struct cipher_speed_template *template, | ||
989 | unsigned int tcount, u8 *keysize) | ||
990 | { | ||
991 | return test_skcipher_speed(algo, enc, secs, template, tcount, keysize, | ||
992 | false); | ||
1146 | } | 993 | } |
1147 | 994 | ||
1148 | static void test_available(void) | 995 | static void test_available(void) |