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