1 /**************************** usha.c ****************************/
2 /******************** See RFC 4634 for details ******************/
5 * This file implements a unified interface to the SHA algorithms.
14 * This function will initialize the SHA Context in preparation
15 * for computing a new SHA message digest.
19 * The context to reset.
21 * Selects which SHA reset to call
27 int USHAReset(USHAContext *ctx, enum SHAversion whichSha)
30 ctx->whichSha = whichSha;
32 case SHA1: return SHA1Reset((SHA1Context*)&ctx->ctx);
33 case SHA224: return SHA224Reset((SHA224Context*)&ctx->ctx);
34 case SHA256: return SHA256Reset((SHA256Context*)&ctx->ctx);
35 case SHA384: return SHA384Reset((SHA384Context*)&ctx->ctx);
36 case SHA512: return SHA512Reset((SHA512Context*)&ctx->ctx);
37 default: return shaBadParam;
48 * This function accepts an array of octets as the next portion
53 * The SHA context to update
55 * An array of characters representing the next portion of
58 * The length of the message in message_array
64 int USHAInput(USHAContext *ctx,
65 const uint8_t *bytes, unsigned int bytecount)
68 switch (ctx->whichSha) {
70 return SHA1Input((SHA1Context*)&ctx->ctx, bytes, bytecount);
72 return SHA224Input((SHA224Context*)&ctx->ctx, bytes,
75 return SHA256Input((SHA256Context*)&ctx->ctx, bytes,
78 return SHA384Input((SHA384Context*)&ctx->ctx, bytes,
81 return SHA512Input((SHA512Context*)&ctx->ctx, bytes,
83 default: return shaBadParam;
93 * This function will add in any final bits of the message.
97 * The SHA context to update
99 * The final bits of the message, in the upper portion of the
100 * byte. (Use 0b###00000 instead of 0b00000### to input the
103 * The number of bits in message_bits, between 1 and 7.
108 int USHAFinalBits(USHAContext *ctx,
109 const uint8_t bits, unsigned int bitcount)
112 switch (ctx->whichSha) {
114 return SHA1FinalBits((SHA1Context*)&ctx->ctx, bits, bitcount);
116 return SHA224FinalBits((SHA224Context*)&ctx->ctx, bits,
119 return SHA256FinalBits((SHA256Context*)&ctx->ctx, bits,
122 return SHA384FinalBits((SHA384Context*)&ctx->ctx, bits,
125 return SHA512FinalBits((SHA512Context*)&ctx->ctx, bits,
127 default: return shaBadParam;
138 * This function will return the 160-bit message digest into the
139 * Message_Digest array provided by the caller.
140 * NOTE: The first octet of hash is stored in the 0th element,
141 * the last octet of hash in the 19th element.
145 * The context to use to calculate the SHA-1 hash.
146 * Message_Digest: [out]
147 * Where the digest is returned.
153 int USHAResult(USHAContext *ctx,
154 uint8_t Message_Digest[USHAMaxHashSize])
157 switch (ctx->whichSha) {
159 return SHA1Result((SHA1Context*)&ctx->ctx, Message_Digest);
161 return SHA224Result((SHA224Context*)&ctx->ctx, Message_Digest);
163 return SHA256Result((SHA256Context*)&ctx->ctx, Message_Digest);
165 return SHA384Result((SHA384Context*)&ctx->ctx, Message_Digest);
167 return SHA512Result((SHA512Context*)&ctx->ctx, Message_Digest);
168 default: return shaBadParam;
179 * This function will return the blocksize for the given SHA
184 * which SHA algorithm to query
190 int USHABlockSize(enum SHAversion whichSha)
193 case SHA1: return SHA1_Message_Block_Size;
194 case SHA224: return SHA224_Message_Block_Size;
195 case SHA256: return SHA256_Message_Block_Size;
196 case SHA384: return SHA384_Message_Block_Size;
198 case SHA512: return SHA512_Message_Block_Size;
206 * This function will return the hashsize for the given SHA
211 * which SHA algorithm to query
217 int USHAHashSize(enum SHAversion whichSha)
220 case SHA1: return SHA1HashSize;
221 case SHA224: return SHA224HashSize;
222 case SHA256: return SHA256HashSize;
223 case SHA384: return SHA384HashSize;
225 case SHA512: return SHA512HashSize;
233 * This function will return the hashsize for the given SHA
234 * algorithm, expressed in bits.
238 * which SHA algorithm to query
244 int USHAHashSizeBits(enum SHAversion whichSha)
247 case SHA1: return SHA1HashSizeBits;
248 case SHA224: return SHA224HashSizeBits;
249 case SHA256: return SHA256HashSizeBits;
250 case SHA384: return SHA384HashSizeBits;
252 case SHA512: return SHA512HashSizeBits;