1 /*
2  * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3  */
4 
5 #ifndef _INFRA_SHA1_H_
6 #define _INFRA_SHA1_H_
7 
8 #include "linkkit/infra/infra_types.h"
9 
10 #define SHA1_DIGEST_SIZE (20)
11 
12 /**
13  * \brief          SHA-1 context structure
14  */
15 typedef struct {
16     uint32_t total[2];        /*!< number of bytes processed  */
17     uint32_t state[5];        /*!< intermediate digest state  */
18     unsigned char buffer[64]; /*!< data block being processed */
19 } iot_sha1_context;
20 
21 /**
22  * \brief          Initialize SHA-1 context
23  *
24  * \param ctx      SHA-1 context to be initialized
25  */
26 void utils_sha1_init(iot_sha1_context *ctx);
27 
28 /**
29  * \brief          Clear SHA-1 context
30  *
31  * \param ctx      SHA-1 context to be cleared
32  */
33 void utils_sha1_free(iot_sha1_context *ctx);
34 
35 /**
36  * \brief          Clone (the state of) a SHA-1 context
37  *
38  * \param dst      The destination context
39  * \param src      The context to be cloned
40  */
41 void utils_sha1_clone(iot_sha1_context *dst, const iot_sha1_context *src);
42 
43 /**
44  * \brief          SHA-1 context setup
45  *
46  * \param ctx      context to be initialized
47  */
48 void utils_sha1_starts(iot_sha1_context *ctx);
49 
50 /**
51  * \brief          SHA-1 process buffer
52  *
53  * \param ctx      SHA-1 context
54  * \param input    buffer holding the  data
55  * \param ilen     length of the input data
56  */
57 void utils_sha1_update(iot_sha1_context *ctx, const unsigned char *input,
58                        uint32_t ilen);
59 
60 /**
61  * \brief          SHA-1 final digest
62  *
63  * \param ctx      SHA-1 context
64  * \param output   SHA-1 checksum result
65  */
66 void utils_sha1_finish(iot_sha1_context *ctx, unsigned char output[20]);
67 
68 /* Internal use */
69 void utils_sha1_process(iot_sha1_context *ctx, const unsigned char data[64]);
70 
71 /**
72  * \brief          Output = SHA-1( input buffer )
73  *
74  * \param input    buffer holding the  data
75  * \param ilen     length of the input data
76  * \param output   SHA-1 checksum result
77  */
78 void utils_sha1(const unsigned char *input, uint32_t ilen,
79                 unsigned char output[20]);
80 
81 void utils_hmac_sha1(const char *msg, int msg_len, char *digest,
82                      const char *key, int key_len);
83 void utils_hmac_sha1_hex(const char *msg, int msg_len, char *digest,
84                          const char *key, int key_len);
85 
86 #endif
87