eapol/eapol_framework/eapol_common/am/common/crypto/rc4/eap_am_crypto_rc4.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 6 
       
    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_am_crypto_rc4.h"
       
    30 
       
    31 //------------------------------------------------------------
       
    32 
       
    33 #if 1
       
    34 	#define EAP_RC4_TRACE_DEBUG EAP_TRACE_DEBUG
       
    35 #else
       
    36 	#define EAP_RC4_TRACE_DEBUG(tools, flags, params)
       
    37 #endif
       
    38 
       
    39 #if defined(USE_EAP_TRACE)	
       
    40 	static const u32_t EAP_TRACE_MASK_RC4 = eap_am_tools_c::eap_trace_mask_crypto_rc4;
       
    41 #endif //#if defined(USE_EAP_TRACE)	
       
    42 
       
    43 //------------------------------------------------------------
       
    44 
       
    45 /**
       
    46  * Destructor resets the used internal buffers.
       
    47  */
       
    48 EAP_FUNC_EXPORT eap_am_crypto_rc4_c::~eap_am_crypto_rc4_c()
       
    49 {
       
    50 	cleanup();
       
    51 }
       
    52 
       
    53 //------------------------------------------------------------
       
    54 
       
    55 /**
       
    56  * Constructor initializes the used internal buffers.
       
    57  */
       
    58 EAP_FUNC_EXPORT eap_am_crypto_rc4_c::eap_am_crypto_rc4_c(
       
    59 	abs_eap_am_tools_c * const tools)
       
    60 	: m_am_tools(tools)
       
    61 	, m_ind_i(0ul)
       
    62 	, m_ind_j(0ul)
       
    63 	, m_is_valid(false)
       
    64 {
       
    65 	m_state[0] = 0;
       
    66 	set_is_valid();
       
    67 }
       
    68 
       
    69 //--------------------------------------------------
       
    70 
       
    71 /**
       
    72  * This function cleans up the RC4 context.
       
    73  */
       
    74 EAP_FUNC_EXPORT eap_status_e eap_am_crypto_rc4_c::cleanup()
       
    75 {
       
    76 	m_ind_i = 0ul;
       
    77 	m_ind_j = 0ul;
       
    78 	m_is_valid = false;
       
    79 	m_am_tools->memset(m_state, 0, eap_am_crypto_rc4_constant_state_size);
       
    80 
       
    81 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
    82 }
       
    83 
       
    84 //------------------------------------------------------------
       
    85 
       
    86 /**
       
    87  * The set_is_invalid() function sets the state of the eap_am_crypto_rc4_c
       
    88  * object invalid. 
       
    89  * The eap_am_crypto_rc4_c object calls this function after it is initialized.
       
    90  */
       
    91 EAP_FUNC_EXPORT void eap_am_crypto_rc4_c::set_is_invalid()
       
    92 {
       
    93 	m_is_valid = false;
       
    94 }
       
    95 
       
    96 //------------------------------------------------------------
       
    97 
       
    98 /**
       
    99  * The set_is_valid() function sets the state of the eap_am_crypto_rc4_c
       
   100  * object valid. 
       
   101  * The eap_am_crypto_rc4_c object calls this function after it is initialized.
       
   102  */
       
   103 EAP_FUNC_EXPORT void eap_am_crypto_rc4_c::set_is_valid()
       
   104 {
       
   105 	m_is_valid = true;
       
   106 }
       
   107 
       
   108 //------------------------------------------------------------
       
   109 
       
   110 /**
       
   111  * The get_is_valid() function returns the status of the eap_am_crypto_rc4_c
       
   112  * object. 
       
   113  * True indicates the object is allocated successfully.
       
   114  */
       
   115 EAP_FUNC_EXPORT bool eap_am_crypto_rc4_c::get_is_valid()
       
   116 {
       
   117 	return m_is_valid;
       
   118 }
       
   119 
       
   120 //------------------------------------------------------------
       
   121 
       
   122 /**
       
   123  * Function swaps the values.
       
   124  */
       
   125 EAP_FUNC_EXPORT inline void eap_am_crypto_rc4_c::swap(
       
   126 	u8_t * const s_i,
       
   127 	u8_t * const s_j)
       
   128 {
       
   129 	const u8_t tmp = *s_i;
       
   130 	*s_i = *s_j;
       
   131 	*s_j = tmp;
       
   132 }
       
   133 
       
   134 //------------------------------------------------------------
       
   135 
       
   136 /**
       
   137  * This function sets the RC4 key.
       
   138  */
       
   139 EAP_FUNC_EXPORT eap_status_e eap_am_crypto_rc4_c::set_key(
       
   140 	const eap_variable_data_c * const p_key)
       
   141 {
       
   142 	u32_t ind = 0ul;
       
   143 
       
   144 	// Note the size of ind must be bigger than 8-bits.
       
   145 	for (ind = 0ul; ind < eap_am_crypto_rc4_constant_state_size
       
   146 			 ; ind++)
       
   147 	{
       
   148 		m_state[ind] = static_cast<u8_t>(ind);
       
   149 
       
   150 		EAP_RC4_TRACE_DEBUG(m_am_tools, EAP_TRACE_MASK_RC4,
       
   151 					(EAPL("RC4: m_state[%d]=0x%02x\n"),
       
   152 					ind,
       
   153 					m_state[ind]));
       
   154 	} // for()
       
   155 
       
   156 	const u32_t key_length = p_key->get_data_length();
       
   157 	const u8_t * const key = p_key->get_data(key_length);
       
   158 
       
   159 	m_ind_i = 0ul;
       
   160 	m_ind_j = 0ul;
       
   161 
       
   162 	// Note the size of ind must be bigger than 8-bits.
       
   163 	for (ind = 0ul; ind < eap_am_crypto_rc4_constant_state_size
       
   164 			 ; ind++)
       
   165 	{
       
   166 		m_ind_j = static_cast<u8_t>(
       
   167 			static_cast<u8_t>(m_ind_j)
       
   168 			+ static_cast<u8_t>(m_state[ind])
       
   169 			+ static_cast<u8_t>(key[ind % key_length]));
       
   170 
       
   171 		swap(&(m_state[ind]), &(m_state[m_ind_j]));
       
   172 
       
   173 		EAP_RC4_TRACE_DEBUG(m_am_tools, EAP_TRACE_MASK_RC4,
       
   174 					(EAPL("RC4: swapped m_state[%d]=0x%02x and m_state[%d]=0x%02x\n"),
       
   175 					ind,
       
   176 					m_state[ind],
       
   177 					m_ind_j,
       
   178 					m_state[m_ind_j]));
       
   179 	} // for()
       
   180 
       
   181 	m_ind_i = 0ul;
       
   182 	m_ind_j = 0ul;
       
   183 
       
   184 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   185 }
       
   186 
       
   187 //------------------------------------------------------------
       
   188 
       
   189 /**
       
   190  */
       
   191 inline u8_t eap_am_crypto_rc4_c::get_random_byte()
       
   192 {
       
   193 	m_ind_i = static_cast<u8_t>(m_ind_i + 1ul);
       
   194 	
       
   195 	m_ind_j = static_cast<u8_t>(m_ind_j + m_state[m_ind_i]);
       
   196 	
       
   197 	swap(&(m_state[m_ind_i]), &(m_state[m_ind_j]));
       
   198 	
       
   199 	u8_t random_byte = m_state[static_cast<u8_t>(m_state[m_ind_i]
       
   200 									   + m_state[m_ind_j])];
       
   201 
       
   202 	EAP_RC4_TRACE_DEBUG(m_am_tools, EAP_TRACE_MASK_RC4,
       
   203 				(EAPL("RC4: random_byte=0x%02x\n"),
       
   204 				random_byte));
       
   205 
       
   206 	return random_byte;
       
   207 }
       
   208 
       
   209 //------------------------------------------------------------
       
   210 
       
   211 /**
       
   212  * This function does RC4 encryption.
       
   213  */
       
   214 inline eap_status_e eap_am_crypto_rc4_c::process_data(
       
   215 	const void * const p_data_in,
       
   216 	void * const p_data_out,
       
   217 	const u32_t data_length)
       
   218 {
       
   219 	const u8_t * const data_in = static_cast<const u8_t *>(p_data_in);
       
   220 	u8_t * const data_out = static_cast<u8_t *>(p_data_out);
       
   221 
       
   222 	for (u32_t ind = 0ul; ind < data_length; ind++)
       
   223 	{
       
   224 		EAP_RC4_TRACE_DEBUG(m_am_tools, EAP_TRACE_MASK_RC4,
       
   225 					(EAPL("RC4: data_in[%d]=0x%02x\n"),
       
   226 					ind,
       
   227 					data_in[ind]));
       
   228 
       
   229 		data_out[ind] = static_cast<u8_t>(data_in[ind] ^ get_random_byte());
       
   230 
       
   231 		EAP_RC4_TRACE_DEBUG(m_am_tools, EAP_TRACE_MASK_RC4,
       
   232 					(EAPL("RC4: data_out[%d]=0x%02x\n"),
       
   233 					ind,
       
   234 					data_out[ind]));
       
   235 	} // for()
       
   236 
       
   237 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   238 }
       
   239 
       
   240 //------------------------------------------------------------
       
   241 
       
   242 /**
       
   243  * This function does RC4 encryption.
       
   244  */
       
   245 EAP_FUNC_EXPORT eap_status_e eap_am_crypto_rc4_c::encrypt_data(
       
   246 	const void * const p_data_in,
       
   247 	void * const p_data_out,
       
   248 	const u32_t data_length)
       
   249 {
       
   250 	return EAP_STATUS_RETURN(
       
   251 		m_am_tools,
       
   252 		process_data(p_data_in, p_data_out, data_length));
       
   253 }
       
   254 
       
   255 //------------------------------------------------------------
       
   256 
       
   257 /**
       
   258  * This function does RC4 decryption.
       
   259  */
       
   260 EAP_FUNC_EXPORT eap_status_e eap_am_crypto_rc4_c::decrypt_data(
       
   261 	const void * const p_data_in,
       
   262 	void * const p_data_out,
       
   263 	const u32_t data_length)
       
   264 {
       
   265 	return EAP_STATUS_RETURN(
       
   266 		m_am_tools,
       
   267 		process_data(p_data_in, p_data_out, data_length));
       
   268 }
       
   269 
       
   270 //------------------------------------------------------------
       
   271 
       
   272 
       
   273 
       
   274 // End.