Skip to content

Commit

Permalink
Review0 fixes #1
Browse files Browse the repository at this point in the history
Fix incorrect hash output (value of 00 in part of hash result in loop out)
File & Folder rename
Conform to Mozilla style guide
Maintain consistency with extern linkage declarations
  • Loading branch information
Sachin-A committed Dec 3, 2016
1 parent 6d888db commit 27fb9a5
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 307 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ $(ODIR)/%.o: src/%.c
$(CC) -c $< -o $@ $(CFLAGS)

clean:
rm -f $(ODIR)/*.o *~ core $(out)
rm -f $(ODIR)/*.o *~ core $(out)
23 changes: 12 additions & 11 deletions include/blake2b.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,19 @@ typedef struct blake2b_param

typedef struct blake2b_state
{
uint64_t h[8]; // Chained state
uint64_t t[2]; // total number of bytes
uint64_t f[2]; // last block flag
uint8_t buf[BLAKE2B_BLOCKBYTES]; // input buffer
size_t buflen; // size of buffer
size_t outlen; // digest size
uint64_t h[8]; /* Chained state */
uint64_t t[2]; /* total number of bytes */
uint64_t f[2]; /* last block flag */
uint8_t buf[BLAKE2B_BLOCKBYTES]; /* input buffer */
size_t buflen; /* size of buffer */
size_t outlen; /* digest size */
} blake2b_state;

