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