^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Cryptographic API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * SHA1 Secure Hash Algorithm.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Derived from cryptoapi implementation, adapted for in-place
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * scatterlist interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (c) Alan Smithee.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <crypto/sha.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <crypto/sha1_base.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/byteorder.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) const u8 sha1_zero_message_hash[SHA1_DIGEST_SIZE] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) 0xaf, 0xd8, 0x07, 0x09
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) EXPORT_SYMBOL_GPL(sha1_zero_message_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static void sha1_generic_block_fn(struct sha1_state *sst, u8 const *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) int blocks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) u32 temp[SHA1_WORKSPACE_WORDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) while (blocks--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) sha1_transform(sst->state, src, temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) src += SHA1_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) memzero_explicit(temp, sizeof(temp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) int crypto_sha1_update(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return sha1_base_do_update(desc, data, len, sha1_generic_block_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) EXPORT_SYMBOL(crypto_sha1_update);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int sha1_final(struct shash_desc *desc, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) sha1_base_do_finalize(desc, sha1_generic_block_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return sha1_base_finish(desc, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) int crypto_sha1_finup(struct shash_desc *desc, const u8 *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) unsigned int len, u8 *out)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) sha1_base_do_update(desc, data, len, sha1_generic_block_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) return sha1_final(desc, out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) EXPORT_SYMBOL(crypto_sha1_finup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) static struct shash_alg alg = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) .digestsize = SHA1_DIGEST_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) .init = sha1_base_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) .update = crypto_sha1_update,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) .final = sha1_final,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) .finup = crypto_sha1_finup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) .descsize = sizeof(struct sha1_state),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) .base = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) .cra_name = "sha1",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) .cra_driver_name= "sha1-generic",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) .cra_priority = 100,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) .cra_blocksize = SHA1_BLOCK_SIZE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) .cra_module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static int __init sha1_generic_mod_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return crypto_register_shash(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) static void __exit sha1_generic_mod_fini(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) crypto_unregister_shash(&alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) subsys_initcall(sha1_generic_mod_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) module_exit(sha1_generic_mod_fini);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) MODULE_ALIAS_CRYPTO("sha1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) MODULE_ALIAS_CRYPTO("sha1-generic");