|
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 <cbcmode.h> |
|
20 |
|
21 #include "cbcmodeshim.h" |
|
22 #include "../common/inlines.h" |
|
23 |
|
24 void CBlockChainingMode::Reset() |
|
25 { |
|
26 iRegister.Copy(iIV); |
|
27 iBT->Reset(); |
|
28 } |
|
29 |
|
30 TInt CBlockChainingMode::BlockSize() const |
|
31 { |
|
32 return (iBT->BlockSize()); |
|
33 } |
|
34 |
|
35 TInt CBlockChainingMode::KeySize() const |
|
36 { |
|
37 return (iBT->KeySize()); |
|
38 } |
|
39 |
|
40 void CBlockChainingMode::SetIV(const TDesC8& aIV) |
|
41 { |
|
42 //We are making the stipulation that anybody calling SetIV is not setting it |
|
43 //to a longer IV than they originally did. Otherwise SetIV needs to leave. |
|
44 assert(aIV.Size() <= iIV.Size()); |
|
45 iIV.Copy(aIV); |
|
46 Reset(); |
|
47 } |
|
48 |
|
49 EXPORT_C CBlockChainingMode::CBlockChainingMode() |
|
50 : iBT(NULL), iRegister(0,0,0), iIV(0,0,0) |
|
51 { |
|
52 } |
|
53 |
|
54 EXPORT_C CBlockChainingMode::~CBlockChainingMode() |
|
55 { |
|
56 delete iBT; |
|
57 delete iRegisterBuf; |
|
58 delete iIVBuf; |
|
59 } |
|
60 |
|
61 EXPORT_C void CBlockChainingMode::ConstructL(CBlockTransformation* aBT, const TDesC8& aIV) |
|
62 { |
|
63 iRegisterBuf = aIV.AllocL(); |
|
64 iRegister.Set(iRegisterBuf->Des()); |
|
65 iIVBuf = aIV.AllocL(); |
|
66 iIV.Set(iIVBuf->Des()); |
|
67 |
|
68 // Take ownership last - doesn't take ownership if we leave |
|
69 iBT = aBT; |
|
70 } |
|
71 |
|
72 /* CModeCBCEncryptor */ |
|
73 EXPORT_C CModeCBCEncryptor* CModeCBCEncryptor::NewL(CBlockTransformation* aBT, |
|
74 const TDesC8& aIV) |
|
75 { |
|
76 CModeCBCEncryptor* self = CModeCBCEncryptorShim::NewL(aBT, aIV); |
|
77 if (! self) |
|
78 { |
|
79 // not able to use CryptoSpi, possibly due to an exterally |
|
80 // derived legacy class so fallback to old implementation. |
|
81 self = NewLC(aBT,aIV); |
|
82 CleanupStack::Pop(self); |
|
83 } |
|
84 return self; |
|
85 } |
|
86 |
|
87 EXPORT_C CModeCBCEncryptor* CModeCBCEncryptor::NewLC(CBlockTransformation* aBT, |
|
88 const TDesC8& aIV) |
|
89 { |
|
90 CModeCBCEncryptor* self = new (ELeave)CModeCBCEncryptor(); |
|
91 CleanupStack::PushL(self); |
|
92 self->ConstructL(aBT, aIV); |
|
93 return self; |
|
94 } |
|
95 |
|
96 CModeCBCEncryptor::CModeCBCEncryptor() |
|
97 { |
|
98 } |
|
99 |
|
100 void CModeCBCEncryptor::Transform(TDes8& aBlock) |
|
101 { |
|
102 assert(aBlock.Size() == iBT->BlockSize()); |
|
103 assert(iRegister.Size() == aBlock.Size()); |
|
104 |
|
105 XorBuf(const_cast<TUint8*>(iRegister.Ptr()), aBlock.Ptr(), aBlock.Size()); |
|
106 iBT->Transform(iRegister); |
|
107 aBlock.Copy(iRegister); |
|
108 } |
|
109 |
|
110 /* CModeCBCDecryptor */ |
|
111 EXPORT_C CModeCBCDecryptor* CModeCBCDecryptor::NewL(CBlockTransformation* aBT, |
|
112 const TDesC8& aIV) |
|
113 { |
|
114 CModeCBCDecryptor* self = CModeCBCDecryptorShim::NewL(aBT, aIV); |
|
115 if (! self) |
|
116 { |
|
117 // not able to use CryptoSpi, possibly due to an exterally |
|
118 // derived legacy class so fallback to old implementation. |
|
119 self = NewLC(aBT,aIV); |
|
120 CleanupStack::Pop(self); |
|
121 } |
|
122 return self; |
|
123 } |
|
124 |
|
125 EXPORT_C CModeCBCDecryptor* CModeCBCDecryptor::NewLC(CBlockTransformation* aBT, |
|
126 const TDesC8& aIV) |
|
127 { |
|
128 CModeCBCDecryptor* self = new (ELeave)CModeCBCDecryptor(); |
|
129 CleanupStack::PushL(self); |
|
130 self->ConstructL(aBT, aIV); |
|
131 return self; |
|
132 } |
|
133 |
|
134 void CModeCBCDecryptor::ConstructL(CBlockTransformation* aBT, const TDesC8& aIV) |
|
135 { |
|
136 iIVBakBuf = aIV.AllocL(); |
|
137 iIVBak.Set(iIVBakBuf->Des()); |
|
138 CBlockChainingMode::ConstructL(aBT, aIV); |
|
139 } |
|
140 |
|
141 CModeCBCDecryptor::~CModeCBCDecryptor(void) |
|
142 { |
|
143 delete iIVBakBuf; |
|
144 } |
|
145 |
|
146 CModeCBCDecryptor::CModeCBCDecryptor() |
|
147 : iIVBak(0,0,0) |
|
148 { |
|
149 } |
|
150 |
|
151 void CModeCBCDecryptor::Transform(TDes8& aBlock) |
|
152 { |
|
153 assert(aBlock.Size() == iBT->BlockSize()); |
|
154 assert(iRegister.Size() == aBlock.Size()); |
|
155 assert(iIVBak.Size() == aBlock.Size()); |
|
156 |
|
157 // Take a copy of incoming block |
|
158 iIVBak.Copy(aBlock); |
|
159 |
|
160 // transform the block |
|
161 iBT->Transform(aBlock); |
|
162 |
|
163 // xor the output with the register |
|
164 XorBuf(const_cast<TUint8*>(aBlock.Ptr()), iRegister.Ptr(), |
|
165 aBlock.Size()); |
|
166 |
|
167 // Update the register to be the original block |
|
168 iRegister.Copy(iIVBak); |
|
169 } |
|
170 |