0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-23 02:01:55 +00:00

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

This commit is contained in:
Pavel Tvrdík 2015-05-19 09:57:10 +02:00
parent ddb80bd8c5
commit 1673814087
14 changed files with 233 additions and 226 deletions

View File

@ -1,5 +1,5 @@
/*
* 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);
}

View File

@ -1,5 +1,5 @@
/*
* 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_ */

View File

@ -1,5 +1,5 @@
/*
* 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);

View File

@ -1,5 +1,5 @@
/*
* 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);

View File

@ -1,5 +1,5 @@
/*
* 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_ */

View File

@ -1,5 +1,5 @@
/*
* BIRD -- SHA-1 and HMAC-SHA-1 Tests
* BIRD Library -- SHA-1 and HMAC-SHA-1 Tests
*
* (c) 2015 CZ.NIC z.s.p.o.
*
@ -16,7 +16,7 @@
static void
get_sha1(const char *str, char (*out_hash)[SHA1_HEX_SIZE])
{
sha1_context ctx;
struct sha1_context ctx;
sha1_init(&ctx);
sha1_update(&ctx, str, strlen(str));
byte *hash = sha1_final(&ctx);
@ -83,7 +83,7 @@ struct hmac_data_in {
static void
get_sha1_hmac(const struct hmac_data_in in, char (*out_hash)[SHA1_HEX_SIZE])
{
sha1_hmac_context ctx;
struct sha1_hmac_context ctx;
sha1_hmac_init(&ctx, in.key, in.key_len);
sha1_hmac_update(&ctx, in.data, in.data_len);
byte *hash_byte = sha1_hmac_final(&ctx);

View File

@ -1,6 +1,6 @@
/*
* BIRD -- SHA-256 and SHA-224 Hash Functions,
* HMAC-SHA-256 and HMAC-SHA-224 Functions
* BIRD Library -- SHA-256 and SHA-224 Hash Functions,
* HMAC-SHA-256 and HMAC-SHA-224 Functions
*
* (c) 2015 CZ.NIC z.s.p.o.
*
@ -18,8 +18,11 @@
#include "lib/sha256.h"
#include "lib/unaligned.h"
static uint sha256_transform(void *ctx, const byte *data, size_t nblks);
void
sha256_init(sha256_context *ctx)
sha256_init(struct sha256_context *ctx)
{
ctx->h0 = 0x6a09e667;
ctx->h1 = 0xbb67ae85;
@ -38,7 +41,7 @@ sha256_init(sha256_context *ctx)
}
void
sha224_init(sha224_context *ctx)
sha224_init(struct sha224_context *ctx)
{
ctx->h0 = 0xc1059ed8;
ctx->h1 = 0x367cd507;
@ -115,7 +118,7 @@ sum1(u32 x)
32-bit-words. See FIPS 180-2 for details.
*/
static uint
sha256_transform_block(sha256_context *ctx, const byte *data)
sha256_transform_block(struct sha256_context *ctx, const byte *data)
{
static const u32 K[64] = {
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
@ -217,7 +220,7 @@ sha256_transform_block(sha256_context *ctx, const byte *data)
static uint
sha256_transform(void *ctx, const byte *data, size_t nblks)
{
sha256_context *hd = ctx;
struct sha256_context *hd = ctx;
uint burn;
do
@ -237,7 +240,7 @@ sha256_transform(void *ctx, const byte *data, size_t nblks)
not have any meaning but writing after finalize is sometimes
helpful to mitigate timing attacks. */
void
sha256_update(sha256_context *ctx, const byte *in_buf, size_t in_len)
sha256_update(struct sha256_context *ctx, const byte *in_buf, size_t in_len)
{
const uint blocksize = ctx->blocksize;
size_t inblocks;
@ -284,7 +287,7 @@ sha256_update(sha256_context *ctx, const byte *in_buf, size_t in_len)
to the handle will the destroy the returned buffer. Returns: 32
bytes with the message the digest. */
byte*
sha256_final(sha256_context *ctx)
sha256_final(struct sha256_context *ctx)
{
u32 t, th, msb, lsb;
byte *p;
@ -345,14 +348,15 @@ sha256_final(sha256_context *ctx)
return ctx->buf;
}
/**
/*
* SHA256-HMAC
*/
static void
sha256_hash_buffer(byte *outbuf, const byte *buffer, size_t length)
{
sha256_context hd_tmp;
struct sha256_context hd_tmp;
sha256_init(&hd_tmp);
sha256_update(&hd_tmp, buffer, length);
@ -360,11 +364,11 @@ sha256_hash_buffer(byte *outbuf, const byte *buffer, size_t length)
}
void
sha256_hmac_init(sha256_hmac_context *ctx, const byte *key, size_t keylen)
sha256_hmac_init(struct sha256_hmac_context *ctx, const byte *key, size_t keylen)
{
byte keybuf[SHA256_BLOCK_SIZE], buf[SHA256_BLOCK_SIZE];
// Hash the key if necessary
/* Hash the key if necessary */
if (keylen <= SHA256_BLOCK_SIZE)
{
memcpy(keybuf, key, keylen);
@ -376,44 +380,45 @@ sha256_hmac_init(sha256_hmac_context *ctx, const byte *key, size_t keylen)
bzero(keybuf + SHA256_SIZE, SHA256_BLOCK_SIZE - SHA256_SIZE);
}
// Initialize the inner digest
/* Initialize the inner digest */
sha256_init(&ctx->ictx);
int i;
for (i = 0; i < SHA256_BLOCK_SIZE; i++)
buf[i] = keybuf[i] ^ 0x36;
sha256_update(&ctx->ictx, buf, SHA256_BLOCK_SIZE);
// Initialize the outer digest
/* Initialize the outer digest */
sha256_init(&ctx->octx);
for (i = 0; i < SHA256_BLOCK_SIZE; i++)
buf[i] = keybuf[i] ^ 0x5c;
sha256_update(&ctx->octx, buf, SHA256_BLOCK_SIZE);
}
void sha256_hmac_update(sha256_hmac_context *ctx, const byte *buf, size_t buflen)
void sha256_hmac_update(struct sha256_hmac_context *ctx, const byte *buf, size_t buflen)
{
// Just update the inner digest
/* Just update the inner digest */
sha256_update(&ctx->ictx, buf, buflen);
}
byte *sha256_hmac_final(sha256_hmac_context *ctx)
byte *sha256_hmac_final(struct sha256_hmac_context *ctx)
{
// Finish the inner digest
/* Finish the inner digest */
byte *isha = sha256_final(&ctx->ictx);
// Finish the outer digest
/* Finish the outer digest */
sha256_update(&ctx->octx, isha, SHA256_SIZE);
return sha256_final(&ctx->octx);
}
/**
/*
* SHA224-HMAC
*/
static void
sha224_hash_buffer(byte *outbuf, const byte *buffer, size_t length)
{
sha224_context hd_tmp;
struct sha224_context hd_tmp;
sha224_init(&hd_tmp);
sha224_update(&hd_tmp, buffer, length);
@ -421,11 +426,11 @@ sha224_hash_buffer(byte *outbuf, const byte *buffer, size_t length)
}
void
sha224_hmac_init(sha224_hmac_context *ctx, const byte *key, size_t keylen)
sha224_hmac_init(struct sha224_hmac_context *ctx, const byte *key, size_t keylen)
{
byte keybuf[SHA224_BLOCK_SIZE], buf[SHA224_BLOCK_SIZE];
// Hash the key if necessary
/* Hash the key if necessary */
if (keylen <= SHA224_BLOCK_SIZE)
{
memcpy(keybuf, key, keylen);
@ -437,32 +442,32 @@ sha224_hmac_init(sha224_hmac_context *ctx, const byte *key, size_t keylen)
bzero(keybuf + SHA224_SIZE, SHA224_BLOCK_SIZE - SHA224_SIZE);
}
// Initialize the inner digest
/* Initialize the inner digest */
sha224_init(&ctx->ictx);
int i;
for (i = 0; i < SHA224_BLOCK_SIZE; i++)
buf[i] = keybuf[i] ^ 0x36;
sha224_update(&ctx->ictx, buf, SHA224_BLOCK_SIZE);
// Initialize the outer digest
/* Initialize the outer digest */
sha224_init(&ctx->octx);
for (i = 0; i < SHA224_BLOCK_SIZE; i++)
buf[i] = keybuf[i] ^ 0x5c;
sha224_update(&ctx->octx, buf, SHA224_BLOCK_SIZE);
}
void sha224_hmac_update(sha224_hmac_context *ctx, const byte *buf, size_t buflen)
void sha224_hmac_update(struct sha224_hmac_context *ctx, const byte *buf, size_t buflen)
{
// Just update the inner digest
/* Just update the inner digest */
sha256_update(&ctx->ictx, buf, buflen);
}
byte *sha224_hmac_final(sha224_hmac_context *ctx)
byte *sha224_hmac_final(struct sha224_hmac_context *ctx)
{
// Finish the inner digest
/* Finish the inner digest */
byte *isha = sha224_final(&ctx->ictx);
// Finish the outer digest
/* Finish the outer digest */
sha224_update(&ctx->octx, isha, SHA224_SIZE);
return sha224_final(&ctx->octx);
}

View File

@ -1,6 +1,6 @@
/*
* BIRD -- SHA-256 and SHA-224 Hash Functions,
* HMAC-SHA-256 and HMAC-SHA-224 Functions
* BIRD Library -- SHA-256 and SHA-224 Hash Functions,
* HMAC-SHA-256 and HMAC-SHA-224 Functions
*
* (c) 2015 CZ.NIC z.s.p.o.
*
@ -25,7 +25,7 @@
typedef uint sha_transform_fn (void *c, const byte *blks, size_t nblks);
typedef struct {
struct sha256_context {
u32 h0,h1,h2,h3,h4,h5,h6,h7;
byte buf[128]; /* 128 is for SHA384 and SHA512 support, otherwise for SHA224 and SHA256 is 64 enough */
u32 nblocks;
@ -33,43 +33,41 @@ typedef struct {
int count;
u32 blocksize;
sha_transform_fn *transform;
} sha256_context;
typedef sha256_context sha224_context;
};
#define sha224_context sha256_context /* aliasing 'struct sha224_context' to 'struct sha256_context' */
void sha256_init(sha256_context *ctx);
void sha224_init(sha224_context *ctx);
void sha256_init(struct sha256_context *ctx);
void sha224_init(struct sha224_context *ctx);
void sha256_update(sha256_context *ctx, const byte *in_buf, size_t in_len);
void sha224_update(sha224_context *ctx, const byte *in_buf, size_t in_len)
void sha256_update(struct sha256_context *ctx, const byte *in_buf, size_t in_len);
static inline void sha224_update(struct sha224_context *ctx, const byte *in_buf, size_t in_len)
{
sha256_update(ctx, in_buf, in_len);
}
byte* sha256_final(sha256_context *ctx);
byte* sha224_final(sha224_context *ctx)
byte* sha256_final(struct sha256_context *ctx);
static inline byte* sha224_final(struct sha224_context *ctx)
{
return sha256_final(ctx);
}
static uint sha256_transform(void *ctx, const byte *data, size_t nblks);
/**
/*
* HMAC-SHA256, HMAC-SHA224
*/
typedef struct
struct sha256_hmac_context
{
sha256_context ictx;
sha256_context octx;
} sha256_hmac_context;
typedef sha256_hmac_context sha224_hmac_context;
struct sha256_context ictx;
struct sha256_context octx;
};
#define sha224_hmac_context sha256_hmac_context /* aliasing 'struct sha224_hmac_context' to 'struct sha256_hmac_context' */
void sha256_hmac_init(sha256_hmac_context *ctx, const byte *key, size_t keylen);
void sha224_hmac_init(sha224_hmac_context *ctx, const byte *key, size_t keylen);
void sha256_hmac_init(struct sha256_hmac_context *ctx, const byte *key, size_t keylen);
void sha224_hmac_init(struct sha224_hmac_context *ctx, const byte *key, size_t keylen);
void sha256_hmac_update(sha256_hmac_context *ctx, const byte *buf, size_t buflen);
void sha224_hmac_update(sha224_hmac_context *ctx, const byte *buf, size_t buflen);
void sha256_hmac_update(struct sha256_hmac_context *ctx, const byte *buf, size_t buflen);
void sha224_hmac_update(struct sha224_hmac_context *ctx, const byte *buf, size_t buflen);
byte *sha256_hmac_final(sha256_hmac_context *ctx);
byte *sha224_hmac_final(sha224_hmac_context *ctx);
byte *sha256_hmac_final(struct sha256_hmac_context *ctx);
byte *sha224_hmac_final(struct sha224_hmac_context *ctx);
#endif /* _BIRD_SHA256_H_ */

View File

@ -1,5 +1,5 @@
/*
* BIRD -- SHA-256, SHA-224, HMAC-SHA-256 and HMAC-SHA224 Tests
* BIRD Library -- SHA-256, SHA-224, HMAC-SHA-256 and HMAC-SHA224 Tests
*
* (c) 2015 CZ.NIC z.s.p.o.
*
@ -26,7 +26,7 @@ byte_to_hex(char *out, const byte *in, uint len)
static void
get_sha256(const char *str, char (*out_hash)[SHA256_HEX_SIZE])
{
sha256_context ctx;
struct sha256_context ctx;
sha256_init(&ctx);
sha256_update(&ctx, str, strlen(str));
byte *hash = sha256_final(&ctx);
@ -36,7 +36,7 @@ get_sha256(const char *str, char (*out_hash)[SHA256_HEX_SIZE])
static void
get_sha224(const char *str, char (*out_hash)[SHA256_HEX_SIZE])
{
sha224_context ctx;
struct sha224_context ctx;
sha224_init(&ctx);
sha224_update(&ctx, str, strlen(str));
byte *hash = sha224_final(&ctx);
@ -126,13 +126,13 @@ t_sha256_concating(void)
char *str_b5 = "eeeee" ;
char *str_b6 = "ffffff";
sha256_context ctx_a;
struct sha256_context ctx_a;
sha256_init(&ctx_a);
sha256_update(&ctx_a, str_a, strlen(str_a));
byte *hash_a_ = sha256_final(&ctx_a);
byte_to_hex(hash_a, hash_a_, SHA256_SIZE);
sha256_context ctx_b;
struct sha256_context ctx_b;
sha256_init(&ctx_b);
sha256_update(&ctx_b, str_b1, strlen(str_b1));
sha256_update(&ctx_b, str_b2, strlen(str_b2));
@ -160,7 +160,7 @@ struct hmac_data_in {
static void
get_sha256_hmac(const struct hmac_data_in in, char (*out_hash)[SHA256_HEX_SIZE])
{
sha256_hmac_context ctx;
struct sha256_hmac_context ctx;
sha256_hmac_init(&ctx, in.key, in.key_len);
sha256_hmac_update(&ctx, in.data, in.data_len);
byte *hash_byte = sha256_hmac_final(&ctx);
@ -170,7 +170,7 @@ get_sha256_hmac(const struct hmac_data_in in, char (*out_hash)[SHA256_HEX_SIZE])
static void
get_sha224_hmac(const struct hmac_data_in in, char (*out_hash)[SHA224_HEX_SIZE])
{
sha224_hmac_context ctx;
struct sha224_hmac_context ctx;
sha224_hmac_init(&ctx, in.key, in.key_len);
sha224_hmac_update(&ctx, in.data, in.data_len);
byte *hash_byte = sha224_hmac_final(&ctx);

View File

@ -1,6 +1,6 @@
/*
* BIRD -- SHA-512 and SHA-384 Hash Functions,
* HMAC-SHA-512 and HMAC-SHA-384 Functions
* BIRD Library -- SHA-512 and SHA-384 Hash Functions,
* HMAC-SHA-512 and HMAC-SHA-384 Functions
*
* (c) 2015 CZ.NIC z.s.p.o.
*
@ -22,10 +22,12 @@
#define U64_C(c) (c ## UL) /* Maybe is system dependent */
static uint sha512_transform(void *context, const byte *data, size_t nblks);
void
sha512_init(sha512_context *ctx)
sha512_init(struct sha512_context *ctx)
{
sha512_state *hd = &ctx->state;
struct sha512_state *hd = &ctx->state;
hd->h0 = U64_C(0x6a09e667f3bcc908);
hd->h1 = U64_C(0xbb67ae8584caa73b);
@ -44,9 +46,9 @@ sha512_init(sha512_context *ctx)
}
void
sha384_init(sha384_context *ctx)
sha384_init(struct sha384_context *ctx)
{
sha512_state *hd = &ctx->state;
struct sha512_state *hd = &ctx->state;
hd->h0 = U64_C(0xcbbb9d5dc1059ed8);
hd->h1 = U64_C(0x629a292a367cd507);
@ -64,7 +66,7 @@ sha384_init(sha384_context *ctx)
ctx->bctx.transform = sha512_transform;
}
void sha512_update(sha512_context *ctx, const byte *in_buf, size_t in_len)
void sha512_update(struct sha512_context *ctx, const byte *in_buf, size_t in_len)
{
sha256_update(&ctx->bctx, in_buf, in_len);
}
@ -147,7 +149,7 @@ static const u64 k[] =
* Transform the message W which consists of 16 64-bit-words
*/
static uint
sha512_transform_block(sha512_state *hd, const byte *data)
sha512_transform_block(struct sha512_state *hd, const byte *data)
{
u64 a, b, c, d, e, f, g, h;
u64 w[16];
@ -412,7 +414,7 @@ sha512_transform_block(sha512_state *hd, const byte *data)
static uint
sha512_transform(void *context, const byte *data, size_t nblks)
{
sha512_context *ctx = context;
struct sha512_context *ctx = context;
uint burn;
do
@ -433,7 +435,7 @@ sha512_transform(void *context, const byte *data, size_t nblks)
* we take the leftmost 48 of those bytes.
*/
byte *
sha512_final(sha512_context *ctx)
sha512_final(struct sha512_context *ctx)
{
u64 t, th, msb, lsb;
byte *p;
@ -495,14 +497,15 @@ sha512_final(sha512_context *ctx)
return ctx->bctx.buf;
}
/**
/*
* SHA512-HMAC
*/
static void
sha512_hash_buffer(byte *outbuf, const byte *buffer, size_t length)
{
sha512_context hd_tmp;
struct sha512_context hd_tmp;
sha512_init(&hd_tmp);
sha512_update(&hd_tmp, buffer, length);
@ -510,11 +513,11 @@ sha512_hash_buffer(byte *outbuf, const byte *buffer, size_t length)
}
void
sha512_hmac_init(sha512_hmac_context *ctx, const byte *key, size_t keylen)
sha512_hmac_init(struct sha512_hmac_context *ctx, const byte *key, size_t keylen)
{
byte keybuf[SHA512_BLOCK_SIZE], buf[SHA512_BLOCK_SIZE];
// Hash the key if necessary
/* Hash the key if necessary */
if (keylen <= SHA512_BLOCK_SIZE)
{
memcpy(keybuf, key, keylen);
@ -526,44 +529,45 @@ sha512_hmac_init(sha512_hmac_context *ctx, const byte *key, size_t keylen)
bzero(keybuf + SHA512_SIZE, SHA512_BLOCK_SIZE - SHA512_SIZE);
}
// Initialize the inner digest
/* Initialize the inner digest */
sha512_init(&ctx->ictx);
int i;
for (i = 0; i < SHA512_BLOCK_SIZE; i++)
buf[i] = keybuf[i] ^ 0x36;
sha512_update(&ctx->ictx, buf, SHA512_BLOCK_SIZE);
// Initialize the outer digest
/* Initialize the outer digest */
sha512_init(&ctx->octx);
for (i = 0; i < SHA512_BLOCK_SIZE; i++)
buf[i] = keybuf[i] ^ 0x5c;
sha512_update(&ctx->octx, buf, SHA512_BLOCK_SIZE);
}
void sha512_hmac_update(sha512_hmac_context *ctx, const byte *buf, size_t buflen)
void sha512_hmac_update(struct sha512_hmac_context *ctx, const byte *buf, size_t buflen)
{
// Just update the inner digest
/* Just update the inner digest */
sha512_update(&ctx->ictx, buf, buflen);
}
byte *sha512_hmac_final(sha512_hmac_context *ctx)
byte *sha512_hmac_final(struct sha512_hmac_context *ctx)
{
// Finish the inner digest
/* Finish the inner digest */
byte *isha = sha512_final(&ctx->ictx);
// Finish the outer digest
/* Finish the outer digest */
sha512_update(&ctx->octx, isha, SHA512_SIZE);
return sha512_final(&ctx->octx);
}
/**
/*
* SHA384-HMAC
*/
static void
sha384_hash_buffer(byte *outbuf, const byte *buffer, size_t length)
{
sha384_context hd_tmp;
struct sha384_context hd_tmp;
sha384_init(&hd_tmp);
sha384_update(&hd_tmp, buffer, length);
@ -571,11 +575,11 @@ sha384_hash_buffer(byte *outbuf, const byte *buffer, size_t length)
}
void
sha384_hmac_init(sha384_hmac_context *ctx, const byte *key, size_t keylen)
sha384_hmac_init(struct sha384_hmac_context *ctx, const byte *key, size_t keylen)
{
byte keybuf[SHA384_BLOCK_SIZE], buf[SHA384_BLOCK_SIZE];
// Hash the key if necessary
/* Hash the key if necessary */
if (keylen <= SHA384_BLOCK_SIZE)
{
memcpy(keybuf, key, keylen);
@ -587,32 +591,32 @@ sha384_hmac_init(sha384_hmac_context *ctx, const byte *key, size_t keylen)
bzero(keybuf + SHA384_SIZE, SHA384_BLOCK_SIZE - SHA384_SIZE);
}
// Initialize the inner digest
/* Initialize the inner digest */
sha384_init(&ctx->ictx);
int i;
for (i = 0; i < SHA384_BLOCK_SIZE; i++)
buf[i] = keybuf[i] ^ 0x36;
sha384_update(&ctx->ictx, buf, SHA384_BLOCK_SIZE);
// Initialize the outer digest
/* Initialize the outer digest */
sha384_init(&ctx->octx);
for (i = 0; i < SHA384_BLOCK_SIZE; i++)
buf[i] = keybuf[i] ^ 0x5c;
sha384_update(&ctx->octx, buf, SHA384_BLOCK_SIZE);
}
void sha384_hmac_update(sha384_hmac_context *ctx, const byte *buf, size_t buflen)
void sha384_hmac_update(struct sha384_hmac_context *ctx, const byte *buf, size_t buflen)
{
// Just update the inner digest
/* Just update the inner digest */
sha384_update(&ctx->ictx, buf, buflen);
}
byte *sha384_hmac_final(sha384_hmac_context *ctx)
byte *sha384_hmac_final(struct sha384_hmac_context *ctx)
{
// Finish the inner digest
/* Finish the inner digest */
byte *isha = sha384_final(&ctx->ictx);
// Finish the outer digest
/* Finish the outer digest */
sha384_update(&ctx->octx, isha, SHA384_SIZE);
return sha384_final(&ctx->octx);
}

View File

@ -1,6 +1,6 @@
/*
* BIRD -- SHA-512 and SHA-384 Hash Functions,
* HMAC-SHA-512 and HMAC-SHA-384 Functions
* BIRD Library -- SHA-512 and SHA-384 Hash Functions,
* HMAC-SHA-512 and HMAC-SHA-384 Functions
*
* (c) 2015 CZ.NIC z.s.p.o.
*
@ -24,53 +24,51 @@
#define SHA512_HEX_SIZE 129
#define SHA512_BLOCK_SIZE 128
typedef struct
struct sha512_state
{
u64 h0, h1, h2, h3, h4, h5, h6, h7;
} sha512_state;
};
typedef struct
struct sha512_context
{
sha256_context bctx;
sha512_state state;
} sha512_context;
typedef sha512_context sha384_context;
struct sha256_context bctx;
struct sha512_state state;
};
#define sha384_context sha512_context /* aliasing 'struct sha384_context' to 'struct sha512_context' */
void sha512_init(sha512_context *ctx);
void sha384_init(sha384_context *ctx);
void sha512_init(struct sha512_context *ctx);
void sha384_init(struct sha384_context *ctx);
void sha512_update(sha512_context *ctx, const byte *in_buf, size_t in_len);
void sha384_update(sha384_context *ctx, const byte *in_buf, size_t in_len)
void sha512_update(struct sha512_context *ctx, const byte *in_buf, size_t in_len);
static inline void sha384_update(struct sha384_context *ctx, const byte *in_buf, size_t in_len)
{
sha512_update(ctx, in_buf, in_len);
}
byte* sha512_final(sha512_context *ctx);
byte* sha384_final(sha384_context *ctx)
byte* sha512_final(struct sha512_context *ctx);
static inline byte* sha384_final(struct sha384_context *ctx)
{
return sha512_final(ctx);
}
static uint sha512_transform(void *context, const byte *data, size_t nblks);
/**
/*
* HMAC-SHA512, HMAC-SHA384
*/
typedef struct
struct sha512_hmac_context
{
sha512_context ictx;
sha512_context octx;
} sha512_hmac_context;
typedef sha512_hmac_context sha384_hmac_context;
struct sha512_context ictx;
struct sha512_context octx;
} ;
#define sha384_hmac_context sha512_hmac_context /* aliasing 'struct sha384_hmac_context' to 'struct sha384_hmac_context' */
void sha512_hmac_init(sha512_hmac_context *ctx, const byte *key, size_t keylen);
void sha384_hmac_init(sha384_hmac_context *ctx, const byte *key, size_t keylen);
void sha512_hmac_init(struct sha512_hmac_context *ctx, const byte *key, size_t keylen);
void sha384_hmac_init(struct sha384_hmac_context *ctx, const byte *key, size_t keylen);
void sha512_hmac_update(sha512_hmac_context *ctx, const byte *buf, size_t buflen);
void sha384_hmac_update(sha384_hmac_context *ctx, const byte *buf, size_t buflen);
void sha512_hmac_update(struct sha512_hmac_context *ctx, const byte *buf, size_t buflen);
void sha384_hmac_update(struct sha384_hmac_context *ctx, const byte *buf, size_t buflen);
byte *sha512_hmac_final(sha512_hmac_context *ctx);
byte *sha384_hmac_final(sha384_hmac_context *ctx);
byte *sha512_hmac_final(struct sha512_hmac_context *ctx);
byte *sha384_hmac_final(struct sha384_hmac_context *ctx);
#endif /* _BIRD_SHA512_H_ */

View File

@ -1,5 +1,5 @@
/*
* BIRD -- SHA-512, SHA-384, HMAC-SHA-512 and HMAC-SHA-384 Tests
* BIRD Library -- SHA-512, SHA-384, HMAC-SHA-512 and HMAC-SHA-384 Tests
*
* (c) 2015 CZ.NIC z.s.p.o.
*
@ -25,7 +25,7 @@ byte_to_hex(char *out, const byte *in, uint len)
static void
get_sha512(const char *str, char (*out_hash)[SHA512_HEX_SIZE])
{
sha512_context ctx;
struct sha512_context ctx;
sha512_init(&ctx);
sha512_update(&ctx, str, strlen(str));
byte *hash = sha512_final(&ctx);
@ -35,7 +35,7 @@ get_sha512(const char *str, char (*out_hash)[SHA512_HEX_SIZE])
static void
get_sha384(const char *str, char (*out_hash)[SHA384_HEX_SIZE])
{
sha384_context ctx;
struct sha384_context ctx;
sha384_init(&ctx);
sha384_update(&ctx, str, strlen(str));
byte *hash = sha384_final(&ctx);
@ -125,13 +125,13 @@ t_sha512_concating(void)
char *str_b5 = "eeeee" ;
char *str_b6 = "ffffff";
sha512_context ctx_a;
struct sha512_context ctx_a;
sha512_init(&ctx_a);
sha512_update(&ctx_a, str_a, strlen(str_a));
byte *hash_a_ = sha512_final(&ctx_a);
byte_to_hex(hash_a, hash_a_, SHA512_SIZE);
sha512_context ctx_b;
struct sha512_context ctx_b;
sha512_init(&ctx_b);
sha512_update(&ctx_b, str_b1, strlen(str_b1));
sha512_update(&ctx_b, str_b2, strlen(str_b2));
@ -159,7 +159,7 @@ struct hmac_data_in {
static void
get_sha512_hmac(const struct hmac_data_in in, char (*out_hash)[SHA512_HEX_SIZE])
{
sha512_hmac_context ctx;
struct sha512_hmac_context ctx;
sha512_hmac_init(&ctx, in.key, in.key_len);
sha512_hmac_update(&ctx, in.data, in.data_len);
byte *hash_byte = sha512_hmac_final(&ctx);
@ -169,7 +169,7 @@ get_sha512_hmac(const struct hmac_data_in in, char (*out_hash)[SHA512_HEX_SIZE])
static void
get_sha384_hmac(const struct hmac_data_in in, char (*out_hash)[SHA384_HEX_SIZE])
{
sha384_hmac_context ctx;
struct sha384_hmac_context ctx;
sha384_hmac_init(&ctx, in.key, in.key_len);
sha384_hmac_update(&ctx, in.data, in.data_len);
byte *hash_byte = sha384_hmac_final(&ctx);

View File

@ -109,7 +109,7 @@ ospf_pkt_finalize(struct ospf_iface *ifa, struct ospf_packet *pkt)
char password[OSPF_AUTH_CRYPT_SIZE];
strncpy(password, passwd->password, sizeof(password));
md5_context ctxt;
struct md5_context ctxt;
md5_init(&ctxt);
md5_update(&ctxt, (char *) pkt, plen);
md5_update(&ctxt, password, OSPF_AUTH_CRYPT_SIZE);
@ -181,7 +181,7 @@ ospf_pkt_checkauth(struct ospf_neighbor *n, struct ospf_iface *ifa, struct ospf_
strncpy(passwd, pass->password, OSPF_AUTH_CRYPT_SIZE);
md5_context ctxt;
struct md5_context ctxt;
md5_init(&ctxt);
md5_update(&ctxt, (char *) pkt, plen);
md5_update(&ctxt, passwd, OSPF_AUTH_CRYPT_SIZE);

View File

@ -35,42 +35,42 @@
int
rip_incoming_authentication( struct proto *p, struct rip_block_auth *block, struct rip_packet *packet, int num, ip_addr whotoldme )
{
DBG( "Incoming authentication: " );
DBG("Incoming authentication: " );
switch (ntohs(block->authtype)) { /* Authentication type */
case AT_PLAINTEXT:
{
struct password_item *passwd = password_find(P_CF->passwords, 1);
DBG( "Plaintext passwd" );
DBG("Plaintext passwd" );
if (!passwd) {
log( L_AUTH "No passwords set and password authentication came" );
log(L_AUTH "No passwords set and password authentication came" );
return 1;
}
if (strncmp( (char *) (&block->packetlen), passwd->password, 16)) {
log( L_AUTH "Passwd authentication failed!" );
DBG( "Expected %s, got %.16s\n", passwd->password, &block->packetlen );
log(L_AUTH "Passwd authentication failed!" );
DBG("Expected %s, got %.16s\n", passwd->password, &block->packetlen );
return 1;
}
}
break;
case AT_MD5:
DBG( "md5 password" );
DBG("md5 password" );
{
struct password_item *pass = NULL, *ptmp;
struct rip_md5_tail *tail;
md5_context ctxt;
struct md5_context ctxt;
char md5sum_packet[16];
char *md5sum_computed;
struct neighbor *neigh = neigh_find(p, &whotoldme, 0);
list *l = P_CF->passwords;
if (ntohs(block->packetlen) != PACKETLEN(num) - sizeof(struct rip_md5_tail) ) {
log( L_ERR "Packet length in MD5 does not match computed value" );
log(L_ERR "Packet length in MD5 does not match computed value" );
return 1;
}
tail = (struct rip_md5_tail *) ((char *) packet + (ntohs(block->packetlen) ));
if ((tail->mustbeFFFF != 0xffff) || (tail->mustbe0001 != 0x0100)) {
log( L_ERR "MD5 tail signature is not there" );
log(L_ERR "MD5 tail signature is not there" );
return 1;
}
@ -85,10 +85,10 @@ rip_incoming_authentication( struct proto *p, struct rip_block_auth *block, stru
if(!pass) return 1;
if (!neigh) {
log( L_AUTH "Non-neighbour MD5 checksummed packet?" );
log(L_AUTH "Non-neighbour MD5 checksummed packet?" );
} else {
if (neigh->aux > block->seq) {
log( L_AUTH "MD5 protected packet with lower numbers" );
log(L_AUTH "MD5 protected packet with lower numbers" );
return 1;
}
neigh->aux = block->seq;
@ -120,10 +120,10 @@ rip_outgoing_authentication( struct proto *p, struct rip_block_auth *block, stru
if (!P_CF->authtype)
return PACKETLEN(num);
DBG( "Outgoing authentication: " );
DBG("Outgoing authentication: " );
if (!passwd) {
log( L_ERR "No suitable password found for authentication" );
log(L_ERR "No suitable password found for authentication" );
return PACKETLEN(num);
}
@ -136,11 +136,11 @@ rip_outgoing_authentication( struct proto *p, struct rip_block_auth *block, stru
case AT_MD5:
{
struct rip_md5_tail *tail;
md5_context ctxt;
struct md5_context ctxt;
static u32 sequence = 0;
if (num > PACKET_MD5_MAX)
bug( "We can not add MD5 authentication to this long packet" );
bug("We can not add MD5 authentication to this long packet" );
/* need to preset the sequence number to a sane value */
if (!sequence)
@ -163,6 +163,6 @@ rip_outgoing_authentication( struct proto *p, struct rip_block_auth *block, stru
return PACKETLEN(num) + block->authlen;
}
default:
bug( "Unknown authtype in outgoing authentication?" );
bug("Unknown authtype in outgoing authentication?" );
}
}