epoc32/include/padding.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
williamr@4
     1
/*
williamr@4
     2
* Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
williamr@4
     3
* All rights reserved.
williamr@4
     4
* This component and the accompanying materials are made available
williamr@4
     5
* under the terms of the License "Eclipse Public License v1.0"
williamr@4
     6
* which accompanies this distribution, and is available
williamr@4
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
williamr@4
     8
*
williamr@4
     9
* Initial Contributors:
williamr@4
    10
* Nokia Corporation - initial contribution.
williamr@4
    11
*
williamr@4
    12
* Contributors:
williamr@4
    13
*
williamr@4
    14
* Description: 
williamr@4
    15
* ** IMPORTANT **  API's in this file are published to 3rd party developers via the 
williamr@4
    16
* Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted.
williamr@4
    17
* Padding codes
williamr@4
    18
*
williamr@4
    19
*/
williamr@4
    20
williamr@4
    21
williamr@4
    22
/**
williamr@4
    23
 @file 
williamr@4
    24
 @publishedAll
williamr@4
    25
 @released
williamr@4
    26
*/
williamr@4
    27
 
williamr@4
    28
#ifndef __PADDING_H__
williamr@4
    29
#define __PADDING_H__
williamr@4
    30
williamr@4
    31
#include <random.h>
williamr@4
    32
/** 
williamr@4
    33
* Abstract base class defining the interface to padding schemes.
williamr@4
    34
*
williamr@4
    35
* It is designed to be used by both symmetric and asymmetric ciphers.
williamr@4
    36
*
williamr@4
    37
*/
williamr@4
    38
