crypto/weakcrypto/source/asymmetric/rsadecryptor.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 <cryptostrength.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    23
#include <securityerr.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    24
#include <cryptopanic.h>
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    25
#include "rsafunction.h"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    26
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    27
/* CRSAPKCS1v15Decryptor */
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    28
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    29
EXPORT_C CRSAPKCS1v15Decryptor* CRSAPKCS1v15Decryptor::NewL(
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    30
	const CRSAPrivateKey& aKey)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    31
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    32
	CRSAPKCS1v15Decryptor* 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 CRSAPKCS1v15Decryptor* CRSAPKCS1v15Decryptor::NewLC(
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    38
	const CRSAPrivateKey& aKey)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    39
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    40
	CRSAPKCS1v15Decryptor* self = new(ELeave) CRSAPKCS1v15Decryptor(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 CRSAPKCS1v15Decryptor::MaxInputLength(void) const
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    47
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    48
	return iPrivateKey.N().ByteCount();
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 CRSAPKCS1v15Decryptor::MaxOutputLength(void) const
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    52
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    53
	return MaxInputLength() - iPadding->MinPaddingLength();
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 CRSAPKCS1v15Decryptor::DecryptL(const TDesC8& aInput, 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    57
	TDes8& aOutput)const
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    58
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    59
	__ASSERT_DEBUG(aOutput.MaxLength() >= MaxOutputLength(), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    60
	__ASSERT_DEBUG(aInput.Length() <= MaxInputLength(), User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge));
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    61
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    62
	RInteger input = RInteger::NewL(aInput);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    63
	CleanupStack::PushL(input);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    64
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    65
	RInteger output;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    66
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    67
	RSAFunction::DecryptL(iPrivateKey, input, output);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    68
	CleanupStack::PushL(output);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    69
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    70
    TPtrC8 ptr = *(output.BufferLC());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    71
    iPadding->UnPadL(ptr, aOutput);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    72
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    73
    CleanupStack::PopAndDestroy(3, &input); //BufferLC(), output, input
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    74
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    75
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    76
CRSAPKCS1v15Decryptor::CRSAPKCS1v15Decryptor(const CRSAPrivateKey& aKey)  
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    77
	: iPrivateKey(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 CRSAPKCS1v15Decryptor::ConstructL(void)  
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    82
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    83
	iPadding = CPaddingPKCS1Encryption::NewL(MaxInputLength());
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(MaxOutputLength() <= 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
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    92
	TCrypto::IsAsymmetricWeakEnoughL(iPrivateKey.N().BitCount());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    93
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    94
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    95
CRSAPKCS1v15Decryptor::~CRSAPKCS1v15Decryptor(void)  
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    96
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    97
	delete iPadding;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    98
	}