aboutsummaryrefslogtreecommitdiffstats
path: root/arch/x86/crypto/crc32c-intel.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/x86/crypto/crc32c-intel.c')
-rw-r--r--arch/x86/crypto/crc32c-intel.c101
1 files changed, 47 insertions, 54 deletions
diff --git a/arch/x86/crypto/crc32c-intel.c b/arch/x86/crypto/crc32c-intel.c
index a2c539cc52b7..b9d00261703c 100644
--- a/arch/x86/crypto/crc32c-intel.c
+++ b/arch/x86/crypto/crc32c-intel.c
@@ -84,99 +84,92 @@ static u32 __pure crc32c_intel_le_hw(u32 crc, unsigned char const *p, size_t len
84 * If your algorithm starts with ~0, then XOR with ~0 before you set 84 * If your algorithm starts with ~0, then XOR with ~0 before you set
85 * the seed. 85 * the seed.
86 */ 86 */
87static int crc32c_intel_setkey(struct crypto_ahash *hash, const u8 *key, 87static int crc32c_intel_setkey(struct crypto_shash *hash, const u8 *key,
88 unsigned int keylen) 88 unsigned int keylen)
89{ 89{
90 u32 *mctx = crypto_ahash_ctx(hash); 90 u32 *mctx = crypto_shash_ctx(hash);
91 91
92 if (keylen != sizeof(u32)) { 92 if (keylen != sizeof(u32)) {
93 crypto_ahash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN); 93 crypto_shash_set_flags(hash, CRYPTO_TFM_RES_BAD_KEY_LEN);
94 return -EINVAL; 94 return -EINVAL;
95 } 95 }
96 *mctx = le32_to_cpup((__le32 *)key); 96 *mctx = le32_to_cpup((__le32 *)key);
97 return 0; 97 return 0;
98} 98}
99 99
100static int crc32c_intel_init(struct ahash_request *req) 100static int crc32c_intel_init(struct shash_desc *desc)
101{ 101{
102 u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); 102 u32 *mctx = crypto_shash_ctx(desc->tfm);
103 u32 *crcp = ahash_request_ctx(req); 103 u32 *crcp = shash_desc_ctx(desc);
104 104
105 *crcp = *mctx; 105 *crcp = *mctx;
106 106
107 return 0; 107 return 0;
108} 108}
109 109
110static int crc32c_intel_update(struct ahash_request *req) 110static int crc32c_intel_update(struct shash_desc *desc, const u8 *data,
111 unsigned int len)
111{ 112{
112 struct crypto_hash_walk walk; 113 u32 *crcp = shash_desc_ctx(desc);
113 u32 *crcp = ahash_request_ctx(req);
114 u32 crc = *crcp;
115 int nbytes;
116 114
117 for (nbytes = crypto_hash_walk_first(req, &walk); nbytes; 115 *crcp = crc32c_intel_le_hw(*crcp, data, len);
118 nbytes = crypto_hash_walk_done(&walk, 0))
119 crc = crc32c_intel_le_hw(crc, walk.data, nbytes);
120
121 *crcp = crc;
122 return 0; 116 return 0;
123} 117}
124 118
125static int crc32c_intel_final(struct ahash_request *req) 119static int __crc32c_intel_finup(u32 *crcp, const u8 *data, unsigned int len,
120 u8 *out)
126{ 121{
127 u32 *crcp = ahash_request_ctx(req); 122 *(__le32 *)out = ~cpu_to_le32(crc32c_intel_le_hw(*crcp, data, len));
128
129 *(__le32 *)req->result = ~cpu_to_le32p(crcp);
130 return 0; 123 return 0;
131} 124}
132 125
133static int crc32c_intel_digest(struct ahash_request *req) 126static int crc32c_intel_finup(struct shash_desc *desc, const u8 *data,
127 unsigned int len, u8 *out)
134{ 128{
135 struct crypto_hash_walk walk; 129 return __crc32c_intel_finup(shash_desc_ctx(desc), data, len, out);
136 u32 *mctx = crypto_ahash_ctx(crypto_ahash_reqtfm(req)); 130}
137 u32 crc = *mctx;
138 int nbytes;
139 131
140 for (nbytes = crypto_hash_walk_first(req, &walk); nbytes; 132static int crc32c_intel_final(struct shash_desc *desc, u8 *out)
141 nbytes = crypto_hash_walk_done(&walk, 0)) 133{
142 crc = crc32c_intel_le_hw(crc, walk.data, nbytes); 134 u32 *crcp = shash_desc_ctx(desc);
143 135
144 *(__le32 *)req->result = ~cpu_to_le32(crc); 136 *(__le32 *)out = ~cpu_to_le32p(crcp);
145 return 0; 137 return 0;
146} 138}
147 139
140static int crc32c_intel_digest(struct shash_desc *desc, const u8 *data,
141 unsigned int len, u8 *out)
142{
143 return __crc32c_intel_finup(crypto_shash_ctx(desc->tfm), data, len,
144 out);
145}
146
148static int crc32c_intel_cra_init(struct crypto_tfm *tfm) 147static int crc32c_intel_cra_init(struct crypto_tfm *tfm)
149{ 148{
150 u32 *key = crypto_tfm_ctx(tfm); 149 u32 *key = crypto_tfm_ctx(tfm);
151 150
152 *key = ~0; 151 *key = ~0;
153 152
154 tfm->crt_ahash.reqsize = sizeof(u32);
155
156 return 0; 153 return 0;
157} 154}
158 155
159static struct crypto_alg alg = { 156static struct shash_alg alg = {
160 .cra_name = "crc32c", 157 .setkey = crc32c_intel_setkey,
161 .cra_driver_name = "crc32c-intel", 158 .init = crc32c_intel_init,
162 .cra_priority = 200, 159 .update = crc32c_intel_update,
163 .cra_flags = CRYPTO_ALG_TYPE_AHASH, 160 .final = crc32c_intel_final,
164 .cra_blocksize = CHKSUM_BLOCK_SIZE, 161 .finup = crc32c_intel_finup,
165 .cra_alignmask = 3, 162 .digest = crc32c_intel_digest,
166 .cra_ctxsize = sizeof(u32), 163 .descsize = sizeof(u32),
167 .cra_module = THIS_MODULE, 164 .digestsize = CHKSUM_DIGEST_SIZE,
168 .cra_list = LIST_HEAD_INIT(alg.cra_list), 165 .base = {
169 .cra_init = crc32c_intel_cra_init, 166 .cra_name = "crc32c",
170 .cra_type = &crypto_ahash_type, 167 .cra_driver_name = "crc32c-intel",
171 .cra_u = { 168 .cra_priority = 200,
172 .ahash = { 169 .cra_blocksize = CHKSUM_BLOCK_SIZE,
173 .digestsize = CHKSUM_DIGEST_SIZE, 170 .cra_ctxsize = sizeof(u32),
174 .setkey = crc32c_intel_setkey, 171 .cra_module = THIS_MODULE,
175 .init = crc32c_intel_init, 172 .cra_init = crc32c_intel_cra_init,
176 .update = crc32c_intel_update,
177 .final = crc32c_intel_final,
178 .digest = crc32c_intel_digest,
179 }
180 } 173 }
181}; 174};
182 175
@@ -184,14 +177,14 @@ static struct crypto_alg alg = {
184static int __init crc32c_intel_mod_init(void) 177static int __init crc32c_intel_mod_init(void)
185{ 178{
186 if (cpu_has_xmm4_2) 179 if (cpu_has_xmm4_2)
187 return crypto_register_alg(&alg); 180 return crypto_register_shash(&alg);
188 else 181 else
189 return -ENODEV; 182 return -ENODEV;
190} 183}
191 184
192static void __exit crc32c_intel_mod_fini(void) 185static void __exit crc32c_intel_mod_fini(void)
193{ 186{
194 crypto_unregister_alg(&alg); 187 crypto_unregister_shash(&alg);
195} 188}
196 189
197module_init(crc32c_intel_mod_init); 190module_init(crc32c_intel_mod_init);