eapol/eapol_framework/eapol_common/am/common/bloom_algorithm/eap_am_bloom_algorithm.cpp
changeset 0 c8830336c852
child 2 1c7bc153c08e
equal deleted inserted replaced
-1:000000000000 0:c8830336c852
       
     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 3 
       
    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_memory.h"
       
    29 #include "eap_tools.h"
       
    30 #include "eap_crypto_api.h"
       
    31 #include "eap_am_bloom_algorithm.h"
       
    32 
       
    33 //--------------------------------------------------
       
    34 
       
    35 //
       
    36 EAP_FUNC_EXPORT eap_am_bloom_algorithm_c::~eap_am_bloom_algorithm_c()
       
    37 {
       
    38 }
       
    39 
       
    40 //--------------------------------------------------
       
    41 
       
    42 //
       
    43 EAP_FUNC_EXPORT eap_am_bloom_algorithm_c::eap_am_bloom_algorithm_c(
       
    44 		abs_eap_am_tools_c * const tools,
       
    45 		abs_eap_am_bloom_algorithm_store_c * const store,
       
    46 		const u32_t bloom_bit_index_size)
       
    47 	: m_am_tools(tools)
       
    48 	, m_store(store)
       
    49 	, m_bloom_bit_index_size(bloom_bit_index_size)
       
    50 	, m_is_valid(false)
       
    51 {
       
    52 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
    53 
       
    54 	eap_status_e status = set_bloom_bit_index_size(bloom_bit_index_size);
       
    55 	if (status != eap_status_ok)
       
    56 	{
       
    57 		EAP_TRACE_ERROR(
       
    58 			m_am_tools,
       
    59 			TRACE_FLAGS_DEFAULT,
       
    60 			(EAPL("ERROR: BLOOM: Illegal bit index size %d.\n"),
       
    61 			 bloom_bit_index_size));
       
    62 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
    63 		return;
       
    64 	}
       
    65 
       
    66 	if (m_store == 0
       
    67 		|| m_store->get_is_valid() == false)
       
    68 	{
       
    69 		EAP_TRACE_ERROR(m_am_tools, TRACE_FLAGS_DEFAULT, (EAPL("ERROR: BLOOM: Store is invalid.\n")));
       
    70 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
    71 		return;
       
    72 	}
       
    73 
       
    74 	status = m_store->set_bloom_bit_index_size(bloom_bit_index_size);
       
    75 	if (status != eap_status_ok)
       
    76 	{
       
    77 		EAP_TRACE_ERROR(
       
    78 			m_am_tools,
       
    79 			TRACE_FLAGS_DEFAULT,
       
    80 			(EAPL("ERROR: BLOOM: Illegal bit index size %d.\n"),
       
    81 			 bloom_bit_index_size));
       
    82 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
    83 		return;
       
    84 	}
       
    85 
       
    86 	set_is_valid();
       
    87 
       
    88 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
    89 }
       
    90 
       
    91 //--------------------------------------------------
       
    92 
       
    93 /// This is the count of bits in the index of Bloom algorithm.
       
    94 EAP_FUNC_EXPORT eap_status_e eap_am_bloom_algorithm_c::set_bloom_bit_index_size(const u32_t bloom_bit_index_size)
       
    95 {
       
    96 	if (bloom_bit_index_size > 32ul)
       
    97 	{
       
    98 		// This is absolut maximum value.
       
    99 		// Much smaller value should be used in real application.
       
   100 		// 32 bits long index means 2^32 bit long bit store.
       
   101 		return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
       
   102 	}
       
   103 	else if (bloom_bit_index_size < 4ul)
       
   104 	{
       
   105 		// This is absolut minimum value.
       
   106 		// Much bigger value should be used in real application.
       
   107 		// 4 bits long index means 2^4 bit long bit store.
       
   108 		return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
       
   109 	}
       
   110 
       
   111 	m_bloom_bit_index_size = bloom_bit_index_size;
       
   112 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   113 }
       
   114 
       
   115 //--------------------------------------------------
       
   116 
       
   117 /**
       
   118  * @code
       
   119  *  0                   1                   2                   3                   4                   5
       
   120  *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
       
   121  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
   122  *  | data 0        | data 1        | data 2        | data 3        | data 4        | data 5        | ...
       
   123  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
   124  *  ^                     ^                     ^                     ^                     ^
       
   125  *  |                     |                     |                     |                     |
       
   126  *  |                     |                     |                     |                     |
       
   127  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
   128  *  |  bit_index 0        |  bit_index 1        | bit_index 2         | bit_index 3         |
       
   129  *  +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
       
   130  * @endcode
       
   131  */
       
   132 EAP_FUNC_EXPORT u32_t eap_am_bloom_algorithm_c::bloom_filter_get_index(
       
   133 	const u32_t queried_bit_index,
       
   134 	const void * const message_digest,
       
   135 	const u32_t message_digest_length)
       
   136 {
       
   137 	const u8_t * const input_data = static_cast<const u8_t *>(message_digest);
       
   138 	u32_t bit_index = 0ul;
       
   139 	u32_t start_byte = (queried_bit_index * m_bloom_bit_index_size) / 8ul;
       
   140 	u32_t end_byte = (queried_bit_index * m_bloom_bit_index_size + (m_bloom_bit_index_size - 1ul)) / 8ul;
       
   141 
       
   142 	for (u32_t data_ind = start_byte; data_ind <= end_byte; data_ind++)
       
   143 	{
       
   144 		i32_t shift = ((data_ind+1ul) * 8ul) - ((queried_bit_index + 1ul) * m_bloom_bit_index_size);
       
   145 		if (shift < 0)
       
   146 		{
       
   147 			const u32_t pre_mask = 0xffffffff >> (32ul - m_bloom_bit_index_size);
       
   148 			const u8_t mask = static_cast<u8_t>(pre_mask >> (-shift));
       
   149 			bit_index |= (input_data[data_ind] & mask) << (-shift);
       
   150 		}
       
   151 		else
       
   152 		{
       
   153 			const u8_t mask = 0xff << shift;
       
   154 			bit_index |= (input_data[data_ind] & mask) >> (shift);
       
   155 		}
       
   156 	}
       
   157 
       
   158 	return bit_index;
       
   159 }
       
   160 
       
   161 //--------------------------------------------------
       
   162 
       
   163 //
       
   164 EAP_FUNC_EXPORT eap_status_e eap_am_bloom_algorithm_c::bloom_filter_create_message_digest(
       
   165 	const void * const blob,
       
   166 	const u32_t blob_length,
       
   167 	void * const message_digest,
       
   168 	u32_t * const message_digest_length)
       
   169 {
       
   170 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   171 
       
   172 	crypto_sha1_c sha1(m_am_tools);
       
   173 
       
   174 	if (sha1.get_is_valid() == false)
       
   175 	{
       
   176 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   177 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   178 	}
       
   179 
       
   180 	eap_status_e status = sha1.hash_init();
       
   181 	if (status != eap_status_ok)
       
   182 	{
       
   183 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   184 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   185 	}
       
   186 
       
   187 	if (message_digest == 0
       
   188 		|| message_digest_length == 0
       
   189 		|| *message_digest_length < sha1.get_digest_length())
       
   190 	{
       
   191 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   192 		return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
       
   193 	}
       
   194 
       
   195 	*message_digest_length = sha1.get_digest_length();
       
   196 
       
   197 	status = sha1.hash_update(blob, blob_length);
       
   198 	if (status != eap_status_ok)
       
   199 	{
       
   200 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   201 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   202 	}
       
   203 
       
   204 	status = sha1.hash_final(
       
   205 		message_digest,
       
   206 		message_digest_length);
       
   207 
       
   208 	EAP_ASSERT_ALWAYS(sha1.get_digest_length() == *message_digest_length);
       
   209 
       
   210 	EAP_TRACE_DATA_DEBUG(m_am_tools, TRACE_FLAGS_DEFAULT, (EAPL("BLOOM: blob"),
       
   211 		blob,
       
   212 		blob_length));
       
   213 
       
   214 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   215 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   216 }
       
   217 
       
   218 //--------------------------------------------------
       
   219 
       
   220 //
       
   221 EAP_FUNC_EXPORT eap_status_e eap_am_bloom_algorithm_c::bloom_filter_check_is_blob_new(
       
   222 	const void * const blob,
       
   223 	const u32_t blob_length)
       
   224 {
       
   225 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   226 
       
   227 	if (m_store->bloom_filter_check_does_bit_store_exists() != eap_status_ok)
       
   228 	{
       
   229 		// No valid BIT file. blobs are assumed new.
       
   230 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   231 		return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   232 	}
       
   233 
       
   234 	static const u32_t DIGEST_LENGTH = 32ul;
       
   235 
       
   236 	u8_t message_digest[DIGEST_LENGTH];
       
   237 	u32_t message_digest_length = DIGEST_LENGTH;
       
   238 
       
   239 	eap_status_e status = bloom_filter_create_message_digest(
       
   240 		blob,
       
   241 		blob_length,
       
   242 		message_digest,
       
   243 		&message_digest_length);
       
   244 	if (status != eap_status_ok)
       
   245 	{
       
   246 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   247 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   248 	}
       
   249 
       
   250 
       
   251 	for (u32_t ind = 0; ind < (message_digest_length * 8ul)/m_bloom_bit_index_size; ind++)
       
   252 	{
       
   253 		u32_t bit_index = bloom_filter_get_index(ind, message_digest, message_digest_length);
       
   254 
       
   255 		if (m_store->bloom_filter_get_bit_index(bit_index) == 0)
       
   256 		{
       
   257 			// Because bit is NOT set this blob is new.
       
   258 			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   259 			return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   260 		}
       
   261 	}
       
   262 
       
   263 	// Because all bits are set this blob is most probably already used.
       
   264 
       
   265 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   266 	return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_data_payload);
       
   267 }
       
   268 
       
   269 //--------------------------------------------------
       
   270 
       
   271 //
       
   272 EAP_FUNC_EXPORT eap_status_e eap_am_bloom_algorithm_c::bloom_filter_set_blob_is_used(
       
   273 	const void * const blob,
       
   274 	const u32_t blob_length)
       
   275 {
       
   276 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   277 
       
   278 	if (m_store->bloom_filter_check_does_bit_store_exists() != eap_status_ok)
       
   279 	{
       
   280 		// No valid BIT file. blobs are assumed new.
       
   281 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   282 		return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   283 	}
       
   284 
       
   285 	static const u32_t DIGEST_LENGTH = 32ul;
       
   286 
       
   287 	u8_t message_digest[DIGEST_LENGTH];
       
   288 	u32_t message_digest_length = DIGEST_LENGTH;
       
   289 
       
   290 	eap_status_e status = bloom_filter_create_message_digest(
       
   291 		blob,
       
   292 		blob_length,
       
   293 		message_digest,
       
   294 		&message_digest_length);
       
   295 	if (status != eap_status_ok)
       
   296 	{
       
   297 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   298 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   299 	}
       
   300 
       
   301 
       
   302 	for (u32_t ind = 0; ind < (message_digest_length * 8ul)/m_bloom_bit_index_size; ind++)
       
   303 	{
       
   304 		u32_t bit_index = bloom_filter_get_index(ind, message_digest, message_digest_length);
       
   305 
       
   306 		status = m_store->bloom_filter_set_bit_index(bit_index);
       
   307 		if (status != eap_status_ok)
       
   308 		{
       
   309 			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   310 			return EAP_STATUS_RETURN(m_am_tools, status);
       
   311 		}
       
   312 	}
       
   313 
       
   314 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   315 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   316 }
       
   317 
       
   318 //--------------------------------------------------
       
   319 
       
   320 //
       
   321 EAP_FUNC_EXPORT void eap_am_bloom_algorithm_c::set_is_valid()
       
   322 {
       
   323 	m_is_valid = true;
       
   324 }
       
   325 
       
   326 //--------------------------------------------------
       
   327 
       
   328 //
       
   329 EAP_FUNC_EXPORT bool eap_am_bloom_algorithm_c::get_is_valid()
       
   330 {
       
   331 	return m_is_valid;
       
   332 }
       
   333 
       
   334 //--------------------------------------------------
       
   335 
       
   336 
       
   337 
       
   338 // End.