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