From 00596c167335912314757fd6efc4b46c9c3c029a Mon Sep 17 00:00:00 2001 From: Iaroslav Gridin Date: Thu, 20 Apr 2023 13:13:37 +0000 Subject: Bug 1784163 - Fix reading raw negative numbers r=nss-reviewers,nkulatova,mt set sign after adding digits Differential Revision: https://phabricator.services.mozilla.com/D154315 --- cmd/mpitests/mpi-test.c | 16 ++++++++++++++++ lib/freebl/mpi/mpi.c | 12 ++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/cmd/mpitests/mpi-test.c b/cmd/mpitests/mpi-test.c index cc045a17c..af4633aa9 100644 --- a/cmd/mpitests/mpi-test.c +++ b/cmd/mpitests/mpi-test.c @@ -2011,9 +2011,11 @@ test_raw(void) { int len, out = 0; mp_int a; + mp_int b; char *buf; mp_init(&a); + mp_init(&b); mp_read_radix(&a, mp4, 16); len = mp_raw_size(&a); @@ -2032,8 +2034,22 @@ test_raw(void) out = 1; } + // read the binary output back and compare with the original number + memcpy(buf, b_mp4, sizeof(b_mp4)); + mp_err read_err = mp_read_raw(&b, buf, sizeof(b_mp4)); + if (read_err != MP_OKAY) { + reason("error: reading raw mp_int failed: %d\n", read_err); + out = 1; + } + + if (mp_cmp(&a, &b) != 0) { + reason("error: test_raw: mp_int from read_raw does not match the original number\n"); + out = 1; + } + free(buf); mp_clear(&a); + mp_clear(&b); return out; } diff --git a/lib/freebl/mpi/mpi.c b/lib/freebl/mpi/mpi.c index bc4c148d6..2e6cd8466 100644 --- a/lib/freebl/mpi/mpi.c +++ b/lib/freebl/mpi/mpi.c @@ -2520,12 +2520,6 @@ mp_read_raw(mp_int *mp, char *str, int len) mp_zero(mp); - /* Get sign from first byte */ - if (ustr[0]) - SIGN(mp) = NEG; - else - SIGN(mp) = ZPOS; - /* Read the rest of the digits */ for (ix = 1; ix < len; ix++) { if ((res = mp_mul_d(mp, 256, mp)) != MP_OKAY) @@ -2534,6 +2528,12 @@ mp_read_raw(mp_int *mp, char *str, int len) return res; } + /* Get sign from first byte */ + if (ustr[0]) + SIGN(mp) = NEG; + else + SIGN(mp) = ZPOS; + return MP_OKAY; } /* end mp_read_raw() */ -- cgit v1.2.1