[PATCH] Curve25519 patch revised

NIIBE Yutaka gniibe at fsij.org
Wed Jul 16 10:11:58 CEST 2014


Hello,

I'm back.

On 2014-06-20 at 11:08 +0200, Werner Koch wrote:
> Can you change the name to mpi_swap_cond ?  I would also prefer to keep
> it an internal function for now and add it to the public API only
> later.  This allows to backport it to 1.6.

Here it is.  I'm going to commit&push this.  No build issue.
 
    mpi: Add mpi_swap_cond.
    
    * mpi/mpiutil.c (_gcry_mpi_swap_cond): New.
    * src/mpi.h (mpi_swap_cond): New.
    --
    
    This is an internal function for now.

diff --git a/mpi/mpiutil.c b/mpi/mpiutil.c
index fdce578..f74dd91 100644
--- a/mpi/mpiutil.c
+++ b/mpi/mpiutil.c
@@ -542,6 +542,34 @@ _gcry_mpi_swap (gcry_mpi_t a, gcry_mpi_t b)
 }
 

+void
+_gcry_mpi_swap_cond (gcry_mpi_t a, gcry_mpi_t b, unsigned long swap)
+{
+  size_t i;
+  size_t nlimbs = a->alloced;
+  unsigned long mask = 0UL - !!swap;
+  unsigned long x;
+
+  if (a->alloced != b->alloced)
+    log_bug ("mpi_swap_cond: different sizes\n");
+
+  for (i = 0; i < nlimbs; i++)
+    {
+      x = mask & (a->d[i] ^ b->d[i]);
+      a->d[i] = a->d[i] ^ x;
+      b->d[i] = b->d[i] ^ x;
+    }
+
+  x = mask & (a->nlimbs ^ b->nlimbs);
+  a->nlimbs = a->nlimbs ^ x;
+  b->nlimbs = b->nlimbs ^ x;
+
+  x = mask & (a->sign ^ b->sign);
+  a->sign = a->sign ^ x;
+  b->sign = b->sign ^ x;
+}
+
+
 gcry_mpi_t
 _gcry_mpi_new (unsigned int nbits)
 {
diff --git a/src/mpi.h b/src/mpi.h
index eb0730e..2479560 100644
--- a/src/mpi.h
+++ b/src/mpi.h
@@ -119,12 +119,14 @@ void _gcry_mpi_immutable_failed (void);
 #define mpi_alloc_set_ui(a)   _gcry_mpi_alloc_set_ui ((a))
 #define mpi_m_check(a)        _gcry_mpi_m_check ((a))
 #define mpi_const(n)          _gcry_mpi_const ((n))
+#define mpi_swap_cond(a,b,sw)  _gcry_mpi_swap_cond ((a),(b),(sw))
 
 void _gcry_mpi_clear( gcry_mpi_t a );
 gcry_mpi_t  _gcry_mpi_alloc_like( gcry_mpi_t a );
 gcry_mpi_t  _gcry_mpi_alloc_set_ui( unsigned long u);
 void _gcry_mpi_m_check( gcry_mpi_t a );
 void _gcry_mpi_swap( gcry_mpi_t a, gcry_mpi_t b);
+void _gcry_mpi_swap_cond (gcry_mpi_t a, gcry_mpi_t b, unsigned long swap);
 gcry_mpi_t _gcry_mpi_new (unsigned int nbits);
 gcry_mpi_t _gcry_mpi_snew (unsigned int nbits);
 gcry_mpi_t _gcry_mpi_set_opaque_copy (gcry_mpi_t a,
-- 





More information about the Gcrypt-devel mailing list