blob: dd66258bbe94ac4c3cee4267bf89b0dc2d663b20 [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
Heinrich Schuchardtbd198b32024-12-06 12:37:09 +010017#include <linux/kconfig.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060018#include <linux/types.h>
19
Heinrich Schuchardtbd198b32024-12-06 12:37:09 +010020#if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO)
Raymond Maoa571b982024-10-03 14:50:16 -070021/*
22 * FIXME:
23 * MbedTLS define the members of "mbedtls_sha256_context" as private,
24 * but "state" needs to be access by arch/arm/cpu/armv8/sha1_ce_glue.
25 * MBEDTLS_ALLOW_PRIVATE_ACCESS needs to be enabled to allow the external
26 * access.
27 * Directly including <external/mbedtls/library/common.h> is not allowed,
28 * since this will include <malloc.h> and break the sandbox test.
29 */
30#define MBEDTLS_ALLOW_PRIVATE_ACCESS
31
32#include <mbedtls/sha1.h>
33#endif
34
Heiko Schocher633e03a2007-06-22 19:11:54 +020035#ifdef __cplusplus
36extern "C" {
37#endif
38
39#define SHA1_SUM_POS -0x20
40#define SHA1_SUM_LEN 20
Andrew Duda3db9ff02016-11-08 18:53:40 +000041#define SHA1_DER_LEN 15
42
Raymond Mao9ec00882024-10-03 14:50:18 -070043#define SHA1_DEF_CHUNK_SZ 0x10000
44
Raymond Maof51f3552024-10-03 14:50:19 -070045#define K_IPAD_VAL 0x36
46#define K_OPAD_VAL 0x5C
47#define K_PAD_LEN 64
48
Andrew Duda3db9ff02016-11-08 18:53:40 +000049extern const uint8_t sha1_der_prefix[];
Heiko Schocher633e03a2007-06-22 19:11:54 +020050
Heinrich Schuchardtbd198b32024-12-06 12:37:09 +010051#if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO)
Raymond Maoa571b982024-10-03 14:50:16 -070052typedef mbedtls_sha1_context sha1_context;
53#else
Heiko Schocher633e03a2007-06-22 19:11:54 +020054/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +020055 * \brief SHA-1 context structure
Heiko Schocher633e03a2007-06-22 19:11:54 +020056 */
57typedef struct
58{
Wolfgang Denka0453aa2007-07-10 00:01:28 +020059 unsigned long total[2]; /*!< number of bytes processed */
Loic Poulainc7799b02022-06-01 20:26:28 +020060 uint32_t state[5]; /*!< intermediate digest state */
Wolfgang Denka0453aa2007-07-10 00:01:28 +020061 unsigned char buffer[64]; /*!< data block being processed */
Heiko Schocher633e03a2007-06-22 19:11:54 +020062}
63sha1_context;
Raymond Maoa571b982024-10-03 14:50:16 -070064#endif
Heiko Schocher633e03a2007-06-22 19:11:54 +020065
66/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +020067 * \brief SHA-1 context setup
Heiko Schocher633e03a2007-06-22 19:11:54 +020068 *
Wolfgang Denka0453aa2007-07-10 00:01:28 +020069 * \param ctx SHA-1 context to be initialized
Heiko Schocher633e03a2007-06-22 19:11:54 +020070 */
Raymond Maoa571b982024-10-03 14:50:16 -070071void sha1_starts(sha1_context *ctx);
Heiko Schocher633e03a2007-06-22 19:11:54 +020072
73/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +020074 * \brief SHA-1 process buffer
Heiko Schocher633e03a2007-06-22 19:11:54 +020075 *
Wolfgang Denka0453aa2007-07-10 00:01:28 +020076 * \param ctx SHA-1 context
Heiko Schocher633e03a2007-06-22 19:11:54 +020077 * \param input buffer holding the data
Wolfgang Denka0453aa2007-07-10 00:01:28 +020078 * \param ilen length of the input data
Heiko Schocher633e03a2007-06-22 19:11:54 +020079 */
Simon Glass5f60fa22012-12-05 14:46:33 +000080void sha1_update(sha1_context *ctx, const unsigned char *input,
81 unsigned int ilen);
Heiko Schocher633e03a2007-06-22 19:11:54 +020082
83/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +020084 * \brief SHA-1 final digest
Heiko Schocher633e03a2007-06-22 19:11:54 +020085 *
Wolfgang Denka0453aa2007-07-10 00:01:28 +020086 * \param ctx SHA-1 context
Heiko Schocher633e03a2007-06-22 19:11:54 +020087 * \param output SHA-1 checksum result
88 */
89void sha1_finish( sha1_context *ctx, unsigned char output[20] );
90
91/**
Bartlomiej Siekada5045d2008-04-22 12:27:56 +020092 * \brief Output = SHA-1( input buffer ), with watchdog triggering
93 *
94 * \param input buffer holding the data
95 * \param ilen length of the input data
96 * \param output SHA-1 checksum result
97 * \param chunk_sz watchdog triggering period (in bytes of input processed)
98 */
Simon Glass5f60fa22012-12-05 14:46:33 +000099void sha1_csum_wd(const unsigned char *input, unsigned int ilen,
100 unsigned char *output, unsigned int chunk_sz);
Bartlomiej Siekada5045d2008-04-22 12:27:56 +0200101
102/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +0200103 * \brief Output = HMAC-SHA-1( input buffer, hmac key )
Heiko Schocher633e03a2007-06-22 19:11:54 +0200104 *
Wolfgang Denka0453aa2007-07-10 00:01:28 +0200105 * \param key HMAC secret key
Heiko Schocher633e03a2007-06-22 19:11:54 +0200106 * \param keylen length of the HMAC key
107 * \param input buffer holding the data
Wolfgang Denka0453aa2007-07-10 00:01:28 +0200108 * \param ilen length of the input data
Heiko Schocher633e03a2007-06-22 19:11:54 +0200109 * \param output HMAC-SHA-1 result
110 */
Simon Glass5f60fa22012-12-05 14:46:33 +0000111void sha1_hmac(const unsigned char *key, int keylen,
112 const unsigned char *input, unsigned int ilen,
113 unsigned char *output);
Heiko Schocher633e03a2007-06-22 19:11:54 +0200114
115/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +0200116 * \brief Checkup routine
Heiko Schocher633e03a2007-06-22 19:11:54 +0200117 *
Wolfgang Denka0453aa2007-07-10 00:01:28 +0200118 * \return 0 if successful, or 1 if the test failed
Heiko Schocher633e03a2007-06-22 19:11:54 +0200119 */
120int sha1_self_test( void );
121
122#ifdef __cplusplus
123}
124#endif
125
126#endif /* sha1.h */