First public contribution.
2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
20 #include "rsafunction.h"
21 #include "pluginconfig.h"
22 #include <cryptopanic.h>
23 #include <cryptostrength.h>
24 #include <securityerr.h>
26 using namespace SoftwareCrypto;
32 CAsymmetricCipherImpl(aCryptoMode, aPadding)
36 CRSAImpl* CRSAImpl::NewL(const CKey& aKey, TUid aCryptoMode, TUid aPadding)
38 CRSAImpl* self = CRSAImpl::NewLC(aKey, aCryptoMode, aPadding);
39 CleanupStack::Pop(self);
43 CRSAImpl* CRSAImpl::NewLC(const CKey& aKey, TUid aCryptoMode, TUid aPadding)
45 CRSAImpl* self = new(ELeave) CRSAImpl(aCryptoMode, aPadding);
46 CleanupStack::PushL(self);
47 self->ConstructL(aKey);
55 TInt CRSAImpl::GetMaximumOutputLengthL() const
57 const TInteger& N = iKey->GetBigIntL(KRsaKeyParameterNUid);
59 if (iCryptoMode.iUid == KCryptoModeDecrypt)
60 return N.ByteCount() - iPadding->MinPaddingLength();
65 TInt CRSAImpl::GetMaximumInputLengthL() const
67 const TInteger& N = iKey->GetBigIntL(KRsaKeyParameterNUid);
69 if (iCryptoMode.iUid == KCryptoModeEncrypt)
70 return N.ByteCount() - iPadding->MinPaddingLength();
75 void CRSAImpl::ConstructL(const CKey& aKey)
77 const TInteger& N = aKey.GetBigIntL(KRsaKeyParameterNUid);
78 TCrypto::IsAsymmetricWeakEnoughL(N.BitCount());
79 CAsymmetricCipherImpl::ConstructL(aKey);
81 if (! IsValidKeyLengthL(N.ByteCount()))
83 User::Leave(KErrKeySize);
87 CExtendedCharacteristics* CRSAImpl::CreateExtendedCharacteristicsL()
89 // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
90 // for exclusive use and are not CERTIFIED to be standards compliant.
91 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
94 const CExtendedCharacteristics* CRSAImpl::GetExtendedCharacteristicsL()
96 return CRSAImpl::CreateExtendedCharacteristicsL();
99 TUid CRSAImpl::ImplementationUid() const
101 return KCryptoPluginRsaCipherUid;
104 void CRSAImpl::EncryptL(const TDesC8& aInput, TDes8& aOutput) const
106 __ASSERT_DEBUG(aOutput.MaxLength() >= GetMaximumOutputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
107 __ASSERT_DEBUG(aInput.Length() <= GetMaximumInputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge));
109 HBufC8* buf = HBufC8::NewLC(GetMaximumOutputLengthL());
110 TPtr8 ptr = buf->Des();
112 iPadding->PadL(aInput, ptr);
113 RInteger input = RInteger::NewL(ptr);
114 CleanupStack::PushL(input);
117 RSAFunction::EncryptL(*iKey, input, output);
118 CleanupStack::PushL(output);
120 aOutput.Append(*(output.BufferLC()));
121 CleanupStack::PopAndDestroy(4, buf); //BufferLC, output, input, buf
124 void CRSAImpl::DecryptL(const TDesC8& aInput, TDes8& aOutput) const
126 __ASSERT_DEBUG(aOutput.MaxLength() >= GetMaximumOutputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
127 __ASSERT_DEBUG(aInput.Length() <= GetMaximumInputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge));
129 RInteger input = RInteger::NewL(aInput);
130 CleanupStack::PushL(input);
134 RSAFunction::DecryptL(*iKey, input, output);
135 CleanupStack::PushL(output);
137 TPtrC8 ptr = *(output.BufferLC());
138 iPadding->UnPadL(ptr, aOutput);
140 CleanupStack::PopAndDestroy(3, &input); //BufferLC(), output, input
143 void CRSAImpl::ProcessL(const TDesC8& aInput, TDes8& aOutput)
145 if (iCryptoMode.iUid == KCryptoModeEncrypt)
147 EncryptL(aInput, aOutput);
151 DecryptL(aInput, aOutput);
155 TBool CRSAImpl::IsValidKeyLengthL(TInt aKeyBytes) const
160 switch (iCryptoMode.iUid)
162 case KCryptoModeEncrypt:
163 // Check if GetMaximumInputLengthL() makes sense,
164 // if not the key length must be too small
165 if (GetMaximumInputLengthL() <= 0)
169 case KCryptoModeDecrypt:
170 // Check if GetMaximumOutputLengthL() makes sense,
171 // if not the key length must be too small
172 if (GetMaximumOutputLengthL() <= 0)