os/security/crypto/weakcrypto/source/hash/shacommon.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200 (2012-06-15)
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 #ifndef __CRYPTO_SHACOMMON_H_
     2 #define __CRYPTO_SHACOMMON_H_
     3 
     4 #include <e32base.h>/*
     5 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     6 * All rights reserved.
     7 * This component and the accompanying materials are made available
     8 * under the terms of the License "Eclipse Public License v1.0"
     9 * which accompanies this distribution, and is available
    10 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
    11 *
    12 * Initial Contributors:
    13 * Nokia Corporation - initial contribution.
    14 *
    15 * Contributors:
    16 *
    17 * Description: 
    18 * Same as used in SHA1
    19 * SHA_CH		> CSHA1_F
    20 * SHA_Maj		> CSHA1_H
    21 * SHA_Parity	> CSHA1_G
    22 * The following definitions are equivalent and potentially faster.
    23 * #define SHA_Ch(x, y, z)      (((x) & ((y) ^ (z))) ^ (z))
    24 * #define SHA_Maj(x, y, z)     (((x) & ((y) | (z))) | ((y) & (z)))
    25 * These functions are defined in FIPS 180-2 Section 4.1
    26 * Equation 4.1, 4.2, 4.3, 4.8, 4.9
    27 *
    28 */
    29 
    30 
    31 
    32 
    33 /**
    34  @file
    35  @internalComponent
    36  @released
    37 */
    38 template<typename T>
    39 inline T SHA_Ch(T aX, T aY, T aZ)
    40 	{
    41 	return ((aX & aY) ^ ((~aX) & aZ));
    42 	}
    43 
    44 template<typename T>
    45 inline T SHA_Maj(T aX, T aY, T aZ)
    46 	{
    47 	return ((aX & aY) ^ (aX & aZ) ^ (aY & aZ));
    48 	}
    49 
    50 template<typename T>
    51 inline T SHA_Parity(T aX, T aY, T aZ)
    52 	{
    53 	return (aX ^ aY ^ aZ);
    54 	}
    55 
    56 /**
    57  * Define the SHA shift, and rotate right macro 
    58  * Defined in FIPS 180-2 Section 3.2
    59  */
    60 /** 
    61  * SHA Right Shift operation: The right shift operation SHR^n(x), 
    62  * where x is a w-bit word and n is an integer with 0 <= n < w, 
    63  * is defined by  SHR^n(x) = x >> n.
    64  */
    65 template<typename T>
    66 inline T SHA_SHR(T aBits, T aWord)
    67 	{
    68 	return (aWord >> aBits);
    69 	}
    70 
    71 /**
    72  * SHA Rotate Right Operation: The rotate right (circular right shift) operation
    73  * ROTR^n(x), where x is a w-bit word and n is an integer with 0 <= n < w, 
    74  * is defined by ROTR n(x)=(x >> n) || (x << w - n).
    75  */
    76 template<typename T>
    77 inline T SHA_ROTR(T aBits, T aWord)
    78 	{
    79 	TInt totalBits = sizeof(T) << 3;
    80 	return ((aWord >> aBits) | (aWord << (totalBits-aBits)));
    81 	}
    82 
    83 NONSHARABLE_CLASS(MSHA2Impl)
    84 	{
    85 public:
    86 	/**
    87 	 * This function will reset the state of hash.
    88 	 */
    89 	virtual void Reset(const TAny*) = 0;
    90 	/**
    91 	 * This function will finalize the hash and return
    92 	 * the calculated hash.
    93 	 * @return Final hash
    94 	 */ 
    95 	virtual const TDesC8& Final() = 0;
    96 	/**
    97 	 * This function will add the message to the internal
    98 	 * buffer and if the block size is reached then calcualte
    99 	 * the hash till that point.
   100 	 * @param aMessage Message to be updated.
   101 	 * @param aLength Length of the message to be updated.
   102 	 */ 
   103 	virtual void Update(const TUint8* aMessage, TUint aLength) = 0;
   104 	/**
   105 	 * This function will save the internal state of the hash.
   106 	 */ 
   107 	virtual void StoreState() = 0;
   108 	/**
   109 	 * This function will retrieve the saved the internal state 
   110 	 * of the hash.
   111 	 */ 
   112 	virtual void RestoreState() = 0;
   113 	/**
   114 	 * virtual distructor.
   115 	 */
   116 	virtual ~MSHA2Impl(){}
   117 	};
   118 
   119 #endif //__CRYPTO_SHACOMMON_H_