Update contrib.
2 * Copyright (c) 1999-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
22 #include <securityerr.h>
23 #include <cryptopanic.h>
25 #include "paddingshim.h"
28 CPadding::CPadding(void) : iBlockBytes(-1)
32 EXPORT_C CPadding::CPadding(TInt aBlockBytes) : iBlockBytes(aBlockBytes)
34 __ASSERT_ALWAYS(aBlockBytes > 0, User::Invariant());
37 EXPORT_C void CPadding::SetBlockSize(TInt aBlockBytes)
39 __ASSERT_ALWAYS(aBlockBytes > 0, User::Invariant());
40 iBlockBytes = aBlockBytes;
43 EXPORT_C TInt CPadding::BlockSize(void) const
48 EXPORT_C TInt CPadding::MaxPaddedLength(TInt /*aInputBytes*/) const
53 EXPORT_C TInt CPadding::MaxUnPaddedLength(TInt aInputBytes) const
55 return aInputBytes - MinPaddingLength();
58 EXPORT_C void CPadding::PadL(const TDesC8& aInput, TDes8& aOutput)
60 // Check that the input is small enough to fit inside one padded block
61 // Won't leave if input text is equal to blocksize. Let DoPadL handle such situations
62 if(aInput.Length() > BlockSize() - MinPaddingLength()
63 && aInput.Length() != BlockSize())
64 User::Leave(KErrArgument);
66 // Check that the output descriptor supplied is large enough to store the result
67 if(aOutput.MaxLength() < MaxPaddedLength(aInput.Length()))
68 User::Leave(KErrOverflow);
70 // Call the virtual function, implemented by derived classes
71 DoPadL(aInput, aOutput);
74 TInt CPadding::GetExtension(TUint aExtensionId, TAny*& a0, TAny* a1)
76 return Extension_(aExtensionId, a0, a1);
80 EXPORT_C CPaddingNone* CPaddingNone::NewL(TInt aBlockBytes)
82 __ASSERT_ALWAYS(aBlockBytes > 0, User::Leave(KErrArgument));
83 return CPaddingNoneShim::NewL(aBlockBytes);
86 EXPORT_C CPaddingNone* CPaddingNone::NewLC(TInt aBlockBytes)
88 CPaddingNone* self = CPaddingNone::NewL(aBlockBytes);
89 CleanupStack::PushL(self);
93 EXPORT_C CPaddingNone::CPaddingNone(TInt aBlockBytes):CPadding(aBlockBytes)
97 void CPaddingNone::DoPadL(const TDesC8& aInput,TDes8& aOutput)
99 aOutput.Append(aInput);
102 void CPaddingNone::UnPadL(const TDesC8& aInput,TDes8& aOutput)
104 __ASSERT_DEBUG(aOutput.MaxLength() >= MaxPaddedLength(aInput.Length()), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
105 aOutput.Append(aInput);
108 TInt CPaddingNone::MinPaddingLength(void) const
113 TInt CPaddingNone::MaxPaddedLength(TInt aInputSize) const
119 EXPORT_C CPaddingSSLv3* CPaddingSSLv3::NewL(TInt aBlockBytes)
121 __ASSERT_ALWAYS(aBlockBytes > 0, User::Leave(KErrArgument));
122 return CPaddingSSLv3Shim::NewL(aBlockBytes);
125 EXPORT_C CPaddingSSLv3* CPaddingSSLv3::NewLC(TInt aBlockBytes)
127 CPaddingSSLv3* self = CPaddingSSLv3::NewL(aBlockBytes);
128 CleanupStack::PushL(self);
132 EXPORT_C CPaddingSSLv3::CPaddingSSLv3(TInt aBlockBytes):CPadding(aBlockBytes)
136 void CPaddingSSLv3::DoPadL(const TDesC8& aInput,TDes8& aOutput)
138 TInt paddingBytes=BlockSize()-(aInput.Length()%BlockSize());
139 aOutput.Append(aInput);
140 aOutput.SetLength(aOutput.Length()+paddingBytes);
141 for (TInt i=1;i<=paddingBytes;i++)
143 aOutput[aOutput.Length()-i]=(TUint8)(paddingBytes-1);
147 void CPaddingSSLv3::UnPadL(const TDesC8& aInput,TDes8& aOutput)
149 TInt paddingLen = aInput[aInput.Length()-1] + 1;
151 if (paddingLen > aInput.Length())
153 User::Leave(KErrInvalidPadding);
156 TInt outlen = aInput.Length() - paddingLen;
158 __ASSERT_DEBUG(aOutput.MaxLength() >= outlen, User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
160 aOutput.Append(aInput.Left(outlen));
163 TInt CPaddingSSLv3::MinPaddingLength(void) const
165 //if aInputBytes is 1 less than the blocksize then we get 1 byte of padding
169 TInt CPaddingSSLv3::MaxPaddedLength(TInt aInputBytes) const
171 TUint padBytes = BlockSize() - (aInputBytes % BlockSize());
172 return padBytes + aInputBytes;
175 /* CPaddingPKCS1Signature */
176 EXPORT_C CPaddingPKCS1Signature* CPaddingPKCS1Signature::NewL(TInt aBlockBytes)
178 return CPaddingPKCS1SignatureShim::NewL(aBlockBytes);
181 EXPORT_C CPaddingPKCS1Signature* CPaddingPKCS1Signature::NewLC(TInt aBlockBytes)
183 CPaddingPKCS1Signature* self = CPaddingPKCS1Signature::NewL(aBlockBytes);
184 CleanupStack::PushL(self);
188 EXPORT_C CPaddingPKCS1Signature::CPaddingPKCS1Signature(TInt aBlockBytes)
189 : CPadding(aBlockBytes)
193 void CPaddingPKCS1Signature::DoPadL(const TDesC8& aInput,TDes8& aOutput)
195 aOutput.SetLength(BlockSize());
199 TInt startOfData=BlockSize()-aInput.Length();
200 // PKCS1 also specifies a block type 0 for private key operations but
201 // does not recommend its use. This block type (0) is compatible with
202 // unpadded data though so you can create PKCS1 type 0 blocks using
204 aOutput[1]=1; // Block type 1 (private key operation)
205 for (i=2;i<(startOfData-1);i++)
210 aOutput[startOfData-1]=0; // separator
211 for (i=startOfData;i<BlockSize();i++,j++)
213 aOutput[i]=aInput[j];
217 void CPaddingPKCS1Signature::UnPadL(const TDesC8& aInput,TDes8& aOutput)
219 // erm, oops, this is not quite as simplistic as it first looks...
220 // our integer class will strip any leading zeros so we might actually
221 // get some real data that starts out looking like padding but isn't
224 TInt inputLen = aInput.Length();
226 User::Leave(KErrInvalidPadding); // Invalid padding data
228 // Leading zero may have been stripped off by integer class
230 if (aInput[dataStart] == 0)
235 if (dataStart < inputLen && aInput[dataStart]) // might be mode one or mode zero,
238 while (dataStart < inputLen && aInput[dataStart] == 0xff)
243 if (dataStart == inputLen || aInput[dataStart]) // this would mean theres no zero between 0x01ff and data...so its not mode one
244 dataStart=0; // mode zero, start from begining of data
248 else // We've definitely got a mode zero
249 { // or broken data, assume mode zero
253 TInt len=inputLen-dataStart;
255 __ASSERT_DEBUG(aOutput.MaxLength() >= len, User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
257 aOutput.SetLength(len);
259 while (dataStart<inputLen)
261 aOutput[i++]=aInput[dataStart++];
265 TInt CPaddingPKCS1Signature::MinPaddingLength(void) const
267 return 11; //0x00, 0x01, <MIN of 8 0xFF octets> , 0x00
270 /* CPaddingPKCS1Encryption */
271 EXPORT_C CPaddingPKCS1Encryption* CPaddingPKCS1Encryption::NewL(
274 return CPaddingPKCS1EncryptionShim::NewL(aBlockBytes);
277 EXPORT_C CPaddingPKCS1Encryption* CPaddingPKCS1Encryption::NewLC(
280 CPaddingPKCS1Encryption* self = CPaddingPKCS1Encryption::NewL(aBlockBytes);
281 CleanupStack::PushL(self);
285 EXPORT_C CPaddingPKCS1Encryption::CPaddingPKCS1Encryption(TInt aBlockBytes)
286 : CPadding(aBlockBytes)
290 void CPaddingPKCS1Encryption::DoPadL(const TDesC8& aInput,TDes8& aOutput)
292 aOutput.SetLength(BlockSize());
295 TInt startOfData=BlockSize()-aInput.Length();
296 aOutput[1]=2; // Block type 2 (public key operation)
298 TRAPD(err, GenerateRandomBytesL(rnd));
299 if((err != KErrNone) && (err != KErrNotSecure))
304 for (; i<(startOfData-1);)
312 TRAP(err, GenerateRandomBytesL(rnd));
313 if((err != KErrNone) && (err != KErrNotSecure))
320 aOutput[startOfData-1]=0; // separator
321 for (i=startOfData;i<BlockSize();i++,j++)
323 aOutput[i]=aInput[j];
327 void CPaddingPKCS1Encryption::UnPadL(const TDesC8& aInput,TDes8& aOutput)
329 TInt inputLen = aInput.Length();
331 User::Leave(KErrInvalidPadding); // Invalid padding data
333 // Leading zero may have been stripped off by integer class
335 if (aInput[dataStart] == 0)
340 // expecting mode 2 padding, otherwise broken
341 if (dataStart == inputLen || aInput[dataStart] != 2)
343 User::Leave(KErrInvalidPadding);
347 // skip random non zero bytes
348 while (dataStart < inputLen && aInput[dataStart])
353 // expecting zero separator
354 if (dataStart == inputLen || aInput[dataStart] != 0)
356 User::Leave(KErrInvalidPadding);
360 TInt len = inputLen - dataStart;
361 __ASSERT_DEBUG(aOutput.MaxLength() >= len, User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow));
363 aOutput.SetLength(len);
365 while (dataStart<inputLen)
367 aOutput[i++]=aInput[dataStart++];
371 TInt CPaddingPKCS1Encryption::MinPaddingLength(void) const
373 return 11; //0x00, 0x02, <min of 8 random octets>, 0x00