blob: 6877cbb9105fbb24cb0a65f441f0c9f272a22733 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Cryptographic API.
3 *
4 * SHA1 Secure Hash Algorithm.
5 *
6 * Derived from cryptoapi implementation, adapted for in-place
7 * scatterlist interface.
8 *
9 * Copyright (c) Alan Smithee.
10 * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
11 * Copyright (c) Jean-Francois Dive <jef@linuxbe.org>
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 2 of the License, or (at your option)
16 * any later version.
17 *
18 */
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080019#include <crypto/internal/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21#include <linux/module.h>
22#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/cryptohash.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110024#include <linux/types.h>
Jan Glauber5265eeb2007-10-09 22:43:13 +080025#include <crypto/sha.h>
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020026#include <crypto/sha1_base.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <asm/byteorder.h>
28
LABBE Corentin0c4c78d2015-12-17 13:45:39 +010029const u8 sha1_zero_message_hash[SHA1_DIGEST_SIZE] = {
30 0xda, 0x39, 0xa3, 0xee, 0x5e, 0x6b, 0x4b, 0x0d,
31 0x32, 0x55, 0xbf, 0xef, 0x95, 0x60, 0x18, 0x90,
32 0xaf, 0xd8, 0x07, 0x09
33};
34EXPORT_SYMBOL_GPL(sha1_zero_message_hash);
35
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020036static void sha1_generic_block_fn(struct sha1_state *sst, u8 const *src,
37 int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020039 u32 temp[SHA_WORKSPACE_WORDS];
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080040
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020041 while (blocks--) {
42 sha_transform(sst->state, src, temp);
43 src += SHA1_BLOCK_SIZE;
44 }
45 memzero_explicit(temp, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
Mathias Krause7c390172011-08-04 20:19:24 +020048int crypto_sha1_update(struct shash_desc *desc, const u8 *data,
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020049 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020051 return sha1_base_do_update(desc, data, len, sha1_generic_block_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
Mathias Krause7c390172011-08-04 20:19:24 +020053EXPORT_SYMBOL(crypto_sha1_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080055static int sha1_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020057 sha1_base_do_finalize(desc, sha1_generic_block_fn);
58 return sha1_base_finish(desc, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020061int crypto_sha1_finup(struct shash_desc *desc, const u8 *data,
62 unsigned int len, u8 *out)
Herbert Xue2a7ce42009-07-09 21:27:13 +080063{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020064 sha1_base_do_update(desc, data, len, sha1_generic_block_fn);
65 return sha1_final(desc, out);
Herbert Xue2a7ce42009-07-09 21:27:13 +080066}
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020067EXPORT_SYMBOL(crypto_sha1_finup);
Herbert Xue2a7ce42009-07-09 21:27:13 +080068
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080069static struct shash_alg alg = {
70 .digestsize = SHA1_DIGEST_SIZE,
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020071 .init = sha1_base_init,
Mathias Krause7c390172011-08-04 20:19:24 +020072 .update = crypto_sha1_update,
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080073 .final = sha1_final,
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020074 .finup = crypto_sha1_finup,
Herbert Xue2a7ce42009-07-09 21:27:13 +080075 .descsize = sizeof(struct sha1_state),
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080076 .base = {
77 .cra_name = "sha1",
78 .cra_driver_name= "sha1-generic",
79 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
80 .cra_blocksize = SHA1_BLOCK_SIZE,
81 .cra_module = THIS_MODULE,
82 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070083};
84
Kamalesh Babulal3af5b902008-04-05 21:00:57 +080085static int __init sha1_generic_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080087 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Kamalesh Babulal3af5b902008-04-05 21:00:57 +080090static void __exit sha1_generic_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080092 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
Kamalesh Babulal3af5b902008-04-05 21:00:57 +080095module_init(sha1_generic_mod_init);
96module_exit(sha1_generic_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98MODULE_LICENSE("GPL");
99MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
Michal Ludvigb3be9a62006-07-09 08:59:38 +1000100
Kees Cook5d26a102014-11-20 17:05:53 -0800101MODULE_ALIAS_CRYPTO("sha1");
Mathias Krause3e14dcf2015-01-11 18:17:42 +0100102MODULE_ALIAS_CRYPTO("sha1-generic");