blob: 39e3acc438d95c1ed1c15aaf9916b9489febbade [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
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020029static void sha1_generic_block_fn(struct sha1_state *sst, u8 const *src,
30 int blocks)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020032 u32 temp[SHA_WORKSPACE_WORDS];
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080033
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020034 while (blocks--) {
35 sha_transform(sst->state, src, temp);
36 src += SHA1_BLOCK_SIZE;
37 }
38 memzero_explicit(temp, sizeof(temp));
Linus Torvalds1da177e2005-04-16 15:20:36 -070039}
40
Mathias Krause7c390172011-08-04 20:19:24 +020041int crypto_sha1_update(struct shash_desc *desc, const u8 *data,
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020042 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020044 return sha1_base_do_update(desc, data, len, sha1_generic_block_fn);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
Mathias Krause7c390172011-08-04 20:19:24 +020046EXPORT_SYMBOL(crypto_sha1_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080048static int sha1_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020050 sha1_base_do_finalize(desc, sha1_generic_block_fn);
51 return sha1_base_finish(desc, out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052}
53
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020054int crypto_sha1_finup(struct shash_desc *desc, const u8 *data,
55 unsigned int len, u8 *out)
Herbert Xue2a7ce42009-07-09 21:27:13 +080056{
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020057 sha1_base_do_update(desc, data, len, sha1_generic_block_fn);
58 return sha1_final(desc, out);
Herbert Xue2a7ce42009-07-09 21:27:13 +080059}
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020060EXPORT_SYMBOL(crypto_sha1_finup);
Herbert Xue2a7ce42009-07-09 21:27:13 +080061
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080062static struct shash_alg alg = {
63 .digestsize = SHA1_DIGEST_SIZE,
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020064 .init = sha1_base_init,
Mathias Krause7c390172011-08-04 20:19:24 +020065 .update = crypto_sha1_update,
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080066 .final = sha1_final,
Ard Biesheuvel7c71f0f2015-04-09 12:55:36 +020067 .finup = crypto_sha1_finup,
Herbert Xue2a7ce42009-07-09 21:27:13 +080068 .descsize = sizeof(struct sha1_state),
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080069 .base = {
70 .cra_name = "sha1",
71 .cra_driver_name= "sha1-generic",
72 .cra_flags = CRYPTO_ALG_TYPE_SHASH,
73 .cra_blocksize = SHA1_BLOCK_SIZE,
74 .cra_module = THIS_MODULE,
75 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070076};
77
Kamalesh Babulal3af5b902008-04-05 21:00:57 +080078static int __init sha1_generic_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080080 return crypto_register_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Kamalesh Babulal3af5b902008-04-05 21:00:57 +080083static void __exit sha1_generic_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Adrian-Ken Rueegsegger54ccb362008-12-02 21:08:20 +080085 crypto_unregister_shash(&alg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Kamalesh Babulal3af5b902008-04-05 21:00:57 +080088module_init(sha1_generic_mod_init);
89module_exit(sha1_generic_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91MODULE_LICENSE("GPL");
92MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm");
Michal Ludvigb3be9a62006-07-09 08:59:38 +100093
Kees Cook5d26a102014-11-20 17:05:53 -080094MODULE_ALIAS_CRYPTO("sha1");
Mathias Krause3e14dcf2015-01-11 18:17:42 +010095MODULE_ALIAS_CRYPTO("sha1-generic");