Skip to content

Commit

Permalink
Streamlined indentation
Browse files Browse the repository at this point in the history
  • Loading branch information
FD- committed Jan 2, 2020
1 parent d914d5c commit ff96a00
Show file tree
Hide file tree
Showing 20 changed files with 1,306 additions and 1,306 deletions.
166 changes: 83 additions & 83 deletions lib/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
#include <stdbool.h>

struct aes_ctx_s {
EVP_CIPHER_CTX *cipher_ctx;
uint8_t key[AES_128_BLOCK_SIZE];
uint8_t iv[AES_128_BLOCK_SIZE];
aes_direction_t direction;
EVP_CIPHER_CTX *cipher_ctx;
uint8_t key[AES_128_BLOCK_SIZE];
uint8_t iv[AES_128_BLOCK_SIZE];
aes_direction_t direction;
uint8_t block_offset;
};

Expand All @@ -39,85 +39,85 @@ uint8_t waste[AES_128_BLOCK_SIZE];
// Common AES utilities

void handle_error(const char* location) {
long error = ERR_get_error();
long error = ERR_get_error();
const char* error_str = ERR_error_string(error, NULL);
printf("Crypto error at %s: %s\n", location, error_str);
assert(false);
}

aes_ctx_t *aes_init(const uint8_t *key, const uint8_t *iv, const EVP_CIPHER *type, aes_direction_t direction) {
aes_ctx_t *ctx = malloc(sizeof(aes_ctx_t));
assert(ctx != NULL);
ctx->cipher_ctx = EVP_CIPHER_CTX_new();
assert(ctx->cipher_ctx != NULL);
aes_ctx_t *ctx = malloc(sizeof(aes_ctx_t));
assert(ctx != NULL);
ctx->cipher_ctx = EVP_CIPHER_CTX_new();
assert(ctx->cipher_ctx != NULL);

ctx->block_offset = 0;
ctx->direction = direction;
ctx->block_offset = 0;
ctx->direction = direction;

if (direction == AES_ENCRYPT) {
if (!EVP_EncryptInit_ex(ctx->cipher_ctx, type, NULL, key, iv)) {
handle_error(__func__);
}
} else {
if (!EVP_DecryptInit_ex(ctx->cipher_ctx, type, NULL, key, iv)) {
handle_error(__func__);
}
}
if (direction == AES_ENCRYPT) {
if (!EVP_EncryptInit_ex(ctx->cipher_ctx, type, NULL, key, iv)) {
handle_error(__func__);
}
} else {
if (!EVP_DecryptInit_ex(ctx->cipher_ctx, type, NULL, key, iv)) {
handle_error(__func__);
}
}

memcpy(ctx->key, key, AES_128_BLOCK_SIZE);
memcpy(ctx->iv, iv, AES_128_BLOCK_SIZE);
return ctx;
memcpy(ctx->key, key, AES_128_BLOCK_SIZE);
memcpy(ctx->iv, iv, AES_128_BLOCK_SIZE);
return ctx;
}

void aes_encrypt(aes_ctx_t *ctx, const uint8_t *in, uint8_t *out, int in_len) {
int out_len = 0;
if (!EVP_EncryptUpdate(ctx->cipher_ctx, out, &out_len, in, in_len)) {
handle_error(__func__);
}
int out_len = 0;
if (!EVP_EncryptUpdate(ctx->cipher_ctx, out, &out_len, in, in_len)) {
handle_error(__func__);
}

assert(out_len <= in_len);
assert(out_len <= in_len);
}

void aes_decrypt(aes_ctx_t *ctx, const uint8_t *in, uint8_t *out, int in_len) {
int out_len = 0;
if (!EVP_DecryptUpdate(ctx->cipher_ctx, out, &out_len, in, in_len)) {
handle_error(__func__);
}
int out_len = 0;
if (!EVP_DecryptUpdate(ctx->cipher_ctx, out, &out_len, in, in_len)) {
handle_error(__func__);
}

assert(out_len <= in_len);
assert(out_len <= in_len);
}

