/*----- 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
#include <limits.h>
-#include <mLib/bits.h>
-
+#include "fake-mLib-bits.h"
/*----- Signed integer types ----------------------------------------------*/
-/* See if we can find a suitable 64-bit or wider type. Don't bother if we
- * don't have a corresponding unsigned type, because we really need both.
- */
-#ifdef HAVE_UINT64
-# if INT_MAX >> 31 == 0xffffffff
-# define HAVE_INT64
- typedef int int64;
-# elif LONG_MAX >> 31 == 0xffffffff
-# define HAVE_INT64
- typedef long int64;
-# elif defined(LLONG_MAX)
-# define HAVE_INT64
- MLIB_BITS_EXTENSION typedef long long int64;
-# endif
-#endif
-
-/* Choose suitable 32- and 16-bit types. */
-#if INT_MAX >= 0x7fffffff
- typedef int int32;
-#else
- typedef long int32;
-#endif
-
-typedef short int16;
+typedef int32_t int32;
+typedef int64_t int64;
+#define HAVE_INT64 1
/*----- General bit-hacking utilities -------------------------------------*/
/*----- Constant-time utilities -------------------------------------------*/
/* The following have better implementations on a two's complement target. */
-#ifdef NEG_TWOC
-
- /* If we have two's complement arithmetic then masks are signed; this
- * avoids a switch to unsigned representation, with the consequent problem
- * of overflow when we convert back.
- */
- typedef int32 mask32;
-
- /* Convert an unsigned mask M into a `mask32'. This is a hairy-looking
- * no-op on many targets, but, given that we have two's complement
- * integers, it is free of arithmetic overflow.
- */
-# define FIX_MASK32(m) \
- ((mask32)((m)&0x7fffffffu) + (-(mask32)0x7fffffff - 1)*(((m) >> 31)&1u))
-
- /* If Z is zero and M has its low 32 bits set, then copy (at least) the low
- * 32 bits of X to Z; if M is zero, do nothing. Otherwise, scramble Z
- * unhelpfully.
- */
-# define CONDPICK(z, x, m) do { (z) |= (x)&(m); } while (0)
-
- /* If M has its low 32 bits set, then return (at least) the low 32 bits of
- * X in Z; if M is zero, then return (at least) the low 32 bits of Y in Z.
- * Otherwise, return an unhelful result.
- */
-# define PICK2(x, y, m) (((x)&(m)) | ((y)&~(m)))
-
- /* If M has its low 32 bits set then swap (at least) the low 32 bits of X
- * and Y; if M is zero, then do nothing. Otherwise, scramble X and Y
- * unhelpfully.
- */
-# define CONDSWAP(x, y, m) \
- do { mask32 t_ = ((x) ^ (y))&(m); (x) ^= t_; (y) ^= t_; } while (0)
-#else
-
- /* We don't have two's complement arithmetic. We can't use bithacking at
- * all: if we try to hack on the bits of signed numbers we'll come unstuck
- * when we hit the other representation of zero; and if we switch to
- * unsigned arithmetic then we'll have overflow when we try to convert a
- * negative number back. So fall back to simple arithmetic.
- */
- typedef uint32 mask32;
-
- /* Convert an unsigned mask M into a `mask32'. Our masks are unsigned, so
- * this does nothing.
- */
-# define FIX_MASK32(m) ((mask32)(m))
-
- /* If Z is zero and M has its low 32 bits set, then copy (at least) the low
- * 32 bits of X to Z; if M is zero, do nothing. Otherwise, scramble Z
- * unhelpfully.
- */
-# define CONDPICK(z, x, m) \
- do { (z) += (x)*(int)((unsigned)(m)&1u); } while (0)
-
- /* If M has its low 32 bits set, then return (at least) the low 32 bits of
- * X in Z; if M is zero, then return (at least) the low 32 bits of Y in Z.
- * Otherwise, return an unhelful result.
- */
-# define PICK2(x, y, m) \
- ((x)*(int)((unsigned)(m)&1u) + (y)*(int)(1 - ((unsigned)(m)&1u)))
-
- /* If M has its low 32 bits set then swap (at least) the low 32 bits of X
- * and Y; if M is zero, then do nothing. Otherwise, scramble X and Y
- * unhelpfully.
- */
-# define CONDSWAP(x, y, m) do { \
- int32 x_ = PICK2((y), (x), (m)), y_ = PICK2((x), (y), (m)); \
- (x) = x_; (y) = y_; \
- } while (0)
-#endif
+
+/* If we have two's complement arithmetic then masks are signed; this
+ * avoids a switch to unsigned representation, with the consequent problem
+ * of overflow when we convert back.
+ */
+typedef int32 mask32;
+
+/* Convert an unsigned mask M into a `mask32'. This is a hairy-looking
+ * no-op on many targets, but, given that we have two's complement
+ * integers, it is free of arithmetic overflow.
+ */
+#define FIX_MASK32(m) \
+ ((mask32)((m)&0x7fffffffu) + (-(mask32)0x7fffffff - 1)*(((m) >> 31)&1u))
+
+/* If Z is zero and M has its low 32 bits set, then copy (at least) the low
+ * 32 bits of X to Z; if M is zero, do nothing. Otherwise, scramble Z
+ * unhelpfully.
+ */
+#define CONDPICK(z, x, m) do { (z) |= (x)&(m); } while (0)
+
+/* If M has its low 32 bits set, then return (at least) the low 32 bits of
+ * X in Z; if M is zero, then return (at least) the low 32 bits of Y in Z.
+ * Otherwise, return an unhelful result.
+ */
+#define PICK2(x, y, m) (((x)&(m)) | ((y)&~(m)))
+
+/* If M has its low 32 bits set then swap (at least) the low 32 bits of X
+ * and Y; if M is zero, then do nothing. Otherwise, scramble X and Y
+ * unhelpfully.
+ */
+#define CONDSWAP(x, y, m) \
+ do { mask32 t_ = ((x) ^ (y))&(m); (x) ^= t_; (y) ^= t_; } while (0)
/* Return zero if bit 31 of X is clear, or a mask with (at least) the low 32
* bits set if bit 31 of X is set.