crypto/weakcrypto/source/hash/md4.cpp
changeset 71 dd83586b62d6
equal deleted inserted replaced
66:8873e6835f7b 71:dd83586b62d6
       
     1 /*
       
     2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 /**
       
    20  @file
       
    21 */
       
    22 
       
    23 #include <e32std.h>
       
    24 #include <hash.h>
       
    25 #include "hashinc.h"
       
    26 #define EXPANDLOOP
       
    27 
       
    28 
       
    29 CMD4::CMD4(void)
       
    30 : CMessageDigest(),iHash(MD4_HASH)
       
    31 	{
       
    32 	}
       
    33 
       
    34 CMD4::CMD4(const CMD4& aMD)
       
    35 : CMessageDigest(aMD),
       
    36   iHash(aMD.iHash),iA(aMD.iA),iB(aMD.iB),iC(aMD.iC),iD(aMD.iD),
       
    37   iNl(aMD.iNl),iNh(aMD.iNh)
       
    38 	{
       
    39 	(void)Mem::Copy(iData,aMD.iData,sizeof(iData));
       
    40 	}
       
    41 	
       
    42 EXPORT_C CMD4* CMD4::NewL(void)
       
    43 	{
       
    44 	CMD4* self = new (ELeave) CMD4();
       
    45 	self->Reset();
       
    46 	return (self);
       
    47 	}
       
    48 	
       
    49 EXPORT_C CMessageDigest* CMD4::ReplicateL(void)
       
    50 	{
       
    51 	 return CMD4::NewL();
       
    52 	}
       
    53 	
       
    54 EXPORT_C CMD4::~CMD4(void)
       
    55 	{
       
    56 	}
       
    57 
       
    58 EXPORT_C TPtrC8 CMD4::Hash(const TDesC8& aMessage)
       
    59 	{
       
    60 	//Adds the aMessage to the internal representation of data to be hashed i.e iData
       
    61 	DoUpdate(aMessage.Ptr(),aMessage.Size());
       
    62 	StoreState();
       
    63 	//Does the padding and does the block operation on the final 512 bit block.
       
    64 	DoFinal();
       
    65 	RestoreState();
       
    66 	return iHash;
       
    67 	}
       
    68 
       
    69 EXPORT_C CMessageDigest* CMD4::CopyL(void)
       
    70 	{
       
    71 	return new(ELeave) CMD4(*this);
       
    72 	}
       
    73 	
       
    74 EXPORT_C TInt CMD4::BlockSize(void)
       
    75 	{
       
    76 	return sizeof(iData);
       
    77 	}
       
    78 	
       
    79 EXPORT_C TInt CMD4::HashSize(void)
       
    80 	{
       
    81 	return MD4_HASH;
       
    82 	}
       
    83 	
       
    84 EXPORT_C void CMD4::Reset(void)
       
    85 	{
       
    86 	iA=0x67452301;
       
    87 	iB=0xefcdab89;
       
    88 	iC=0x98badcfe;
       
    89 	iD=0x10325476;
       
    90 	iNh=0;
       
    91 	iNl=0;
       
    92 	}
       
    93 
       
    94 EXPORT_C void CMD4::Update(const TDesC8& aMessage)
       
    95 	{
       
    96 	DoUpdate(aMessage.Ptr(),aMessage.Size());
       
    97 	}
       
    98 	
       
    99 EXPORT_C TPtrC8 CMD4::Final(const TDesC8& aMessage)
       
   100 	{
       
   101 	//Adds the aMessage to the internal representation of data to be hashed i.e iData
       
   102 	DoUpdate(aMessage.Ptr(),aMessage.Size());
       
   103 	//Does the padding and does the block operation on the final 512 bit block.
       
   104 	DoFinal();
       
   105 	//Resets the values of iA, iB, iC and iD, iNh, iNl
       
   106 	Reset();
       
   107 	return iHash;
       
   108 	}
       
   109 
       
   110 EXPORT_C TPtrC8 CMD4::Final()
       
   111 	{
       
   112 	//Does the padding and does the block operation on the final 512 bit block.
       
   113 	DoFinal();
       
   114 	//Resets the values of iA, iB, iC and iD, iNh, iNl
       
   115 	Reset();
       
   116 	return iHash;
       
   117 	}
       
   118 
       
   119 void CMD4::DoUpdate(const TUint8* aData,TUint aLength)
       
   120 	{
       
   121 	const TUint8* pend=aData+aLength;	
       
   122 	for (const TUint8* paData=aData;paData<pend;paData++) 
       
   123 		{
       
   124 		const TUint8 byte=*paData;
       
   125 		switch (iNl&3) 
       
   126 			{
       
   127 			case 0:
       
   128 				iData[iNl>>2]=byte;
       
   129 				break;
       
   130 			case 1:
       
   131 				iData[iNl>>2]|=byte<<8;
       
   132 				break;
       
   133 			case 2:
       
   134 				iData[iNl>>2]|=byte<<16;
       
   135 				break;
       
   136 			case 3:
       
   137 				iData[iNl>>2]|=byte<<24;
       
   138 				break;
       
   139 			default:
       
   140 				break;
       
   141 			};
       
   142 		if(++iNl==64) 
       
   143 			{
       
   144 			Block();
       
   145 			iNh+=64;
       
   146 			iNl=0;
       
   147 			}
       
   148 		}
       
   149 	}
       
   150 
       
   151 static inline TUint CMD4_F(TUint x,TUint y,TUint z)
       
   152 	{
       
   153 	return ((x)&(y)) | ((~x)&(z));
       
   154 	}
       
   155 	
       
   156 static inline TUint CMD4_G(TUint x,TUint y,TUint z)
       
   157 	{
       
   158 	return ((x)&(y)) | ((x)&(z)) | ((y)&(z));
       
   159 	}
       
   160 	
       
   161 static inline TUint CMD4_H(TUint x,TUint y,TUint z)
       
   162 	{
       
   163 	return (x)^(y)^(z);
       
   164 	}
       
   165 
       
   166 	
       
   167 #ifdef NOREFS
       
   168 static inline TUint CMD4_FF(TUint a,const TUint b,const TUint c,const TUint d,const TUint x,const TUint s)
       
   169 	{
       
   170 	a+=CMD4_F(b,c,d) + x; 
       
   171 	a=CMD_R(a,s);
       
   172 	return a;
       
   173 	}
       
   174 	
       
   175 static inline TUint CMD4_GG(TUint a,const TUint b,const TUint c,const TUint d,const TUint x,const TUint s)
       
   176 	{
       
   177 	a+=CMD4_G(b,c,d) + x + (TUint32)0x5a827999; 
       
   178 	a=CMD_R(a,s);
       
   179 	return a;
       
   180 	}
       
   181 	
       
   182 static inline TUint CMD4_HH(TUint a,const TUint b,const TUint c,const TUint d,const TUint x,const TUint s)
       
   183 	{
       
   184 	a+=CMD4_H(b,c,d) + x + (TUint32)0x6ed9eba1; 
       
   185 	a=CMD_R(a,s);
       
   186 	return a;
       
   187 	}
       
   188 	
       
   189 void CMD4::Block()
       
   190 	{
       
   191 	register TUint tempA=iA;
       
   192 	register TUint tempB=iB;
       
   193 	register TUint tempC=iC;
       
   194 	register TUint tempD=iD;
       
   195 
       
   196 	tempA = CMD4_FF(tempA,tempB,tempC,tempD,iData[ 0],3);
       
   197 	tempD = CMD4_FF(tempD,tempA,tempB,tempC,iData[ 1],7);
       
   198 	tempC = CMD4_FF(tempC,tempD,tempA,tempB,iData[ 2],11);
       
   199 	tempB = CMD4_FF(tempB,tempC,tempD,tempA,iData[ 3],19);
       
   200 	tempA = CMD4_FF(tempA,tempB,tempC,tempD,iData[ 4],3);
       
   201 	tempD = CMD4_FF(tempD,tempA,tempB,tempC,iData[ 5],7);
       
   202 	tempC = CMD4_FF(tempC,tempD,tempA,tempB,iData[ 6],11);
       
   203 	tempB = CMD4_FF(tempB,tempC,tempD,tempA,iData[ 7],19);
       
   204 	tempA = CMD4_FF(tempA,tempB,tempC,tempD,iData[ 8],3);
       
   205 	tempD = CMD4_FF(tempD,tempA,tempB,tempC,iData[ 9],7);
       
   206 	tempC = CMD4_FF(tempC,tempD,tempA,tempB,iData[10],11);
       
   207 	tempB = CMD4_FF(tempB,tempC,tempD,tempA,iData[11],19);
       
   208 	tempA = CMD4_FF(tempA,tempB,tempC,tempD,iData[12],3);
       
   209 	tempD = CMD4_FF(tempD,tempA,tempB,tempC,iData[13],7);
       
   210 	tempC = CMD4_FF(tempC,tempD,tempA,tempB,iData[14],11);
       
   211 	tempB = CMD4_FF(tempB,tempC,tempD,tempA,iData[15],19);
       
   212 
       
   213 	tempA = CMD4_GG(tempA,tempB,tempC,tempD,iData[ 0],3);
       
   214 	tempD = CMD4_GG(tempD,tempA,tempB,tempC,iData[ 4],5);
       
   215 	tempC = CMD4_GG(tempC,tempD,tempA,tempB,iData[ 8],9);
       
   216 	tempB = CMD4_GG(tempB,tempC,tempD,tempA,iData[12],13);
       
   217 	tempA = CMD4_GG(tempA,tempB,tempC,tempD,iData[ 1],3);
       
   218 	tempD = CMD4_GG(tempD,tempA,tempB,tempC,iData[ 5],5);
       
   219 	tempC = CMD4_GG(tempC,tempD,tempA,tempB,iData[ 9],9);
       
   220 	tempB = CMD4_GG(tempB,tempC,tempD,tempA,iData[13],13);
       
   221 	tempA = CMD4_GG(tempA,tempB,tempC,tempD,iData[ 2],3);
       
   222 	tempD = CMD4_GG(tempD,tempA,tempB,tempC,iData[ 6],5);
       
   223 	tempC = CMD4_GG(tempC,tempD,tempA,tempB,iData[10],9);
       
   224 	tempB = CMD4_GG(tempB,tempC,tempD,tempA,iData[14],13);
       
   225 	tempA = CMD4_GG(tempA,tempB,tempC,tempD,iData[ 3],3);
       
   226 	tempD = CMD4_GG(tempD,tempA,tempB,tempC,iData[ 7],5);
       
   227 	tempC = CMD4_GG(tempC,tempD,tempA,tempB,iData[11],9);
       
   228 	tempB = CMD4_GG(tempB,tempC,tempD,tempA,iData[15],13);
       
   229 
       
   230 	tempA = CMD4_HH(tempA,tempB,tempC,tempD,iData[ 0],3);
       
   231 	tempD = CMD4_HH(tempD,tempA,tempB,tempC,iData[ 8],9);
       
   232 	tempC = CMD4_HH(tempC,tempD,tempA,tempB,iData[ 4],11);
       
   233 	tempB = CMD4_HH(tempB,tempC,tempD,tempA,iData[12],15);
       
   234 	tempA = CMD4_HH(tempA,tempB,tempC,tempD,iData[ 2],3);
       
   235 	tempD = CMD4_HH(tempD,tempA,tempB,tempC,iData[10],9);
       
   236 	tempC = CMD4_HH(tempC,tempD,tempA,tempB,iData[ 6],11);
       
   237 	tempB = CMD4_HH(tempB,tempC,tempD,tempA,iData[14],15);
       
   238 	tempA = CMD4_HH(tempA,tempB,tempC,tempD,iData[ 1],3);
       
   239 	tempD = CMD4_HH(tempD,tempA,tempB,tempC,iData[ 9],9);
       
   240 	tempC = CMD4_HH(tempC,tempD,tempA,tempB,iData[ 5],11);
       
   241 	tempB = CMD4_HH(tempB,tempC,tempD,tempA,iData[13],15);
       
   242 	tempA = CMD4_HH(tempA,tempB,tempC,tempD,iData[ 3],3);
       
   243 	tempD = CMD4_HH(tempD,tempA,tempB,tempC,iData[11],9);
       
   244 	tempC = CMD4_HH(tempC,tempD,tempA,tempB,iData[ 7],11);
       
   245 	tempB = CMD4_HH(tempB,tempC,tempD,tempA,iData[15],15);
       
   246 
       
   247 	iA+=tempA;
       
   248 	iB+=tempB;
       
   249 	iC+=tempC;
       
   250 	iD+=tempD;
       
   251 	}
       
   252 
       
   253 #else
       
   254 #ifdef MACRO
       
   255 #define CMD4_FF(a, b, c, d, x, s) (CMD_R(a += CMD4_F(b,c,d) + x, s))
       
   256 #define CMD4_GG(a, b, c, d, x, s) (CMD_R(a += CMD4_G(b,c,d) + x + (TUint32)0x5a827999, s))
       
   257 #define CMD4_HH(a, b, c, d, x, s) (CMD_R(a += CMD4_H(b,c,d) + x + (TUint32)0x6ed9eba1, s))
       
   258 void CMD4::Block()
       
   259 	{
       
   260 	register TUint tempA=iA;
       
   261 	register TUint tempB=iB;
       
   262 	register TUint tempC=iC;
       
   263 	register TUint tempD=iD;
       
   264 	
       
   265 	tempA = CMD4_FF(tempA,tempB,tempC,tempD,iData[ 0],3);
       
   266 	tempD = CMD4_FF(tempD,tempA,tempB,tempC,iData[ 1],7);
       
   267 	tempC = CMD4_FF(tempC,tempD,tempA,tempB,iData[ 2],11);
       
   268 	tempB = CMD4_FF(tempB,tempC,tempD,tempA,iData[ 3],19);
       
   269 	tempA = CMD4_FF(tempA,tempB,tempC,tempD,iData[ 4],3);
       
   270 	tempD = CMD4_FF(tempD,tempA,tempB,tempC,iData[ 5],7);
       
   271 	tempC = CMD4_FF(tempC,tempD,tempA,tempB,iData[ 6],11);
       
   272 	tempB = CMD4_FF(tempB,tempC,tempD,tempA,iData[ 7],19);
       
   273 	tempA = CMD4_FF(tempA,tempB,tempC,tempD,iData[ 8],3);
       
   274 	tempD = CMD4_FF(tempD,tempA,tempB,tempC,iData[ 9],7);
       
   275 	tempC = CMD4_FF(tempC,tempD,tempA,tempB,iData[10],11);
       
   276 	tempB = CMD4_FF(tempB,tempC,tempD,tempA,iData[11],19);
       
   277 	tempA = CMD4_FF(tempA,tempB,tempC,tempD,iData[12],3);
       
   278 	tempD = CMD4_FF(tempD,tempA,tempB,tempC,iData[13],7);
       
   279 	tempC = CMD4_FF(tempC,tempD,tempA,tempB,iData[14],11);
       
   280 	tempB = CMD4_FF(tempB,tempC,tempD,tempA,iData[15],19);
       
   281 
       
   282 	tempA = CMD4_GG(tempA,tempB,tempC,tempD,iData[ 0],3);
       
   283 	tempD = CMD4_GG(tempD,tempA,tempB,tempC,iData[ 4],5);
       
   284 	tempC = CMD4_GG(tempC,tempD,tempA,tempB,iData[ 8],9);
       
   285 	tempB = CMD4_GG(tempB,tempC,tempD,tempA,iData[12],13);
       
   286 	tempA = CMD4_GG(tempA,tempB,tempC,tempD,iData[ 1],3);
       
   287 	tempD = CMD4_GG(tempD,tempA,tempB,tempC,iData[ 5],5);
       
   288 	tempC = CMD4_GG(tempC,tempD,tempA,tempB,iData[ 9],9);
       
   289 	tempB = CMD4_GG(tempB,tempC,tempD,tempA,iData[13],13);
       
   290 	tempA = CMD4_GG(tempA,tempB,tempC,tempD,iData[ 2],3);
       
   291 	tempD = CMD4_GG(tempD,tempA,tempB,tempC,iData[ 6],5);
       
   292 	tempC = CMD4_GG(tempC,tempD,tempA,tempB,iData[10],9);
       
   293 	tempB = CMD4_GG(tempB,tempC,tempD,tempA,iData[14],13);
       
   294 	tempA = CMD4_GG(tempA,tempB,tempC,tempD,iData[ 3],3);
       
   295 	tempD = CMD4_GG(tempD,tempA,tempB,tempC,iData[ 7],5);
       
   296 	tempC = CMD4_GG(tempC,tempD,tempA,tempB,iData[11],9);
       
   297 	tempB = CMD4_GG(tempB,tempC,tempD,tempA,iData[15],13);
       
   298 
       
   299 	tempA = CMD4_HH(tempA,tempB,tempC,tempD,iData[ 0],3);
       
   300 	tempD = CMD4_HH(tempD,tempA,tempB,tempC,iData[ 8],9);
       
   301 	tempC = CMD4_HH(tempC,tempD,tempA,tempB,iData[ 4],11);
       
   302 	tempB = CMD4_HH(tempB,tempC,tempD,tempA,iData[12],15);
       
   303 	tempA = CMD4_HH(tempA,tempB,tempC,tempD,iData[ 2],3);
       
   304 	tempD = CMD4_HH(tempD,tempA,tempB,tempC,iData[10],9);
       
   305 	tempC = CMD4_HH(tempC,tempD,tempA,tempB,iData[ 6],11);
       
   306 	tempB = CMD4_HH(tempB,tempC,tempD,tempA,iData[14],15);
       
   307 	tempA = CMD4_HH(tempA,tempB,tempC,tempD,iData[ 1],3);
       
   308 	tempD = CMD4_HH(tempD,tempA,tempB,tempC,iData[ 9],9);
       
   309 	tempC = CMD4_HH(tempC,tempD,tempA,tempB,iData[ 5],11);
       
   310 	tempB = CMD4_HH(tempB,tempC,tempD,tempA,iData[13],15);
       
   311 	tempA = CMD4_HH(tempA,tempB,tempC,tempD,iData[ 3],3);
       
   312 	tempD = CMD4_HH(tempD,tempA,tempB,tempC,iData[11],9);
       
   313 	tempC = CMD4_HH(tempC,tempD,tempA,tempB,iData[ 7],11);
       
   314 	tempB = CMD4_HH(tempB,tempC,tempD,tempA,iData[15],15);
       
   315 
       
   316 	iA+=tempA;
       
   317 	iB+=tempB;
       
   318 	iC+=tempC;
       
   319 	iD+=tempD;
       
   320 	}
       
   321 	
       
   322 #else
       
   323 static inline void CMD4_FF(TUint& a,const TUint b,const TUint c,const TUint d,const TUint x,const TUint s)
       
   324 	{
       
   325 	a+=CMD4_F(b,c,d) + x; 
       
   326 	a=CMD_R(a,s);
       
   327 	}
       
   328 	
       
   329 static inline void CMD4_GG(TUint& a,const TUint b,const TUint c,const TUint d,const TUint x,const TUint s)
       
   330 	{
       
   331 	a+=CMD4_G(b,c,d) + x + (TUint32)0x5a827999; 
       
   332 	a=CMD_R(a,s);
       
   333 	}
       
   334 	
       
   335 static inline void CMD4_HH(TUint& a,const TUint b,const TUint c,const TUint d,const TUint x,const TUint s)
       
   336 	{
       
   337 	a+=CMD4_H(b,c,d) + x + (TUint32)0x6ed9eba1; 
       
   338 	a=CMD_R(a,s);
       
   339 	}
       
   340 
       
   341 void CMD4::Block()
       
   342 	{
       
   343 	register TUint tempA=iA;
       
   344 	register TUint tempB=iB;
       
   345 	register TUint tempC=iC;
       
   346 	register TUint tempD=iD;
       
   347 
       
   348 	CMD4_FF(tempA,tempB,tempC,tempD,iData[ 0],3);
       
   349 	CMD4_FF(tempD,tempA,tempB,tempC,iData[ 1],7);
       
   350 	CMD4_FF(tempC,tempD,tempA,tempB,iData[ 2],11);
       
   351 	CMD4_FF(tempB,tempC,tempD,tempA,iData[ 3],19);
       
   352 	CMD4_FF(tempA,tempB,tempC,tempD,iData[ 4],3);
       
   353 	CMD4_FF(tempD,tempA,tempB,tempC,iData[ 5],7);
       
   354 	CMD4_FF(tempC,tempD,tempA,tempB,iData[ 6],11);
       
   355 	CMD4_FF(tempB,tempC,tempD,tempA,iData[ 7],19);
       
   356 	CMD4_FF(tempA,tempB,tempC,tempD,iData[ 8],3);
       
   357 	CMD4_FF(tempD,tempA,tempB,tempC,iData[ 9],7);
       
   358 	CMD4_FF(tempC,tempD,tempA,tempB,iData[10],11);
       
   359 	CMD4_FF(tempB,tempC,tempD,tempA,iData[11],19);
       
   360 	CMD4_FF(tempA,tempB,tempC,tempD,iData[12],3);
       
   361 	CMD4_FF(tempD,tempA,tempB,tempC,iData[13],7);
       
   362 	CMD4_FF(tempC,tempD,tempA,tempB,iData[14],11);
       
   363 	CMD4_FF(tempB,tempC,tempD,tempA,iData[15],19);
       
   364 
       
   365 	CMD4_GG(tempA,tempB,tempC,tempD,iData[ 0],3);
       
   366 	CMD4_GG(tempD,tempA,tempB,tempC,iData[ 4],5);
       
   367 	CMD4_GG(tempC,tempD,tempA,tempB,iData[ 8],9);
       
   368 	CMD4_GG(tempB,tempC,tempD,tempA,iData[12],13);
       
   369 	CMD4_GG(tempA,tempB,tempC,tempD,iData[ 1],3);
       
   370 	CMD4_GG(tempD,tempA,tempB,tempC,iData[ 5],5);
       
   371 	CMD4_GG(tempC,tempD,tempA,tempB,iData[ 9],9);
       
   372 	CMD4_GG(tempB,tempC,tempD,tempA,iData[13],13);
       
   373 	CMD4_GG(tempA,tempB,tempC,tempD,iData[ 2],3);
       
   374 	CMD4_GG(tempD,tempA,tempB,tempC,iData[ 6],5);
       
   375 	CMD4_GG(tempC,tempD,tempA,tempB,iData[10],9);
       
   376 	CMD4_GG(tempB,tempC,tempD,tempA,iData[14],13);
       
   377 	CMD4_GG(tempA,tempB,tempC,tempD,iData[ 3],3);
       
   378 	CMD4_GG(tempD,tempA,tempB,tempC,iData[ 7],5);
       
   379 	CMD4_GG(tempC,tempD,tempA,tempB,iData[11],9);
       
   380 	CMD4_GG(tempB,tempC,tempD,tempA,iData[15],13);
       
   381 
       
   382 	CMD4_HH(tempA,tempB,tempC,tempD,iData[ 0],3);
       
   383 	CMD4_HH(tempD,tempA,tempB,tempC,iData[ 8],9);
       
   384 	CMD4_HH(tempC,tempD,tempA,tempB,iData[ 4],11);
       
   385 	CMD4_HH(tempB,tempC,tempD,tempA,iData[12],15);
       
   386 	CMD4_HH(tempA,tempB,tempC,tempD,iData[ 2],3);
       
   387 	CMD4_HH(tempD,tempA,tempB,tempC,iData[10],9);
       
   388 	CMD4_HH(tempC,tempD,tempA,tempB,iData[ 6],11);
       
   389 	CMD4_HH(tempB,tempC,tempD,tempA,iData[14],15);
       
   390 	CMD4_HH(tempA,tempB,tempC,tempD,iData[ 1],3);
       
   391 	CMD4_HH(tempD,tempA,tempB,tempC,iData[ 9],9);
       
   392 	CMD4_HH(tempC,tempD,tempA,tempB,iData[ 5],11);
       
   393 	CMD4_HH(tempB,tempC,tempD,tempA,iData[13],15);
       
   394 	CMD4_HH(tempA,tempB,tempC,tempD,iData[ 3],3);
       
   395 	CMD4_HH(tempD,tempA,tempB,tempC,iData[11],9);
       
   396 	CMD4_HH(tempC,tempD,tempA,tempB,iData[ 7],11);
       
   397 	CMD4_HH(tempB,tempC,tempD,tempA,iData[15],15);
       
   398 	
       
   399 	iA+=tempA;
       
   400 	iB+=tempB;
       
   401 	iC+=tempC;
       
   402 	iD+=tempD;
       
   403 	}
       
   404 #endif
       
   405 #endif
       
   406 
       
   407 void CMD4::DoFinal(void)
       
   408 	{
       
   409 	iNh += iNl;
       
   410 	const TUint ul128=128;
       
   411 	switch (iNl&3) 
       
   412 		{
       
   413 		case 0:
       
   414 			iData[iNl>>2] = ul128;
       
   415 			break;
       
   416 		case 1:
       
   417 			iData[iNl>>2] += ul128<<8;
       
   418 			break;
       
   419 		case 2:
       
   420 			iData[iNl>>2] += ul128<<16;
       
   421 			break;
       
   422 		case 3:
       
   423 			iData[iNl>>2] += ul128<<24;
       
   424 			break;
       
   425 		default:
       
   426 			break;
       
   427 		};
       
   428 	if (iNl>=56) 
       
   429 		{
       
   430 		if (iNl<60)
       
   431 			iData[15]=0;		
       
   432 		Block();
       
   433 		Mem::FillZ(iData,14*sizeof(TUint));
       
   434 		} 
       
   435 	else
       
   436 		{
       
   437 		const TUint offset=(iNl+4)>>2;
       
   438 		Mem::FillZ(iData+offset,(14-offset)*sizeof(TUint));
       
   439 		}
       
   440 	
       
   441 	iData[14]=iNh<<3;//number in bits
       
   442 	// this will fail if the total input length is longer than 2^32 in bits
       
   443 	//(2^31 in bytes) which is roughly half a gig.
       
   444 	iData[15]=0;
       
   445 
       
   446 	Block();
       
   447 	//
       
   448 	// Generate hash value into iHash
       
   449 	//
       
   450 	TUint tmp=iA;
       
   451 	iHash[0]=(TUint8)(tmp & 255);
       
   452 	iHash[1]=(TUint8)((tmp >>= 8) & 255);
       
   453 	iHash[2]=(TUint8)((tmp >>= 8) & 255);
       
   454 	iHash[3]=(TUint8)((tmp >>= 8) & 255);
       
   455 
       
   456 	tmp=iB;
       
   457 	iHash[4]=(TUint8)(tmp & 255);
       
   458 	iHash[5]=(TUint8)((tmp >>= 8) & 255);
       
   459 	iHash[6]=(TUint8)((tmp >>= 8) & 255);
       
   460 	iHash[7]=(TUint8)((tmp >>= 8) & 255);
       
   461 
       
   462 	tmp=iC;
       
   463 	iHash[8]=(TUint8)(tmp & 255);
       
   464 	iHash[9]=(TUint8)((tmp >>= 8) & 255);
       
   465 	iHash[10]=(TUint8)((tmp >>= 8) & 255);
       
   466 	iHash[11]=(TUint8)((tmp >>= 8) & 255);
       
   467 
       
   468 	tmp=iD;
       
   469 	iHash[12]=(TUint8)(tmp & 255);
       
   470 	iHash[13]=(TUint8)((tmp >>= 8) & 255);
       
   471 	iHash[14]=(TUint8)((tmp >>= 8) & 255);
       
   472 	iHash[15]=(TUint8)((tmp >>= 8) & 255);
       
   473 	}
       
   474 
       
   475 void CMD4::RestoreState()
       
   476 {
       
   477 	iA = iACopy;
       
   478 	iB = iBCopy;
       
   479 	iC = iCCopy;
       
   480 	iD = iDCopy;
       
   481 	iNl = iNlCopy;
       
   482 	iNh = iNhCopy;	
       
   483 	Mem::Copy(&iData[0], &iDataCopy[0], MD4_LBLOCK*sizeof(TUint)); 
       
   484 }
       
   485 
       
   486 void CMD4::StoreState()
       
   487 {
       
   488 	iACopy = iA;
       
   489 	iBCopy = iB;
       
   490 	iCCopy = iC;
       
   491 	iDCopy = iD;
       
   492 	iNlCopy = iNl;
       
   493 	iNhCopy = iNh;	
       
   494 	Mem::Copy(&iDataCopy[0], &iData[0], MD4_LBLOCK*sizeof(TUint));
       
   495 }