os/security/crypto/weakcryptospi/source/symmetric/cbcmode.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) 2002-2009 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 <cbcmode.h>
sl@0
    20
sl@0
    21
#include "cbcmodeshim.h"
sl@0
    22
#include "../common/inlines.h"
sl@0
    23
sl@0
    24
void CBlockChainingMode::Reset()
sl@0
    25
	{
sl@0
    26
	iRegister.Copy(iIV);
sl@0
    27
	iBT->Reset();
sl@0
    28
	}
sl@0
    29
sl@0
    30
TInt CBlockChainingMode::BlockSize() const
sl@0
    31
	{
sl@0
    32
	return (iBT->BlockSize());
sl@0
    33
	}
sl@0
    34
sl@0
    35
TInt CBlockChainingMode::KeySize() const
sl@0
    36
	{
sl@0
    37
	return (iBT->KeySize());
sl@0
    38
	}
sl@0
    39
sl@0
    40
void CBlockChainingMode::SetIV(const TDesC8& aIV)
sl@0
    41
	{
sl@0
    42
	//We are making the stipulation that anybody calling SetIV is not setting it
sl@0
    43
	//to a longer IV than they originally did.  Otherwise SetIV needs to leave.
sl@0
    44
	assert(aIV.Size() <= iIV.Size());
sl@0
    45
	iIV.Copy(aIV);
sl@0
    46
	Reset();
sl@0
    47
	}
sl@0
    48
sl@0
    49
EXPORT_C CBlockChainingMode::CBlockChainingMode() 
sl@0
    50
	: iBT(NULL), iRegister(0,0,0), iIV(0,0,0)
sl@0
    51
	{
sl@0
    52
	}
sl@0
    53
sl@0
    54
EXPORT_C CBlockChainingMode::~CBlockChainingMode()
sl@0
    55
	{
sl@0
    56
	delete iBT;
sl@0
    57
	delete iRegisterBuf;
sl@0
    58
	delete iIVBuf;
sl@0
    59
	}
sl@0
    60
sl@0
    61
EXPORT_C void CBlockChainingMode::ConstructL(CBlockTransformation* aBT, const TDesC8& aIV)
sl@0
    62
	{
sl@0
    63
	iRegisterBuf = aIV.AllocL();
sl@0
    64
	iRegister.Set(iRegisterBuf->Des());
sl@0
    65
	iIVBuf = aIV.AllocL();
sl@0
    66
	iIV.Set(iIVBuf->Des());
sl@0
    67
sl@0
    68
	// Take ownership last - doesn't take ownership if we leave
sl@0
    69
	iBT = aBT;
sl@0
    70
	}
sl@0
    71
sl@0
    72
/* CModeCBCEncryptor */
sl@0
    73
EXPORT_C CModeCBCEncryptor* CModeCBCEncryptor::NewL(CBlockTransformation* aBT, 
sl@0
    74
	const TDesC8& aIV)
sl@0
    75
	{	
sl@0
    76
	CModeCBCEncryptor* self = CModeCBCEncryptorShim::NewL(aBT, aIV);
sl@0
    77
	if (! self)
sl@0
    78
		{			
sl@0
    79
		// not able to use CryptoSpi, possibly due to an exterally 
sl@0
    80
		// derived legacy class so fallback to old implementation.			
sl@0
    81
		self = NewLC(aBT,aIV);
sl@0
    82
		CleanupStack::Pop(self);
sl@0
    83
		}	
sl@0
    84
	return self;	
sl@0
    85
	}
sl@0
    86
sl@0
    87
EXPORT_C CModeCBCEncryptor* CModeCBCEncryptor::NewLC(CBlockTransformation* aBT,
sl@0
    88
	const TDesC8& aIV)
sl@0
    89
	{
sl@0
    90
	CModeCBCEncryptor* self = new (ELeave)CModeCBCEncryptor();	
sl@0
    91
	CleanupStack::PushL(self);
sl@0
    92
	self->ConstructL(aBT, aIV);
sl@0
    93
	return self;
sl@0
    94
	}
sl@0
    95
