/* $OpenBSD$ */ /* * Copyright (c) 2019 David Gwynne * * Permission to use, copy, modify, and distribute this software for any * purpose with or without fee is hereby granted, provided that the above * copyright notice and this permission notice appear in all copies. * * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ /* * toeplitz key byte cache */ struct toeplitz_bcache { uint32_t bytes[256]; }; void toeplitz_bcache_init(struct toeplitz_bcache *, uint8_t); static inline uint32_t toeplitz_bcache_byte(const struct toeplitz_bcache *bcache, uint8_t byte) { return (bcache->bytes[byte]); } /* * symmetric toeplitz */ typedef uint16_t stoeplitz_key; struct stoeplitz_cache { struct toeplitz_bcache bcache[sizeof(stoeplitz_key)]; }; #define STOEPLITZ_KEYSEED 0x6d5a #define stoeplitz_htokey(_h) htons(_h) void stoeplitz_cache_init(struct stoeplitz_cache *, stoeplitz_key); void stoeplitz_tokey(uint8_t *, size_t, stoeplitz_key) __bounded((__buffer__, 1, 2)); uint32_t stoeplitz_cache_ip4(const struct stoeplitz_cache *, in_addr_t, in_addr_t); uint32_t stoeplitz_cache_ip4port(const struct stoeplitz_cache *, in_addr_t, in_addr_t, in_port_t, in_port_t); /* hash a uint16_t in network byte order */ static inline uint32_t stoeplitz_cache_16(const struct stoeplitz_cache *scache, uint16_t u16) { uint32_t h; h = toeplitz_bcache_byte(&scache->bcache[0], u16 >> 8); h ^= toeplitz_bcache_byte(&scache->bcache[1], u16); return (h); } /* hash a uint16_t in host byte order */ static inline uint32_t stoeplitz_cache_h16(const struct stoeplitz_cache *scache, uint16_t u16) { uint32_t h; #if _BYTE_ORDER == _BIG_ENDIAN h = toeplitz_bcache_byte(&scache->bcache[0], u16 >> 8); h ^= toeplitz_bcache_byte(&scache->bcache[1], u16); #else h = toeplitz_bcache_byte(&scache->bcache[0], u16); h ^= toeplitz_bcache_byte(&scache->bcache[1], u16 >> 8); #endif return (h); } /* * system provided symmetric toeplitz */ extern const struct stoeplitz_cache *const stoeplitz_syskey; void stoeplitz_init(void); #define stoeplitz_h16(_h16) stoeplitz_cache_h16(stoeplitz_syskey, (_h16))