os/security/crypto/weakcrypto/inc/arc4.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.
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 ** PublishedPartner 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
* RC4 implementation
sl@0
    18
*
sl@0
    19
*/
sl@0
    20
sl@0
    21
sl@0
    22
/**
sl@0
    23
 @file 
sl@0
    24
 @publishedPartner
sl@0
    25
 @released 
sl@0
    26
*/
sl@0
    27
 
sl@0
    28
#ifndef __ARC4_H__
sl@0
    29
#define __ARC4_H__
sl@0
    30
sl@0
    31
#include "streamcipher.h"
sl@0
    32
sl@0
    33
/** The size of the substitution box (i.e. lookup table) in bytes. */
sl@0
    34
const TInt KSBoxSize = 256;
sl@0
    35
sl@0
    36
/** Maximum ARC4 key size in bytes. */
sl@0
    37
const TInt KMaxARC4KeyBytes = 256; //2048 bits
sl@0
    38
sl@0
    39
/** Number of bytes to discard by default from an ARC4 key stream. */
sl@0
    40
const TUint KDefaultDiscardBytes = 768;
sl@0
    41
sl@0
    42
/**
sl@0
    43
* Implements an RC4-compatible stream cipher that outputs a pseudorandom stream
sl@0
    44
* of bits, having been initialised with a key. 
sl@0
    45
*
sl@0
    46
*/
sl@0
    47
class CARC4 : public CStreamCipher
sl@0
    48
{
sl@0
    49
public:
sl@0
    50
	/**
sl@0
    51
	* Constructs an instance of a CARC4 object, and initialises it with a key and
sl@0
    52
	* (optionally) the number of initial bytes to discard. Defaults to 256. 
sl@0
    53
	*
sl@0
    54
	* The number of dropped bytes <b>must</b> be agreed with the other
sl@0
    55
	* party, with which information is to be exchanged, prior to encipherment.
sl@0
    56
	*
sl@0
    57
	* @note	Several papers have been published indicating that there are weaknesses 
sl@0
    58
	*		in the first bytes of an ARC4 byte stream.  A search for "ARC4
sl@0
    59
	*		discard" should find these papers.  Recommended practice is to drop the first
sl@0
    60
	*		KDefaultDiscardBytes bytes of the key stream.  
sl@0
    61
	*
sl@0
    62
	* @param aKey			The key to use.  aKey must be less than or equal to
sl@0
    63
	*						KRC4MaxKeySizeBytes.  
sl@0
    64
	* @param aDiscardBytes	The number of bytes to drop from the beginning of the key
sl@0
    65
	*						stream.
sl@0
    66
	* @return				A pointer to the new CARC4 object.
sl@0
    67
	*  
sl@0
    68
	* @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
sl@0
    69
	*								cipher strength restrictions of the crypto library.
sl@0
    70
	*								See TCrypto::IsSymmetricWeakEnoughL()
sl@0
    71
	*/
sl@0
    72
	IMPORT_C static CARC4* NewL(const TDesC8& aKey, 
sl@0
    73
		TUint aDiscardBytes = KDefaultDiscardBytes);
sl@0
    74
sl@0
    75
	/**
sl@0
    76
	* Constructs an instance of a CARC4 object, and initialises it with a key and
sl@0
    77
	* (optionally) the number of initial bytes to discard. Defaults to 256. 
sl@0
    78
	*
sl@0
    79
	* The number of dropped bytes <b>must</b> be agreed with the other
sl@0
    80
	* party, with which information is to be exchanged, prior to encipherment.
sl@0
    81
	*
sl@0
    82
	* @see CARC4::NewL()
sl@0
    83
	*
sl@0
    84
	* @param aKey			The key to use.  aKey must be less than or equal to
sl@0
    85
	*						KRC4MaxKeySizeBytes.  
sl@0
    86
	* @param aDiscardBytes	The number of bytes to drop from the beginning of the key
sl@0
    87
	*						stream.
sl@0
    88
	* @return				A pointer to the new CARC4 object.
sl@0
    89
	*  
sl@0
    90
	* @leave KErrKeyNotWeakEnough	If the key size is larger than that allowed by the
sl@0
    91
	*								cipher strength restrictions of the crypto library.
sl@0
    92
	*								See TCrypto::IsSymmetricWeakEnoughL()
sl@0
    93
	*/
sl@0
    94
	IMPORT_C static CARC4* NewLC(const TDesC8& aKey, 
sl@0
    95
		TUint aDiscardBytes = KDefaultDiscardBytes);
sl@0
    96
public:	
sl@0
    97
	virtual void Reset(void);
sl@0
    98
	virtual TInt KeySize(void) const;
sl@0
    99
protected:
sl@0
   100
	/**	
sl@0
   101
	 * Performs an ARC4 encryption or decryption on supplied data.
sl@0
   102
	 * 
sl@0
   103
	 * @note ARC4 encryption and decryption are symmetrical.
sl@0
   104
	 *
sl@0
   105
	 * @param aData	On input, data to be transformed; 
sl@0
   106
	 *				on return, transformed data.
sl@0
   107
	 */
sl@0
   108
	virtual void DoProcess(TDes8& aData);
sl@0
   109
private:
sl@0
   110
	CARC4(const TDesC8& aKey, TUint aDiscardBytes);
sl@0
   111
	void GenerateSBox();
sl@0
   112
	inline TUint8 GenerateByte();
sl@0
   113
	void DiscardBytes(TInt aDiscardBytes);
sl@0
   114
private:
sl@0
   115
	TUint8 ix;
sl@0
   116
	TUint8 iy;
sl@0
   117
	TInt iDiscardBytes;
sl@0
   118
	TUint8 iState[KSBoxSize];
sl@0
   119
	TBuf8<KMaxARC4KeyBytes> iKey;
sl@0
   120
};
sl@0
   121
sl@0
   122
#endif	//	__ARC4_H__