|
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 "3desimpl.h" |
|
20 |
|
21 #include "destables.h" |
|
22 #include "common/inlines.h" |
|
23 #include "des.inl" |
|
24 #include "pluginconfig.h" |
|
25 #include "symmetriccipherimpl.h" |
|
26 #include <cryptostrength.h> |
|
27 |
|
28 using namespace SoftwareCrypto; |
|
29 |
|
30 /* C3DesImpl */ |
|
31 C3DesImpl::C3DesImpl( |
|
32 TUid aCryptoMode, |
|
33 TUid aOperationMode, |
|
34 TUid aPadding) : |
|
35 CDesImpl(KDesBlockBytes, aCryptoMode, aOperationMode, aPadding) |
|
36 { |
|
37 } |
|
38 |
|
39 C3DesImpl* C3DesImpl::NewL(const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding) |
|
40 { |
|
41 C3DesImpl* self = C3DesImpl::NewLC(aKey, aCryptoMode, aOperationMode, aPadding); |
|
42 CleanupStack::Pop(self); |
|
43 return self; |
|
44 } |
|
45 |
|
46 C3DesImpl* C3DesImpl::NewLC(const CKey& aKey, TUid aCryptoMode, TUid aOperationMode, TUid aPadding) |
|
47 { |
|
48 C3DesImpl* self = new(ELeave) C3DesImpl(aCryptoMode, aOperationMode, aPadding); |
|
49 CleanupStack::PushL(self); |
|
50 self->ConstructL(aKey); |
|
51 |
|
52 const TDesC8& keyContent = aKey.GetTDesC8L(KSymmetricKeyParameterUid); |
|
53 TCrypto::IsSymmetricWeakEnoughL(BytesToBits(keyContent.Size()) - keyContent.Size()); |
|
54 return self; |
|
55 } |
|
56 |
|
57 C3DesImpl::~C3DesImpl() |
|
58 { |
|
59 // make sure key information isn't visible to other processes if the |
|
60 // page is reused. |
|
61 Mem::FillZ(&iK1, sizeof(iK1)); |
|
62 Mem::FillZ(&iK2, sizeof(iK2)); |
|
63 Mem::FillZ(&iK3, sizeof(iK3)); |
|
64 } |
|
65 |
|
66 void C3DesImpl::ConstructL(const CKey& aKey) |
|
67 { |
|
68 CDesImpl::ConstructL(aKey); |
|
69 SetKeySchedule(); |
|
70 } |
|
71 |
|
72 CExtendedCharacteristics* C3DesImpl::CreateExtendedCharacteristicsL() |
|
73 { |
|
74 // All Symbian software plug-ins have unlimited concurrency, cannot be reserved |
|
75 // for exclusive use and are not CERTIFIED to be standards compliant. |
|
76 return CExtendedCharacteristics::NewL(KMaxTInt, EFalse); |
|
77 } |
|
78 |
|
79 const CExtendedCharacteristics* C3DesImpl::GetExtendedCharacteristicsL() |
|
80 { |
|
81 return C3DesImpl::CreateExtendedCharacteristicsL(); |
|
82 } |
|
83 |
|
84 TUid C3DesImpl::ImplementationUid() const |
|
85 { |
|
86 return KCryptoPlugin3DesUid; |
|
87 } |
|
88 |
|
89 TBool C3DesImpl::IsValidKeyLength(TInt aKeyBytes) const |
|
90 { |
|
91 return (aKeyBytes == K3DesKeyBytes); |
|
92 } |
|
93 |
|
94 TInt C3DesImpl::GetKeyStrength() const |
|
95 { |
|
96 // Exclude parity bits from each subkey |
|
97 return BytesToBits(K3DesKeyBytes - (3 * 8)); |
|
98 } |
|
99 |
|
100 void C3DesImpl::TransformEncrypt( |
|
101 TUint8* aBuffer, |
|
102 TUint aNumBlocks) |
|
103 { |
|
104 for (TInt i = 0; i < aNumBlocks; ++i) |
|
105 { |
|
106 ModeEncryptStart(aBuffer); |
|
107 |
|
108 TUint32 l, r; |
|
109 // Split the block into 2 word-sized big endian portions |
|
110 GetBlockBigEndian(aBuffer, l, r); |
|
111 |
|
112 IPerm(l,r); |
|
113 // The mode is applied to the entire operation and NOT |
|
114 // for each DES transform |
|
115 TUid opMode = iOperationMode; |
|
116 iOperationMode = KOperationModeECBUid; |
|
117 DoTransform(l, r, iK1); |
|
118 DoTransform(r, l, iK2); |
|
119 DoTransform(l, r, iK3); |
|
120 iOperationMode = opMode; |
|
121 FPerm(l,r); |
|
122 |
|
123 // Put the portions back into the block as little endian |
|
124 PutBlockBigEndian(aBuffer, r, l); |
|
125 ModeEncryptEnd(aBuffer); |
|
126 aBuffer += KDesBlockBytes; |
|
127 } |
|
128 } |
|
129 |
|
130 void C3DesImpl::TransformDecrypt( |
|
131 TUint8* aBuffer, |
|
132 const TUint aNumBlocks) |
|
133 { |
|
134 for (TInt i = 0; i < aNumBlocks; ++i) |
|
135 { |
|
136 ModeDecryptStart(aBuffer); |
|
137 |
|
138 TUint32 l, r; |
|
139 // Split the block into 2 word-sized big endian portions |
|
140 GetBlockBigEndian(aBuffer, l, r); |
|
141 |
|
142 IPerm(l,r); |
|
143 |
|
144 // The mode is applied to the entire operation and NOT |
|
145 // for each DES transform |
|
146 TUid opMode = iOperationMode; |
|
147 iOperationMode = KOperationModeECBUid; |
|
148 DoTransform(l, r, iK1); |
|
149 DoTransform(r, l, iK2); |
|
150 DoTransform(l, r, iK3); |
|
151 iOperationMode = opMode; |
|
152 FPerm(l,r); |
|
153 |
|
154 // Put the portions back into the block as little endian |
|
155 PutBlockBigEndian(aBuffer, r, l); |
|
156 ModeDecryptEnd(aBuffer); |
|
157 aBuffer += K3DesBlockBytes; |
|
158 } |
|
159 } |
|
160 |
|
161 void C3DesImpl::SetKeySchedule() |
|
162 { |
|
163 if (iCryptoMode.iUid == KCryptoModeEncrypt) |
|
164 { |
|
165 // Encrypt -> Decrypt -> Encrypt |
|
166 // Encryptor key |
|
167 SetEncryptKeySchedule(iKey->Mid(0, KDesKeyBytes), iK1); |
|
168 |
|
169 // Decryptor key |
|
170 SetDecryptKeySchedule(iKey->Mid(KDesKeyBytes, 2 * KDesKeyBytes), iK2); |
|
171 |
|
172 // Encryptor key |
|
173 SetEncryptKeySchedule(iKey->Mid(2 * KDesKeyBytes), iK3); |
|
174 } |
|
175 else |
|
176 { |
|
177 // Decrypt -> Encrypt -> Decrypt |
|
178 // Key order is reversed ! |
|
179 |
|
180 ASSERT(iCryptoMode.iUid == KCryptoModeDecrypt); |
|
181 // Decryptor key |
|
182 SetDecryptKeySchedule(iKey->Mid(0, KDesKeyBytes), iK3); |
|
183 |
|
184 // Encryptor key |
|
185 SetEncryptKeySchedule(iKey->Mid(KDesKeyBytes, 2 * KDesKeyBytes), iK2); |
|
186 |
|
187 // Decryptor key |
|
188 SetDecryptKeySchedule(iKey->Mid(2 * KDesKeyBytes), iK1); |
|
189 } |
|
190 } |