os/security/crypto/weakcrypto/inc/padding.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     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".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    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.
    17 * Padding codes
    18 *
    19 */
    20 
    21 
    22 /**
    23  @file 
    24  @publishedAll
    25  @released
    26 */
    27  
    28 #ifndef __PADDING_H__
    29 #define __PADDING_H__
    30 
    31 #include <random.h>
    32 /** 
    33 * Abstract base class defining the interface to padding schemes.
    34 *
    35 * It is designed to be used by both symmetric and asymmetric ciphers.
    36 *
    37 */
    38 class CPadding : public CBase
    39 	{
    40 public:
    41 	/** 
    42 	* Pads aInput to be BlockSize() bytes long and places the result in aOutput.  
    43 	*
    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.  
    48 	*/
    49 	IMPORT_C void PadL(const TDesC8& aInput,TDes8& aOutput);
    50 
    51 	/**
    52 	* Pads aInput to be BlockSize() long and places the result in aOutput.  
    53 	*
    54 	* Deriving implementations of DoPadL() can assume that aInput is less than or
    55 	* equal to BlockSize() minus MinPaddingLength().
    56 	*
    57 	* @param aInput		Data to be padded.  
    58 	* @param aOutput	On return, the resulting padded, aBlockBytes aligned data 
    59 	*					appended to aOutput.  
    60 	*/
    61 	virtual void DoPadL(const TDesC8& aInput,TDes8& aOutput) = 0;
    62 
    63 	/**
    64 	* Removes padding from aInput and appends unpadded result to aOutput.
    65 	* 
    66 	* @param aInput		Data to be unpadded.
    67 	* @param aOutput	On return, the unpadded data.
    68 	*/
    69 	virtual void UnPadL(const TDesC8& aInput,TDes8& aOutput) = 0;
    70 
    71 	/**
    72 	* Sets the block size for this padding system.
    73 	* 
    74 	* @param aBlockBytes	The block size in bytes.
    75 	*/
    76 	IMPORT_C void SetBlockSize(TInt aBlockBytes);
    77 
    78 	/**
    79 	* Retrieves the block size for this padding system.
    80 	*
    81 	* @return	The block size in bytes.
    82 	*/
    83 	IMPORT_C TInt BlockSize(void) const;
    84 
    85 	/**
    86 	* Gets the smallest number of bytes that PadL() will ever add to aInput in
    87 	* order to get a valid block aligned aOutput.  
    88 	*
    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)
    92 	* of padded data.
    93 	* 
    94 	* @return	A TInt containing the smallest number of padding bytes possible.
    95 	*/
    96 	virtual TInt MinPaddingLength(void) const = 0;
    97 
    98 	/**
    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
   101 	* aInputBytes.
   102 	* 
   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.
   106 	*
   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.
   110 	*/
   111 	IMPORT_C virtual TInt MaxPaddedLength(TInt aInputBytes) const;
   112 
   113 	/**
   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.
   116 	*
   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.
   121 	*
   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.
   125 	*/
   126 	IMPORT_C virtual TInt MaxUnPaddedLength(TInt aInputBytes) const;
   127 protected:
   128 	/** 
   129 	* Constructor
   130 	* 
   131 	* @param aBlockBytes	The block size in bytes.
   132 	*/
   133 	IMPORT_C CPadding(TInt aBlockBytes);
   134 private:
   135 	CPadding(void);
   136 	CPadding(const CPadding&);
   137 	CPadding& operator=(const CPadding&);
   138 private:
   139 	TInt iBlockBytes;
   140 	};
   141 
   142 /**
   143 * This concrete subclass of CPadding appends no padding.
   144 *
   145 * aOutput will be a copy of aInput after any call to PadL() or UnPadL().
   146 *
   147 */
   148 class CPaddingNone:public CPadding
   149 	{
   150 public:
   151 	/**
   152 	* Creates a new CPaddingNone object.
   153 	*
   154 	* @param aBlockBytes	The block size in bytes.
   155 	* @return				A pointer to the new CPaddingNone object.
   156 	*/
   157 	IMPORT_C static CPaddingNone* NewL(TInt aBlockBytes=KMaxTInt);
   158 
   159 	/**
   160 	* Creates a new CPaddingNone object and leaves a pointer to it on the cleanup stack.
   161 	* 
   162 	* @param aBlockBytes	The block size in bytes.
   163 	* @return				A pointer to the new CPaddingNone object.
   164 	*/
   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;
   170 protected:
   171 	/** 
   172 	* Constructor
   173 	* 
   174 	* @param aBlockBytes	The block size in bytes.
   175 	*/
   176 	IMPORT_C CPaddingNone(TInt aBlockBytes);
   177 private:
   178 	CPaddingNone(void);
   179 	CPaddingNone(const CPaddingNone&);
   180 	CPaddingNone& operator=(const CPaddingNone&);
   181 	};
   182 
   183 /**
   184 * This concrete subclass of CPadding implements PKCS#1 v1.5 signature padding.
   185 *
   186 * It is intended for use with RSA signing/verifying.
   187 *
   188 */
   189 class CPaddingPKCS1Signature : public CPadding
   190 	{
   191 public:
   192 	/**
   193 	* Creates a new CPaddingPKCS1Signature object.
   194 	*
   195 	* @param aBlockBytes	The block size in bytes.
   196 	* @return				A pointer to the new CPaddingPKCS1Signature object.
   197 	*/
   198 	IMPORT_C static CPaddingPKCS1Signature* NewL(TInt aBlockBytes);
   199 
   200 	/**
   201 	* Creates a new CPaddingPKCS1Signature object and leaves a pointer to it on the
   202 	* cleanup stack.
   203 	*
   204 	* @param aBlockBytes	The block size in bytes.
   205 	* @return				A pointer to the new CPaddingPKCS1Signature object.
   206 	*/
   207 	IMPORT_C static CPaddingPKCS1Signature* NewLC(
   208 		TInt aBlockBytes);
   209 	void DoPadL(const TDesC8& aInput,TDes8& aOutput);
   210 	void UnPadL(const TDesC8& aInput,TDes8& aOutput);
   211 	TInt MinPaddingLength(void) const;
   212 protected:
   213 	/** 
   214 	* Constructor
   215 	* 
   216 	* @param aBlockBytes	The block size in bytes.
   217 	*/
   218 	IMPORT_C CPaddingPKCS1Signature(TInt aBlockBytes);
   219 private:
   220 	CPaddingPKCS1Signature(void);
   221 	CPaddingPKCS1Signature(const CPaddingPKCS1Signature&);
   222 	CPaddingPKCS1Signature& operator=(const CPaddingPKCS1Signature&);
   223 	};
   224 
   225 /**
   226 * This concrete subclass of CPadding implements PKCS#1 v1.5 encryption padding.
   227 * It is intended for use with RSA encryption/decryption.
   228 *
   229 */
   230 class CPaddingPKCS1Encryption : public CPadding
   231 	{
   232 public:
   233 	/**
   234 	* Creates a new CPaddingPKCS1Encryption object.
   235 	*
   236 	* @param aBlockBytes	The block size in bytes.
   237 	* @return				A pointer to the new CPaddingPKCS1Encryption object.
   238 	*/
   239 	IMPORT_C static CPaddingPKCS1Encryption* NewL(TInt aBlockBytes);
   240 
   241 	/**
   242 	* Creates a new CPaddingPKCS1Encryption object and leaves a pointer to it on the
   243 	* cleanup stack.
   244 	*
   245 	* @param aBlockBytes	The block size in bytes.
   246 	* @return				A pointer to the new CPaddingPKCS1Encryption object.
   247 	*/
   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;
   252 protected:
   253 	/** 
   254 	* Constructor
   255 	* 
   256 	* @param aBlockBytes	The block size in bytes.
   257 	*/
   258 	IMPORT_C CPaddingPKCS1Encryption(TInt aBlockBytes);
   259 private:
   260 	CPaddingPKCS1Encryption(void);
   261 	CPaddingPKCS1Encryption(const CPaddingPKCS1Encryption&);
   262 	CPaddingPKCS1Encryption& operator=(const CPaddingPKCS1Encryption&);
   263 	};
   264 
   265 /**
   266 * This concrete subclass of CPadding implements padding according to 
   267 * the SSLv3/TLS standard.
   268 *
   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
   274 * 3.0.
   275 * 
   276 */
   277 class CPaddingSSLv3 : public CPadding
   278 	{
   279 public:
   280 	/**
   281 	* Creates a new CPaddingSSLv3 object.
   282 	*
   283 	* @param aBlockBytes	The block size in bytes.
   284 	* @return				A pointer to the new CPaddingSSLv3 object.
   285 	*/
   286 	IMPORT_C static CPaddingSSLv3* NewL(TInt aBlockBytes);
   287 
   288 	/**
   289 	* Creates a new CPaddingSSLv3 object and leaves a pointer to it on the cleanup stack.
   290 	*
   291 	* @param aBlockBytes	The block size in bytes.
   292 	* @return				A pointer to the new CPaddingSSLv3 object.
   293 	*/
   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;
   299 
   300 protected:
   301 	/** 
   302 	* Constructor
   303 	* 
   304 	* @param aBlockBytes	The block size in bytes.
   305 	*/
   306 	IMPORT_C CPaddingSSLv3(TInt aBlockBytes);
   307 private:
   308 	CPaddingSSLv3(void);
   309 	CPaddingSSLv3(const CPaddingSSLv3&);
   310 	CPaddingSSLv3& operator=(const CPaddingSSLv3&);
   311 	};
   312 
   313 /**
   314 * This concrete subclass of CPadding implements padding according to 
   315 * the PKCS#7/TLS standard.
   316 *
   317 */
   318 class CPaddingPKCS7 : public CPadding
   319 	{
   320 public:
   321 	/**
   322 	* Creates a new CPaddingPKCS7 object.
   323 	* 
   324 	* @param aBlockBytes	The block size in bytes.
   325 	* @return				A pointer to the new CPaddingPKCS7 object.
   326 	*/
   327 	IMPORT_C static CPaddingPKCS7* NewL(TInt aBlockBytes);
   328 
   329 	/**
   330 	* Creates a new CPaddingPKCS7 object and leaves a pointer to it on the cleanup stack.
   331 	*
   332 	* @param aBlockBytes	The block size in bytes.
   333 	* @return				A pointer to the new CPaddingPKCS7 object.
   334 	*/
   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;
   340 
   341 protected:
   342 	/** 
   343 	* Constructor
   344 	* 
   345 	* @param aBlockBytes	The block size in bytes.
   346 	*/
   347 	IMPORT_C CPaddingPKCS7(TInt aBlockBytes);
   348 private:
   349 	CPaddingPKCS7(void);
   350 	CPaddingPKCS7(const CPaddingPKCS7&);
   351 	CPaddingPKCS7& operator=(const CPaddingPKCS7&);
   352 	};
   353 
   354 #endif