1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/sha2impl.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,329 @@
1.4 +/*
1.5 +* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +* software SHA2 implementation
1.19 +* RFC 4634 (US Secure Hash Algorithms (SHA and HMAC-SHA))
1.20 +* FIPS 180-2 (With change notice)
1.21 +*
1.22 +*/
1.23 +
1.24 +
1.25 +/**
1.26 + @file
1.27 +*/
1.28 +
1.29 +#include "sha2impl.h"
1.30 +
1.31 +#include <cryptospi/hashplugin.h>
1.32 +#include "pluginconfig.h"
1.33 +#include "sha224and256impl.h"
1.34 +#include "sha384and512impl.h"
1.35 +
1.36 +using namespace SoftwareCrypto;
1.37 +
1.38 +// Initial Hash Values of SHA2 algorithms
1.39 +/**
1.40 + * Initial Hash Value for SHA-224
1.41 + *
1.42 + * These words were obtained by taking the first thirty-two bits
1.43 + * of the fractional parts of the square roots of the first eight
1.44 + * prime numbers.
1.45 + *
1.46 + * FIPS 180-2 Appendix
1.47 + * FIPS 180-3 Section 5.3.2
1.48 + */
1.49 +const TUint SHA224InitVals[] =
1.50 + {
1.51 + 0xc1059ed8, // A
1.52 + 0x367cd507, // B
1.53 + 0x3070dd17, // C
1.54 + 0xf70e5939, // D
1.55 + 0xffc00b31, // E
1.56 + 0x68581511, // F
1.57 + 0x64f98fa7, // G
1.58 + 0xbefa4fa4 // H
1.59 + };
1.60 +
1.61 +/**
1.62 + * Initial Hash Value for SHA-256
1.63 + *
1.64 + * These words were obtained by taking the first thirty-two bits
1.65 + * of the fractional parts of the square roots of the first eight
1.66 + * prime numbers.
1.67 + *
1.68 + * FIPS 180-2 Section 5.3.2
1.69 + */
1.70 +const TUint SHA256InitVals[] =
1.71 + {
1.72 + 0x6a09e667, // A
1.73 + 0xbb67ae85, // B
1.74 + 0x3c6ef372, // C
1.75 + 0xa54ff53a, // D
1.76 + 0x510e527f, // E
1.77 + 0x9b05688c, // F
1.78 + 0x1f83d9ab, // G
1.79 + 0x5be0cd19 // H
1.80 + };
1.81 +
1.82 +/**
1.83 + * Initial Hash Value for SHA-384
1.84 + *
1.85 + * These words were obtained by taking the first sixty-four bits
1.86 + * of the fractional parts of the square roots of the first eight
1.87 + * prime numbers.
1.88 + *
1.89 + * FIPS 180-2 Section 5.3.3
1.90 + */
1.91 +const TUint64 SHA384InitVals[] =
1.92 + {
1.93 + UI64LIT(0xcbbb9d5dc1059ed8), // A
1.94 + UI64LIT(0x629a292a367cd507), // B
1.95 + UI64LIT(0x9159015a3070dd17), // C
1.96 + UI64LIT(0x152fecd8f70e5939), // D
1.97 + UI64LIT(0x67332667ffc00b31), // E
1.98 + UI64LIT(0x8eb44a8768581511), // F
1.99 + UI64LIT(0xdb0c2e0d64f98fa7), // G
1.100 + UI64LIT(0x47b5481dbefa4fa4) // H
1.101 + };
1.102 +
1.103 +/**
1.104 + * Initial Hash Value for SHA-512
1.105 + *
1.106 + * These words were obtained by taking the first sixty-four bits
1.107 + * of the fractional parts of the square roots of the first eight
1.108 + * prime numbers.
1.109 + *
1.110 + * FIPS 180-2 Section 5.3.4
1.111 + */
1.112 +const TUint64 SHA512InitVals[] =
1.113 + {
1.114 + UI64LIT(0x6a09e667f3bcc908), // A
1.115 + UI64LIT(0xbb67ae8584caa73b), // B
1.116 + UI64LIT(0x3c6ef372fe94f82b), // C
1.117 + UI64LIT(0xa54ff53a5f1d36f1), // D
1.118 + UI64LIT(0x510e527fade682d1), // E
1.119 + UI64LIT(0x9b05688c2b3e6c1f), // F
1.120 + UI64LIT(0x1f83d9abfb41bd6b), // G
1.121 + UI64LIT(0x5be0cd19137e2179) // H
1.122 + };
1.123 +
1.124 +
1.125 +CSHA2Impl* CSHA2Impl::NewL(TInt32 aAlgorithmId)
1.126 + {
1.127 + CSHA2Impl* self = CSHA2Impl::NewLC(aAlgorithmId);
1.128 + CleanupStack::Pop(self);
1.129 + return self;
1.130 + }
1.131 +
1.132 +CSHA2Impl* CSHA2Impl::NewLC(TInt32 aAlgorithmId)
1.133 + {
1.134 + CSHA2Impl* self = new (ELeave) CSHA2Impl();
1.135 + CleanupStack::PushL(self);
1.136 + self->ConstructL(aAlgorithmId);
1.137 + return self;
1.138 + }
1.139 +
1.140 +void CSHA2Impl::ConstructL(const CSHA2Impl& aSHA2Impl)
1.141 + {
1.142 + iImplementationUid = aSHA2Impl.iImplementationUid;
1.143 + iInitValues = aSHA2Impl.iInitValues;
1.144 + iHashSize = aSHA2Impl.iHashSize;
1.145 + switch(iImplementationUid.iUid)
1.146 + {
1.147 + case KCryptoPluginSha224:
1.148 + case KCryptoPluginSha256:
1.149 + {
1.150 + const CSHA224And256Impl* const impl = static_cast<CSHA224And256Impl*>(aSHA2Impl.iImplementation);
1.151 + iImplementation = new (ELeave) CSHA224And256Impl(*impl);
1.152 + break;
1.153 + }
1.154 + case KCryptoPluginSha384:
1.155 + case KCryptoPluginSha512:
1.156 + {
1.157 + const CSHA384And512Impl* const impl = static_cast<CSHA384And512Impl*>(aSHA2Impl.iImplementation);
1.158 + iImplementation = new (ELeave) CSHA384And512Impl(*impl);
1.159 + break;
1.160 + }
1.161 + default:
1.162 + {
1.163 + User::Leave(KErrNotSupported);
1.164 + }
1.165 + }
1.166 + }
1.167 +
1.168 +void CSHA2Impl::ConstructL(TInt32 aAlgorithmId)
1.169 + {
1.170 + switch(aAlgorithmId)
1.171 + {
1.172 + case KCryptoPluginSha224:
1.173 + {
1.174 + iImplementation = CSHA224And256Impl::NewL();
1.175 + iInitValues = SHA224InitVals;
1.176 + iImplementationUid = KCryptoPluginSha224Uid;
1.177 + iHashSize = KSHA224HashSize;
1.178 + break;
1.179 + }
1.180 + case KCryptoPluginSha256:
1.181 + {
1.182 + iImplementation = CSHA224And256Impl::NewL();
1.183 + iInitValues = SHA256InitVals;
1.184 + iImplementationUid = KCryptoPluginSha256Uid;
1.185 + iHashSize = KSHA256HashSize;
1.186 + break;
1.187 + }
1.188 + case KCryptoPluginSha384:
1.189 + {
1.190 + iImplementation = CSHA384And512Impl::NewL();
1.191 + iInitValues = SHA384InitVals;
1.192 + iImplementationUid = KCryptoPluginSha384Uid;
1.193 + iHashSize = KSHA384HashSize;
1.194 + break;
1.195 + }
1.196 + case KCryptoPluginSha512:
1.197 + {
1.198 + iImplementation = CSHA384And512Impl::NewL();
1.199 + iInitValues = SHA512InitVals;
1.200 + iImplementationUid = KCryptoPluginSha512Uid;
1.201 + iHashSize = KSHA512HashSize;
1.202 + break;
1.203 + }
1.204 + default:
1.205 + {
1.206 + User::Leave(KErrNotSupported);
1.207 + }
1.208 + }
1.209 +
1.210 + Reset();
1.211 + }
1.212 +
1.213 +CSHA2Impl::~CSHA2Impl()
1.214 + {
1.215 + delete iImplementation;
1.216 + }
1.217 +
1.218 +void CSHA2Impl::Reset()
1.219 + {
1.220 + iImplementation->Reset(iInitValues);
1.221 + }
1.222 +
1.223 +void CSHA2Impl::Close()
1.224 + {
1.225 + delete this;
1.226 + }
1.227 +
1.228 +MHash* CSHA2Impl::ReplicateL()
1.229 + {
1.230 + return CSHA2Impl::NewL(iImplementationUid.iUid);
1.231 + }
1.232 +
1.233 +MHash* CSHA2Impl::CopyL()
1.234 + {
1.235 + CSHA2Impl* hash = new(ELeave) CSHA2Impl();
1.236 + CleanupStack::PushL(hash);
1.237 + hash->ConstructL(*this);
1.238 + CleanupStack::Pop(hash);
1.239 + return hash;
1.240 + }
1.241 +
1.242 +TUid CSHA2Impl::ImplementationUid()
1.243 + {
1.244 + return iImplementationUid;
1.245 + }
1.246 +
1.247 +void CSHA2Impl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
1.248 + {
1.249 + aPluginCharacteristics=NULL;
1.250 + TInt hashNum=sizeof(KHashCharacteristics)/sizeof(THashCharacteristics*);
1.251 + for (TInt i=0;i<hashNum;i++)
1.252 + {
1.253 + if (KHashCharacteristics[i]->cmn.iImplementationUID == ImplementationUid().iUid)
1.254 + {
1.255 + aPluginCharacteristics = KHashCharacteristics[i];
1.256 + break;
1.257 + }
1.258 + }
1.259 + }
1.260 +
1.261 +CExtendedCharacteristics* CSHA2Impl::CreateExtendedCharacteristicsL()
1.262 + {
1.263 + // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
1.264 + // for exclusive use and are not CERTIFIED to be standards compliant.
1.265 + return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
1.266 + }
1.267 +
1.268 +const CExtendedCharacteristics* CSHA2Impl::GetExtendedCharacteristicsL()
1.269 + {
1.270 + return CSHA2Impl::CreateExtendedCharacteristicsL();
1.271 + }
1.272 +
1.273 +TPtrC8 CSHA2Impl::Hash(const TDesC8& aMessage)
1.274 + {
1.275 + TPtrC8 ptr(KNullDesC8());
1.276 + iImplementation->Update(aMessage.Ptr(),aMessage.Size());
1.277 + iImplementation->StoreState();
1.278 + ptr.Set(iImplementation->Final().Ptr(), iHashSize);
1.279 + iImplementation->RestoreState();
1.280 + return ptr;
1.281 + }
1.282 +
1.283 +void CSHA2Impl::Update(const TDesC8& aMessage)
1.284 + {
1.285 + iImplementation->Update(aMessage.Ptr(),aMessage.Size());
1.286 + }
1.287 +
1.288 +TPtrC8 CSHA2Impl::Final(const TDesC8& aMessage)
1.289 + {
1.290 + TPtrC8 ptr(KNullDesC8());
1.291 + if (aMessage!=KNullDesC8())
1.292 + {
1.293 + iImplementation->Update(aMessage.Ptr(),aMessage.Size());
1.294 + }
1.295 + ptr.Set(iImplementation->Final().Ptr(), iHashSize);
1.296 + Reset();
1.297 + return ptr;
1.298 + }
1.299 +
1.300 +void CSHA2Impl::RestoreState()
1.301 + {
1.302 + iImplementation->RestoreState();
1.303 + }
1.304 +
1.305 +void CSHA2Impl::StoreState()
1.306 + {
1.307 + iImplementation->StoreState();
1.308 + }
1.309 +
1.310 +// Implemented in hmacimpl.cpp or softwarehashbase.cpp
1.311 +// but required as derived from MHash. No coverage here.
1.312 +#ifdef _BullseyeCoverage
1.313 +#pragma suppress_warnings on
1.314 +#pragma BullseyeCoverage off
1.315 +#pragma suppress_warnings off
1.316 +#endif
1.317 +
1.318 +TAny* CSHA2Impl::GetExtension(TUid /*aExtensionId*/)
1.319 + {
1.320 + return NULL;
1.321 + }
1.322 +
1.323 +void CSHA2Impl::SetOperationModeL(TUid /*aOperationMode*/)
1.324 + {
1.325 + User::Leave(KErrNotSupported);
1.326 + }
1.327 +
1.328 +void CSHA2Impl::SetKeyL(const CKey& /*aKey*/)
1.329 + {
1.330 + User::Leave(KErrNotSupported);
1.331 + }
1.332 +