class CPadding : public CBase
williamr@4
    39
	{
williamr@4
    40
public:
williamr@4
    41
	/** 
williamr@4
    42
	* Pads aInput to be BlockSize() bytes long and places the result in aOutput.  
williamr@4
    43
	*
williamr@4
    44
	* @param aInput		Data to be padded.  The size must be less than or equal to
williamr@4
    45
	* 					BlockSize() minus MinPaddingLength().
williamr@4
    46
	* @param aOutput	On return, the resulting padded, block size aligned data
williamr@4
    47
	*					appended to aOutput.  
williamr@4
    48
	*/
williamr@4
    49
	IMPORT_C void PadL(const TDesC8& aInput,TDes8& aOutput);
williamr@4
    50
williamr@4
    51
	
williamr@4
    52
	/**
williamr@4
    53
	* Removes padding from aInput and appends unpadded result to aOutput.
williamr@4
    54
	* 
williamr@4
    55
	* @param aInput		Data to be unpadded.
williamr@4
    56
	* @param aOutput	On return, the unpadded data.
williamr@4
    57
	*/
williamr@4
    58
	virtual void UnPadL(const TDesC8& aInput,TDes8& aOutput) = 0;
williamr@4
    59
williamr@4
    60
	/**
williamr@4
    61
	* Sets the block size for this padding system.
williamr@4
    62
	* 
williamr@4
    63
	* @param aBlockBytes	The block size in bytes.
williamr@4
    64
	*/
williamr@4
    65
	IMPORT_C void SetBlockSize(TInt aBlockBytes);
williamr@4
    66
williamr@4
    67
	/**
williamr@4
    68
	* Retrieves the block size for this padding system.
williamr@4
    69
	*
williamr@4
    70
	* @return	The block size in bytes.
williamr@4
    71
	*/
williamr@4
    72
	IMPORT_C TInt BlockSize(void) const;
williamr@4
    73
williamr@4
    74
	/**
williamr@4
    75
	* Gets the smallest number of bytes that PadL() will ever add to aInput in
williamr@4
    76
	* order to get a valid block aligned aOutput.  
williamr@4
    77
	*
williamr@4
    78
	* For example, in SSLv3 padding, if the block size is 8 and aInput is 7 bytes,
williamr@4
    79
	* it will append 1 byte of padding. For SSLv3 padding, this is the smallest 
williamr@4
    80
	* amount possible as an 8 byte input will add another block size (8 more bytes)
williamr@4
    81
	* of padded data.
williamr@4
    82
	* 
williamr@4
    83
	* @return	A TInt containing the smallest number of padding bytes possible.
williamr@4
    84
	*/
williamr@4
    85
	virtual TInt MinPaddingLength(void) const = 0;
williamr@4
    86
williamr@4
    87
	/**
williamr@4
    88
	* Gets the size of the aOutput buffer, in a call to PadL(), must be in
williamr@4
    89
	* order to accommodate a block size of BlockSize() and an input size of
williamr@4
    90
	* aInputBytes.
williamr@4
    91
	* 
williamr@4
    92
	* @note	By default, this function returns the output of BlockSize().  If
williamr@4
    93
	*		a derived padding system outputs more than a single block of padding,
williamr@4
    94
	*		one must override this function and return the appropriate value.
williamr@4
    95
	*
williamr@4
    96
	* @param aInputBytes	The amount of data to be padded out in bytes.
williamr@4
    97
	* @return				A TInt representing the maximum amount of padded output data
williamr@4
    98
	*						(in bytes) for a given block and input size.
williamr@4
    99
	*/
williamr@4
   100
	IMPORT_C virtual TInt MaxPaddedLength(TInt aInputBytes) const;
williamr@4
   101
williamr@4
   102
	/**
williamr@4
   103
	* Gets the size of the aOutput buffer, in a call to UnPadL(), must be in
williamr@4
   104
	* order to accommodate an input size of aInputBytes.
williamr@4
   105
	*
williamr@4
   106
	* @note	By default, this function returns the value of aInputBytes minus MinPaddingBytes().
williamr@4
   107
	*		Most padding systems cannot determine anything about the unpadded length
williamr@4
   108
	*		without looking at the data.  If your padding system allows you to give a
williamr@4
   109
	*		better bound, then you should reimplement this function.
williamr@4
   110
	*
williamr@4
   111
	* @param aInputBytes	The amount of data to be unpadded in bytes.
williamr@4
   112
	* @return				A TInt containing the maximum amount of unpadded output data
williamr@4
   113
	*						(in bytes) for a given padded input.
williamr@4
   114
	*/
williamr@4
   115
	IMPORT_C virtual TInt MaxUnPaddedLength(TInt aInputBytes) const;
williamr@4
   116
	
williamr@4
   117
	/**
williamr@4
   118
	@internalComponent
williamr@4
   119
	Used to retrieve the extended interfaces by id. For Crypto
williamr@4
   120
	SPI internal use only.
williamr@4
   121
	*/		
williamr@4
   122
	TInt GetExtension(TUint aExtensionId, TAny*& a0, TAny* a1);		
williamr@4
   123
	
williamr@4
   124
protected:
williamr@4
   125
	/** 
williamr@4
   126
	* Constructor
williamr@4
   127
	* 
williamr@4
   128
	* @param aBlockBytes	The block size in bytes.
williamr@4
   129
	*/
williamr@4
   130
	IMPORT_C CPadding(TInt aBlockBytes);
williamr@4
   131
private:
williamr@4
   132
	CPadding(void);
williamr@4
   133
	CPadding(const CPadding&);
williamr@4
   134
	CPadding& operator=(const CPadding&);
williamr@4
   135
	virtual void DoPadL(const TDesC8& aInput,TDes8& aOutput) = 0;
williamr@4
   136
private:
williamr@4
   137
	TInt iBlockBytes;
williamr@4
   138
	};
williamr@4
   139
williamr@4
   140
/**
williamr@4
   141
* This concrete subclass of CPadding appends no padding.
williamr@4
   142
*
williamr@4
   143
* aOutput will be a copy of aInput after any call to PadL() or UnPadL().
williamr@4
   144
*
williamr@4
   145
*/
williamr@4
   146
