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
 max key length for BLAKE2b
 
#define NN_MAX   64
 max length of BLAKE2b digest in bytes
 
#define CEIL(a, b)   (((a) / (b)) + ((a) % (b) != 0))
 ceiling division macro without floats
 
#define MIN(a, b)   ((a) < (b) ? (a) : (b))
 returns minimum value
 
#define MAX(a, b)   ((a) > (b) ? (a) : (b))
 returns maximum value
 
#define ROTR64(n, offset)   (((n) >> (offset)) ^ ((n) << (64 - (offset))))
 macro to rotate 64-bit ints to the right Ripped from RFC 7693
 
#define U128_ZERO
 zero-value initializer for u128 type
 

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)
 blake2b hash function
 
static void assert_bytes (const uint8_t *expected, const uint8_t *actual, uint8_t len)
 Self-test implementations.
 
static void test ()
 testing function
 
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
434{
435 uint8_t i;
436
437 assert(expected != NULL);
438 assert(actual != NULL);
439 assert(len > 0);
440
441 for (i = 0; i < len; i++)
442 {
443 assert(expected[i] == actual[i]);
444 }
445}

◆ main()

int main ( void  )

main function

Returns
0 on successful program exit
548{
549 test();
550 return 0;
551}
static void test()
testing function
Definition hash_blake2b.c:452
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

testing function

Returns
void
453{
454 uint8_t *digest = NULL;
455
456 /* "abc" example straight out of RFC-7693 */
457 uint8_t abc[3] = {'a', 'b', 'c'};
458 uint8_t abc_answer[64] = {
459 0xBA, 0x80, 0xA5, 0x3F, 0x98, 0x1C, 0x4D, 0x0D, 0x6A, 0x27, 0x97,
460 0xB6, 0x9F, 0x12, 0xF6, 0xE9, 0x4C, 0x21, 0x2F, 0x14, 0x68, 0x5A,
461 0xC4, 0xB7, 0x4B, 0x12, 0xBB, 0x6F, 0xDB, 0xFF, 0xA2, 0xD1, 0x7D,
462 0x87, 0xC5, 0x39, 0x2A, 0xAB, 0x79, 0x2D, 0xC2, 0x52, 0xD5, 0xDE,
463 0x45, 0x33, 0xCC, 0x95, 0x18, 0xD3, 0x8A, 0xA8, 0xDB, 0xF1, 0x92,
464 0x5A, 0xB9, 0x23, 0x86, 0xED, 0xD4, 0x00, 0x99, 0x23};
465
466 digest = blake2b(abc, 3, NULL, 0, 64);
467 assert_bytes(abc_answer, digest, 64);
468
469 free(digest);
470
471 uint8_t key[64] = {
472 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
473 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
474 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
475 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
476 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
477 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f};
478 uint8_t key_answer[64] = {
479 0x10, 0xeb, 0xb6, 0x77, 0x00, 0xb1, 0x86, 0x8e, 0xfb, 0x44, 0x17,
480 0x98, 0x7a, 0xcf, 0x46, 0x90, 0xae, 0x9d, 0x97, 0x2f, 0xb7, 0xa5,
481 0x90, 0xc2, 0xf0, 0x28, 0x71, 0x79, 0x9a, 0xaa, 0x47, 0x86, 0xb5,
482 0xe9, 0x96, 0xe8, 0xf0, 0xf4, 0xeb, 0x98, 0x1f, 0xc2, 0x14, 0xb0,
483 0x05, 0xf4, 0x2d, 0x2f, 0xf4, 0x23, 0x34, 0x99, 0x39, 0x16, 0x53,
484 0xdf, 0x7a, 0xef, 0xcb, 0xc1, 0x3f, 0xc5, 0x15, 0x68};
485
486 digest = blake2b(NULL, 0, key, 64, 64);
487 assert_bytes(key_answer, digest, 64);
488
489 free(digest);
490
491 uint8_t zero[1] = {0};
492 uint8_t zero_key[64] = {
493 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
494 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
495 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
496 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
497 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
498 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f};
499 uint8_t zero_answer[64] = {
500 0x96, 0x1f, 0x6d, 0xd1, 0xe4, 0xdd, 0x30, 0xf6, 0x39, 0x01, 0x69,
501 0x0c, 0x51, 0x2e, 0x78, 0xe4, 0xb4, 0x5e, 0x47, 0x42, 0xed, 0x19,
502 0x7c, 0x3c, 0x5e, 0x45, 0xc5, 0x49, 0xfd, 0x25, 0xf2, 0xe4, 0x18,
503 0x7b, 0x0b, 0xc9, 0xfe, 0x30, 0x49, 0x2b, 0x16, 0xb0, 0xd0, 0xbc,
504 0x4e, 0xf9, 0xb0, 0xf3, 0x4c, 0x70, 0x03, 0xfa, 0xc0, 0x9a, 0x5e,
505 0xf1, 0x53, 0x2e, 0x69, 0x43, 0x02, 0x34, 0xce, 0xbd};
506
507 digest = blake2b(zero, 1, zero_key, 64, 64);
508 assert_bytes(zero_answer, digest, 64);
509
510 free(digest);
511
512 uint8_t filled[64] = {
513 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
514 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
515 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
516 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
517 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
518 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f};
519 uint8_t filled_key[64] = {
520 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
521 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
522 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
523 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
524 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36,
525 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f};
526 uint8_t filled_answer[64] = {
527 0x65, 0x67, 0x6d, 0x80, 0x06, 0x17, 0x97, 0x2f, 0xbd, 0x87, 0xe4,
528 0xb9, 0x51, 0x4e, 0x1c, 0x67, 0x40, 0x2b, 0x7a, 0x33, 0x10, 0x96,
529 0xd3, 0xbf, 0xac, 0x22, 0xf1, 0xab, 0xb9, 0x53, 0x74, 0xab, 0xc9,
530 0x42, 0xf1, 0x6e, 0x9a, 0xb0, 0xea, 0xd3, 0x3b, 0x87, 0xc9, 0x19,
531 0x68, 0xa6, 0xe5, 0x09, 0xe1, 0x19, 0xff, 0x07, 0x78, 0x7b, 0x3e,
532 0xf4, 0x83, 0xe1, 0xdc, 0xdc, 0xcf, 0x6e, 0x30, 0x22};
533
534 digest = blake2b(filled, 64, filled_key, 64, 64);
535 assert_bytes(filled_answer, digest, 64);
536
537 free(digest);
538
539 printf("All tests have successfully passed!\n");
540}
uint8_t * blake2b(const uint8_t *message, size_t len, const uint8_t *key, uint8_t kk, uint8_t nn)
blake2b hash function
Definition hash_blake2b.c:354
static void assert_bytes(const uint8_t *expected, const uint8_t *actual, uint8_t len)
Self-test implementations.
Definition hash_blake2b.c:432
#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: