From a9e733f0cab617b7d27feab41e93896907079821 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Sat, 3 Oct 2015 14:43:33 +0000 Subject: [PATCH] Use an enum for encrypt / decrypt + minor cleanup --- include/cryb/aes.h | 2 +- include/cryb/cipher.h | 7 ++++++- include/cryb/rc4.h | 2 +- lib/cipher/cryb_aes.c | 6 +++--- lib/cipher/cryb_rc4.c | 6 +----- t/t_aes.c | 4 ++-- t/t_rc4.c | 2 +- 7 files changed, 15 insertions(+), 14 deletions(-) diff --git a/include/cryb/aes.h b/include/cryb/aes.h index cae5691..3308e0d 100644 --- a/include/cryb/aes.h +++ b/include/cryb/aes.h @@ -51,7 +51,7 @@ typedef struct { uint32_t buf[68]; } aes_ctx; -void aes_init(aes_ctx *, int, const uint8_t *, size_t); +void aes_init(aes_ctx *, cipher_mode, const uint8_t *, size_t); void aes_update(aes_ctx *, const void *, size_t, void *); void aes_finish(aes_ctx *); diff --git a/include/cryb/cipher.h b/include/cryb/cipher.h index e29df77..7d5ca6f 100644 --- a/include/cryb/cipher.h +++ b/include/cryb/cipher.h @@ -36,8 +36,13 @@ #define cipher_finish_func cryb_cipher_finish_func #define cipher_algorithm cryb_cipher_algorithm +typedef enum { + CIPHER_MODE_ENCRYPT = 0, + CIPHER_MODE_DECRYPT = 1, +} cipher_mode; + typedef void cipher_ctx; -typedef void (*cipher_init_func)(cipher_ctx *, int, const uint8_t *, size_t); +typedef void (*cipher_init_func)(cipher_ctx *, cipher_mode, const uint8_t *, size_t); typedef void (*cipher_update_func)(cipher_ctx *, const void *, size_t, void *); typedef void (*cipher_finish_func)(cipher_ctx *); diff --git a/include/cryb/rc4.h b/include/cryb/rc4.h index 362421f..5dc6956 100644 --- a/include/cryb/rc4.h +++ b/include/cryb/rc4.h @@ -44,7 +44,7 @@ typedef struct { uint8_t s[256], i, j; } rc4_ctx; -void rc4_init(rc4_ctx *, int, const uint8_t *, size_t); +void rc4_init(rc4_ctx *, cipher_mode, const uint8_t *, size_t); void rc4_update(rc4_ctx *, const void *, size_t, void *); void rc4_finish(rc4_ctx *); diff --git a/lib/cipher/cryb_aes.c b/lib/cipher/cryb_aes.c index 9a94bfa..f3ee569 100644 --- a/lib/cipher/cryb_aes.c +++ b/lib/cipher/cryb_aes.c @@ -616,12 +616,12 @@ aes_dec(aes_ctx *ctx, const uint8_t *input, uint8_t *output) } void -aes_init(aes_ctx *ctx, int mode, const uint8_t *key, size_t keylen) +aes_init(aes_ctx *ctx, cipher_mode mode, const uint8_t *key, size_t keylen) { memset(ctx, 0, sizeof *ctx); ctx->mode = mode; - if (ctx->mode) + if (ctx->mode == CIPHER_MODE_DECRYPT) aes_setkey_dec(ctx, key, keylen * 8); else aes_setkey_enc(ctx, key, keylen * 8); @@ -633,7 +633,7 @@ aes_update(aes_ctx *ctx, const void *in, size_t len, void *out) { (void)len; - if (ctx->mode) + if (ctx->mode == CIPHER_MODE_DECRYPT) aes_dec(ctx, in, out); else aes_enc(ctx, in, out); diff --git a/lib/cipher/cryb_rc4.c b/lib/cipher/cryb_rc4.c index 4c4d303..16b4aec 100644 --- a/lib/cipher/cryb_rc4.c +++ b/lib/cipher/cryb_rc4.c @@ -37,12 +37,8 @@ #include - -#include - - void -rc4_init(rc4_ctx *ctx, int mode, const uint8_t *key, size_t keylen) +rc4_init(rc4_ctx *ctx, cipher_mode mode, const uint8_t *key, size_t keylen) { unsigned int i, j; uint8_t t; diff --git a/t/t_aes.c b/t/t_aes.c index 368e971..bfea79c 100644 --- a/t/t_aes.c +++ b/t/t_aes.c @@ -68,7 +68,7 @@ t_aes_enc(char **desc CRYB_UNUSED, void *arg) aes_ctx ctx; asprintf(desc, "%s (encrypt)", t->desc); - aes_init(&ctx, 0, t->key, sizeof t->key); + aes_init(&ctx, CIPHER_MODE_ENCRYPT, t->key, sizeof t->key); aes_update(&ctx, t->ptext, AES_BLOCK_LEN, out); aes_finish(&ctx); return (t_compare_mem(t->ctext, out, AES_BLOCK_LEN)); @@ -82,7 +82,7 @@ t_aes_dec(char **desc CRYB_UNUSED, void *arg) aes_ctx ctx; asprintf(desc, "%s (decrypt)", t->desc); - aes_init(&ctx, 1, t->key, sizeof t->key); + aes_init(&ctx, CIPHER_MODE_DECRYPT, t->key, sizeof t->key); aes_update(&ctx, t->ctext, AES_BLOCK_LEN, out); aes_finish(&ctx); return (t_compare_mem(t->ptext, out, AES_BLOCK_LEN)); diff --git a/t/t_rc4.c b/t/t_rc4.c index d5abbaf..9970968 100644 --- a/t/t_rc4.c +++ b/t/t_rc4.c @@ -257,7 +257,7 @@ t_rc4(char **desc CRYB_UNUSED, void *arg) uint8_t out[32]; rc4_ctx ctx; - rc4_init(&ctx, 0xf00l, t->key, t->keylen); + rc4_init(&ctx, CIPHER_MODE_ENCRYPT, t->key, t->keylen); rc4_update(&ctx, t_zero, 32, out); rc4_finish(&ctx); return (t_compare_mem(t->stream, out, 32));