First public contribution.
2 * Copyright (c) 2002-2009 Nokia Corporation and/or its subsidiary(-ies).
4 * This component and the accompanying materials are made available
5 * under the terms of the License "Eclipse Public License v1.0"
6 * which accompanies this distribution, and is available
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * ** IMPORTANT ** PublishedPartner API's in this file are published to 3rd party developers via the
16 * Symbian website. Changes to these API's should be treated as PublishedAll API changes and the Security TA should be consulted.
31 #include "streamcipher.h"
33 /** The size of the substitution box (i.e. lookup table) in bytes. */
34 const TInt KSBoxSize = 256;
36 /** Maximum ARC4 key size in bytes. */
37 const TInt KMaxARC4KeyBytes = 256; //2048 bits
39 /** Number of bytes to discard by default from an ARC4 key stream. */
40 const TUint KDefaultDiscardBytes = 768;
43 * Implements an RC4-compatible stream cipher that outputs a pseudorandom stream
44 * of bits, having been initialised with a key.
47 class CARC4 : public CStreamCipher
51 * Constructs an instance of a CARC4 object, and initialises it with a key and
52 * (optionally) the number of initial bytes to discard. Defaults to 256.
54 * The number of dropped bytes <b>must</b> be agreed with the other
55 * party, with which information is to be exchanged, prior to encipherment.
57 * @note Several papers have been published indicating that there are weaknesses
58 * in the first bytes of an ARC4 byte stream. A search for "ARC4
59 * discard" should find these papers. Recommended practice is to drop the first
60 * KDefaultDiscardBytes bytes of the key stream.
62 * @param aKey The key to use. aKey must be less than or equal to
63 * KRC4MaxKeySizeBytes.
64 * @param aDiscardBytes The number of bytes to drop from the beginning of the key
66 * @return A pointer to the new CARC4 object.
68 * @leave KErrKeyNotWeakEnough If the key size is larger than that allowed by the
69 * cipher strength restrictions of the crypto library.
70 * See TCrypto::IsSymmetricWeakEnoughL()
72 IMPORT_C static CARC4* NewL(const TDesC8& aKey,
73 TUint aDiscardBytes = KDefaultDiscardBytes);
76 * Constructs an instance of a CARC4 object, and initialises it with a key and
77 * (optionally) the number of initial bytes to discard. Defaults to 256.
79 * The number of dropped bytes <b>must</b> be agreed with the other
80 * party, with which information is to be exchanged, prior to encipherment.
84 * @param aKey The key to use. aKey must be less than or equal to
85 * KRC4MaxKeySizeBytes.
86 * @param aDiscardBytes The number of bytes to drop from the beginning of the key
88 * @return A pointer to the new CARC4 object.
90 * @leave KErrKeyNotWeakEnough If the key size is larger than that allowed by the
91 * cipher strength restrictions of the crypto library.
92 * See TCrypto::IsSymmetricWeakEnoughL()
94 IMPORT_C static CARC4* NewLC(const TDesC8& aKey,
95 TUint aDiscardBytes = KDefaultDiscardBytes);
97 virtual void Reset(void);
98 virtual TInt KeySize(void) const;
101 * Performs an ARC4 encryption or decryption on supplied data.
103 * @note ARC4 encryption and decryption are symmetrical.
105 * @param aData On input, data to be transformed;
106 * on return, transformed data.
108 virtual void DoProcess(TDes8& aData);
110 CARC4(const TDesC8& aKey, TUint aDiscardBytes);
112 inline TUint8 GenerateByte();
113 void DiscardBytes(TInt aDiscardBytes);
118 TUint8 iState[KSBoxSize];
119 TBuf8<KMaxARC4KeyBytes> iKey;