12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2ad5d2789SSebastian Siewior /*
3ad5d2789SSebastian Siewior * Cryptographic API.
4ad5d2789SSebastian Siewior *
5ad5d2789SSebastian Siewior * SHA1 Secure Hash Algorithm.
6ad5d2789SSebastian Siewior *
7ad5d2789SSebastian Siewior * Derived from cryptoapi implementation, adapted for in-place
8ad5d2789SSebastian Siewior * scatterlist interface.
9ad5d2789SSebastian Siewior *
10ad5d2789SSebastian Siewior * Copyright (c) Alan Smithee.
11ad5d2789SSebastian Siewior * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
12ad5d2789SSebastian Siewior * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
13ad5d2789SSebastian Siewior */
1454ccb367SAdrian-Ken Rueegsegger #include <crypto/internal/hash.h>
15ad5d2789SSebastian Siewior #include <linux/init.h>
16ad5d2789SSebastian Siewior #include <linux/module.h>
17ad5d2789SSebastian Siewior #include <linux/mm.h>
18ad5d2789SSebastian Siewior #include <linux/types.h>
19*a24d22b2SEric Biggers #include <crypto/sha1.h>
207c71f0f7SArd Biesheuvel #include <crypto/sha1_base.h>
21ad5d2789SSebastian Siewior #include <asm/byteorder.h>
22ad5d2789SSebastian Siewior
230c4c78deSLABBE Corentin const u8 sha1_zero_message_hash[SHA1_DIGEST_SIZE] = {
240c4c78deSLABBE Corentin 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d,
250c4c78deSLABBE Corentin 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
260c4c78deSLABBE Corentin 0xaf, 0xd8, 0x07, 0x09
270c4c78deSLABBE Corentin };
280c4c78deSLABBE Corentin EXPORT_SYMBOL_GPL(sha1_zero_message_hash);
290c4c78deSLABBE Corentin
sha1_generic_block_fn(struct sha1_state * sst,u8 const * src,int blocks)307c71f0f7SArd Biesheuvel static void sha1_generic_block_fn(struct sha1_state *sst, u8 const *src,
317c71f0f7SArd Biesheuvel int blocks)
32ad5d2789SSebastian Siewior {
336b0b0fa2SEric Biggers u32 temp[SHA1_WORKSPACE_WORDS];
3454ccb367SAdrian-Ken Rueegsegger
357c71f0f7SArd Biesheuvel while (blocks--) {
366b0b0fa2SEric Biggers sha1_transform(sst->state, src, temp);
377c71f0f7SArd Biesheuvel src += SHA1_BLOCK_SIZE;
387c71f0f7SArd Biesheuvel }
397c71f0f7SArd Biesheuvel memzero_explicit(temp, sizeof(temp));
40ad5d2789SSebastian Siewior }
41ad5d2789SSebastian Siewior
crypto_sha1_update(struct shash_desc * desc,const u8 * data,unsigned int len)427c390170SMathias Krause int crypto_sha1_update(struct shash_desc *desc, const u8 *data,
43ad5d2789SSebastian Siewior unsigned int len)
44ad5d2789SSebastian Siewior {
457c71f0f7SArd Biesheuvel return sha1_base_do_update(desc, data, len, sha1_generic_block_fn);
46ad5d2789SSebastian Siewior }
477c390170SMathias Krause EXPORT_SYMBOL(crypto_sha1_update);
48ad5d2789SSebastian Siewior
sha1_final(struct shash_desc * desc,u8 * out)4954ccb367SAdrian-Ken Rueegsegger static int sha1_final(struct shash_desc *desc, u8 *out)
50ad5d2789SSebastian Siewior {
517c71f0f7SArd Biesheuvel sha1_base_do_finalize(desc, sha1_generic_block_fn);
527c71f0f7SArd Biesheuvel return sha1_base_finish(desc, out);
53ad5d2789SSebastian Siewior }
54ad5d2789SSebastian Siewior
crypto_sha1_finup(struct shash_desc * desc,const u8 * data,unsigned int len,u8 * out)557c71f0f7SArd Biesheuvel int crypto_sha1_finup(struct shash_desc *desc, const u8 *data,
567c71f0f7SArd Biesheuvel unsigned int len, u8 *out)
57e2a7ce4eSHerbert Xu {
587c71f0f7SArd Biesheuvel sha1_base_do_update(desc, data, len, sha1_generic_block_fn);
597c71f0f7SArd Biesheuvel return sha1_final(desc, out);
60e2a7ce4eSHerbert Xu }
617c71f0f7SArd Biesheuvel EXPORT_SYMBOL(crypto_sha1_finup);
62e2a7ce4eSHerbert Xu
6354ccb367SAdrian-Ken Rueegsegger static struct shash_alg alg = {
6454ccb367SAdrian-Ken Rueegsegger .digestsize = SHA1_DIGEST_SIZE,
657c71f0f7SArd Biesheuvel .init = sha1_base_init,
667c390170SMathias Krause .update = crypto_sha1_update,
6754ccb367SAdrian-Ken Rueegsegger .final = sha1_final,
687c71f0f7SArd Biesheuvel .finup = crypto_sha1_finup,
69e2a7ce4eSHerbert Xu .descsize = sizeof(struct sha1_state),
7054ccb367SAdrian-Ken Rueegsegger .base = {
71ad5d2789SSebastian Siewior .cra_name = "sha1",
72ad5d2789SSebastian Siewior .cra_driver_name= "sha1-generic",
7390ef3e48SEric Biggers .cra_priority = 100,
745265eeb2SJan Glauber .cra_blocksize = SHA1_BLOCK_SIZE,
75ad5d2789SSebastian Siewior .cra_module = THIS_MODULE,
7654ccb367SAdrian-Ken Rueegsegger }
77ad5d2789SSebastian Siewior };
78ad5d2789SSebastian Siewior
sha1_generic_mod_init(void)793af5b90bSKamalesh Babulal static int __init sha1_generic_mod_init(void)
80ad5d2789SSebastian Siewior {
8154ccb367SAdrian-Ken Rueegsegger return crypto_register_shash(&alg);
82ad5d2789SSebastian Siewior }
83ad5d2789SSebastian Siewior
sha1_generic_mod_fini(void)843af5b90bSKamalesh Babulal static void __exit sha1_generic_mod_fini(void)
85ad5d2789SSebastian Siewior {
8654ccb367SAdrian-Ken Rueegsegger crypto_unregister_shash(&alg);
87ad5d2789SSebastian Siewior }
88ad5d2789SSebastian Siewior
89c4741b23SEric Biggers subsys_initcall(sha1_generic_mod_init);
903af5b90bSKamalesh Babulal module_exit(sha1_generic_mod_fini);
91ad5d2789SSebastian Siewior
92ad5d2789SSebastian Siewior MODULE_LICENSE("GPL");
93ad5d2789SSebastian Siewior MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
94ad5d2789SSebastian Siewior
955d26a105SKees Cook MODULE_ALIAS_CRYPTO("sha1");
963e14dcf7SMathias Krause MODULE_ALIAS_CRYPTO("sha1-generic");
97