diff -uNr a/eucrypt/mpi/README b/eucrypt/mpi/README --- a/eucrypt/mpi/README 553fd1dbee5e1f5d3a5ec56690fea143943cd029cb4db51651f1ba01ee3069385ea513f0ce5c22a062020a3acef6cac219d0d86ccc56cb2467605776949f915b +++ b/eucrypt/mpi/README 9e9083c3afa2a9b5528ace65cfd5ab71dcf1b6ff27c4a6cb72fd3ecebd1ae1f88133ea40ab6bef2438375e8b93452afd01e831bb628d7576ca21854650d1e5ef @@ -1,3 +1,5 @@ +NB: this is used by the smg_rsa component of EuCrypt. + What you see here is a very classic version of the GNU MPI (bignum) library. It has been surgically removed from GnuPG 1.4.10, specifically as found at: diff -uNr a/eucrypt/smg_rsa/Makefile b/eucrypt/smg_rsa/Makefile --- a/eucrypt/smg_rsa/Makefile false +++ b/eucrypt/smg_rsa/Makefile 8ebd567a5f4b3c1a8aabcca0501ed2d4eb3c4eadb593f99ce07c8fe7c1dfb9879ec8457c7125d99cbda9ceb9d8e3eab7931f0a4879e33344df6e0904e489faea @@ -0,0 +1,27 @@ +PROGRAM = smg_rsa.a + +BUILD=obj +DIST=bin + +CXX = gcc +OBJECTS = $(addprefix $(BUILD)/, $(patsubst %.c,%.o,$(wildcard *.c))) +MPI = ../mpi +FLAGS = -g -Wall +INCLUDE = -I include -I $(MPI)/include + +.SUFFIXES: .o .c + +$(BUILD)/%.o: + $(CXX) $(FLAGS) $(INCLUDE) -c $*.c -o $@ + +all: $(PROGRAM) + +$(PROGRAM): $(OBJECTS) + ar rcs $(DIST)/$(PROGRAM) $(OBJECTS) + #ld -o $(DIST)/$(PROGRAM).o $(OBJECTS) -lc + +clean : + rm -rf nul core *flymake* $(BUILD)/*.o $(DIST)/$(PROGRAM) *~ bin/* + +check-syntax: + $(CXX) -c $(FLAGS) $(INCLUDE) -o nul -Wall -S $(CHK_SOURCES) diff -uNr a/eucrypt/smg_rsa/README b/eucrypt/smg_rsa/README --- a/eucrypt/smg_rsa/README fe2917ef90a8e9deb4d9f7450cbbc20fdf3ca9f76630b6956137b4648916e143c89f857e0bf0fde968fd241f3049050ef7f146254a9e8daead54fc0b720c7620 +++ b/eucrypt/smg_rsa/README abc69adfa42130e5269734f91c5f3f61f4c42055e27e9ea4b8b664801f2dc83b8a5ed6c6e339982805b4ffdda8548cc61d8fd6cf3fdfeeeec77324f6d95c54d0 @@ -1,2 +1,5 @@ S.MG, 2017 +This is the S.MG implementation of RSA, used by the Eulora server. + +NB: this lib is part of EuCrypt and as such, it relies on other EuCrypt components (most notably: mpi). diff -uNr a/eucrypt/smg_rsa/bin/README b/eucrypt/smg_rsa/bin/README --- a/eucrypt/smg_rsa/bin/README false +++ b/eucrypt/smg_rsa/bin/README ef968e74fee9d5c5451883afc1a47d0eb8a99c775a6b045040134a7f9c0d80dbec3644ff9d69cf941bdbc718a6d4bed3a27a11d2ef6530453ab6e4955aa45f32 @@ -0,0 +1 @@ +bin folder for smg_rsa lib diff -uNr a/eucrypt/smg_rsa/include/knobs.h b/eucrypt/smg_rsa/include/knobs.h --- a/eucrypt/smg_rsa/include/knobs.h false +++ b/eucrypt/smg_rsa/include/knobs.h 39addb10b86590187c6f9020dd894b4efa7256381acefdeb2c68abf8a37cbf011909e9bb9dfdb38ffb2fb4b3f0341ae39cb2c9be6a1425ec139da4e47c37b33b @@ -0,0 +1,7 @@ +#ifndef SMG_RSA_KNOBS_H +#define SMG_RSA_KNOBS_H + +#define ENTROPY_SOURCE "/dev/ttyUSB0" + +#endif /*SMG_RSA_KNOBS_H*/ + diff -uNr a/eucrypt/smg_rsa/include/smg_rsa.h b/eucrypt/smg_rsa/include/smg_rsa.h --- a/eucrypt/smg_rsa/include/smg_rsa.h false +++ b/eucrypt/smg_rsa/include/smg_rsa.h 54afe77a6a278eb793ecf8ca19cd3b1dec64ae9d59826dcad3abc4df46c0c3bc7a16e178a05690bf930c1935a94dd12eb635e6d37b4f6f89cb478fd92a2a0b7a @@ -0,0 +1,44 @@ +/* smg_rsa.h + * S.MG, 2017 + */ + +#ifndef SMG_RSA_H +#define SMG_RSA_H + +#include "mpi.h" +#include "knobs.h" + +/*********truerandom.c*********/ + +/* + * Opens and configures (as per FG requirements) the specified entropy source (e.g. "/dev/ttyUSB0") + * @param source_name the name of the file to open (e.g. "/dev/ttyUSB0") + * @return the descriptor of the open file when successful; negative value otherwise + */ +int open_entropy_source(char* source_name); + + +/* + * Returns noctets random octets (i.e. 8*noctets bits in total) as obtained from EuCrypt's preferred source. + * Preferred source is defined in knobs.h as ENTROPY_SOURCE and should be a TRNG (e.g. Fuckgoats). + * @param nboctets the length of desired random sequence, in octets + * @param out pointer to allocated memory space for the requested random noctets; NB: this method does NOT allocate space! + * @return the actual number of octets that were obtained from the currently configured entropy source (this is equal to noctets on successful read of required noctets) + */ +int get_random_octets(int noctets, unsigned char *out); + +/* Returns noctets random octets as obtained from the specified "from" source; + * NB: the "from" source is considered to be the handle of an already opened stream; + * This method will simply attempt to read from the source as needed! + * + * @param noctets the length of desired random sequence, in octets + * @param out pointer to allocated memory space for the requested random octets; + * NB: this method does NOT allocate space! + * @param from handle of an already opened entropy source - this method will just READ from it as needed + * @return the actual number of octets that were obtained + */ +int get_random_octets_from(int noctets, unsigned char *out, int from); + + +#endif /*SMG_RSA*/ + diff -uNr a/eucrypt/smg_rsa/obj/README b/eucrypt/smg_rsa/obj/README --- a/eucrypt/smg_rsa/obj/README false +++ b/eucrypt/smg_rsa/obj/README a2eb4b7afc8afe95836f6cf05b0eb589977606525747a45776a1409a65ccd52edb062ef150226ca65e91f66acebec828b050c1a2e946c542eb86d7830323daf3 @@ -0,0 +1 @@ +obj folder for smg_rsa diff -uNr a/eucrypt/smg_rsa/tests/Makefile b/eucrypt/smg_rsa/tests/Makefile --- a/eucrypt/smg_rsa/tests/Makefile false +++ b/eucrypt/smg_rsa/tests/Makefile e426fdca88edc45327759e751831fa6cb81f88d9c65a1da25ae513e391cfdc69a99027ccddef4d02aa8893ebbac32cc60a853101962b51f4269dc86ee41f311d @@ -0,0 +1,25 @@ +PROGRAM = tests + +CXX = gcc +OBJECTS := $(patsubst %.c,%.o,$(wildcard *.c)) +FLAGS = -g -Wall +INCLUDE = -I ../include -I ../../mpi/include +SMG_RSA = ../bin/smg_rsa.a +MPI = ../../mpi/bin/mpi.a +LIBS := $(SMG_RSA) $(MPI) + +.SUFFIXES: .o .c + +.c.o: + $(CXX) $(FLAGS) $(INCLUDE) -c $< -o $@ + +all: $(PROGRAM) + +$(PROGRAM): $(OBJECTS) + $(CXX) $(FLAGS) $(INCLUDE) -o $(PROGRAM) $(OBJECTS) $(LIBS) + +clean : + rm -rf nul core *flymake* *.o $(PROGRAM) *~ bin obj + +check-syntax: + $(CXX) -c $(FLAGS) $(INCLUDE) -o nul -Wall -S $(CHK_SOURCES) diff -uNr a/eucrypt/smg_rsa/tests/tests.c b/eucrypt/smg_rsa/tests/tests.c --- a/eucrypt/smg_rsa/tests/tests.c false +++ b/eucrypt/smg_rsa/tests/tests.c 925d35158574838825aee8b2269787e17fa2f3c3fc6bc6929ef573bba3b1477f157fb9f162e627b13c5e60f02d6befccce395123fc061f8f2317173bd5ce8dc4 @@ -0,0 +1,46 @@ +#include "smg_rsa.h" + +#include +#include + +void err(char *msg) +{ + fprintf(stderr, "%s\n", msg); + exit(1); +} + +void time_entropy_source(int nruns, int noctets) { + unsigned char buffer[noctets]; + int read, i; + struct timespec tstart, tend; + long int diff; + + clock_gettime(CLOCK_MONOTONIC, &tstart); + for (i=0; i +#include +#include + +#include +#include +#include +#include + +#include "smg_rsa.h" + + +int set_usb_attribs(int fd, int speed) { + struct termios tty; + if (tcgetattr(fd, &tty) < 0) { + return -1; + } + + //input and output speeds + cfsetospeed(&tty, (speed_t)speed); + cfsetispeed(&tty, (speed_t)speed); + + tty.c_cflag |= (CLOCAL | CREAD); //ignore modem controls + tty.c_cflag &= ~CSIZE; + tty.c_cflag |= CS8; //8 bit characters + tty.c_cflag &= ~PARENB; //no parity bit + tty.c_cflag &= ~CSTOPB; //only need 1 stop bit + tty.c_cflag &= ~CRTSCTS; //no hardware flow control + + //non-canonical mode + tty.c_cflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); + tty.c_cflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); + tty.c_cflag &= ~OPOST; + + //read at least one octet at a time; timeout 1 tenth of second between octets read + tty.c_cc[VMIN] = 1; + tty.c_cc[VTIME] = 1; + + if (tcsetattr(fd, TCSANOW, &tty) != 0) + return -1; + + return 0; +} + +int open_entropy_source(char* source_name) { + int in, err; + + in = open(source_name, O_RDONLY | O_NOCTTY | O_NDELAY); + if (in == -1) { + printf("ERROR: failure to open entropy source %s: %s\n", source_name, strerror(errno)); + return in; //failed to access entropy source + } + + fcntl(in, F_SETFL, 0); + + err = set_usb_attribs(in, B115200); + if (err==-1) { + printf("Error setting attributes on %s: %s\n", source_name, strerror(errno)); + return err; + } + + return in; //source opened, return its descriptor +} + +int get_random_octets_from(int noctets, unsigned char *out, int from) { + + int nread; + int total = 0; + + while (total < noctets) { + nread = read(from, out+total, noctets-total); + //on interrupt received just try again + if (nread == -1 && errno == EINTR) + continue; + //on error condition abort + if (nread == -1 || nread == 0) { + printf("Error reading from entropy source %s: %s\n", ENTROPY_SOURCE, strerror(errno)); + return total; //total read so far + } + + if (nread > 0) + total = total + nread; + } + return total; //return number of octets read +} + +int get_random_octets(int noctets, unsigned char *out) { + int in; + int nread = 0; + + in = open_entropy_source(ENTROPY_SOURCE); + if (in > 0) { + nread = get_random_octets_from(noctets, out, in); + close(in); + } + return nread; +} +