gwenhywfar  5.10.1
hashalgo.c
Go to the documentation of this file.
1 /***************************************************************************
2  begin : Wed Mar 16 2005
3  copyright : (C) 2005 by Martin Preuss
4  email : martin@libchipcard.de
5 
6  ***************************************************************************
7  * Please see toplevel file COPYING for license details *
8  ***************************************************************************/
9 
10 #ifdef HAVE_CONFIG_H
11 # include <config.h>
12 #endif
13 
14 #define DISABLE_DEBUGLOG
15 
16 
17 #include "hashalgo_p.h"
18 #include <gwenhywfar/misc.h>
19 #include <gwenhywfar/debug.h>
20 
21 
22 
23 GWEN_LIST2_FUNCTIONS(GWEN_CRYPT_HASHALGO, GWEN_Crypt_HashAlgo)
24 
25 
26 
28 {
29  assert(s);
30  if (strcasecmp(s, "none")==0)
31  return GWEN_Crypt_HashAlgoId_None;
32  else if (strcasecmp(s, "sha1")==0)
33  return GWEN_Crypt_HashAlgoId_Sha1;
34  else if (strcasecmp(s, "rmd160")==0)
35  return GWEN_Crypt_HashAlgoId_Rmd160;
36  else if (strcasecmp(s, "md5")==0)
37  return GWEN_Crypt_HashAlgoId_Md5;
38  else if (strcasecmp(s, "any")==0)
39  return GWEN_Crypt_HashAlgoId_Any;
40  else if (strcasecmp(s, "sha256")==0)
41  return GWEN_Crypt_HashAlgoId_Sha256;
42  return GWEN_Crypt_HashAlgoId_Unknown;
43 }
44 
45 
46 
48 {
49  switch (a) {
50  case GWEN_Crypt_HashAlgoId_None:
51  return "none";
52  case GWEN_Crypt_HashAlgoId_Sha1:
53  return "sha1";
54  case GWEN_Crypt_HashAlgoId_Rmd160:
55  return "rmd160";
56  case GWEN_Crypt_HashAlgoId_Md5:
57  return "md5";
58  case GWEN_Crypt_HashAlgoId_Sha256:
59  return "sha256";
60  case GWEN_Crypt_HashAlgoId_Any:
61  return "any";
62  default:
63  return "unknown";
64  }
65 }
66 
67 
68 
70 {
72 
74  a->refCount=1;
75 
76  a->id=id;
77 
78  return a;
79 }
80 
81 
82 
84 {
85  assert(a);
86  assert(a->refCount);
87  a->refCount++;
88 }
89 
90 
91 
93 {
94  const char *s;
95 
96  assert(db);
97  s=GWEN_DB_GetCharValue(db, "id", 0, NULL);
98  if (s) {
101  const void *p;
102  unsigned int len;
103 
105  if (id==GWEN_Crypt_HashAlgoId_Unknown) {
106  DBG_INFO(GWEN_LOGDOMAIN, "Unknown hashalgo id [%s]", s);
107  return NULL;
108  }
110  assert(a);
111  p=GWEN_DB_GetBinValue(db, "initVector", 0, NULL, 0, &len);
112  if (p && len)
114 
115  return a;
116  }
117  else {
118  DBG_INFO(GWEN_LOGDOMAIN, "Missing hashalgo id");
119  return NULL;
120  }
121 }
122 
123 
124 
126 {
127  assert(a);
128  assert(a->refCount);
129 
131  "id",
133  if (a->pInitVector && a->lInitVector)
135  "initVector",
136  a->pInitVector, a->lInitVector);
137 
138  return 0;
139 }
140 
141 
142 
144 {
146 
147  assert(na);
148  a=GWEN_Crypt_HashAlgo_new(na->id);
149  if (na->pInitVector && na->lInitVector) {
150  a->pInitVector=(uint8_t *) malloc(na->lInitVector);
151  if (a->pInitVector==NULL) {
153  return NULL;
154  }
155  else
156  memmove(a->pInitVector, na->pInitVector, na->lInitVector);
157  a->lInitVector=na->lInitVector;
158  }
159 
160  return a;
161 }
162 
163 
164 
166 {
167  if (a) {
168  assert(a->refCount);
169  if (a->refCount==1) {
170  if (a->pInitVector) {
171  free(a->pInitVector);
172  a->pInitVector=NULL;
173  }
174  a->refCount--;
175  GWEN_FREE_OBJECT(a);
176  }
177  else {
178  a->refCount--;
179  }
180  }
181 }
182 
183 
184 
186 {
187  assert(a);
188  assert(a->refCount);
189  return a->id;
190 }
191 
192 
193 
195 {
196  assert(a);
197  assert(a->refCount);
198  return a->pInitVector;
199 }
200 
201 
202 
204 {
205  assert(a);
206  assert(a->refCount);
207  return a->lInitVector;
208 }
209 
210 
211 
213  const uint8_t *pv,
214  uint32_t lv)
215 {
216  uint8_t *nv=NULL;
217 
218  assert(a);
219  assert(a->refCount);
220 
221  if (pv && lv) {
222  nv=(uint8_t *) malloc(lv);
223  if (nv==NULL)
224  return GWEN_ERROR_MEMORY_FULL;
225  memmove(nv, pv, lv);
226  }
227 
228  if (a->pInitVector && a->lInitVector)
229  free(a->pInitVector);
230 
231  a->pInitVector=nv;
232  a->lInitVector=(nv!=NULL)?lv:0;
233 
234  return 0;
235 }
236 
237 
238 
239 
int GWEN_Crypt_HashAlgo_SetInitVector(GWEN_CRYPT_HASHALGO *a, const uint8_t *pv, uint32_t lv)
Definition: hashalgo.c:212
GWEN_CRYPT_HASHALGOID
Definition: hashalgo.h:48
#define GWEN_DB_FLAGS_OVERWRITE_VARS
Definition: db.h:121
#define GWEN_LIST2_FUNCTIONS(t, pr)
Definition: list2.h:99
struct GWEN_DB_NODE GWEN_DB_NODE
Definition: db.h:228
GWEN_CRYPT_HASHALGOID GWEN_Crypt_HashAlgo_GetId(const GWEN_CRYPT_HASHALGO *a)
Definition: hashalgo.c:185
#define GWEN_FREE_OBJECT(varname)
Definition: memory.h:61
const char * GWEN_Crypt_HashAlgoId_toString(GWEN_CRYPT_HASHALGOID a)
Definition: hashalgo.c:47
#define NULL
Definition: binreloc.c:300
#define GWEN_LOGDOMAIN
Definition: logger.h:35
int GWEN_Crypt_HashAlgo_toDb(const GWEN_CRYPT_HASHALGO *a, GWEN_DB_NODE *db)
Definition: hashalgo.c:125
GWEN_CRYPT_HASHALGO * GWEN_Crypt_HashAlgo_fromDb(GWEN_DB_NODE *db)
Definition: hashalgo.c:92
uint8_t * GWEN_Crypt_HashAlgo_GetInitVectorPtr(const GWEN_CRYPT_HASHALGO *a)
Definition: hashalgo.c:194
int GWEN_DB_SetBinValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const void *val, unsigned int valSize)
Definition: db.c:1269
#define GWEN_NEW_OBJECT(typ, varname)
Definition: memory.h:55
const void * GWEN_DB_GetBinValue(GWEN_DB_NODE *n, const char *path, int idx, const void *defVal, unsigned int defValSize, unsigned int *returnValueSize)
Definition: db.c:1237
void GWEN_Crypt_HashAlgo_free(GWEN_CRYPT_HASHALGO *a)
Definition: hashalgo.c:165
const char * GWEN_DB_GetCharValue(GWEN_DB_NODE *n, const char *path, int idx, const char *defVal)
Definition: db.c:971
uint32_t GWEN_Crypt_HashAlgo_GetInitVectorLen(const GWEN_CRYPT_HASHALGO *a)
Definition: hashalgo.c:203
GWEN_CRYPT_HASHALGO * GWEN_Crypt_HashAlgo_dup(const GWEN_CRYPT_HASHALGO *na)
Definition: hashalgo.c:143
GWEN_CRYPT_HASHALGO * GWEN_Crypt_HashAlgo_new(GWEN_CRYPT_HASHALGOID id)
Definition: hashalgo.c:69
int GWEN_DB_SetCharValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const char *val)
Definition: db.c:997
#define DBG_INFO(dbg_logger, format, args...)
Definition: debug.h:181
void GWEN_Crypt_HashAlgo_Attach(GWEN_CRYPT_HASHALGO *a)
Definition: hashalgo.c:83
struct GWEN_CRYPT_HASHALGO GWEN_CRYPT_HASHALGO
Definition: hashalgo.h:21
#define GWEN_ERROR_MEMORY_FULL
Definition: error.h:77
GWEN_CRYPT_HASHALGOID GWEN_Crypt_HashAlgoId_fromString(const char *s)
Definition: hashalgo.c:27