os/security/cryptoplugins/cryptospiplugins/source/softwarecrypto/rsaverifyimpl.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) 2006-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
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "rsaverifyimpl.h"
sl@0
    20
#include "pluginconfig.h"
sl@0
    21
#include "rsafunction.h"
sl@0
    22
sl@0
    23
using namespace SoftwareCrypto;
sl@0
    24
sl@0
    25
// Implementation of CRSAVerifierImpl
sl@0
    26
CRSAVerifierImpl* CRSAVerifierImpl::NewL(const CKey& aKey, TUid aPaddingMode)
sl@0
    27
	{
sl@0
    28
	CRSAVerifierImpl* self = CRSAVerifierImpl::NewLC(aKey, aPaddingMode);
sl@0
    29
	CleanupStack::Pop(self);
sl@0
    30
	return self;
sl@0
    31
	}
sl@0
    32
	
sl@0
    33
CRSAVerifierImpl* CRSAVerifierImpl::NewLC(const CKey& aKey, TUid aPaddingMode)
sl@0
    34
	{
sl@0
    35
	CRSAVerifierImpl* self = new(ELeave) CRSAVerifierImpl(aPaddingMode);
sl@0
    36
	CleanupStack::PushL(self);
sl@0
    37
	self->ConstructL(aKey);
sl@0
    38
	return self;
sl@0
    39
	}
sl@0
    40
sl@0
    41
CRSAVerifierImpl::CRSAVerifierImpl(TUid aPaddingMode)
sl@0
    42
	: iPaddingMode(aPaddingMode)
sl@0
    43
	{
sl@0
    44
	}
sl@0
    45
sl@0
    46
CRSAVerifierImpl::~CRSAVerifierImpl()
sl@0
    47
	{
sl@0
    48
	delete iPadding;
sl@0
    49
	}
sl@0
    50
	
sl@0
    51
void CRSAVerifierImpl::ConstructL(const CKey& aKey)
sl@0
    52
	{
sl@0
    53
	CVerifierImpl::ConstructL(aKey);
sl@0
    54
	SetPaddingModeL(iPaddingMode);
sl@0
    55
	}
sl@0
    56
	
sl@0
    57
CExtendedCharacteristics* CRSAVerifierImpl::CreateExtendedCharacteristicsL()
sl@0
    58
	{
sl@0
    59
	// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
sl@0
    60
	// for exclusive use and are not CERTIFIED to be standards compliant.
sl@0
    61
	return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
sl@0
    62
	}
sl@0
    63
sl@0
    64
const CExtendedCharacteristics* CRSAVerifierImpl::GetExtendedCharacteristicsL()
sl@0
    65
	{
sl@0
    66
	return CRSAVerifierImpl::CreateExtendedCharacteristicsL();
sl@0
    67
	}
sl@0
    68
sl@0
    69
TUid CRSAVerifierImpl::ImplementationUid() const
sl@0
    70
	{
sl@0
    71
	return KCryptoPluginRsaVerifierUid;
sl@0
    72
	}
sl@0
    73
sl@0
    74
void CRSAVerifierImpl::SetPaddingModeL(TUid aPaddingMode) 
sl@0
    75
	{
sl@0
    76
	CPadding* padding(0);
sl@0
    77
	switch (aPaddingMode.iUid)
sl@0
    78
		{
sl@0
    79
		case KPaddingModeNone:
sl@0
    80
			padding = CPaddingNone::NewL(GetMaximumOutputLengthL());
sl@0
    81
			break;
sl@0
    82
		case KPaddingModePkcs1_v1_5_Signature:
sl@0
    83
			padding = CPaddingPKCS1Signature::NewL(GetMaximumOutputLengthL());
sl@0
    84
			break;
sl@0
    85
		default:
sl@0
    86
			User::Leave(KErrNotSupported);
sl@0
    87
		}
sl@0
    88
	delete iPadding;
sl@0
    89
	iPadding = padding;
sl@0
    90
	iPaddingMode = aPaddingMode;
sl@0
    91
	Reset();	
sl@0
    92
	}
sl@0
    93
sl@0
    94
void CRSAVerifierImpl::SetKeyL(const CKey& aPublicKey)
sl@0
    95
	{
sl@0
    96
	DoSetKeyL(aPublicKey);
sl@0
    97
	Reset();	
sl@0
    98
	}
sl@0
    99
sl@0
   100
TInt CRSAVerifierImpl::GetMaximumInputLengthL() const
sl@0
   101
	{
sl@0
   102
	return GetMaximumOutputLengthL() - iPadding->MinPaddingLength();	
sl@0
   103
	}
sl@0
   104
sl@0
   105
TInt CRSAVerifierImpl::GetMaximumOutputLengthL() const
sl@0
   106
	{
sl@0
   107
	const TInteger& paramN = iKey->GetBigIntL(KRsaKeyParameterNUid);
sl@0
   108
	return paramN.ByteCount();	
sl@0
   109
	}
sl@0
   110
sl@0
   111
void CRSAVerifierImpl::VerifyL(const TDesC8& aInput, const CCryptoParams& aSignature, TBool& aVerificationResult)
sl@0
   112
	{
sl@0
   113
	HBufC8* output = NULL;
sl@0
   114
	InverseSignL(output, aSignature);
sl@0
   115
	CleanupStack::PushL(output);
sl@0
   116
sl@0
   117
	// is the original hash the same as the hash extracted from the signature
sl@0
   118
	aVerificationResult = EFalse;
sl@0
   119
	if (!output->Compare(aInput))
sl@0
   120
		{
sl@0
   121
		aVerificationResult = ETrue;
sl@0
   122
		}
sl@0
   123
	CleanupStack::PopAndDestroy(output);
sl@0
   124
	}
sl@0
   125
sl@0
   126
void CRSAVerifierImpl::InverseSignL(HBufC8*& aOutput, const CCryptoParams& aSignature)
sl@0
   127
	{
sl@0
   128
	// extract the original hash from the signature
sl@0
   129
	const TInteger& signature = aSignature.GetBigIntL(KRsaSignatureParameterSUid);
sl@0
   130
	RInteger output;
sl@0
   131
	RSAFunction::VerifyL(*iKey, signature, output);
sl@0
   132
	CleanupClosePushL(output);
sl@0
   133
sl@0
   134
	// format the extracted hash so it can be compared with the original hash
sl@0
   135
	HBufC8* paddedHashPtr = output.BufferLC();
sl@0
   136
	aOutput = HBufC8::NewLC(GetMaximumOutputLengthL());
sl@0
   137
	TPtr8 unpaddedHash = aOutput->Des();
sl@0
   138
sl@0
   139
	iPadding->UnPadL(*paddedHashPtr, unpaddedHash);
sl@0
   140
sl@0
   141
	CleanupStack::Pop(aOutput);
sl@0
   142
	CleanupStack::PopAndDestroy(2, &output);
sl@0
   143
	}