blob: d7a3403270b65c5c066921fccdefe9c6cd50328a [file] [log] [blame]
Jean-Christophe PLAGNIOL-VILLARDc58a6b82008-06-07 12:29:52 +02001#ifndef _SHA256_H
2#define _SHA256_H
3
Philippe Reynes843a1e72024-12-19 14:05:52 +01004#include <linux/compiler_attributes.h>
5#include <linux/errno.h>
Heinrich Schuchardtbd198b32024-12-06 12:37:09 +01006#include <linux/kconfig.h>
Tom Rinidec7ea02024-05-20 13:35:03 -06007#include <linux/types.h>
8
Heinrich Schuchardtbd198b32024-12-06 12:37:09 +01009#if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO)
Raymond Maoa571b982024-10-03 14:50:16 -070010/*
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-VILLARDc58a6b82008-06-07 12:29:52 +020025#define SHA256_SUM_LEN 32
Andrew Duda3db9ff02016-11-08 18:53:40 +000026#define SHA256_DER_LEN 19
27
28extern const uint8_t sha256_der_prefix[];
Jean-Christophe PLAGNIOL-VILLARDc58a6b82008-06-07 12:29:52 +020029
Simon Glass0df82432012-12-05 14:46:34 +000030/* Reset watchdog each time we process this many bytes */
31#define CHUNKSZ_SHA256 (64 * 1024)
32
Heinrich Schuchardtbd198b32024-12-06 12:37:09 +010033#if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO)
Raymond Maoa571b982024-10-03 14:50:16 -070034typedef mbedtls_sha256_context sha256_context;
35#else
Jean-Christophe PLAGNIOL-VILLARDc58a6b82008-06-07 12:29:52 +020036typedef struct {
37 uint32_t total[2];
38 uint32_t state[8];
39 uint8_t buffer[64];
40} sha256_context;
Raymond Maoa571b982024-10-03 14:50:16 -070041#endif
Jean-Christophe PLAGNIOL-VILLARDc58a6b82008-06-07 12:29:52 +020042
43void sha256_starts(sha256_context * ctx);
Simon Glass0df82432012-12-05 14:46:34 +000044void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length);
Jean-Christophe PLAGNIOL-VILLARDc58a6b82008-06-07 12:29:52 +020045void sha256_finish(sha256_context * ctx, uint8_t digest[SHA256_SUM_LEN]);
46
Simon Glass0df82432012-12-05 14:46:34 +000047void sha256_csum_wd(const unsigned char *input, unsigned int ilen,
48 unsigned char *output, unsigned int chunk_sz);
49
Philippe Reynes496c0062024-12-19 14:05:50 +010050int sha256_hmac(const unsigned char *key, int keylen,
51 const unsigned char *input, unsigned int ilen,
52 unsigned char *output);
53
Philippe Reynes843a1e72024-12-19 14:05:52 +010054#if CONFIG_IS_ENABLED(HKDF_MBEDTLS)
55int 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
60static 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-VILLARDc58a6b82008-06-07 12:29:52 +020072#endif /* _SHA256_H */