eapol/eapol_framework/eapol_common/am/include/eap_am_crypto_sha1.h
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 
       
    20 #if !defined( _EAP_AM_CRYPTO_SHA1_H_ )
       
    21 #define _EAP_AM_CRYPTO_SHA1_H_
       
    22 
       
    23 #include "eap_am_types.h"
       
    24 #include "eap_variable_data.h"
       
    25 #include "eap_am_export.h"
       
    26 #include "eap_am_tools.h"
       
    27 
       
    28 
       
    29 //--------------------------------------------------
       
    30 
       
    31 /// The eap_am_crypto_sha1_c class includes the state of 
       
    32 /// one instance of SHA1 algorithm.
       
    33 class EAP_EXPORT eap_am_crypto_sha1_c
       
    34 {
       
    35 
       
    36 private:
       
    37 	// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       
    38 
       
    39 	enum eap_sha1_H_init
       
    40 	{
       
    41 		EAP_SHA1_INIT_H0 = 0x67452301ul,
       
    42 		EAP_SHA1_INIT_H1 = 0xefcdab89ul,
       
    43 		EAP_SHA1_INIT_H2 = 0x98badcfeul,
       
    44 		EAP_SHA1_INIT_H3 = 0x10325476ul,
       
    45 		EAP_SHA1_INIT_H4 = 0xc3d2e1f0ul,
       
    46 	};
       
    47 
       
    48 	enum eap_sha1_K
       
    49 	{
       
    50 		EAP_SHA1_K__0_19 = 0x5a827999ul,
       
    51 		EAP_SHA1_K_20_39 = 0x6ed9eba1ul,
       
    52 		EAP_SHA1_K_40_59 = 0x8f1bbcdcul,
       
    53 		EAP_SHA1_K_60_79 = 0xca62c1d6ul,
       
    54 	};
       
    55 
       
    56 	enum eap_sha1_sizes
       
    57 	{
       
    58 		EAP_AM_CRYPTO_SHA1_BLOCK_u32_COUNT = 16ul, ///< 16 u32_t integers.
       
    59 		EAP_AM_CRYPTO_SHA1_BLOCK_BYTE_SIZE
       
    60 			= EAP_AM_CRYPTO_SHA1_BLOCK_u32_COUNT
       
    61 			* sizeof(u32_t), ///< in bytes.
       
    62 		EAP_AM_CRYPTO_SHA1_DIGEST_BUFFER_u32_COUNT
       
    63 			= 5ul, ///< 5 u32_t integers.
       
    64 		EAP_AM_CRYPTO_SHA1_DIGEST_BUFFER_BYTE_SIZE
       
    65 			= EAP_AM_CRYPTO_SHA1_DIGEST_BUFFER_u32_COUNT
       
    66 			* sizeof(u32_t), ///< in bytes.
       
    67 	};
       
    68 
       
    69 	// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       
    70 
       
    71 	/// This is pointer to the tools class.
       
    72 	abs_eap_am_tools_c * const m_am_tools;
       
    73 
       
    74 	/// This buffer saves remaining data between subsequent calls
       
    75 	/// of hash_update().
       
    76 	eap_variable_data_c m_saved_data;
       
    77 
       
    78 	/// This attribute saves the length of hashed data.
       
    79 	u64_t m_full_hashed_data_length;
       
    80 
       
    81 	/// Array of 16 temporary 32-bit unsigned integers.
       
    82 	u32_t m_T[EAP_AM_CRYPTO_SHA1_BLOCK_u32_COUNT];
       
    83 
       
    84 	
       
    85 	u32_t m_W_in_host_order[EAP_AM_CRYPTO_SHA1_BLOCK_u32_COUNT];
       
    86 
       
    87 	/// This buffer saves the digest value between subsequent call
       
    88 	/// of hash_update().
       
    89 	u32_t m_H[EAP_AM_CRYPTO_SHA1_DIGEST_BUFFER_u32_COUNT];
       
    90 
       
    91 	/// This indicates whether this object was generated successfully.
       
    92 	bool m_is_valid;
       
    93 
       
    94 	// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       
    95 
       
    96 	inline u32_t eap_sha1_rotate(
       
    97 		const u32_t value,
       
    98 		const u32_t shift
       
    99 		);
       
   100 
       
   101 	inline u32_t eap_sha1_b_substitution(
       
   102 		const u32_t Wt_3,
       
   103 		const u32_t Wt_8,
       
   104 		const u32_t Wt_14,
       
   105 		const u32_t Wt_16
       
   106 		);
       
   107 
       
   108 	inline u32_t eap_sha1_ft_0_19(
       
   109 		const u32_t B,
       
   110 		const u32_t C,
       
   111 		const u32_t D
       
   112 		);
       
   113 
       
   114 	inline u32_t eap_sha1_ft_20_39(
       
   115 		const u32_t B,
       
   116 		const u32_t C,
       
   117 		const u32_t D
       
   118 		);
       
   119 
       
   120 	inline u32_t eap_sha1_ft_40_59(
       
   121 		const u32_t B,
       
   122 		const u32_t C,
       
   123 		const u32_t D
       
   124 		);
       
   125 
       
   126 	inline u32_t eap_sha1_ft_60_79(
       
   127 		const u32_t B,
       
   128 		const u32_t C,
       
   129 		const u32_t D
       
   130 		);
       
   131 
       
   132 	inline void d_substitution(
       
   133 		u32_t * const A,
       
   134 		u32_t * const B,
       
   135 		u32_t * const C,
       
   136 		u32_t * const D,
       
   137 		u32_t * const E,
       
   138 		const u32_t temp
       
   139 		);
       
   140 
       
   141 	inline void d_substitution_0_15(
       
   142 		const u32_t t,
       
   143 		u32_t * const A,
       
   144 		u32_t * const B,
       
   145 		u32_t * const C,
       
   146 		u32_t * const D,
       
   147 		u32_t * const E,
       
   148 		const u32_t Wt
       
   149 		);
       
   150 
       
   151 	inline void d_substitution_16_19(
       
   152 		const u32_t t,
       
   153 		u32_t * const A,
       
   154 		u32_t * const B,
       
   155 		u32_t * const C,
       
   156 		u32_t * const D,
       
   157 		u32_t * const E,
       
   158 		u32_t * const Wt,
       
   159 		const u32_t Wt_3,
       
   160 		const u32_t Wt_8,
       
   161 		const u32_t Wt_14,
       
   162 		const u32_t Wt_16
       
   163 		);
       
   164 
       
   165 	inline void d_substitution_20_39(
       
   166 		const u32_t t,
       
   167 		u32_t * const A,
       
   168 		u32_t * const B,
       
   169 		u32_t * const C,
       
   170 		u32_t * const D,
       
   171 		u32_t * const E,
       
   172 		u32_t * const Wt,
       
   173 		const u32_t Wt_3,
       
   174 		const u32_t Wt_8,
       
   175 		const u32_t Wt_14,
       
   176 		const u32_t Wt_16
       
   177 		);
       
   178 
       
   179 	inline void d_substitution_40_59(
       
   180 		const u32_t t,
       
   181 		u32_t * const A,
       
   182 		u32_t * const B,
       
   183 		u32_t * const C,
       
   184 		u32_t * const D,
       
   185 		u32_t * const E,
       
   186 		u32_t * const Wt,
       
   187 		const u32_t Wt_3,
       
   188 		const u32_t Wt_8,
       
   189 		const u32_t Wt_14,
       
   190 		const u32_t Wt_16
       
   191 		);
       
   192 
       
   193 	inline void d_substitution_60_79(
       
   194 		const u32_t t,
       
   195 		u32_t * const A,
       
   196 		u32_t * const B,
       
   197 		u32_t * const C,
       
   198 		u32_t * const D,
       
   199 		u32_t * const E,
       
   200 		u32_t * const Wt,
       
   201 		const u32_t Wt_3,
       
   202 		const u32_t Wt_8,
       
   203 		const u32_t Wt_14,
       
   204 		const u32_t Wt_16
       
   205 		);
       
   206 	
       
   207 	/// @param W is an array of modulo 16 input 32-bit unsigned integers
       
   208 	/// in host order.
       
   209 	/// @param W_count is count of integers in W array.
       
   210 	EAP_FUNC_IMPORT eap_status_e eap_sha1_process_data_host_order(
       
   211 		const u32_t * W,
       
   212 		u32_t W_count
       
   213 		);
       
   214 
       
   215 	/// @param W is an array of modulo 16 input 32-bit unsigned integers
       
   216 	/// in host order.
       
   217 	/// @param W_count is count of integers in W array.
       
   218 	EAP_FUNC_IMPORT eap_status_e eap_sha1_process_data_network_order(
       
   219 		const u32_t * W,
       
   220 		u32_t W_count
       
   221 		);
       
   222 
       
   223 	/**
       
   224 	 * This function cleans up the SHA1 context.
       
   225 	 */
       
   226 	EAP_FUNC_IMPORT eap_status_e hash_cleanup();
       
   227 
       
   228 	/**
       
   229 	 * This function copies the message digest to output buffer.
       
   230 	 */
       
   231 	EAP_FUNC_IMPORT eap_status_e copy_message_digest(
       
   232 		void * const output,
       
   233 		u32_t * const max_output_size);
       
   234 
       
   235 
       
   236 	/**
       
   237 	 * The set_is_invalid() function sets the state of the eap_am_crypto_md4_c
       
   238 	 * object invalid. 
       
   239 	 * The eap_am_crypto_md4_c object calls this function after it is
       
   240 	 * initialized.
       
   241 	 */
       
   242 	EAP_FUNC_IMPORT void set_is_invalid();
       
   243 
       
   244 	/**
       
   245 	 * The set_is_valid() function sets the state of the eap_am_crypto_md4_c
       
   246 	 * object valid. 
       
   247 	 * The eap_am_crypto_md4_c object calls this function after it is
       
   248 	 * initialized.
       
   249 	 */
       
   250 	EAP_FUNC_IMPORT void set_is_valid();
       
   251 
       
   252 	/**
       
   253 	 * This function copies the context of SHA1.
       
   254 	 */
       
   255 	EAP_FUNC_IMPORT eap_status_e copy_context(
       
   256 		const eap_variable_data_c * const saved_data,
       
   257 		const u64_t full_hashed_data_length,
       
   258 		const u32_t * const H,
       
   259 		const u32_t * const T,
       
   260 		const u32_t * const W_in_host_order);
       
   261 
       
   262 	// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       
   263 public:
       
   264 	// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       
   265 
       
   266 	/**
       
   267 	 * Destructor does nothing special.
       
   268 	 */
       
   269 	EAP_FUNC_IMPORT virtual ~eap_am_crypto_sha1_c();
       
   270 
       
   271 	/**
       
   272 	 * Constructor initializes the member attributes.
       
   273 	 */
       
   274 	EAP_FUNC_IMPORT eap_am_crypto_sha1_c(abs_eap_am_tools_c * const tools);
       
   275 
       
   276 	/**
       
   277 	 * The get_is_valid() function returns the status of the
       
   278 	 * eap_am_crypto_md4_c object. 
       
   279 	 * True indicates the object is allocated successfully.
       
   280 	 */
       
   281 	EAP_FUNC_IMPORT bool get_is_valid();
       
   282 
       
   283 	/**
       
   284 	 * This function returns the size of message digest of HASH-algorithm.
       
   285 	 */
       
   286 	EAP_FUNC_IMPORT u32_t get_digest_length();
       
   287 
       
   288 	/**
       
   289 	 * This function returns the size of block of HASH-algorithm.
       
   290 	 */
       
   291 	EAP_FUNC_IMPORT u32_t get_block_size();
       
   292 
       
   293 	/**
       
   294 	 * This function initializes the context of SHA1-algorithm.
       
   295 	 */
       
   296 	EAP_FUNC_IMPORT eap_status_e hash_init();
       
   297 
       
   298 	/**
       
   299 	 * This function updates the context of SHA1-algorithm with data.
       
   300 	 */
       
   301 	EAP_FUNC_IMPORT eap_status_e hash_update(
       
   302 		const void * const data,
       
   303 		const u32_t data_length);
       
   304 
       
   305 	/**
       
   306 	 * This function writes the message digest to buffer.
       
   307 	 * @param Length is set if md_length_or_null is non-NULL.
       
   308 	 */
       
   309 	EAP_FUNC_IMPORT eap_status_e hash_final(
       
   310 		void * const message_digest,
       
   311 		u32_t *md_length_or_null);
       
   312 
       
   313 	/**
       
   314 	 *  
       
   315 	 *  eap_sha1_dss_G_function() implements the G() function using
       
   316 	 *  modified SHA-1 using the routine in Appendix 3.3 Constructing
       
   317 	 *  The Function G From SHA-1 in the SECURE HASH STANDARD, FIPS PUB 180-1.
       
   318 	 */
       
   319 	EAP_FUNC_IMPORT eap_status_e eap_sha1_dss_G_function(
       
   320 		const void * const data,
       
   321 		const u32_t data_length,
       
   322 		void * const output,
       
   323 		u32_t * const output_length
       
   324 		);
       
   325 
       
   326 	/**
       
   327 	 * This function copies the context of SHA1.
       
   328 	 */
       
   329 	EAP_FUNC_IMPORT eap_am_crypto_sha1_c * copy();
       
   330 
       
   331 };
       
   332 
       
   333 //--------------------------------------------------
       
   334 
       
   335 #endif //#if !defined( _EAP_AM_CRYPTO_OPENSSL_H_ )
       
   336 
       
   337 
       
   338 
       
   339 // End.