Commit 33b4f40a authored by Pavel Tvrdík's avatar Pavel Tvrdík Committed by Ondřej Zajíček

MD5: Mormalize naming style

parent 90f78507
This diff is collapsed.
#ifndef MD5_H /*
#define MD5_H * BIRD Library -- MD5 Hash Function and HMAC-MD5 Function
*
* (c) 2015 CZ.NIC z.s.p.o.
*
* Adapted for BIRD by Martin Mares <mj@ucw.cz>
*
* Can be freely distributed and used under the terms of the GNU GPL.
*/
struct MD5Context { #ifndef _BIRD_MD5_H_
u32 buf[4]; #define _BIRD_MD5_H_
u32 bits[2];
unsigned char in[64]; #include "nest/bird.h"
#define MD5_SIZE 16
#define MD5_HEX_SIZE 33
#define MD5_BLOCK_SIZE 64
struct md5_context {
u32 buf[4];
u32 bits[2];
byte in[64];
}; };
void MD5Init(struct MD5Context *context); void md5_init(struct md5_context *ctx);
void MD5Update(struct MD5Context *context, unsigned char const *buf, void md5_update(struct md5_context *ctx, const byte *buf, uint len);
unsigned len); byte *md5_final(struct md5_context *ctx);
void MD5Final(unsigned char digest[16], struct MD5Context *context);
void MD5Transform(u32 buf[4], u32 const in[16]);
/*
* HMAC-MD5
*/
struct md5_hmac_context {
struct md5_context ictx;
struct md5_context octx;
};
void md5_hmac_init(struct md5_hmac_context *ctx, const byte *key, size_t keylen);
void md5_hmac_update(struct md5_hmac_context *ctx, const byte *buf, size_t buflen);
byte *md5_hmac_final(struct md5_hmac_context *ctx);
#endif /* !MD5_H */ #endif /* _BIRD_MD5_H_ */
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "ospf.h" #include "ospf.h"
#include "nest/password.h" #include "nest/password.h"
#include "lib/md5.h" #include "lib/md5.h"
#include "lib/socket.h"
void void
ospf_pkt_fill_hdr(struct ospf_iface *ifa, void *buf, u8 h_type) 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) ...@@ -108,11 +109,11 @@ ospf_pkt_finalize(struct ospf_iface *ifa, struct ospf_packet *pkt)
char password[OSPF_AUTH_CRYPT_SIZE]; char password[OSPF_AUTH_CRYPT_SIZE];
strncpy(password, passwd->password, sizeof(password)); strncpy(password, passwd->password, sizeof(password));
struct MD5Context ctxt; struct md5_context ctx;
MD5Init(&ctxt); md5_init(&ctx);
MD5Update(&ctxt, (char *) pkt, plen); md5_update(&ctx, (char *) pkt, plen);
MD5Update(&ctxt, password, OSPF_AUTH_CRYPT_SIZE); md5_update(&ctx, password, OSPF_AUTH_CRYPT_SIZE);
MD5Final(tail, &ctxt); memcpy((byte *) tail, md5_final(&ctx), MD5_SIZE);
break; break;
default: default:
...@@ -174,19 +175,17 @@ ospf_pkt_checkauth(struct ospf_neighbor *n, struct ospf_iface *ifa, struct ospf_ ...@@ -174,19 +175,17 @@ ospf_pkt_checkauth(struct ospf_neighbor *n, struct ospf_iface *ifa, struct ospf_
if (!pass) if (!pass)
DROP("no suitable password found", auth->md5.keyid); DROP("no suitable password found", auth->md5.keyid);
void *tail = ((void *) pkt) + plen; byte *tail = ((byte *) pkt) + plen;
char passwd[OSPF_AUTH_CRYPT_SIZE]; char received[OSPF_AUTH_CRYPT_SIZE];
char md5sum[OSPF_AUTH_CRYPT_SIZE]; memcpy(received, tail, OSPF_AUTH_CRYPT_SIZE);
strncpy(tail, pass->password, OSPF_AUTH_CRYPT_SIZE);
strncpy(passwd, pass->password, OSPF_AUTH_CRYPT_SIZE);
struct MD5Context ctxt; struct md5_context ctx;
MD5Init(&ctxt); md5_init(&ctx);
MD5Update(&ctxt, (char *) pkt, plen); md5_update(&ctx, (byte *) pkt, plen + OSPF_AUTH_CRYPT_SIZE);
MD5Update(&ctxt, passwd, OSPF_AUTH_CRYPT_SIZE); char *computed = md5_final(&ctx);
MD5Final(md5sum, &ctxt);
if (memcmp(md5sum, tail, OSPF_AUTH_CRYPT_SIZE)) if (memcmp(received, computed, OSPF_AUTH_CRYPT_SIZE))
DROP("wrong MD5 digest", pass->id); DROP("wrong MD5 digest", pass->id);
if (n) if (n)
......
...@@ -241,10 +241,10 @@ rip_fill_authentication(struct rip_proto *p, struct rip_iface *ifa, struct rip_p ...@@ -241,10 +241,10 @@ rip_fill_authentication(struct rip_proto *p, struct rip_iface *ifa, struct rip_p
*plen += sizeof(struct rip_auth_tail) + RIP_MD5_LENGTH; *plen += sizeof(struct rip_auth_tail) + RIP_MD5_LENGTH;
struct MD5Context ctxt; struct md5_context ctx;
MD5Init(&ctxt); md5_init(&ctx);
MD5Update(&ctxt, (byte *) pkt, *plen); md5_update(&ctx, (byte *) pkt, *plen);
MD5Final(tail->auth_data, &ctxt); memcpy(tail->auth_data, md5_final(&ctx), RIP_MD5_LENGTH);
return; return;
default: default:
...@@ -312,15 +312,13 @@ rip_check_authentication(struct rip_proto *p, struct rip_iface *ifa, struct rip_ ...@@ -312,15 +312,13 @@ rip_check_authentication(struct rip_proto *p, struct rip_iface *ifa, struct rip_
} }
char received[RIP_MD5_LENGTH]; char received[RIP_MD5_LENGTH];
char computed[RIP_MD5_LENGTH];
memcpy(received, tail->auth_data, RIP_MD5_LENGTH); memcpy(received, tail->auth_data, RIP_MD5_LENGTH);
strncpy(tail->auth_data, pass->password, RIP_MD5_LENGTH); strncpy(tail->auth_data, pass->password, RIP_MD5_LENGTH);
struct MD5Context ctxt; struct md5_context ctx;
MD5Init(&ctxt); md5_init(&ctx);
MD5Update(&ctxt, (byte *) pkt, *plen); md5_update(&ctx, (byte *) pkt, *plen);
MD5Final(computed, &ctxt); char *computed = md5_final(&ctx);
if (memcmp(received, computed, RIP_MD5_LENGTH)) if (memcmp(received, computed, RIP_MD5_LENGTH))
DROP("wrong MD5 digest", pass->id); DROP("wrong MD5 digest", pass->id);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment