Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 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 Rueegsegger | 54ccb36 | 2008-12-02 21:08:20 +0800 | [diff] [blame] | 19 | #include <crypto/internal/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/init.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <linux/cryptohash.h> |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 24 | #include <linux/types.h> |
Jan Glauber | 5265eeb | 2007-10-09 22:43:13 +0800 | [diff] [blame] | 25 | #include <crypto/sha.h> |
Ard Biesheuvel | 7c71f0f | 2015-04-09 12:55:36 +0200 | [diff] [blame^] | 26 | #include <crypto/sha1_base.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <asm/byteorder.h> |
| 28 | |
Ard Biesheuvel | 7c71f0f | 2015-04-09 12:55:36 +0200 | [diff] [blame^] | 29 | static void sha1_generic_block_fn(struct sha1_state *sst, u8 const *src, |
| 30 | int blocks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | { |
Ard Biesheuvel | 7c71f0f | 2015-04-09 12:55:36 +0200 | [diff] [blame^] | 32 | u32 temp[SHA_WORKSPACE_WORDS]; |
Adrian-Ken Rueegsegger | 54ccb36 | 2008-12-02 21:08:20 +0800 | [diff] [blame] | 33 | |
Ard Biesheuvel | 7c71f0f | 2015-04-09 12:55:36 +0200 | [diff] [blame^] | 34 | while (blocks--) { |
| 35 | sha_transform(sst->state, src, temp); |
| 36 | src += SHA1_BLOCK_SIZE; |
| 37 | } |
| 38 | memzero_explicit(temp, sizeof(temp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Mathias Krause | 7c39017 | 2011-08-04 20:19:24 +0200 | [diff] [blame] | 41 | int crypto_sha1_update(struct shash_desc *desc, const u8 *data, |
Ard Biesheuvel | 7c71f0f | 2015-04-09 12:55:36 +0200 | [diff] [blame^] | 42 | unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | { |
Ard Biesheuvel | 7c71f0f | 2015-04-09 12:55:36 +0200 | [diff] [blame^] | 44 | return sha1_base_do_update(desc, data, len, sha1_generic_block_fn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
Mathias Krause | 7c39017 | 2011-08-04 20:19:24 +0200 | [diff] [blame] | 46 | EXPORT_SYMBOL(crypto_sha1_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Adrian-Ken Rueegsegger | 54ccb36 | 2008-12-02 21:08:20 +0800 | [diff] [blame] | 48 | static int sha1_final(struct shash_desc *desc, u8 *out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | { |
Ard Biesheuvel | 7c71f0f | 2015-04-09 12:55:36 +0200 | [diff] [blame^] | 50 | sha1_base_do_finalize(desc, sha1_generic_block_fn); |
| 51 | return sha1_base_finish(desc, out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Ard Biesheuvel | 7c71f0f | 2015-04-09 12:55:36 +0200 | [diff] [blame^] | 54 | int crypto_sha1_finup(struct shash_desc *desc, const u8 *data, |
| 55 | unsigned int len, u8 *out) |
Herbert Xu | e2a7ce4 | 2009-07-09 21:27:13 +0800 | [diff] [blame] | 56 | { |
Ard Biesheuvel | 7c71f0f | 2015-04-09 12:55:36 +0200 | [diff] [blame^] | 57 | sha1_base_do_update(desc, data, len, sha1_generic_block_fn); |
| 58 | return sha1_final(desc, out); |
Herbert Xu | e2a7ce4 | 2009-07-09 21:27:13 +0800 | [diff] [blame] | 59 | } |
Ard Biesheuvel | 7c71f0f | 2015-04-09 12:55:36 +0200 | [diff] [blame^] | 60 | EXPORT_SYMBOL(crypto_sha1_finup); |
Herbert Xu | e2a7ce4 | 2009-07-09 21:27:13 +0800 | [diff] [blame] | 61 | |
Adrian-Ken Rueegsegger | 54ccb36 | 2008-12-02 21:08:20 +0800 | [diff] [blame] | 62 | static struct shash_alg alg = { |
| 63 | .digestsize = SHA1_DIGEST_SIZE, |
Ard Biesheuvel | 7c71f0f | 2015-04-09 12:55:36 +0200 | [diff] [blame^] | 64 | .init = sha1_base_init, |
Mathias Krause | 7c39017 | 2011-08-04 20:19:24 +0200 | [diff] [blame] | 65 | .update = crypto_sha1_update, |
Adrian-Ken Rueegsegger | 54ccb36 | 2008-12-02 21:08:20 +0800 | [diff] [blame] | 66 | .final = sha1_final, |
Ard Biesheuvel | 7c71f0f | 2015-04-09 12:55:36 +0200 | [diff] [blame^] | 67 | .finup = crypto_sha1_finup, |
Herbert Xu | e2a7ce4 | 2009-07-09 21:27:13 +0800 | [diff] [blame] | 68 | .descsize = sizeof(struct sha1_state), |
Adrian-Ken Rueegsegger | 54ccb36 | 2008-12-02 21:08:20 +0800 | [diff] [blame] | 69 | .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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 78 | static int __init sha1_generic_mod_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | { |
Adrian-Ken Rueegsegger | 54ccb36 | 2008-12-02 21:08:20 +0800 | [diff] [blame] | 80 | return crypto_register_shash(&alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 83 | static void __exit sha1_generic_mod_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | { |
Adrian-Ken Rueegsegger | 54ccb36 | 2008-12-02 21:08:20 +0800 | [diff] [blame] | 85 | crypto_unregister_shash(&alg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 88 | module_init(sha1_generic_mod_init); |
| 89 | module_exit(sha1_generic_mod_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | MODULE_LICENSE("GPL"); |
| 92 | MODULE_DESCRIPTION("SHA1 Secure Hash Algorithm"); |
Michal Ludvig | b3be9a6 | 2006-07-09 08:59:38 +1000 | [diff] [blame] | 93 | |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 94 | MODULE_ALIAS_CRYPTO("sha1"); |
Mathias Krause | 3e14dcf | 2015-01-11 18:17:42 +0100 | [diff] [blame] | 95 | MODULE_ALIAS_CRYPTO("sha1-generic"); |