raw
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 /* first generate a prime of half key length, to make sure M-R will run max number of iterations */
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 /* now do the actual runs and time it all */
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 /* Test encryption+decryption on noctets of random data, using sk
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_shorter_e 308 void test_rsa_8e(int nruns) {
smg_comms_shorter_e 309 RSA_secret_key sk;
smg_comms_shorter_e 310 int noctets = KEY_LENGTH_OCTETS;
smg_comms_shorter_e 311 int noctets_pq = noctets / 2;
smg_comms_shorter_e 312 int nlimbs_pq = mpi_nlimb_hint_from_nbytes(noctets_pq);
smg_comms_shorter_e 313
smg_comms_shorter_e 314 sk.n = mpi_alloc(0);
smg_comms_shorter_e 315 sk.e = mpi_alloc(0);
smg_comms_shorter_e 316 sk.d = mpi_alloc(0);
smg_comms_shorter_e 317 sk.p = mpi_alloc(0);
smg_comms_shorter_e 318 sk.q = mpi_alloc(0);
smg_comms_shorter_e 319 sk.u = mpi_alloc(0);
smg_comms_shorter_e 320
smg_comms_shorter_e 321 mpi_fromstr(sk.n, "0x\
smg_comms_shorter_e 322 B51BE851F39159EAC714F3E0376713A84DAD36A82D446D0A257A391870F45FAE13C4CC\
smg_comms_shorter_e 323 F400DDA9F604991134C0934161554EEFEAA3147BF0EADC77B99E2B9B6E4EE942EA9D07\
smg_comms_shorter_e 324 5F015EE2465B491F4130E04E1BBB6CCDC98F6E8789D4F7FCA3E3FF83C6100CAF2B764E\
smg_comms_shorter_e 325 A5AF7CBA9B27C13EE72EA7A8602F34B32E17C2BA56CFBA4223F7D9A03C23336095D34F\
smg_comms_shorter_e 326 BF66E88BF5CE661D66C251DFAD4CB2BA8D1E1669AC927894EA20DABABD2495BC2A4BA3\
smg_comms_shorter_e 327 A25C79ABEC2D57F45F0F889D962C777A663D0AB25D3650DFDC6D77C528803C0C6E12BD\
smg_comms_shorter_e 328 05281B33C603BEA66A0C2ACBEBD1CA53D32C2269294C9B93E742CA563AF39E939C32CE\
smg_comms_shorter_e 329 51D5ED827F9C217EF58CC518B635D0E03BA778BCEBAF9A2CDB493282D751A5977CB907\
smg_comms_shorter_e 330 C8708D1EF1CAE644C1F2525DDE98E29761B1ADF0965F08AA856DF540AEFD67F96B92AE\
smg_comms_shorter_e 331 83636C31A507C59635C6D435C5E7EE333DC2257C07BC0FCE27CF400F6EB7A6B90FFF00\
smg_comms_shorter_e 332 C3C1179615BF5DA6137476926C09D8CCD03257DFCAEF12BE9DC1D3F621D6C97D7F3E6D\
smg_comms_shorter_e 333 534337579B4B65AE212ACC26FC3861E24033E6F12A601D473A65EFC5F25ABD5D6049EA\
smg_comms_shorter_e 334 DD6D76BA60AA218C5EBE13439AAFFF0088C49ACC0E9F7DE56DB03F585E1AC2862EB990\
smg_comms_shorter_e 335 59724FD407C4ACD3DD14A53A6A35F6AFAE03EA53A4E742CC370087692E206A2422FF9D");
smg_comms_shorter_e 336
smg_comms_shorter_e 337 sk.e = mpi_alloc(nlimbs_pq);
smg_comms_shorter_e 338 int i;
smg_comms_shorter_e 339 char echar[109];
smg_comms_shorter_e 340 int ne = 109;
smg_comms_shorter_e 341 for (i=0;i<ne;i++)
smg_comms_shorter_e 342 echar[i] = 0;
smg_comms_shorter_e 343
smg_comms_shorter_e 344 echar[ne-1] = 59;
smg_comms_shorter_e 345 echar[ne-2] = 153;
smg_comms_shorter_e 346 echar[ne-3] = 145;
smg_comms_shorter_e 347 echar[ne-4] = 109;
smg_comms_shorter_e 348 echar[ne-5] = 236;
smg_comms_shorter_e 349 echar[ne-6] = 157;
smg_comms_shorter_e 350 echar[ne-7] = 64;
smg_comms_shorter_e 351 echar[ne-8] = 195;
smg_comms_shorter_e 352 //NB: this test WILL FAIL if only mpi_set_buffer called.
smg_comms_shorter_e 353 mpi_set_buffer(sk.e, echar, ne, 0);
smg_comms_shorter_e 354 mpi_normalize(sk.e);
smg_comms_shorter_e 355
smg_comms_shorter_e 356 mpi_fromstr(sk.d, "0x\
smg_comms_shorter_e 357 7694F5266A995D31DCF3AE40A35AAE4B6F6E2D625EDE8AEF3DF4418C1C5B6D46FBC8AC\
smg_comms_shorter_e 358 1C018B475BB531807C6CB649A3D2A3044B11560B15DFD17E29E2294CBBE4D2E787B048\
smg_comms_shorter_e 359 D7EF19DB02392534DBBE8C0F57DCA17448B5DCDA08A69C3177749BEA150BBC506372DD\
smg_comms_shorter_e 360 D6DBE48869567B31B00FB855AB4700A0458570393F2AA6A5887DAB0E6FD194B970AED2\
smg_comms_shorter_e 361 A6AA1AE13C1B4BA1154F7D2C55699429CD634425460B961C581639E6CC005B6FDEF273\
smg_comms_shorter_e 362 E5A8A34F39E5F7999AEA6E6767A9842203C00763BEDECFB2FCBC4D071AADB81D3DBB32\
smg_comms_shorter_e 363 58872EBA06804183AE66DE1859758BB1752486DC95887DA71BA231E115FFA43AB5F596\
smg_comms_shorter_e 364 21F5DA9BDF780BB35E3DAA06C2BE83FFE160BE6C625D8D33A42491510E22AD545AE0FE\
smg_comms_shorter_e 365 933F2A70FCBF794156EE6FDB4351404706EC528BD865A3F75B66ACE3186620B5713F44\
smg_comms_shorter_e 366 D2D185BC2F8B42B2C9BD8274D11D4E4449D9237398A4D576A0722871B96C2675E32EAD\
smg_comms_shorter_e 367 C8E2DB958FB016D9DBDE9653FC7A346689D348F1161C3E183C19DDD92A313E2191435E\
smg_comms_shorter_e 368 449E819FC3B04CA8B7874B3283AE52FEE822525BAAA14F7E28DF2B1EA9383FBEFA7A4B\
smg_comms_shorter_e 369 AB22BC178CDCF5064E09D8EAAAC4EEF50550E8CB5D3F3079D80FECC4A7B2587BFA6B68\
smg_comms_shorter_e 370 0798D6A6175BA3ABAC52B2BE583849FC3C47A6B7B9BB6CE18AF23BFC54E8F04AA6AF3");
smg_comms_shorter_e 371 mpi_fromstr(sk.p, "0x\
smg_comms_shorter_e 372 C9AE5920BA2CFC9A765C25D8B75E8A42845F5B13F8B9386698C31D01BDDE572BD181E4\
smg_comms_shorter_e 373 5FD46666BB9D3F5043D2B3F7B4B3D301DA403CED22ADBB60544D2000EBB3FEF3E1674D\
smg_comms_shorter_e 374 B2F2BB81A81573B3556689FD2AF740367EB9C7CFFC08BD95471849C456B9AD93BA9462\
smg_comms_shorter_e 375 A541FBD7618E9BF8F8B8DC76233D22826085E06F8CC22C4DB46DE8A6E666FBF678052E\
smg_comms_shorter_e 376 80B5D116B4CAC4FCA1C3348F45895D823842E6A4E2B605F0F8CCD9A75F59B3B4BBC102\
smg_comms_shorter_e 377 B9B7F948EC28AB60BF4FD5441E4EF48B25D6A50C5AAEB882EE9862E042F54510366730\
smg_comms_shorter_e 378 B8DD56C02FC43CD95F6EF7B92379E6DED28764CDD22899C845F484EF8F777B9A527FC1");
smg_comms_shorter_e 379 mpi_fromstr(sk.q, "0x\
smg_comms_shorter_e 380 E5E324CDCAEA29DE1EFB3E2280B51E4461A692A5844F64E6917677907A7825A8ABBEF1\
smg_comms_shorter_e 381 1E0D8A5D70DF703D4D4014FE0A125E5A409AD7DC9B5F2ECBDF518C8ECE783F217268CF\
smg_comms_shorter_e 382 ED93689AEB5D970B3C898B7ACB8868E2D284A85F1B82926C67F6F6675F78799CC280E4\
smg_comms_shorter_e 383 197AB3800B00ED9B063B9A00F483CDD5158DCE31B575F579E4206D32E483994B58D5EA\
smg_comms_shorter_e 384 7FBE224326672ED00BA1B432796E78B80816D641BE4F81BF8CCA5A2E93A0E9FA4D6133\
smg_comms_shorter_e 385 4E893903E133DA215295FAF00F9BA1F224BC700ABCD6A94FB4C1A006BD5673D304B207\
smg_comms_shorter_e 386 F8EE6752F41C4D2F7DE310266EAF61B0545A26FAC66A46B90015FC2374D4FD01F836DD");
smg_comms_shorter_e 387 mpi_fromstr(sk.u, "0x\
smg_comms_shorter_e 388 746B5BD406C84FDBC8EF4DEC7D79831630B45579D7865695BDB288C0E8DB39749200B0\
smg_comms_shorter_e 389 720F6FAF1CBB3F2EE6C6FF3DA72B4C8505D50732DBEE2E92CD9A13B6919D5173DBA708\
smg_comms_shorter_e 390 B44C36129A3494DCD77B9991F88F528FF11706E0D6EFC67705937F5A11FCBC984F89F2\
smg_comms_shorter_e 391 A4168A8ED2A391FAD06C1E15F838B890126DF650DA803661448F8CEA755F15432A83C8\
smg_comms_shorter_e 392 99CB0AFCB5D922514DF19697416DDC79CA2AC7CB30AEADEC2D68B0F43D178DAB33656E\
smg_comms_shorter_e 393 3CDF08A3D2B903797E74210478526D49AA740F906789740826DDD3CF2E5F032B9E0536\
smg_comms_shorter_e 394 85560848F127A1A844BF00C5819FFEB2016E0DBC86BF375554DC28EC2DC7FD8830AA3D");
smg_comms_shorter_e 395
smg_comms_shorter_e 396 test_rsa_keys(&sk, noctets_pq, stdout);
smg_comms_shorter_e 397
smg_comms_shorter_e 398 mpi_free(sk.n);
smg_comms_shorter_e 399 mpi_free(sk.e);
smg_comms_shorter_e 400 mpi_free(sk.d);
smg_comms_shorter_e 401 mpi_free(sk.p);
smg_comms_shorter_e 402 mpi_free(sk.q);
smg_comms_shorter_e 403 mpi_free(sk.u);
smg_comms_shorter_e 404 }
smg_comms_shorter_e 405
smg_comms_c_wrappers 406 void test_rsa_exp() {
smg_comms_c_wrappers 407 MPI msg = mpi_alloc(0);
smg_comms_c_wrappers 408 MPI expected = mpi_alloc(0);
smg_comms_c_wrappers 409 MPI result;
smg_comms_c_wrappers 410
smg_comms_c_wrappers 411 RSA_public_key pk;
smg_comms_c_wrappers 412 pk.n = mpi_alloc(0);
smg_comms_c_wrappers 413 pk.e = mpi_alloc(0);
smg_comms_c_wrappers 414
smg_comms_c_wrappers 415 printf("TEST verify of rsa exponentiation on input data: \n");
smg_comms_c_wrappers 416
smg_comms_c_wrappers 417 mpi_fromstr(msg, "0x\
smg_comms_c_wrappers 418 5B6A8A0ACF4F4DB3F82EAC2D20255E4DF3E4B7C799603210766F26EF87C8980E737579\
smg_comms_c_wrappers 419 EC08E6505A51D19654C26D806BAF1B62F9C032E0B13D02AF99F7313BFCFD68DA46836E\
smg_comms_c_wrappers 420 CA529D7360948550F982C6476C054A97FD01635AB44BFBDBE2A90BE06F7984AC8534C3\
smg_comms_c_wrappers 421 8613747F340C18176E6D5F0C10246A2FCE3A668EACB6165C2052497CA2EE483F4FD8D0\
smg_comms_c_wrappers 422 6A9911BD97E9B6720521D872BD08FF8DA11A1B8DB147F252E4E69AE6201D3B374B171D\
smg_comms_c_wrappers 423 F445EF2BF509D468FD57CEB5840349B14C6E2AAA194D9531D238B85B8F0DD352D1E596\
smg_comms_c_wrappers 424 71539B429849E5D965E438BF9EFFC338DF9AADF304C4130D5A05E006ED855F37A06242\
smg_comms_c_wrappers 425 28097EF92F6E78CAE0CB97");
smg_comms_c_wrappers 426
smg_comms_c_wrappers 427 mpi_fromstr(expected, "0x\
smg_comms_c_wrappers 428 1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
smg_comms_c_wrappers 429 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
smg_comms_c_wrappers 430 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
smg_comms_c_wrappers 431 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
smg_comms_c_wrappers 432 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003051300\
smg_comms_c_wrappers 433 D0609608648016503040203050004406255509399A3AF322C486C770C5F7F6E05E18FC\
smg_comms_c_wrappers 434 3E2219A03CA56C7501426A597187468B2F71B4A198C807171B73D0E7DBC3EEF6EA6AFF\
smg_comms_c_wrappers 435 693DE58E18FF84395BE");
smg_comms_c_wrappers 436 result = mpi_alloc( mpi_get_nlimbs(expected) );
smg_comms_c_wrappers 437
smg_comms_c_wrappers 438 mpi_fromstr(pk.n, "0x\
smg_comms_c_wrappers 439 CDD49A674BAF76D3B73E25BC6DF66EF3ABEDDCA461D3CCB6416793E3437C7806562694\
smg_comms_c_wrappers 440 73C2212D5FD5EED17AA067FEC001D8E76EC901EDEDF960304F891BD3CAD7F9A335D1A2\
smg_comms_c_wrappers 441 EC37EABEFF3FBE6D3C726DC68E599EBFE5456EF19813398CD7D548D746A30AA47D4293\
smg_comms_c_wrappers 442 968BFBAFCBF65A90DFFC87816FEE2A01E1DC699F4DDABB84965514C0D909D54FDA7062\
smg_comms_c_wrappers 443 A2037B50B771C153D5429BA4BA335EAB840F9551E9CD9DF8BB4A6DC3ED1318FF3969F7\
smg_comms_c_wrappers 444 B99D9FB90CAB968813F8AD4F9A069C9639A74D70A659C69C29692567CE863B88E191CC\
smg_comms_c_wrappers 445 9535B91B417D0AF14BE09C78B53AF9C5F494BCF2C60349FFA93C81E817AC682F0055A6\
smg_comms_c_wrappers 446 07BB56D6A281C1A04CEFE1");
smg_comms_c_wrappers 447
smg_comms_c_wrappers 448 mpi_fromstr( pk.e, "0x10001");
smg_comms_c_wrappers 449
smg_comms_c_wrappers 450 mpi_print( stdout, msg, 1);
smg_comms_c_wrappers 451 printf("\n");
smg_comms_c_wrappers 452
smg_comms_c_wrappers 453 public_rsa( result, msg, &pk);
smg_comms_c_wrappers 454 if ( mpi_cmp( result, expected) != 0 )
smg_comms_c_wrappers 455 printf( "FAILED\n");
smg_comms_c_wrappers 456 else
smg_comms_c_wrappers 457 printf( "PASSED\n");
smg_comms_c_wrappers 458
smg_comms_c_wrappers 459 printf("Expected:\n");
smg_comms_c_wrappers 460 mpi_print( stdout, expected, 1);
smg_comms_c_wrappers 461 printf("\n");
smg_comms_c_wrappers 462
smg_comms_c_wrappers 463 printf("Obtained:\n");
smg_comms_c_wrappers 464 mpi_print( stdout, result, 1);
smg_comms_c_wrappers 465 printf("\n");
smg_comms_c_wrappers 466
smg_comms_c_wrappers 467 mpi_free( pk.n );
smg_comms_c_wrappers 468 mpi_free( pk.e );
smg_comms_c_wrappers 469 mpi_free( msg );
smg_comms_c_wrappers 470 mpi_free( expected );
smg_comms_c_wrappers 471 mpi_free( result );
smg_comms_c_wrappers 472 }
smg_comms_c_wrappers 473
smg_comms_c_wrappers 474 void time_rsa_gen( int nruns ) {
smg_comms_c_wrappers 475 struct timespec tstart, tend;
smg_comms_c_wrappers 476 long int diff;
smg_comms_c_wrappers 477 int i;
smg_comms_c_wrappers 478
smg_comms_c_wrappers 479 RSA_secret_key sk;
smg_comms_c_wrappers 480 int noctets = KEY_LENGTH_OCTETS;
smg_comms_c_wrappers 481 int noctets_pq = noctets / 2;
smg_comms_c_wrappers 482 int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
smg_comms_c_wrappers 483 int nlimbs_pq = mpi_nlimb_hint_from_nbytes(noctets_pq);
smg_comms_c_wrappers 484 sk.n = mpi_alloc(nlimbs);
smg_comms_c_wrappers 485 sk.e = mpi_alloc(nlimbs);
smg_comms_c_wrappers 486 sk.d = mpi_alloc(nlimbs);
smg_comms_c_wrappers 487 sk.p = mpi_alloc(nlimbs_pq);
smg_comms_c_wrappers 488 sk.q = mpi_alloc(nlimbs_pq);
smg_comms_c_wrappers 489 sk.u = mpi_alloc(nlimbs_pq);
smg_comms_c_wrappers 490
smg_comms_c_wrappers 491 clock_gettime(CLOCK_MONOTONIC, &tstart);
smg_comms_c_wrappers 492 for (i = 0;i < nruns; i++) {
smg_comms_c_wrappers 493 gen_keypair(&sk);
smg_comms_c_wrappers 494 }
smg_comms_c_wrappers 495 clock_gettime(CLOCK_MONOTONIC, &tend);
smg_comms_c_wrappers 496
smg_comms_c_wrappers 497 diff = tend.tv_sec-tstart.tv_sec;
smg_comms_c_wrappers 498
smg_comms_c_wrappers 499 printf("TOTAL: %ld seconds for generating %d key pairs\n", diff, nruns);
smg_comms_c_wrappers 500 printf("Average (%d runs): %f seconds per TMSR RSA key pair.\n",
smg_comms_c_wrappers 501 nruns, diff / (1.0*nruns));
smg_comms_c_wrappers 502 mpi_free(sk.n);
smg_comms_c_wrappers 503 mpi_free(sk.e);
smg_comms_c_wrappers 504 mpi_free(sk.d);
smg_comms_c_wrappers 505 mpi_free(sk.p);
smg_comms_c_wrappers 506 mpi_free(sk.q);
smg_comms_c_wrappers 507 mpi_free(sk.u);
smg_comms_c_wrappers 508 }
smg_comms_c_wrappers 509
smg_comms_c_wrappers 510 void test_mpi_buffer() {
smg_comms_c_wrappers 511 unsigned int noctets = 10;
smg_comms_c_wrappers 512 int nlimbs = mpi_nlimb_hint_from_nbytes( noctets );
smg_comms_c_wrappers 513 MPI m = mpi_alloc( nlimbs );
smg_comms_c_wrappers 514 unsigned char *setbuffer = xmalloc( noctets );
smg_comms_c_wrappers 515 unsigned char *getbuffer;
smg_comms_c_wrappers 516 unsigned int i, sign, mpilen, nerrors;
smg_comms_c_wrappers 517
smg_comms_c_wrappers 518 for (i=0; i< noctets; i++)
smg_comms_c_wrappers 519 setbuffer[i] = i;
smg_comms_c_wrappers 520
smg_comms_c_wrappers 521 mpi_set_buffer( m, setbuffer, noctets, 0);
smg_comms_c_wrappers 522
smg_comms_c_wrappers 523 getbuffer = mpi_get_buffer( m, &mpilen, &sign );
smg_comms_c_wrappers 524
smg_comms_c_wrappers 525 if (mpilen == noctets -1 ) {
smg_comms_c_wrappers 526 nerrors = 0;
smg_comms_c_wrappers 527 for (i=0;i<noctets-1;i++)
smg_comms_c_wrappers 528 if (setbuffer[i+1] != getbuffer[i])
smg_comms_c_wrappers 529 nerrors = nerrors + 1;
smg_comms_c_wrappers 530 if (nerrors == 0) {
smg_comms_c_wrappers 531 printf("WARNING: 0-led octet discarded by mpi_set_buffer!\n");
smg_comms_c_wrappers 532 printf("Value ret by mpi_get_buffer != value given to set_buffer!\n");
smg_comms_c_wrappers 533 }
smg_comms_c_wrappers 534 else
smg_comms_c_wrappers 535 printf("FAIL: got different lengths and %d different values!\n", nerrors);
smg_comms_c_wrappers 536 }
smg_comms_c_wrappers 537 else if (mpilen != noctets)
smg_comms_c_wrappers 538 printf("FAIL: mpilen is %d; noctets is %d\n", mpilen, noctets);
smg_comms_c_wrappers 539 else
smg_comms_c_wrappers 540 {
smg_comms_c_wrappers 541 nerrors = 0;
smg_comms_c_wrappers 542 for (i=0;i<noctets-1;i++) {
smg_comms_c_wrappers 543 if (setbuffer[i]!=getbuffer[i])
smg_comms_c_wrappers 544 nerrors= nerrors+1;
smg_comms_c_wrappers 545 }
smg_comms_c_wrappers 546 if (nerrors>0)
smg_comms_c_wrappers 547 printf("FAIL: got %d different values!\n", nerrors);
smg_comms_c_wrappers 548 else printf("PASSED: mpi_get/set_buffer\n");
smg_comms_c_wrappers 549 }
smg_comms_c_wrappers 550
smg_comms_c_wrappers 551 mpi_free(m);
smg_comms_c_wrappers 552 xfree(setbuffer);
smg_comms_c_wrappers 553 xfree(getbuffer);
smg_comms_c_wrappers 554 }
smg_comms_c_wrappers 555
smg_comms_c_wrappers 556 void test_dirty_float_rng( int nruns ) {
smg_comms_c_wrappers 557 int i, status;
smg_comms_c_wrappers 558 float dirty;
smg_comms_c_wrappers 559
smg_comms_c_wrappers 560 printf("Running test for smg rng dirty float with %d runs\n", nruns);
smg_comms_c_wrappers 561 for (i=0; i<nruns; i++) {
smg_comms_c_wrappers 562 status = rng_dirty_float( &dirty );
smg_comms_c_wrappers 563 printf("Run %d: %f status %s\n", i+1, dirty, status>0 ? "OK" : "FAIL");
smg_comms_c_wrappers 564 }
smg_comms_c_wrappers 565 }
smg_comms_c_wrappers 566
smg_comms_c_wrappers 567 void test_ieee_float_rng( int nruns ) {
smg_comms_c_wrappers 568 int i, status;
smg_comms_c_wrappers 569 float ieee;
smg_comms_c_wrappers 570
smg_comms_c_wrappers 571 printf("Running test for smg rng ieee 745/1985 float with %d runs\n", nruns);
smg_comms_c_wrappers 572 for (i=0; i<nruns; i++) {
smg_comms_c_wrappers 573 status = rng_float_754_1985( &ieee );
smg_comms_c_wrappers 574 printf("Run %d: %f status %s\n", i+1, ieee, status>0 ? "OK" : "FAIL");
smg_comms_c_wrappers 575 }
smg_comms_c_wrappers 576 }
smg_comms_c_wrappers 577
smg_comms_c_wrappers 578 void test_uint32_rng( int nruns ) {
smg_comms_c_wrappers 579 int i, status;
smg_comms_c_wrappers 580 uint32_t n;
smg_comms_c_wrappers 581
smg_comms_c_wrappers 582 printf("Running test for smg rng unsigned int32 with %d runs\n", nruns);
smg_comms_c_wrappers 583 for (i=0; i<nruns; i++) {
smg_comms_c_wrappers 584 status = rng_uint32( &n );
smg_comms_c_wrappers 585 printf("Run %d: %"PRIu32" status %s\n", i+1, n, status>0 ? "OK" : "FAIL");
smg_comms_c_wrappers 586 }
smg_comms_c_wrappers 587 }
smg_comms_c_wrappers 588
smg_comms_c_wrappers 589 void test_uint64_rng( int nruns ) {
smg_comms_c_wrappers 590 int i, status;
smg_comms_c_wrappers 591 uint64_t n;
smg_comms_c_wrappers 592
smg_comms_c_wrappers 593 printf("Running test for smg rng unsigned int64 with %d runs\n", nruns);
smg_comms_c_wrappers 594 for (i=0; i<nruns; i++) {
smg_comms_c_wrappers 595 status = rng_uint64( &n );
smg_comms_c_wrappers 596 printf("Run %d: %"PRIu64" status %s\n", i+1, n, status>0 ? "OK" : "FAIL");
smg_comms_c_wrappers 597 }
smg_comms_c_wrappers 598 }
smg_comms_c_wrappers 599
smg_comms_c_wrappers 600
smg_comms_c_wrappers 601 int main(int ac, char **av)
smg_comms_c_wrappers 602 {
smg_comms_c_wrappers 603 int nruns;
smg_comms_c_wrappers 604 int id;
smg_comms_c_wrappers 605 FILE *fk;
smg_comms_c_wrappers 606 FILE *fout;
smg_comms_c_wrappers 607
smg_comms_c_wrappers 608 if (ac<2) {
smg_comms_c_wrappers 609 printf("Usage: %s number_of_runs/octets [testID]\n", av[0]);
smg_comms_c_wrappers 610 return -1;
smg_comms_c_wrappers 611 }
smg_comms_c_wrappers 612 nruns = atoi(av[1]);
smg_comms_c_wrappers 613
smg_comms_c_wrappers 614 if (ac < 3)
smg_comms_c_wrappers 615 id = -1;
smg_comms_c_wrappers 616 else
smg_comms_c_wrappers 617 id = atoi(av[2]);
smg_comms_c_wrappers 618
smg_comms_c_wrappers 619 switch ( id ) {
smg_comms_c_wrappers 620 case 0:
smg_comms_c_wrappers 621 printf("Timing entropy source...\n");
smg_comms_c_wrappers 622 time_entropy_source(nruns, 4096);
smg_comms_c_wrappers 623 break;
smg_comms_c_wrappers 624 case 1:
smg_comms_c_wrappers 625 test_entropy_output(nruns, "entropy_source_output.txt");
smg_comms_c_wrappers 626 break;
smg_comms_c_wrappers 627 case 2:
smg_comms_c_wrappers 628 /* tests on miller-rabin */
smg_comms_c_wrappers 629 /* a few primes (decimal): 65537, 116447, 411949103, 20943302231 */
smg_comms_c_wrappers 630 test_is_composite(nruns, "0x10001", 0);
smg_comms_c_wrappers 631 test_is_composite(nruns, "0x1C6DF", 0);
smg_comms_c_wrappers 632 test_is_composite(nruns, "0x188DD82F", 0);
smg_comms_c_wrappers 633 test_is_composite(nruns, "0x4E0516E57", 0);
smg_comms_c_wrappers 634 /* a few mersenne primes (decimal): 2^13 - 1 = 8191, 2^17 - 1 = 131071, 2^31 - 1 = 2147483647 */
smg_comms_c_wrappers 635 test_is_composite(nruns, "0x1FFF", 0);
smg_comms_c_wrappers 636 test_is_composite(nruns, "0x1FFFF", 0);
smg_comms_c_wrappers 637 test_is_composite(nruns, "0x7FFFFFFF", 0);
smg_comms_c_wrappers 638 /* a few carmichael numbers, in decimal: 561, 60977817398996785 */
smg_comms_c_wrappers 639 test_is_composite(nruns, "0x231", 1);
smg_comms_c_wrappers 640 test_is_composite(nruns, "0xD8A300793EEF31", 1);
smg_comms_c_wrappers 641 /* an even number */
smg_comms_c_wrappers 642 test_is_composite(nruns, "0x15A9E672864B1E", 1);
smg_comms_c_wrappers 643 /* a phuctor-found non-prime public exponent: 170141183460469231731687303715884105731 */
smg_comms_c_wrappers 644 test_is_composite(nruns, "0x80000000000000000000000000000003", 1);
smg_comms_c_wrappers 645 break;
smg_comms_c_wrappers 646 case 3:
smg_comms_c_wrappers 647 time_mr(nruns);
smg_comms_c_wrappers 648 break;
smg_comms_c_wrappers 649 case 4:
smg_comms_c_wrappers 650 test_rpng(nruns);
smg_comms_c_wrappers 651 break;
smg_comms_c_wrappers 652 case 5:
smg_comms_c_wrappers 653 time_rpng(nruns);
smg_comms_c_wrappers 654 break;
smg_comms_c_wrappers 655 case 6:
smg_comms_c_wrappers 656 fk = fopen("keys.asc", "a");
smg_comms_c_wrappers 657 if ( fk == NULL )
smg_comms_c_wrappers 658 err("Failed to open file keys.asc!");
smg_comms_c_wrappers 659 fout = fopen("check_keys.asc", "a");
smg_comms_c_wrappers 660 if ( fout == NULL ) {
smg_comms_c_wrappers 661 fclose(fk);
smg_comms_c_wrappers 662 err("Failed to open file keys_check.asc!");
smg_comms_c_wrappers 663 }
smg_comms_c_wrappers 664 test_rsa(nruns, fk, fout);
smg_comms_c_wrappers 665 fclose(fk);
smg_comms_c_wrappers 666 fclose(fout);
smg_comms_c_wrappers 667 break;
smg_comms_c_wrappers 668 case 7:
smg_comms_c_wrappers 669 test_rsa_exp();
smg_comms_c_wrappers 670 break;
smg_comms_c_wrappers 671 case 8:
smg_comms_c_wrappers 672 time_rsa_gen(nruns);
smg_comms_c_wrappers 673 break;
smg_comms_c_wrappers 674 case 9:
smg_comms_c_wrappers 675 test_mpi_buffer();
smg_comms_c_wrappers 676 break;
smg_comms_rsa_oaep 677 case 10:
smg_comms_c_wrappers 678 test_dirty_float_rng(nruns);
smg_comms_c_wrappers 679 break;
smg_comms_rsa_oaep 680 case 11:
smg_comms_c_wrappers 681 test_ieee_float_rng(nruns);
smg_comms_c_wrappers 682 break;
smg_comms_rsa_oaep 683 case 12:
smg_comms_c_wrappers 684 test_uint32_rng(nruns);
smg_comms_c_wrappers 685 break;
smg_comms_rsa_oaep 686 case 13:
smg_comms_c_wrappers 687 test_uint64_rng(nruns);
smg_comms_c_wrappers 688 break;
smg_comms_shorter_e 689 case 14:
smg_comms_shorter_e 690 test_rsa_8e(nruns);
smg_comms_shorter_e 691 break;
smg_comms_c_wrappers 692 default:
smg_comms_c_wrappers 693 printf("Current test ids:\n");
smg_comms_c_wrappers 694 printf("0 for timing entropy source\n");
smg_comms_c_wrappers 695 printf("1 for entropy output test\n");
smg_comms_c_wrappers 696 printf("2 for is_composite (Miller-Rabin) test\n");
smg_comms_c_wrappers 697 printf("3 for timing Miller-Rabin\n");
smg_comms_c_wrappers 698 printf("4 for random prime number generator test\n");
smg_comms_c_wrappers 699 printf("5 for timing random prime number generator\n");
smg_comms_c_wrappers 700 printf("6 for testing rsa key pair generation and use; \
smg_comms_c_wrappers 701 writes to keys.asc and check_keys.asc\n");
smg_comms_c_wrappers 702 printf("7 for testing rsa exponentiation (fixed data)\n");
smg_comms_c_wrappers 703 printf("8 for timing rsa key pair generator\n");
smg_comms_rsa_oaep 704 printf("9 for testing mpi_set/get_buffer\n");
smg_comms_rsa_oaep 705 printf("10 for testing smg_rng dirty float\n");
smg_comms_rsa_oaep 706 printf("11 for testing smg_rng ieee 745/1985 float\n");
smg_comms_rsa_oaep 707 printf("12 for testing smg_rng uint32 \n");
smg_comms_rsa_oaep 708 printf("13 for testing smg_rng uint64 \n");
smg_comms_shorter_e 709 printf("14 for testing rsa with 8-octets e \n");
smg_comms_c_wrappers 710 }
smg_comms_c_wrappers 711
smg_comms_c_wrappers 712 return 0;
smg_comms_c_wrappers 713 }