|
1 /* |
|
2 * Copyright (c) 2001-2006 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: EAP and WLAN authentication protocols. |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 // This is enumeration of EAPOL source code. |
|
20 #if defined(USE_EAP_MINIMUM_RELEASE_TRACES) |
|
21 #undef EAP_FILE_NUMBER_ENUM |
|
22 #define EAP_FILE_NUMBER_ENUM 5 |
|
23 #undef EAP_FILE_NUMBER_DATE |
|
24 #define EAP_FILE_NUMBER_DATE 1127594498 |
|
25 #endif //#if defined(USE_EAP_MINIMUM_RELEASE_TRACES) |
|
26 |
|
27 |
|
28 #include "eap_am_random_test.h" |
|
29 |
|
30 //-------------------------------------------------- |
|
31 |
|
32 #if 1 |
|
33 #define EAP_TEST_RANDOM_TRACE_DEBUG EAP_TRACE_DEBUG |
|
34 #define EAP_TEST_RANDOM_TRACE_DATA_DEBUG EAP_TRACE_DATA_DEBUG |
|
35 #else |
|
36 #define EAP_TEST_RANDOM_TRACE_DEBUG(tools, flags, params) |
|
37 #define EAP_TEST_RANDOM_TRACE_DATA_DEBUG(tools, flags, prefix, data, data_length) |
|
38 #endif |
|
39 |
|
40 static const u32_t EAP_TRACE_MASK_TEST_RANDOM = eap_am_tools_c::eap_trace_mask_crypto_test_random; |
|
41 |
|
42 //-------------------------------------------------- |
|
43 |
|
44 // |
|
45 EAP_FUNC_EXPORT eap_am_random_test_c::~eap_am_random_test_c() |
|
46 { |
|
47 } |
|
48 |
|
49 //-------------------------------------------------- |
|
50 |
|
51 // |
|
52 EAP_FUNC_EXPORT eap_am_random_test_c::eap_am_random_test_c( |
|
53 abs_eap_am_tools_c * const tools) |
|
54 : m_am_tools(tools) |
|
55 , m_state(tools) |
|
56 , m_hash(tools) |
|
57 , m_rand_counter(0ul) |
|
58 , m_do_seeding(false) |
|
59 , m_is_valid(false) |
|
60 { |
|
61 } |
|
62 |
|
63 //-------------------------------------------------- |
|
64 |
|
65 // |
|
66 EAP_FUNC_EXPORT void eap_am_random_test_c::set_do_reseeding(const bool does_reseeding_when_true) |
|
67 { |
|
68 m_do_seeding = does_reseeding_when_true; |
|
69 } |
|
70 |
|
71 //-------------------------------------------------- |
|
72 |
|
73 // |
|
74 EAP_FUNC_EXPORT eap_status_e eap_am_random_test_c::init( |
|
75 const void * const bytes, |
|
76 const u32_t length) |
|
77 { |
|
78 if (m_is_valid == true) |
|
79 { |
|
80 // Already initialized. |
|
81 return eap_status_ok; |
|
82 } |
|
83 |
|
84 m_state.reset(); |
|
85 |
|
86 eap_status_e status = m_state.set_buffer_length(m_hash.get_digest_length()); |
|
87 if (status != eap_status_ok) |
|
88 { |
|
89 return EAP_STATUS_RETURN(m_am_tools, status); |
|
90 } |
|
91 m_state.set_data_length(m_state.get_buffer_length()); |
|
92 |
|
93 if (m_state.get_is_valid_data() == false) |
|
94 { |
|
95 return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error); |
|
96 } |
|
97 |
|
98 m_am_tools->memset(m_state.get_data(m_state.get_data_length()), 0, m_state.get_data_length()); |
|
99 |
|
100 m_is_valid = true; |
|
101 |
|
102 if (length > 0ul) |
|
103 { |
|
104 status = internal_rand_seed( |
|
105 bytes, |
|
106 length); |
|
107 } |
|
108 |
|
109 return EAP_STATUS_RETURN(m_am_tools, status); |
|
110 } |
|
111 |
|
112 //-------------------------------------------------- |
|
113 |
|
114 // |
|
115 EAP_FUNC_EXPORT eap_status_e eap_am_random_test_c::internal_rand_seed( |
|
116 const void * const bytes, |
|
117 const u32_t length) |
|
118 { |
|
119 eap_status_e status = eap_status_process_general_error; |
|
120 |
|
121 EAP_ASSERT_ALWAYS(m_is_valid == true); |
|
122 |
|
123 status = m_hash.hash_init(); |
|
124 if (status != eap_status_ok) |
|
125 { |
|
126 return EAP_STATUS_RETURN(m_am_tools, status); |
|
127 } |
|
128 |
|
129 status = m_hash.hash_update( |
|
130 m_state.get_data(m_state.get_data_length()), |
|
131 m_state.get_data_length()); |
|
132 if (status != eap_status_ok) |
|
133 { |
|
134 return EAP_STATUS_RETURN(m_am_tools, status); |
|
135 } |
|
136 |
|
137 status = m_hash.hash_update( |
|
138 bytes, |
|
139 length); |
|
140 if (status != eap_status_ok) |
|
141 { |
|
142 return EAP_STATUS_RETURN(m_am_tools, status); |
|
143 } |
|
144 |
|
145 u32_t md_length = m_state.get_data_length(); |
|
146 |
|
147 status = m_hash.hash_final( |
|
148 m_state.get_data(m_state.get_data_length()), |
|
149 &md_length); |
|
150 if (status != eap_status_ok) |
|
151 { |
|
152 return EAP_STATUS_RETURN(m_am_tools, status); |
|
153 } |
|
154 |
|
155 EAP_ASSERT(md_length == m_state.get_data_length()); |
|
156 |
|
157 return EAP_STATUS_RETURN(m_am_tools, status); |
|
158 } |
|
159 |
|
160 //-------------------------------------------------- |
|
161 |
|
162 // |
|
163 EAP_FUNC_EXPORT eap_status_e eap_am_random_test_c::add_rand_seed( |
|
164 const void * const bytes, |
|
165 const u32_t length) |
|
166 { |
|
167 eap_status_e status = eap_status_ok; |
|
168 |
|
169 EAP_ASSERT_ALWAYS(m_is_valid == true); |
|
170 |
|
171 if (m_do_seeding == true) |
|
172 { |
|
173 EAP_TEST_RANDOM_TRACE_DATA_DEBUG( |
|
174 m_am_tools, |
|
175 EAP_TRACE_MASK_TEST_RANDOM, |
|
176 (EAPL("PSEUDO RANDOM SEED:"), |
|
177 bytes, |
|
178 length)); |
|
179 |
|
180 status = internal_rand_seed( |
|
181 bytes, |
|
182 length); |
|
183 } |
|
184 |
|
185 return EAP_STATUS_RETURN(m_am_tools, status); |
|
186 } |
|
187 |
|
188 //-------------------------------------------------- |
|
189 |
|
190 // |
|
191 EAP_FUNC_EXPORT eap_status_e eap_am_random_test_c::get_rand_bytes( |
|
192 void * const p_bytes, |
|
193 const u32_t length) |
|
194 { |
|
195 eap_status_e status = eap_status_process_general_error; |
|
196 |
|
197 EAP_ASSERT_ALWAYS(m_is_valid == true); |
|
198 |
|
199 if (p_bytes == 0) |
|
200 { |
|
201 return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter); |
|
202 } |
|
203 |
|
204 if (length == 0ul) |
|
205 { |
|
206 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
207 } |
|
208 |
|
209 u32_t count = length / m_state.get_data_length(); |
|
210 if ((length % m_state.get_data_length()) != 0) |
|
211 { |
|
212 ++count; |
|
213 } |
|
214 |
|
215 u32_t missing_bytes = length; |
|
216 u8_t * const bytes = static_cast<u8_t *>(p_bytes); |
|
217 |
|
218 for (u32_t ind = 0ul; ind < count; ind++) |
|
219 { |
|
220 ++m_rand_counter; |
|
221 |
|
222 status = internal_rand_seed( |
|
223 &m_rand_counter, |
|
224 sizeof(m_rand_counter)); |
|
225 if (status != eap_status_ok) |
|
226 { |
|
227 return EAP_STATUS_RETURN(m_am_tools, status); |
|
228 } |
|
229 |
|
230 status = internal_rand_seed( |
|
231 m_state.get_data(m_state.get_data_length()), |
|
232 m_state.get_data_length()); |
|
233 if (status != eap_status_ok) |
|
234 { |
|
235 return EAP_STATUS_RETURN(m_am_tools, status); |
|
236 } |
|
237 |
|
238 u32_t copied_data = missing_bytes; |
|
239 if (copied_data >= m_state.get_data_length()) |
|
240 { |
|
241 copied_data = m_state.get_data_length(); |
|
242 } |
|
243 |
|
244 m_am_tools->memmove(bytes+(m_state.get_data_length()*ind), m_state.get_data(copied_data), copied_data); |
|
245 |
|
246 missing_bytes -= copied_data; |
|
247 } |
|
248 |
|
249 EAP_TEST_RANDOM_TRACE_DATA_DEBUG( |
|
250 m_am_tools, |
|
251 EAP_TRACE_MASK_TEST_RANDOM, |
|
252 (EAPL("PSEUDO RANDOM DATA:"), |
|
253 p_bytes, |
|
254 length)); |
|
255 |
|
256 return EAP_STATUS_RETURN(m_am_tools, status); |
|
257 } |
|
258 |
|
259 //-------------------------------------------------- |
|
260 |
|
261 |
|
262 //-------------------------------------------------- |
|
263 // End. |