sl@0: /* sl@0: * Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * ** IMPORTANT ** PublishedPartner API's in this file are published to 3rd party developers via the sl@0: * Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted. sl@0: * This header contains the definition of the message digest classes sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: @publishedAll sl@0: @released sl@0: */ sl@0: sl@0: #ifndef __HASH_H__ sl@0: #define __HASH_H__ sl@0: sl@0: #include sl@0: sl@0: /** sl@0: * Base class for message digests. sl@0: */ sl@0: class CMessageDigest:public CBase sl@0: { sl@0: public: sl@0: /** sl@0: * Enumeration to identify hash functions (aka message-digest algorithms). sl@0: */ sl@0: enum THashId sl@0: { sl@0: /** sl@0: * Message Digest algorithm developed by Rivest for digital signature sl@0: * applications (and optimized for 8-bit machines). sl@0: * sl@0: * Takes a message of arbitrary length and produces a 128-bit message digest. sl@0: * sl@0: * See RFC 1319 sl@0: */ sl@0: EMD2, sl@0: /** sl@0: * Message Digest algorithm developed by Rivest for digital signature sl@0: * applications (and optimized for 32-bit machines). sl@0: * sl@0: * Takes a message of arbitrary length and produces a 128-bit message digest. sl@0: * sl@0: * See RFC 1321 sl@0: */ sl@0: EMD5, sl@0: /** sl@0: * Secure Hash Algorithm (version 1) is a message digest algorithm developed by sl@0: * NIST, along with the NSA, for use with the Digital Signature Standard (DSS). sl@0: * sl@0: * It takes a message of less than 2^64 bits in length and produces sl@0: * a 160-bit message digest. sl@0: * sl@0: * See FIPS 180-1 and RFC 3174 sl@0: */ sl@0: ESHA1, sl@0: /** sl@0: * HMAC - Hash function based Message Authentication Code is a mechanism sl@0: * for message authentication using cryptographic hash functions. (A checksum.) sl@0: * sl@0: * It can be used with any iterative cryptographic hash function, sl@0: * e.g., MD5, SHA-1, in combination with a secret shared key sl@0: * to produce a checksum that is appended to the message. sl@0: * The cryptographic strength of HMAC depends on the properties sl@0: * of the underlying hash function. sl@0: * sl@0: * See RFC 2104 sl@0: */ sl@0: HMAC, sl@0: /** sl@0: * Message Digest algorithm developed by Rivest for digital signature sl@0: * applications (and optimized for 32-bit machines). sl@0: * sl@0: * Takes a message of arbitrary length and produces a 128-bit message digest. sl@0: * sl@0: * See RFC 1320 sl@0: */ sl@0: EMD4, sl@0: /** sl@0: * Secure Hash Algorithm - 224 (version 2) is a message digest algorithm developed by sl@0: * NIST, along with the NSA, for use with the Digital Signature Standard (DSS). sl@0: * sl@0: * It takes a message of less than 2^64 bits in length and produces sl@0: * a 224-bit message digest. sl@0: * sl@0: * See FIPS 180-2 (with change notice), RFC3874 and FIPS 180-3 sl@0: */ sl@0: ESHA224, sl@0: /** sl@0: * Secure Hash Algorithm - 256 (version 2) is a message digest algorithm developed by sl@0: * NIST, along with the NSA, for use with the Digital Signature Standard (DSS). sl@0: * sl@0: * It takes a message of less than 2^64 bits in length and produces sl@0: * a 256-bit message digest. sl@0: * sl@0: * See FIPS 180-2 and RFC 4634 sl@0: */ sl@0: ESHA256, sl@0: /** sl@0: * Secure Hash Algorithm - 384 (version 2) is a message digest algorithm developed by sl@0: * NIST, along with the NSA, for use with the Digital Signature Standard (DSS). sl@0: * sl@0: * It takes a message of less than 2^128 bits in length and produces sl@0: * a 384-bit message digest. sl@0: * sl@0: * See FIPS 180-2 and RFC 4634 sl@0: */ sl@0: ESHA384, sl@0: /** sl@0: * Secure Hash Algorithm - 512 (version 2) is a message digest algorithm developed by sl@0: * NIST, along with the NSA, for use with the Digital Signature Standard (DSS). sl@0: * sl@0: * It takes a message of less than 2^128 bits in length and produces sl@0: * a 512-bit message digest. sl@0: * sl@0: * See FIPS 180-2 and RFC 4634 sl@0: */ sl@0: ESHA512 sl@0: }; sl@0: sl@0: public: sl@0: /** sl@0: * Creates a brand new reset CMessageDigest object containing no state sl@0: * information from the current object. sl@0: * sl@0: * To make a copy of a message digest with its internal state intact, sl@0: * see CopyL(). sl@0: * sl@0: * @return A pointer to the new reset CMessageDigest object sl@0: */ sl@0: IMPORT_C virtual CMessageDigest* ReplicateL(void)=0; sl@0: sl@0: /** sl@0: * Adds aMessage to the internal representation of data to be hashed, sl@0: * then returns a TPtrC8 of the finalised hash of all the previously sl@0: * appended messages. sl@0: * sl@0: * @param aMessage Data to be included in the hash. sl@0: * @return A descriptor pointer to the buffer containing the sl@0: * resulting hash. sl@0: */ sl@0: IMPORT_C virtual TPtrC8 Hash(const TDesC8& aMessage)=0; sl@0: sl@0: /** sl@0: * Creates a new CMessageDigest object with the exact same state as sl@0: * the current object. sl@0: * sl@0: * This function copies all internal state of the message digest. sl@0: * To create a new CMessageDigest object without the state of sl@0: * the current object, see ReplicateL(). sl@0: * sl@0: * @return A pointer to the new CMessageDigest object sl@0: */ sl@0: IMPORT_C virtual CMessageDigest* CopyL(void)=0; sl@0: sl@0: /** sl@0: * Gets the internal block size of the message digest. sl@0: * sl@0: * @return Internal block size of message digest in bytes. sl@0: */ sl@0: IMPORT_C virtual TInt BlockSize(void)=0; sl@0: sl@0: /** sl@0: * Gets the size of the message digest output. sl@0: * sl@0: * @return Output size of the message digest in bytes. sl@0: */ sl@0: IMPORT_C virtual TInt HashSize(void)=0; sl@0: sl@0: /** sl@0: * Resets the internal state of the message digest. sl@0: * sl@0: * A reset hash object loses all internal state representing the hashed sl@0: * data. A reset message digest is suitable to begin a new, distinct hash sl@0: * of different data. Any previously returned TPtrC8 from a call to sl@0: * Final() remains valid until any subsequent call to Update() or sl@0: * Final(). sl@0: */ sl@0: IMPORT_C virtual void Reset(void)=0; sl@0: sl@0: /** sl@0: * Destructor. sl@0: */ sl@0: IMPORT_C ~CMessageDigest(void); sl@0: public: sl@0: /** sl@0: * Adds data to the internal representation of messages to be hashed. sl@0: * sl@0: * @param aMessage Data to be included in the hash. sl@0: * @since v8.0 sl@0: */ sl@0: IMPORT_C virtual void Update(const TDesC8& aMessage)=0; sl@0: sl@0: /** sl@0: * Adds aMessage to the internal representation of data to be hashed, sl@0: * returns a TPtrC8 of the finalised hash of all the previously sl@0: * appended messages, and calls Reset(). sl@0: * sl@0: * @param aMessage Data to be included in the hash sl@0: * @return A descriptor pointer to the buffer containing the sl@0: * resulting hash. sl@0: * @since v8.0 sl@0: */ sl@0: IMPORT_C virtual TPtrC8 Final(const TDesC8& aMessage)=0; sl@0: sl@0: /** sl@0: * Gets a TPtrC8 of the finalised hash of all the previously sl@0: * appended messages and then calls Reset(). sl@0: * sl@0: * @return A descriptor pointer to the buffer containing the sl@0: * resulting hash. sl@0: * @since v8.0 sl@0: */ sl@0: IMPORT_C virtual TPtrC8 Final(void)=0; sl@0: public: sl@0: /** sl@0: * Restores the internal state of the message digest sl@0: * to a previously stored state. sl@0: * sl@0: * @see StoreState() sl@0: */ sl@0: virtual void RestoreState() = 0; sl@0: sl@0: /** sl@0: * Stores the internal state of the message digest. sl@0: */ sl@0: virtual void StoreState() = 0; sl@0: sl@0: /** sl@0: @internalComponent sl@0: Used to retrieve the extended interfaces extension sl@0: */ sl@0: TInt GetExtension(TUint aExtensionId, TAny*& a0, TAny* a1); sl@0: sl@0: protected: sl@0: /** sl@0: * Constructor sl@0: */ sl@0: IMPORT_C CMessageDigest(void); sl@0: sl@0: /** sl@0: * Copy constructor sl@0: * sl@0: * @param aMD A CMessageDigest object sl@0: */ sl@0: IMPORT_C CMessageDigest(const CMessageDigest& aMD); sl@0: }; sl@0: sl@0: /** sl@0: The MD2 block size (in bytes) sl@0: */ sl@0: const TInt MD2_BLOCK=16; sl@0: sl@0: /** sl@0: The size (in bytes) of the MD2 message digest sl@0: */ sl@0: const TInt MD2_HASH=16; sl@0: sl@0: /** sl@0: * An MD2 message digest sl@0: */ sl@0: class CMD2:public CMessageDigest sl@0: sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new MD2 object. sl@0: * sl@0: * @return A pointer to the new CMD2 object sl@0: */ sl@0: IMPORT_C static CMD2* NewL(void); sl@0: IMPORT_C CMessageDigest* ReplicateL(void); sl@0: IMPORT_C TPtrC8 Hash(const TDesC8& aMessage); sl@0: /** Destructor */ sl@0: IMPORT_C ~CMD2(void); sl@0: IMPORT_C CMessageDigest* CopyL(void); sl@0: IMPORT_C TInt BlockSize(void); sl@0: IMPORT_C TInt HashSize(void); sl@0: IMPORT_C void Reset(void); sl@0: IMPORT_C void Update(const TDesC8& aMessage); sl@0: IMPORT_C TPtrC8 Final(const TDesC8& aMessage); sl@0: IMPORT_C TPtrC8 Final(); sl@0: public: sl@0: void RestoreState(); sl@0: void StoreState(); sl@0: protected: sl@0: /** @internalComponent */ sl@0: CMD2(void); sl@0: }; sl@0: sl@0: /** sl@0: The MD5 block size (in bytes) sl@0: */ sl@0: const TUint MD5_LBLOCK=16; sl@0: sl@0: /** sl@0: The size (in bytes) of the MD5 message digest sl@0: */ sl@0: const TUint MD5_HASH=16; sl@0: sl@0: /** sl@0: * An MD5 message digest sl@0: * sl@0: * Takes a message of arbitrary length as input and produces a 128-bit message digest. sl@0: * sl@0: * The total input length of data should not be longer than 2^32 in bits(2^31 in bytes) sl@0: * which is roughly half a gig. sl@0: * sl@0: */ sl@0: class CMD5:public CMessageDigest sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new MD5 object. sl@0: * sl@0: * @return A pointer to the new CMD5 object sl@0: */ sl@0: IMPORT_C static CMD5* NewL(void); sl@0: IMPORT_C CMessageDigest* ReplicateL(void); sl@0: IMPORT_C TPtrC8 Hash(const TDesC8& aMessage); sl@0: /** Destructor */ sl@0: IMPORT_C ~CMD5(void); sl@0: IMPORT_C CMessageDigest* CopyL(void); sl@0: IMPORT_C TInt BlockSize(void); sl@0: IMPORT_C TInt HashSize(void); sl@0: IMPORT_C void Reset(void); sl@0: IMPORT_C void Update(const TDesC8& aMessage); sl@0: IMPORT_C TPtrC8 Final(const TDesC8& aMessage); sl@0: IMPORT_C TPtrC8 Final(); sl@0: public: sl@0: void RestoreState(); sl@0: void StoreState(); sl@0: protected: sl@0: /** @internalComponent */ sl@0: CMD5(void); sl@0: }; sl@0: sl@0: sl@0: /** sl@0: The SHA-1 block size (in bytes) sl@0: */ sl@0: const TUint SHA1_LBLOCK=16; sl@0: sl@0: /** sl@0: The size (in bytes) of the SHA-1 message digest sl@0: */ sl@0: const TUint SHA1_HASH=20; sl@0: sl@0: /** sl@0: The size (in bytes) of the SHA message digest sl@0: */ sl@0: const TUint SHA_HASH=SHA1_HASH; sl@0: sl@0: /** sl@0: * A SHA-1 message digest sl@0: */ sl@0: class CSHA1:public CMessageDigest sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new SHA-1 object. sl@0: * sl@0: * @return A pointer to the new SHA-1 object sl@0: */ sl@0: IMPORT_C static CSHA1* NewL(void); sl@0: IMPORT_C CMessageDigest* ReplicateL(void); sl@0: IMPORT_C TPtrC8 Hash(const TDesC8& aMessage); sl@0: /** Destructor */ sl@0: IMPORT_C ~CSHA1(void); sl@0: IMPORT_C CMessageDigest* CopyL(void); sl@0: IMPORT_C TInt BlockSize(void); sl@0: IMPORT_C TInt HashSize(void); sl@0: IMPORT_C void Reset(void); sl@0: IMPORT_C void Update(const TDesC8& aMessage); sl@0: IMPORT_C TPtrC8 Final(const TDesC8& aMessage); sl@0: IMPORT_C TPtrC8 Final(); sl@0: public: sl@0: void RestoreState(); sl@0: void StoreState(); sl@0: protected: sl@0: /** @internalComponent */ sl@0: CSHA1(void); sl@0: }; sl@0: sl@0: enum TSH2Algo sl@0: { sl@0: E224Bit, sl@0: E256Bit, sl@0: E384Bit, sl@0: E512Bit sl@0: }; sl@0: sl@0: /** sl@0: * A SHA-2 message digest sl@0: * sl@0: * SHA-2 is comprised of SHA-224, SHA256, SHA384 and SHA512 sl@0: */ sl@0: class CSHA2 : public CMessageDigest sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new SHA-1 object. sl@0: * sl@0: * @return A pointer to the new SHA-1 object sl@0: */ sl@0: IMPORT_C static CSHA2* NewL(TSH2Algo aAlgorithmId); sl@0: IMPORT_C static CSHA2* NewLC(TSH2Algo aAlgorithmId); sl@0: /** Destructor */ sl@0: IMPORT_C ~CSHA2(void); sl@0: public: sl@0: void RestoreState(); sl@0: void StoreState(); sl@0: protected: sl@0: /** @internalComponent */ sl@0: CSHA2(void); sl@0: }; sl@0: sl@0: sl@0: /** sl@0: * A SHA message digest sl@0: * sl@0: * @deprecated Replaced by CSHA1 sl@0: */ sl@0: class CSHA:public CMessageDigest sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new SHA object. sl@0: * sl@0: * @return A pointer to the new SHA object sl@0: */ sl@0: IMPORT_C static CSHA* NewL(void); sl@0: IMPORT_C CMessageDigest* ReplicateL(void); sl@0: IMPORT_C TPtrC8 Hash(const TDesC8& aMessage); sl@0: /** Destructor */ sl@0: IMPORT_C ~CSHA(void); sl@0: IMPORT_C CMessageDigest* CopyL(void); sl@0: IMPORT_C TInt BlockSize(void); sl@0: IMPORT_C TInt HashSize(void); sl@0: IMPORT_C void Reset(void); sl@0: IMPORT_C void Update(const TDesC8& aMessage); sl@0: IMPORT_C TPtrC8 Final(const TDesC8& aMessage); sl@0: IMPORT_C TPtrC8 Final(); sl@0: public: sl@0: void RestoreState(); sl@0: void StoreState(); sl@0: }; sl@0: sl@0: /** sl@0: * This is the maximum block size currently supported by HMAC implementation. sl@0: */ sl@0: const TUint KMaxBlockSize=128; sl@0: sl@0: /** sl@0: * An HMAC (Hashed Message Authentication Code) sl@0: */ sl@0: class CHMAC:public CMessageDigest sl@0: sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new HMAC object from a specified type of message digest sl@0: * and authentication key. sl@0: * sl@0: * @param aKey Authentication key. sl@0: * @param aDigest A message digest to construct the HMAC from. sl@0: * @return A pointer to the new CHMAC object. sl@0: * The resulting HMAC object takes ownership of aDigest sl@0: * and is responsible for its deletion. sl@0: */ sl@0: IMPORT_C static CHMAC* NewL(const TDesC8& aKey,CMessageDigest* aDigest); sl@0: IMPORT_C CMessageDigest* ReplicateL(void); sl@0: IMPORT_C TPtrC8 Hash(const TDesC8& aMessage); sl@0: /** Destructor */ sl@0: IMPORT_C ~CHMAC(void); sl@0: IMPORT_C CMessageDigest* CopyL(void); sl@0: IMPORT_C TInt BlockSize(void); sl@0: IMPORT_C TInt HashSize(void); sl@0: IMPORT_C void Reset(void); sl@0: IMPORT_C void Update(const TDesC8& aMessage); sl@0: IMPORT_C TPtrC8 Final(const TDesC8& aMessage); sl@0: IMPORT_C TPtrC8 Final(); sl@0: public: sl@0: void RestoreState(); sl@0: void StoreState(); sl@0: protected: sl@0: /** @internalComponent */ sl@0: CHMAC(void); sl@0: /** @internalComponent */ sl@0: CHMAC(CMessageDigest* aDigest); sl@0: /** @internalComponent */ sl@0: CHMAC(const CHMAC& aHMAC); sl@0: /** @internalComponent */ sl@0: void InitialiseL(const TDesC8& aKey); sl@0: sl@0: private: sl@0: void InitBlockSizeL(); sl@0: sl@0: private: sl@0: CMessageDigest* iDigest; sl@0: TBuf8 iInnerPad; sl@0: TBuf8 iOuterPad; sl@0: TBuf8 iInnerPadCopy; sl@0: TBuf8 iOuterPadCopy; sl@0: TInt iBlockSize; sl@0: sl@0: }; sl@0: sl@0: /** sl@0: The MD4 block size (in bytes) sl@0: */ sl@0: const TUint MD4_LBLOCK=16; sl@0: sl@0: /** sl@0: The size (in bytes) of the MD4 message digest sl@0: */ sl@0: const TUint MD4_HASH=16; sl@0: sl@0: /** sl@0: * An MD4 message digest Algorithm. sl@0: * Takes a message of arbitrary length as input and produces a 128-bit message digest. sl@0: * sl@0: * The total input length of data should not be longer than 2^32 in bits(2^31 in bytes) sl@0: * which is roughly half a gig. sl@0: * sl@0: */ sl@0: class CMD4:public CMessageDigest sl@0: { sl@0: public: sl@0: /** sl@0: * Creates a new MD4 object. sl@0: * sl@0: * @return A pointer to the new CMD4 object sl@0: */ sl@0: IMPORT_C static CMD4* NewL(void); sl@0: IMPORT_C CMessageDigest* ReplicateL(void); sl@0: IMPORT_C TPtrC8 Hash(const TDesC8& aMessage); sl@0: /** Destructor */ sl@0: IMPORT_C ~CMD4(void); sl@0: IMPORT_C CMessageDigest* CopyL(void); sl@0: IMPORT_C TInt BlockSize(void); sl@0: IMPORT_C TInt HashSize(void); sl@0: IMPORT_C void Reset(void); sl@0: IMPORT_C void Update(const TDesC8& aMessage); sl@0: IMPORT_C TPtrC8 Final(const TDesC8& aMessage); sl@0: IMPORT_C TPtrC8 Final(); sl@0: public: sl@0: virtual void RestoreState(); sl@0: virtual void StoreState(); sl@0: protected: sl@0: /** @internalComponent */ sl@0: CMD4(void); sl@0: }; sl@0: sl@0: sl@0: /** sl@0: * Factory to create a CMessageDigest derived object according to the identity of the hash algorithm. sl@0: */ sl@0: class CMessageDigestFactory : public CBase sl@0: { sl@0: public: sl@0: /** sl@0: *Creates a CMessageDigest derived object according to the specified type of hash algorithm. sl@0: * sl@0: * @param aHashId The identity of the hash algorithm sl@0: * @return A pointer to a CMessageDigest object sl@0: */ sl@0: IMPORT_C static CMessageDigest* NewDigestL(CMessageDigest::THashId aHashId); sl@0: sl@0: /** sl@0: * Creates a CMessageDigest derived object according to the specified type of hash algorithm. sl@0: * sl@0: * The returned pointer is put onto the cleanup stack. sl@0: * sl@0: * @param aHashId The identity of the hash algorithm sl@0: * @return A pointer to a CMessageDigest object sl@0: */ sl@0: IMPORT_C static CMessageDigest* NewDigestLC(CMessageDigest::THashId aHashId); sl@0: sl@0: /** sl@0: * Creates a CMessageDigest derived object according to the specified type of hash algorithm sl@0: * and authentication key. sl@0: * sl@0: * @param aHashId The identity of the hash algorithm sl@0: * @param aKey The authentication key sl@0: * @return A pointer to a CMessageDigest object sl@0: */ sl@0: IMPORT_C static CMessageDigest* NewHMACL(CMessageDigest::THashId aHashId, const TDesC8& aKey); sl@0: sl@0: /** sl@0: * Creates a CMessageDigest derived object according to the specified type of hash algorithm sl@0: * and authentication key. sl@0: * sl@0: * The returned pointer is put onto the cleanup stack. sl@0: * sl@0: * @param aHashId The identity of the hash algorithm sl@0: * @param aKey The authentication key sl@0: * @return A pointer to a CMessageDigest object sl@0: */ sl@0: IMPORT_C static CMessageDigest* NewHMACLC(CMessageDigest::THashId aHashId, const TDesC8& aKey); sl@0: }; sl@0: sl@0: #endif // __HASH_H__