blob: eafd10f9bf86a6ab6806518634773989331c9ffb [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/*
Hans de Goede08c327f2019-08-17 16:24:35 +02003 * Crypto API wrapper for the generic SHA256 code from lib/crypto/sha256.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
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 Lynchcd12fb92007-11-10 20:08:25 +08008 * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Adrian-Ken Rueegsegger50e109b52008-12-03 19:57:49 +080010#include <crypto/internal/hash.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/mm.h>
Herbert Xu06ace7a2005-10-30 21:25:15 +110014#include <linux/types.h>
Jan Glauber5265eeb2007-10-09 22:43:13 +080015#include <crypto/sha.h>
Hans de Goede08c327f2019-08-17 16:24:35 +020016#include <crypto/sha256.h>
Ard Biesheuvela2e5ba42015-04-09 12:55:37 +020017#include <crypto/sha256_base.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/byteorder.h>
David S. Millerbe34c4ef2014-10-02 14:52:37 +080019#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
LABBE Corentin0c4c78d2015-12-17 13:45:39 +010021const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE] = {
22 0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47,
23 0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2,
24 0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4,
25 0x2f
26};
27EXPORT_SYMBOL_GPL(sha224_zero_message_hash);
28
29const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE] = {
30 0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
31 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
32 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
33 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
34};
35EXPORT_SYMBOL_GPL(sha256_zero_message_hash);
36
Hans de Goede08c327f2019-08-17 16:24:35 +020037static int crypto_sha256_init(struct shash_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Hans de Goede08c327f2019-08-17 16:24:35 +020039 return sha256_init(shash_desc_ctx(desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -070040}
41
Hans de Goede08c327f2019-08-17 16:24:35 +020042static int crypto_sha224_init(struct shash_desc *desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
Hans de Goede08c327f2019-08-17 16:24:35 +020044 return sha224_init(shash_desc_ctx(desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
Tim Chen35d2c9d2013-03-26 13:58:49 -070047int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
Herbert Xu6c2bb982006-05-16 22:09:29 +100048 unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Hans de Goede08c327f2019-08-17 16:24:35 +020050 return sha256_update(shash_desc_ctx(desc), data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051}
Tim Chen35d2c9d2013-03-26 13:58:49 -070052EXPORT_SYMBOL(crypto_sha256_update);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Hans de Goede08c327f2019-08-17 16:24:35 +020054static int crypto_sha256_final(struct shash_desc *desc, u8 *out)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Hans de Goede08c327f2019-08-17 16:24:35 +020056 if (crypto_shash_digestsize(desc->tfm) == SHA224_DIGEST_SIZE)
57 return sha224_final(shash_desc_ctx(desc), out);
58 else
59 return sha256_final(shash_desc_ctx(desc), out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060}
61
Ard Biesheuvela2e5ba42015-04-09 12:55:37 +020062int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
63 unsigned int len, u8 *hash)
Jonathan Lynchcd12fb92007-11-10 20:08:25 +080064{
Hans de Goede08c327f2019-08-17 16:24:35 +020065 sha256_update(shash_desc_ctx(desc), data, len);
66 return crypto_sha256_final(desc, hash);
Jonathan Lynchcd12fb92007-11-10 20:08:25 +080067}
Ard Biesheuvela2e5ba42015-04-09 12:55:37 +020068EXPORT_SYMBOL(crypto_sha256_finup);
Herbert Xu9b2fda72009-07-10 13:00:27 +080069
Jussi Kivilinna6aeb49b2012-07-11 14:20:30 +030070static struct shash_alg sha256_algs[2] = { {
Adrian-Ken Rueegsegger50e109b52008-12-03 19:57:49 +080071 .digestsize = SHA256_DIGEST_SIZE,
Hans de Goede08c327f2019-08-17 16:24:35 +020072 .init = crypto_sha256_init,
Tim Chen35d2c9d2013-03-26 13:58:49 -070073 .update = crypto_sha256_update,
Hans de Goede08c327f2019-08-17 16:24:35 +020074 .final = crypto_sha256_final,
Ard Biesheuvela2e5ba42015-04-09 12:55:37 +020075 .finup = crypto_sha256_finup,
Herbert Xu9b2fda72009-07-10 13:00:27 +080076 .descsize = sizeof(struct sha256_state),
Adrian-Ken Rueegsegger50e109b52008-12-03 19:57:49 +080077 .base = {
78 .cra_name = "sha256",
79 .cra_driver_name= "sha256-generic",
Eric Biggersb73b7ac02018-06-29 17:01:42 -070080 .cra_priority = 100,
Adrian-Ken Rueegsegger50e109b52008-12-03 19:57:49 +080081 .cra_blocksize = SHA256_BLOCK_SIZE,
82 .cra_module = THIS_MODULE,
83 }
Jussi Kivilinna6aeb49b2012-07-11 14:20:30 +030084}, {
Adrian-Ken Rueegsegger50e109b52008-12-03 19:57:49 +080085 .digestsize = SHA224_DIGEST_SIZE,
Hans de Goede08c327f2019-08-17 16:24:35 +020086 .init = crypto_sha224_init,
Tim Chen35d2c9d2013-03-26 13:58:49 -070087 .update = crypto_sha256_update,
Hans de Goede08c327f2019-08-17 16:24:35 +020088 .final = crypto_sha256_final,
Ard Biesheuvela2e5ba42015-04-09 12:55:37 +020089 .finup = crypto_sha256_finup,
Herbert Xu9b2fda72009-07-10 13:00:27 +080090 .descsize = sizeof(struct sha256_state),
Adrian-Ken Rueegsegger50e109b52008-12-03 19:57:49 +080091 .base = {
92 .cra_name = "sha224",
93 .cra_driver_name= "sha224-generic",
Eric Biggersb73b7ac02018-06-29 17:01:42 -070094 .cra_priority = 100,
Adrian-Ken Rueegsegger50e109b52008-12-03 19:57:49 +080095 .cra_blocksize = SHA224_BLOCK_SIZE,
96 .cra_module = THIS_MODULE,
97 }
Jussi Kivilinna6aeb49b2012-07-11 14:20:30 +030098} };
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800100static int __init sha256_generic_mod_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Jussi Kivilinna6aeb49b2012-07-11 14:20:30 +0300102 return crypto_register_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800105static void __exit sha256_generic_mod_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Jussi Kivilinna6aeb49b2012-07-11 14:20:30 +0300107 crypto_unregister_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Eric Biggersc4741b22019-04-11 21:57:42 -0700110subsys_initcall(sha256_generic_mod_init);
Kamalesh Babulal3af5b902008-04-05 21:00:57 +0800111module_exit(sha256_generic_mod_fini);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113MODULE_LICENSE("GPL");
Jonathan Lynchcd12fb92007-11-10 20:08:25 +0800114MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm");
Michal Ludvigb3be9a62006-07-09 08:59:38 +1000115
Kees Cook5d26a102014-11-20 17:05:53 -0800116MODULE_ALIAS_CRYPTO("sha224");
Mathias Krause3e14dcf2015-01-11 18:17:42 +0100117MODULE_ALIAS_CRYPTO("sha224-generic");
Kees Cook5d26a102014-11-20 17:05:53 -0800118MODULE_ALIAS_CRYPTO("sha256");
Mathias Krause3e14dcf2015-01-11 18:17:42 +0100119MODULE_ALIAS_CRYPTO("sha256-generic");