os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/sha2impl.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.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
* software SHA2 implementation
sl@0
    16
* RFC 4634 (US Secure Hash Algorithms (SHA and HMAC-SHA))
sl@0
    17
* FIPS 180-2 (With change notice)
sl@0
    18
*
sl@0
    19
*/
sl@0
    20
sl@0
    21
sl@0
    22
/**
sl@0
    23
 @file
sl@0
    24
*/
sl@0
    25
sl@0
    26
#include "sha2impl.h"
sl@0
    27
sl@0
    28
#include <cryptospi/hashplugin.h>
sl@0
    29
#include "pluginconfig.h"
sl@0
    30
#include "sha224and256impl.h"
sl@0
    31
#include "sha384and512impl.h"
sl@0
    32
sl@0
    33
using namespace SoftwareCrypto;
sl@0
    34
sl@0
    35
// Initial Hash Values of SHA2 algorithms
sl@0
    36
/**
sl@0
    37
 * Initial Hash Value for SHA-224
sl@0
    38
 * 
sl@0
    39
 * These words were obtained by taking the first thirty-two bits 
sl@0
    40
 * of the fractional parts of the square roots of the first eight
sl@0
    41
 * prime numbers.
sl@0
    42
 * 
sl@0
    43
 * FIPS 180-2 Appendix
sl@0
    44
 * FIPS 180-3 Section 5.3.2
sl@0
    45
 */
sl@0
    46
const TUint SHA224InitVals[] = 
sl@0
    47
    			{
sl@0
    48
    			0xc1059ed8, // A
sl@0
    49
    			0x367cd507, // B
sl@0
    50
    			0x3070dd17, // C
sl@0
    51
    			0xf70e5939, // D
sl@0
    52
    			0xffc00b31, // E
sl@0
    53
    			0x68581511, // F
sl@0
    54
    			0x64f98fa7, // G
sl@0
    55
    			0xbefa4fa4  // H
sl@0
    56
    			};
sl@0
    57
sl@0
    58
/**
sl@0
    59
 * Initial Hash Value for SHA-256
sl@0
    60
 * 
sl@0
    61
 * These words were obtained by taking the first thirty-two bits 
sl@0
    62
 * of the fractional parts of the square roots of the first eight
sl@0
    63
 * prime numbers.
sl@0
    64
 * 
sl@0
    65
 * FIPS 180-2 Section 5.3.2
sl@0
    66
 */
sl@0
    67
const TUint SHA256InitVals[] = 
sl@0
    68
    			{
sl@0
    69
    			0x6a09e667, // A
sl@0
    70
    			0xbb67ae85, // B
sl@0
    71
    			0x3c6ef372, // C
sl@0
    72
    			0xa54ff53a, // D
sl@0
    73
    			0x510e527f, // E
sl@0
    74
    			0x9b05688c, // F
sl@0
    75
    			0x1f83d9ab, // G
sl@0
    76
    			0x5be0cd19  // H
sl@0
    77
    			};
sl@0
    78
sl@0
    79
/**
sl@0
    80
 * Initial Hash Value for SHA-384
sl@0
    81
 * 
sl@0
    82
 *  These words were obtained by taking the first sixty-four bits 
sl@0
    83
 * of the fractional parts of the square roots of the first eight
sl@0
    84
 * prime numbers.
sl@0
    85
 * 
sl@0
    86
 * FIPS 180-2 Section 5.3.3
sl@0
    87
 */
sl@0
    88
const TUint64 SHA384InitVals[] = 
sl@0
    89
    			{
sl@0
    90
    			UI64LIT(0xcbbb9d5dc1059ed8), // A
sl@0
    91
    			UI64LIT(0x629a292a367cd507), // B
sl@0
    92
    			UI64LIT(0x9159015a3070dd17), // C
sl@0
    93
    			UI64LIT(0x152fecd8f70e5939), // D
sl@0
    94
    			UI64LIT(0x67332667ffc00b31), // E
sl@0
    95
    			UI64LIT(0x8eb44a8768581511), // F
sl@0
    96
    			UI64LIT(0xdb0c2e0d64f98fa7), // G
sl@0
    97
    			UI64LIT(0x47b5481dbefa4fa4)  // H
sl@0
    98
    			};
