os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/sha384and512impl.cpp
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
/*
sl@0
     2
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* Common implementation of SHA384 and SHA512
sl@0
    16
* RFC 4634 (US Secure Hash Algorithms (SHA and HMAC-SHA))
sl@0
    17
*
sl@0
    18
*/
sl@0
    19
sl@0
    20
sl@0
    21
/**
sl@0
    22
 @file
sl@0
    23
*/
sl@0
    24
sl@0
    25
#include <cryptospi/hashplugin.h>
sl@0
    26
#include "sha384and512impl.h"
sl@0
    27
#include "pluginconfig.h"
sl@0
    28
sl@0
    29
/**
sl@0
    30
 * SHA512 Constants
sl@0
    31
 * 
sl@0
    32
 * SHA-512 uses a sequence of eighty constant 64-bit words. 
sl@0
    33
 * These words represent the first sixty-four bits of the fractional 
sl@0
    34
 * parts of the cube roots of the first eighty prime numbers.
sl@0
    35
 * 
sl@0
    36
 * FIPS 180-2 Section 4.2.3
sl@0
    37
 */
sl@0
    38
const TUint64 K[80] = 
sl@0
    39
	{
sl@0
    40
	UI64LIT(0x428a2f98d728ae22), UI64LIT(0x7137449123ef65cd), UI64LIT(0xb5c0fbcfec4d3b2f), UI64LIT(0xe9b5dba58189dbbc),
sl@0
    41
	UI64LIT(0x3956c25bf348b538), UI64LIT(0x59f111f1b605d019), UI64LIT(0x923f82a4af194f9b), UI64LIT(0xab1c5ed5da6d8118),
sl@0
    42
	UI64LIT(0xd807aa98a3030242), UI64LIT(0x12835b0145706fbe), UI64LIT(0x243185be4ee4b28c), UI64LIT(0x550c7dc3d5ffb4e2),
sl@0
    43
	UI64LIT(0x72be5d74f27b896f), UI64LIT(0x80deb1fe3b1696b1), UI64LIT(0x9bdc06a725c71235), UI64LIT(0xc19bf174cf692694),
sl@0
    44
	UI64LIT(0xe49b69c19ef14ad2), UI64LIT(0xefbe4786384f25e3), UI64LIT(0x0fc19dc68b8cd5b5), UI64LIT(0x240ca1cc77ac9c65),
sl@0
    45
	UI64LIT(0x2de92c6f592b0275), UI64LIT(0x4a7484aa6ea6e483), UI64LIT(0x5cb0a9dcbd41fbd4), UI64LIT(0x76f988da831153b5),
sl@0
    46
	UI64LIT(0x983e5152ee66dfab), UI64LIT(0xa831c66d2db43210), UI64LIT(0xb00327c898fb213f), UI64LIT(0xbf597fc7beef0ee4),
sl@0
    47
	UI64LIT(0xc6e00bf33da88fc2), UI64LIT(0xd5a79147930aa725), UI64LIT(0x06ca6351e003826f), UI64LIT(0x142929670a0e6e70),
sl@0
    48
	UI64LIT(0x27b70a8546d22ffc), UI64LIT(0x2e1b21385c26c926), UI64LIT(0x4d2c6dfc5ac42aed), UI64LIT(0x53380d139d95b3df),
sl@0
    49
	UI64LIT(0x650a73548baf63de), UI64LIT(0x766a0abb3c77b2a8), UI64LIT(0x81c2c92e47edaee6), UI64LIT(0x92722c851482353b),
sl@0
    50
	UI64LIT(0xa2bfe8a14cf10364), UI64LIT(0xa81a664bbc423001), UI64LIT(0xc24b8b70d0f89791), UI64LIT(0xc76c51a30654be30),
sl@0
    51
	UI64LIT(0xd192e819d6ef5218), UI64LIT(0xd69906245565a910), UI64LIT(0xf40e35855771202a), UI64LIT(0x106aa07032bbd1b8),
sl@0
    52
	UI64LIT(0x19a4c116b8d2d0c8), UI64LIT(0x1e376c085141ab53), UI64LIT(0x2748774cdf8eeb99), UI64LIT(0x34b0bcb5e19b48a8),
sl@0
    53
	UI64LIT(0x391c0cb3c5c95a63), UI64LIT(0x4ed8aa4ae3418acb), UI64LIT(0x5b9cca4f7763e373), UI64LIT(0x682e6ff3d6b2b8a3),
sl@0
    54
	UI64LIT(0x748f82ee5defb2fc), UI64LIT(0x78a5636f43172f60), UI64LIT(0x84c87814a1f0ab72), UI64LIT(0x8cc702081a6439ec),
sl@0
    55
	UI64LIT(0x90befffa23631e28), UI64LIT(0xa4506cebde82bde9), UI64LIT(0xbef9a3f7b2c67915), UI64LIT(0xc67178f2e372532b),
sl@0
    56
	UI64LIT(0xca273eceea26619c), UI64LIT(0xd186b8c721c0c207), UI64LIT(0xeada7dd6cde0eb1e), UI64LIT(0xf57d4f7fee6ed178),
sl@0
    57
	UI64LIT(0x06f067aa72176fba), UI64LIT(0x0a637dc5a2c898a6), UI64LIT(0x113f9804bef90dae), UI64LIT(0x1b710b35131c471b),
sl@0
    58
	UI64LIT(0x28db77f523047d84), UI64LIT(0x32caab7b40c72493), UI64LIT(0x3c9ebe0a15c9bebc), UI64LIT(0x431d67c49c100d4c),
sl@0
    59
	UI64LIT(0x4cc5d4becb3e42b6), UI64LIT(0x597f299cfc657e2a), UI64LIT(0x5fcb6fab3ad6faec), UI64LIT(0x6c44198c4a475817)
sl@0
    60
	};
