toeplitz.c revision 1.1.4.2 1 1.1.4.2 thorpej /* $OpenBSD: toeplitz.c,v 1.9 2020/09/01 19:18:26 tb Exp $ */
2 1.1.4.2 thorpej
3 1.1.4.2 thorpej /*
4 1.1.4.2 thorpej * Copyright (c) 2009 The DragonFly Project. All rights reserved.
5 1.1.4.2 thorpej *
6 1.1.4.2 thorpej * This code is derived from software contributed to The DragonFly Project
7 1.1.4.2 thorpej * by Sepherosa Ziehau <sepherosa (at) gmail.com>
8 1.1.4.2 thorpej *
9 1.1.4.2 thorpej * Redistribution and use in source and binary forms, with or without
10 1.1.4.2 thorpej * modification, are permitted provided that the following conditions
11 1.1.4.2 thorpej * are met:
12 1.1.4.2 thorpej *
13 1.1.4.2 thorpej * 1. Redistributions of source code must retain the above copyright
14 1.1.4.2 thorpej * notice, this list of conditions and the following disclaimer.
15 1.1.4.2 thorpej * 2. Redistributions in binary form must reproduce the above copyright
16 1.1.4.2 thorpej * notice, this list of conditions and the following disclaimer in
17 1.1.4.2 thorpej * the documentation and/or other materials provided with the
18 1.1.4.2 thorpej * distribution.
19 1.1.4.2 thorpej * 3. Neither the name of The DragonFly Project nor the names of its
20 1.1.4.2 thorpej * contributors may be used to endorse or promote products derived
21 1.1.4.2 thorpej * from this software without specific, prior written permission.
22 1.1.4.2 thorpej *
23 1.1.4.2 thorpej * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 1.1.4.2 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 1.1.4.2 thorpej * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 1.1.4.2 thorpej * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 1.1.4.2 thorpej * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 1.1.4.2 thorpej * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 1.1.4.2 thorpej * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 1.1.4.2 thorpej * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 1.1.4.2 thorpej * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 1.1.4.2 thorpej * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 1.1.4.2 thorpej * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1.4.2 thorpej * SUCH DAMAGE.
35 1.1.4.2 thorpej */
36 1.1.4.2 thorpej
37 1.1.4.2 thorpej /*
38 1.1.4.2 thorpej * Copyright (c) 2019 David Gwynne <dlg (at) openbsd.org>
39 1.1.4.2 thorpej * Copyright (c) 2020 Theo Buehler <tb (at) openbsd.org>
40 1.1.4.2 thorpej *
41 1.1.4.2 thorpej * Permission to use, copy, modify, and distribute this software for any
42 1.1.4.2 thorpej * purpose with or without fee is hereby granted, provided that the above
43 1.1.4.2 thorpej * copyright notice and this permission notice appear in all copies.
44 1.1.4.2 thorpej *
45 1.1.4.2 thorpej * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
46 1.1.4.2 thorpej * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
47 1.1.4.2 thorpej * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
48 1.1.4.2 thorpej * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
49 1.1.4.2 thorpej * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
50 1.1.4.2 thorpej * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
51 1.1.4.2 thorpej * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
52 1.1.4.2 thorpej */
53 1.1.4.2 thorpej
54 1.1.4.2 thorpej #include <sys/param.h>
55 1.1.4.2 thorpej #include <sys/systm.h>
56 1.1.4.2 thorpej #include <sys/kernel.h>
57 1.1.4.2 thorpej #include <sys/sysctl.h>
58 1.1.4.2 thorpej #include <sys/cprng.h>
59 1.1.4.2 thorpej
60 1.1.4.2 thorpej #include <netinet/in.h>
61 1.1.4.2 thorpej
62 1.1.4.2 thorpej #include <net/toeplitz.h>
63 1.1.4.2 thorpej
64 1.1.4.2 thorpej /*
65 1.1.4.2 thorpej * symmetric toeplitz
66 1.1.4.2 thorpej */
67 1.1.4.2 thorpej
68 1.1.4.2 thorpej static stoeplitz_key stoeplitz_keyseed = STOEPLITZ_KEYSEED;
69 1.1.4.2 thorpej static struct stoeplitz_cache stoeplitz_syskey_cache;
70 1.1.4.2 thorpej const struct stoeplitz_cache *const
71 1.1.4.2 thorpej stoeplitz_cache = &stoeplitz_syskey_cache;
72 1.1.4.2 thorpej
73 1.1.4.2 thorpej /* parity of n16: count (mod 2) of ones in the binary representation. */
74 1.1.4.2 thorpej static int
75 1.1.4.2 thorpej parity(uint16_t n16)
76 1.1.4.2 thorpej {
77 1.1.4.2 thorpej n16 = ((n16 & 0xaaaa) >> 1) ^ (n16 & 0x5555);
78 1.1.4.2 thorpej n16 = ((n16 & 0xcccc) >> 2) ^ (n16 & 0x3333);
79 1.1.4.2 thorpej n16 = ((n16 & 0xf0f0) >> 4) ^ (n16 & 0x0f0f);
80 1.1.4.2 thorpej n16 = ((n16 & 0xff00) >> 8) ^ (n16 & 0x00ff);
81 1.1.4.2 thorpej
82 1.1.4.2 thorpej return (n16);
83 1.1.4.2 thorpej }
84 1.1.4.2 thorpej
85 1.1.4.2 thorpej /*
86 1.1.4.2 thorpej * The Toeplitz matrix obtained from a seed is invertible if and only if the
87 1.1.4.2 thorpej * parity of the seed is 1. Generate such a seed uniformly at random.
88 1.1.4.2 thorpej */
89 1.1.4.2 thorpej static stoeplitz_key
90 1.1.4.2 thorpej stoeplitz_random_seed(void)
91 1.1.4.2 thorpej {
92 1.1.4.2 thorpej stoeplitz_key seed;
93 1.1.4.2 thorpej
94 1.1.4.2 thorpej seed = cprng_strong32() & UINT16_MAX;
95 1.1.4.2 thorpej if (parity(seed) == 0)
96 1.1.4.2 thorpej seed ^= 1;
97 1.1.4.2 thorpej
98 1.1.4.2 thorpej return (seed);
99 1.1.4.2 thorpej }
100 1.1.4.2 thorpej
101 1.1.4.2 thorpej void
102 1.1.4.2 thorpej stoeplitz_init(void)
103 1.1.4.2 thorpej {
104 1.1.4.2 thorpej stoeplitz_keyseed = stoeplitz_random_seed();
105 1.1.4.2 thorpej stoeplitz_cache_init(&stoeplitz_syskey_cache, stoeplitz_keyseed);
106 1.1.4.2 thorpej }
107 1.1.4.2 thorpej
108 1.1.4.2 thorpej #define NBSK (NBBY * sizeof(stoeplitz_key))
109 1.1.4.2 thorpej
110 1.1.4.2 thorpej /*
111 1.1.4.2 thorpej * The Toeplitz hash of a 16-bit number considered as a column vector over
112 1.1.4.2 thorpej * the field with two elements is calculated as a matrix multiplication with
113 1.1.4.2 thorpej * a 16x16 circulant Toeplitz matrix T generated by skey.
114 1.1.4.2 thorpej *
115 1.1.4.2 thorpej * The first eight columns H of T generate the remaining eight columns using
116 1.1.4.2 thorpej * the byteswap operation J = swap16: T = [H JH]. Thus, the Toeplitz hash of
117 1.1.4.2 thorpej * n = [hi lo] is computed via the formula T * n = (H * hi) ^ swap16(H * lo).
118 1.1.4.2 thorpej *
119 1.1.4.2 thorpej * Therefore the results H * val for all values of a byte are cached in scache.
120 1.1.4.2 thorpej */
121 1.1.4.2 thorpej void
122 1.1.4.2 thorpej stoeplitz_cache_init(struct stoeplitz_cache *scache, stoeplitz_key skey)
123 1.1.4.2 thorpej {
124 1.1.4.2 thorpej uint16_t column[NBBY];
125 1.1.4.2 thorpej unsigned int b, shift, val;
126 1.1.4.2 thorpej
127 1.1.4.2 thorpej bzero(column, sizeof(column));
128 1.1.4.2 thorpej
129 1.1.4.2 thorpej /* Calculate the first eight columns H of the Toeplitz matrix T. */
130 1.1.4.2 thorpej for (b = 0; b < NBBY; ++b)
131 1.1.4.2 thorpej column[b] = skey << b | skey >> (NBSK - b);
132 1.1.4.2 thorpej
133 1.1.4.2 thorpej /* Cache the results of H * val for all possible values of a byte. */
134 1.1.4.2 thorpej for (val = 0; val < 256; ++val) {
135 1.1.4.2 thorpej uint16_t res = 0;
136 1.1.4.2 thorpej
137 1.1.4.2 thorpej for (b = 0; b < NBBY; ++b) {
138 1.1.4.2 thorpej shift = NBBY - b - 1;
139 1.1.4.2 thorpej if (val & (1 << shift))
140 1.1.4.2 thorpej res ^= column[b];
141 1.1.4.2 thorpej }
142 1.1.4.2 thorpej scache->bytes[val] = res;
143 1.1.4.2 thorpej }
144 1.1.4.2 thorpej }
145 1.1.4.2 thorpej
146 1.1.4.2 thorpej uint16_t
147 1.1.4.2 thorpej stoeplitz_hash_ip4(const struct stoeplitz_cache *scache,
148 1.1.4.2 thorpej in_addr_t faddr, in_addr_t laddr)
149 1.1.4.2 thorpej {
150 1.1.4.2 thorpej return (stoeplitz_hash_n32(scache, faddr ^ laddr));
151 1.1.4.2 thorpej }
152 1.1.4.2 thorpej
153 1.1.4.2 thorpej uint16_t
154 1.1.4.2 thorpej stoeplitz_hash_ip4port(const struct stoeplitz_cache *scache,
155 1.1.4.2 thorpej in_addr_t faddr, in_addr_t laddr, in_port_t fport, in_port_t lport)
156 1.1.4.2 thorpej {
157 1.1.4.2 thorpej return (stoeplitz_hash_n32(scache, faddr ^ laddr ^ fport ^ lport));
158 1.1.4.2 thorpej }
159 1.1.4.2 thorpej
160 1.1.4.2 thorpej #ifdef INET6
161 1.1.4.2 thorpej uint16_t
162 1.1.4.2 thorpej stoeplitz_hash_ip6(const struct stoeplitz_cache *scache,
163 1.1.4.2 thorpej const struct in6_addr *faddr6, const struct in6_addr *laddr6)
164 1.1.4.2 thorpej {
165 1.1.4.2 thorpej uint32_t n32 = 0;
166 1.1.4.2 thorpej size_t i;
167 1.1.4.2 thorpej
168 1.1.4.2 thorpej for (i = 0; i < nitems(faddr6->s6_addr32); i++)
169 1.1.4.2 thorpej n32 ^= faddr6->s6_addr32[i] ^ laddr6->s6_addr32[i];
170 1.1.4.2 thorpej
171 1.1.4.2 thorpej return (stoeplitz_hash_n32(scache, n32));
172 1.1.4.2 thorpej }
173 1.1.4.2 thorpej
174 1.1.4.2 thorpej uint16_t
175 1.1.4.2 thorpej stoeplitz_hash_ip6port(const struct stoeplitz_cache *scache,
176 1.1.4.2 thorpej const struct in6_addr *faddr6, const struct in6_addr *laddr6,
177 1.1.4.2 thorpej in_port_t fport, in_port_t lport)
178 1.1.4.2 thorpej {
179 1.1.4.2 thorpej uint32_t n32 = 0;
180 1.1.4.2 thorpej size_t i;
181 1.1.4.2 thorpej
182 1.1.4.2 thorpej for (i = 0; i < nitems(faddr6->s6_addr32); i++)
183 1.1.4.2 thorpej n32 ^= faddr6->s6_addr32[i] ^ laddr6->s6_addr32[i];
184 1.1.4.2 thorpej
185 1.1.4.2 thorpej n32 ^= fport ^ lport;
186 1.1.4.2 thorpej
187 1.1.4.2 thorpej return (stoeplitz_hash_n32(scache, n32));
188 1.1.4.2 thorpej }
189 1.1.4.2 thorpej #endif /* INET6 */
190 1.1.4.2 thorpej
191 1.1.4.2 thorpej void
192 1.1.4.2 thorpej stoeplitz_to_key(void *key, size_t klen)
193 1.1.4.2 thorpej {
194 1.1.4.2 thorpej uint8_t *k = key;
195 1.1.4.2 thorpej uint16_t skey = htons(stoeplitz_keyseed);
196 1.1.4.2 thorpej size_t i;
197 1.1.4.2 thorpej
198 1.1.4.2 thorpej KASSERT((klen % 2) == 0);
199 1.1.4.2 thorpej
200 1.1.4.2 thorpej for (i = 0; i < klen; i += sizeof(skey)) {
201 1.1.4.2 thorpej k[i + 0] = skey >> 8;
202 1.1.4.2 thorpej k[i + 1] = skey;
203 1.1.4.2 thorpej }
204 1.1.4.2 thorpej }
205