epoc32/include/hash.h
branchSymbian3
changeset 4 837f303aceeb
parent 2 2fe1408b6811
equal deleted inserted replaced
3:e1b950c65cb4 4:837f303aceeb
    17 * This header contains the definition of the message digest classes
    17 * This header contains the definition of the message digest classes
    18 *
    18 *
    19 */
    19 */
    20 
    20 
    21 
    21 
    22 
       
    23 
       
    24 /**
    22 /**
    25  @file 
    23  @file 
    26  @publishedAll
    24  @publishedAll
    27  @released
    25  @released
    28 */
    26 */
    29 
    27 
    30 #ifndef __HASH_H__
    28 #ifndef __HASH_H__
    31 #define __HASH_H__
    29 #define __HASH_H__
    32 
    30 
    33 #include <e32base.h>
    31 #include <e32base.h>
    34 
       
    35 
       
    36 //Forward Declarations
       
    37 class MSHA2Impl;
       
    38 
    32 
    39 /**
    33 /**
    40  * Base class for message digests.
    34  * Base class for message digests.
    41  */
    35  */
    42 class CMessageDigest:public CBase
    36 class CMessageDigest:public CBase
   244 
   238 
   245 		/**
   239 		/**
   246 		 * Stores the internal state of the message digest. 
   240 		 * Stores the internal state of the message digest. 
   247 		 */
   241 		 */
   248 		virtual void StoreState() = 0;
   242 		virtual void StoreState() = 0;
       
   243 
       
   244 		/**
       
   245 		@internalComponent
       
   246 		Used to retrieve the extended interfaces extension
       
   247 		*/		
       
   248 		TInt GetExtension(TUint aExtensionId, TAny*& a0, TAny* a1);		
       
   249 
   249 	protected:
   250 	protected:
   250 		/**
   251 		/**
   251 		 * Constructor
   252 		 * Constructor
   252 		 */
   253 		 */
   253 		IMPORT_C CMessageDigest(void);
   254 		IMPORT_C CMessageDigest(void);
   259 		 */
   260 		 */
   260 		IMPORT_C CMessageDigest(const CMessageDigest& aMD);
   261 		IMPORT_C CMessageDigest(const CMessageDigest& aMD);
   261 	};
   262 	};
   262 
   263 
   263 /** 
   264 /** 
   264  * The MD2 block size (in bytes) 
   265 The MD2 block size (in bytes) 
   265  * 
   266 */
   266  * @internalTechnology 
       
   267  */
       
   268 const TInt MD2_BLOCK=16;
   267 const TInt MD2_BLOCK=16;
   269 
   268 
   270 /** The size (in bytes) of the MD2 message digest */
   269 /** 
       
   270 The size (in bytes) of the MD2 message digest 
       
   271 */
   271 const TInt MD2_HASH=16;
   272 const TInt MD2_HASH=16;
   272 
   273 
   273 /**
   274 /**
   274  * An MD2 message digest
   275  * An MD2 message digest
   275  */
   276  */
   295 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
   296 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
   296 		IMPORT_C TPtrC8 Final();
   297 		IMPORT_C TPtrC8 Final();
   297 	public:
   298 	public:
   298 		void RestoreState();
   299 		void RestoreState();
   299 		void StoreState();
   300 		void StoreState();
   300 	private:
   301 	protected:	
   301 		void DoUpdate(const TUint8* aData,TUint aLength);
   302 		/** @internalComponent */
   302 		void DoFinal(void);
       
   303 		void Block(const TUint8* aData);
       
   304 	private:
       
   305 		CMD2(void);
   303 		CMD2(void);
   306 		CMD2(const CMD2& aMD2);
   304 	};
   307 	private:
   305 
   308 		TBuf8<MD2_HASH> iHash;
   306 /** 
   309 		TInt iNum;
   307 The MD5 block size (in bytes)
   310 		TUint8 iData[MD2_BLOCK];
   308 */
   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;
   309 const TUint MD5_LBLOCK=16;
   326 
   310 
   327 /** The size (in bytes) of the MD5 message digest */
   311 /** 
       
   312 The size (in bytes) of the MD5 message digest 
       
   313 */
   328 const TUint MD5_HASH=16;
   314 const TUint MD5_HASH=16;
   329 
   315 
   330 /**
   316 /**
   331  * An MD5 message digest
   317  * An MD5 message digest
   332  *
   318  *
   333  * Takes a message of arbitrary length as input and produces a 128-bit message digest. 
   319  * Takes a message of arbitrary length as input and produces a 128-bit message digest. 
   334  *
   320  * 
   335  * The length of input data should not be longer than 2^32 in bits(2^31 in bytes)
   321  * The total input length of data should not be longer than 2^32 in bits(2^31 in bytes)
   336  * which is roughly half a gig.
   322  * which is roughly half a gig.
   337  *
   323  *
   338  */
   324  */
   339 class CMD5:public CMessageDigest
   325 class CMD5:public CMessageDigest
   340 	{
   326 	{
   357 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
   343 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
   358 		IMPORT_C TPtrC8 Final();
   344 		IMPORT_C TPtrC8 Final();
   359 	public:
   345 	public:
   360 		void RestoreState();
   346 		void RestoreState();
   361 		void StoreState();
   347 		void StoreState();
   362 	private:
   348 	protected:
       
   349 		/** @internalComponent */
   363 		CMD5(void);
   350 		CMD5(void);
   364 		CMD5(const CMD5& aMD5);
   351 	};
   365 	private:
   352 
   366 		void DoUpdate(const TUint8* aData,TUint aLength);
   353 
   367 		void DoFinal(void);
   354 /** 
   368 		void Block();
   355 The SHA-1 block size (in bytes) 
   369 	private:
   356 */
   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;
   357 const TUint SHA1_LBLOCK=16;
   395 
   358 
   396 /** The size (in bytes) of the SHA-1 message digest */
   359 /** 
       
   360 The size (in bytes) of the SHA-1 message digest 
       
   361 */
   397 const TUint SHA1_HASH=20;
   362 const TUint SHA1_HASH=20;
   398 
   363 
   399 /** The size (in bytes) of the SHA message digest */
   364 /** 
       
   365 The size (in bytes) of the SHA message digest 
       
   366 */
   400 const TUint SHA_HASH=SHA1_HASH;
   367 const TUint SHA_HASH=SHA1_HASH;
   401 
   368 
   402 /**
   369 /**
   403  * A SHA-1 message digest
   370  * A SHA-1 message digest
   404  */
   371  */
   423 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
   390 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
   424 		IMPORT_C TPtrC8 Final();
   391 		IMPORT_C TPtrC8 Final();
   425 	public:
   392 	public:
   426 		void RestoreState();
   393 		void RestoreState();
   427 		void StoreState();
   394 		void StoreState();
   428 	private:
   395 	protected:
       
   396 		/** @internalComponent */
   429 		CSHA1(void);
   397 		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 	};
   398 	};
   456 
   399 
   457 enum TSH2Algo
   400 enum TSH2Algo
   458 	{
   401 	{
   459 	E224Bit,
   402 	E224Bit,
   463 	};
   406 	};
   464 	
   407 	
   465 /**
   408 /**
   466  * A SHA-2 message digest
   409  * A SHA-2 message digest
   467  * 
   410  * 
   468  * SHA-2 comprises of SHA-224, SHA256, SHA384 and SHA512
   411  * SHA-2 is comprised of SHA-224, SHA256, SHA384 and SHA512
   469  */
   412  */
   470 class CSHA2 : public CMessageDigest
   413 class CSHA2 : public CMessageDigest
   471 	{
   414 	{
   472 public:
   415 	public:
   473 	//NewL & NewLC	
   416 		/**
   474 	IMPORT_C static CSHA2* NewL(TSH2Algo aAlgorithmId);
   417 		 * Creates a new SHA-1 object.
   475 	IMPORT_C static CSHA2* NewLC(TSH2Algo aAlgorithmId);
   418 		 *
   476 	
   419 		 * @return	A pointer to the new SHA-1 object
   477 	/** Destructor */
   420 		 */
   478 	IMPORT_C ~CSHA2(void);
   421 		IMPORT_C static CSHA2* NewL(TSH2Algo aAlgorithmId);
   479 	
   422 		IMPORT_C static CSHA2* NewLC(TSH2Algo aAlgorithmId);
   480 	//From CMessageDigest
   423 		/** Destructor */
   481 	IMPORT_C CMessageDigest* ReplicateL(void);
   424 		IMPORT_C ~CSHA2(void);
   482 	IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
   425 	public:
   483 	IMPORT_C CMessageDigest* CopyL(void);
   426 		void RestoreState();
   484 	IMPORT_C TInt BlockSize(void);
   427 		void StoreState();		
   485 	IMPORT_C TInt HashSize(void);
   428 	protected:
   486 	IMPORT_C void Reset(void);
   429 		/** @internalComponent */
   487 	IMPORT_C void Update(const TDesC8& aMessage);
   430 		CSHA2(void);
   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 	};	
   431 	};	
   506 	
   432 
       
   433 
   507 /**
   434 /**
   508  * A SHA message digest
   435  * A SHA message digest
   509  *
   436  *
   510  * @deprecated Replaced by CSHA1
   437  * @deprecated Replaced by CSHA1
   511  */
   438  */
   535 	};
   462 	};
   536 
   463 
   537 /**
   464 /**
   538  * This is the maximum block size currently supported by HMAC implementation.
   465  * This is the maximum block size currently supported by HMAC implementation.
   539  */ 
   466  */ 
   540 	const TUint KMaxBlockSize=128;
   467 const TUint KMaxBlockSize=128;
   541 
   468 
   542 /**
   469 /**
   543  * An HMAC (Hashed Message Authentication Code)
   470  * An HMAC (Hashed Message Authentication Code)
   544  */
   471  */
   545 	class CHMAC:public CMessageDigest
   472 class CHMAC:public CMessageDigest
   546 
   473 
   547 	{
   474 	{
   548 	public:
   475 	public:
   549 		/**
   476 		/**
   550 		 * Creates a new HMAC object from a specified type of message digest 
   477 		 * Creates a new HMAC object from a specified type of message digest 
   569 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
   496 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
   570 		IMPORT_C TPtrC8 Final();
   497 		IMPORT_C TPtrC8 Final();
   571 	public:
   498 	public:
   572 		void RestoreState();
   499 		void RestoreState();
   573 		void StoreState();
   500 		void StoreState();
       
   501 	protected:
       
   502 		/** @internalComponent */
       
   503 		CHMAC(void);
       
   504 		/** @internalComponent */
       
   505 		CHMAC(CMessageDigest* aDigest);
       
   506 		/** @internalComponent */
       
   507 		CHMAC(const CHMAC& aHMAC);
       
   508 		/** @internalComponent */
       
   509 		void InitialiseL(const TDesC8& aKey);
       
   510 		
   574 	private:
   511 	private:
   575 		CHMAC(void);
       
   576 		CHMAC(CMessageDigest* aDigest);
       
   577 		CHMAC(const CHMAC& aHMAC);
       
   578 		void InitialiseL(const TDesC8& aKey);
       
   579 		void InitBlockSizeL();
   512 		void InitBlockSizeL();
   580 		
   513 
   581 	private:
   514 	private:
   582 		CMessageDigest* iDigest;
   515 		CMessageDigest* iDigest;
   583 		TBuf8<KMaxBlockSize> iInnerPad;
   516 		TBuf8<KMaxBlockSize> iInnerPad;
   584 		TBuf8<KMaxBlockSize> iOuterPad;
   517 		TBuf8<KMaxBlockSize> iOuterPad;
   585 		TBuf8<KMaxBlockSize> iInnerPadCopy;
   518 		TBuf8<KMaxBlockSize> iInnerPadCopy;
   586 		TBuf8<KMaxBlockSize> iOuterPadCopy;		
   519 		TBuf8<KMaxBlockSize> iOuterPadCopy;		
   587 		TInt iBlockSize;
   520 		TInt iBlockSize;
   588 	};
   521 
   589 	
   522 	};
   590 /** 
   523 
   591  * The MD4 block size 
   524 /** 
   592  * @internalTechnology 
   525 The MD4 block size (in bytes)
   593  */
   526 */
   594  const TUint MD4_LBLOCK=16;
   527 const TUint MD4_LBLOCK=16;
   595 
   528 
   596 /** The size (in bytes) of the MD4 message digest */
   529 /** 
   597  const TUint MD4_HASH=16;
   530 The size (in bytes) of the MD4 message digest 
   598 
   531 */
       
   532 const TUint MD4_HASH=16;
   599 
   533 
   600 /**
   534 /**
   601  * An MD4 message digest Algorithm.
   535  * An MD4 message digest Algorithm.
   602  *
       
   603  * Takes a message of arbitrary length as input and produces a 128-bit message digest. 
   536  * Takes a message of arbitrary length as input and produces a 128-bit message digest. 
   604  *
   537  * 
   605  * The total input length of data should not be longer than 2^32 in bits(2^31 in bytes)
   538  * 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.
   539  * which is roughly half a gig.
   607  *
   540  *
   608  */
   541  */
   609 class CMD4:public CMessageDigest
   542 class CMD4:public CMessageDigest
   627 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
   560 		IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
   628 		IMPORT_C TPtrC8 Final();
   561 		IMPORT_C TPtrC8 Final();
   629 	public:
   562 	public:
   630 		virtual void RestoreState();
   563 		virtual void RestoreState();
   631 		virtual void StoreState();
   564 		virtual void StoreState();
   632 	private:
   565 	protected:
       
   566 		/** @internalComponent */
   633 		CMD4(void);
   567 		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 	};
   568 	};
   672 
   569 
   673 
   570 
   674 /**
   571 /**
   675  *	Factory to create a CMessageDigest derived object according to the identity of the hash algorithm.
   572  *	Factory to create a CMessageDigest derived object according to the identity of the hash algorithm.
   676  */
   573  */
   677 class CMessageDigestFactory : public CBase
   574 class CMessageDigestFactory : public CBase
   678 {
   575 {
   679 public:
   576 public:
   680 	/**
   577 	/**
   681 	 * Creates a CMessageDigest derived object according to the specified type of hash algorithm.
   578 	 *Creates a CMessageDigest derived object according to the specified type of hash algorithm.
   682 	 *
   579 	 *
   683 	 * @param aHashId	The identity of the hash algorithm
   580 	 * @param aHashId	The identity of the hash algorithm
   684 	 * @return			A pointer to a CMessageDigest object
   581 	 * @return			A pointer to a CMessageDigest object
   685 	 */
   582 	 */
   686 	IMPORT_C static CMessageDigest* NewDigestL(CMessageDigest::THashId aHashId);
   583 	IMPORT_C static CMessageDigest* NewDigestL(CMessageDigest::THashId aHashId);