sl@0
    61
sl@0
    62
/**
sl@0
    63
 * Define the SHA SIGMA and sigma macros 
sl@0
    64
 * 
sl@0
    65
 * FIPS 180-2 section 4.1.3
sl@0
    66
 */
sl@0
    67
// Equation 4.10
sl@0
    68
inline TUint64 SHA512_SIGMA0(TUint64 aWord)
sl@0
    69
	{
sl@0
    70
	return (SHA_ROTR<TUint64>(28,aWord) ^ SHA_ROTR<TUint64>(34,aWord) ^ SHA_ROTR<TUint64>(39,aWord));
sl@0
    71
	}
sl@0
    72
// Equation 4.11
sl@0
    73
inline TUint64 SHA512_SIGMA1(TUint64 aWord)
sl@0
    74
	{
sl@0
    75
	return (SHA_ROTR<TUint64>(14,aWord) ^ SHA_ROTR<TUint64>(18,aWord) ^ SHA_ROTR<TUint64>(41,aWord));
sl@0
    76
	}
sl@0
    77
// Equation 4.12
sl@0
    78
inline TUint64 SHA512_sigma0(TUint64 aWord)
sl@0
    79
	{
sl@0
    80
	return (SHA_ROTR<TUint64>(1, aWord) ^ SHA_ROTR<TUint64>(8, aWord) ^ SHA_SHR<TUint64>(7, aWord));
sl@0
    81
	}
sl@0
    82
// Equation 4.13
sl@0
    83
inline TUint64 SHA512_sigma1(TUint64 aWord)
sl@0
    84
	{
sl@0
    85
	return (SHA_ROTR<TUint64>(19,aWord) ^ SHA_ROTR<TUint64>(61,aWord) ^ SHA_SHR<TUint64>(6,aWord));
sl@0
    86
	}
sl@0
    87
sl@0
    88
// Macros
sl@0
    89
inline TUint64 Make64BitWord(const TUint8* aData)
sl@0
    90
	{
sl@0
    91
	TUint64 result = (TUint64)aData[0] << 56 | (TUint64)aData[1] << 48 | (TUint64)aData[2] << 40 | 
sl@0
    92
					 (TUint64)aData[3] << 32 | (TUint64)aData[4] << 24 | (TUint64)aData[5] << 16 |
sl@0
    93
					 (TUint64)aData[6] << 8 | (TUint64)aData[7];
sl@0
    94
	return result;
sl@0
    95
	}
sl@0
    96
sl@0
    97
sl@0
    98
using namespace SoftwareCrypto;
sl@0
    99
	
sl@0
   100
CSHA384And512Impl* CSHA384And512Impl::NewL(TInt aHashSize)
sl@0
   101
	{
sl@0
   102
	CSHA384And512Impl* self=new (ELeave) CSHA384And512Impl(aHashSize);
sl@0
   103
	return self;						
sl@0
   104
	}
sl@0
   105
														
sl@0
   106
CSHA384And512Impl::CSHA384And512Impl(TInt aHashSize) : iHash(aHashSize)
sl@0
   107
	{		
sl@0
   108
	}
sl@0
   109
	
sl@0
   110
CSHA384And512Impl::CSHA384And512Impl(const CSHA384And512Impl& aSHA512Impl)
sl@0
   111
				: 	iHash(aSHA512Impl.iHash),
sl@0
   112
					iA(aSHA512Impl.iA),
sl@0
   113
					iB(aSHA512Impl.iB),
sl@0
   114
					iC(aSHA512Impl.iC),
sl@0
   115
					iD(aSHA512Impl.iD),
sl@0
   116
					iE(aSHA512Impl.iE),
