Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | /* SPDX-License-Identifier: LGPL-2.1 */ |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 2 | /** |
| 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 Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 8 | */ |
| 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 Schuchardt | bd198b3 | 2024-12-06 12:37:09 +0100 | [diff] [blame] | 17 | #include <linux/kconfig.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 18 | #include <linux/types.h> |
| 19 | |
Heinrich Schuchardt | bd198b3 | 2024-12-06 12:37:09 +0100 | [diff] [blame] | 20 | #if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO) |
Raymond Mao | 8e79549 | 2025-02-03 14:08:13 -0800 | [diff] [blame^] | 21 | #include "mbedtls_options.h" |
Raymond Mao | a571b98 | 2024-10-03 14:50:16 -0700 | [diff] [blame] | 22 | #include <mbedtls/sha1.h> |
| 23 | #endif |
| 24 | |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
| 27 | #endif |
| 28 | |
| 29 | #define SHA1_SUM_POS -0x20 |
| 30 | #define SHA1_SUM_LEN 20 |
Andrew Duda | 3db9ff0 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 31 | #define SHA1_DER_LEN 15 |
| 32 | |
Raymond Mao | 9ec0088 | 2024-10-03 14:50:18 -0700 | [diff] [blame] | 33 | #define SHA1_DEF_CHUNK_SZ 0x10000 |
| 34 | |
Raymond Mao | f51f355 | 2024-10-03 14:50:19 -0700 | [diff] [blame] | 35 | #define K_IPAD_VAL 0x36 |
| 36 | #define K_OPAD_VAL 0x5C |
| 37 | #define K_PAD_LEN 64 |
| 38 | |
Andrew Duda | 3db9ff0 | 2016-11-08 18:53:40 +0000 | [diff] [blame] | 39 | extern const uint8_t sha1_der_prefix[]; |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 40 | |
Heinrich Schuchardt | bd198b3 | 2024-12-06 12:37:09 +0100 | [diff] [blame] | 41 | #if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO) |
Raymond Mao | a571b98 | 2024-10-03 14:50:16 -0700 | [diff] [blame] | 42 | typedef mbedtls_sha1_context sha1_context; |
| 43 | #else |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 44 | /** |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 45 | * \brief SHA-1 context structure |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 46 | */ |
| 47 | typedef struct |
| 48 | { |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 49 | unsigned long total[2]; /*!< number of bytes processed */ |
Loic Poulain | c7799b0 | 2022-06-01 20:26:28 +0200 | [diff] [blame] | 50 | uint32_t state[5]; /*!< intermediate digest state */ |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 51 | unsigned char buffer[64]; /*!< data block being processed */ |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 52 | } |
| 53 | sha1_context; |
Raymond Mao | a571b98 | 2024-10-03 14:50:16 -0700 | [diff] [blame] | 54 | #endif |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 55 | |
| 56 | /** |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 57 | * \brief SHA-1 context setup |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 58 | * |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 59 | * \param ctx SHA-1 context to be initialized |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 60 | */ |
Raymond Mao | a571b98 | 2024-10-03 14:50:16 -0700 | [diff] [blame] | 61 | void sha1_starts(sha1_context *ctx); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 62 | |
| 63 | /** |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 64 | * \brief SHA-1 process buffer |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 65 | * |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 66 | * \param ctx SHA-1 context |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 67 | * \param input buffer holding the data |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 68 | * \param ilen length of the input data |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 69 | */ |
Simon Glass | 5f60fa2 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 70 | void sha1_update(sha1_context *ctx, const unsigned char *input, |
| 71 | unsigned int ilen); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 72 | |
| 73 | /** |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 74 | * \brief SHA-1 final digest |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 75 | * |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 76 | * \param ctx SHA-1 context |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 77 | * \param output SHA-1 checksum result |
| 78 | */ |
| 79 | void sha1_finish( sha1_context *ctx, unsigned char output[20] ); |
| 80 | |
| 81 | /** |
Bartlomiej Sieka | da5045d | 2008-04-22 12:27:56 +0200 | [diff] [blame] | 82 | * \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 Glass | 5f60fa2 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 89 | void sha1_csum_wd(const unsigned char *input, unsigned int ilen, |
| 90 | unsigned char *output, unsigned int chunk_sz); |
Bartlomiej Sieka | da5045d | 2008-04-22 12:27:56 +0200 | [diff] [blame] | 91 | |
| 92 | /** |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 93 | * \brief Output = HMAC-SHA-1( input buffer, hmac key ) |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 94 | * |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 95 | * \param key HMAC secret key |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 96 | * \param keylen length of the HMAC key |
| 97 | * \param input buffer holding the data |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 98 | * \param ilen length of the input data |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 99 | * \param output HMAC-SHA-1 result |
| 100 | */ |
Simon Glass | 5f60fa2 | 2012-12-05 14:46:33 +0000 | [diff] [blame] | 101 | void sha1_hmac(const unsigned char *key, int keylen, |
| 102 | const unsigned char *input, unsigned int ilen, |
| 103 | unsigned char *output); |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 104 | |
| 105 | /** |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 106 | * \brief Checkup routine |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 107 | * |
Wolfgang Denk | a0453aa | 2007-07-10 00:01:28 +0200 | [diff] [blame] | 108 | * \return 0 if successful, or 1 if the test failed |
Heiko Schocher | 633e03a | 2007-06-22 19:11:54 +0200 | [diff] [blame] | 109 | */ |
| 110 | int sha1_self_test( void ); |
| 111 | |
| 112 | #ifdef __cplusplus |
| 113 | } |
| 114 | #endif |
| 115 | |
| 116 | #endif /* sha1.h */ |