void aes_destroy(aes_ctx_t *ctx) {
if (ctx) {
EVP_CIPHER_CTX_free(ctx->cipher_ctx);
free(ctx);
}
if (ctx) {
EVP_CIPHER_CTX_free(ctx->cipher_ctx);
free(ctx);
}
}

void aes_reset(aes_ctx_t *ctx, const EVP_CIPHER *type, aes_direction_t direction) {
if (!EVP_CIPHER_CTX_reset(ctx->cipher_ctx)) {
if (!EVP_CIPHER_CTX_reset(ctx->cipher_ctx)) {
handle_error(__func__);
}
}

if (direction == AES_ENCRYPT) {
if (!EVP_EncryptInit_ex(ctx->cipher_ctx, type, NULL, ctx->key, ctx->iv)) {
handle_error(__func__);
}
} else {
if (!EVP_DecryptInit_ex(ctx->cipher_ctx, type, NULL, ctx->key, ctx->iv)) {
handle_error(__func__);
}
}
if (direction == AES_ENCRYPT) {
if (!EVP_EncryptInit_ex(ctx->cipher_ctx, type, NULL, ctx->key, ctx->iv)) {
handle_error(__func__);
}
} else {
if (!EVP_DecryptInit_ex(ctx->cipher_ctx, type, NULL, ctx->key, ctx->iv)) {
handle_error(__func__);
}
}
}

// AES CTR

aes_ctx_t *aes_ctr_init(const uint8_t *key, const uint8_t *iv) {
return aes_init(key, iv, EVP_aes_128_ctr(), AES_ENCRYPT);
return aes_init(key, iv, EVP_aes_128_ctr(), AES_ENCRYPT);
}

void aes_ctr_encrypt(aes_ctx_t *ctx, const uint8_t *in, uint8_t *out, int len) {
aes_encrypt(ctx, in, out, len);
aes_encrypt(ctx, in, out, len);
ctx->block_offset = (ctx->block_offset + len) % AES_128_BLOCK_SIZE;
}

Expand All @@ -128,82 +128,82 @@ void aes_ctr_start_fresh_block(aes_ctx_t *ctx) {
}

void aes_ctr_decrypt(aes_ctx_t *ctx, const uint8_t *in, uint8_t *out, int len) {
aes_encrypt(ctx, in, out, len);
aes_encrypt(ctx, in, out, len);
}

void aes_ctr_reset(aes_ctx_t *ctx) {
aes_reset(ctx, EVP_aes_128_ctr(), AES_ENCRYPT);
aes_reset(ctx, EVP_aes_128_ctr(), AES_ENCRYPT);
}

void aes_ctr_destroy(aes_ctx_t *ctx) {
aes_destroy(ctx);
aes_destroy(ctx);
}

// AES CBC

aes_ctx_t *aes_cbc_init(const uint8_t *key, const uint8_t *iv, aes_direction_t direction) {
return aes_init(key, iv, EVP_aes_128_cbc(), direction);
return aes_init(key, iv, EVP_aes_128_cbc(), direction);
}

void aes_cbc_encrypt(aes_ctx_t *ctx, const uint8_t *in, uint8_t *out, int len) {
assert(ctx->direction == AES_ENCRYPT);
aes_encrypt(ctx, in, out, len);
assert(ctx->direction == AES_ENCRYPT);
aes_encrypt(ctx, in, out, len);
}

void aes_cbc_decrypt(aes_ctx_t *ctx, const uint8_t *in, uint8_t *out, int len) {
assert(ctx->direction == AES_DECRYPT);
aes_decrypt(ctx, in, out, len);
assert(ctx->direction == AES_DECRYPT);
aes_decrypt(ctx, in, out, len);
}

void aes_cbc_reset(aes_ctx_t *ctx) {
aes_reset(ctx, EVP_aes_128_ctr(), ctx->direction);
aes_reset(ctx, EVP_aes_128_ctr(), ctx->direction);
}

void aes_cbc_destroy(aes_ctx_t *ctx) {
aes_destroy(ctx);
aes_destroy(ctx);
}

// SHA 512

struct sha_ctx_s {
EVP_MD_CTX *digest_ctx;
EVP_MD_CTX *digest_ctx;
};

