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
00043 #include "wizard/studio.h"
00044 #include "wizard/cipher.h"
00045 #include "wizard/exception.h"
00046 #include "wizard/exception-private.h"
00047 #include "wizard/hmac.h"
00048 #include "wizard/key.h"
00049 #include "wizard/memory_.h"
00050 #include "wizard/random_.h"
00051 #include "wizard/splay-tree.h"
00052
00053
00054
00055
00056 #define KeymapCipher AESCipher
00057 #define KeymapMode CTRMode
00058 #define SessionKeyHash SHA256Hash
00059 #define SessionKeyLength 512
00060
00061
00062
00063
00064 struct _KeyInfo
00065 {
00066 SplayTreeInfo
00067 *key_map;
00068
00069 StringInfo
00070 *id,
00071 *nonce;
00072
00073 CipherInfo
00074 *cipher_info;
00075
00076 RandomInfo
00077 *random_info;
00078
00079 time_t
00080 timestamp;
00081
00082 size_t
00083 signature;
00084 };
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 static void *DestroyNode(void *entry)
00106 {
00107 entry=(void *) DestroyStringInfo((StringInfo *) entry);
00108 return((void *) NULL);
00109 }
00110
00111 WizardExport KeyInfo *AcquireKeyInfo(void)
00112 {
00113 KeyInfo
00114 *key_info;
00115
00116 key_info=(KeyInfo *) AcquireAlignedMemory(1,sizeof(*key_info));
00117 if (key_info == (KeyInfo *) NULL)
00118 ThrowWizardFatalError(KeymapDomain,MemoryError);
00119 (void) ResetWizardMemory(key_info,0,sizeof(*key_info));
00120 key_info->key_map=NewSplayTree(CompareSplayTreeStringInfo,DestroyNode,
00121 DestroyNode);
00122 key_info->cipher_info=AcquireCipherInfo(KeymapCipher,KeymapMode);
00123 key_info->nonce=GenerateCipherNonce(key_info->cipher_info);
00124 key_info->random_info=AcquireRandomInfo(SessionKeyHash);
00125 key_info->timestamp=time((time_t *) NULL);
00126 key_info->signature=WizardSignature;
00127 key_info->id=GenerateSessionKey(key_info);
00128 return(key_info);
00129 }
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 WizardExport KeyInfo *DestroyKeyInfo(KeyInfo *key_info)
00155 {
00156 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00157 WizardAssert(CipherDomain,key_info != (KeyInfo *) NULL);
00158 WizardAssert(CipherDomain,key_info->signature == WizardSignature);
00159 if (key_info->cipher_info != (CipherInfo *) NULL)
00160 key_info->cipher_info=DestroyCipherInfo(key_info->cipher_info);
00161 if (key_info->random_info != (RandomInfo *) NULL)
00162 key_info->random_info=DestroyRandomInfo(key_info->random_info);
00163 if (key_info->nonce != (StringInfo *) NULL)
00164 key_info->nonce=DestroyStringInfo(key_info->nonce);
00165 if (key_info->id != (StringInfo *) NULL)
00166 key_info->id=DestroyStringInfo(key_info->id);
00167 if (key_info->key_map != (SplayTreeInfo *) NULL)
00168 key_info->key_map=DestroySplayTree(key_info->key_map);
00169 key_info->signature=(~WizardSignature);
00170 key_info=(KeyInfo *) RelinquishWizardMemory(key_info);
00171 return(key_info);
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 WizardExport StringInfo *GenerateSessionKey(KeyInfo *key_info)
00197 {
00198 HMACInfo
00199 *hmac_info;
00200
00201 WizardBooleanType
00202 status;
00203
00204 StringInfo
00205 *id,
00206 *key,
00207 *mac_key;
00208
00209 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00210 WizardAssert(CipherDomain,key_info != (KeyInfo *) NULL);
00211 WizardAssert(CipherDomain,key_info->signature == WizardSignature);
00212 mac_key=GetRandomKey(key_info->random_info,SessionKeyLength/8);
00213 key=GetRandomKey(key_info->random_info,SessionKeyLength/8);
00214 hmac_info=AcquireHMACInfo(SessionKeyHash);
00215 ConstructHMAC(hmac_info,mac_key,key);
00216 id=CloneStringInfo(GetHMACDigest(hmac_info));
00217 hmac_info=DestroyHMACInfo(hmac_info);
00218 mac_key=DestroyStringInfo(mac_key);
00219 if (key_info->id != (StringInfo *) NULL)
00220 status=SetKeyInfo(key_info,id,key);
00221 else
00222 status=AddValueToSplayTree(key_info->key_map,CloneStringInfo(id),
00223 CloneStringInfo(key));
00224 key=DestroyStringInfo(key);
00225 if (status == WizardFalse)
00226 ThrowWizardFatalError(KeymapDomain,KeyError);
00227 key_info->timestamp=time((time_t *) NULL);
00228 return(id);
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255 WizardExport StringInfo *GetKeyInfo(KeyInfo *key_info,const StringInfo *id)
00256 {
00257 StringInfo
00258 *key,
00259 *session_key;
00260
00261 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00262 WizardAssert(CipherDomain,key_info != (KeyInfo *) NULL);
00263 WizardAssert(CipherDomain,id != (const StringInfo *) NULL);
00264 session_key=(StringInfo *) GetValueFromSplayTree(key_info->key_map,
00265 key_info->id);
00266 if (session_key == (StringInfo *) NULL)
00267 ThrowWizardFatalError(KeymapDomain,KeyError);
00268 key=(StringInfo *) GetValueFromSplayTree(key_info->key_map,id);
00269 if (key == (StringInfo *) NULL)
00270 ThrowWizardFatalError(KeymapDomain,KeyError);
00271 SetCipherKey(key_info->cipher_info,session_key);
00272 SetCipherNonce(key_info->cipher_info,key_info->nonce);
00273 key=CloneStringInfo(key);
00274 (void) DecipherCipher(key_info->cipher_info,key);
00275 return(key);
00276 }
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305 WizardExport WizardBooleanType SetKeyInfo(KeyInfo *key_info,const StringInfo *id,
00306 const StringInfo *key)
00307 {
00308 WizardBooleanType
00309 status;
00310
00311 StringInfo
00312 *cipherkey,
00313 *session_key;
00314
00315 (void) LogWizardEvent(TraceEvent,GetWizardModule(),"...");
00316 WizardAssert(CipherDomain,key_info != (KeyInfo *) NULL);
00317 WizardAssert(CipherDomain,id != (const StringInfo *) NULL);
00318 WizardAssert(CipherDomain,key != (const StringInfo *) NULL);
00319 session_key=(StringInfo *) GetValueFromSplayTree(key_info->key_map,
00320 key_info->id);
00321 if (session_key == (StringInfo *) NULL)
00322 ThrowWizardFatalError(KeymapDomain,KeyError);
00323 SetCipherKey(key_info->cipher_info,session_key);
00324 SetCipherNonce(key_info->cipher_info,key_info->nonce);
00325 cipherkey=CloneStringInfo(key);
00326 (void) EncipherCipher(key_info->cipher_info,cipherkey);
00327 status=AddValueToSplayTree(key_info->key_map,CloneStringInfo(id),cipherkey);
00328 return(status);
00329 }