Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
hash_blake2b.c File Reference

Blake2b cryptographic hash function More...

#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for hash_blake2b.c:

Macros

#define bb   128
 for asserts
 
#define KK_MAX   64
 @define KK_MAX
 
#define NN_MAX   64
 @define NN_MAX
 
#define CEIL(a, b)   (((a) / (b)) + ((a) % (b) != 0))
 @define CEIL
 
#define MIN(a, b)   ((a) < (b) ? (a) : (b))
 @define MIN
 
#define MAX(a, b)   ((a) > (b) ? (a) : (b))
 @define MAX
 
#define ROTR64(n, offset)   (((n) >> (offset)) ^ ((n) << (64 - (offset))))
 @define ROTR64
 
#define U128_ZERO
 @define U128_ZERO
 

Typedefs

typedef uint64_t u128[2]
 128-bit number represented as two uint64's
 
typedef uint64_t block_t[bb/sizeof(uint64_t)]
 Padded input block containing bb bytes.
 

Functions

static void u128_fill (u128 dest, size_t n)
 put value of n into dest
 
static void u128_increment (u128 dest, uint64_t n)
 increment an 128-bit number by a given amount
 
static void G (block_t v, uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint64_t x, uint64_t y)
 blake2b mixing function G
 
static void F (uint64_t h[8], block_t m, u128 t, int f)
 compression function F
 
static int BLAKE2B (uint8_t *dest, block_t *d, size_t dd, u128 ll, uint8_t kk, uint8_t nn)
 driver function to perform the hashing as described in specification
 
uint8_t * blake2b (const uint8_t *message, size_t len, const uint8_t *key, uint8_t kk, uint8_t nn)
 
static void assert_bytes (const uint8_t *expected, const uint8_t *actual, uint8_t len)
 Self-test implementations.
 
int main ()
 Main function.
 

Variables

static const uint8_t R1 = 32
 Rotation constant 1 for mixing function G.
 
static const uint8_t R2 = 24
 Rotation constant 2 for mixing function G.
 
static const uint8_t R3 = 16
 Rotation constant 3 for mixing function G.
 
static const uint8_t R4 = 63
 Rotation constant 4 for mixing function G.
 
static const uint64_t blake2b_iv [8]
 BLAKE2b Initialization vector blake2b_iv[i] = floor(2**64 * frac(sqrt(prime(i+1)))), where prime(i) is the i:th prime number.
 
static const uint8_t blake2b_sigma [12][16]
 word schedule permutations for each round of the algorithm
 

Detailed Description

Blake2b cryptographic hash function

Author
Daniel Murrow

The Blake2b cryptographic hash function provides hashes for data that are secure enough to be used in cryptographic applications. It is designed to perform optimally on 64-bit platforms. The algorithm can output digests between 1 and 64 bytes long, for messages up to 128 bits in length. Keyed hashing is also supported for keys up to 64 bytes in length.

Function Documentation

◆ assert_bytes()

static void assert_bytes ( const uint8_t *  expected,
const uint8_t *  actual,
uint8_t  len 
)
static

Self-test implementations.

Returns
void
441{
442 uint8_t i;
443
444 assert(expected != NULL);
445 assert(actual != NULL);
446 assert(len > 0);
447
448 for (i = 0; i < len; i++)
449 {
450 assert(expected[i] == actual[i]);
451 }
452
453 printf("All tests have successfully passed!\n");
454}

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
461{
462 uint8_t *digest = NULL;
463
464 /* "abc" example straight out of RFC-7693 */
465 uint8_t abc[3] = {'a', 'b', 'c'};
466 uint8_t abc_answer[64] = {
467 0xBA, 0x80, 0xA5, 0x3F, 0x98, 0x1C, 0x4D, 0x0D, 0x6A, 0x27, 0x97,
468 0xB6, 0x9F, 0x12, 0xF6, 0xE9, 0x4C, 0x21, 0x2F, 0x14, 0x68, 0x5A,
469 0xC4, 0xB7, 0x4B, 0x12, 0xBB, 0x6F, 0xDB, 0xFF, 0xA2, 0xD1, 0x7D,
470 0x87, 0xC5, 0x39, 0x2A, 0xAB, 0x79, 0x2D, 0xC2, 0x52, 0xD5, 0xDE,
471 0x45, 0x33, 0xCC, 0x95, 0x18, 0xD3, 0x8A, 0xA8, 0xDB, 0xF1, 0x92,
472 0x5A, 0xB9, 0x23, 0x86, 0xED, 0xD4, 0x00, 0x99, 0x23};
473
474 digest = blake2b(abc, 3, NULL, 0, 64);
475 assert_bytes(abc_answer, digest, 64);
476
477 free(digest);
478
479 return 0;
480}
static void assert_bytes(const uint8_t *expected, const uint8_t *actual, uint8_t len)
Self-test implementations.
Definition: hash_blake2b.c:439
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition: malloc_dbg.h:26
Here is the call graph for this function: