blob: c2d62e9cf0f500301f3dcaf21aad9a82ca88b631 [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 Mao8e795492025-02-03 14:08:13 -080021#include "mbedtls_options.h"
Raymond Maoa571b982024-10-03 14:50:16 -070022#include <mbedtls/sha1.h>
23#endif
24
Heiko Schocher633e03a2007-06-22 19:11:54 +020025#ifdef __cplusplus
26extern "C" {
27#endif
28
29#define SHA1_SUM_POS -0x20
30#define SHA1_SUM_LEN 20
Andrew Duda3db9ff02016-11-08 18:53:40 +000031#define SHA1_DER_LEN 15
32
Raymond Mao9ec00882024-10-03 14:50:18 -070033#define SHA1_DEF_CHUNK_SZ 0x10000
34
Raymond Maof51f3552024-10-03 14:50:19 -070035#define K_IPAD_VAL 0x36
36#define K_OPAD_VAL 0x5C
37#define K_PAD_LEN 64
38
Andrew Duda3db9ff02016-11-08 18:53:40 +000039extern const uint8_t sha1_der_prefix[];
Heiko Schocher633e03a2007-06-22 19:11:54 +020040
Heinrich Schuchardtbd198b32024-12-06 12:37:09 +010041#if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO)
Raymond Maoa571b982024-10-03 14:50:16 -070042typedef mbedtls_sha1_context sha1_context;
43#else
Heiko Schocher633e03a2007-06-22 19:11:54 +020044/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +020045 * \brief SHA-1 context structure
Heiko Schocher633e03a2007-06-22 19:11:54 +020046 */
47typedef struct
48{
Wolfgang Denka0453aa2007-07-10 00:01:28 +020049 unsigned long total[2]; /*!< number of bytes processed */
Loic Poulainc7799b02022-06-01 20:26:28 +020050 uint32_t state[5]; /*!< intermediate digest state */
Wolfgang Denka0453aa2007-07-10 00:01:28 +020051 unsigned char buffer[64]; /*!< data block being processed */
Heiko Schocher633e03a2007-06-22 19:11:54 +020052}
53sha1_context;
Raymond Maoa571b982024-10-03 14:50:16 -070054#endif
Heiko Schocher633e03a2007-06-22 19:11:54 +020055
56/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +020057 * \brief SHA-1 context setup
Heiko Schocher633e03a2007-06-22 19:11:54 +020058 *
Wolfgang Denka0453aa2007-07-10 00:01:28 +020059 * \param ctx SHA-1 context to be initialized
Heiko Schocher633e03a2007-06-22 19:11:54 +020060 */
Raymond Maoa571b982024-10-03 14:50:16 -070061void sha1_starts(sha1_context *ctx);
Heiko Schocher633e03a2007-06-22 19:11:54 +020062
63/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +020064 * \brief SHA-1 process buffer
Heiko Schocher633e03a2007-06-22 19:11:54 +020065 *
Wolfgang Denka0453aa2007-07-10 00:01:28 +020066 * \param ctx SHA-1 context
Heiko Schocher633e03a2007-06-22 19:11:54 +020067 * \param input buffer holding the data
Wolfgang Denka0453aa2007-07-10 00:01:28 +020068 * \param ilen length of the input data
Heiko Schocher633e03a2007-06-22 19:11:54 +020069 */
Simon Glass5f60fa22012-12-05 14:46:33 +000070void sha1_update(sha1_context *ctx, const unsigned char *input,
71 unsigned int ilen);
Heiko Schocher633e03a2007-06-22 19:11:54 +020072
73/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +020074 * \brief SHA-1 final digest
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 output SHA-1 checksum result
78 */
79void sha1_finish( sha1_context *ctx, unsigned char output[20] );
80
81/**
Bartlomiej Siekada5045d2008-04-22 12:27:56 +020082 * \brief Output = SHA-1( input buffer ), with watchdog triggering
83 *
84 * \param input buffer holding the data
85 * \param ilen length of the input data
86 * \param output SHA-1 checksum result
87 * \param chunk_sz watchdog triggering period (in bytes of input processed)
88 */
Simon Glass5f60fa22012-12-05 14:46:33 +000089void sha1_csum_wd(const unsigned char *input, unsigned int ilen,
90 unsigned char *output, unsigned int chunk_sz);
Bartlomiej Siekada5045d2008-04-22 12:27:56 +020091
92/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +020093 * \brief Output = HMAC-SHA-1( input buffer, hmac key )
Heiko Schocher633e03a2007-06-22 19:11:54 +020094 *
Wolfgang Denka0453aa2007-07-10 00:01:28 +020095 * \param key HMAC secret key
Heiko Schocher633e03a2007-06-22 19:11:54 +020096 * \param keylen length of the HMAC key
97 * \param input buffer holding the data
Wolfgang Denka0453aa2007-07-10 00:01:28 +020098 * \param ilen length of the input data
Heiko Schocher633e03a2007-06-22 19:11:54 +020099 * \param output HMAC-SHA-1 result
100 */
Simon Glass5f60fa22012-12-05 14:46:33 +0000101void sha1_hmac(const unsigned char *key, int keylen,
102 const unsigned char *input, unsigned int ilen,
103 unsigned char *output);
Heiko Schocher633e03a2007-06-22 19:11:54 +0200104
105/**
Wolfgang Denka0453aa2007-07-10 00:01:28 +0200106 * \brief Checkup routine
Heiko Schocher633e03a2007-06-22 19:11:54 +0200107 *
Wolfgang Denka0453aa2007-07-10 00:01:28 +0200108 * \return 0 if successful, or 1 if the test failed
Heiko Schocher633e03a2007-06-22 19:11:54 +0200109 */
110int sha1_self_test( void );
111
112#ifdef __cplusplus
113}
114#endif
115
116#endif /* sha1.h */