hmac.c

Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                        H   H  M   M   AAA    CCCC                           %
00006 %                        H   H  MM MM  A   A  C                               %
00007 %                        HHHHH  M M M  AAAAA  C                               %
00008 %                        H   H  M   M  A   A  C                               %
00009 %                        H   H  M   M  A   A   CCCC                           %
00010 %                                                                             %
00011 %                                                                             %
00012 %           Wizard's Toolkit Keyed-Hashing for Message Authentication         %
00013 %                                                                             %
00014 %                             Software Design                                 %
00015 %                               John Cristy                                   %
00016 %                               March 2003                                    %
00017 %                                                                             %
00018 %                                                                             %
00019 %  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
00020 %  dedicated to making software imaging solutions freely available.           %
00021 %                                                                             %
00022 %  You may not use this file except in compliance with the License.  You may  %
00023 %  obtain a copy of the License at                                            %
00024 %                                                                             %
00025 %    http://www.wizards-toolkit.org/script/license.php                        %
00026 %                                                                             %
00027 %  Unless required by applicable law or agreed to in writing, software        %
00028 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00029 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00030 %  See the License for the specific language governing permissions and        %
00031 %  limitations under the License.                                             %
00032 %                                                                             %
00033 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00034 %
00035 %
00036 %
00037 */
00038 
00039 /*
00040   Include declarations.
00041 */
00042 #include "wizard/studio.h"
00043 #include "wizard/exception.h"
00044 #include "wizard/exception-private.h"
00045 #include "wizard/hash.h"
00046 #include "wizard/hmac.h"
00047 #include "wizard/memory_.h"
00048 
00049 /*
00050   Typedef declarations.
00051 */
00052 struct _HMACInfo
00053 {
00054   HashInfo
00055     *hash_info;
00056 
00057   StringInfo
00058     *digest,
00059     *final_nonce,
00060     *initial_nonce;
00061 
00062   time_t
00063     timestamp;
00064 
00065   size_t
00066     signature;
00067 };
00068 
00069 /*
00070 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00071 %                                                                             %
00072 %                                                                             %
00073 %                                                                             %
00074 %   A c q u i r e H M A C I n f o                                             %
00075 %                                                                             %
00076 %                                                                             %
00077 %                                                                             %
00078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00079 %
00080 %  AcquireHMACInfo() allocate the HMACInfo structure.
00081 %
00082 %  The format of the AcquireHMACInfo method is:
00083 %
00084 %      HMACInfo *AcquireHMACInfo(const HashType hash)
00085 %
00086 %  A description of each parameter follows:
00087 %
00088 %    o hash: The hash type.
00089 %
00090 */
00091 WizardExport HMACInfo *AcquireHMACInfo(const HashType hash)
00092 {
00093   HMACInfo
00094     *hmac_info;
00095 
00096   hmac_info=(HMACInfo *) AcquireAlignedMemory(1,sizeof(*hmac_info));
00097   if (hmac_info == (HMACInfo *) NULL)
00098     ThrowWizardFatalError(MACDomain,MemoryError);
00099   (void) ResetWizardMemory(hmac_info,0,sizeof(*hmac_info));
00100   hmac_info->hash_info=AcquireHashInfo(hash);
00101   hmac_info->digest=AcquireStringInfo((size_t) GetHashDigestsize(
00102     hmac_info->hash_info));
00103   hmac_info->initial_nonce=AcquireStringInfo((size_t) GetHashBlocksize(
00104     hmac_info->hash_info));
00105   hmac_info->final_nonce=AcquireStringInfo((size_t) GetHashBlocksize(
00106     hmac_info->hash_info));
00107   hmac_info->timestamp=time((time_t *) NULL);
00108   hmac_info->signature=WizardSignature;
00109   return(hmac_info);
00110 }
00111 
00112 /*
00113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00114 %                                                                             %
00115 %                                                                             %
00116 %                                                                             %
00117 %   C o n s t r u c t H M A C                                                 %
00118 %                                                                             %
00119 %                                                                             %
00120 %                                                                             %
00121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00122 %
00123 %  ConstructHMAC() constructs the HMAC digest.
00124 %
00125 %  The format of the ConstructHMAC method is:
00126 %
00127 %      ConstructHMAC(HMACInfo *hmac_info,const StringInfo *key,
00128 %        const StringInfo *message)
00129 %
00130 %  A description of each parameter follows:
00131 %
00132 %    o hmac_info: The address of a structure of type HMACInfo.
00133 %
00134 %    o key: The key.
00135 %
00136 %    o message: The message.
00137 %
00138 */
00139 WizardExport void ConstructHMAC(HMACInfo *hmac_info,const StringInfo *key,
00140   const StringInfo *message)
00141 {
00142   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00143   assert(hmac_info != (HMACInfo *) NULL);
00144   assert(hmac_info->signature == WizardSignature);
00145   assert(key != (StringInfo *) NULL);
00146   assert(message != (StringInfo *) NULL);
00147   InitializeHMAC(hmac_info,key);
00148   UpdateHMAC(hmac_info,message);
00149   FinalizeHMAC(hmac_info);
00150 }
00151 
00152 /*
00153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00154 %                                                                             %
00155 %                                                                             %
00156 %                                                                             %
00157 %   D e s t r o y H M A C I n f o                                             %
00158 %                                                                             %
00159 %                                                                             %
00160 %                                                                             %
00161 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00162 %
00163 %  DestroyHMACInfo() zeros memory associated with the HMACInfo structure.
00164 %
00165 %  The format of the DestroyHMACInfo method is:
00166 %
00167 %      HMACInfo *DestroyHMACInfo(HMACInfo *hmac_info)
00168 %
00169 %  A description of each parameter follows:
00170 %
00171 %    o hmac_info: The cipher hmac_info.
00172 %
00173 */
00174 WizardExport HMACInfo *DestroyHMACInfo(HMACInfo *hmac_info)
00175 {
00176   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00177   assert(hmac_info != (HMACInfo *) NULL);
00178   assert(hmac_info->signature == WizardSignature);
00179   if (hmac_info->final_nonce != (StringInfo *)  NULL)
00180     hmac_info->final_nonce=DestroyStringInfo(hmac_info->final_nonce);
00181   if (hmac_info->initial_nonce != (StringInfo *)  NULL)
00182     hmac_info->initial_nonce=DestroyStringInfo(hmac_info->initial_nonce);
00183   if (hmac_info->digest != (StringInfo *) NULL)
00184     hmac_info->digest=DestroyStringInfo(hmac_info->digest);
00185   if (hmac_info->hash_info != (HashInfo *) NULL)
00186     hmac_info->hash_info=DestroyHashInfo(hmac_info->hash_info);
00187   hmac_info->signature=(~WizardSignature);
00188   hmac_info=(HMACInfo *) RelinquishWizardMemory(hmac_info);
00189   return(hmac_info);
00190 }
00191 
00192 /*
00193 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00194 %                                                                             %
00195 %                                                                             %
00196 %                                                                             %
00197 %   F i n a l i z e H M A C                                                   %
00198 %                                                                             %
00199 %                                                                             %
00200 %                                                                             %
00201 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00202 %
00203 %  FinalizeHMAC() finalizes the HMAC message digest computation.
00204 %
00205 %  The format of the FinalizeHMAC method is:
00206 %
00207 %      FinalizeHMAC(HMACInfo *hmac_info)
00208 %
00209 %  A description of each parameter follows:
00210 %
00211 %    o hmac_info: The address of a structure of type HMACInfo.
00212 %
00213 */
00214 WizardExport void FinalizeHMAC(HMACInfo *hmac_info)
00215 {
00216   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00217   assert(hmac_info != (HMACInfo *) NULL);
00218   assert(hmac_info->signature == WizardSignature);
00219   FinalizeHash(hmac_info->hash_info);
00220   SetStringInfo(hmac_info->digest,GetHashDigest(hmac_info->hash_info));
00221   InitializeHash(hmac_info->hash_info);
00222   UpdateHash(hmac_info->hash_info,hmac_info->final_nonce);
00223   UpdateHash(hmac_info->hash_info,hmac_info->digest);
00224   FinalizeHash(hmac_info->hash_info);
00225   SetStringInfo(hmac_info->digest,GetHashDigest(hmac_info->hash_info));
00226 }
00227 
00228 /*
00229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00230 %                                                                             %
00231 %                                                                             %
00232 %                                                                             %
00233 %   G e t H M A C D i g e s t                                                 %
00234 %                                                                             %
00235 %                                                                             %
00236 %                                                                             %
00237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00238 %
00239 %  GetHMACDigest() returns the hmac digest.
00240 %
00241 %  The format of the GetHMACDigest method is:
00242 %
00243 %      const StringInfo *GetHMACDigest(const hmacInfo *hmac_info)
00244 %
00245 %  A description of each parameter follows:
00246 %
00247 %    o hmac_info: The hmac info.
00248 %
00249 */
00250 WizardExport const StringInfo *GetHMACDigest(const HMACInfo *hmac_info)
00251 {
00252   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00253   WizardAssert(MACDomain,hmac_info != (HMACInfo *) NULL);
00254   WizardAssert(MACDomain,hmac_info->signature == WizardSignature);
00255   return(hmac_info->digest);
00256 }
00257 
00258 /*
00259 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00260 %                                                                             %
00261 %                                                                             %
00262 %                                                                             %
00263 %   G e t H M A C D i g e s t s i z e                                         %
00264 %                                                                             %
00265 %                                                                             %
00266 %                                                                             %
00267 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00268 %
00269 %  GetHMACDigestsize() returns the HMAC digest size.
00270 %
00271 %  The format of the GetHMACDigestsize method is:
00272 %
00273 %      unsigned int *GetHMACDigestsize(const HMACInfo *hmac_info)
00274 %
00275 %  A description of each parameter follows:
00276 %
00277 %    o hmac_info: The hmac info.
00278 %
00279 */
00280 WizardExport size_t GetHMACDigestsize(const HMACInfo *hmac_info)
00281 {
00282   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00283   WizardAssert(CipherDomain,hmac_info != (HMACInfo *) NULL);
00284   WizardAssert(CipherDomain,hmac_info->signature == WizardSignature);
00285   return(GetHashDigestsize(hmac_info->hash_info));
00286 }
00287 
00288 /*
00289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00290 %                                                                             %
00291 %                                                                             %
00292 %                                                                             %
00293 %   I n i t i a l i z e H M A C                                               %
00294 %                                                                             %
00295 %                                                                             %
00296 %                                                                             %
00297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00298 %
00299 %  IntializeHMAC() intializes the HMAC digest.
00300 %
00301 %  The format of the DestroyHMACInfo method is:
00302 %
00303 %      void InitializeHMACInfo(HMACInfo *hmac_info,const StringInfo *key)
00304 %
00305 %  A description of each parameter follows:
00306 %
00307 %    o hmac_info: The message authentication info.
00308 %
00309 %    o key: The key.
00310 %
00311 */
00312 WizardExport void InitializeHMAC(HMACInfo *hmac_info,const StringInfo *key)
00313 {
00314   register size_t
00315     i;
00316 
00317   unsigned char
00318     *datum;
00319 
00320   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00321   assert(hmac_info != (HMACInfo *) NULL);
00322   assert(hmac_info->signature == WizardSignature);
00323   (void) ResetStringInfo(hmac_info->initial_nonce);
00324   if (GetStringInfoLength(key) <= GetStringInfoLength(hmac_info->initial_nonce))
00325     SetStringInfo(hmac_info->initial_nonce,key);
00326   else
00327     {
00328       InitializeHash(hmac_info->hash_info);
00329       UpdateHash(hmac_info->hash_info,key);
00330       FinalizeHash(hmac_info->hash_info);
00331       SetStringInfo(hmac_info->initial_nonce,GetHashDigest(
00332         hmac_info->hash_info));
00333     }
00334   SetStringInfo(hmac_info->final_nonce,hmac_info->initial_nonce);
00335   datum=GetStringInfoDatum(hmac_info->initial_nonce);
00336   for (i=0; i < GetStringInfoLength(hmac_info->initial_nonce); i++)
00337     datum[i]^=0x36;
00338   datum=GetStringInfoDatum(hmac_info->final_nonce);
00339   for (i=0; i < GetStringInfoLength(hmac_info->final_nonce); i++)
00340     datum[i]^=0x5c;
00341   ResetHMAC(hmac_info);
00342 }
00343 
00344 /*
00345 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00346 %                                                                             %
00347 %                                                                             %
00348 %                                                                             %
00349 %   R e s e t H M A C                                                         %
00350 %                                                                             %
00351 %                                                                             %
00352 %                                                                             %
00353 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00354 %
00355 %  ResetHMAC() resets the HMAC message digest computation.
00356 %
00357 %  The format of the ResetHMAC method is:
00358 %
00359 %      ResetHMAC(HMACInfo *hmac_info)
00360 %
00361 %  A description of each parameter follows:
00362 %
00363 %    o hmac_info: The address of a structure of type HMACInfo.
00364 %
00365 */
00366 WizardExport void ResetHMAC(HMACInfo *hmac_info)
00367 {
00368   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00369   assert(hmac_info != (HMACInfo *) NULL);
00370   assert(hmac_info->signature == WizardSignature);
00371   InitializeHash(hmac_info->hash_info);
00372   UpdateHash(hmac_info->hash_info,hmac_info->initial_nonce);
00373 }
00374 
00375 /*
00376 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00377 %                                                                             %
00378 %                                                                             %
00379 %                                                                             %
00380 %   U p d a t e H M A C                                                       %
00381 %                                                                             %
00382 %                                                                             %
00383 %                                                                             %
00384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00385 %
00386 %  UpdateHMAC() updates the HMAC message digest.
00387 %
00388 %  The format of the UpdateHMAC method is:
00389 %
00390 %      UpdateHMAC(HMACInfo *hmac_info,const StringInfo *message)
00391 %
00392 %  A description of each parameter follows:
00393 %
00394 %    o hmac_info: The address of a structure of type HMACInfo.
00395 %
00396 %    o message: The message
00397 %
00398 */
00399 WizardExport void UpdateHMAC(HMACInfo *hmac_info,const StringInfo *message)
00400 {
00401   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00402   assert(hmac_info != (HMACInfo *) NULL);
00403   assert(hmac_info->signature == WizardSignature);
00404   UpdateHash(hmac_info->hash_info,message);
00405 }
Generated by  doxygen 1.6.2-20100208