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