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