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