Hash utilities: maintain a hash state object, not a bundle of arguments.
[u/mdw/catacomb] / blowfish.c
index 16c862e..2750cfb 100644 (file)
@@ -1,13 +1,13 @@
 /* -*-c-*-
  *
- * $Id: blowfish.c,v 1.1 1999/09/03 08:41:11 mdw Exp $
+ * $Id: blowfish.c,v 1.4 2004/04/08 01:36:15 mdw Exp $
  *
  * The Blowfish block cipher
  *
  * (c) 1998 Straylight/Edgeware
  */
 
-/*----- Licensing notice --------------------------------------------------* 
+/*----- Licensing notice --------------------------------------------------*
  *
  * This file is part of Catacomb.
  *
  * it under the terms of the GNU Library General Public License as
  * published by the Free Software Foundation; either version 2 of the
  * License, or (at your option) any later version.
- * 
+ *
  * Catacomb 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 Library General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU Library General Public
  * License along with Catacomb; if not, write to the Free
  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: blowfish.c,v $
- * Revision 1.1  1999/09/03 08:41:11  mdw
- * Initial import.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include <mLib/bits.h>
 
 #include "blowfish.h"
-#include "bf_ikey.h"
+#include "blowfish-tab.h"
 #include "blkc.h"
+#include "gcipher.h"
 #include "paranoia.h"
 
 /*----- Global variables --------------------------------------------------*/
 
-static blowfish_ctx ikey = BLOWFISH_IKEY;
+static const blowfish_ctx ikey = BLOWFISH_IKEY;
+
+const octet blowfish_keysz[] = { KSZ_RANGE, BLOWFISH_KEYSZ, 1, 56, 1 };
 
 /*----- Macros ------------------------------------------------------------*/
 
-#define ROUND(k, x, y, r)                                              \
-  ((x) ^= (k)->p[(r)],                                                 \
-   (y) ^= (((((k)->s0[((x) >> 24) & MASK8]) +                          \
-            ((k)->s1[((x) >> 16) & MASK8])) ^                          \
-           ((k)->s2[((x) >>  8) & MASK8])) +                           \
-          ((k)->s3[((x) >>  0) & MASK8])))
+#define ROUND(k, x, y, r) do {                                         \
+  x ^= *r;                                                             \
+  y ^= ((k->s0[U8(x >> 24)] +                                          \
+        k->s1[U8(x >> 16)]) ^                                          \
+        k->s2[U8(x >>  8)]) +                                          \
+        k->s3[U8(x >>  0)];                                            \
+} while (0)
 
 #define EBLK(k, a, b, c, d) do {                                       \
-  uint32 _x = (a);                                                     \
-  uint32 _y = (b);                                                     \
-  ROUND((k), _x, _y,  0);                                              \
-  ROUND((k), _y, _x,  1);                                              \
-  ROUND((k), _x, _y,  2);                                              \
-  ROUND((k), _y, _x,  3);                                              \
-  ROUND((k), _x, _y,  4);                                              \
-  ROUND((k), _y, _x,  5);                                              \
-  ROUND((k), _x, _y,  6);                                              \
-  ROUND((k), _y, _x,  7);                                              \
-  ROUND((k), _x, _y,  8);                                              \
-  ROUND((k), _y, _x,  9);                                              \
-  ROUND((k), _x, _y, 10);                                              \
-  ROUND((k), _y, _x, 11);                                              \
-  ROUND((k), _x, _y, 12);                                              \
-  ROUND((k), _y, _x, 13);                                              \
-  ROUND((k), _x, _y, 14);                                              \
-  ROUND((k), _y, _x, 15);                                              \
-  (c) = _y ^ (k)->p[17];                                               \
-  (d) = _x ^ (k)->p[16];                                               \
+  const uint32 *_r = k->p;                                             \
+  uint32 _x = a;                                                       \
+  uint32 _y = b;                                                       \
+  ROUND(k, _x, _y, _r++);                                              \
+  ROUND(k, _y, _x, _r++);                                              \
+  ROUND(k, _x, _y, _r++);                                              \
+  ROUND(k, _y, _x, _r++);                                              \
+  ROUND(k, _x, _y, _r++);                                              \
+  ROUND(k, _y, _x, _r++);                                              \
+  ROUND(k, _x, _y, _r++);                                              \
+  ROUND(k, _y, _x, _r++);                                              \
+  ROUND(k, _x, _y, _r++);                                              \
+  ROUND(k, _y, _x, _r++);                                              \
+  ROUND(k, _x, _y, _r++);                                              \
+  ROUND(k, _y, _x, _r++);                                              \
+  ROUND(k, _x, _y, _r++);                                              \
+  ROUND(k, _y, _x, _r++);                                              \
+  ROUND(k, _x, _y, _r++);                                              \
+  ROUND(k, _y, _x, _r++);                                              \
+  c = _y ^ k->p[17];                                                   \
+  d = _x ^ k->p[16];                                                   \
 } while (0)
 
 #define DBLK(k, a, b, c, d) do {                                       \
