os/security/crypto/weakcrypto/source/pbe/pbencryptorset.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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 <e32std.h>
    20 #include <random.h>
    21 
    22 #include "pbencryptor.h"
    23 #include "pbesymmetricfactory.h"
    24 
    25 #ifdef SYMBIAN_ENABLE_SPLIT_HEADERS
    26 /** The maximum block size supported (in bytes) */
    27 const TUint KMaxBlockSizeSupported = 32;
    28 #endif
    29 
    30 EXPORT_C CPBEncryptorSet* CPBEncryptorSet::NewL(
    31 	const TPBECipher aCipher, const TDesC8& aKey) 
    32 	{
    33 	CPBEncryptorSet* self = NewLC(aCipher, aKey);
    34 	CleanupStack::Pop();
    35 	return self;
    36 	}
    37 
    38 EXPORT_C CPBEncryptorSet* CPBEncryptorSet::NewLC(
    39 	const TPBECipher aCipher, const TDesC8& aKey)
    40 	{
    41 	CPBEncryptorSet* self = new(ELeave) CPBEncryptorSet;
    42 	CleanupStack::PushL(self);
    43 
    44 	self->ConstructL(aCipher, aKey);
    45 	
    46 	return self;
    47 	}
    48 
    49 void CPBEncryptorSet::ConstructL(const TPBECipher aCipher,const TDesC8& aKey)
    50 	{
    51 	TInt blocksize = PBE::GetBlockBytes(aCipher);
    52 	iIV = HBufC8::NewMaxL(blocksize); 
    53 	TPtr8 iv = iIV->Des();
    54 	TRandom::RandomL(iv);
    55 
    56 	iCipher = PBE::MakeEncryptorL(aCipher, aKey, iv);
    57 	}
    58 
    59 void CPBEncryptorSet::Process(const TDesC8& aInput, TDes8& aOutput)
    60 	{
    61 	if(!iIVSent)
    62 		{
    63 		aOutput.Append(*iIV);
    64 		iIVSent = ETrue;
    65 		}
    66 	iCipher->Process(aInput, aOutput);
    67 	}
    68 
    69 void CPBEncryptorSet::ProcessFinalL(const TDesC8& aInput, 
    70 	TDes8& aOutput)
    71 	{
    72 	if(!iIVSent)
    73 		{
    74 		aOutput.Append(*iIV);
    75 		iIVSent = ETrue;
    76 		}
    77 	
    78 	iCipher->ProcessFinalL(aInput, aOutput);
    79 	}
    80 
    81 
    82 CPBEncryptorSet::CPBEncryptorSet() : iIVSent(EFalse)
    83 	{
    84 	}
    85 
    86 CPBEncryptorSet::~CPBEncryptorSet()
    87 	{
    88 	delete iIV;
    89 	delete iCipher;	//	Don't delete iCBCDecryptor, this belongs to iCipher
    90 	}
    91 
    92 TInt CPBEncryptorSet::MaxOutputLength(TUint aMaxInputLength) const
    93     {
    94 	if(iIVSent)
    95 		{
    96 		return (iCipher->MaxOutputLength(aMaxInputLength));
    97 		}
    98 	else 
    99 		{//	If we've not sent the iv yet then its the length 
   100 		//	from the cipher plus a blocksize for the iv
   101 		return (iCipher->MaxOutputLength(aMaxInputLength + iCipher->BlockSize()));
   102 		}
   103     }
   104 
   105 TInt CPBEncryptorSet::MaxFinalOutputLength(TUint aMaxInputLength) const
   106     {
   107 	if(iIVSent)
   108 		{
   109 		return (iCipher->MaxFinalOutputLength(aMaxInputLength));
   110 		}
   111 	else 
   112 		{//	If we've not sent the iv yet then its the length 
   113 		//	from the cipher plus a blocksize for the iv
   114 		return (iCipher->MaxFinalOutputLength(aMaxInputLength + iCipher->BlockSize()));
   115 		}
   116     }
   117 
   118 EXPORT_C CPBDecryptorSet* CPBDecryptorSet::NewL(const TPBECipher aCipher, 
   119 	const TDesC8& aKey)
   120 	{
   121 	CPBDecryptorSet* self = NewLC(aCipher, aKey);
   122 	CleanupStack::Pop();
   123 	return self;
   124 	}
   125 
   126 EXPORT_C CPBDecryptorSet* CPBDecryptorSet::NewLC(const TPBECipher aCipher,
   127 	const TDesC8& aKey)
   128 	{
   129 	CPBDecryptorSet* self = new(ELeave) CPBDecryptorSet;
   130 	CleanupStack::PushL(self);
   131 	TBuf8<KMaxBlockSizeSupported> fakeIV;
   132 	fakeIV.SetLength(PBE::GetBlockBytes(aCipher));
   133 	self->ConstructL(aCipher, aKey, fakeIV);
   134 	return self;
   135 	}
   136 
   137 void CPBDecryptorSet::ConstructL(const TPBECipher aCipher, 
   138 	const TDesC8& aKey, const TDesC8& aIV)
   139 	{
   140 	iCipher = PBE::MakeDecryptorL(aCipher, aKey, aIV);
   141 	iIVBuf = HBufC8::NewL(PBE::GetBlockBytes(aCipher));
   142 	}
   143 
   144 void CPBDecryptorSet::Process(const TDesC8& aInput, TDes8& aOutput)
   145 	{
   146 	TPtrC8 input = ProcessIV(aInput);
   147 	iCipher->Process(input, aOutput);
   148 	}
   149 
   150 void CPBDecryptorSet::ProcessFinalL(const TDesC8& aInput, TDes8& aOutput)
   151 	{
   152 	TPtrC8 input = ProcessIV(aInput);
   153 	iCipher->ProcessFinalL(input, aOutput);
   154 	}
   155 
   156 TPtrC8 CPBDecryptorSet::ProcessIV(const TDesC8& aInput)
   157 	{
   158 	if(!iIVSent)
   159 		{
   160 		TPtr8 iIVBufPtr(iIVBuf->Des());
   161 		if(aInput.Length() + iIVBufPtr.Length() < iCipher->BlockSize())
   162 			{
   163 			iIVBufPtr.Append(aInput);
   164 			return TPtrC8();
   165 			}
   166 		else 
   167 			{
   168 			TInt rem = iCipher->BlockSize() - iIVBufPtr.Length();
   169 			iIVBufPtr.Append(aInput.Mid(0, rem));
   170 			CBufferedDecryptor* blockDecryptor = static_cast<CBufferedDecryptor*>(iCipher);
   171 			static_cast<CModeCBCDecryptor*>(blockDecryptor->BlockTransformer())->SetIV(iIVBufPtr);
   172 			iIVSent = ETrue;
   173 			return aInput.Mid(rem);
   174 			}
   175 		}
   176 	else 
   177 		{
   178 		return aInput;
   179 		}
   180 	}
   181 
   182 CPBDecryptorSet::CPBDecryptorSet() : iIVSent(EFalse)
   183 	{
   184 	}
   185 
   186 CPBDecryptorSet::~CPBDecryptorSet()
   187 	{
   188 	delete iCipher;
   189 	//iCipher owns iCBCDecryptor we _don't_ need to delete it
   190 	delete iIVBuf;
   191 	}
   192 
   193 TInt CPBDecryptorSet::MaxOutputLength(TUint aMaxInputLength) const
   194     {
   195 	if(iIVSent)
   196 		{
   197 		return (iCipher->MaxOutputLength(aMaxInputLength));
   198 		}
   199 	else	//	if we haven't sent the iv yet, then the length the max length of the
   200 		{	//	cipher minus a BlockSize where the IV was sitting (as the iv would be part
   201 			//	of the length they've sent us)
   202 		return (iCipher->MaxOutputLength(aMaxInputLength - iCipher->BlockSize()));
   203 		}
   204     }
   205 
   206 TInt CPBDecryptorSet::MaxFinalOutputLength(TUint aMaxInputLength) const
   207     {
   208 	if(iIVSent)
   209 		{
   210 		return (iCipher->MaxFinalOutputLength(aMaxInputLength));
   211 		}
   212 	else	//	if we haven't sent the iv yet, then the length the max length of the
   213 		{	//	cipher minus a BlockSize where the IV was sitting (as the iv would be part
   214 			//	of the length they've sent us)
   215 		return (iCipher->MaxFinalOutputLength(aMaxInputLength - iCipher->BlockSize()));
   216 		}
   217     }