diff -uNr a/eucrypt/mpi/include/mpi-internal.h b/eucrypt/mpi/include/mpi-internal.h --- a/eucrypt/mpi/include/mpi-internal.h 4c06fe251cb2613489112e141996aacc96df8663971921487aa423f35648f26b8c57eaebb4c392196ef382778c6150b5d73921aaafc3ed3cd65216f4eb6c1cf0 +++ b/eucrypt/mpi/include/mpi-internal.h 0ec90eeed7bb58e2d737fddaf5cd8101cfb114c9d0824457384e5c199b4e1a75f1762bcd465cd4b675fe19da36197ee9bff0d3e24ed6b9e4c26a1a05ffc7386e @@ -1,5 +1,6 @@ /* mpi-internal.h - Internal to the Multi Precision Integers * Modified by No Such Labs. (C) 2015. See README. + * Modified by S.MG, 2017: fixing broken MPN_COPY_INCR macro. See EuCrypt patches. * * This file was originally part of Gnu Privacy Guard (GPG), ver. 1.4.10, * SHA256(gnupg-1.4.10.tar.gz): @@ -100,7 +101,7 @@ do { \ mpi_size_t _i; \ for( _i = 0; _i < (n); _i++ ) \ - (d)[_i] = (d)[_i]; \ + (d)[_i] = (s)[_i]; \ } while (0) #define MPN_COPY_DECR( d, s, n ) \ diff -uNr a/eucrypt/mpi/tests/test_mpi.c b/eucrypt/mpi/tests/test_mpi.c --- a/eucrypt/mpi/tests/test_mpi.c 85384bc9e48590da125a7440103ebc5549a2c1e10dd2335810c06f6bca4ca4f22f0626b0f625128f8d73de47c0a2aa9260f1014eacccc5cbdb2e37498347e99c +++ b/eucrypt/mpi/tests/test_mpi.c 587d20e7235b57076203a9c3b8774f60ead751978f1a4ed87ae7cc96548a021919e77c0799b8fff9900a83edb74e173114bac8fddb7ff23ff275472fb0c24603 @@ -1,4 +1,5 @@ #include "mpi.h" +#include "mpi-internal.h" /* for BITS_PER_MPI_LIMB */ #include void err(char *msg) @@ -12,10 +13,66 @@ fprintf(fp, "\n"); } +void print_results(MPI in, MPI out, char * title) +{ + fprintf(stdout, "******** %s ********", title); + terpri(stdout); + + fprintf(stdout, "input : "); + mpi_print(stdout, in, 1); + terpri(stdout); + + fprintf(stdout, "output: "); + mpi_print(stdout, out, 1); + terpri(stdout); + + terpri(stdout); + fflush(stdout); +} + +/* + * Test that will fail on original code and will pass after EuCrypt fix is applied. + */ +void test_rshift() +{ + MPI out, in, copy_in; + out = mpi_alloc(0); + in = mpi_alloc(0); + copy_in = mpi_alloc(0); + + mpi_fromstr(out, "0x20E92FE28E1929"); /* some value */ + mpi_fromstr(in, "0x2000000010000001000000002"); + mpi_fromstr(copy_in, "0x2000000010000001000000002"); /* to make sure the actual input is print, since call can modify in */ + + /* print value of BITS_PER_MPI_LIMB */ + fprintf(stdout, "BITS_PER_MPI_LIMB is %d\n", BITS_PER_MPI_LIMB); + + /* shift by 0 */ + mpi_tdiv_q_2exp(out, in, 0); + print_results(copy_in, out, "TEST: right shift by 0"); + + /* shift by multiple of BITS_PER_MPI_LIMB */ + mpi_fromstr(in, "0x2000000010000001000000002"); + + mpi_tdiv_q_2exp(out, in, BITS_PER_MPI_LIMB); + print_results(copy_in, out, "TEST: right shift by BITS_PER_MPI_LIMB"); + + /* shift by non-multiple of BITS_PER_MPI_LIMB */ + mpi_fromstr(in, "0x2000000010000001000000002"); + mpi_tdiv_q_2exp(out, in, BITS_PER_MPI_LIMB - 3); + print_results(copy_in, out, "TEST: right shift by BITS_PER_MPI_LIMB - 3"); + + mpi_free(copy_in); + mpi_free(out); + mpi_free(in); +} + int main(int ac, char **av) { MPI a, b, y; int r; + + test_rshift(); r = secmem_init(1000); if (r==0) err("secmem init"); @@ -29,6 +86,8 @@ mpi_free(a); mpi_free(b); + fprintf(stdout, "******** TEST: mpi_mul, see README ********"); + terpri(stdout); mpi_print(stdout, y, 1); mpi_free(y);