From cdad40dfcebdf0f94abdc63396d003cc942a7c80 Mon Sep 17 00:00:00 2001 From: Simon Butcher Date: Sun, 24 Jun 2018 12:58:31 +0100 Subject: [PATCH 1/3] Add ebx to the i386 clobber list for MPI assembly This fix adds the ebx register to the clobber list for the i386 inline assembly for the multiply helper function. ebx was used but not listed, so when the compiler chose to also use it, ebx was getting corrupted. I'm surprised this wasn't spotted sooner. Fixes Github issues #1550. --- include/mbedtls/bn_mul.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/mbedtls/bn_mul.h b/include/mbedtls/bn_mul.h index 7f8eb1a696..0590882cc6 100644 --- a/include/mbedtls/bn_mul.h +++ b/include/mbedtls/bn_mul.h @@ -141,7 +141,7 @@ "movl %%esi, %3 \n\t" \ : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \ : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \ - : "eax", "ecx", "edx", "esi", "edi" \ + : "eax", "ebx", "ecx", "edx", "esi", "edi" \ ); #else @@ -153,7 +153,7 @@ "movl %%esi, %3 \n\t" \ : "=m" (t), "=m" (c), "=m" (d), "=m" (s) \ : "m" (t), "m" (s), "m" (d), "m" (c), "m" (b) \ - : "eax", "ecx", "edx", "esi", "edi" \ + : "eax", "ebx", "ecx", "edx", "esi", "edi" \ ); #endif /* SSE2 */ #endif /* i386 */ From 54cf322c0575d64f785ded4df08d8c3e306978ed Mon Sep 17 00:00:00 2001 From: Simon Butcher Date: Sun, 24 Jun 2018 16:20:56 +0100 Subject: [PATCH 2/3] Add fix for #1550 and credit to the ChangeLog --- ChangeLog | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ChangeLog b/ChangeLog index 24d20503a0..63a1b4375c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -3,6 +3,8 @@ mbed TLS ChangeLog (Sorted per branch, date) = mbed TLS x.x.x branch released xxxx-xx-xx Bugfix + * Fix the inline assembly for the MPI multiply helper function for i386 and + i386 with SSE2. Found by László Langó. Fixes #1550 * Fix a memory leak in mbedtls_x509_csr_parse(), found by catenacyber, Philippe Antoine. Fixes #1623. * Clarify documentation for mbedtls_ssl_write() to include 0 as a valid From 4171347709cfe0beb7e685d27fa7efd9e4bc89e1 Mon Sep 17 00:00:00 2001 From: Simon Butcher Date: Tue, 10 Jul 2018 20:18:29 +0100 Subject: [PATCH 3/3] Disable use of the i386 assembly for option -O0 We don't compile in the assembly code if compiler optimisations are disabled as the number of registers used in the assembly code doesn't work with the -O0 option. Also anyone select -O0 probably doesn't want to compile in the assembly code anyway. --- include/mbedtls/bn_mul.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/include/mbedtls/bn_mul.h b/include/mbedtls/bn_mul.h index 0590882cc6..8d799ec9b4 100644 --- a/include/mbedtls/bn_mul.h +++ b/include/mbedtls/bn_mul.h @@ -48,7 +48,14 @@ /* armcc5 --gnu defines __GNUC__ but doesn't support GNU's extended asm */ #if defined(__GNUC__) && \ ( !defined(__ARMCC_VERSION) || __ARMCC_VERSION >= 6000000 ) -#if defined(__i386__) + +/* + * Disable use of the i386 assembly code below if option -O0, to disable all + * compiler optimisations, is passed, detected with __OPTIMIZE__ + * This is done as the number of registers used in the assembly code doesn't + * work with the -O0 option. + */ +#if defined(__i386__) && !defined(__OPTIMIZE__) #define MULADDC_INIT \ asm( \