blob: 1d43472fecbde1967d0a1b26e814329c65c67ffb [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Cryptographic API.
4 *
5 * SHA1 Secure Hash Algorithm.
6 *
7 * Derived from cryptoapi implementation, adapted for in-place
8 * scatterlist interface.
9 *
10 * Copyright (c) Alan Smithee.
11 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
12 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080014#include <crypto/internal/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/module.h>
17#include <linux/mm.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110018#include <linux/types.h>
Jan Glauber5265eeb2007-10-09 22:43:13 +080019#include <crypto/sha.h>
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020020#include <crypto/sha1_base.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/byteorder.h>
22
LABBE Corentin0c4c78d2015-12-17 13:45:39 +010023const u8 sha1_zero_message_hash[SHA1_DIGEST_SIZE] = {
24 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d,
25 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
26 0xaf, 0xd8, 0x07, 0x09
27};
28EXPORT_SYMBOL_GPL(sha1_zero_message_hash);
29
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020030static void sha1_generic_block_fn(struct sha1_state *sst, u8 const *src,
31 int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -070032{
Eric Biggers6b0b0fa2020-05-02 11:24:25 -070033 u32 temp[SHA1_WORKSPACE_WORDS];
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080034
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020035 while (blocks--) {
Eric Biggers6b0b0fa2020-05-02 11:24:25 -070036 sha1_transform(sst->state, src, temp);
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020037 src += SHA1_BLOCK_SIZE;
38 }
39 memzero_explicit(temp, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Mathias Krause7c390172011-08-04 20:19:24 +020042int crypto_sha1_update(struct shash_desc *desc, const u8 *data,
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020043 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020045 return sha1_base_do_update(desc, data, len, sha1_generic_block_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
Mathias Krause7c390172011-08-04 20:19:24 +020047EXPORT_SYMBOL(crypto_sha1_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080049static int sha1_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020051 sha1_base_do_finalize(desc, sha1_generic_block_fn);
52 return sha1_base_finish(desc, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020055int crypto_sha1_finup(struct shash_desc *desc, const u8 *data,
56 unsigned int len, u8 *out)
Herbert Xue2a7ce42009-07-09 21:27:13 +080057{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020058 sha1_base_do_update(desc, data, len, sha1_generic_block_fn);
59 return sha1_final(desc, out);
Herbert Xue2a7ce42009-07-09 21:27:13 +080060}
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020061EXPORT_SYMBOL(crypto_sha1_finup);
Herbert Xue2a7ce42009-07-09 21:27:13 +080062
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080063static struct shash_alg alg = {
64 .digestsize = SHA1_DIGEST_SIZE,
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020065 .init = sha1_base_init,
Mathias Krause7c390172011-08-04 20:19:24 +020066 .update = crypto_sha1_update,
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080067 .final = sha1_final,
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020068 .finup = crypto_sha1_finup,
Herbert Xue2a7ce42009-07-09 21:27:13 +080069 .descsize = sizeof(struct sha1_state),
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080070 .base = {
71 .cra_name = "sha1",
72 .cra_driver_name= "sha1-generic",
Eric Biggers90ef3e42018-06-29 17:01:41 -070073 .cra_priority = 100,
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080074 .cra_blocksize = SHA1_BLOCK_SIZE,
75 .cra_module = THIS_MODULE,
76 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070077};
78
Kamalesh Babulal3af5b902008-04-05 21:00:57 +080079static int __init sha1_generic_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080081 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Kamalesh Babulal3af5b902008-04-05 21:00:57 +080084static void __exit sha1_generic_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080086 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Eric Biggersc4741b22019-04-11 21:57:42 -070089subsys_initcall(sha1_generic_mod_init);
Kamalesh Babulal3af5b902008-04-05 21:00:57 +080090module_exit(sha1_generic_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92MODULE_LICENSE("GPL");
93MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
Michal Ludvigb3be9a62006-07-09 08:59:38 +100094
Kees Cook5d26a102014-11-20 17:05:53 -080095MODULE_ALIAS_CRYPTO("sha1");
Mathias Krause3e14dcf2015-01-11 18:17:42 +010096MODULE_ALIAS_CRYPTO("sha1-generic");