blob: 2fca7f1be168caef9205dfcee31321c6a55c7b3e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: LGPL-2.1 */
Heiko Schocher633e03a2007-06-22 19:11:54 +02002/**
3 * \file sha1.h
4 * based from http://xyssl.org/code/source/sha1/
5 * FIPS-180-1 compliant SHA-1 implementation
6 *
7 * Copyright (C) 2003-2006 Christophe Devine
Heiko Schocher633e03a2007-06-22 19:11:54 +02008 */
9/*
10 * The SHA-1 standard was published by NIST in 1993.
11 *
12 * http://www.itl.nist.gov/fipspubs/fip180-1.htm
13 */
14#ifndef _SHA1_H
15#define _SHA1_H
16
Tom Rinidec7ea02024-05-20 13:35:03 -060017#include <linux/types.h>
18
Raymond Maoa571b982024-10-03 14:50:16 -070019#if defined(CONFIG_MBEDTLS_LIB_CRYPTO)
20/*
21 * FIXME:
22 * MbedTLS define the members of "mbedtls_sha256_context" as private,
23 * but "state" needs to be access by arch/arm/cpu/armv8/sha1_ce_glue.
24 * MBEDTLS_ALLOW_PRIVATE_ACCESS needs to be enabled to allow the external
25 * access.
26 * Directly including <external/mbedtls/library/common.h> is not allowed,
27 * since this will include <malloc.h> and break the sandbox test.
28 */
29#define MBEDTLS_ALLOW_PRIVATE_ACCESS
30
31#include <mbedtls/sha1.h>
32#endif
33
Heiko Schocher633e03a2007-06-22 19:11:54 +020034#ifdef __cplusplus
35extern "C" {
36#endif
37
38#define SHA1_SUM_POS -0x20
39#define SHA1_SUM_LEN 20
Andrew Duda3db9ff02016-11-08 18:53:40 +000040#define SHA1_DER_LEN 15
41
Raymond Mao9ec00882024-10-03 14:50:18 -070042#define SHA1_DEF_CHUNK_SZ 0x10000
43
Raymond Maof51f3552024-10-03 14:50:19 -070044#define K_IPAD_VAL 0x36
45#define K_OPAD_VAL 0x5C
46#define K_PAD_LEN 64
47
Andrew Duda3db9ff02016-11-08 18:53:40 +000048extern const uint8_t sha1_der_prefix[];
Heiko Schocher633e03a2007-06-22 19:11:54 +020049
Raymond Maoa571b982024-10-03 14:50:16 -070050#if defined(CONFIG_MBEDTLS_LIB_CRYPTO)
51typedef mbedtls_sha1_context sha1_context;
52#else
Heiko Schocher633e03a2007-06-22 19:11:54 +020053/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +020054 * \brief SHA-1 context structure
Heiko Schocher633e03a2007-06-22 19:11:54 +020055 */
56typedef struct
57{
Wolfgang Denka0453aa2007-07-10 00:01:28 +020058 unsigned long total[2]; /*!< number of bytes processed */
Loic Poulainc7799b02022-06-01 20:26:28 +020059 uint32_t state[5]; /*!< intermediate digest state */
Wolfgang Denka0453aa2007-07-10 00:01:28 +020060 unsigned char buffer[64]; /*!< data block being processed */
Heiko Schocher633e03a2007-06-22 19:11:54 +020061}
62sha1_context;
Raymond Maoa571b982024-10-03 14:50:16 -070063#endif
Heiko Schocher633e03a2007-06-22 19:11:54 +020064
65/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +020066 * \brief SHA-1 context setup
Heiko Schocher633e03a2007-06-22 19:11:54 +020067 *
Wolfgang Denka0453aa2007-07-10 00:01:28 +020068 * \param ctx SHA-1 context to be initialized
Heiko Schocher633e03a2007-06-22 19:11:54 +020069 */
Raymond Maoa571b982024-10-03 14:50:16 -070070void sha1_starts(sha1_context *ctx);
Heiko Schocher633e03a2007-06-22 19:11:54 +020071
72/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +020073 * \brief SHA-1 process buffer
Heiko Schocher633e03a2007-06-22 19:11:54 +020074 *
Wolfgang Denka0453aa2007-07-10 00:01:28 +020075 * \param ctx SHA-1 context
Heiko Schocher633e03a2007-06-22 19:11:54 +020076 * \param input buffer holding the data
Wolfgang Denka0453aa2007-07-10 00:01:28 +020077 * \param ilen length of the input data
Heiko Schocher633e03a2007-06-22 19:11:54 +020078 */
Simon Glass5f60fa22012-12-05 14:46:33 +000079void sha1_update(sha1_context *ctx, const unsigned char *input,
80 unsigned int ilen);
Heiko Schocher633e03a2007-06-22 19:11:54 +020081
82/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +020083 * \brief SHA-1 final digest
Heiko Schocher633e03a2007-06-22 19:11:54 +020084 *
Wolfgang Denka0453aa2007-07-10 00:01:28 +020085 * \param ctx SHA-1 context
Heiko Schocher633e03a2007-06-22 19:11:54 +020086 * \param output SHA-1 checksum result
87 */
88void sha1_finish( sha1_context *ctx, unsigned char output[20] );
89
90/**
Bartlomiej Siekada5045d2008-04-22 12:27:56 +020091 * \brief Output = SHA-1( input buffer ), with watchdog triggering
92 *
93 * \param input buffer holding the data
94 * \param ilen length of the input data
95 * \param output SHA-1 checksum result
96 * \param chunk_sz watchdog triggering period (in bytes of input processed)
97 */
Simon Glass5f60fa22012-12-05 14:46:33 +000098void sha1_csum_wd(const unsigned char *input, unsigned int ilen,
99 unsigned char *output, unsigned int chunk_sz);
Bartlomiej Siekada5045d2008-04-22 12:27:56 +0200100
101/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +0200102 * \brief Output = HMAC-SHA-1( input buffer, hmac key )
Heiko Schocher633e03a2007-06-22 19:11:54 +0200103 *
Wolfgang Denka0453aa2007-07-10 00:01:28 +0200104 * \param key HMAC secret key
Heiko Schocher633e03a2007-06-22 19:11:54 +0200105 * \param keylen length of the HMAC key
106 * \param input buffer holding the data
Wolfgang Denka0453aa2007-07-10 00:01:28 +0200107 * \param ilen length of the input data
Heiko Schocher633e03a2007-06-22 19:11:54 +0200108 * \param output HMAC-SHA-1 result
109 */
Simon Glass5f60fa22012-12-05 14:46:33 +0000110void sha1_hmac(const unsigned char *key, int keylen,
111 const unsigned char *input, unsigned int ilen,
112 unsigned char *output);
Heiko Schocher633e03a2007-06-22 19:11:54 +0200113
114/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +0200115 * \brief Checkup routine
Heiko Schocher633e03a2007-06-22 19:11:54 +0200116 *
Wolfgang Denka0453aa2007-07-10 00:01:28 +0200117 * \return 0 if successful, or 1 if the test failed
Heiko Schocher633e03a2007-06-22 19:11:54 +0200118 */
119int sha1_self_test( void );
120
121#ifdef __cplusplus
122}
123#endif
124
125#endif /* sha1.h */