class CPaddingNone:public CPadding
williamr@4
   147
	{
williamr@4
   148
public:
williamr@4
   149
	/**
williamr@4
   150
	* Creates a new CPaddingNone object.
williamr@4
   151
	*
williamr@4
   152
	* @param aBlockBytes	The block size in bytes.
williamr@4
   153
	* @return				A pointer to the new CPaddingNone object.
williamr@4
   154
	*/
williamr@4
   155
	IMPORT_C static CPaddingNone* NewL(TInt aBlockBytes=KMaxTInt);
williamr@4
   156
williamr@4
   157
	/**
williamr@4
   158
	* Creates a new CPaddingNone object and leaves a pointer to it on the cleanup stack.
williamr@4
   159
	* 
williamr@4
   160
	* @param aBlockBytes	The block size in bytes.
williamr@4
   161
	* @return				A pointer to the new CPaddingNone object.
williamr@4
   162
	*/
williamr@4
   163
	IMPORT_C static CPaddingNone* NewLC(TInt aBlockBytes=KMaxTInt);
williamr@4
   164
	void UnPadL(const TDesC8& aInput,TDes8& aOutput);
williamr@4
   165
	TInt MinPaddingLength(void) const;
williamr@4
   166
	TInt MaxPaddedLength(TInt aInputBytes) const;
williamr@4
   167
protected:
williamr@4
   168
	/** 
williamr@4
   169
	* Constructor
williamr@4
   170
	* 
williamr@4
   171
	* @param aBlockBytes	The block size in bytes.
williamr@4
   172
	*/
williamr@4
   173
	IMPORT_C CPaddingNone(TInt aBlockBytes);
williamr@4
   174
private:
williamr@4
   175
	CPaddingNone(void);
williamr@4
   176
	CPaddingNone(const CPaddingNone&);
williamr@4
   177
	CPaddingNone& operator=(const CPaddingNone&);
williamr@4
   178
	void DoPadL(const TDesC8& aInput,TDes8& aOutput);
williamr@4
   179
	};
williamr@4
   180
williamr@4
   181
/**
williamr@4
   182
* This concrete subclass of CPadding implements PKCS#1 v1.5 signature padding.
williamr@4
   183
*
williamr@4
   184
* It is intended for use with RSA signing/verifying.
williamr@4
   185
*
williamr@4
   186
*/
williamr@4
   187
class CPaddingPKCS1Signature : public CPadding
williamr@4
   188
	{
williamr@4
   189
public:
williamr@4
   190
	/**
williamr@4
   191
	* Creates a new CPaddingPKCS1Signature object.
williamr@4
   192
	*
williamr@4
   193
	* @param aBlockBytes	The block size in bytes.
williamr@4
   194
	* @return				A pointer to the new CPaddingPKCS1Signature object.
williamr@4
   195
	*/
williamr@4
   196
	IMPORT_C static CPaddingPKCS1Signature* NewL(TInt aBlockBytes);
williamr@4
   197
williamr@4
   198
	/**
williamr@4
   199
	* Creates a new CPaddingPKCS1Signature object and leaves a pointer to it on the
williamr@4
   200
	* cleanup stack.
williamr@4
   201
	*
williamr@4
   202
	* @param aBlockBytes	The block size in bytes.
williamr@4
   203
	* @return				A pointer to the new CPaddingPKCS1Signature object.
williamr@4
   204
	*/
williamr@4
   205
	IMPORT_C static CPaddingPKCS1Signature* NewLC(
williamr@4
   206
		TInt aBlockBytes);
williamr@4
   207
	void UnPadL(const TDesC8& aInput,TDes8& aOutput);
williamr@4
   208
	TInt MinPaddingLength(void) const;
williamr@4
   209
protected:
williamr@4
   210
	/** 
williamr@4
   211
	* Constructor
williamr@4
   212
	* 
williamr@4
   213
	* @param aBlockBytes	The block size in bytes.
williamr@4
   214
	*/
williamr@4
   215
	IMPORT_C CPaddingPKCS1Signature(TInt aBlockBytes);
williamr@4
   216
private:
williamr@4
   217
	CPaddingPKCS1Signature(void);
williamr@4
   218
	CPaddingPKCS1Signature(const CPaddingPKCS1Signature&);
williamr@4
   219
	CPaddingPKCS1Signature& operator=(const CPaddingPKCS1Signature&);
williamr@4
   220
	void DoPadL(const TDesC8& aInput,TDes8& aOutput);
williamr@4
   221
	};
