os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/hmacimpl.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2006-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 * software hmac implementation
    16 * software hmac implementation
    17 *
    18 */
    19 
    20 
    21 /**
    22  @file
    23 */
    24 
    25 #include "hmacimpl.h"
    26 #include "pluginconfig.h"
    27 #include "md2impl.h"
    28 #include "md5impl.h"
    29 #include "sha1impl.h"
    30 #include "keys.h"
    31 #include "md4impl.h"
    32 
    33 
    34 using namespace SoftwareCrypto;
    35 
    36 CHMacImpl* CHMacImpl::NewL(const CKey& aKey, MSoftwareHash* aHash)
    37 	{
    38 	CHMacImpl* self=NewLC(aKey, aHash);
    39 	CleanupStack::Pop();
    40 	return self;
    41 	}
    42 	
    43 CHMacImpl* CHMacImpl::NewLC(const CKey& aKey, MSoftwareHash* aHash)
    44 	{
    45 	CHMacImpl* self=new(ELeave) CHMacImpl();
    46 	CleanupStack::PushL(self);
    47 	self->ConstructL(aKey, aHash);
    48 	return self;
    49 	}
    50 
    51 CHMacImpl* CHMacImpl::NewL(MSoftwareHash* aHash)
    52 	{
    53 	CHMacImpl* self=NewLC(aHash);
    54 	CleanupStack::Pop();
    55 	return self;
    56 	}
    57 	
    58 CHMacImpl* CHMacImpl::NewLC(MSoftwareHash* aHash)
    59 	{
    60 	CHMacImpl* self=new(ELeave) CHMacImpl();
    61 	CleanupStack::PushL(self);
    62 	self->ConstructL(aHash);
    63 	return self;
    64 	}
    65 
    66 
    67 CHMacImpl::CHMacImpl()
    68 	{
    69 	}
    70 
    71 CHMacImpl::CHMacImpl(const CHMacImpl& aMD)
    72 	: 	iDigest(NULL), iInnerPad(aMD.iInnerPad), 
    73 		iOuterPad(aMD.iOuterPad), iBlockSize(aMD.iBlockSize)
    74 	{
    75 	}
    76 
    77 CHMacImpl::~CHMacImpl()
    78 	{
    79 	if (iDigest)
    80 		{
    81 		iDigest->Close();	
    82 		}
    83 	}
    84 
    85 void CHMacImpl::ConstructL(const CKey& aKey, MSoftwareHash* aHash)
    86 	{
    87 	//Clone the hash implementation
    88 	iDigest=static_cast<MSoftwareHash*>(aHash->ReplicateL());
    89 	InitBlockSizeL();
    90 	const TDesC8& keyContent=aKey.GetTDesC8L(KHmacKeyParameterUid);
    91 	Initialise(keyContent);	
    92 	}
    93 
    94 void CHMacImpl::ConstructL(MSoftwareHash* aHash)
    95 	{
    96 	//Clone the hash implementation
    97 	iDigest=static_cast<MSoftwareHash*>(aHash->ReplicateL());
    98 	InitBlockSizeL();
    99 	}
   100 
   101 void CHMacImpl::InitBlockSizeL()
   102 	{
   103 	 const TCharacteristics* ptr(NULL);
   104 	 iDigest->GetCharacteristicsL(ptr);
   105 	 const THashCharacteristics* hashPtr=static_cast<const THashCharacteristics*>(ptr);
   106 	 iBlockSize = hashPtr->iBlockSize/8;
   107 	 
   108 	 iInnerPad.SetLength(iBlockSize);
   109 	 iOuterPad.SetLength(iBlockSize);
   110 	 iInnerPadCopy.SetLength(iBlockSize);
   111 	 iOuterPadCopy.SetLength(iBlockSize);
   112 	}
   113 
   114 void CHMacImpl::Initialise(const TDesC8& aKey)
   115 	{
   116 	// initialisation
   117 	if (iDigest)
   118 		{
   119 		iDigest->Reset();
   120 		if( (TUint32)aKey.Size() > iBlockSize)
   121 			{
   122 			iInnerPad = iDigest->Final(aKey);
   123 			}
   124 		else 
   125 			{
   126 			iInnerPad = aKey;
   127 			}
   128 			
   129 		TUint i;
   130 		for (i=iInnerPad.Size();i<iBlockSize;i++)
   131 			iInnerPad.Append(0);
   132 
   133 		iOuterPad=iInnerPad;
   134 
   135 		const TUint8 Magic1=0x36, Magic2=0x5c;
   136 		for (i=0;i<iBlockSize;i++)
   137 			{
   138 			iInnerPad[i]^=Magic1;
   139 			iOuterPad[i]^=Magic2;
   140 			}
   141 		//start inner hash
   142 		iDigest->Hash(iInnerPad);
   143 		}
   144 	}
   145 	
   146 MHash* CHMacImpl::CopyL()
   147 	{
   148 	CHMacImpl* that=new(ELeave) CHMacImpl(*this);
   149 	CleanupStack::PushL(that);
   150 	that->iDigest=iDigest ? static_cast<MSoftwareHash*>(iDigest->CopyL()) : NULL;
   151 	CleanupStack::Pop();
   152 	return that;
   153 	}
   154 	
   155 MHash* CHMacImpl::ReplicateL()
   156 	{
   157 	CHMacImpl* that=new(ELeave) CHMacImpl(*this);
   158 	CleanupStack::PushL(that);
   159 	that->iDigest=iDigest ? static_cast<MSoftwareHash*>(iDigest->ReplicateL()) : NULL;
   160 	that->Reset();
   161 	CleanupStack::Pop();
   162 	return that;
   163 	}
   164 	
   165 void CHMacImpl::Reset()
   166 	{
   167 	if (iDigest)
   168 		{
   169 		iDigest->Reset();
   170 		iDigest->Update(iInnerPad);
   171 		}
   172 	}
   173 
   174 TPtrC8 CHMacImpl::Hash(const TDesC8& aMessage)
   175 	{
   176 	TPtrC8 ptr(KNullDesC8());
   177 	TPtrC8 finalPtr(KNullDesC8());
   178 	StoreState();
   179 	if (iDigest)
   180 		{
   181 		ptr.Set(iDigest->Final(aMessage));
   182 		iDigest->Update(iOuterPad);
   183 		finalPtr.Set(iDigest->Final(ptr));
   184 		}
   185 
   186 	RestoreState();
   187 
   188 	if(iDigest)
   189 		{
   190 		iDigest->Update(aMessage);
   191 		}
   192 	
   193 	
   194 		
   195 	return (finalPtr);
   196 	}
   197 
   198 void CHMacImpl::Update(const TDesC8& aMessage)
   199 	{
   200 	if(iDigest)
   201 		{
   202 		iDigest->Update(aMessage);
   203 		}
   204 	}
   205 
   206 TPtrC8 CHMacImpl::Final(const TDesC8& aMessage)
   207 	{
   208 	TPtrC8 ptr(KNullDesC8());
   209 	if(iDigest)
   210 		{
   211 		ptr.Set(iDigest->Final(aMessage));
   212 		iDigest->Update(iOuterPad);
   213 		iDigest->Final(ptr);
   214 		Reset();
   215 		}
   216 	return (ptr);
   217 	}
   218 	
   219 void CHMacImpl::RestoreState()
   220 	{
   221 	iOuterPad.Copy(iOuterPadCopy);
   222 	iInnerPad.Copy(iInnerPadCopy);
   223 	if (iDigest)
   224 		{
   225 		iDigest->RestoreState();
   226 		}
   227 	}
   228 
   229 void CHMacImpl::StoreState()
   230 	{
   231 	iOuterPadCopy.Copy(iOuterPad);
   232 	iInnerPadCopy.Copy(iInnerPad);
   233 	if (iDigest)
   234 		{
   235 		iDigest->StoreState();	
   236 		}
   237 	}
   238 
   239 void CHMacImpl::SetKeyL(const CKey& aKey)
   240 	{
   241 	const TDesC8& keyContent=aKey.GetTDesC8L(KHmacKeyParameterUid);
   242 	Initialise(keyContent);	
   243 	}
   244 
   245 void CHMacImpl::Close()
   246 	{
   247 	delete this;	
   248 	}
   249 	
   250 void CHMacImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
   251 	{
   252 	iDigest->GetCharacteristicsL(aPluginCharacteristics);	
   253 	}
   254 	
   255 const CExtendedCharacteristics* CHMacImpl::GetExtendedCharacteristicsL()
   256 	{
   257 	return iDigest->GetExtendedCharacteristicsL();
   258 	}
   259 
   260 // Methods which are not supported can be excluded from the coverage.
   261 #ifdef _BullseyeCoverage
   262 #pragma suppress_warnings on
   263 #pragma BullseyeCoverage off
   264 #pragma suppress_warnings off
   265 #endif
   266 
   267 TAny* CHMacImpl::GetExtension(TUid /*aExtensionId*/)
   268 	{
   269 	return NULL;	
   270 	}
   271 	
   272 void CHMacImpl::SetOperationModeL(TUid /*aOperationMode*/)
   273 	{
   274 	User::Leave(KErrNotSupported);
   275 	}
   276