Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Formatting: AFL tools #421

Merged
merged 2 commits into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion tools/afl/fuzzy.mk
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ FUZZ_SRC_PATH = $(FUZZ_PATH)/src
FUZZ_THEMIS_PATH = $(BIN_PATH)/afl-themis
FUZZ_THEMIS_LIB = $(FUZZ_THEMIS_PATH)/lib$(THEMIS_BIN).a

FUZZ_SOURCES = $(wildcard $(FUZZ_SRC_PATH)/*.c)
FUZZ_HEADERS = $(wildcard $(FUZZ_SRC_PATH)/*.h)

FUZZ_TOOLS = $(addprefix $(FUZZ_BIN_PATH)/,$(notdir $(wildcard $(FUZZ_PATH)/input/*)))
FUZZ_OBJS = $(patsubst $(FUZZ_SRC_PATH)/%.c,$(FUZZ_BIN_PATH)/%.o,$(wildcard $(FUZZ_SRC_PATH)/*.c))
FUZZ_OBJS = $(patsubst $(FUZZ_SRC_PATH)/%.c,$(FUZZ_BIN_PATH)/%.o,$(FUZZ_SOURCES))
FUZZ_UTILS = $(filter-out $(addsuffix .o,$(FUZZ_TOOLS)),$(FUZZ_OBJS))

AFL_CFLAGS += $(CFLAGS) -I$(FUZZ_SRC_PATH)
Expand Down Expand Up @@ -69,3 +72,22 @@ $(FUZZ_BIN_PATH)/%: $(FUZZ_BIN_PATH)/%.o $(FUZZ_UTILS) $(FUZZ_THEMIS_LIB)

$(FUZZ_THEMIS_LIB):
@AFL_QUIET=1 make themis_static CC=$(AFL_CC) BUILD_PATH=$(FUZZ_THEMIS_PATH)

FMT_FIXUP += $(patsubst $(FUZZ_SRC_PATH)/%,$(FUZZ_BIN_PATH)/%.fmt_fixup,$(FUZZ_SOURCES) $(FUZZ_HEADERS))
FMT_CHECK += $(patsubst $(FUZZ_SRC_PATH)/%,$(FUZZ_BIN_PATH)/%.fmt_check,$(FUZZ_SOURCES) $(FUZZ_HEADERS))

$(FUZZ_BIN_PATH)/%.c.fmt_fixup $(FUZZ_BIN_PATH)/%.h.fmt_fixup: \
CMD = $(CLANG_TIDY) -fix $< -- $(CFLAGS) -I$(FUZZ_SRC_PATH) 2>/dev/null && $(CLANG_FORMAT) -i $< && touch $@

$(FUZZ_BIN_PATH)/%.c.fmt_check $(FUZZ_BIN_PATH)/%.h.fmt_check: \
CMD = $(CLANG_FORMAT) $< | diff -u $< - && $(CLANG_TIDY) $< -- $(CFLAGS) -I$(FUZZ_SRC_PATH) 2>/dev/null && touch $@

$(FUZZ_BIN_PATH)/%.fmt_fixup: $(FUZZ_SRC_PATH)/%
@mkdir -p $(@D)
@echo -n "fixup $< "
@$(BUILD_CMD_)

$(FUZZ_BIN_PATH)/%.fmt_check: $(FUZZ_SRC_PATH)/%
@mkdir -p $(@D)
@echo -n "check $< "
@$(BUILD_CMD_)
72 changes: 34 additions & 38 deletions tools/afl/src/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,39 @@

#include <stdlib.h>

int read_line_binary(FILE *input, uint8_t **out_bytes, size_t *out_size)
int read_line_binary(FILE* input, uint8_t** out_bytes, size_t* out_size)
{
uint8_t length_bytes[4] = {0};
uint32_t length = 0;

if (!input || !out_bytes || !out_size)
{
return -1;
}

*out_bytes = NULL;
*out_size = 0;

if (fread(length_bytes, 1, 4, input) != 4)
{
return -1;
}

length = (length << 8) | length_bytes[0];
length = (length << 8) | length_bytes[1];
length = (length << 8) | length_bytes[2];
length = (length << 8) | length_bytes[3];

*out_bytes = malloc(length);
if (!*out_bytes)
{
return -1;
}

if (fread(*out_bytes, 1, length, input) != length)
{
free(*out_bytes);
*out_bytes = NULL;
return -1;
}

*out_size = length;

return 0;
uint8_t length_bytes[4] = {0};
uint32_t length = 0;

if (!input || !out_bytes || !out_size) {
return -1;
}

*out_bytes = NULL;
*out_size = 0;

if (fread(length_bytes, 1, 4, input) != 4) {
return -1;
}

length = (length << 8) | length_bytes[0];
length = (length << 8) | length_bytes[1];
length = (length << 8) | length_bytes[2];
length = (length << 8) | length_bytes[3];

*out_bytes = malloc(length);
if (!*out_bytes) {
return -1;
}

if (fread(*out_bytes, 1, length, input) != length) {
free(*out_bytes);
*out_bytes = NULL;
return -1;
}

*out_size = length;

return 0;
}
2 changes: 1 addition & 1 deletion tools/afl/src/readline.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@
*
* @returns zero on success, negative value on error.
*/
int read_line_binary(FILE *input, uint8_t **out_bytes, size_t *out_size);
int read_line_binary(FILE* input, uint8_t** out_bytes, size_t* out_size);

#endif /* READ_LINE_H */
175 changes: 86 additions & 89 deletions tools/afl/src/scell_seal_decrypt.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,94 +24,91 @@

#include "readline.h"

int main(int argc, char **argv)
int main(int argc, char** argv)
{
themis_status_t err = THEMIS_SUCCESS;

/*
* Read test data.
*/

if (argc != 2)
{
fprintf(stderr, "usage:\n\t%s <input-file>\n", argv[0]);
return 1;
}

FILE* input = fopen(argv[1], "rb");
if (!input)
{
fprintf(stderr, "failed to open %s: %s\n", argv[1], strerror(errno));
return 1;
}

uint8_t *master_key_bytes = NULL;
size_t master_key_size = 0;

if (read_line_binary(input, &master_key_bytes, &master_key_size))
{
fprintf(stderr, "failed to read %s: %s\n", argv[1], strerror(errno));
return 1;
}

uint8_t *user_context_bytes = NULL;
size_t user_context_size = 0;

if (read_line_binary(input, &user_context_bytes, &user_context_size))
{
fprintf(stderr, "failed to read %s: %s\n", argv[1], strerror(errno));
return 1;
}

uint8_t *message_bytes = NULL;
size_t message_size = 0;

if (read_line_binary(input, &message_bytes, &message_size))
{
fprintf(stderr, "failed to read %s: %s\n", argv[1], strerror(errno));
return 1;
}

fclose(input);

/*
* Try decrypting it.
*/

uint8_t *decrypted_bytes = NULL;
size_t decrypted_size = 0;

err = themis_secure_cell_decrypt_seal(
master_key_bytes, master_key_size,
user_context_bytes, user_context_size,
message_bytes, message_size,
NULL, &decrypted_size);

if (err != THEMIS_BUFFER_TOO_SMALL)
{
fprintf(stderr, "failed to determine decrypted message size: %d\n", err);
return 2;
}

decrypted_bytes = malloc(decrypted_size);
if (!decrypted_bytes)
{
fprintf(stderr, "failed to allocate memory for decrypted message (%zu bytes)\n",
decrypted_size);
return 2;
}

err = themis_secure_cell_decrypt_seal(
master_key_bytes, master_key_size,
user_context_bytes, user_context_size,
message_bytes, message_size,
decrypted_bytes, &decrypted_size);

if (err != THEMIS_SUCCESS)
{
fprintf(stderr, "failed to decrypt message: %d\n", err);
return 2;
}

return 0;
themis_status_t err = THEMIS_SUCCESS;

/*
* Read test data.
*/

if (argc != 2) {
fprintf(stderr, "usage:\n\t%s <input-file>\n", argv[0]);
return 1;
}

FILE* input = fopen(argv[1], "rb");
if (!input) {
fprintf(stderr, "failed to open %s: %s\n", argv[1], strerror(errno));
return 1;
}

uint8_t* master_key_bytes = NULL;
size_t master_key_size = 0;

if (read_line_binary(input, &master_key_bytes, &master_key_size)) {
fprintf(stderr, "failed to read %s: %s\n", argv[1], strerror(errno));
return 1;
}

uint8_t* user_context_bytes = NULL;
size_t user_context_size = 0;

if (read_line_binary(input, &user_context_bytes, &user_context_size)) {
fprintf(stderr, "failed to read %s: %s\n", argv[1], strerror(errno));
return 1;
}

uint8_t* message_bytes = NULL;
size_t message_size = 0;

if (read_line_binary(input, &message_bytes, &message_size)) {
fprintf(stderr, "failed to read %s: %s\n", argv[1], strerror(errno));
return 1;
}

fclose(input);

/*
* Try decrypting it.
*/

uint8_t* decrypted_bytes = NULL;
size_t decrypted_size = 0;

err = themis_secure_cell_decrypt_seal(master_key_bytes,
master_key_size,
user_context_bytes,
user_context_size,
message_bytes,
message_size,
NULL,
&decrypted_size);

if (err != THEMIS_BUFFER_TOO_SMALL) {
fprintf(stderr, "failed to determine decrypted message size: %d\n", err);
return 2;
}

decrypted_bytes = malloc(decrypted_size);
if (!decrypted_bytes) {
fprintf(stderr, "failed to allocate memory for decrypted message (%zu bytes)\n", decrypted_size);
return 2;
}

err = themis_secure_cell_decrypt_seal(master_key_bytes,
master_key_size,
user_context_bytes,
user_context_size,
message_bytes,
message_size,
decrypted_bytes,
&decrypted_size);

if (err != THEMIS_SUCCESS) {
fprintf(stderr, "failed to decrypt message: %d\n", err);
return 2;
}

return 0;
}
Loading