diff --git a/include/cryb/Makefile.am b/include/cryb/Makefile.am index 1747566..45ccb08 100644 --- a/include/cryb/Makefile.am +++ b/include/cryb/Makefile.am @@ -9,6 +9,7 @@ cryb_HEADERS = \ digest.h \ hmac.h \ hotp.h \ + md.h \ md2.h \ md4.h \ md5.h \ @@ -18,6 +19,7 @@ cryb_HEADERS = \ rand.h \ rfc3986.h \ rfc4648.h \ + sha.h \ sha1.h \ sha256.h \ sha384.h \ diff --git a/include/cryb/digest.h b/include/cryb/digest.h index 92d136f..85d3ae7 100644 --- a/include/cryb/digest.h +++ b/include/cryb/digest.h @@ -27,33 +27,29 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * - * Author: Dag-Erling Smørgrav - * Sponsor: the University of Oslo - * * $Cryb$ */ #ifndef CRYB_DIGEST_H_INCLUDED #define CRYB_DIGEST_H_INCLUDED +#define digest_ctx cryb_digest_ctx #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_update_func)(void *, const void *, size_t); -typedef void (*digest_final_func)(void *, void *); +typedef void digest_ctx; +typedef void (*digest_init_func)(digest_ctx *); +typedef void (*digest_update_func)(digest_ctx *, const void *, size_t); +typedef void (*digest_final_func)(digest_ctx *, void *); typedef int (*digest_complete_func)(const void *, size_t, void *); -typedef struct digest_algorithm { +typedef struct { const char *name; /* algorithm name */ size_t contextlen; /* size of context structure */ + size_t blocklen; /* block length */ size_t digestlen; /* length of the digest */ digest_init_func init; /* initialization method */ digest_update_func update; /* update method */ @@ -61,9 +57,17 @@ typedef struct digest_algorithm { digest_complete_func complete; /* one-shot method */ } digest_algorithm; -void *digest_init(const char *); -void digest_update(void *, const void *, size_t); -void digest_final(void *, void *); -int digest_complete(const char *, const void *, size_t, void *); +#define get_digest_algorithm cryb_get_digest_algorithm + +const digest_algorithm *get_digest_algorithm(const char *); + +#define digest_init(alg, ctx) \ + (alg)->init((ctx)) +#define digest_update(alg, ctx, buf, len) \ + (alg)->update((ctx), (buf), (len)) +#define digest_final(alg, ctx, md) \ + (alg)->final((ctx), (md)) +#define digest_complete(alg, buf, len, md) \ + (alg)->update((buf), (len), (md)) #endif diff --git a/include/cryb/hmac.h b/include/cryb/hmac.h index 9a70576..35b2ed8 100644 --- a/include/cryb/hmac.h +++ b/include/cryb/hmac.h @@ -36,10 +36,12 @@ #ifndef CRYB_HMAC_H_INCLUDED #define CRYB_HMAC_H_INCLUDED +#include + #define HMAC_LEN 20 -typedef struct hmac_ctx { - struct sha1_ctx sha1_ctx; +typedef struct { + sha1_ctx sha1_ctx; uint8_t key[64]; size_t keylen; } hmac_ctx; diff --git a/include/cryb/md.h b/include/cryb/md.h new file mode 100644 index 0000000..d8c8f0c --- /dev/null +++ b/include/cryb/md.h @@ -0,0 +1,4 @@ +/* $Cryb$ */ +#include +#include +#include diff --git a/include/cryb/md2.h b/include/cryb/md2.h index 3cd5c59..ecb89f1 100644 --- a/include/cryb/md2.h +++ b/include/cryb/md2.h @@ -32,7 +32,10 @@ #ifndef CRYB_MD2_H_INCLUDED #define CRYB_MD2_H_INCLUDED -#define MD2_DIGEST_LEN 16 +#include + +#define MD2_BLOCK_LEN 16 +#define MD2_DIGEST_LEN 16 #define md2_digest cryb_md2_digest #define md2_ctx cryb_md2_ctx @@ -41,7 +44,7 @@ #define md2_final cryb_md2_final #define md2_complete cryb_md2_complete -extern struct digest_algorithm md2_digest; +extern digest_algorithm md2_algorithm; /** * \brief MD2 context structure @@ -55,8 +58,7 @@ typedef struct unsigned char ipad[64]; /*!< HMAC: inner padding */ unsigned char opad[64]; /*!< HMAC: outer padding */ int left; /*!< amount of data in buffer */ -} -md2_ctx; +} md2_ctx; #ifdef __cplusplus extern "C" { diff --git a/include/cryb/md4.h b/include/cryb/md4.h index 624c088..5801161 100644 --- a/include/cryb/md4.h +++ b/include/cryb/md4.h @@ -32,7 +32,10 @@ #ifndef CRYB_MD4_H_INCLUDED #define CRYB_MD4_H_INCLUDED -#define MD4_DIGEST_LEN 16 +#include + +#define MD4_BLOCK_LEN 64 +#define MD4_DIGEST_LEN 16 #define md4_digest cryb_md4_digest #define md4_ctx cryb_md4_ctx @@ -41,7 +44,7 @@ #define md4_final cryb_md4_final #define md4_complete cryb_md4_complete -extern struct digest_algorithm md4_digest; +extern digest_algorithm md4_digest; /** * \brief MD4 context structure diff --git a/include/cryb/md5.h b/include/cryb/md5.h index a0ac9db..af5f760 100644 --- a/include/cryb/md5.h +++ b/include/cryb/md5.h @@ -36,7 +36,10 @@ #ifndef CRYB_MD5_H_INCLUDED #define CRYB_MD5_H_INCLUDED -#define MD5_DIGEST_LEN 16 +#include + +#define MD5_BLOCK_LEN 64 +#define MD5_DIGEST_LEN 16 #define md5_digest cryb_md5_digest #define md5_ctx cryb_md5_ctx @@ -45,9 +48,9 @@ #define md5_final cryb_md5_final #define md5_complete cryb_md5_complete -extern struct digest_algorithm md5_digest; +extern digest_algorithm md5_digest; -typedef struct md5_ctx { +typedef struct { uint8_t block[64]; size_t blocklen; uint64_t bitlen; diff --git a/include/cryb/sha.h b/include/cryb/sha.h new file mode 100644 index 0000000..96e9c0a --- /dev/null +++ b/include/cryb/sha.h @@ -0,0 +1,5 @@ +/* $Cryb$ */ +#include +#include +#include +#include diff --git a/include/cryb/sha1.h b/include/cryb/sha1.h index e8f8701..c5b991a 100644 --- a/include/cryb/sha1.h +++ b/include/cryb/sha1.h @@ -36,7 +36,10 @@ #ifndef CRYB_SHA1_H_INCLUDED #define CRYB_SHA1_H_INCLUDED -#define SHA1_DIGEST_LEN 20 +#include + +#define SHA1_BLOCK_LEN 64 +#define SHA1_DIGEST_LEN 20 #define sha1_digest cryb_sha1_digest #define sha1_ctx cryb_sha1_ctx @@ -45,16 +48,16 @@ #define sha1_final cryb_sha1_final #define sha1_complete cryb_sha1_complete -extern struct digest_algorithm sha1_digest; +extern digest_algorithm sha1_digest; -typedef struct sha1_ctx { +typedef struct { uint8_t block[64]; size_t blocklen; uint64_t bitlen; uint32_t h[5], k[4]; } sha1_ctx; -extern struct digest_algorithm sha1_algorithm; +extern digest_algorithm sha1_algorithm; void sha1_init(sha1_ctx *); void sha1_update(sha1_ctx *, const void *, size_t); diff --git a/include/cryb/sha256.h b/include/cryb/sha256.h index c44f706..e9016b5 100644 --- a/include/cryb/sha256.h +++ b/include/cryb/sha256.h @@ -29,7 +29,10 @@ #ifndef CRYB_SHA256_H_INCLUDED #define CRYB_SHA256_H_INCLUDED -#define SHA256_DIGEST_LEN 32 +#include + +#define SHA256_BLOCK_LEN 64 +#define SHA256_DIGEST_LEN 32 /* * Use #defines in order to avoid namespace collisions with anyone else's @@ -42,7 +45,7 @@ #define sha256_final cryb_sha256_final #define sha256_complete cryb_sha256_complete -extern struct digest_algorithm sha256_digest; +extern digest_algorithm sha256_digest; #define hmac_sha256_init cryb_hmac_sha256_init #define hmac_sha256_update cryb_hmac_sha256_update diff --git a/include/cryb/sha384.h b/include/cryb/sha384.h index f16da76..8352814 100644 --- a/include/cryb/sha384.h +++ b/include/cryb/sha384.h @@ -32,7 +32,10 @@ #ifndef CRYB_SHA384_H_INCLUDED #define CRYB_SHA384_H_INCLUDED -#define SHA384_DIGEST_LEN 48 +#include + +#define SHA384_BLOCK_LEN 128 +#define SHA384_DIGEST_LEN 48 #define sha384_digest cryb_sha384_digest #define sha384_ctx cryb_sha384_ctx @@ -41,7 +44,7 @@ #define sha384_final cryb_sha384_final #define sha384_complete cryb_sha384_complete -extern struct digest_algorithm sha384_digest; +extern digest_algorithm sha384_digest; /** * \brief SHA-384 context structure diff --git a/include/cryb/sha512.h b/include/cryb/sha512.h index 373fdcf..28c8f80 100644 --- a/include/cryb/sha512.h +++ b/include/cryb/sha512.h @@ -32,7 +32,10 @@ #ifndef CRYB_SHA512_H_INCLUDED #define CRYB_SHA512_H_INCLUDED -#define SHA512_DIGEST_LEN 64 +#include + +#define SHA512_BLOCK_LEN 128 +#define SHA512_DIGEST_LEN 64 #define sha512_digest cryb_sha512_digest #define sha512_ctx cryb_sha512_ctx @@ -41,7 +44,7 @@ #define sha512_final cryb_sha512_final #define sha512_complete cryb_sha512_complete -extern struct digest_algorithm sha512_digest; +extern digest_algorithm sha512_digest; /** * \brief SHA-512 context structure diff --git a/lib/digest/digest.c b/lib/digest/digest.c index c0f9daa..9c6654e 100644 --- a/lib/digest/digest.c +++ b/lib/digest/digest.c @@ -37,27 +37,36 @@ #include -#include +#include +#include #include #include +#include +#include +#include -static const struct digest_algorithm **cryb_digest_algorithms; +static const digest_algorithm **cryb_digest_algorithms; static void -cryb_init_digest_algorithms(void) +init_digest_algorithms(void) { - static const struct digest_algorithm *algorithms[] = { - &cryb_md5_algorithm, - &cryb_sha1_algorithm, + static const digest_algorithm *algorithms[] = { + &md2_algorithm, + &md4_algorithm, + &md5_algorithm, + &sha1_algorithm, + &sha256_algorithm, + &sha384_algorithm, + &sha512_algorithm, NULL }; cryb_digest_algorithms = algorithms; } -const struct digest_algorithm * -cryb_digest_algorithm(const char *name) +const digest_algorithm * +get_digest_algorithm(const char *name) { - const struct digest_algorithm **algp; + const digest_algorithm **algp; if (cryb_digest_algorithms == NULL) cryb_init_digest_algorithms(); @@ -66,4 +75,3 @@ cryb_digest_algorithm(const char *name) return (*algp); return (NULL); } - diff --git a/lib/digest/md2.c b/lib/digest/md2.c index bac2f9e..4d32748 100644 --- a/lib/digest/md2.c +++ b/lib/digest/md2.c @@ -39,7 +39,6 @@ #include -#include #include static const unsigned char PI_SUBST[256] = @@ -246,9 +245,10 @@ void md2_hmac_complete( unsigned char *key, int keylen, unsigned char *input, in memset( &ctx, 0, sizeof( md2_ctx ) ); } -struct digest_algorithm md2_digest = { +digest_algorithm md2_algorithm = { .name = "md2", - .contextlen = sizeof md2_digest, + .contextlen = sizeof(md2_ctx), + .blocklen = MD2_BLOCK_LEN, .digestlen = MD2_DIGEST_LEN, .init = (digest_init_func)md2_init, .update = (digest_update_func)md2_update, diff --git a/lib/digest/md4.c b/lib/digest/md4.c index a81c44e..552f54a 100644 --- a/lib/digest/md4.c +++ b/lib/digest/md4.c @@ -39,7 +39,6 @@ #include -#include #include /* @@ -345,9 +344,10 @@ void md4_hmac_complete( unsigned char *key, int keylen, unsigned char *input, in memset( &ctx, 0, sizeof( md4_ctx ) ); } -struct digest_algorithm md4_digest = { +digest_algorithm md4_algorithm = { .name = "md4", - .contextlen = sizeof md4_digest, + .contextlen = sizeof(md4_ctx), + .blocklen = MD4_BLOCK_LEN, .digestlen = MD4_DIGEST_LEN, .init = (digest_init_func)md4_init, .update = (digest_update_func)md4_update, diff --git a/lib/digest/md5.c b/lib/digest/md5.c index f1afb5f..f3bd580 100644 --- a/lib/digest/md5.c +++ b/lib/digest/md5.c @@ -47,7 +47,6 @@ #include #include -#include #include #include @@ -76,7 +75,7 @@ static const uint32_t md5_k[64] = { }; void -md5_init(struct md5_ctx *ctx) +md5_init(md5_ctx *ctx) { memset(ctx, 0, sizeof *ctx); @@ -112,7 +111,7 @@ md5_init(struct md5_ctx *ctx) } while (0) static void -md5_compute(struct md5_ctx *ctx, const uint8_t *block) +md5_compute(md5_ctx *ctx, const uint8_t *block) { uint32_t w[16], a, b, c, d; int i; @@ -203,7 +202,7 @@ md5_compute(struct md5_ctx *ctx, const uint8_t *block) } void -md5_update(struct md5_ctx *ctx, const void *buf, size_t len) +md5_update(md5_ctx *ctx, const void *buf, size_t len) { size_t copylen; @@ -230,7 +229,7 @@ md5_update(struct md5_ctx *ctx, const void *buf, size_t len) } void -md5_final(struct md5_ctx *ctx, void *digest) +md5_final(md5_ctx *ctx, void *digest) { ctx->block[ctx->blocklen++] = 0x80; @@ -252,16 +251,17 @@ md5_final(struct md5_ctx *ctx, void *digest) void md5_complete(const void *buf, size_t len, void *digest) { - struct md5_ctx ctx; + md5_ctx ctx; md5_init(&ctx); md5_update(&ctx, buf, len); md5_final(&ctx, digest); } -struct digest_algorithm md5_digest = { +digest_algorithm md5_digest = { .name = "md5", - .contextlen = sizeof md5_digest, + .contextlen = sizeof(md5_ctx), + .blocklen = MD5_BLOCK_LEN, .digestlen = MD5_DIGEST_LEN, .init = (digest_init_func)md5_init, .update = (digest_update_func)md5_update, diff --git a/lib/digest/sha1.c b/lib/digest/sha1.c index c0383ad..c4fcc0f 100644 --- a/lib/digest/sha1.c +++ b/lib/digest/sha1.c @@ -47,10 +47,7 @@ #include #include -#include #include - -#include #include static uint32_t sha1_h[5] = { @@ -62,7 +59,7 @@ static uint32_t sha1_k[4] = { }; void -sha1_init(struct sha1_ctx *ctx) +sha1_init(sha1_ctx *ctx) { memset(ctx, 0, sizeof *ctx); @@ -71,7 +68,7 @@ sha1_init(struct sha1_ctx *ctx) } static void -sha1_compute(struct sha1_ctx *ctx, const uint8_t *block) +sha1_compute(sha1_ctx *ctx, const uint8_t *block) { uint32_t w[80], a, b, c, d, e, f, temp; @@ -113,7 +110,7 @@ sha1_compute(struct sha1_ctx *ctx, const uint8_t *block) } void -sha1_update(struct sha1_ctx *ctx, const void *buf, size_t len) +sha1_update(sha1_ctx *ctx, const void *buf, size_t len) { size_t copylen; @@ -140,7 +137,7 @@ sha1_update(struct sha1_ctx *ctx, const void *buf, size_t len) } void -sha1_final(struct sha1_ctx *ctx, void *digest) +sha1_final(sha1_ctx *ctx, void *digest) { uint32_t hi, lo; @@ -165,16 +162,17 @@ sha1_final(struct sha1_ctx *ctx, void *digest) void sha1_complete(const void *buf, size_t len, void *digest) { - struct sha1_ctx ctx; + sha1_ctx ctx; sha1_init(&ctx); sha1_update(&ctx, buf, len); sha1_final(&ctx, digest); } -struct digest_algorithm sha1_digest = { +digest_algorithm sha1_digest = { .name = "sha1", .contextlen = sizeof sha1_digest, + .blocklen = SHA1_BLOCK_LEN, .digestlen = SHA1_DIGEST_LEN, .init = (digest_init_func)sha1_init, .update = (digest_update_func)sha1_update, diff --git a/lib/digest/sha256.c b/lib/digest/sha256.c index a641da7..fa813a3 100644 --- a/lib/digest/sha256.c +++ b/lib/digest/sha256.c @@ -40,7 +40,6 @@ #include #include -#include #include /* @@ -466,9 +465,10 @@ pbkdf2_sha256(const uint8_t * passwd, size_t passwdlen, const uint8_t * salt, memset(&PShctx, 0, sizeof(hmac_sha256_ctx)); } -struct digest_algorithm sha256_digest = { +digest_algorithm sha256_digest = { .name = "sha256", .contextlen = sizeof sha256_digest, + .blocklen = SHA256_BLOCK_LEN, .digestlen = SHA256_DIGEST_LEN, .init = (digest_init_func)sha256_init, .update = (digest_update_func)sha256_update, diff --git a/lib/digest/sha384.c b/lib/digest/sha384.c index 00d336a..b51359a 100644 --- a/lib/digest/sha384.c +++ b/lib/digest/sha384.c @@ -39,7 +39,6 @@ #include #include -#include #include static int is384 = 1; @@ -408,9 +407,10 @@ void sha384_hmac_complete( unsigned char *key, int keylen, memset( &ctx, 0, sizeof( sha384_ctx ) ); } -struct digest_algorithm sha384_digest = { +digest_algorithm sha384_digest = { .name = "sha384", .contextlen = sizeof sha384_digest, + .blocklen = SHA384_BLOCK_LEN, .digestlen = SHA384_DIGEST_LEN, .init = (digest_init_func)sha384_init, .update = (digest_update_func)sha384_update, diff --git a/lib/digest/sha512.c b/lib/digest/sha512.c index 1ffd38f..7d49416 100644 --- a/lib/digest/sha512.c +++ b/lib/digest/sha512.c @@ -39,7 +39,6 @@ #include #include -#include #include static int is384 = 0; @@ -408,9 +407,10 @@ void sha512_hmac_complete( unsigned char *key, int keylen, memset( &ctx, 0, sizeof( sha512_ctx ) ); } -struct digest_algorithm sha512_digest = { +digest_algorithm sha512_digest = { .name = "sha512", .contextlen = sizeof sha512_digest, + .blocklen = SHA512_BLOCK_LEN, .digestlen = SHA512_DIGEST_LEN, .init = (digest_init_func)sha512_init, .update = (digest_update_func)sha512_update, diff --git a/lib/mac/hmac.c b/lib/mac/hmac.c index c407b6f..260e9d0 100644 --- a/lib/mac/hmac.c +++ b/lib/mac/hmac.c @@ -52,7 +52,7 @@ #include void -hmac_init(struct hmac_ctx *ctx, const uint8_t *key, size_t keylen) +hmac_init(hmac_ctx *ctx, const uint8_t *key, size_t keylen) { uint8_t ipad[64]; @@ -77,14 +77,14 @@ hmac_init(struct hmac_ctx *ctx, const uint8_t *key, size_t keylen) } void -hmac_update(struct hmac_ctx *ctx, const uint8_t *buf, size_t len) +hmac_update(hmac_ctx *ctx, const uint8_t *buf, size_t len) { sha1_update(&ctx->sha1_ctx, buf, len); } void -hmac_final(struct hmac_ctx *ctx, uint8_t *mac) +hmac_final(hmac_ctx *ctx, uint8_t *mac) { uint8_t digest[20], opad[64]; @@ -102,7 +102,7 @@ void hmac_complete(const uint8_t *key, size_t keylen, const uint8_t *buf, size_t len, uint8_t *mac) { - struct hmac_ctx ctx; + hmac_ctx ctx; hmac_init(&ctx, key, keylen); hmac_update(&ctx, buf, len);