crypto/weakcrypto/source/asymmetric/rsaverifier.cpp
author hgs
Thu, 12 Aug 2010 21:07:10 +0530
changeset 90 8c545fea2798
parent 72 de46a57f75fb
permissions -rw-r--r--
201031_01
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     1
/*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     2
* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     3
* All rights reserved.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     8
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     9
* Initial Contributors:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    11
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    12
* Contributors:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    13
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    14
* Description: 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    15
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    16
*/
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    17
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    18
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    19
#include <asymmetric.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    20
#include <asymmetrickeys.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    21
#include <bigint.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    22
#include <securityerr.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    23
#include "rsafunction.h"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    24
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    25
/* CRSAVerifier */
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    26
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    27
EXPORT_C CRSAVerifier::CRSAVerifier(void)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    28
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    29
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    30
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    31
EXPORT_C TBool CRSAVerifier::VerifyL(const TDesC8& aInput, 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    32
	const CRSASignature& aSignature) const
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    33
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    34
	TBool retval = EFalse;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    35
	HBufC8* inverseSign = InverseSignLC(aSignature);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    36
	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    37
	if (inverseSign->Compare(aInput)==0)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    38
		{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    39
		retval = ETrue;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    40
		}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    41
	CleanupStack::PopAndDestroy(inverseSign);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    42
	return retval;	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    43
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    44
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    45
/* CRSAPKCS1v15Verifier */
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    46
EXPORT_C CRSAPKCS1v15Verifier* CRSAPKCS1v15Verifier::NewL( 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    47
	const CRSAPublicKey& aKey)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    48
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    49
	CRSAPKCS1v15Verifier* self = NewLC(aKey);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    50
	CleanupStack::Pop();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    51
	return self;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    52
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    53
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    54
EXPORT_C CRSAPKCS1v15Verifier* CRSAPKCS1v15Verifier::NewLC(
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    55
	const CRSAPublicKey& aKey)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    56
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    57
	CRSAPKCS1v15Verifier* self = new(ELeave) CRSAPKCS1v15Verifier(aKey);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    58
	CleanupStack::PushL(self);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    59
	self->ConstructL();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    60
	return self;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    61
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    62
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    63
TInt CRSAPKCS1v15Verifier::MaxInputLength(void) const
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    64
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    65
	return MaxOutputLength() - iPadding->MinPaddingLength();	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    66
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    67
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    68
TInt CRSAPKCS1v15Verifier::MaxOutputLength(void) const
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    69
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    70
	return iPublicKey.N().ByteCount();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    71
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    72
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    73
HBufC8* CRSAPKCS1v15Verifier::InverseSignLC(
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    74
	const CRSASignature& aSignature) const
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    75
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    76
	HBufC8* unpaddedBuf = HBufC8::NewMaxLC(MaxOutputLength());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    77
	TPtr8 unpaddedHash = unpaddedBuf->Des();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    78
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    79
	RInteger input = RInteger::NewL(aSignature.S());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    80
	CleanupStack::PushL(input);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    81
	RInteger output;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    82
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    83
	RSAFunction::VerifyL(iPublicKey, input, output);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    84
	CleanupStack::PushL(output);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    85
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    86
	TPtrC8 paddedHashPtr = *(output.BufferLC());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    87
	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    88
	iPadding->UnPadL(paddedHashPtr, unpaddedHash);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    89
	CleanupStack::PopAndDestroy(3, &input); //BufferLC, output, input
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    90
	return unpaddedBuf;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    91
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    92
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    93
CRSAPKCS1v15Verifier::CRSAPKCS1v15Verifier(const CRSAPublicKey& aKey)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    94
	: iPublicKey(aKey)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    95
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    96
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    97
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    98
void CRSAPKCS1v15Verifier::ConstructL(void)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    99
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   100
	iPadding = CPaddingPKCS1Signature::NewL(MaxOutputLength());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   101
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   102
	// Check if MaxInputLength() makes sense, if not the key length must 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   103
	// be too small
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   104
	if(MaxInputLength() <= 0)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   105
		{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   106
		User::Leave(KErrKeySize);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   107
		}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   108
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   109
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   110
CRSAPKCS1v15Verifier::~CRSAPKCS1v15Verifier(void)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   111
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   112
	delete iPadding;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   113
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   114