Modular add/sub/mul incorrect result if result and modulus pointer are equal

Guido Vranken guidovranken at gmail.com
Fri Jun 2 03:20:39 CEST 2023


In the following program, the results of addm, subm or mulm are all zero,
but should be 6, 5 and 1, respectively.

#include <gcrypt.h>

#define CF_CHECK_EQ(expr, res) if ( (expr) != (res) ) { goto end; }

int main(void)
{
    gcry_mpi_t A;
    gcry_mpi_t B;
    gcry_mpi_t C;
    gcry_error_t err;
    char *buf;

    CF_CHECK_EQ(err = gcry_mpi_scan(&A, GCRYMPI_FMT_HEX, "2", 0, NULL), 0);
    CF_CHECK_EQ(err = gcry_mpi_scan(&B, GCRYMPI_FMT_HEX, "4", 0, NULL), 0);
    CF_CHECK_EQ(err = gcry_mpi_scan(&C, GCRYMPI_FMT_HEX, "7", 0, NULL), 0);
    gcry_mpi_addm(C, A, B, C);
    //gcry_mpi_subm(C, A, B, C);
    //gcry_mpi_mulm(C, A, B, C);
    CF_CHECK_EQ(err = gcry_mpi_aprint(GCRYMPI_FMT_HEX, (unsigned
char**)&buf, NULL, C), 0);
    printf("%s\n", buf);
    gcry_mpi_release(A);
    gcry_mpi_release(B);
    gcry_mpi_release(C);
    gcry_free(buf);
end:
    return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.gnupg.org/pipermail/gcrypt-devel/attachments/20230602/d539375e/attachment-0001.html>


More information about the Gcrypt-devel mailing list