tree checksum vpatch file split hunks
all signers: diana_coman
antecedents: eucrypt_genesis ch1_mpi
press order:
eucrypt_genesis | diana_coman |
ch1_mpi | diana_coman |
ch2_truerandom | diana_coman |
patch:
(1 . 3)(1 . 5)
5 NB: this is used by the smg_rsa component of EuCrypt.
6
7 What you see here is a very classic version of the GNU MPI (bignum) library.
8 It has been surgically removed from GnuPG 1.4.10, specifically as found at:
9
-(0 . 0)(1 . 27)
14 PROGRAM = smg_rsa.a
15
16 BUILD=obj
17 DIST=bin
18
19 CXX = gcc
20 OBJECTS = $(addprefix $(BUILD)/, $(patsubst %.c,%.o,$(wildcard *.c)))
21 MPI = ../mpi
22 FLAGS = -g -Wall
23 INCLUDE = -I include -I $(MPI)/include
24
25 .SUFFIXES: .o .c
26
27 $(BUILD)/%.o:
28 $(CXX) $(FLAGS) $(INCLUDE) -c $*.c -o $@
29
30 all: $(PROGRAM)
31
32 $(PROGRAM): $(OBJECTS)
33 ar rcs $(DIST)/$(PROGRAM) $(OBJECTS)
34 #ld -o $(DIST)/$(PROGRAM).o $(OBJECTS) -lc
35
36 clean :
37 rm -rf nul core *flymake* $(BUILD)/*.o $(DIST)/$(PROGRAM) *~ bin/*
38
39 check-syntax:
40 $(CXX) -c $(FLAGS) $(INCLUDE) -o nul -Wall -S $(CHK_SOURCES)
- FE2917EF90A8E9DEB4D9F7450CBBC20FDF3CA9F76630B6956137B4648916E143C89F857E0BF0FDE968FD241F3049050EF7F146254A9E8DAEAD54FC0B720C7620(1 . 2)(1 . 5)
45 S.MG, 2017
46
47 This is the S.MG implementation of RSA, used by the Eulora server.
48
49 NB: this lib is part of EuCrypt and as such, it relies on other EuCrypt components (most notably: mpi).
-(0 . 0)(1 . 1)
54 bin folder for smg_rsa lib
-(0 . 0)(1 . 7)
59 #ifndef SMG_RSA_KNOBS_H
60 #define SMG_RSA_KNOBS_H
61
62 #define ENTROPY_SOURCE "/dev/ttyUSB0"
63
64 #endif /*SMG_RSA_KNOBS_H*/
65
-(0 . 0)(1 . 44)
70 /* smg_rsa.h
71 * S.MG, 2017
72 */
73
74 #ifndef SMG_RSA_H
75 #define SMG_RSA_H
76
77 #include "mpi.h"
78 #include "knobs.h"
79
80 /*********truerandom.c*********/
81
82 /*
83 * Opens and configures (as per FG requirements) the specified entropy source (e.g. "/dev/ttyUSB0")
84 * @param source_name the name of the file to open (e.g. "/dev/ttyUSB0")
85 * @return the descriptor of the open file when successful; negative value otherwise
86 */
87 int open_entropy_source(char* source_name);
88
89
90 /*
91 * Returns noctets random octets (i.e. 8*noctets bits in total) as obtained from EuCrypt's preferred source.
92 * Preferred source is defined in knobs.h as ENTROPY_SOURCE and should be a TRNG (e.g. Fuckgoats).
93 * @param nboctets the length of desired random sequence, in octets
94 * @param out pointer to allocated memory space for the requested random noctets; NB: this method does NOT allocate space!
95 * @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)
96 */
97 int get_random_octets(int noctets, unsigned char *out);
98
99 /* Returns noctets random octets as obtained from the specified "from" source;
100 * NB: the "from" source is considered to be the handle of an already opened stream;
101 * This method will simply attempt to read from the source as needed!
102 *
103 * @param noctets the length of desired random sequence, in octets
104 * @param out pointer to allocated memory space for the requested random octets;
105 * NB: this method does NOT allocate space!
106 * @param from handle of an already opened entropy source - this method will just READ from it as needed
107 * @return the actual number of octets that were obtained
108 */
109 int get_random_octets_from(int noctets, unsigned char *out, int from);
110
111
112 #endif /*SMG_RSA*/
113
-(0 . 0)(1 . 1)
118 obj folder for smg_rsa
-(0 . 0)(1 . 25)
123 PROGRAM = tests
124
125 CXX = gcc
126 OBJECTS := $(patsubst %.c,%.o,$(wildcard *.c))
127 FLAGS = -g -Wall
128 INCLUDE = -I ../include -I ../../mpi/include
129 SMG_RSA = ../bin/smg_rsa.a
130 MPI = ../../mpi/bin/mpi.a
131 LIBS := $(SMG_RSA) $(MPI)
132
133 .SUFFIXES: .o .c
134
135 .c.o:
136 $(CXX) $(FLAGS) $(INCLUDE) -c $< -o $@
137
138 all: $(PROGRAM)
139
140 $(PROGRAM): $(OBJECTS)
141 $(CXX) $(FLAGS) $(INCLUDE) -o $(PROGRAM) $(OBJECTS) $(LIBS)
142
143 clean :
144 rm -rf nul core *flymake* *.o $(PROGRAM) *~ bin obj
145
146 check-syntax:
147 $(CXX) -c $(FLAGS) $(INCLUDE) -o nul -Wall -S $(CHK_SOURCES)
-(0 . 0)(1 . 46)
152 #include "smg_rsa.h"
153
154 #include <stdlib.h>
155 #include <time.h>
156
157 void err(char *msg)
158 {
159 fprintf(stderr, "%s\n", msg);
160 exit(1);
161 }
162
163 void time_entropy_source(int nruns, int noctets) {
164 unsigned char buffer[noctets];
165 int read, i;
166 struct timespec tstart, tend;
167 long int diff;
168
169 clock_gettime(CLOCK_MONOTONIC, &tstart);
170 for (i=0; i<nruns; i++) {
171 read = get_random_octets(noctets,buffer);
172 if (read != noctets)
173 err("Failed reading from entropy source!");
174 }
175 clock_gettime(CLOCK_MONOTONIC, &tend);
176
177 diff = tend.tv_sec-tstart.tv_sec;
178 double kbps = (nruns*noctets) / (diff*1000.0);
179 printf("ENTROPY source timing: %d kB in %ld seconds, at an average speed of %f kB/s over %d runs of %d octets each\n", nruns*noctets, diff, kbps, nruns, noctets);
180 }
181
182
183 int main(int ac, char **av)
184 {
185 int nruns;
186
187 if (ac<2) {
188 printf("Usage: %s number_of_runs\n", av[0]);
189 return -1;
190 }
191 nruns = atoi(av[1]);
192
193 printf("Timing entropy source...\n");
194 time_entropy_source(nruns,4096);
195
196 return 0;
197 }
-(0 . 0)(1 . 98)
202 #include <stdio.h>
203 #include <stdlib.h>
204 #include <string.h>
205
206 #include <fcntl.h>
207 #include <unistd.h>
208 #include <termios.h>
209 #include <errno.h>
210
211 #include "smg_rsa.h"
212
213
214 int set_usb_attribs(int fd, int speed) {
215 struct termios tty;
216 if (tcgetattr(fd, &tty) < 0) {
217 return -1;
218 }
219
220 //input and output speeds
221 cfsetospeed(&tty, (speed_t)speed);
222 cfsetispeed(&tty, (speed_t)speed);
223
224 tty.c_cflag |= (CLOCAL | CREAD); //ignore modem controls
225 tty.c_cflag &= ~CSIZE;
226 tty.c_cflag |= CS8; //8 bit characters
227 tty.c_cflag &= ~PARENB; //no parity bit
228 tty.c_cflag &= ~CSTOPB; //only need 1 stop bit
229 tty.c_cflag &= ~CRTSCTS; //no hardware flow control
230
231 //non-canonical mode
232 tty.c_cflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
233 tty.c_cflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
234 tty.c_cflag &= ~OPOST;
235
236 //read at least one octet at a time; timeout 1 tenth of second between octets read
237 tty.c_cc[VMIN] = 1;
238 tty.c_cc[VTIME] = 1;
239
240 if (tcsetattr(fd, TCSANOW, &tty) != 0)
241 return -1;
242
243 return 0;
244 }
245
246 int open_entropy_source(char* source_name) {
247 int in, err;
248
249 in = open(source_name, O_RDONLY | O_NOCTTY | O_NDELAY);
250 if (in == -1) {
251 printf("ERROR: failure to open entropy source %s: %s\n", source_name, strerror(errno));
252 return in; //failed to access entropy source
253 }
254
255 fcntl(in, F_SETFL, 0);
256
257 err = set_usb_attribs(in, B115200);
258 if (err==-1) {
259 printf("Error setting attributes on %s: %s\n", source_name, strerror(errno));
260 return err;
261 }
262
263 return in; //source opened, return its descriptor
264 }
265
266 int get_random_octets_from(int noctets, unsigned char *out, int from) {
267
268 int nread;
269 int total = 0;
270
271 while (total < noctets) {
272 nread = read(from, out+total, noctets-total);
273 //on interrupt received just try again
274 if (nread == -1 && errno == EINTR)
275 continue;
276 //on error condition abort
277 if (nread == -1 || nread == 0) {
278 printf("Error reading from entropy source %s: %s\n", ENTROPY_SOURCE, strerror(errno));
279 return total; //total read so far
280 }
281
282 if (nread > 0)
283 total = total + nread;
284 }
285 return total; //return number of octets read
286 }
287
288 int get_random_octets(int noctets, unsigned char *out) {
289 int in;
290 int nread = 0;
291
292 in = open_entropy_source(ENTROPY_SOURCE);
293 if (in > 0) {
294 nread = get_random_octets_from(noctets, out, in);
295 close(in);
296 }
297 return nread;
298 }
299