sl@0
    99
sl@0
   100
/**
sl@0
   101
 * Initial Hash Value for SHA-512
sl@0
   102
 * 
sl@0
   103
 * These words were obtained by taking the first sixty-four bits 
sl@0
   104
 * of the fractional parts of the square roots of the first eight
sl@0
   105
 * prime numbers.
sl@0
   106
 * 
sl@0
   107
 * FIPS 180-2 Section 5.3.4
sl@0
   108
 */
sl@0
   109
const TUint64 SHA512InitVals[] = 
sl@0
   110
    			{
sl@0
   111
    			UI64LIT(0x6a09e667f3bcc908), // A
sl@0
   112
    			UI64LIT(0xbb67ae8584caa73b), // B
sl@0
   113
    			UI64LIT(0x3c6ef372fe94f82b), // C
sl@0
   114
    			UI64LIT(0xa54ff53a5f1d36f1), // D
sl@0
   115
    			UI64LIT(0x510e527fade682d1), // E
sl@0
   116
    			UI64LIT(0x9b05688c2b3e6c1f), // F
sl@0
   117
    			UI64LIT(0x1f83d9abfb41bd6b), // G
sl@0
   118
    			UI64LIT(0x5be0cd19137e2179)  // H
sl@0
   119
    			};
sl@0
   120
sl@0
   121
	
sl@0
   122
CSHA2Impl* CSHA2Impl::NewL(TInt32 aAlgorithmId)
sl@0
   123
	{
sl@0
   124
	CSHA2Impl* self = CSHA2Impl::NewLC(aAlgorithmId);
sl@0
   125
	CleanupStack::Pop(self);
sl@0
   126
	return self;						
sl@0
   127
	}
sl@0
   128
														
sl@0
   129
CSHA2Impl* CSHA2Impl::NewLC(TInt32 aAlgorithmId)
sl@0
   130
	{
sl@0
   131
	CSHA2Impl* self = new (ELeave) CSHA2Impl();
sl@0
   132
	CleanupStack::PushL(self);
sl@0
   133
	self->ConstructL(aAlgorithmId);
sl@0
   134
	return self;						
sl@0
   135
	}
sl@0
   136
														
sl@0
   137
void CSHA2Impl::ConstructL(const CSHA2Impl& aSHA2Impl)
sl@0
   138
	{
sl@0
   139
	iImplementationUid = aSHA2Impl.iImplementationUid;
sl@0
   140
	iInitValues = aSHA2Impl.iInitValues;
sl@0
   141
	iHashSize = aSHA2Impl.iHashSize;
sl@0
   142
    switch(iImplementationUid.iUid)
sl@0
   143
		{
sl@0
   144
		case KCryptoPluginSha224:
sl@0
   145
		case KCryptoPluginSha256:
sl@0
   146
			{
sl@0
   147
			const CSHA224And256Impl* const impl = static_cast<CSHA224And256Impl*>(aSHA2Impl.iImplementation);
sl@0
   148
			iImplementation = new (ELeave) CSHA224And256Impl(*impl);
sl@0
   149
			break;
sl@0
   150
			}
sl@0
   151
		case KCryptoPluginSha384:
sl@0
   152
		case KCryptoPluginSha512:
sl@0
   153
			{
sl@0
   154
			const CSHA384And512Impl* const impl = static_cast<CSHA384And512Impl*>(aSHA2Impl.iImplementation);
sl@0
   155
			iImplementation = new (ELeave) CSHA384And512Impl(*impl);
sl@0
   156
			break;
sl@0
   157
			}
sl@0
   158
		default:
sl@0
   159
			{
sl@0
   160
			User::Leave(KErrNotSupported);
sl@0
   161
			}
sl@0
   162
		}
sl@0
   163
	}
sl@0
   164
sl@0
   165
