Commit bd9d20dba182ce4541b16b083eccd30fb252b9f4

Authored by Adrian-Ken Rueegsegger
Committed by Herbert Xu
1 parent f9e2bca6c2

crypto: sha512 - Switch to shash

This patch changes sha512 and sha384 to the new shash interface.

Signed-off-by: Adrian-Ken Rueegsegger <ken@codelabs.ch>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Showing 2 changed files with 60 additions and 54 deletions Side-by-side Diff

... ... @@ -369,7 +369,7 @@
369 369  
370 370 config CRYPTO_SHA512
371 371 tristate "SHA384 and SHA512 digest algorithms"
372   - select CRYPTO_ALGAPI
  372 + select CRYPTO_HASH
373 373 help
374 374 SHA512 secure hash standard (DFIPS 180-2).
375 375  
crypto/sha512_generic.c
... ... @@ -10,7 +10,7 @@
10 10 * later version.
11 11 *
12 12 */
13   -
  13 +#include <crypto/internal/hash.h>
14 14 #include <linux/kernel.h>
15 15 #include <linux/module.h>
16 16 #include <linux/mm.h>
17 17  
... ... @@ -138,10 +138,10 @@
138 138 put_cpu_var(msg_schedule);
139 139 }
140 140  
141   -static void
142   -sha512_init(struct crypto_tfm *tfm)
  141 +static int
  142 +sha512_init(struct shash_desc *desc)
143 143 {
144   - struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
  144 + struct sha512_ctx *sctx = shash_desc_ctx(desc);
145 145 sctx->state[0] = SHA512_H0;
146 146 sctx->state[1] = SHA512_H1;
147 147 sctx->state[2] = SHA512_H2;
148 148  
149 149  
... ... @@ -151,12 +151,14 @@
151 151 sctx->state[6] = SHA512_H6;
152 152 sctx->state[7] = SHA512_H7;
153 153 sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0;
  154 +
  155 + return 0;
154 156 }
155 157  
156   -static void
157   -sha384_init(struct crypto_tfm *tfm)
  158 +static int
  159 +sha384_init(struct shash_desc *desc)
158 160 {
159   - struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
  161 + struct sha512_ctx *sctx = shash_desc_ctx(desc);
160 162 sctx->state[0] = SHA384_H0;
161 163 sctx->state[1] = SHA384_H1;
162 164 sctx->state[2] = SHA384_H2;
163 165  
164 166  
... ... @@ -166,12 +168,14 @@
166 168 sctx->state[6] = SHA384_H6;
167 169 sctx->state[7] = SHA384_H7;
168 170 sctx->count[0] = sctx->count[1] = sctx->count[2] = sctx->count[3] = 0;
  171 +
  172 + return 0;
169 173 }
170 174  
171   -static void
172   -sha512_update(struct crypto_tfm *tfm, const u8 *data, unsigned int len)
  175 +static int
  176 +sha512_update(struct shash_desc *desc, const u8 *data, unsigned int len)
173 177 {
174   - struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
  178 + struct sha512_ctx *sctx = shash_desc_ctx(desc);
175 179  
176 180 unsigned int i, index, part_len;
177 181  
178 182  
179 183  
... ... @@ -203,12 +207,14 @@
203 207  
204 208 /* Buffer remaining input */
205 209 memcpy(&sctx->buf[index], &data[i], len - i);
  210 +
  211 + return 0;
206 212 }
207 213  
208   -static void
209   -sha512_final(struct crypto_tfm *tfm, u8 *hash)
  214 +static int
  215 +sha512_final(struct shash_desc *desc, u8 *hash)
210 216 {
211   - struct sha512_ctx *sctx = crypto_tfm_ctx(tfm);
  217 + struct sha512_ctx *sctx = shash_desc_ctx(desc);
212 218 static u8 padding[128] = { 0x80, };
213 219 __be64 *dst = (__be64 *)hash;
214 220 __be32 bits[4];
215 221  
... ... @@ -224,10 +230,10 @@
224 230 /* Pad out to 112 mod 128. */
225 231 index = (sctx->count[0] >> 3) & 0x7f;
226 232 pad_len = (index < 112) ? (112 - index) : ((128+112) - index);
227   - sha512_update(tfm, padding, pad_len);
  233 + sha512_update(desc, padding, pad_len);
228 234  
229 235 /* Append length (before padding) */
230   - sha512_update(tfm, (const u8 *)bits, sizeof(bits));
  236 + sha512_update(desc, (const u8 *)bits, sizeof(bits));
231 237  
232 238 /* Store state in digest */
233 239 for (i = 0; i < 8; i++)
234 240  
235 241  
236 242  
237 243  
238 244  
239 245  
240 246  
241 247  
242 248  
... ... @@ -235,66 +241,66 @@
235 241  
236 242 /* Zeroize sensitive information. */
237 243 memset(sctx, 0, sizeof(struct sha512_ctx));
  244 +
  245 + return 0;
238 246 }
239 247  
240   -static void sha384_final(struct crypto_tfm *tfm, u8 *hash)
  248 +static int sha384_final(struct shash_desc *desc, u8 *hash)
