diff options
Diffstat (limited to '')
-rw-r--r-- | libraries/irrlicht-1.8/source/Irrlicht/aesGladman/pwd2key.cpp | 372 |
1 files changed, 186 insertions, 186 deletions
diff --git a/libraries/irrlicht-1.8/source/Irrlicht/aesGladman/pwd2key.cpp b/libraries/irrlicht-1.8/source/Irrlicht/aesGladman/pwd2key.cpp index 051e45b..c40ae56 100644 --- a/libraries/irrlicht-1.8/source/Irrlicht/aesGladman/pwd2key.cpp +++ b/libraries/irrlicht-1.8/source/Irrlicht/aesGladman/pwd2key.cpp | |||
@@ -1,186 +1,186 @@ | |||
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 | 32 | ||
33 | This is an implementation of RFC2898, which specifies key derivation from | 33 | This is an implementation of RFC2898, which specifies key derivation from |
34 | a password and a salt value. | 34 | a password and a salt value. |
35 | */ | 35 | */ |
36 | 36 | ||
37 | #include <memory.h> | 37 | #include <memory.h> |
38 | #include "hmac.h" | 38 | #include "hmac.h" |
39 | 39 | ||
40 | void derive_key(const unsigned char pwd[], /* the PASSWORD */ | 40 | void derive_key(const unsigned char pwd[], /* the PASSWORD */ |
41 | unsigned int pwd_len, /* and its length */ | 41 | unsigned int pwd_len, /* and its length */ |
42 | const unsigned char salt[], /* the SALT and its */ | 42 | const unsigned char salt[], /* the SALT and its */ |
43 | unsigned int salt_len, /* length */ | 43 | unsigned int salt_len, /* length */ |
44 | unsigned int iter, /* the number of iterations */ | 44 | unsigned int iter, /* the number of iterations */ |
45 | unsigned char key[], /* space for the output key */ | 45 | unsigned char key[], /* space for the output key */ |
46 | unsigned int key_len)/* and its required length */ | 46 | unsigned int key_len)/* and its required length */ |
47 | { | 47 | { |
48 | unsigned int i, j, k, n_blk; | 48 | unsigned int i, j, k, n_blk; |
49 | unsigned char uu[HMAC_HASH_OUTPUT_SIZE], ux[HMAC_HASH_OUTPUT_SIZE]; | 49 | unsigned char uu[HMAC_HASH_OUTPUT_SIZE], ux[HMAC_HASH_OUTPUT_SIZE]; |
50 | hmac_ctx c1[1], c2[1], c3[1]; | 50 | hmac_ctx c1[1], c2[1], c3[1]; |
51 | 51 | ||
52 | /* set HMAC context (c1) for password */ | 52 | /* set HMAC context (c1) for password */ |
53 | hmac_sha_begin(c1); | 53 | hmac_sha_begin(c1); |
54 | hmac_sha_key(pwd, pwd_len, c1); | 54 | hmac_sha_key(pwd, pwd_len, c1); |
55 | 55 | ||
56 | /* set HMAC context (c2) for password and salt */ | 56 | /* set HMAC context (c2) for password and salt */ |
57 | memcpy(c2, c1, sizeof(hmac_ctx)); | 57 | memcpy(c2, c1, sizeof(hmac_ctx)); |
58 | hmac_sha_data(salt, salt_len, c2); | 58 | hmac_sha_data(salt, salt_len, c2); |
59 | 59 | ||
60 | /* find the number of SHA blocks in the key */ | 60 | /* find the number of SHA blocks in the key */ |
61 | n_blk = 1 + (key_len - 1) / HMAC_HASH_OUTPUT_SIZE; | 61 | n_blk = 1 + (key_len - 1) / HMAC_HASH_OUTPUT_SIZE; |
62 | 62 | ||
63 | for(i = 0; i < n_blk; ++i) /* for each block in key */ | 63 | for(i = 0; i < n_blk; ++i) /* for each block in key */ |
64 | { | 64 | { |
65 | /* ux[] holds the running xor value */ | 65 | /* ux[] holds the running xor value */ |
66 | memset(ux, 0, HMAC_HASH_OUTPUT_SIZE); | 66 | memset(ux, 0, HMAC_HASH_OUTPUT_SIZE); |
67 | 67 | ||
68 | /* set HMAC context (c3) for password and salt */ | 68 | /* set HMAC context (c3) for password and salt */ |
69 | memcpy(c3, c2, sizeof(hmac_ctx)); | 69 | memcpy(c3, c2, sizeof(hmac_ctx)); |
70 | 70 | ||
71 | /* enter additional data for 1st block into uu */ | 71 | /* enter additional data for 1st block into uu */ |
72 | uu[0] = (unsigned char)((i + 1) >> 24); | 72 | uu[0] = (unsigned char)((i + 1) >> 24); |
73 | uu[1] = (unsigned char)((i + 1) >> 16); | 73 | uu[1] = (unsigned char)((i + 1) >> 16); |
74 | uu[2] = (unsigned char)((i + 1) >> 8); | 74 | uu[2] = (unsigned char)((i + 1) >> 8); |
75 | uu[3] = (unsigned char)(i + 1); | 75 | uu[3] = (unsigned char)(i + 1); |
76 | 76 | ||
77 | /* this is the key mixing iteration */ | 77 | /* this is the key mixing iteration */ |
78 | for(j = 0, k = 4; j < iter; ++j) | 78 | for(j = 0, k = 4; j < iter; ++j) |
79 | { | 79 | { |
80 | /* add previous round data to HMAC */ | 80 | /* add previous round data to HMAC */ |
81 | hmac_sha_data(uu, k, c3); | 81 | hmac_sha_data(uu, k, c3); |
82 | 82 | ||
83 | /* obtain HMAC for uu[] */ | 83 | /* obtain HMAC for uu[] */ |
84 | hmac_sha_end(uu, HMAC_HASH_OUTPUT_SIZE, c3); | 84 | hmac_sha_end(uu, HMAC_HASH_OUTPUT_SIZE, c3); |
85 | 85 | ||
86 | /* xor into the running xor block */ | 86 | /* xor into the running xor block */ |
87 | for(k = 0; k < HMAC_HASH_OUTPUT_SIZE; ++k) | 87 | for(k = 0; k < HMAC_HASH_OUTPUT_SIZE; ++k) |
88 | ux[k] ^= uu[k]; | 88 | ux[k] ^= uu[k]; |
89 | 89 | ||
90 | /* set HMAC context (c3) for password */ | 90 | /* set HMAC context (c3) for password */ |
91 | memcpy(c3, c1, sizeof(hmac_ctx)); | 91 | memcpy(c3, c1, sizeof(hmac_ctx)); |
92 | } | 92 | } |
93 | 93 | ||
94 | /* compile key blocks into the key output */ | 94 | /* compile key blocks into the key output */ |
95 | j = 0; k = i * HMAC_HASH_OUTPUT_SIZE; | 95 | j = 0; k = i * HMAC_HASH_OUTPUT_SIZE; |
96 | while(j < HMAC_HASH_OUTPUT_SIZE && k < key_len) | 96 | while(j < HMAC_HASH_OUTPUT_SIZE && k < key_len) |
97 | key[k++] = ux[j++]; | 97 | key[k++] = ux[j++]; |
98 | } | 98 | } |
99 | } | 99 | } |
100 | 100 | ||
101 | #ifdef TEST | 101 | #ifdef TEST |
102 | 102 | ||
103 | #include <stdio.h> | 103 | #include <stdio.h> |
104 | 104 | ||
105 | struct | 105 | struct |
106 | { unsigned int pwd_len; | 106 | { unsigned int pwd_len; |
107 | unsigned int salt_len; | 107 | unsigned int salt_len; |
108 | unsigned int it_count; | 108 | unsigned int it_count; |
109 | unsigned char *pwd; | 109 | unsigned char *pwd; |
110 | unsigned char salt[32]; | 110 | unsigned char salt[32]; |
111 | unsigned char key[32]; | 111 | unsigned char key[32]; |
112 | } tests[] = | 112 | } tests[] = |
113 | { | 113 | { |
114 | { 8, 4, 5, (unsigned char*)"password", | 114 | { 8, 4, 5, (unsigned char*)"password", |
115 | { | 115 | { |
116 | 0x12, 0x34, 0x56, 0x78 | 116 | 0x12, 0x34, 0x56, 0x78 |
117 | }, | 117 | }, |
118 | { | 118 | { |
119 | 0x5c, 0x75, 0xce, 0xf0, 0x1a, 0x96, 0x0d, 0xf7, | 119 | 0x5c, 0x75, 0xce, 0xf0, 0x1a, 0x96, 0x0d, 0xf7, |
120 | 0x4c, 0xb6, 0xb4, 0x9b, 0x9e, 0x38, 0xe6, 0xb5 | 120 | 0x4c, 0xb6, 0xb4, 0x9b, 0x9e, 0x38, 0xe6, 0xb5 |
121 | } | 121 | } |
122 | }, | 122 | }, |
123 | { 8, 8, 5, (unsigned char*)"password", | 123 | { 8, 8, 5, (unsigned char*)"password", |
124 | { | 124 | { |
125 | 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 | 125 | 0x12, 0x34, 0x56, 0x78, 0x78, 0x56, 0x34, 0x12 |
126 | }, | 126 | }, |
127 | { | 127 | { |
128 | 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6, | 128 | 0xd1, 0xda, 0xa7, 0x86, 0x15, 0xf2, 0x87, 0xe6, |
129 | 0xa1, 0xc8, 0xb1, 0x20, 0xd7, 0x06, 0x2a, 0x49 | 129 | 0xa1, 0xc8, 0xb1, 0x20, 0xd7, 0x06, 0x2a, 0x49 |
130 | } | 130 | } |
131 | }, | 131 | }, |
132 | { 8, 21, 1, (unsigned char*)"password", | 132 | { 8, 21, 1, (unsigned char*)"password", |
133 | { | 133 | { |
134 | "ATHENA.MIT.EDUraeburn" | 134 | "ATHENA.MIT.EDUraeburn" |
135 | }, | 135 | }, |
136 | { | 136 | { |
137 | 0xcd, 0xed, 0xb5, 0x28, 0x1b, 0xb2, 0xf8, 0x01, | 137 | 0xcd, 0xed, 0xb5, 0x28, 0x1b, 0xb2, 0xf8, 0x01, |
138 | 0x56, 0x5a, 0x11, 0x22, 0xb2, 0x56, 0x35, 0x15 | 138 | 0x56, 0x5a, 0x11, 0x22, 0xb2, 0x56, 0x35, 0x15 |
139 | } | 139 | } |
140 | }, | 140 | }, |
141 | { 8, 21, 2, (unsigned char*)"password", | 141 | { 8, 21, 2, (unsigned char*)"password", |
142 | { | 142 | { |
143 | "ATHENA.MIT.EDUraeburn" | 143 | "ATHENA.MIT.EDUraeburn" |
144 | }, | 144 | }, |
145 | { | 145 | { |
146 | 0x01, 0xdb, 0xee, 0x7f, 0x4a, 0x9e, 0x24, 0x3e, | 146 | 0x01, 0xdb, 0xee, 0x7f, 0x4a, 0x9e, 0x24, 0x3e, |
147 | 0x98, 0x8b, 0x62, 0xc7, 0x3c, 0xda, 0x93, 0x5d | 147 | 0x98, 0x8b, 0x62, 0xc7, 0x3c, 0xda, 0x93, 0x5d |
148 | } | 148 | } |
149 | }, | 149 | }, |
150 | { 8, 21, 1200, (unsigned char*)"password", | 150 | { 8, 21, 1200, (unsigned char*)"password", |
151 | { | 151 | { |
152 | "ATHENA.MIT.EDUraeburn" | 152 | "ATHENA.MIT.EDUraeburn" |
153 | }, | 153 | }, |
154 | { | 154 | { |
155 | 0x5c, 0x08, 0xeb, 0x61, 0xfd, 0xf7, 0x1e, 0x4e, | 155 | 0x5c, 0x08, 0xeb, 0x61, 0xfd, 0xf7, 0x1e, 0x4e, |
156 | 0x4e, 0xc3, 0xcf, 0x6b, 0xa1, 0xf5, 0x51, 0x2b | 156 | 0x4e, 0xc3, 0xcf, 0x6b, 0xa1, 0xf5, 0x51, 0x2b |
157 | } | 157 | } |
158 | } | 158 | } |
159 | }; | 159 | }; |
160 | 160 | ||
161 | int main() | 161 | int main() |
162 | { unsigned int i, j, key_len = 256; | 162 | { unsigned int i, j, key_len = 256; |
163 | unsigned char key[256]; | 163 | unsigned char key[256]; |
164 | 164 | ||
165 | printf("\nTest of RFC2898 Password Based Key Derivation"); | 165 | printf("\nTest of RFC2898 Password Based Key Derivation"); |
166 | for(i = 0; i < 5; ++i) | 166 | for(i = 0; i < 5; ++i) |
167 | { | 167 | { |
168 | derive_key(tests[i].pwd, tests[i].pwd_len, tests[i].salt, | 168 | derive_key(tests[i].pwd, tests[i].pwd_len, tests[i].salt, |
169 | tests[i].salt_len, tests[i].it_count, key, key_len); | 169 | tests[i].salt_len, tests[i].it_count, key, key_len); |
170 | 170 | ||
171 | printf("\ntest %i: ", i + 1); | 171 | printf("\ntest %i: ", i + 1); |
172 | printf("key %s", memcmp(tests[i].key, key, 16) ? "is bad" : "is good"); | 172 | printf("key %s", memcmp(tests[i].key, key, 16) ? "is bad" : "is good"); |
173 | for(j = 0; j < key_len && j < 64; j += 4) | 173 | for(j = 0; j < key_len && j < 64; j += 4) |
174 | { | 174 | { |
175 | if(j % 16 == 0) | 175 | if(j % 16 == 0) |
176 | printf("\n"); | 176 | printf("\n"); |
177 | printf("0x%02x%02x%02x%02x ", key[j], key[j + 1], key[j + 2], key[j + 3]); | 177 | printf("0x%02x%02x%02x%02x ", key[j], key[j + 1], key[j + 2], key[j + 3]); |
178 | } | 178 | } |
179 | printf(j < key_len ? " ... \n" : "\n"); | 179 | printf(j < key_len ? " ... \n" : "\n"); |
180 | } | 180 | } |
181 | printf("\n"); | 181 | printf("\n"); |
182 | return 0; | 182 | return 0; |
183 | } | 183 | } |
184 | 184 | ||
185 | #endif | 185 | #endif |
186 | 186 | ||