|
1 /* |
|
2 * Copyright (c) 2005-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 |
|
20 |
|
21 /** |
|
22 @file |
|
23 */ |
|
24 |
|
25 |
|
26 #include <e32std.h> |
|
27 #include <hash.h> |
|
28 |
|
29 #define EXPANDLOOP |
|
30 |
|
31 // |
|
32 // HMAC implementation |
|
33 // |
|
34 |
|
35 CHMAC::CHMAC() |
|
36 :CMessageDigest(){} |
|
37 |
|
38 CHMAC::CHMAC(CMessageDigest* aDigest) |
|
39 :CMessageDigest(), |
|
40 iDigest(aDigest), |
|
41 iInnerPad(KMaxBlockSize), |
|
42 iOuterPad(KMaxBlockSize) |
|
43 {} |
|
44 |
|
45 CHMAC::CHMAC(const CHMAC& aMD) |
|
46 :CMessageDigest(aMD), |
|
47 iDigest(NULL), |
|
48 iInnerPad(aMD.iInnerPad), |
|
49 iOuterPad(aMD.iOuterPad), |
|
50 iBlockSize(aMD.iBlockSize) |
|
51 {} |
|
52 |
|
53 EXPORT_C CHMAC::~CHMAC(void) |
|
54 { |
|
55 delete iDigest; |
|
56 } |
|
57 |
|
58 EXPORT_C CHMAC* CHMAC::NewL(const TDesC8& aKey,CMessageDigest* aDigest) |
|
59 { |
|
60 CHMAC* self=new(ELeave) CHMAC(aDigest); |
|
61 CleanupStack::PushL(self); |
|
62 self->InitialiseL(aKey); |
|
63 CleanupStack::Pop(self); |
|
64 return self; |
|
65 } |
|
66 void CHMAC::InitialiseL(const TDesC8& aKey) |
|
67 { |
|
68 InitBlockSizeL(); |
|
69 // initialisation |
|
70 if (iDigest) |
|
71 { |
|
72 iDigest->Reset(); |
|
73 if( (TUint32)aKey.Size() > iBlockSize) |
|
74 { |
|
75 iInnerPad = iDigest->Final(aKey); |
|
76 } |
|
77 else |
|
78 { |
|
79 iInnerPad = aKey; |
|
80 } |
|
81 |
|
82 TUint i; |
|
83 for (i=iInnerPad.Size();i<iBlockSize;i++) |
|
84 iInnerPad.Append(0); |
|
85 |
|
86 iOuterPad=iInnerPad; |
|
87 |
|
88 const TUint8 Magic1=0x36, Magic2=0x5c; |
|
89 for (i=0;i<iBlockSize;i++) |
|
90 { |
|
91 iInnerPad[i]^=Magic1; |
|
92 iOuterPad[i]^=Magic2; |
|
93 } |
|
94 //start inner hash |
|
95 iDigest->Hash(iInnerPad); |
|
96 } |
|
97 } |
|
98 |
|
99 void CHMAC::InitBlockSizeL() |
|
100 { |
|
101 |
|
102 iBlockSize = iDigest->BlockSize(); |
|
103 if(iBlockSize > KMaxBlockSize) |
|
104 { |
|
105 User::Leave(KErrNotSupported); |
|
106 } |
|
107 |
|
108 iInnerPad.SetLength(iBlockSize); |
|
109 iOuterPad.SetLength(iBlockSize); |
|
110 iInnerPadCopy.SetLength(iBlockSize); |
|
111 iOuterPadCopy.SetLength(iBlockSize); |
|
112 } |
|
113 |
|
114 EXPORT_C CMessageDigest* CHMAC::CopyL(void) |
|
115 { |
|
116 CHMAC* that=new(ELeave) CHMAC(*this); |
|
117 CleanupStack::PushL(that); |
|
118 that->iDigest=iDigest ? iDigest->CopyL() : NULL; |
|
119 CleanupStack::Pop(); |
|
120 return that; |
|
121 } |
|
122 EXPORT_C CMessageDigest* CHMAC::ReplicateL(void) |
|
123 { |
|
124 CHMAC* that=new(ELeave) CHMAC(*this); |
|
125 CleanupStack::PushL(that); |
|
126 that->iDigest=iDigest ? iDigest->ReplicateL() : NULL; |
|
127 that->Reset(); |
|
128 CleanupStack::Pop(); |
|
129 return that; |
|
130 } |
|
131 EXPORT_C TInt CHMAC::BlockSize(void) |
|
132 { |
|
133 return iBlockSize; |
|
134 } |
|
135 |
|
136 EXPORT_C TInt CHMAC::HashSize(void) |
|
137 { |
|
138 return iDigest ? iDigest->HashSize() : 0; |
|
139 } |
|
140 EXPORT_C void CHMAC::Reset(void) |
|
141 { |
|
142 if (iDigest) |
|
143 { |
|
144 iDigest->Reset(); |
|
145 iDigest->Update(iInnerPad); |
|
146 } |
|
147 } |
|
148 |
|
149 // JCS, There may be a more efficient method but I can't find it |
|
150 // because using the DoFinal/DoUpdate functions directly calls |
|
151 // Store/Restore at inappropriate times and scribbles over stored |
|
152 // data |
|
153 // This is the only way I've found to both generate a hash value |
|
154 // and get this in the correctly updated state |
|
155 EXPORT_C TPtrC8 CHMAC::Hash(const TDesC8& aMessage) |
|
156 { |
|
157 TPtrC8 ptr(KNullDesC8()); |
|
158 TPtrC8 finalPtr(KNullDesC8()); |
|
159 StoreState(); |
|
160 if (iDigest) |
|
161 { |
|
162 ptr.Set(iDigest->Final(aMessage)); |
|
163 iDigest->Update(iOuterPad); |
|
164 finalPtr.Set(iDigest->Final(ptr)); |
|
165 } |
|
166 |
|
167 RestoreState(); |
|
168 iDigest->Update(aMessage); |
|
169 |
|
170 return (finalPtr); |
|
171 } |
|
172 |
|
173 EXPORT_C void CHMAC::Update(const TDesC8& aMessage) |
|
174 { |
|
175 if(iDigest) |
|
176 { |
|
177 iDigest->Update(aMessage); |
|
178 } |
|
179 } |
|
180 |
|
181 EXPORT_C TPtrC8 CHMAC::Final(const TDesC8& aMessage) |
|
182 { |
|
183 TPtrC8 ptr(KNullDesC8()); |
|
184 if(iDigest) |
|
185 { |
|
186 ptr.Set(iDigest->Final(aMessage)); |
|
187 iDigest->Update(iOuterPad); |
|
188 iDigest->Final(ptr); |
|
189 Reset(); |
|
190 } |
|
191 return (ptr); |
|
192 } |
|
193 |
|
194 EXPORT_C TPtrC8 CHMAC::Final() |
|
195 { |
|
196 TPtrC8 ptr(KNullDesC8()); |
|
197 if(iDigest) |
|
198 { |
|
199 ptr.Set(iDigest->Final()); |
|
200 iDigest->Update(iOuterPad); |
|
201 iDigest->Final(ptr); |
|
202 Reset(); |
|
203 } |
|
204 return (ptr); |
|
205 } |
|
206 |
|
207 void CHMAC::RestoreState() |
|
208 { |
|
209 iOuterPad.Copy(iOuterPadCopy); |
|
210 iInnerPad.Copy(iInnerPadCopy); |
|
211 if (iDigest) |
|
212 iDigest->RestoreState(); |
|
213 } |
|
214 |
|
215 void CHMAC::StoreState() |
|
216 { |
|
217 iOuterPadCopy.Copy(iOuterPad); |
|
218 iInnerPadCopy.Copy(iInnerPad); |
|
219 if (iDigest) |
|
220 iDigest->StoreState(); |
|
221 } |