void CSHA2Impl::ConstructL(TInt32 aAlgorithmId)
sl@0
   166
    {
sl@0
   167
    switch(aAlgorithmId)
sl@0
   168
    	{
sl@0
   169
    	case KCryptoPluginSha224:
sl@0
   170
    		{
sl@0
   171
    		iImplementation = CSHA224And256Impl::NewL();
sl@0
   172
    		iInitValues = SHA224InitVals;
sl@0
   173
    		iImplementationUid = KCryptoPluginSha224Uid;
sl@0
   174
    		iHashSize = KSHA224HashSize;
sl@0
   175
    		break;
sl@0
   176
    		}
sl@0
   177
    	case KCryptoPluginSha256:
sl@0
   178
    		{
sl@0
   179
    		iImplementation = CSHA224And256Impl::NewL();
sl@0
   180
    		iInitValues = SHA256InitVals;
sl@0
   181
    		iImplementationUid = KCryptoPluginSha256Uid;
sl@0
   182
    		iHashSize = KSHA256HashSize;
sl@0
   183
    		break;
sl@0
   184
    		}
sl@0
   185
    	case KCryptoPluginSha384:
sl@0
   186
    		{
sl@0
   187
    		iImplementation = CSHA384And512Impl::NewL();
sl@0
   188
    		iInitValues = SHA384InitVals;
sl@0
   189
    		iImplementationUid = KCryptoPluginSha384Uid;
sl@0
   190
    		iHashSize = KSHA384HashSize;
sl@0
   191
    		break;
sl@0
   192
    		}
sl@0
   193
    	case KCryptoPluginSha512:
sl@0
   194
    		{
sl@0
   195
    		iImplementation = CSHA384And512Impl::NewL();
sl@0
   196
    		iInitValues = SHA512InitVals;
sl@0
   197
    		iImplementationUid = KCryptoPluginSha512Uid;
sl@0
   198
    		iHashSize = KSHA512HashSize;
sl@0
   199
    		break;
sl@0
   200
    		}
sl@0
   201
    	default:
sl@0
   202
    		{
sl@0
   203
    		User::Leave(KErrNotSupported);
sl@0
   204
    		}
sl@0
   205
    	}
sl@0
   206
    
sl@0
   207
    Reset();
sl@0
   208
    }
sl@0
   209
sl@0
   210
CSHA2Impl::~CSHA2Impl()
sl@0
   211
	{
sl@0
   212
	delete iImplementation;
sl@0
   213
	}
sl@0
   214
sl@0
   215
void CSHA2Impl::Reset()
sl@0
   216
	{
sl@0
   217
	iImplementation->Reset(iInitValues);
sl@0
   218
	}
sl@0
   219
sl@0
   220
void CSHA2Impl::Close()
sl@0
   221
	{
sl@0
   222
	delete this;	
sl@0
   223
	}
sl@0
   224
sl@0
   225
MHash* CSHA2Impl::ReplicateL()
sl@0
   226
	{	 
sl@0
   227
	return CSHA2Impl::NewL(iImplementationUid.iUid);
sl@0
   228
	}
sl@0
   229
	
sl@0
   230
MHash* CSHA2Impl::CopyL()
sl@0
   231
	{
sl@0
   232
	CSHA2Impl* hash = new(ELeave) CSHA2Impl();
sl@0
   233
	CleanupStack::PushL(hash);
sl@0
   234
	hash->ConstructL(*this);
sl@0
   235
	CleanupStack::Pop(hash);
sl@0
   236
	return hash;
sl@0
   237
	}
sl@0
   238
sl@0
   239
TUid CSHA2Impl::ImplementationUid()
sl@0
   240
	{
sl@0
   241
	return iImplementationUid; 
sl@0
   242
	}
sl@0
   243
sl@0
   244