sl@0
    96
CModeCBCEncryptor::CModeCBCEncryptor()
sl@0
    97
	{
sl@0
    98
	}
sl@0
    99
sl@0
   100
void CModeCBCEncryptor::Transform(TDes8& aBlock)
sl@0
   101
	{
sl@0
   102
	assert(aBlock.Size() == iBT->BlockSize());
sl@0
   103
	assert(iRegister.Size() == aBlock.Size());
sl@0
   104
sl@0
   105
	XorBuf(const_cast<TUint8*>(iRegister.Ptr()), aBlock.Ptr(), aBlock.Size());
sl@0
   106
	iBT->Transform(iRegister);
sl@0
   107
	aBlock.Copy(iRegister);
sl@0
   108
	}
sl@0
   109
sl@0
   110
/* CModeCBCDecryptor */
sl@0
   111
EXPORT_C CModeCBCDecryptor* CModeCBCDecryptor::NewL(CBlockTransformation* aBT, 
sl@0
   112
	const TDesC8& aIV)
sl@0
   113
	{
sl@0
   114
	CModeCBCDecryptor* self = CModeCBCDecryptorShim::NewL(aBT, aIV);
sl@0
   115
	if (! self)
sl@0
   116
		{			
sl@0
   117
		// not able to use CryptoSpi, possibly due to an exterally 
sl@0
   118
		// derived legacy class so fallback to old implementation.			
sl@0
   119
		self = NewLC(aBT,aIV);
sl@0
   120
		CleanupStack::Pop(self);
sl@0
   121
		}	
sl@0
   122
	return self;	
sl@0
   123
	}
sl@0
   124
sl@0
   125
EXPORT_C CModeCBCDecryptor* CModeCBCDecryptor::NewLC(CBlockTransformation* aBT, 
sl@0
   126
	const TDesC8& aIV)
sl@0
   127
	{
sl@0
   128
	CModeCBCDecryptor* self = new (ELeave)CModeCBCDecryptor();	
sl@0
   129
	CleanupStack::PushL(self);
sl@0
   130
	self->ConstructL(aBT, aIV);
sl@0
   131
	return self;	
sl@0
   132
	}
sl@0
   133
sl@0
   134
void CModeCBCDecryptor::ConstructL(CBlockTransformation* aBT, const TDesC8& aIV)
sl@0
   135
	{
sl@0
   136
	iIVBakBuf = aIV.AllocL();
sl@0
   137
	iIVBak.Set(iIVBakBuf->Des());
sl@0
   138
	CBlockChainingMode::ConstructL(aBT, aIV);
sl@0
   139
	}
sl@0
   140
sl@0
   141
CModeCBCDecryptor::~CModeCBCDecryptor(void)
sl@0
   142
	{
sl@0
   143
	delete iIVBakBuf;
sl@0
   144
	}
sl@0
   145
sl@0
   146
CModeCBCDecryptor::CModeCBCDecryptor()
sl@0
   147
	: iIVBak(0,0,0)
sl@0
   148
	{
sl@0
   149
	}
sl@0
   150
sl@0
   151
void CModeCBCDecryptor::Transform(TDes8& aBlock)
sl@0
   152
	{
sl@0
   153
	assert(aBlock.Size() == iBT->BlockSize());
sl@0
   154
	assert(iRegister.Size() == aBlock.Size());
sl@0
   155
	assert(iIVBak.Size() == aBlock.Size());
sl@0
   156
	
sl@0
   157
	// Take a copy of incoming block
sl@0
   158
	iIVBak.Copy(aBlock);
sl@0
   159
sl@0
   160
	// transform the block
sl@0
   161
	iBT->Transform(aBlock);
sl@0
   162
sl@0
   163
	// xor the output with the register
sl@0
   164
	XorBuf(const_cast<TUint8*>(aBlock.Ptr()), iRegister.Ptr(), 
sl@0
   165
		aBlock.Size());
sl@0
   166
sl@0
   167
	// Update the register to be the original block
sl@0
   168
	iRegister.Copy(iIVBak);
sl@0
   169
}
sl@0
   170