1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcryptospi/source/padding/pkcs7.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,91 @@
1.4 +/*
1.5 +* Copyright (c) 2003-2009 Nokia Corporation and/or its subsidiary(-ies).
1.6 +* All rights reserved.
1.7 +* This component and the accompanying materials are made available
1.8 +* under the terms of the License "Eclipse Public License v1.0"
1.9 +* which accompanies this distribution, and is available
1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.11 +*
1.12 +* Initial Contributors:
1.13 +* Nokia Corporation - initial contribution.
1.14 +*
1.15 +* Contributors:
1.16 +*
1.17 +* Description:
1.18 +*
1.19 +*/
1.20 +
1.21 +
1.22 +#include <e32base.h>
1.23 +#include <padding.h>
1.24 +#include <securityerr.h>
1.25 +#include <cryptopanic.h>
1.26 +
1.27 +#include "paddingshim.h"
1.28 +
1.29 +/* CPaddingPKCS7 */
1.30 +EXPORT_C CPaddingPKCS7* CPaddingPKCS7::NewL(TInt aBlockBytes)
1.31 + {
1.32 + __ASSERT_ALWAYS(aBlockBytes > 0, User::Leave(KErrArgument));
1.33 + return CPaddingPKCS7Shim::NewL(aBlockBytes);
1.34 + }
1.35 +
1.36 +EXPORT_C CPaddingPKCS7* CPaddingPKCS7::NewLC(TInt aBlockBytes)
1.37 + {
1.38 + CPaddingPKCS7* self = CPaddingPKCS7::NewL(aBlockBytes);
1.39 + CleanupStack::PushL(self);
1.40 + return self;
1.41 + }
1.42 +
1.43 +EXPORT_C CPaddingPKCS7::CPaddingPKCS7(TInt aBlockBytes):CPadding(aBlockBytes)
1.44 + {
1.45 + }
1.46 +
1.47 +void CPaddingPKCS7::DoPadL(const TDesC8& aInput,TDes8& aOutput)
1.48 + {
1.49 + TInt paddingBytes = BlockSize()-(aInput.Length()%BlockSize());
1.50 + aOutput.Append(aInput);
1.51 + aOutput.SetLength(aOutput.Length()+paddingBytes);
1.52 + for (TInt i=1;i<=paddingBytes;i++)
1.53 + {
1.54 + aOutput[aOutput.Length()-i]=(TUint8)(paddingBytes);
1.55 + }
1.56 + }
1.57 +
1.58 +void CPaddingPKCS7::UnPadL(const TDesC8& aInput,TDes8& aOutput)
1.59 + {
1.60 + TUint inputLen = aInput.Length();
1.61 + TUint paddingLen = (TUint)(aInput[inputLen - 1]);
1.62 +
1.63 + if (paddingLen > inputLen)
1.64 + {
1.65 + User::Leave(KErrInvalidPadding);
1.66 + }
1.67 +
1.68 + TInt outlen = aInput.Length() - paddingLen;
1.69 +
1.70 + __ASSERT_DEBUG(aOutput.MaxLength() >= outlen, User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
1.71 +
1.72 + aOutput.Append(aInput.Left(outlen));
1.73 + for (TInt i=outlen;i<inputLen;i++)
1.74 + {
1.75 + //All padding bytes must equal the length of padding expected
1.76 + if (aInput[i]!=paddingLen)
1.77 + {
1.78 + User::Leave(KErrInvalidPadding);
1.79 + }
1.80 + }
1.81 + }
1.82 +
1.83 +TInt CPaddingPKCS7::MinPaddingLength(void) const
1.84 + {
1.85 + //if aInputBytes is 1 less than the blocksize then we get 1 byte of padding
1.86 + return 1;
1.87 + }
1.88 +
1.89 +TInt CPaddingPKCS7::MaxPaddedLength(TInt aInputBytes) const
1.90 + {
1.91 + TUint padBytes = BlockSize() - (aInputBytes % BlockSize());
1.92 + return padBytes + aInputBytes;
1.93 + }
1.94 +