17
|
1 |
/*
|
|
2 |
* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
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".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
#include "arc4impl.h"
|
|
20 |
#include "keys.h"
|
|
21 |
#include "pluginconfig.h"
|
|
22 |
#include "symmetriccipherimpl.h"
|
|
23 |
#include <cryptostrength.h>
|
|
24 |
#include "common/inlines.h"
|
|
25 |
|
|
26 |
|
|
27 |
using namespace SoftwareCrypto;
|
|
28 |
|
|
29 |
CArc4Impl* CArc4Impl::NewL(const CKey& aKey, TInt aDiscardBytes)
|
|
30 |
{
|
|
31 |
CArc4Impl* self = CArc4Impl::NewLC(aKey, aDiscardBytes);
|
|
32 |
CleanupStack::Pop(self);
|
|
33 |
return self;
|
|
34 |
}
|
|
35 |
|
|
36 |
CArc4Impl* CArc4Impl::NewLC(const CKey& aKey, TInt aDiscardBytes)
|
|
37 |
{
|
|
38 |
CArc4Impl* self = new(ELeave) CArc4Impl(aDiscardBytes);
|
|
39 |
CleanupStack::PushL(self);
|
|
40 |
self->ConstructL(aKey);
|
|
41 |
|
|
42 |
const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid);
|
|
43 |
TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size());
|
|
44 |
|
|
45 |
return self;
|
|
46 |
}
|
|
47 |
|
|
48 |
CArc4Impl::CArc4Impl(TInt aDiscardBytes)
|
|
49 |
:ix(1), iy(0), iDiscardBytes(aDiscardBytes)
|
|
50 |
{
|
|
51 |
}
|
|
52 |
|
|
53 |
CArc4Impl::~CArc4Impl()
|
|
54 |
{
|
|
55 |
}
|
|
56 |
|
|
57 |
void CArc4Impl::ConstructL(const CKey& aKey)
|
|
58 |
{
|
|
59 |
CSymmetricStreamCipherImpl::ConstructL(aKey);
|
|
60 |
GenerateSBox();
|
|
61 |
}
|
|
62 |
|
|
63 |
CExtendedCharacteristics* CArc4Impl::CreateExtendedCharacteristicsL()
|
|
64 |
{
|
|
65 |
// All Symbian software plug-ins have unlimited concurrency, cannot be reserved
|
|
66 |
// for exclusive use and are not CERTIFIED to be standards compliant.
|
|
67 |
return CExtendedCharacteristics::NewL(KMaxTInt, EFalse);
|
|
68 |
}
|
|
69 |
|
|
70 |
const CExtendedCharacteristics* CArc4Impl::GetExtendedCharacteristicsL()
|
|
71 |
{
|
|
72 |
return CArc4Impl::CreateExtendedCharacteristicsL();
|
|
73 |
}
|
|
74 |
|
|
75 |
void CArc4Impl::DoProcess(TDes8& aData)
|
|
76 |
{
|
|
77 |
TInt blockLen = aData.Size();
|
|
78 |
|
|
79 |
if (blockLen > 0)
|
|
80 |
{
|
|
81 |
TUint8* blockPtr = (TUint8*)&aData[0];
|
|
82 |
do
|
|
83 |
{
|
|
84 |
*blockPtr++ ^= GenerateByte();
|
|
85 |
}
|
|
86 |
while (--blockLen);
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
TBool CArc4Impl::IsValidKeyLength(TInt aKeyBytes) const
|
|
91 |
{
|
|
92 |
return ((aKeyBytes > 0 && aKeyBytes <= KMaxARC4KeyBytes) ? ETrue : EFalse);
|
|
93 |
}
|
|
94 |
|
|
95 |
void CArc4Impl::Reset()
|
|
96 |
{
|
|
97 |
ix = 1;
|
|
98 |
iy = 0;
|
|
99 |
GenerateSBox();
|
|
100 |
}
|
|
101 |
|
|
102 |
TUid CArc4Impl::ImplementationUid() const
|
|
103 |
{
|
|
104 |
return KCryptoPluginArc4Uid;
|
|
105 |
}
|
|
106 |
|
|
107 |
TUint8 CArc4Impl::GenerateByte()
|
|
108 |
{
|
|
109 |
TUint8 a = iState[ix];
|
|
110 |
iy = (TUint8)((iy + a) & 0xff);
|
|
111 |
TUint8 b = iState[iy];
|
|
112 |
|
|
113 |
iState[ix] = b;
|
|
114 |
iState[iy] = a;
|
|
115 |
ix = (TUint8)((ix + 1) & 0xff);
|
|
116 |
return (iState[(a + b) & 0xff]);
|
|
117 |
}
|
|
118 |
|
|
119 |
void CArc4Impl::DiscardBytes(TInt aDiscardBytes)
|
|
120 |
{
|
|
121 |
if (aDiscardBytes > 0)
|
|
122 |
{
|
|
123 |
do
|
|
124 |
{
|
|
125 |
GenerateByte();
|
|
126 |
}
|
|
127 |
while(--aDiscardBytes);
|
|
128 |
}
|
|
129 |
}
|
|
130 |
|
|
131 |
void CArc4Impl::GenerateSBox(void)
|
|
132 |
{
|
|
133 |
TUint keyBytes = iKey->Size();
|
|
134 |
|
|
135 |
TInt i = 0;
|
|
136 |
for (; i < KSBoxSize; i++)
|
|
137 |
iState[i] = (TUint8)i;
|
|
138 |
|
|
139 |
TUint keyIndex = 0, stateIndex = 0;
|
|
140 |
i = 0;
|
|
141 |
for (; i < KSBoxSize; i++)
|
|
142 |
{
|
|
143 |
TUint a = iState[i];
|
|
144 |
stateIndex += (*iKey)[keyIndex] + a;
|
|
145 |
stateIndex &= 0xff;
|
|
146 |
iState[i] = iState[stateIndex];
|
|
147 |
iState[stateIndex] = (TUint8)a;
|
|
148 |
if (++keyIndex >= (TUint)keyBytes)
|
|
149 |
keyIndex = 0;
|
|
150 |
}
|
|
151 |
|
|
152 |
DiscardBytes(iDiscardBytes);
|
|
153 |
}
|