sl@0
   117
					iF(aSHA512Impl.iF),
sl@0
   118
					iG(aSHA512Impl.iG),
sl@0
   119
					iH(aSHA512Impl.iH),
sl@0
   120
					iNl(aSHA512Impl.iNl),
sl@0
   121
					iNh(aSHA512Impl.iNh),
sl@0
   122
					iBlockIndex(aSHA512Impl.iBlockIndex)
sl@0
   123
	{
sl@0
   124
	Mem::Copy(iData, aSHA512Impl.iData, KSHA512BlockSize*sizeof(TUint64));
sl@0
   125
	}
sl@0
   126
	
sl@0
   127
void CSHA384And512Impl::Reset(const TAny* aValueArr)
sl@0
   128
	{
sl@0
   129
	const TUint64* values = static_cast<const TUint64*>(aValueArr);
sl@0
   130
	/**
sl@0
   131
	 * Initial Hash Value
sl@0
   132
	 * 
sl@0
   133
	 * These words were obtained by taking the first thirty-two bits 
sl@0
   134
	 * of the fractional parts of the square roots of the first eight
sl@0
   135
	 * prime numbers.
sl@0
   136
	 * 
sl@0
   137
	 * FIPS 180-2 Section 5.3.2
sl@0
   138
	 */
sl@0
   139
	iA = values[0];
sl@0
   140
	iB = values[1];
sl@0
   141
	iC = values[2];
sl@0
   142
	iD = values[3];
sl@0
   143
	iE = values[4];
sl@0
   144
	iF = values[5];
sl@0
   145
	iG = values[6];
sl@0
   146
	iH = values[7];
sl@0
   147
	iNh = 0;
sl@0
   148
	iNl = 0;
sl@0
   149
	iBlockIndex = 0;
sl@0
   150
	}
sl@0
   151
sl@0
   152
//This function will panic if the total input length is longer than 2^128 in bits
sl@0
   153
_LIT(KPanicString, "Message length exceeds supported length");
sl@0
   154
inline void CSHA384And512Impl::AddLength(const TUint64 aLength)
sl@0
   155
	{
sl@0
   156
	TUint64 temp = iNl;
sl@0
   157
	iNl += aLength << 3;
sl@0
   158
	__ASSERT_ALWAYS(((iNh != KMaxTUint64) || (temp <= iNl)), User::Panic(KPanicString, KErrOverflow));
sl@0
   159
	iNh += (temp > iNl);
sl@0
   160
	}
sl@0
   161
sl@0
   162
// This assumes a big-endian architecture
sl@0
   163
void CSHA384And512Impl::Update(const TUint8* aData,TUint aLength)
sl@0
   164
	{
sl@0
   165
	while((aLength / 8) > 0 && (iBlockIndex % 8 == 0))
sl@0
   166
		{
sl@0
   167
		iData[iBlockIndex>>3] = Make64BitWord(aData);
sl@0
   168
		iBlockIndex+=8;
sl@0
   169
		aData+=8;
sl@0
   170
		aLength-=8;
sl@0
   171
		if(iBlockIndex==KSHA512BlockSize) 
sl@0
   172
			{
sl@0
   173
			Block();
sl@0
   174
			AddLength(KSHA512BlockSize);
sl@0
   175
			}
sl@0
   176
		}
sl@0
   177
sl@0
   178
	while(aLength--)
sl@0
   179
		{
sl@0
   180
		if(!(iBlockIndex&0x07))
sl@0
   181
			{
sl@0
   182
			iData[iBlockIndex >> 3] = 0;
sl@0
   183
			}
sl@0
   184
		iData[iBlockIndex >> 3] |= static_cast<TUint64>(*aData) << ((7 - iBlockIndex&0x07) << 3) ;
sl@0
   185
		++aData;
sl@0
   186
		++iBlockIndex;
sl@0
   187
		if(iBlockIndex==KSHA512BlockSize) 
sl@0
   188
			{
sl@0
   189
			Block();
sl@0
   190
			AddLength(KSHA512BlockSize);
sl@0
   191
			}
sl@0
   192
		}
sl@0
   193
	}
sl@0
   194
sl@0
   195
static inline void CSHA512_16(	const TUint64 aA, 
sl@0
   196
								const TUint64 aB, 
sl@0
   197
								const TUint64 aC,
sl@0
   198
								TUint64& aD, 
sl@0
   199
								const TUint64 aE, 
sl@0
   200
								const TUint64 aF,
sl@0
   201
								const TUint64 aG, 
sl@0
   202
								TUint64& aH,
sl@0
   203
								TUint64 aTemp1,
sl@0
   204
								TUint64 aTemp2,
sl@0
   205
								const TUint64 aK,
sl@0
   206
								const TUint64 aWord)
