os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/dsaverifyimpl.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/dsaverifyimpl.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,160 @@
     1.4 +/*
     1.5 +* Copyright (c) 2006-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 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "dsaverifyimpl.h"
    1.23 +#include "pluginconfig.h"
    1.24 +
    1.25 +using namespace SoftwareCrypto;
    1.26 +
    1.27 +// Implementation of CDSAVerifierImpl
    1.28 +CDSAVerifierImpl* CDSAVerifierImpl::NewL(const CKey& aKey)
    1.29 +	{
    1.30 +	CDSAVerifierImpl* self = CDSAVerifierImpl::NewLC(aKey);
    1.31 +	CleanupStack::Pop(self);
    1.32 +	return self;
    1.33 +	}
    1.34 +	
    1.35 +CDSAVerifierImpl* CDSAVerifierImpl::NewLC(const CKey& aKey)
    1.36 +	{
    1.37 +	CDSAVerifierImpl* self = new(ELeave) CDSAVerifierImpl();
    1.38 +	CleanupStack::PushL(self);
    1.39 +	self->ConstructL(aKey);
    1.40 +	return self;
    1.41 +	}
    1.42 +	
    1.43 +CDSAVerifierImpl::CDSAVerifierImpl() 
    1.44 +	{
    1.45 +	}
    1.46 +
    1.47 +CDSAVerifierImpl::~CDSAVerifierImpl()
    1.48 +	{
    1.49 +	}
    1.50 +	
    1.51 +void CDSAVerifierImpl::ConstructL(const CKey& aKey)
    1.52 +	{
    1.53 +	CVerifierImpl::ConstructL(aKey);
    1.54 +	}
    1.55 +	
    1.56 +CExtendedCharacteristics* CDSAVerifierImpl::CreateExtendedCharacteristicsL()
    1.57 +	{
    1.58 +	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
    1.59 +	// for exclusive use and are not CERTIFIED to be standards compliant.
    1.60 +	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
    1.61 +	}
    1.62 +	
    1.63 +const CExtendedCharacteristics* CDSAVerifierImpl::GetExtendedCharacteristicsL()
    1.64 +	{
    1.65 +	return CDSAVerifierImpl::CreateExtendedCharacteristicsL();
    1.66 +	}
    1.67 +	
    1.68 +TUid CDSAVerifierImpl::ImplementationUid() const
    1.69 +	{
    1.70 +	return KCryptoPluginDsaVerifierUid;
    1.71 +	}
    1.72 +	
    1.73 +void CDSAVerifierImpl::SetKeyL(const CKey& aPublicKey)
    1.74 +	{
    1.75 +	DoSetKeyL(aPublicKey);
    1.76 +	Reset();	
    1.77 +	}
    1.78 +
    1.79 +TInt CDSAVerifierImpl::GetMaximumInputLengthL() const
    1.80 +	{
    1.81 +	return KSha1HashLength;
    1.82 +	}
    1.83 +
    1.84 +void CDSAVerifierImpl::VerifyL(const TDesC8& aInput, const CCryptoParams& aSignature, TBool& aVerificationResult)
    1.85 +	{
    1.86 +	//Retrieve the parameter Q from the key	
    1.87 +	const TInteger& tQ=iKey->GetBigIntL(KDsaKeyParameterQUid);
    1.88 +
    1.89 +	//see HAC 11.56 or DSS section 6
    1.90 +	//I'll follow HAC as I like the description better
    1.91 +
    1.92 +	// a) Obtain A's authenticate public key
    1.93 +
    1.94 +	// b) Verify that 0 < r < q and 0 < s < q; if not reject signature
    1.95 +
    1.96 +	//Retrieve the R&S in DSA signature from the array
    1.97 +
    1.98 +	const TInteger& tR=aSignature.GetBigIntL(KDsaSignatureParameterRUid);
    1.99 +	const TInteger& tS=aSignature.GetBigIntL(KDsaSignatureParameterSUid);
   1.100 +
   1.101 +	if (tR <= 0 || tR >= tQ)
   1.102 +		{
   1.103 +		aVerificationResult=EFalse;
   1.104 +		return;
   1.105 +		}
   1.106 +	if (tS <= 0 || tS >= tQ)
   1.107 +		{
   1.108 +		aVerificationResult=EFalse;
   1.109 +		return;
   1.110 +		}
   1.111 +		
   1.112 +		
   1.113 +	// c) Compute w = s^(-1) mod q and h(m)
   1.114 +	RInteger w = tS.InverseModL(tQ);
   1.115 +	CleanupStack::PushL(w);
   1.116 +	// Note that in order to be interoperable, compliant with the DSS, and
   1.117 +	// secure, aInput must be the result of a SHA-1 hash
   1.118 +	RInteger hm = RInteger::NewL(aInput);
   1.119 +	CleanupStack::PushL(hm);
   1.120 +
   1.121 +	// d) Compute u1 = w * hm mod q and u2 = r * w mod q
   1.122 +	RInteger u1 = TInteger::ModularMultiplyL(w, hm, tQ);
   1.123 +	CleanupStack::PushL(u1);
   1.124 +
   1.125 +	RInteger u2 = TInteger::ModularMultiplyL(tR, w, tQ);
   1.126 +	CleanupStack::PushL(u2);
   1.127 +
   1.128 +	// e) Compute v = ((g^u1 * y^u2) mod p) mod q
   1.129 +	
   1.130 +	const TInteger& tG=iKey->GetBigIntL(KDsaKeyParameterGUid);
   1.131 +	const TInteger& tY=iKey->GetBigIntL(KDsaKeyParameterYUid);
   1.132 +	const TInteger& tP=iKey->GetBigIntL(KDsaKeyParameterPUid);
   1.133 +
   1.134 +	RInteger temp = TInteger::ModularExponentiateL(tG, u1, tP);
   1.135 +	CleanupStack::PushL(temp);
   1.136 +	RInteger temp1 = TInteger::ModularExponentiateL(tY, u2, tP);
   1.137 +	CleanupStack::PushL(temp1);
   1.138 +	RInteger v = TInteger::ModularMultiplyL(temp, temp1, tP);
   1.139 +	CleanupStack::PushL(v);
   1.140 +	v %= tQ;
   1.141 +
   1.142 +	// f) Accept the signature if v == r
   1.143 +	if(v == tR)
   1.144 +		{
   1.145 +		aVerificationResult = ETrue;
   1.146 +		}
   1.147 +
   1.148 +	CleanupStack::PopAndDestroy(7, &w);
   1.149 +	}
   1.150 +
   1.151 +
   1.152 +// Methods which are not supported can be excluded from the coverage.
   1.153 +#ifdef _BullseyeCoverage
   1.154 +#pragma suppress_warnings on
   1.155 +#pragma BullseyeCoverage off
   1.156 +#pragma suppress_warnings off
   1.157 +#endif
   1.158 +
   1.159 +void CDSAVerifierImpl::InverseSignL(HBufC8*& /*aOutput*/, const CCryptoParams& /*aSignature*/)
   1.160 +	{
   1.161 +	// Override in subclass
   1.162 +	User::Leave(KErrNotSupported);
   1.163 +	}