/* $OpenBSD$ */ /* * Copyright (c) 2009 The DragonFly Project. All rights reserved. * * This code is derived from software contributed to The DragonFly Project * by Sepherosa Ziehau * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * 3. Neither the name of The DragonFly Project nor the names of its * contributors may be used to endorse or promote products derived * from this software without specific, prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ /* * 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. */ #include #include #include #include #include #include /* * toeplitz byte cache */ void toeplitz_bcache_init(struct toeplitz_bcache *bcache, uint8_t k) { uint32_t key[NBBY]; unsigned int j, b, shift, val; bzero(key, sizeof(key)); /* * Calculate 32bit keys for one byte; one key for each bit. */ for (b = 0; b < NBBY; ++b) { for (j = 0; j < 32; ++j) { unsigned int bit; bit = b + j; shift = NBBY - (bit % NBBY) - 1; if (k & (1 << shift)) key[b] |= 1 << (31 - j); } } /* * Cache the results of all possible bit combination of * one byte. */ for (val = 0; val < 256; ++val) { uint32_t res = 0; for (b = 0; b < NBBY; ++b) { shift = NBBY - b - 1; if (val & (1 << shift)) res ^= key[b]; } bcache->bytes[val] = res; } } /* * symmetric toeplitz */ static struct stoeplitz_cache stoeplitz_syskey_cache; const struct stoeplitz_cache *const stoeplitz_syskey = &stoeplitz_syskey_cache; stoeplitz_key stoeplitz_syskeyseed = STOEPLITZ_KEYSEED; void stoeplitz_init(void) { static int initialized = 0; if (initialized) return; stoeplitz_cache_init(&stoeplitz_syskey_cache, stoeplitz_syskeyseed); initialized = 1; } void stoeplitz_cache_init(struct stoeplitz_cache *scache, stoeplitz_key skey) { skey = stoeplitz_htokey(skey); toeplitz_bcache_init(&scache->bcache[0], skey >> 8); toeplitz_bcache_init(&scache->bcache[1], skey); } uint32_t stoeplitz_ip4(const struct stoeplitz_cache *scache, in_addr_t faddr, in_addr_t laddr) { uint32_t res = 0; res ^= toeplitz_bcache_byte(&scache->bcache[0], faddr >> 0); res ^= toeplitz_bcache_byte(&scache->bcache[0], faddr >> 16); res ^= toeplitz_bcache_byte(&scache->bcache[0], laddr >> 0); res ^= toeplitz_bcache_byte(&scache->bcache[0], laddr >> 16); res ^= toeplitz_bcache_byte(&scache->bcache[1], faddr >> 8); res ^= toeplitz_bcache_byte(&scache->bcache[1], faddr >> 24); res ^= toeplitz_bcache_byte(&scache->bcache[1], laddr >> 8); res ^= toeplitz_bcache_byte(&scache->bcache[1], laddr >> 24); return (res); } uint32_t stoeplitz_ip4port(const struct stoeplitz_cache *scache, in_addr_t faddr, in_addr_t laddr, in_port_t fport, in_port_t lport) { uint32_t res = 0; res ^= toeplitz_bcache_byte(&scache->bcache[0], faddr >> 0); res ^= toeplitz_bcache_byte(&scache->bcache[0], faddr >> 16); res ^= toeplitz_bcache_byte(&scache->bcache[0], laddr >> 0); res ^= toeplitz_bcache_byte(&scache->bcache[0], laddr >> 16); res ^= toeplitz_bcache_byte(&scache->bcache[0], fport >> 0); res ^= toeplitz_bcache_byte(&scache->bcache[0], lport >> 0); res ^= toeplitz_bcache_byte(&scache->bcache[1], faddr >> 8); res ^= toeplitz_bcache_byte(&scache->bcache[1], faddr >> 24); res ^= toeplitz_bcache_byte(&scache->bcache[1], laddr >> 8); res ^= toeplitz_bcache_byte(&scache->bcache[1], laddr >> 24); res ^= toeplitz_bcache_byte(&scache->bcache[1], fport >> 8); res ^= toeplitz_bcache_byte(&scache->bcache[1], lport >> 8); return (res); } uint32_t stoeplitz_ip6(const struct stoeplitz_cache *scache, const struct in6_addr *faddr, const struct in6_addr * laddr) { uint32_t res = 0; unsigned int i; for (i = 0; i < nitems(faddr->s6_addr32); i++) { res ^= toeplitz_bcache_byte(&scache->bcache[0], faddr->s6_addr32[i] >> 0); res ^= toeplitz_bcache_byte(&scache->bcache[0], faddr->s6_addr32[i] >> 16); res ^= toeplitz_bcache_byte(&scache->bcache[0], laddr->s6_addr32[i] >> 0); res ^= toeplitz_bcache_byte(&scache->bcache[0], laddr->s6_addr32[i] >> 16); } for (i = 0; i < nitems(faddr->s6_addr32); i++) { res ^= toeplitz_bcache_byte(&scache->bcache[1], faddr->s6_addr32[i] >> 8); res ^= toeplitz_bcache_byte(&scache->bcache[1], faddr->s6_addr32[i] >> 24); res ^= toeplitz_bcache_byte(&scache->bcache[1], laddr->s6_addr32[i] >> 8); res ^= toeplitz_bcache_byte(&scache->bcache[1], laddr->s6_addr32[i] >> 24); } return (res); } uint32_t stoeplitz_ip6port(const struct stoeplitz_cache *scache, const struct in6_addr *faddr, const struct in6_addr * laddr, in_port_t fport, in_port_t lport) { uint32_t res = 0; unsigned int i; for (i = 0; i < nitems(faddr->s6_addr32); i++) { res ^= toeplitz_bcache_byte(&scache->bcache[0], faddr->s6_addr32[i] >> 0); res ^= toeplitz_bcache_byte(&scache->bcache[0], faddr->s6_addr32[i] >> 16); res ^= toeplitz_bcache_byte(&scache->bcache[0], laddr->s6_addr32[i] >> 0); res ^= toeplitz_bcache_byte(&scache->bcache[0], laddr->s6_addr32[i] >> 16); } res ^= toeplitz_bcache_byte(&scache->bcache[0], fport >> 0); res ^= toeplitz_bcache_byte(&scache->bcache[0], lport >> 0); for (i = 0; i < nitems(faddr->s6_addr32); i++) { res ^= toeplitz_bcache_byte(&scache->bcache[1], faddr->s6_addr32[i] >> 8); res ^= toeplitz_bcache_byte(&scache->bcache[1], faddr->s6_addr32[i] >> 24); res ^= toeplitz_bcache_byte(&scache->bcache[1], laddr->s6_addr32[i] >> 8); res ^= toeplitz_bcache_byte(&scache->bcache[1], laddr->s6_addr32[i] >> 24); } res ^= toeplitz_bcache_byte(&scache->bcache[1], fport >> 8); res ^= toeplitz_bcache_byte(&scache->bcache[1], lport >> 8); return (res); } void stoeplitz_tokey(uint8_t *k, size_t klen, stoeplitz_key skey) { skey = stoeplitz_htokey(skey); size_t i; KASSERT((klen % 2) == 0); for (i = 0; i < skey; i += sizeof(skey)) { k[i + 0] = skey >> 8; k[i + 1] = skey; } }