crypto/weakcrypto/source/hash/hash.cpp
changeset 0 2c201484c85f
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 
       
    21 /**
       
    22  @file
       
    23 */
       
    24 
       
    25 #include <e32std.h>
       
    26 #include <hash.h>
       
    27 #include "hashinc.h"
       
    28 #define EXPANDLOOP
       
    29 
       
    30 EXPORT_C CMessageDigest::CMessageDigest(void):CBase()
       
    31 {}
       
    32 
       
    33 EXPORT_C CMessageDigest::CMessageDigest(const CMessageDigest& /*aMD*/):CBase()
       
    34 {}
       
    35 
       
    36 EXPORT_C CMessageDigest::~CMessageDigest(void)
       
    37 {}
       
    38 
       
    39 /* Note that the shifts here are NOT guaranteed to work if s == 0, and it is
       
    40  * assumed in R() (and hence in all inline) that a is unsigned. */
       
    41 // CMD_R - rotate a left by n bits; the main bottleneck of the algorithm 
       
    42 // implementation
       
    43 /*
       
    44 C++ version compiles to:
       
    45 
       
    46 CMD_R__FUiUi:
       
    47 	add	r3, r0, #0
       
    48 	lsl	r3, r3, r1
       
    49 	mov	r2, #32
       
    50 	sub	r2, r2, r1
       
    51 	lsr	r0, r0, r2
       
    52 	add	r3, r3, r0
       
    53 	add	r0, r3, #0
       
    54 	bx	lr
       
    55 
       
    56 */
       
    57 
       
    58 //////////////////////////////////////////////////////////////////
       
    59 //	Factory class to create CMessageDigest derived objects
       
    60 //////////////////////////////////////////////////////////////////
       
    61 EXPORT_C CMessageDigest* CMessageDigestFactory::NewDigestL(CMessageDigest::THashId aHashId)
       
    62 {
       
    63 	CMessageDigest* hash = NULL;
       
    64 	switch (aHashId)
       
    65 	{
       
    66 	case (CMessageDigest::EMD2):
       
    67 		{
       
    68 			hash = CMD2::NewL();
       
    69 			break;
       
    70 		}
       
    71 	case (CMessageDigest::EMD5):
       
    72 		{
       
    73 			hash = CMD5::NewL();
       
    74 			break;
       
    75 		}
       
    76 	case (CMessageDigest::ESHA1):
       
    77 		{
       
    78 			hash = CSHA1::NewL();
       
    79 			break;
       
    80 		}
       
    81         case (CMessageDigest::EMD4):
       
    82 		{
       
    83 			hash = CMD4::NewL();
       
    84 			break;
       
    85 		}
       
    86 	case (CMessageDigest::ESHA224):
       
    87 		{
       
    88 			hash = CSHA2::NewL(E224Bit);
       
    89 			break;
       
    90 		}
       
    91 	case (CMessageDigest::ESHA256):
       
    92 		{
       
    93 			hash = CSHA2::NewL(E256Bit);
       
    94 			break;
       
    95 		}
       
    96 	case (CMessageDigest::ESHA384):
       
    97 		{
       
    98 			hash = CSHA2::NewL(E384Bit);
       
    99 			break;
       
   100 		}
       
   101 	case (CMessageDigest::ESHA512):
       
   102 		{
       
   103 			hash = CSHA2::NewL(E512Bit);
       
   104 			break;
       
   105 		}
       
   106 	case (CMessageDigest::HMAC):
       
   107 	default:	
       
   108 		User::Leave(KErrNotSupported);
       
   109 	}
       
   110 
       
   111 	return (hash);
       
   112 }
       
   113 
       
   114 EXPORT_C CMessageDigest* CMessageDigestFactory::NewDigestLC(CMessageDigest::THashId aHashId)
       
   115 {
       
   116 	CMessageDigest* hash = CMessageDigestFactory::NewDigestL(aHashId);
       
   117 	CleanupStack::PushL(hash);
       
   118 	return (hash);
       
   119 }
       
   120 
       
   121 EXPORT_C CMessageDigest* CMessageDigestFactory::NewHMACL(CMessageDigest::THashId aHashId, const TDesC8& aKey)
       
   122 {
       
   123 	CMessageDigest* hash = CMessageDigestFactory::NewDigestLC(aHashId);
       
   124 	CMessageDigest* hmac = CHMAC::NewL(aKey, hash);
       
   125 	CleanupStack::Pop(hash);	//	Now owned by hmac
       
   126 	return (hmac);
       
   127 }
       
   128 
       
   129 EXPORT_C CMessageDigest* CMessageDigestFactory::NewHMACLC(CMessageDigest::THashId aHashId, const TDesC8& aKey)
       
   130 {
       
   131 	CMessageDigest* hmac = CMessageDigestFactory::NewHMACL(aHashId, aKey);
       
   132 	CleanupStack::PushL(hmac);
       
   133 	return (hmac);
       
   134 }