Update contrib.
2 * Copyright (c) 2006-2010 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 <cryptospi/cryptoasymmetriccipherapi.h>
21 #include <cryptospi/cryptosignatureapi.h>
22 #include <cryptospi/cryptospidef.h>
23 #include "keyconverter.h"
24 #include <cryptospi/keys.h>
26 #include "../common/inlines.h"
29 using namespace CryptoSpi;
31 // CRSAPKCS1v15EncryptorShim ////////////////////////////////////////////////////////
33 CRSAPKCS1v15EncryptorShim* CRSAPKCS1v15EncryptorShim::NewL(const CRSAPublicKey& aKey)
35 CRSAPKCS1v15EncryptorShim* self = CRSAPKCS1v15EncryptorShim::NewLC(aKey);
36 CleanupStack::Pop(self);
40 CRSAPKCS1v15EncryptorShim* CRSAPKCS1v15EncryptorShim::NewLC(const CRSAPublicKey& aKey)
42 CRSAPKCS1v15EncryptorShim* self = new (ELeave) CRSAPKCS1v15EncryptorShim(aKey);
43 CleanupStack::PushL(self);
44 self->ConstructL(aKey);
48 CRSAPKCS1v15EncryptorShim::CRSAPKCS1v15EncryptorShim(const CRSAPublicKey& aKey)
49 : CRSAPKCS1v15Encryptor(aKey)
53 CRSAPKCS1v15EncryptorShim::~CRSAPKCS1v15EncryptorShim()
55 delete iAsymmetricCipherImpl;
59 void CRSAPKCS1v15EncryptorShim::ConstructL(const CRSAPublicKey& aKey)
61 iKey = KeyConverter::CreateKeyL(aKey);
62 CAsymmetricCipherFactory::CreateAsymmetricCipherL(
63 iAsymmetricCipherImpl,
66 KCryptoModeEncryptUid,
67 KPaddingModePkcs1_v1_5_EncryptionUid,
71 void CRSAPKCS1v15EncryptorShim::EncryptL(const TDesC8& aInput, TDes8& aOutput) const
73 iAsymmetricCipherImpl->ProcessL(aInput, aOutput);
76 TInt CRSAPKCS1v15EncryptorShim::MaxInputLength(void) const
78 TInt maxInputLength=0;
79 TRAPD(err, maxInputLength=iAsymmetricCipherImpl->GetMaximumInputLengthL())
82 return maxInputLength;
90 TInt CRSAPKCS1v15EncryptorShim::MaxOutputLength(void) const
92 TInt maxOutputLength=0;
93 TRAPD(err, maxOutputLength=iAsymmetricCipherImpl->GetMaximumOutputLengthL())
96 return maxOutputLength;
104 // CRSAPKCS1v15DecryptorShim ////////////////////////////////////////////////////////
105 CRSAPKCS1v15DecryptorShim* CRSAPKCS1v15DecryptorShim::NewL(const CRSAPrivateKey& aKey)
107 CRSAPKCS1v15DecryptorShim* self = CRSAPKCS1v15DecryptorShim::NewLC(aKey);
108 CleanupStack::Pop(self);
113 CRSAPKCS1v15DecryptorShim* CRSAPKCS1v15DecryptorShim::NewLC(const CRSAPrivateKey& aKey)
115 CRSAPKCS1v15DecryptorShim* self = new (ELeave) CRSAPKCS1v15DecryptorShim(aKey);
116 CleanupStack::PushL(self);
117 self->ConstructL(aKey);
121 CRSAPKCS1v15DecryptorShim::CRSAPKCS1v15DecryptorShim(const CRSAPrivateKey& aKey)
122 : CRSAPKCS1v15Decryptor(aKey)
126 CRSAPKCS1v15DecryptorShim::~CRSAPKCS1v15DecryptorShim()
128 delete iAsymmetricCipherImpl;
132 void CRSAPKCS1v15DecryptorShim::ConstructL(const CRSAPrivateKey& aKey)
134 iKey = KeyConverter::CreateKeyL(aKey);
135 CAsymmetricCipherFactory::CreateAsymmetricCipherL(
136 iAsymmetricCipherImpl,
139 KCryptoModeDecryptUid,
140 KPaddingModePkcs1_v1_5_EncryptionUid,
144 void CRSAPKCS1v15DecryptorShim::DecryptL(const TDesC8& aInput, TDes8& aOutput) const
146 iAsymmetricCipherImpl->ProcessL(aInput, aOutput);
149 TInt CRSAPKCS1v15DecryptorShim::MaxInputLength(void) const
151 TInt maxInputLength=0;
152 TRAPD(err, maxInputLength=iAsymmetricCipherImpl->GetMaximumInputLengthL())
155 return maxInputLength;
163 TInt CRSAPKCS1v15DecryptorShim::MaxOutputLength(void) const
165 TInt maxOutputLength=0;
166 TRAPD(err, maxOutputLength=iAsymmetricCipherImpl->GetMaximumOutputLengthL())
169 return maxOutputLength;
177 // CRSAPKCS1v15SignerShim ////////////////////////////////////////////////////////
178 CRSAPKCS1v15SignerShim* CRSAPKCS1v15SignerShim::NewL(const CRSAPrivateKey& aKey)
180 CRSAPKCS1v15SignerShim* self = CRSAPKCS1v15SignerShim::NewLC(aKey);
181 CleanupStack::Pop(self);
185 CRSAPKCS1v15SignerShim* CRSAPKCS1v15SignerShim::NewLC(const CRSAPrivateKey& aKey)
187 CRSAPKCS1v15SignerShim* self = new (ELeave) CRSAPKCS1v15SignerShim(aKey);
188 CleanupStack::PushL(self);
189 self->ConstructL(aKey);
193 CRSASignature* CRSAPKCS1v15SignerShim::SignL(const TDesC8& aInput) const
195 //Sign the input data
196 CCryptoParams* signature = CCryptoParams::NewLC();
197 iSignerImpl->SignL(aInput, *signature);
199 //Retrieve the S in RSA signature from the array
200 const TInteger& cS=signature->GetBigIntL(KRsaSignatureParameterSUid);
202 //Make copies of the RSA signature
203 RInteger s=RInteger::NewL(cS);
204 CleanupClosePushL(s);
206 //Create the RSA signature object, the ownership of s is transfered to rsaSig
207 CRSASignature* rsaSig=CRSASignature::NewL(s);
210 CleanupStack::Pop(&s);
211 CleanupStack::PopAndDestroy(signature);
215 TInt CRSAPKCS1v15SignerShim::MaxInputLength(void) const
217 TInt maxInputLength=0;
218 TRAPD(err, maxInputLength=iSignerImpl->GetMaximumInputLengthL())
221 return maxInputLength;
229 TInt CRSAPKCS1v15SignerShim::MaxOutputLength(void) const
231 TInt maxOutputLength=0;
232 TRAPD(err, maxOutputLength=iSignerImpl->GetMaximumOutputLengthL())
235 return maxOutputLength;
243 CRSAPKCS1v15SignerShim::~CRSAPKCS1v15SignerShim(void)
249 CRSAPKCS1v15SignerShim::CRSAPKCS1v15SignerShim(const CRSAPrivateKey& aKey)
250 : CRSAPKCS1v15Signer(aKey)
254 void CRSAPKCS1v15SignerShim::ConstructL(const CRSAPrivateKey& aKey)
256 iKey = KeyConverter::CreateKeyL(aKey);
257 CSignatureFactory::CreateSignerL(
261 KPaddingModePkcs1_v1_5_SignatureUid,
265 // CRSAPKCS1v15VerifierShim ////////////////////////////////////////////////////////
266 CRSAPKCS1v15VerifierShim* CRSAPKCS1v15VerifierShim::NewL(const CRSAPublicKey& aKey)
268 CRSAPKCS1v15VerifierShim* self = CRSAPKCS1v15VerifierShim::NewLC(aKey);
269 CleanupStack::Pop(self);
273 CRSAPKCS1v15VerifierShim* CRSAPKCS1v15VerifierShim::NewLC(const CRSAPublicKey& aKey)
275 CRSAPKCS1v15VerifierShim* self = new (ELeave) CRSAPKCS1v15VerifierShim(aKey);
276 CleanupStack::PushL(self);
277 self->ConstructL(aKey);
281 TBool CRSAPKCS1v15VerifierShim::VerifyL(const TDesC8& aInput, const CRSASignature& aSignature) const
283 //create the array format rsa signature for the new crypto spi
284 CCryptoParams* rsaSig = CCryptoParams::NewLC();
286 rsaSig->AddL(aSignature.S(), KRsaSignatureParameterSUid);
288 //pass the signature and input to crypto spi to be verified
289 TBool verificationResult = EFalse;
290 iVerifierImpl->VerifyL(aInput, *rsaSig, verificationResult);
293 CleanupStack::PopAndDestroy(rsaSig);
294 return verificationResult;
297 HBufC8* CRSAPKCS1v15VerifierShim::InverseSignLC(const CRSASignature& aSignature) const
299 //create the array format rsa signature for the new crypto spi
300 CCryptoParams* rsaSig = CCryptoParams::NewLC();
302 rsaSig->AddL(aSignature.S(), KRsaSignatureParameterSUid);
304 //pass the signature and input to crypto spi to be verified
305 HBufC8* output = NULL;
306 iVerifierImpl->InverseSignL(output, *rsaSig);
309 CleanupStack::PopAndDestroy(rsaSig);
311 // leave output on the cleanup stack
312 CleanupStack::PushL(output);
316 TInt CRSAPKCS1v15VerifierShim::MaxInputLength(void) const
318 TInt maxInputLength=0;
319 TRAPD(err, maxInputLength=iVerifierImpl->GetMaximumInputLengthL())
322 return maxInputLength;
330 TInt CRSAPKCS1v15VerifierShim::MaxOutputLength(void) const
332 TInt maxOutputLength=0;
333 TRAPD(err, maxOutputLength=iVerifierImpl->GetMaximumOutputLengthL())
336 return maxOutputLength;
344 CRSAPKCS1v15VerifierShim::~CRSAPKCS1v15VerifierShim(void)
346 delete iVerifierImpl;
350 CRSAPKCS1v15VerifierShim::CRSAPKCS1v15VerifierShim(const CRSAPublicKey& aKey)
351 : CRSAPKCS1v15Verifier(aKey)
355 void CRSAPKCS1v15VerifierShim::ConstructL(const CRSAPublicKey& aKey)
357 iKey = KeyConverter::CreateKeyL(aKey);
358 CSignatureFactory::CreateVerifierL(
362 KPaddingModePkcs1_v1_5_SignatureUid,