williamr@4
   222
williamr@4
   223
/**
williamr@4
   224
* This concrete subclass of CPadding implements PKCS#1 v1.5 encryption padding.
williamr@4
   225
* It is intended for use with RSA encryption/decryption.
williamr@4
   226
*
williamr@4
   227
*/
williamr@4
   228
class CPaddingPKCS1Encryption : public CPadding
williamr@4
   229
	{
williamr@4
   230
public:
williamr@4
   231
	/**
williamr@4
   232
	* Creates a new CPaddingPKCS1Encryption object.
williamr@4
   233
	*
williamr@4
   234
	* @param aBlockBytes	The block size in bytes.
williamr@4
   235
	* @return				A pointer to the new CPaddingPKCS1Encryption object.
williamr@4
   236
	*/
williamr@4
   237
	IMPORT_C static CPaddingPKCS1Encryption* NewL(TInt aBlockBytes);
williamr@4
   238
williamr@4
   239
	/**
williamr@4
   240
	* Creates a new CPaddingPKCS1Encryption object and leaves a pointer to it on the
williamr@4
   241
	* cleanup stack.
williamr@4
   242
	*
williamr@4
   243
	* @param aBlockBytes	The block size in bytes.
williamr@4
   244
	* @return				A pointer to the new CPaddingPKCS1Encryption object.
williamr@4
   245
	*/
williamr@4
   246
	IMPORT_C static CPaddingPKCS1Encryption* NewLC(TInt aBlockBytes);
williamr@4
   247
	void UnPadL(const TDesC8& aInput,TDes8& aOutput);
williamr@4
   248
	TInt MinPaddingLength(void) const;
williamr@4
   249
protected:
williamr@4
   250
	/** 
williamr@4
   251
	* Constructor
williamr@4
   252
	* 
williamr@4
   253
	* @param aBlockBytes	The block size in bytes.
williamr@4
   254
	*/
williamr@4
   255
	IMPORT_C CPaddingPKCS1Encryption(TInt aBlockBytes);
williamr@4
   256
private:
williamr@4
   257
	CPaddingPKCS1Encryption(void);
williamr@4
   258
	CPaddingPKCS1Encryption(const CPaddingPKCS1Encryption&);
williamr@4
   259
	CPaddingPKCS1Encryption& operator=(const CPaddingPKCS1Encryption&);
williamr@4
   260
	void DoPadL(const TDesC8& aInput,TDes8& aOutput);
williamr@4
   261
	};
williamr@4
   262
williamr@4
   263
/**
williamr@4
   264
* This concrete subclass of CPadding implements padding according to 
williamr@4
   265
* the SSLv3/TLS standard.
williamr@4
   266
*
williamr@4
   267
* The SSL 3.0 spec does not specifiy the padding bytes to be used - it is
williamr@4
   268
* assumed to be arbitrary (and the openssl implementation uses non-zero random
williamr@4
   269
* data).  The TLS spec however states that padding bytes should be the length
williamr@4
   270
* of the padding - 1.  This class implements the latter when padding, but does
williamr@4
   271
* not check the padding byes when unpadding, so as to be interoperable with SSL
williamr@4
   272
* 3.0.
williamr@4
   273
* 
williamr@4
   274
*/
williamr@4
   275
