os/security/crypto/weakcrypto/source/hash/shacommon.h
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcrypto/source/hash/shacommon.h	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,119 @@
     1.4 +#ifndef __CRYPTO_SHACOMMON_H_
     1.5 +#define __CRYPTO_SHACOMMON_H_
     1.6 +
     1.7 +#include <e32base.h>/*
     1.8 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.9 +* All rights reserved.
    1.10 +* This component and the accompanying materials are made available
    1.11 +* under the terms of the License "Eclipse Public License v1.0"
    1.12 +* which accompanies this distribution, and is available
    1.13 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.14 +*
    1.15 +* Initial Contributors:
    1.16 +* Nokia Corporation - initial contribution.
    1.17 +*
    1.18 +* Contributors:
    1.19 +*
    1.20 +* Description: 
    1.21 +* Same as used in SHA1
    1.22 +* SHA_CH		> CSHA1_F
    1.23 +* SHA_Maj		> CSHA1_H
    1.24 +* SHA_Parity	> CSHA1_G
    1.25 +* The following definitions are equivalent and potentially faster.
    1.26 +* #define SHA_Ch(x, y, z)      (((x) & ((y) ^ (z))) ^ (z))
    1.27 +* #define SHA_Maj(x, y, z)     (((x) & ((y) | (z))) | ((y) & (z)))
    1.28 +* These functions are defined in FIPS 180-2 Section 4.1
    1.29 +* Equation 4.1, 4.2, 4.3, 4.8, 4.9
    1.30 +*
    1.31 +*/
    1.32 +
    1.33 +
    1.34 +
    1.35 +
    1.36 +/**
    1.37 + @file
    1.38 + @internalComponent
    1.39 + @released
    1.40 +*/
    1.41 +template<typename T>
    1.42 +inline T SHA_Ch(T aX, T aY, T aZ)
    1.43 +	{
    1.44 +	return ((aX & aY) ^ ((~aX) & aZ));
    1.45 +	}
    1.46 +
    1.47 +template<typename T>
    1.48 +inline T SHA_Maj(T aX, T aY, T aZ)
    1.49 +	{
    1.50 +	return ((aX & aY) ^ (aX & aZ) ^ (aY & aZ));
    1.51 +	}
    1.52 +
    1.53 +template<typename T>
    1.54 +inline T SHA_Parity(T aX, T aY, T aZ)
    1.55 +	{
    1.56 +	return (aX ^ aY ^ aZ);
    1.57 +	}
    1.58 +
    1.59 +/**
    1.60 + * Define the SHA shift, and rotate right macro 
    1.61 + * Defined in FIPS 180-2 Section 3.2
    1.62 + */
    1.63 +/** 
    1.64 + * SHA Right Shift operation: The right shift operation SHR^n(x), 
    1.65 + * where x is a w-bit word and n is an integer with 0 <= n < w, 
    1.66 + * is defined by  SHR^n(x) = x >> n.
    1.67 + */
    1.68 +template<typename T>
    1.69 +inline T SHA_SHR(T aBits, T aWord)
    1.70 +	{
    1.71 +	return (aWord >> aBits);
    1.72 +	}
    1.73 +
    1.74 +/**
    1.75 + * SHA Rotate Right Operation: The rotate right (circular right shift) operation
    1.76 + * ROTR^n(x), where x is a w-bit word and n is an integer with 0 <= n < w, 
    1.77 + * is defined by ROTR n(x)=(x >> n) || (x << w - n).
    1.78 + */
    1.79 +template<typename T>
    1.80 +inline T SHA_ROTR(T aBits, T aWord)
    1.81 +	{
    1.82 +	TInt totalBits = sizeof(T) << 3;
    1.83 +	return ((aWord >> aBits) | (aWord << (totalBits-aBits)));
    1.84 +	}
    1.85 +
    1.86 +NONSHARABLE_CLASS(MSHA2Impl)
    1.87 +	{
    1.88 +public:
    1.89 +	/**
    1.90 +	 * This function will reset the state of hash.
    1.91 +	 */
    1.92 +	virtual void Reset(const TAny*) = 0;
    1.93 +	/**
    1.94 +	 * This function will finalize the hash and return
    1.95 +	 * the calculated hash.
    1.96 +	 * @return Final hash
    1.97 +	 */ 
    1.98 +	virtual const TDesC8& Final() = 0;
    1.99 +	/**
   1.100 +	 * This function will add the message to the internal
   1.101 +	 * buffer and if the block size is reached then calcualte
   1.102 +	 * the hash till that point.
   1.103 +	 * @param aMessage Message to be updated.
   1.104 +	 * @param aLength Length of the message to be updated.
   1.105 +	 */ 
   1.106 +	virtual void Update(const TUint8* aMessage, TUint aLength) = 0;
   1.107 +	/**
   1.108 +	 * This function will save the internal state of the hash.
   1.109 +	 */ 
   1.110 +	virtual void StoreState() = 0;
   1.111 +	/**
   1.112 +	 * This function will retrieve the saved the internal state 
   1.113 +	 * of the hash.
   1.114 +	 */ 
   1.115 +	virtual void RestoreState() = 0;
   1.116 +	/**
   1.117 +	 * virtual distructor.
   1.118 +	 */
   1.119 +	virtual ~MSHA2Impl(){}
   1.120 +	};
   1.121 +
   1.122 +#endif //__CRYPTO_SHACOMMON_H_