1 /*
2  ---------------------------------------------------------------------------
3  Copyright (c) 2002, Dr Brian Gladman <                 >, Worcester, UK.
4  All rights reserved.
5 
6  LICENSE TERMS
7 
8  The free distribution and use of this software in both source and binary
9  form is allowed (with or without changes) provided that:
10 
11    1. distributions of this source code include the above copyright
12       notice, this list of conditions and the following disclaimer;
13 
14    2. distributions in binary form include the above copyright
15       notice, this list of conditions and the following disclaimer
16       in the documentation and/or other associated materials;
17 
18    3. the copyright holder's name is not used to endorse products
19       built using this software without specific written permission.
20 
21  ALTERNATIVELY, provided that this notice is retained in full, this product
22  may be distributed under the terms of the GNU General Public License (GPL),
23  in which case the provisions of the GPL apply INSTEAD OF those given above.
24 
25  DISCLAIMER
26 
27  This software is provided 'as is' with no explicit or implied warranties
28  in respect of its properties, including, but not limited to, correctness
29  and/or fitness for purpose.
30  ---------------------------------------------------------------------------
31  Issue Date: 26/08/2003
32  Includes a bugfix from Dr Brian Gladman made on 16/04/2012 for compiling on 64-bit
33 
34  This is an implementation of HMAC, the FIPS standard keyed hash function
35 */
36 
37 #ifndef _HMAC_H
38 #define _HMAC_H
39 
40 #include <memory.h>
41 
42 #define USE_SHA1	// Irrlicht only cares about SHA1 for now
43 #if !defined(USE_SHA1) && !defined(USE_SHA256)
44 #error define USE_SHA1 or USE_SHA256 to set the HMAC hash algorithm
45 #endif
46 
47 #ifdef USE_SHA1
48 
49 #include "sha1.h"
50 
51 #define HMAC_HASH_INPUT_SIZE    SHA1_BLOCK_SIZE
52 #define HMAC_HASH_OUTPUT_SIZE   SHA1_DIGEST_SIZE
53 #define sha_ctx             	sha1_ctx
54 #define sha_begin           	sha1_begin
55 #define sha_hash            	sha1_hash
56 #define sha_end             	sha1_end
57 
58 #endif
59 
60 #ifdef USE_SHA256
61 
62 #include "sha2.h"
63 
64 #define HMAC_HASH_INPUT_SIZE     SHA256_BLOCK_SIZE
65 #define HMAC_HASH_OUTPUT_SIZE    SHA256_DIGEST_SIZE
66 #define sha_ctx             sha256_ctx
67 #define sha_begin           sha256_begin
68 #define sha_hash            sha256_hash
69 #define sha_end             sha256_end
70 
71 #endif
72 
73 #define HMAC_OK                0
74 #define HMAC_BAD_MODE         -1
75 #define HMAC_IN_DATA  0xffffffff
76 
77 typedef struct
78 {   unsigned char   key[HMAC_HASH_INPUT_SIZE];
79     sha_ctx         ctx[1];
80     unsigned long   klen;
81 } hmac_ctx;
82 
83 void hmac_sha_begin(hmac_ctx cx[1]);
84 
85 int  hmac_sha_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1]);
86 
87 void hmac_sha_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1]);
88 
89 void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1]);
90 
91 void hmac_sha(const unsigned char key[], unsigned long key_len,
92           const unsigned char data[], unsigned long data_len,
93           unsigned char mac[], unsigned long mac_len);
94 
95 #endif
96