os/security/crypto/weakcrypto/source/hash/hmac.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 
    24 #include <e32std.h>
    25 #include <hash.h>
    26 
    27 #define EXPANDLOOP
    28 
    29 //
    30 // HMAC implementation
    31 //
    32 
    33 CHMAC::CHMAC()
    34 :CMessageDigest(){}
    35 
    36 CHMAC::CHMAC(CMessageDigest* aDigest)
    37 	:CMessageDigest(),
    38 	iDigest(aDigest),
    39 	iInnerPad(KMaxBlockSize),
    40 	iOuterPad(KMaxBlockSize)
    41 {}
    42 
    43 CHMAC::CHMAC(const CHMAC& aMD)
    44 	:CMessageDigest(aMD), 
    45 	iDigest(NULL),
    46 	iInnerPad(aMD.iInnerPad), 
    47 	iOuterPad(aMD.iOuterPad),
    48 	iBlockSize(aMD.iBlockSize)
    49 	{}
    50 
    51 EXPORT_C CHMAC::~CHMAC(void)
    52 	{
    53 	delete iDigest;
    54 	}
    55 
    56 EXPORT_C CHMAC* CHMAC::NewL(const TDesC8& aKey,CMessageDigest* aDigest)
    57 	{
    58 	CHMAC* self=new(ELeave) CHMAC(aDigest);
    59 	CleanupStack::PushL(self);
    60 	self->InitialiseL(aKey);
    61 	CleanupStack::Pop(self);
    62 	return self;
    63 	}
    64 void CHMAC::InitialiseL(const TDesC8& aKey)
    65 	{
    66 	InitBlockSizeL();
    67 	// initialisation
    68 	if (iDigest)
    69 		{
    70 		iDigest->Reset();
    71 		if( (TUint32)aKey.Size() > iBlockSize)
    72 			{
    73 			iInnerPad = iDigest->Final(aKey);
    74 			}
    75 		else 
    76 			{
    77 			iInnerPad = aKey;
    78 			}
    79 			
    80 		TUint i;
    81 		for (i=iInnerPad.Size();i<iBlockSize;i++)
    82 			iInnerPad.Append(0);
    83 
    84 		iOuterPad=iInnerPad;
    85 
    86 		const TUint8 Magic1=0x36, Magic2=0x5c;
    87 		for (i=0;i<iBlockSize;i++)
    88 		{
    89 			iInnerPad[i]^=Magic1;
    90 			iOuterPad[i]^=Magic2;
    91 		}
    92 		//start inner hash
    93 		iDigest->Hash(iInnerPad);
    94 		}
    95 	}
    96 
    97 void CHMAC::InitBlockSizeL()
    98  	{
    99 	
   100  	 iBlockSize = iDigest->BlockSize();
   101 	 if(iBlockSize > KMaxBlockSize)
   102 		 {
   103 		 User::Leave(KErrNotSupported);
   104 		 }
   105  	 
   106  	 iInnerPad.SetLength(iBlockSize);
   107  	 iOuterPad.SetLength(iBlockSize);
   108  	 iInnerPadCopy.SetLength(iBlockSize);
   109  	 iOuterPadCopy.SetLength(iBlockSize);
   110    	}
   111 
   112 EXPORT_C CMessageDigest* CHMAC::CopyL(void)
   113 	{
   114 	CHMAC* that=new(ELeave) CHMAC(*this);
   115 	CleanupStack::PushL(that);
   116 	that->iDigest=iDigest ? iDigest->CopyL() : NULL;
   117 	CleanupStack::Pop();
   118 	return that;
   119 	}
   120 EXPORT_C CMessageDigest*  CHMAC::ReplicateL(void)
   121 	{
   122 	CHMAC* that=new(ELeave) CHMAC(*this);
   123 	CleanupStack::PushL(that);
   124 	that->iDigest=iDigest ? iDigest->ReplicateL() : NULL;
   125 	that->Reset();
   126 	CleanupStack::Pop();
   127 	return that;
   128 	}
   129 EXPORT_C TInt CHMAC::BlockSize(void)
   130 	{
   131 	return iBlockSize;
   132 	}
   133 
   134 EXPORT_C TInt CHMAC::HashSize(void)
   135 	{
   136 	return iDigest ? iDigest->HashSize() : 0;
   137 	}
   138 EXPORT_C void CHMAC::Reset(void)
   139 	{
   140 	if (iDigest)
   141 		{
   142 		iDigest->Reset();
   143 		iDigest->Update(iInnerPad);
   144 		}
   145 	}
   146 
   147 //	JCS, There may be a more efficient method but I can't find it
   148 //	because using the DoFinal/DoUpdate functions directly calls
   149 //	Store/Restore at inappropriate times and scribbles over stored
   150 //	data
   151 //	This is the only way I've found to both generate a hash value
   152 //	and get this in the correctly updated state
   153 EXPORT_C TPtrC8 CHMAC::Hash(const TDesC8& aMessage)
   154 {
   155 	TPtrC8 ptr(KNullDesC8());
   156 	TPtrC8 finalPtr(KNullDesC8());
   157 	StoreState();
   158 	if (iDigest)
   159 	{
   160 		ptr.Set(iDigest->Final(aMessage));
   161 		iDigest->Update(iOuterPad);
   162 		finalPtr.Set(iDigest->Final(ptr));
   163 	}
   164 
   165 	RestoreState();
   166 	iDigest->Update(aMessage);
   167 
   168 	return (finalPtr);
   169 }
   170 
   171 EXPORT_C void CHMAC::Update(const TDesC8& aMessage)
   172 	{
   173 	if(iDigest)
   174 		{
   175 		iDigest->Update(aMessage);
   176 		}
   177 	}
   178 
   179 EXPORT_C TPtrC8 CHMAC::Final(const TDesC8& aMessage)
   180 	{
   181 	TPtrC8 ptr(KNullDesC8());
   182 	if(iDigest)
   183 		{
   184 		ptr.Set(iDigest->Final(aMessage));
   185 		iDigest->Update(iOuterPad);
   186 		iDigest->Final(ptr);
   187 		Reset();
   188 		}
   189 	return (ptr);
   190 	}
   191 
   192 EXPORT_C TPtrC8 CHMAC::Final()
   193 	{
   194 	TPtrC8 ptr(KNullDesC8());
   195 	if(iDigest)
   196 		{
   197 		ptr.Set(iDigest->Final());
   198 		iDigest->Update(iOuterPad);
   199 		iDigest->Final(ptr);
   200 		Reset();
   201 		}
   202 	return (ptr);
   203 	}
   204 
   205 void CHMAC::RestoreState()
   206 {
   207 	iOuterPad.Copy(iOuterPadCopy);
   208 	iInnerPad.Copy(iInnerPadCopy);
   209 	if (iDigest)
   210 		iDigest->RestoreState();
   211 }
   212 
   213 void CHMAC::StoreState()
   214 {
   215 	iOuterPadCopy.Copy(iOuterPad);
   216 	iInnerPadCopy.Copy(iInnerPad);
   217 	if (iDigest)
   218 		iDigest->StoreState();
   219 }
   220 
   221