]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
MD5: change interface of md5_final()
authorPavel Tvrdík <pawel.tvrdik@gmail.cz>
Wed, 13 May 2015 08:55:02 +0000 (10:55 +0200)
committerPavel Tvrdík <pawel.tvrdik@gmail.cz>
Wed, 13 May 2015 09:18:10 +0000 (11:18 +0200)
originally:
  void md5_final(unsigned char digest[16], struct md5_context *ctx);

newly:
  byte * md5_final(md5_context *ctx);

conf/conf.h
lib/md5.c
lib/md5.h
lib/md5_test.c
lib/resource.h
proto/ospf/packet.c
proto/rip/auth.c

index 6ab53e25c3205eeb5c609875c6c6ba320241a991..f4933c38666a2f9ca849c517ade2a52dc746c4ac 100644 (file)
@@ -9,6 +9,8 @@
 #ifndef _BIRD_CONF_H_
 #define _BIRD_CONF_H_
 
+#include "sysdep/config.h"
+#include "lib/ip.h"
 #include "lib/resource.h"
 #include "lib/timer.h"
 
index 25f39b0b211bd9cef84b2f56f83ce3e93941d8c1..e00f92a4634f4384f7987784e0b4f372665ae614 100644 (file)
--- a/lib/md5.c
+++ b/lib/md5.c
@@ -38,7 +38,7 @@ void byteReverse(unsigned char *buf, unsigned longs)
  * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
  * initialization constants.
  */
