crypto/weakcrypto/source/symmetric/cbcmode.cpp
author hgs
Thu, 12 Aug 2010 21:07:10 +0530
changeset 90 8c545fea2798
parent 72 de46a57f75fb
permissions -rw-r--r--
201031_01
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
72
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     1
/*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     2
* Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     3
* All rights reserved.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     4
* This component and the accompanying materials are made available
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     6
* which accompanies this distribution, and is available
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     8
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
     9
* Initial Contributors:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    11
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    12
* Contributors:
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    13
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    14
* Description: 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    15
*
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    16
*/
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    17
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    18
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    19
#include "cbcmode.h"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    20
#include "../common/inlines.h"
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    21
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    22
void CBlockChainingMode::Reset()
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    23
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    24
	iRegister.Copy(iIV);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    25
	iBT->Reset();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    26
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    27
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    28
TInt CBlockChainingMode::BlockSize() const
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    29
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    30
	return (iBT->BlockSize());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    31
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    32
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    33
TInt CBlockChainingMode::KeySize() const
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    34
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    35
	return (iBT->KeySize());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    36
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    37
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    38
void CBlockChainingMode::SetIV(const TDesC8& aIV)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    39
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    40
	//We are making the stipulation that anybody calling SetIV is not setting it
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    41
	//to a longer IV than they originally did.  Otherwise SetIV needs to leave.
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    42
	assert(aIV.Size() <= iIV.Size());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    43
	iIV.Copy(aIV);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    44
	Reset();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    45
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    46
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    47
EXPORT_C CBlockChainingMode::CBlockChainingMode() 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    48
	: iBT(NULL), iRegister(0,0,0), iIV(0,0,0)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    49
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    50
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    51
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    52
EXPORT_C CBlockChainingMode::~CBlockChainingMode()
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    53
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    54
	delete iBT;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    55
	delete iRegisterBuf;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    56
	delete iIVBuf;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    57
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    58
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    59
EXPORT_C void CBlockChainingMode::ConstructL(CBlockTransformation* aBT, const TDesC8& aIV)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    60
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    61
	iRegisterBuf = aIV.AllocL();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    62
	iRegister.Set(iRegisterBuf->Des());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    63
	iIVBuf = aIV.AllocL();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    64
	iIV.Set(iIVBuf->Des());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    65
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    66
	// Take ownership last - doesn't take ownership if we leave
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    67
	iBT = aBT;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    68
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    69
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    70
/* CModeCBCEncryptor */
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    71
EXPORT_C CModeCBCEncryptor* CModeCBCEncryptor::NewL(CBlockTransformation* aBT, 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    72
	const TDesC8& aIV)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    73
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    74
	CModeCBCEncryptor* self = NewLC(aBT, aIV);	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    75
	CleanupStack::Pop(self);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    76
	return self;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    77
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    78
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    79
EXPORT_C CModeCBCEncryptor* CModeCBCEncryptor::NewLC(CBlockTransformation* aBT,
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    80
	const TDesC8& aIV)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    81
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    82
	CModeCBCEncryptor* self = new (ELeave)CModeCBCEncryptor();	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    83
	CleanupStack::PushL(self);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    84
	self->ConstructL(aBT, aIV);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    85
	return self;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    86
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    87
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    88
CModeCBCEncryptor::CModeCBCEncryptor()
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    89
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    90
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    91
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    92
void CModeCBCEncryptor::Transform(TDes8& aBlock)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    93
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    94
	assert(aBlock.Size() == iBT->BlockSize());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    95
	assert(iRegister.Size() == aBlock.Size());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    96
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    97
	XorBuf(const_cast<TUint8*>(iRegister.Ptr()), aBlock.Ptr(), aBlock.Size());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    98
	iBT->Transform(iRegister);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
    99
	aBlock.Copy(iRegister);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   100
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   101
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   102
/* CModeCBCDecryptor */
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   103
EXPORT_C CModeCBCDecryptor* CModeCBCDecryptor::NewL(CBlockTransformation* aBT, 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   104
	const TDesC8& aIV)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   105
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   106
	CModeCBCDecryptor* self = NewLC(aBT, aIV);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   107
	CleanupStack::Pop(self);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   108
	return self;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   109
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   110
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   111
EXPORT_C CModeCBCDecryptor* CModeCBCDecryptor::NewLC(CBlockTransformation* aBT, 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   112
	const TDesC8& aIV)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   113
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   114
	CModeCBCDecryptor* self = new (ELeave)CModeCBCDecryptor();	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   115
	CleanupStack::PushL(self);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   116
	self->ConstructL(aBT, aIV);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   117
	return self;	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   118
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   119
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   120
void CModeCBCDecryptor::ConstructL(CBlockTransformation* aBT, const TDesC8& aIV)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   121
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   122
	iIVBakBuf = aIV.AllocL();
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   123
	iIVBak.Set(iIVBakBuf->Des());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   124
	CBlockChainingMode::ConstructL(aBT, aIV);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   125
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   126
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   127
CModeCBCDecryptor::~CModeCBCDecryptor(void)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   128
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   129
	delete iIVBakBuf;
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   130
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   131
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   132
CModeCBCDecryptor::CModeCBCDecryptor()
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   133
	: iIVBak(0,0,0)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   134
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   135
	}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   136
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   137
void CModeCBCDecryptor::Transform(TDes8& aBlock)
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   138
	{
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   139
	assert(aBlock.Size() == iBT->BlockSize());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   140
	assert(iRegister.Size() == aBlock.Size());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   141
	assert(iIVBak.Size() == aBlock.Size());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   142
	
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   143
	// Take a copy of incoming block
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   144
	iIVBak.Copy(aBlock);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   145
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   146
	// transform the block
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   147
	iBT->Transform(aBlock);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   148
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   149
	// xor the output with the register
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   150
	XorBuf(const_cast<TUint8*>(aBlock.Ptr()), iRegister.Ptr(), 
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   151
		aBlock.Size());
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   152
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   153
	// Update the register to be the original block
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   154
	iRegister.Copy(iIVBak);
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   155
}
de46a57f75fb 201023_02
hgs
parents:
diff changeset
   156