blob: 7c57b844c38275c06e48171b301b0745b83d5f2b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/cryptohash.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110019#include <linux/types.h>
Jan Glauber5265eeb2007-10-09 22:43:13 +080020#include <crypto/sha.h>
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020021#include <crypto/sha1_base.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/byteorder.h>
23
LABBE Corentin0c4c78d2015-12-17 13:45:39 +010024const u8 sha1_zero_message_hash[SHA1_DIGEST_SIZE] = {
25 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d,
26 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
27 0xaf, 0xd8, 0x07, 0x09
28};
29EXPORT_SYMBOL_GPL(sha1_zero_message_hash);
30
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020031static void sha1_generic_block_fn(struct sha1_state *sst, u8 const *src,
32 int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020034 u32 temp[SHA_WORKSPACE_WORDS];
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080035
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020036 while (blocks--) {
37 sha_transform(sst->state, src, temp);
38 src += SHA1_BLOCK_SIZE;
39 }
40 memzero_explicit(temp, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Mathias Krause7c390172011-08-04 20:19:24 +020043int crypto_sha1_update(struct shash_desc *desc, const u8 *data,
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020044 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020046 return sha1_base_do_update(desc, data, len, sha1_generic_block_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
Mathias Krause7c390172011-08-04 20:19:24 +020048EXPORT_SYMBOL(crypto_sha1_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080050static int sha1_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020052 sha1_base_do_finalize(desc, sha1_generic_block_fn);
53 return sha1_base_finish(desc, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054}
55
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020056int crypto_sha1_finup(struct shash_desc *desc, const u8 *data,
57 unsigned int len, u8 *out)
Herbert Xue2a7ce42009-07-09 21:27:13 +080058{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020059 sha1_base_do_update(desc, data, len, sha1_generic_block_fn);
60 return sha1_final(desc, out);
Herbert Xue2a7ce42009-07-09 21:27:13 +080061}
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020062EXPORT_SYMBOL(crypto_sha1_finup);
Herbert Xue2a7ce42009-07-09 21:27:13 +080063
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080064static struct shash_alg alg = {
65 .digestsize = SHA1_DIGEST_SIZE,
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020066 .init = sha1_base_init,
Mathias Krause7c390172011-08-04 20:19:24 +020067 .update = crypto_sha1_update,
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080068 .final = sha1_final,
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020069 .finup = crypto_sha1_finup,
Herbert Xue2a7ce42009-07-09 21:27:13 +080070 .descsize = sizeof(struct sha1_state),
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080071 .base = {
72 .cra_name = "sha1",
73 .cra_driver_name= "sha1-generic",
Eric Biggers90ef3e42018-06-29 17:01:41 -070074 .cra_priority = 100,
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080075 .cra_blocksize = SHA1_BLOCK_SIZE,
76 .cra_module = THIS_MODULE,
77 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
Kamalesh Babulal3af5b902008-04-05 21:00:57 +080080static int __init sha1_generic_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080082 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Kamalesh Babulal3af5b902008-04-05 21:00:57 +080085static void __exit sha1_generic_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080087 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Eric Biggersc4741b22019-04-11 21:57:42 -070090subsys_initcall(sha1_generic_mod_init);
Kamalesh Babulal3af5b902008-04-05 21:00:57 +080091module_exit(sha1_generic_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93MODULE_LICENSE("GPL");
94MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
Michal Ludvigb3be9a62006-07-09 08:59:38 +100095
Kees Cook5d26a102014-11-20 17:05:53 -080096MODULE_ALIAS_CRYPTO("sha1");
Mathias Krause3e14dcf2015-01-11 18:17:42 +010097MODULE_ALIAS_CRYPTO("sha1-generic");