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