crypto/weakcrypto/source/asymmetric/rsadecryptor.cpp
changeset 71 dd83586b62d6
equal deleted inserted replaced
66:8873e6835f7b 71:dd83586b62d6
       
     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 <cryptostrength.h>
       
    23 #include <securityerr.h>
       
    24 #include <cryptopanic.h>
       
    25 #include "rsafunction.h"
       
    26 
       
    27 /* CRSAPKCS1v15Decryptor */
       
    28 
       
    29 EXPORT_C CRSAPKCS1v15Decryptor* CRSAPKCS1v15Decryptor::NewL(
       
    30 	const CRSAPrivateKey& aKey)
       
    31 	{
       
    32 	CRSAPKCS1v15Decryptor* self = NewLC(aKey);
       
    33 	CleanupStack::Pop(self);
       
    34 	return self;
       
    35 	}
       
    36 
       
    37 EXPORT_C CRSAPKCS1v15Decryptor* CRSAPKCS1v15Decryptor::NewLC(
       
    38 	const CRSAPrivateKey& aKey)
       
    39 	{
       
    40 	CRSAPKCS1v15Decryptor* self = new(ELeave) CRSAPKCS1v15Decryptor(aKey);
       
    41 	CleanupStack::PushL(self);
       
    42 	self->ConstructL();
       
    43 	return self;
       
    44 	}
       
    45 
       
    46 TInt CRSAPKCS1v15Decryptor::MaxInputLength(void) const
       
    47 	{
       
    48 	return iPrivateKey.N().ByteCount();
       
    49 	}
       
    50 
       
    51 TInt CRSAPKCS1v15Decryptor::MaxOutputLength(void) const
       
    52 	{
       
    53 	return MaxInputLength() - iPadding->MinPaddingLength();
       
    54 	}
       
    55 
       
    56 void CRSAPKCS1v15Decryptor::DecryptL(const TDesC8& aInput, 
       
    57 	TDes8& aOutput)const
       
    58 	{
       
    59 	__ASSERT_DEBUG(aOutput.MaxLength() >= MaxOutputLength(), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
       
    60 	__ASSERT_DEBUG(aInput.Length() <= MaxInputLength(), User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge));
       
    61 
       
    62 	RInteger input = RInteger::NewL(aInput);
       
    63 	CleanupStack::PushL(input);
       
    64 
       
    65 	RInteger output;
       
    66 
       
    67 	RSAFunction::DecryptL(iPrivateKey, input, output);
       
    68 	CleanupStack::PushL(output);
       
    69 
       
    70     TPtrC8 ptr = *(output.BufferLC());
       
    71     iPadding->UnPadL(ptr, aOutput);
       
    72 
       
    73     CleanupStack::PopAndDestroy(3, &input); //BufferLC(), output, input
       
    74 	}
       
    75 
       
    76 CRSAPKCS1v15Decryptor::CRSAPKCS1v15Decryptor(const CRSAPrivateKey& aKey)  
       
    77 	: iPrivateKey(aKey)
       
    78 	{
       
    79 	}
       
    80 
       
    81 void CRSAPKCS1v15Decryptor::ConstructL(void)  
       
    82 	{
       
    83 	iPadding = CPaddingPKCS1Encryption::NewL(MaxInputLength());
       
    84 
       
    85 	// Check if MaxInputLength() makes sense, if not the key length must 
       
    86 	// be too small
       
    87 	if(MaxOutputLength() <= 0)
       
    88 		{
       
    89 		User::Leave(KErrKeySize);
       
    90 		}
       
    91 
       
    92 	TCrypto::IsAsymmetricWeakEnoughL(iPrivateKey.N().BitCount());
       
    93 	}
       
    94 
       
    95 CRSAPKCS1v15Decryptor::~CRSAPKCS1v15Decryptor(void)  
       
    96 	{
       
    97 	delete iPadding;
       
    98 	}