MagickCore 7.0.10
hmac.c
Go to the documentation of this file.
1/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% H H M M AAA CCCC %
6% H H MM MM A A C %
7% HHHHH M M M AAAAA C %
8% H H M M A A C %
9% H H M M A A CCCC %
10% %
11% %
12% Wizard's Toolkit Keyed-Hashing for Message Authentication %
13% %
14% Software Design %
15% Cristy %
16% March 2003 %
17% %
18% %
19% Copyright @ 1999 ImageMagick Studio LLC, a non-profit organization %
20% dedicated to making software imaging solutions freely available. %
21% %
22% You may not use this file except in compliance with the License. You may %
23% obtain a copy of the License at %
24% %
25% https://imagemagick.org/script/license.php %
26% %
27% Unless required by applicable law or agreed to in writing, software %
28% distributed under the License is distributed on an "AS IS" BASIS, %
29% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
30% See the License for the specific language governing permissions and %
31% limitations under the License. %
32% %
33%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
34%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
42#include "wizard/studio.h"
43#include "wizard/exception.h"
45#include "wizard/hash.h"
46#include "wizard/hmac.h"
47#include "wizard/memory_.h"
48
49/*
50 Typedef declarations.
51*/
53{
56
61
62 time_t
64
65 size_t
67};
68
69/*
70%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
71% %
72% %
73% %
74% A c q u i r e H M A C I n f o %
75% %
76% %
77% %
78%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79%
80% AcquireHMACInfo() allocate the HMACInfo structure.
81%
82% The format of the AcquireHMACInfo method is:
83%
84% HMACInfo *AcquireHMACInfo(const HashType hash)
85%
86% A description of each parameter follows:
87%
88% o hash: The hash type.
89%
90*/
92{
94 *hmac_info;
95
96 hmac_info=(HMACInfo *) AcquireWizardMemory(sizeof(*hmac_info));
97 if (hmac_info == (HMACInfo *) NULL)
99 (void) memset(hmac_info,0,sizeof(*hmac_info));
100 hmac_info->hash_info=AcquireHashInfo(hash);
101 hmac_info->digest=AcquireStringInfo((size_t) GetHashDigestsize(
102 hmac_info->hash_info));
104 hmac_info->hash_info));
106 hmac_info->hash_info));
107 hmac_info->timestamp=time((time_t *) NULL);
108 hmac_info->signature=WizardSignature;
109 return(hmac_info);
110}
111
112/*
113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114% %
115% %
116% %
117% C o n s t r u c t H M A C %
118% %
119% %
120% %
121%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122%
123% ConstructHMAC() constructs the HMAC digest.
124%
125% The format of the ConstructHMAC method is:
126%
127% ConstructHMAC(HMACInfo *hmac_info,const StringInfo *key,
128% const StringInfo *message)
129%
130% A description of each parameter follows:
131%
132% o hmac_info: The address of a structure of type HMACInfo.
133%
134% o key: The key.
135%
136% o message: The message.
137%
138*/
140 const StringInfo *message)
141{
143 assert(hmac_info != (HMACInfo *) NULL);
144 assert(hmac_info->signature == WizardSignature);
145 assert(key != (StringInfo *) NULL);
146 assert(message != (StringInfo *) NULL);
147 InitializeHMAC(hmac_info,key);
148 UpdateHMAC(hmac_info,message);
149 FinalizeHMAC(hmac_info);
150}
151
152/*
153%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154% %
155% %
156% %
157% D e s t r o y H M A C I n f o %
158% %
159% %
160% %
161%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
162%
163% DestroyHMACInfo() zeros memory associated with the HMACInfo structure.
164%
165% The format of the DestroyHMACInfo method is:
166%
167% HMACInfo *DestroyHMACInfo(HMACInfo *hmac_info)
168%
169% A description of each parameter follows:
170%
171% o hmac_info: The cipher hmac_info.
172%
173*/
175{
177 assert(hmac_info != (HMACInfo *) NULL);
178 assert(hmac_info->signature == WizardSignature);
179 if (hmac_info->final_nonce != (StringInfo *) NULL)
180 hmac_info->final_nonce=DestroyStringInfo(hmac_info->final_nonce);
181 if (hmac_info->initial_nonce != (StringInfo *) NULL)
182 hmac_info->initial_nonce=DestroyStringInfo(hmac_info->initial_nonce);
183 if (hmac_info->digest != (StringInfo *) NULL)
184 hmac_info->digest=DestroyStringInfo(hmac_info->digest);
185 if (hmac_info->hash_info != (HashInfo *) NULL)
186 hmac_info->hash_info=DestroyHashInfo(hmac_info->hash_info);
187 hmac_info->signature=(~WizardSignature);
188 hmac_info=(HMACInfo *) RelinquishWizardMemory(hmac_info);
189 return(hmac_info);
190}
191
192/*
193%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
194% %
195% %
196% %
197% F i n a l i z e H M A C %
198% %
199% %
200% %
201%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202%
203% FinalizeHMAC() finalizes the HMAC message digest computation.
204%
205% The format of the FinalizeHMAC method is:
206%
207% FinalizeHMAC(HMACInfo *hmac_info)
208%
209% A description of each parameter follows:
210%
211% o hmac_info: The address of a structure of type HMACInfo.
212%
213*/
215{
217 assert(hmac_info != (HMACInfo *) NULL);
218 assert(hmac_info->signature == WizardSignature);
219 FinalizeHash(hmac_info->hash_info);
220 SetStringInfo(hmac_info->digest,GetHashDigest(hmac_info->hash_info));
221 InitializeHash(hmac_info->hash_info);
222 UpdateHash(hmac_info->hash_info,hmac_info->final_nonce);
223 UpdateHash(hmac_info->hash_info,hmac_info->digest);
224 FinalizeHash(hmac_info->hash_info);
225 SetStringInfo(hmac_info->digest,GetHashDigest(hmac_info->hash_info));
226}
227
228/*
229%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230% %
231% %
232% %
233% G e t H M A C D i g e s t %
234% %
235% %
236% %
237%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238%
239% GetHMACDigest() returns the hmac digest.
240%
241% The format of the GetHMACDigest method is:
242%
243% const StringInfo *GetHMACDigest(const hmacInfo *hmac_info)
244%
245% A description of each parameter follows:
246%
247% o hmac_info: The hmac info.
248%
249*/
251{
253 WizardAssert(MACDomain,hmac_info != (HMACInfo *) NULL);
255 return(hmac_info->digest);
256}
257
258/*
259%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260% %
261% %
262% %
263% G e t H M A C D i g e s t s i z e %
264% %
265% %
266% %
267%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
268%
269% GetHMACDigestsize() returns the HMAC digest size.
270%
271% The format of the GetHMACDigestsize method is:
272%
273% unsigned int *GetHMACDigestsize(const HMACInfo *hmac_info)
274%
275% A description of each parameter follows:
276%
277% o hmac_info: The hmac info.
278%
279*/
281{
283 WizardAssert(CipherDomain,hmac_info != (HMACInfo *) NULL);
285 return(GetHashDigestsize(hmac_info->hash_info));
286}
287
288/*
289%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290% %
291% %
292% %
293% I n i t i a l i z e H M A C %
294% %
295% %
296% %
297%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298%
299% IntializeHMAC() intializes the HMAC digest.
300%
301% The format of the DestroyHMACInfo method is:
302%
303% void InitializeHMACInfo(HMACInfo *hmac_info,const StringInfo *key)
304%
305% A description of each parameter follows:
306%
307% o hmac_info: The message authentication info.
308%
309% o key: The key.
310%
311*/
313{
314 size_t
315 i;
316
317 unsigned char
318 *datum;
319
321 assert(hmac_info != (HMACInfo *) NULL);
322 assert(hmac_info->signature == WizardSignature);
323 (void) ResetStringInfo(hmac_info->initial_nonce);
325 SetStringInfo(hmac_info->initial_nonce,key);
326 else
327 {
328 InitializeHash(hmac_info->hash_info);
329 UpdateHash(hmac_info->hash_info,key);
330 FinalizeHash(hmac_info->hash_info);
332 hmac_info->hash_info));
333 }
334 SetStringInfo(hmac_info->final_nonce,hmac_info->initial_nonce);
335 datum=GetStringInfoDatum(hmac_info->initial_nonce);
336 for (i=0; i < GetStringInfoLength(hmac_info->initial_nonce); i++)
337 datum[i]^=0x36;
338 datum=GetStringInfoDatum(hmac_info->final_nonce);
339 for (i=0; i < GetStringInfoLength(hmac_info->final_nonce); i++)
340 datum[i]^=0x5c;
341 ResetHMAC(hmac_info);
342}
343
344/*
345%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
346% %
347% %
348% %
349% R e s e t H M A C %
350% %
351% %
352% %
353%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
354%
355% ResetHMAC() resets the HMAC message digest computation.
356%
357% The format of the ResetHMAC method is:
358%
359% ResetHMAC(HMACInfo *hmac_info)
360%
361% A description of each parameter follows:
362%
363% o hmac_info: The address of a structure of type HMACInfo.
364%
365*/
367{
369 assert(hmac_info != (HMACInfo *) NULL);
370 assert(hmac_info->signature == WizardSignature);
371 InitializeHash(hmac_info->hash_info);
372 UpdateHash(hmac_info->hash_info,hmac_info->initial_nonce);
373}
374
375/*
376%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
377% %
378% %
379% %
380% U p d a t e H M A C %
381% %
382% %
383% %
384%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
385%
386% UpdateHMAC() updates the HMAC message digest.
387%
388% The format of the UpdateHMAC method is:
389%
390% UpdateHMAC(HMACInfo *hmac_info,const StringInfo *message)
391%
392% A description of each parameter follows:
393%
394% o hmac_info: The address of a structure of type HMACInfo.
395%
396% o message: The message
397%
398*/
399WizardExport void UpdateHMAC(HMACInfo *hmac_info,const StringInfo *message)
400{
402 assert(hmac_info != (HMACInfo *) NULL);
403 assert(hmac_info->signature == WizardSignature);
404 UpdateHash(hmac_info->hash_info,message);
405}
#define WizardAssert(domain, predicate)
#define ThrowWizardFatalError(domain, error)
@ MACDomain
Definition exception.h:33
@ CipherDomain
Definition exception.h:34
@ MemoryError
Definition exception.h:49
WizardExport HashInfo * AcquireHashInfo(const HashType hash)
Definition hash.c:99
WizardExport WizardBooleanType UpdateHash(HashInfo *hash_info, const StringInfo *message)
Definition hash.c:849
WizardExport WizardBooleanType InitializeHash(HashInfo *hash_info)
Definition hash.c:760
WizardExport HashInfo * DestroyHashInfo(HashInfo *hash_info)
Definition hash.c:231
WizardExport size_t GetHashBlocksize(const HashInfo *hash_info)
Definition hash.c:453
WizardExport const StringInfo * GetHashDigest(const HashInfo *hash_info)
Definition hash.c:568
WizardExport size_t GetHashDigestsize(const HashInfo *hash_info)
Definition hash.c:598
WizardExport WizardBooleanType FinalizeHash(HashInfo *hash_info)
Definition hash.c:324
HashType
Definition hash.h:28
WizardExport void ConstructHMAC(HMACInfo *hmac_info, const StringInfo *key, const StringInfo *message)
Definition hmac.c:139
WizardExport void FinalizeHMAC(HMACInfo *hmac_info)
Definition hmac.c:214
WizardExport HMACInfo * AcquireHMACInfo(const HashType hash)
Definition hmac.c:91
WizardExport const StringInfo * GetHMACDigest(const HMACInfo *hmac_info)
Definition hmac.c:250
WizardExport void UpdateHMAC(HMACInfo *hmac_info, const StringInfo *message)
Definition hmac.c:399
WizardExport HMACInfo * DestroyHMACInfo(HMACInfo *hmac_info)
Definition hmac.c:174
WizardExport void InitializeHMAC(HMACInfo *hmac_info, const StringInfo *key)
Definition hmac.c:312
WizardExport size_t GetHMACDigestsize(const HMACInfo *hmac_info)
Definition hmac.c:280
WizardExport void ResetHMAC(HMACInfo *hmac_info)
Definition hmac.c:366
WizardBooleanType LogWizardEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition log.c:1390
@ TraceEvent
Definition log.h:39
#define GetWizardModule()
Definition log.h:30
WizardExport void * AcquireWizardMemory(const size_t size)
Definition memory.c:586
WizardExport void * RelinquishWizardMemory(void *memory)
Definition memory.c:1039
#define WizardExport
#define WizardSignature
WizardExport void SetStringInfo(StringInfo *string_info, const StringInfo *source)
Definition string.c:1792
WizardExport size_t GetStringInfoLength(const StringInfo *string_info)
Definition string.c:1280
WizardExport StringInfo * AcquireStringInfo(const size_t length)
Definition string.c:179
WizardExport unsigned char * GetStringInfoDatum(const StringInfo *string_info)
Definition string.c:1251
WizardExport void ResetStringInfo(StringInfo *string_info)
Definition string.c:1761
WizardExport StringInfo * DestroyStringInfo(StringInfo *string_info)
Definition string.c:857
HashInfo * hash_info
Definition hmac.c:55
StringInfo * digest
Definition hmac.c:58
size_t signature
Definition hmac.c:66
StringInfo * initial_nonce
Definition hmac.c:60
StringInfo * final_nonce
Definition hmac.c:59
time_t timestamp
Definition hmac.c:63