os/security/crypto/weakcryptospi/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 #include "paddingshim.h"
    25 
    26 /* CPaddingPKCS7 */
    27 EXPORT_C CPaddingPKCS7* CPaddingPKCS7::NewL(TInt aBlockBytes)
    28 	{
    29 	__ASSERT_ALWAYS(aBlockBytes > 0, User::Leave(KErrArgument));
    30 	return CPaddingPKCS7Shim::NewL(aBlockBytes);
    31 	}
    32 
    33 EXPORT_C CPaddingPKCS7* CPaddingPKCS7::NewLC(TInt aBlockBytes)
    34 	{
    35 	CPaddingPKCS7* self = CPaddingPKCS7::NewL(aBlockBytes);
    36 	CleanupStack::PushL(self);
    37 	return self;
    38 	}
    39 
    40 EXPORT_C CPaddingPKCS7::CPaddingPKCS7(TInt aBlockBytes):CPadding(aBlockBytes)
    41 	{
    42 	}
    43 
    44 void CPaddingPKCS7::DoPadL(const TDesC8& aInput,TDes8& aOutput)
    45 	{
    46 	TInt paddingBytes = BlockSize()-(aInput.Length()%BlockSize());		
    47 	aOutput.Append(aInput);
    48 	aOutput.SetLength(aOutput.Length()+paddingBytes);
    49 	for (TInt i=1;i<=paddingBytes;i++)
    50 		{
    51 		aOutput[aOutput.Length()-i]=(TUint8)(paddingBytes);
    52 		}
    53 	}
    54 
    55 void CPaddingPKCS7::UnPadL(const TDesC8& aInput,TDes8& aOutput)
    56 	{
    57 	TUint inputLen = aInput.Length();
    58  	TUint paddingLen = (TUint)(aInput[inputLen - 1]);
    59 
    60 	if (paddingLen > inputLen)
    61  		{
    62  		User::Leave(KErrInvalidPadding);
    63  		}
    64    
    65 	TInt outlen = aInput.Length() - paddingLen;
    66 
    67 	__ASSERT_DEBUG(aOutput.MaxLength() >= outlen, User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
    68 
    69 	aOutput.Append(aInput.Left(outlen));
    70 	for (TInt i=outlen;i<inputLen;i++)
    71 		{
    72 		//All padding bytes must equal the length of padding expected
    73 		if (aInput[i]!=paddingLen)
    74 			{
    75 			User::Leave(KErrInvalidPadding);
    76 			}
    77 		}
    78 	}
    79 
    80 TInt CPaddingPKCS7::MinPaddingLength(void) const
    81 	{
    82 	//if aInputBytes is 1 less than the blocksize then we get 1 byte of padding
    83 	return 1;
    84 	}
    85 
    86 TInt CPaddingPKCS7::MaxPaddedLength(TInt aInputBytes) const
    87 	{
    88 	TUint padBytes = BlockSize() - (aInputBytes % BlockSize());
    89 	return padBytes + aInputBytes;
    90 	}
    91