First public contribution.
2 * Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
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
34 //Forward Declarations
38 * Base class for message digests.
40 class CMessageDigest:public CBase
44 * Enumeration to identify hash functions (aka message-digest algorithms).
49 * Message Digest algorithm developed by Rivest for digital signature
50 * applications (and optimized for 8-bit machines).
52 * Takes a message of arbitrary length and produces a 128-bit message digest.
58 * Message Digest algorithm developed by Rivest for digital signature
59 * applications (and optimized for 32-bit machines).
61 * Takes a message of arbitrary length and produces a 128-bit message digest.
67 * Secure Hash Algorithm (version 1) is a message digest algorithm developed by
68 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
70 * It takes a message of less than 2^64 bits in length and produces
71 * a 160-bit message digest.
73 * See FIPS 180-1 and RFC 3174
77 * HMAC - Hash function based Message Authentication Code is a mechanism
78 * for message authentication using cryptographic hash functions. (A checksum.)
80 * It can be used with any iterative cryptographic hash function,
81 * e.g., MD5, SHA-1, in combination with a secret shared key
82 * to produce a checksum that is appended to the message.
83 * The cryptographic strength of HMAC depends on the properties
84 * of the underlying hash function.
90 * Message Digest algorithm developed by Rivest for digital signature
91 * applications (and optimized for 32-bit machines).
93 * Takes a message of arbitrary length and produces a 128-bit message digest.
99 * Secure Hash Algorithm - 224 (version 2) is a message digest algorithm developed by
100 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
102 * It takes a message of less than 2^64 bits in length and produces
103 * a 224-bit message digest.
105 * See FIPS 180-2 (with change notice), RFC3874 and FIPS 180-3
109 * Secure Hash Algorithm - 256 (version 2) is a message digest algorithm developed by
110 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
112 * It takes a message of less than 2^64 bits in length and produces
113 * a 256-bit message digest.
115 * See FIPS 180-2 and RFC 4634
119 * Secure Hash Algorithm - 384 (version 2) is a message digest algorithm developed by
120 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
122 * It takes a message of less than 2^128 bits in length and produces
123 * a 384-bit message digest.
125 * See FIPS 180-2 and RFC 4634
129 * Secure Hash Algorithm - 512 (version 2) is a message digest algorithm developed by
130 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
132 * It takes a message of less than 2^128 bits in length and produces
133 * a 512-bit message digest.
135 * See FIPS 180-2 and RFC 4634
142 * Creates a brand new reset CMessageDigest object containing no state
143 * information from the current object.
145 * To make a copy of a message digest with its internal state intact,
148 * @return A pointer to the new reset CMessageDigest object
150 IMPORT_C virtual CMessageDigest* ReplicateL(void)=0;
153 * Adds aMessage to the internal representation of data to be hashed,
154 * then returns a TPtrC8 of the finalised hash of all the previously
157 * @param aMessage Data to be included in the hash.
158 * @return A descriptor pointer to the buffer containing the
161 IMPORT_C virtual TPtrC8 Hash(const TDesC8& aMessage)=0;
164 * Creates a new CMessageDigest object with the exact same state as
165 * the current object.
167 * This function copies all internal state of the message digest.
168 * To create a new CMessageDigest object without the state of
169 * the current object, see ReplicateL().
171 * @return A pointer to the new CMessageDigest object
173 IMPORT_C virtual CMessageDigest* CopyL(void)=0;
176 * Gets the internal block size of the message digest.
178 * @return Internal block size of message digest in bytes.
180 IMPORT_C virtual TInt BlockSize(void)=0;
183 * Gets the size of the message digest output.
185 * @return Output size of the message digest in bytes.
187 IMPORT_C virtual TInt HashSize(void)=0;
190 * Resets the internal state of the message digest.
192 * A reset hash object loses all internal state representing the hashed
193 * data. A reset message digest is suitable to begin a new, distinct hash
194 * of different data. Any previously returned TPtrC8 from a call to
195 * Final() remains valid until any subsequent call to Update() or
198 IMPORT_C virtual void Reset(void)=0;
203 IMPORT_C ~CMessageDigest(void);
206 * Adds data to the internal representation of messages to be hashed.
208 * @param aMessage Data to be included in the hash.
211 IMPORT_C virtual void Update(const TDesC8& aMessage)=0;
214 * Adds aMessage to the internal representation of data to be hashed,
215 * returns a TPtrC8 of the finalised hash of all the previously
216 * appended messages, and calls Reset().
218 * @param aMessage Data to be included in the hash
219 * @return A descriptor pointer to the buffer containing the
223 IMPORT_C virtual TPtrC8 Final(const TDesC8& aMessage)=0;
226 * Gets a TPtrC8 of the finalised hash of all the previously
227 * appended messages and then calls Reset().
229 * @return A descriptor pointer to the buffer containing the
233 IMPORT_C virtual TPtrC8 Final(void)=0;
236 * Restores the internal state of the message digest
237 * to a previously stored state.
241 virtual void RestoreState() = 0;
244 * Stores the internal state of the message digest.
246 virtual void StoreState() = 0;
251 IMPORT_C CMessageDigest(void);
256 * @param aMD A CMessageDigest object
258 IMPORT_C CMessageDigest(const CMessageDigest& aMD);
262 * The MD2 block size (in bytes)
265 const TInt MD2_BLOCK=16;
267 /** The size (in bytes) of the MD2 message digest */
268 const TInt MD2_HASH=16;
271 * An MD2 message digest
273 class CMD2:public CMessageDigest
278 * Creates a new MD2 object.
280 * @return A pointer to the new CMD2 object
282 IMPORT_C static CMD2* NewL(void);
283 IMPORT_C CMessageDigest* ReplicateL(void);
284 IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
286 IMPORT_C ~CMD2(void);
287 IMPORT_C CMessageDigest* CopyL(void);
288 IMPORT_C TInt BlockSize(void);
289 IMPORT_C TInt HashSize(void);
290 IMPORT_C void Reset(void);
291 IMPORT_C void Update(const TDesC8& aMessage);
292 IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
293 IMPORT_C TPtrC8 Final();
298 void DoUpdate(const TUint8* aData,TUint aLength);
300 void Block(const TUint8* aData);
303 CMD2(const CMD2& aMD2);
305 TBuf8<MD2_HASH> iHash;
307 TUint8 iData[MD2_BLOCK];
308 TUint iChecksum[MD2_BLOCK];
309 TUint iState[MD2_BLOCK];
311 TBuf8<MD2_HASH> iHashBuf;
312 TUint8 iDataTemp[MD2_BLOCK];
313 TUint iChecksumTemp[MD2_BLOCK];
314 TUint iStateTemp[MD2_BLOCK];
318 * The MD5 block size (in bytes)
321 const TUint MD5_LBLOCK=16;
323 /** The size (in bytes) of the MD5 message digest */
324 const TUint MD5_HASH=16;
327 * An MD5 message digest
329 * Takes a message of arbitrary length as input and produces a 128-bit message digest.
331 * The length of input data should not be longer than 2^32 in bits(2^31 in bytes)
332 * which is roughly half a gig.
335 class CMD5:public CMessageDigest
339 * Creates a new MD5 object.
341 * @return A pointer to the new CMD5 object
343 IMPORT_C static CMD5* NewL(void);
344 IMPORT_C CMessageDigest* ReplicateL(void);
345 IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
347 IMPORT_C ~CMD5(void);
348 IMPORT_C CMessageDigest* CopyL(void);
349 IMPORT_C TInt BlockSize(void);
350 IMPORT_C TInt HashSize(void);
351 IMPORT_C void Reset(void);
352 IMPORT_C void Update(const TDesC8& aMessage);
353 IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
354 IMPORT_C TPtrC8 Final();
360 CMD5(const CMD5& aMD5);
362 void DoUpdate(const TUint8* aData,TUint aLength);
366 TBuf8<MD5_HASH> iHash;
374 TUint iData[MD5_LBLOCK];
382 TUint iDataCopy[MD5_LBLOCK];
386 * The SHA-1 block size (in bytes)
389 const TUint SHA1_LBLOCK=16;
391 /** The size (in bytes) of the SHA-1 message digest */
392 const TUint SHA1_HASH=20;
394 /** The size (in bytes) of the SHA message digest */
395 const TUint SHA_HASH=SHA1_HASH;
398 * A SHA-1 message digest
400 class CSHA1:public CMessageDigest
404 * Creates a new SHA-1 object.
406 * @return A pointer to the new SHA-1 object
408 IMPORT_C static CSHA1* NewL(void);
409 IMPORT_C CMessageDigest* ReplicateL(void);
410 IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
412 IMPORT_C ~CSHA1(void);
413 IMPORT_C CMessageDigest* CopyL(void);
414 IMPORT_C TInt BlockSize(void);
415 IMPORT_C TInt HashSize(void);
416 IMPORT_C void Reset(void);
417 IMPORT_C void Update(const TDesC8& aMessage);
418 IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
419 IMPORT_C TPtrC8 Final();
425 CSHA1(const CSHA1& aSHA1);
426 void ConstructL(void);
428 void DoUpdate(const TUint8* aData,TUint aLength);
432 TBuf8<SHA1_HASH> iHash;
440 TUint iData[SHA1_LBLOCK*5];
449 TUint iDataCopy[SHA1_LBLOCK*5];
461 * A SHA-2 message digest
463 * SHA-2 comprises of SHA-224, SHA256, SHA384 and SHA512
465 class CSHA2 : public CMessageDigest
469 IMPORT_C static CSHA2* NewL(TSH2Algo aAlgorithmId);
470 IMPORT_C static CSHA2* NewLC(TSH2Algo aAlgorithmId);
473 IMPORT_C ~CSHA2(void);
475 //From CMessageDigest
476 IMPORT_C CMessageDigest* ReplicateL(void);
477 IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
478 IMPORT_C CMessageDigest* CopyL(void);
479 IMPORT_C TInt BlockSize(void);
480 IMPORT_C TInt HashSize(void);
481 IMPORT_C void Reset(void);
482 IMPORT_C void Update(const TDesC8& aMessage);
483 IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
484 IMPORT_C TPtrC8 Final();
492 void ConstructL(TSH2Algo aAlgorithmId);
493 void ConstructL(const CSHA2& aSHA512);
496 MSHA2Impl* iImplementation;
497 const TAny* iInitValues;
498 TSH2Algo iAlgorithmType;
503 * A SHA message digest
505 * @deprecated Replaced by CSHA1
507 class CSHA:public CMessageDigest
511 * Creates a new SHA object.
513 * @return A pointer to the new SHA object
515 IMPORT_C static CSHA* NewL(void);
516 IMPORT_C CMessageDigest* ReplicateL(void);
517 IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
519 IMPORT_C ~CSHA(void);
520 IMPORT_C CMessageDigest* CopyL(void);
521 IMPORT_C TInt BlockSize(void);
522 IMPORT_C TInt HashSize(void);
523 IMPORT_C void Reset(void);
524 IMPORT_C void Update(const TDesC8& aMessage);
525 IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
526 IMPORT_C TPtrC8 Final();
533 * This is the maximum block size currently supported by HMAC implementation.
535 const TUint KMaxBlockSize=128;
538 * An HMAC (Hashed Message Authentication Code)
540 class CHMAC:public CMessageDigest
545 * Creates a new HMAC object from a specified type of message digest
546 * and authentication key.
548 * @param aKey Authentication key.
549 * @param aDigest A message digest to construct the HMAC from.
550 * @return A pointer to the new CHMAC object.
551 * The resulting HMAC object takes ownership of aDigest
552 * and is responsible for its deletion.
554 IMPORT_C static CHMAC* NewL(const TDesC8& aKey,CMessageDigest* aDigest);
555 IMPORT_C CMessageDigest* ReplicateL(void);
556 IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
558 IMPORT_C ~CHMAC(void);
559 IMPORT_C CMessageDigest* CopyL(void);
560 IMPORT_C TInt BlockSize(void);
561 IMPORT_C TInt HashSize(void);
562 IMPORT_C void Reset(void);
563 IMPORT_C void Update(const TDesC8& aMessage);
564 IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
565 IMPORT_C TPtrC8 Final();
571 CHMAC(CMessageDigest* aDigest);
572 CHMAC(const CHMAC& aHMAC);
573 void InitialiseL(const TDesC8& aKey);
574 void InitBlockSizeL();
577 CMessageDigest* iDigest;
578 TBuf8<KMaxBlockSize> iInnerPad;
579 TBuf8<KMaxBlockSize> iOuterPad;
580 TBuf8<KMaxBlockSize> iInnerPadCopy;
581 TBuf8<KMaxBlockSize> iOuterPadCopy;
588 const TUint MD4_LBLOCK=16;
590 /** The size (in bytes) of the MD4 message digest */
591 const TUint MD4_HASH=16;
595 * An MD4 message digest Algorithm.
597 * Takes a message of arbitrary length as input and produces a 128-bit message digest.
599 * The total input length of data should not be longer than 2^32 in bits(2^31 in bytes)
600 * which is roughly half a gig.
603 class CMD4:public CMessageDigest
607 * Creates a new MD4 object.
609 * @return A pointer to the new CMD4 object
611 IMPORT_C static CMD4* NewL(void);
612 IMPORT_C CMessageDigest* ReplicateL(void);
613 IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
615 IMPORT_C ~CMD4(void);
616 IMPORT_C CMessageDigest* CopyL(void);
617 IMPORT_C TInt BlockSize(void);
618 IMPORT_C TInt HashSize(void);
619 IMPORT_C void Reset(void);
620 IMPORT_C void Update(const TDesC8& aMessage);
621 IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
622 IMPORT_C TPtrC8 Final();
624 virtual void RestoreState();
625 virtual void StoreState();
628 CMD4(const CMD4& aMD4);
631 * Divides the message into blocks of 512 bits and performs the
632 * Block operation on them.
634 void DoUpdate(const TUint8* aData,TUint aLength);
636 * Performs the Block operation on the last 512 bit block.
637 * This function does the padding on the last 512 bit block
638 * and also appends the length of the message to the last 64-bits
643 * Performs the Block operation on the 512 bit blocks
647 /*Holds the generated 128-bit Message Digest*/
648 TBuf8<MD4_HASH> iHash;
656 TUint iData[MD4_LBLOCK];
664 TUint iDataCopy[MD4_LBLOCK];
669 * Factory to create a CMessageDigest derived object according to the identity of the hash algorithm.
671 class CMessageDigestFactory : public CBase
675 * Creates a CMessageDigest derived object according to the specified type of hash algorithm.
677 * @param aHashId The identity of the hash algorithm
678 * @return A pointer to a CMessageDigest object
680 IMPORT_C static CMessageDigest* NewDigestL(CMessageDigest::THashId aHashId);
683 * Creates a CMessageDigest derived object according to the specified type of hash algorithm.
685 * The returned pointer is put onto the cleanup stack.
687 * @param aHashId The identity of the hash algorithm
688 * @return A pointer to a CMessageDigest object
690 IMPORT_C static CMessageDigest* NewDigestLC(CMessageDigest::THashId aHashId);
693 * Creates a CMessageDigest derived object according to the specified type of hash algorithm
694 * and authentication key.
696 * @param aHashId The identity of the hash algorithm
697 * @param aKey The authentication key
698 * @return A pointer to a CMessageDigest object
700 IMPORT_C static CMessageDigest* NewHMACL(CMessageDigest::THashId aHashId, const TDesC8& aKey);
703 * Creates a CMessageDigest derived object according to the specified type of hash algorithm
704 * and authentication key.
706 * The returned pointer is put onto the cleanup stack.
708 * @param aHashId The identity of the hash algorithm
709 * @param aKey The authentication key
710 * @return A pointer to a CMessageDigest object
712 IMPORT_C static CMessageDigest* NewHMACLC(CMessageDigest::THashId aHashId, const TDesC8& aKey);