crypto/weakcryptospi/source/symmetric/cbcmodeshim.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 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "cbcmodeshim.h"
       
    20 
       
    21 #include <cryptopanic.h>
       
    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 // CModeCBCEncryptorShim
       
    29 CModeCBCEncryptorShim::CModeCBCEncryptorShim(CryptoSpi::CSymmetricCipher* aSymmetricCipherImpl) :
       
    30 	iSymmetricCipherImpl(aSymmetricCipherImpl)
       
    31 	{
       
    32 	}
       
    33 
       
    34 CModeCBCEncryptorShim* CModeCBCEncryptorShim::NewL(CBlockTransformation* aBT, const TDesC8& aIv)
       
    35 	{
       
    36 	CModeCBCEncryptorShim* 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 		// Verify that the plug-in supports CBC mode
       
    52 		if (err == KErrNone && 
       
    53 			cipherCharacteristics->IsOperationModeSupported(CryptoSpi::KOperationModeCBCUid))
       
    54 			{
       
    55 			// Set block transform to encrypt-cbc
       
    56 			impl->SetCryptoModeL(CryptoSpi::KCryptoModeEncryptUid);
       
    57 			impl->SetOperationModeL(CryptoSpi::KOperationModeCBCUid);
       
    58 			impl->SetIvL(aIv);		
       
    59 			self = new(ELeave) CModeCBCEncryptorShim(impl);
       
    60 			CleanupStack::PushL(self);
       
    61 			self->ConstructL(aBT, aIv);
       
    62 			CleanupStack::Pop(self);
       
    63 			}
       
    64 		}				
       
    65 	return self;
       
    66 	}
       
    67 
       
    68 void CModeCBCEncryptorShim::ConstructL(CBlockTransformation* aBT, const TDesC8& aIv)
       
    69 	{
       
    70 	CModeCBCEncryptor::ConstructL(aBT, aIv);
       
    71 	}
       
    72 
       
    73 void CModeCBCEncryptorShim::Reset()
       
    74 	{
       
    75 	iSymmetricCipherImpl->Reset();
       
    76 	}
       
    77 	
       
    78 TInt CModeCBCEncryptorShim::BlockSize() const
       
    79 	{
       
    80 	return BitsToBytes(iSymmetricCipherImpl->BlockSize());
       
    81 	}
       
    82 	
       
    83 TInt CModeCBCEncryptorShim::KeySize() const
       
    84 	{
       
    85 	return iSymmetricCipherImpl->KeySize();
       
    86 	}
       
    87 	
       
    88 void CModeCBCEncryptorShim::Transform(TDes8& aBlock) 
       
    89 	{
       
    90 	// This function will never get called if a buffered
       
    91 	// encryptor is used because Process and ProcessFinalL call
       
    92 	// iSymmetricCipherImpl directly
       
    93 	iBT->Transform(aBlock);	
       
    94 	}
       
    95 	
       
    96 void CModeCBCEncryptorShim::SetIV(const TDesC8& aIv)
       
    97 	{
       
    98 	TRAPD(err, iSymmetricCipherImpl->SetIvL(aIv));
       
    99 	if (err == KErrOverflow)
       
   100 		{
       
   101 		User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge);
       
   102 		}
       
   103 	else if (err != KErrNone)
       
   104 		{
       
   105 		// SetIvL should only leave if the aIv is incorrect
       
   106 		User::Panic(KCryptoPanic, KErrArgument);
       
   107 		}
       
   108 	}
       
   109 
       
   110 TInt CModeCBCEncryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
       
   111 	{
       
   112 	TInt ret(KErrExtensionNotSupported);
       
   113 	
       
   114 	if (CryptoSpi::KSymmetricCipherInterface == aExtensionId)
       
   115 		{		
       
   116 		a0=iSymmetricCipherImpl;
       
   117 		ret=KErrNone;	
       
   118 		}		
       
   119 	return ret;
       
   120 	}		
       
   121 
       
   122 // CModeCBCDecryptorShim
       
   123 CModeCBCDecryptorShim::CModeCBCDecryptorShim(CryptoSpi::CSymmetricCipher* aSymmetricCipherImpl) :
       
   124 	iSymmetricCipherImpl(aSymmetricCipherImpl)
       
   125 	{
       
   126 	}
       
   127 
       
   128 CModeCBCDecryptorShim* CModeCBCDecryptorShim::NewL(CBlockTransformation* aBT, const TDesC8& aIv)
       
   129 	{
       
   130 	CModeCBCDecryptorShim* self(0);
       
   131 	
       
   132 	// Check whether the block transform contains an SPI plug-in
       
   133 	TAny* implPtr(0);
       
   134 	TInt err = aBT->GetExtension(CryptoSpi::KSymmetricCipherInterface, implPtr, NULL);	
       
   135 	if (err == KErrNone && implPtr)
       
   136 		{
       
   137 		CryptoSpi::CSymmetricCipher* impl(static_cast<CryptoSpi::CSymmetricCipher*>(implPtr));
       
   138 		
       
   139 		const CryptoSpi::TCharacteristics* c(0);
       
   140 		impl->GetCharacteristicsL(c);
       
   141 	
       
   142 		const CryptoSpi::TSymmetricCipherCharacteristics* cipherCharacteristics(
       
   143 			static_cast<const CryptoSpi::TSymmetricCipherCharacteristics*>(c));
       
   144 			
       
   145 		// Verify that the plug-in supports CBC mode
       
   146 		if (err == KErrNone && 
       
   147 			cipherCharacteristics->IsOperationModeSupported(CryptoSpi::KOperationModeCBCUid))
       
   148 			{
       
   149 			// Set block transform to encrypt-cbc
       
   150 			impl->SetCryptoModeL(CryptoSpi::KCryptoModeDecryptUid);
       
   151 			impl->SetOperationModeL(CryptoSpi::KOperationModeCBCUid);
       
   152 			impl->SetIvL(aIv);				
       
   153 			self = new(ELeave) CModeCBCDecryptorShim(impl);
       
   154 			CleanupStack::PushL(self);
       
   155 			self->ConstructL(aBT, aIv);
       
   156 			CleanupStack::Pop(self);
       
   157 			}
       
   158 		}				
       
   159 	return self;
       
   160 	}
       
   161 
       
   162 void CModeCBCDecryptorShim::ConstructL(CBlockTransformation* aBT, const TDesC8& aIv)
       
   163 	{
       
   164 	CModeCBCDecryptor::ConstructL(aBT, aIv);
       
   165 	}
       
   166 	
       
   167 void CModeCBCDecryptorShim::Reset()
       
   168 	{
       
   169 	iSymmetricCipherImpl->Reset();
       
   170 	}
       
   171 	
       
   172 TInt CModeCBCDecryptorShim::BlockSize() const
       
   173 	{
       
   174 	return BitsToBytes(iSymmetricCipherImpl->BlockSize());
       
   175 	}
       
   176 	
       
   177 TInt CModeCBCDecryptorShim::KeySize() const
       
   178 	{
       
   179 	return iSymmetricCipherImpl->KeySize();
       
   180 	}
       
   181 
       
   182 void CModeCBCDecryptorShim::Transform(TDes8& aBlock) 
       
   183 	{
       
   184 	// This function will never get called if a buffered
       
   185 	// encryptor is used because Process and ProcessFinalL call
       
   186 	// iSymmetricCipherImpl directly
       
   187 	iBT->Transform(aBlock);	
       
   188 	}
       
   189 
       
   190 void CModeCBCDecryptorShim::SetIV(const TDesC8& aIv)
       
   191 	{
       
   192 	TRAPD(err, iSymmetricCipherImpl->SetIvL(aIv));
       
   193 	if (err == KErrOverflow)
       
   194 		{
       
   195 		User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge);
       
   196 		}
       
   197 	else if (err != KErrNone)
       
   198 		{
       
   199 		// SetIvL should only leave if the aIv is incorrect
       
   200 		User::Panic(KCryptoPanic, KErrArgument);
       
   201 		}
       
   202 	}
       
   203 	
       
   204 TInt CModeCBCDecryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
       
   205 	{
       
   206 	TInt ret(KErrExtensionNotSupported);
       
   207 	
       
   208 	if (CryptoSpi::KSymmetricCipherInterface == aExtensionId)
       
   209 		{		
       
   210 		a0=iSymmetricCipherImpl;
       
   211 		ret=KErrNone;	
       
   212 		}		
       
   213 	return ret;
       
   214 	}