key.c

Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                                                                             %
00006 %                             K   K  EEEEE  Y   Y                             %
00007 %                             K  K   E      Y   Y                             %
00008 %                             KKK    EEE     Y Y                              %
00009 %                             K  K   E        Y                               %
00010 %                             K   K  EEEEE    Y                               %
00011 %                                                                             %
00012 %                                                                             %
00013 %                          Wizard's Toolkit Key Methods                       %
00014 %                                                                             %
00015 %                             Software Design                                 %
00016 %                               John Cristy                                   %
00017 %                               March 2003                                    %
00018 %                                                                             %
00019 %                                                                             %
00020 %  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
00021 %  dedicated to making software imaging solutions freely available.           %
00022 %                                                                             %
00023 %  You may not use this file except in compliance with the License.  You may  %
00024 %  obtain a copy of the License at                                            %
00025 %                                                                             %
00026 %    http://www.wizards-toolkit.org/script/license.php                        %
00027 %                                                                             %
00028 %  Unless required by applicable law or agreed to in writing, software        %
00029 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00030 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00031 %  Wizard the License for the specific language governing permissions and        %
00032 %  limitations under the License.                                             %
00033 %                                                                             %
00034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00035 %
00036 %
00037 %
00038 */
00039 
00040 /*
00041   Include declarations.
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   Define declarations.
00055 */
00056 #define KeymapCipher  AESCipher
00057 #define KeymapMode  CTRMode
00058 #define SessionKeyHash  SHA256Hash
00059 #define SessionKeyLength  512
00060 
00061 /*
00062   Typedef declarations.
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 %   A c q u i r e K e y I n f o                                               %
00092 %                                                                             %
00093 %                                                                             %
00094 %                                                                             %
00095 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00096 %
00097 %  AcquireKeyInfo() allocates the KeyInfo structure.
00098 %
00099 %  The format of the AcquireKeyInfo method is:
00100 %
00101 %      KeyInfo *AcquireKeyInfo()
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 %   D e s t r o y K e y I n f o                                               %
00137 %                                                                             %
00138 %                                                                             %
00139 %                                                                             %
00140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00141 %
00142 %  DestroyKeyInfo() zeros memory associated with the KeyInfo
00143 %  structure.
00144 %
00145 %  The format of the DestroyKeyInfo method is:
00146 %
00147 %      KeyInfo *DestroyKeyInfo(KeyInfo *key_info)
00148 %
00149 %  A description of each parameter follows:
00150 %
00151 %    o key_info: The key info.
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 %   G e n e r a t e S e s s i o n K e y                                       %
00180 %                                                                             %
00181 %                                                                             %
00182 %                                                                             %
00183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00184 %
00185 %  GenerateSessionKey() generate a session key.
00186 %
00187 %  The format of the GenerateSessionKey method is:
00188 %
00189 %      void GenerateSessionKey(KeyInfo *key_info)
00190 %
00191 %  A description of each parameter follows:
00192 %
00193 %    o key_info: The key info.
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 %   G e t K e y I n f o                                                       %
00237 %                                                                             %
00238 %                                                                             %
00239 %                                                                             %
00240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00241 %
00242 %  GetKeyInfo() gets a key from the key splay-tree.
00243 %
00244 %  The format of the GetKeyInfo method is:
00245 %
00246 %      StringInfo *GetKeyInfo(KeyInfo *key_info,const StringInfo *id)
00247 %
00248 %  A description of each parameter follows:
00249 %
00250 %    o key_info: The key info.
00251 %
00252 %    o id: The key id.
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 %   S e t K e y I n f o                                                       %
00284 %                                                                             %
00285 %                                                                             %
00286 %                                                                             %
00287 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00288 %
00289 %  SetKeyInfo() saves a key to the splay-tree.
00290 %
00291 %  The format of the SetKeyInfo
00292 %
00293 %      WizardBooleanType SetKeyInfo(KeyInfo *key_info,const StringInfo *id,
00294 %        const StringInfo *key)
00295 %
00296 %  A description of each parameter follows:
00297 %
00298 %    o key_info: The key info.
00299 %
00300 %    o id: The key id.
00301 %
00302 %    o key: The key.
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 }
Generated by  doxygen 1.6.2-20100208