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