From 4dbfe1e6635cdddd6f0e076cd3810ee77a578ed9 Mon Sep 17 00:00:00 2001 From: hasufell Date: Mon, 26 May 2014 21:30:42 +0200 Subject: [PATCH] FILE: add file subsystem and move string to ntru_string subs. --- src/Makefile | 5 ++- src/ascii_poly.c | 3 +- src/ascii_poly.h | 1 + src/common.h | 19 -------- src/decrypt.c | 1 + src/decrypt.h | 2 +- src/encrypt.c | 1 + src/encrypt.h | 4 +- src/file.c | 107 ++++++++++++++++++++++++++++++++++++++++++++++ src/file.h | 49 +++++++++++++++++++++ src/ntru_string.c | 27 ++++++++++++ src/ntru_string.h | 54 +++++++++++++++++++++++ 12 files changed, 248 insertions(+), 25 deletions(-) create mode 100644 src/file.c create mode 100644 src/file.h create mode 100644 src/ntru_string.c create mode 100644 src/ntru_string.h diff --git a/src/Makefile b/src/Makefile index b07ff04..bed8d03 100644 --- a/src/Makefile +++ b/src/Makefile @@ -37,8 +37,9 @@ endif LIBS += -L. -lgmp -lmpfr -lm # objects -PQC_OBJS = poly.o mem.o encrypt.o decrypt.o keypair.o ascii_poly.o -PQC_HEADERS = err.h poly.h context.h encrypt.h decrypt.h keypair.h ascii_poly.h common.h +PQC_OBJS = poly.o mem.o encrypt.o decrypt.o keypair.o ascii_poly.o file.o +PQC_HEADERS = err.h poly.h context.h encrypt.h decrypt.h keypair.h \ + ascii_poly.h common.h file.h ntru_string.h # CUNIT_OBJS = cunit.o # includes diff --git a/src/ascii_poly.c b/src/ascii_poly.c index 7b23986..c8db45a 100644 --- a/src/ascii_poly.c +++ b/src/ascii_poly.c @@ -26,11 +26,12 @@ * @brief asci->poly and poly->ascii */ +#include "ascii_poly.h" #include "common.h" #include "context.h" #include "mem.h" +#include "ntru_string.h" #include "poly.h" -#include "ascii_poly.h" #include #include diff --git a/src/ascii_poly.h b/src/ascii_poly.h index 95f9375..6b153f4 100644 --- a/src/ascii_poly.h +++ b/src/ascii_poly.h @@ -31,6 +31,7 @@ #include "common.h" #include "context.h" +#include "ntru_string.h" #include #include diff --git a/src/common.h b/src/common.h index 1cc744a..cf3fe4b 100644 --- a/src/common.h +++ b/src/common.h @@ -38,23 +38,4 @@ #define ASCII_BITS 8 -typedef struct string string; - - -/** - * Represents a string. - */ -struct string { - /** - * Pointer to the char array, - * holding the actual string. - */ - char *ptr; - /** - * Length of the string. - */ - size_t len; -}; - - #endif /* NTRU_COMMON_H */ diff --git a/src/decrypt.c b/src/decrypt.c index c11975d..7d8c8f2 100644 --- a/src/decrypt.c +++ b/src/decrypt.c @@ -28,6 +28,7 @@ #include "ascii_poly.h" #include "decrypt.h" +#include "ntru_string.h" #include diff --git a/src/decrypt.h b/src/decrypt.h index 000b9c4..5e1f15b 100644 --- a/src/decrypt.h +++ b/src/decrypt.h @@ -28,8 +28,8 @@ #ifndef NTRU_DECRYPT_H #define NTRU_DECRYPT_H -#include "common.h" #include "context.h" +#include "ntru_string.h" #include "poly.h" #include diff --git a/src/encrypt.c b/src/encrypt.c index 097372f..3fdb1b8 100644 --- a/src/encrypt.c +++ b/src/encrypt.c @@ -29,6 +29,7 @@ #include "ascii_poly.h" #include "encrypt.h" #include "mem.h" +#include "ntru_string.h" #include diff --git a/src/encrypt.h b/src/encrypt.h index 4b0420a..5a45e70 100644 --- a/src/encrypt.h +++ b/src/encrypt.h @@ -29,8 +29,8 @@ #define PQC_ENCRYPT_H -#include "common.h" #include "context.h" +#include "ntru_string.h" #include "poly.h" #include @@ -68,7 +68,7 @@ ntru_encrypt_poly( ntru_context *ctx); /** - * Encrypt a message int he form of a null-terminated char array and + * Encrypt a message in the form of a null-terminated char array and * return a string. * * @param msg the message diff --git a/src/file.c b/src/file.c new file mode 100644 index 0000000..34da401 --- /dev/null +++ b/src/file.c @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +/** + * @file file.c + * Allows operations of files, such as reading + * and writing. + * @brief file operations + */ + +#include "common.h" +#include "err.h" +#include "mem.h" +#include "ntru_string.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define STD_FILE_BUF 4096 + + +string * +read_file(char const * const filename) +{ + char buf[STD_FILE_BUF], + *cstring = NULL; + int fd = 0; + size_t str_size = 0; + ssize_t n; + string *result_string; + + fd = open(filename, O_RDONLY); + + if (fd != -1) { + /* read and copy chunks */ + while ((n = read(fd, buf, STD_FILE_BUF)) != 0) { + + if (n == -1) + NTRU_ABORT("Failed while reading file descriptor %d\n", fd); + + str_size += n; /* count total bytes read */ + + REALLOC( /* allocate correct size */ + cstring, /* pointer to realloc */ + str_size /* total bytes read */ + + 1); /* space for trailing NULL byte */ + + /* append buffer to string */ + memcpy(cstring + (str_size - n), buf, (size_t)n); + } + /* add trailing NULL byte */ + cstring[str_size] = '\0'; + + if (close(fd)) + NTRU_ABORT("Failed to close file descripter %d\n", fd); + + result_string = ntru_malloc(sizeof(*result_string)); + result_string->ptr = cstring; + result_string->len = str_size; + + return result_string; + + } else { + return NULL; + } +} + +void +write_file(string const *file_content, char const * const filename) +{ + FILE *fp; + + fp = fopen(filename, "w"); + + if (!fp) + NTRU_ABORT("Failed while creating file\n"); + + for (uint32_t i = 0; i < file_content->len; i++) + fprintf(fp, "%c", file_content->ptr[i]); + + fclose(fp); +} diff --git a/src/file.h b/src/file.h new file mode 100644 index 0000000..4d09e46 --- /dev/null +++ b/src/file.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +/** + * @file file.h + * Header for the external API of file.c. + * @brief header for file.c + */ + +#ifndef NTRU_FILE_H +#define NTRU_FILE_H + + +#include "common.h" + + +/** + * Reads a file and returns a newly allocated string. + * + * @param filename file to open + * @return a newly allocated string which must be freed by the caller + * or NULL on failure + */ +string * +read_file(char const * const filename); + +void +write_file(string const *file_content, char const * const filename); + + +#endif /* NTRU_FILE_H */ diff --git a/src/ntru_string.c b/src/ntru_string.c new file mode 100644 index 0000000..ff91c05 --- /dev/null +++ b/src/ntru_string.c @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +/** + * @file ntru_string.c + * Sets up a string type that does not suck + * like C strings and provides operations on it. + * @brief string type and operations + */ diff --git a/src/ntru_string.h b/src/ntru_string.h new file mode 100644 index 0000000..d052b39 --- /dev/null +++ b/src/ntru_string.h @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2014 FH Bielefeld + * + * This file is part of a FH Bielefeld project. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +/** + * @file ntru_string.h + * Header for the external API of ntru_string.c. + * @brief header for ntru_string.c + */ + +#ifndef NTRU_STRING_H +#define NTRU_STRING_H + + +#include + + +typedef struct string string; + + +/** + * Represents a string. + */ +struct string { + /** + * Pointer to the char array, + * holding the actual string. + */ + char *ptr; + /** + * Length of the string. + */ + size_t len; +}; + + +#endif /* NTRU_STRING_H */