int blake2b_init(blake2b_state* S, size_t outlen, const void* key,
extern int blake2b_init(blake2b_state* S, size_t outlen, const void* key,
size_t keylen);
int blake2b_update(blake2b_state* S, const void* in, size_t inlen);
int blake2b_final(blake2b_state* S, void* out, size_t outlen);
int blake2b(void* out, size_t outlen, const void* in, size_t inlen,
extern int blake2b_update(blake2b_state* S, const void* in, size_t inlen);
extern int blake2b_final(blake2b_state* S, void* out, size_t outlen);
extern int blake2b(void* out, size_t outlen, const void* in, size_t inlen,
const void* key, size_t keylen);
#endif

#endif /* BLAKE_H */
10 changes: 5 additions & 5 deletions include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

#include "blake2b.h"

uint64_t rotr64(const uint64_t w, const unsigned c);
uint64_t load64(const void* src);
void store64(void* dst, uint64_t w);
void store32(void* dst, uint32_t w);
void blake2b_increment_counter(blake2b_state* S, const uint64_t inc);
extern uint64_t rotr64(const uint64_t w, const unsigned c);
extern uint64_t load64(const void* src);
extern void store64(void* dst, uint64_t w);
extern void store32(void* dst, uint32_t w);
extern void blake2b_increment_counter(blake2b_state* S, const uint64_t inc);

#endif
204 changes: 185 additions & 19 deletions src/blake2b.c
Original file line number Diff line number Diff line change
@@ -1,38 +1,204 @@
#include "blake2b.h"
#include "utils.h"
#include <inttypes.h>
#include <math.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#define BUF_LENGTH 256
/**
* The Mix function is called by the Compress function, and mixes two 8-byte
* words from the message into the hash state
*
* @param v the work vector V
* @params a, b, c, d indices to 8-byte word entries from the work vector V
* @params x, y two 8-byte word entries from padded message v
*/
static void
G(uint64_t v[16], int a, int b, int c, int d, int64_t x, int64_t y)
{
v[a] = v[a] + v[b] + x;
v[d] = rotr64(v[d] ^ v[a], 32);

v[c] = v[c] + v[d];
v[b] = rotr64(v[b] ^ v[c], 24);

v[a] = v[a] + v[b] + y;
v[d] = rotr64(v[d] ^ v[a], 16);

void
print_hex(const uint8_t* s)
v[c] = v[c] + v[d];
v[b] = rotr64(v[b] ^ v[c], 63);
}

/**
* The blake2b compress function which takes a full 128-byte chunk of the
* input message and mixes it into the ongoing state array
*
* @param state blake2b_state instance
* @param block The input block
*/
static void
F(blake2b_state* state, uint8_t block[BLAKE2B_BLOCKBYTES])
{
printf("hash :");
while (*s)
printf("%02x", *s++);
printf("\n");
size_t i, j;
uint64_t v[16], m[16], s[16];

for (i = 0; i < 16; ++i) {
m[i] = load64(block + i * sizeof(m[i]));
}

for (i = 0; i < 8; ++i) {
v[i] = state->h[i];
v[i + 8] = blake2b_IV[i];
}

v[12] ^= state->t[0];
v[13] ^= state->t[1];
v[14] ^= state->f[0];
v[15] ^= state->f[1];

for (i = 0; i < 12; i++) {
for (j = 0; j < 16; j++) {
s[j] = blake2b_sigma[i][j];
}
G(v, 0, 4, 8, 12, m[s[0]], m[s[1]]);
G(v, 1, 5, 9, 13, m[s[2]], m[s[3]]);
G(v, 2, 6, 10, 14, m[s[4]], m[s[5]]);
G(v, 3, 7, 11, 15, m[s[6]], m[s[7]]);
G(v, 0, 5, 10, 15, m[s[8]], m[s[9]]);
G(v, 1, 6, 11, 12, m[s[10]], m[s[11]]);
G(v, 2, 7, 8, 13, m[s[12]], m[s[13]]);
G(v, 3, 4, 9, 14, m[s[14]], m[s[15]]);
}

for (i = 0; i < 8; i++) {
state->h[i] = state->h[i] ^ v[i] ^ v[i + 8];
}
}

/**
* Initializes blake2b state
*
* @param state blake2b_state instance passed by reference
* @param[in] outlen The hash output length
*
* @return sanity value
*/
int
main(int argc, char const* argv[])
blake2b_init(blake2b_state* state, size_t outlen, const void* key, size_t keylen)
{
blake2b_param P[1];
const uint8_t* p;
size_t i;

P->digest_length = (uint8_t)outlen;
P->key_length = 0;
P->fanout = 1;
P->depth = 1;
store32(&P->leaf_length, 0);
store64(&P->node_offset, 0);
P->node_depth = 0;
P->inner_length = 0;
memset(P->reserved, 0, sizeof(P->reserved));
memset(P->salt, 0, sizeof(P->salt));
memset(P->personal, 0, sizeof(P->personal));

uint8_t buf[] = "The quick brown fox jumps over the lazy dog";
uint8_t key[] = "";
uint8_t hash[BLAKE2B_OUTBYTES];
size_t buflen = strlen((char *)buf);
size_t keylen = strlen((char *)key);
p = (const uint8_t*)(P);
memset(state, 0, sizeof(blake2b_state));
for (i = 0; i < 8; ++i) {
state->h[i] = blake2b_IV[i];
}
for (i = 0; i < 8; ++i) {
state->h[i] ^= load64(p + sizeof(state->h[i]) * i);
}
state->outlen = P->digest_length;

if (keylen > 0) {
uint8_t block[BLAKE2B_BLOCKBYTES];
memset(block, 0, BLAKE2B_BLOCKBYTES);
memcpy(block, key, keylen);
blake2b_update(state, block, BLAKE2B_BLOCKBYTES);
}
return 0;
}

/**
* Updates blake2b state
*
* @param state blake2b state instance
* @param[in] input_buffer The input buffer
* @param[in] inlen The input lenth
*
* @return error code
*/
int
blake2b_update(blake2b_state* state, const void* input_buffer, size_t inlen)
{
unsigned char* in;

in = (unsigned char*)input_buffer;

while (inlen > BLAKE2B_BLOCKBYTES) {
blake2b_increment_counter(state, BLAKE2B_BLOCKBYTES);
F(state, in);
in += BLAKE2B_BLOCKBYTES;
inlen -= BLAKE2B_BLOCKBYTES;
}
memcpy(state->buf + state->buflen, in, inlen);
state->buflen += inlen;
return 0;
}

int
blake2b_final(blake2b_state* state, void* out, size_t outlen)
{

uint8_t buffer[BLAKE2B_OUTBYTES] = { 0 };
size_t i;
blake2b_increment_counter(state, state->buflen);

/* set last chunk = true */
state->f[0] = (uint64_t)-1;

/* padding */
memset(state->buf + state->buflen, 0, BLAKE2B_BLOCKBYTES - state->buflen);
F(state, state->buf);

for (i = 0; i <= buflen; ++i) {
blake2b(hash, BLAKE2B_OUTBYTES, buf, i, key, keylen);
/* Store back in little endian */
for (i = 0; i < 8; ++i) {
store64(buffer + sizeof(state->h[i]) * i, state->h[i]);
}

printf("input :%s\n", buf);
printf("key :%s\n", key);
print_hex(hash);
/* Copy first outlen bytes nto output buffer*/
memcpy(out, buffer, state->outlen);
return 0;
}

/**
* The main blake2b function
*
* @param output The hash output
* @param[in] outlen The hash length
* @param[in] input The message input
* @param[in] inlen The message length
* @param[in] key The key
* @param[in] keylen The key length
*
* @return sanity value
*/
int
blake2b(void* output, size_t outlen, const void* input, size_t inlen,
const void* key, size_t keylen)
{
blake2b_state state[1];

if (blake2b_init(state, outlen, key, keylen) < 0) {
return -1;
}
if (blake2b_update(state, (const uint8_t*)input, inlen) < 0) {
return -1;
}
if (blake2b_final(state, output, outlen) < 0) {
return -1;
}
return 0;
}
Loading

0 comments on commit 27fb9a5

Please sign in to comment.