ch2_truerandom 1 #include "smg_rsa.h"
eucrypt_ch3_mille... 2 #include "mpi.h"
ch2_truerandom 3
ch2_truerandom 4 #include <stdlib.h>
eucrypt_ch3_mille... 5 #include <unistd.h>
ch2_truerandom 6 #include <time.h>
eucrypt_ch4_rpng 7 #include <stdio.h>
ch2_truerandom 8
eucrypt_ch12_wrap... 9 extern void adainit(void);
eucrypt_ch12_wrap... 10 extern void adafinal(void);
eucrypt_ch12_wrap... 11
ch2_truerandom 12 void err(char *msg)
ch2_truerandom 13 {
ch2_truerandom 14 fprintf(stderr, "%s\n", msg);
ch2_truerandom 15 exit(1);
ch2_truerandom 16 }
ch2_truerandom 17
ch2_truerandom 18 void time_entropy_source(int nruns, int noctets) {
ch2_truerandom 19 unsigned char buffer[noctets];
ch2_truerandom 20 int read, i;
ch2_truerandom 21 struct timespec tstart, tend;
ch2_truerandom 22 long int diff;
ch2_truerandom 23
ch2_truerandom 24 clock_gettime(CLOCK_MONOTONIC, &tstart);
ch2_truerandom 25 for (i=0; i<nruns; i++) {
ch2_truerandom 26 read = get_random_octets(noctets,buffer);
ch2_truerandom 27 if (read != noctets)
ch2_truerandom 28 err("Failed reading from entropy source!");
ch2_truerandom 29 }
ch2_truerandom 30 clock_gettime(CLOCK_MONOTONIC, &tend);
ch2_truerandom 31
ch2_truerandom 32 diff = tend.tv_sec-tstart.tv_sec;
ch2_truerandom 33 double kbps = (nruns*noctets) / (diff*1000.0);
ch2_truerandom 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);
ch2_truerandom 35 }
ch2_truerandom 36
eucrypt_ch4_rpng 37 void test_entropy_output(unsigned int noctets, char * filename) {
eucrypt_ch4_rpng 38 FILE * out;
eucrypt_ch4_rpng 39 int source;
eucrypt_ch4_rpng 40 unsigned int nread, total_read, to_read;
eucrypt_ch4_rpng 41 const int buffer_length = 1000;
eucrypt_ch4_rpng 42 unsigned char buffer[buffer_length];
eucrypt_ch4_rpng 43
eucrypt_ch4_rpng 44 source = open_entropy_source(ENTROPY_SOURCE);
eucrypt_ch4_rpng 45 if (source <= 0)
eucrypt_ch4_rpng 46 err("unable to access entropy source!");
eucrypt_ch4_rpng 47
eucrypt_ch4_rpng 48 out = fopen(filename, "wb");
eucrypt_ch4_rpng 49 if ( !out )
eucrypt_ch4_rpng 50 err("unable to open output file for test_entropy_output!");
eucrypt_ch4_rpng 51
eucrypt_ch4_rpng 52 printf("TEST_ENTROPY_SOURCE: reading %u octets from %s ", noctets, ENTROPY_SOURCE);
eucrypt_ch4_rpng 53 total_read = 0;
eucrypt_ch4_rpng 54 while (total_read < noctets) {
eucrypt_ch4_rpng 55 to_read = noctets - total_read;
eucrypt_ch4_rpng 56 if (to_read > buffer_length)
eucrypt_ch4_rpng 57 to_read = buffer_length;
eucrypt_ch4_rpng 58
eucrypt_ch4_rpng 59 nread = get_random_octets_from(to_read, buffer, source);
eucrypt_ch4_rpng 60 if (nread > 0) {
eucrypt_ch4_rpng 61 total_read = total_read + nread;
eucrypt_ch4_rpng 62 fwrite(buffer, 1, nread, out);
eucrypt_ch4_rpng 63 fflush(out);
eucrypt_ch4_rpng 64 printf(".");
eucrypt_ch4_rpng 65 fflush(stdout);
eucrypt_ch4_rpng 66 }
eucrypt_ch4_rpng 67 }
eucrypt_ch4_rpng 68 printf("done.\n");
eucrypt_ch4_rpng 69
eucrypt_ch4_rpng 70 fclose(out);
eucrypt_ch4_rpng 71 close(source);
eucrypt_ch4_rpng 72 }
eucrypt_ch4_rpng 73
eucrypt_ch3_mille... 74 void test_is_composite(int nruns, char *hex_number, int expected) {
eucrypt_ch3_mille... 75 int i;
eucrypt_ch3_mille... 76 int output;
eucrypt_ch3_mille... 77 int count_ok = 0;
eucrypt_ch3_mille... 78 int source = open_entropy_source(ENTROPY_SOURCE);
eucrypt_ch3_mille... 79 MPI p = mpi_alloc(0);
eucrypt_ch3_mille... 80
eucrypt_ch3_mille... 81 mpi_fromstr(p, hex_number);
eucrypt_ch3_mille... 82 printf("TEST is_composite on MPI(hex) ");
eucrypt_ch3_mille... 83 mpi_print(stdout, p, 1);
eucrypt_ch3_mille... 84 for (i=0; i < nruns; i++) {
eucrypt_ch3_mille... 85 printf(".");
eucrypt_ch3_mille... 86 fflush(stdout);
eucrypt_ch3_mille... 87 output = is_composite(p, M_R_ITERATIONS, source);
eucrypt_ch3_mille... 88 if (output == expected)
eucrypt_ch3_mille... 89 count_ok = count_ok + 1;
eucrypt_ch3_mille... 90 }
eucrypt_ch3_mille... 91 printf("done, with %d out of %d correct runs for expected=%d: %s\n", count_ok, nruns, expected, count_ok==nruns? "PASS":"FAIL");
eucrypt_ch3_mille... 92 mpi_free(p);
eucrypt_ch3_mille... 93 close(source);
eucrypt_ch3_mille... 94 }
ch2_truerandom 95
eucrypt_ch4_rpng 96 void time_mr(int nruns) {
eucrypt_ch4_rpng 97 struct timespec tstart, tend;
eucrypt_ch4_rpng 98 long int diff;
eucrypt_ch4_rpng 99 int i;
eucrypt_ch4_rpng 100 MPI prime;
eucrypt_ch4_rpng 101 unsigned int noctets = KEY_LENGTH_OCTETS / 2;
eucrypt_ch4_rpng 102 unsigned int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
eucrypt_ch4_rpng 103
eucrypt_ch4_rpng 104 int entropy_source = open_entropy_source(ENTROPY_SOURCE);
eucrypt_ch4_rpng 105 if (entropy_source <= 0)
eucrypt_ch4_rpng 106 err("can't open entropy source!");
eucrypt_ch4_rpng 107
eucrypt_ch4_rpng 108
eucrypt_ch4_rpng 109 printf("Generating a prime number of %d octets length for M-R timing test\n", noctets);
eucrypt_ch4_rpng 110 prime = mpi_alloc(nlimbs);
eucrypt_ch4_rpng 111 gen_random_prime(noctets, prime);
eucrypt_ch4_rpng 112
eucrypt_ch4_rpng 113 printf("Running timing test for Miller-Rabin with %d repetitions and %d witnesses on prime number ", nruns, M_R_ITERATIONS);
eucrypt_ch4_rpng 114 mpi_print(stdout, prime, 1);
eucrypt_ch4_rpng 115 printf("\n");
eucrypt_ch4_rpng 116
eucrypt_ch4_rpng 117 clock_gettime(CLOCK_MONOTONIC, &tstart);
eucrypt_ch4_rpng 118 for (i=0; i<nruns; i++) {
eucrypt_ch4_rpng 119 if (is_composite(prime, M_R_ITERATIONS, entropy_source))
eucrypt_ch4_rpng 120 printf("FAIL");
eucrypt_ch4_rpng 121 else printf(".");
eucrypt_ch4_rpng 122 fflush(stdout);
eucrypt_ch4_rpng 123 }
eucrypt_ch4_rpng 124 clock_gettime(CLOCK_MONOTONIC, &tend);
eucrypt_ch4_rpng 125
eucrypt_ch4_rpng 126 diff = tend.tv_sec-tstart.tv_sec;
eucrypt_ch4_rpng 127 printf("\nTimings on prime number %d octets long, %d runs of MR with %d iterations (witnesses checked) each\n", \
eucrypt_ch4_rpng 128 noctets, nruns, M_R_ITERATIONS);
eucrypt_ch4_rpng 129 printf("Total time: %ld seconds\nTime per MR run: %f seconds\nTime per MR iteration: %f seconds\n",\
eucrypt_ch4_rpng 130 diff, diff / (1.0*nruns), diff / (1.0*nruns * M_R_ITERATIONS));
eucrypt_ch4_rpng 131
eucrypt_ch4_rpng 132 mpi_free(prime);
eucrypt_ch4_rpng 133 close(entropy_source);
eucrypt_ch4_rpng 134 }
eucrypt_ch4_rpng 135
eucrypt_ch4_rpng 136 void test_rpng(int nruns) {
eucrypt_ch4_rpng 137 unsigned int noctets = KEY_LENGTH_OCTETS / 2;
eucrypt_ch4_rpng 138 unsigned int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
eucrypt_ch4_rpng 139 int entropy_source = open_entropy_source(ENTROPY_SOURCE);
eucrypt_ch4_rpng 140 if (entropy_source <= 0)
eucrypt_ch4_rpng 141 err("can't open entropy source!");
eucrypt_ch4_rpng 142
eucrypt_ch4_rpng 143 MPI prime = mpi_alloc(nlimbs);
eucrypt_ch4_rpng 144 int i;
eucrypt_ch4_rpng 145
eucrypt_ch4_rpng 146 printf("TEST: random prime number generator with %d runs\n", nruns);
eucrypt_ch4_rpng 147 for (i = 0;i < nruns; i++) {
eucrypt_ch4_rpng 148 gen_random_prime(noctets, prime);
eucrypt_ch4_rpng 149 printf("Run %d: ", i+1);
eucrypt_ch4_rpng 150 mpi_print(stdout, prime, 1);
eucrypt_ch4_rpng 151 if (is_composite(prime, M_R_ITERATIONS, entropy_source))
eucrypt_ch4_rpng 152 printf(" **FAIL**\n");
eucrypt_ch4_rpng 153 else
eucrypt_ch4_rpng 154 printf(" **PASS**\n");
eucrypt_ch4_rpng 155 }
eucrypt_ch4_rpng 156
eucrypt_ch4_rpng 157 mpi_free(prime);
eucrypt_ch4_rpng 158 close(entropy_source);
eucrypt_ch4_rpng 159 }
eucrypt_ch4_rpng 160
eucrypt_ch4_rpng 161 void time_rpng(int nruns) {
eucrypt_ch4_rpng 162 struct timespec tstart, tend;
eucrypt_ch4_rpng 163 long int diff;
eucrypt_ch4_rpng 164
eucrypt_ch4_rpng 165 unsigned int noctets = KEY_LENGTH_OCTETS / 2;
eucrypt_ch4_rpng 166 unsigned int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
eucrypt_ch4_rpng 167
eucrypt_ch4_rpng 168 int entropy_source = open_entropy_source(ENTROPY_SOURCE);
eucrypt_ch4_rpng 169 if (entropy_source <= 0)
eucrypt_ch4_rpng 170 err("can't open entropy source!");
eucrypt_ch4_rpng 171
eucrypt_ch4_rpng 172 MPI prime = mpi_alloc(nlimbs);
eucrypt_ch4_rpng 173 int i;
eucrypt_ch4_rpng 174
eucrypt_ch4_rpng 175 printf("TIMING: random prime number generator with %d runs\n", nruns);
eucrypt_ch4_rpng 176 clock_gettime(CLOCK_MONOTONIC, &tstart);
eucrypt_ch4_rpng 177 for (i = 0;i < nruns; i++) {
eucrypt_ch4_rpng 178 gen_random_prime(noctets, prime);
eucrypt_ch4_rpng 179 }
eucrypt_ch4_rpng 180 clock_gettime(CLOCK_MONOTONIC, &tend);
eucrypt_ch4_rpng 181
eucrypt_ch4_rpng 182 diff = tend.tv_sec-tstart.tv_sec;
eucrypt_ch4_rpng 183
eucrypt_ch4_rpng 184 printf("TOTAL: %ld seconds\n", diff);
eucrypt_ch4_rpng 185 printf("Average: %f seconds to generate one random prime of %d octets length\n", diff / (1.0*nruns), noctets);
eucrypt_ch4_rpng 186 mpi_free(prime);
eucrypt_ch4_rpng 187 close(entropy_source);
eucrypt_ch4_rpng 188 }
eucrypt_ch4_rpng 189
eucrypt_ch5_rsa_keys 190
eucrypt_ch5_rsa_keys 191 * Output is written to file.
eucrypt_ch5_rsa_keys 192 */
eucrypt_ch5_rsa_keys 193 void test_rsa_keys( RSA_secret_key *sk, unsigned int noctets, FILE *file ) {
eucrypt_ch5_rsa_keys 194 RSA_public_key pk;
eucrypt_ch5_rsa_keys 195 MPI test = mpi_alloc ( mpi_nlimb_hint_from_nbytes (noctets) );
eucrypt_ch5_rsa_keys 196 MPI out1 = mpi_alloc ( mpi_nlimb_hint_from_nbytes (noctets) );
eucrypt_ch5_rsa_keys 197 MPI out2 = mpi_alloc ( mpi_nlimb_hint_from_nbytes (noctets) );
eucrypt_ch5_rsa_keys 198
eucrypt_ch5_rsa_keys 199 pk.n = mpi_copy(sk->n);
eucrypt_ch5_rsa_keys 200 pk.e = mpi_copy(sk->e);
eucrypt_ch5_rsa_keys 201 unsigned char *p;
eucrypt_ch5_rsa_keys 202 p = xmalloc(noctets);
eucrypt_ch5_rsa_keys 203
eucrypt_ch5_rsa_keys 204 fprintf(file, "TEST encrypt/decrypt on %d octets of random data\n", noctets);
eucrypt_ch5_rsa_keys 205 fflush(file);
eucrypt_ch5_rsa_keys 206 if (get_random_octets( noctets, p) == noctets) {
eucrypt_ch5_rsa_keys 207 mpi_set_buffer( test, p, noctets, 0 );
eucrypt_ch5_rsa_keys 208
eucrypt_ch5_rsa_keys 209 fprintf(file, "TEST data:\n");
eucrypt_ch5_rsa_keys 210 mpi_print(file, test, 1);
eucrypt_ch5_rsa_keys 211 fprintf(file, "\n");
eucrypt_ch5_rsa_keys 212 fflush(file);
eucrypt_ch5_rsa_keys 213
eucrypt_ch5_rsa_keys 214 public_rsa( out1, test, &pk );
eucrypt_ch5_rsa_keys 215 secret_rsa( out2, out1, sk );
eucrypt_ch5_rsa_keys 216
eucrypt_ch5_rsa_keys 217 fprintf(file, "ENCRYPTED with PUBLIC key data:\n");
eucrypt_ch5_rsa_keys 218 mpi_print(file, out1, 1);
eucrypt_ch5_rsa_keys 219 fprintf(file, "\n");
eucrypt_ch5_rsa_keys 220 fflush(file);
eucrypt_ch5_rsa_keys 221
eucrypt_ch5_rsa_keys 222 fprintf(file, "DECRYPTED with SECRET key:\n");
eucrypt_ch5_rsa_keys 223 mpi_print(file, out2, 1);
eucrypt_ch5_rsa_keys 224 fprintf(file, "\n");
eucrypt_ch5_rsa_keys 225 fflush(file);
eucrypt_ch5_rsa_keys 226
eucrypt_ch5_rsa_keys 227 if( mpi_cmp( test, out2 ) )
eucrypt_ch5_rsa_keys 228 fprintf(file, "FAILED: RSA operation: public(secret) failed\n");
eucrypt_ch5_rsa_keys 229 else
eucrypt_ch5_rsa_keys 230 fprintf(file, "PASSED: RSA operation: public(secret) passed\n");
eucrypt_ch5_rsa_keys 231 fflush(file);
eucrypt_ch5_rsa_keys 232
eucrypt_ch5_rsa_keys 233 secret_rsa( out1, test, sk );
eucrypt_ch5_rsa_keys 234 public_rsa( out2, out1, &pk );
eucrypt_ch5_rsa_keys 235 if( mpi_cmp( test, out2 ) )
eucrypt_ch5_rsa_keys 236 fprintf(file, "FAILED: RSA operation: secret(public) failed\n");
eucrypt_ch5_rsa_keys 237 else
eucrypt_ch5_rsa_keys 238 fprintf(file, "PASSED: RSA operation: secret(public) passed\n");
eucrypt_ch5_rsa_keys 239 }
eucrypt_ch5_rsa_keys 240 else
eucrypt_ch5_rsa_keys 241 fprintf(file, "FAILED: not enough bits returned from entropy source\n");
eucrypt_ch5_rsa_keys 242
eucrypt_ch5_rsa_keys 243 fflush(file);
eucrypt_ch5_rsa_keys 244 xfree(p);
eucrypt_ch5_rsa_keys 245 mpi_free( pk.n);
eucrypt_ch5_rsa_keys 246 mpi_free( pk.e);
eucrypt_ch5_rsa_keys 247
eucrypt_ch5_rsa_keys 248 mpi_free( test );
eucrypt_ch5_rsa_keys 249 mpi_free( out1 );
eucrypt_ch5_rsa_keys 250 mpi_free( out2 );
eucrypt_ch5_rsa_keys 251 }
eucrypt_ch5_rsa_keys 252
eucrypt_ch5_rsa_keys 253 void test_rsa( int nruns, FILE *fkeys, FILE *fout) {
eucrypt_ch5_rsa_keys 254 RSA_secret_key sk;
eucrypt_ch5_rsa_keys 255 int noctets = KEY_LENGTH_OCTETS;
eucrypt_ch5_rsa_keys 256 int noctets_pq = noctets / 2;
eucrypt_ch5_rsa_keys 257 int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
eucrypt_ch5_rsa_keys 258 int nlimbs_pq = mpi_nlimb_hint_from_nbytes(noctets_pq);
eucrypt_ch5_rsa_keys 259 int i;
eucrypt_ch5_rsa_keys 260
eucrypt_ch5_rsa_keys 261 sk.n = mpi_alloc(nlimbs);
eucrypt_ch5_rsa_keys 262 sk.e = mpi_alloc(nlimbs);
eucrypt_ch5_rsa_keys 263 sk.d = mpi_alloc(nlimbs);
eucrypt_ch5_rsa_keys 264 sk.p = mpi_alloc(nlimbs_pq);
eucrypt_ch5_rsa_keys 265 sk.q = mpi_alloc(nlimbs_pq);
eucrypt_ch5_rsa_keys 266 sk.u = mpi_alloc(nlimbs_pq);
eucrypt_ch5_rsa_keys 267
eucrypt_ch5_rsa_keys 268 printf("TEST RSA key generation and use with %d runs\n", nruns);
eucrypt_ch5_rsa_keys 269 fflush(stdout);
eucrypt_ch5_rsa_keys 270
eucrypt_ch5_rsa_keys 271 for (i = 0;i < nruns; i++) {
eucrypt_ch5_rsa_keys 272 gen_keypair(&sk);
eucrypt_ch5_rsa_keys 273 printf(".");
eucrypt_ch5_rsa_keys 274 fflush(stdout);
eucrypt_ch5_rsa_keys 275
eucrypt_ch5_rsa_keys 276 mpi_print(fkeys, sk.n, 1);
eucrypt_ch5_rsa_keys 277 fwrite("\n", sizeof(char), 1, fkeys);
eucrypt_ch5_rsa_keys 278
eucrypt_ch5_rsa_keys 279 mpi_print(fkeys, sk.e, 1);
eucrypt_ch5_rsa_keys 280 fwrite("\n", sizeof(char), 1, fkeys);
eucrypt_ch5_rsa_keys 281
eucrypt_ch5_rsa_keys 282 mpi_print(fkeys, sk.d, 1);
eucrypt_ch5_rsa_keys 283 fwrite("\n", sizeof(char), 1, fkeys);
eucrypt_ch5_rsa_keys 284
eucrypt_ch5_rsa_keys 285 mpi_print(fkeys, sk.p, 1);
eucrypt_ch5_rsa_keys 286 fwrite("\n", sizeof(char), 1, fkeys);
eucrypt_ch5_rsa_keys 287
eucrypt_ch5_rsa_keys 288 mpi_print(fkeys, sk.q, 1);
eucrypt_ch5_rsa_keys 289 fwrite("\n", sizeof(char), 1, fkeys);
eucrypt_ch5_rsa_keys 290
eucrypt_ch5_rsa_keys 291 mpi_print(fkeys, sk.u, 1);
eucrypt_ch5_rsa_keys 292 fwrite("\n", sizeof(char), 1, fkeys);
eucrypt_ch5_rsa_keys 293
eucrypt_ch5_rsa_keys 294 test_rsa_keys(&sk, noctets_pq, fout);
eucrypt_ch5_rsa_keys 295 printf("*");
eucrypt_ch5_rsa_keys 296 fflush(stdout);
eucrypt_ch5_rsa_keys 297 }
eucrypt_ch5_rsa_keys 298
eucrypt_ch5_rsa_keys 299 mpi_free(sk.n);
eucrypt_ch5_rsa_keys 300 mpi_free(sk.e);
eucrypt_ch5_rsa_keys 301 mpi_free(sk.d);
eucrypt_ch5_rsa_keys 302 mpi_free(sk.p);
eucrypt_ch5_rsa_keys 303 mpi_free(sk.q);
eucrypt_ch5_rsa_keys 304 mpi_free(sk.u);
eucrypt_ch5_rsa_keys 305
eucrypt_ch5_rsa_keys 306 }
eucrypt_ch5_rsa_keys 307
eucrypt_ch5_rsa_keys 308 void test_rsa_exp() {
eucrypt_ch5_rsa_keys 309 MPI msg = mpi_alloc(0);
eucrypt_ch5_rsa_keys 310 MPI expected = mpi_alloc(0);
eucrypt_ch5_rsa_keys 311 MPI result;
eucrypt_ch5_rsa_keys 312
eucrypt_ch5_rsa_keys 313 RSA_public_key pk;
eucrypt_ch5_rsa_keys 314 pk.n = mpi_alloc(0);
eucrypt_ch5_rsa_keys 315 pk.e = mpi_alloc(0);
eucrypt_ch5_rsa_keys 316
eucrypt_ch5_rsa_keys 317 printf("TEST verify of rsa exponentiation on input data: \n");
eucrypt_ch5_rsa_keys 318
eucrypt_ch5_rsa_keys 319 mpi_fromstr(msg, "0x\
eucrypt_ch5_rsa_keys 320 5B6A8A0ACF4F4DB3F82EAC2D20255E4DF3E4B7C799603210766F26EF87C8980E737579\
eucrypt_ch5_rsa_keys 321 EC08E6505A51D19654C26D806BAF1B62F9C032E0B13D02AF99F7313BFCFD68DA46836E\
eucrypt_ch5_rsa_keys 322 CA529D7360948550F982C6476C054A97FD01635AB44BFBDBE2A90BE06F7984AC8534C3\
eucrypt_ch5_rsa_keys 323 8613747F340C18176E6D5F0C10246A2FCE3A668EACB6165C2052497CA2EE483F4FD8D0\
eucrypt_ch5_rsa_keys 324 6A9911BD97E9B6720521D872BD08FF8DA11A1B8DB147F252E4E69AE6201D3B374B171D\
eucrypt_ch5_rsa_keys 325 F445EF2BF509D468FD57CEB5840349B14C6E2AAA194D9531D238B85B8F0DD352D1E596\
eucrypt_ch5_rsa_keys 326 71539B429849E5D965E438BF9EFFC338DF9AADF304C4130D5A05E006ED855F37A06242\
eucrypt_ch5_rsa_keys 327 28097EF92F6E78CAE0CB97");
eucrypt_ch5_rsa_keys 328
eucrypt_ch5_rsa_keys 329 mpi_fromstr(expected, "0x\
eucrypt_ch5_rsa_keys 330 1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
eucrypt_ch5_rsa_keys 331 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
eucrypt_ch5_rsa_keys 332 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
eucrypt_ch5_rsa_keys 333 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
eucrypt_ch5_rsa_keys 334 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003051300\
eucrypt_ch5_rsa_keys 335 D0609608648016503040203050004406255509399A3AF322C486C770C5F7F6E05E18FC\
eucrypt_ch5_rsa_keys 336 3E2219A03CA56C7501426A597187468B2F71B4A198C807171B73D0E7DBC3EEF6EA6AFF\
eucrypt_ch5_rsa_keys 337 693DE58E18FF84395BE");
eucrypt_ch5_rsa_keys 338 result = mpi_alloc( mpi_get_nlimbs(expected) );
eucrypt_ch5_rsa_keys 339
eucrypt_ch5_rsa_keys 340 mpi_fromstr(pk.n, "0x\
eucrypt_ch5_rsa_keys 341 CDD49A674BAF76D3B73E25BC6DF66EF3ABEDDCA461D3CCB6416793E3437C7806562694\
eucrypt_ch5_rsa_keys 342 73C2212D5FD5EED17AA067FEC001D8E76EC901EDEDF960304F891BD3CAD7F9A335D1A2\
eucrypt_ch5_rsa_keys 343 EC37EABEFF3FBE6D3C726DC68E599EBFE5456EF19813398CD7D548D746A30AA47D4293\
eucrypt_ch5_rsa_keys 344 968BFBAFCBF65A90DFFC87816FEE2A01E1DC699F4DDABB84965514C0D909D54FDA7062\
eucrypt_ch5_rsa_keys 345 A2037B50B771C153D5429BA4BA335EAB840F9551E9CD9DF8BB4A6DC3ED1318FF3969F7\
eucrypt_ch5_rsa_keys 346 B99D9FB90CAB968813F8AD4F9A069C9639A74D70A659C69C29692567CE863B88E191CC\
eucrypt_ch5_rsa_keys 347 9535B91B417D0AF14BE09C78B53AF9C5F494BCF2C60349FFA93C81E817AC682F0055A6\
eucrypt_ch5_rsa_keys 348 07BB56D6A281C1A04CEFE1");
eucrypt_ch5_rsa_keys 349
eucrypt_ch5_rsa_keys 350 mpi_fromstr( pk.e, "0x10001");
eucrypt_ch5_rsa_keys 351
eucrypt_ch5_rsa_keys 352 mpi_print( stdout, msg, 1);
eucrypt_ch5_rsa_keys 353 printf("\n");
eucrypt_ch5_rsa_keys 354
eucrypt_ch5_rsa_keys 355 public_rsa( result, msg, &pk);
eucrypt_ch5_rsa_keys 356 if ( mpi_cmp( result, expected) != 0 )
eucrypt_ch5_rsa_keys 357 printf( "FAILED\n");
eucrypt_ch5_rsa_keys 358 else
eucrypt_ch5_rsa_keys 359 printf( "PASSED\n");
eucrypt_ch5_rsa_keys 360
eucrypt_ch5_rsa_keys 361 printf("Expected:\n");
eucrypt_ch5_rsa_keys 362 mpi_print( stdout, expected, 1);
eucrypt_ch5_rsa_keys 363 printf("\n");
eucrypt_ch5_rsa_keys 364
eucrypt_ch5_rsa_keys 365 printf("Obtained:\n");
eucrypt_ch5_rsa_keys 366 mpi_print( stdout, result, 1);
eucrypt_ch5_rsa_keys 367 printf("\n");
eucrypt_ch5_rsa_keys 368
eucrypt_ch5_rsa_keys 369 mpi_free( pk.n );
eucrypt_ch5_rsa_keys 370 mpi_free( pk.e );
eucrypt_ch5_rsa_keys 371 mpi_free( msg );
eucrypt_ch5_rsa_keys 372 mpi_free( expected );
eucrypt_ch5_rsa_keys 373 mpi_free( result );
eucrypt_ch5_rsa_keys 374 }
eucrypt_ch5_rsa_keys 375
eucrypt_ch5_rsa_keys 376 void time_rsa_gen( int nruns ) {
eucrypt_ch5_rsa_keys 377 struct timespec tstart, tend;
eucrypt_ch5_rsa_keys 378 long int diff;
eucrypt_ch5_rsa_keys 379 int i;
eucrypt_ch5_rsa_keys 380
eucrypt_ch5_rsa_keys 381 RSA_secret_key sk;
eucrypt_ch5_rsa_keys 382 int noctets = KEY_LENGTH_OCTETS;
eucrypt_ch5_rsa_keys 383 int noctets_pq = noctets / 2;
eucrypt_ch5_rsa_keys 384 int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
eucrypt_ch5_rsa_keys 385 int nlimbs_pq = mpi_nlimb_hint_from_nbytes(noctets_pq);
eucrypt_ch5_rsa_keys 386 sk.n = mpi_alloc(nlimbs);
eucrypt_ch5_rsa_keys 387 sk.e = mpi_alloc(nlimbs);
eucrypt_ch5_rsa_keys 388 sk.d = mpi_alloc(nlimbs);
eucrypt_ch5_rsa_keys 389 sk.p = mpi_alloc(nlimbs_pq);
eucrypt_ch5_rsa_keys 390 sk.q = mpi_alloc(nlimbs_pq);
eucrypt_ch5_rsa_keys 391 sk.u = mpi_alloc(nlimbs_pq);
eucrypt_ch5_rsa_keys 392
eucrypt_ch5_rsa_keys 393 clock_gettime(CLOCK_MONOTONIC, &tstart);
eucrypt_ch5_rsa_keys 394 for (i = 0;i < nruns; i++) {
eucrypt_ch5_rsa_keys 395 gen_keypair(&sk);
eucrypt_ch5_rsa_keys 396 }
eucrypt_ch5_rsa_keys 397 clock_gettime(CLOCK_MONOTONIC, &tend);
eucrypt_ch5_rsa_keys 398
eucrypt_ch5_rsa_keys 399 diff = tend.tv_sec-tstart.tv_sec;
eucrypt_ch5_rsa_keys 400
eucrypt_ch5_rsa_keys 401 printf("TOTAL: %ld seconds for generating %d key pairs\n", diff, nruns);
eucrypt_ch5_rsa_keys 402 printf("Average (%d runs): %f seconds per TMSR RSA key pair.\n",
eucrypt_ch5_rsa_keys 403 nruns, diff / (1.0*nruns));
eucrypt_ch5_rsa_keys 404 mpi_free(sk.n);
eucrypt_ch5_rsa_keys 405 mpi_free(sk.e);
eucrypt_ch5_rsa_keys 406 mpi_free(sk.d);
eucrypt_ch5_rsa_keys 407 mpi_free(sk.p);
eucrypt_ch5_rsa_keys 408 mpi_free(sk.q);
eucrypt_ch5_rsa_keys 409 mpi_free(sk.u);
eucrypt_ch5_rsa_keys 410 }
eucrypt_ch5_rsa_keys 411
eucrypt_ch12_wrap... 412 void test_oaep_encr_decr( int nruns ) {
eucrypt_ch12_wrap... 413
eucrypt_ch12_wrap... 414 RSA_public_key pk;
eucrypt_ch12_wrap... 415 pk.n = mpi_alloc(0);
eucrypt_ch12_wrap... 416 pk.e = mpi_alloc(0);
eucrypt_ch12_wrap... 417
eucrypt_ch12_wrap... 418 RSA_secret_key sk;
eucrypt_ch12_wrap... 419 sk.n = mpi_alloc(0);
eucrypt_ch12_wrap... 420 sk.e = mpi_alloc(0);
eucrypt_ch12_wrap... 421 sk.d = mpi_alloc(0);
eucrypt_ch12_wrap... 422 sk.p = mpi_alloc(0);
eucrypt_ch12_wrap... 423 sk.q = mpi_alloc(0);
eucrypt_ch12_wrap... 424 sk.u = mpi_alloc(0);
eucrypt_ch12_wrap... 425
eucrypt_ch12_wrap... 426 mpi_fromstr(sk.n, "0x\
eucrypt_ch12_wrap... 427 CD2C025323BEA46FFF2FA8D7A9D39817EA713421F4AE03FA8120641193892A70BFECF5\
eucrypt_ch12_wrap... 428 83101635A432110D3DDE6339E3CC7ECC0AD91C026FCACE832DD3888A6FCA7BCE56C390\
eucrypt_ch12_wrap... 429 5A5AC8C7BC921DA675E4B62489B254EB34659D547D71165BC998983A81937BD251AEE1\
eucrypt_ch12_wrap... 430 2D985EC387D5376F5DCC5EF7EC530FBD6FD2AA7285EE1AF3335EA73163F0954F30402E\
eucrypt_ch12_wrap... 431 D7B374EE84A97B1849B0674B0DA0A2050BD79B71ABB1559F3A9CFDB8557DED7BC90CF2\
eucrypt_ch12_wrap... 432 09E8A847E9C226140845B7D03842162E7DA5DD16326CB1F71A248D841FE9076A09911F\
eucrypt_ch12_wrap... 433 2F4F5E3EA44EA8DE40332BF00406990BCCF61C322A03C456EF3A98B341E0BDBC1088CE\
eucrypt_ch12_wrap... 434 683E78510E76B72C2BCC1EE9AEDD80FFF18ABFC5923B2F36B581C25114AB2DF9F6C2B1\
eucrypt_ch12_wrap... 435 9481703FD19E313DCD7ACE15FA11B27D25BCE5388C180A7E21167FB87750599E1ED7C7\
eucrypt_ch12_wrap... 436 50F4A844E1DC2270C62D19671CF8F4C25B81E366B09FC850AE642136D204A9160AEECE\
eucrypt_ch12_wrap... 437 575B57378AA439E9DD46DC990288CD54BAA35EEE1C02456CD39458A6F1CBF012DCEDF4\
eucrypt_ch12_wrap... 438 27CCF3F3F53645658FC49C9C9D7F2856DB571D92B967AB5845514E0054DDB49099F5DD\
eucrypt_ch12_wrap... 439 04A6F6F5C5CE642276834B932881AEB648D1F25E9223971F56E249EF40CF7D80F22621\
eucrypt_ch12_wrap... 440 CDD0260E9E7D23746960ADB52CF2987584FB1DE95A69A39E5CB12B76E0F5C1A0529C0C\
eucrypt_ch12_wrap... 441 065D2E35720810F7C7983180B9A9EA0E00C11B79DC3D");
eucrypt_ch12_wrap... 442
eucrypt_ch12_wrap... 443 mpi_fromstr(sk.e, "0x\
eucrypt_ch12_wrap... 444 DD4856B4EE3D099A8604AE392D8EFEC094CDF01546A28BE87CB484F999E8E75CDFCD01\
eucrypt_ch12_wrap... 445 D04D455A6A9254C60BD28C0B03611FC3E751CC27EF768C0B401C4FD2B27C092834A6F2\
eucrypt_ch12_wrap... 446 49A145C4EDC47A3B3D363EC352462C945334D160AF9AA72202862912493AC6190AA3A6\
eucrypt_ch12_wrap... 447 149D4D8B9996BA7927D3D0D2AD00D30FD630CF464E6CAF9CF49355B9A70E05DB7AE915\
eucrypt_ch12_wrap... 448 F9F602772F8D11E5FCDFC7709210F248052615967090CC1F43D410C83724AA5912B2F0\
eucrypt_ch12_wrap... 449 52E6B39449A89A97C79C92DC8CB8DEEFCF248C1E1D2FC5BFE85165ECA31839CAA9CEB3\
eucrypt_ch12_wrap... 450 3A92EBDC0EB3BAC0F810938BB173C7DA21DCBB2220D44CBA0FD40A2C868FC93AC5243E\
eucrypt_ch12_wrap... 451 C137C27B0A76D65634EBB3");
eucrypt_ch12_wrap... 452
eucrypt_ch12_wrap... 453 mpi_fromstr(sk.d, "0x\
eucrypt_ch12_wrap... 454 7C8A6FA1199D99DCA45E9BDF567CA49D02B237340D7E999150BC4883AE29DEC5158521\
eucrypt_ch12_wrap... 455 B338F35DC883792356BDDBB3C8B3030A6DD4C6522599A3254E751F9BA1CB1061C5633C\
eucrypt_ch12_wrap... 456 81BBFACF6FCD64502614102DFED3F3FA284066C342D5E00953B415915331E30812E5FB\
eucrypt_ch12_wrap... 457 CD6680ADCCDEE40B8376A3A225F2E160EA59C7566804526D73BB660A648A3EF9802313\
eucrypt_ch12_wrap... 458 B2F841E8458B2AAACE7AACF31083E8F3F630298138393BC88BBD7D4AA4334949651D25\
eucrypt_ch12_wrap... 459 365B10DBF4A4A08E20A6CC74BFDD37C1C38E2ADC2A283DF06590DF06B46F67F6ACA67F\
eucrypt_ch12_wrap... 460 AC464C795261659A2F9558802D0BBAA05FD1E1AF2CDC70654723DF7EFAEA148B8CDBEB\
eucrypt_ch12_wrap... 461 C89EA2320AB9BBB1BC4311475DF3D91446F02EF192368DFEBAC598CCFD4407DEC58FDC\
eucrypt_ch12_wrap... 462 1A94CCDD6E5FBA9C52164ACEA8AEE633E557BCCEACB7A1AF656C379482D784A120A725\
eucrypt_ch12_wrap... 463 32F9B2B35173D505F21D5AD4CB9511BC836DC923730B70291B70290A216CA3B21CFF79\
eucrypt_ch12_wrap... 464 E895C35F4F7AF80E1BD9ED2773BD26919A76E4298D169160593E0335BE2A2A2D2E8516\
eucrypt_ch12_wrap... 465 948F657E1B1260E18808A9D463C108535FB60B3B28F711C81E5DE24F40214134A53CE5\
eucrypt_ch12_wrap... 466 9A952C8970A1D771EBEFFA2F4359DCF157995B3F1950DE3C6EC41B7FF837148F55F323\
eucrypt_ch12_wrap... 467 372AF3F20CE8B8038E750C23D8F5041FA951327859B0E47483F0A47103EF808C72C251\
eucrypt_ch12_wrap... 468 006FA526245291C8C84C12D2EF63FB2301EA3EEDA42B");
eucrypt_ch12_wrap... 469
eucrypt_ch12_wrap... 470 mpi_fromstr(sk.p, "0x\
eucrypt_ch12_wrap... 471 E236732452039C14EC1D3B8095BDDCFB7625CE27B1EA5394CF4ED09D3CEECAA4FC0BF6\
eucrypt_ch12_wrap... 472 2F7CE975E0C8929CE84B0259D773EA038396479BF15DA065BA70E549B248D77B4B23ED\
eucrypt_ch12_wrap... 473 A267308510DBEE2FD44E35D880EE7CFB81E0646AA8630165BD8988C3A8776D9E704C20\
eucrypt_ch12_wrap... 474 AA25CA0A3C32F27F592D5FD363B04DD57D8C61FFDCDFCCC59E2913DE0EE47769180340\
eucrypt_ch12_wrap... 475 E1EA5A803AA2301A010FF553A380F002601F0853FCACDB82D76FE2FACBCD6E5F294439\
eucrypt_ch12_wrap... 476 0799EA5AE9D7880D4E1D4AE146DC1D4E8495B9DD30E57E883923C5FC26682B7142D35C\
eucrypt_ch12_wrap... 477 D8A0FC561FE725A6CF419B15341F40FE0C31132CBD81DD8E50697BD1EBFFA16B522E16\
eucrypt_ch12_wrap... 478 F5B49A03B707218C7DA60B");
eucrypt_ch12_wrap... 479
eucrypt_ch12_wrap... 480 mpi_fromstr(sk.q, "0x\
eucrypt_ch12_wrap... 481 E830482A3C4F5C3A7E59C10FF8BA760DB1C6D55880B796FFDA4A82E0B60E974E81D04B\
eucrypt_ch12_wrap... 482 2A4AD417823EBFB4E8EFB13782943562B19B6C4A680E3BA0C8E37B5023470F4F1AC1F8\
eucrypt_ch12_wrap... 483 A0B10672EF75CD58BCD45E6B14503B8A6A70AFE79F6201AF56E7364A1C742BE1453FD2\
eucrypt_ch12_wrap... 484 24FDC9D66522EAF4466A084BCB9E46D455A2946E94CBF028770F38D0B741C2CC59308F\
eucrypt_ch12_wrap... 485 71D8C2B4B9C928E0AE8D68DEB48A3E9EFD84A10301EBD55F8221CA32FC567B306B2A8E\
eucrypt_ch12_wrap... 486 116350AFB995859FDF4378C5CFD06901494E8CFA5D8FAC564D6531FA8A2E4761F5EFBA\
eucrypt_ch12_wrap... 487 F78750B6F4662BE9EA4C2FAD67AF73EEB36B41FC15CB678810C19A51DF23555695C4C1\
eucrypt_ch12_wrap... 488 546F3FACA39CAA7BB8DBD7");
eucrypt_ch12_wrap... 489
eucrypt_ch12_wrap... 490 mpi_fromstr(sk.u, "0x\
eucrypt_ch12_wrap... 491 846232322775C1CD7D5569DC59E2F3E61A885AE2E9C4A4F8CB3ACBE8C3A5441E5FE348\
eucrypt_ch12_wrap... 492 A2A8AC9C2998FBF282222BF508AA1ECF66A76AEDD2D9C97028BFD3F6CA0542E38A5312\
eucrypt_ch12_wrap... 493 603C70B95650CE73F80FDD729988FBDB5595A5BF8A007EA34E54994A697906CE56354C\
eucrypt_ch12_wrap... 494 E00DF10EB711DEC274A62494E3D350D88736CF67A477FB600AC9F1D6580727585092BF\
eucrypt_ch12_wrap... 495 5EBC092CC4D6CF75769051033A1197103BE269942F372168A53771746FBA18ED6972D5\
eucrypt_ch12_wrap... 496 0B935A9B1D6B5B3DD50CD89A27FE93C10924E9103FACF7B4C5724A046C3D3B50CC1C78\
eucrypt_ch12_wrap... 497 5F5C8E00DBE1D6561F120F5294C170914BC10F978ED4356EED67A9F3A60D70AFE540FC\
eucrypt_ch12_wrap... 498 5373CBAE3D0A7FD1C87273");
eucrypt_ch12_wrap... 499
eucrypt_ch12_wrap... 500
eucrypt_ch12_wrap... 501 pk.n = mpi_copy( sk.n );
eucrypt_ch12_wrap... 502 pk.e = mpi_copy( sk.e );
eucrypt_ch12_wrap... 503
eucrypt_ch12_wrap... 504
eucrypt_ch12_wrap... 505 MPI msg = mpi_alloc(0);
eucrypt_ch12_wrap... 506 mpi_fromstr(msg, "0x\
eucrypt_ch12_wrap... 507 5B6A8A0ACF4F4DB3F82EAC2D20255E4DF3E4B7C799603210766F26EF87C8980E737579\
eucrypt_ch12_wrap... 508 EC08E6505A51D19654C26D806BAF1B62F9C032E0B13D02AF99F7313BFCFD68DA46836E\
eucrypt_ch12_wrap... 509 CA529D7360948550F982C6476C054A97FD01635AB44BFBDBE2A90BE06F7984AC8534C3\
eucrypt_ch12_wrap... 510 28097EF92F6E78CAE0CB97");
eucrypt_ch12_wrap... 511
eucrypt_ch12_wrap... 512
eucrypt_ch12_wrap... 513 printf("TEST verify oaep_encr_decr on message: \n");
eucrypt_ch12_wrap... 514 mpi_print( stdout, msg, 1);
eucrypt_ch12_wrap... 515 printf("\n");
eucrypt_ch12_wrap... 516
eucrypt_ch12_wrap... 517 int nlimbs_n = mpi_nlimb_hint_from_nbytes( KEY_LENGTH_OCTETS);
eucrypt_ch12_wrap... 518 MPI encr = mpi_alloc( nlimbs_n );
eucrypt_ch12_wrap... 519 MPI decr = mpi_alloc( nlimbs_n );
eucrypt_ch12_wrap... 520 int success;
eucrypt_ch12_wrap... 521
eucrypt_ch12_wrap... 522 adainit();
eucrypt_ch12_wrap... 523 rsa_oaep_encrypt( encr, msg, &pk );
eucrypt_ch12_wrap... 524 rsa_oaep_decrypt( decr, encr, &sk, &success );
eucrypt_ch12_wrap... 525
eucrypt_ch12_wrap... 526 if (success <= 0 ||
eucrypt_ch12_wrap... 527 mpi_cmp(encr, msg) == 0 ||
eucrypt_ch12_wrap... 528 mpi_cmp(msg, decr) != 0)
eucrypt_ch12_wrap... 529 printf("FAILED: success flag is %d\n", success);
eucrypt_ch12_wrap... 530 else
eucrypt_ch12_wrap... 531 printf("PASSED\n");
eucrypt_ch12_wrap... 532
eucrypt_ch12_wrap... 533
eucrypt_ch12_wrap... 534 mpi_clear( decr );
eucrypt_ch12_wrap... 535 rsa_oaep_decrypt( decr, pk.n, &sk, &success);
eucrypt_ch12_wrap... 536 if (success > 0)
eucrypt_ch12_wrap... 537 printf("FAILED: attempt to decrypt non-/corrupted oaep block\n");
eucrypt_ch12_wrap... 538 else
eucrypt_ch12_wrap... 539 printf("PASSED: attempt to decrypt non-/corrupted oaep block\n");
eucrypt_ch12_wrap... 540 adafinal();
eucrypt_ch12_wrap... 541
eucrypt_ch12_wrap... 542
eucrypt_ch12_wrap... 543 mpi_free( sk.n );
eucrypt_ch12_wrap... 544 mpi_free( sk.e );
eucrypt_ch12_wrap... 545 mpi_free( sk.d );
eucrypt_ch12_wrap... 546 mpi_free( sk.p );
eucrypt_ch12_wrap... 547 mpi_free( sk.q );
eucrypt_ch12_wrap... 548 mpi_free( sk.u );
eucrypt_ch12_wrap... 549
eucrypt_ch12_wrap... 550 mpi_free( pk.n );
eucrypt_ch12_wrap... 551 mpi_free( pk.e );
eucrypt_ch12_wrap... 552
eucrypt_ch12_wrap... 553 mpi_free( msg );
eucrypt_ch12_wrap... 554 mpi_free( encr );
eucrypt_ch12_wrap... 555 mpi_free( decr );
eucrypt_ch12_wrap... 556 }
eucrypt_ch12_wrap... 557
eucrypt_ch12_wrap... 558 void test_mpi_buffer() {
eucrypt_ch12_wrap... 559 unsigned int noctets = 10;
eucrypt_ch12_wrap... 560 int nlimbs = mpi_nlimb_hint_from_nbytes( noctets );
eucrypt_ch12_wrap... 561 MPI m = mpi_alloc( nlimbs );
eucrypt_ch12_wrap... 562 unsigned char *setbuffer = xmalloc( noctets );
eucrypt_ch12_wrap... 563 unsigned char *getbuffer;
eucrypt_ch12_wrap... 564 unsigned int i, sign, mpilen, nerrors;
eucrypt_ch12_wrap... 565
eucrypt_ch12_wrap... 566 for (i=0; i< noctets; i++)
eucrypt_ch12_wrap... 567 setbuffer[i] = i;
eucrypt_ch12_wrap... 568
eucrypt_ch12_wrap... 569 mpi_set_buffer( m, setbuffer, noctets, 0);
eucrypt_ch12_wrap... 570
eucrypt_ch12_wrap... 571 getbuffer = mpi_get_buffer( m, &mpilen, &sign );
eucrypt_ch12_wrap... 572
eucrypt_ch12_wrap... 573 if (mpilen == noctets -1 ) {
eucrypt_ch12_wrap... 574 nerrors = 0;
eucrypt_ch12_wrap... 575 for (i=0;i<noctets-1;i++)
eucrypt_ch12_wrap... 576 if (setbuffer[i+1] != getbuffer[i])
eucrypt_ch12_wrap... 577 nerrors = nerrors + 1;
eucrypt_ch12_wrap... 578 if (nerrors == 0) {
eucrypt_ch12_wrap... 579 printf("WARNING: 0-led octet discarded by mpi_set_buffer!\n");
eucrypt_ch12_wrap... 580 printf("Value ret by mpi_get_buffer != value given to set_buffer!\n");
eucrypt_ch12_wrap... 581 }
eucrypt_ch12_wrap... 582 else
eucrypt_ch12_wrap... 583 printf("FAIL: got different lengths and %d different values!\n", nerrors);
eucrypt_ch12_wrap... 584 }
eucrypt_ch12_wrap... 585 else if (mpilen != noctets)
eucrypt_ch12_wrap... 586 printf("FAIL: mpilen is %d; noctets is %d\n", mpilen, noctets);
eucrypt_ch12_wrap... 587 else
eucrypt_ch12_wrap... 588 {
eucrypt_ch12_wrap... 589 nerrors = 0;
eucrypt_ch12_wrap... 590 for (i=0;i<noctets-1;i++) {
eucrypt_ch12_wrap... 591 if (setbuffer[i]!=getbuffer[i])
eucrypt_ch12_wrap... 592 nerrors= nerrors+1;
eucrypt_ch12_wrap... 593 }
eucrypt_ch12_wrap... 594 if (nerrors>0)
eucrypt_ch12_wrap... 595 printf("FAIL: got %d different values!\n", nerrors);
eucrypt_ch12_wrap... 596 else printf("PASSED: mpi_get/set_buffer\n");
eucrypt_ch12_wrap... 597 }
eucrypt_ch12_wrap... 598
eucrypt_ch12_wrap... 599 mpi_free(m);
eucrypt_ch12_wrap... 600 xfree(setbuffer);
eucrypt_ch12_wrap... 601 xfree(getbuffer);
eucrypt_ch12_wrap... 602 }
eucrypt_ch12_wrap... 603
eucrypt_ch13_smg_rng 604 void test_dirty_float_rng( int nruns ) {
eucrypt_ch13_smg_rng 605 int i, status;
eucrypt_ch13_smg_rng 606 float dirty;
eucrypt_ch13_smg_rng 607
eucrypt_ch13_smg_rng 608 printf("Running test for smg rng dirty float with %d runs\n", nruns);
eucrypt_ch13_smg_rng 609 for (i=0; i<nruns; i++) {
eucrypt_ch13_smg_rng 610 status = rng_dirty_float( &dirty );
eucrypt_ch13_smg_rng 611 printf("Run %d: %f status %s\n", i+1, dirty, status>0 ? "OK" : "FAIL");
eucrypt_ch13_smg_rng 612 }
eucrypt_ch13_smg_rng 613 }
eucrypt_ch13_smg_rng 614
eucrypt_ch13_smg_rng 615 void test_ieee_float_rng( int nruns ) {
eucrypt_ch13_smg_rng 616 int i, status;
eucrypt_ch13_smg_rng 617 float ieee;
eucrypt_ch13_smg_rng 618
eucrypt_ch13_smg_rng 619 printf("Running test for smg rng ieee 745/1985 float with %d runs\n", nruns);
eucrypt_ch13_smg_rng 620 for (i=0; i<nruns; i++) {
eucrypt_ch13_smg_rng 621 status = rng_float_754_1985( &ieee );
eucrypt_ch13_smg_rng 622 printf("Run %d: %f status %s\n", i+1, ieee, status>0 ? "OK" : "FAIL");
eucrypt_ch13_smg_rng 623 }
eucrypt_ch13_smg_rng 624 }
eucrypt_ch13_smg_rng 625
eucrypt_ch13_smg_rng 626 void test_uint32_rng( int nruns ) {
eucrypt_ch13_smg_rng 627 int i, status;
eucrypt_ch13_smg_rng 628 uint32_t n;
eucrypt_ch13_smg_rng 629
eucrypt_ch13_smg_rng 630 printf("Running test for smg rng unsigned int32 with %d runs\n", nruns);
eucrypt_ch13_smg_rng 631 for (i=0; i<nruns; i++) {
eucrypt_ch13_smg_rng 632 status = rng_uint32( &n );
eucrypt_ch13_smg_rng 633 printf("Run %d: %"PRIu32" status %s\n", i+1, n, status>0 ? "OK" : "FAIL");
eucrypt_ch13_smg_rng 634 }
eucrypt_ch13_smg_rng 635 }
eucrypt_ch13_smg_rng 636
eucrypt_ch13_smg_rng 637 void test_uint64_rng( int nruns ) {
eucrypt_ch13_smg_rng 638 int i, status;
eucrypt_ch13_smg_rng 639 uint64_t n;
eucrypt_ch13_smg_rng 640
eucrypt_ch13_smg_rng 641 printf("Running test for smg rng unsigned int64 with %d runs\n", nruns);
eucrypt_ch13_smg_rng 642 for (i=0; i<nruns; i++) {
eucrypt_ch13_smg_rng 643 status = rng_uint64( &n );
eucrypt_ch13_smg_rng 644 printf("Run %d: %"PRIu64" status %s\n", i+1, n, status>0 ? "OK" : "FAIL");
eucrypt_ch13_smg_rng 645 }
eucrypt_ch13_smg_rng 646 }
eucrypt_ch13_smg_rng 647
eucrypt_ch13_smg_rng 648
ch2_truerandom 649 int main(int ac, char **av)
ch2_truerandom 650 {
ch2_truerandom 651 int nruns;
eucrypt_ch3_mille... 652 int id;
eucrypt_ch5_rsa_keys 653 FILE *fk;
eucrypt_ch5_rsa_keys 654 FILE *fout;
ch2_truerandom 655
ch2_truerandom 656 if (ac<2) {
eucrypt_ch4_rpng 657 printf("Usage: %s number_of_runs/octets [testID]\n", av[0]);
ch2_truerandom 658 return -1;
ch2_truerandom 659 }
ch2_truerandom 660 nruns = atoi(av[1]);
ch2_truerandom 661
eucrypt_ch3_mille... 662 if (ac < 3)
eucrypt_ch4_rpng 663 id = -1;
eucrypt_ch3_mille... 664 else
eucrypt_ch3_mille... 665 id = atoi(av[2]);
eucrypt_ch3_mille... 666
eucrypt_ch4_rpng 667 switch ( id ) {
eucrypt_ch4_rpng 668 case 0:
eucrypt_ch4_rpng 669 printf("Timing entropy source...\n");
eucrypt_ch4_rpng 670 time_entropy_source(nruns, 4096);
eucrypt_ch4_rpng 671 break;
eucrypt_ch4_rpng 672 case 1:
eucrypt_ch4_rpng 673 test_entropy_output(nruns, "entropy_source_output.txt");
eucrypt_ch4_rpng 674 break;
eucrypt_ch4_rpng 675 case 2:
eucrypt_ch4_rpng 676
eucrypt_ch4_rpng 677
eucrypt_ch4_rpng 678 test_is_composite(nruns, "0x10001", 0);
eucrypt_ch4_rpng 679 test_is_composite(nruns, "0x1C6DF", 0);
eucrypt_ch4_rpng 680 test_is_composite(nruns, "0x188DD82F", 0);
eucrypt_ch4_rpng 681 test_is_composite(nruns, "0x4E0516E57", 0);
eucrypt_ch4_rpng 682
eucrypt_ch4_rpng 683 test_is_composite(nruns, "0x1FFF", 0);
eucrypt_ch4_rpng 684 test_is_composite(nruns, "0x1FFFF", 0);
eucrypt_ch4_rpng 685 test_is_composite(nruns, "0x7FFFFFFF", 0);
eucrypt_ch4_rpng 686
eucrypt_ch4_rpng 687 test_is_composite(nruns, "0x231", 1);
eucrypt_ch4_rpng 688 test_is_composite(nruns, "0xD8A300793EEF31", 1);
eucrypt_ch4_rpng 689
eucrypt_ch4_rpng 690 test_is_composite(nruns, "0x15A9E672864B1E", 1);
eucrypt_ch4_rpng 691
eucrypt_ch4_rpng 692 test_is_composite(nruns, "0x80000000000000000000000000000003", 1);
eucrypt_ch4_rpng 693 break;
eucrypt_ch4_rpng 694 case 3:
eucrypt_ch4_rpng 695 time_mr(nruns);
eucrypt_ch4_rpng 696 break;
eucrypt_ch4_rpng 697 case 4:
eucrypt_ch4_rpng 698 test_rpng(nruns);
eucrypt_ch4_rpng 699 break;
eucrypt_ch4_rpng 700 case 5:
eucrypt_ch4_rpng 701 time_rpng(nruns);
eucrypt_ch4_rpng 702 break;
eucrypt_ch5_rsa_keys 703 case 6:
eucrypt_ch5_rsa_keys 704 fk = fopen("keys.asc", "a");
eucrypt_ch5_rsa_keys 705 if ( fk == NULL )
eucrypt_ch5_rsa_keys 706 err("Failed to open file keys.asc!");
eucrypt_ch5_rsa_keys 707 fout = fopen("check_keys.asc", "a");
eucrypt_ch5_rsa_keys 708 if ( fout == NULL ) {
eucrypt_ch5_rsa_keys 709 fclose(fk);
eucrypt_ch5_rsa_keys 710 err("Failed to open file keys_check.asc!");
eucrypt_ch5_rsa_keys 711 }
eucrypt_ch5_rsa_keys 712 test_rsa(nruns, fk, fout);
eucrypt_ch5_rsa_keys 713 fclose(fk);
eucrypt_ch5_rsa_keys 714 fclose(fout);
eucrypt_ch5_rsa_keys 715 break;
eucrypt_ch5_rsa_keys 716 case 7:
eucrypt_ch5_rsa_keys 717 test_rsa_exp();
eucrypt_ch5_rsa_keys 718 break;
eucrypt_ch5_rsa_keys 719 case 8:
eucrypt_ch5_rsa_keys 720 time_rsa_gen(nruns);
eucrypt_ch5_rsa_keys 721 break;
eucrypt_ch12_wrap... 722 case 9:
eucrypt_ch12_wrap... 723 test_oaep_encr_decr(nruns);
eucrypt_ch12_wrap... 724 break;
eucrypt_ch12_wrap... 725 case 10:
eucrypt_ch12_wrap... 726 test_mpi_buffer();
eucrypt_ch12_wrap... 727 break;
eucrypt_ch13_smg_rng 728 case 11:
eucrypt_ch13_smg_rng 729 test_dirty_float_rng(nruns);
eucrypt_ch13_smg_rng 730 break;
eucrypt_ch13_smg_rng 731 case 12:
eucrypt_ch13_smg_rng 732 test_ieee_float_rng(nruns);
eucrypt_ch13_smg_rng 733 break;
eucrypt_ch13_smg_rng 734 case 13:
eucrypt_ch13_smg_rng 735 test_uint32_rng(nruns);
eucrypt_ch13_smg_rng 736 break;
eucrypt_ch13_smg_rng 737 case 14:
eucrypt_ch13_smg_rng 738 test_uint64_rng(nruns);
eucrypt_ch13_smg_rng 739 break;
eucrypt_ch4_rpng 740 default:
eucrypt_ch4_rpng 741 printf("Current test ids:\n");
eucrypt_ch4_rpng 742 printf("0 for timing entropy source\n");
eucrypt_ch4_rpng 743 printf("1 for entropy output test\n");
eucrypt_ch4_rpng 744 printf("2 for is_composite (Miller-Rabin) test\n");
eucrypt_ch4_rpng 745 printf("3 for timing Miller-Rabin\n");
eucrypt_ch4_rpng 746 printf("4 for random prime number generator test\n");
eucrypt_ch4_rpng 747 printf("5 for timing random prime number generator\n");
eucrypt_ch5_rsa_keys 748 printf("6 for testing rsa key pair generation and use; \
eucrypt_ch5_rsa_keys 749 writes to keys.asc and check_keys.asc\n");
eucrypt_ch5_rsa_keys 750 printf("7 for testing rsa exponentiation (fixed data)\n");
eucrypt_ch5_rsa_keys 751 printf("8 for timing rsa key pair generator\n");
eucrypt_ch12_wrap... 752 printf("9 for oaep encrypt/decrypt\n");
eucrypt_ch12_wrap... 753 printf("10 for testing mpi_set/get_buffer\n");
eucrypt_ch13_smg_rng 754 printf("11 for testing smg_rng dirty float\n");
eucrypt_ch13_smg_rng 755 printf("12 for testing smg_rng ieee 745/1985 float\n");
eucrypt_ch13_smg_rng 756 printf("13 for testing smg_rng uint32 \n");
eucrypt_ch13_smg_rng 757 printf("14 for testing smg_rng uint64 \n");
eucrypt_ch3_mille... 758 }
eucrypt_ch3_mille... 759
ch2_truerandom 760 return 0;
ch2_truerandom 761 }