sl@0
   207
	{
sl@0
   208
	aTemp1 = aH + SHA512_SIGMA1(aE) + SHA_Ch(aE,aF,aG) + aK + aWord;
sl@0
   209
	aTemp2 = SHA512_SIGMA0(aA) + SHA_Maj(aA,aB,aC);
sl@0
   210
	aD = aD + aTemp1;
sl@0
   211
	aH = aTemp1 + aTemp2;
sl@0
   212
	}
sl@0
   213
sl@0
   214
static inline void CSHA512_64(	const TUint64 aA, 
sl@0
   215
								const TUint64 aB, 
sl@0
   216
								const TUint64 aC,
sl@0
   217
								TUint64& aD, 
sl@0
   218
								const TUint64 aE, 
sl@0
   219
								const TUint64 aF,
sl@0
   220
								const TUint64 aG, 
sl@0
   221
								TUint64& aH,
sl@0
   222
								TUint64 aTemp1,
sl@0
   223
								TUint64 aTemp2,
sl@0
   224
								const TUint64 aK,
sl@0
   225
								TUint64& aWord0,
sl@0
   226
								const TUint64 aWord2,
sl@0
   227
								const TUint64 aWord7,
sl@0
   228
								const TUint64 aWord15,
sl@0
   229
								const TUint64 aWord16)
sl@0
   230
	{
sl@0
   231
	aWord0 = SHA512_sigma1(aWord2) + aWord7 + SHA512_sigma0(aWord15) + aWord16;
sl@0
   232
	CSHA512_16(aA, aB, aC, aD, aE, aF, aG, aH, aTemp1, aTemp2, aK, aWord0);
sl@0
   233
	}
sl@0
   234
sl@0
   235
/**
sl@0
   236
 * This function actually calculates the hash.
sl@0
   237
 * Function is defined in FIPS 180-2 section 6.3.2
sl@0
   238
 * 
sl@0
   239
 * This function is the expanded version of the following loop.
sl@0
   240
 *	for(TUint i = 0; i < 80; ++i)
sl@0
   241
 *		{
sl@0
   242
 *		if(i >= 16)
sl@0
   243
 *			{
sl@0
   244
 *			iData[i] = SHA512_sigma1(iData[i-2]) + iData[i-7] + SHA512_sigma0(iData[i-15]) + iData[i-16];
sl@0
   245
 *			}
sl@0
   246
 *
sl@0
   247
 *		temp1 = tempH + SHA512_SIGMA1(tempE) + SHA_Ch(tempE,tempF,tempG) + K[i] + iData[i];
sl@0
   248
 *	    temp2 = SHA512_SIGMA0(tempA) + SHA_Maj(tempA,tempB,tempC);
sl@0
   249
 *	    tempH = tempG;
sl@0
   250
 *	    tempG = tempF;
sl@0
   251
 *	    tempF = tempE;
sl@0
   252
 *	    tempE = tempD + temp1;
sl@0
   253
 *	    tempD = tempC;
sl@0
   254
 *	    tempC = tempB;
sl@0
   255
 *	    tempB = tempA;
sl@0
   256
 *	    tempA = temp1 + temp2;		
sl@0
   257
 *		}
sl@0
   258
 */
sl@0
   259
