aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--crypto/Kconfig2
-rw-r--r--crypto/sha256_generic.c104
2 files changed, 57 insertions, 49 deletions
diff --git a/crypto/Kconfig b/crypto/Kconfig
index 989304ca3eab..3d04fa880e44 100644
--- a/crypto/Kconfig
+++ b/crypto/Kconfig
@@ -357,7 +357,7 @@ config CRYPTO_SHA1
357 357
358config CRYPTO_SHA256 358config CRYPTO_SHA256
359 tristate "SHA224 and SHA256 digest algorithm" 359 tristate "SHA224 and SHA256 digest algorithm"
360 select CRYPTO_ALGAPI 360 select CRYPTO_HASH
361 help 361 help
362 SHA256 secure hash standard (DFIPS 180-2). 362 SHA256 secure hash standard (DFIPS 180-2).
363 363
diff --git a/crypto/sha256_generic.c b/crypto/sha256_generic.c
index 5a8dd47558e5..caa3542e6ce8 100644
--- a/crypto/sha256_generic.c
+++ b/crypto/sha256_generic.c
@@ -17,10 +17,10 @@
17 * any later version. 17 * any later version.
18 * 18 *
19 */ 19 */
20#include <crypto/internal/hash.h>
20#include <linux/init.h> 21#include <linux/init.h>
21#include <linux/module.h> 22#include <linux/module.h>
22#include <linux/mm.h> 23#include <linux/mm.h>
23#include <linux/crypto.h>
24#include <linux/types.h> 24#include <linux/types.h>
25#include <crypto/sha.h> 25#include <crypto/sha.h>
26#include <asm/byteorder.h> 26#include <asm/byteorder.h>
@@ -69,7 +69,7 @@ static void sha256_transform(u32 *state, const u8 *input)
69 /* now blend */ 69 /* now blend */
70 for (i = 16; i < 64; i++) 70 for (i = 16; i < 64; i++)
71 BLEND_OP(i, W); 71 BLEND_OP(i, W);
72 72
73 /* load the state into our registers */ 73 /* load the state into our registers */
74 a=state[0]; b=state[1]; c=state[2]; d=state[3]; 74 a=state[0]; b=state[1]; c=state[2]; d=state[3];
75 e=state[4]; f=state[5]; g=state[6]; h=state[7]; 75 e=state[4]; f=state[5]; g=state[6]; h=state[7];
@@ -220,9 +220,9 @@ static void sha256_transform(u32 *state, const u8 *input)
220} 220}
221 221
222 222
223static void sha224_init(struct crypto_tfm *tfm) 223static int sha224_init(struct shash_desc *desc)
224{ 224{
225 struct sha256_ctx *sctx = crypto_tfm_ctx(tfm); 225 struct sha256_ctx *sctx = shash_desc_ctx(desc);
226 sctx->state[0] = SHA224_H0; 226 sctx->state[0] = SHA224_H0;
227 sctx->state[1] = SHA224_H1; 227 sctx->state[1] = SHA224_H1;
228 sctx->state[2] = SHA224_H2; 228 sctx->state[2] = SHA224_H2;
@@ -233,11 +233,13 @@ static void sha224_init(struct crypto_tfm *tfm)
233 sctx->state[7] = SHA224_H7; 233 sctx->state[7] = SHA224_H7;
234 sctx->count[0] = 0; 234 sctx->count[0] = 0;
235 sctx->count[1] = 0; 235 sctx->count[1] = 0;
236
237 return 0;
236} 238}
237 239
238static void sha256_init(struct crypto_tfm *tfm) 240static int sha256_init(struct shash_desc *desc)
239{ 241{
240 struct sha256_ctx *sctx = crypto_tfm_ctx(tfm); 242 struct sha256_ctx *sctx = shash_desc_ctx(desc);
241 sctx->state[0] = SHA256_H0; 243 sctx->state[0] = SHA256_H0;
242 sctx->state[1] = SHA256_H1; 244 sctx->state[1] = SHA256_H1;
243 sctx->state[2] = SHA256_H2; 245 sctx->state[2] = SHA256_H2;
@@ -247,12 +249,14 @@ static void sha256_init(struct crypto_tfm *tfm)
247 sctx->state[6] = SHA256_H6; 249 sctx->state[6] = SHA256_H6;
248 sctx->state[7] = SHA256_H7; 250 sctx->state[7] = SHA256_H7;
249 sctx->count[0] = sctx->count[1] = 0; 251 sctx->count[0] = sctx->count[1] = 0;
252
253 return 0;
250} 254}
251 255
252static void sha256_update(struct crypto_tfm *tfm, const u8 *data, 256static int sha256_update(struct shash_desc *desc, const u8 *data,
253 unsigned int len) 257 unsigned int len)
254{ 258{
255 struct sha256_ctx *sctx = crypto_tfm_ctx(tfm); 259 struct sha256_ctx *sctx = shash_desc_ctx(desc);
256 unsigned int i, index, part_len; 260 unsigned int i, index, part_len;
257 261
258 /* Compute number of bytes mod 128 */ 262 /* Compute number of bytes mod 128 */
@@ -277,14 +281,16 @@ static void sha256_update(struct crypto_tfm *tfm, const u8 *data,
277 } else { 281 } else {
278 i = 0; 282 i = 0;
279 } 283 }
280 284
281 /* Buffer remaining input */ 285 /* Buffer remaining input */
282 memcpy(&sctx->buf[index], &data[i], len-i); 286 memcpy(&sctx->buf[index], &data[i], len-i);
287
288 return 0;
283} 289}
284 290
285static void sha256_final(struct crypto_tfm *tfm, u8 *out) 291static int sha256_final(struct shash_desc *desc, u8 *out)
286{ 292{
287 struct sha256_ctx *sctx = crypto_tfm_ctx(tfm); 293 struct sha256_ctx *sctx = shash_desc_ctx(desc);
288 __be32 *dst = (__be32 *)out; 294 __be32 *dst = (__be32 *)out;
289 __be32 bits[2]; 295 __be32 bits[2];
290 unsigned int index, pad_len; 296 unsigned int index, pad_len;
@@ -298,10 +304,10 @@ static void sha256_final(struct crypto_tfm *tfm, u8 *out)
298 /* Pad out to 56 mod 64. */ 304 /* Pad out to 56 mod 64. */
299 index = (sctx->count[0] >> 3) & 0x3f; 305 index = (sctx->count[0] >> 3) & 0x3f;
300 pad_len = (index < 56) ? (56 - index) : ((64+56) - index); 306 pad_len = (index < 56) ? (56 - index) : ((64+56) - index);
301 sha256_update(tfm, padding, pad_len); 307 sha256_update(desc, padding, pad_len);
302 308
303 /* Append length (before padding) */ 309 /* Append length (before padding) */
304 sha256_update(tfm, (const u8 *)bits, sizeof(bits)); 310 sha256_update(desc, (const u8 *)bits, sizeof(bits));
305 311
306 /* Store state in digest */ 312 /* Store state in digest */
307 for (i = 0; i < 8; i++) 313 for (i = 0; i < 8; i++)
@@ -309,71 +315,73 @@ static void sha256_final(struct crypto_tfm *tfm, u8 *out)
309 315
310 /* Zeroize sensitive information. */ 316 /* Zeroize sensitive information. */
311 memset(sctx, 0, sizeof(*sctx)); 317 memset(sctx, 0, sizeof(*sctx));
318
319 return 0;
312} 320}
313 321
314static void sha224_final(struct crypto_tfm *tfm, u8 *hash) 322static int sha224_final(struct shash_desc *desc, u8 *hash)
315{ 323{
316 u8 D[SHA256_DIGEST_SIZE]; 324 u8 D[SHA256_DIGEST_SIZE];
317 325
318 sha256_final(tfm, D); 326 sha256_final(desc, D);
319 327
320 memcpy(hash, D, SHA224_DIGEST_SIZE); 328 memcpy(hash, D, SHA224_DIGEST_SIZE);
321 memset(D, 0, SHA256_DIGEST_SIZE); 329 memset(D, 0, SHA256_DIGEST_SIZE);
330
331 return 0;
322} 332}
323 333
324static struct crypto_alg sha256 = { 334static struct shash_alg sha256 = {
325 .cra_name = "sha256", 335 .digestsize = SHA256_DIGEST_SIZE,
326 .cra_driver_name= "sha256-generic", 336 .init = sha256_init,
327 .cra_flags = CRYPTO_ALG_TYPE_DIGEST, 337 .update = sha256_update,
328 .cra_blocksize = SHA256_BLOCK_SIZE, 338 .final = sha256_final,
329 .cra_ctxsize = sizeof(struct sha256_ctx), 339 .descsize = sizeof(struct sha256_ctx),
330 .cra_module = THIS_MODULE, 340 .base = {
331 .cra_alignmask = 3, 341 .cra_name = "sha256",
332 .cra_list = LIST_HEAD_INIT(sha256.cra_list), 342 .cra_driver_name= "sha256-generic",
333 .cra_u = { .digest = { 343 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
334 .dia_digestsize = SHA256_DIGEST_SIZE, 344 .cra_blocksize = SHA256_BLOCK_SIZE,
335 .dia_init = sha256_init, 345 .cra_module = THIS_MODULE,
336 .dia_update = sha256_update, 346 }
337 .dia_final = sha256_final } }
338}; 347};
339 348
340static struct crypto_alg sha224 = { 349static struct shash_alg sha224 = {
341 .cra_name = "sha224", 350 .digestsize = SHA224_DIGEST_SIZE,
342 .cra_driver_name = "sha224-generic", 351 .init = sha224_init,
343 .cra_flags = CRYPTO_ALG_TYPE_DIGEST, 352 .update = sha256_update,
344 .cra_blocksize = SHA224_BLOCK_SIZE, 353 .final = sha224_final,
345 .cra_ctxsize = sizeof(struct sha256_ctx), 354 .descsize = sizeof(struct sha256_ctx),
346 .cra_module = THIS_MODULE, 355 .base = {
347 .cra_alignmask = 3, 356 .cra_name = "sha224",
348 .cra_list = LIST_HEAD_INIT(sha224.cra_list), 357 .cra_driver_name= "sha224-generic",
349 .cra_u = { .digest = { 358 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
350 .dia_digestsize = SHA224_DIGEST_SIZE, 359 .cra_blocksize = SHA224_BLOCK_SIZE,
351 .dia_init = sha224_init, 360 .cra_module = THIS_MODULE,
352 .dia_update = sha256_update, 361 }
353 .dia_final = sha224_final } }
354}; 362};
355 363
356static int __init sha256_generic_mod_init(void) 364static int __init sha256_generic_mod_init(void)
357{ 365{
358 int ret = 0; 366 int ret = 0;
359 367
360 ret = crypto_register_alg(&sha224); 368 ret = crypto_register_shash(&sha224);
361 369
362 if (ret < 0) 370 if (ret < 0)
363 return ret; 371 return ret;
364 372
365 ret = crypto_register_alg(&sha256); 373 ret = crypto_register_shash(&sha256);
366 374
367 if (ret < 0) 375 if (ret < 0)
368 crypto_unregister_alg(&sha224); 376 crypto_unregister_shash(&sha224);
369 377
370 return ret; 378 return ret;
371} 379}
372 380
373static void __exit sha256_generic_mod_fini(void) 381static void __exit sha256_generic_mod_fini(void)
374{ 382{
375 crypto_unregister_alg(&sha224); 383 crypto_unregister_shash(&sha224);
376 crypto_unregister_alg(&sha256); 384 crypto_unregister_shash(&sha256);
377} 385}
378 386
379module_init(sha256_generic_mod_init); 387module_init(sha256_generic_mod_init);