Update contrib.
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;
30 TUid aImplementationUid,
33 CAsymmetricCipherImpl(aCryptoMode, aPadding),
34 iImplementationUid(aImplementationUid)
38 CRSAImpl* CRSAImpl::NewL(TUid aImplementationUid, const CKey& aKey, TUid aCryptoMode, TUid aPadding)
40 CRSAImpl* self = CRSAImpl::NewLC(aImplementationUid, aKey, aCryptoMode, aPadding);
41 CleanupStack::Pop(self);
45 CRSAImpl* CRSAImpl::NewLC(TUid aImplementationUid, const CKey& aKey, TUid aCryptoMode, TUid aPadding)
47 CRSAImpl* self = new(ELeave) CRSAImpl(aImplementationUid, aCryptoMode, aPadding);
48 CleanupStack::PushL(self);
49 self->ConstructL(aKey);
57 TInt CRSAImpl::GetMaximumOutputLengthL() const
59 const TInteger& N = iKey->GetBigIntL(KRsaKeyParameterNUid);
61 if (iCryptoMode.iUid == KCryptoModeDecrypt)
62 return N.ByteCount() - iPadding->MinPaddingLength();
67 TInt CRSAImpl::GetMaximumInputLengthL() const
69 const TInteger& N = iKey->GetBigIntL(KRsaKeyParameterNUid);
71 if (iCryptoMode.iUid == KCryptoModeEncrypt)
72 return N.ByteCount() - iPadding->MinPaddingLength();
77 void CRSAImpl::ConstructL(const CKey& aKey)
79 const TInteger& N = aKey.GetBigIntL(KRsaKeyParameterNUid);
80 TCrypto::IsAsymmetricWeakEnoughL(N.BitCount());
81 CAsymmetricCipherImpl::ConstructL(aKey);
83 if (! IsValidKeyLengthL(N.ByteCount()))
85 User::Leave(KErrKeySize);
89 CExtendedCharacteristics* CRSAImpl::CreateExtendedCharacteristicsL()
91 // All Symbian software plug-ins have unlimited concurrency, cannot be reserved
92 // for exclusive use and are not CERTIFIED to be standards compliant.
93 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
96 const CExtendedCharacteristics* CRSAImpl::GetExtendedCharacteristicsL()
98 return CRSAImpl::CreateExtendedCharacteristicsL();
101 TUid CRSAImpl::ImplementationUid() const
103 return iImplementationUid;
106 void CRSAImpl::EncryptL(const TDesC8& aInput, TDes8& aOutput) const
108 __ASSERT_DEBUG(aOutput.MaxLength() >= GetMaximumOutputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
109 __ASSERT_DEBUG(aInput.Length() <= GetMaximumInputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge));
111 HBufC8* buf = HBufC8::NewLC(GetMaximumOutputLengthL());
112 TPtr8 ptr = buf->Des();
114 iPadding->PadL(aInput, ptr);
115 RInteger input = RInteger::NewL(ptr);
116 CleanupStack::PushL(input);
119 RSAFunction::EncryptL(*iKey, input, output);
120 CleanupStack::PushL(output);
122 aOutput.Append(*(output.BufferLC()));
123 CleanupStack::PopAndDestroy(4, buf); //BufferLC, output, input, buf
126 void CRSAImpl::DecryptL(const TDesC8& aInput, TDes8& aOutput) const
128 __ASSERT_DEBUG(aOutput.MaxLength() >= GetMaximumOutputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
129 __ASSERT_DEBUG(aInput.Length() <= GetMaximumInputLengthL(), User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge));
131 RInteger input = RInteger::NewL(aInput);
132 CleanupStack::PushL(input);
136 RSAFunction::DecryptL(*iKey, input, output);
137 CleanupStack::PushL(output);
139 TPtrC8 ptr = *(output.BufferLC());
140 iPadding->UnPadL(ptr, aOutput);
142 CleanupStack::PopAndDestroy(3, &input); //BufferLC(), output, input
145 void CRSAImpl::ProcessL(const TDesC8& aInput, TDes8& aOutput)
147 if (iCryptoMode.iUid == KCryptoModeEncrypt)
149 EncryptL(aInput, aOutput);
153 DecryptL(aInput, aOutput);
157 TBool CRSAImpl::IsValidKeyLengthL(TInt aKeyBytes) const
162 switch (iCryptoMode.iUid)
164 case KCryptoModeEncrypt:
165 // Check if GetMaximumInputLengthL() makes sense,
166 // if not the key length must be too small
167 if (GetMaximumInputLengthL() <= 0)
171 case KCryptoModeDecrypt:
172 // Check if GetMaximumOutputLengthL() makes sense,
173 // if not the key length must be too small
174 if (GetMaximumOutputLengthL() <= 0)