source: azure_iot_hub_f767zi/trunk/azure_iot_sdk/c-utility/inc/azure_c_shared_utility/sha.h@ 457

Last change on this file since 457 was 457, checked in by coas-nagasima, 4 years ago

ファイルを追加

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-chdr;charset=UTF-8
File size: 9.1 KB
Line 
1// Copyright (c) Microsoft. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4/**************************** sha.h ****************************/
5/******************* See RFC 4634 for details ******************/
6#ifndef _SHA_H_
7#define _SHA_H_
8
9/*
10 * Description:
11 * This file implements the Secure Hash Signature Standard
12 * algorithms as defined in the National Institute of Standards
13 * and Technology Federal Information Processing Standards
14 * Publication (FIPS PUB) 180-1 published on April 17, 1995, 180-2
15 * published on August 1, 2002, and the FIPS PUB 180-2 Change
16 * Notice published on February 28, 2004.
17 *
18 * A combined document showing all algorithms is available at
19 * http://csrc.nist.gov/publications/fips/
20 * fips180-2/fips180-2withchangenotice.pdf
21 *
22 * The five hashes are defined in these sizes:
23 * SHA-1 20 byte / 160 bit
24 * SHA-224 28 byte / 224 bit
25 * SHA-256 32 byte / 256 bit
26 * SHA-384 48 byte / 384 bit
27 * SHA-512 64 byte / 512 bit
28 */
29
30#ifdef __cplusplus
31#include <cstdint>
32#else
33#include <stdint.h>
34#endif
35
36/*
37 * If you do not have the ISO standard stdint.h header file, then you
38 * must typedef the following:
39 * name meaning
40 * uint64_t unsigned 64 bit integer
41 * uint32_t unsigned 32 bit integer
42 * uint8_t unsigned 8 bit integer (i.e., unsigned char)
43 * int_least16_t integer of >= 16 bits
44 *
45 */
46
47#ifdef __cplusplus
48extern "C"
49{
50#endif
51
52#ifndef _SHA_enum_
53#define _SHA_enum_
54/*
55 * All SHA functions return one of these values.
56 */
57enum {
58 shaSuccess = 0,
59 shaNull, /* Null pointer parameter */
60 shaInputTooLong, /* input data too long */
61 shaStateError, /* called Input after FinalBits or Result */
62 shaBadParam /* passed a bad parameter */
63};
64#endif /* _SHA_enum_ */
65
66/*
67 * These constants hold size information for each of the SHA
68 * hashing operations
69 */
70enum {
71 SHA1_Message_Block_Size = 64, SHA224_Message_Block_Size = 64,
72 SHA256_Message_Block_Size = 64, SHA384_Message_Block_Size = 128,
73 SHA512_Message_Block_Size = 128,
74 USHA_Max_Message_Block_Size = SHA512_Message_Block_Size,
75
76 SHA1HashSize = 20, SHA224HashSize = 28, SHA256HashSize = 32,
77 SHA384HashSize = 48, SHA512HashSize = 64,
78 USHAMaxHashSize = SHA512HashSize,
79
80 SHA1HashSizeBits = 160, SHA224HashSizeBits = 224,
81 SHA256HashSizeBits = 256, SHA384HashSizeBits = 384,
82 SHA512HashSizeBits = 512, USHAMaxHashSizeBits = SHA512HashSizeBits
83};
84
85/*
86 * These constants are used in the USHA (unified sha) functions.
87 */
88typedef enum SHAversion {
89 SHA1, SHA224, SHA256, SHA384, SHA512
90} SHAversion;
91
92/*
93 * This structure will hold context information for the SHA-1
94 * hashing operation.
95 */
96typedef struct SHA1Context {
97 uint32_t Intermediate_Hash[SHA1HashSize/4]; /* Message Digest */
98
99 uint32_t Length_Low; /* Message length in bits */
100 uint32_t Length_High; /* Message length in bits */
101
102 int_least16_t Message_Block_Index; /* Message_Block array index */
103 /* 512-bit message blocks */
104 uint8_t Message_Block[SHA1_Message_Block_Size];
105
106 int Computed; /* Is the digest computed? */
107 int Corrupted; /* Is the digest corrupted? */
108} SHA1Context;
109
110/*
111 * This structure will hold context information for the SHA-256
112 * hashing operation.
113 */
114typedef struct SHA256Context {
115 uint32_t Intermediate_Hash[SHA256HashSize/4]; /* Message Digest */
116
117 uint32_t Length_Low; /* Message length in bits */
118 uint32_t Length_High; /* Message length in bits */
119
120 int_least16_t Message_Block_Index; /* Message_Block array index */
121 /* 512-bit message blocks */
122 uint8_t Message_Block[SHA256_Message_Block_Size];
123
124 int Computed; /* Is the digest computed? */
125 int Corrupted; /* Is the digest corrupted? */
126} SHA256Context;
127
128/*
129 * This structure will hold context information for the SHA-512
130 * hashing operation.
131 */
132typedef struct SHA512Context {
133#ifdef USE_32BIT_ONLY
134 uint32_t Intermediate_Hash[SHA512HashSize/4]; /* Message Digest */
135 uint32_t Length[4]; /* Message length in bits */
136#else /* !USE_32BIT_ONLY */
137 uint64_t Intermediate_Hash[SHA512HashSize/8]; /* Message Digest */
138 uint64_t Length_Low, Length_High; /* Message length in bits */
139#endif /* USE_32BIT_ONLY */
140
141 int_least16_t Message_Block_Index; /* Message_Block array index */
142 /* 1024-bit message blocks */
143 uint8_t Message_Block[SHA512_Message_Block_Size];
144
145 int Computed; /* Is the digest computed?*/
146 int Corrupted; /* Is the digest corrupted? */
147} SHA512Context;
148
149/*
150 * This structure will hold context information for the SHA-224
151 * hashing operation. It uses the SHA-256 structure for computation.
152 */
153typedef struct SHA256Context SHA224Context;
154
155/*
156 * This structure will hold context information for the SHA-384
157 * hashing operation. It uses the SHA-512 structure for computation.
158 */
159typedef struct SHA512Context SHA384Context;
160
161/*
162 * This structure holds context information for all SHA
163 * hashing operations.
164 */
165typedef struct USHAContext {
166 int whichSha; /* which SHA is being used */
167 union {
168 SHA1Context sha1Context;
169 SHA224Context sha224Context; SHA256Context sha256Context;
170 SHA384Context sha384Context; SHA512Context sha512Context;
171 } ctx;
172} USHAContext;
173
174/*
175 * This structure will hold context information for the HMAC
176 * keyed hashing operation.
177 */
178typedef struct HMACContext {
179 int whichSha; /* which SHA is being used */
180 int hashSize; /* hash size of SHA being used */
181 int blockSize; /* block size of SHA being used */
182 USHAContext shaContext; /* SHA context */
183 unsigned char k_opad[USHA_Max_Message_Block_Size];
184 /* outer padding - key XORd with opad */
185} HMACContext;
186
187
188/*
189 * Function Prototypes
190 */
191
192/* SHA-1 */
193int SHA1Reset(SHA1Context *);
194int SHA1Input(SHA1Context *, const uint8_t *bytes, unsigned int bytecount);
195int SHA1FinalBits(SHA1Context *, const uint8_t bits, unsigned int bitcount);
196int SHA1Result(SHA1Context *, uint8_t Message_Digest[SHA1HashSize]);
197
198/* SHA-224 */
199int SHA224Reset(SHA224Context *);
200int SHA224Input(SHA224Context *, const uint8_t *bytes, unsigned int bytecount);
201int SHA224FinalBits(SHA224Context *, const uint8_t bits, unsigned int bitcount);
202int SHA224Result(SHA224Context *, uint8_t Message_Digest[SHA224HashSize]);
203
204/* SHA-256 */
205int SHA256Reset(SHA256Context *);
206int SHA256Input(SHA256Context *, const uint8_t *bytes, unsigned int bytecount);
207int SHA256FinalBits(SHA256Context *, const uint8_t bits, unsigned int bitcount);
208int SHA256Result(SHA256Context *, uint8_t Message_Digest[SHA256HashSize]);
209
210/* SHA-384 */
211int SHA384Reset(SHA384Context *);
212int SHA384Input(SHA384Context *, const uint8_t *bytes, unsigned int bytecount);
213int SHA384FinalBits(SHA384Context *, const uint8_t bits, unsigned int bitcount);
214int SHA384Result(SHA384Context *, uint8_t Message_Digest[SHA384HashSize]);
215
216/* SHA-512 */
217int SHA512Reset(SHA512Context *);
218int SHA512Input(SHA512Context *, const uint8_t *bytes, unsigned int bytecount);
219int SHA512FinalBits(SHA512Context *, const uint8_t bits, unsigned int bitcount);
220int SHA512Result(SHA512Context *, uint8_t Message_Digest[SHA512HashSize]);
221
222/* Unified SHA functions, chosen by whichSha */
223int USHAReset(USHAContext *, SHAversion whichSha);
224int USHAInput(USHAContext *, const uint8_t *bytes, unsigned int bytecount);
225int USHAFinalBits(USHAContext *, const uint8_t bits, unsigned int bitcount);
226int USHAResult(USHAContext *, uint8_t Message_Digest[USHAMaxHashSize]);
227int USHABlockSize(enum SHAversion whichSha);
228int USHAHashSize(enum SHAversion whichSha);
229int USHAHashSizeBits(enum SHAversion whichSha);
230
231/*
232 * HMAC Keyed-Hashing for Message Authentication, RFC2104,
233 * for all SHAs.
234 * This interface allows a fixed-length text input to be used.
235 */
236int hmac(SHAversion whichSha, /* which SHA algorithm to use */
237 const unsigned char *text, /* pointer to data stream */
238 int text_len, /* length of data stream */
239 const unsigned char *key, /* pointer to authentication key */
240 int key_len, /* length of authentication key */
241 uint8_t digest[USHAMaxHashSize]); /* caller digest to fill in */
242
243/*
244 * HMAC Keyed-Hashing for Message Authentication, RFC2104,
245 * for all SHAs.
246 * This interface allows any length of text input to be used.
247 */
248int hmacReset(HMACContext *ctx, enum SHAversion whichSha, const unsigned char *key, int key_len);
249int hmacInput(HMACContext *ctx, const unsigned char *text, int text_len);
250
251int hmacFinalBits(HMACContext *ctx, const uint8_t bits, unsigned int bitcount);
252int hmacResult(HMACContext *ctx, uint8_t digest[USHAMaxHashSize]);
253
254
255#ifdef __cplusplus
256}
257#endif
258
259#endif /* _SHA_H_ */
260
Note: See TracBrowser for help on using the repository browser.