sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2008-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
* software Mac implementation
|
sl@0
|
16 |
* Software Mac Implementation
|
sl@0
|
17 |
*
|
sl@0
|
18 |
*/
|
sl@0
|
19 |
|
sl@0
|
20 |
|
sl@0
|
21 |
/**
|
sl@0
|
22 |
@file
|
sl@0
|
23 |
*/
|
sl@0
|
24 |
|
sl@0
|
25 |
|
sl@0
|
26 |
// MAC plugin header
|
sl@0
|
27 |
#include "macimpl.h"
|
sl@0
|
28 |
#include "pluginconfig.h"
|
sl@0
|
29 |
|
sl@0
|
30 |
// headers from cryptospi framework
|
sl@0
|
31 |
#include <cryptospi/cryptospidef.h>
|
sl@0
|
32 |
|
sl@0
|
33 |
#include "hmacimpl.h"
|
sl@0
|
34 |
|
sl@0
|
35 |
|
sl@0
|
36 |
using namespace SoftwareCrypto;
|
sl@0
|
37 |
|
sl@0
|
38 |
|
sl@0
|
39 |
CMacImpl* CMacImpl::NewL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* aAlgorithmParams)
|
sl@0
|
40 |
{
|
sl@0
|
41 |
CMacImpl* self = new (ELeave) CMacImpl();
|
sl@0
|
42 |
CleanupStack::PushL(self);
|
sl@0
|
43 |
self->ConstructL(aKey, aImplementationId, aAlgorithmParams);
|
sl@0
|
44 |
CleanupStack::Pop();
|
sl@0
|
45 |
return self;
|
sl@0
|
46 |
}
|
sl@0
|
47 |
|
sl@0
|
48 |
CMacImpl::CMacImpl()
|
sl@0
|
49 |
{
|
sl@0
|
50 |
}
|
sl@0
|
51 |
|
sl@0
|
52 |
void CMacImpl::ConstructL(const CKey& aKey, const TUid aImplementationId, const CCryptoParams* /*aAlgorithmParams*/)
|
sl@0
|
53 |
{
|
sl@0
|
54 |
iImplementationUid = aImplementationId;
|
sl@0
|
55 |
iKey = CryptoSpi::CKey::NewL(aKey);
|
sl@0
|
56 |
|
sl@0
|
57 |
MSoftwareHash* hashImpl = NULL;
|
sl@0
|
58 |
|
sl@0
|
59 |
switch (aImplementationId.iUid)
|
sl@0
|
60 |
{
|
sl@0
|
61 |
case KTestPlugin02XcbcMac96:
|
sl@0
|
62 |
{
|
sl@0
|
63 |
CSymmetricCipher* symmetricCipher = NULL;
|
sl@0
|
64 |
CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher,
|
sl@0
|
65 |
CryptoSpi::KAesUid, *iKey,
|
sl@0
|
66 |
CryptoSpi::KCryptoModeEncryptUid,
|
sl@0
|
67 |
CryptoSpi::KOperationModeCBCUid,
|
sl@0
|
68 |
CryptoSpi::KPaddingModeNoneUid,
|
sl@0
|
69 |
NULL);
|
sl@0
|
70 |
|
sl@0
|
71 |
iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcMac96);
|
sl@0
|
72 |
iBase = ECipherBased;
|
sl@0
|
73 |
}
|
sl@0
|
74 |
break;
|
sl@0
|
75 |
|
sl@0
|
76 |
case KTestPlugin02XcbcPrf128:
|
sl@0
|
77 |
{
|
sl@0
|
78 |
TBuf8<16> tempKey;
|
sl@0
|
79 |
tempKey.SetLength(16);
|
sl@0
|
80 |
|
sl@0
|
81 |
CryptoSpi::CCryptoParams* keyParams = CryptoSpi::CCryptoParams::NewLC();
|
sl@0
|
82 |
keyParams->AddL(tempKey, CryptoSpi::KSymmetricKeyParameterUid);
|
sl@0
|
83 |
CryptoSpi::CKey* key = CryptoSpi::CKey::NewLC(aKey.KeyProperty(),*keyParams);
|
sl@0
|
84 |
|
sl@0
|
85 |
CSymmetricCipher* symmetricCipher = NULL;
|
sl@0
|
86 |
CryptoSpi::CSymmetricCipherFactory::CreateSymmetricCipherL(symmetricCipher,
|
sl@0
|
87 |
CryptoSpi::KAesUid, *key,
|
sl@0
|
88 |
CryptoSpi::KCryptoModeEncryptUid,
|
sl@0
|
89 |
CryptoSpi::KOperationModeCBCUid,
|
sl@0
|
90 |
CryptoSpi::KPaddingModeNoneUid,
|
sl@0
|
91 |
NULL);
|
sl@0
|
92 |
CleanupStack::PopAndDestroy(2, keyParams); //key and keyParams
|
sl@0
|
93 |
|
sl@0
|
94 |
iCmacImpl= CCMacImpl::NewL(*iKey, symmetricCipher, CryptoSpi::KAlgorithmCipherAesXcbcPrf128);
|
sl@0
|
95 |
iBase = ECipherBased;
|
sl@0
|
96 |
}
|
sl@0
|
97 |
break;
|
sl@0
|
98 |
|
sl@0
|
99 |
default:
|
sl@0
|
100 |
{
|
sl@0
|
101 |
User::Leave(KErrNotSupported);
|
sl@0
|
102 |
}
|
sl@0
|
103 |
}
|
sl@0
|
104 |
|
sl@0
|
105 |
if(iBase == EHashBased)
|
sl@0
|
106 |
{
|
sl@0
|
107 |
CleanupStack::PopAndDestroy(hashImpl);
|
sl@0
|
108 |
}
|
sl@0
|
109 |
}
|
sl@0
|
110 |
|
sl@0
|
111 |
CMacImpl::~CMacImpl()
|
sl@0
|
112 |
{
|
sl@0
|
113 |
delete iKey;
|
sl@0
|
114 |
if(iHmacImpl)
|
sl@0
|
115 |
{
|
sl@0
|
116 |
iHmacImpl->Close();
|
sl@0
|
117 |
}
|
sl@0
|
118 |
delete iCmacImpl;
|
sl@0
|
119 |
}
|
sl@0
|
120 |
|
sl@0
|
121 |
void CMacImpl::Reset()
|
sl@0
|
122 |
{
|
sl@0
|
123 |
if (iBase == EHashBased)
|
sl@0
|
124 |
{
|
sl@0
|
125 |
iHmacImpl->Reset();
|
sl@0
|
126 |
}
|
sl@0
|
127 |
else if (iBase == ECipherBased)
|
sl@0
|
128 |
{
|
sl@0
|
129 |
iCmacImpl->Reset();
|
sl@0
|
130 |
}
|
sl@0
|
131 |
}
|
sl@0
|
132 |
|
sl@0
|
133 |
void CMacImpl::Close()
|
sl@0
|
134 |
{
|
sl@0
|
135 |
delete this;
|
sl@0
|
136 |
}
|
sl@0
|
137 |
|
sl@0
|
138 |
void CMacImpl::GetCharacteristicsL(const TCharacteristics*& aPluginCharacteristics)
|
sl@0
|
139 |
{
|
sl@0
|
140 |
aPluginCharacteristics=NULL;
|
sl@0
|
141 |
TInt macNum=sizeof(KMacCharacteristics)/sizeof(TMacCharacteristics*);
|
sl@0
|
142 |
for (TInt i=0;i<macNum;++i)
|
sl@0
|
143 |
{
|
sl@0
|
144 |
if (KMacCharacteristics[i]->iMacChar.iImplementationUID==ImplementationUid().iUid)
|
sl@0
|
145 |
{
|
sl@0
|
146 |
aPluginCharacteristics = KMacCharacteristics[i];
|
sl@0
|
147 |
break;
|
sl@0
|
148 |
}
|
sl@0
|
149 |
}
|
sl@0
|
150 |
}
|
sl@0
|
151 |
|
sl@0
|
152 |
const CExtendedCharacteristics* CMacImpl::GetExtendedCharacteristicsL()
|
sl@0
|
153 |
{
|
sl@0
|
154 |
return (iBase == EHashBased) ? iHmacImpl->GetExtendedCharacteristicsL(): iCmacImpl->GetExtendedCharacteristicsL();
|
sl@0
|
155 |
}
|
sl@0
|
156 |
|
sl@0
|
157 |
TAny* CMacImpl::GetExtension(TUid aExtensionId)
|
sl@0
|
158 |
{
|
sl@0
|
159 |
return (iBase == EHashBased) ? iHmacImpl->GetExtension(aExtensionId): NULL;
|
sl@0
|
160 |
}
|
sl@0
|
161 |
|
sl@0
|
162 |
TUid CMacImpl::ImplementationUid() const
|
sl@0
|
163 |
{
|
sl@0
|
164 |
return iImplementationUid;
|
sl@0
|
165 |
}
|
sl@0
|
166 |
|
sl@0
|
167 |
TPtrC8 CMacImpl::MacL(const TDesC8& aMessage)
|
sl@0
|
168 |
{
|
sl@0
|
169 |
return (iBase == EHashBased) ? iHmacImpl->Hash(aMessage):iCmacImpl->MacL(aMessage);
|
sl@0
|
170 |
}
|
sl@0
|
171 |
|
sl@0
|
172 |
void CMacImpl::UpdateL(const TDesC8& aMessage)
|
sl@0
|
173 |
{
|
sl@0
|
174 |
(iBase == EHashBased) ? iHmacImpl->Update(aMessage):iCmacImpl->UpdateL(aMessage);
|
sl@0
|
175 |
}
|
sl@0
|
176 |
|
sl@0
|
177 |
TPtrC8 CMacImpl::FinalL(const TDesC8& aMessage)
|
sl@0
|
178 |
{
|
sl@0
|
179 |
return (iBase == EHashBased) ? iHmacImpl->Final(aMessage):iCmacImpl->FinalL(aMessage);
|
sl@0
|
180 |
}
|
sl@0
|
181 |
|
sl@0
|
182 |
void CMacImpl::ReInitialiseAndSetKeyL(const CKey& aKey)
|
sl@0
|
183 |
{
|
sl@0
|
184 |
delete iKey;
|
sl@0
|
185 |
iKey = NULL;
|
sl@0
|
186 |
iKey = CryptoSpi::CKey::NewL(aKey);
|
sl@0
|
187 |
|
sl@0
|
188 |
if (iBase == EHashBased)
|
sl@0
|
189 |
{
|
sl@0
|
190 |
iHmacImpl->SetKeyL(aKey);
|
sl@0
|
191 |
iHmacImpl->Reset();
|
sl@0
|
192 |
}
|
sl@0
|
193 |
else if (iBase == ECipherBased)
|
sl@0
|
194 |
{
|
sl@0
|
195 |
iCmacImpl->ReInitialiseAndSetKeyL(aKey);
|
sl@0
|
196 |
}
|
sl@0
|
197 |
}
|
sl@0
|
198 |
|
sl@0
|
199 |
MMac* CMacImpl::ReplicateL()
|
sl@0
|
200 |
{
|
sl@0
|
201 |
CMacImpl* that= new(ELeave) CMacImpl();
|
sl@0
|
202 |
CleanupStack::PushL(that);
|
sl@0
|
203 |
that->iImplementationUid = iImplementationUid;
|
sl@0
|
204 |
that->iBase = iBase;
|
sl@0
|
205 |
that->iKey=CKey::NewL(*iKey);
|
sl@0
|
206 |
|
sl@0
|
207 |
if(iBase == EHashBased)
|
sl@0
|
208 |
{
|
sl@0
|
209 |
that->iHmacImpl=static_cast<CHMacImpl*>(iHmacImpl->ReplicateL());
|
sl@0
|
210 |
}
|
sl@0
|
211 |
else if (iBase == ECipherBased)
|
sl@0
|
212 |
{
|
sl@0
|
213 |
that->iCmacImpl= iCmacImpl->ReplicateL();
|
sl@0
|
214 |
}
|
sl@0
|
215 |
CleanupStack::Pop(that);
|
sl@0
|
216 |
return that;
|
sl@0
|
217 |
}
|
sl@0
|
218 |
|
sl@0
|
219 |
MMac* CMacImpl::CopyL()
|
sl@0
|
220 |
{
|
sl@0
|
221 |
CMacImpl* that= new(ELeave) CMacImpl();
|
sl@0
|
222 |
CleanupStack::PushL(that);
|
sl@0
|
223 |
that->iImplementationUid = iImplementationUid;
|
sl@0
|
224 |
that->iBase = iBase;
|
sl@0
|
225 |
that->iKey=CKey::NewL(*iKey);
|
sl@0
|
226 |
|
sl@0
|
227 |
if(iBase == EHashBased)
|
sl@0
|
228 |
{
|
sl@0
|
229 |
that->iHmacImpl=static_cast<CHMacImpl*>(iHmacImpl->CopyL());
|
sl@0
|
230 |
}
|
sl@0
|
231 |
else if (iBase == ECipherBased)
|
sl@0
|
232 |
{
|
sl@0
|
233 |
that->iCmacImpl= iCmacImpl->CopyL();
|
sl@0
|
234 |
}
|
sl@0
|
235 |
CleanupStack::Pop(that);
|
sl@0
|
236 |
return that;
|
sl@0
|
237 |
}
|