2019-05-27 14:55:01 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
2019-08-17 22:24:35 +08:00
|
|
|
* Crypto API wrapper for the generic SHA256 code from lib/crypto/sha256.c
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
|
|
|
* Copyright (c) Jean-Luc Cooke <jlcooke@certainkey.com>
|
|
|
|
* Copyright (c) Andrew McDonald <andrew@mcdonald.org.uk>
|
|
|
|
* Copyright (c) 2002 James Morris <jmorris@intercode.com.au>
|
2007-11-10 20:08:25 +08:00
|
|
|
* SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2008-12-03 19:57:49 +08:00
|
|
|
#include <crypto/internal/hash.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/mm.h>
|
2005-10-30 18:25:15 +08:00
|
|
|
#include <linux/types.h>
|
2020-11-13 13:20:21 +08:00
|
|
|
#include <crypto/sha2.h>
|
2015-04-09 18:55:37 +08:00
|
|
|
#include <crypto/sha256_base.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
#include <asm/byteorder.h>
|
2014-10-02 14:52:37 +08:00
|
|
|
#include <asm/unaligned.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2015-12-17 20:45:39 +08:00
|
|
|
const u8 sha224_zero_message_hash[SHA224_DIGEST_SIZE] = {
|
|
|
|
0xd1, 0x4a, 0x02, 0x8c, 0x2a, 0x3a, 0x2b, 0xc9, 0x47,
|
|
|
|
0x61, 0x02, 0xbb, 0x28, 0x82, 0x34, 0xc4, 0x15, 0xa2,
|
|
|
|
0xb0, 0x1f, 0x82, 0x8e, 0xa6, 0x2a, 0xc5, 0xb3, 0xe4,
|
|
|
|
0x2f
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(sha224_zero_message_hash);
|
|
|
|
|
|
|
|
const u8 sha256_zero_message_hash[SHA256_DIGEST_SIZE] = {
|
|
|
|
0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14,
|
|
|
|
0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24,
|
|
|
|
0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c,
|
|
|
|
0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55
|
|
|
|
};
|
|
|
|
EXPORT_SYMBOL_GPL(sha256_zero_message_hash);
|
|
|
|
|
2019-08-17 22:24:35 +08:00
|
|
|
static int crypto_sha256_init(struct shash_desc *desc)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2020-05-02 00:42:29 +08:00
|
|
|
sha256_init(shash_desc_ctx(desc));
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2019-08-17 22:24:35 +08:00
|
|
|
static int crypto_sha224_init(struct shash_desc *desc)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2020-05-02 00:42:29 +08:00
|
|
|
sha224_init(shash_desc_ctx(desc));
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2013-03-27 04:58:49 +08:00
|
|
|
int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
|
2006-05-16 20:09:29 +08:00
|
|
|
unsigned int len)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2020-05-02 00:42:29 +08:00
|
|
|
sha256_update(shash_desc_ctx(desc), data, len);
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2013-03-27 04:58:49 +08:00
|
|
|
EXPORT_SYMBOL(crypto_sha256_update);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2019-08-17 22:24:35 +08:00
|
|
|
static int crypto_sha256_final(struct shash_desc *desc, u8 *out)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2019-08-17 22:24:35 +08:00
|
|
|
if (crypto_shash_digestsize(desc->tfm) == SHA224_DIGEST_SIZE)
|
2020-05-02 00:42:29 +08:00
|
|
|
sha224_final(shash_desc_ctx(desc), out);
|
2019-08-17 22:24:35 +08:00
|
|
|
else
|
2020-05-02 00:42:29 +08:00
|
|
|
sha256_final(shash_desc_ctx(desc), out);
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2015-04-09 18:55:37 +08:00
|
|
|
int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
|
|
|
|
unsigned int len, u8 *hash)
|
2007-11-10 20:08:25 +08:00
|
|
|
{
|
2019-08-17 22:24:35 +08:00
|
|
|
sha256_update(shash_desc_ctx(desc), data, len);
|
|
|
|
return crypto_sha256_final(desc, hash);
|
2009-07-10 13:00:27 +08:00
|
|
|
}
|
2015-04-09 18:55:37 +08:00
|
|
|
EXPORT_SYMBOL(crypto_sha256_finup);
|
2009-07-10 13:00:27 +08:00
|
|
|
|
2012-07-11 19:20:30 +08:00
|
|
|
static struct shash_alg sha256_algs[2] = { {
|
2008-12-03 19:57:49 +08:00
|
|
|
.digestsize = SHA256_DIGEST_SIZE,
|
2019-08-17 22:24:35 +08:00
|
|
|
.init = crypto_sha256_init,
|
2013-03-27 04:58:49 +08:00
|
|
|
.update = crypto_sha256_update,
|
2019-08-17 22:24:35 +08:00
|
|
|
.final = crypto_sha256_final,
|
2015-04-09 18:55:37 +08:00
|
|
|
.finup = crypto_sha256_finup,
|
2009-07-10 13:00:27 +08:00
|
|
|
.descsize = sizeof(struct sha256_state),
|
2008-12-03 19:57:49 +08:00
|
|
|
.base = {
|
|
|
|
.cra_name = "sha256",
|
|
|
|
.cra_driver_name= "sha256-generic",
|
2018-06-30 08:01:42 +08:00
|
|
|
.cra_priority = 100,
|
2008-12-03 19:57:49 +08:00
|
|
|
.cra_blocksize = SHA256_BLOCK_SIZE,
|
|
|
|
.cra_module = THIS_MODULE,
|
|
|
|
}
|
2012-07-11 19:20:30 +08:00
|
|
|
}, {
|
2008-12-03 19:57:49 +08:00
|
|
|
.digestsize = SHA224_DIGEST_SIZE,
|
2019-08-17 22:24:35 +08:00
|
|
|
.init = crypto_sha224_init,
|
2013-03-27 04:58:49 +08:00
|
|
|
.update = crypto_sha256_update,
|
2019-08-17 22:24:35 +08:00
|
|
|
.final = crypto_sha256_final,
|
2015-04-09 18:55:37 +08:00
|
|
|
.finup = crypto_sha256_finup,
|
2009-07-10 13:00:27 +08:00
|
|
|
.descsize = sizeof(struct sha256_state),
|
2008-12-03 19:57:49 +08:00
|
|
|
.base = {
|
|
|
|
.cra_name = "sha224",
|
|
|
|
.cra_driver_name= "sha224-generic",
|
2018-06-30 08:01:42 +08:00
|
|
|
.cra_priority = 100,
|
2008-12-03 19:57:49 +08:00
|
|
|
.cra_blocksize = SHA224_BLOCK_SIZE,
|
|
|
|
.cra_module = THIS_MODULE,
|
|
|
|
}
|
2012-07-11 19:20:30 +08:00
|
|
|
} };
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-04-05 21:00:57 +08:00
|
|
|
static int __init sha256_generic_mod_init(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-07-11 19:20:30 +08:00
|
|
|
return crypto_register_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-04-05 21:00:57 +08:00
|
|
|
static void __exit sha256_generic_mod_fini(void)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2012-07-11 19:20:30 +08:00
|
|
|
crypto_unregister_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2019-04-12 12:57:42 +08:00
|
|
|
subsys_initcall(sha256_generic_mod_init);
|
2008-04-05 21:00:57 +08:00
|
|
|
module_exit(sha256_generic_mod_fini);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
2007-11-10 20:08:25 +08:00
|
|
|
MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm");
|
2006-07-09 06:59:38 +08:00
|
|
|
|
2014-11-21 09:05:53 +08:00
|
|
|
MODULE_ALIAS_CRYPTO("sha224");
|
2015-01-12 01:17:42 +08:00
|
|
|
MODULE_ALIAS_CRYPTO("sha224-generic");
|
2014-11-21 09:05:53 +08:00
|
|
|
MODULE_ALIAS_CRYPTO("sha256");
|
2015-01-12 01:17:42 +08:00
|
|
|
MODULE_ALIAS_CRYPTO("sha256-generic");
|