xref: /openbmc/linux/crypto/sha1_generic.c (revision c4741b23059794bd99beef0f700103b0d983b3fd)
1ad5d2789SSebastian Siewior /*
2ad5d2789SSebastian Siewior  * Cryptographic API.
3ad5d2789SSebastian Siewior  *
4ad5d2789SSebastian Siewior  * SHA1 Secure Hash Algorithm.
5ad5d2789SSebastian Siewior  *
6ad5d2789SSebastian Siewior  * Derived from cryptoapi implementation, adapted for in-place
7ad5d2789SSebastian Siewior  * scatterlist interface.
8ad5d2789SSebastian Siewior  *
9ad5d2789SSebastian Siewior  * Copyright (c) Alan Smithee.
10ad5d2789SSebastian Siewior  * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
11ad5d2789SSebastian Siewior  * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
12ad5d2789SSebastian Siewior  *
13ad5d2789SSebastian Siewior  * This program is free software; you can redistribute it and/or modify it
14ad5d2789SSebastian Siewior  * under the terms of the GNU General Public License as published by the Free
15ad5d2789SSebastian Siewior  * Software Foundation; either version 2 of the License, or (at your option)
16ad5d2789SSebastian Siewior  * any later version.
17ad5d2789SSebastian Siewior  *
18ad5d2789SSebastian Siewior  */
1954ccb367SAdrian-Ken Rueegsegger #include <crypto/internal/hash.h>
20ad5d2789SSebastian Siewior #include <linux/init.h>
21ad5d2789SSebastian Siewior #include <linux/module.h>
22ad5d2789SSebastian Siewior #include <linux/mm.h>
23ad5d2789SSebastian Siewior #include <linux/cryptohash.h>
24ad5d2789SSebastian Siewior #include <linux/types.h>
255265eeb2SJan Glauber #include <crypto/sha.h>
267c71f0f7SArd Biesheuvel #include <crypto/sha1_base.h>
27ad5d2789SSebastian Siewior #include <asm/byteorder.h>
28ad5d2789SSebastian Siewior 
290c4c78deSLABBE Corentin const u8 sha1_zero_message_hash[SHA1_DIGEST_SIZE] = {
300c4c78deSLABBE Corentin 	0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d,
310c4c78deSLABBE Corentin 	0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
320c4c78deSLABBE Corentin 	0xaf, 0xd8, 0x07, 0x09
330c4c78deSLABBE Corentin };
340c4c78deSLABBE Corentin EXPORT_SYMBOL_GPL(sha1_zero_message_hash);
350c4c78deSLABBE Corentin 
367c71f0f7SArd Biesheuvel static void sha1_generic_block_fn(struct sha1_state *sst, u8 const *src,
377c71f0f7SArd Biesheuvel 				  int blocks)
38ad5d2789SSebastian Siewior {
397c71f0f7SArd Biesheuvel 	u32 temp[SHA_WORKSPACE_WORDS];
4054ccb367SAdrian-Ken Rueegsegger 
417c71f0f7SArd Biesheuvel 	while (blocks--) {
427c71f0f7SArd Biesheuvel 		sha_transform(sst->state, src, temp);
437c71f0f7SArd Biesheuvel 		src += SHA1_BLOCK_SIZE;
447c71f0f7SArd Biesheuvel 	}
457c71f0f7SArd Biesheuvel 	memzero_explicit(temp, sizeof(temp));
46ad5d2789SSebastian Siewior }
47ad5d2789SSebastian Siewior 
487c390170SMathias Krause int crypto_sha1_update(struct shash_desc *desc, const u8 *data,
49ad5d2789SSebastian Siewior 		       unsigned int len)
50ad5d2789SSebastian Siewior {
517c71f0f7SArd Biesheuvel 	return sha1_base_do_update(desc, data, len, sha1_generic_block_fn);
52ad5d2789SSebastian Siewior }
537c390170SMathias Krause EXPORT_SYMBOL(crypto_sha1_update);
54ad5d2789SSebastian Siewior 
5554ccb367SAdrian-Ken Rueegsegger static int sha1_final(struct shash_desc *desc, u8 *out)
56ad5d2789SSebastian Siewior {
577c71f0f7SArd Biesheuvel 	sha1_base_do_finalize(desc, sha1_generic_block_fn);
587c71f0f7SArd Biesheuvel 	return sha1_base_finish(desc, out);
59ad5d2789SSebastian Siewior }
60ad5d2789SSebastian Siewior 
617c71f0f7SArd Biesheuvel int crypto_sha1_finup(struct shash_desc *desc, const u8 *data,
627c71f0f7SArd Biesheuvel 		      unsigned int len, u8 *out)
63e2a7ce4eSHerbert Xu {
647c71f0f7SArd Biesheuvel 	sha1_base_do_update(desc, data, len, sha1_generic_block_fn);
657c71f0f7SArd Biesheuvel 	return sha1_final(desc, out);
66e2a7ce4eSHerbert Xu }
677c71f0f7SArd Biesheuvel EXPORT_SYMBOL(crypto_sha1_finup);
68e2a7ce4eSHerbert Xu 
6954ccb367SAdrian-Ken Rueegsegger static struct shash_alg alg = {
7054ccb367SAdrian-Ken Rueegsegger 	.digestsize	=	SHA1_DIGEST_SIZE,
717c71f0f7SArd Biesheuvel 	.init		=	sha1_base_init,
727c390170SMathias Krause 	.update		=	crypto_sha1_update,
7354ccb367SAdrian-Ken Rueegsegger 	.final		=	sha1_final,
747c71f0f7SArd Biesheuvel 	.finup		=	crypto_sha1_finup,
75e2a7ce4eSHerbert Xu 	.descsize	=	sizeof(struct sha1_state),
7654ccb367SAdrian-Ken Rueegsegger 	.base		=	{
77ad5d2789SSebastian Siewior 		.cra_name	=	"sha1",
78ad5d2789SSebastian Siewior 		.cra_driver_name=	"sha1-generic",
7990ef3e48SEric Biggers 		.cra_priority	=	100,
805265eeb2SJan Glauber 		.cra_blocksize	=	SHA1_BLOCK_SIZE,
81ad5d2789SSebastian Siewior 		.cra_module	=	THIS_MODULE,
8254ccb367SAdrian-Ken Rueegsegger 	}
83ad5d2789SSebastian Siewior };
84ad5d2789SSebastian Siewior 
853af5b90bSKamalesh Babulal static int __init sha1_generic_mod_init(void)
86ad5d2789SSebastian Siewior {
8754ccb367SAdrian-Ken Rueegsegger 	return crypto_register_shash(&alg);
88ad5d2789SSebastian Siewior }
89ad5d2789SSebastian Siewior 
903af5b90bSKamalesh Babulal static void __exit sha1_generic_mod_fini(void)
91ad5d2789SSebastian Siewior {
9254ccb367SAdrian-Ken Rueegsegger 	crypto_unregister_shash(&alg);
93ad5d2789SSebastian Siewior }
94ad5d2789SSebastian Siewior 
95*c4741b23SEric Biggers subsys_initcall(sha1_generic_mod_init);
963af5b90bSKamalesh Babulal module_exit(sha1_generic_mod_fini);
97ad5d2789SSebastian Siewior 
98ad5d2789SSebastian Siewior MODULE_LICENSE("GPL");
99ad5d2789SSebastian Siewior MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
100ad5d2789SSebastian Siewior 
1015d26a105SKees Cook MODULE_ALIAS_CRYPTO("sha1");
1023e14dcf7SMathias Krause MODULE_ALIAS_CRYPTO("sha1-generic");
103