crypto/weakcryptospi/source/symmetric/bufferedtransformationshim.cpp
changeset 19 cd501b96611d
child 43 2f10d260163b
equal deleted inserted replaced
15:da2ae96f639b 19:cd501b96611d
       
     1 /*
       
     2 * Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     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".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * blocktransformationshim.cpp
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "bufferedtransformationshim.h"
       
    21 
       
    22 #include <cryptospi/cryptospidef.h>
       
    23 #include <padding.h>
       
    24 #include "cryptosymmetriccipherapi.h"
       
    25 #include <cryptospi/plugincharacteristics.h>
       
    26 #include "../common/inlines.h"
       
    27 
       
    28 // CBufferedEncryptorShim
       
    29 CBufferedEncryptorShim::CBufferedEncryptorShim(CryptoSpi::CSymmetricCipher* aSymmetricCipherImpl) :
       
    30 	iSymmetricCipherImpl(aSymmetricCipherImpl)
       
    31 	{
       
    32 	}
       
    33 
       
    34 CBufferedEncryptorShim* CBufferedEncryptorShim::NewL(CBlockTransformation* aBT, CPadding* aPadding)
       
    35 	{
       
    36 	CBufferedEncryptorShim* self(0);
       
    37 	
       
    38 	// Check whether the block transform contains an SPI plug-in
       
    39 	TAny* implPtr(0);
       
    40 	TInt err = aBT->GetExtension(CryptoSpi::KSymmetricCipherInterface, implPtr, NULL);	
       
    41 	if (err == KErrNone && implPtr)
       
    42 		{
       
    43 		CryptoSpi::CSymmetricCipher* impl(static_cast<CryptoSpi::CSymmetricCipher*>(implPtr));
       
    44 		
       
    45 		const CryptoSpi::TCharacteristics* c(0);
       
    46 		impl->GetCharacteristicsL(c);
       
    47 	
       
    48 		const CryptoSpi::TSymmetricCipherCharacteristics* cipherCharacteristics(
       
    49 			static_cast<const CryptoSpi::TSymmetricCipherCharacteristics*>(c));
       
    50 			
       
    51 		// See if the padding mode is recognised by CryptoSpi and if so, check
       
    52 		// whether the plug-in supports that padding mode.
       
    53 		TUid paddingMode;
       
    54 		TAny* paddingPtr = &paddingMode;		
       
    55 		err = aPadding->GetExtension(CryptoSpi::KPaddingInterface, paddingPtr, 0);		
       
    56 		if (err == KErrNone && cipherCharacteristics->IsPaddingModeSupported(paddingMode))
       
    57 			{
       
    58 			impl->SetCryptoModeL(CryptoSpi::KCryptoModeEncryptUid);
       
    59 			impl->SetPaddingModeL(paddingMode);		
       
    60 			self = new(ELeave) CBufferedEncryptorShim(impl);
       
    61 			CleanupStack::PushL(self);
       
    62 			self->ConstructL(aBT, aPadding);
       
    63 			CleanupStack::Pop(self);
       
    64 			}
       
    65 		}				
       
    66 	return self;
       
    67 	}
       
    68 
       
    69 void CBufferedEncryptorShim::ConstructL(CBlockTransformation* aBT, CPadding* aPadding)
       
    70 	{
       
    71 	CBufferedEncryptor::ConstructL(aBT, aPadding);
       
    72 	}
       
    73 
       
    74 void CBufferedEncryptorShim::Process(const TDesC8& aInput, TDes8& aOutput)
       
    75 	{
       
    76 	TRAP_IGNORE(iSymmetricCipherImpl->ProcessL(aInput, aOutput);)
       
    77 	}
       
    78 	
       
    79 TInt CBufferedEncryptorShim::MaxOutputLength(TInt aInputLength) const
       
    80 	{
       
    81 	return iSymmetricCipherImpl->MaxOutputLength(aInputLength);
       
    82 	}
       
    83 	
       
    84 void CBufferedEncryptorShim::Reset()
       
    85 	{
       
    86 	iSymmetricCipherImpl->Reset();
       
    87 	}
       
    88 	
       
    89 TInt CBufferedEncryptorShim::BlockSize() const
       
    90 	{
       
    91 	return BitsToBytes(iSymmetricCipherImpl->BlockSize());
       
    92 	}
       
    93 	
       
    94 TInt CBufferedEncryptorShim::KeySize() const
       
    95 	{
       
    96 	return iSymmetricCipherImpl->KeySize();
       
    97 	}
       
    98 	
       
    99 void CBufferedEncryptorShim::ProcessFinalL(const TDesC8& aInput, TDes8& aOutput)
       
   100 	{
       
   101 	iSymmetricCipherImpl->ProcessFinalL(aInput, aOutput);
       
   102 	}
       
   103 	
       
   104 TInt CBufferedEncryptorShim::MaxFinalOutputLength(TInt aInputLength) const
       
   105 	{
       
   106 	return iSymmetricCipherImpl->MaxFinalOutputLength(aInputLength);
       
   107 	}
       
   108 
       
   109 // CBufferedDecryptorShim
       
   110 CBufferedDecryptorShim::CBufferedDecryptorShim(CryptoSpi::CSymmetricCipher* aSymmetricCipherImpl) :
       
   111 	iSymmetricCipherImpl(aSymmetricCipherImpl)
       
   112 	{
       
   113 	}
       
   114 
       
   115 CBufferedDecryptorShim* CBufferedDecryptorShim::NewL(CBlockTransformation* aBT, CPadding* aPadding)
       
   116 	{
       
   117 	CBufferedDecryptorShim* self(0);
       
   118 	
       
   119 	// Check whether the block transform contains an SPI plug-in
       
   120 	TAny* implPtr(0);
       
   121 	TInt err = aBT->GetExtension(CryptoSpi::KSymmetricCipherInterface, implPtr, NULL);	
       
   122 	if (err == KErrNone && implPtr)
       
   123 		{
       
   124 		CryptoSpi::CSymmetricCipher* impl(static_cast<CryptoSpi::CSymmetricCipher*>(implPtr));
       
   125 		
       
   126 		const CryptoSpi::TCharacteristics* c(0);
       
   127 		impl->GetCharacteristicsL(c);
       
   128 	
       
   129 		const CryptoSpi::TSymmetricCipherCharacteristics* cipherCharacteristics(
       
   130 			static_cast<const CryptoSpi::TSymmetricCipherCharacteristics*>(c));
       
   131 			
       
   132 		// See if the padding mode is recognised by CryptoSpi and if so, check
       
   133 		// whether the plug-in supports that padding mode.
       
   134 		TUid paddingMode;
       
   135 		TAny* paddingPtr = &paddingMode;		
       
   136 		err = aPadding->GetExtension(CryptoSpi::KPaddingInterface, paddingPtr, 0);		
       
   137 		if (err == KErrNone && cipherCharacteristics->IsPaddingModeSupported(paddingMode))
       
   138 			{
       
   139 			impl->SetCryptoModeL(CryptoSpi::KCryptoModeDecryptUid);			
       
   140 			impl->SetPaddingModeL(paddingMode);
       
   141 			
       
   142 			self = new(ELeave) CBufferedDecryptorShim(impl);
       
   143 			CleanupStack::PushL(self);
       
   144 			self->ConstructL(aBT, aPadding);
       
   145 			CleanupStack::Pop(self);
       
   146 			}
       
   147 		}				
       
   148 	return self;
       
   149 	}
       
   150 
       
   151 void CBufferedDecryptorShim::ConstructL(CBlockTransformation* aBT, CPadding* aPadding)
       
   152 	{
       
   153 	CBufferedDecryptor::ConstructL(aBT, aPadding);
       
   154 	}
       
   155 
       
   156 void CBufferedDecryptorShim::Process(const TDesC8& aInput, TDes8& aOutput)
       
   157 	{
       
   158 	TRAP_IGNORE(iSymmetricCipherImpl->ProcessL(aInput, aOutput);)
       
   159 	}
       
   160 	
       
   161 TInt CBufferedDecryptorShim::MaxOutputLength(TInt aInputLength) const
       
   162 	{
       
   163 	return iSymmetricCipherImpl->MaxOutputLength(aInputLength);
       
   164 	}
       
   165 	
       
   166 void CBufferedDecryptorShim::Reset()
       
   167 	{
       
   168 	iSymmetricCipherImpl->Reset();
       
   169 	}
       
   170 	
       
   171 TInt CBufferedDecryptorShim::BlockSize() const
       
   172 	{
       
   173 	return BitsToBytes(iSymmetricCipherImpl->BlockSize());
       
   174 	}
       
   175 	
       
   176 TInt CBufferedDecryptorShim::KeySize() const
       
   177 	{
       
   178 	return iSymmetricCipherImpl->KeySize();
       
   179 	}
       
   180 	
       
   181 void CBufferedDecryptorShim::ProcessFinalL(const TDesC8& aInput, TDes8& aOutput)
       
   182 	{
       
   183 	iSymmetricCipherImpl->ProcessFinalL(aInput, aOutput);
       
   184 	}
       
   185 	
       
   186 TInt CBufferedDecryptorShim::MaxFinalOutputLength(TInt aInputLength) const
       
   187 	{
       
   188 	return iSymmetricCipherImpl->MaxFinalOutputLength(aInputLength);
       
   189 	}