os/security/crypto/weakcrypto/source/asymmetric/rsaverifier.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200 (2014-06-10)
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2003-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 <asymmetric.h>
sl@0
    20
#include <asymmetrickeys.h>
sl@0
    21
#include <bigint.h>
sl@0
    22
#include <securityerr.h>
sl@0
    23
#include "rsafunction.h"
sl@0
    24
sl@0
    25
/* CRSAVerifier */
sl@0
    26
sl@0
    27
EXPORT_C CRSAVerifier::CRSAVerifier(void)
sl@0
    28
	{
sl@0
    29
	}
sl@0
    30
sl@0
    31
EXPORT_C TBool CRSAVerifier::VerifyL(const TDesC8& aInput, 
sl@0
    32
	const CRSASignature& aSignature) const
sl@0
    33
	{
sl@0
    34
	TBool retval = EFalse;
sl@0
    35
	HBufC8* inverseSign = InverseSignLC(aSignature);
sl@0
    36
	
sl@0
    37
	if (inverseSign->Compare(aInput)==0)
sl@0
    38
		{
sl@0
    39
		retval = ETrue;
sl@0
    40
		}
sl@0
    41
	CleanupStack::PopAndDestroy(inverseSign);
sl@0
    42
	return retval;	
sl@0
    43
	}
sl@0
    44
sl@0
    45
/* CRSAPKCS1v15Verifier */
sl@0
    46
EXPORT_C CRSAPKCS1v15Verifier* CRSAPKCS1v15Verifier::NewL( 
sl@0
    47
	const CRSAPublicKey& aKey)
sl@0
    48
	{
sl@0
    49
	CRSAPKCS1v15Verifier* self = NewLC(aKey);
sl@0
    50
	CleanupStack::Pop();
sl@0
    51
	return self;
sl@0
    52
	}
sl@0
    53
sl@0
    54
EXPORT_C CRSAPKCS1v15Verifier* CRSAPKCS1v15Verifier::NewLC(
sl@0
    55
	const CRSAPublicKey& aKey)
sl@0
    56
	{
sl@0
    57
	CRSAPKCS1v15Verifier* self = new(ELeave) CRSAPKCS1v15Verifier(aKey);
sl@0
    58
	CleanupStack::PushL(self);
sl@0
    59
	self->ConstructL();
sl@0
    60
	return self;
sl@0
    61
	}
sl@0
    62
sl@0
    63
TInt CRSAPKCS1v15Verifier::MaxInputLength(void) const
sl@0
    64
	{
sl@0
    65
	return MaxOutputLength() - iPadding->MinPaddingLength();	
sl@0
    66
	}
sl@0
    67
sl@0
    68
TInt CRSAPKCS1v15Verifier::MaxOutputLength(void) const
sl@0
    69
	{
sl@0
    70
	return iPublicKey.N().ByteCount();
sl@0
    71
	}
sl@0
    72
sl@0
    73
HBufC8* CRSAPKCS1v15Verifier::InverseSignLC(
sl@0
    74
	const CRSASignature& aSignature) const
sl@0
    75
	{
sl@0
    76
	HBufC8* unpaddedBuf = HBufC8::NewMaxLC(MaxOutputLength());
sl@0
    77
	TPtr8 unpaddedHash = unpaddedBuf->Des();
sl@0
    78
sl@0
    79
	RInteger input = RInteger::NewL(aSignature.S());
sl@0
    80
	CleanupStack::PushL(input);
sl@0
    81
	RInteger output;
sl@0
    82
sl@0
    83
	RSAFunction::VerifyL(iPublicKey, input, output);
sl@0
    84
	CleanupStack::PushL(output);
sl@0
    85
sl@0
    86
	TPtrC8 paddedHashPtr = *(output.BufferLC());
sl@0
    87
	
sl@0
    88
	iPadding->UnPadL(paddedHashPtr, unpaddedHash);
sl@0
    89
	CleanupStack::PopAndDestroy(3, &input); //BufferLC, output, input
sl@0
    90
	return unpaddedBuf;
sl@0
    91
	}
sl@0
    92
sl@0
    93
CRSAPKCS1v15Verifier::CRSAPKCS1v15Verifier(const CRSAPublicKey& aKey)
sl@0
    94
	: iPublicKey(aKey)
sl@0
    95
	{
sl@0
    96
	}
sl@0
    97
sl@0
    98
void CRSAPKCS1v15Verifier::ConstructL(void)
sl@0
    99
	{
sl@0
   100
	iPadding = CPaddingPKCS1Signature::NewL(MaxOutputLength());
sl@0
   101
sl@0
   102
	// Check if MaxInputLength() makes sense, if not the key length must 
sl@0
   103
	// be too small
sl@0
   104
	if(MaxInputLength() <= 0)
sl@0
   105
		{
sl@0
   106
		User::Leave(KErrKeySize);
sl@0
   107
		}
sl@0
   108
	}
sl@0
   109
sl@0
   110
CRSAPKCS1v15Verifier::~CRSAPKCS1v15Verifier(void)
sl@0
   111
	{
sl@0
   112
	delete iPadding;
sl@0
   113
	}
sl@0
   114