[Gnuk-users] [PATCH 7/7] polarssl: use a bigger sliding window when possible

Aurelien Jarno aurelien at aurel32.net
Thu Oct 12 20:02:25 UTC 2017


---
 polarssl/library/bignum.c | 9 ++++++++-
 src/configure             | 4 +++-
 src/stack-def.h           | 8 +++++++-
 3 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/polarssl/library/bignum.c b/polarssl/library/bignum.c
index 1597317..0d8fbc9 100644
--- a/polarssl/library/bignum.c
+++ b/polarssl/library/bignum.c
@@ -1666,11 +1666,18 @@ static void mpi_montsqr( size_t n, const t_uint *np, t_uint mm, t_uint *d )
 /*
  * Sliding-window exponentiation: X = A^E mod N  (HAC 14.85)
  */
+#if MEMORY_SIZE >= 32
+#define MAX_WSIZE 6
+#elif MEMORY_SIZE >= 24
+#define MAX_WSIZE 5
+#else
+#define MAX_WSIZE 4
+#endif
 int mpi_exp_mod( mpi *X, const mpi *A, const mpi *E, const mpi *N, mpi *_RR )
 {
     int ret;
     size_t i = mpi_msb( E );
-    size_t wsize = ( i > 1024 ) ? 4 : /* Because of not enough memory.  */
+    size_t wsize = ( i > 1024 ) ? MAX_WSIZE :
       		   ( i > 671 ) ? 6 : ( i > 239 ) ? 5 :
                    ( i >  79 ) ? 4 : ( i >  23 ) ? 3 : 1;
     size_t wbits, one = 1;
diff --git a/src/configure b/src/configure
index b53c8e1..09925e2 100755
--- a/src/configure
+++ b/src/configure
@@ -207,6 +207,7 @@ if test "$target" = "GNU_LINUX"; then
   cross=""
   mcu="none"
   def_emulation="-DGNU_LINUX_EMULATION"
+  def_memory_size="-DMEMORY_SIZE=1024"
   enable_hexoutput=""
   libs="-lpthread"
 else
@@ -216,6 +217,7 @@ else
   cross="arm-none-eabi-"
   mcu="cortex-m3"
   def_emulation=""
+  def_memory_size="-DMEMORY_SIZE=$MEMORY_SIZE"
   enable_hexoutput=yes
   libs=""
 fi
@@ -410,7 +412,7 @@ fi
  echo "EMULATION=$emulation";
  echo "CROSS=$cross";
  echo "MCU=$mcu";
- echo "DEFS=$use_sys3 $flash_override $def_emulation";
+ echo "DEFS=$use_sys3 $flash_override $def_emulation $def_memory_size";
  echo "LDSCRIPT=$ldscript";
  echo "LIBS=$libs";
  echo "$DEBUG_MAKE_OPTION";
diff --git a/src/stack-def.h b/src/stack-def.h
index 45aa047..4215290 100644
--- a/src/stack-def.h
+++ b/src/stack-def.h
@@ -1,12 +1,18 @@
 #ifdef GNU_LINUX_EMULATION
 #define SIZE_1 4096
 #define SIZE_2 4096
-#define SIZE_3 (4 * 4096)
+#define SIZE_3 (5 * 4096)
 #else
 #define SIZE_0 0x0100 /* Main         */
 #define SIZE_1 0x01a0 /* CCID         */
 #define SIZE_2 0x0180 /* RNG          */
+#if MEMORY_SIZE >= 32
+#define SIZE_3 0x4640 /* openpgp-card */
+#elif MEMORY_SIZE >= 24
+#define SIZE_3 0x2640 /* openpgp-card */
+#else
 #define SIZE_3 0x1640 /* openpgp-card */
+#endif
 #define SIZE_4 0x0000 /* ---          */
 #define SIZE_5 0x0200 /* msc          */
 #define SIZE_6 0x00c0 /* timer (cir)  */
-- 
2.14.2




More information about the gnuk-users mailing list