Merge tag 'csky-for-linus-6.4' of https://github.com/c-sky/csky-linux
[linux-block.git] / crypto / sha256_generic.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4 2/*
08c327f6 3 * Crypto API wrapper for the generic SHA256 code from lib/crypto/sha256.c
1da177e4
LT
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>
cd12fb90 8 * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
1da177e4 9 */
50e109b5 10#include <crypto/internal/hash.h>
1da177e4
LT
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/mm.h>
06ace7a9 14#include <linux/types.h>
a24d22b2 15#include <crypto/sha2.h>
a2e5ba4f 16#include <crypto/sha256_base.h>
1da177e4 17#include <asm/byteorder.h>
be34c4ef 18#include <asm/unaligned.h>
1da177e4 19
0c4c78de
LC
20const 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};
26EXPORT_SYMBOL_GPL(sha224_zero_message_hash);
27
28const 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};
34EXPORT_SYMBOL_GPL(sha256_zero_message_hash);
35
35d2c9d0 36int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
6c2bb98b 37 unsigned int len)
1da177e4 38{
13855fd8
EB
39 sha256_update(shash_desc_ctx(desc), data, len);
40 return 0;
1da177e4 41}
35d2c9d0 42EXPORT_SYMBOL(crypto_sha256_update);
1da177e4 43
08c327f6 44static int crypto_sha256_final(struct shash_desc *desc, u8 *out)
1da177e4 45{
08c327f6 46 if (crypto_shash_digestsize(desc->tfm) == SHA224_DIGEST_SIZE)
13855fd8 47 sha224_final(shash_desc_ctx(desc), out);
08c327f6 48 else
13855fd8
EB
49 sha256_final(shash_desc_ctx(desc), out);
50 return 0;
1da177e4
LT
51}
52
a2e5ba4f
AB
53int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
54 unsigned int len, u8 *hash)
cd12fb90 55{
08c327f6
HG
56 sha256_update(shash_desc_ctx(desc), data, len);
57 return crypto_sha256_final(desc, hash);
9b2fda7b 58}
a2e5ba4f 59EXPORT_SYMBOL(crypto_sha256_finup);
9b2fda7b 60
6aeb49bc 61static struct shash_alg sha256_algs[2] = { {
50e109b5 62 .digestsize = SHA256_DIGEST_SIZE,
96ede30f 63 .init = sha256_base_init,
35d2c9d0 64 .update = crypto_sha256_update,
08c327f6 65 .final = crypto_sha256_final,
a2e5ba4f 66 .finup = crypto_sha256_finup,
9b2fda7b 67 .descsize = sizeof(struct sha256_state),
50e109b5
AKR
68 .base = {
69 .cra_name = "sha256",
70 .cra_driver_name= "sha256-generic",
b73b7ac0 71 .cra_priority = 100,
50e109b5
AKR
72 .cra_blocksize = SHA256_BLOCK_SIZE,
73 .cra_module = THIS_MODULE,
74 }
6aeb49bc 75}, {
50e109b5 76 .digestsize = SHA224_DIGEST_SIZE,
96ede30f 77 .init = sha224_base_init,
35d2c9d0 78 .update = crypto_sha256_update,
08c327f6 79 .final = crypto_sha256_final,
a2e5ba4f 80 .finup = crypto_sha256_finup,
9b2fda7b 81 .descsize = sizeof(struct sha256_state),
50e109b5
AKR
82 .base = {
83 .cra_name = "sha224",
84 .cra_driver_name= "sha224-generic",
b73b7ac0 85 .cra_priority = 100,
50e109b5
AKR
86 .cra_blocksize = SHA224_BLOCK_SIZE,
87 .cra_module = THIS_MODULE,
88 }
6aeb49bc 89} };
1da177e4 90
3af5b90b 91static int __init sha256_generic_mod_init(void)
1da177e4 92{
6aeb49bc 93 return crypto_register_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
1da177e4
LT
94}
95
3af5b90b 96static void __exit sha256_generic_mod_fini(void)
1da177e4 97{
6aeb49bc 98 crypto_unregister_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
1da177e4
LT
99}
100
c4741b23 101subsys_initcall(sha256_generic_mod_init);
3af5b90b 102module_exit(sha256_generic_mod_fini);
1da177e4
LT
103
104MODULE_LICENSE("GPL");
cd12fb90 105MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm");
b3be9a6d 106
5d26a105 107MODULE_ALIAS_CRYPTO("sha224");
3e14dcf7 108MODULE_ALIAS_CRYPTO("sha224-generic");
5d26a105 109MODULE_ALIAS_CRYPTO("sha256");
3e14dcf7 110MODULE_ALIAS_CRYPTO("sha256-generic");