crypto/weakcrypto/inc/hash.h
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2000-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 * ** IMPORTANT ** PublishedPartner API's in this file are published to 3rd party developers via the 
       
    16 * Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted.
       
    17 * This header contains the definition of the message digest classes
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 /**
       
    25  @file 
       
    26  @publishedAll
       
    27  @released
       
    28 */
       
    29 
       
    30 #ifndef __HASH_H__
       
    31 #define __HASH_H__
       
    32 
       
    33 #include <e32base.h>
       
    34 
       
    35 
       
    36 //Forward Declarations
       
    37 class MSHA2Impl;
       
    38 
       
    39 /**
       
    40  * Base class for message digests.
       
    41  */
       
    42 class CMessageDigest:public CBase
       
    43 	{
       
    44 	public:
       
    45 	/**
       
    46 	 *	Enumeration to identify hash functions (aka message-digest algorithms).
       
    47 	 */
       
    48 	enum THashId
       
    49 	{
       
    50 		/** 
       
    51 		 * Message Digest algorithm developed by Rivest for digital signature
       
    52 		 * applications (and optimized for 8-bit machines). 
       
    53 		 * 
       
    54 		 * Takes a message of arbitrary length and produces a 128-bit message digest. 
       
    55 		 *
       
    56 		 * See RFC 1319
       
    57 		 */
       
    58 		EMD2,
       
    59 		/** 
       
    60 		 * Message Digest algorithm developed by Rivest for digital signature
       
    61 		 * applications (and optimized for 32-bit machines). 
       
    62 		 * 
       
    63 		 * Takes a message of arbitrary length and produces a 128-bit message digest. 
       
    64 		 * 
       
    65 		 * See RFC 1321
       
    66 		 */
       
    67 		EMD5,
       
    68 		/** 
       
    69 		 * Secure Hash Algorithm (version 1) is a message digest algorithm developed by 
       
    70 		 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
       
    71 		 * 
       
    72 		 * It takes a message of less than 2^64 bits in length and produces
       
    73 		 * a 160-bit message digest.
       
    74 		 *
       
    75 		 * See FIPS 180-1 and RFC 3174
       
    76 		 */
       
    77 		ESHA1,
       
    78 		/** 
       
    79 		 * HMAC - Hash function based Message Authentication Code is a mechanism
       
    80 		 * for message authentication using cryptographic hash functions. (A checksum.)
       
    81 		 * 
       
    82 		 * It can be used with any iterative cryptographic hash function,
       
    83    		 * e.g., MD5, SHA-1, in combination with a secret shared key
       
    84    		 * to produce a checksum that is appended to the message.
       
    85    		 * The cryptographic strength of HMAC depends on the properties
       
    86    		 * of the underlying hash function.
       
    87 		 *
       
    88 		 * See RFC 2104 
       
    89 		 */
       
    90 		HMAC,
       
    91 		/** 
       
    92 		 * Message Digest algorithm developed by Rivest for digital signature
       
    93 		 * applications (and optimized for 32-bit machines). 
       
    94 		 * 
       
    95 		 * Takes a message of arbitrary length and produces a 128-bit message digest. 
       
    96 		 * 
       
    97 		 * See RFC 1320
       
    98 		 */
       
    99 		EMD4,
       
   100 		/** 
       
   101 		 * Secure Hash Algorithm - 224 (version 2) is a message digest algorithm developed by 
       
   102 		 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
       
   103 		 * 
       
   104 		 * It takes a message of less than 2^64 bits in length and produces
       
   105 		 * a 224-bit message digest.
       
   106 		 *
       
   107 		 * See FIPS 180-2 (with change notice), RFC3874 and FIPS 180-3
       
   108 		 */
       
   109 		ESHA224,
       
   110 		/** 
       
   111 		 * Secure Hash Algorithm - 256 (version 2) is a message digest algorithm developed by 
       
   112 		 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
       
   113 		 * 
       
   114 		 * It takes a message of less than 2^64 bits in length and produces
       
   115 		 * a 256-bit message digest.
       
   116 		 *
       
   117 		 * See FIPS 180-2 and RFC 4634
       
   118 		 */
       
   119 		ESHA256,
       
   120 		/** 
       
   121 		 * Secure Hash Algorithm - 384 (version 2) is a message digest algorithm developed by 
       
   122 		 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
       
   123 		 * 
       
   124 		 * It takes a message of less than 2^128 bits in length and produces
       
   125 		 * a 384-bit message digest.
       
   126 		 *
       
   127 		 * See FIPS 180-2 and RFC 4634
       
   128 		 */
       
   129 		ESHA384,
       
   130 		/** 
       
   131 		 * Secure Hash Algorithm - 512 (version 2) is a message digest algorithm developed by 
       
   132 		 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
       
   133 		 * 
       
   134 		 * It takes a message of less than 2^128 bits in length and produces
       
   135 		 * a 512-bit message digest.
       
   136 		 *
       
   137 		 * See FIPS 180-2 and RFC 4634
       
   138 		 */
       
   139 		ESHA512
       
   140 	};
       
   141 	
       
   142 	public:
       
   143 		/** 
       
   144 		 * Creates a brand new reset CMessageDigest object containing no state
       
   145 		 * information from the current object.  
       
   146 		 * 
       
   147 		 * To make a copy of a message digest with its internal state intact,
       
   148 		 * see CopyL().
       
   149 		 *
       
   150 		 * @return A pointer to the new reset CMessageDigest object
       
   151 		 */
       
   152 		IMPORT_C virtual CMessageDigest* ReplicateL(void)=0;		
       
   153 
       
   154 		/** 
       
   155 		 * Adds aMessage to the internal representation of data to be hashed,
       
   156 		 * then returns a TPtrC8 of the finalised hash of all the previously
       
   157 		 * appended messages.
       
   158 		 * 
       
   159 		 * @param aMessage	Data to be included in the hash.
       
   160 		 * @return			A descriptor pointer to the buffer containing the
       
   161 		 *					resulting hash.
       
   162 		 */
       
   163 		IMPORT_C virtual TPtrC8 Hash(const TDesC8& aMessage)=0;
       
   164 
       
   165 		/** 
       
   166 		 * Creates a new CMessageDigest object with the exact same state as
       
   167 		 * the current object.  
       
   168 		 *
       
   169 		 * This function copies all internal state of the message digest.
       
   170 		 * To create a new CMessageDigest object without the state of
       
   171 		 * the current object, see ReplicateL().
       
   172 		 *
       
   173 		 * @return A pointer to the new CMessageDigest object
       
   174 		 */
       
   175 		IMPORT_C virtual CMessageDigest* CopyL(void)=0;
       
   176 		
       
   177 		/** 
       
   178 		 * Gets the internal block size of the message digest.
       
   179 		 * 
       
   180 		 * @return	Internal block size of message digest in bytes.
       
   181 		 */
       
   182 		IMPORT_C virtual TInt BlockSize(void)=0;
       
   183 		
       
   184 		/** 
       
   185 		 * Gets the size of the message digest output.
       
   186 		 *
       
   187 		 * @return	Output size of the message digest in bytes.
       
   188 		 */
       
   189 		IMPORT_C virtual TInt HashSize(void)=0;
       
   190 		
       
   191 		/** 
       
   192 		 * Resets the internal state of the message digest.  
       
   193 		 *
       
   194 		 * A reset hash object loses all internal state representing the hashed
       
   195 		 * data. A reset message digest is suitable to begin a new, distinct hash
       
   196 		 * of different data.  Any previously returned TPtrC8 from a call to
       
   197 		 * Final() remains valid until any subsequent call to Update() or
       
   198 		 * Final().
       
   199 		 */
       
   200 		IMPORT_C virtual void Reset(void)=0;
       
   201 		
       
   202 		/**
       
   203 		 * Destructor.
       
   204 		 */		
       
   205 		IMPORT_C ~CMessageDigest(void);
       
   206 	public:
       
   207 		/** 
       
   208 		 * Adds data to the internal representation of messages to be hashed.
       
   209 		 *
       
   210 		 * @param aMessage	Data to be included in the hash.
       
   211 		 * @since v8.0
       
   212 		 */
       
   213 		IMPORT_C virtual void Update(const TDesC8& aMessage)=0;
       
   214 		
       
   215 		/** 
       
   216 		 * Adds aMessage to the internal representation of data to be hashed,
       
   217 		 * returns a TPtrC8 of the finalised hash of all the previously
       
   218 		 * appended messages, and calls Reset().
       
   219 		 * 
       
   220 		 * @param aMessage	Data to be included in the hash 
       
   221 		 * @return			A descriptor pointer to the buffer containing the
       
   222 		 *					resulting hash.
       
   223 		 * @since v8.0
       
   224 		 */
       
   225 		IMPORT_C virtual TPtrC8 Final(const TDesC8& aMessage)=0;
       
   226 		
       
   227 		/** 
       
   228 		 * Gets a TPtrC8 of the finalised hash of all the previously
       
   229 		 * appended messages and then calls Reset().
       
   230 		 * 
       
   231 		 * @return	A descriptor pointer to the buffer containing the
       
   232 		 * 			resulting hash.
       
   233 		 * @since v8.0
       
   234 		 */
       
   235 		IMPORT_C virtual TPtrC8 Final(void)=0;
       
   236 	public:
       
   237 		/**
       
   238 		 * Restores the internal state of the message digest
       
   239 		 * to a previously stored state.
       
   240 		 *
       
   241 		 * @see StoreState()
       
   242 		 */
       
   243 		virtual void RestoreState() = 0;
       
   244 
       
   245 		/**
       
   246 		 * Stores the internal state of the message digest. 
       
   247 		 */
       
   248 		virtual void StoreState() = 0;
       
   249 	protected:
       
   250 		/**
       
   251 		 * Constructor
       
   252 		 */
       
   253 		IMPORT_C CMessageDigest(void);
       
   254 
       
   255 		/**
       
   256 		 * Copy constructor
       
   257 		 *
       
   258 		 * @param aMD	A CMessageDigest object
       
   259 		 */
       
   260 		IMPORT_C CMessageDigest(const CMessageDigest& aMD);
       
   261 	};
       
   262 
       
   263 /** 
       
   264  * The MD2 block size (in bytes) 
       
   265  * 
       
   266  * @internalTechnology 
       
   267  */
       
   268 const TInt MD2_BLOCK=16;
       
   269 
       
   270 /** The size (in bytes) of the MD2 message digest */
       
   271 const TInt MD2_HASH=16;
       
   272 
       
   273 /**
       
   274  * An MD2 message digest
       
   275  */
       
   276 class CMD2:public CMessageDigest
       
   277 
       
   278 	{
       
   279 	public:
       
   280 		/**
       
   281 		 * Creates a new MD2 object.
       
   282 		 *
       
   283 		 * @return	A pointer to the new CMD2 object
       
   284 		 */
       
   285 		IMPORT_C static CMD2* NewL(void);
       
   286 		IMPORT_C CMessageDigest* ReplicateL(void);
       
   287 		IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
       
   288 		/** Destructor */
       
   289 		IMPORT_C ~CMD2(void);
       
   290 		IMPORT_C CMessageDigest* CopyL(void);
       
   291 		IMPORT_C TInt BlockSize(void);
       
   292 		IMPORT_C TInt HashSize(void);
       
   293 		IMPORT_C void Reset(void);
       
   294 		IMPORT_C void Update(const TDesC8& aMessage);
       
   295 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
       
   296 		IMPORT_C TPtrC8 Final();
       
   297 	public:
       
   298 		void RestoreState();
       
   299 		void StoreState();
       
   300 	private:
       
   301 		void DoUpdate(const TUint8* aData,TUint aLength);
       
   302 		void DoFinal(void);
       
   303 		void Block(const TUint8* aData);
       
   304 	private:
       
   305 		CMD2(void);
       
   306 		CMD2(const CMD2& aMD2);
       
   307 	private:
       
   308 		TBuf8<MD2_HASH> iHash;
       
   309 		TInt iNum;
       
   310 		TUint8 iData[MD2_BLOCK];
       
   311 		TUint iChecksum[MD2_BLOCK];
       
   312 		TUint iState[MD2_BLOCK];
       
   313 	private:
       
   314 		TBuf8<MD2_HASH> iHashBuf;
       
   315 		TUint8 iDataTemp[MD2_BLOCK];
       
   316 		TUint iChecksumTemp[MD2_BLOCK];
       
   317 		TUint iStateTemp[MD2_BLOCK];
       
   318 	};
       
   319 
       
   320 /** 
       
   321  * The MD5 block size (in bytes)
       
   322  * 
       
   323  * @internalTechnology 
       
   324  */
       
   325 const TUint MD5_LBLOCK=16;
       
   326 
       
   327 /** The size (in bytes) of the MD5 message digest */
       
   328 const TUint MD5_HASH=16;
       
   329 
       
   330 /**
       
   331  * An MD5 message digest
       
   332  *
       
   333  * Takes a message of arbitrary length as input and produces a 128-bit message digest. 
       
   334  *
       
   335  * The length of input data should not be longer than 2^32 in bits(2^31 in bytes)
       
   336  * which is roughly half a gig.
       
   337  *
       
   338  */
       
   339 class CMD5:public CMessageDigest
       
   340 	{
       
   341 	public:
       
   342 		/**
       
   343 		 * Creates a new MD5 object.
       
   344 		 *
       
   345 		 * @return	A pointer to the new CMD5 object
       
   346 		 */
       
   347 		IMPORT_C static CMD5* NewL(void);
       
   348 		IMPORT_C CMessageDigest* ReplicateL(void);
       
   349 		IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
       
   350 		/** Destructor */
       
   351 		IMPORT_C ~CMD5(void);
       
   352 		IMPORT_C CMessageDigest* CopyL(void);
       
   353 		IMPORT_C TInt BlockSize(void);
       
   354 		IMPORT_C TInt HashSize(void);
       
   355 		IMPORT_C void Reset(void);
       
   356 		IMPORT_C void Update(const TDesC8& aMessage);
       
   357 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
       
   358 		IMPORT_C TPtrC8 Final();
       
   359 	public:
       
   360 		void RestoreState();
       
   361 		void StoreState();
       
   362 	private:
       
   363 		CMD5(void);
       
   364 		CMD5(const CMD5& aMD5);
       
   365 	private:
       
   366 		void DoUpdate(const TUint8* aData,TUint aLength);
       
   367 		void DoFinal(void);
       
   368 		void Block();
       
   369 	private:
       
   370 		TBuf8<MD5_HASH> iHash;
       
   371 	private:
       
   372 		TUint iA;
       
   373 		TUint iB;
       
   374 		TUint iC;
       
   375 		TUint iD;
       
   376 		TUint iNl;
       
   377 		TUint iNh;
       
   378 		TUint iData[MD5_LBLOCK];
       
   379 	private:
       
   380 		TUint iACopy;
       
   381 		TUint iBCopy;
       
   382 		TUint iCCopy;
       
   383 		TUint iDCopy;
       
   384 		TUint iNlCopy;
       
   385 		TUint iNhCopy;
       
   386 		TUint iDataCopy[MD5_LBLOCK];
       
   387 	};
       
   388 
       
   389 /** 
       
   390  * The SHA-1 block size (in bytes) 
       
   391  * 
       
   392  * @internalTechnology 
       
   393  */
       
   394 const TUint SHA1_LBLOCK=16;
       
   395 
       
   396 /** The size (in bytes) of the SHA-1 message digest */
       
   397 const TUint SHA1_HASH=20;
       
   398 
       
   399 /** The size (in bytes) of the SHA message digest */
       
   400 const TUint SHA_HASH=SHA1_HASH;
       
   401 
       
   402 /**
       
   403  * A SHA-1 message digest
       
   404  */
       
   405 class CSHA1:public CMessageDigest
       
   406 	{
       
   407 	public:
       
   408 		/**
       
   409 		 * Creates a new SHA-1 object.
       
   410 		 *
       
   411 		 * @return	A pointer to the new SHA-1 object
       
   412 		 */
       
   413 		IMPORT_C static CSHA1* NewL(void);
       
   414 		IMPORT_C CMessageDigest* ReplicateL(void);
       
   415 		IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
       
   416 		/** Destructor */
       
   417 		IMPORT_C ~CSHA1(void);
       
   418 		IMPORT_C CMessageDigest* CopyL(void);
       
   419 		IMPORT_C TInt BlockSize(void);
       
   420 		IMPORT_C TInt HashSize(void);
       
   421 		IMPORT_C void Reset(void);
       
   422 		IMPORT_C void Update(const TDesC8& aMessage);
       
   423 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
       
   424 		IMPORT_C TPtrC8 Final();
       
   425 	public:
       
   426 		void RestoreState();
       
   427 		void StoreState();
       
   428 	private:
       
   429 		CSHA1(void);
       
   430 		CSHA1(const CSHA1& aSHA1);
       
   431 		void ConstructL(void);
       
   432 	private:
       
   433 		void DoUpdate(const TUint8* aData,TUint aLength);
       
   434 		void DoFinal(void);
       
   435 		void Block();
       
   436 	private:
       
   437 		TBuf8<SHA1_HASH> iHash;
       
   438 		TUint iA;
       
   439 		TUint iB;
       
   440 		TUint iC;
       
   441 		TUint iD;
       
   442 		TUint iE;
       
   443 		TUint iNl;
       
   444 		TUint iNh;
       
   445 		TUint iData[SHA1_LBLOCK*5];
       
   446 	private:
       
   447 		TUint iACopy;
       
   448 		TUint iBCopy;
       
   449 		TUint iCCopy;
       
   450 		TUint iDCopy;
       
   451 		TUint iECopy;
       
   452 		TUint iNlCopy;
       
   453 		TUint iNhCopy;	
       
   454 		TUint iDataCopy[SHA1_LBLOCK*5];
       
   455 	};
       
   456 
       
   457 enum TSH2Algo
       
   458 	{
       
   459 	E224Bit,
       
   460 	E256Bit,
       
   461 	E384Bit,
       
   462 	E512Bit
       
   463 	};
       
   464 	
       
   465 /**
       
   466  * A SHA-2 message digest
       
   467  * 
       
   468  * SHA-2 comprises of SHA-224, SHA256, SHA384 and SHA512
       
   469  */
       
   470 class CSHA2 : public CMessageDigest
       
   471 	{
       
   472 public:
       
   473 	//NewL & NewLC	
       
   474 	IMPORT_C static CSHA2* NewL(TSH2Algo aAlgorithmId);
       
   475 	IMPORT_C static CSHA2* NewLC(TSH2Algo aAlgorithmId);
       
   476 	
       
   477 	/** Destructor */
       
   478 	IMPORT_C ~CSHA2(void);
       
   479 	
       
   480 	//From CMessageDigest
       
   481 	IMPORT_C CMessageDigest* ReplicateL(void);
       
   482 	IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
       
   483 	IMPORT_C CMessageDigest* CopyL(void);
       
   484 	IMPORT_C TInt BlockSize(void);
       
   485 	IMPORT_C TInt HashSize(void);
       
   486 	IMPORT_C void Reset(void);
       
   487 	IMPORT_C void Update(const TDesC8& aMessage);
       
   488 	IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
       
   489 	IMPORT_C TPtrC8 Final();
       
   490 
       
   491 public:
       
   492 	void RestoreState();
       
   493 	void StoreState();
       
   494 	
       
   495 private:
       
   496 	//Constructors
       
   497 	void ConstructL(TSH2Algo aAlgorithmId);
       
   498 	void ConstructL(const CSHA2& aSHA512);
       
   499 	
       
   500 private:
       
   501 	MSHA2Impl*	iImplementation;
       
   502 	const TAny*	iInitValues;
       
   503 	TSH2Algo	iAlgorithmType;
       
   504 	TUint		iHashSize;
       
   505 	};	
       
   506 	
       
   507 /**
       
   508  * A SHA message digest
       
   509  *
       
   510  * @deprecated Replaced by CSHA1
       
   511  */
       
   512 class CSHA:public CMessageDigest
       
   513 	{
       
   514 	public:
       
   515 		/**
       
   516 		 * Creates a new SHA object.
       
   517 		 *
       
   518 		 * @return	A pointer to the new SHA object
       
   519 		 */
       
   520 		IMPORT_C static CSHA* NewL(void);
       
   521 		IMPORT_C CMessageDigest* ReplicateL(void);
       
   522 		IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
       
   523 		/** Destructor */
       
   524 		IMPORT_C ~CSHA(void);
       
   525 		IMPORT_C CMessageDigest* CopyL(void);
       
   526 		IMPORT_C TInt BlockSize(void);
       
   527 		IMPORT_C TInt HashSize(void);
       
   528 		IMPORT_C void Reset(void);
       
   529 		IMPORT_C void Update(const TDesC8& aMessage);
       
   530 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
       
   531 		IMPORT_C TPtrC8 Final();
       
   532 	public:
       
   533 		void RestoreState();
       
   534 		void StoreState();
       
   535 	};
       
   536 
       
   537 /**
       
   538  * This is the maximum block size currently supported by HMAC implementation.
       
   539  */ 
       
   540 	const TUint KMaxBlockSize=128;
       
   541 
       
   542 /**
       
   543  * An HMAC (Hashed Message Authentication Code)
       
   544  */
       
   545 	class CHMAC:public CMessageDigest
       
   546 
       
   547 	{
       
   548 	public:
       
   549 		/**
       
   550 		 * Creates a new HMAC object from a specified type of message digest 
       
   551 		 * and authentication key.
       
   552 		 * 
       
   553 		 * @param aKey		Authentication key.
       
   554 		 * @param aDigest	A message digest to construct the HMAC from.
       
   555 		 * @return			A pointer to the new CHMAC object. 
       
   556 		 *					The resulting HMAC object takes ownership of aDigest
       
   557 		 *					and is responsible for its deletion.
       
   558 		 */
       
   559 		IMPORT_C static CHMAC* NewL(const TDesC8& aKey,CMessageDigest* aDigest);
       
   560 		IMPORT_C CMessageDigest* ReplicateL(void);
       
   561 		IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
       
   562 		/** Destructor */
       
   563 		IMPORT_C ~CHMAC(void);
       
   564 		IMPORT_C CMessageDigest* CopyL(void);
       
   565 		IMPORT_C TInt BlockSize(void);
       
   566 		IMPORT_C TInt HashSize(void);
       
   567 		IMPORT_C void Reset(void);
       
   568 		IMPORT_C void Update(const TDesC8& aMessage);
       
   569 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
       
   570 		IMPORT_C TPtrC8 Final();
       
   571 	public:
       
   572 		void RestoreState();
       
   573 		void StoreState();
       
   574 	private:
       
   575 		CHMAC(void);
       
   576 		CHMAC(CMessageDigest* aDigest);
       
   577 		CHMAC(const CHMAC& aHMAC);
       
   578 		void InitialiseL(const TDesC8& aKey);
       
   579 		void InitBlockSizeL();
       
   580 		
       
   581 	private:
       
   582 		CMessageDigest* iDigest;
       
   583 		TBuf8<KMaxBlockSize> iInnerPad;
       
   584 		TBuf8<KMaxBlockSize> iOuterPad;
       
   585 		TBuf8<KMaxBlockSize> iInnerPadCopy;
       
   586 		TBuf8<KMaxBlockSize> iOuterPadCopy;		
       
   587 		TInt iBlockSize;
       
   588 	};
       
   589 	
       
   590 /** 
       
   591  * The MD4 block size 
       
   592  * @internalTechnology 
       
   593  */
       
   594  const TUint MD4_LBLOCK=16;
       
   595 
       
   596 /** The size (in bytes) of the MD4 message digest */
       
   597  const TUint MD4_HASH=16;
       
   598 
       
   599 
       
   600 /**
       
   601  * An MD4 message digest Algorithm.
       
   602  *
       
   603  * Takes a message of arbitrary length as input and produces a 128-bit message digest. 
       
   604  *
       
   605  * The total input length of data should not be longer than 2^32 in bits(2^31 in bytes)
       
   606  * which is roughly half a gig.
       
   607  *
       
   608  */
       
   609 class CMD4:public CMessageDigest
       
   610 	{
       
   611 	public:
       
   612 		/**
       
   613 		 * Creates a new MD4 object.
       
   614 		 *
       
   615 		 * @return	A pointer to the new CMD4 object
       
   616 		 */
       
   617 		IMPORT_C static CMD4* NewL(void);
       
   618 		IMPORT_C CMessageDigest* ReplicateL(void);
       
   619 		IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
       
   620 		/** Destructor */
       
   621 		IMPORT_C ~CMD4(void);
       
   622 		IMPORT_C CMessageDigest* CopyL(void);
       
   623 		IMPORT_C TInt BlockSize(void);
       
   624 		IMPORT_C TInt HashSize(void);
       
   625 		IMPORT_C void Reset(void);
       
   626 		IMPORT_C void Update(const TDesC8& aMessage);
       
   627 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
       
   628 		IMPORT_C TPtrC8 Final();
       
   629 	public:
       
   630 		virtual void RestoreState();
       
   631 		virtual void StoreState();
       
   632 	private:
       
   633 		CMD4(void);
       
   634 		CMD4(const CMD4& aMD4);
       
   635 	private:
       
   636 	   /**
       
   637 		* Divides the message into blocks of 512 bits and performs the
       
   638 		* Block operation on them.
       
   639 		*/
       
   640 		void DoUpdate(const TUint8* aData,TUint aLength);
       
   641 	   /**
       
   642 		* Performs the Block operation on the last 512 bit block.
       
   643 		* This function does the padding on the last 512 bit block
       
   644 		* and also appends the length of the message to the last 64-bits
       
   645 		* of the block.
       
   646 		*/
       
   647 		void DoFinal(void);
       
   648 	   /**
       
   649 		* Performs the Block operation on the 512 bit blocks
       
   650 		*/
       
   651 		void Block();
       
   652 	private:
       
   653 		/*Holds the generated 128-bit Message Digest*/
       
   654 		TBuf8<MD4_HASH> iHash;
       
   655 	private:
       
   656 		TUint iA;
       
   657 		TUint iB;
       
   658 		TUint iC;
       
   659 		TUint iD;
       
   660 		TUint iNl;
       
   661 		TUint iNh;
       
   662 		TUint iData[MD4_LBLOCK];
       
   663 	private:
       
   664 		TUint iACopy;
       
   665 		TUint iBCopy;
       
   666 		TUint iCCopy;
       
   667 		TUint iDCopy;
       
   668 		TUint iNlCopy;
       
   669 		TUint iNhCopy;
       
   670 		TUint iDataCopy[MD4_LBLOCK];
       
   671 	};
       
   672 
       
   673 
       
   674 /**
       
   675  *	Factory to create a CMessageDigest derived object according to the identity of the hash algorithm.
       
   676  */
       
   677 class CMessageDigestFactory : public CBase
       
   678 {
       
   679 public:
       
   680 	/**
       
   681 	 * Creates a CMessageDigest derived object according to the specified type of hash algorithm.
       
   682 	 *
       
   683 	 * @param aHashId	The identity of the hash algorithm
       
   684 	 * @return			A pointer to a CMessageDigest object
       
   685 	 */
       
   686 	IMPORT_C static CMessageDigest* NewDigestL(CMessageDigest::THashId aHashId);
       
   687 
       
   688 	/**
       
   689 	 * Creates a CMessageDigest derived object according to the specified type of hash algorithm.
       
   690 	 * 
       
   691 	 * The returned pointer is put onto the cleanup stack.
       
   692 	 *
       
   693 	 * @param aHashId	The identity of the hash algorithm
       
   694 	 * @return			A pointer to a CMessageDigest object
       
   695 	 */
       
   696 	IMPORT_C static CMessageDigest* NewDigestLC(CMessageDigest::THashId aHashId);
       
   697 
       
   698 	/**
       
   699 	 * Creates a CMessageDigest derived object according to the specified type of hash algorithm
       
   700 	 * and authentication key.
       
   701 	 *
       
   702 	 * @param aHashId	The identity of the hash algorithm
       
   703 	 * @param aKey		The authentication key 
       
   704 	 * @return			A pointer to a CMessageDigest object
       
   705 	 */
       
   706 	IMPORT_C static CMessageDigest* NewHMACL(CMessageDigest::THashId aHashId, const TDesC8& aKey);
       
   707 
       
   708 	/**
       
   709 	 * Creates a CMessageDigest derived object according to the specified type of hash algorithm
       
   710 	 * and authentication key.
       
   711 	 * 
       
   712 	 * The returned pointer is put onto the cleanup stack.
       
   713 	 *
       
   714 	 * @param aHashId	The identity of the hash algorithm
       
   715 	 * @param aKey		The authentication key 
       
   716 	 * @return			A pointer to a CMessageDigest object
       
   717 	 */
       
   718 	IMPORT_C static CMessageDigest* NewHMACLC(CMessageDigest::THashId aHashId, const TDesC8& aKey);
       
   719 };
       
   720 
       
   721 #endif // __HASH_H__