From 6c343d7d9ad1de103c3091ff8edaab1cec1a61a9 Mon Sep 17 00:00:00 2001
From: Paul Bakker
Date: Thu, 10 Jul 2014 14:36:19 +0200
Subject: [PATCH] Fix mpi_write_string() to write "00" as hex output for empty
MPI
---
ChangeLog | 2 ++
library/bignum.c | 2 +-
tests/suites/test_suite_mpi.data | 6 ++++++
3 files changed, 9 insertions(+), 1 deletion(-)
diff --git a/ChangeLog b/ChangeLog
index eb3889d91..c24aaee90 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -66,6 +66,8 @@ Bugfix
* Disable broken Sparc64 bn_mul assembly (found by Florian Obser).
* Fix base64_decode() to return and check length correctly (in case of
tight buffers)
+ * Fix mpi_write_string() to write "00" as hex output for empty MPI (found
+ by Hui Dong)
= PolarSSL 1.3.7 released on 2014-05-02
Features
diff --git a/library/bignum.c b/library/bignum.c
index e7b8d6d6a..80cf0f800 100644
--- a/library/bignum.c
+++ b/library/bignum.c
@@ -530,7 +530,7 @@ int mpi_write_string( const mpi *X, int radix, char *s, size_t *slen )
{
c = ( X->p[i - 1] >> ( ( j - 1 ) << 3) ) & 0xFF;
- if( c == 0 && k == 0 && ( i + j + 3 ) != 0 )
+ if( c == 0 && k == 0 && ( i + j ) != 2 )
continue;
*(p++) = "0123456789ABCDEF" [c / 16];
diff --git a/tests/suites/test_suite_mpi.data b/tests/suites/test_suite_mpi.data
index 5693a80fb..7908f9144 100644
--- a/tests/suites/test_suite_mpi.data
+++ b/tests/suites/test_suite_mpi.data
@@ -37,6 +37,12 @@ mpi_read_write_string:10:"29":15:"1e":100:0:0
Test mpi_read_write_string #7
mpi_read_write_string:10:"56125680981752282334141896320372489490613963693556392520816017892111350604111697682705498319512049040516698827829292076808006940873974979584527073481012636016353913462376755556720019831187364993587901952757307830896531678727717924":16:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":200:0:0
+Test mpi_read_write_string #8 (Empty MPI -> hex)
+mpi_read_write_string:16:"":16:"00":4:0:0
+
+Test mpi_read_write_string #9 (Empty MPI -> dec)
+mpi_read_write_string:16:"":10:"0":4:0:0
+
Base test mpi_read_binary #1
mpi_read_binary:"0941379d00fed1491fe15df284dfde4a142f68aa8d412023195cee66883e6290ffe703f4ea5963bf212713cee46b107c09182b5edcd955adac418bf4918e2889af48e1099d513830cec85c26ac1e158b52620e33ba8692f893efbb2f958b4424":10:"56125680981752282334141896320372489490613963693556392520816017892111350604111697682705498319512049040516698827829292076808006940873974979584527073481012636016353913462376755556720019831187364993587901952757307830896531678727717924"