author | sl |
Tue, 10 Jun 2014 14:32:02 +0200 | |
changeset 1 | 260cb5ec6c19 |
permissions | -rw-r--r-- |
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 |
* |
sl@0 | 16 |
*/ |
sl@0 | 17 |
|
sl@0 | 18 |
|
sl@0 | 19 |
#include "arc4.h" |
sl@0 | 20 |
#include "../common/inlines.h" |
sl@0 | 21 |
#include <e32base.h> |
sl@0 | 22 |
#include <cryptostrength.h> |
sl@0 | 23 |
|
sl@0 | 24 |
#ifdef SYMBIAN_ENABLE_SPLIT_HEADERS |
sl@0 | 25 |
/** The size of the substitution box (i.e. lookup table) in bytes. */ |
sl@0 | 26 |
const TInt KSBoxSize = 256; |
sl@0 | 27 |
#endif |
sl@0 | 28 |
|
sl@0 | 29 |
inline TUint8 CARC4::GenerateByte() |
sl@0 | 30 |
{ |
sl@0 | 31 |
TUint8 a = iState[ix]; |
sl@0 | 32 |
iy = (TUint8)((iy + a) & 0xff); |
sl@0 | 33 |
TUint8 b = iState[iy]; |
sl@0 | 34 |
|
sl@0 | 35 |
iState[ix] = b; |
sl@0 | 36 |
iState[iy] = a; |
sl@0 | 37 |
ix = (TUint8)((ix + 1) & 0xff); |
sl@0 | 38 |
return (iState[(a + b) & 0xff]); |
sl@0 | 39 |
} |
sl@0 | 40 |
|
sl@0 | 41 |
CARC4::CARC4(const TDesC8& aKey, TUint aDiscardBytes) |
sl@0 | 42 |
: ix(1), iy(0), iDiscardBytes(aDiscardBytes) |
sl@0 | 43 |
{ |
sl@0 | 44 |
iKey.Copy(aKey); |
sl@0 | 45 |
GenerateSBox(); |
sl@0 | 46 |
} |
sl@0 | 47 |
|
sl@0 | 48 |
EXPORT_C CARC4* CARC4::NewL(const TDesC8& aKey, TUint aDiscardBytes) |
sl@0 | 49 |
{ |
sl@0 | 50 |
CARC4* self = NewLC(aKey, aDiscardBytes); |
sl@0 | 51 |
CleanupStack::Pop(self); |
sl@0 | 52 |
return self; |
sl@0 | 53 |
} |
sl@0 | 54 |
|
sl@0 | 55 |
EXPORT_C CARC4* CARC4::NewLC(const TDesC8& aKey, TUint aDiscardBytes) |
sl@0 | 56 |
{ |
sl@0 | 57 |
CARC4* self = new(ELeave)CARC4(aKey, aDiscardBytes); |
sl@0 | 58 |
CleanupStack::PushL(self); |
sl@0 | 59 |
TCrypto::IsSymmetricWeakEnoughL(BytesToBits(aKey.Size())); |
sl@0 | 60 |
return self; |
sl@0 | 61 |
} |
sl@0 | 62 |
|
sl@0 | 63 |
void CARC4::DoProcess(TDes8& aData) |
sl@0 | 64 |
{ |
sl@0 | 65 |
TInt blockLen = aData.Size(); |
sl@0 | 66 |
|
sl@0 | 67 |
if (blockLen > 0) |
sl@0 | 68 |
{ |
sl@0 | 69 |
TUint8* blockPtr = (TUint8*)&aData[0]; |
sl@0 | 70 |
do |
sl@0 | 71 |
{ |
sl@0 | 72 |
*blockPtr++ ^= GenerateByte(); |
sl@0 | 73 |
} |
sl@0 | 74 |
while (--blockLen); |
sl@0 | 75 |
} |
sl@0 | 76 |
} |
sl@0 | 77 |
|
sl@0 | 78 |
void CARC4::Reset() |
sl@0 | 79 |
{ |
sl@0 | 80 |
ix = 1; |
sl@0 | 81 |
iy = 0; |
sl@0 | 82 |
GenerateSBox(); |
sl@0 | 83 |
} |
sl@0 | 84 |
|
sl@0 | 85 |
TInt CARC4::KeySize() const |
sl@0 | 86 |
{ |
sl@0 | 87 |
return (iKey.Size()); |
sl@0 | 88 |
} |
sl@0 | 89 |
|
sl@0 | 90 |
void CARC4::DiscardBytes(TInt aDiscardBytes) |
sl@0 | 91 |
{ |
sl@0 | 92 |
if (aDiscardBytes > 0) |
sl@0 | 93 |
{ |
sl@0 | 94 |
do |
sl@0 | 95 |
{ |
sl@0 | 96 |
GenerateByte(); |
sl@0 | 97 |
} |
sl@0 | 98 |
while(--aDiscardBytes); |
sl@0 | 99 |
} |
sl@0 | 100 |
} |
sl@0 | 101 |
|
sl@0 | 102 |
void CARC4::GenerateSBox(void) |
sl@0 | 103 |
{ |
sl@0 | 104 |
TUint keyBytes = iKey.Size(); |
sl@0 | 105 |
|
sl@0 | 106 |
TInt i = 0; |
sl@0 | 107 |
for (; i < KSBoxSize; i++) |
sl@0 | 108 |
iState[i] = (TUint8)i; |
sl@0 | 109 |
|
sl@0 | 110 |
TUint keyIndex = 0, stateIndex = 0; |
sl@0 | 111 |
i = 0; |
sl@0 | 112 |
for (; i < KSBoxSize; i++) |
sl@0 | 113 |
{ |
sl@0 | 114 |
TUint a = iState[i]; |
sl@0 | 115 |
stateIndex += iKey[keyIndex] + a; |
sl@0 | 116 |
stateIndex &= 0xff; |
sl@0 | 117 |
iState[i] = iState[stateIndex]; |
sl@0 | 118 |
iState[stateIndex] = (TUint8)a; |
sl@0 | 119 |
if (++keyIndex >= (TUint)keyBytes) |
sl@0 | 120 |
keyIndex = 0; |
sl@0 | 121 |
} |
sl@0 | 122 |
|
sl@0 | 123 |
DiscardBytes(iDiscardBytes); |
sl@0 | 124 |
} |