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