void CSHA384And512Impl::Block()
sl@0
   260
	{
sl@0
   261
	TUint64 tempA=iA;
sl@0
   262
	TUint64 tempB=iB;
sl@0
   263
	TUint64 tempC=iC;
sl@0
   264
	TUint64 tempD=iD;
sl@0
   265
	TUint64 tempE=iE;
sl@0
   266
	TUint64 tempF=iF;
sl@0
   267
	TUint64 tempG=iG;
sl@0
   268
	TUint64 tempH=iH;
sl@0
   269
	TUint64 temp1=0;
sl@0
   270
	TUint64 temp2=0;
sl@0
   271
	
sl@0
   272
	CSHA512_16(tempA,tempB,tempC,tempD,tempE,tempF,tempG,tempH,temp1,temp2,K[0],iData[0]);
sl@0
   273
	CSHA512_16(tempH,tempA,tempB,tempC,tempD,tempE,tempF,tempG,temp1,temp2,K[1],iData[1]);
sl@0
   274
	CSHA512_16(tempG,tempH,tempA,tempB,tempC,tempD,tempE,tempF,temp1,temp2,K[2],iData[2]);
sl@0
   275
	CSHA512_16(tempF,tempG,tempH,tempA,tempB,tempC,tempD,tempE,temp1,temp2,K[3],iData[3]);
sl@0
   276
	CSHA512_16(tempE,tempF,tempG,tempH,tempA,tempB,tempC,tempD,temp1,temp2,K[4],iData[4]);
sl@0
   277
	CSHA512_16(tempD,tempE,tempF,tempG,tempH,tempA,tempB,tempC,temp1,temp2,K[5],iData[5]);
sl@0
   278
	CSHA512_16(tempC,tempD,tempE,tempF,tempG,tempH,tempA,tempB,temp1,temp2,K[6],iData[6]);
sl@0
   279
	CSHA512_16(tempB,tempC,tempD,tempE,tempF,tempG,tempH,tempA,temp1,temp2,K[7],iData[7]);
sl@0
   280
sl@0
   281
	CSHA512_16(tempA,tempB,tempC,tempD,tempE,tempF,tempG,tempH,temp1,temp2,K[8],iData[8]);
sl@0
   282
	CSHA512_16(tempH,tempA,tempB,tempC,tempD,tempE,tempF,tempG,temp1,temp2,K[9],iData[9]);
sl@0
   283
	CSHA512_16(tempG,tempH,tempA,tempB,tempC,tempD,tempE,tempF,temp1,temp2,K[10],iData[10]);
sl@0
   284
	CSHA512_16(tempF,tempG,tempH,tempA,tempB,tempC,tempD,tempE,temp1,temp2,K[11],iData[11]);
sl@0
   285
	CSHA512_16(tempE,tempF,tempG,tempH,tempA,tempB,tempC,tempD,temp1,temp2,K[12],iData[12]);
sl@0
   286
	CSHA512_16(tempD,tempE,tempF,tempG,tempH,tempA,tempB,tempC,temp1,temp2,K[13],iData[13]);
sl@0
   287
	CSHA512_16(tempC,tempD,tempE,tempF,tempG,tempH,tempA,tempB,temp1,temp2,K[14],iData[14]);
sl@0
   288
	CSHA512_16(tempB,tempC,tempD,tempE,tempF,tempG,tempH,tempA,temp1,temp2,K[15],iData[15]);
sl@0
   289
sl@0
   290
	CSHA512_64(	tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2,
sl@0
   291
				K[16], iData[16], iData[14], iData[9], iData[1], iData[0]);
sl@0
   292
	CSHA512_64(	tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2,
sl@0
   293
				K[17], iData[17], iData[15], iData[10], iData[2], iData[1]);
sl@0
   294
	CSHA512_64(	tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2,
sl@0
   295
				K[18], iData[18], iData[16], iData[11], iData[3], iData[2]);
sl@0
   296
	CSHA512_64(	tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2,
sl@0
   297
				K[19], iData[19], iData[17], iData[12], iData[4], iData[3]);
sl@0
   298
	CSHA512_64(	tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2,
sl@0
   299
				K[20], iData[20], iData[18], iData[13], iData[5], iData[4]);
sl@0
   300
	CSHA512_64(	tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2,
sl@0
   301
				K[21], iData[21], iData[19], iData[14], iData[6], iData[5]);
sl@0
   302
	CSHA512_64(	tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2,
sl@0
   303
				K[22], iData[22], iData[20], iData[15], iData[7], iData[6]);
sl@0
   304
	CSHA512_64(	tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2,
sl@0
   305
				K[23], iData[23], iData[21], iData[16], iData[8], iData[7]);
sl@0
   306
sl@0
   307
	CSHA512_64(	tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2,
sl@0
   308
				K[24], iData[24], iData[22], iData[17], iData[9], iData[8]);
sl@0
   309
	CSHA512_64(	tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2,
sl@0
   310
				K[25], iData[25], iData[23], iData[18], iData[10], iData[9]);
sl@0
   311
	CSHA512_64(	tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2,
sl@0
   312
				K[26], iData[26], iData[24], iData[19], iData[11], iData[10]);
sl@0
   313
	CSHA512_64(	tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2,
sl@0
   314
				K[27], iData[27], iData[25], iData[20], iData[12], iData[11]);
sl@0
   315
	CSHA512_64(	tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2,
sl@0
   316
				K[28], iData[28], iData[26], iData[21], iData[13], iData[12]);
sl@0
   317
	CSHA512_64(	tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2,
sl@0
   318
				K[29], iData[29], iData[27], iData[22], iData[14], iData[13]);
sl@0
   319
	CSHA512_64(	tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2,
sl@0
   320
				K[30], iData[30], iData[28], iData[23], iData[15], iData[14]);
sl@0
   321
	CSHA512_64(	tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2,
sl@0
   322
				K[31], iData[31], iData[29], iData[24], iData[16], iData[15]);
sl@0
   323
sl@0
   324
	CSHA512_64(	tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2,
sl@0
   325
				K[32], iData[32], iData[30], iData[25], iData[17], iData[16]);
sl@0
   326
	CSHA512_64(	tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2,
sl@0
   327
				K[33], iData[33], iData[31], iData[26], iData[18], iData[17]);
sl@0
   328
	CSHA512_64(	tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2,
sl@0
   329
				K[34], iData[34], iData[32], iData[27], iData[19], iData[18]);
sl@0
   330
	CSHA512_64(	tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2,
sl@0
   331
				K[35], iData[35], iData[33], iData[28], iData[20], iData[19]);
sl@0
   332
	CSHA512_64(	tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2,
sl@0
   333
				K[36], iData[36], iData[34], iData[29], iData[21], iData[20]);
sl@0
   334
	CSHA512_64(	tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2,
sl@0
   335
				K[37], iData[37], iData[35], iData[30], iData[22], iData[21]);
sl@0
   336
	CSHA512_64(	tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2,
sl@0
   337
				K[38], iData[38], iData[36], iData[31], iData[23], iData[22]);
sl@0
   338
	CSHA512_64(	tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2,
sl@0
   339
				K[39], iData[39], iData[37], iData[32], iData[24], iData[23]);
sl@0
   340
sl@0
   341
	CSHA512_64(	tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2,
sl@0
   342
				K[40], iData[40], iData[38], iData[33], iData[25], iData[24]);
sl@0
   343
	CSHA512_64(	tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2,
sl@0
   344
				K[41], iData[41], iData[39], iData[34], iData[26], iData[25]);
sl@0
   345
	CSHA512_64(	tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2,
sl@0
   346
				K[42], iData[42], iData[40], iData[35], iData[27], iData[26]);
sl@0
   347
	CSHA512_64(	tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2,
sl@0
   348
				K[43], iData[43], iData[41], iData[36], iData[28], iData[27]);
sl@0
   349
	CSHA512_64(	tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2,
sl@0
   350
				K[44], iData[44], iData[42], iData[37], iData[29], iData[28]);
sl@0
   351
	CSHA512_64(	tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2,
sl@0
   352
				K[45], iData[45], iData[43], iData[38], iData[30], iData[29]);
sl@0
   353
	CSHA512_64(	tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2,
sl@0
   354
				K[46], iData[46], iData[44], iData[39], iData[31], iData[30]);
sl@0
   355
	CSHA512_64(	tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2,
sl@0
   356
				K[47], iData[47], iData[45], iData[40], iData[32], iData[31]);
sl@0
   357
sl@0
   358
	CSHA512_64(	tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2,
sl@0
   359
				K[48], iData[48], iData[46], iData[41], iData[33], iData[32]);
sl@0
   360
	CSHA512_64(	tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2,
sl@0
   361
				K[49], iData[49], iData[47], iData[42], iData[34], iData[33]);
sl@0
   362
	CSHA512_64(	tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2,
sl@0
   363
				K[50], iData[50], iData[48], iData[43], iData[35], iData[34]);
sl@0
   364
	CSHA512_64(	tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2,
sl@0
   365
				K[51], iData[51], iData[49], iData[44], iData[36], iData[35]);
sl@0
   366
	CSHA512_64(	tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2,
sl@0
   367
				K[52], iData[52], iData[50], iData[45], iData[37], iData[36]);
sl@0
   368
	CSHA512_64(	tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2,
sl@0
   369
				K[53], iData[53], iData[51], iData[46], iData[38], iData[37]);
sl@0
   370
	CSHA512_64(	tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2,
sl@0
   371
				K[54], iData[54], iData[52], iData[47], iData[39], iData[38]);
sl@0
   372
	CSHA512_64(	tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2,
sl@0
   373
				K[55], iData[55], iData[53], iData[48], iData[40], iData[39]);
sl@0
   374
sl@0
   375
	CSHA512_64(	tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2,
sl@0
   376
				K[56], iData[56], iData[54], iData[49], iData[41], iData[40]);
sl@0
   377
	CSHA512_64(	tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2,
sl@0
   378
				K[57], iData[57], iData[55], iData[50], iData[42], iData[41]);
sl@0
   379
	CSHA512_64(	tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2,
sl@0
   380
				K[58], iData[58], iData[56], iData[51], iData[43], iData[42]);
sl@0
   381
	CSHA512_64(	tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2,
sl@0
   382
				K[59], iData[59], iData[57], iData[52], iData[44], iData[43]);
sl@0
   383
	CSHA512_64(	tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2,
sl@0
   384
				K[60], iData[60], iData[58], iData[53], iData[45], iData[44]);
sl@0
   385
	CSHA512_64(	tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2,
sl@0
   386
				K[61], iData[61], iData[59], iData[54], iData[46], iData[45]);
sl@0
   387
	CSHA512_64(	tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2,
sl@0
   388
				K[62], iData[62], iData[60], iData[55], iData[47], iData[46]);
sl@0
   389
	CSHA512_64(	tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2,
sl@0
   390
				K[63], iData[63], iData[61], iData[56], iData[48], iData[47]);
sl@0
   391
sl@0
   392
	CSHA512_64(	tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2,
sl@0
   393
				K[64], iData[64], iData[62], iData[57], iData[49], iData[48]);
sl@0
   394
	CSHA512_64(	tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2,
sl@0
   395
				K[65], iData[65], iData[63], iData[58], iData[50], iData[49]);
sl@0
   396
	CSHA512_64(	tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2,
sl@0
   397
				K[66], iData[66], iData[64], iData[59], iData[51], iData[50]);
sl@0
   398
	CSHA512_64(	tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2,
sl@0
   399
				K[67], iData[67], iData[65], iData[60], iData[52], iData[51]);
sl@0
   400
	CSHA512_64(	tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2,
sl@0
   401
				K[68], iData[68], iData[66], iData[61], iData[53], iData[52]);
sl@0
   402
	CSHA512_64(	tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2,
sl@0
   403
				K[69], iData[69], iData[67], iData[62], iData[54], iData[53]);
sl@0
   404
	CSHA512_64(	tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2,
sl@0
   405
				K[70], iData[70], iData[68], iData[63], iData[55], iData[54]);
sl@0
   406
	CSHA512_64(	tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2,
sl@0
   407
				K[71], iData[71], iData[69], iData[64], iData[56], iData[55]);
sl@0
   408
sl@0
   409
	CSHA512_64(	tempA, tempB, tempC, tempD, tempE, tempF, tempG, tempH, temp1, temp2,
sl@0
   410
				K[72], iData[72], iData[70], iData[65], iData[57], iData[56]);
sl@0
   411
	CSHA512_64(	tempH, tempA, tempB, tempC, tempD, tempE, tempF, tempG, temp1, temp2,
sl@0
   412
				K[73], iData[73], iData[71], iData[66], iData[58], iData[57]);
sl@0
   413
	CSHA512_64(	tempG, tempH, tempA, tempB, tempC, tempD, tempE, tempF, temp1, temp2,
sl@0
   414
				K[74], iData[74], iData[72], iData[67], iData[59], iData[58]);
sl@0
   415
	CSHA512_64(	tempF, tempG, tempH, tempA, tempB, tempC, tempD, tempE, temp1, temp2,
sl@0
   416
				K[75], iData[75], iData[73], iData[68], iData[60], iData[59]);
sl@0
   417
	CSHA512_64(	tempE, tempF, tempG, tempH, tempA, tempB, tempC, tempD, temp1, temp2,
sl@0
   418
				K[76], iData[76], iData[74], iData[69], iData[61], iData[60]);
sl@0
   419
	CSHA512_64(	tempD, tempE, tempF, tempG, tempH, tempA, tempB, tempC, temp1, temp2,
sl@0
   420
				K[77], iData[77], iData[75], iData[70], iData[62], iData[61]);
sl@0
   421
	CSHA512_64(	tempC, tempD, tempE, tempF, tempG, tempH, tempA, tempB, temp1, temp2,
sl@0
   422
				K[78], iData[78], iData[76], iData[71], iData[63], iData[62]);
sl@0
   423
	CSHA512_64(	tempB, tempC, tempD, tempE, tempF, tempG, tempH, tempA, temp1, temp2,
sl@0
   424
				K[79], iData[79], iData[77], iData[72], iData[64], iData[63]);
sl@0
   425
sl@0
   426
	iA+=tempA;
sl@0
   427
	iB+=tempB;
sl@0
   428
	iC+=tempC;
sl@0
   429
	iD+=tempD;
sl@0
   430
	iE+=tempE;
sl@0
   431
	iF+=tempF;
sl@0
   432
	iG+=tempG;
sl@0
   433
	iH+=tempH;
sl@0
   434
sl@0
   435
	iBlockIndex=0;
sl@0
   436
	}
