os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/rsaverifyimpl.cpp
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/rsaverifyimpl.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,143 @@
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 "rsaverifyimpl.h"
1.23 +#include "pluginconfig.h"
1.24 +#include "rsafunction.h"
1.25 +
1.26 +using namespace SoftwareCrypto;
1.27 +
1.28 +// Implementation of CRSAVerifierImpl
1.29 +CRSAVerifierImpl* CRSAVerifierImpl::NewL(const CKey& aKey, TUid aPaddingMode)
1.30 + {
1.31 + CRSAVerifierImpl* self = CRSAVerifierImpl::NewLC(aKey, aPaddingMode);
1.32 + CleanupStack::Pop(self);
1.33 + return self;
1.34 + }
1.35 +
1.36 +CRSAVerifierImpl* CRSAVerifierImpl::NewLC(const CKey& aKey, TUid aPaddingMode)
1.37 + {
1.38 + CRSAVerifierImpl* self = new(ELeave) CRSAVerifierImpl(aPaddingMode);
1.39 + CleanupStack::PushL(self);
1.40 + self->ConstructL(aKey);
1.41 + return self;
1.42 + }
1.43 +
1.44 +CRSAVerifierImpl::CRSAVerifierImpl(TUid aPaddingMode)
1.45 + : iPaddingMode(aPaddingMode)
1.46 + {
1.47 + }
1.48 +
1.49 +CRSAVerifierImpl::~CRSAVerifierImpl()
1.50 + {
1.51 + delete iPadding;
1.52 + }
1.53 +
1.54 +void CRSAVerifierImpl::ConstructL(const CKey& aKey)
1.55 + {
1.56 + CVerifierImpl::ConstructL(aKey);
1.57 + SetPaddingModeL(iPaddingMode);
1.58 + }
1.59 +
1.60 +CExtendedCharacteristics* CRSAVerifierImpl::CreateExtendedCharacteristicsL()
1.61 + {
1.62 + // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
1.63 + // for exclusive use and are not CERTIFIED to be standards compliant.
1.64 + return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
1.65 + }
1.66 +
1.67 +const CExtendedCharacteristics* CRSAVerifierImpl::GetExtendedCharacteristicsL()
1.68 + {
1.69 + return CRSAVerifierImpl::CreateExtendedCharacteristicsL();
1.70 + }
1.71 +
1.72 +TUid CRSAVerifierImpl::ImplementationUid() const
1.73 + {
1.74 + return KCryptoPluginRsaVerifierUid;
1.75 + }
1.76 +
1.77 +void CRSAVerifierImpl::SetPaddingModeL(TUid aPaddingMode)
1.78 + {
1.79 + CPadding* padding(0);
1.80 + switch (aPaddingMode.iUid)
1.81 + {
1.82 + case KPaddingModeNone:
1.83 + padding = CPaddingNone::NewL(GetMaximumOutputLengthL());
1.84 + break;
1.85 + case KPaddingModePkcs1_v1_5_Signature:
1.86 + padding = CPaddingPKCS1Signature::NewL(GetMaximumOutputLengthL());
1.87 + break;
1.88 + default:
1.89 + User::Leave(KErrNotSupported);
1.90 + }
1.91 + delete iPadding;
1.92 + iPadding = padding;
1.93 + iPaddingMode = aPaddingMode;
1.94 + Reset();
1.95 + }
1.96 +
1.97 +void CRSAVerifierImpl::SetKeyL(const CKey& aPublicKey)
1.98 + {
1.99 + DoSetKeyL(aPublicKey);
1.100 + Reset();
1.101 + }
1.102 +
1.103 +TInt CRSAVerifierImpl::GetMaximumInputLengthL() const
1.104 + {
1.105 + return GetMaximumOutputLengthL() - iPadding->MinPaddingLength();
1.106 + }
1.107 +
1.108 +TInt CRSAVerifierImpl::GetMaximumOutputLengthL() const
1.109 + {
1.110 + const TInteger& paramN = iKey->GetBigIntL(KRsaKeyParameterNUid);
1.111 + return paramN.ByteCount();
1.112 + }
1.113 +
1.114 +void CRSAVerifierImpl::VerifyL(const TDesC8& aInput, const CCryptoParams& aSignature, TBool& aVerificationResult)
1.115 + {
1.116 + HBufC8* output = NULL;
1.117 + InverseSignL(output, aSignature);
1.118 + CleanupStack::PushL(output);
1.119 +
1.120 + // is the original hash the same as the hash extracted from the signature
1.121 + aVerificationResult = EFalse;
1.122 + if (!output->Compare(aInput))
1.123 + {
1.124 + aVerificationResult = ETrue;
1.125 + }
1.126 + CleanupStack::PopAndDestroy(output);
1.127 + }
1.128 +
1.129 +void CRSAVerifierImpl::InverseSignL(HBufC8*& aOutput, const CCryptoParams& aSignature)
1.130 + {
1.131 + // extract the original hash from the signature
1.132 + const TInteger& signature = aSignature.GetBigIntL(KRsaSignatureParameterSUid);
1.133 + RInteger output;
1.134 + RSAFunction::VerifyL(*iKey, signature, output);
1.135 + CleanupClosePushL(output);
1.136 +
1.137 + // format the extracted hash so it can be compared with the original hash
1.138 + HBufC8* paddedHashPtr = output.BufferLC();
1.139 + aOutput = HBufC8::NewLC(GetMaximumOutputLengthL());
1.140 + TPtr8 unpaddedHash = aOutput->Des();
1.141 +
1.142 + iPadding->UnPadL(*paddedHashPtr, unpaddedHash);
1.143 +
1.144 + CleanupStack::Pop(aOutput);
1.145 + CleanupStack::PopAndDestroy(2, &output);
1.146 + }