changeset 215:f04743489c9e

Add endian converting th_md5_append_{he,le,be}{16,32,64}() functions.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 12 Feb 2016 18:17:44 +0200
parents 817a65d7af1e
children 4fee16cb28c1
files th_crypto.c th_crypto.h
diffstat 2 files changed, 50 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/th_crypto.c	Fri Feb 12 18:17:11 2016 +0200
+++ b/th_crypto.c	Fri Feb 12 18:17:44 2016 +0200
@@ -8,6 +8,7 @@
  * This code is in the public domain; do with it what you wish.
  */
 #include "th_crypto.h"
+#include "th_endian.h"
 #include "th_util.h"
 
 
@@ -28,8 +29,8 @@
 #endif
 
 
-/* Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
- * initialization constants.
+/* Start MD5 accumulation.  Set bit count to 0 and buffer
+ * to mysterious initialization constants.
  */
 void th_md5_init(th_md5state_t *ctx)
 {
@@ -44,8 +45,8 @@
 
 
 /* The core of the MD5 algorithm, this alters an existing MD5 hash to
- * reflect the addition of 16 longwords of new data.  th_md5_update blocks
- * the data and converts bytes into longwords for this routine.
+ * reflect the addition of 16 longwords of new data. th_md5_update()
+ * blocks the data and converts bytes into longwords for this routine.
  */
 #define F1(x, y, z) (z ^ (x & (y ^ z)))
 #define F2(x, y, z) F1(z, x, y)
@@ -239,3 +240,34 @@
     for (i = 0; i < TH_MD5HASH_LENGTH; i++)
         fprintf(fp, "%02x", digest[i]);
 }
+
+
+void th_md5_append_uint8(th_md5state_t *ctx, uint8_t val)
+{
+    th_md5_append(ctx, &val, 1);
+}
+
+
+#define TH_DEFINE_FUNCS(xname, xtype) \
+void th_md5_append_ ## xname ## 16 (th_md5state_t *ctx, uint16_t pval) \
+{ \
+    uint16_t val = TH_NATIVE_TO_ ## xtype ## 16 (pval); \
+    th_md5_append(ctx, (uint8_t *) &val, sizeof(val)); \
+} \
+\
+void th_md5_append_ ## xname ## 32 (th_md5state_t *ctx, uint32_t pval) \
+{ \
+    uint32_t val = TH_NATIVE_TO_ ## xtype ## 32 (pval); \
+    th_md5_append(ctx, (uint8_t *) &val, sizeof(val)); \
+} \
+\
+void th_md5_append_ ## xname ## 64 (th_md5state_t *ctx, uint64_t pval) \
+{ \
+    uint64_t val = TH_NATIVE_TO_ ## xtype ## 64 (pval); \
+    th_md5_append(ctx, (uint8_t *) &val, sizeof(val)); \
+}
+
+
+TH_DEFINE_FUNCS(he, HE)
+TH_DEFINE_FUNCS(le, LE)
+TH_DEFINE_FUNCS(be, BE)
--- a/th_crypto.h	Fri Feb 12 18:17:11 2016 +0200
+++ b/th_crypto.h	Fri Feb 12 18:17:44 2016 +0200
@@ -40,6 +40,20 @@
 void    th_md5_finish(th_md5state_t *ctx, th_md5hash_t digest);
 void    th_md5_print(FILE *, const th_md5hash_t digest);
 
+void    th_md5_append_uint8(th_md5state_t *ctx, uint8_t val);
+
+#define TH_DEFINE_HEADER(xname) \
+void    th_md5_append_ ## xname ## 16 (th_md5state_t *ctx, uint16_t val); \
+void    th_md5_append_ ## xname ## 32 (th_md5state_t *ctx, uint32_t val); \
+void    th_md5_append_ ## xname ## 64 (th_md5state_t *ctx, uint64_t val);
+
+TH_DEFINE_HEADER(he)
+TH_DEFINE_HEADER(le)
+TH_DEFINE_HEADER(be)
+
+#undef TH_DEFINE_HEADER
+
+
 #ifdef __cplusplus
 }
 #endif