gwenhywfar 5.14.1
cryptalgo.c
Go to the documentation of this file.
1/***************************************************************************
2 begin : Wed Mar 16 2005
3 copyright : (C) 2005-2010 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
15#define DISABLE_DEBUGLOG
16
17
18#include "cryptalgo_p.h"
19#include <gwenhywfar/misc.h>
20#include <gwenhywfar/debug.h>
21
22
23
24GWEN_LIST2_FUNCTIONS(GWEN_CRYPT_CRYPTALGO, GWEN_Crypt_CryptAlgo)
25
26
27
29{
30 assert(s);
31 if (strcasecmp(s, "none")==0)
32 return GWEN_Crypt_CryptAlgoId_None;
33 else if (strcasecmp(s, "rsa")==0)
34 return GWEN_Crypt_CryptAlgoId_Rsa;
35 else if (strcasecmp(s, "dsa")==0)
36 return GWEN_Crypt_CryptAlgoId_Dsa;
37 else if (strcasecmp(s, "des")==0)
38 return GWEN_Crypt_CryptAlgoId_Des;
39 else if (strcasecmp(s, "des_3k")==0 ||
40 strcasecmp(s, "des3k")==0)
41 return GWEN_Crypt_CryptAlgoId_Des3K;
42 else if (strcasecmp(s, "blowfish")==0)
43 return GWEN_Crypt_CryptAlgoId_BlowFish;
44 else if (strcasecmp(s, "aes128")==0)
45 return GWEN_Crypt_CryptAlgoId_Aes128;
46 else if (strcasecmp(s, "any")==0)
47 return GWEN_Crypt_CryptAlgoId_Any;
48 return GWEN_Crypt_CryptAlgoId_Unknown;
49}
50
51
52
54{
55 switch (a) {
56 case GWEN_Crypt_CryptAlgoId_None:
57 return "none";
58 case GWEN_Crypt_CryptAlgoId_Rsa:
59 return "rsa";
60 case GWEN_Crypt_CryptAlgoId_Dsa:
61 return "dsa";
62 case GWEN_Crypt_CryptAlgoId_Des:
63 return "des";
64 case GWEN_Crypt_CryptAlgoId_Des3K:
65 return "des_3k";
66 case GWEN_Crypt_CryptAlgoId_BlowFish:
67 return "blowfish";
68 case GWEN_Crypt_CryptAlgoId_Aes128:
69 return "aes128";
70 case GWEN_Crypt_CryptAlgoId_Any:
71 return "any";
72 default:
73 return "unknown";
74 }
75}
76
77
78
80{
81 assert(s);
82 if (strcasecmp(s, "none")==0)
84 else if (strcasecmp(s, "ecb")==0)
86 else if (strcasecmp(s, "cfb")==0)
88 else if (strcasecmp(s, "cbc")==0)
91}
92
93
94
96{
97 switch (m) {
99 return "none";
101 return "ecb";
103 return "cfb";
105 return "cbc";
106 default:
107 return "unknown";
108 }
109}
110
111
112
115{
117
119 a->refCount=1;
120
121 a->id=id;
122 a->mode=m;
123
124 return a;
125}
126
127
128
130{
131 assert(a);
132 assert(a->refCount);
133 a->refCount++;
134}
135
136
137
139{
140 const char *s;
141
142 assert(db);
143 s=GWEN_DB_GetCharValue(db, "id", 0, NULL);
144 if (s) {
148 const void *p;
149 unsigned int len;
150
152 if (id==GWEN_Crypt_CryptAlgoId_Unknown) {
153 DBG_INFO(GWEN_LOGDOMAIN, "Unknown cryptalgo id [%s]", s);
154 return NULL;
155 }
156
157 s=GWEN_DB_GetCharValue(db, "mode", 0, NULL);
158 if (s)
160 else {
161 DBG_INFO(GWEN_LOGDOMAIN, "Missing crypt mode");
162 return NULL;
163 }
164
166 assert(a);
167 p=GWEN_DB_GetBinValue(db, "initVector", 0, NULL, 0, &len);
168 if (p && len)
170
171 a->chunkSize=GWEN_DB_GetIntValue(db, "chunkSize", 0, 0);
172 a->keySizeInBits=GWEN_DB_GetIntValue(db, "keySizeInBits", 0, 0);
173
174 return a;
175 }
176 else {
177 DBG_INFO(GWEN_LOGDOMAIN, "Missing cryptalgo id");
178 return NULL;
179 }
180}
181
182
183
185{
186 assert(a);
187 assert(a->refCount);
188
190 "id",
193 "mode",
195 if (a->pInitVector && a->lInitVector)
197 "initVector",
198 a->pInitVector, a->lInitVector);
200 "chunkSize",
201 a->chunkSize);
203 "keySizeInBits",
204 a->keySizeInBits);
205
206 return 0;
207}
208
209
210
212{
214
215 assert(na);
216 a=GWEN_Crypt_CryptAlgo_new(na->id, na->mode);
217 if (na->pInitVector && na->lInitVector) {
218 a->pInitVector=(uint8_t *) malloc(na->lInitVector);
219 if (a->pInitVector==NULL) {
221 return NULL;
222 }
223 else
224 memmove(a->pInitVector, na->pInitVector, na->lInitVector);
225 a->lInitVector=na->lInitVector;
226 }
227 a->chunkSize=na->chunkSize;
228 a->keySizeInBits=na->keySizeInBits;
229 return a;
230}
231
232
233
235{
236 if (a) {
237 assert(a->refCount);
238 if (a->refCount==1) {
239 if (a->pInitVector) {
240 free(a->pInitVector);
241 a->pInitVector=NULL;
242 }
243 a->refCount--;
245 }
246 else {
247 a->refCount--;
248 }
249 }
250}
251
252
253
255{
256 assert(a);
257 assert(a->refCount);
258 return a->id;
259}
260
261
262
264{
265 assert(a);
266 assert(a->refCount);
267 return a->mode;
268}
269
270
271
273{
274 assert(a);
275 assert(a->refCount);
276 return a->pInitVector;
277}
278
279
280
282{
283 assert(a);
284 assert(a->refCount);
285 return a->lInitVector;
286}
287
288
289
291 const uint8_t *pv,
292 uint32_t lv)
293{
294 uint8_t *nv=NULL;
295
296 assert(a);
297 assert(a->refCount);
298
299 if (pv && lv) {
300 nv=(uint8_t *) malloc(lv);
301 if (nv==NULL)
303 memmove(nv, pv, lv);
304 }
305
306 if (a->pInitVector && a->lInitVector)
307 free(a->pInitVector);
308
309 a->pInitVector=nv;
310 a->lInitVector=(nv!=NULL)?lv:0;
311
312 return 0;
313}
314
315
316
318{
319 assert(a);
320 assert(a->refCount);
321
322 return a->chunkSize;
323}
324
325
326
328{
329 assert(a);
330 assert(a->refCount);
331
332 a->chunkSize=s;
333}
334
335
336
338{
339 assert(a);
340 assert(a->refCount);
341
342 return a->keySizeInBits;
343}
344
345
346
348{
349 assert(a);
350 assert(a->refCount);
351
352 a->keySizeInBits=s;
353}
354
355
356
357
358
359
360
#define NULL
Definition binreloc.c:300
int GWEN_Crypt_CryptAlgo_SetInitVector(GWEN_CRYPT_CRYPTALGO *a, const uint8_t *pv, uint32_t lv)
Definition cryptalgo.c:290
GWEN_CRYPT_CRYPTALGO * GWEN_Crypt_CryptAlgo_dup(const GWEN_CRYPT_CRYPTALGO *na)
Definition cryptalgo.c:211
const char * GWEN_Crypt_CryptAlgoId_toString(GWEN_CRYPT_CRYPTALGOID a)
Definition cryptalgo.c:53
void GWEN_Crypt_CryptAlgo_SetChunkSize(GWEN_CRYPT_CRYPTALGO *a, int s)
Definition cryptalgo.c:327
void GWEN_Crypt_CryptAlgo_SetKeySizeInBits(GWEN_CRYPT_CRYPTALGO *a, int s)
Definition cryptalgo.c:347
uint8_t * GWEN_Crypt_CryptAlgo_GetInitVectorPtr(const GWEN_CRYPT_CRYPTALGO *a)
Definition cryptalgo.c:272
int GWEN_Crypt_CryptAlgo_GetKeySizeInBits(const GWEN_CRYPT_CRYPTALGO *a)
Definition cryptalgo.c:337
uint32_t GWEN_Crypt_CryptAlgo_GetInitVectorLen(const GWEN_CRYPT_CRYPTALGO *a)
Definition cryptalgo.c:281
GWEN_CRYPT_CRYPTMODE GWEN_Crypt_CryptMode_fromString(const char *s)
Definition cryptalgo.c:79
void GWEN_Crypt_CryptAlgo_free(GWEN_CRYPT_CRYPTALGO *a)
Definition cryptalgo.c:234
int GWEN_Crypt_CryptAlgo_GetChunkSize(const GWEN_CRYPT_CRYPTALGO *a)
Definition cryptalgo.c:317
int GWEN_Crypt_CryptAlgo_toDb(const GWEN_CRYPT_CRYPTALGO *a, GWEN_DB_NODE *db)
Definition cryptalgo.c:184
GWEN_CRYPT_CRYPTALGO * GWEN_Crypt_CryptAlgo_fromDb(GWEN_DB_NODE *db)
Definition cryptalgo.c:138
void GWEN_Crypt_CryptAlgo_Attach(GWEN_CRYPT_CRYPTALGO *a)
Definition cryptalgo.c:129
const char * GWEN_Crypt_CryptMode_toString(GWEN_CRYPT_CRYPTMODE m)
Definition cryptalgo.c:95
GWEN_CRYPT_CRYPTALGO * GWEN_Crypt_CryptAlgo_new(GWEN_CRYPT_CRYPTALGOID id, GWEN_CRYPT_CRYPTMODE m)
Definition cryptalgo.c:113
GWEN_CRYPT_CRYPTALGOID GWEN_Crypt_CryptAlgoId_fromString(const char *s)
Definition cryptalgo.c:28
GWEN_CRYPT_CRYPTMODE GWEN_Crypt_CryptAlgo_GetMode(const GWEN_CRYPT_CRYPTALGO *a)
Definition cryptalgo.c:263
GWEN_CRYPT_CRYPTALGOID GWEN_Crypt_CryptAlgo_GetId(const GWEN_CRYPT_CRYPTALGO *a)
Definition cryptalgo.c:254
GWEN_CRYPT_CRYPTALGOID
Definition cryptalgo.h:52
struct GWEN_CRYPT_CRYPTALGO GWEN_CRYPT_CRYPTALGO
Definition cryptalgo.h:20
GWEN_CRYPT_CRYPTMODE
Definition cryptalgo.h:55
@ GWEN_Crypt_CryptMode_Unknown
Definition cryptalgo.h:56
@ GWEN_Crypt_CryptMode_Ecb
Definition cryptalgo.h:58
@ GWEN_Crypt_CryptMode_Cbc
Definition cryptalgo.h:60
@ GWEN_Crypt_CryptMode_Cfb
Definition cryptalgo.h:59
@ GWEN_Crypt_CryptMode_None
Definition cryptalgo.h:57
const char * GWEN_DB_GetCharValue(GWEN_DB_NODE *n, const char *path, int idx, const char *defVal)
Definition db.c:971
int GWEN_DB_SetIntValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, int val)
Definition db.c:1202
int GWEN_DB_SetCharValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const char *val)
Definition db.c:997
int GWEN_DB_SetBinValue(GWEN_DB_NODE *n, uint32_t flags, const char *path, const void *val, unsigned int valSize)
Definition db.c:1269
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
int GWEN_DB_GetIntValue(GWEN_DB_NODE *n, const char *path, int idx, int defVal)
Definition db.c:1163
#define GWEN_DB_FLAGS_OVERWRITE_VARS
Definition db.h:121
struct GWEN_DB_NODE GWEN_DB_NODE
Definition db.h:228
#define DBG_INFO(dbg_logger, format,...)
Definition debug.h:181
#define GWEN_ERROR_MEMORY_FULL
Definition error.h:77
#define GWEN_LIST2_FUNCTIONS(t, pr)
Definition list2.h:99
#define GWEN_LOGDOMAIN
Definition logger.h:32
#define GWEN_FREE_OBJECT(varname)
Definition memory.h:61
#define GWEN_NEW_OBJECT(typ, varname)
Definition memory.h:55