| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 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 |
|
|---|
| 63 |
|
|---|
| 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 |
|---|