void CSHA2Impl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
sl@0
   245
	{
sl@0
   246
	aPluginCharacteristics=NULL;
sl@0
   247
	TInt hashNum=sizeof(KHashCharacteristics)/sizeof(THashCharacteristics*);
sl@0
   248
	for (TInt i=0;i<hashNum;i++)
sl@0
   249
		{
sl@0
   250
		if (KHashCharacteristics[i]->cmn.iImplementationUID == ImplementationUid().iUid)
sl@0
   251
			{
sl@0
   252
			aPluginCharacteristics = KHashCharacteristics[i];
sl@0
   253
			break;
sl@0
   254
			}
sl@0
   255
		}	
sl@0
   256
	}
sl@0
   257
sl@0
   258
CExtendedCharacteristics* CSHA2Impl::CreateExtendedCharacteristicsL()
sl@0
   259
	{
sl@0
   260
	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
sl@0
   261
	// for exclusive use and are not CERTIFIED to be standards compliant.
sl@0
   262
	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
sl@0
   263
	}
sl@0
   264
sl@0
   265
const CExtendedCharacteristics* CSHA2Impl::GetExtendedCharacteristicsL()
sl@0
   266
	{
sl@0
   267
	return CSHA2Impl::CreateExtendedCharacteristicsL();
sl@0
   268
	}	
sl@0
   269
sl@0
   270
TPtrC8 CSHA2Impl::Hash(const TDesC8& aMessage)
sl@0
   271
	{
sl@0
   272
	TPtrC8 ptr(KNullDesC8());
sl@0
   273
	iImplementation->Update(aMessage.Ptr(),aMessage.Size());
sl@0
   274
	iImplementation->StoreState();
sl@0
   275
	ptr.Set(iImplementation->Final().Ptr(), iHashSize);
sl@0
   276
	iImplementation->RestoreState();
sl@0
   277
	return ptr;
sl@0
   278
	}
sl@0
   279
	
sl@0
   280
void CSHA2Impl::Update(const TDesC8& aMessage)
sl@0
   281
	{
sl@0
   282
	iImplementation->Update(aMessage.Ptr(),aMessage.Size());	
sl@0
   283
	}
sl@0
   284
	
sl@0
   285
TPtrC8 CSHA2Impl::Final(const TDesC8& aMessage)
sl@0
   286
	{
sl@0
   287
	TPtrC8 ptr(KNullDesC8());
sl@0
   288
	if (aMessage!=KNullDesC8())
sl@0
   289
		{
sl@0
   290
		iImplementation->Update(aMessage.Ptr(),aMessage.Size());			
sl@0
   291
		}
sl@0
   292
	ptr.Set(iImplementation->Final().Ptr(), iHashSize);
sl@0
   293
	Reset();
sl@0
   294
	return ptr;
sl@0
   295
	}
sl@0
   296
sl@0
   297
void CSHA2Impl::RestoreState()
sl@0
   298
	{
sl@0
   299
	iImplementation->RestoreState();
sl@0
   300
	}
sl@0
   301
sl@0
   302
void CSHA2Impl::StoreState()
sl@0
   303
	{
sl@0
   304
	iImplementation->StoreState();
sl@0
   305
	}
sl@0
   306
sl@0
   307
// Implemented in hmacimpl.cpp or softwarehashbase.cpp
sl@0
   308
// but required as derived from MHash. No coverage here.
sl@0
   309
#ifdef _BullseyeCoverage
sl@0
   310
#pragma suppress_warnings on
sl@0
   311
#pragma BullseyeCoverage off
sl@0
   312
#pragma suppress_warnings off
sl@0
   313
#endif
sl@0
   314
sl@0
   315
TAny* CSHA2Impl::GetExtension(TUid /*aExtensionId*/)
sl@0
   316
	{
sl@0
   317
	return NULL;	
sl@0
   318
	}
sl@0
   319
sl@0
   320
void CSHA2Impl::SetOperationModeL(TUid /*aOperationMode*/)
sl@0
   321
	{
sl@0
   322
	User::Leave(KErrNotSupported);
sl@0
   323
	}
sl@0
   324
sl@0
   325
void CSHA2Impl::SetKeyL(const CKey& /*aKey*/)
sl@0
   326
	{
sl@0
   327
	User::Leave(KErrNotSupported);
sl@0
   328
	}
sl@0
   329