1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/security/crypto/weakcrypto/source/padding/pkcs7.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,89 @@
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 +/* CPaddingPKCS7 */
1.28 +EXPORT_C CPaddingPKCS7* CPaddingPKCS7::NewL(TInt aBlockBytes)
1.29 + {
1.30 + __ASSERT_ALWAYS(aBlockBytes > 0, User::Leave(KErrArgument));
1.31 + return new(ELeave)CPaddingPKCS7(aBlockBytes);
1.32 + }
1.33 +
1.34 +EXPORT_C CPaddingPKCS7* CPaddingPKCS7::NewLC(TInt aBlockBytes)
1.35 + {
1.36 + CPaddingPKCS7* self = CPaddingPKCS7::NewL(aBlockBytes);
1.37 + CleanupStack::PushL(self);
1.38 + return self;
1.39 + }
1.40 +
1.41 +EXPORT_C CPaddingPKCS7::CPaddingPKCS7(TInt aBlockBytes):CPadding(aBlockBytes)
1.42 + {
1.43 + }
1.44 +
1.45 +void CPaddingPKCS7::DoPadL(const TDesC8& aInput,TDes8& aOutput)
1.46 + {
1.47 + TInt paddingBytes = BlockSize()-(aInput.Length()%BlockSize());
1.48 + aOutput.Append(aInput);
1.49 + aOutput.SetLength(aOutput.Length()+paddingBytes);
1.50 + for (TInt i=1;i<=paddingBytes;i++)
1.51 + {
1.52 + aOutput[aOutput.Length()-i]=(TUint8)(paddingBytes);
1.53 + }
1.54 + }
1.55 +
1.56 +void CPaddingPKCS7::UnPadL(const TDesC8& aInput,TDes8& aOutput)
1.57 + {
1.58 + TUint inputLen = aInput.Length();
1.59 + TUint paddingLen = (TUint)(aInput[inputLen - 1]);
1.60 +
1.61 + if (paddingLen > inputLen)
1.62 + {
1.63 + User::Leave(KErrInvalidPadding);
1.64 + }
1.65 +
1.66 + TInt outlen = aInput.Length() - paddingLen;
1.67 +
1.68 + __ASSERT_DEBUG(aOutput.MaxLength() >= outlen, User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
1.69 +
1.70 + aOutput.Append(aInput.Left(outlen));
1.71 + for (TInt i=outlen;i<inputLen;i++)
1.72 + {
1.73 + //All padding bytes must equal the length of padding expected
1.74 + if (aInput[i]!=paddingLen)
1.75 + {
1.76 + User::Leave(KErrInvalidPadding);
1.77 + }
1.78 + }
1.79 + }
1.80 +
1.81 +TInt CPaddingPKCS7::MinPaddingLength(void) const
1.82 + {
1.83 + //if aInputBytes is 1 less than the blocksize then we get 1 byte of padding
1.84 + return 1;
1.85 + }
1.86 +
1.87 +TInt CPaddingPKCS7::MaxPaddedLength(TInt aInputBytes) const
1.88 + {
1.89 + TUint padBytes = BlockSize() - (aInputBytes % BlockSize());
1.90 + return padBytes + aInputBytes;
1.91 + }
1.92 +