os/security/crypto/weakcryptospi/source/symmetric/cbcmodeshim.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of the License "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include "cbcmodeshim.h"
sl@0
    20
sl@0
    21
#include <cryptopanic.h>
sl@0
    22
#include <cryptospi/cryptospidef.h>
sl@0
    23
#include <padding.h>
sl@0
    24
#include <cryptospi/cryptosymmetriccipherapi.h>
sl@0
    25
#include <cryptospi/plugincharacteristics.h>
sl@0
    26
#include "../common/inlines.h"
sl@0
    27
sl@0
    28
// CModeCBCEncryptorShim
sl@0
    29
CModeCBCEncryptorShim::CModeCBCEncryptorShim(CryptoSpi::CSymmetricCipher* aSymmetricCipherImpl) :
sl@0
    30
	iSymmetricCipherImpl(aSymmetricCipherImpl)
sl@0
    31
	{
sl@0
    32
	}
sl@0
    33
sl@0
    34
CModeCBCEncryptorShim* CModeCBCEncryptorShim::NewL(CBlockTransformation* aBT, const TDesC8& aIv)
sl@0
    35
	{
sl@0
    36
	CModeCBCEncryptorShim* self(0);
sl@0
    37
	
sl@0
    38
	// Check whether the block transform contains an SPI plug-in
sl@0
    39
	TAny* implPtr(0);
sl@0
    40
	TInt err = aBT->GetExtension(CryptoSpi::KSymmetricCipherInterface, implPtr, NULL);	
sl@0
    41
	if (err == KErrNone && implPtr)
sl@0
    42
		{
sl@0
    43
		CryptoSpi::CSymmetricCipher* impl(static_cast<CryptoSpi::CSymmetricCipher*>(implPtr));
sl@0
    44
		
sl@0
    45
		const CryptoSpi::TCharacteristics* c(0);
sl@0
    46
		impl->GetCharacteristicsL(c);
sl@0
    47
	
sl@0
    48
		const CryptoSpi::TSymmetricCipherCharacteristics* cipherCharacteristics(
sl@0
    49
			static_cast<const CryptoSpi::TSymmetricCipherCharacteristics*>(c));
sl@0
    50
			
sl@0
    51
		// Verify that the plug-in supports CBC mode
sl@0
    52
		if (err == KErrNone && 
sl@0
    53
			cipherCharacteristics->IsOperationModeSupported(CryptoSpi::KOperationModeCBCUid))
sl@0
    54
			{
sl@0
    55
			// Set block transform to encrypt-cbc
sl@0
    56
			impl->SetCryptoModeL(CryptoSpi::KCryptoModeEncryptUid);
sl@0
    57
			impl->SetOperationModeL(CryptoSpi::KOperationModeCBCUid);
sl@0
    58
			impl->SetIvL(aIv);		
sl@0
    59
			self = new(ELeave) CModeCBCEncryptorShim(impl);
sl@0
    60
			CleanupStack::PushL(self);
sl@0
    61
			self->ConstructL(aBT, aIv);
sl@0
    62
			CleanupStack::Pop(self);
sl@0
    63
			}
sl@0
    64
		}				
sl@0
    65
	return self;
sl@0
    66
	}
sl@0
    67
sl@0
    68
void CModeCBCEncryptorShim::ConstructL(CBlockTransformation* aBT, const TDesC8& aIv)
sl@0
    69
	{
sl@0
    70
	CModeCBCEncryptor::ConstructL(aBT, aIv);
sl@0
    71
	}
sl@0
    72
sl@0
    73
void CModeCBCEncryptorShim::Reset()
sl@0
    74
	{
sl@0
    75
	iSymmetricCipherImpl->Reset();
sl@0
    76
	}
sl@0
    77
	
sl@0
    78
TInt CModeCBCEncryptorShim::BlockSize() const
sl@0
    79
	{
sl@0
    80
	return BitsToBytes(iSymmetricCipherImpl->BlockSize());
sl@0
    81
	}
sl@0
    82
	
sl@0
    83
TInt CModeCBCEncryptorShim::KeySize() const
sl@0
    84
	{
sl@0
    85
	return iSymmetricCipherImpl->KeySize();
sl@0
    86
	}
sl@0
    87
	
sl@0
    88
void CModeCBCEncryptorShim::Transform(TDes8& aBlock) 
sl@0
    89
	{
sl@0
    90
	// This function will never get called if a buffered
sl@0
    91
	// encryptor is used because Process and ProcessFinalL call
sl@0
    92
	// iSymmetricCipherImpl directly
sl@0
    93
	iBT->Transform(aBlock);	
sl@0
    94
	}
sl@0
    95
	
sl@0
    96
void CModeCBCEncryptorShim::SetIV(const TDesC8& aIv)
sl@0
    97
	{
sl@0
    98
	TRAPD(err, iSymmetricCipherImpl->SetIvL(aIv));
sl@0
    99
	if (err == KErrOverflow)
sl@0
   100
		{
sl@0
   101
		User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge);
sl@0
   102
		}
sl@0
   103
	else if (err != KErrNone)
sl@0
   104
		{
sl@0
   105
		// SetIvL should only leave if the aIv is incorrect
sl@0
   106
		User::Panic(KCryptoPanic, KErrArgument);
sl@0
   107
		}
sl@0
   108
	}
sl@0
   109
sl@0
   110
TInt CModeCBCEncryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
sl@0
   111
	{
sl@0
   112
	TInt ret(KErrExtensionNotSupported);
sl@0
   113
	
sl@0
   114
	if (CryptoSpi::KSymmetricCipherInterface == aExtensionId)
sl@0
   115
		{		
sl@0
   116
		a0=iSymmetricCipherImpl;
sl@0
   117
		ret=KErrNone;	
sl@0
   118
		}		
sl@0
   119
	return ret;
sl@0
   120
	}		
