Commit 16738140 authored by Pavel Tvrdík's avatar Pavel Tvrdík
Browse files

MD5, SHA1/256/512 libraries: fixing code style

parent ddb80bd8
Loading
Loading
Loading
Loading
+19 −16
Original line number Diff line number Diff line
/*
 *	BIRD -- MD5 Hash Function and HMAC-MD5 Function
 *	BIRD Library -- MD5 Hash Function and HMAC-MD5 Function
 *
 *	(c) 2015 CZ.NIC z.s.p.o.
 *
@@ -34,12 +34,14 @@ void byteReverse(byte *buf, uint longs)
}
#endif

static void md5_transform(u32 buf[4], u32 const in[16]);

/*
 * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
 * initialization constants.
 */
void
md5_init(md5_context *ctx)
md5_init(struct md5_context *ctx)
{
  ctx->buf[0] = 0x67452301;
  ctx->buf[1] = 0xefcdab89;
@@ -55,7 +57,7 @@ md5_init(md5_context *ctx)
 * of bytes.
 */
void
md5_update(md5_context *ctx, byte const *buf, uint len)
md5_update(struct md5_context *ctx, byte const *buf, uint len)
{
  u32 t;

@@ -107,7 +109,7 @@ md5_update(md5_context *ctx, byte const *buf, uint len)
 * 1 0* (64-bit count of bits processed, MSB-first)
 */
byte *
md5_final(md5_context *ctx)
md5_final(struct md5_context *ctx)
{
  uint count;
  byte *p;
@@ -153,7 +155,7 @@ md5_final(md5_context *ctx)

/* I am a hard paranoid */
void
md5_erase_ctx(md5_context *ctx)
md5_erase_ctx(struct md5_context *ctx)
{
  memset((char *) ctx, 0, sizeof(*ctx));	/* In case it's sensitive */
}
@@ -259,14 +261,15 @@ md5_transform(u32 buf[4], u32 const in[16])
  buf[3] += d;
}

/**

/*
 * 	MD5-HMAC
 */

static void
md5_hash_buffer(byte *outbuf, const byte *buffer, size_t length)
{
  md5_context hd_tmp;
  struct md5_context hd_tmp;

  md5_init(&hd_tmp);
  md5_update(&hd_tmp, buffer, length);
@@ -274,11 +277,11 @@ md5_hash_buffer(byte *outbuf, const byte *buffer, size_t length)
}

void
md5_hmac_init(md5_hmac_context *ctx, const byte *key, size_t keylen)
md5_hmac_init(struct md5_hmac_context *ctx, const byte *key, size_t keylen)
{
  byte keybuf[MD5_BLOCK_SIZE], buf[MD5_BLOCK_SIZE];

  // Hash the key if necessary
  /* Hash the key if necessary */
  if (keylen <= MD5_BLOCK_SIZE)
  {
    memcpy(keybuf, key, keylen);
@@ -290,14 +293,14 @@ md5_hmac_init(md5_hmac_context *ctx, const byte *key, size_t keylen)
    bzero(keybuf + MD5_SIZE, MD5_BLOCK_SIZE - MD5_SIZE);
  }

  // Initialize the inner digest
  /* Initialize the inner digest */
  md5_init(&ctx->ictx);
  int i;
  for (i = 0; i < MD5_BLOCK_SIZE; i++)
    buf[i] = keybuf[i] ^ 0x36;
  md5_update(&ctx->ictx, buf, MD5_BLOCK_SIZE);

  // Initialize the outer digest
  /* Initialize the outer digest */
  md5_init(&ctx->octx);
  for (i = 0; i < MD5_BLOCK_SIZE; i++)
    buf[i] = keybuf[i] ^ 0x5c;
@@ -305,19 +308,19 @@ md5_hmac_init(md5_hmac_context *ctx, const byte *key, size_t keylen)
}

void
md5_hmac_update(md5_hmac_context *ctx, const byte *buf, size_t buflen)
md5_hmac_update(struct md5_hmac_context *ctx, const byte *buf, size_t buflen)
{
  // Just update the inner digest
  /* Just update the inner digest */
  md5_update(&ctx->ictx, buf, buflen);
}

byte *
md5_hmac_final(md5_hmac_context *ctx)
md5_hmac_final(struct md5_hmac_context *ctx)
{
  // Finish the inner digest
  /* Finish the inner digest */
  byte *isha = md5_final(&ctx->ictx);

  // Finish the outer digest
  /* Finish the outer digest */
  md5_update(&ctx->octx, isha, MD5_SIZE);
  return md5_final(&ctx->octx);
}
+14 −16
Original line number Diff line number Diff line
/*
 *	BIRD -- MD5 Hash Function and HMAC-MD5 Function
 *	BIRD Library -- MD5 Hash Function and HMAC-MD5 Function
 *
 *	(c) 2015 CZ.NIC z.s.p.o.
 *
@@ -15,30 +15,28 @@
#define MD5_HEX_SIZE	33
#define MD5_BLOCK_SIZE	64

typedef struct
struct md5_context
{
  u32 buf[4];
  u32 bits[2];
  byte in[64];
} md5_context;
};

void md5_init(md5_context *context);
void md5_update(md5_context *context, byte const *buf, uint len);
byte *md5_final(md5_context *context);
void md5_init(struct md5_context *context);
void md5_update(struct md5_context *context, byte const *buf, uint len);
byte *md5_final(struct md5_context *context);

void md5_transform(u32 buf[4], u32 const in[16]);

/**
/*
 *	HMAC-MD5
 */
typedef struct
struct md5_hmac_context
{
  md5_context ictx;
  md5_context octx;
} md5_hmac_context;
  struct md5_context ictx;
  struct md5_context octx;
};

void md5_hmac_init(md5_hmac_context *ctx, const byte *key, size_t keylen);
void md5_hmac_update(md5_hmac_context *ctx, const byte *buf, size_t buflen);
byte *md5_hmac_final(md5_hmac_context *ctx);
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 /* _BIRD_MD5_H_ */
+3 −3
Original line number Diff line number Diff line
/*
 *	BIRD -- MD5 and HMAC-MD5 Tests
 *	BIRD Library -- MD5 and HMAC-MD5 Tests
 *
 *	(c) 2015 CZ.NIC z.s.p.o.
 *
@@ -16,7 +16,7 @@
static void
get_md5(const char *str, char (*out_hash)[MD5_HEX_SIZE])
{
  md5_context ctxt;
  struct md5_context ctxt;

  md5_init(&ctxt);
  md5_update(&ctxt, str, strlen(str));
@@ -80,7 +80,7 @@ struct hmac_data_in {
static void
get_md5_hmac(const struct hmac_data_in in, char (*out_hash)[MD5_HEX_SIZE])
{
  md5_hmac_context ctx;
  struct md5_hmac_context ctx;
  md5_hmac_init(&ctx, in.key, in.key_len);
  md5_hmac_update(&ctx, in.data, in.data_len);
  byte *hash_byte = md5_hmac_final(&ctx);
+19 −18
Original line number Diff line number Diff line
/*
 *	BIRD -- SHA-1 Hash Function (FIPS 180-1, RFC 3174) and HMAC-SHA-1
 *	BIRD Library -- SHA-1 Hash Function (FIPS 180-1, RFC 3174) and HMAC-SHA-1
 *
 *	(c) 2015 CZ.NIC z.s.p.o.
 *
@@ -19,7 +19,7 @@
#include "lib/unaligned.h"

void
sha1_init(sha1_context *hd)
sha1_init(struct sha1_context *hd)
{
  hd->h0 = 0x67452301;
  hd->h1 = 0xefcdab89;
@@ -34,7 +34,7 @@ sha1_init(sha1_context *hd)
 * Transform the message X which consists of 16 32-bit-words
 */
static void
sha1_transform(sha1_context *hd, const byte *data)
sha1_transform(struct sha1_context *hd, const byte *data)
{
  u32 a,b,c,d,e,tm;
  u32 x[16];
@@ -65,7 +65,7 @@ sha1_transform(sha1_context *hd, const byte *data)

#define M(i) (tm = x[i&0x0f] ^ x[(i-14)&0x0f] ^ x[(i-8)&0x0f] ^ x[(i-3)&0x0f], (x[i&0x0f] = ROL(tm, 1)))

/** Bitwise rotation of an unsigned int to the left **/
/* Bitwise rotation of an unsigned int to the left **/
#define	ROL(x, bits) (((x) << (bits)) | ((uint)(x) >> (sizeof(uint)*8 - (bits))))

  #define R(a, b, c, d, e, f, k, m)		\
@@ -169,7 +169,7 @@ sha1_transform(sha1_context *hd, const byte *data)
 * of INBUF with length INLEN.
 */
void
sha1_update(sha1_context *hd, const byte *inbuf, uint inlen)
sha1_update(struct sha1_context *hd, const byte *inbuf, uint inlen)
{
  if (hd->count == 64)  /* flush the buffer */
  {
@@ -209,7 +209,7 @@ sha1_update(sha1_context *hd, const byte *inbuf, uint inlen)
 * Returns: 20 bytes representing the digest.
 */
byte *
sha1_final(sha1_context *hd)
sha1_final(struct sha1_context *hd)
{
  u32 t, msb, lsb;
  u32 *p;
@@ -267,7 +267,8 @@ sha1_final(sha1_context *hd)
  return hd->buf;
}

/**

/*
 * 	SHA1-HMAC
 */

@@ -278,7 +279,7 @@ sha1_final(sha1_context *hd)
void
sha1_hash_buffer(byte *outbuf, const byte *buffer, uint length)
{
  sha1_context ctx;
  struct sha1_context ctx;

  sha1_init(&ctx);
  sha1_update(&ctx, buffer, length);
@@ -286,11 +287,11 @@ sha1_hash_buffer(byte *outbuf, const byte *buffer, uint length)
}

void
sha1_hmac_init(sha1_hmac_context *ctx, const byte *key, uint keylen)
sha1_hmac_init(struct sha1_hmac_context *ctx, const byte *key, uint keylen)
{
  byte keybuf[SHA1_BLOCK_SIZE], buf[SHA1_BLOCK_SIZE];

  // Hash the key if necessary
  /* Hash the key if necessary */
  if (keylen <= SHA1_BLOCK_SIZE)
  {
    memcpy(keybuf, key, keylen);
@@ -302,14 +303,14 @@ sha1_hmac_init(sha1_hmac_context *ctx, const byte *key, uint keylen)
    bzero(keybuf + SHA1_SIZE, SHA1_BLOCK_SIZE - SHA1_SIZE);
  }

  // Initialize the inner digest
  /* Initialize the inner digest */
  sha1_init(&ctx->ictx);
  int i;
  for (i = 0; i < SHA1_BLOCK_SIZE; i++)
    buf[i] = keybuf[i] ^ 0x36;
  sha1_update(&ctx->ictx, buf, SHA1_BLOCK_SIZE);

  // Initialize the outer digest
  /* Initialize the outer digest */
  sha1_init(&ctx->octx);
  for (i = 0; i < SHA1_BLOCK_SIZE; i++)
    buf[i] = keybuf[i] ^ 0x5c;
@@ -317,18 +318,18 @@ sha1_hmac_init(sha1_hmac_context *ctx, const byte *key, uint keylen)
}

void
sha1_hmac_update(sha1_hmac_context *ctx, const byte *data, uint datalen)
sha1_hmac_update(struct sha1_hmac_context *ctx, const byte *data, uint datalen)
{
  // Just update the inner digest
  /* Just update the inner digest */
  sha1_update(&ctx->ictx, data, datalen);
}

byte *sha1_hmac_final(sha1_hmac_context *ctx)
byte *sha1_hmac_final(struct sha1_hmac_context *ctx)
{
  // Finish the inner digest
  /* Finish the inner digest */
  byte *isha = sha1_final(&ctx->ictx);

  // Finish the outer digest
  /* Finish the outer digest */
  sha1_update(&ctx->octx, isha, SHA1_SIZE);
  return sha1_final(&ctx->octx);
}
@@ -336,7 +337,7 @@ byte *sha1_hmac_final(sha1_hmac_context *ctx)
void
sha1_hmac(byte *outbuf, const byte *key, uint keylen, const byte *data, uint datalen)
{
  sha1_hmac_context hd;
  struct sha1_hmac_context hd;
  sha1_hmac_init(&hd, key, keylen);
  sha1_hmac_update(&hd, data, datalen);
  byte *osha = sha1_hmac_final(&hd);
+22 −22
Original line number Diff line number Diff line
/*
 *	BIRD -- SHA-1 Hash Function (FIPS 180-1, RFC 3174) and HMAC-SHA-1
 *	BIRD Library -- SHA-1 Hash Function (FIPS 180-1, RFC 3174) and HMAC-SHA-1
 *
 *	(c) 2015 CZ.NIC z.s.p.o.
 *
@@ -17,27 +17,27 @@

#include "sysdep/config.h"

/**
/*
 * Internal SHA1 state.
 * You should use it just as an opaque handle only.
 */
typedef struct {
struct sha1_context {
  u32 h0,h1,h2,h3,h4;
  u32 nblocks;
  byte buf[64];
  int count;
} sha1_context;
} ;

void sha1_init(sha1_context *hd); /** Initialize new algorithm run in the @hd context. **/
/**
void sha1_init(struct sha1_context *hd); /* Initialize new algorithm run in the @hd context. **/
/*
 * Push another @inlen bytes of data pointed to by @inbuf onto the
 * SHA1 hash currently in @hd. You can call this any times you want on
 * the same hash (and you do not need to reinitialize it by
 * @sha1_init()). It has the same effect as concatenating all the data
 * together and passing them at once.
 */
void sha1_update(sha1_context *hd, const byte *inbuf, uint inlen);
/**
void sha1_update(struct sha1_context *hd, const byte *inbuf, uint inlen);
/*
 * No more @sha1_update() calls will be done. This terminates the hash
 * and returns a pointer to it.
 *
@@ -47,9 +47,9 @@ void sha1_update(sha1_context *hd, const byte *inbuf, uint inlen);
 * To convert the hash to its usual hexadecimal representation, see
 * <<string:mem_to_hex()>>.
 */
byte *sha1_final(sha1_context *hd);
byte *sha1_final(struct sha1_context *hd);

/**
/*
 * A convenience one-shot function for SHA1 hash.
 * It is equivalent to this snippet of code:
 *
@@ -60,27 +60,27 @@ byte *sha1_final(sha1_context *hd);
 */
void sha1_hash_buffer(byte *outbuf, const byte *buffer, uint length);

/**
/*
 * SHA1 HMAC message authentication. If you provide @key and @data,
 * the result will be stored in @outbuf.
 */
void sha1_hmac(byte *outbuf, const byte *key, uint keylen, const byte *data, uint datalen);

/**
/*
 * The HMAC also exists in a stream version in a way analogous to the
 * plain SHA1. Pass this as a context.
 */
typedef struct {
  sha1_context ictx;
  sha1_context octx;
} sha1_hmac_context;
struct sha1_hmac_context {
  struct sha1_context ictx;
  struct sha1_context octx;
};

void sha1_hmac_init(sha1_hmac_context *hd, const byte *key, uint keylen);	/** Initialize HMAC with context @hd and the given key. See sha1_init(). */
void sha1_hmac_update(sha1_hmac_context *hd, const byte *data, uint datalen);	/** Hash another @datalen bytes of data. See sha1_update(). */
byte *sha1_hmac_final(sha1_hmac_context *hd);					/** Terminate the HMAC and return a pointer to the allocated hash. See sha1_final(). */
void sha1_hmac_init(struct sha1_hmac_context *hd, const byte *key, uint keylen);	/* Initialize HMAC with context @hd and the given key. See sha1_init(). */
void sha1_hmac_update(struct sha1_hmac_context *hd, const byte *data, uint datalen);	/* Hash another @datalen bytes of data. See sha1_update(). */
byte *sha1_hmac_final(struct sha1_hmac_context *hd);					/* Terminate the HMAC and return a pointer to the allocated hash. See sha1_final(). */

#define SHA1_SIZE 20 		/** Size of the SHA1 hash in its binary representation **/
#define SHA1_HEX_SIZE 41 	/** Buffer length for a string containing SHA1 in hexadecimal format. **/
#define SHA1_BLOCK_SIZE 64 	/** SHA1 splits input to blocks of this size. **/
#define SHA1_SIZE 20 		/* Size of the SHA1 hash in its binary representation **/
#define SHA1_HEX_SIZE 41 	/* Buffer length for a string containing SHA1 in hexadecimal format. **/
#define SHA1_BLOCK_SIZE 64 	/* SHA1 splits input to blocks of this size. **/

#endif /* _BIRD_SHA1_H_ */
Loading