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 * Base class for message digests.
36 class CMessageDigest:public CBase
40 * Enumeration to identify hash functions (aka message-digest algorithms).
45 * Message Digest algorithm developed by Rivest for digital signature
46 * applications (and optimized for 8-bit machines).
48 * Takes a message of arbitrary length and produces a 128-bit message digest.
54 * Message Digest algorithm developed by Rivest for digital signature
55 * applications (and optimized for 32-bit machines).
57 * Takes a message of arbitrary length and produces a 128-bit message digest.
63 * Secure Hash Algorithm (version 1) is a message digest algorithm developed by
64 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
66 * It takes a message of less than 2^64 bits in length and produces
67 * a 160-bit message digest.
69 * See FIPS 180-1 and RFC 3174
73 * HMAC - Hash function based Message Authentication Code is a mechanism
74 * for message authentication using cryptographic hash functions. (A checksum.)
76 * It can be used with any iterative cryptographic hash function,
77 * e.g., MD5, SHA-1, in combination with a secret shared key
78 * to produce a checksum that is appended to the message.
79 * The cryptographic strength of HMAC depends on the properties
80 * of the underlying hash function.
86 * Message Digest algorithm developed by Rivest for digital signature
87 * applications (and optimized for 32-bit machines).
89 * Takes a message of arbitrary length and produces a 128-bit message digest.
95 * Secure Hash Algorithm - 224 (version 2) is a message digest algorithm developed by
96 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
98 * It takes a message of less than 2^64 bits in length and produces
99 * a 224-bit message digest.
101 * See FIPS 180-2 (with change notice), RFC3874 and FIPS 180-3
105 * Secure Hash Algorithm - 256 (version 2) is a message digest algorithm developed by
106 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
108 * It takes a message of less than 2^64 bits in length and produces
109 * a 256-bit message digest.
111 * See FIPS 180-2 and RFC 4634
115 * Secure Hash Algorithm - 384 (version 2) is a message digest algorithm developed by
116 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
118 * It takes a message of less than 2^128 bits in length and produces
119 * a 384-bit message digest.
121 * See FIPS 180-2 and RFC 4634
125 * Secure Hash Algorithm - 512 (version 2) is a message digest algorithm developed by
126 * NIST, along with the NSA, for use with the Digital Signature Standard (DSS).
128 * It takes a message of less than 2^128 bits in length and produces
129 * a 512-bit message digest.
131 * See FIPS 180-2 and RFC 4634
138 * Creates a brand new reset CMessageDigest object containing no state
139 * information from the current object.
141 * To make a copy of a message digest with its internal state intact,
144 * @return A pointer to the new reset CMessageDigest object
146 IMPORT_C virtual CMessageDigest* ReplicateL(void)=0;
149 * Adds aMessage to the internal representation of data to be hashed,
150 * then returns a TPtrC8 of the finalised hash of all the previously
153 * @param aMessage Data to be included in the hash.
154 * @return A descriptor pointer to the buffer containing the
157 IMPORT_C virtual TPtrC8 Hash(const TDesC8& aMessage)=0;
160 * Creates a new CMessageDigest object with the exact same state as
161 * the current object.
163 * This function copies all internal state of the message digest.
164 * To create a new CMessageDigest object without the state of
165 * the current object, see ReplicateL().
167 * @return A pointer to the new CMessageDigest object
169 IMPORT_C virtual CMessageDigest* CopyL(void)=0;
172 * Gets the internal block size of the message digest.
174 * @return Internal block size of message digest in bytes.
176 IMPORT_C virtual TInt BlockSize(void)=0;
179 * Gets the size of the message digest output.
181 * @return Output size of the message digest in bytes.
183 IMPORT_C virtual TInt HashSize(void)=0;
186 * Resets the internal state of the message digest.
188 * A reset hash object loses all internal state representing the hashed
189 * data. A reset message digest is suitable to begin a new, distinct hash
190 * of different data. Any previously returned TPtrC8 from a call to
191 * Final() remains valid until any subsequent call to Update() or
194 IMPORT_C virtual void Reset(void)=0;
199 IMPORT_C ~CMessageDigest(void);
202 * Adds data to the internal representation of messages to be hashed.
204 * @param aMessage Data to be included in the hash.
207 IMPORT_C virtual void Update(const TDesC8& aMessage)=0;
210 * Adds aMessage to the internal representation of data to be hashed,
211 * returns a TPtrC8 of the finalised hash of all the previously
212 * appended messages, and calls Reset().
214 * @param aMessage Data to be included in the hash
215 * @return A descriptor pointer to the buffer containing the
219 IMPORT_C virtual TPtrC8 Final(const TDesC8& aMessage)=0;
222 * Gets a TPtrC8 of the finalised hash of all the previously
223 * appended messages and then calls Reset().
225 * @return A descriptor pointer to the buffer containing the
229 IMPORT_C virtual TPtrC8 Final(void)=0;
232 * Restores the internal state of the message digest
233 * to a previously stored state.
237 virtual void RestoreState() = 0;
240 * Stores the internal state of the message digest.
242 virtual void StoreState() = 0;
246 Used to retrieve the extended interfaces extension
248 TInt GetExtension(TUint aExtensionId, TAny*& a0, TAny* a1);
254 IMPORT_C CMessageDigest(void);
259 * @param aMD A CMessageDigest object
261 IMPORT_C CMessageDigest(const CMessageDigest& aMD);
265 The MD2 block size (in bytes)
267 const TInt MD2_BLOCK=16;
270 The size (in bytes) of the MD2 message digest
272 const TInt MD2_HASH=16;
275 * An MD2 message digest
277 class CMD2:public CMessageDigest
282 * Creates a new MD2 object.
284 * @return A pointer to the new CMD2 object
286 IMPORT_C static CMD2* NewL(void);
287 IMPORT_C CMessageDigest* ReplicateL(void);
288 IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
290 IMPORT_C ~CMD2(void);
291 IMPORT_C CMessageDigest* CopyL(void);
292 IMPORT_C TInt BlockSize(void);
293 IMPORT_C TInt HashSize(void);
294 IMPORT_C void Reset(void);
295 IMPORT_C void Update(const TDesC8& aMessage);
296 IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
297 IMPORT_C TPtrC8 Final();
302 /** @internalComponent */
307 The MD5 block size (in bytes)
309 const TUint MD5_LBLOCK=16;
312 The size (in bytes) of the MD5 message digest
314 const TUint MD5_HASH=16;
317 * An MD5 message digest
319 * Takes a message of arbitrary length as input and produces a 128-bit message digest.
321 * The total input length of data should not be longer than 2^32 in bits(2^31 in bytes)
322 * which is roughly half a gig.
325 class CMD5:public CMessageDigest
329 * Creates a new MD5 object.
331 * @return A pointer to the new CMD5 object
333 IMPORT_C static CMD5* NewL(void);
334 IMPORT_C CMessageDigest* ReplicateL(void);
335 IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
337 IMPORT_C ~CMD5(void);
338 IMPORT_C CMessageDigest* CopyL(void);
339 IMPORT_C TInt BlockSize(void);
340 IMPORT_C TInt HashSize(void);
341 IMPORT_C void Reset(void);
342 IMPORT_C void Update(const TDesC8& aMessage);
343 IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
344 IMPORT_C TPtrC8 Final();
349 /** @internalComponent */
355 The SHA-1 block size (in bytes)
357 const TUint SHA1_LBLOCK=16;
360 The size (in bytes) of the SHA-1 message digest
362 const TUint SHA1_HASH=20;
365 The size (in bytes) of the SHA message digest
367 const TUint SHA_HASH=SHA1_HASH;
370 * A SHA-1 message digest
372 class CSHA1:public CMessageDigest
376 * Creates a new SHA-1 object.
378 * @return A pointer to the new SHA-1 object
380 IMPORT_C static CSHA1* NewL(void);
381 IMPORT_C CMessageDigest* ReplicateL(void);
382 IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
384 IMPORT_C ~CSHA1(void);
385 IMPORT_C CMessageDigest* CopyL(void);
386 IMPORT_C TInt BlockSize(void);
387 IMPORT_C TInt HashSize(void);
388 IMPORT_C void Reset(void);
389 IMPORT_C void Update(const TDesC8& aMessage);
390 IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
391 IMPORT_C TPtrC8 Final();
396 /** @internalComponent */
409 * A SHA-2 message digest
411 * SHA-2 is comprised of SHA-224, SHA256, SHA384 and SHA512
413 class CSHA2 : public CMessageDigest
417 * Creates a new SHA-1 object.
419 * @return A pointer to the new SHA-1 object
421 IMPORT_C static CSHA2* NewL(TSH2Algo aAlgorithmId);
422 IMPORT_C static CSHA2* NewLC(TSH2Algo aAlgorithmId);
424 IMPORT_C ~CSHA2(void);
429 /** @internalComponent */
435 * A SHA message digest
437 * @deprecated Replaced by CSHA1
439 class CSHA:public CMessageDigest
443 * Creates a new SHA object.
445 * @return A pointer to the new SHA object
447 IMPORT_C static CSHA* NewL(void);
448 IMPORT_C CMessageDigest* ReplicateL(void);
449 IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
451 IMPORT_C ~CSHA(void);
452 IMPORT_C CMessageDigest* CopyL(void);
453 IMPORT_C TInt BlockSize(void);
454 IMPORT_C TInt HashSize(void);
455 IMPORT_C void Reset(void);
456 IMPORT_C void Update(const TDesC8& aMessage);
457 IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
458 IMPORT_C TPtrC8 Final();
465 * This is the maximum block size currently supported by HMAC implementation.
467 const TUint KMaxBlockSize=128;
470 * An HMAC (Hashed Message Authentication Code)
472 class CHMAC:public CMessageDigest
477 * Creates a new HMAC object from a specified type of message digest
478 * and authentication key.
480 * @param aKey Authentication key.
481 * @param aDigest A message digest to construct the HMAC from.
482 * @return A pointer to the new CHMAC object.
483 * The resulting HMAC object takes ownership of aDigest
484 * and is responsible for its deletion.
486 IMPORT_C static CHMAC* NewL(const TDesC8& aKey,CMessageDigest* aDigest);
487 IMPORT_C CMessageDigest* ReplicateL(void);
488 IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
490 IMPORT_C ~CHMAC(void);
491 IMPORT_C CMessageDigest* CopyL(void);
492 IMPORT_C TInt BlockSize(void);
493 IMPORT_C TInt HashSize(void);
494 IMPORT_C void Reset(void);
495 IMPORT_C void Update(const TDesC8& aMessage);
496 IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
497 IMPORT_C TPtrC8 Final();
502 /** @internalComponent */
504 /** @internalComponent */
505 CHMAC(CMessageDigest* aDigest);
506 /** @internalComponent */
507 CHMAC(const CHMAC& aHMAC);
508 /** @internalComponent */
509 void InitialiseL(const TDesC8& aKey);
512 void InitBlockSizeL();
515 CMessageDigest* iDigest;
516 TBuf8<KMaxBlockSize> iInnerPad;
517 TBuf8<KMaxBlockSize> iOuterPad;
518 TBuf8<KMaxBlockSize> iInnerPadCopy;
519 TBuf8<KMaxBlockSize> iOuterPadCopy;
525 The MD4 block size (in bytes)
527 const TUint MD4_LBLOCK=16;
530 The size (in bytes) of the MD4 message digest
532 const TUint MD4_HASH=16;
535 * An MD4 message digest Algorithm.
536 * Takes a message of arbitrary length as input and produces a 128-bit message digest.
538 * The total input length of data should not be longer than 2^32 in bits(2^31 in bytes)
539 * which is roughly half a gig.
542 class CMD4:public CMessageDigest
546 * Creates a new MD4 object.
548 * @return A pointer to the new CMD4 object
550 IMPORT_C static CMD4* NewL(void);
551 IMPORT_C CMessageDigest* ReplicateL(void);
552 IMPORT_C TPtrC8 Hash(const TDesC8& aMessage);
554 IMPORT_C ~CMD4(void);
555 IMPORT_C CMessageDigest* CopyL(void);
556 IMPORT_C TInt BlockSize(void);
557 IMPORT_C TInt HashSize(void);
558 IMPORT_C void Reset(void);
559 IMPORT_C void Update(const TDesC8& aMessage);
560 IMPORT_C TPtrC8 Final(const TDesC8& aMessage);
561 IMPORT_C TPtrC8 Final();
563 virtual void RestoreState();
564 virtual void StoreState();
566 /** @internalComponent */
572 * Factory to create a CMessageDigest derived object according to the identity of the hash algorithm.
574 class CMessageDigestFactory : public CBase
578 *Creates a CMessageDigest derived object according to the specified type of hash algorithm.
580 * @param aHashId The identity of the hash algorithm
581 * @return A pointer to a CMessageDigest object
583 IMPORT_C static CMessageDigest* NewDigestL(CMessageDigest::THashId aHashId);
586 * Creates a CMessageDigest derived object according to the specified type of hash algorithm.
588 * The returned pointer is put onto the cleanup stack.
590 * @param aHashId The identity of the hash algorithm
591 * @return A pointer to a CMessageDigest object
593 IMPORT_C static CMessageDigest* NewDigestLC(CMessageDigest::THashId aHashId);
596 * Creates a CMessageDigest derived object according to the specified type of hash algorithm
597 * and authentication key.
599 * @param aHashId The identity of the hash algorithm
600 * @param aKey The authentication key
601 * @return A pointer to a CMessageDigest object
603 IMPORT_C static CMessageDigest* NewHMACL(CMessageDigest::THashId aHashId, const TDesC8& aKey);
606 * Creates a CMessageDigest derived object according to the specified type of hash algorithm
607 * and authentication key.
609 * The returned pointer is put onto the cleanup stack.
611 * @param aHashId The identity of the hash algorithm
612 * @param aKey The authentication key
613 * @return A pointer to a CMessageDigest object
615 IMPORT_C static CMessageDigest* NewHMACLC(CMessageDigest::THashId aHashId, const TDesC8& aKey);