sl@0
   437
sl@0
   438
/**
sl@0
   439
 * According to the standard, the message must be padded to an
sl@0
   440
 * even 512 bits. The first padding bit must be a '1'. The last
sl@0
   441
 * 64 bits represent the length of the original message. All bits 
sl@0
   442
 * in between should be 0. This helper function will pad the 
sl@0
   443
 * message according to those rules by filling the iData array 
sl@0
   444
 * accordingly. 
sl@0
   445
 */ 
sl@0
   446
void CSHA384And512Impl::PadMessage()
sl@0
   447
	{
sl@0
   448
	const TUint64 padByte = 0x80;
sl@0
   449
	
sl@0
   450
	if(!(iBlockIndex&0x07))
sl@0
   451
		{
sl@0
   452
		iData[iBlockIndex >> 3] = 0;
sl@0
   453
		}
sl@0
   454
	iData[iBlockIndex >> 3] |= padByte << ((7 - iBlockIndex&0x07) << 3) ;
sl@0
   455
	
sl@0
   456
	if (iBlockIndex >= (KSHA512BlockSize - 2*sizeof(TUint64))) 
sl@0
   457
		{
sl@0
   458
		if (iBlockIndex < (KSHA512BlockSize - sizeof(TUint64)))
sl@0
   459
			iData[(KSHA512BlockSize>>3)-1]=0;		
sl@0
   460
		Block();
sl@0
   461
		Mem::FillZ(iData,KSHA512BlockSize);
sl@0
   462
		} 
sl@0
   463
	else
sl@0
   464
		{
sl@0
   465
		const TUint offset=(iBlockIndex+8)>>3; //+8 to account for the word added in the
sl@0
   466
		//switch statement above
sl@0
   467
		Mem::FillZ(iData+offset, (KSHA512BlockSize - offset*sizeof(TUint64)));
sl@0
   468
		}
sl@0
   469
sl@0
   470
	iData[(KSHA512BlockSize >> 3) - 2] = iNh;
sl@0
   471
	iData[(KSHA512BlockSize >> 3) - 1] = iNl;	
sl@0
   472
	}
