williamr@4: /* williamr@4: * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies). williamr@4: * All rights reserved. williamr@4: * This component and the accompanying materials are made available williamr@4: * under the terms of the License "Eclipse Public License v1.0" williamr@4: * which accompanies this distribution, and is available williamr@4: * at the URL "http://www.eclipse.org/legal/epl-v10.html". williamr@4: * williamr@4: * Initial Contributors: williamr@4: * Nokia Corporation - initial contribution. williamr@4: * williamr@4: * Contributors: williamr@4: * williamr@4: * Description: williamr@4: * ** IMPORTANT ** API's in this file are published to 3rd party developers via the williamr@4: * Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted. williamr@4: * Padding codes williamr@4: * williamr@4: */ williamr@4: williamr@4: williamr@4: /** williamr@4: @file williamr@4: @publishedAll williamr@4: @released williamr@4: */ williamr@4: williamr@4: #ifndef __PADDING_H__ williamr@4: #define __PADDING_H__ williamr@4: williamr@4: #include williamr@4: /** williamr@4: * Abstract base class defining the interface to padding schemes. williamr@4: * williamr@4: * It is designed to be used by both symmetric and asymmetric ciphers. williamr@4: * williamr@4: */ williamr@4: class CPadding : public CBase williamr@4: { williamr@4: public: williamr@4: /** williamr@4: * Pads aInput to be BlockSize() bytes long and places the result in aOutput. williamr@4: * williamr@4: * @param aInput Data to be padded. The size must be less than or equal to williamr@4: * BlockSize() minus MinPaddingLength(). williamr@4: * @param aOutput On return, the resulting padded, block size aligned data williamr@4: * appended to aOutput. williamr@4: */ williamr@4: IMPORT_C void PadL(const TDesC8& aInput,TDes8& aOutput); williamr@4: williamr@4: williamr@4: /** williamr@4: * Removes padding from aInput and appends unpadded result to aOutput. williamr@4: * williamr@4: * @param aInput Data to be unpadded. williamr@4: * @param aOutput On return, the unpadded data. williamr@4: */ williamr@4: virtual void UnPadL(const TDesC8& aInput,TDes8& aOutput) = 0; williamr@4: williamr@4: /** williamr@4: * Sets the block size for this padding system. williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: */ williamr@4: IMPORT_C void SetBlockSize(TInt aBlockBytes); williamr@4: williamr@4: /** williamr@4: * Retrieves the block size for this padding system. williamr@4: * williamr@4: * @return The block size in bytes. williamr@4: */ williamr@4: IMPORT_C TInt BlockSize(void) const; williamr@4: williamr@4: /** williamr@4: * Gets the smallest number of bytes that PadL() will ever add to aInput in williamr@4: * order to get a valid block aligned aOutput. williamr@4: * williamr@4: * For example, in SSLv3 padding, if the block size is 8 and aInput is 7 bytes, williamr@4: * it will append 1 byte of padding. For SSLv3 padding, this is the smallest williamr@4: * amount possible as an 8 byte input will add another block size (8 more bytes) williamr@4: * of padded data. williamr@4: * williamr@4: * @return A TInt containing the smallest number of padding bytes possible. williamr@4: */ williamr@4: virtual TInt MinPaddingLength(void) const = 0; williamr@4: williamr@4: /** williamr@4: * Gets the size of the aOutput buffer, in a call to PadL(), must be in williamr@4: * order to accommodate a block size of BlockSize() and an input size of williamr@4: * aInputBytes. williamr@4: * williamr@4: * @note By default, this function returns the output of BlockSize(). If williamr@4: * a derived padding system outputs more than a single block of padding, williamr@4: * one must override this function and return the appropriate value. williamr@4: * williamr@4: * @param aInputBytes The amount of data to be padded out in bytes. williamr@4: * @return A TInt representing the maximum amount of padded output data williamr@4: * (in bytes) for a given block and input size. williamr@4: */ williamr@4: IMPORT_C virtual TInt MaxPaddedLength(TInt aInputBytes) const; williamr@4: williamr@4: /** williamr@4: * Gets the size of the aOutput buffer, in a call to UnPadL(), must be in williamr@4: * order to accommodate an input size of aInputBytes. williamr@4: * williamr@4: * @note By default, this function returns the value of aInputBytes minus MinPaddingBytes(). williamr@4: * Most padding systems cannot determine anything about the unpadded length williamr@4: * without looking at the data. If your padding system allows you to give a williamr@4: * better bound, then you should reimplement this function. williamr@4: * williamr@4: * @param aInputBytes The amount of data to be unpadded in bytes. williamr@4: * @return A TInt containing the maximum amount of unpadded output data williamr@4: * (in bytes) for a given padded input. williamr@4: */ williamr@4: IMPORT_C virtual TInt MaxUnPaddedLength(TInt aInputBytes) const; williamr@4: williamr@4: /** williamr@4: @internalComponent williamr@4: Used to retrieve the extended interfaces by id. For Crypto williamr@4: SPI internal use only. williamr@4: */ williamr@4: TInt GetExtension(TUint aExtensionId, TAny*& a0, TAny* a1); williamr@4: williamr@4: protected: williamr@4: /** williamr@4: * Constructor williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: */ williamr@4: IMPORT_C CPadding(TInt aBlockBytes); williamr@4: private: williamr@4: CPadding(void); williamr@4: CPadding(const CPadding&); williamr@4: CPadding& operator=(const CPadding&); williamr@4: virtual void DoPadL(const TDesC8& aInput,TDes8& aOutput) = 0; williamr@4: private: williamr@4: TInt iBlockBytes; williamr@4: }; williamr@4: williamr@4: /** williamr@4: * This concrete subclass of CPadding appends no padding. williamr@4: * williamr@4: * aOutput will be a copy of aInput after any call to PadL() or UnPadL(). williamr@4: * williamr@4: */ williamr@4: class CPaddingNone:public CPadding williamr@4: { williamr@4: public: williamr@4: /** williamr@4: * Creates a new CPaddingNone object. williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: * @return A pointer to the new CPaddingNone object. williamr@4: */ williamr@4: IMPORT_C static CPaddingNone* NewL(TInt aBlockBytes=KMaxTInt); williamr@4: williamr@4: /** williamr@4: * Creates a new CPaddingNone object and leaves a pointer to it on the cleanup stack. williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: * @return A pointer to the new CPaddingNone object. williamr@4: */ williamr@4: IMPORT_C static CPaddingNone* NewLC(TInt aBlockBytes=KMaxTInt); williamr@4: void UnPadL(const TDesC8& aInput,TDes8& aOutput); williamr@4: TInt MinPaddingLength(void) const; williamr@4: TInt MaxPaddedLength(TInt aInputBytes) const; williamr@4: protected: williamr@4: /** williamr@4: * Constructor williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: */ williamr@4: IMPORT_C CPaddingNone(TInt aBlockBytes); williamr@4: private: williamr@4: CPaddingNone(void); williamr@4: CPaddingNone(const CPaddingNone&); williamr@4: CPaddingNone& operator=(const CPaddingNone&); williamr@4: void DoPadL(const TDesC8& aInput,TDes8& aOutput); williamr@4: }; williamr@4: williamr@4: /** williamr@4: * This concrete subclass of CPadding implements PKCS#1 v1.5 signature padding. williamr@4: * williamr@4: * It is intended for use with RSA signing/verifying. williamr@4: * williamr@4: */ williamr@4: class CPaddingPKCS1Signature : public CPadding williamr@4: { williamr@4: public: williamr@4: /** williamr@4: * Creates a new CPaddingPKCS1Signature object. williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: * @return A pointer to the new CPaddingPKCS1Signature object. williamr@4: */ williamr@4: IMPORT_C static CPaddingPKCS1Signature* NewL(TInt aBlockBytes); williamr@4: williamr@4: /** williamr@4: * Creates a new CPaddingPKCS1Signature object and leaves a pointer to it on the williamr@4: * cleanup stack. williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: * @return A pointer to the new CPaddingPKCS1Signature object. williamr@4: */ williamr@4: IMPORT_C static CPaddingPKCS1Signature* NewLC( williamr@4: TInt aBlockBytes); williamr@4: void UnPadL(const TDesC8& aInput,TDes8& aOutput); williamr@4: TInt MinPaddingLength(void) const; williamr@4: protected: williamr@4: /** williamr@4: * Constructor williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: */ williamr@4: IMPORT_C CPaddingPKCS1Signature(TInt aBlockBytes); williamr@4: private: williamr@4: CPaddingPKCS1Signature(void); williamr@4: CPaddingPKCS1Signature(const CPaddingPKCS1Signature&); williamr@4: CPaddingPKCS1Signature& operator=(const CPaddingPKCS1Signature&); williamr@4: void DoPadL(const TDesC8& aInput,TDes8& aOutput); williamr@4: }; williamr@4: williamr@4: /** williamr@4: * This concrete subclass of CPadding implements PKCS#1 v1.5 encryption padding. williamr@4: * It is intended for use with RSA encryption/decryption. williamr@4: * williamr@4: */ williamr@4: class CPaddingPKCS1Encryption : public CPadding williamr@4: { williamr@4: public: williamr@4: /** williamr@4: * Creates a new CPaddingPKCS1Encryption object. williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: * @return A pointer to the new CPaddingPKCS1Encryption object. williamr@4: */ williamr@4: IMPORT_C static CPaddingPKCS1Encryption* NewL(TInt aBlockBytes); williamr@4: williamr@4: /** williamr@4: * Creates a new CPaddingPKCS1Encryption object and leaves a pointer to it on the williamr@4: * cleanup stack. williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: * @return A pointer to the new CPaddingPKCS1Encryption object. williamr@4: */ williamr@4: IMPORT_C static CPaddingPKCS1Encryption* NewLC(TInt aBlockBytes); williamr@4: void UnPadL(const TDesC8& aInput,TDes8& aOutput); williamr@4: TInt MinPaddingLength(void) const; williamr@4: protected: williamr@4: /** williamr@4: * Constructor williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: */ williamr@4: IMPORT_C CPaddingPKCS1Encryption(TInt aBlockBytes); williamr@4: private: williamr@4: CPaddingPKCS1Encryption(void); williamr@4: CPaddingPKCS1Encryption(const CPaddingPKCS1Encryption&); williamr@4: CPaddingPKCS1Encryption& operator=(const CPaddingPKCS1Encryption&); williamr@4: void DoPadL(const TDesC8& aInput,TDes8& aOutput); williamr@4: }; williamr@4: williamr@4: /** williamr@4: * This concrete subclass of CPadding implements padding according to williamr@4: * the SSLv3/TLS standard. williamr@4: * williamr@4: * The SSL 3.0 spec does not specifiy the padding bytes to be used - it is williamr@4: * assumed to be arbitrary (and the openssl implementation uses non-zero random williamr@4: * data). The TLS spec however states that padding bytes should be the length williamr@4: * of the padding - 1. This class implements the latter when padding, but does williamr@4: * not check the padding byes when unpadding, so as to be interoperable with SSL williamr@4: * 3.0. williamr@4: * williamr@4: */ williamr@4: class CPaddingSSLv3 : public CPadding williamr@4: { williamr@4: public: williamr@4: /** williamr@4: * Creates a new CPaddingSSLv3 object. williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: * @return A pointer to the new CPaddingSSLv3 object. williamr@4: */ williamr@4: IMPORT_C static CPaddingSSLv3* NewL(TInt aBlockBytes); williamr@4: williamr@4: /** williamr@4: * Creates a new CPaddingSSLv3 object and leaves a pointer to it on the cleanup stack. williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: * @return A pointer to the new CPaddingSSLv3 object. williamr@4: */ williamr@4: IMPORT_C static CPaddingSSLv3* NewLC(TInt aBlockBytes); williamr@4: void UnPadL(const TDesC8& aInput,TDes8& aOutput); williamr@4: TInt MinPaddingLength(void) const; williamr@4: TInt MaxPaddedLength(TInt aInputBytes) const; williamr@4: williamr@4: protected: williamr@4: /** williamr@4: * Constructor williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: */ williamr@4: IMPORT_C CPaddingSSLv3(TInt aBlockBytes); williamr@4: private: williamr@4: CPaddingSSLv3(void); williamr@4: CPaddingSSLv3(const CPaddingSSLv3&); williamr@4: CPaddingSSLv3& operator=(const CPaddingSSLv3&); williamr@4: void DoPadL(const TDesC8& aInput,TDes8& aOutput); williamr@4: }; williamr@4: williamr@4: /** williamr@4: * This concrete subclass of CPadding implements padding according to williamr@4: * the PKCS#7/TLS standard. williamr@4: * williamr@4: */ williamr@4: class CPaddingPKCS7 : public CPadding williamr@4: { williamr@4: public: williamr@4: /** williamr@4: * Creates a new CPaddingPKCS7 object. williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: * @return A pointer to the new CPaddingPKCS7 object. williamr@4: */ williamr@4: IMPORT_C static CPaddingPKCS7* NewL(TInt aBlockBytes); williamr@4: williamr@4: /** williamr@4: * Creates a new CPaddingPKCS7 object and leaves a pointer to it on the cleanup stack. williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: * @return A pointer to the new CPaddingPKCS7 object. williamr@4: */ williamr@4: IMPORT_C static CPaddingPKCS7* NewLC(TInt aBlockBytes); williamr@4: void UnPadL(const TDesC8& aInput,TDes8& aOutput); williamr@4: TInt MinPaddingLength(void) const; williamr@4: TInt MaxPaddedLength(TInt aInputBytes) const; williamr@4: williamr@4: protected: williamr@4: /** williamr@4: * Constructor williamr@4: * williamr@4: * @param aBlockBytes The block size in bytes. williamr@4: */ williamr@4: IMPORT_C CPaddingPKCS7(TInt aBlockBytes); williamr@4: private: williamr@4: CPaddingPKCS7(void); williamr@4: CPaddingPKCS7(const CPaddingPKCS7&); williamr@4: CPaddingPKCS7& operator=(const CPaddingPKCS7&); williamr@4: void DoPadL(const TDesC8& aInput,TDes8& aOutput); williamr@4: }; williamr@4: williamr@4: #endif