Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
|
Files | |
file | hash_adler32.c |
32-bit Adler hash algorithm | |
file | hash_blake2b.c |
Blake2b cryptographic hash function | |
file | hash_crc32.c |
32-bit CRC hash algorithm | |
file | hash_djb2.c |
DJB2 hash algorithm | |
file | hash_sdbm.c |
SDBM hash algorithm | |
file | hash_xor8.c |
8-bit XOR hash algorithm for ASCII characters | |
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 | |
uint32_t | adler32 (const char *s) |
32-bit Adler algorithm implementation | |
void | test_adler32 () |
Test function for adler32. | |
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 | |
uint32_t | crc32 (const char *s) |
32-bit CRC algorithm implementation | |
void | test_crc32 () |
Test function for crc32. | |
uint64_t | djb2 (const char *s) |
DJB2 algorithm implementation. | |
void | test_djb2 (void) |
Test function for djb2. | |
uint64_t | sdbm (const char *s) |
SDBM algorithm implementation. | |
void | test_sdbm () |
Test function for sdbm. | |
uint8_t | xor8 (const char *s) |
8-bit XOR algorithm implementation | |
void | test_xor8 () |
Test function for xor8. | |
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 | |
#define bb 128 |
for asserts
for fixed-width integer types e.g. uint64_t and uint8_t for IO for malloc, calloc, and free. As well as size_t
the size of a data block in bytes
#define CEIL | ( | a, | |
b | |||
) | (((a) / (b)) + ((a) % (b) != 0)) |
ceiling division macro without floats
a | dividend |
b | divisor |
#define U128_ZERO |
zero-value initializer for u128 type
uint32_t adler32 | ( | const char * | s | ) |
32-bit Adler algorithm implementation
s | NULL terminated ASCII string to hash |
uint8_t * blake2b | ( | const uint8_t * | message, |
size_t | len, | ||
const uint8_t * | key, | ||
uint8_t | kk, | ||
uint8_t | nn | ||
) |
blake2b hash function
This is the front-end function that sets up the argument for BLAKE2B().
message | the message to be hashed |
len | length of message (0 <= len < 2**128) (depends on sizeof(size_t) for this implementation) |
key | optional secret key |
kk | length of optional secret key (0 <= kk <= 64) |
nn | length of output digest (1 <= nn < 64) |
|
static |
driver function to perform the hashing as described in specification
pseudocode: (credit to authors of RFC 7693 listed above) FUNCTION BLAKE2( d[0..dd-1], ll, kk, nn ) | | h[0..7] := IV[0..7] // Initialization Vector. | | // Parameter block p[0] | h[0] := h[0] ^ 0x01010000 ^ (kk << 8) ^ nn | | // Process padded key and data blocks | IF dd > 1 THEN | | FOR i = 0 TO dd - 2 DO | | | h := F( h, d[i], (i + 1) * bb, FALSE ) | | END FOR. | END IF. | | // Final block. | IF kk = 0 THEN | | h := F( h, d[dd - 1], ll, TRUE ) | ELSE | | h := F( h, d[dd - 1], ll + bb, TRUE ) | END IF. | | RETURN first "nn" bytes from little-endian word array h[]. | END FUNCTION.
dest | destination of hashing digest |
d | message blocks |
dd | length of d |
ll | 128-bit length of message |
kk | length of secret key |
nn | length of hash digest |
uint32_t crc32 | ( | const char * | s | ) |
32-bit CRC algorithm implementation
s | NULL terminated ASCII string to hash |
uint64_t djb2 | ( | const char * | s | ) |
DJB2 algorithm implementation.
s | NULL terminated string to hash |
compression function F
Securely mixes the values in block m into the state vector h. Value at v[14] is also inverted if this is the final block to be compressed.
h | the state vector |
m | message vector to be compressed into h |
t | 128-bit offset counter |
f | flag to indicate whether this is the final block |
|
static |
blake2b mixing function G
Shuffles values in block v depending on provided indeces a, b, c, and d. x and y are also mixed into the block.
v | array of words to be mixed |
a | first index |
b | second index |
c | third index |
d | fourth index |
x | first word being mixed into v |
y | second word being mixed into y |
uint64_t sdbm | ( | const char * | s | ) |
SDBM algorithm implementation.
s | NULL terminated string to hash |
void test_adler32 | ( | ) |
Test function for adler32.
void test_crc32 | ( | ) |
Test function for crc32.
void test_djb2 | ( | void | ) |
Test function for djb2.
void test_sdbm | ( | ) |
Test function for sdbm.
void test_xor8 | ( | ) |
Test function for xor8.
|
inlinestatic |
put value of n into dest
dest | 128-bit number to get copied from n |
n | value put into dest |
|
inlinestatic |
increment an 128-bit number by a given amount
dest | the value being incremented |
n | what dest is being increased by |
uint8_t xor8 | ( | const char * | s | ) |
8-bit XOR algorithm implementation
s | NULL terminated ASCII string to hash |
|
static |
BLAKE2b Initialization vector blake2b_iv[i] = floor(2**64 * frac(sqrt(prime(i+1)))), where prime(i) is the i:th prime number.
|
static |
word schedule permutations for each round of the algorithm