sl@0
   473
sl@0
   474
inline void CSHA384And512Impl::CopyWordToHash(TUint64 aVal, TUint aIndex)
sl@0
   475
	{
sl@0
   476
	TUint64 value = Make64BitWord(reinterpret_cast<TUint8*>(&aVal));
sl@0
   477
	Mem::Copy(const_cast<TUint8*>(iHash.Ptr())+ (8*aIndex), &value, sizeof(aVal));
sl@0
   478
	}
sl@0
   479
sl@0
   480
const TDesC8& CSHA384And512Impl::Final()
sl@0
   481
	{
sl@0
   482
	AddLength(iBlockIndex);
sl@0
   483
	PadMessage();
sl@0
   484
	Block();
sl@0
   485
	//
sl@0
   486
	// Generate hash value into iHash
sl@0
   487
	//
sl@0
   488
	CopyWordToHash(iA, 0);
sl@0
   489
	CopyWordToHash(iB, 1);
sl@0
   490
	CopyWordToHash(iC, 2);
sl@0
   491
	CopyWordToHash(iD, 3);
sl@0
   492
	CopyWordToHash(iE, 4);
sl@0
   493
	CopyWordToHash(iF, 5);
sl@0
   494
	CopyWordToHash(iG, 6);
sl@0
   495
	CopyWordToHash(iH, 7);
sl@0
   496
	
sl@0
   497
	return iHash;
sl@0
   498
	}
