First public contribution.
2 * Copyright (c) 2002-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.
15 * ** IMPORTANT ** API's in this file are published to 3rd party developers via the
16 * Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted.
33 * Abstract base class defining the interface to padding schemes.
35 * It is designed to be used by both symmetric and asymmetric ciphers.
38 class CPadding : public CBase
42 * Pads aInput to be BlockSize() bytes long and places the result in aOutput.
44 * @param aInput Data to be padded. The size must be less than or equal to
45 * BlockSize() minus MinPaddingLength().
46 * @param aOutput On return, the resulting padded, block size aligned data
47 * appended to aOutput.
49 IMPORT_C void PadL(const TDesC8& aInput,TDes8& aOutput);
52 * Pads aInput to be BlockSize() long and places the result in aOutput.
54 * Deriving implementations of DoPadL() can assume that aInput is less than or
55 * equal to BlockSize() minus MinPaddingLength().
57 * @param aInput Data to be padded.
58 * @param aOutput On return, the resulting padded, aBlockBytes aligned data
59 * appended to aOutput.
61 virtual void DoPadL(const TDesC8& aInput,TDes8& aOutput) = 0;
64 * Removes padding from aInput and appends unpadded result to aOutput.
66 * @param aInput Data to be unpadded.
67 * @param aOutput On return, the unpadded data.
69 virtual void UnPadL(const TDesC8& aInput,TDes8& aOutput) = 0;
72 * Sets the block size for this padding system.
74 * @param aBlockBytes The block size in bytes.
76 IMPORT_C void SetBlockSize(TInt aBlockBytes);
79 * Retrieves the block size for this padding system.
81 * @return The block size in bytes.
83 IMPORT_C TInt BlockSize(void) const;
86 * Gets the smallest number of bytes that PadL() will ever add to aInput in
87 * order to get a valid block aligned aOutput.
89 * For example, in SSLv3 padding, if the block size is 8 and aInput is 7 bytes,
90 * it will append 1 byte of padding. For SSLv3 padding, this is the smallest
91 * amount possible as an 8 byte input will add another block size (8 more bytes)
94 * @return A TInt containing the smallest number of padding bytes possible.
96 virtual TInt MinPaddingLength(void) const = 0;
99 * Gets the size of the aOutput buffer, in a call to PadL(), must be in
100 * order to accommodate a block size of BlockSize() and an input size of
103 * @note By default, this function returns the output of BlockSize(). If
104 * a derived padding system outputs more than a single block of padding,
105 * one must override this function and return the appropriate value.
107 * @param aInputBytes The amount of data to be padded out in bytes.
108 * @return A TInt representing the maximum amount of padded output data
109 * (in bytes) for a given block and input size.
111 IMPORT_C virtual TInt MaxPaddedLength(TInt aInputBytes) const;
114 * Gets the size of the aOutput buffer, in a call to UnPadL(), must be in
115 * order to accommodate an input size of aInputBytes.
117 * @note By default, this function returns the value of aInputBytes minus MinPaddingBytes().
118 * Most padding systems cannot determine anything about the unpadded length
119 * without looking at the data. If your padding system allows you to give a
120 * better bound, then you should reimplement this function.
122 * @param aInputBytes The amount of data to be unpadded in bytes.
123 * @return A TInt containing the maximum amount of unpadded output data
124 * (in bytes) for a given padded input.
126 IMPORT_C virtual TInt MaxUnPaddedLength(TInt aInputBytes) const;
131 * @param aBlockBytes The block size in bytes.
133 IMPORT_C CPadding(TInt aBlockBytes);
136 CPadding(const CPadding&);
137 CPadding& operator=(const CPadding&);
143 * This concrete subclass of CPadding appends no padding.
145 * aOutput will be a copy of aInput after any call to PadL() or UnPadL().
148 class CPaddingNone:public CPadding
152 * Creates a new CPaddingNone object.
154 * @param aBlockBytes The block size in bytes.
155 * @return A pointer to the new CPaddingNone object.
157 IMPORT_C static CPaddingNone* NewL(TInt aBlockBytes=KMaxTInt);
160 * Creates a new CPaddingNone object and leaves a pointer to it on the cleanup stack.
162 * @param aBlockBytes The block size in bytes.
163 * @return A pointer to the new CPaddingNone object.
165 IMPORT_C static CPaddingNone* NewLC(TInt aBlockBytes=KMaxTInt);
166 void DoPadL(const TDesC8& aInput,TDes8& aOutput);
167 void UnPadL(const TDesC8& aInput,TDes8& aOutput);
168 TInt MinPaddingLength(void) const;
169 TInt MaxPaddedLength(TInt aInputBytes) const;
174 * @param aBlockBytes The block size in bytes.
176 IMPORT_C CPaddingNone(TInt aBlockBytes);
179 CPaddingNone(const CPaddingNone&);
180 CPaddingNone& operator=(const CPaddingNone&);
184 * This concrete subclass of CPadding implements PKCS#1 v1.5 signature padding.
186 * It is intended for use with RSA signing/verifying.
189 class CPaddingPKCS1Signature : public CPadding
193 * Creates a new CPaddingPKCS1Signature object.
195 * @param aBlockBytes The block size in bytes.
196 * @return A pointer to the new CPaddingPKCS1Signature object.
198 IMPORT_C static CPaddingPKCS1Signature* NewL(TInt aBlockBytes);
201 * Creates a new CPaddingPKCS1Signature object and leaves a pointer to it on the
204 * @param aBlockBytes The block size in bytes.
205 * @return A pointer to the new CPaddingPKCS1Signature object.
207 IMPORT_C static CPaddingPKCS1Signature* NewLC(
209 void DoPadL(const TDesC8& aInput,TDes8& aOutput);
210 void UnPadL(const TDesC8& aInput,TDes8& aOutput);
211 TInt MinPaddingLength(void) const;
216 * @param aBlockBytes The block size in bytes.
218 IMPORT_C CPaddingPKCS1Signature(TInt aBlockBytes);
220 CPaddingPKCS1Signature(void);
221 CPaddingPKCS1Signature(const CPaddingPKCS1Signature&);
222 CPaddingPKCS1Signature& operator=(const CPaddingPKCS1Signature&);
226 * This concrete subclass of CPadding implements PKCS#1 v1.5 encryption padding.
227 * It is intended for use with RSA encryption/decryption.
230 class CPaddingPKCS1Encryption : public CPadding
234 * Creates a new CPaddingPKCS1Encryption object.
236 * @param aBlockBytes The block size in bytes.
237 * @return A pointer to the new CPaddingPKCS1Encryption object.
239 IMPORT_C static CPaddingPKCS1Encryption* NewL(TInt aBlockBytes);
242 * Creates a new CPaddingPKCS1Encryption object and leaves a pointer to it on the
245 * @param aBlockBytes The block size in bytes.
246 * @return A pointer to the new CPaddingPKCS1Encryption object.
248 IMPORT_C static CPaddingPKCS1Encryption* NewLC(TInt aBlockBytes);
249 void DoPadL(const TDesC8& aInput,TDes8& aOutput);
250 void UnPadL(const TDesC8& aInput,TDes8& aOutput);
251 TInt MinPaddingLength(void) const;
256 * @param aBlockBytes The block size in bytes.
258 IMPORT_C CPaddingPKCS1Encryption(TInt aBlockBytes);
260 CPaddingPKCS1Encryption(void);
261 CPaddingPKCS1Encryption(const CPaddingPKCS1Encryption&);
262 CPaddingPKCS1Encryption& operator=(const CPaddingPKCS1Encryption&);
266 * This concrete subclass of CPadding implements padding according to
267 * the SSLv3/TLS standard.
269 * The SSL 3.0 spec does not specifiy the padding bytes to be used - it is
270 * assumed to be arbitrary (and the openssl implementation uses non-zero random
271 * data). The TLS spec however states that padding bytes should be the length
272 * of the padding - 1. This class implements the latter when padding, but does
273 * not check the padding byes when unpadding, so as to be interoperable with SSL
277 class CPaddingSSLv3 : public CPadding
281 * Creates a new CPaddingSSLv3 object.
283 * @param aBlockBytes The block size in bytes.
284 * @return A pointer to the new CPaddingSSLv3 object.
286 IMPORT_C static CPaddingSSLv3* NewL(TInt aBlockBytes);
289 * Creates a new CPaddingSSLv3 object and leaves a pointer to it on the cleanup stack.
291 * @param aBlockBytes The block size in bytes.
292 * @return A pointer to the new CPaddingSSLv3 object.
294 IMPORT_C static CPaddingSSLv3* NewLC(TInt aBlockBytes);
295 void DoPadL(const TDesC8& aInput,TDes8& aOutput);
296 void UnPadL(const TDesC8& aInput,TDes8& aOutput);
297 TInt MinPaddingLength(void) const;
298 TInt MaxPaddedLength(TInt aInputBytes) const;
304 * @param aBlockBytes The block size in bytes.
306 IMPORT_C CPaddingSSLv3(TInt aBlockBytes);
309 CPaddingSSLv3(const CPaddingSSLv3&);
310 CPaddingSSLv3& operator=(const CPaddingSSLv3&);
314 * This concrete subclass of CPadding implements padding according to
315 * the PKCS#7/TLS standard.
318 class CPaddingPKCS7 : public CPadding
322 * Creates a new CPaddingPKCS7 object.
324 * @param aBlockBytes The block size in bytes.
325 * @return A pointer to the new CPaddingPKCS7 object.
327 IMPORT_C static CPaddingPKCS7* NewL(TInt aBlockBytes);
330 * Creates a new CPaddingPKCS7 object and leaves a pointer to it on the cleanup stack.
332 * @param aBlockBytes The block size in bytes.
333 * @return A pointer to the new CPaddingPKCS7 object.
335 IMPORT_C static CPaddingPKCS7* NewLC(TInt aBlockBytes);
336 void DoPadL(const TDesC8& aInput,TDes8& aOutput);
337 void UnPadL(const TDesC8& aInput,TDes8& aOutput);
338 TInt MinPaddingLength(void) const;
339 TInt MaxPaddedLength(TInt aInputBytes) const;
345 * @param aBlockBytes The block size in bytes.
347 IMPORT_C CPaddingPKCS7(TInt aBlockBytes);
350 CPaddingPKCS7(const CPaddingPKCS7&);
351 CPaddingPKCS7& operator=(const CPaddingPKCS7&);