sl@0: /* sl@0: * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "rsashim.h" sl@0: #include sl@0: #include sl@0: #include sl@0: #include "keyconverter.h" sl@0: #include sl@0: sl@0: #include "../common/inlines.h" sl@0: sl@0: sl@0: using namespace CryptoSpi; sl@0: sl@0: // CRSAPKCS1v15EncryptorShim //////////////////////////////////////////////////////// sl@0: sl@0: CRSAPKCS1v15EncryptorShim* CRSAPKCS1v15EncryptorShim::NewL(const CRSAPublicKey& aKey) sl@0: { sl@0: CRSAPKCS1v15EncryptorShim* self = CRSAPKCS1v15EncryptorShim::NewLC(aKey); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CRSAPKCS1v15EncryptorShim* CRSAPKCS1v15EncryptorShim::NewLC(const CRSAPublicKey& aKey) sl@0: { sl@0: CRSAPKCS1v15EncryptorShim* self = new (ELeave) CRSAPKCS1v15EncryptorShim(aKey); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aKey); sl@0: return self; sl@0: } sl@0: sl@0: CRSAPKCS1v15EncryptorShim::CRSAPKCS1v15EncryptorShim(const CRSAPublicKey& aKey) sl@0: : CRSAPKCS1v15Encryptor(aKey) sl@0: { sl@0: } sl@0: sl@0: CRSAPKCS1v15EncryptorShim::~CRSAPKCS1v15EncryptorShim() sl@0: { sl@0: delete iAsymmetricCipherImpl; sl@0: delete iKey; sl@0: } sl@0: sl@0: void CRSAPKCS1v15EncryptorShim::ConstructL(const CRSAPublicKey& aKey) sl@0: { sl@0: iKey = KeyConverter::CreateKeyL(aKey); sl@0: CAsymmetricCipherFactory::CreateAsymmetricCipherL( sl@0: iAsymmetricCipherImpl, sl@0: KRsaCipherUid, sl@0: *iKey, sl@0: KCryptoModeEncryptUid, sl@0: KPaddingModePkcs1_v1_5_EncryptionUid, sl@0: NULL); sl@0: } sl@0: sl@0: void CRSAPKCS1v15EncryptorShim::EncryptL(const TDesC8& aInput, TDes8& aOutput) const sl@0: { sl@0: iAsymmetricCipherImpl->ProcessL(aInput, aOutput); sl@0: } sl@0: sl@0: TInt CRSAPKCS1v15EncryptorShim::MaxInputLength(void) const sl@0: { sl@0: TInt maxInputLength=0; sl@0: TRAPD(err, maxInputLength=iAsymmetricCipherImpl->GetMaximumInputLengthL()) sl@0: if (err==KErrNone) sl@0: { sl@0: return maxInputLength; sl@0: } sl@0: else sl@0: { sl@0: return err; sl@0: } sl@0: } sl@0: sl@0: TInt CRSAPKCS1v15EncryptorShim::MaxOutputLength(void) const sl@0: { sl@0: TInt maxOutputLength=0; sl@0: TRAPD(err, maxOutputLength=iAsymmetricCipherImpl->GetMaximumOutputLengthL()) sl@0: if (err==KErrNone) sl@0: { sl@0: return maxOutputLength; sl@0: } sl@0: else sl@0: { sl@0: return err; sl@0: } sl@0: } sl@0: sl@0: // CRSAPKCS1v15DecryptorShim //////////////////////////////////////////////////////// sl@0: CRSAPKCS1v15DecryptorShim* CRSAPKCS1v15DecryptorShim::NewL(const CRSAPrivateKey& aKey) sl@0: { sl@0: CRSAPKCS1v15DecryptorShim* self = CRSAPKCS1v15DecryptorShim::NewLC(aKey); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: sl@0: CRSAPKCS1v15DecryptorShim* CRSAPKCS1v15DecryptorShim::NewLC(const CRSAPrivateKey& aKey) sl@0: { sl@0: CRSAPKCS1v15DecryptorShim* self = new (ELeave) CRSAPKCS1v15DecryptorShim(aKey); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aKey); sl@0: return self; sl@0: } sl@0: sl@0: CRSAPKCS1v15DecryptorShim::CRSAPKCS1v15DecryptorShim(const CRSAPrivateKey& aKey) sl@0: : CRSAPKCS1v15Decryptor(aKey) sl@0: { sl@0: } sl@0: sl@0: CRSAPKCS1v15DecryptorShim::~CRSAPKCS1v15DecryptorShim() sl@0: { sl@0: delete iAsymmetricCipherImpl; sl@0: delete iKey; sl@0: } sl@0: sl@0: void CRSAPKCS1v15DecryptorShim::ConstructL(const CRSAPrivateKey& aKey) sl@0: { sl@0: iKey = KeyConverter::CreateKeyL(aKey); sl@0: CAsymmetricCipherFactory::CreateAsymmetricCipherL( sl@0: iAsymmetricCipherImpl, sl@0: KRsaCipherUid, sl@0: *iKey, sl@0: KCryptoModeDecryptUid, sl@0: KPaddingModePkcs1_v1_5_EncryptionUid, sl@0: NULL); sl@0: } sl@0: sl@0: void CRSAPKCS1v15DecryptorShim::DecryptL(const TDesC8& aInput, TDes8& aOutput) const sl@0: { sl@0: iAsymmetricCipherImpl->ProcessL(aInput, aOutput); sl@0: } sl@0: sl@0: TInt CRSAPKCS1v15DecryptorShim::MaxInputLength(void) const sl@0: { sl@0: TInt maxInputLength=0; sl@0: TRAPD(err, maxInputLength=iAsymmetricCipherImpl->GetMaximumInputLengthL()) sl@0: if (err==KErrNone) sl@0: { sl@0: return maxInputLength; sl@0: } sl@0: else sl@0: { sl@0: return err; sl@0: } sl@0: } sl@0: sl@0: TInt CRSAPKCS1v15DecryptorShim::MaxOutputLength(void) const sl@0: { sl@0: TInt maxOutputLength=0; sl@0: TRAPD(err, maxOutputLength=iAsymmetricCipherImpl->GetMaximumOutputLengthL()) sl@0: if (err==KErrNone) sl@0: { sl@0: return maxOutputLength; sl@0: } sl@0: else sl@0: { sl@0: return err; sl@0: } sl@0: } sl@0: sl@0: // CRSAPKCS1v15SignerShim //////////////////////////////////////////////////////// sl@0: CRSAPKCS1v15SignerShim* CRSAPKCS1v15SignerShim::NewL(const CRSAPrivateKey& aKey) sl@0: { sl@0: CRSAPKCS1v15SignerShim* self = CRSAPKCS1v15SignerShim::NewLC(aKey); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CRSAPKCS1v15SignerShim* CRSAPKCS1v15SignerShim::NewLC(const CRSAPrivateKey& aKey) sl@0: { sl@0: CRSAPKCS1v15SignerShim* self = new (ELeave) CRSAPKCS1v15SignerShim(aKey); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aKey); sl@0: return self; sl@0: } sl@0: sl@0: CRSASignature* CRSAPKCS1v15SignerShim::SignL(const TDesC8& aInput) const sl@0: { sl@0: //Sign the input data sl@0: CCryptoParams* signature = CCryptoParams::NewLC(); sl@0: iSignerImpl->SignL(aInput, *signature); sl@0: sl@0: //Retrieve the S in RSA signature from the array sl@0: const TInteger& cS=signature->GetBigIntL(KRsaSignatureParameterSUid); sl@0: sl@0: //Make copies of the RSA signature sl@0: RInteger s=RInteger::NewL(cS); sl@0: CleanupClosePushL(s); sl@0: sl@0: //Create the RSA signature object, the ownership of s is transfered to rsaSig sl@0: CRSASignature* rsaSig=CRSASignature::NewL(s); sl@0: sl@0: //Cleanup sl@0: CleanupStack::Pop(&s); sl@0: CleanupStack::PopAndDestroy(signature); sl@0: return rsaSig; sl@0: } sl@0: sl@0: TInt CRSAPKCS1v15SignerShim::MaxInputLength(void) const sl@0: { sl@0: TInt maxInputLength=0; sl@0: TRAPD(err, maxInputLength=iSignerImpl->GetMaximumInputLengthL()) sl@0: if (err==KErrNone) sl@0: { sl@0: return maxInputLength; sl@0: } sl@0: else sl@0: { sl@0: return err; sl@0: } sl@0: } sl@0: sl@0: TInt CRSAPKCS1v15SignerShim::MaxOutputLength(void) const sl@0: { sl@0: TInt maxOutputLength=0; sl@0: TRAPD(err, maxOutputLength=iSignerImpl->GetMaximumOutputLengthL()) sl@0: if (err==KErrNone) sl@0: { sl@0: return maxOutputLength; sl@0: } sl@0: else sl@0: { sl@0: return err; sl@0: } sl@0: } sl@0: sl@0: CRSAPKCS1v15SignerShim::~CRSAPKCS1v15SignerShim(void) sl@0: { sl@0: delete iSignerImpl; sl@0: delete iKey; sl@0: } sl@0: sl@0: CRSAPKCS1v15SignerShim::CRSAPKCS1v15SignerShim(const CRSAPrivateKey& aKey) sl@0: : CRSAPKCS1v15Signer(aKey) sl@0: { sl@0: } sl@0: sl@0: void CRSAPKCS1v15SignerShim::ConstructL(const CRSAPrivateKey& aKey) sl@0: { sl@0: iKey = KeyConverter::CreateKeyL(aKey); sl@0: CSignatureFactory::CreateSignerL( sl@0: iSignerImpl, sl@0: KRsaSignerUid, sl@0: *iKey, sl@0: KPaddingModePkcs1_v1_5_SignatureUid, sl@0: NULL); sl@0: } sl@0: sl@0: // CRSAPKCS1v15VerifierShim //////////////////////////////////////////////////////// sl@0: CRSAPKCS1v15VerifierShim* CRSAPKCS1v15VerifierShim::NewL(const CRSAPublicKey& aKey) sl@0: { sl@0: CRSAPKCS1v15VerifierShim* self = CRSAPKCS1v15VerifierShim::NewLC(aKey); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CRSAPKCS1v15VerifierShim* CRSAPKCS1v15VerifierShim::NewLC(const CRSAPublicKey& aKey) sl@0: { sl@0: CRSAPKCS1v15VerifierShim* self = new (ELeave) CRSAPKCS1v15VerifierShim(aKey); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aKey); sl@0: return self; sl@0: } sl@0: sl@0: TBool CRSAPKCS1v15VerifierShim::VerifyL(const TDesC8& aInput, const CRSASignature& aSignature) const sl@0: { sl@0: //create the array format rsa signature for the new crypto spi sl@0: CCryptoParams* rsaSig = CCryptoParams::NewLC(); sl@0: sl@0: rsaSig->AddL(aSignature.S(), KRsaSignatureParameterSUid); sl@0: sl@0: //pass the signature and input to crypto spi to be verified sl@0: TBool verificationResult = EFalse; sl@0: iVerifierImpl->VerifyL(aInput, *rsaSig, verificationResult); sl@0: sl@0: //Cleanup the array sl@0: CleanupStack::PopAndDestroy(rsaSig); sl@0: return verificationResult; sl@0: } sl@0: sl@0: HBufC8* CRSAPKCS1v15VerifierShim::InverseSignLC(const CRSASignature& aSignature) const sl@0: { sl@0: //create the array format rsa signature for the new crypto spi sl@0: CCryptoParams* rsaSig = CCryptoParams::NewLC(); sl@0: sl@0: rsaSig->AddL(aSignature.S(), KRsaSignatureParameterSUid); sl@0: sl@0: //pass the signature and input to crypto spi to be verified sl@0: HBufC8* output = NULL; sl@0: iVerifierImpl->InverseSignL(output, *rsaSig); sl@0: sl@0: //Cleanup the array sl@0: CleanupStack::PopAndDestroy(rsaSig); sl@0: sl@0: // leave output on the cleanup stack sl@0: CleanupStack::PushL(output); sl@0: return output; sl@0: } sl@0: sl@0: TInt CRSAPKCS1v15VerifierShim::MaxInputLength(void) const sl@0: { sl@0: TInt maxInputLength=0; sl@0: TRAPD(err, maxInputLength=iVerifierImpl->GetMaximumInputLengthL()) sl@0: if (err==KErrNone) sl@0: { sl@0: return maxInputLength; sl@0: } sl@0: else sl@0: { sl@0: return err; sl@0: } sl@0: } sl@0: sl@0: TInt CRSAPKCS1v15VerifierShim::MaxOutputLength(void) const sl@0: { sl@0: TInt maxOutputLength=0; sl@0: TRAPD(err, maxOutputLength=iVerifierImpl->GetMaximumOutputLengthL()) sl@0: if (err==KErrNone) sl@0: { sl@0: return maxOutputLength; sl@0: } sl@0: else sl@0: { sl@0: return err; sl@0: } sl@0: } sl@0: sl@0: CRSAPKCS1v15VerifierShim::~CRSAPKCS1v15VerifierShim(void) sl@0: { sl@0: delete iVerifierImpl; sl@0: delete iKey; sl@0: } sl@0: sl@0: CRSAPKCS1v15VerifierShim::CRSAPKCS1v15VerifierShim(const CRSAPublicKey& aKey) sl@0: : CRSAPKCS1v15Verifier(aKey) sl@0: { sl@0: } sl@0: sl@0: void CRSAPKCS1v15VerifierShim::ConstructL(const CRSAPublicKey& aKey) sl@0: { sl@0: iKey = KeyConverter::CreateKeyL(aKey); sl@0: CSignatureFactory::CreateVerifierL( sl@0: iVerifierImpl, sl@0: KRsaVerifierUid, sl@0: *iKey, sl@0: KPaddingModePkcs1_v1_5_SignatureUid, sl@0: NULL); sl@0: } sl@0: