os/security/crypto/weakcrypto/source/asymmetric/rsadecryptor.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include <asymmetric.h>
sl@0
    20
#include <asymmetrickeys.h>
sl@0
    21
#include <bigint.h>
sl@0
    22
#include <cryptostrength.h>
sl@0
    23
#include <securityerr.h>
sl@0
    24
#include <cryptopanic.h>
sl@0
    25
#include "rsafunction.h"
sl@0
    26
sl@0
    27
/* CRSAPKCS1v15Decryptor */
sl@0
    28
sl@0
    29
EXPORT_C CRSAPKCS1v15Decryptor* CRSAPKCS1v15Decryptor::NewL(
sl@0
    30
	const CRSAPrivateKey& aKey)
sl@0
    31
	{
sl@0
    32
	CRSAPKCS1v15Decryptor* self = NewLC(aKey);
sl@0
    33
	CleanupStack::Pop(self);
sl@0
    34
	return self;
sl@0
    35
	}
sl@0
    36
sl@0
    37
EXPORT_C CRSAPKCS1v15Decryptor* CRSAPKCS1v15Decryptor::NewLC(
sl@0
    38
	const CRSAPrivateKey& aKey)
sl@0
    39
	{
sl@0
    40
	CRSAPKCS1v15Decryptor* self = new(ELeave) CRSAPKCS1v15Decryptor(aKey);
sl@0
    41
	CleanupStack::PushL(self);
sl@0
    42
	self->ConstructL();
sl@0
    43
	return self;
sl@0
    44
	}
sl@0
    45
sl@0
    46
TInt CRSAPKCS1v15Decryptor::MaxInputLength(void) const
sl@0
    47
	{
sl@0
    48
	return iPrivateKey.N().ByteCount();
sl@0
    49
	}
sl@0
    50
sl@0
    51
TInt CRSAPKCS1v15Decryptor::MaxOutputLength(void) const
sl@0
    52
	{
sl@0
    53
	return MaxInputLength() - iPadding->MinPaddingLength();
sl@0
    54
	}
sl@0
    55
sl@0
    56
void CRSAPKCS1v15Decryptor::DecryptL(const TDesC8& aInput, 
sl@0
    57
	TDes8& aOutput)const
sl@0
    58
	{
sl@0
    59
	__ASSERT_DEBUG(aOutput.MaxLength() >= MaxOutputLength(), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
sl@0
    60
	__ASSERT_DEBUG(aInput.Length() <= MaxInputLength(), User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge));
sl@0
    61
sl@0
    62
	RInteger input = RInteger::NewL(aInput);
sl@0
    63
	CleanupStack::PushL(input);
sl@0
    64
sl@0
    65
	RInteger output;
sl@0
    66
sl@0
    67
	RSAFunction::DecryptL(iPrivateKey, input, output);
sl@0
    68
	CleanupStack::PushL(output);
sl@0
    69
sl@0
    70
    TPtrC8 ptr = *(output.BufferLC());
sl@0
    71
    iPadding->UnPadL(ptr, aOutput);
sl@0
    72
sl@0
    73
    CleanupStack::PopAndDestroy(3, &input); //BufferLC(), output, input
sl@0
    74
	}
sl@0
    75
sl@0
    76
CRSAPKCS1v15Decryptor::CRSAPKCS1v15Decryptor(const CRSAPrivateKey& aKey)  
sl@0
    77
	: iPrivateKey(aKey)
sl@0
    78
	{
sl@0
    79
	}
sl@0
    80
sl@0
    81
void CRSAPKCS1v15Decryptor::ConstructL(void)  
sl@0
    82
	{
sl@0
    83
	iPadding = CPaddingPKCS1Encryption::NewL(MaxInputLength());
sl@0
    84
sl@0
    85
	// Check if MaxInputLength() makes sense, if not the key length must 
sl@0
    86
	// be too small
sl@0
    87
	if(MaxOutputLength() <= 0)
sl@0
    88
		{
sl@0
    89
		User::Leave(KErrKeySize);
sl@0
    90
		}
sl@0
    91
sl@0
    92
	TCrypto::IsAsymmetricWeakEnoughL(iPrivateKey.N().BitCount());
sl@0
    93
	}
sl@0
    94
sl@0
    95
CRSAPKCS1v15Decryptor::~CRSAPKCS1v15Decryptor(void)  
sl@0
    96
	{
sl@0
    97
	delete iPadding;
sl@0
    98
	}