-void md5_init(struct md5_context *ctx)
+void md5_init(md5_context *ctx)
 {
     ctx->buf[0] = 0x67452301;
     ctx->buf[1] = 0xefcdab89;
@@ -53,7 +53,7 @@ void md5_init(struct md5_context *ctx)
  * Update context to reflect the concatenation of another buffer full
  * of bytes.
  */
-void md5_update(struct md5_context *ctx, unsigned char const *buf, unsigned len)
+void md5_update(md5_context *ctx, unsigned char const *buf, unsigned len)
 {
     u32 t;
 
@@ -101,7 +101,7 @@ void md5_update(struct md5_context *ctx, unsigned char const *buf, unsigned len)
  * Final wrapup - pad to 64-byte boundary with the bit pattern 
  * 1 0* (64-bit count of bits processed, MSB-first)
  */
-void md5_final(unsigned char digest[16], struct md5_context *ctx)
+byte *md5_final(md5_context *ctx)
 {
     unsigned count;
     unsigned char *p;
@@ -138,8 +138,14 @@ void md5_final(unsigned char digest[16], struct md5_context *ctx)
 
     md5_transform(ctx->buf, (u32 *) ctx->in);
     byteReverse((unsigned char *) ctx->buf, 4);
-    memcpy(digest, ctx->buf, 16);
-    memset((char *) ctx, 0, sizeof(ctx));      /* In case it's sensitive */
+
+    return (byte*) ctx->buf;
+}
+
+/* I am a hard paranoid */
+void md5_erase_ctx(md5_context *ctx)
+{
+  memset((char *) ctx, 0, sizeof(*ctx));       /* In case it's sensitive */
 }
 
 /* The four core functions - F1 is optimized somewhat */
index 0553fe90209ca48db0217dedd5a364a3ba705990..ef3ebdfc5b546ef1966e6a729311de72afa4b70d 100644 (file)
--- a/lib/md5.h
+++ b/lib/md5.h
 #define MD5_HEX_SIZE   33
 #define MD5_BLOCK_SIZE 64
 
-struct md5_context {
+typedef struct
+{
   u32 buf[4];
   u32 bits[2];
   unsigned char in[64];
 } md5_context;
 
-void md5_init(struct md5_context *context);
-void md5_update(struct md5_context *context, unsigned char const *buf, unsigned len);
-void md5_final(unsigned char digest[16], struct md5_context *context);
+void md5_init(md5_context *context);
+void md5_update(md5_context *context, unsigned char const *buf, unsigned len);
+byte *md5_final(md5_context *context);
+
 void md5_transform(u32 buf[4], u32 const in[16]);
 
 
index 1528424d4e15d3c20285996f048f49463caaba07..83ce5000f9673ecb70aeb48498b4e8511c982f92 100644 (file)
 static void
 get_md5(const char *str, char (*out_hash)[MD5_HEX_SIZE])
 {
-  unsigned char hash[MD5_SIZE];
-  struct md5_context ctxt;
+  md5_context ctxt;
 
   md5_init(&ctxt);
   md5_update(&ctxt, str, strlen(str));
-  md5_final(hash, &ctxt);
+  byte *hash = md5_final(&ctxt);
 
   int i;
   for(i = 0; i < MD5_SIZE; i++)
index 1a62d3891b5a0297330d0f94198da5443b7be283..bfa77c93f7031c05483fd3e973b1f31487b60340 100644 (file)
@@ -9,6 +9,7 @@
 #ifndef _BIRD_RESOURCE_H_
 #define _BIRD_RESOURCE_H_
 
+#include <stddef.h>
 #include "lib/lists.h"
 
 /* Resource */
index c1a2ca5df146820a0d454aedb72c5b682c8e24bc..a1dd245406602c8d0a66614c2244af28d40bd12c 100644 (file)
@@ -11,6 +11,7 @@
 #include "ospf.h"
 #include "nest/password.h"
 #include "lib/md5.h"
+#include "lib/socket.h"
 
 void
 ospf_pkt_fill_hdr(struct ospf_iface *ifa, void *buf, u8 h_type)
@@ -108,11 +109,11 @@ ospf_pkt_finalize(struct ospf_iface *ifa, struct ospf_packet *pkt)
     char password[OSPF_AUTH_CRYPT_SIZE];
     strncpy(password, passwd->password, sizeof(password));
 
-    struct md5_context ctxt;
+    md5_context ctxt;
     md5_init(&ctxt);
     md5_update(&ctxt, (char *) pkt, plen);
     md5_update(&ctxt, password, OSPF_AUTH_CRYPT_SIZE);
-    md5_final(tail, &ctxt);
+    memcpy((byte *) tail, md5_final(&ctxt), MD5_SIZE);
     break;
 
   default:
@@ -180,11 +181,11 @@ ospf_pkt_checkauth(struct ospf_neighbor *n, struct ospf_iface *ifa, struct ospf_
 
     strncpy(passwd, pass->password, OSPF_AUTH_CRYPT_SIZE);
 
-    struct md5_context ctxt;
+    md5_context ctxt;
     md5_init(&ctxt);
     md5_update(&ctxt, (char *) pkt, plen);
     md5_update(&ctxt, passwd, OSPF_AUTH_CRYPT_SIZE);
-    md5_final(md5sum, &ctxt);
+    memcpy(md5sum, md5_final(&ctxt), MD5_SIZE);
 
     if (memcmp(md5sum, tail, OSPF_AUTH_CRYPT_SIZE))
       DROP("wrong MD5 digest", pass->id);
index 7262475855c34ca4032d9597daa5b45948e51bd8..ba05cdd53413fb41d6009ff63aa88ac3b9d5f4f9 100644 (file)
@@ -57,9 +57,9 @@ rip_incoming_authentication( struct proto *p, struct rip_block_auth *block, stru
     {
       struct password_item *pass = NULL, *ptmp;
       struct rip_md5_tail *tail;
-      struct md5_context ctxt;
+      md5_context ctxt;
       char md5sum_packet[16];
-      char md5sum_computed[16];
+      char *md5sum_computed;
       struct neighbor *neigh = neigh_find(p, &whotoldme, 0);
       list *l = P_CF->passwords;
 
@@ -99,7 +99,7 @@ rip_incoming_authentication( struct proto *p, struct rip_block_auth *block, stru
 
       md5_init(&ctxt);
       md5_update(&ctxt, (char *) packet, ntohs(block->packetlen) +  sizeof(struct rip_block_auth) );
-      md5_final(md5sum_computed, &ctxt);
+      md5sum_computed = md5_final(&ctxt);
       if (memcmp(md5sum_packet, md5sum_computed, 16))
         return 1;
     }
@@ -136,7 +136,7 @@ rip_outgoing_authentication( struct proto *p, struct rip_block_auth *block, stru
   case AT_MD5:
     {
       struct rip_md5_tail *tail;
-      struct md5_context ctxt;
+      md5_context ctxt;
       static u32 sequence = 0;
 
       if (num > PACKET_MD5_MAX)
@@ -159,7 +159,7 @@ rip_outgoing_authentication( struct proto *p, struct rip_block_auth *block, stru
       strncpy(tail->md5, passwd->password, 16);
       md5_init(&ctxt);
       md5_update(&ctxt, (char *) packet, PACKETLEN(num) + sizeof(struct  rip_md5_tail));
-      md5_final(tail->md5, &ctxt);
+      memcpy(tail->md5, md5_final(&ctxt), MD5_SIZE);
       return PACKETLEN(num) + block->authlen;
     }
   default: