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