raw
ch1_mpi                 1 #include "mpi.h"
eucrypt_mpi_fix_c... 2 #include "mpi-internal.h" /* for BITS_PER_MPI_LIMB */
ch1_mpi 3 #include <stdlib.h>
ch1_mpi 4
ch1_mpi 5 void err(char *msg)
ch1_mpi 6 {
ch1_mpi 7 fprintf(stderr, "%s\n", msg);
ch1_mpi 8 exit(1);
ch1_mpi 9 }
ch1_mpi 10
ch1_mpi 11 void terpri(FILE *fp)
ch1_mpi 12 {
ch1_mpi 13 fprintf(fp, "\n");
ch1_mpi 14 }
ch1_mpi 15
eucrypt_mpi_fix_c... 16 void print_results(MPI in, MPI out, char * title)
eucrypt_mpi_fix_c... 17 {
eucrypt_mpi_fix_c... 18 fprintf(stdout, "******** %s ********", title);
eucrypt_mpi_fix_c... 19 terpri(stdout);
eucrypt_mpi_fix_c... 20
eucrypt_mpi_fix_c... 21 fprintf(stdout, "input : ");
eucrypt_mpi_fix_c... 22 mpi_print(stdout, in, 1);
eucrypt_mpi_fix_c... 23 terpri(stdout);
eucrypt_mpi_fix_c... 24
eucrypt_mpi_fix_c... 25 fprintf(stdout, "output: ");
eucrypt_mpi_fix_c... 26 mpi_print(stdout, out, 1);
eucrypt_mpi_fix_c... 27 terpri(stdout);
eucrypt_mpi_fix_c... 28
eucrypt_mpi_fix_c... 29 terpri(stdout);
eucrypt_mpi_fix_c... 30 fflush(stdout);
eucrypt_mpi_fix_c... 31 }
eucrypt_mpi_fix_c... 32
eucrypt_mpi_fix_c... 33 /*
eucrypt_mpi_fix_c... 34 * Test that will fail on original code and will pass after EuCrypt fix is applied.
eucrypt_mpi_fix_c... 35 */
eucrypt_mpi_fix_c... 36 void test_rshift()
eucrypt_mpi_fix_c... 37 {
eucrypt_mpi_fix_c... 38 MPI out, in, copy_in;
eucrypt_mpi_fix_c... 39 out = mpi_alloc(0);
eucrypt_mpi_fix_c... 40 in = mpi_alloc(0);
eucrypt_mpi_fix_c... 41 copy_in = mpi_alloc(0);
eucrypt_mpi_fix_c... 42
eucrypt_mpi_fix_c... 43 mpi_fromstr(out, "0x20E92FE28E1929"); /* some value */
eucrypt_mpi_fix_c... 44 mpi_fromstr(in, "0x2000000010000001000000002");
eucrypt_mpi_fix_c... 45 mpi_fromstr(copy_in, "0x2000000010000001000000002"); /* to make sure the actual input is print, since call can modify in */
eucrypt_mpi_fix_c... 46
eucrypt_mpi_fix_c... 47 /* print value of BITS_PER_MPI_LIMB */
eucrypt_mpi_fix_c... 48 fprintf(stdout, "BITS_PER_MPI_LIMB is %d\n", BITS_PER_MPI_LIMB);
eucrypt_mpi_fix_c... 49
eucrypt_mpi_fix_c... 50 /* shift by 0 */
eucrypt_mpi_fix_c... 51 mpi_tdiv_q_2exp(out, in, 0);
eucrypt_mpi_fix_c... 52 print_results(copy_in, out, "TEST: right shift by 0");
eucrypt_mpi_fix_c... 53
eucrypt_mpi_fix_c... 54 /* shift by multiple of BITS_PER_MPI_LIMB */
eucrypt_mpi_fix_c... 55 mpi_fromstr(in, "0x2000000010000001000000002");
eucrypt_mpi_fix_c... 56
eucrypt_mpi_fix_c... 57 mpi_tdiv_q_2exp(out, in, BITS_PER_MPI_LIMB);
eucrypt_mpi_fix_c... 58 print_results(copy_in, out, "TEST: right shift by BITS_PER_MPI_LIMB");
eucrypt_mpi_fix_c... 59
eucrypt_mpi_fix_c... 60 /* shift by non-multiple of BITS_PER_MPI_LIMB */
eucrypt_mpi_fix_c... 61 mpi_fromstr(in, "0x2000000010000001000000002");
eucrypt_mpi_fix_c... 62 mpi_tdiv_q_2exp(out, in, BITS_PER_MPI_LIMB - 3);
eucrypt_mpi_fix_c... 63 print_results(copy_in, out, "TEST: right shift by BITS_PER_MPI_LIMB - 3");
eucrypt_mpi_fix_c... 64
eucrypt_mpi_fix_c... 65 mpi_free(copy_in);
eucrypt_mpi_fix_c... 66 mpi_free(out);
eucrypt_mpi_fix_c... 67 mpi_free(in);
eucrypt_mpi_fix_c... 68 }
eucrypt_mpi_fix_c... 69
eucrypt_ch3_mille... 70 void test_highbit()
eucrypt_ch3_mille... 71 {
eucrypt_ch3_mille... 72 MPI in, set_out, clear_out;
eucrypt_ch3_mille... 73
eucrypt_ch3_mille... 74 in = mpi_alloc(0);
eucrypt_ch3_mille... 75 set_out = mpi_alloc(0);
eucrypt_ch3_mille... 76 clear_out = mpi_alloc(0);
eucrypt_ch3_mille... 77
eucrypt_ch3_mille... 78 mpi_fromstr(in, "0x2000000010000002000000004");
eucrypt_ch3_mille... 79 mpi_fromstr(set_out, "0x2000000010000002000000004");
eucrypt_ch3_mille... 80 mpi_fromstr(clear_out, "0x2000000010000002000000004");
eucrypt_ch3_mille... 81
eucrypt_ch3_mille... 82 mpi_set_highbit(set_out, 91);
eucrypt_ch3_mille... 83 print_results(in, set_out, "TEST: mpi_set_highbit(in, 91)");
eucrypt_ch3_mille... 84
eucrypt_ch3_mille... 85 mpi_fromstr(set_out, "0x2000000010000002000000004");
eucrypt_ch3_mille... 86 mpi_set_highbit(set_out, 96);
eucrypt_ch3_mille... 87 print_results(in, set_out, "TEST: mpi_set_highbit(in, 96)");
eucrypt_ch3_mille... 88
eucrypt_ch3_mille... 89
eucrypt_ch3_mille... 90 mpi_clear_highbit(clear_out, 96);
eucrypt_ch3_mille... 91 print_results(in, clear_out, "TEST: mpi_clear_highbit(in, 96)");
eucrypt_ch3_mille... 92
eucrypt_ch3_mille... 93 mpi_fromstr(clear_out, "0x2000000010000002000000004");
eucrypt_ch3_mille... 94 mpi_clear_highbit(clear_out, 1);
eucrypt_ch3_mille... 95 print_results(in, clear_out, "TEST: mpi_clear_highbit(in, 1)");
eucrypt_ch3_mille... 96
eucrypt_ch3_mille... 97 mpi_free(in);
eucrypt_ch3_mille... 98 mpi_free(set_out);
eucrypt_ch3_mille... 99 mpi_free(clear_out);
eucrypt_ch3_mille... 100 }
eucrypt_ch3_mille... 101
eucrypt_ch3_mille... 102 void test_get_nbits()
eucrypt_ch3_mille... 103 {
eucrypt_ch3_mille... 104 MPI m;
eucrypt_ch3_mille... 105 int nbits;
eucrypt_ch3_mille... 106
eucrypt_ch3_mille... 107 m = mpi_alloc(0);
eucrypt_ch3_mille... 108 mpi_fromstr(m, "0x0");
eucrypt_ch3_mille... 109 nbits = mpi_get_nbits(m);
eucrypt_ch3_mille... 110 print_results(m, m, "TEST: get_nbits");
eucrypt_ch3_mille... 111 fprintf(stdout, "nbits: %d\n", nbits);
eucrypt_ch3_mille... 112 mpi_free(m);
eucrypt_ch3_mille... 113 }
eucrypt_ch3_mille... 114
ch1_mpi 115 int main(int ac, char **av)
ch1_mpi 116 {
ch1_mpi 117 MPI a, b, y;
ch1_mpi 118 int r;
eucrypt_mpi_fix_c... 119
eucrypt_mpi_fix_c... 120 test_rshift();
eucrypt_ch3_mille... 121 test_highbit();
eucrypt_ch3_mille... 122 test_get_nbits();
ch1_mpi 123
ch1_mpi 124 r = secmem_init(1000);
ch1_mpi 125 if (r==0) err("secmem init");
ch1_mpi 126
ch1_mpi 127 a = mpi_alloc_secure(0);
ch1_mpi 128 b = mpi_alloc_secure(0);
ch1_mpi 129 y = mpi_alloc_secure(0);
ch1_mpi 130 mpi_fromstr(a, "0x1B0B206C488601");
ch1_mpi 131 mpi_fromstr(b, "0x20E92FE28E1929");
ch1_mpi 132 mpi_mul(y, a, b);
ch1_mpi 133 mpi_free(a);
ch1_mpi 134 mpi_free(b);
eucrypt_ch3_mille... 135
eucrypt_mpi_fix_c... 136 fprintf(stdout, "******** TEST: mpi_mul, see README ********");
eucrypt_mpi_fix_c... 137 terpri(stdout);
ch1_mpi 138 mpi_print(stdout, y, 1);
ch1_mpi 139 mpi_free(y);
ch1_mpi 140
ch1_mpi 141 terpri(stdout);
ch1_mpi 142 secmem_term();
ch1_mpi 143
ch1_mpi 144 return 0;
ch1_mpi 145 }