]> err.no Git - yubikey-personalization/blob - usha.c
Merge tag 'v1.15.1'
[yubikey-personalization] / usha.c
1 /**************************** usha.c ****************************/
2 /******************** See RFC 4634 for details ******************/
3 /*
4  *  Description:
5  *     This file implements a unified interface to the SHA algorithms.
6  */
7
8 #include "sha.h"
9
10 /*
11  *  USHAReset
12  *
13  *  Description:
14  *      This function will initialize the SHA Context in preparation
15  *      for computing a new SHA message digest.
16  *
17  *  Parameters:
18  *      context: [in/out]
19  *          The context to reset.
20  *      whichSha: [in]
21  *          Selects which SHA reset to call
22  *
23  *  Returns:
24  *      sha Error Code.
25  *
26  */
27 int USHAReset(USHAContext *ctx, enum SHAversion whichSha)
28 {
29   if (ctx) {
30     ctx->whichSha = whichSha;
31     switch (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;
38     }
39   } else {
40     return shaNull;
41   }
42 }
43
44 /*
45  *  USHAInput
46  *
47  *  Description:
48  *      This function accepts an array of octets as the next portion
49  *      of the message.
50  *
51  *  Parameters:
52  *      context: [in/out]
53  *          The SHA context to update
54  *      message_array: [in]
55  *          An array of characters representing the next portion of
56  *          the message.
57  *      length: [in]
58  *          The length of the message in message_array
59  *
60  *  Returns:
61  *      sha Error Code.
62  *
63  */
64 int USHAInput(USHAContext *ctx,
65               const uint8_t *bytes, unsigned int bytecount)
66 {
67   if (ctx) {
68     switch (ctx->whichSha) {
69       case SHA1:
70         return SHA1Input((SHA1Context*)&ctx->ctx, bytes, bytecount);
71       case SHA224:
72         return SHA224Input((SHA224Context*)&ctx->ctx, bytes,
73             bytecount);
74       case SHA256:
75         return SHA256Input((SHA256Context*)&ctx->ctx, bytes,
76             bytecount);
77       case SHA384:
78         return SHA384Input((SHA384Context*)&ctx->ctx, bytes,
79             bytecount);
80       case SHA512:
81         return SHA512Input((SHA512Context*)&ctx->ctx, bytes,
82             bytecount);
83       default: return shaBadParam;
84     }
85   } else {
86     return shaNull;
87   }
88 }
89 /*
90  * USHAFinalBits
91  *
92  * Description:
93  *   This function will add in any final bits of the message.
94  *
95  * Parameters:
96  *   context: [in/out]
97  *     The SHA context to update
98  *   message_bits: [in]
99  *     The final bits of the message, in the upper portion of the
100  *     byte. (Use 0b###00000 instead of 0b00000### to input the
101  *     three bits ###.)
102  *   length: [in]
103  *     The number of bits in message_bits, between 1 and 7.
104  *
105  * Returns:
106  *   sha Error Code.
107  */
108 int USHAFinalBits(USHAContext *ctx,
109                   const uint8_t bits, unsigned int bitcount)
110 {
111   if (ctx) {
112     switch (ctx->whichSha) {
113       case SHA1:
114         return SHA1FinalBits((SHA1Context*)&ctx->ctx, bits, bitcount);
115       case SHA224:
116         return SHA224FinalBits((SHA224Context*)&ctx->ctx, bits,
117             bitcount);
118       case SHA256:
119         return SHA256FinalBits((SHA256Context*)&ctx->ctx, bits,
120             bitcount);
121       case SHA384:
122         return SHA384FinalBits((SHA384Context*)&ctx->ctx, bits,
123             bitcount);
124       case SHA512:
125         return SHA512FinalBits((SHA512Context*)&ctx->ctx, bits,
126             bitcount);
127       default: return shaBadParam;
128     }
129   } else {
130     return shaNull;
131   }
132 }
133
134 /*
135  * USHAResult
136  *
137  * Description:
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.
142  *
143  * Parameters:
144  *   context: [in/out]
145  *     The context to use to calculate the SHA-1 hash.
146  *   Message_Digest: [out]
147  *     Where the digest is returned.
148  *
149  * Returns:
150  *   sha Error Code.
151  *
152  */
153 int USHAResult(USHAContext *ctx,
154                uint8_t Message_Digest[USHAMaxHashSize])
155 {
156   if (ctx) {
157     switch (ctx->whichSha) {
158       case SHA1:
159         return SHA1Result((SHA1Context*)&ctx->ctx, Message_Digest);
160       case SHA224:
161         return SHA224Result((SHA224Context*)&ctx->ctx, Message_Digest);
162       case SHA256:
163         return SHA256Result((SHA256Context*)&ctx->ctx, Message_Digest);
164       case SHA384:
165         return SHA384Result((SHA384Context*)&ctx->ctx, Message_Digest);
166       case SHA512:
167         return SHA512Result((SHA512Context*)&ctx->ctx, Message_Digest);
168       default: return shaBadParam;
169     }
170   } else {
171     return shaNull;
172   }
173 }
174
175 /*
176  * USHABlockSize
177  *
178  * Description:
179  *   This function will return the blocksize for the given SHA
180  *   algorithm.
181  *
182  * Parameters:
183  *   whichSha:
184  *     which SHA algorithm to query
185  *
186  * Returns:
187  *   block size
188  *
189  */
190 int USHABlockSize(enum SHAversion whichSha)
191 {
192   switch (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;
197     default:
198     case SHA512: return SHA512_Message_Block_Size;
199   }
200 }
201
202 /*
203  * USHAHashSize
204  *
205  * Description:
206  *   This function will return the hashsize for the given SHA
207  *   algorithm.
208  *
209  * Parameters:
210  *   whichSha:
211  *     which SHA algorithm to query
212  *
213  * Returns:
214  *   hash size
215  *
216  */
217 int USHAHashSize(enum SHAversion whichSha)
218 {
219   switch (whichSha) {
220     case SHA1:   return SHA1HashSize;
221     case SHA224: return SHA224HashSize;
222     case SHA256: return SHA256HashSize;
223     case SHA384: return SHA384HashSize;
224     default:
225     case SHA512: return SHA512HashSize;
226   }
227 }
228
229 /*
230  * USHAHashSizeBits
231  *
232  * Description:
233  *   This function will return the hashsize for the given SHA
234  *   algorithm, expressed in bits.
235  *
236  * Parameters:
237  *   whichSha:
238  *     which SHA algorithm to query
239  *
240  * Returns:
241  *   hash size in bits
242  *
243  */
244 int USHAHashSizeBits(enum SHAversion whichSha)
245 {
246   switch (whichSha) {
247     case SHA1:   return SHA1HashSizeBits;
248     case SHA224: return SHA224HashSizeBits;
249     case SHA256: return SHA256HashSizeBits;
250     case SHA384: return SHA384HashSizeBits;
251     default:
252     case SHA512: return SHA512HashSizeBits;
253   }
254 }
255