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 
00161     return a;
00162   }
00163   else {
00164     DBG_INFO(GWEN_LOGDOMAIN, "Missing cryptalgo id");
00165     return NULL;
00166   }
00167 }
00168 
00169 
00170 
00171 int GWEN_Crypt_CryptAlgo_toDb(const GWEN_CRYPT_CRYPTALGO *a, GWEN_DB_NODE *db) {
00172   assert(a);
00173   assert(a->refCount);
00174 
00175   GWEN_DB_SetCharValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS,
00176                        "id",
00177                        GWEN_Crypt_CryptAlgoId_toString(a->id));
00178   GWEN_DB_SetCharValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS,
00179                        "mode",
00180                        GWEN_Crypt_CryptMode_toString(a->id));
00181   if (a->pInitVector && a->lInitVector)
00182     GWEN_DB_SetBinValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS,
00183                         "initVector",
00184                         a->pInitVector, a->lInitVector);
00185   GWEN_DB_SetIntValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS,
00186                       "chunkSize",
00187                       a->chunkSize);
00188 
00189   return 0;
00190 }
00191 
00192 
00193 
00194 GWEN_CRYPT_CRYPTALGO *GWEN_Crypt_CryptAlgo_dup(const GWEN_CRYPT_CRYPTALGO *na) {
00195   GWEN_CRYPT_CRYPTALGO *a;
00196 
00197   assert(na);
00198   a=GWEN_Crypt_CryptAlgo_new(na->id, na->mode);
00199   if (na->pInitVector && na->lInitVector) {
00200     a->pInitVector=(uint8_t*) malloc(na->lInitVector);
00201     if (a->pInitVector==NULL) {
00202       GWEN_Crypt_CryptAlgo_free(a);
00203       return NULL;
00204     }
00205     else
00206       memmove(a->pInitVector, na->pInitVector, na->lInitVector);
00207     a->lInitVector=na->lInitVector;
00208   }
00209   a->chunkSize=na->chunkSize;
00210 
00211   return a;
00212 }
00213 
00214 
00215 
00216 void GWEN_Crypt_CryptAlgo_free(GWEN_CRYPT_CRYPTALGO *a) {
00217   if (a) {
00218     assert(a->refCount);
00219     if (a->refCount==1) {
00220       if (a->pInitVector) {
00221         free(a->pInitVector);
00222         a->pInitVector=NULL;
00223       }
00224       a->refCount--;
00225       GWEN_FREE_OBJECT(a);
00226     }
00227     else {
00228       a->refCount--;
00229     }
00230   }
00231 }
00232 
00233 
00234 
00235 GWEN_CRYPT_CRYPTALGOID GWEN_Crypt_CryptAlgo_GetId(const GWEN_CRYPT_CRYPTALGO *a){
00236   assert(a);
00237   assert(a->refCount);
00238   return a->id;
00239 }
00240 
00241 
00242 
00243 GWEN_CRYPT_CRYPTMODE GWEN_Crypt_CryptAlgo_GetMode(const GWEN_CRYPT_CRYPTALGO *a) {
00244   assert(a);
00245   assert(a->refCount);
00246   return a->mode;
00247 }
00248 
00249 
00250 
00251 uint8_t *GWEN_Crypt_CryptAlgo_GetInitVectorPtr(const GWEN_CRYPT_CRYPTALGO *a){
00252   assert(a);
00253   assert(a->refCount);
00254   return a->pInitVector;
00255 }
00256 
00257 
00258 
00259 uint32_t GWEN_Crypt_CryptAlgo_GetInitVectorLen(const GWEN_CRYPT_CRYPTALGO *a){
00260   assert(a);
00261   assert(a->refCount);
00262   return a->lInitVector;
00263 }
00264 
00265 
00266 
00267 int GWEN_Crypt_CryptAlgo_SetInitVector(GWEN_CRYPT_CRYPTALGO *a,
00268                                        const uint8_t *pv,
00269                                        uint32_t lv) {
00270   uint8_t *nv=NULL;
00271 
00272   assert(a);
00273   assert(a->refCount);
00274 
00275   if (pv && lv) {
00276     nv=(uint8_t*) malloc(lv);
00277     if (nv==NULL)
00278       return GWEN_ERROR_MEMORY_FULL;
00279     memmove(nv, pv, lv);
00280   }
00281 
00282   if (a->pInitVector && a->lInitVector)
00283     free(a->pInitVector);
00284 
00285   a->pInitVector=nv;
00286   a->lInitVector=(nv!=NULL)?lv:0;
00287 
00288   return 0;
00289 }
00290 
00291 
00292 
00293 int GWEN_Crypt_CryptAlgo_GetChunkSize(const GWEN_CRYPT_CRYPTALGO *a) {
00294   assert(a);
00295   assert(a->refCount);
00296 
00297   return a->chunkSize;
00298 }
00299 
00300 
00301 
00302 void GWEN_Crypt_CryptAlgo_SetChunkSize(GWEN_CRYPT_CRYPTALGO *a, int s) {
00303   assert(a);
00304   assert(a->refCount);
00305 
00306   a->chunkSize=s;
00307 }
00308 
00309 
00310 
00311 
00312 
00313 
00314 

Generated on Wed Jul 9 13:12:27 2008 for gwenhywfar by  doxygen 1.5.6