eapol/eapol_framework/eapol_common/am/include/eap_am_crypto_sha_256.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_SHA_256_H_ )
       
    21 #define _EAP_AM_CRYPTO_SHA_256_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_sha_256_c class includes the state of 
       
    32 /// one instance of SHA_256 algorithm.
       
    33 class EAP_EXPORT eap_am_crypto_sha_256_c
       
    34 {
       
    35 
       
    36 private:
       
    37 	// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       
    38 
       
    39 	enum eap_sha_256_H_init
       
    40 	{
       
    41 		EAP_SHA_256_INIT_H0 = 0x6a09e667,
       
    42 		EAP_SHA_256_INIT_H1 = 0xbb67ae85,
       
    43 		EAP_SHA_256_INIT_H2 = 0x3c6ef372,
       
    44 		EAP_SHA_256_INIT_H3 = 0xa54ff53a,
       
    45 		EAP_SHA_256_INIT_H4 = 0x510e527f,
       
    46 		EAP_SHA_256_INIT_H5 = 0x9b05688c,
       
    47 		EAP_SHA_256_INIT_H6 = 0x1f83d9ab,
       
    48 		EAP_SHA_256_INIT_H7 = 0x5be0cd19,
       
    49 	};
       
    50 
       
    51 	enum eap_sha_256_sizes
       
    52 	{
       
    53 		EAP_AM_CRYPTO_SHA_256_BLOCK_u32_COUNT = 16ul, ///< 16 u32_t integers.
       
    54 		EAP_AM_CRYPTO_SHA_256_BLOCK_BYTE_SIZE
       
    55 			= EAP_AM_CRYPTO_SHA_256_BLOCK_u32_COUNT
       
    56 			* sizeof(u32_t), ///< in bytes.
       
    57 		EAP_AM_CRYPTO_SHA_256_DIGEST_BUFFER_u32_COUNT
       
    58 			= 8ul, ///< 8 u32_t integers.
       
    59 		EAP_AM_CRYPTO_SHA_256_DIGEST_BUFFER_BYTE_SIZE
       
    60 			= EAP_AM_CRYPTO_SHA_256_DIGEST_BUFFER_u32_COUNT
       
    61 			* sizeof(u32_t), ///< in bytes.
       
    62 		EAP_AM_CRYPTO_SHA_256_SCHEDULE_u32_COUNT = 64ul, ///< 64 u32_t integers.
       
    63 	};
       
    64 
       
    65 	// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       
    66 
       
    67 	/// This is pointer to the tools class.
       
    68 	abs_eap_am_tools_c * const m_am_tools;
       
    69 
       
    70 	/// This buffer saves remaining data between subsequent calls
       
    71 	/// of hash_update().
       
    72 	eap_variable_data_c m_saved_data;
       
    73 
       
    74 	/// This attribute saves the length of hashed data.
       
    75 	u64_t m_full_hashed_data_length;
       
    76 
       
    77 	/// Array of 16 temporary 32-bit unsigned integers.
       
    78 	u32_t m_T[EAP_AM_CRYPTO_SHA_256_BLOCK_u32_COUNT];
       
    79 
       
    80 	static const unsigned long m_K[EAP_AM_CRYPTO_SHA_256_SCHEDULE_u32_COUNT];
       
    81 	
       
    82 	u32_t m_M_in_host_order[EAP_AM_CRYPTO_SHA_256_BLOCK_u32_COUNT];
       
    83 
       
    84 	/// This buffer saves the digest value between subsequent call
       
    85 	/// of hash_update().
       
    86 	u32_t m_H[EAP_AM_CRYPTO_SHA_256_DIGEST_BUFFER_u32_COUNT];
       
    87 
       
    88 	/// This indicates whether this object was generated successfully.
       
    89 	bool m_is_valid;
       
    90 
       
    91 	// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       
    92 
       
    93 	inline u32_t eap_sha_256_rotate(
       
    94 		const u32_t value,
       
    95 		const u32_t shift
       
    96 		);
       
    97 
       
    98 	/// @param W is an array of modulo 16 input 32-bit unsigned integers
       
    99 	/// in host order.
       
   100 	/// @param W_count is count of integers in W array.
       
   101 	EAP_FUNC_IMPORT eap_status_e eap_sha_256_process_data_host_order(
       
   102 		const u32_t * W,
       
   103 		u32_t W_count
       
   104 		);
       
   105 
       
   106 	/// @param W is an array of modulo 16 input 32-bit unsigned integers
       
   107 	/// in host order.
       
   108 	/// @param W_count is count of integers in W array.
       
   109 	EAP_FUNC_IMPORT eap_status_e eap_sha_256_process_data_network_order(
       
   110 		const u32_t * W,
       
   111 		u32_t W_count
       
   112 		);
       
   113 
       
   114 	/**
       
   115 	 * This function cleans up the SHA_256 context.
       
   116 	 */
       
   117 	EAP_FUNC_IMPORT eap_status_e hash_cleanup();
       
   118 
       
   119 	/**
       
   120 	 * This function copies the message digest to output buffer.
       
   121 	 */
       
   122 	EAP_FUNC_IMPORT eap_status_e copy_message_digest(
       
   123 		void * const output,
       
   124 		u32_t * const max_output_size);
       
   125 
       
   126 
       
   127 	/**
       
   128 	 * The set_is_invalid() function sets the state of the eap_am_crypto_md4_c
       
   129 	 * object invalid. 
       
   130 	 * The eap_am_crypto_md4_c object calls this function after it is
       
   131 	 * initialized.
       
   132 	 */
       
   133 	EAP_FUNC_IMPORT void set_is_invalid();
       
   134 
       
   135 	/**
       
   136 	 * The set_is_valid() function sets the state of the eap_am_crypto_md4_c
       
   137 	 * object valid. 
       
   138 	 * The eap_am_crypto_md4_c object calls this function after it is
       
   139 	 * initialized.
       
   140 	 */
       
   141 	EAP_FUNC_IMPORT void set_is_valid();
       
   142 
       
   143 	/**
       
   144 	 * This function copies the context of SHA_256.
       
   145 	 */
       
   146 	EAP_FUNC_IMPORT eap_status_e copy_context(
       
   147 		const eap_variable_data_c * const saved_data,
       
   148 		const u64_t full_hashed_data_length,
       
   149 		const u32_t * const H,
       
   150 		const u32_t * const T,
       
   151 		const u32_t * const W_in_host_order);
       
   152 
       
   153 	// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       
   154 public:
       
   155 	// - - - - - - - - - - - - - - - - - - - - - - - - - - - -
       
   156 
       
   157 	/**
       
   158 	 * Destructor does nothing special.
       
   159 	 */
       
   160 	EAP_FUNC_IMPORT virtual ~eap_am_crypto_sha_256_c();
       
   161 
       
   162 	/**
       
   163 	 * Constructor initializes the member attributes.
       
   164 	 */
       
   165 	EAP_FUNC_IMPORT eap_am_crypto_sha_256_c(abs_eap_am_tools_c * const tools);
       
   166 
       
   167 	/**
       
   168 	 * The get_is_valid() function returns the status of the
       
   169 	 * eap_am_crypto_md4_c object. 
       
   170 	 * True indicates the object is allocated successfully.
       
   171 	 */
       
   172 	EAP_FUNC_IMPORT bool get_is_valid();
       
   173 
       
   174 	/**
       
   175 	 * This function returns the size of message digest of HASH-algorithm.
       
   176 	 */
       
   177 	EAP_FUNC_IMPORT u32_t get_digest_length();
       
   178 
       
   179 	/**
       
   180 	 * This function returns the size of block of HASH-algorithm.
       
   181 	 */
       
   182 	EAP_FUNC_IMPORT u32_t get_block_size();
       
   183 
       
   184 	/**
       
   185 	 * This function initializes the context of SHA_256-algorithm.
       
   186 	 */
       
   187 	EAP_FUNC_IMPORT eap_status_e hash_init();
       
   188 
       
   189 	/**
       
   190 	 * This function updates the context of SHA_256-algorithm with data.
       
   191 	 */
       
   192 	EAP_FUNC_IMPORT eap_status_e hash_update(
       
   193 		const void * const data,
       
   194 		const u32_t data_length);
       
   195 
       
   196 	/**
       
   197 	 * This function writes the message digest to buffer.
       
   198 	 * @param Length is set if md_length_or_null is non-NULL.
       
   199 	 */
       
   200 	EAP_FUNC_IMPORT eap_status_e hash_final(
       
   201 		void * const message_digest,
       
   202 		u32_t *md_length_or_null);
       
   203 
       
   204 	/**
       
   205 	 * This function copies the context of SHA_256.
       
   206 	 */
       
   207 	EAP_FUNC_IMPORT eap_am_crypto_sha_256_c * copy();
       
   208 
       
   209 };
       
   210 
       
   211 //--------------------------------------------------
       
   212 
       
   213 #endif //#if !defined( _EAP_AM_CRYPTO_SHA_256_H_ )
       
   214 
       
   215 
       
   216 
       
   217 // End.