Update crypto code from Catacomb 2.3.0.1-45-g9c14.
[secnet] / f25519.c
index 4e0d1cc..d16943c 100644 (file)
--- a/f25519.c
+++ b/f25519.c
@@ -1,44 +1,3 @@
-/*
- * f25519.c: arithmetic modulo 2^255 - 19
- */
-/*
- * This file is Free Software.  It has been modified to as part of its
- * incorporation into secnet.
- *
- * Copyright 2017 Mark Wooding
- *
- * You may redistribute this file and/or modify it under the terms of
- * the permissive licence shown below.
- *
- * You may redistribute secnet as a whole and/or modify it under the
- * terms of the GNU General Public License as published by the Free
- * Software Foundation; either version 3, or (at your option) any
- * later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, see
- * https://www.gnu.org/licenses/gpl.html.
- */
-/*
- * Imported from Catacomb, and modified for Secnet (2017-04-30):
- *
- *   * Use `fake-mLib-bits.h' in place of the real <mLib/bits.h>.
- *
- *   * Remove the 16/32-bit implementation, since C99 always has 64-bit
- *     arithmetic.
- *
- *   * Remove the test rig code: a replacement is in a separate source file.
- *
- *   * Disable some of the operations which aren't needed for X25519.
- *     (They're used for Ed25519, which we don't need.)
- *
- * The file's original comment headers are preserved below.
- */
 /* -*-c-*-
  *
  * Arithmetic modulo 2^255 - 19
@@ -48,7 +7,26 @@
 
 /*----- Licensing notice --------------------------------------------------*
  *
- * This file is part of Catacomb.
+ * This file is part of secnet.
+ * See README for full list of copyright holders.
+ *
+ * secnet is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version d of the License, or
+ * (at your option) any later version.
+ *
+ * secnet is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * version 3 along with secnet; if not, see
+ * https://www.gnu.org/licenses/gpl.html.
+ *
+ * This file was originally part of Catacomb, but has been automatically
+ * modified for incorporation into secnet: see `import-catacomb-crypto'
+ * for details.
  *
  * Catacomb is free software; you can redistribute it and/or modify
  * it under the terms of the GNU Library General Public License as
@@ -150,6 +128,7 @@ DEF_FDUMP(fdump, piece, PIECEWD, NPIECE, 32, get_2p255m91())
 
 void f25519_load(f25519 *z, const octet xv[32])
 {
+
   uint32 xw0 = LOAD32_L(xv +  0), xw1 = LOAD32_L(xv +  4),
         xw2 = LOAD32_L(xv +  8), xw3 = LOAD32_L(xv + 12),
         xw4 = LOAD32_L(xv + 16), xw5 = LOAD32_L(xv + 20),
@@ -229,6 +208,7 @@ void f25519_load(f25519 *z, const octet xv[32])
 
 void f25519_store(octet zv[32], const f25519 *x)
 {
+
   piece PIECES(x), PIECES(y), c, d;
   uint32 zw0, zw1, zw2, zw3, zw4, zw5, zw6, zw7;
   mask32 m;
@@ -387,8 +367,6 @@ void f25519_sub(f25519 *z, const f25519 *x, const f25519 *y)
   z->P[8] = x->P[8] - y->P[8]; z->P[9] = x->P[9] - y->P[9];
 }
 
-#ifndef F25519_TRIM_X25519
-
 /* --- @f25519_neg@ --- *
  *
  * Arguments:  @f25519 *z@ = where to put the result (may alias @x@)
@@ -408,12 +386,8 @@ void f25519_neg(f25519 *z, const f25519 *x)
   z->P[8] = -x->P[8]; z->P[9] = -x->P[9];
 }
 
-#endif
-
 /*----- Constant-time utilities -------------------------------------------*/
 
-#ifndef F25519_TRIM_X25519
-
 /* --- @f25519_pick2@ --- *
  *
  * Arguments:  @f25519 *z@ = where to put the result (may alias @x@ or @y@)
@@ -480,8 +454,6 @@ void f25519_pickn(f25519 *z, const f25519 *v, size_t n, size_t i)
   }
 }
 
-#endif
-
 /* --- @f25519_condswap@ --- *
  *
  * Arguments:  @f25519 *x, *y@ = two operands
@@ -510,8 +482,6 @@ void f25519_condswap(f25519 *x, f25519 *y, uint32 m)
   CONDSWAP(x->P[9], y->P[9], mm);
 }
 
-#ifndef F25519_TRIM_X25519
-
 /* --- @f25519_condneg@ --- *
  *
  * Arguments:  @f25519 *z@ = where to put the result (may alias @x@)
@@ -545,8 +515,6 @@ void f25519_condneg(f25519 *z, const f25519 *x, uint32 m)
 #undef CONDNEG
 }
 
-#endif
-
 /*----- Multiplication ----------------------------------------------------*/
 
 /* Let B = 2^63 - 1 be the largest value such that +B and -B can be
@@ -596,6 +564,7 @@ void f25519_condneg(f25519 *z, const f25519 *x, uint32 m)
 
 void f25519_mulconst(f25519 *z, const f25519 *x, long a)
 {
+
   piece PIECES(x);
   dblpiece PIECES(z), aa = a;
 
@@ -624,6 +593,7 @@ void f25519_mulconst(f25519 *z, const f25519 *x, long a)
 
 void f25519_mul(f25519 *z, const f25519 *x, const f25519 *y)
 {
+
   piece PIECES(x), PIECES(y);
   dblpiece PIECES(z);
   unsigned i;
@@ -702,6 +672,7 @@ void f25519_mul(f25519 *z, const f25519 *x, const f25519 *y)
 
 void f25519_sqr(f25519 *z, const f25519 *x)
 {
+
   piece PIECES(x);
   dblpiece PIECES(z);
   unsigned i;
@@ -801,8 +772,6 @@ void f25519_inv(f25519 *z, const f25519 *x)
 #undef SQRN
 }
 
-#ifndef F25519_TRIM_X25519
-
 /* --- @f25519_quosqrt@ --- *
  *
  * Arguments:  @f25519 *z@ = where to put the result (may alias @x@ or @y@)
@@ -817,14 +786,8 @@ void f25519_inv(f25519 *z, const f25519 *x)
  */
 
 static const piece sqrtm1_pieces[NPIECE] = {
-#if F25519_IMPL == 26
   -32595792,  -7943725,   9377950,   3500415,  12389472,
     -272473, -25146209,  -2005654,    326686,  11406482
-#elif F25519_IMPL == 10
-   176,  -88,  161,  157, -485, -196, -231, -220, -416,
-  -169, -255,   50,  189,  -89, -266,  -32,  202, -511,
-   423,  357,  248, -249,   80,  288,   50,  174
-#endif
 };
 #define SQRTM1 ((const f25519 *)sqrtm1_pieces)
 
@@ -900,16 +863,14 @@ int f25519_quosqrt(f25519 *z, const f25519 *x, const f25519 *y)
   f25519_store(b1, &u);
   f25519_mul(&u, &beta, SQRTM1);
 
-  m = -ct_memeq(b0, xb, 32);
+  m = -consttime_memeq(b0, xb, 32);
   rc = PICK2(0, rc, m);
   f25519_pick2(z, &beta, &u, m);
-  m = -ct_memeq(b1, xb, 32);
+  m = -consttime_memeq(b1, xb, 32);
   rc = PICK2(0, rc, m);
 
   /* And we're done. */
   return (rc);
 }
 
-#endif
-
 /*----- That's all, folks -------------------------------------------------*/