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 | a571b98 | 2024-10-03 14:50:16 -0700 | [diff] [blame] | 10 | /* |
| 11 | * FIXME: |
| 12 | * MbedTLS define the members of "mbedtls_sha256_context" as private, |
| 13 | * but "state" needs to be access by arch/arm/cpu/armv8/sha256_ce_glue. |
| 14 | * MBEDTLS_ALLOW_PRIVATE_ACCESS needs to be enabled to allow the external |
| 15 | * access. |
| 16 | * Directly including <external/mbedtls/library/common.h> is not allowed, |
| 17 | * since this will include <malloc.h> and break the sandbox test. |
| 18 | */ |
| 19 | #define MBEDTLS_ALLOW_PRIVATE_ACCESS |
| 20 | |
| 21 | #include <mbedtls/sha256.h> |
| 22 | #endif |
| 23 | |
| 24 | #define SHA224_SUM_LEN 28 |
Jean-Christophe PLAGNIOL-VILLARD | c58a6b8 | 2008-06-07 12:29:52 +0200 | [diff] [blame] | 25 | #define SHA256_SUM_LEN 32 |
Andrew Duda | 3db9ff0 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 26 | #define SHA256_DER_LEN 19 |
| 27 | |
| 28 | extern const uint8_t sha256_der_prefix[]; |
Jean-Christophe PLAGNIOL-VILLARD | c58a6b8 | 2008-06-07 12:29:52 +0200 | [diff] [blame] | 29 | |
Simon Glass | 0df8243 | 2012-12-05 14:46:34 +0000 | [diff] [blame] | 30 | /* Reset watchdog each time we process this many bytes */ |
| 31 | #define CHUNKSZ_SHA256 (64 * 1024) |
| 32 | |
Heinrich Schuchardt | bd198b3 | 2024-12-06 12:37:09 +0100 | [diff] [blame] | 33 | #if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO) |
Raymond Mao | a571b98 | 2024-10-03 14:50:16 -0700 | [diff] [blame] | 34 | typedef mbedtls_sha256_context sha256_context; |
| 35 | #else |
Jean-Christophe PLAGNIOL-VILLARD | c58a6b8 | 2008-06-07 12:29:52 +0200 | [diff] [blame] | 36 | typedef struct { |
| 37 | uint32_t total[2]; |
| 38 | uint32_t state[8]; |
| 39 | uint8_t buffer[64]; |
| 40 | } sha256_context; |
Raymond Mao | a571b98 | 2024-10-03 14:50:16 -0700 | [diff] [blame] | 41 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | c58a6b8 | 2008-06-07 12:29:52 +0200 | [diff] [blame] | 42 | |
| 43 | void sha256_starts(sha256_context * ctx); |
Simon Glass | 0df8243 | 2012-12-05 14:46:34 +0000 | [diff] [blame] | 44 | 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] | 45 | void sha256_finish(sha256_context * ctx, uint8_t digest[SHA256_SUM_LEN]); |
| 46 | |
Simon Glass | 0df8243 | 2012-12-05 14:46:34 +0000 | [diff] [blame] | 47 | void sha256_csum_wd(const unsigned char *input, unsigned int ilen, |
| 48 | unsigned char *output, unsigned int chunk_sz); |
| 49 | |
Philippe Reynes | 496c006 | 2024-12-19 14:05:50 +0100 | [diff] [blame] | 50 | int sha256_hmac(const unsigned char *key, int keylen, |
| 51 | const unsigned char *input, unsigned int ilen, |
| 52 | unsigned char *output); |
| 53 | |
Philippe Reynes | 843a1e7 | 2024-12-19 14:05:52 +0100 | [diff] [blame] | 54 | #if CONFIG_IS_ENABLED(HKDF_MBEDTLS) |
| 55 | int sha256_hkdf(const unsigned char *salt, int saltlen, |
| 56 | const unsigned char *ikm, int ikmlen, |
| 57 | const unsigned char *info, int infolen, |
| 58 | unsigned char *output, int outputlen); |
| 59 | #else |
| 60 | static inline int sha256_hkdf(const unsigned char __always_unused *salt, |
| 61 | int __always_unused saltlen, |
| 62 | const unsigned char __always_unused *ikm, |
| 63 | int __always_unused ikmlen, |
| 64 | const unsigned char __always_unused *info, |
| 65 | int __always_unused infolen, |
| 66 | unsigned char __always_unused *output, |
| 67 | int __always_unused outputlen) { |
| 68 | return -EOPNOTSUPP; |
| 69 | } |
| 70 | #endif |
| 71 | |
Jean-Christophe PLAGNIOL-VILLARD | c58a6b8 | 2008-06-07 12:29:52 +0200 | [diff] [blame] | 72 | #endif /* _SHA256_H */ |