Jean-Christophe PLAGNIOL-VILLARD | c58a6b8 | 2008-06-07 12:29:52 +0200 | [diff] [blame] | 1 | #ifndef _SHA256_H |
| 2 | #define _SHA256_H |
| 3 | |
Philippe Reynes | 843a1e7 | 2024-12-19 14:05:52 +0100 | [diff] [blame] | 4 | #include <linux/compiler_attributes.h> |
| 5 | #include <linux/errno.h> |
Heinrich Schuchardt | bd198b3 | 2024-12-06 12:37:09 +0100 | [diff] [blame] | 6 | #include <linux/kconfig.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 7 | #include <linux/types.h> |
| 8 | |
Heinrich Schuchardt | bd198b3 | 2024-12-06 12:37:09 +0100 | [diff] [blame] | 9 | #if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO) |
Raymond Mao | 8e79549 | 2025-02-03 14:08:13 -0800 | [diff] [blame^] | 10 | #include "mbedtls_options.h" |
Raymond Mao | a571b98 | 2024-10-03 14:50:16 -0700 | [diff] [blame] | 11 | #include <mbedtls/sha256.h> |
| 12 | #endif |
| 13 | |
| 14 | #define SHA224_SUM_LEN 28 |
Jean-Christophe PLAGNIOL-VILLARD | c58a6b8 | 2008-06-07 12:29:52 +0200 | [diff] [blame] | 15 | #define SHA256_SUM_LEN 32 |
Andrew Duda | 3db9ff0 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 16 | #define SHA256_DER_LEN 19 |
| 17 | |
| 18 | extern const uint8_t sha256_der_prefix[]; |
Jean-Christophe PLAGNIOL-VILLARD | c58a6b8 | 2008-06-07 12:29:52 +0200 | [diff] [blame] | 19 | |
Simon Glass | 0df8243 | 2012-12-05 14:46:34 +0000 | [diff] [blame] | 20 | /* Reset watchdog each time we process this many bytes */ |
| 21 | #define CHUNKSZ_SHA256 (64 * 1024) |
| 22 | |
Heinrich Schuchardt | bd198b3 | 2024-12-06 12:37:09 +0100 | [diff] [blame] | 23 | #if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO) |
Raymond Mao | a571b98 | 2024-10-03 14:50:16 -0700 | [diff] [blame] | 24 | typedef mbedtls_sha256_context sha256_context; |
| 25 | #else |
Jean-Christophe PLAGNIOL-VILLARD | c58a6b8 | 2008-06-07 12:29:52 +0200 | [diff] [blame] | 26 | typedef struct { |
| 27 | uint32_t total[2]; |
| 28 | uint32_t state[8]; |
| 29 | uint8_t buffer[64]; |
| 30 | } sha256_context; |
Raymond Mao | a571b98 | 2024-10-03 14:50:16 -0700 | [diff] [blame] | 31 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | c58a6b8 | 2008-06-07 12:29:52 +0200 | [diff] [blame] | 32 | |
| 33 | void sha256_starts(sha256_context * ctx); |
Simon Glass | 0df8243 | 2012-12-05 14:46:34 +0000 | [diff] [blame] | 34 | void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length); |
Jean-Christophe PLAGNIOL-VILLARD | c58a6b8 | 2008-06-07 12:29:52 +0200 | [diff] [blame] | 35 | void sha256_finish(sha256_context * ctx, uint8_t digest[SHA256_SUM_LEN]); |
| 36 | |
Simon Glass | 0df8243 | 2012-12-05 14:46:34 +0000 | [diff] [blame] | 37 | void sha256_csum_wd(const unsigned char *input, unsigned int ilen, |
| 38 | unsigned char *output, unsigned int chunk_sz); |
| 39 | |
Philippe Reynes | 496c006 | 2024-12-19 14:05:50 +0100 | [diff] [blame] | 40 | int sha256_hmac(const unsigned char *key, int keylen, |
| 41 | const unsigned char *input, unsigned int ilen, |
| 42 | unsigned char *output); |
| 43 | |
Philippe Reynes | 843a1e7 | 2024-12-19 14:05:52 +0100 | [diff] [blame] | 44 | #if CONFIG_IS_ENABLED(HKDF_MBEDTLS) |
| 45 | int sha256_hkdf(const unsigned char *salt, int saltlen, |
| 46 | const unsigned char *ikm, int ikmlen, |
| 47 | const unsigned char *info, int infolen, |
| 48 | unsigned char *output, int outputlen); |
| 49 | #else |
| 50 | static inline int sha256_hkdf(const unsigned char __always_unused *salt, |
| 51 | int __always_unused saltlen, |
| 52 | const unsigned char __always_unused *ikm, |
| 53 | int __always_unused ikmlen, |
| 54 | const unsigned char __always_unused *info, |
| 55 | int __always_unused infolen, |
| 56 | unsigned char __always_unused *output, |
| 57 | int __always_unused outputlen) { |
| 58 | return -EOPNOTSUPP; |
| 59 | } |
| 60 | #endif |
| 61 | |
Jean-Christophe PLAGNIOL-VILLARD | c58a6b8 | 2008-06-07 12:29:52 +0200 | [diff] [blame] | 62 | #endif /* _SHA256_H */ |