241 249 {
242   - u8 D[64];
  250 + u8 D[64];
243 251  
244   - sha512_final(tfm, D);
  252 + sha512_final(desc, D);
245 253  
246   - memcpy(hash, D, 48);
247   - memset(D, 0, 64);
  254 + memcpy(hash, D, 48);
  255 + memset(D, 0, 64);
  256 +
  257 + return 0;
248 258 }
249 259  
250   -static struct crypto_alg sha512 = {
251   - .cra_name = "sha512",
252   - .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
253   - .cra_blocksize = SHA512_BLOCK_SIZE,
254   - .cra_ctxsize = sizeof(struct sha512_ctx),
255   - .cra_module = THIS_MODULE,
256   - .cra_alignmask = 3,
257   - .cra_list = LIST_HEAD_INIT(sha512.cra_list),
258   - .cra_u = { .digest = {
259   - .dia_digestsize = SHA512_DIGEST_SIZE,
260   - .dia_init = sha512_init,
261   - .dia_update = sha512_update,
262   - .dia_final = sha512_final }
263   - }
  260 +static struct shash_alg sha512 = {
  261 + .digestsize = SHA512_DIGEST_SIZE,
  262 + .init = sha512_init,
  263 + .update = sha512_update,
  264 + .final = sha512_final,
  265 + .descsize = sizeof(struct sha512_ctx),
  266 + .base = {
  267 + .cra_name = "sha512",
  268 + .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  269 + .cra_blocksize = SHA512_BLOCK_SIZE,
  270 + .cra_module = THIS_MODULE,
  271 + }
264 272 };
265 273  
266   -static struct crypto_alg sha384 = {
267   - .cra_name = "sha384",
268   - .cra_flags = CRYPTO_ALG_TYPE_DIGEST,
269   - .cra_blocksize = SHA384_BLOCK_SIZE,
270   - .cra_ctxsize = sizeof(struct sha512_ctx),
271   - .cra_alignmask = 3,
272   - .cra_module = THIS_MODULE,
273   - .cra_list = LIST_HEAD_INIT(sha384.cra_list),
274   - .cra_u = { .digest = {
275   - .dia_digestsize = SHA384_DIGEST_SIZE,
276   - .dia_init = sha384_init,
277   - .dia_update = sha512_update,
278   - .dia_final = sha384_final }
279   - }
  274 +static struct shash_alg sha384 = {
  275 + .digestsize = SHA384_DIGEST_SIZE,
  276 + .init = sha384_init,
  277 + .update = sha512_update,
  278 + .final = sha384_final,
  279 + .descsize = sizeof(struct sha512_ctx),
  280 + .base = {
  281 + .cra_name = "sha384",
  282 + .cra_flags = CRYPTO_ALG_TYPE_SHASH,
  283 + .cra_blocksize = SHA384_BLOCK_SIZE,
  284 + .cra_module = THIS_MODULE,
  285 + }
280 286 };
281 287  
282 288 static int __init sha512_generic_mod_init(void)
283 289 {
284 290 int ret = 0;
285 291  
286   - if ((ret = crypto_register_alg(&sha384)) < 0)
  292 + if ((ret = crypto_register_shash(&sha384)) < 0)
287 293 goto out;
288   - if ((ret = crypto_register_alg(&sha512)) < 0)
289   - crypto_unregister_alg(&sha384);
  294 + if ((ret = crypto_register_shash(&sha512)) < 0)
  295 + crypto_unregister_shash(&sha384);
290 296 out:
291 297 return ret;
292 298 }
293 299  
294 300 static void __exit sha512_generic_mod_fini(void)
295 301 {
296   - crypto_unregister_alg(&sha384);
297   - crypto_unregister_alg(&sha512);
  302 + crypto_unregister_shash(&sha384);
  303 + crypto_unregister_shash(&sha512);
298 304 }
299 305  
300 306 module_init(sha512_generic_mod_init);