epoc32/include/padding.h
branchSymbian3
changeset 4 837f303aceeb
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/epoc32/include/padding.h	Wed Mar 31 12:33:34 2010 +0100
     1.3 @@ -0,0 +1,352 @@
     1.4 +/*
     1.5 +* Copyright (c) 2002-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 +* ** IMPORTANT **  API's in this file are published to 3rd party developers via the 
    1.19 +* Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted.
    1.20 +* Padding codes
    1.21 +*
    1.22 +*/
    1.23 +
    1.24 +
    1.25 +/**
    1.26 + @file 
    1.27 + @publishedAll
    1.28 + @released
    1.29 +*/
    1.30 + 
    1.31 +#ifndef __PADDING_H__
    1.32 +#define __PADDING_H__
    1.33 +
    1.34 +#include <random.h>
    1.35 +/** 
    1.36 +* Abstract base class defining the interface to padding schemes.
    1.37 +*
    1.38 +* It is designed to be used by both symmetric and asymmetric ciphers.
    1.39 +*
    1.40 +*/
    1.41 +class CPadding : public CBase
    1.42 +	{
    1.43 +public:
    1.44 +	/** 
    1.45 +	* Pads aInput to be BlockSize() bytes long and places the result in aOutput.  
    1.46 +	*
    1.47 +	* @param aInput		Data to be padded.  The size must be less than or equal to
    1.48 +	* 					BlockSize() minus MinPaddingLength().
    1.49 +	* @param aOutput	On return, the resulting padded, block size aligned data
    1.50 +	*					appended to aOutput.  
    1.51 +	*/
    1.52 +	IMPORT_C void PadL(const TDesC8& aInput,TDes8& aOutput);
    1.53 +
    1.54 +	
    1.55 +	/**
    1.56 +	* Removes padding from aInput and appends unpadded result to aOutput.
    1.57 +	* 
    1.58 +	* @param aInput		Data to be unpadded.
    1.59 +	* @param aOutput	On return, the unpadded data.
    1.60 +	*/
    1.61 +	virtual void UnPadL(const TDesC8& aInput,TDes8& aOutput) = 0;
    1.62 +
    1.63 +	/**
    1.64 +	* Sets the block size for this padding system.
    1.65 +	* 
    1.66 +	* @param aBlockBytes	The block size in bytes.
    1.67 +	*/
    1.68 +	IMPORT_C void SetBlockSize(TInt aBlockBytes);
    1.69 +
    1.70 +	/**
    1.71 +	* Retrieves the block size for this padding system.
    1.72 +	*
    1.73 +	* @return	The block size in bytes.
    1.74 +	*/
    1.75 +	IMPORT_C TInt BlockSize(void) const;
    1.76 +
    1.77 +	/**
    1.78 +	* Gets the smallest number of bytes that PadL() will ever add to aInput in
    1.79 +	* order to get a valid block aligned aOutput.  
    1.80 +	*
    1.81 +	* For example, in SSLv3 padding, if the block size is 8 and aInput is 7 bytes,
    1.82 +	* it will append 1 byte of padding. For SSLv3 padding, this is the smallest 
    1.83 +	* amount possible as an 8 byte input will add another block size (8 more bytes)
    1.84 +	* of padded data.
    1.85 +	* 
    1.86 +	* @return	A TInt containing the smallest number of padding bytes possible.
    1.87 +	*/
    1.88 +	virtual TInt MinPaddingLength(void) const = 0;
    1.89 +
    1.90 +	/**
    1.91 +	* Gets the size of the aOutput buffer, in a call to PadL(), must be in
    1.92 +	* order to accommodate a block size of BlockSize() and an input size of
    1.93 +	* aInputBytes.
    1.94 +	* 
    1.95 +	* @note	By default, this function returns the output of BlockSize().  If
    1.96 +	*		a derived padding system outputs more than a single block of padding,
    1.97 +	*		one must override this function and return the appropriate value.
    1.98 +	*
    1.99 +	* @param aInputBytes	The amount of data to be padded out in bytes.
   1.100 +	* @return				A TInt representing the maximum amount of padded output data
   1.101 +	*						(in bytes) for a given block and input size.
   1.102 +	*/
   1.103 +	IMPORT_C virtual TInt MaxPaddedLength(TInt aInputBytes) const;
   1.104 +
   1.105 +	/**
   1.106 +	* Gets the size of the aOutput buffer, in a call to UnPadL(), must be in
   1.107 +	* order to accommodate an input size of aInputBytes.
   1.108 +	*
   1.109 +	* @note	By default, this function returns the value of aInputBytes minus MinPaddingBytes().
   1.110 +	*		Most padding systems cannot determine anything about the unpadded length
   1.111 +	*		without looking at the data.  If your padding system allows you to give a
   1.112 +	*		better bound, then you should reimplement this function.
   1.113 +	*
   1.114 +	* @param aInputBytes	The amount of data to be unpadded in bytes.
   1.115 +	* @return				A TInt containing the maximum amount of unpadded output data
   1.116 +	*						(in bytes) for a given padded input.
   1.117 +	*/
   1.118 +	IMPORT_C virtual TInt MaxUnPaddedLength(TInt aInputBytes) const;
   1.119 +	
   1.120 +	/**
   1.121 +	@internalComponent
   1.122 +	Used to retrieve the extended interfaces by id. For Crypto
   1.123 +	SPI internal use only.
   1.124 +	*/		
   1.125 +	TInt GetExtension(TUint aExtensionId, TAny*& a0, TAny* a1);		
   1.126 +	
   1.127 +protected:
   1.128 +	/** 
   1.129 +	* Constructor
   1.130 +	* 
   1.131 +	* @param aBlockBytes	The block size in bytes.
   1.132 +	*/
   1.133 +	IMPORT_C CPadding(TInt aBlockBytes);
   1.134 +private:
   1.135 +	CPadding(void);
   1.136 +	CPadding(const CPadding&);
   1.137 +	CPadding& operator=(const CPadding&);
   1.138 +	virtual void DoPadL(const TDesC8& aInput,TDes8& aOutput) = 0;
   1.139 +private:
   1.140 +	TInt iBlockBytes;
   1.141 +	};
   1.142 +
   1.143 +/**
   1.144 +* This concrete subclass of CPadding appends no padding.
   1.145 +*
   1.146 +* aOutput will be a copy of aInput after any call to PadL() or UnPadL().
   1.147 +*
   1.148 +*/
   1.149 +class CPaddingNone:public CPadding
   1.150 +	{
   1.151 +public:
   1.152 +	/**
   1.153 +	* Creates a new CPaddingNone object.
   1.154 +	*
   1.155 +	* @param aBlockBytes	The block size in bytes.
   1.156 +	* @return				A pointer to the new CPaddingNone object.
   1.157 +	*/
   1.158 +	IMPORT_C static CPaddingNone* NewL(TInt aBlockBytes=KMaxTInt);
   1.159 +
   1.160 +	/**
   1.161 +	* Creates a new CPaddingNone object and leaves a pointer to it on the cleanup stack.
   1.162 +	* 
   1.163 +	* @param aBlockBytes	The block size in bytes.
   1.164 +	* @return				A pointer to the new CPaddingNone object.
   1.165 +	*/
   1.166 +	IMPORT_C static CPaddingNone* NewLC(TInt aBlockBytes=KMaxTInt);
   1.167 +	void UnPadL(const TDesC8& aInput,TDes8& aOutput);
   1.168 +	TInt MinPaddingLength(void) const;
   1.169 +	TInt MaxPaddedLength(TInt aInputBytes) const;
   1.170 +protected:
   1.171 +	/** 
   1.172 +	* Constructor
   1.173 +	* 
   1.174 +	* @param aBlockBytes	The block size in bytes.
   1.175 +	*/
   1.176 +	IMPORT_C CPaddingNone(TInt aBlockBytes);
   1.177 +private:
   1.178 +	CPaddingNone(void);
   1.179 +	CPaddingNone(const CPaddingNone&);
   1.180 +	CPaddingNone& operator=(const CPaddingNone&);
   1.181 +	void DoPadL(const TDesC8& aInput,TDes8& aOutput);
   1.182 +	};
   1.183 +
   1.184 +/**
   1.185 +* This concrete subclass of CPadding implements PKCS#1 v1.5 signature padding.
   1.186 +*
   1.187 +* It is intended for use with RSA signing/verifying.
   1.188 +*
   1.189 +*/
   1.190 +class CPaddingPKCS1Signature : public CPadding
   1.191 +	{
   1.192 +public:
   1.193 +	/**
   1.194 +	* Creates a new CPaddingPKCS1Signature object.
   1.195 +	*
   1.196 +	* @param aBlockBytes	The block size in bytes.
   1.197 +	* @return				A pointer to the new CPaddingPKCS1Signature object.
   1.198 +	*/
   1.199 +	IMPORT_C static CPaddingPKCS1Signature* NewL(TInt aBlockBytes);
   1.200 +
   1.201 +	/**
   1.202 +	* Creates a new CPaddingPKCS1Signature object and leaves a pointer to it on the
   1.203 +	* cleanup stack.
   1.204 +	*
   1.205 +	* @param aBlockBytes	The block size in bytes.
   1.206 +	* @return				A pointer to the new CPaddingPKCS1Signature object.
   1.207 +	*/
   1.208 +	IMPORT_C static CPaddingPKCS1Signature* NewLC(
   1.209 +		TInt aBlockBytes);
   1.210 +	void UnPadL(const TDesC8& aInput,TDes8& aOutput);
   1.211 +	TInt MinPaddingLength(void) const;
   1.212 +protected:
   1.213 +	/** 
   1.214 +	* Constructor
   1.215 +	* 
   1.216 +	* @param aBlockBytes	The block size in bytes.
   1.217 +	*/
   1.218 +	IMPORT_C CPaddingPKCS1Signature(TInt aBlockBytes);
   1.219 +private:
   1.220 +	CPaddingPKCS1Signature(void);
   1.221 +	CPaddingPKCS1Signature(const CPaddingPKCS1Signature&);
   1.222 +	CPaddingPKCS1Signature& operator=(const CPaddingPKCS1Signature&);
   1.223 +	void DoPadL(const TDesC8& aInput,TDes8& aOutput);
   1.224 +	};
   1.225 +
   1.226 +/**
   1.227 +* This concrete subclass of CPadding implements PKCS#1 v1.5 encryption padding.
   1.228 +* It is intended for use with RSA encryption/decryption.
   1.229 +*
   1.230 +*/
   1.231 +class CPaddingPKCS1Encryption : public CPadding
   1.232 +	{
   1.233 +public:
   1.234 +	/**
   1.235 +	* Creates a new CPaddingPKCS1Encryption object.
   1.236 +	*
   1.237 +	* @param aBlockBytes	The block size in bytes.
   1.238 +	* @return				A pointer to the new CPaddingPKCS1Encryption object.
   1.239 +	*/
   1.240 +	IMPORT_C static CPaddingPKCS1Encryption* NewL(TInt aBlockBytes);
   1.241 +
   1.242 +	/**
   1.243 +	* Creates a new CPaddingPKCS1Encryption object and leaves a pointer to it on the
   1.244 +	* cleanup stack.
   1.245 +	*
   1.246 +	* @param aBlockBytes	The block size in bytes.
   1.247 +	* @return				A pointer to the new CPaddingPKCS1Encryption object.
   1.248 +	*/
   1.249 +	IMPORT_C static CPaddingPKCS1Encryption* NewLC(TInt aBlockBytes);
   1.250 +	void UnPadL(const TDesC8& aInput,TDes8& aOutput);
   1.251 +	TInt MinPaddingLength(void) const;
   1.252 +protected:
   1.253 +	/** 
   1.254 +	* Constructor
   1.255 +	* 
   1.256 +	* @param aBlockBytes	The block size in bytes.
   1.257 +	*/
   1.258 +	IMPORT_C CPaddingPKCS1Encryption(TInt aBlockBytes);
   1.259 +private:
   1.260 +	CPaddingPKCS1Encryption(void);
   1.261 +	CPaddingPKCS1Encryption(const CPaddingPKCS1Encryption&);
   1.262 +	CPaddingPKCS1Encryption& operator=(const CPaddingPKCS1Encryption&);
   1.263 +	void DoPadL(const TDesC8& aInput,TDes8& aOutput);
   1.264 +	};
   1.265 +
   1.266 +/**
   1.267 +* This concrete subclass of CPadding implements padding according to 
   1.268 +* the SSLv3/TLS standard.
   1.269 +*
   1.270 +* The SSL 3.0 spec does not specifiy the padding bytes to be used - it is
   1.271 +* assumed to be arbitrary (and the openssl implementation uses non-zero random
   1.272 +* data).  The TLS spec however states that padding bytes should be the length
   1.273 +* of the padding - 1.  This class implements the latter when padding, but does
   1.274 +* not check the padding byes when unpadding, so as to be interoperable with SSL
   1.275 +* 3.0.
   1.276 +* 
   1.277 +*/
   1.278 +class CPaddingSSLv3 : public CPadding
   1.279 +	{
   1.280 +public:
   1.281 +	/**
   1.282 +	* Creates a new CPaddingSSLv3 object.
   1.283 +	*
   1.284 +	* @param aBlockBytes	The block size in bytes.
   1.285 +	* @return				A pointer to the new CPaddingSSLv3 object.
   1.286 +	*/
   1.287 +	IMPORT_C static CPaddingSSLv3* NewL(TInt aBlockBytes);
   1.288 +
   1.289 +	/**
   1.290 +	* Creates a new CPaddingSSLv3 object and leaves a pointer to it on the cleanup stack.
   1.291 +	*
   1.292 +	* @param aBlockBytes	The block size in bytes.
   1.293 +	* @return				A pointer to the new CPaddingSSLv3 object.
   1.294 +	*/
   1.295 +	IMPORT_C static CPaddingSSLv3* NewLC(TInt aBlockBytes);
   1.296 +	void UnPadL(const TDesC8& aInput,TDes8& aOutput);
   1.297 +	TInt MinPaddingLength(void) const;
   1.298 +	TInt MaxPaddedLength(TInt aInputBytes) const;
   1.299 +
   1.300 +protected:
   1.301 +	/** 
   1.302 +	* Constructor
   1.303 +	* 
   1.304 +	* @param aBlockBytes	The block size in bytes.
   1.305 +	*/
   1.306 +	IMPORT_C CPaddingSSLv3(TInt aBlockBytes);
   1.307 +private:
   1.308 +	CPaddingSSLv3(void);
   1.309 +	CPaddingSSLv3(const CPaddingSSLv3&);
   1.310 +	CPaddingSSLv3& operator=(const CPaddingSSLv3&);
   1.311 +	void DoPadL(const TDesC8& aInput,TDes8& aOutput);
   1.312 +	};
   1.313 +
   1.314 +/**
   1.315 +* This concrete subclass of CPadding implements padding according to 
   1.316 +* the PKCS#7/TLS standard.
   1.317 +*
   1.318 +*/
   1.319 +class CPaddingPKCS7 : public CPadding
   1.320 +	{
   1.321 +public:
   1.322 +	/**
   1.323 +	* Creates a new CPaddingPKCS7 object.
   1.324 +	* 
   1.325 +	* @param aBlockBytes	The block size in bytes.
   1.326 +	* @return				A pointer to the new CPaddingPKCS7 object.
   1.327 +	*/
   1.328 +	IMPORT_C static CPaddingPKCS7* NewL(TInt aBlockBytes);
   1.329 +
   1.330 +	/**
   1.331 +	* Creates a new CPaddingPKCS7 object and leaves a pointer to it on the cleanup stack.
   1.332 +	*
   1.333 +	* @param aBlockBytes	The block size in bytes.
   1.334 +	* @return				A pointer to the new CPaddingPKCS7 object.
   1.335 +	*/
   1.336 +	IMPORT_C static CPaddingPKCS7* NewLC(TInt aBlockBytes);
   1.337 +	void UnPadL(const TDesC8& aInput,TDes8& aOutput);
   1.338 +	TInt MinPaddingLength(void) const;
   1.339 +	TInt MaxPaddedLength(TInt aInputBytes) const;
   1.340 +
   1.341 +protected:
   1.342 +	/** 
   1.343 +	* Constructor
   1.344 +	* 
   1.345 +	* @param aBlockBytes	The block size in bytes.
   1.346 +	*/
   1.347 +	IMPORT_C CPaddingPKCS7(TInt aBlockBytes);
   1.348 +private:
   1.349 +	CPaddingPKCS7(void);
   1.350 +	CPaddingPKCS7(const CPaddingPKCS7&);
   1.351 +	CPaddingPKCS7& operator=(const CPaddingPKCS7&);
   1.352 +	void DoPadL(const TDesC8& aInput,TDes8& aOutput);
   1.353 +	};
   1.354 +
   1.355 +#endif