Thomas Gleixner | 2874c5f | 2019-05-27 08:55:01 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Hans de Goede | 08c327f | 2019-08-17 16:24:35 +0200 | [diff] [blame] | 3 | * Crypto API wrapper for the generic SHA256 code from lib/crypto/sha256.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com> |
| 6 | * Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk> |
| 7 | * Copyright (c) 2002 James Morris <jmorris@intercode.com.au> |
Jonathan Lynch | cd12fb9 | 2007-11-10 20:08:25 +0800 | [diff] [blame] | 8 | * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
Adrian-Ken Rueegsegger | 50e109b5 | 2008-12-03 19:57:49 +0800 | [diff] [blame] | 10 | #include <crypto/internal/hash.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/init.h> |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/mm.h> |
Herbert Xu | 06ace7a | 2005-10-30 21:25:15 +1100 | [diff] [blame] | 14 | #include <linux/types.h> |
Jan Glauber | 5265eeb | 2007-10-09 22:43:13 +0800 | [diff] [blame] | 15 | #include <crypto/sha.h> |
Ard Biesheuvel | a2e5ba4 | 2015-04-09 12:55:37 +0200 | [diff] [blame] | 16 | #include <crypto/sha256_base.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #include <asm/byteorder.h> |
David S. Miller | be34c4ef | 2014-10-02 14:52:37 +0800 | [diff] [blame] | 18 | #include <asm/unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
LABBE Corentin | 0c4c78d | 2015-12-17 13:45:39 +0100 | [diff] [blame] | 20 | const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE] = { |
| 21 | 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47, |
| 22 | 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2, |
| 23 | 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4, |
| 24 | 0x2f |
| 25 | }; |
| 26 | EXPORT_SYMBOL_GPL(sha224_zero_message_hash); |
| 27 | |
| 28 | const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE] = { |
| 29 | 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, |
| 30 | 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, |
| 31 | 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, |
| 32 | 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55 |
| 33 | }; |
| 34 | EXPORT_SYMBOL_GPL(sha256_zero_message_hash); |
| 35 | |
Hans de Goede | 08c327f | 2019-08-17 16:24:35 +0200 | [diff] [blame] | 36 | static int crypto_sha256_init(struct shash_desc *desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | { |
Hans de Goede | 08c327f | 2019-08-17 16:24:35 +0200 | [diff] [blame] | 38 | return sha256_init(shash_desc_ctx(desc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Hans de Goede | 08c327f | 2019-08-17 16:24:35 +0200 | [diff] [blame] | 41 | static int crypto_sha224_init(struct shash_desc *desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
Hans de Goede | 08c327f | 2019-08-17 16:24:35 +0200 | [diff] [blame] | 43 | return sha224_init(shash_desc_ctx(desc)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Tim Chen | 35d2c9d | 2013-03-26 13:58:49 -0700 | [diff] [blame] | 46 | int crypto_sha256_update(struct shash_desc *desc, const u8 *data, |
Herbert Xu | 6c2bb98 | 2006-05-16 22:09:29 +1000 | [diff] [blame] | 47 | unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | { |
Hans de Goede | 08c327f | 2019-08-17 16:24:35 +0200 | [diff] [blame] | 49 | return sha256_update(shash_desc_ctx(desc), data, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | } |
Tim Chen | 35d2c9d | 2013-03-26 13:58:49 -0700 | [diff] [blame] | 51 | EXPORT_SYMBOL(crypto_sha256_update); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Hans de Goede | 08c327f | 2019-08-17 16:24:35 +0200 | [diff] [blame] | 53 | static int crypto_sha256_final(struct shash_desc *desc, u8 *out) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
Hans de Goede | 08c327f | 2019-08-17 16:24:35 +0200 | [diff] [blame] | 55 | if (crypto_shash_digestsize(desc->tfm) == SHA224_DIGEST_SIZE) |
| 56 | return sha224_final(shash_desc_ctx(desc), out); |
| 57 | else |
| 58 | return sha256_final(shash_desc_ctx(desc), out); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Ard Biesheuvel | a2e5ba4 | 2015-04-09 12:55:37 +0200 | [diff] [blame] | 61 | int crypto_sha256_finup(struct shash_desc *desc, const u8 *data, |
| 62 | unsigned int len, u8 *hash) |
Jonathan Lynch | cd12fb9 | 2007-11-10 20:08:25 +0800 | [diff] [blame] | 63 | { |
Hans de Goede | 08c327f | 2019-08-17 16:24:35 +0200 | [diff] [blame] | 64 | sha256_update(shash_desc_ctx(desc), data, len); |
| 65 | return crypto_sha256_final(desc, hash); |
Jonathan Lynch | cd12fb9 | 2007-11-10 20:08:25 +0800 | [diff] [blame] | 66 | } |
Ard Biesheuvel | a2e5ba4 | 2015-04-09 12:55:37 +0200 | [diff] [blame] | 67 | EXPORT_SYMBOL(crypto_sha256_finup); |
Herbert Xu | 9b2fda7 | 2009-07-10 13:00:27 +0800 | [diff] [blame] | 68 | |
Jussi Kivilinna | 6aeb49b | 2012-07-11 14:20:30 +0300 | [diff] [blame] | 69 | static struct shash_alg sha256_algs[2] = { { |
Adrian-Ken Rueegsegger | 50e109b5 | 2008-12-03 19:57:49 +0800 | [diff] [blame] | 70 | .digestsize = SHA256_DIGEST_SIZE, |
Hans de Goede | 08c327f | 2019-08-17 16:24:35 +0200 | [diff] [blame] | 71 | .init = crypto_sha256_init, |
Tim Chen | 35d2c9d | 2013-03-26 13:58:49 -0700 | [diff] [blame] | 72 | .update = crypto_sha256_update, |
Hans de Goede | 08c327f | 2019-08-17 16:24:35 +0200 | [diff] [blame] | 73 | .final = crypto_sha256_final, |
Ard Biesheuvel | a2e5ba4 | 2015-04-09 12:55:37 +0200 | [diff] [blame] | 74 | .finup = crypto_sha256_finup, |
Herbert Xu | 9b2fda7 | 2009-07-10 13:00:27 +0800 | [diff] [blame] | 75 | .descsize = sizeof(struct sha256_state), |
Adrian-Ken Rueegsegger | 50e109b5 | 2008-12-03 19:57:49 +0800 | [diff] [blame] | 76 | .base = { |
| 77 | .cra_name = "sha256", |
| 78 | .cra_driver_name= "sha256-generic", |
Eric Biggers | b73b7ac0 | 2018-06-29 17:01:42 -0700 | [diff] [blame] | 79 | .cra_priority = 100, |
Adrian-Ken Rueegsegger | 50e109b5 | 2008-12-03 19:57:49 +0800 | [diff] [blame] | 80 | .cra_blocksize = SHA256_BLOCK_SIZE, |
| 81 | .cra_module = THIS_MODULE, |
| 82 | } |
Jussi Kivilinna | 6aeb49b | 2012-07-11 14:20:30 +0300 | [diff] [blame] | 83 | }, { |
Adrian-Ken Rueegsegger | 50e109b5 | 2008-12-03 19:57:49 +0800 | [diff] [blame] | 84 | .digestsize = SHA224_DIGEST_SIZE, |
Hans de Goede | 08c327f | 2019-08-17 16:24:35 +0200 | [diff] [blame] | 85 | .init = crypto_sha224_init, |
Tim Chen | 35d2c9d | 2013-03-26 13:58:49 -0700 | [diff] [blame] | 86 | .update = crypto_sha256_update, |
Hans de Goede | 08c327f | 2019-08-17 16:24:35 +0200 | [diff] [blame] | 87 | .final = crypto_sha256_final, |
Ard Biesheuvel | a2e5ba4 | 2015-04-09 12:55:37 +0200 | [diff] [blame] | 88 | .finup = crypto_sha256_finup, |
Herbert Xu | 9b2fda7 | 2009-07-10 13:00:27 +0800 | [diff] [blame] | 89 | .descsize = sizeof(struct sha256_state), |
Adrian-Ken Rueegsegger | 50e109b5 | 2008-12-03 19:57:49 +0800 | [diff] [blame] | 90 | .base = { |
| 91 | .cra_name = "sha224", |
| 92 | .cra_driver_name= "sha224-generic", |
Eric Biggers | b73b7ac0 | 2018-06-29 17:01:42 -0700 | [diff] [blame] | 93 | .cra_priority = 100, |
Adrian-Ken Rueegsegger | 50e109b5 | 2008-12-03 19:57:49 +0800 | [diff] [blame] | 94 | .cra_blocksize = SHA224_BLOCK_SIZE, |
| 95 | .cra_module = THIS_MODULE, |
| 96 | } |
Jussi Kivilinna | 6aeb49b | 2012-07-11 14:20:30 +0300 | [diff] [blame] | 97 | } }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 99 | static int __init sha256_generic_mod_init(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
Jussi Kivilinna | 6aeb49b | 2012-07-11 14:20:30 +0300 | [diff] [blame] | 101 | return crypto_register_shashes(sha256_algs, ARRAY_SIZE(sha256_algs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 104 | static void __exit sha256_generic_mod_fini(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | { |
Jussi Kivilinna | 6aeb49b | 2012-07-11 14:20:30 +0300 | [diff] [blame] | 106 | crypto_unregister_shashes(sha256_algs, ARRAY_SIZE(sha256_algs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Eric Biggers | c4741b2 | 2019-04-11 21:57:42 -0700 | [diff] [blame] | 109 | subsys_initcall(sha256_generic_mod_init); |
Kamalesh Babulal | 3af5b90 | 2008-04-05 21:00:57 +0800 | [diff] [blame] | 110 | module_exit(sha256_generic_mod_fini); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | MODULE_LICENSE("GPL"); |
Jonathan Lynch | cd12fb9 | 2007-11-10 20:08:25 +0800 | [diff] [blame] | 113 | MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm"); |
Michal Ludvig | b3be9a6 | 2006-07-09 08:59:38 +1000 | [diff] [blame] | 114 | |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 115 | MODULE_ALIAS_CRYPTO("sha224"); |
Mathias Krause | 3e14dcf | 2015-01-11 18:17:42 +0100 | [diff] [blame] | 116 | MODULE_ALIAS_CRYPTO("sha224-generic"); |
Kees Cook | 5d26a10 | 2014-11-20 17:05:53 -0800 | [diff] [blame] | 117 | MODULE_ALIAS_CRYPTO("sha256"); |
Mathias Krause | 3e14dcf | 2015-01-11 18:17:42 +0100 | [diff] [blame] | 118 | MODULE_ALIAS_CRYPTO("sha256-generic"); |