hash.h (3119B)
1 /* Copyright (C) 2013-2023, 2025 Vincent Forest (vaplv@free.fr) 2 * 3 * The RSys library is free software: you can redistribute it and/or modify 4 * it under the terms of the GNU General Public License as published 5 * by the Free Software Foundation, either version 3 of the License, or 6 * (at your option) any later version. 7 * 8 * The RSys library is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with the RSys library. If not, see <http://www.gnu.org/licenses/>. */ 15 16 #ifndef HASH_H 17 #define HASH_H 18 19 #include "rsys.h" 20 21 struct mem_allocator; 22 23 struct sha256_ctx { 24 char chunk[64]; 25 uint32_t len; /* #bytes registered in chunk */ 26 uint32_t state[8]; /* Current hash state */ 27 uint64_t nbits; /* Overall size of the message in bits */ 28 }; 29 30 typedef char hash256_T[32]; 31 32 struct chunked_data_desc { 33 /* Get the data per chunk of 512 bits. The last chunk length can be lesser 34 * than 512 bits */ 35 void (*get_chunk512) 36 (char dst[64], /* Destination */ 37 const size_t ichunk512, /* Indices of the chunck of 512 bits to fetch */ 38 void* ctx); /* User defined variable */ 39 size_t size; /* Size in bytes of the data */ 40 void* context; /* User defined variable */ 41 }; 42 #define CHUNKED_DATA_DESC_NULL__ {NULL, 0, NULL} 43 static const struct chunked_data_desc CHUNKED_DATA_DESC_NULL = 44 CHUNKED_DATA_DESC_NULL__; 45 46 /* 32-bits Fowler/Noll/Vo hash function */ 47 static INLINE uint32_t 48 hash_fnv32(const void* data, const size_t len) 49 { 50 const uint32_t FNV32_PRIME = 51 (uint32_t)(((uint32_t)1<<24) + ((uint32_t)1<<8) + 0x93); 52 const uint32_t OFFSET32_BASIS = 2166136261u; 53 const char* octets = (const char*)data; 54 uint32_t hash = OFFSET32_BASIS; 55 size_t i; 56 ASSERT(data); 57 58 FOR_EACH(i, 0, len) { 59 hash = hash ^ (uint32_t)((unsigned char)octets[i]); 60 hash = hash * FNV32_PRIME; 61 } 62 return hash; 63 } 64 65 /* 64-bits Fowler/Noll/Vo hash function */ 66 static INLINE uint64_t 67 hash_fnv64(const void* data, const size_t len) 68 { 69 const uint64_t FNV64_PRIME = 70 (uint64_t)(((uint64_t)1<<40) + ((uint64_t)1<<8) + 0xB3); 71 const uint64_t OFFSET64_BASIS = (uint64_t)14695981039346656037u; 72 const char* octets = (const char*)data; 73 uint64_t hash = OFFSET64_BASIS; 74 size_t i; 75 ASSERT(data); 76 77 FOR_EACH(i, 0, len) { 78 hash = hash ^ (uint64_t)((unsigned char)octets[i]); 79 hash = hash * FNV64_PRIME; 80 } 81 return hash; 82 } 83 84 BEGIN_DECLS 85 86 RSYS_API void 87 sha256_ctx_init 88 (struct sha256_ctx* ctx); 89 90 RSYS_API void 91 sha256_ctx_update 92 (struct sha256_ctx* ctx, 93 const char* bytes, 94 const size_t len); /* #bytes */ 95 96 RSYS_API void 97 sha256_ctx_finalize 98 (struct sha256_ctx* ctx, 99 hash256_T hash); 100 101 RSYS_API void 102 hash_sha256 103 (const void* data, 104 const size_t len, 105 hash256_T hash); 106 107 RSYS_API void 108 hash256_to_cstr 109 (const hash256_T hash, 110 char cstr[65]); 111 112 RSYS_API int 113 hash256_eq 114 (const hash256_T hash0, 115 const hash256_T hash1); 116 117 END_DECLS 118 119 #endif /* HASH_H */