blob: 88156e3e2a33e0c24611a55b6a196c4c2de2b5ca [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
David Brazdil0f672f62019-12-10 10:32:29 +00003 * Crypto API wrapper for the generic SHA256 code from lib/crypto/sha256.c
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00004 *
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>
8 * SHA224 Support Copyright 2007 Intel Corporation <jonathan.lynch@intel.com>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00009 */
10#include <crypto/internal/hash.h>
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/mm.h>
14#include <linux/types.h>
15#include <crypto/sha.h>
16#include <crypto/sha256_base.h>
17#include <asm/byteorder.h>
18#include <asm/unaligned.h>
19
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
David Brazdil0f672f62019-12-10 10:32:29 +000036static int crypto_sha256_init(struct shash_desc *desc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000037{
Olivier Deprez157378f2022-04-04 15:47:50 +020038 sha256_init(shash_desc_ctx(desc));
39 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000040}
41
David Brazdil0f672f62019-12-10 10:32:29 +000042static int crypto_sha224_init(struct shash_desc *desc)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000043{
Olivier Deprez157378f2022-04-04 15:47:50 +020044 sha224_init(shash_desc_ctx(desc));
45 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000046}
47
48int crypto_sha256_update(struct shash_desc *desc, const u8 *data,
49 unsigned int len)
50{
Olivier Deprez157378f2022-04-04 15:47:50 +020051 sha256_update(shash_desc_ctx(desc), data, len);
52 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000053}
54EXPORT_SYMBOL(crypto_sha256_update);
55
David Brazdil0f672f62019-12-10 10:32:29 +000056static int crypto_sha256_final(struct shash_desc *desc, u8 *out)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000057{
David Brazdil0f672f62019-12-10 10:32:29 +000058 if (crypto_shash_digestsize(desc->tfm) == SHA224_DIGEST_SIZE)
Olivier Deprez157378f2022-04-04 15:47:50 +020059 sha224_final(shash_desc_ctx(desc), out);
David Brazdil0f672f62019-12-10 10:32:29 +000060 else
Olivier Deprez157378f2022-04-04 15:47:50 +020061 sha256_final(shash_desc_ctx(desc), out);
62 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063}
64
65int crypto_sha256_finup(struct shash_desc *desc, const u8 *data,
66 unsigned int len, u8 *hash)
67{
David Brazdil0f672f62019-12-10 10:32:29 +000068 sha256_update(shash_desc_ctx(desc), data, len);
69 return crypto_sha256_final(desc, hash);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000070}
71EXPORT_SYMBOL(crypto_sha256_finup);
72
73static struct shash_alg sha256_algs[2] = { {
74 .digestsize = SHA256_DIGEST_SIZE,
David Brazdil0f672f62019-12-10 10:32:29 +000075 .init = crypto_sha256_init,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000076 .update = crypto_sha256_update,
David Brazdil0f672f62019-12-10 10:32:29 +000077 .final = crypto_sha256_final,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000078 .finup = crypto_sha256_finup,
79 .descsize = sizeof(struct sha256_state),
80 .base = {
81 .cra_name = "sha256",
82 .cra_driver_name= "sha256-generic",
83 .cra_priority = 100,
84 .cra_blocksize = SHA256_BLOCK_SIZE,
85 .cra_module = THIS_MODULE,
86 }
87}, {
88 .digestsize = SHA224_DIGEST_SIZE,
David Brazdil0f672f62019-12-10 10:32:29 +000089 .init = crypto_sha224_init,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000090 .update = crypto_sha256_update,
David Brazdil0f672f62019-12-10 10:32:29 +000091 .final = crypto_sha256_final,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000092 .finup = crypto_sha256_finup,
93 .descsize = sizeof(struct sha256_state),
94 .base = {
95 .cra_name = "sha224",
96 .cra_driver_name= "sha224-generic",
97 .cra_priority = 100,
98 .cra_blocksize = SHA224_BLOCK_SIZE,
99 .cra_module = THIS_MODULE,
100 }
101} };
102
103static int __init sha256_generic_mod_init(void)
104{
105 return crypto_register_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
106}
107
108static void __exit sha256_generic_mod_fini(void)
109{
110 crypto_unregister_shashes(sha256_algs, ARRAY_SIZE(sha256_algs));
111}
112
David Brazdil0f672f62019-12-10 10:32:29 +0000113subsys_initcall(sha256_generic_mod_init);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000114module_exit(sha256_generic_mod_fini);
115
116MODULE_LICENSE("GPL");
117MODULE_DESCRIPTION("SHA-224 and SHA-256 Secure Hash Algorithm");
118
119MODULE_ALIAS_CRYPTO("sha224");
120MODULE_ALIAS_CRYPTO("sha224-generic");
121MODULE_ALIAS_CRYPTO("sha256");
122MODULE_ALIAS_CRYPTO("sha256-generic");