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