sl@0: /*
sl@0: * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0: * All rights reserved.
sl@0: * This component and the accompanying materials are made available
sl@0: * under the terms of the License "Eclipse Public License v1.0"
sl@0: * which accompanies this distribution, and is available
sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0: *
sl@0: * Initial Contributors:
sl@0: * Nokia Corporation - initial contribution.
sl@0: *
sl@0: * Contributors:
sl@0: *
sl@0: * Description: 
sl@0: *
sl@0: */
sl@0: 
sl@0: 
sl@0: #include "rsaverifyimpl.h"
sl@0: #include "pluginconfig.h"
sl@0: #include "rsafunction.h"
sl@0: 
sl@0: using namespace SoftwareCrypto;
sl@0: 
sl@0: // Implementation of CRSAVerifierImpl
sl@0: CRSAVerifierImpl* CRSAVerifierImpl::NewL(const CKey& aKey, TUid aPaddingMode)
sl@0: 	{
sl@0: 	CRSAVerifierImpl* self = CRSAVerifierImpl::NewLC(aKey, aPaddingMode);
sl@0: 	CleanupStack::Pop(self);
sl@0: 	return self;
sl@0: 	}
sl@0: 	
sl@0: CRSAVerifierImpl* CRSAVerifierImpl::NewLC(const CKey& aKey, TUid aPaddingMode)
sl@0: 	{
sl@0: 	CRSAVerifierImpl* self = new(ELeave) CRSAVerifierImpl(aPaddingMode);
sl@0: 	CleanupStack::PushL(self);
sl@0: 	self->ConstructL(aKey);
sl@0: 	return self;
sl@0: 	}
sl@0: 
sl@0: CRSAVerifierImpl::CRSAVerifierImpl(TUid aPaddingMode)
sl@0: 	: iPaddingMode(aPaddingMode)
sl@0: 	{
sl@0: 	}
sl@0: 
sl@0: CRSAVerifierImpl::~CRSAVerifierImpl()
sl@0: 	{
sl@0: 	delete iPadding;
sl@0: 	}
sl@0: 	
sl@0: void CRSAVerifierImpl::ConstructL(const CKey& aKey)
sl@0: 	{
sl@0: 	CVerifierImpl::ConstructL(aKey);
sl@0: 	SetPaddingModeL(iPaddingMode);
sl@0: 	}
sl@0: 	
sl@0: CExtendedCharacteristics* CRSAVerifierImpl::CreateExtendedCharacteristicsL()
sl@0: 	{
sl@0: 	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
sl@0: 	// for exclusive use and are not CERTIFIED to be standards compliant.
sl@0: 	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
sl@0: 	}
sl@0: 
sl@0: const CExtendedCharacteristics* CRSAVerifierImpl::GetExtendedCharacteristicsL()
sl@0: 	{
sl@0: 	return CRSAVerifierImpl::CreateExtendedCharacteristicsL();
sl@0: 	}
sl@0: 
sl@0: TUid CRSAVerifierImpl::ImplementationUid() const
sl@0: 	{
sl@0: 	return KCryptoPluginRsaVerifierUid;
sl@0: 	}
sl@0: 
sl@0: void CRSAVerifierImpl::SetPaddingModeL(TUid aPaddingMode) 
sl@0: 	{
sl@0: 	CPadding* padding(0);
sl@0: 	switch (aPaddingMode.iUid)
sl@0: 		{
sl@0: 		case KPaddingModeNone:
sl@0: 			padding = CPaddingNone::NewL(GetMaximumOutputLengthL());
sl@0: 			break;
sl@0: 		case KPaddingModePkcs1_v1_5_Signature:
sl@0: 			padding = CPaddingPKCS1Signature::NewL(GetMaximumOutputLengthL());
sl@0: 			break;
sl@0: 		default:
sl@0: 			User::Leave(KErrNotSupported);
sl@0: 		}
sl@0: 	delete iPadding;
sl@0: 	iPadding = padding;
sl@0: 	iPaddingMode = aPaddingMode;
sl@0: 	Reset();	
sl@0: 	}
sl@0: 
sl@0: void CRSAVerifierImpl::SetKeyL(const CKey& aPublicKey)
sl@0: 	{
sl@0: 	DoSetKeyL(aPublicKey);
sl@0: 	Reset();	
sl@0: 	}
sl@0: 
sl@0: TInt CRSAVerifierImpl::GetMaximumInputLengthL() const
sl@0: 	{
sl@0: 	return GetMaximumOutputLengthL() - iPadding->MinPaddingLength();	
sl@0: 	}
sl@0: 
sl@0: TInt CRSAVerifierImpl::GetMaximumOutputLengthL() const
sl@0: 	{
sl@0: 	const TInteger& paramN = iKey->GetBigIntL(KRsaKeyParameterNUid);
sl@0: 	return paramN.ByteCount();	
sl@0: 	}
sl@0: 
sl@0: void CRSAVerifierImpl::VerifyL(const TDesC8& aInput, const CCryptoParams& aSignature, TBool& aVerificationResult)
sl@0: 	{
sl@0: 	HBufC8* output = NULL;
sl@0: 	InverseSignL(output, aSignature);
sl@0: 	CleanupStack::PushL(output);
sl@0: 
sl@0: 	// is the original hash the same as the hash extracted from the signature
sl@0: 	aVerificationResult = EFalse;
sl@0: 	if (!output->Compare(aInput))
sl@0: 		{
sl@0: 		aVerificationResult = ETrue;
sl@0: 		}
sl@0: 	CleanupStack::PopAndDestroy(output);
sl@0: 	}
sl@0: 
sl@0: void CRSAVerifierImpl::InverseSignL(HBufC8*& aOutput, const CCryptoParams& aSignature)
sl@0: 	{
sl@0: 	// extract the original hash from the signature
sl@0: 	const TInteger& signature = aSignature.GetBigIntL(KRsaSignatureParameterSUid);
sl@0: 	RInteger output;
sl@0: 	RSAFunction::VerifyL(*iKey, signature, output);
sl@0: 	CleanupClosePushL(output);
sl@0: 
sl@0: 	// format the extracted hash so it can be compared with the original hash
sl@0: 	HBufC8* paddedHashPtr = output.BufferLC();
sl@0: 	aOutput = HBufC8::NewLC(GetMaximumOutputLengthL());
sl@0: 	TPtr8 unpaddedHash = aOutput->Des();
sl@0: 
sl@0: 	iPadding->UnPadL(*paddedHashPtr, unpaddedHash);
sl@0: 
sl@0: 	CleanupStack::Pop(aOutput);
sl@0: 	CleanupStack::PopAndDestroy(2, &output);
sl@0: 	}