added iOS source code
[wl-app.git] / iOS / Pods / SSZipArchive / SSZipArchive / minizip / aes / hmac.c
1 /*
2 ---------------------------------------------------------------------------
3 Copyright (c) 1998-2010, Brian Gladman, Worcester, UK. All rights reserved.
4
5 The redistribution and use of this software (with or without changes)
6 is allowed without the payment of fees or royalties provided that:
7
8   source code distributions include the above copyright notice, this
9   list of conditions and the following disclaimer;
10
11   binary distributions include the above copyright notice, this list
12   of conditions and the following disclaimer in their documentation.
13
14 This software is provided 'as is' with no explicit or implied warranties
15 in respect of its operation, including, but not limited to, correctness
16 and fitness for purpose.
17 ---------------------------------------------------------------------------
18 Issue Date: 20/12/2007
19
20 This is an implementation of HMAC, the FIPS standard keyed hash function
21 */
22
23 #include "hmac.h"
24
25 #if defined(__cplusplus)
26 extern "C"
27 {
28 #endif
29
30 /* initialise the HMAC context to zero */
31 int hmac_sha_begin(enum hmac_hash hash, hmac_ctx cx[1])
32 {
33     memset(cx, 0, sizeof(hmac_ctx));
34     switch(hash)
35     {
36 #ifdef SHA_1
37     case HMAC_SHA1:
38         cx->f_begin = (hf_begin *)sha1_begin;
39         cx->f_hash  = (hf_hash *)sha1_hash;
40         cx->f_end   = (hf_end *)sha1_end;
41         cx->input_len = SHA1_BLOCK_SIZE;
42         cx->output_len = SHA1_DIGEST_SIZE;
43         break;
44 #endif
45 #ifdef SHA_224
46     case HMAC_SHA224:
47         cx->f_begin = (hf_begin *)sha224_begin;
48         cx->f_hash  = (hf_hash *)sha224_hash;
49         cx->f_end   = (hf_end *)sha224_end;
50         cx->input_len = SHA224_BLOCK_SIZE;
51         cx->output_len = SHA224_DIGEST_SIZE;
52         break;
53 #endif
54 #ifdef SHA_256
55     case HMAC_SHA256:
56         cx->f_begin = (hf_begin *)sha256_begin;
57         cx->f_hash  = (hf_hash *)sha256_hash;
58         cx->f_end   = (hf_end *)sha256_end;
59         cx->input_len = SHA256_BLOCK_SIZE;
60         cx->output_len = SHA256_DIGEST_SIZE;
61         break;
62 #endif
63 #ifdef SHA_384
64     case HMAC_SHA384:
65         cx->f_begin = (hf_begin *)sha384_begin;
66         cx->f_hash  = (hf_hash *)sha384_hash;
67         cx->f_end   = (hf_end *)sha384_end;
68         cx->input_len = SHA384_BLOCK_SIZE;
69         cx->output_len = SHA384_DIGEST_SIZE;
70         break;
71 #endif
72 #ifdef SHA_512
73     case HMAC_SHA512:
74         cx->f_begin = (hf_begin *)sha512_begin;
75         cx->f_hash  = (hf_hash *)sha512_hash;
76         cx->f_end   = (hf_end *)sha512_end;
77         cx->input_len = SHA512_BLOCK_SIZE;
78         cx->output_len = SHA512_DIGEST_SIZE;
79         break;
80     case HMAC_SHA512_256:
81         cx->f_begin = (hf_begin *)sha512_256_begin;
82         cx->f_hash  = (hf_hash *)sha512_256_hash;
83         cx->f_end   = (hf_end *)sha512_256_end;
84         cx->input_len = SHA512_256_BLOCK_SIZE;
85         cx->output_len = SHA512_256_DIGEST_SIZE;
86         break;
87     case HMAC_SHA512_224:
88         cx->f_begin = (hf_begin *)sha512_224_begin;
89         cx->f_hash  = (hf_hash *)sha512_224_hash;
90         cx->f_end   = (hf_end *)sha512_224_end;
91         cx->input_len = SHA512_224_BLOCK_SIZE;
92         cx->output_len = SHA512_224_DIGEST_SIZE;
93         break;
94     case HMAC_SHA512_192:
95         cx->f_begin = (hf_begin *)sha512_192_begin;
96         cx->f_hash  = (hf_hash *)sha512_192_hash;
97         cx->f_end   = (hf_end *)sha512_192_end;
98         cx->input_len = SHA512_192_BLOCK_SIZE;
99         cx->output_len = SHA512_192_DIGEST_SIZE;
100         break;
101     case HMAC_SHA512_128:
102         cx->f_begin = (hf_begin *)sha512_128_begin;
103         cx->f_hash  = (hf_hash *)sha512_128_hash;
104         cx->f_end   = (hf_begin *)sha512_128_end;
105         cx->input_len = SHA512_128_BLOCK_SIZE;
106         cx->output_len = SHA512_128_DIGEST_SIZE;
107         break;
108 #endif
109     }
110     return (int)cx->output_len;
111 }
112
113 /* input the HMAC key (can be called multiple times)    */
114 int hmac_sha_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1])
115 {
116     if(cx->klen == HMAC_IN_DATA)                /* error if further key input   */
117         return EXIT_FAILURE;                    /* is attempted in data mode    */
118
119     if(cx->klen + key_len > cx->input_len)      /* if the key has to be hashed  */
120     {
121         if(cx->klen <= cx->input_len)           /* if the hash has not yet been */
122         {                                       /* started, initialise it and   */
123             cx->f_begin(cx->sha_ctx);           /* hash stored key characters   */
124             cx->f_hash(cx->key, cx->klen, cx->sha_ctx);
125         }
126
127         cx->f_hash(key, key_len, cx->sha_ctx);  /* hash long key data into hash */
128     }
129     else                                        /* otherwise store key data     */
130         memcpy(cx->key + cx->klen, key, key_len);
131
132     cx->klen += key_len;                        /* update the key length count  */
133     return EXIT_SUCCESS;
134 }
135
136 /* input the HMAC data (can be called multiple times) - */
137 /* note that this call terminates the key input phase   */
138 void hmac_sha_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1])
139 {   unsigned int i;
140
141     if(cx->klen != HMAC_IN_DATA)                /* if not yet in data phase */
142     {
143         if(cx->klen > cx->input_len)            /* if key is being hashed   */
144         {                                       /* complete the hash and    */
145             cx->f_end(cx->key, cx->sha_ctx);    /* store the result as the  */
146             cx->klen = cx->output_len;          /* key and set new length   */
147         }
148
149         /* pad the key if necessary */
150         memset(cx->key + cx->klen, 0, cx->input_len - cx->klen);
151
152         /* xor ipad into key value  */
153         for(i = 0; i < (cx->input_len >> 2); ++i)
154             ((uint32_t*)cx->key)[i] ^= 0x36363636;
155
156         /* and start hash operation */
157         cx->f_begin(cx->sha_ctx);
158         cx->f_hash(cx->key, cx->input_len, cx->sha_ctx);
159
160         /* mark as now in data mode */
161         cx->klen = HMAC_IN_DATA;
162     }
163
164     /* hash the data (if any)       */
165     if(data_len)
166         cx->f_hash(data, data_len, cx->sha_ctx);
167 }
168
169 /* compute and output the MAC value */
170 void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1])
171 {   unsigned char dig[HMAC_MAX_OUTPUT_SIZE];
172     unsigned int i;
173
174     /* if no data has been entered perform a null data phase        */
175     if(cx->klen != HMAC_IN_DATA)
176         hmac_sha_data((const unsigned char*)0, 0, cx);
177
178     cx->f_end(dig, cx->sha_ctx);        /* complete the inner hash       */
179
180     /* set outer key value using opad and removing ipad */
181     for(i = 0; i < (cx->input_len >> 2); ++i)
182         ((uint32_t*)cx->key)[i] ^= 0x36363636 ^ 0x5c5c5c5c;
183
184     /* perform the outer hash operation */
185     cx->f_begin(cx->sha_ctx);
186     cx->f_hash(cx->key, cx->input_len, cx->sha_ctx);
187     cx->f_hash(dig, cx->output_len, cx->sha_ctx);
188     cx->f_end(dig, cx->sha_ctx);
189
190     /* output the hash value            */
191     for(i = 0; i < mac_len; ++i)
192         mac[i] = dig[i];
193 }
194
195 /* 'do it all in one go' subroutine     */
196 void hmac_sha(enum hmac_hash hash, const unsigned char key[], unsigned long key_len,
197           const unsigned char data[], unsigned long data_len,
198           unsigned char mac[], unsigned long mac_len)
199 {   hmac_ctx    cx[1];
200
201     hmac_sha_begin(hash, cx);
202     hmac_sha_key(key, key_len, cx);
203     hmac_sha_data(data, data_len, cx);
204     hmac_sha_end(mac, mac_len, cx);
205 }
206
207 #if defined(__cplusplus)
208 }
209 #endif