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_ch15_arbi... 308 void test_rsa_8e(int nruns) {
eucrypt_ch15_arbi... 309 RSA_secret_key sk;
eucrypt_ch15_arbi... 310 int noctets = KEY_LENGTH_OCTETS;
eucrypt_ch15_arbi... 311 int noctets_pq = noctets / 2;
eucrypt_ch15_arbi... 312 int nlimbs_pq = mpi_nlimb_hint_from_nbytes(noctets_pq);
eucrypt_ch15_arbi... 313
eucrypt_ch15_arbi... 314 sk.n = mpi_alloc(0);
eucrypt_ch15_arbi... 315 sk.e = mpi_alloc(0);
eucrypt_ch15_arbi... 316 sk.d = mpi_alloc(0);
eucrypt_ch15_arbi... 317 sk.p = mpi_alloc(0);
eucrypt_ch15_arbi... 318 sk.q = mpi_alloc(0);
eucrypt_ch15_arbi... 319 sk.u = mpi_alloc(0);
eucrypt_ch15_arbi... 320
eucrypt_ch15_arbi... 321 mpi_fromstr(sk.n, "0x\
eucrypt_ch15_arbi... 322 B51BE851F39159EAC714F3E0376713A84DAD36A82D446D0A257A391870F45FAE13C4CC\
eucrypt_ch15_arbi... 323 F400DDA9F604991134C0934161554EEFEAA3147BF0EADC77B99E2B9B6E4EE942EA9D07\
eucrypt_ch15_arbi... 324 5F015EE2465B491F4130E04E1BBB6CCDC98F6E8789D4F7FCA3E3FF83C6100CAF2B764E\
eucrypt_ch15_arbi... 325 A5AF7CBA9B27C13EE72EA7A8602F34B32E17C2BA56CFBA4223F7D9A03C23336095D34F\
eucrypt_ch15_arbi... 326 BF66E88BF5CE661D66C251DFAD4CB2BA8D1E1669AC927894EA20DABABD2495BC2A4BA3\
eucrypt_ch15_arbi... 327 A25C79ABEC2D57F45F0F889D962C777A663D0AB25D3650DFDC6D77C528803C0C6E12BD\
eucrypt_ch15_arbi... 328 05281B33C603BEA66A0C2ACBEBD1CA53D32C2269294C9B93E742CA563AF39E939C32CE\
eucrypt_ch15_arbi... 329 51D5ED827F9C217EF58CC518B635D0E03BA778BCEBAF9A2CDB493282D751A5977CB907\
eucrypt_ch15_arbi... 330 C8708D1EF1CAE644C1F2525DDE98E29761B1ADF0965F08AA856DF540AEFD67F96B92AE\
eucrypt_ch15_arbi... 331 83636C31A507C59635C6D435C5E7EE333DC2257C07BC0FCE27CF400F6EB7A6B90FFF00\
eucrypt_ch15_arbi... 332 C3C1179615BF5DA6137476926C09D8CCD03257DFCAEF12BE9DC1D3F621D6C97D7F3E6D\
eucrypt_ch15_arbi... 333 534337579B4B65AE212ACC26FC3861E24033E6F12A601D473A65EFC5F25ABD5D6049EA\
eucrypt_ch15_arbi... 334 DD6D76BA60AA218C5EBE13439AAFFF0088C49ACC0E9F7DE56DB03F585E1AC2862EB990\
eucrypt_ch15_arbi... 335 59724FD407C4ACD3DD14A53A6A35F6AFAE03EA53A4E742CC370087692E206A2422FF9D");
eucrypt_ch15_arbi... 336
eucrypt_ch15_arbi... 337 sk.e = mpi_alloc(nlimbs_pq);
eucrypt_ch15_arbi... 338 int i;
eucrypt_ch15_arbi... 339 char echar[109];
eucrypt_ch15_arbi... 340 int ne = 109;
eucrypt_ch15_arbi... 341 for (i=0;i<ne;i++)
eucrypt_ch15_arbi... 342 echar[i] = 0;
eucrypt_ch15_arbi... 343
eucrypt_ch15_arbi... 344 echar[ne-1] = 59;
eucrypt_ch15_arbi... 345 echar[ne-2] = 153;
eucrypt_ch15_arbi... 346 echar[ne-3] = 145;
eucrypt_ch15_arbi... 347 echar[ne-4] = 109;
eucrypt_ch15_arbi... 348 echar[ne-5] = 236;
eucrypt_ch15_arbi... 349 echar[ne-6] = 157;
eucrypt_ch15_arbi... 350 echar[ne-7] = 64;
eucrypt_ch15_arbi... 351 echar[ne-8] = 195;
eucrypt_ch15_arbi... 352
eucrypt_ch15_arbi... 353 mpi_set_buffer(sk.e, echar, ne, 0);
eucrypt_ch15_arbi... 354 mpi_normalize(sk.e);
eucrypt_ch15_arbi... 355
eucrypt_ch15_arbi... 356 mpi_fromstr(sk.d, "0x\
eucrypt_ch15_arbi... 357 7694F5266A995D31DCF3AE40A35AAE4B6F6E2D625EDE8AEF3DF4418C1C5B6D46FBC8AC\
eucrypt_ch15_arbi... 358 1C018B475BB531807C6CB649A3D2A3044B11560B15DFD17E29E2294CBBE4D2E787B048\
eucrypt_ch15_arbi... 359 D7EF19DB02392534DBBE8C0F57DCA17448B5DCDA08A69C3177749BEA150BBC506372DD\
eucrypt_ch15_arbi... 360 D6DBE48869567B31B00FB855AB4700A0458570393F2AA6A5887DAB0E6FD194B970AED2\
eucrypt_ch15_arbi... 361 A6AA1AE13C1B4BA1154F7D2C55699429CD634425460B961C581639E6CC005B6FDEF273\
eucrypt_ch15_arbi... 362 E5A8A34F39E5F7999AEA6E6767A9842203C00763BEDECFB2FCBC4D071AADB81D3DBB32\
eucrypt_ch15_arbi... 363 58872EBA06804183AE66DE1859758BB1752486DC95887DA71BA231E115FFA43AB5F596\
eucrypt_ch15_arbi... 364 21F5DA9BDF780BB35E3DAA06C2BE83FFE160BE6C625D8D33A42491510E22AD545AE0FE\
eucrypt_ch15_arbi... 365 933F2A70FCBF794156EE6FDB4351404706EC528BD865A3F75B66ACE3186620B5713F44\
eucrypt_ch15_arbi... 366 D2D185BC2F8B42B2C9BD8274D11D4E4449D9237398A4D576A0722871B96C2675E32EAD\
eucrypt_ch15_arbi... 367 C8E2DB958FB016D9DBDE9653FC7A346689D348F1161C3E183C19DDD92A313E2191435E\
eucrypt_ch15_arbi... 368 449E819FC3B04CA8B7874B3283AE52FEE822525BAAA14F7E28DF2B1EA9383FBEFA7A4B\
eucrypt_ch15_arbi... 369 AB22BC178CDCF5064E09D8EAAAC4EEF50550E8CB5D3F3079D80FECC4A7B2587BFA6B68\
eucrypt_ch15_arbi... 370 0798D6A6175BA3ABAC52B2BE583849FC3C47A6B7B9BB6CE18AF23BFC54E8F04AA6AF3");
eucrypt_ch15_arbi... 371 mpi_fromstr(sk.p, "0x\
eucrypt_ch15_arbi... 372 C9AE5920BA2CFC9A765C25D8B75E8A42845F5B13F8B9386698C31D01BDDE572BD181E4\
eucrypt_ch15_arbi... 373 5FD46666BB9D3F5043D2B3F7B4B3D301DA403CED22ADBB60544D2000EBB3FEF3E1674D\
eucrypt_ch15_arbi... 374 B2F2BB81A81573B3556689FD2AF740367EB9C7CFFC08BD95471849C456B9AD93BA9462\
eucrypt_ch15_arbi... 375 A541FBD7618E9BF8F8B8DC76233D22826085E06F8CC22C4DB46DE8A6E666FBF678052E\
eucrypt_ch15_arbi... 376 80B5D116B4CAC4FCA1C3348F45895D823842E6A4E2B605F0F8CCD9A75F59B3B4BBC102\
eucrypt_ch15_arbi... 377 B9B7F948EC28AB60BF4FD5441E4EF48B25D6A50C5AAEB882EE9862E042F54510366730\
eucrypt_ch15_arbi... 378 B8DD56C02FC43CD95F6EF7B92379E6DED28764CDD22899C845F484EF8F777B9A527FC1");
eucrypt_ch15_arbi... 379 mpi_fromstr(sk.q, "0x\
eucrypt_ch15_arbi... 380 E5E324CDCAEA29DE1EFB3E2280B51E4461A692A5844F64E6917677907A7825A8ABBEF1\
eucrypt_ch15_arbi... 381 1E0D8A5D70DF703D4D4014FE0A125E5A409AD7DC9B5F2ECBDF518C8ECE783F217268CF\
eucrypt_ch15_arbi... 382 ED93689AEB5D970B3C898B7ACB8868E2D284A85F1B82926C67F6F6675F78799CC280E4\
eucrypt_ch15_arbi... 383 197AB3800B00ED9B063B9A00F483CDD5158DCE31B575F579E4206D32E483994B58D5EA\
eucrypt_ch15_arbi... 384 7FBE224326672ED00BA1B432796E78B80816D641BE4F81BF8CCA5A2E93A0E9FA4D6133\
eucrypt_ch15_arbi... 385 4E893903E133DA215295FAF00F9BA1F224BC700ABCD6A94FB4C1A006BD5673D304B207\
eucrypt_ch15_arbi... 386 F8EE6752F41C4D2F7DE310266EAF61B0545A26FAC66A46B90015FC2374D4FD01F836DD");
eucrypt_ch15_arbi... 387 mpi_fromstr(sk.u, "0x\
eucrypt_ch15_arbi... 388 746B5BD406C84FDBC8EF4DEC7D79831630B45579D7865695BDB288C0E8DB39749200B0\
eucrypt_ch15_arbi... 389 720F6FAF1CBB3F2EE6C6FF3DA72B4C8505D50732DBEE2E92CD9A13B6919D5173DBA708\
eucrypt_ch15_arbi... 390 B44C36129A3494DCD77B9991F88F528FF11706E0D6EFC67705937F5A11FCBC984F89F2\
eucrypt_ch15_arbi... 391 A4168A8ED2A391FAD06C1E15F838B890126DF650DA803661448F8CEA755F15432A83C8\
eucrypt_ch15_arbi... 392 99CB0AFCB5D922514DF19697416DDC79CA2AC7CB30AEADEC2D68B0F43D178DAB33656E\
eucrypt_ch15_arbi... 393 3CDF08A3D2B903797E74210478526D49AA740F906789740826DDD3CF2E5F032B9E0536\
eucrypt_ch15_arbi... 394 85560848F127A1A844BF00C5819FFEB2016E0DBC86BF375554DC28EC2DC7FD8830AA3D");
eucrypt_ch15_arbi... 395
eucrypt_ch15_arbi... 396 test_rsa_keys(&sk, noctets_pq, stdout);
eucrypt_ch15_arbi... 397
eucrypt_ch15_arbi... 398 mpi_free(sk.n);
eucrypt_ch15_arbi... 399 mpi_free(sk.e);
eucrypt_ch15_arbi... 400 mpi_free(sk.d);
eucrypt_ch15_arbi... 401 mpi_free(sk.p);
eucrypt_ch15_arbi... 402 mpi_free(sk.q);
eucrypt_ch15_arbi... 403 mpi_free(sk.u);
eucrypt_ch15_arbi... 404 }
eucrypt_ch15_arbi... 405
eucrypt_ch5_rsa_keys 406 void test_rsa_exp() {
eucrypt_ch5_rsa_keys 407 MPI msg = mpi_alloc(0);
eucrypt_ch5_rsa_keys 408 MPI expected = mpi_alloc(0);
eucrypt_ch5_rsa_keys 409 MPI result;
eucrypt_ch5_rsa_keys 410
eucrypt_ch5_rsa_keys 411 RSA_public_key pk;
eucrypt_ch5_rsa_keys 412 pk.n = mpi_alloc(0);
eucrypt_ch5_rsa_keys 413 pk.e = mpi_alloc(0);
eucrypt_ch5_rsa_keys 414
eucrypt_ch5_rsa_keys 415 printf("TEST verify of rsa exponentiation on input data: \n");
eucrypt_ch5_rsa_keys 416
eucrypt_ch5_rsa_keys 417 mpi_fromstr(msg, "0x\
eucrypt_ch5_rsa_keys 418 5B6A8A0ACF4F4DB3F82EAC2D20255E4DF3E4B7C799603210766F26EF87C8980E737579\
eucrypt_ch5_rsa_keys 419 EC08E6505A51D19654C26D806BAF1B62F9C032E0B13D02AF99F7313BFCFD68DA46836E\
eucrypt_ch5_rsa_keys 420 CA529D7360948550F982C6476C054A97FD01635AB44BFBDBE2A90BE06F7984AC8534C3\
eucrypt_ch5_rsa_keys 421 8613747F340C18176E6D5F0C10246A2FCE3A668EACB6165C2052497CA2EE483F4FD8D0\
eucrypt_ch5_rsa_keys 422 6A9911BD97E9B6720521D872BD08FF8DA11A1B8DB147F252E4E69AE6201D3B374B171D\
eucrypt_ch5_rsa_keys 423 F445EF2BF509D468FD57CEB5840349B14C6E2AAA194D9531D238B85B8F0DD352D1E596\
eucrypt_ch5_rsa_keys 424 71539B429849E5D965E438BF9EFFC338DF9AADF304C4130D5A05E006ED855F37A06242\
eucrypt_ch5_rsa_keys 425 28097EF92F6E78CAE0CB97");
eucrypt_ch5_rsa_keys 426
eucrypt_ch5_rsa_keys 427 mpi_fromstr(expected, "0x\
eucrypt_ch5_rsa_keys 428 1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
eucrypt_ch5_rsa_keys 429 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
eucrypt_ch5_rsa_keys 430 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
eucrypt_ch5_rsa_keys 431 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF\
eucrypt_ch5_rsa_keys 432 FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF003051300\
eucrypt_ch5_rsa_keys 433 D0609608648016503040203050004406255509399A3AF322C486C770C5F7F6E05E18FC\
eucrypt_ch5_rsa_keys 434 3E2219A03CA56C7501426A597187468B2F71B4A198C807171B73D0E7DBC3EEF6EA6AFF\
eucrypt_ch5_rsa_keys 435 693DE58E18FF84395BE");
eucrypt_ch5_rsa_keys 436 result = mpi_alloc( mpi_get_nlimbs(expected) );
eucrypt_ch5_rsa_keys 437
eucrypt_ch5_rsa_keys 438 mpi_fromstr(pk.n, "0x\
eucrypt_ch5_rsa_keys 439 CDD49A674BAF76D3B73E25BC6DF66EF3ABEDDCA461D3CCB6416793E3437C7806562694\
eucrypt_ch5_rsa_keys 440 73C2212D5FD5EED17AA067FEC001D8E76EC901EDEDF960304F891BD3CAD7F9A335D1A2\
eucrypt_ch5_rsa_keys 441 EC37EABEFF3FBE6D3C726DC68E599EBFE5456EF19813398CD7D548D746A30AA47D4293\
eucrypt_ch5_rsa_keys 442 968BFBAFCBF65A90DFFC87816FEE2A01E1DC699F4DDABB84965514C0D909D54FDA7062\
eucrypt_ch5_rsa_keys 443 A2037B50B771C153D5429BA4BA335EAB840F9551E9CD9DF8BB4A6DC3ED1318FF3969F7\
eucrypt_ch5_rsa_keys 444 B99D9FB90CAB968813F8AD4F9A069C9639A74D70A659C69C29692567CE863B88E191CC\
eucrypt_ch5_rsa_keys 445 9535B91B417D0AF14BE09C78B53AF9C5F494BCF2C60349FFA93C81E817AC682F0055A6\
eucrypt_ch5_rsa_keys 446 07BB56D6A281C1A04CEFE1");
eucrypt_ch5_rsa_keys 447
eucrypt_ch5_rsa_keys 448 mpi_fromstr( pk.e, "0x10001");
eucrypt_ch5_rsa_keys 449
eucrypt_ch5_rsa_keys 450 mpi_print( stdout, msg, 1);
eucrypt_ch5_rsa_keys 451 printf("\n");
eucrypt_ch5_rsa_keys 452
eucrypt_ch5_rsa_keys 453 public_rsa( result, msg, &pk);
eucrypt_ch5_rsa_keys 454 if ( mpi_cmp( result, expected) != 0 )
eucrypt_ch5_rsa_keys 455 printf( "FAILED\n");
eucrypt_ch5_rsa_keys 456 else
eucrypt_ch5_rsa_keys 457 printf( "PASSED\n");
eucrypt_ch5_rsa_keys 458
eucrypt_ch5_rsa_keys 459 printf("Expected:\n");
eucrypt_ch5_rsa_keys 460 mpi_print( stdout, expected, 1);
eucrypt_ch5_rsa_keys 461 printf("\n");
eucrypt_ch5_rsa_keys 462
eucrypt_ch5_rsa_keys 463 printf("Obtained:\n");
eucrypt_ch5_rsa_keys 464 mpi_print( stdout, result, 1);
eucrypt_ch5_rsa_keys 465 printf("\n");
eucrypt_ch5_rsa_keys 466
eucrypt_ch5_rsa_keys 467 mpi_free( pk.n );
eucrypt_ch5_rsa_keys 468 mpi_free( pk.e );
eucrypt_ch5_rsa_keys 469 mpi_free( msg );
eucrypt_ch5_rsa_keys 470 mpi_free( expected );
eucrypt_ch5_rsa_keys 471 mpi_free( result );
eucrypt_ch5_rsa_keys 472 }
eucrypt_ch5_rsa_keys 473
eucrypt_ch5_rsa_keys 474 void time_rsa_gen( int nruns ) {
eucrypt_ch5_rsa_keys 475 struct timespec tstart, tend;
eucrypt_ch5_rsa_keys 476 long int diff;
eucrypt_ch5_rsa_keys 477 int i;
eucrypt_ch5_rsa_keys 478
eucrypt_ch5_rsa_keys 479 RSA_secret_key sk;
eucrypt_ch5_rsa_keys 480 int noctets = KEY_LENGTH_OCTETS;
eucrypt_ch5_rsa_keys 481 int noctets_pq = noctets / 2;
eucrypt_ch5_rsa_keys 482 int nlimbs = mpi_nlimb_hint_from_nbytes(noctets);
eucrypt_ch5_rsa_keys 483 int nlimbs_pq = mpi_nlimb_hint_from_nbytes(noctets_pq);
eucrypt_ch5_rsa_keys 484 sk.n = mpi_alloc(nlimbs);
eucrypt_ch5_rsa_keys 485 sk.e = mpi_alloc(nlimbs);
eucrypt_ch5_rsa_keys 486 sk.d = mpi_alloc(nlimbs);
eucrypt_ch5_rsa_keys 487 sk.p = mpi_alloc(nlimbs_pq);
eucrypt_ch5_rsa_keys 488 sk.q = mpi_alloc(nlimbs_pq);
eucrypt_ch5_rsa_keys 489 sk.u = mpi_alloc(nlimbs_pq);
eucrypt_ch5_rsa_keys 490
eucrypt_ch5_rsa_keys 491 clock_gettime(CLOCK_MONOTONIC, &tstart);
eucrypt_ch5_rsa_keys 492 for (i = 0;i < nruns; i++) {
eucrypt_ch5_rsa_keys 493 gen_keypair(&sk);
eucrypt_ch5_rsa_keys 494 }
eucrypt_ch5_rsa_keys 495 clock_gettime(CLOCK_MONOTONIC, &tend);
eucrypt_ch5_rsa_keys 496
eucrypt_ch5_rsa_keys 497 diff = tend.tv_sec-tstart.tv_sec;
eucrypt_ch5_rsa_keys 498
eucrypt_ch5_rsa_keys 499 printf("TOTAL: %ld seconds for generating %d key pairs\n", diff, nruns);
eucrypt_ch5_rsa_keys 500 printf("Average (%d runs): %f seconds per TMSR RSA key pair.\n",
eucrypt_ch5_rsa_keys 501 nruns, diff / (1.0*nruns));
eucrypt_ch5_rsa_keys 502 mpi_free(sk.n);
eucrypt_ch5_rsa_keys 503 mpi_free(sk.e);
eucrypt_ch5_rsa_keys 504 mpi_free(sk.d);
eucrypt_ch5_rsa_keys 505 mpi_free(sk.p);
eucrypt_ch5_rsa_keys 506 mpi_free(sk.q);
eucrypt_ch5_rsa_keys 507 mpi_free(sk.u);
eucrypt_ch5_rsa_keys 508 }
eucrypt_ch5_rsa_keys 509
eucrypt_ch12_wrap... 510 void test_oaep_encr_decr( int nruns ) {
eucrypt_ch12_wrap... 511
eucrypt_ch12_wrap... 512 RSA_public_key pk;
eucrypt_ch12_wrap... 513 pk.n = mpi_alloc(0);
eucrypt_ch12_wrap... 514 pk.e = mpi_alloc(0);
eucrypt_ch12_wrap... 515
eucrypt_ch12_wrap... 516 RSA_secret_key sk;
eucrypt_ch12_wrap... 517 sk.n = mpi_alloc(0);
eucrypt_ch12_wrap... 518 sk.e = mpi_alloc(0);
eucrypt_ch12_wrap... 519 sk.d = mpi_alloc(0);
eucrypt_ch12_wrap... 520 sk.p = mpi_alloc(0);
eucrypt_ch12_wrap... 521 sk.q = mpi_alloc(0);
eucrypt_ch12_wrap... 522 sk.u = mpi_alloc(0);
eucrypt_ch12_wrap... 523
eucrypt_ch12_wrap... 524 mpi_fromstr(sk.n, "0x\
eucrypt_ch12_wrap... 525 CD2C025323BEA46FFF2FA8D7A9D39817EA713421F4AE03FA8120641193892A70BFECF5\
eucrypt_ch12_wrap... 526 83101635A432110D3DDE6339E3CC7ECC0AD91C026FCACE832DD3888A6FCA7BCE56C390\
eucrypt_ch12_wrap... 527 5A5AC8C7BC921DA675E4B62489B254EB34659D547D71165BC998983A81937BD251AEE1\
eucrypt_ch12_wrap... 528 2D985EC387D5376F5DCC5EF7EC530FBD6FD2AA7285EE1AF3335EA73163F0954F30402E\
eucrypt_ch12_wrap... 529 D7B374EE84A97B1849B0674B0DA0A2050BD79B71ABB1559F3A9CFDB8557DED7BC90CF2\
eucrypt_ch12_wrap... 530 09E8A847E9C226140845B7D03842162E7DA5DD16326CB1F71A248D841FE9076A09911F\
eucrypt_ch12_wrap... 531 2F4F5E3EA44EA8DE40332BF00406990BCCF61C322A03C456EF3A98B341E0BDBC1088CE\
eucrypt_ch12_wrap... 532 683E78510E76B72C2BCC1EE9AEDD80FFF18ABFC5923B2F36B581C25114AB2DF9F6C2B1\
eucrypt_ch12_wrap... 533 9481703FD19E313DCD7ACE15FA11B27D25BCE5388C180A7E21167FB87750599E1ED7C7\
eucrypt_ch12_wrap... 534 50F4A844E1DC2270C62D19671CF8F4C25B81E366B09FC850AE642136D204A9160AEECE\
eucrypt_ch12_wrap... 535 575B57378AA439E9DD46DC990288CD54BAA35EEE1C02456CD39458A6F1CBF012DCEDF4\
eucrypt_ch12_wrap... 536 27CCF3F3F53645658FC49C9C9D7F2856DB571D92B967AB5845514E0054DDB49099F5DD\
eucrypt_ch12_wrap... 537 04A6F6F5C5CE642276834B932881AEB648D1F25E9223971F56E249EF40CF7D80F22621\
eucrypt_ch12_wrap... 538 CDD0260E9E7D23746960ADB52CF2987584FB1DE95A69A39E5CB12B76E0F5C1A0529C0C\
eucrypt_ch12_wrap... 539 065D2E35720810F7C7983180B9A9EA0E00C11B79DC3D");
eucrypt_ch12_wrap... 540
eucrypt_ch12_wrap... 541 mpi_fromstr(sk.e, "0x\
eucrypt_ch12_wrap... 542 DD4856B4EE3D099A8604AE392D8EFEC094CDF01546A28BE87CB484F999E8E75CDFCD01\
eucrypt_ch12_wrap... 543 D04D455A6A9254C60BD28C0B03611FC3E751CC27EF768C0B401C4FD2B27C092834A6F2\
eucrypt_ch12_wrap... 544 49A145C4EDC47A3B3D363EC352462C945334D160AF9AA72202862912493AC6190AA3A6\
eucrypt_ch12_wrap... 545 149D4D8B9996BA7927D3D0D2AD00D30FD630CF464E6CAF9CF49355B9A70E05DB7AE915\
eucrypt_ch12_wrap... 546 F9F602772F8D11E5FCDFC7709210F248052615967090CC1F43D410C83724AA5912B2F0\
eucrypt_ch12_wrap... 547 52E6B39449A89A97C79C92DC8CB8DEEFCF248C1E1D2FC5BFE85165ECA31839CAA9CEB3\
eucrypt_ch12_wrap... 548 3A92EBDC0EB3BAC0F810938BB173C7DA21DCBB2220D44CBA0FD40A2C868FC93AC5243E\
eucrypt_ch12_wrap... 549 C137C27B0A76D65634EBB3");
eucrypt_ch12_wrap... 550
eucrypt_ch12_wrap... 551 mpi_fromstr(sk.d, "0x\
eucrypt_ch12_wrap... 552 7C8A6FA1199D99DCA45E9BDF567CA49D02B237340D7E999150BC4883AE29DEC5158521\
eucrypt_ch12_wrap... 553 B338F35DC883792356BDDBB3C8B3030A6DD4C6522599A3254E751F9BA1CB1061C5633C\
eucrypt_ch12_wrap... 554 81BBFACF6FCD64502614102DFED3F3FA284066C342D5E00953B415915331E30812E5FB\
eucrypt_ch12_wrap... 555 CD6680ADCCDEE40B8376A3A225F2E160EA59C7566804526D73BB660A648A3EF9802313\
eucrypt_ch12_wrap... 556 B2F841E8458B2AAACE7AACF31083E8F3F630298138393BC88BBD7D4AA4334949651D25\
eucrypt_ch12_wrap... 557 365B10DBF4A4A08E20A6CC74BFDD37C1C38E2ADC2A283DF06590DF06B46F67F6ACA67F\
eucrypt_ch12_wrap... 558 AC464C795261659A2F9558802D0BBAA05FD1E1AF2CDC70654723DF7EFAEA148B8CDBEB\
eucrypt_ch12_wrap... 559 C89EA2320AB9BBB1BC4311475DF3D91446F02EF192368DFEBAC598CCFD4407DEC58FDC\
eucrypt_ch12_wrap... 560 1A94CCDD6E5FBA9C52164ACEA8AEE633E557BCCEACB7A1AF656C379482D784A120A725\
eucrypt_ch12_wrap... 561 32F9B2B35173D505F21D5AD4CB9511BC836DC923730B70291B70290A216CA3B21CFF79\
eucrypt_ch12_wrap... 562 E895C35F4F7AF80E1BD9ED2773BD26919A76E4298D169160593E0335BE2A2A2D2E8516\
eucrypt_ch12_wrap... 563 948F657E1B1260E18808A9D463C108535FB60B3B28F711C81E5DE24F40214134A53CE5\
eucrypt_ch12_wrap... 564 9A952C8970A1D771EBEFFA2F4359DCF157995B3F1950DE3C6EC41B7FF837148F55F323\
eucrypt_ch12_wrap... 565 372AF3F20CE8B8038E750C23D8F5041FA951327859B0E47483F0A47103EF808C72C251\
eucrypt_ch12_wrap... 566 006FA526245291C8C84C12D2EF63FB2301EA3EEDA42B");
eucrypt_ch12_wrap... 567
eucrypt_ch12_wrap... 568 mpi_fromstr(sk.p, "0x\
eucrypt_ch12_wrap... 569 E236732452039C14EC1D3B8095BDDCFB7625CE27B1EA5394CF4ED09D3CEECAA4FC0BF6\
eucrypt_ch12_wrap... 570 2F7CE975E0C8929CE84B0259D773EA038396479BF15DA065BA70E549B248D77B4B23ED\
eucrypt_ch12_wrap... 571 A267308510DBEE2FD44E35D880EE7CFB81E0646AA8630165BD8988C3A8776D9E704C20\
eucrypt_ch12_wrap... 572 AA25CA0A3C32F27F592D5FD363B04DD57D8C61FFDCDFCCC59E2913DE0EE47769180340\
eucrypt_ch12_wrap... 573 E1EA5A803AA2301A010FF553A380F002601F0853FCACDB82D76FE2FACBCD6E5F294439\
eucrypt_ch12_wrap... 574 0799EA5AE9D7880D4E1D4AE146DC1D4E8495B9DD30E57E883923C5FC26682B7142D35C\
eucrypt_ch12_wrap... 575 D8A0FC561FE725A6CF419B15341F40FE0C31132CBD81DD8E50697BD1EBFFA16B522E16\
eucrypt_ch12_wrap... 576 F5B49A03B707218C7DA60B");
eucrypt_ch12_wrap... 577
eucrypt_ch12_wrap... 578 mpi_fromstr(sk.q, "0x\
eucrypt_ch12_wrap... 579 E830482A3C4F5C3A7E59C10FF8BA760DB1C6D55880B796FFDA4A82E0B60E974E81D04B\
eucrypt_ch12_wrap... 580 2A4AD417823EBFB4E8EFB13782943562B19B6C4A680E3BA0C8E37B5023470F4F1AC1F8\
eucrypt_ch12_wrap... 581 A0B10672EF75CD58BCD45E6B14503B8A6A70AFE79F6201AF56E7364A1C742BE1453FD2\
eucrypt_ch12_wrap... 582 24FDC9D66522EAF4466A084BCB9E46D455A2946E94CBF028770F38D0B741C2CC59308F\
eucrypt_ch12_wrap... 583 71D8C2B4B9C928E0AE8D68DEB48A3E9EFD84A10301EBD55F8221CA32FC567B306B2A8E\
eucrypt_ch12_wrap... 584 116350AFB995859FDF4378C5CFD06901494E8CFA5D8FAC564D6531FA8A2E4761F5EFBA\
eucrypt_ch12_wrap... 585 F78750B6F4662BE9EA4C2FAD67AF73EEB36B41FC15CB678810C19A51DF23555695C4C1\
eucrypt_ch12_wrap... 586 546F3FACA39CAA7BB8DBD7");
eucrypt_ch12_wrap... 587
eucrypt_ch12_wrap... 588 mpi_fromstr(sk.u, "0x\
eucrypt_ch12_wrap... 589 846232322775C1CD7D5569DC59E2F3E61A885AE2E9C4A4F8CB3ACBE8C3A5441E5FE348\
eucrypt_ch12_wrap... 590 A2A8AC9C2998FBF282222BF508AA1ECF66A76AEDD2D9C97028BFD3F6CA0542E38A5312\
eucrypt_ch12_wrap... 591 603C70B95650CE73F80FDD729988FBDB5595A5BF8A007EA34E54994A697906CE56354C\
eucrypt_ch12_wrap... 592 E00DF10EB711DEC274A62494E3D350D88736CF67A477FB600AC9F1D6580727585092BF\
eucrypt_ch12_wrap... 593 5EBC092CC4D6CF75769051033A1197103BE269942F372168A53771746FBA18ED6972D5\
eucrypt_ch12_wrap... 594 0B935A9B1D6B5B3DD50CD89A27FE93C10924E9103FACF7B4C5724A046C3D3B50CC1C78\
eucrypt_ch12_wrap... 595 5F5C8E00DBE1D6561F120F5294C170914BC10F978ED4356EED67A9F3A60D70AFE540FC\
eucrypt_ch12_wrap... 596 5373CBAE3D0A7FD1C87273");
eucrypt_ch12_wrap... 597
eucrypt_ch12_wrap... 598
eucrypt_ch12_wrap... 599 pk.n = mpi_copy( sk.n );
eucrypt_ch12_wrap... 600 pk.e = mpi_copy( sk.e );
eucrypt_ch12_wrap... 601
eucrypt_ch12_wrap... 602
eucrypt_ch12_wrap... 603 MPI msg = mpi_alloc(0);
eucrypt_ch12_wrap... 604 mpi_fromstr(msg, "0x\
eucrypt_ch12_wrap... 605 5B6A8A0ACF4F4DB3F82EAC2D20255E4DF3E4B7C799603210766F26EF87C8980E737579\
eucrypt_ch12_wrap... 606 EC08E6505A51D19654C26D806BAF1B62F9C032E0B13D02AF99F7313BFCFD68DA46836E\
eucrypt_ch12_wrap... 607 CA529D7360948550F982C6476C054A97FD01635AB44BFBDBE2A90BE06F7984AC8534C3\
eucrypt_ch12_wrap... 608 28097EF92F6E78CAE0CB97");
eucrypt_ch12_wrap... 609
eucrypt_ch12_wrap... 610
eucrypt_ch12_wrap... 611 printf("TEST verify oaep_encr_decr on message: \n");
eucrypt_ch12_wrap... 612 mpi_print( stdout, msg, 1);
eucrypt_ch12_wrap... 613 printf("\n");
eucrypt_ch12_wrap... 614
eucrypt_ch12_wrap... 615 int nlimbs_n = mpi_nlimb_hint_from_nbytes( KEY_LENGTH_OCTETS);
eucrypt_ch12_wrap... 616 MPI encr = mpi_alloc( nlimbs_n );
eucrypt_ch12_wrap... 617 MPI decr = mpi_alloc( nlimbs_n );
eucrypt_ch12_wrap... 618 int success;
eucrypt_ch12_wrap... 619
eucrypt_ch12_wrap... 620 adainit();
eucrypt_ch12_wrap... 621 rsa_oaep_encrypt( encr, msg, &pk );
eucrypt_ch12_wrap... 622 rsa_oaep_decrypt( decr, encr, &sk, &success );
eucrypt_ch12_wrap... 623
eucrypt_ch12_wrap... 624 if (success <= 0 ||
eucrypt_ch12_wrap... 625 mpi_cmp(encr, msg) == 0 ||
eucrypt_ch12_wrap... 626 mpi_cmp(msg, decr) != 0)
eucrypt_ch12_wrap... 627 printf("FAILED: success flag is %d\n", success);
eucrypt_ch12_wrap... 628 else
eucrypt_ch12_wrap... 629 printf("PASSED\n");
eucrypt_ch12_wrap... 630
eucrypt_ch12_wrap... 631
eucrypt_ch12_wrap... 632 mpi_clear( decr );
eucrypt_ch12_wrap... 633 rsa_oaep_decrypt( decr, pk.n, &sk, &success);
eucrypt_ch12_wrap... 634 if (success > 0)
eucrypt_ch12_wrap... 635 printf("FAILED: attempt to decrypt non-/corrupted oaep block\n");
eucrypt_ch12_wrap... 636 else
eucrypt_ch12_wrap... 637 printf("PASSED: attempt to decrypt non-/corrupted oaep block\n");
eucrypt_ch12_wrap... 638 adafinal();
eucrypt_ch12_wrap... 639
eucrypt_ch12_wrap... 640
eucrypt_ch12_wrap... 641 mpi_free( sk.n );
eucrypt_ch12_wrap... 642 mpi_free( sk.e );
eucrypt_ch12_wrap... 643 mpi_free( sk.d );
eucrypt_ch12_wrap... 644 mpi_free( sk.p );
eucrypt_ch12_wrap... 645 mpi_free( sk.q );
eucrypt_ch12_wrap... 646 mpi_free( sk.u );
eucrypt_ch12_wrap... 647
eucrypt_ch12_wrap... 648 mpi_free( pk.n );
eucrypt_ch12_wrap... 649 mpi_free( pk.e );
eucrypt_ch12_wrap... 650
eucrypt_ch12_wrap... 651 mpi_free( msg );
eucrypt_ch12_wrap... 652 mpi_free( encr );
eucrypt_ch12_wrap... 653 mpi_free( decr );
eucrypt_ch12_wrap... 654 }
eucrypt_ch12_wrap... 655
eucrypt_ch12_wrap... 656 void test_mpi_buffer() {
eucrypt_ch12_wrap... 657 unsigned int noctets = 10;
eucrypt_ch12_wrap... 658 int nlimbs = mpi_nlimb_hint_from_nbytes( noctets );
eucrypt_ch12_wrap... 659 MPI m = mpi_alloc( nlimbs );
eucrypt_ch12_wrap... 660 unsigned char *setbuffer = xmalloc( noctets );
eucrypt_ch12_wrap... 661 unsigned char *getbuffer;
eucrypt_ch12_wrap... 662 unsigned int i, sign, mpilen, nerrors;
eucrypt_ch12_wrap... 663
eucrypt_ch12_wrap... 664 for (i=0; i< noctets; i++)
eucrypt_ch12_wrap... 665 setbuffer[i] = i;
eucrypt_ch12_wrap... 666
eucrypt_ch12_wrap... 667 mpi_set_buffer( m, setbuffer, noctets, 0);
eucrypt_ch12_wrap... 668
eucrypt_ch12_wrap... 669 getbuffer = mpi_get_buffer( m, &mpilen, &sign );
eucrypt_ch12_wrap... 670
eucrypt_ch12_wrap... 671 if (mpilen == noctets -1 ) {
eucrypt_ch12_wrap... 672 nerrors = 0;
eucrypt_ch12_wrap... 673 for (i=0;i<noctets-1;i++)
eucrypt_ch12_wrap... 674 if (setbuffer[i+1] != getbuffer[i])
eucrypt_ch12_wrap... 675 nerrors = nerrors + 1;
eucrypt_ch12_wrap... 676 if (nerrors == 0) {
eucrypt_ch12_wrap... 677 printf("WARNING: 0-led octet discarded by mpi_set_buffer!\n");
eucrypt_ch12_wrap... 678 printf("Value ret by mpi_get_buffer != value given to set_buffer!\n");
eucrypt_ch12_wrap... 679 }
eucrypt_ch12_wrap... 680 else
eucrypt_ch12_wrap... 681 printf("FAIL: got different lengths and %d different values!\n", nerrors);
eucrypt_ch12_wrap... 682 }
eucrypt_ch12_wrap... 683 else if (mpilen != noctets)
eucrypt_ch12_wrap... 684 printf("FAIL: mpilen is %d; noctets is %d\n", mpilen, noctets);
eucrypt_ch12_wrap... 685 else
eucrypt_ch12_wrap... 686 {
eucrypt_ch12_wrap... 687 nerrors = 0;
eucrypt_ch12_wrap... 688 for (i=0;i<noctets-1;i++) {
eucrypt_ch12_wrap... 689 if (setbuffer[i]!=getbuffer[i])
eucrypt_ch12_wrap... 690 nerrors= nerrors+1;
eucrypt_ch12_wrap... 691 }
eucrypt_ch12_wrap... 692 if (nerrors>0)
eucrypt_ch12_wrap... 693 printf("FAIL: got %d different values!\n", nerrors);
eucrypt_ch12_wrap... 694 else printf("PASSED: mpi_get/set_buffer\n");
eucrypt_ch12_wrap... 695 }
eucrypt_ch12_wrap... 696
eucrypt_ch12_wrap... 697 mpi_free(m);
eucrypt_ch12_wrap... 698 xfree(setbuffer);
eucrypt_ch12_wrap... 699 xfree(getbuffer);
eucrypt_ch12_wrap... 700 }
eucrypt_ch12_wrap... 701
eucrypt_ch13_smg_rng 702 void test_dirty_float_rng( int nruns ) {
eucrypt_ch13_smg_rng 703 int i, status;
eucrypt_ch13_smg_rng 704 float dirty;
eucrypt_ch13_smg_rng 705
eucrypt_ch13_smg_rng 706 printf("Running test for smg rng dirty float with %d runs\n", nruns);
eucrypt_ch13_smg_rng 707 for (i=0; i<nruns; i++) {
eucrypt_ch13_smg_rng 708 status = rng_dirty_float( &dirty );
eucrypt_ch13_smg_rng 709 printf("Run %d: %f status %s\n", i+1, dirty, status>0 ? "OK" : "FAIL");
eucrypt_ch13_smg_rng 710 }
eucrypt_ch13_smg_rng 711 }
eucrypt_ch13_smg_rng 712
eucrypt_ch13_smg_rng 713 void test_ieee_float_rng( int nruns ) {
eucrypt_ch13_smg_rng 714 int i, status;
eucrypt_ch13_smg_rng 715 float ieee;
eucrypt_ch13_smg_rng 716
eucrypt_ch13_smg_rng 717 printf("Running test for smg rng ieee 745/1985 float with %d runs\n", nruns);
eucrypt_ch13_smg_rng 718 for (i=0; i<nruns; i++) {
eucrypt_ch13_smg_rng 719 status = rng_float_754_1985( &ieee );
eucrypt_ch13_smg_rng 720 printf("Run %d: %f status %s\n", i+1, ieee, status>0 ? "OK" : "FAIL");
eucrypt_ch13_smg_rng 721 }
eucrypt_ch13_smg_rng 722 }
eucrypt_ch13_smg_rng 723
eucrypt_ch13_smg_rng 724 void test_uint32_rng( int nruns ) {
eucrypt_ch13_smg_rng 725 int i, status;
eucrypt_ch13_smg_rng 726 uint32_t n;
eucrypt_ch13_smg_rng 727
eucrypt_ch13_smg_rng 728 printf("Running test for smg rng unsigned int32 with %d runs\n", nruns);
eucrypt_ch13_smg_rng 729 for (i=0; i<nruns; i++) {
eucrypt_ch13_smg_rng 730 status = rng_uint32( &n );
eucrypt_ch13_smg_rng 731 printf("Run %d: %"PRIu32" status %s\n", i+1, n, status>0 ? "OK" : "FAIL");
eucrypt_ch13_smg_rng 732 }
eucrypt_ch13_smg_rng 733 }
eucrypt_ch13_smg_rng 734
eucrypt_ch13_smg_rng 735 void test_uint64_rng( int nruns ) {
eucrypt_ch13_smg_rng 736 int i, status;
eucrypt_ch13_smg_rng 737 uint64_t n;
eucrypt_ch13_smg_rng 738
eucrypt_ch13_smg_rng 739 printf("Running test for smg rng unsigned int64 with %d runs\n", nruns);
eucrypt_ch13_smg_rng 740 for (i=0; i<nruns; i++) {
eucrypt_ch13_smg_rng 741 status = rng_uint64( &n );
eucrypt_ch13_smg_rng 742 printf("Run %d: %"PRIu64" status %s\n", i+1, n, status>0 ? "OK" : "FAIL");
eucrypt_ch13_smg_rng 743 }
eucrypt_ch13_smg_rng 744 }
eucrypt_ch13_smg_rng 745
eucrypt_ch13_smg_rng 746
ch2_truerandom 747 int main(int ac, char **av)
ch2_truerandom 748 {
ch2_truerandom 749 int nruns;
eucrypt_ch3_mille... 750 int id;
eucrypt_ch5_rsa_keys 751 FILE *fk;
eucrypt_ch5_rsa_keys 752 FILE *fout;
ch2_truerandom 753
ch2_truerandom 754 if (ac<2) {
eucrypt_ch4_rpng 755 printf("Usage: %s number_of_runs/octets [testID]\n", av[0]);
ch2_truerandom 756 return -1;
ch2_truerandom 757 }
ch2_truerandom 758 nruns = atoi(av[1]);
ch2_truerandom 759
eucrypt_ch3_mille... 760 if (ac < 3)
eucrypt_ch4_rpng 761 id = -1;
eucrypt_ch3_mille... 762 else
eucrypt_ch3_mille... 763 id = atoi(av[2]);
eucrypt_ch3_mille... 764
eucrypt_ch4_rpng 765 switch ( id ) {
eucrypt_ch4_rpng 766 case 0:
eucrypt_ch4_rpng 767 printf("Timing entropy source...\n");
eucrypt_ch4_rpng 768 time_entropy_source(nruns, 4096);
eucrypt_ch4_rpng 769 break;
eucrypt_ch4_rpng 770 case 1:
eucrypt_ch4_rpng 771 test_entropy_output(nruns, "entropy_source_output.txt");
eucrypt_ch4_rpng 772 break;
eucrypt_ch4_rpng 773 case 2:
eucrypt_ch4_rpng 774
eucrypt_ch4_rpng 775
eucrypt_ch4_rpng 776 test_is_composite(nruns, "0x10001", 0);
eucrypt_ch4_rpng 777 test_is_composite(nruns, "0x1C6DF", 0);
eucrypt_ch4_rpng 778 test_is_composite(nruns, "0x188DD82F", 0);
eucrypt_ch4_rpng 779 test_is_composite(nruns, "0x4E0516E57", 0);
eucrypt_ch4_rpng 780
eucrypt_ch4_rpng 781 test_is_composite(nruns, "0x1FFF", 0);
eucrypt_ch4_rpng 782 test_is_composite(nruns, "0x1FFFF", 0);
eucrypt_ch4_rpng 783 test_is_composite(nruns, "0x7FFFFFFF", 0);
eucrypt_ch4_rpng 784
eucrypt_ch4_rpng 785 test_is_composite(nruns, "0x231", 1);
eucrypt_ch4_rpng 786 test_is_composite(nruns, "0xD8A300793EEF31", 1);
eucrypt_ch4_rpng 787
eucrypt_ch4_rpng 788 test_is_composite(nruns, "0x15A9E672864B1E", 1);
eucrypt_ch4_rpng 789
eucrypt_ch4_rpng 790 test_is_composite(nruns, "0x80000000000000000000000000000003", 1);
eucrypt_ch4_rpng 791 break;
eucrypt_ch4_rpng 792 case 3:
eucrypt_ch4_rpng 793 time_mr(nruns);
eucrypt_ch4_rpng 794 break;
eucrypt_ch4_rpng 795 case 4:
eucrypt_ch4_rpng 796 test_rpng(nruns);
eucrypt_ch4_rpng 797 break;
eucrypt_ch4_rpng 798 case 5:
eucrypt_ch4_rpng 799 time_rpng(nruns);
eucrypt_ch4_rpng 800 break;
eucrypt_ch5_rsa_keys 801 case 6:
eucrypt_ch5_rsa_keys 802 fk = fopen("keys.asc", "a");
eucrypt_ch5_rsa_keys 803 if ( fk == NULL )
eucrypt_ch5_rsa_keys 804 err("Failed to open file keys.asc!");
eucrypt_ch5_rsa_keys 805 fout = fopen("check_keys.asc", "a");
eucrypt_ch5_rsa_keys 806 if ( fout == NULL ) {
eucrypt_ch5_rsa_keys 807 fclose(fk);
eucrypt_ch5_rsa_keys 808 err("Failed to open file keys_check.asc!");
eucrypt_ch5_rsa_keys 809 }
eucrypt_ch5_rsa_keys 810 test_rsa(nruns, fk, fout);
eucrypt_ch5_rsa_keys 811 fclose(fk);
eucrypt_ch5_rsa_keys 812 fclose(fout);
eucrypt_ch5_rsa_keys 813 break;
eucrypt_ch5_rsa_keys 814 case 7:
eucrypt_ch5_rsa_keys 815 test_rsa_exp();
eucrypt_ch5_rsa_keys 816 break;
eucrypt_ch5_rsa_keys 817 case 8:
eucrypt_ch5_rsa_keys 818 time_rsa_gen(nruns);
eucrypt_ch5_rsa_keys 819 break;
eucrypt_ch12_wrap... 820 case 9:
eucrypt_ch12_wrap... 821 test_oaep_encr_decr(nruns);
eucrypt_ch12_wrap... 822 break;
eucrypt_ch12_wrap... 823 case 10:
eucrypt_ch12_wrap... 824 test_mpi_buffer();
eucrypt_ch12_wrap... 825 break;
eucrypt_ch13_smg_rng 826 case 11:
eucrypt_ch13_smg_rng 827 test_dirty_float_rng(nruns);
eucrypt_ch13_smg_rng 828 break;
eucrypt_ch13_smg_rng 829 case 12:
eucrypt_ch13_smg_rng 830 test_ieee_float_rng(nruns);
eucrypt_ch13_smg_rng 831 break;
eucrypt_ch13_smg_rng 832 case 13:
eucrypt_ch13_smg_rng 833 test_uint32_rng(nruns);
eucrypt_ch13_smg_rng 834 break;
eucrypt_ch13_smg_rng 835 case 14:
eucrypt_ch13_smg_rng 836 test_uint64_rng(nruns);
eucrypt_ch13_smg_rng 837 break;
eucrypt_ch15_arbi... 838 case 15:
eucrypt_ch15_arbi... 839 test_rsa_8e(nruns);
eucrypt_ch15_arbi... 840 break;
eucrypt_ch4_rpng 841 default:
eucrypt_ch4_rpng 842 printf("Current test ids:\n");
eucrypt_ch4_rpng 843 printf("0 for timing entropy source\n");
eucrypt_ch4_rpng 844 printf("1 for entropy output test\n");
eucrypt_ch4_rpng 845 printf("2 for is_composite (Miller-Rabin) test\n");
eucrypt_ch4_rpng 846 printf("3 for timing Miller-Rabin\n");
eucrypt_ch4_rpng 847 printf("4 for random prime number generator test\n");
eucrypt_ch4_rpng 848 printf("5 for timing random prime number generator\n");
eucrypt_ch5_rsa_keys 849 printf("6 for testing rsa key pair generation and use; \
eucrypt_ch5_rsa_keys 850 writes to keys.asc and check_keys.asc\n");
eucrypt_ch5_rsa_keys 851 printf("7 for testing rsa exponentiation (fixed data)\n");
eucrypt_ch5_rsa_keys 852 printf("8 for timing rsa key pair generator\n");
eucrypt_ch12_wrap... 853 printf("9 for oaep encrypt/decrypt\n");
eucrypt_ch12_wrap... 854 printf("10 for testing mpi_set/get_buffer\n");
eucrypt_ch13_smg_rng 855 printf("11 for testing smg_rng dirty float\n");
eucrypt_ch13_smg_rng 856 printf("12 for testing smg_rng ieee 745/1985 float\n");
eucrypt_ch13_smg_rng 857 printf("13 for testing smg_rng uint32 \n");
eucrypt_ch13_smg_rng 858 printf("14 for testing smg_rng uint64 \n");
eucrypt_ch15_arbi... 859 printf("15 for testing rsa with 8-octets e \n");
eucrypt_ch3_mille... 860 }
eucrypt_ch3_mille... 861
ch2_truerandom 862 return 0;
ch2_truerandom 863 }