os/security/crypto/weakcryptospi/test/tplugins/src/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-2010 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 <cryptospi/keys.h>
    31 
    32 using namespace SoftwareCrypto;
    33 
    34 CHMacImpl* CHMacImpl::NewL(const CKey& aKey, MSoftwareHash* aHash)
    35 	{
    36 	CHMacImpl* self=NewLC(aKey, aHash);
    37 	CleanupStack::Pop();
    38 	return self;
    39 	}
    40 	
    41 CHMacImpl* CHMacImpl::NewLC(const CKey& aKey, MSoftwareHash* aHash)
    42 	{
    43 	CHMacImpl* self=new(ELeave) CHMacImpl();
    44 	CleanupStack::PushL(self);
    45 	self->ConstructL(aKey, aHash);
    46 	return self;
    47 	}
    48 
    49 CHMacImpl* CHMacImpl::NewL(MSoftwareHash* aHash)
    50 	{
    51 	CHMacImpl* self=NewLC(aHash);
    52 	CleanupStack::Pop();
    53 	return self;
    54 	}
    55 	
    56 CHMacImpl* CHMacImpl::NewLC(MSoftwareHash* aHash)
    57 	{
    58 	CHMacImpl* self=new(ELeave) CHMacImpl();
    59 	CleanupStack::PushL(self);
    60 	self->ConstructL(aHash);
    61 	return self;
    62 	}
    63 
    64 
    65 CHMacImpl::CHMacImpl()
    66 	: iInnerPad(KHMacPad), iOuterPad(KHMacPad)
    67 	{
    68 	}
    69 
    70 CHMacImpl::CHMacImpl(const CHMacImpl& aMD)
    71 	: iDigest(NULL), iInnerPad(aMD.iInnerPad), iOuterPad(aMD.iOuterPad)
    72 	{
    73 	}
    74 
    75 CHMacImpl::~CHMacImpl()
    76 	{
    77 	if (iDigest)
    78 		{
    79 		iDigest->Close();	
    80 		}
    81 	}
    82 
    83 void CHMacImpl::ConstructL(const CKey& aKey, MSoftwareHash* aHash)
    84 	{
    85 	//Clone the hash implementation
    86 	iDigest=static_cast<MSoftwareHash*>(aHash->ReplicateL());
    87 	const TDesC8& keyContent=aKey.GetTDesC8L(KHmacKeyParameterUid);
    88 	Initialise(keyContent);	
    89 	}
    90 
    91 void CHMacImpl::ConstructL(MSoftwareHash* aHash)
    92 	{
    93 	//Clone the hash implementation
    94 	iDigest=static_cast<MSoftwareHash*>(aHash->ReplicateL());
    95 	}
    96 
    97 	
    98 void CHMacImpl::Initialise(const TDesC8& aKey)
    99 	{
   100 	// initialisation
   101 	if (iDigest)
   102 		{
   103 		iDigest->Reset();
   104 		if( (TUint32)aKey.Size() > KHMacPad)
   105 			{
   106 			iInnerPad = iDigest->Final(aKey);
   107 			}
   108 		else 
   109 			{
   110 			iInnerPad = aKey;
   111 			}
   112 			
   113 		TUint i;
   114 		for (i=iInnerPad.Size();i<KHMacPad;i++)
   115 			iInnerPad.Append(0);
   116 
   117 		iOuterPad=iInnerPad;
   118 
   119 		const TUint8 Magic1=0x36, Magic2=0x5c;
   120 		for (i=0;i<KHMacPad;i++)
   121 			{
   122 			iInnerPad[i]^=Magic1;
   123 			iOuterPad[i]^=Magic2;
   124 			}
   125 		//start inner hash
   126 		iDigest->Hash(iInnerPad);
   127 		}
   128 	}
   129 	
   130 MHash* CHMacImpl::CopyL()
   131 	{
   132 	CHMacImpl* that=new(ELeave) CHMacImpl(*this);
   133 	CleanupStack::PushL(that);
   134 	that->iDigest=iDigest ? static_cast<MSoftwareHash*>(iDigest->CopyL()) : NULL;
   135 	CleanupStack::Pop();
   136 	return that;
   137 	}
   138 	
   139 MHash* CHMacImpl::ReplicateL()
   140 	{
   141 	CHMacImpl* that=new(ELeave) CHMacImpl(*this);
   142 	CleanupStack::PushL(that);
   143 	that->iDigest=iDigest ? static_cast<MSoftwareHash*>(iDigest->ReplicateL()) : NULL;
   144 	that->Reset();
   145 	CleanupStack::Pop();
   146 	return that;
   147 	}
   148 	
   149 void CHMacImpl::Reset()
   150 	{
   151 	if (iDigest)
   152 		{
   153 		iDigest->Reset();
   154 		iDigest->Update(iInnerPad);
   155 		}
   156 	}
   157 
   158 TPtrC8 CHMacImpl::Hash(const TDesC8& aMessage)
   159 	{
   160 	TPtrC8 ptr(KNullDesC8());
   161 	TPtrC8 finalPtr(KNullDesC8());
   162 	StoreState();
   163 	if (iDigest)
   164 		{
   165 		ptr.Set(iDigest->Final(aMessage));
   166 		iDigest->Update(iOuterPad);
   167 		finalPtr.Set(iDigest->Final(ptr));
   168 		}
   169 
   170 	RestoreState();
   171 	iDigest->Update(aMessage);
   172 
   173 	return (finalPtr);
   174 	}
   175 
   176 void CHMacImpl::Update(const TDesC8& aMessage)
   177 	{
   178 	if(iDigest)
   179 		{
   180 		iDigest->Update(aMessage);
   181 		}
   182 	}
   183 
   184 TPtrC8 CHMacImpl::Final(const TDesC8& aMessage)
   185 	{
   186 	TPtrC8 ptr(KNullDesC8());
   187 	if(iDigest)
   188 		{
   189 		ptr.Set(iDigest->Final(aMessage));
   190 		iDigest->Update(iOuterPad);
   191 		iDigest->Final(ptr);
   192 		Reset();
   193 		}
   194 	return (ptr);
   195 	}
   196 	
   197 void CHMacImpl::RestoreState()
   198 	{
   199 	iOuterPad.Copy(iOuterPadCopy);
   200 	iInnerPad.Copy(iInnerPadCopy);
   201 	if (iDigest)
   202 		{
   203 		iDigest->RestoreState();
   204 		}
   205 	}
   206 
   207 void CHMacImpl::StoreState()
   208 	{
   209 	iOuterPadCopy.Copy(iOuterPad);
   210 	iInnerPadCopy.Copy(iInnerPad);
   211 	if (iDigest)
   212 		{
   213 		iDigest->StoreState();	
   214 		}
   215 	}
   216 
   217 void CHMacImpl::SetKeyL(const CKey& aKey)
   218 	{
   219 	const TDesC8& keyContent=aKey.GetTDesC8L(KHmacKeyParameterUid);
   220 	Initialise(keyContent);	
   221 	}
   222 
   223 void CHMacImpl::Close()
   224 	{
   225 	delete this;	
   226 	}
   227 	
   228 void CHMacImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
   229 	{
   230 	iDigest->GetCharacteristicsL(aPluginCharacteristics);	
   231 	}
   232 	
   233 const CExtendedCharacteristics* CHMacImpl::GetExtendedCharacteristicsL()
   234 	{
   235 	return iDigest->GetExtendedCharacteristicsL();
   236 	}
   237 	
   238 TAny* CHMacImpl::GetExtension(TUid /*aExtensionId*/)
   239 	{
   240 	return NULL;	
   241 	}
   242 	
   243 void CHMacImpl::SetOperationModeL(TUid /*aOperationMode*/)
   244 	{
   245 	User::Leave(KErrNotSupported);
   246 	}
   247