File: crypto-wrapper-beecrypt.h

package info (click to toggle)
util-vserver 0.30.216-pre3054-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,556 kB
  • ctags: 3,909
  • sloc: ansic: 23,121; sh: 16,905; xml: 1,972; makefile: 383; python: 301; perl: 85; awk: 4
file content (130 lines) | stat: -rw-r--r-- 3,120 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*	--*- c -*--
 * Copyright (C) 2008 Enrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 and/or 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef H_UTIL_VSERVER_LIB_INTERNAL_CRYPTO_WRAPPER_BEECRYPT_H
#define H_UTIL_VSERVER_LIB_INTERNAL_CRYPTO_WRAPPER_BEECRYPT_H

#include <beecrypt/beecrypt.h>
#include <ctype.h>

typedef hashFunction			ensc_hash_method;
typedef hashFunctionContext		ensc_hash_context;

inline static void
ensc_crypto_init(void)
{
}

inline static ensc_hash_method const *
ensc_crypto_hash_get_default(void)
{
	return hashFunctionDefault();
}

inline static ensc_hash_method const *
ensc_crypto_hash_find(char const *id_c)
{
	char			*id = strdupa(id_c);
	char			*ptr = id;
	char const		*name;

	while (*ptr) {
		*ptr = tolower(*ptr);
		++ptr;
	}

	ptr = id;
	while ((ptr=strchr(ptr, '-'))!=NULL)
		memmove(ptr, ptr+1, strlen(ptr));
	
	if (strcmp(id, "md2")==0)
		name = "MD2";
	else if (strcmp(id, "md5")==0)
		name = "MD5";
	else if (strcmp(id, "sha1")==0)
		name = "SHA-1";
	else if (strcasecmp(id, "sha256")==0)
		name = "SHA-256";
#if 0
	/* sha-384 in beecrypt seems to be broken; digestsize is reported as
	 * 64 there although 48 is the correct value */
	else if (strcasecmp(id, "sha384")==0)
		name = "SHA-384";
#endif
	else if (strcasecmp(id, "sha512")==0)
		name = "SHA-512";
	else
		name = NULL;
		
	return hashFunctionFind(name);
}

inline static char const *
ensc_crypto_hash_get_name(ensc_hash_method const *m)
{
	return m->name;
}

inline static size_t
ensc_crypto_hash_get_digestsize(ensc_hash_method const *m)
{
	return m->digestsize;
}



inline static size_t
ensc_crypto_hashctx_get_digestsize(ensc_hash_context const *ctx)
{
	return ensc_crypto_hash_get_digestsize(ctx->algo);
}

inline static int
ensc_crypto_hashctx_get_digest(ensc_hash_context *ctx, void *result,
			       size_t *res_len, size_t UNUSED max_res_len)
{
	int	rc = hashFunctionContextDigest(ctx, result);
	if (res_len)
		*res_len = ctx->algo->digestsize;

	return rc;
}

inline static int
ensc_crypto_hashctx_update(ensc_hash_context *ctx, void const *src, size_t len)
{
	return hashFunctionContextUpdate(ctx, src, len);
}

inline static int
ensc_crypto_hashctx_init(ensc_hash_context *ctx, ensc_hash_method const *m)
{
	return hashFunctionContextInit(ctx, m);
}

inline static int
ensc_crypto_hashctx_reset(ensc_hash_context *ctx)
{
	return hashFunctionContextReset(ctx);
}

inline static void
ensc_crypto_hashctx_free(ensc_hash_context *ctx)
{
	hashFunctionContextFree(ctx);
}

#endif	/* H_UTIL_VSERVER_LIB_INTERNAL_CRYPTO_WRAPPER_BEECRYPT_H */