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