os/security/crypto/weakcryptospi/source/symmetric/bufferedtransformation.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2002-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 "bufferedtransformation.h"
sl@0
    20
sl@0
    21
#include <cryptospi/cryptospidef.h>
sl@0
    22
#include <cryptopanic.h>
sl@0
    23
#include <e32cmn.h>
sl@0
    24
#include <cryptospi/symmetriccipherplugin.h>
sl@0
    25
sl@0
    26
#include "blocktransformation.h"
sl@0
    27
#include "bufferedtransformationshim.h"
sl@0
    28
#include "padding.h"
sl@0
    29
#include "../common/inlines.h"
sl@0
    30
sl@0
    31
EXPORT_C CBufferedTransformation::~CBufferedTransformation()
sl@0
    32
	{
sl@0
    33
	delete iBT;
sl@0
    34
	delete iPadding;
sl@0
    35
	delete iInputStoreBuf;
sl@0
    36
	}
sl@0
    37
sl@0
    38
void CBufferedTransformation::Process(const TDesC8& aInput, TDes8& aOutput)
sl@0
    39
	{
sl@0
    40
	__ASSERT_DEBUG(aOutput.MaxLength() >= MaxOutputLength(aInput.Length()), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
sl@0
    41
sl@0
    42
	TInt blockSize = iBT->BlockSize();
sl@0
    43
sl@0
    44
	if ( (aInput.Size() + iInputStore.Size()) < blockSize )
sl@0
    45
		{
sl@0
    46
		iInputStore.Append(aInput);
sl@0
    47
		}
sl@0
    48
	else
sl@0
    49
		{
sl@0
    50
		TInt outputIndex = aOutput.Size();
sl@0
    51
		aOutput.Append(iInputStore);
sl@0
    52
sl@0
    53
		TInt inputIndex = blockSize - iInputStore.Size();
sl@0
    54
		aOutput.Append(aInput.Mid(0, inputIndex));
sl@0
    55
sl@0
    56
		TPtr8 transformBuf((TUint8*)(aOutput.Ptr()) + outputIndex, blockSize,
sl@0
    57
			blockSize);
sl@0
    58
		//This should read: 
sl@0
    59
		//TPtr8 transformBuf(aOutput.Mid(outputIndex, blockSize));
sl@0
    60
		//but in the wonderful world of descriptors, Mid returns a TPtrC8 even
sl@0
    61
		//when called on a TPtr8.  Fantastic eh?
sl@0
    62
		iBT->Transform(transformBuf);
sl@0
    63
sl@0
    64
		outputIndex += blockSize;
sl@0
    65
sl@0
    66
		TInt len = aInput.Size() - blockSize;
sl@0
    67
sl@0
    68
		for (; inputIndex<=len; inputIndex+=blockSize)
sl@0
    69
			{
sl@0
    70
			aOutput.Append(aInput.Mid(inputIndex, blockSize));			
sl@0
    71
			transformBuf.Set((TUint8*)(aOutput.Ptr()) + outputIndex, blockSize,
sl@0
    72
				blockSize);
sl@0
    73
			iBT->Transform(transformBuf);
sl@0
    74
			outputIndex += blockSize;
sl@0
    75
			}
sl@0
    76
sl@0
    77
		iInputStore.Zero();
sl@0
    78
		if (inputIndex < aInput.Size())
sl@0
    79
			iInputStore.Append(aInput.Mid(inputIndex));
sl@0
    80
		}
sl@0
    81
	}
sl@0
    82
sl@0
    83
TInt CBufferedTransformation::MaxOutputLength(TInt aInputLength) const
sl@0
    84
	{
sl@0
    85
	TInt rem = (aInputLength + iInputStore.Size()) % (iBT->BlockSize());
sl@0
    86
	return ((aInputLength + iInputStore.Size()) - rem);
sl@0
    87
	}
sl@0
    88
sl@0
    89
void CBufferedTransformation::Reset()
sl@0
    90
	{
sl@0
    91
	iBT->Reset();
sl@0
    92
	iInputStore.Zero();
sl@0
    93
	}
sl@0
    94
sl@0
    95
TInt CBufferedTransformation::BlockSize() const
sl@0
    96
	{
sl@0
    97
	return (iBT->BlockSize());
sl@0
    98
	}
sl@0
    99
sl@0
   100
TInt CBufferedTransformation::KeySize() const
sl@0
   101
	{
sl@0
   102
	return (iBT->KeySize());
sl@0
   103
	}
sl@0
   104
sl@0
   105
EXPORT_C CBlockTransformation* CBufferedTransformation::BlockTransformer() const
sl@0
   106
{
sl@0
   107
	return (iBT);
sl@0
   108
}
sl@0
   109
sl@0
   110
CBufferedTransformation::CBufferedTransformation()
sl@0
   111
	: iInputStore(0,0,0)
sl@0
   112
	{
sl@0
   113
	}
sl@0
   114
sl@0
   115
void CBufferedTransformation::ConstructL(CBlockTransformation* aBT, CPadding* aPadding)
sl@0
   116
	{
sl@0
   117
	iInputStoreBuf = HBufC8::NewL(aBT->BlockSize());
sl@0
   118
	iInputStore.Set(iInputStoreBuf->Des());
sl@0
   119
sl@0
   120
	// Take ownership last - doesn't take ownership if we leave
sl@0
   121
	iBT = aBT;
sl@0
   122
	iPadding = aPadding;
sl@0
   123
	}
sl@0
   124
sl@0
   125
sl@0
   126
// CBufferedEncryptor
sl@0
   127
sl@0
   128
EXPORT_C CBufferedEncryptor* CBufferedEncryptor::NewL(
sl@0
   129
	CBlockTransformation* aBT, CPadding* aPadding)
sl@0
   130
	{
sl@0
   131
	CBufferedEncryptor* self = CBufferedEncryptorShim::NewL(aBT, aPadding);
sl@0
   132
	if (! self)
sl@0
   133
		{			
sl@0
   134
		// not able to use CryptoSpi, possibly due to an exterally 
sl@0
   135
		// derived legacy class so fallback to old implementation.			
sl@0
   136
		self = NewLC(aBT,aPadding);
sl@0
   137
		CleanupStack::Pop(self);
sl@0
   138
		}	
sl@0
   139
	return self;
sl@0
   140
	}
sl@0
   141
sl@0
   142
EXPORT_C CBufferedEncryptor* CBufferedEncryptor::NewLC(
sl@0
   143
	CBlockTransformation* aBT, CPadding* aPadding)
sl@0
   144
	{
sl@0
   145
	CBufferedEncryptor* self = new (ELeave) CBufferedEncryptor();
sl@0
   146
	CleanupStack::PushL(self);
sl@0
   147
	self->ConstructL(aBT, aPadding);
sl@0
   148
	return self;
sl@0
   149
	}
sl@0
   150
sl@0
   151
void CBufferedEncryptor::ConstructL(CBlockTransformation* aBT, CPadding* aPadding) 
sl@0
   152
	{
sl@0
   153
	CBufferedTransformation::ConstructL(aBT, aPadding);
sl@0
   154
	}
sl@0
   155
sl@0
   156
CBufferedEncryptor::CBufferedEncryptor()
sl@0
   157
	{
sl@0
   158
	}
sl@0
   159
sl@0
   160
void CBufferedEncryptor::ProcessFinalL(const TDesC8& aInput, TDes8& aOutput)
sl@0
   161
	{
sl@0
   162
	__ASSERT_DEBUG(aOutput.MaxLength() >= MaxFinalOutputLength(aInput.Length()), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
sl@0
   163
	Process(aInput, aOutput);
sl@0
   164
	
sl@0
   165
	TInt outputIndex = aOutput.Size();
sl@0
   166
	iPadding->PadL(iInputStore, aOutput);
sl@0
   167
	assert(aOutput.Size() % iBT->BlockSize() == 0);
sl@0
   168
sl@0
   169
	TUint blockSize = iBT->BlockSize();
sl@0
   170
	TInt len = aOutput.Size() - outputIndex;
sl@0
   171
	
sl@0
   172
	for(TInt i=len; i>0; i-=blockSize)
sl@0
   173
		{
sl@0
   174
		TPtr8 transformBuf((TUint8*)(aOutput.Ptr()) + outputIndex, blockSize,
sl@0
   175
			blockSize);
sl@0
   176
		iBT->Transform(transformBuf);
sl@0
   177
		outputIndex+=blockSize;
sl@0
   178
		}
sl@0
   179
sl@0
   180
	iInputStore.Zero();
sl@0
   181
	}
sl@0
   182
sl@0
   183
TInt CBufferedEncryptor::MaxFinalOutputLength(TInt aInputLength) const
sl@0
   184
	{
sl@0
   185
    return iPadding->MaxPaddedLength(iInputStore.Size() + aInputLength);
sl@0
   186
	}
sl@0
   187
sl@0
   188
// CBufferedDecryptor
sl@0
   189
sl@0
   190
EXPORT_C CBufferedDecryptor* CBufferedDecryptor::NewL(
sl@0
   191
	CBlockTransformation* aBT, CPadding* aPadding)
sl@0
   192
	{
sl@0
   193
	CBufferedDecryptor* self = CBufferedDecryptorShim::NewL(aBT, aPadding);
sl@0
   194
	if (! self)
sl@0
   195
		{			
sl@0
   196
		// not able to use CryptoSpi, possibly due to an exterally 
sl@0
   197
		// derived legacy class so fallback to old implementation.			
sl@0
   198
		self = NewLC(aBT,aPadding);
sl@0
   199
		CleanupStack::Pop(self);
sl@0
   200
		}	
sl@0
   201
	return self;
sl@0
   202
	}
sl@0
   203
sl@0
   204
EXPORT_C CBufferedDecryptor* CBufferedDecryptor::NewLC(
sl@0
   205
	CBlockTransformation* aBT, CPadding* aPadding)
sl@0
   206
	{
sl@0
   207
	CBufferedDecryptor* self = new (ELeave) CBufferedDecryptor();
sl@0
   208
	CleanupStack::PushL(self);
sl@0
   209
	self->ConstructL(aBT, aPadding);
sl@0
   210
	return self;
sl@0
   211
	}
sl@0
   212
sl@0
   213
CBufferedDecryptor::CBufferedDecryptor()
sl@0
   214
	{
sl@0
   215
	}
sl@0
   216
sl@0
   217
void CBufferedDecryptor::ProcessFinalL(const TDesC8& aInput, TDes8& aOutput)
sl@0
   218
	{
sl@0
   219
	__ASSERT_DEBUG(aOutput.MaxLength() >= MaxFinalOutputLength(aInput.Length()), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
sl@0
   220
sl@0
   221
	assert((aInput.Size() + iInputStore.Size()) % iPadding->BlockSize()==0);
sl@0
   222
	assert(aInput.Size() + iInputStore.Size() !=0 );
sl@0
   223
	assert(iPadding->BlockSize() % BlockSize() == 0);
sl@0
   224
	//1) Decrypt into aOutput up till the last full _padding_ blocksize
sl@0
   225
	//If this panics with descriptor problems, you've probably called
sl@0
   226
	//ProcessFinalL with a non-_padding_ blocksized aligned amount of data.
sl@0
   227
	TInt lenToDecrypt = aInput.Size() - iPadding->BlockSize();
sl@0
   228
	if(lenToDecrypt > 0)
sl@0
   229
		{
sl@0
   230
		Process(aInput.Left(lenToDecrypt), aOutput);
sl@0
   231
		assert(iInputStore.Size()==0);
sl@0
   232
		}
sl@0
   233
	else
sl@0
   234
		{
sl@0
   235
		lenToDecrypt = 0;
sl@0
   236
		}
sl@0
   237
	
sl@0
   238
	//2) Decrypt the last _padding_ blocksize into a new buffer
sl@0
   239
	HBufC8* padBuf = HBufC8::NewLC(iPadding->BlockSize());
sl@0
   240
	TPtr8 padPtr = padBuf->Des(); 
sl@0
   241
	Process(aInput.Mid(lenToDecrypt), padPtr);
sl@0
   242
	assert(iInputStore.Size()==0);
sl@0
   243
		
sl@0
   244
	//3) Unpad that last _padding_ blocksize into aOutput
sl@0
   245
	// Note that padding systems must always, like everything else in crypto,
sl@0
   246
	// _append_ data.
sl@0
   247
	iPadding->UnPadL(padPtr, aOutput);
sl@0
   248
sl@0
   249
	CleanupStack::PopAndDestroy(padBuf);
sl@0
   250
	}
sl@0
   251
sl@0
   252
TInt CBufferedDecryptor::MaxFinalOutputLength(TInt aInputLength) const
sl@0
   253
	{
sl@0
   254
	return iPadding->MaxUnPaddedLength(aInputLength + iInputStore.Size());
sl@0
   255
	}
sl@0
   256
sl@0
   257
void CBufferedDecryptor::ConstructL(CBlockTransformation* aBT, CPadding* aPadding) 
sl@0
   258
	{
sl@0
   259
	CBufferedTransformation::ConstructL(aBT, aPadding);
sl@0
   260
	}