Tagging module util-vserver - util-vserver-0.30.215-6
[util-vserver.git] / lib_internal / crypto-wrapper-beecrypt.h
1 /*      --*- c -*--
2  * Copyright (C) 2008 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 and/or 3 of the License.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 #ifndef H_UTIL_VSERVER_LIB_INTERNAL_CRYPTO_WRAPPER_BEECRYPT_H
18 #define H_UTIL_VSERVER_LIB_INTERNAL_CRYPTO_WRAPPER_BEECRYPT_H
19
20 #include <beecrypt/beecrypt.h>
21 #include <ctype.h>
22
23 typedef hashFunction                    ensc_hash_method;
24 typedef hashFunctionContext             ensc_hash_context;
25
26 inline static void
27 ensc_crypto_init(void)
28 {
29 }
30
31 inline static ensc_hash_method const *
32 ensc_crypto_hash_get_default(void)
33 {
34         return hashFunctionDefault();
35 }
36
37 inline static ensc_hash_method const *
38 ensc_crypto_hash_find(char const *id_c)
39 {
40         char                    *id = strdupa(id_c);
41         char                    *ptr = id;
42         char const              *name;
43
44         while (*ptr) {
45                 *ptr = tolower(*ptr);
46                 ++ptr;
47         }
48
49         ptr = id;
50         while ((ptr=strchr(ptr, '-'))!=NULL)
51                 memmove(ptr, ptr+1, strlen(ptr));
52         
53         if (strcmp(id, "md2")==0)
54                 name = "MD2";
55         else if (strcmp(id, "md5")==0)
56                 name = "MD5";
57         else if (strcmp(id, "sha1")==0)
58                 name = "SHA-1";
59         else if (strcasecmp(id, "sha256")==0)
60                 name = "SHA-256";
61 #if 0
62         /* sha-384 in beecrypt seems to be broken; digestsize is reported as
63          * 64 there although 48 is the correct value */
64         else if (strcasecmp(id, "sha384")==0)
65                 name = "SHA-384";
66 #endif
67         else if (strcasecmp(id, "sha512")==0)
68                 name = "SHA-512";
69         else
70                 name = NULL;
71                 
72         return hashFunctionFind(name);
73 }
74
75 inline static char const *
76 ensc_crypto_hash_get_name(ensc_hash_method const *m)
77 {
78         return m->name;
79 }
80
81 inline static size_t
82 ensc_crypto_hash_get_digestsize(ensc_hash_method const *m)
83 {
84         return m->digestsize;
85 }
86
87
88
89 inline static size_t
90 ensc_crypto_hashctx_get_digestsize(ensc_hash_context const *ctx)
91 {
92         return ensc_crypto_hash_get_digestsize(ctx->algo);
93 }
94
95 inline static int
96 ensc_crypto_hashctx_get_digest(ensc_hash_context *ctx, void *result,
97                                size_t *res_len, size_t UNUSED max_res_len)
98 {
99         int     rc = hashFunctionContextDigest(ctx, result);
100         if (res_len)
101                 *res_len = ctx->algo->digestsize;
102
103         return rc;
104 }
105
106 inline static int
107 ensc_crypto_hashctx_update(ensc_hash_context *ctx, void const *src, size_t len)
108 {
109         return hashFunctionContextUpdate(ctx, src, len);
110 }
111
112 inline static int
113 ensc_crypto_hashctx_init(ensc_hash_context *ctx, ensc_hash_method const *m)
114 {
115         return hashFunctionContextInit(ctx, m);
116 }
117
118 inline static int
119 ensc_crypto_hashctx_reset(ensc_hash_context *ctx)
120 {
121         return hashFunctionContextReset(ctx);
122 }
123
124 inline static void
125 ensc_crypto_hashctx_free(ensc_hash_context *ctx)
126 {
127         hashFunctionContextFree(ctx);
128 }
129
130 #endif  /* H_UTIL_VSERVER_LIB_INTERNAL_CRYPTO_WRAPPER_BEECRYPT_H */