Use an enum for encrypt / decrypt + minor cleanup

This commit is contained in:
Dag-Erling Smørgrav 2015-10-03 14:43:33 +00:00 committed by des
parent 1826bb12bf
commit a9e733f0ca
7 changed files with 15 additions and 14 deletions

View file

@ -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 *);

View file

@ -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 *);

View file

@ -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 *);

View file

@ -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);

View file

@ -37,12 +37,8 @@
#include <cryb/rc4.h>
#include <stdio.h>
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;

View file

@ -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));

View file

@ -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));