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