diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/aesGladman/hmac.cpp | 284 |
1 files changed, 142 insertions, 142 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/aesGladman/hmac.cpp b/libraries/irrlicht-1.8/source/Irrlicht/aesGladman/hmac.cpp index b24294d..147afa2 100644 --- a/libraries/irrlicht-1.8/source/Irrlicht/aesGladman/hmac.cpp +++ b/libraries/irrlicht-1.8/source/Irrlicht/aesGladman/hmac.cpp | |||
@@ -1,142 +1,142 @@ | |||
1 | /* | 1 | /* |
2 | --------------------------------------------------------------------------- | 2 | --------------------------------------------------------------------------- |
3 | Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. | 3 | Copyright (c) 2002, Dr Brian Gladman < >, Worcester, UK. |
4 | All rights reserved. | 4 | All rights reserved. |
5 | 5 | ||
6 | LICENSE TERMS | 6 | LICENSE TERMS |
7 | 7 | ||
8 | The free distribution and use of this software in both source and binary | 8 | The free distribution and use of this software in both source and binary |
9 | form is allowed (with or without changes) provided that: | 9 | form is allowed (with or without changes) provided that: |
10 | 10 | ||
11 | 1. distributions of this source code include the above copyright | 11 | 1. distributions of this source code include the above copyright |
12 | notice, this list of conditions and the following disclaimer; | 12 | notice, this list of conditions and the following disclaimer; |
13 | 13 | ||
14 | 2. distributions in binary form include the above copyright | 14 | 2. distributions in binary form include the above copyright |
15 | notice, this list of conditions and the following disclaimer | 15 | notice, this list of conditions and the following disclaimer |
16 | in the documentation and/or other associated materials; | 16 | in the documentation and/or other associated materials; |
17 | 17 | ||
18 | 3. the copyright holder's name is not used to endorse products | 18 | 3. the copyright holder's name is not used to endorse products |
19 | built using this software without specific written permission. | 19 | built using this software without specific written permission. |
20 | 20 | ||
21 | ALTERNATIVELY, provided that this notice is retained in full, this product | 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), | 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. | 23 | in which case the provisions of the GPL apply INSTEAD OF those given above. |
24 | 24 | ||
25 | DISCLAIMER | 25 | DISCLAIMER |
26 | 26 | ||
27 | This software is provided 'as is' with no explicit or implied warranties | 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 | 28 | in respect of its properties, including, but not limited to, correctness |
29 | and/or fitness for purpose. | 29 | and/or fitness for purpose. |
30 | --------------------------------------------------------------------------- | 30 | --------------------------------------------------------------------------- |
31 | Issue Date: 26/08/2003 | 31 | Issue Date: 26/08/2003 |
32 | Includes a bugfix from Dr Brian Gladman made on 16/04/2012 for compiling on 64-bit | 32 | Includes a bugfix from Dr Brian Gladman made on 16/04/2012 for compiling on 64-bit |
33 | 33 | ||
34 | This is an implementation of HMAC, the FIPS standard keyed hash function | 34 | This is an implementation of HMAC, the FIPS standard keyed hash function |
35 | */ | 35 | */ |
36 | 36 | ||
37 | 37 | ||
38 | #include "hmac.h" | 38 | #include "hmac.h" |
39 | 39 | ||
40 | #define HMAC_IPAD (0x36 * (((unsigned long)-1) / 0xff)) | 40 | #define HMAC_IPAD (0x36 * (((unsigned long)-1) / 0xff)) |
41 | #define HMAC_OPAD (0x5c * (((unsigned long)-1) / 0xff)) | 41 | #define HMAC_OPAD (0x5c * (((unsigned long)-1) / 0xff)) |
42 | 42 | ||
43 | /* initialise the HMAC context to zero */ | 43 | /* initialise the HMAC context to zero */ |
44 | void hmac_sha_begin(hmac_ctx cx[1]) | 44 | void hmac_sha_begin(hmac_ctx cx[1]) |
45 | { | 45 | { |
46 | memset(cx, 0, sizeof(hmac_ctx)); | 46 | memset(cx, 0, sizeof(hmac_ctx)); |
47 | } | 47 | } |
48 | 48 | ||
49 | /* input the HMAC key (can be called multiple times) */ | 49 | /* input the HMAC key (can be called multiple times) */ |
50 | int hmac_sha_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1]) | 50 | int hmac_sha_key(const unsigned char key[], unsigned long key_len, hmac_ctx cx[1]) |
51 | { | 51 | { |
52 | if(cx->klen == HMAC_IN_DATA) /* error if further key input */ | 52 | if(cx->klen == HMAC_IN_DATA) /* error if further key input */ |
53 | return HMAC_BAD_MODE; /* is attempted in data mode */ | 53 | return HMAC_BAD_MODE; /* is attempted in data mode */ |
54 | 54 | ||
55 | if(cx->klen + key_len > HMAC_HASH_INPUT_SIZE) /* if the key has to be hashed */ | 55 | if(cx->klen + key_len > HMAC_HASH_INPUT_SIZE) /* if the key has to be hashed */ |
56 | { | 56 | { |
57 | if(cx->klen <= HMAC_HASH_INPUT_SIZE) /* if the hash has not yet been */ | 57 | if(cx->klen <= HMAC_HASH_INPUT_SIZE) /* if the hash has not yet been */ |
58 | { /* started, initialise it and */ | 58 | { /* started, initialise it and */ |
59 | sha_begin(cx->ctx); /* hash stored key characters */ | 59 | sha_begin(cx->ctx); /* hash stored key characters */ |
60 | sha_hash(cx->key, cx->klen, cx->ctx); | 60 | sha_hash(cx->key, cx->klen, cx->ctx); |
61 | } | 61 | } |
62 | 62 | ||
63 | sha_hash(key, key_len, cx->ctx); /* hash long key data into hash */ | 63 | sha_hash(key, key_len, cx->ctx); /* hash long key data into hash */ |
64 | } | 64 | } |
65 | else /* otherwise store key data */ | 65 | else /* otherwise store key data */ |
66 | memcpy(cx->key + cx->klen, key, key_len); | 66 | memcpy(cx->key + cx->klen, key, key_len); |
67 | 67 | ||
68 | cx->klen += key_len; /* update the key length count */ | 68 | cx->klen += key_len; /* update the key length count */ |
69 | return HMAC_OK; | 69 | return HMAC_OK; |
70 | } | 70 | } |
71 | 71 | ||
72 | /* input the HMAC data (can be called multiple times) - */ | 72 | /* input the HMAC data (can be called multiple times) - */ |
73 | /* note that this call terminates the key input phase */ | 73 | /* note that this call terminates the key input phase */ |
74 | void hmac_sha_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1]) | 74 | void hmac_sha_data(const unsigned char data[], unsigned long data_len, hmac_ctx cx[1]) |
75 | { unsigned int i; | 75 | { unsigned int i; |
76 | 76 | ||
77 | if(cx->klen != HMAC_IN_DATA) /* if not yet in data phase */ | 77 | if(cx->klen != HMAC_IN_DATA) /* if not yet in data phase */ |
78 | { | 78 | { |
79 | if(cx->klen > HMAC_HASH_INPUT_SIZE) /* if key is being hashed */ | 79 | if(cx->klen > HMAC_HASH_INPUT_SIZE) /* if key is being hashed */ |
80 | { /* complete the hash and */ | 80 | { /* complete the hash and */ |
81 | sha_end(cx->key, cx->ctx); /* store the result as the */ | 81 | sha_end(cx->key, cx->ctx); /* store the result as the */ |
82 | cx->klen = HMAC_HASH_OUTPUT_SIZE; /* key and set new length */ | 82 | cx->klen = HMAC_HASH_OUTPUT_SIZE; /* key and set new length */ |
83 | } | 83 | } |
84 | 84 | ||
85 | /* pad the key if necessary */ | 85 | /* pad the key if necessary */ |
86 | memset(cx->key + cx->klen, 0, HMAC_HASH_INPUT_SIZE - cx->klen); | 86 | memset(cx->key + cx->klen, 0, HMAC_HASH_INPUT_SIZE - cx->klen); |
87 | 87 | ||
88 | /* xor ipad into key value */ | 88 | /* xor ipad into key value */ |
89 | for(i = 0; i < HMAC_HASH_INPUT_SIZE / sizeof(unsigned long); ++i) | 89 | for(i = 0; i < HMAC_HASH_INPUT_SIZE / sizeof(unsigned long); ++i) |
90 | ((unsigned long*)cx->key)[i] ^= HMAC_IPAD; | 90 | ((unsigned long*)cx->key)[i] ^= HMAC_IPAD; |
91 | 91 | ||
92 | /* and start hash operation */ | 92 | /* and start hash operation */ |
93 | sha_begin(cx->ctx); | 93 | sha_begin(cx->ctx); |
94 | sha_hash(cx->key, HMAC_HASH_INPUT_SIZE, cx->ctx); | 94 | sha_hash(cx->key, HMAC_HASH_INPUT_SIZE, cx->ctx); |
95 | 95 | ||
96 | /* mark as now in data mode */ | 96 | /* mark as now in data mode */ |
97 | cx->klen = HMAC_IN_DATA; | 97 | cx->klen = HMAC_IN_DATA; |
98 | } | 98 | } |
99 | 99 | ||
100 | /* hash the data (if any) */ | 100 | /* hash the data (if any) */ |
101 | if(data_len) | 101 | if(data_len) |
102 | sha_hash(data, data_len, cx->ctx); | 102 | sha_hash(data, data_len, cx->ctx); |
103 | } | 103 | } |
104 | 104 | ||
105 | /* compute and output the MAC value */ | 105 | /* compute and output the MAC value */ |
106 | void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1]) | 106 | void hmac_sha_end(unsigned char mac[], unsigned long mac_len, hmac_ctx cx[1]) |
107 | { unsigned char dig[HMAC_HASH_OUTPUT_SIZE]; | 107 | { unsigned char dig[HMAC_HASH_OUTPUT_SIZE]; |
108 | unsigned int i; | 108 | unsigned int i; |
109 | 109 | ||
110 | /* if no data has been entered perform a null data phase */ | 110 | /* if no data has been entered perform a null data phase */ |
111 | if(cx->klen != HMAC_IN_DATA) | 111 | if(cx->klen != HMAC_IN_DATA) |
112 | hmac_sha_data((const unsigned char*)0, 0, cx); | 112 | hmac_sha_data((const unsigned char*)0, 0, cx); |
113 | 113 | ||
114 | sha_end(dig, cx->ctx); /* complete the inner hash */ | 114 | sha_end(dig, cx->ctx); /* complete the inner hash */ |
115 | 115 | ||
116 | /* set outer key value using opad and removing ipad */ | 116 | /* set outer key value using opad and removing ipad */ |
117 | for(i = 0; i < HMAC_HASH_INPUT_SIZE / sizeof(unsigned long); ++i) | 117 | for(i = 0; i < HMAC_HASH_INPUT_SIZE / sizeof(unsigned long); ++i) |
118 | ((unsigned long*)cx->key)[i] ^= HMAC_OPAD ^ HMAC_IPAD; | 118 | ((unsigned long*)cx->key)[i] ^= HMAC_OPAD ^ HMAC_IPAD; |
119 | 119 | ||
120 | /* perform the outer hash operation */ | 120 | /* perform the outer hash operation */ |
121 | sha_begin(cx->ctx); | 121 | sha_begin(cx->ctx); |
122 | sha_hash(cx->key, HMAC_HASH_INPUT_SIZE, cx->ctx); | 122 | sha_hash(cx->key, HMAC_HASH_INPUT_SIZE, cx->ctx); |
123 | sha_hash(dig, HMAC_HASH_OUTPUT_SIZE, cx->ctx); | 123 | sha_hash(dig, HMAC_HASH_OUTPUT_SIZE, cx->ctx); |
124 | sha_end(dig, cx->ctx); | 124 | sha_end(dig, cx->ctx); |
125 | 125 | ||
126 | /* output the hash value */ | 126 | /* output the hash value */ |
127 | for(i = 0; i < mac_len; ++i) | 127 | for(i = 0; i < mac_len; ++i) |
128 | mac[i] = dig[i]; | 128 | mac[i] = dig[i]; |
129 | } | 129 | } |
130 | 130 | ||
131 | /* 'do it all in one go' subroutine */ | 131 | /* 'do it all in one go' subroutine */ |
132 | void hmac_sha(const unsigned char key[], unsigned long key_len, | 132 | void hmac_sha(const unsigned char key[], unsigned long key_len, |
133 | const unsigned char data[], unsigned long data_len, | 133 | const unsigned char data[], unsigned long data_len, |
134 | unsigned char mac[], unsigned long mac_len) | 134 | unsigned char mac[], unsigned long mac_len) |
135 | { hmac_ctx cx[1]; | 135 | { hmac_ctx cx[1]; |
136 | 136 | ||
137 | hmac_sha_begin(cx); | 137 | hmac_sha_begin(cx); |
138 | hmac_sha_key(key, key_len, cx); | 138 | hmac_sha_key(key, key_len, cx); |
139 | hmac_sha_data(data, data_len, cx); | 139 | hmac_sha_data(data, data_len, cx); |
140 | hmac_sha_end(mac, mac_len, cx); | 140 | hmac_sha_end(mac, mac_len, cx); |
141 | } | 141 | } |
142 | 142 | ||