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