sl@0
   499
sl@0
   500
void CSHA384And512Impl::RestoreState()
sl@0
   501
	{
sl@0
   502
	iA = iACopy;
sl@0
   503
	iB = iBCopy;
sl@0
   504
	iC = iCCopy;
sl@0
   505
	iD = iDCopy;
sl@0
   506
	iE = iECopy;
sl@0
   507
	iF = iFCopy;
sl@0
   508
	iG = iGCopy;
sl@0
   509
	iH = iHCopy;
sl@0
   510
	iNl = iNlCopy;
sl@0
   511
	iNh = iNhCopy;	
sl@0
   512
	iBlockIndex = iBlockIndexCopy;
sl@0
   513
	Mem::Copy((TAny*)iData, (TAny*)iDataCopy, KSHA512BlockSize*sizeof(TUint64)); 
sl@0
   514
	}
sl@0
   515
sl@0
   516
void CSHA384And512Impl::StoreState()
sl@0
   517
	{
sl@0
   518
	iACopy = iA;
sl@0
   519
	iBCopy = iB;
sl@0
   520
	iCCopy = iC;
sl@0
   521
	iDCopy = iD;
sl@0
   522
	iECopy = iE;
sl@0
   523
	iFCopy = iF;
sl@0
   524
	iGCopy = iG;
sl@0
   525
	iHCopy = iH;
sl@0
   526
	iNlCopy = iNl;
sl@0
   527
	iNhCopy = iNh;	
sl@0
   528
	iBlockIndexCopy = iBlockIndex;
sl@0
   529
	Mem::Copy((TAny*)iDataCopy, (TAny*)iData, KSHA512BlockSize*sizeof(TUint64));
sl@0
   530
	}
sl@0
   531
sl@0
   532
// Implemented in hmacimpl.cpp or softwarehashbase.cpp
sl@0
   533
// but required as derived from MHash. No coverage here.
sl@0
   534
#ifdef _BullseyeCoverage
sl@0
   535
#pragma suppress_warnings on
sl@0
   536
#pragma BullseyeCoverage off
sl@0
   537
#pragma suppress_warnings off
sl@0
   538
#endif
sl@0
   539