blob: 2d86508332e56b4d6636ebf9ec95d445b144c4a9 [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 Mao8e795492025-02-03 14:08:13 -080010#include "mbedtls_options.h"
Raymond Maoa571b982024-10-03 14:50:16 -070011#include <mbedtls/sha256.h>
12#endif
13
14#define SHA224_SUM_LEN 28
Jean-Christophe PLAGNIOL-VILLARDc58a6b82008-06-07 12:29:52 +020015#define SHA256_SUM_LEN 32
Andrew Duda3db9ff02016-11-08 18:53:40 +000016#define SHA256_DER_LEN 19
17
18extern const uint8_t sha256_der_prefix[];
Jean-Christophe PLAGNIOL-VILLARDc58a6b82008-06-07 12:29:52 +020019
Simon Glass0df82432012-12-05 14:46:34 +000020/* Reset watchdog each time we process this many bytes */
21#define CHUNKSZ_SHA256 (64 * 1024)
22
Heinrich Schuchardtbd198b32024-12-06 12:37:09 +010023#if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO)
Raymond Maoa571b982024-10-03 14:50:16 -070024typedef mbedtls_sha256_context sha256_context;
25#else
Jean-Christophe PLAGNIOL-VILLARDc58a6b82008-06-07 12:29:52 +020026typedef struct {
27 uint32_t total[2];
28 uint32_t state[8];
29 uint8_t buffer[64];
30} sha256_context;
Raymond Maoa571b982024-10-03 14:50:16 -070031#endif
Jean-Christophe PLAGNIOL-VILLARDc58a6b82008-06-07 12:29:52 +020032
33void sha256_starts(sha256_context * ctx);
Simon Glass0df82432012-12-05 14:46:34 +000034void sha256_update(sha256_context *ctx, const uint8_t *input, uint32_t length);
Jean-Christophe PLAGNIOL-VILLARDc58a6b82008-06-07 12:29:52 +020035void sha256_finish(sha256_context * ctx, uint8_t digest[SHA256_SUM_LEN]);
36
Simon Glass0df82432012-12-05 14:46:34 +000037void sha256_csum_wd(const unsigned char *input, unsigned int ilen,
38 unsigned char *output, unsigned int chunk_sz);
39
Philippe Reynes496c0062024-12-19 14:05:50 +010040int sha256_hmac(const unsigned char *key, int keylen,
41 const unsigned char *input, unsigned int ilen,
42 unsigned char *output);
43
Philippe Reynes843a1e72024-12-19 14:05:52 +010044#if CONFIG_IS_ENABLED(HKDF_MBEDTLS)
45int 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
50static 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-VILLARDc58a6b82008-06-07 12:29:52 +020062#endif /* _SHA256_H */