-  uint32 _x = (a);                                                     \
-  uint32 _y = (b);                                                     \
-  ROUND((k), _x, _y, 17);                                              \
-  ROUND((k), _y, _x, 16);                                              \
-  ROUND((k), _x, _y, 15);                                              \
-  ROUND((k), _y, _x, 14);                                              \
-  ROUND((k), _x, _y, 13);                                              \
-  ROUND((k), _y, _x, 12);                                              \
-  ROUND((k), _x, _y, 11);                                              \
-  ROUND((k), _y, _x, 10);                                              \
-  ROUND((k), _x, _y,  9);                                              \
-  ROUND((k), _y, _x,  8);                                              \
-  ROUND((k), _x, _y,  7);                                              \
-  ROUND((k), _y, _x,  6);                                              \
-  ROUND((k), _x, _y,  5);                                              \
-  ROUND((k), _y, _x,  4);                                              \
-  ROUND((k), _x, _y,  3);                                              \
-  ROUND((k), _y, _x,  2);                                              \
-  (c) = _y ^ (k)->p[0];                                                        \
-  (d) = _x ^ (k)->p[1];                                                        \
+  const uint32 *_r = k->p + 18;                                                \
+  uint32 _x = a;                                                       \
+  uint32 _y = b;                                                       \
+  ROUND(k, _x, _y, --_r);                                              \
+  ROUND(k, _y, _x, --_r);                                              \
+  ROUND(k, _x, _y, --_r);                                              \
+  ROUND(k, _y, _x, --_r);                                              \
+  ROUND(k, _x, _y, --_r);                                              \
+  ROUND(k, _y, _x, --_r);                                              \
+  ROUND(k, _x, _y, --_r);                                              \
+  ROUND(k, _y, _x, --_r);                                              \
+  ROUND(k, _x, _y, --_r);                                              \
+  ROUND(k, _y, _x, --_r);                                              \
+  ROUND(k, _x, _y, --_r);                                              \
+  ROUND(k, _y, _x, --_r);                                              \
+  ROUND(k, _x, _y, --_r);                                              \
+  ROUND(k, _y, _x, --_r);                                              \
+  ROUND(k, _x, _y, --_r);                                              \
+  ROUND(k, _y, _x, --_r);                                              \
+  c = _y ^ k->p[0];                                                    \
+  d = _x ^ k->p[1];                                                    \
 } while (0)
 
 /*----- Low-level encryption interface ------------------------------------*/
@@ -119,6 +117,8 @@ static blowfish_ctx ikey = BLOWFISH_IKEY;
 
 void blowfish_init(blowfish_ctx *k, const void *buf, size_t sz)
 {
+  KSZ_ASSERT(blowfish, sz);
+
   /* --- Copy the initial value over --- */
 
   memcpy(k, &ikey, sizeof(ikey));