smg_comms_c_wrappers 1 #include "smg_rsa.h"
smg_comms_c_wrappers 2 #include "mpi.h"
smg_comms_c_wrappers 3
smg_comms_c_wrappers 4 #include <stdlib.h>
smg_comms_c_wrappers 5 #include <unistd.h>
smg_comms_c_wrappers 6 #include <time.h>
smg_comms_c_wrappers 7 #include <stdio.h>
smg_comms_c_wrappers 8
smg_comms_c_wrappers 9 extern void adainit(void);
smg_comms_c_wrappers 10 extern void adafinal(void);
smg_comms_c_wrappers 11
smg_comms_c_wrappers 12 void err(char *msg)
smg_comms_c_wrappers 13 {
smg_comms_c_wrappers 14 fprintf(stderr, "%s\n", msg);
smg_comms_c_wrappers 15 exit(1);
smg_comms_c_wrappers 16 }
smg_comms_c_wrappers 17
smg_comms_c_wrappers 18 void time_entropy_source(int nruns, int noctets) {
smg_comms_c_wrappers 19 unsigned char buffer[noctets];
smg_comms_c_wrappers 20 int read, i;
smg_comms_c_wrappers 21 struct timespec tstart, tend;
smg_comms_c_wrappers 22 long int diff;
smg_comms_c_wrappers 23
smg_comms_c_wrappers 24 clock_gettime(CLOCK_MONOTONIC, &tstart);
smg_comms_c_wrappers 25 for (i=0; i<nruns; i++) {
smg_comms_c_wrappers 26 read = get_random_octets(noctets,buffer);
smg_comms_c_wrappers 27 if (read != noctets)
smg_comms_c_wrappers 28 err("Failed reading from entropy source!");
smg_comms_c_wrappers 29 }
smg_comms_c_wrappers 30 clock_gettime(CLOCK_MONOTONIC, &tend);
smg_comms_c_wrappers 31
smg_comms_c_wrappers 32 diff = tend.tv_sec-tstart.tv_sec;
smg_comms_c_wrappers 33 double kbps = (nruns*noctets) / (diff*1000.0);
smg_comms_c_wrappers 34 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);
smg_comms_c_wrappers 35 }
smg_comms_c_wrappers 36
smg_comms_c_wrappers 37 void test_entropy_output(unsigned int noctets, char * filename) {
smg_comms_c_wrappers 38 FILE * out;
smg_comms_c_wrappers 39 int source;
smg_comms_c_wrappers 40 unsigned int nread, total_read, to_read;
smg_comms_c_wrappers 41 const int buffer_length = 1000;
smg_comms_c_wrappers 42 unsigned char buffer[buffer_length];
smg_comms_c_wrappers 43
smg_comms_c_wrappers 44 source = open_entropy_source(ENTROPY_SOURCE);
smg_comms_c_wrappers 45 if (source <= 0)
smg_comms_c_wrappers 46 err("unable to access entropy source!");
smg_comms_c_wrappers 47
smg_comms_c_wrappers 48 out = fopen(filename, "wb");
smg_comms_c_wrappers 49 if ( !out )
smg_comms_c_wrappers 50 err("unable to open output file for test_entropy_output!");
smg_comms_c_wrappers 51
smg_comms_c_wrappers 52 printf("TEST_ENTROPY_SOURCE: reading %u octets from %s ", noctets, ENTROPY_SOURCE);
smg_comms_c_wrappers 53 total_read = 0;
smg_comms_c_wrappers 54 while (total_read < noctets) {
smg_comms_c_wrappers 55 to_read = noctets - total_read;
smg_comms_c_wrappers 56 if (to_read > buffer_length)
smg_comms_c_wrappers 57 to_read = buffer_length;
smg_comms_c_wrappers 58
smg_comms_c_wrappers 59 nread = get_random_octets_from(to_read, buffer, source);
smg_comms_c_wrappers 60 if (nread > 0) {
smg_comms_c_wrappers 61 total_read = total_read + nread;
smg_comms_c_wrappers 62 fwrite(buffer, 1, nread, out);
smg_comms_c_wrappers 63 fflush(out);
smg_comms_c_wrappers 64 printf(".");
smg_comms_c_wrappers 65 fflush(stdout);
smg_comms_c_wrappers 66 }
smg_comms_c_wrappers 67 }
smg_comms_c_wrappers 68 printf("done.\n");
smg_comms_c_wrappers 69
smg_comms_c_wrappers 70 fclose(out);
smg_comms_c_wrappers 71 close(source);
smg_comms_c_wrappers 72 }
smg_comms_c_wrappers 73
smg_comms_c_wrappers 74 void test_is_composite(int nruns, char *hex_number, int expected) {
smg_comms_c_wrappers 75 int i;
smg_comms_c_wrappers 76 int output;
smg_comms_c_wrappers 77 int count_ok = 0;
smg_comms_c_wrappers 78 int source = open_entropy_source(ENTROPY_SOURCE);
smg_comms_c_wrappers 79 MPI p = mpi_alloc(0);
smg_comms_c_wrappers 80
smg_comms_c_wrappers 81 mpi_fromstr(p, hex_number);
smg_comms_c_wrappers 82 printf("TEST is_composite on MPI(hex) ");
smg_comms_c_wrappers 83 mpi_print(stdout, p, 1);
smg_comms_c_wrappers 84 for (i=0; i < nruns; i++) {
smg_comms_c_wrappers 85 printf(".");
smg_comms_c_wrappers 86 fflush(stdout);
smg_comms_c_wrappers 87 output = is_composite(p, M_R_ITERATIONS, source);
smg_comms_c_wrappers 88 if (output == expected)
smg_comms_c_wrappers 89 count_ok = count_ok + 1;
smg_comms_c_wrappers 90 }
smg_comms_c_wrappers 91 printf("done, with %d out of %d correct runs for expected=%d: %s\n", count_ok, nruns, expected, count_ok==nruns? "PASS":"FAIL");
smg_comms_c_wrappers 92 mpi_free(p);
smg_comms_c_wrappers 93 close(source);
smg_comms_c_wrappers 94 }
smg_comms_c_wrappers 95
smg_comms_c_wrappers 96 void time_mr(int nruns) {
smg_comms_c_wrappers 97 struct timespec tstart, tend;
smg_comms_c_wrappers 98 long int diff;
smg_comms_c_wrappers 99 int i;
smg_comms_c_wrappers 100 MPI prime;
smg_comms_c_wrappers 101 unsigned int noctets = KEY_LENGTH_OCTETS / 2;
smg_comms_c_wrappers 102 unsigned int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
smg_comms_c_wrappers 103
smg_comms_c_wrappers 104 int entropy_source = open_entropy_source(ENTROPY_SOURCE);
smg_comms_c_wrappers 105 if (entropy_source <= 0)
smg_comms_c_wrappers 106 err("can't open entropy source!");
smg_comms_c_wrappers 107
smg_comms_c_wrappers 108
smg_comms_c_wrappers 109 printf("Generating a prime number of %d octets length for M-R timing test\n", noctets);
smg_comms_c_wrappers 110 prime = mpi_alloc(nlimbs);
smg_comms_c_wrappers 111 gen_random_prime(noctets, prime);
smg_comms_c_wrappers 112
smg_comms_c_wrappers 113 printf("Running timing test for Miller-Rabin with %d repetitions and %d witnesses on prime number ", nruns, M_R_ITERATIONS);
smg_comms_c_wrappers 114 mpi_print(stdout, prime, 1);
smg_comms_c_wrappers 115 printf("\n");
smg_comms_c_wrappers 116
smg_comms_c_wrappers 117 clock_gettime(CLOCK_MONOTONIC, &tstart);
smg_comms_c_wrappers 118 for (i=0; i<nruns; i++) {
smg_comms_c_wrappers 119 if (is_composite(prime, M_R_ITERATIONS, entropy_source))
smg_comms_c_wrappers 120 printf("FAIL");
smg_comms_c_wrappers 121 else printf(".");
smg_comms_c_wrappers 122 fflush(stdout);
smg_comms_c_wrappers 123 }
smg_comms_c_wrappers 124 clock_gettime(CLOCK_MONOTONIC, &tend);
smg_comms_c_wrappers 125
smg_comms_c_wrappers 126 diff = tend.tv_sec-tstart.tv_sec;
smg_comms_c_wrappers 127 printf("\nTimings on prime number %d octets long, %d runs of MR with %d iterations (witnesses checked) each\n", \
smg_comms_c_wrappers 128 noctets, nruns, M_R_ITERATIONS);
smg_comms_c_wrappers 129 printf("Total time: %ld seconds\nTime per MR run: %f seconds\nTime per MR iteration: %f seconds\n",\
smg_comms_c_wrappers 130 diff, diff / (1.0*nruns), diff / (1.0*nruns * M_R_ITERATIONS));
smg_comms_c_wrappers 131
smg_comms_c_wrappers 132 mpi_free(prime);
smg_comms_c_wrappers 133 close(entropy_source);
smg_comms_c_wrappers 134 }
smg_comms_c_wrappers 135
smg_comms_c_wrappers 136 void test_rpng(int nruns) {
smg_comms_c_wrappers 137 unsigned int noctets = KEY_LENGTH_OCTETS / 2;
smg_comms_c_wrappers 138 unsigned int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
smg_comms_c_wrappers 139 int entropy_source = open_entropy_source(ENTROPY_SOURCE);
smg_comms_c_wrappers 140 if (entropy_source <= 0)
smg_comms_c_wrappers 141 err("can't open entropy source!");
smg_comms_c_wrappers 142
smg_comms_c_wrappers 143 MPI prime = mpi_alloc(nlimbs);
smg_comms_c_wrappers 144 int i;
smg_comms_c_wrappers 145
smg_comms_c_wrappers 146 printf("TEST: random prime number generator with %d runs\n", nruns);
smg_comms_c_wrappers 147 for (i = 0;i < nruns; i++) {
smg_comms_c_wrappers 148 gen_random_prime(noctets, prime);
smg_comms_c_wrappers 149 printf("Run %d: ", i+1);
smg_comms_c_wrappers 150 mpi_print(stdout, prime, 1);
smg_comms_c_wrappers 151 if (is_composite(prime, M_R_ITERATIONS, entropy_source))
smg_comms_c_wrappers 152 printf(" **FAIL**\n");
smg_comms_c_wrappers 153 else
smg_comms_c_wrappers 154 printf(" **PASS**\n");
smg_comms_c_wrappers 155 }
smg_comms_c_wrappers 156
smg_comms_c_wrappers 157 mpi_free(prime);
smg_comms_c_wrappers 158 close(entropy_source);
smg_comms_c_wrappers 159 }
smg_comms_c_wrappers 160
smg_comms_c_wrappers 161 void time_rpng(int nruns) {
smg_comms_c_wrappers 162 struct timespec tstart, tend;
smg_comms_c_wrappers 163 long int diff;
smg_comms_c_wrappers 164
smg_comms_c_wrappers 165 unsigned int noctets = KEY_LENGTH_OCTETS / 2;
smg_comms_c_wrappers 166 unsigned int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
smg_comms_c_wrappers 167
smg_comms_c_wrappers 168 int entropy_source = open_entropy_source(ENTROPY_SOURCE);
smg_comms_c_wrappers 169 if (entropy_source <= 0)
smg_comms_c_wrappers 170 err("can't open entropy source!");
smg_comms_c_wrappers 171
smg_comms_c_wrappers 172 MPI prime = mpi_alloc(nlimbs);
smg_comms_c_wrappers 173 int i;
smg_comms_c_wrappers 174
smg_comms_c_wrappers 175 printf("TIMING: random prime number generator with %d runs\n", nruns);
smg_comms_c_wrappers 176 clock_gettime(CLOCK_MONOTONIC, &tstart);
smg_comms_c_wrappers 177 for (i = 0;i < nruns; i++) {
smg_comms_c_wrappers 178 gen_random_prime(noctets, prime);
smg_comms_c_wrappers 179 }
smg_comms_c_wrappers 180 clock_gettime(CLOCK_MONOTONIC, &tend);
smg_comms_c_wrappers 181
smg_comms_c_wrappers 182 diff = tend.tv_sec-tstart.tv_sec;
smg_comms_c_wrappers 183
smg_comms_c_wrappers 184 printf("TOTAL: %ld seconds\n", diff);
smg_comms_c_wrappers 185 printf("Average: %f seconds to generate one random prime of %d octets length\n", diff / (1.0*nruns), noctets);
smg_comms_c_wrappers 186 mpi_free(prime);
smg_comms_c_wrappers 187 close(entropy_source);
smg_comms_c_wrappers 188 }
smg_comms_c_wrappers 189
smg_comms_c_wrappers 190
smg_comms_c_wrappers 191 * Output is written to file.
smg_comms_c_wrappers 192 */
smg_comms_c_wrappers 193 void test_rsa_keys( RSA_secret_key *sk, unsigned int noctets, FILE *file ) {
smg_comms_c_wrappers 194 RSA_public_key pk;
smg_comms_c_wrappers 195 MPI test = mpi_alloc ( mpi_nlimb_hint_from_nbytes (noctets) );
smg_comms_c_wrappers 196 MPI out1 = mpi_alloc ( mpi_nlimb_hint_from_nbytes (noctets) );
smg_comms_c_wrappers 197 MPI out2 = mpi_alloc ( mpi_nlimb_hint_from_nbytes (noctets) );
smg_comms_c_wrappers 198
smg_comms_c_wrappers 199 pk.n = mpi_copy(sk->n);
smg_comms_c_wrappers 200 pk.e = mpi_copy(sk->e);
smg_comms_c_wrappers 201 unsigned char *p;
smg_comms_c_wrappers 202 p = xmalloc(noctets);
smg_comms_c_wrappers 203
smg_comms_c_wrappers 204 fprintf(file, "TEST encrypt/decrypt on %d octets of random data\n", noctets);
smg_comms_c_wrappers 205 fflush(file);
smg_comms_c_wrappers 206 if (get_random_octets( noctets, p) == noctets) {
smg_comms_c_wrappers 207 mpi_set_buffer( test, p, noctets, 0 );
smg_comms_c_wrappers 208
smg_comms_c_wrappers 209 fprintf(file, "TEST data:\n");
smg_comms_c_wrappers 210 mpi_print(file, test, 1);
smg_comms_c_wrappers 211 fprintf(file, "\n");
smg_comms_c_wrappers 212 fflush(file);
smg_comms_c_wrappers 213
smg_comms_c_wrappers 214 public_rsa( out1, test, &pk );
smg_comms_c_wrappers 215 secret_rsa( out2, out1, sk );
smg_comms_c_wrappers 216
smg_comms_c_wrappers 217 fprintf(file, "ENCRYPTED with PUBLIC key data:\n");
smg_comms_c_wrappers 218 mpi_print(file, out1, 1);
smg_comms_c_wrappers 219 fprintf(file, "\n");
smg_comms_c_wrappers 220 fflush(file);
smg_comms_c_wrappers 221
smg_comms_c_wrappers 222 fprintf(file, "DECRYPTED with SECRET key:\n");
smg_comms_c_wrappers 223 mpi_print(file, out2, 1);
smg_comms_c_wrappers 224 fprintf(file, "\n");
smg_comms_c_wrappers 225 fflush(file);
smg_comms_c_wrappers 226
smg_comms_c_wrappers 227 if( mpi_cmp( test, out2 ) )
smg_comms_c_wrappers 228 fprintf(file, "FAILED: RSA operation: public(secret) failed\n");
smg_comms_c_wrappers 229 else
smg_comms_c_wrappers 230 fprintf(file, "PASSED: RSA operation: public(secret) passed\n");
smg_comms_c_wrappers 231 fflush(file);
smg_comms_c_wrappers 232
smg_comms_c_wrappers 233 secret_rsa( out1, test, sk );
smg_comms_c_wrappers 234 public_rsa( out2, out1, &pk );
smg_comms_c_wrappers 235 if( mpi_cmp( test, out2 ) )
smg_comms_c_wrappers 236 fprintf(file, "FAILED: RSA operation: secret(public) failed\n");
smg_comms_c_wrappers 237 else
smg_comms_c_wrappers 238 fprintf(file, "PASSED: RSA operation: secret(public) passed\n");
smg_comms_c_wrappers 239 }
smg_comms_c_wrappers 240 else
smg_comms_c_wrappers 241 fprintf(file, "FAILED: not enough bits returned from entropy source\n");
smg_comms_c_wrappers 242
smg_comms_c_wrappers 243 fflush(file);
smg_comms_c_wrappers 244 xfree(p);
smg_comms_c_wrappers 245 mpi_free( pk.n);
smg_comms_c_wrappers 246 mpi_free( pk.e);
smg_comms_c_wrappers 247
smg_comms_c_wrappers 248 mpi_free( test );
smg_comms_c_wrappers 249 mpi_free( out1 );
smg_comms_c_wrappers 250 mpi_free( out2 );
smg_comms_c_wrappers 251 }
smg_comms_c_wrappers 252
smg_comms_c_wrappers 253 void test_rsa( int nruns, FILE *fkeys, FILE *fout) {
smg_comms_c_wrappers 254 RSA_secret_key sk;
smg_comms_c_wrappers 255 int noctets = KEY_LENGTH_OCTETS;
smg_comms_c_wrappers 256 int noctets_pq = noctets / 2;
smg_comms_c_wrappers 257 int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
smg_comms_c_wrappers 258 int nlimbs_pq = mpi_nlimb_hint_from_nbytes(noctets_pq);
smg_comms_c_wrappers 259 int i;
smg_comms_c_wrappers 260
smg_comms_c_wrappers 261 sk.n = mpi_alloc(nlimbs);
smg_comms_c_wrappers 262 sk.e = mpi_alloc(nlimbs);
smg_comms_c_wrappers 263 sk.d = mpi_alloc(nlimbs);
smg_comms_c_wrappers 264 sk.p = mpi_alloc(nlimbs_pq);
smg_comms_c_wrappers 265 sk.q = mpi_alloc(nlimbs_pq);
smg_comms_c_wrappers 266 sk.u = mpi_alloc(nlimbs_pq);
smg_comms_c_wrappers 267
smg_comms_c_wrappers 268 printf("TEST RSA key generation and use with %d runs\n", nruns);
smg_comms_c_wrappers 269 fflush(stdout);
smg_comms_c_wrappers 270
smg_comms_c_wrappers 271 for (i = 0;i < nruns; i++) {
smg_comms_c_wrappers 272 gen_keypair(&sk);
smg_comms_c_wrappers 273 printf(".");
smg_comms_c_wrappers 274 fflush(stdout);
smg_comms_c_wrappers 275
smg_comms_c_wrappers 276 mpi_print(fkeys, sk.n, 1);
smg_comms_c_wrappers 277 fwrite("\n", sizeof(char), 1, fkeys);
smg_comms_c_wrappers 278
smg_comms_c_wrappers 279 mpi_print(fkeys, sk.e, 1);
smg_comms_c_wrappers 280 fwrite("\n", sizeof(char), 1, fkeys);
smg_comms_c_wrappers 281
smg_comms_c_wrappers 282 mpi_print(fkeys, sk.d, 1);
smg_comms_c_wrappers 283 fwrite("\n", sizeof(char), 1, fkeys);
smg_comms_c_wrappers 284
smg_comms_c_wrappers 285 mpi_print(fkeys, sk.p, 1);
smg_comms_c_wrappers 286 fwrite("\n", sizeof(char), 1, fkeys);
smg_comms_c_wrappers 287
smg_comms_c_wrappers 288 mpi_print(fkeys, sk.q, 1);
smg_comms_c_wrappers 289 fwrite("\n", sizeof(char), 1, fkeys);
smg_comms_c_wrappers 290
smg_comms_c_wrappers 291 mpi_print(fkeys, sk.u, 1);
smg_comms_c_wrappers 292 fwrite("\n", sizeof(char), 1, fkeys);
smg_comms_c_wrappers 293
smg_comms_c_wrappers 294 test_rsa_keys(&sk, noctets_pq, fout);
smg_comms_c_wrappers 295 printf("*");
smg_comms_c_wrappers 296 fflush(stdout);
smg_comms_c_wrappers 297 }
smg_comms_c_wrappers 298
smg_comms_c_wrappers 299 mpi_free(sk.n);
smg_comms_c_wrappers 300 mpi_free(sk.e);
smg_comms_c_wrappers 301 mpi_free(sk.d);
smg_comms_c_wrappers 302 mpi_free(sk.p);
smg_comms_c_wrappers 303 mpi_free(sk.q);
smg_comms_c_wrappers 304 mpi_free(sk.u);
smg_comms_c_wrappers 305
smg_comms_c_wrappers 306 }
smg_comms_c_wrappers 307
smg_comms_c_wrappers 308 void test_rsa_exp() {
smg_comms_c_wrappers 309 MPI msg = mpi_alloc(0);
smg_comms_c_wrappers 310 MPI expected = mpi_alloc(0);
smg_comms_c_wrappers 311 MPI result;
smg_comms_c_wrappers 312
smg_comms_c_wrappers 313 RSA_public_key pk;
smg_comms_c_wrappers 314 pk.n = mpi_alloc(0);
smg_comms_c_wrappers 315 pk.e = mpi_alloc(0);
smg_comms_c_wrappers 316
smg_comms_c_wrappers 317 printf("TEST verify of rsa exponentiation on input data: \n");
smg_comms_c_wrappers 318
smg_comms_c_wrappers 319 mpi_fromstr(msg, "0x\
smg_comms_c_wrappers 320 5B6A8A0ACF4F4DB3F82EAC2D20255E4DF3E4B7C799603210766F26EF87C8980E737579\
smg_comms_c_wrappers 321 EC08E6505A51D19654C26D806BAF1B62F9C032E0B13D02AF99F7313BFCFD68DA46836E\
smg_comms_c_wrappers 322 CA529D7360948550F982C6476C054A97FD01635AB44BFBDBE2A90BE06F7984AC8534C3\
smg_comms_c_wrappers 323 8613747F340C18176E6D5F0C10246A2FCE3A668EACB6165C2052497CA2EE483F4FD8D0\
smg_comms_c_wrappers 324 6A9911BD97E9B6720521D872BD08FF8DA11A1B8DB147F252E4E69AE6201D3B374B171D\
smg_comms_c_wrappers 325 F445EF2BF509D468FD57CEB5840349B14C6E2AAA194D9531D238B85B8F0DD352D1E596\
smg_comms_c_wrappers 326 71539B429849E5D965E438BF9EFFC338DF9AADF304C4130D5A05E006ED855F37A06242\
smg_comms_c_wrappers 327 28097EF92F6E78CAE0CB97");
smg_comms_c_wrappers 328
smg_comms_c_wrappers 329 mpi_fromstr(expected, "0x\
smg_comms_c_wrappers 330 1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
smg_comms_c_wrappers 331 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
smg_comms_c_wrappers 332 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
smg_comms_c_wrappers 333 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
smg_comms_c_wrappers 334 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003051300\
smg_comms_c_wrappers 335 D0609608648016503040203050004406255509399A3AF322C486C770C5F7F6E05E18FC\
smg_comms_c_wrappers 336 3E2219A03CA56C7501426A597187468B2F71B4A198C807171B73D0E7DBC3EEF6EA6AFF\
smg_comms_c_wrappers 337 693DE58E18FF84395BE");
smg_comms_c_wrappers 338 result = mpi_alloc( mpi_get_nlimbs(expected) );
smg_comms_c_wrappers 339
smg_comms_c_wrappers 340 mpi_fromstr(pk.n, "0x\
smg_comms_c_wrappers 341 CDD49A674BAF76D3B73E25BC6DF66EF3ABEDDCA461D3CCB6416793E3437C7806562694\
smg_comms_c_wrappers 342 73C2212D5FD5EED17AA067FEC001D8E76EC901EDEDF960304F891BD3CAD7F9A335D1A2\
smg_comms_c_wrappers 343 EC37EABEFF3FBE6D3C726DC68E599EBFE5456EF19813398CD7D548D746A30AA47D4293\
smg_comms_c_wrappers 344 968BFBAFCBF65A90DFFC87816FEE2A01E1DC699F4DDABB84965514C0D909D54FDA7062\
smg_comms_c_wrappers 345 A2037B50B771C153D5429BA4BA335EAB840F9551E9CD9DF8BB4A6DC3ED1318FF3969F7\
smg_comms_c_wrappers 346 B99D9FB90CAB968813F8AD4F9A069C9639A74D70A659C69C29692567CE863B88E191CC\
smg_comms_c_wrappers 347 9535B91B417D0AF14BE09C78B53AF9C5F494BCF2C60349FFA93C81E817AC682F0055A6\
smg_comms_c_wrappers 348 07BB56D6A281C1A04CEFE1");
smg_comms_c_wrappers 349
smg_comms_c_wrappers 350 mpi_fromstr( pk.e, "0x10001");
smg_comms_c_wrappers 351
smg_comms_c_wrappers 352 mpi_print( stdout, msg, 1);
smg_comms_c_wrappers 353 printf("\n");
smg_comms_c_wrappers 354
smg_comms_c_wrappers 355 public_rsa( result, msg, &pk);
smg_comms_c_wrappers 356 if ( mpi_cmp( result, expected) != 0 )
smg_comms_c_wrappers 357 printf( "FAILED\n");
smg_comms_c_wrappers 358 else
smg_comms_c_wrappers 359 printf( "PASSED\n");
smg_comms_c_wrappers 360
smg_comms_c_wrappers 361 printf("Expected:\n");
smg_comms_c_wrappers 362 mpi_print( stdout, expected, 1);
smg_comms_c_wrappers 363 printf("\n");
smg_comms_c_wrappers 364
smg_comms_c_wrappers 365 printf("Obtained:\n");
smg_comms_c_wrappers 366 mpi_print( stdout, result, 1);
smg_comms_c_wrappers 367 printf("\n");
smg_comms_c_wrappers 368
smg_comms_c_wrappers 369 mpi_free( pk.n );
smg_comms_c_wrappers 370 mpi_free( pk.e );
smg_comms_c_wrappers 371 mpi_free( msg );
smg_comms_c_wrappers 372 mpi_free( expected );
smg_comms_c_wrappers 373 mpi_free( result );
smg_comms_c_wrappers 374 }
smg_comms_c_wrappers 375
smg_comms_c_wrappers 376 void time_rsa_gen( int nruns ) {
smg_comms_c_wrappers 377 struct timespec tstart, tend;
smg_comms_c_wrappers 378 long int diff;
smg_comms_c_wrappers 379 int i;
smg_comms_c_wrappers 380
smg_comms_c_wrappers 381 RSA_secret_key sk;
smg_comms_c_wrappers 382 int noctets = KEY_LENGTH_OCTETS;
smg_comms_c_wrappers 383 int noctets_pq = noctets / 2;
smg_comms_c_wrappers 384 int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
smg_comms_c_wrappers 385 int nlimbs_pq = mpi_nlimb_hint_from_nbytes(noctets_pq);
smg_comms_c_wrappers 386 sk.n = mpi_alloc(nlimbs);
smg_comms_c_wrappers 387 sk.e = mpi_alloc(nlimbs);
smg_comms_c_wrappers 388 sk.d = mpi_alloc(nlimbs);
smg_comms_c_wrappers 389 sk.p = mpi_alloc(nlimbs_pq);
smg_comms_c_wrappers 390 sk.q = mpi_alloc(nlimbs_pq);
smg_comms_c_wrappers 391 sk.u = mpi_alloc(nlimbs_pq);
smg_comms_c_wrappers 392
smg_comms_c_wrappers 393 clock_gettime(CLOCK_MONOTONIC, &tstart);
smg_comms_c_wrappers 394 for (i = 0;i < nruns; i++) {
smg_comms_c_wrappers 395 gen_keypair(&sk);
smg_comms_c_wrappers 396 }
smg_comms_c_wrappers 397 clock_gettime(CLOCK_MONOTONIC, &tend);
smg_comms_c_wrappers 398
smg_comms_c_wrappers 399 diff = tend.tv_sec-tstart.tv_sec;
smg_comms_c_wrappers 400
smg_comms_c_wrappers 401 printf("TOTAL: %ld seconds for generating %d key pairs\n", diff, nruns);
smg_comms_c_wrappers 402 printf("Average (%d runs): %f seconds per TMSR RSA key pair.\n",
smg_comms_c_wrappers 403 nruns, diff / (1.0*nruns));
smg_comms_c_wrappers 404 mpi_free(sk.n);
smg_comms_c_wrappers 405 mpi_free(sk.e);
smg_comms_c_wrappers 406 mpi_free(sk.d);
smg_comms_c_wrappers 407 mpi_free(sk.p);
smg_comms_c_wrappers 408 mpi_free(sk.q);
smg_comms_c_wrappers 409 mpi_free(sk.u);
smg_comms_c_wrappers 410 }
smg_comms_c_wrappers 411
smg_comms_c_wrappers 412 void test_mpi_buffer() {
smg_comms_c_wrappers 413 unsigned int noctets = 10;
smg_comms_c_wrappers 414 int nlimbs = mpi_nlimb_hint_from_nbytes( noctets );
smg_comms_c_wrappers 415 MPI m = mpi_alloc( nlimbs );
smg_comms_c_wrappers 416 unsigned char *setbuffer = xmalloc( noctets );
smg_comms_c_wrappers 417 unsigned char *getbuffer;
smg_comms_c_wrappers 418 unsigned int i, sign, mpilen, nerrors;
smg_comms_c_wrappers 419
smg_comms_c_wrappers 420 for (i=0; i< noctets; i++)
smg_comms_c_wrappers 421 setbuffer[i] = i;
smg_comms_c_wrappers 422
smg_comms_c_wrappers 423 mpi_set_buffer( m, setbuffer, noctets, 0);
smg_comms_c_wrappers 424
smg_comms_c_wrappers 425 getbuffer = mpi_get_buffer( m, &mpilen, &sign );
smg_comms_c_wrappers 426
smg_comms_c_wrappers 427 if (mpilen == noctets -1 ) {
smg_comms_c_wrappers 428 nerrors = 0;
smg_comms_c_wrappers 429 for (i=0;i<noctets-1;i++)
smg_comms_c_wrappers 430 if (setbuffer[i+1] != getbuffer[i])
smg_comms_c_wrappers 431 nerrors = nerrors + 1;
smg_comms_c_wrappers 432 if (nerrors == 0) {
smg_comms_c_wrappers 433 printf("WARNING: 0-led octet discarded by mpi_set_buffer!\n");
smg_comms_c_wrappers 434 printf("Value ret by mpi_get_buffer != value given to set_buffer!\n");
smg_comms_c_wrappers 435 }
smg_comms_c_wrappers 436 else
smg_comms_c_wrappers 437 printf("FAIL: got different lengths and %d different values!\n", nerrors);
smg_comms_c_wrappers 438 }
smg_comms_c_wrappers 439 else if (mpilen != noctets)
smg_comms_c_wrappers 440 printf("FAIL: mpilen is %d; noctets is %d\n", mpilen, noctets);
smg_comms_c_wrappers 441 else
smg_comms_c_wrappers 442 {
smg_comms_c_wrappers 443 nerrors = 0;
smg_comms_c_wrappers 444 for (i=0;i<noctets-1;i++) {
smg_comms_c_wrappers 445 if (setbuffer[i]!=getbuffer[i])
smg_comms_c_wrappers 446 nerrors= nerrors+1;
smg_comms_c_wrappers 447 }
smg_comms_c_wrappers 448 if (nerrors>0)
smg_comms_c_wrappers 449 printf("FAIL: got %d different values!\n", nerrors);
smg_comms_c_wrappers 450 else printf("PASSED: mpi_get/set_buffer\n");
smg_comms_c_wrappers 451 }
smg_comms_c_wrappers 452
smg_comms_c_wrappers 453 mpi_free(m);
smg_comms_c_wrappers 454 xfree(setbuffer);
smg_comms_c_wrappers 455 xfree(getbuffer);
smg_comms_c_wrappers 456 }
smg_comms_c_wrappers 457
smg_comms_c_wrappers 458 void test_dirty_float_rng( int nruns ) {
smg_comms_c_wrappers 459 int i, status;
smg_comms_c_wrappers 460 float dirty;
smg_comms_c_wrappers 461
smg_comms_c_wrappers 462 printf("Running test for smg rng dirty float with %d runs\n", nruns);
smg_comms_c_wrappers 463 for (i=0; i<nruns; i++) {
smg_comms_c_wrappers 464 status = rng_dirty_float( &dirty );
smg_comms_c_wrappers 465 printf("Run %d: %f status %s\n", i+1, dirty, status>0 ? "OK" : "FAIL");
smg_comms_c_wrappers 466 }
smg_comms_c_wrappers 467 }
smg_comms_c_wrappers 468
smg_comms_c_wrappers 469 void test_ieee_float_rng( int nruns ) {
smg_comms_c_wrappers 470 int i, status;
smg_comms_c_wrappers 471 float ieee;
smg_comms_c_wrappers 472
smg_comms_c_wrappers 473 printf("Running test for smg rng ieee 745/1985 float with %d runs\n", nruns);
smg_comms_c_wrappers 474 for (i=0; i<nruns; i++) {
smg_comms_c_wrappers 475 status = rng_float_754_1985( &ieee );
smg_comms_c_wrappers 476 printf("Run %d: %f status %s\n", i+1, ieee, status>0 ? "OK" : "FAIL");
smg_comms_c_wrappers 477 }
smg_comms_c_wrappers 478 }
smg_comms_c_wrappers 479
smg_comms_c_wrappers 480 void test_uint32_rng( int nruns ) {
smg_comms_c_wrappers 481 int i, status;
smg_comms_c_wrappers 482 uint32_t n;
smg_comms_c_wrappers 483
smg_comms_c_wrappers 484 printf("Running test for smg rng unsigned int32 with %d runs\n", nruns);
smg_comms_c_wrappers 485 for (i=0; i<nruns; i++) {
smg_comms_c_wrappers 486 status = rng_uint32( &n );
smg_comms_c_wrappers 487 printf("Run %d: %"PRIu32" status %s\n", i+1, n, status>0 ? "OK" : "FAIL");
smg_comms_c_wrappers 488 }
smg_comms_c_wrappers 489 }
smg_comms_c_wrappers 490
smg_comms_c_wrappers 491 void test_uint64_rng( int nruns ) {
smg_comms_c_wrappers 492 int i, status;
smg_comms_c_wrappers 493 uint64_t n;
smg_comms_c_wrappers 494
smg_comms_c_wrappers 495 printf("Running test for smg rng unsigned int64 with %d runs\n", nruns);
smg_comms_c_wrappers 496 for (i=0; i<nruns; i++) {
smg_comms_c_wrappers 497 status = rng_uint64( &n );
smg_comms_c_wrappers 498 printf("Run %d: %"PRIu64" status %s\n", i+1, n, status>0 ? "OK" : "FAIL");
smg_comms_c_wrappers 499 }
smg_comms_c_wrappers 500 }
smg_comms_c_wrappers 501
smg_comms_c_wrappers 502
smg_comms_c_wrappers 503 int main(int ac, char **av)
smg_comms_c_wrappers 504 {
smg_comms_c_wrappers 505 int nruns;
smg_comms_c_wrappers 506 int id;
smg_comms_c_wrappers 507 FILE *fk;
smg_comms_c_wrappers 508 FILE *fout;
smg_comms_c_wrappers 509
smg_comms_c_wrappers 510 if (ac<2) {
smg_comms_c_wrappers 511 printf("Usage: %s number_of_runs/octets [testID]\n", av[0]);
smg_comms_c_wrappers 512 return -1;
smg_comms_c_wrappers 513 }
smg_comms_c_wrappers 514 nruns = atoi(av[1]);
smg_comms_c_wrappers 515
smg_comms_c_wrappers 516 if (ac < 3)
smg_comms_c_wrappers 517 id = -1;
smg_comms_c_wrappers 518 else
smg_comms_c_wrappers 519 id = atoi(av[2]);
smg_comms_c_wrappers 520
smg_comms_c_wrappers 521 switch ( id ) {
smg_comms_c_wrappers 522 case 0:
smg_comms_c_wrappers 523 printf("Timing entropy source...\n");
smg_comms_c_wrappers 524 time_entropy_source(nruns, 4096);
smg_comms_c_wrappers 525 break;
smg_comms_c_wrappers 526 case 1:
smg_comms_c_wrappers 527 test_entropy_output(nruns, "entropy_source_output.txt");
smg_comms_c_wrappers 528 break;
smg_comms_c_wrappers 529 case 2:
smg_comms_c_wrappers 530
smg_comms_c_wrappers 531
smg_comms_c_wrappers 532 test_is_composite(nruns, "0x10001", 0);
smg_comms_c_wrappers 533 test_is_composite(nruns, "0x1C6DF", 0);
smg_comms_c_wrappers 534 test_is_composite(nruns, "0x188DD82F", 0);
smg_comms_c_wrappers 535 test_is_composite(nruns, "0x4E0516E57", 0);
smg_comms_c_wrappers 536
smg_comms_c_wrappers 537 test_is_composite(nruns, "0x1FFF", 0);
smg_comms_c_wrappers 538 test_is_composite(nruns, "0x1FFFF", 0);
smg_comms_c_wrappers 539 test_is_composite(nruns, "0x7FFFFFFF", 0);
smg_comms_c_wrappers 540
smg_comms_c_wrappers 541 test_is_composite(nruns, "0x231", 1);
smg_comms_c_wrappers 542 test_is_composite(nruns, "0xD8A300793EEF31", 1);
smg_comms_c_wrappers 543
smg_comms_c_wrappers 544 test_is_composite(nruns, "0x15A9E672864B1E", 1);
smg_comms_c_wrappers 545
smg_comms_c_wrappers 546 test_is_composite(nruns, "0x80000000000000000000000000000003", 1);
smg_comms_c_wrappers 547 break;
smg_comms_c_wrappers 548 case 3:
smg_comms_c_wrappers 549 time_mr(nruns);
smg_comms_c_wrappers 550 break;
smg_comms_c_wrappers 551 case 4:
smg_comms_c_wrappers 552 test_rpng(nruns);
smg_comms_c_wrappers 553 break;
smg_comms_c_wrappers 554 case 5:
smg_comms_c_wrappers 555 time_rpng(nruns);
smg_comms_c_wrappers 556 break;
smg_comms_c_wrappers 557 case 6:
smg_comms_c_wrappers 558 fk = fopen("keys.asc", "a");
smg_comms_c_wrappers 559 if ( fk == NULL )
smg_comms_c_wrappers 560 err("Failed to open file keys.asc!");
smg_comms_c_wrappers 561 fout = fopen("check_keys.asc", "a");
smg_comms_c_wrappers 562 if ( fout == NULL ) {
smg_comms_c_wrappers 563 fclose(fk);
smg_comms_c_wrappers 564 err("Failed to open file keys_check.asc!");
smg_comms_c_wrappers 565 }
smg_comms_c_wrappers 566 test_rsa(nruns, fk, fout);
smg_comms_c_wrappers 567 fclose(fk);
smg_comms_c_wrappers 568 fclose(fout);
smg_comms_c_wrappers 569 break;
smg_comms_c_wrappers 570 case 7:
smg_comms_c_wrappers 571 test_rsa_exp();
smg_comms_c_wrappers 572 break;
smg_comms_c_wrappers 573 case 8:
smg_comms_c_wrappers 574 time_rsa_gen(nruns);
smg_comms_c_wrappers 575 break;
smg_comms_c_wrappers 576 case 9:
smg_comms_c_wrappers 577 test_mpi_buffer();
smg_comms_c_wrappers 578 break;
smg_comms_rsa_oaep 579 case 10:
smg_comms_c_wrappers 580 test_dirty_float_rng(nruns);
smg_comms_c_wrappers 581 break;
smg_comms_rsa_oaep 582 case 11:
smg_comms_c_wrappers 583 test_ieee_float_rng(nruns);
smg_comms_c_wrappers 584 break;
smg_comms_rsa_oaep 585 case 12:
smg_comms_c_wrappers 586 test_uint32_rng(nruns);
smg_comms_c_wrappers 587 break;
smg_comms_rsa_oaep 588 case 13:
smg_comms_c_wrappers 589 test_uint64_rng(nruns);
smg_comms_c_wrappers 590 break;
smg_comms_c_wrappers 591 default:
smg_comms_c_wrappers 592 printf("Current test ids:\n");
smg_comms_c_wrappers 593 printf("0 for timing entropy source\n");
smg_comms_c_wrappers 594 printf("1 for entropy output test\n");
smg_comms_c_wrappers 595 printf("2 for is_composite (Miller-Rabin) test\n");
smg_comms_c_wrappers 596 printf("3 for timing Miller-Rabin\n");
smg_comms_c_wrappers 597 printf("4 for random prime number generator test\n");
smg_comms_c_wrappers 598 printf("5 for timing random prime number generator\n");
smg_comms_c_wrappers 599 printf("6 for testing rsa key pair generation and use; \
smg_comms_c_wrappers 600 writes to keys.asc and check_keys.asc\n");
smg_comms_c_wrappers 601 printf("7 for testing rsa exponentiation (fixed data)\n");
smg_comms_c_wrappers 602 printf("8 for timing rsa key pair generator\n");
smg_comms_rsa_oaep 603 printf("9 for testing mpi_set/get_buffer\n");
smg_comms_rsa_oaep 604 printf("10 for testing smg_rng dirty float\n");
smg_comms_rsa_oaep 605 printf("11 for testing smg_rng ieee 745/1985 float\n");
smg_comms_rsa_oaep 606 printf("12 for testing smg_rng uint32 \n");
smg_comms_rsa_oaep 607 printf("13 for testing smg_rng uint64 \n");
smg_comms_c_wrappers 608 }
smg_comms_c_wrappers 609
smg_comms_c_wrappers 610 return 0;
smg_comms_c_wrappers 611 }