serpent: Ad-hoc debugging facility
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 25 Jul 2013 17:30:48 +0000 (18:30 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Thu, 25 Jul 2013 17:30:48 +0000 (18:30 +0100)
Provide an ad-hoc debugging facility in serpent.c.

If the "#if 0" is changed to "#if 1", the key material, plaintext and
ciphertext of all Serpent operations is printed in hex to stderr.

We provide a new header file hexdebug.h to facilitate this.  And we
use this new header file in the "#if 0" debugging in transform_setkey.

No functional change.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
hexdebug.h [new file with mode: 0644]
serpent.c
transform.c

diff --git a/hexdebug.h b/hexdebug.h
new file mode 100644 (file)
index 0000000..4139f72
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef HEXDEBUG_H
+#define HEXDEBUG_H
+
+#include <stdio.h>
+#include <sys/types.h>
+
+static inline void hexdebug(FILE *file, const void *buffer, size_t len)
+{
+    const uint8_t *array=buffer;
+    size_t i;
+    for (i=0; i<len; i++)
+       fprintf(file,"%02x",array[i]);
+}
+
+#endif /*HEXDEBUG_H*/
index 6407c57..7c58505 100644 (file)
--- a/serpent.c
+++ b/serpent.c
@@ -22,6 +22,7 @@
 
 #include <stdint.h>
 
+#include "hexdebug.h"
 #include "serpent.h"
 #include "serpentsboxes.h"
 
 
 #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)
 {
@@ -65,6 +86,8 @@ void SERPENT_DECORATE(makekey)(struct keyInstance *key, int keyLen,
     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)
@@ -125,6 +148,8 @@ void SERPENT_DECORATE(encrypt)(struct keyInstance *key,
     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);
@@ -234,6 +259,8 @@ void SERPENT_DECORATE(encrypt)(struct keyInstance *key,
     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_DECORATE(decrypt)(struct keyInstance *key,
@@ -243,6 +270,8 @@ void SERPENT_DECORATE(decrypt)(struct keyInstance *key,
     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);
@@ -352,4 +381,6 @@ void SERPENT_DECORATE(decrypt)(struct keyInstance *key,
     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");
 }
index a0665ad..281e667 100644 (file)
@@ -13,6 +13,7 @@
 #include "util.h"
 #include "serpent.h"
 #include "unaligned.h"
+#include "hexdebug.h"
 
 /* Required key length in bytes */
 #define REQUIRED_KEYLEN ((512+64+32)/8)
@@ -49,10 +50,8 @@ static bool_t transform_setkey(void *sst, uint8_t *key, int32_t keylen)
 
 #if 0
     {
-       int i;
        printf("Setting key to: ");
-       for (i=0; i<keylen; i++)
-           printf("%02x",key[i]);
+       hexdebug(stdout,key,keylen);
        printf("\n");
     }
 #endif /* 0 */