cryptalgo.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  $RCSfile$
00003                              -------------------
00004     cvs         : $Id: crypttoken.h 1113 2007-01-10 09:14:16Z martin $
00005     begin       : Wed Mar 16 2005
00006     copyright   : (C) 2005 by Martin Preuss
00007     email       : martin@libchipcard.de
00008 
00009  ***************************************************************************
00010  *          Please see toplevel file COPYING for license details           *
00011  ***************************************************************************/
00012 
00013 #ifdef HAVE_CONFIG_H
00014 # include <config.h>
00015 #endif
00016 
00017 
00018 #include "cryptalgo_p.h"
00019 #include <gwenhywfar/misc.h>
00020 #include <gwenhywfar/debug.h>
00021 
00022 
00023 
00024 GWEN_LIST2_FUNCTIONS(GWEN_CRYPT_CRYPTALGO, GWEN_Crypt_CryptAlgo)
00025 
00026 
00027 
00028 GWEN_CRYPT_CRYPTALGOID GWEN_Crypt_CryptAlgoId_fromString(const char *s) {
00029   assert(s);
00030   if (strcasecmp(s, "none")==0)
00031     return GWEN_Crypt_CryptAlgoId_None;
00032   else if (strcasecmp(s, "rsa")==0)
00033     return GWEN_Crypt_CryptAlgoId_Rsa;
00034   else if (strcasecmp(s, "dsa")==0)
00035     return GWEN_Crypt_CryptAlgoId_Dsa;
00036   else if (strcasecmp(s, "des")==0)
00037     return GWEN_Crypt_CryptAlgoId_Des;
00038   else if (strcasecmp(s, "des_3k")==0)
00039     return GWEN_Crypt_CryptAlgoId_Des3K;
00040   else if (strcasecmp(s, "blowfish")==0)
00041     return GWEN_Crypt_CryptAlgoId_BlowFish;
00042   else if (strcasecmp(s, "any")==0)
00043     return GWEN_Crypt_CryptAlgoId_Any;
00044   return GWEN_Crypt_CryptAlgoId_Unknown;
00045 }
00046 
00047 
00048 
00049 const char *GWEN_Crypt_CryptAlgoId_toString(GWEN_CRYPT_CRYPTALGOID a) {
00050   switch(a) {
00051   case GWEN_Crypt_CryptAlgoId_None:
00052     return "none";
00053   case GWEN_Crypt_CryptAlgoId_Rsa:
00054     return "rsa";
00055   case GWEN_Crypt_CryptAlgoId_Dsa:
00056     return "dsa";
00057   case GWEN_Crypt_CryptAlgoId_Des:
00058     return "des";
00059   case GWEN_Crypt_CryptAlgoId_Des3K:
00060     return "des_3k";
00061   case GWEN_Crypt_CryptAlgoId_BlowFish:
00062     return "blowfish";
00063   case GWEN_Crypt_CryptAlgoId_Any:
00064     return "any";
00065   default:
00066     return "unknown";
00067   }
00068 }
00069 
00070 
00071 
00072 GWEN_CRYPT_CRYPTMODE GWEN_Crypt_CryptMode_fromString(const char *s) {
00073   assert(s);
00074   if (strcasecmp(s, "none")==0)
00075     return GWEN_Crypt_CryptMode_None;
00076   else if (strcasecmp(s, "ecb")==0)
00077     return GWEN_Crypt_CryptMode_Ecb;
00078   else if (strcasecmp(s, "cfb")==0)
00079     return GWEN_Crypt_CryptMode_Cfb;
00080   else if (strcasecmp(s, "cbc")==0)
00081     return GWEN_Crypt_CryptMode_Cbc;
00082   return GWEN_Crypt_CryptMode_Unknown;
00083 }
00084 
00085 
00086 
00087 const char *GWEN_Crypt_CryptMode_toString(GWEN_CRYPT_CRYPTMODE m) {
00088   switch(m) {
00089   case GWEN_Crypt_CryptMode_None:
00090     return "none";
00091   case GWEN_Crypt_CryptMode_Ecb:
00092     return "ecb";
00093   case GWEN_Crypt_CryptMode_Cfb:
00094     return "cfb";
00095   case GWEN_Crypt_CryptMode_Cbc:
00096     return "cbc";
00097   default:
00098     return "unknown";
00099   }
00100 }
00101 
00102 
00103 
00104 GWEN_CRYPT_CRYPTALGO *GWEN_Crypt_CryptAlgo_new(GWEN_CRYPT_CRYPTALGOID id,
00105                                                GWEN_CRYPT_CRYPTMODE m) {
00106   GWEN_CRYPT_CRYPTALGO *a;
00107 
00108   GWEN_NEW_OBJECT(GWEN_CRYPT_CRYPTALGO, a);
00109   a->refCount=1;
00110 
00111   a->id=id;
00112   a->mode=m;
00113 
00114   return a;
00115 }
00116 
00117 
00118 
00119 void GWEN_Crypt_CryptAlgo_Attach(GWEN_CRYPT_CRYPTALGO *a) {
00120   assert(a);
00121   assert(a->refCount);
00122   a->refCount++;
00123 }
00124 
00125 
00126 
00127 GWEN_CRYPT_CRYPTALGO *GWEN_Crypt_CryptAlgo_fromDb(GWEN_DB_NODE *db) {
00128   const char *s;
00129 
00130   assert(db);
00131   s=GWEN_DB_GetCharValue(db, "id", 0, NULL);
00132   if (s) {
00133     GWEN_CRYPT_CRYPTALGO *a;
00134     GWEN_CRYPT_CRYPTALGOID id;
00135     GWEN_CRYPT_CRYPTMODE m;
00136     const void *p;
00137     unsigned int len;
00138 
00139     id=GWEN_Crypt_CryptAlgoId_fromString(s);
00140     if (id==GWEN_Crypt_CryptAlgoId_Unknown) {
00141       DBG_INFO(GWEN_LOGDOMAIN, "Unknown cryptalgo id [%s]", s);
00142       return NULL;
00143     }
00144 
00145     s=GWEN_DB_GetCharValue(db, "mode", 0, NULL);
00146     if (s)
00147       m=GWEN_Crypt_CryptMode_fromString(s);
00148     else {
00149       DBG_INFO(GWEN_LOGDOMAIN, "Missing crypt mode");
00150       return NULL;
00151     }
00152 
00153     a=GWEN_Crypt_CryptAlgo_new(id, m);
00154     assert(a);
00155     p=GWEN_DB_GetBinValue(db, "initVector", 0, NULL, 0, &len);
00156     if (p && len)
00157       GWEN_Crypt_CryptAlgo_SetInitVector(a, p, len);
00158 
00159     a->chunkSize=GWEN_DB_GetIntValue(db, "chunkSize", 0, 0);
00160     a->keySizeInBits=GWEN_DB_GetIntValue(db, "keySizeInBits", 0, 0);
00161 
00162     return a;
00163   }
00164   else {
00165     DBG_INFO(GWEN_LOGDOMAIN, "Missing cryptalgo id");
00166     return NULL;
00167   }
00168 }
00169 
00170 
00171 
00172 int GWEN_Crypt_CryptAlgo_toDb(const GWEN_CRYPT_CRYPTALGO *a, GWEN_DB_NODE *db) {
00173   assert(a);
00174   assert(a->refCount);
00175 
00176   GWEN_DB_SetCharValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS,
00177                        "id",
00178                        GWEN_Crypt_CryptAlgoId_toString(a->id));
00179   GWEN_DB_SetCharValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS,
00180                        "mode",
00181                        GWEN_Crypt_CryptMode_toString(a->id));
00182   if (a->pInitVector && a->lInitVector)
00183     GWEN_DB_SetBinValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS,
00184                         "initVector",
00185                         a->pInitVector, a->lInitVector);
00186   GWEN_DB_SetIntValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS,
00187                       "chunkSize",
00188                       a->chunkSize);
00189   GWEN_DB_SetIntValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS,
00190                       "keySizeInBits",
00191                       a->keySizeInBits);
00192 
00193   return 0;
00194 }
00195 
00196 
00197 
00198 GWEN_CRYPT_CRYPTALGO *GWEN_Crypt_CryptAlgo_dup(const GWEN_CRYPT_CRYPTALGO *na) {
00199   GWEN_CRYPT_CRYPTALGO *a;
00200 
00201   assert(na);
00202   a=GWEN_Crypt_CryptAlgo_new(na->id, na->mode);
00203   if (na->pInitVector && na->lInitVector) {
00204     a->pInitVector=(uint8_t*) malloc(na->lInitVector);
00205     if (a->pInitVector==NULL) {
00206       GWEN_Crypt_CryptAlgo_free(a);
00207       return NULL;
00208     }
00209     else
00210       memmove(a->pInitVector, na->pInitVector, na->lInitVector);
00211     a->lInitVector=na->lInitVector;
00212   }
00213   a->chunkSize=na->chunkSize;
00214   a->keySizeInBits=na->keySizeInBits;
00215   return a;
00216 }
00217 
00218 
00219 
00220 void GWEN_Crypt_CryptAlgo_free(GWEN_CRYPT_CRYPTALGO *a) {
00221   if (a) {
00222     assert(a->refCount);
00223     if (a->refCount==1) {
00224       if (a->pInitVector) {
00225         free(a->pInitVector);
00226         a->pInitVector=NULL;
00227       }
00228       a->refCount--;
00229       GWEN_FREE_OBJECT(a);
00230     }
00231     else {
00232       a->refCount--;
00233     }
00234   }
00235 }
00236 
00237 
00238 
00239 GWEN_CRYPT_CRYPTALGOID GWEN_Crypt_CryptAlgo_GetId(const GWEN_CRYPT_CRYPTALGO *a){
00240   assert(a);
00241   assert(a->refCount);
00242   return a->id;
00243 }
00244 
00245 
00246 
00247 GWEN_CRYPT_CRYPTMODE GWEN_Crypt_CryptAlgo_GetMode(const GWEN_CRYPT_CRYPTALGO *a) {
00248   assert(a);
00249   assert(a->refCount);
00250   return a->mode;
00251 }
00252 
00253 
00254 
00255 uint8_t *GWEN_Crypt_CryptAlgo_GetInitVectorPtr(const GWEN_CRYPT_CRYPTALGO *a){
00256   assert(a);
00257   assert(a->refCount);
00258   return a->pInitVector;
00259 }
00260 
00261 
00262 
00263 uint32_t GWEN_Crypt_CryptAlgo_GetInitVectorLen(const GWEN_CRYPT_CRYPTALGO *a){
00264   assert(a);
00265   assert(a->refCount);
00266   return a->lInitVector;
00267 }
00268 
00269 
00270 
00271 int GWEN_Crypt_CryptAlgo_SetInitVector(GWEN_CRYPT_CRYPTALGO *a,
00272                                        const uint8_t *pv,
00273                                        uint32_t lv) {
00274   uint8_t *nv=NULL;
00275 
00276   assert(a);
00277   assert(a->refCount);
00278 
00279   if (pv && lv) {
00280     nv=(uint8_t*) malloc(lv);
00281     if (nv==NULL)
00282       return GWEN_ERROR_MEMORY_FULL;
00283     memmove(nv, pv, lv);
00284   }
00285 
00286   if (a->pInitVector && a->lInitVector)
00287     free(a->pInitVector);
00288 
00289   a->pInitVector=nv;
00290   a->lInitVector=(nv!=NULL)?lv:0;
00291 
00292   return 0;
00293 }
00294 
00295 
00296 
00297 int GWEN_Crypt_CryptAlgo_GetChunkSize(const GWEN_CRYPT_CRYPTALGO *a) {
00298   assert(a);
00299   assert(a->refCount);
00300 
00301   return a->chunkSize;
00302 }
00303 
00304 
00305 
00306 void GWEN_Crypt_CryptAlgo_SetChunkSize(GWEN_CRYPT_CRYPTALGO *a, int s) {
00307   assert(a);
00308   assert(a->refCount);
00309 
00310   a->chunkSize=s;
00311 }
00312 
00313 
00314 
00315 int GWEN_Crypt_CryptAlgo_GetKeySizeInBits(const GWEN_CRYPT_CRYPTALGO *a) {
00316   assert(a);
00317   assert(a->refCount);
00318 
00319   return a->keySizeInBits;
00320 }
00321 
00322 
00323 
00324 void GWEN_Crypt_CryptAlgo_SetKeySizeInBits(GWEN_CRYPT_CRYPTALGO *a, int s) {
00325   assert(a);
00326   assert(a->refCount);
00327 
00328   a->keySizeInBits=s;
00329 }
00330 
00331 
00332 
00333 
00334 
00335 
00336 

Generated by  doxygen 1.6.2