### Build SipHash with Makefile Parameters Source: https://github.com/veorq/siphash/blob/master/README.md Use the make command to build SipHash, specifying the desired number of compression (cROUNDS) and finalization (dROUNDS) rounds. This is an alternative to using GCC -D flags. ```shell make cROUNDS=2 dROUNDS=4 ``` -------------------------------- ### Run SipHash Standard Tests Source: https://github.com/veorq/siphash/blob/master/README.md Execute the standard SipHash test suite to verify 64 test vectors. This confirms the correctness of the SipHash implementation against known values. ```shell ./test ``` -------------------------------- ### Create and Verify MAC with SipHash in C Source: https://context7.com/veorq/siphash/llms.txt Implement SipHash as a Message Authentication Code (MAC) to ensure message integrity and authenticity. This C code demonstrates creating a MAC tag and verifying it against potential tampering. The key must be kept secret, and constant-time comparison is used to prevent timing attacks. ```c #include "siphash.h" #include #include #include // Generate and verify a MAC for a message typedef struct { uint8_t tag[16]; // 128-bit MAC for stronger security } mac_tag_t; // Create a MAC for the given message int create_mac(const void *message, size_t len, const uint8_t key[16], mac_tag_t *tag) { return siphash(message, len, key, tag->tag, 16); } // Verify a MAC - returns 0 if valid, non-zero if invalid int verify_mac(const void *message, size_t len, const uint8_t key[16], const mac_tag_t *tag) { mac_tag_t computed; siphash(message, len, key, computed.tag, 16); // Constant-time comparison to prevent timing attacks uint8_t diff = 0; for (int i = 0; i < 16; i++) { diff |= computed.tag[i] ^ tag->tag[i]; } return diff != 0; } int main() { // Shared secret key (in practice, securely exchanged) uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; const char *message = "Important transaction data"; mac_tag_t tag; // Sender creates MAC create_mac(message, strlen(message), key, &tag); printf("Created MAC: "); for (int i = 0; i < 16; i++) printf("%02x", tag.tag[i]); printf("\n"); // Receiver verifies MAC if (verify_mac(message, strlen(message), key, &tag) == 0) { printf("MAC verification: VALID\n"); } else { printf("MAC verification: INVALID - message may be tampered!\n"); } // Test with tampered message const char *tampered = "Important transaction datb"; if (verify_mac(tampered, strlen(tampered), key, &tag) == 0) { printf("Tampered MAC verification: VALID (unexpected!)\n"); } else { printf("Tampered MAC verification: INVALID (expected)\n"); } return 0; } // Compile: gcc -Wall --std=c99 siphash.c mac_example.c -o mac_example // Expected output: // Created MAC: <32 hex characters> // MAC verification: VALID // Tampered MAC verification: INVALID (expected) ``` -------------------------------- ### Build SipHash Tests Source: https://github.com/veorq/siphash/blob/master/README.md Build the test executables for various SipHash variants including SipHash-2-4-64, SipHash-2-4-128, HalfSipHash-2-4-32, and HalfSipHash-2-4-64. This command is typically run from the project's root directory. ```shell make ``` -------------------------------- ### Using SipHash for Hash Table Protection Source: https://context7.com/veorq/siphash/llms.txt Demonstrates how to use SipHash as a hash function for strings in a hash table implementation to protect against algorithmic complexity attacks. ```APIDOC ## Using SipHash for Hash Table Protection SipHash is commonly used to protect hash tables against algorithmic complexity attacks. Here's an example of using it as a hash function for strings in a hash table implementation. ### Code Example (C) ```c #include "siphash.h" #include #include #include #include #define HASH_TABLE_SIZE 1024 // Generate a random key at application startup void generate_random_key(uint8_t *key, size_t len) { // In production, use a cryptographically secure random source // e.g., /dev/urandom on Unix or CryptGenRandom on Windows FILE *f = fopen("/dev/urandom", "rb"); if (f) { fread(key, 1, len, f); fclose(f); } } // Hash function for strings using SipHash uint32_t hash_string(const char *str, const uint8_t *key) { uint8_t out[8]; siphash(str, strlen(str), key, out, 8); // Convert first 4 bytes to uint32_t for table indexing uint32_t hash = (uint32_t)out[0] | ((uint32_t)out[1] << 8) | ((uint32_t)out[2] << 16) | ((uint32_t)out[3] << 24); return hash % HASH_TABLE_SIZE; } int main() { // Initialize secret key (should be done once at startup) uint8_t key[16]; generate_random_key(key, 16); // Example: hash some strings const char *strings[] = {"apple", "banana", "cherry", "date", "elderberry"}; int num_strings = sizeof(strings) / sizeof(strings[0]); printf("Hash table slot assignments:\n"); for (int i = 0; i < num_strings; i++) { uint32_t slot = hash_string(strings[i], key); printf(" '%s' -> slot %u\n", strings[i], slot); } return 0; } // Compile: gcc -Wall --std=c99 siphash.c hashtable_example.c -o hashtable_example ``` ``` -------------------------------- ### Using SipHash as a Message Authentication Code (MAC) Source: https://context7.com/veorq/siphash/llms.txt Illustrates how SipHash can be used to generate and verify a Message Authentication Code (MAC) for ensuring message integrity. ```APIDOC ## Using SipHash as a Message Authentication Code (MAC) SipHash can be used as a MAC for authenticating short messages. The key must remain secret, and the tag provides integrity verification. ### Code Example (C) ```c #include "siphash.h" #include #include #include // Generate and verify a MAC for a message typedef struct { uint8_t tag[16]; // 128-bit MAC for stronger security } mac_tag_t; // Create a MAC for the given message int create_mac(const void *message, size_t len, const uint8_t key[16], mac_tag_t *tag) { return siphash(message, len, key, tag->tag, 16); } // Verify a MAC - returns 0 if valid, non-zero if invalid int verify_mac(const void *message, size_t len, const uint8_t key[16], const mac_tag_t *tag) { mac_tag_t computed; siphash(message, len, key, computed.tag, 16); // Constant-time comparison to prevent timing attacks uint8_t diff = 0; for (int i = 0; i < 16; i++) { diff |= computed.tag[i] ^ tag->tag[i]; } return diff != 0; } int main() { // Shared secret key (in practice, securely exchanged) uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; const char *message = "Important transaction data"; mac_tag_t tag; // Sender creates MAC create_mac(message, strlen(message), key, &tag); printf("Created MAC: "); for (int i = 0; i < 16; i++) printf("%02x", tag.tag[i]); printf("\n"); // Receiver verifies MAC if (verify_mac(message, strlen(message), key, &tag) == 0) { printf("MAC verification: VALID\n"); } else { printf("MAC verification: INVALID - message may be tampered!\n"); } // Test with tampered message const char *tampered = "Important transaction datb"; if (verify_mac(tampered, strlen(tampered), key, &tag) == 0) { printf("Tampered MAC verification: VALID (unexpected!)\n"); } else { printf("Tampered MAC verification: INVALID (expected)\n"); } return 0; } // Compile: gcc -Wall --std=c99 siphash.c mac_example.c -o mac_example // Expected output: // Created MAC: <32 hex characters> // MAC verification: VALID // Tampered MAC verification: INVALID (expected) ``` ``` -------------------------------- ### SipHash API Reference Source: https://context7.com/veorq/siphash/llms.txt Reference for the SipHash and HalfSipHash functions, detailing their parameters, output sizes, and return values. ```APIDOC ## API Reference | Function | Key Size | Output Size | Description | |----------|----------|-------------|-------------| | `siphash(in, inlen, k, out, outlen)` | 16 bytes | 8 or 16 bytes | Full SipHash with 64-bit operations | | `halfsiphash(in, inlen, k, out, outlen)` | 8 bytes | 4 or 8 bytes | HalfSipHash with 32-bit operations | ### Parameters - `in`: Pointer to input data (read-only, any length) - `inlen`: Length of input data in bytes - `k`: Pointer to secret key (16 bytes for siphash, 8 bytes for halfsiphash) - `out`: Pointer to output buffer (must be pre-allocated) - `outlen`: Output length (8 or 16 for siphash; 4 or 8 for halfsiphash) - **Returns**: 0 on success ``` -------------------------------- ### Run SipHash Debug Tests Source: https://github.com/veorq/siphash/blob/master/README.md Execute the debug version of the SipHash test suite. This verifies test vectors and prints intermediate values for debugging purposes. ```shell ./debug ``` -------------------------------- ### Compute HalfSipHash Value (32-bit or 64-bit) Source: https://context7.com/veorq/siphash/llms.txt Computes a HalfSipHash value using 32-bit word operations with a 64-bit key, suitable for 32-bit platforms. The output can be 4 bytes (32-bit) or 8 bytes (64-bit). Ensure 'halfsiphash.h' is included and the function returns 0 on success. ```c #include "halfsiphash.h" #include #include #include int main() { // 64-bit secret key (8 bytes) for HalfSipHash uint8_t key[8] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; // Input message const char *message = "Hello, HalfSipHash!"; size_t msg_len = strlen(message); // Output buffers uint8_t hash32[4]; // 32-bit output uint8_t hash64[8]; // 64-bit output // Compute HalfSipHash-2-4-32 (32-bit output) int result = halfsiphash(message, msg_len, key, hash32, 4); if (result == 0) { printf("HalfSipHash-2-4-32: "); for (int i = 0; i < 4; i++) { printf("%02x", hash32[i]); } printf("\n"); } // Compute HalfSipHash-2-4-64 (64-bit output) result = halfsiphash(message, msg_len, key, hash64, 8); if (result == 0) { printf("HalfSipHash-2-4-64: "); for (int i = 0; i < 8; i++) { printf("%02x", hash64[i]); } printf("\n"); } return 0; } // Compile: gcc -Wall --std=c99 halfsiphash.c example.c -o example // Expected output format: // HalfSipHash-2-4-32: <8 hex characters> // HalfSipHash-2-4-64: <16 hex characters> ``` -------------------------------- ### Compile SipHash with Custom Rounds Source: https://github.com/veorq/siphash/blob/master/README.md Compile the SipHash C code, specifying the number of compression and finalization rounds using -D arguments. This allows customization of the SipHash variant. ```shell gcc -Wall --std=c99 -DcROUNDS=2 -DdROUNDS=4 siphash.c halfsiphash.c test.c -o test ``` -------------------------------- ### Configuring Custom Round Counts and Building Source: https://context7.com/veorq/siphash/llms.txt Customize the number of compression (cROUNDS) and finalization (dROUNDS) rounds at compile time. The default is SipHash-2-4. Use 'make debug' for a debug build with tracing, or 'make vectors' to generate test vectors. The 'test' executable verifies all variants against 64 test vectors. ```bash # Build with default SipHash-2-4 make # Build with custom rounds (e.g., SipHash-4-8 for higher security margin) make cROUNDS=4 dROUNDS=8 # Or compile directly with gcc gcc -Wall --std=c99 -DcROUNDS=4 -DdROUNDS=8 siphash.c halfsiphash.c test.c testmain.c -o test # Build debug version with intermediate value tracing make debug # Build vectors generator make vectors # Run tests (verifies 64 test vectors for all variants) ./test # Expected output: # SipHash-2-4-64 # OK # SipHash-2-4-128 # OK # HalfSipHash-2-4-32 # OK # HalfSipHash-2-4-64 # OK ``` -------------------------------- ### Compute SipHash-2-4 Value (64-bit or 128-bit) Source: https://context7.com/veorq/siphash/llms.txt Computes a SipHash-2-4 value using 64-bit word operations with a 128-bit key. The output can be 8 bytes (64-bit) or 16 bytes (128-bit). Ensure 'siphash.h' is included and the function returns 0 on success. ```c #include "siphash.h" #include #include #include int main() { // 128-bit secret key (16 bytes) uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f }; // Input message const char *message = "Hello, SipHash!"; size_t msg_len = strlen(message); // Output buffers uint8_t hash64[8]; // 64-bit output uint8_t hash128[16]; // 128-bit output // Compute SipHash-2-4-64 (64-bit output) int result = siphash(message, msg_len, key, hash64, 8); if (result == 0) { printf("SipHash-2-4-64: "); for (int i = 0; i < 8; i++) { printf("%02x", hash64[i]); } printf("\n"); } // Compute SipHash-2-4-128 (128-bit output) result = siphash(message, msg_len, key, hash128, 16); if (result == 0) { printf("SipHash-2-4-128: "); for (int i = 0; i < 16; i++) { printf("%02x", hash128[i]); } printf("\n"); } return 0; } // Compile: gcc -Wall --std=c99 siphash.c example.c -o example // Expected output format: // SipHash-2-4-64: <16 hex characters> // SipHash-2-4-128: <32 hex characters> ``` -------------------------------- ### Hash Strings for Hash Table Protection in C Source: https://context7.com/veorq/siphash/llms.txt Use this C code to hash strings for use in hash tables. It generates a random key and uses SipHash to compute a hash value, ensuring protection against algorithmic complexity attacks. Ensure a cryptographically secure random source is used in production. ```c #include "siphash.h" #include #include #include #include #define HASH_TABLE_SIZE 1024 // Generate a random key at application startup void generate_random_key(uint8_t *key, size_t len) { // In production, use a cryptographically secure random source // e.g., /dev/urandom on Unix or CryptGenRandom on Windows FILE *f = fopen("/dev/urandom", "rb"); if (f) { fread(key, 1, len, f); fclose(f); } } // Hash function for strings using SipHash uint32_t hash_string(const char *str, const uint8_t *key) { uint8_t out[8]; siphash(str, strlen(str), key, out, 8); // Convert first 4 bytes to uint32_t for table indexing uint32_t hash = (uint32_t)out[0] | ((uint32_t)out[1] << 8) | ((uint32_t)out[2] << 16) | ((uint32_t)out[3] << 24); return hash % HASH_TABLE_SIZE; } int main() { // Initialize secret key (should be done once at startup) uint8_t key[16]; generate_random_key(key, 16); // Example: hash some strings const char *strings[] = {"apple", "banana", "cherry", "date", "elderberry"}; int num_strings = sizeof(strings) / sizeof(strings[0]); printf("Hash table slot assignments:\n"); for (int i = 0; i < num_strings; i++) { uint32_t slot = hash_string(strings[i], key); printf(" '%s' -> slot %u\n", strings[i], slot); } return 0; } // Compile: gcc -Wall --std=c99 siphash.c hashtable_example.c -o hashtable_example ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.