hmac.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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
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
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
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
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
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
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
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
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
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
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
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
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
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
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
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
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
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
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
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 }