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