class CPaddingSSLv3 : public CPadding
williamr@4
   276
	{
williamr@4
   277
public:
williamr@4
   278
	/**
williamr@4
   279
	* Creates a new CPaddingSSLv3 object.
williamr@4
   280
	*
williamr@4
   281
	* @param aBlockBytes	The block size in bytes.
williamr@4
   282
	* @return				A pointer to the new CPaddingSSLv3 object.
williamr@4
   283
	*/
williamr@4
   284
	IMPORT_C static CPaddingSSLv3* NewL(TInt aBlockBytes);
williamr@4
   285
williamr@4
   286
	/**
williamr@4
   287
	* Creates a new CPaddingSSLv3 object and leaves a pointer to it on the cleanup stack.
williamr@4
   288
	*
williamr@4
   289
	* @param aBlockBytes	The block size in bytes.
williamr@4
   290
	* @return				A pointer to the new CPaddingSSLv3 object.
williamr@4
   291
	*/
williamr@4
   292
	IMPORT_C static CPaddingSSLv3* NewLC(TInt aBlockBytes);
williamr@4
   293
	void UnPadL(const TDesC8& aInput,TDes8& aOutput);
williamr@4
   294
	TInt MinPaddingLength(void) const;
williamr@4
   295
	TInt MaxPaddedLength(TInt aInputBytes) const;
williamr@4
   296
williamr@4
   297
protected:
williamr@4
   298
	/** 
williamr@4
   299
	* Constructor
williamr@4
   300
	* 
williamr@4
   301
	* @param aBlockBytes	The block size in bytes.
williamr@4
   302
	*/
williamr@4
   303
	IMPORT_C CPaddingSSLv3(TInt aBlockBytes);
williamr@4
   304
private:
williamr@4
   305
	CPaddingSSLv3(void);
williamr@4
   306
	CPaddingSSLv3(const CPaddingSSLv3&);
williamr@4
   307
	CPaddingSSLv3& operator=(const CPaddingSSLv3&);
williamr@4
   308
	void DoPadL(const TDesC8& aInput,TDes8& aOutput);
williamr@4
   309
	};
williamr@4
   310
williamr@4
   311
/**
williamr@4
   312
* This concrete subclass of CPadding implements padding according to 
williamr@4
   313
* the PKCS#7/TLS standard.
williamr@4
   314
*
williamr@4
   315
*/
williamr@4
   316
class CPaddingPKCS7 : public CPadding
williamr@4
   317
	{
williamr@4
   318
public:
williamr@4
   319
	/**
williamr@4
   320
	* Creates a new CPaddingPKCS7 object.
williamr@4
   321
	* 
williamr@4
   322
	* @param aBlockBytes	The block size in bytes.
williamr@4
   323
	* @return				A pointer to the new CPaddingPKCS7 object.
williamr@4
   324
	*/
williamr@4
   325
	IMPORT_C static CPaddingPKCS7* NewL(TInt aBlockBytes);
williamr@4
   326
williamr@4
   327
	/**
williamr@4
   328
	* Creates a new CPaddingPKCS7 object and leaves a pointer to it on the cleanup stack.
williamr@4
   329
	*
williamr@4
   330
	* @param aBlockBytes	The block size in bytes.
williamr@4
   331
	* @return				A pointer to the new CPaddingPKCS7 object.
williamr@4
   332
	*/
williamr@4
   333
	IMPORT_C static CPaddingPKCS7* NewLC(TInt aBlockBytes);
williamr@4
   334
	void UnPadL(const TDesC8& aInput,TDes8& aOutput);
williamr@4
   335
	TInt MinPaddingLength(void) const;
williamr@4
   336
	TInt MaxPaddedLength(TInt aInputBytes) const;
williamr@4
   337
williamr@4
   338
protected:
williamr@4
   339
	/** 
williamr@4
   340
	* Constructor
williamr@4
   341
	* 
williamr@4
   342
	* @param aBlockBytes	The block size in bytes.
williamr@4
   343
	*/
williamr@4
   344
	IMPORT_C CPaddingPKCS7(TInt aBlockBytes);
williamr@4
   345
private:
williamr@4
   346
	CPaddingPKCS7(void);
williamr@4
   347
	CPaddingPKCS7(const CPaddingPKCS7&);
williamr@4
   348
	CPaddingPKCS7& operator=(const CPaddingPKCS7&);
williamr@4
   349
	void DoPadL(const TDesC8& aInput,TDes8& aOutput);
williamr@4
   350
	};
williamr@4
   351
williamr@4
   352
#endif