os/security/crypto/weakcrypto/source/padding/pkcs7.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2003-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 <e32base.h>
    20 #include <padding.h>
    21 #include <securityerr.h>
    22 #include <cryptopanic.h>
    23 
    24 /* CPaddingPKCS7 */
    25 EXPORT_C CPaddingPKCS7* CPaddingPKCS7::NewL(TInt aBlockBytes)
    26 	{
    27 	__ASSERT_ALWAYS(aBlockBytes > 0, User::Leave(KErrArgument));
    28 	return new(ELeave)CPaddingPKCS7(aBlockBytes);
    29 	}
    30 
    31 EXPORT_C CPaddingPKCS7* CPaddingPKCS7::NewLC(TInt aBlockBytes)
    32 	{
    33 	CPaddingPKCS7* self = CPaddingPKCS7::NewL(aBlockBytes);
    34 	CleanupStack::PushL(self);
    35 	return self;
    36 	}
    37 
    38 EXPORT_C CPaddingPKCS7::CPaddingPKCS7(TInt aBlockBytes):CPadding(aBlockBytes)
    39 	{
    40 	}
    41 
    42 void CPaddingPKCS7::DoPadL(const TDesC8& aInput,TDes8& aOutput)
    43 	{
    44 	TInt paddingBytes = BlockSize()-(aInput.Length()%BlockSize());		
    45 	aOutput.Append(aInput);
    46 	aOutput.SetLength(aOutput.Length()+paddingBytes);
    47 	for (TInt i=1;i<=paddingBytes;i++)
    48 		{
    49 		aOutput[aOutput.Length()-i]=(TUint8)(paddingBytes);
    50 		}
    51 	}
    52 
    53 void CPaddingPKCS7::UnPadL(const TDesC8& aInput,TDes8& aOutput)
    54 	{
    55 	TUint inputLen = aInput.Length();
    56  	TUint paddingLen = (TUint)(aInput[inputLen - 1]);
    57 
    58 	if (paddingLen > inputLen)
    59  		{
    60  		User::Leave(KErrInvalidPadding);
    61  		}
    62    
    63 	TInt outlen = aInput.Length() - paddingLen;
    64 
    65 	__ASSERT_DEBUG(aOutput.MaxLength() >= outlen, User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
    66 
    67 	aOutput.Append(aInput.Left(outlen));
    68 	for (TInt i=outlen;i<inputLen;i++)
    69 		{
    70 		//All padding bytes must equal the length of padding expected
    71 		if (aInput[i]!=paddingLen)
    72 			{
    73 			User::Leave(KErrInvalidPadding);
    74 			}
    75 		}
    76 	}
    77 
    78 TInt CPaddingPKCS7::MinPaddingLength(void) const
    79 	{
    80 	//if aInputBytes is 1 less than the blocksize then we get 1 byte of padding
    81 	return 1;
    82 	}
    83 
    84 TInt CPaddingPKCS7::MaxPaddedLength(TInt aInputBytes) const
    85 	{
    86 	TUint padBytes = BlockSize() - (aInputBytes % BlockSize());
    87 	return padBytes + aInputBytes;
    88 	}
    89