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