Clean up namespace macros and provide algorithm definitions.

This commit is contained in:
Dag-Erling Smørgrav 2014-07-10 13:53:05 +00:00 committed by des
parent 2f3ecfa7ba
commit 1d2f0661b3
5 changed files with 63 additions and 46 deletions

View file

@ -36,50 +36,34 @@
#ifndef CRYB_DIGEST_H_INCLUDED #ifndef CRYB_DIGEST_H_INCLUDED
#define CRYB_DIGEST_H_INCLUDED #define CRYB_DIGEST_H_INCLUDED
#define digest_init_func cryb_digest_init_func
#define digest_update_func cryb_digest_update_func
#define digest_final_func cryb_digest_final_func
#define digest_complete_func cryb_digest_complete_func
#define digest_algorithm cryb_digest_algorithm
#define digest_init cryb_digest_init
#define digest_update cryb_digest_update
#define digest_final cryb_digest_final
#define digest_complete cryb_digest_complete
typedef void *(*digest_init_func)(void); typedef void *(*digest_init_func)(void);
typedef void (*digest_update_func)(void *, const void *, size_t); typedef void (*digest_update_func)(void *, const void *, size_t);
typedef void (*digest_final_func)(void *, void *); typedef void (*digest_final_func)(void *, void *);
typedef int (*digest_complete_func)(const void *, size_t, void *); typedef int (*digest_complete_func)(const void *, size_t, void *);
struct digest_algorithm { typedef struct digest_algorithm {
const char *name; /* algorithm name */ const char *name; /* algorithm name */
size_t ctxsize; /* size of context structure */ size_t contextlen; /* size of context structure */
size_t resultlen; /* length of the result */ size_t digestlen; /* length of the digest */
digest_init_func init; digest_init_func init; /* initialization method */
digest_update_func update; digest_update_func update; /* update method */
digest_final_func final; digest_final_func final; /* finalization method */
digest_complete_func complete; digest_complete_func complete; /* one-shot method */
}; } digest_algorithm;
const struct digest_algorithm *cryb_digest_algorithm(const char *); void *digest_init(const char *);
void digest_update(void *, const void *, size_t);
void *cryb_digest_init(const char *); void digest_final(void *, void *);
void cryb_digest_update(void *, const void *, size_t); int digest_complete(const char *, const void *, size_t, void *);
void cryb_digest_final(void *, void *);
int cryb_digest_complete(const char *, const void *, size_t, void *);
static inline void *
digest_init(const char *alg)
{
return (cryb_digest_init(alg));
}
static inline void
digest_update(void *ctx, const void *msg, size_t msglen)
{
cryb_digest_update(ctx, msg, msglen);
}
static inline void
digest_final(void *ctx, void *md)
{
cryb_digest_final(ctx, md);
}
static inline int
digest_complete(const char *alg, const void *msg, size_t msglen, void *md)
{
return (cryb_digest_complete(alg, msg, msglen, md));
}
#endif #endif

View file

@ -38,6 +38,15 @@
#define MD5_DIGEST_LEN 16 #define MD5_DIGEST_LEN 16
#define md5_algorithm cryb_md5_algorithm
#define md5_ctx cryb_md5_ctx
#define md5_init cryb_md5_init
#define md5_update cryb_md5_update
#define md5_final cryb_md5_final
#define md5_complete cryb_md5_complete
extern struct digest_algorithm md5_algorithm;
typedef struct md5_ctx { typedef struct md5_ctx {
uint8_t block[64]; uint8_t block[64];
size_t blocklen; size_t blocklen;
@ -45,10 +54,6 @@ typedef struct md5_ctx {
uint32_t h[4]; uint32_t h[4];
} md5_ctx; } md5_ctx;
#define md5_init cryb_md5_init
#define md5_update cryb_md5_update
#define md5_final cryb_md5_final
#define md5_complete cryb_md5_complete
void md5_init(md5_ctx *); void md5_init(md5_ctx *);
void md5_update(md5_ctx *, const void *, size_t); void md5_update(md5_ctx *, const void *, size_t);
void md5_final(md5_ctx *, void *); void md5_final(md5_ctx *, void *);

View file

@ -38,6 +38,15 @@
#define SHA1_DIGEST_LEN 20 #define SHA1_DIGEST_LEN 20
#define sha1_algorithm cryb_sha1_algorithm
#define sha1_ctx cryb_sha1_ctx
#define sha1_init cryb_sha1_init
#define sha1_update cryb_sha1_update
#define sha1_final cryb_sha1_final
#define sha1_complete cryb_sha1_complete
extern struct digest_algorithm sha1_algorithm;
typedef struct sha1_ctx { typedef struct sha1_ctx {
uint8_t block[64]; uint8_t block[64];
size_t blocklen; size_t blocklen;
@ -45,10 +54,8 @@ typedef struct sha1_ctx {
uint32_t h[5], k[4]; uint32_t h[5], k[4];
} sha1_ctx; } sha1_ctx;
#define sha1_init cryb_sha1_init extern struct digest_algorithm sha1_algorithm;
#define sha1_update cryb_sha1_update
#define sha1_final cryb_sha1_final
#define sha1_complete cryb_sha1_complete
void sha1_init(sha1_ctx *); void sha1_init(sha1_ctx *);
void sha1_update(sha1_ctx *, const void *, size_t); void sha1_update(sha1_ctx *, const void *, size_t);
void sha1_final(sha1_ctx *, void *); void sha1_final(sha1_ctx *, void *);

View file

@ -259,3 +259,13 @@ md5_complete(const void *buf, size_t len, void *digest)
md5_update(&ctx, buf, len); md5_update(&ctx, buf, len);
md5_final(&ctx, digest); md5_final(&ctx, digest);
} }
struct digest_algorithm md5_digest = {
.name = "md5",
.contextlen = sizeof md5_digest,
.digestlen = MD5_DIGEST_LEN,
.init = (digest_init_func)md5_init,
.update = (digest_update_func)md5_update,
.final = (digest_final_func)md5_final,
.complete = (digest_complete_func)md5_complete,
};

View file

@ -51,6 +51,7 @@
#include <cryb/digest.h> #include <cryb/digest.h>
#include <cryb/bitwise.h> #include <cryb/bitwise.h>
#include <cryb/digest.h>
#include <cryb/sha1.h> #include <cryb/sha1.h>
static uint32_t sha1_h[5] = { static uint32_t sha1_h[5] = {
@ -171,3 +172,13 @@ sha1_complete(const void *buf, size_t len, void *digest)
sha1_update(&ctx, buf, len); sha1_update(&ctx, buf, len);
sha1_final(&ctx, digest); sha1_final(&ctx, digest);
} }
struct digest_algorithm sha1_digest = {
.name = "sha1",
.contextlen = sizeof sha1_digest,
.digestlen = SHA1_DIGEST_LEN,
.init = (digest_init_func)sha1_init,
.update = (digest_update_func)sha1_update,
.final = (digest_final_func)sha1_final,
.complete = (digest_complete_func)sha1_complete,
};