sha_ctx_t *sha_init() {
sha_ctx_t *ctx = malloc(sizeof(sha_ctx_t));
assert(ctx != NULL);
ctx->digest_ctx = EVP_MD_CTX_new();
assert(ctx->digest_ctx != NULL);
sha_ctx_t *ctx = malloc(sizeof(sha_ctx_t));
assert(ctx != NULL);
ctx->digest_ctx = EVP_MD_CTX_new();
assert(ctx->digest_ctx != NULL);

if (!EVP_DigestInit_ex(ctx->digest_ctx, EVP_sha512(), NULL)) {
handle_error(__func__);
}
return ctx;
if (!EVP_DigestInit_ex(ctx->digest_ctx, EVP_sha512(), NULL)) {
handle_error(__func__);
}
return ctx;
}

void sha_update(sha_ctx_t *ctx, const uint8_t *in, int len) {
if (!EVP_DigestUpdate(ctx->digest_ctx, in, len)) {
handle_error(__func__);
}
if (!EVP_DigestUpdate(ctx->digest_ctx, in, len)) {
handle_error(__func__);
}
}

void sha_final(sha_ctx_t *ctx, uint8_t *out, unsigned int *len) {
if (!EVP_DigestFinal_ex(ctx->digest_ctx, out, len)) {
handle_error(__func__);
}
if (!EVP_DigestFinal_ex(ctx->digest_ctx, out, len)) {
handle_error(__func__);
}
}

void sha_reset(sha_ctx_t *ctx) {
if (!EVP_MD_CTX_reset(ctx->digest_ctx) ||
!EVP_DigestInit_ex(ctx->digest_ctx, EVP_sha512(), NULL)) {
if (!EVP_MD_CTX_reset(ctx->digest_ctx) ||
!EVP_DigestInit_ex(ctx->digest_ctx, EVP_sha512(), NULL)) {

handle_error(__func__);
}
handle_error(__func__);
}
}

void sha_destroy(sha_ctx_t *ctx) {
if (ctx) {
EVP_MD_CTX_free(ctx->digest_ctx);
free(ctx);
}
if (ctx) {
EVP_MD_CTX_free(ctx->digest_ctx);
free(ctx);
}
}
2 changes: 1 addition & 1 deletion lib/dnssd.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ dnssd_register_airplay(dnssd_t *dnssd, unsigned short port)
return 1;
}

char *
char *
dnssd_get_airplay_txt(dnssd_t *dnssd, int *length)
{
*length = dnssd->TXTRecordGetLength(&dnssd->airplay_record);
Expand Down
32 changes: 16 additions & 16 deletions lib/fairplay_playfair.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ char reply_message[4][142] = {{0x46,0x50,0x4c,0x59,0x03,0x01,0x02,0x00,0x00,0x00
char fp_header[] = {0x46, 0x50, 0x4c, 0x59, 0x03, 0x01, 0x04, 0x00, 0x00, 0x00, 0x00, 0x14};

struct fairplay_s {
logger_t *logger;
logger_t *logger;

unsigned char keymsg[164];
unsigned int keymsglen;
unsigned char keymsg[164];
unsigned int keymsglen;
};

fairplay_t *
fairplay_init(logger_t *logger)
{
fairplay_t *fp;
fairplay_t *fp;

fp = calloc(1, sizeof(fairplay_t));
if (!fp) {
return NULL;
}
fp->logger = logger;
fp = calloc(1, sizeof(fairplay_t));
if (!fp) {
return NULL;
}
fp->logger = logger;

return fp;
return fp;
}

int
Expand Down Expand Up @@ -72,16 +72,16 @@ fairplay_handshake(fairplay_t *fp, const unsigned char req[164], unsigned char r
int
fairplay_decrypt(fairplay_t *fp, const unsigned char input[72], unsigned char output[16])
{
if (fp->keymsglen != 164) {
return -1;
}
if (fp->keymsglen != 164) {
return -1;
}

playfair_decrypt(fp->keymsg, (unsigned char *) input, output);
return 0;
playfair_decrypt(fp->keymsg, (unsigned char *) input, output);
return 0;
}

void
fairplay_destroy(fairplay_t *fp)
{
free(fp);
free(fp);
}
Loading

0 comments on commit ff96a00

Please sign in to comment.