xref: /freebsd/sys/opencrypto/gfmult.h (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * Copyright (c) 2014 The FreeBSD Foundation
3  *
4  * This software was developed by John-Mark Gurney under
5  * the sponsorship of the FreeBSD Foundation and
6  * Rubicon Communications, LLC (Netgate).
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1.  Redistributions of source code must retain the above copyright
11  *     notice, this list of conditions and the following disclaimer.
12  * 2.  Redistributions in binary form must reproduce the above copyright
13  *     notice, this list of conditions and the following disclaimer in the
14  *     documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  *	$FreeBSD$
29  *
30  */
31 
32 #ifndef _GFMULT_H_
33 #define _GFMULT_H_
34 
35 #ifdef __APPLE__
36 #define	__aligned(x)    __attribute__((__aligned__(x)))
37 #define	be64dec(buf)	__builtin_bswap64(*(uint64_t *)buf)
38 #define	be64enc(buf, x)	(*(uint64_t *)buf = __builtin_bswap64(x))
39 #else
40 #include <sys/endian.h>
41 #endif
42 
43 #ifdef _KERNEL
44 #include <sys/types.h>
45 #else
46 #include <stdint.h>
47 #include <strings.h>
48 #endif
49 
50 #define REQ_ALIGN	(16 * 4)
51 /*
52  * The rows are striped across cache lines.  Note that the indexes
53  * are bit reversed to make accesses quicker.
54  */
55 struct gf128table {
56 	uint32_t a[16] __aligned(REQ_ALIGN);	/* bits   0 - 31 */
57 	uint32_t b[16] __aligned(REQ_ALIGN);	/* bits  63 - 32 */
58 	uint32_t c[16] __aligned(REQ_ALIGN);	/* bits  95 - 64 */
59 	uint32_t d[16] __aligned(REQ_ALIGN);	/* bits 127 - 96 */
60 } __aligned(REQ_ALIGN);
61 
62 /*
63  * A set of tables that contain h, h^2, h^3, h^4.  To be used w/ gf128_mul4.
64  */
65 struct gf128table4 {
66 	struct gf128table	tbls[4];
67 };
68 
69 /*
70  * GCM per spec is bit reversed in memory.  So byte 0 is really bit reversed
71  * and contains bits 0-7.  We can deal w/ this by using right shifts and
72  * related math instead of having to bit reverse everything.  This means that
73  * the low bits are in v[0] (bits 0-63) and reverse order, while the high
74  * bits are in v[1] (bits 64-127) and reverse order.  The high bit of v[0] is
75  * bit 0, and the low bit of v[1] is bit 127.
76  */
77 struct gf128 {
78 	uint64_t v[2];
79 };
80 
81 /* Note that we don't bit reverse in MAKE_GF128. */
82 #define MAKE_GF128(a, b)	((struct gf128){.v = { (a), (b) } })
83 #define GF128_EQ(a, b)		((((a).v[0] ^ (b).v[0]) | \
84 				    ((a).v[1] ^ (b).v[1])) == 0)
85 
86 static inline struct gf128
87 gf128_read(const uint8_t *buf)
88 {
89 	struct gf128 r;
90 
91 	r.v[0] = be64dec(buf);
92 	buf += sizeof(uint64_t);
93 
94 	r.v[1] = be64dec(buf);
95 
96 	return r;
97 }
98 
99 static inline void
100 gf128_write(struct gf128 v, uint8_t *buf)
101 {
102 	uint64_t tmp;
103 
104 	be64enc(buf, v.v[0]);
105 	buf += sizeof tmp;
106 
107 	be64enc(buf, v.v[1]);
108 }
109 
110 static inline struct gf128 __pure /* XXX - __pure2 instead */
111 gf128_add(struct gf128 a, struct gf128 b)
112 {
113 	a.v[0] ^= b.v[0];
114 	a.v[1] ^= b.v[1];
115 
116 	return a;
117 }
118 
119 void gf128_genmultable(struct gf128 h, struct gf128table *t);
120 void gf128_genmultable4(struct gf128 h, struct gf128table4 *t);
121 struct gf128 gf128_mul(struct gf128 v, struct gf128table *tbl);
122 struct gf128 gf128_mul4(struct gf128 a, struct gf128 b, struct gf128 c,
123     struct gf128 d, struct gf128table4 *tbl);
124 struct gf128 gf128_mul4b(struct gf128 r, const uint8_t *v,
125     struct gf128table4 *tbl);
126 
127 #endif /* _GFMULT_H_ */
128