Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifdef HAVE_CONFIG_H
00011 # include <config.h>
00012 #endif
00013
00014 #define DISABLE_DEBUGLOG
00015
00016
00017 #include "hashalgo_p.h"
00018 #include <gwenhywfar/misc.h>
00019 #include <gwenhywfar/debug.h>
00020
00021
00022
00023 GWEN_LIST2_FUNCTIONS(GWEN_CRYPT_HASHALGO, GWEN_Crypt_HashAlgo)
00024
00025
00026
00027 GWEN_CRYPT_HASHALGOID GWEN_Crypt_HashAlgoId_fromString(const char *s) {
00028 assert(s);
00029 if (strcasecmp(s, "none")==0)
00030 return GWEN_Crypt_HashAlgoId_None;
00031 else if (strcasecmp(s, "sha1")==0)
00032 return GWEN_Crypt_HashAlgoId_Sha1;
00033 else if (strcasecmp(s, "rmd160")==0)
00034 return GWEN_Crypt_HashAlgoId_Rmd160;
00035 else if (strcasecmp(s, "md5")==0)
00036 return GWEN_Crypt_HashAlgoId_Md5;
00037 else if (strcasecmp(s, "any")==0)
00038 return GWEN_Crypt_HashAlgoId_Any;
00039 else if (strcasecmp(s, "sha256")==0)
00040 return GWEN_Crypt_HashAlgoId_Sha256;
00041 return GWEN_Crypt_HashAlgoId_Unknown;
00042 }
00043
00044
00045
00046 const char *GWEN_Crypt_HashAlgoId_toString(GWEN_CRYPT_HASHALGOID a) {
00047 switch(a) {
00048 case GWEN_Crypt_HashAlgoId_None:
00049 return "none";
00050 case GWEN_Crypt_HashAlgoId_Sha1:
00051 return "sha1";
00052 case GWEN_Crypt_HashAlgoId_Rmd160:
00053 return "rmd160";
00054 case GWEN_Crypt_HashAlgoId_Md5:
00055 return "md5";
00056 case GWEN_Crypt_HashAlgoId_Sha256:
00057 return "sha256";
00058 case GWEN_Crypt_HashAlgoId_Any:
00059 return "any";
00060 default:
00061 return "unknown";
00062 }
00063 }
00064
00065
00066
00067 GWEN_CRYPT_HASHALGO *GWEN_Crypt_HashAlgo_new(GWEN_CRYPT_HASHALGOID id) {
00068 GWEN_CRYPT_HASHALGO *a;
00069
00070 GWEN_NEW_OBJECT(GWEN_CRYPT_HASHALGO, a);
00071 a->refCount=1;
00072
00073 a->id=id;
00074
00075 return a;
00076 }
00077
00078
00079
00080 void GWEN_Crypt_HashAlgo_Attach(GWEN_CRYPT_HASHALGO *a) {
00081 assert(a);
00082 assert(a->refCount);
00083 a->refCount++;
00084 }
00085
00086
00087
00088 GWEN_CRYPT_HASHALGO *GWEN_Crypt_HashAlgo_fromDb(GWEN_DB_NODE *db) {
00089 const char *s;
00090
00091 assert(db);
00092 s=GWEN_DB_GetCharValue(db, "id", 0, NULL);
00093 if (s) {
00094 GWEN_CRYPT_HASHALGO *a;
00095 GWEN_CRYPT_HASHALGOID id;
00096 const void *p;
00097 unsigned int len;
00098
00099 id=GWEN_Crypt_HashAlgoId_fromString(s);
00100 if (id==GWEN_Crypt_HashAlgoId_Unknown) {
00101 DBG_INFO(GWEN_LOGDOMAIN, "Unknown hashalgo id [%s]", s);
00102 return NULL;
00103 }
00104 a=GWEN_Crypt_HashAlgo_new(id);
00105 assert(a);
00106 p=GWEN_DB_GetBinValue(db, "initVector", 0, NULL, 0, &len);
00107 if (p && len)
00108 GWEN_Crypt_HashAlgo_SetInitVector(a, p, len);
00109
00110 return a;
00111 }
00112 else {
00113 DBG_INFO(GWEN_LOGDOMAIN, "Missing hashalgo id");
00114 return NULL;
00115 }
00116 }
00117
00118
00119
00120 int GWEN_Crypt_HashAlgo_toDb(const GWEN_CRYPT_HASHALGO *a, GWEN_DB_NODE *db) {
00121 assert(a);
00122 assert(a->refCount);
00123
00124 GWEN_DB_SetCharValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS,
00125 "id",
00126 GWEN_Crypt_HashAlgoId_toString(a->id));
00127 if (a->pInitVector && a->lInitVector)
00128 GWEN_DB_SetBinValue(db, GWEN_DB_FLAGS_OVERWRITE_VARS,
00129 "initVector",
00130 a->pInitVector, a->lInitVector);
00131
00132 return 0;
00133 }
00134
00135
00136
00137 GWEN_CRYPT_HASHALGO *GWEN_Crypt_HashAlgo_dup(const GWEN_CRYPT_HASHALGO *na) {
00138 GWEN_CRYPT_HASHALGO *a;
00139
00140 assert(na);
00141 a=GWEN_Crypt_HashAlgo_new(na->id);
00142 if (na->pInitVector && na->lInitVector) {
00143 a->pInitVector=(uint8_t*) malloc(na->lInitVector);
00144 if (a->pInitVector==NULL) {
00145 GWEN_Crypt_HashAlgo_free(a);
00146 return NULL;
00147 }
00148 else
00149 memmove(a->pInitVector, na->pInitVector, na->lInitVector);
00150 a->lInitVector=na->lInitVector;
00151 }
00152
00153 return a;
00154 }
00155
00156
00157
00158 void GWEN_Crypt_HashAlgo_free(GWEN_CRYPT_HASHALGO *a) {
00159 if (a) {
00160 assert(a->refCount);
00161 if (a->refCount==1) {
00162 if (a->pInitVector) {
00163 free(a->pInitVector);
00164 a->pInitVector=NULL;
00165 }
00166 a->refCount--;
00167 GWEN_FREE_OBJECT(a);
00168 }
00169 else {
00170 a->refCount--;
00171 }
00172 }
00173 }
00174
00175
00176
00177 GWEN_CRYPT_HASHALGOID GWEN_Crypt_HashAlgo_GetId(const GWEN_CRYPT_HASHALGO *a){
00178 assert(a);
00179 assert(a->refCount);
00180 return a->id;
00181 }
00182
00183
00184
00185 uint8_t *GWEN_Crypt_HashAlgo_GetInitVectorPtr(const GWEN_CRYPT_HASHALGO *a){
00186 assert(a);
00187 assert(a->refCount);
00188 return a->pInitVector;
00189 }
00190
00191
00192
00193 uint32_t GWEN_Crypt_HashAlgo_GetInitVectorLen(const GWEN_CRYPT_HASHALGO *a){
00194 assert(a);
00195 assert(a->refCount);
00196 return a->lInitVector;
00197 }
00198
00199
00200
00201 int GWEN_Crypt_HashAlgo_SetInitVector(GWEN_CRYPT_HASHALGO *a,
00202 const uint8_t *pv,
00203 uint32_t lv) {
00204 uint8_t *nv=NULL;
00205
00206 assert(a);
00207 assert(a->refCount);
00208
00209 if (pv && lv) {
00210 nv=(uint8_t*) malloc(lv);
00211 if (nv==NULL)
00212 return GWEN_ERROR_MEMORY_FULL;
00213 memmove(nv, pv, lv);
00214 }
00215
00216 if (a->pInitVector && a->lInitVector)
00217 free(a->pInitVector);
00218
00219 a->pInitVector=nv;
00220 a->lInitVector=(nv!=NULL)?lv:0;
00221
00222 return 0;
00223 }
00224
00225
00226
00227