1 /* SPDX-License-Identifier: LGPL-2.1 */
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
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 
17 #include <linux/kconfig.h>
18 #include <linux/types.h>
19 
20 #if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO)
21 #include "mbedtls_options.h"
22 #include <mbedtls/sha1.h>
23 #endif
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #define SHA1_SUM_POS	-0x20
30 #define SHA1_SUM_LEN	20
31 #define SHA1_DER_LEN	15
32 
33 #define SHA1_DEF_CHUNK_SZ 0x10000
34 
35 #define K_IPAD_VAL 0x36
36 #define K_OPAD_VAL 0x5C
37 #define K_PAD_LEN 64
38 
39 extern const uint8_t sha1_der_prefix[];
40 
41 #if CONFIG_IS_ENABLED(MBEDTLS_LIB_CRYPTO)
42 typedef mbedtls_sha1_context sha1_context;
43 #else
44 /**
45  * \brief	   SHA-1 context structure
46  */
47 typedef struct
48 {
49     unsigned long total[2];	/*!< number of bytes processed	*/
50     uint32_t state[5];		/*!< intermediate digest state	*/
51     unsigned char buffer[64];	/*!< data block being processed */
52 }
53 sha1_context;
54 #endif
55 
56 /**
57  * \brief	   SHA-1 context setup
58  *
59  * \param ctx	   SHA-1 context to be initialized
60  */
61 void sha1_starts(sha1_context *ctx);
62 
63 /**
64  * \brief	   SHA-1 process buffer
65  *
66  * \param ctx	   SHA-1 context
67  * \param input    buffer holding the  data
68  * \param ilen	   length of the input data
69  */
70 void sha1_update(sha1_context *ctx, const unsigned char *input,
71 		 unsigned int ilen);
72 
73 /**
74  * \brief	   SHA-1 final digest
75  *
76  * \param ctx	   SHA-1 context
77  * \param output   SHA-1 checksum result
78  */
79 void sha1_finish( sha1_context *ctx, unsigned char output[20] );
80 
81 /**
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  */
89 void sha1_csum_wd(const unsigned char *input, unsigned int ilen,
90 		unsigned char *output, unsigned int chunk_sz);
91 
92 /**
93  * \brief	   Output = HMAC-SHA-1( input buffer, hmac key )
94  *
95  * \param key	   HMAC secret key
96  * \param keylen   length of the HMAC key
97  * \param input    buffer holding the  data
98  * \param ilen	   length of the input data
99  * \param output   HMAC-SHA-1 result
100  */
101 void sha1_hmac(const unsigned char *key, int keylen,
102 		const unsigned char *input, unsigned int ilen,
103 		unsigned char *output);
104 
105 /**
106  * \brief	   Checkup routine
107  *
108  * \return	   0 if successful, or 1 if the test failed
109  */
110 int sha1_self_test( void );
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 #endif /* sha1.h */
117