source: azure_iot_hub/trunk/azure_iothub/c-utility/inc/azure_c_shared_utility/sha.h@ 389

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

ビルドが通るよう更新

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