diff options
-rw-r--r-- | crypto/crc32c.c | 25 | ||||
-rw-r--r-- | crypto/tcrypt.c | 4 |
2 files changed, 19 insertions, 10 deletions
diff --git a/crypto/crc32c.c b/crypto/crc32c.c index f2660123aeb4..91ecd895e957 100644 --- a/crypto/crc32c.c +++ b/crypto/crc32c.c | |||
@@ -16,14 +16,14 @@ | |||
16 | #include <linux/string.h> | 16 | #include <linux/string.h> |
17 | #include <linux/crypto.h> | 17 | #include <linux/crypto.h> |
18 | #include <linux/crc32c.h> | 18 | #include <linux/crc32c.h> |
19 | #include <linux/types.h> | 19 | #include <linux/kernel.h> |
20 | #include <asm/byteorder.h> | ||
21 | 20 | ||
22 | #define CHKSUM_BLOCK_SIZE 32 | 21 | #define CHKSUM_BLOCK_SIZE 32 |
23 | #define CHKSUM_DIGEST_SIZE 4 | 22 | #define CHKSUM_DIGEST_SIZE 4 |
24 | 23 | ||
25 | struct chksum_ctx { | 24 | struct chksum_ctx { |
26 | u32 crc; | 25 | u32 crc; |
26 | u32 key; | ||
27 | }; | 27 | }; |
28 | 28 | ||
29 | /* | 29 | /* |
@@ -35,7 +35,7 @@ static void chksum_init(struct crypto_tfm *tfm) | |||
35 | { | 35 | { |
36 | struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); | 36 | struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); |
37 | 37 | ||
38 | mctx->crc = ~(u32)0; /* common usage */ | 38 | mctx->crc = mctx->key; |
39 | } | 39 | } |
40 | 40 | ||
41 | /* | 41 | /* |
@@ -53,7 +53,7 @@ static int chksum_setkey(struct crypto_tfm *tfm, const u8 *key, | |||
53 | *flags = CRYPTO_TFM_RES_BAD_KEY_LEN; | 53 | *flags = CRYPTO_TFM_RES_BAD_KEY_LEN; |
54 | return -EINVAL; | 54 | return -EINVAL; |
55 | } | 55 | } |
56 | mctx->crc = __cpu_to_le32(*(u32 *)key); | 56 | mctx->key = le32_to_cpu(*(__le32 *)key); |
57 | return 0; | 57 | return 0; |
58 | } | 58 | } |
59 | 59 | ||
@@ -61,19 +61,23 @@ static void chksum_update(struct crypto_tfm *tfm, const u8 *data, | |||
61 | unsigned int length) | 61 | unsigned int length) |
62 | { | 62 | { |
63 | struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); | 63 | struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); |
64 | u32 mcrc; | ||
65 | 64 | ||
66 | mcrc = crc32c(mctx->crc, data, (size_t)length); | 65 | mctx->crc = crc32c(mctx->crc, data, length); |
67 | |||
68 | mctx->crc = mcrc; | ||
69 | } | 66 | } |
70 | 67 | ||
71 | static void chksum_final(struct crypto_tfm *tfm, u8 *out) | 68 | static void chksum_final(struct crypto_tfm *tfm, u8 *out) |
72 | { | 69 | { |
73 | struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); | 70 | struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); |
74 | u32 mcrc = (mctx->crc ^ ~(u32)0); | ||
75 | 71 | ||
76 | *(u32 *)out = __le32_to_cpu(mcrc); | 72 | *(__le32 *)out = ~cpu_to_le32(mctx->crc); |
73 | } | ||
74 | |||
75 | static int crc32c_cra_init(struct crypto_tfm *tfm) | ||
76 | { | ||
77 | struct chksum_ctx *mctx = crypto_tfm_ctx(tfm); | ||
78 | |||
79 | mctx->key = ~0; | ||
80 | return 0; | ||
77 | } | 81 | } |
78 | 82 | ||
79 | static struct crypto_alg alg = { | 83 | static struct crypto_alg alg = { |
@@ -83,6 +87,7 @@ static struct crypto_alg alg = { | |||
83 | .cra_ctxsize = sizeof(struct chksum_ctx), | 87 | .cra_ctxsize = sizeof(struct chksum_ctx), |
84 | .cra_module = THIS_MODULE, | 88 | .cra_module = THIS_MODULE, |
85 | .cra_list = LIST_HEAD_INIT(alg.cra_list), | 89 | .cra_list = LIST_HEAD_INIT(alg.cra_list), |
90 | .cra_init = crc32c_cra_init, | ||
86 | .cra_u = { | 91 | .cra_u = { |
87 | .digest = { | 92 | .digest = { |
88 | .dia_digestsize= CHKSUM_DIGEST_SIZE, | 93 | .dia_digestsize= CHKSUM_DIGEST_SIZE, |
diff --git a/crypto/tcrypt.c b/crypto/tcrypt.c index e52f56c5bd5e..bed225e83231 100644 --- a/crypto/tcrypt.c +++ b/crypto/tcrypt.c | |||
@@ -810,6 +810,7 @@ static void test_crc32c(void) | |||
810 | 810 | ||
811 | seed = SEEDTESTVAL; | 811 | seed = SEEDTESTVAL; |
812 | (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32)); | 812 | (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32)); |
813 | crypto_digest_init(tfm); | ||
813 | crypto_digest_final(tfm, (u8*)&crc); | 814 | crypto_digest_final(tfm, (u8*)&crc); |
814 | printk("testing crc32c setkey returns %08x : %s\n", crc, (crc == (SEEDTESTVAL ^ ~(u32)0)) ? | 815 | printk("testing crc32c setkey returns %08x : %s\n", crc, (crc == (SEEDTESTVAL ^ ~(u32)0)) ? |
815 | "pass" : "ERROR"); | 816 | "pass" : "ERROR"); |
@@ -821,6 +822,7 @@ static void test_crc32c(void) | |||
821 | for (i = 0; i < NUMVEC; i++) { | 822 | for (i = 0; i < NUMVEC; i++) { |
822 | seed = ~(u32)0; | 823 | seed = ~(u32)0; |
823 | (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32)); | 824 | (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32)); |
825 | crypto_digest_init(tfm); | ||
824 | crypto_digest_update(tfm, &sg[i], 1); | 826 | crypto_digest_update(tfm, &sg[i], 1); |
825 | crypto_digest_final(tfm, (u8*)&crc); | 827 | crypto_digest_final(tfm, (u8*)&crc); |
826 | if (crc == vec_results[i]) { | 828 | if (crc == vec_results[i]) { |
@@ -836,6 +838,7 @@ static void test_crc32c(void) | |||
836 | for (i = 0; i < NUMVEC; i++) { | 838 | for (i = 0; i < NUMVEC; i++) { |
837 | seed = (crc ^ ~(u32)0); | 839 | seed = (crc ^ ~(u32)0); |
838 | (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32)); | 840 | (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32)); |
841 | crypto_digest_init(tfm); | ||
839 | crypto_digest_update(tfm, &sg[i], 1); | 842 | crypto_digest_update(tfm, &sg[i], 1); |
840 | crypto_digest_final(tfm, (u8*)&crc); | 843 | crypto_digest_final(tfm, (u8*)&crc); |
841 | } | 844 | } |
@@ -849,6 +852,7 @@ static void test_crc32c(void) | |||
849 | printk("\ntesting crc32c using digest:\n"); | 852 | printk("\ntesting crc32c using digest:\n"); |
850 | seed = ~(u32)0; | 853 | seed = ~(u32)0; |
851 | (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32)); | 854 | (void)crypto_digest_setkey(tfm, (const u8*)&seed, sizeof(u32)); |
855 | crypto_digest_init(tfm); | ||
852 | crypto_digest_digest(tfm, sg, NUMVEC, (u8*)&crc); | 856 | crypto_digest_digest(tfm, sg, NUMVEC, (u8*)&crc); |
853 | if (crc == tot_vec_results) { | 857 | if (crc == tot_vec_results) { |
854 | printk(" %08x:OK", crc); | 858 | printk(" %08x:OK", crc); |