sl@0
   121
sl@0
   122
// CModeCBCDecryptorShim
sl@0
   123
CModeCBCDecryptorShim::CModeCBCDecryptorShim(CryptoSpi::CSymmetricCipher* aSymmetricCipherImpl) :
sl@0
   124
	iSymmetricCipherImpl(aSymmetricCipherImpl)
sl@0
   125
	{
sl@0
   126
	}
sl@0
   127
sl@0
   128
CModeCBCDecryptorShim* CModeCBCDecryptorShim::NewL(CBlockTransformation* aBT, const TDesC8& aIv)
sl@0
   129
	{
sl@0
   130
	CModeCBCDecryptorShim* self(0);
sl@0
   131
	
sl@0
   132
	// Check whether the block transform contains an SPI plug-in
sl@0
   133
	TAny* implPtr(0);
sl@0
   134
	TInt err = aBT->GetExtension(CryptoSpi::KSymmetricCipherInterface, implPtr, NULL);	
sl@0
   135
	if (err == KErrNone && implPtr)
sl@0
   136
		{
sl@0
   137
		CryptoSpi::CSymmetricCipher* impl(static_cast<CryptoSpi::CSymmetricCipher*>(implPtr));
sl@0
   138
		
sl@0
   139
		const CryptoSpi::TCharacteristics* c(0);
sl@0
   140
		impl->GetCharacteristicsL(c);
sl@0
   141
	
sl@0
   142
		const CryptoSpi::TSymmetricCipherCharacteristics* cipherCharacteristics(
sl@0
   143
			static_cast<const CryptoSpi::TSymmetricCipherCharacteristics*>(c));
sl@0
   144
			
sl@0
   145
		// Verify that the plug-in supports CBC mode
sl@0
   146
		if (err == KErrNone && 
sl@0
   147
			cipherCharacteristics->IsOperationModeSupported(CryptoSpi::KOperationModeCBCUid))
sl@0
   148
			{
sl@0
   149
			// Set block transform to encrypt-cbc
sl@0
   150
			impl->SetCryptoModeL(CryptoSpi::KCryptoModeDecryptUid);
sl@0
   151
			impl->SetOperationModeL(CryptoSpi::KOperationModeCBCUid);
sl@0
   152
			impl->SetIvL(aIv);				
sl@0
   153
			self = new(ELeave) CModeCBCDecryptorShim(impl);
sl@0
   154
			CleanupStack::PushL(self);
sl@0
   155
			self->ConstructL(aBT, aIv);
sl@0
   156
			CleanupStack::Pop(self);
sl@0
   157
			}
sl@0
   158
		}				
sl@0
   159
	return self;
sl@0
   160
	}
sl@0
   161
sl@0
   162
void CModeCBCDecryptorShim::ConstructL(CBlockTransformation* aBT, const TDesC8& aIv)
sl@0
   163
	{
sl@0
   164
	CModeCBCDecryptor::ConstructL(aBT, aIv);
sl@0
   165
	}
sl@0
   166
	
sl@0
   167
void CModeCBCDecryptorShim::Reset()
sl@0
   168
	{
sl@0
   169
	iSymmetricCipherImpl->Reset();
sl@0
   170
	}
sl@0
   171
	
sl@0
   172
TInt CModeCBCDecryptorShim::BlockSize() const
sl@0
   173
	{
sl@0
   174
	return BitsToBytes(iSymmetricCipherImpl->BlockSize());
sl@0
   175
	}
sl@0
   176
	
sl@0
   177
TInt CModeCBCDecryptorShim::KeySize() const
sl@0
   178
	{
sl@0
   179
	return iSymmetricCipherImpl->KeySize();
sl@0
   180
	}
sl@0
   181
sl@0
   182
void CModeCBCDecryptorShim::Transform(TDes8& aBlock) 
sl@0
   183
	{
sl@0
   184
	// This function will never get called if a buffered
sl@0
   185
	// encryptor is used because Process and ProcessFinalL call
sl@0
   186
	// iSymmetricCipherImpl directly
sl@0
   187
	iBT->Transform(aBlock);	
sl@0
   188
	}
sl@0
   189
sl@0
   190
void CModeCBCDecryptorShim::SetIV(const TDesC8& aIv)
sl@0
   191
	{
sl@0
   192
	TRAPD(err, iSymmetricCipherImpl->SetIvL(aIv));
sl@0
   193
	if (err == KErrOverflow)
sl@0
   194
		{
sl@0
   195
		User::Panic(KCryptoPanic, ECryptoPanicInputTooLarge);
sl@0
   196
		}
sl@0
   197
	else if (err != KErrNone)
sl@0
   198
		{
sl@0
   199
		// SetIvL should only leave if the aIv is incorrect
sl@0
   200
		User::Panic(KCryptoPanic, KErrArgument);
sl@0
   201
		}
sl@0
   202
	}
sl@0
   203
	
sl@0
   204
TInt CModeCBCDecryptorShim::Extension_(TUint aExtensionId, TAny*& a0, TAny* /*a1*/)
sl@0
   205
	{
sl@0
   206
	TInt ret(KErrExtensionNotSupported);
sl@0
   207
	
sl@0
   208
	if (CryptoSpi::KSymmetricCipherInterface == aExtensionId)
sl@0
   209
		{		
sl@0
   210
		a0=iSymmetricCipherImpl;
sl@0
   211
		ret=KErrNone;	
sl@0
   212
		}		
sl@0
   213
	return ret;
sl@0
   214
	}