/*
- * This file is
- * Copyright (C) 1998 Ross Anderson, Eli Biham, Lars Knudsen
+ * serpent.c: Implementation of the Serpent block cipher
+ */
+/*
+ * This file is Free Software. It has been modified to as part of its
+ * incorporation into secnet.
+ *
+ * Copyright 1998 Ross Anderson, Eli Biham, Lars Knudsen
+ * Copyright 1995-2001 Stephen Early <steve@greenend.org.uk>
+ * Copyright 2011-2013 Ian Jackson
+ *
+ * For more information about Serpent see
+ * http://www.cl.cam.ac.uk/users/rja14/serpent.html
*
- * For more information see http://www.cl.cam.ac.uk/users/rja14/serpent.html
+ * 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 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 2, 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, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * You may redistribute this file and/or modify it under the terms of
+ * the GNU General Public License as published by the Free Software
+ * Foundation; either version 2, or (at your option) any later
+ * version.
*
+ * This software 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 software; if not, see
+ * https://www.gnu.org/licenses/gpl.html.
*/
#include <stdint.h>
+#include "hexdebug.h"
#include "serpent.h"
#include "serpentsboxes.h"
+#ifdef SERPENT_BIGENDIAN
+
#define GETPUT_CP(bytenum) \
(((basep) + (lenbytes) - (offset) - 4)[(bytenum)])
+#define SERPENT_DECORATE(func) serpentbe_##func
+
+#else /* !defined(SERPENT_BIGENDIAN) */
+
+#define GETPUT_CP(bytenum) \
+ (((basep) + (offset))[3-(bytenum)])
+
+#define SERPENT_DECORATE(func) serpent_##func
+
+#endif /* !defined(SERPENT_BIGENDIAN) */
+
+#if 0
+
+#include <stdio.h>
+
+static void SERP_DEBUG(const char *str1,
+ const void *ary, int sz,
+ const char *str2)
+{
+ fprintf(stderr,"%s",str1);
+ hexdebug(stderr,ary,sz);
+ fprintf(stderr,"%s",str2);
+}
+
+#else
+
+#define SERP_DEBUG(str1,aryv,sz,str2) /*empty*/
+
+#endif
+
+
static uint32_t serpent_get_32bit(const uint8_t *basep,
int lenbytes, int offset)
{
GETPUT_CP(3) = (char)(value);
}
-void serpent_makekey(struct keyInstance *key, int keyLen,
+void SERPENT_DECORATE(makekey)(struct keyInstance *key, int keyLen,
const uint8_t *keyMaterial)
{
int i;
uint32_t j;
uint32_t w[132],k[132];
+ SERP_DEBUG("SERPENT makekey ",keyMaterial,keyLen/8,"\n");
+
for(i=0; i<keyLen/32; i++)
w[i]=serpent_get_32bit(keyMaterial, keyLen/8, i*4);
if(keyLen<256)
key->subkeys[i][j] = k[4*i+j];
}
-void serpent_encrypt(struct keyInstance *key,
+void SERPENT_DECORATE(encrypt)(struct keyInstance *key,
const uint8_t plaintext[16],
uint8_t ciphertext[16])
{
register uint32_t x0, x1, x2, x3;
register uint32_t y0, y1, y2, y3;
+ SERP_DEBUG("SERPENT encrypt ",plaintext,16," ->");
+
x0=serpent_get_32bit(plaintext,16,+0);
x1=serpent_get_32bit(plaintext,16,+4);
x2=serpent_get_32bit(plaintext,16,+8);
serpent_put_32bit(ciphertext,16,+4, x1);
serpent_put_32bit(ciphertext,16,+8, x2);
serpent_put_32bit(ciphertext,16,12, x3);
+
+ SERP_DEBUG(" ",ciphertext,16,"\n");
}
-void serpent_decrypt(struct keyInstance *key,
+void SERPENT_DECORATE(decrypt)(struct keyInstance *key,
const uint8_t ciphertext[16],
uint8_t plaintext[16])
{
register uint32_t x0, x1, x2, x3;
register uint32_t y0, y1, y2, y3;
+ SERP_DEBUG("SERPENT decrypt ",ciphertext,16," ->");
+
x0=serpent_get_32bit(ciphertext,16,+0);
x1=serpent_get_32bit(ciphertext,16,+4);
x2=serpent_get_32bit(ciphertext,16,+8);
serpent_put_32bit(plaintext,16,+4, x1);
serpent_put_32bit(plaintext,16,+8, x2);
serpent_put_32bit(plaintext,16,12, x3);
+
+ SERP_DEBUG(" ",plaintext,16,"\n");
}