|
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 "bufferedtransformation.h" |
|
20 #include "blocktransformation.h" |
|
21 #include "padding.h" |
|
22 #include "../common/inlines.h" |
|
23 #include <cryptopanic.h> |
|
24 |
|
25 EXPORT_C CBufferedTransformation::~CBufferedTransformation() |
|
26 { |
|
27 delete iBT; |
|
28 delete iPadding; |
|
29 delete iInputStoreBuf; |
|
30 } |
|
31 |
|
32 void CBufferedTransformation::Process(const TDesC8& aInput, TDes8& aOutput) |
|
33 { |
|
34 __ASSERT_DEBUG(aOutput.MaxLength() >= MaxOutputLength(aInput.Length()), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow)); |
|
35 |
|
36 TInt blockSize = iBT->BlockSize(); |
|
37 |
|
38 if ( (aInput.Size() + iInputStore.Size()) < blockSize ) |
|
39 { |
|
40 iInputStore.Append(aInput); |
|
41 } |
|
42 else |
|
43 { |
|
44 TInt outputIndex = aOutput.Size(); |
|
45 aOutput.Append(iInputStore); |
|
46 |
|
47 TInt inputIndex = blockSize - iInputStore.Size(); |
|
48 aOutput.Append(aInput.Mid(0, inputIndex)); |
|
49 |
|
50 TPtr8 transformBuf((TUint8*)(aOutput.Ptr()) + outputIndex, blockSize, |
|
51 blockSize); |
|
52 //This should read: |
|
53 //TPtr8 transformBuf(aOutput.Mid(outputIndex, blockSize)); |
|
54 //but in the wonderful world of descriptors, Mid returns a TPtrC8 even |
|
55 //when called on a TPtr8. Fantastic eh? |
|
56 iBT->Transform(transformBuf); |
|
57 |
|
58 outputIndex += blockSize; |
|
59 |
|
60 TInt len = aInput.Size() - blockSize; |
|
61 |
|
62 for (; inputIndex<=len; inputIndex+=blockSize) |
|
63 { |
|
64 aOutput.Append(aInput.Mid(inputIndex, blockSize)); |
|
65 transformBuf.Set((TUint8*)(aOutput.Ptr()) + outputIndex, blockSize, |
|
66 blockSize); |
|
67 iBT->Transform(transformBuf); |
|
68 outputIndex += blockSize; |
|
69 } |
|
70 |
|
71 iInputStore.Zero(); |
|
72 if (inputIndex < aInput.Size()) |
|
73 iInputStore.Append(aInput.Mid(inputIndex)); |
|
74 } |
|
75 } |
|
76 |
|
77 TInt CBufferedTransformation::MaxOutputLength(TInt aInputLength) const |
|
78 { |
|
79 TInt rem = (aInputLength + iInputStore.Size()) % (iBT->BlockSize()); |
|
80 return ((aInputLength + iInputStore.Size()) - rem); |
|
81 } |
|
82 |
|
83 void CBufferedTransformation::Reset() |
|
84 { |
|
85 iBT->Reset(); |
|
86 iInputStore.Zero(); |
|
87 } |
|
88 |
|
89 TInt CBufferedTransformation::BlockSize() const |
|
90 { |
|
91 return (iBT->BlockSize()); |
|
92 } |
|
93 |
|
94 TInt CBufferedTransformation::KeySize() const |
|
95 { |
|
96 return (iBT->KeySize()); |
|
97 } |
|
98 |
|
99 EXPORT_C CBlockTransformation* CBufferedTransformation::BlockTransformer() const |
|
100 { |
|
101 return (iBT); |
|
102 } |
|
103 |
|
104 CBufferedTransformation::CBufferedTransformation() |
|
105 : iInputStore(0,0,0) |
|
106 { |
|
107 } |
|
108 |
|
109 void CBufferedTransformation::ConstructL(CBlockTransformation* aBT, CPadding* aPadding) |
|
110 { |
|
111 iInputStoreBuf = HBufC8::NewL(aBT->BlockSize()); |
|
112 iInputStore.Set(iInputStoreBuf->Des()); |
|
113 |
|
114 // Take ownership last - doesn't take ownership if we leave |
|
115 iBT = aBT; |
|
116 iPadding = aPadding; |
|
117 } |
|
118 |
|
119 |
|
120 // CBufferedEncryptor |
|
121 |
|
122 EXPORT_C CBufferedEncryptor* CBufferedEncryptor::NewL( |
|
123 CBlockTransformation* aBT, CPadding* aPadding) |
|
124 { |
|
125 CBufferedEncryptor* self = NewLC(aBT,aPadding); |
|
126 CleanupStack::Pop(self); |
|
127 return self; |
|
128 } |
|
129 |
|
130 EXPORT_C CBufferedEncryptor* CBufferedEncryptor::NewLC( |
|
131 CBlockTransformation* aBT, CPadding* aPadding) |
|
132 { |
|
133 CBufferedEncryptor* self = new (ELeave) CBufferedEncryptor(); |
|
134 CleanupStack::PushL(self); |
|
135 self->ConstructL(aBT, aPadding); |
|
136 return self; |
|
137 } |
|
138 |
|
139 CBufferedEncryptor::CBufferedEncryptor() |
|
140 { |
|
141 } |
|
142 |
|
143 void CBufferedEncryptor::ProcessFinalL(const TDesC8& aInput, TDes8& aOutput) |
|
144 { |
|
145 __ASSERT_DEBUG(aOutput.MaxLength() >= MaxFinalOutputLength(aInput.Length()), User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow)); |
|
146 Process(aInput, aOutput); |
|
147 |
|
148 TInt outputIndex = aOutput.Size(); |
|
149 iPadding->PadL(iInputStore, aOutput); |
|
150 assert(aOutput.Size() % iBT->BlockSize() == 0); |
|
151 |
|
152 TUint blockSize = iBT->BlockSize(); |
|
153 TInt len = aOutput.Size() - outputIndex; |
|
154 |
|
155 for(TInt i=len; i>0; i-=blockSize) |
|
156 { |
|
157 TPtr8 transformBuf((TUint8*)(aOutput.Ptr()) + outputIndex, blockSize, |
|
158 blockSize); |
|
159 iBT->Transform(transformBuf); |
|
160 outputIndex+=blockSize; |
|
161 } |
|
162 |
|
163 iInputStore.Zero(); |
|
164 } |
|
165 |
|
166 TInt CBufferedEncryptor::MaxFinalOutputLength(TInt aInputLength) const |
|
167 { |
|
168 return iPadding->MaxPaddedLength(iInputStore.Size() + aInputLength); |
|
169 } |
|
170 |
|
171 // CBufferedDecryptor |
|
172 |
|
173 EXPORT_C CBufferedDecryptor* CBufferedDecryptor::NewL( |
|
174 CBlockTransformation* aBT, CPadding* aPadding) |
|
175 { |
|
176 CBufferedDecryptor* self = NewLC(aBT,aPadding); |
|
177 CleanupStack::Pop(self); |
|
178 return self; |
|
179 } |
|
180 |
|
181 EXPORT_C CBufferedDecryptor* CBufferedDecryptor::NewLC( |
|
182 CBlockTransformation* aBT, CPadding* aPadding) |
|
183 { |
|
184 CBufferedDecryptor* self = new (ELeave) CBufferedDecryptor(); |
|
185 CleanupStack::PushL(self); |
|
186 self->ConstructL(aBT, aPadding); |
|
187 return self; |
|
188 } |
|
189 |
|
190 CBufferedDecryptor::CBufferedDecryptor() |
|
191 { |
|
192 } |
|
193 |
|
194 void CBufferedDecryptor::ProcessFinalL(const TDesC8& aInput, TDes8& aOutput) |
|
195 { |
|
196 TInt inputMax = MaxFinalOutputLength(aInput.Length()); |
|
197 |
|
198 __ASSERT_DEBUG(aOutput.MaxLength() >= inputMax, User::Panic(KCryptoPanic, ECryptoPanicOutputDescriptorOverflow)); |
|
199 |
|
200 assert((aInput.Size() + iInputStore.Size()) % iPadding->BlockSize()==0); |
|
201 assert(aInput.Size() + iInputStore.Size() !=0 ); |
|
202 assert(iPadding->BlockSize() % BlockSize() == 0); |
|
203 |
|
204 // Decrypt aInput into processBuf |
|
205 // If this panics with descriptor problems, you've probably called |
|
206 // ProcessFinalL with a non-_padding_ blocksized aligned amount of data. |
|
207 HBufC8* processBuf = HBufC8::NewLC(inputMax); |
|
208 TPtr8 processPtr = processBuf->Des(); |
|
209 |
|
210 Process(aInput, processPtr); |
|
211 assert(iInputStore.Size()==0); |
|
212 |
|
213 // Unpad processPtr into aOutput |
|
214 iPadding->UnPadL(processPtr, aOutput); |
|
215 |
|
216 CleanupStack::PopAndDestroy(processBuf); |
|
217 } |
|
218 |
|
219 TInt CBufferedDecryptor::MaxFinalOutputLength(TInt aInputLength) const |
|
220 { |
|
221 return iPadding->MaxUnPaddedLength(aInputLength + iInputStore.Size()); |
|
222 } |