xref: /freebsd/sys/opencrypto/gmac.c (revision c1d255d3)
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 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <opencrypto/gfmult.h>
35 #include <opencrypto/gmac.h>
36 
37 void
38 AES_GMAC_Init(void *ctx)
39 {
40 	struct aes_gmac_ctx *agc;
41 
42 	agc = ctx;
43 	bzero(agc, sizeof *agc);
44 }
45 
46 void
47 AES_GMAC_Setkey(void *ctx, const uint8_t *key, u_int klen)
48 {
49 	struct aes_gmac_ctx *agc;
50 	const uint8_t zeros[GMAC_BLOCK_LEN] = {};
51 	struct gf128 h;
52 	uint8_t hbuf[GMAC_BLOCK_LEN];
53 
54 	agc = ctx;
55 	agc->rounds = rijndaelKeySetupEnc(agc->keysched, key, klen * 8);
56 
57 	rijndaelEncrypt(agc->keysched, agc->rounds, zeros, hbuf);
58 
59 	h = gf128_read(hbuf);
60 	gf128_genmultable4(h, &agc->ghashtbl);
61 
62 	explicit_bzero(&h, sizeof h);
63 	explicit_bzero(hbuf, sizeof hbuf);
64 }
65 
66 void
67 AES_GMAC_Reinit(void *ctx, const uint8_t *iv, u_int ivlen)
68 {
69 	struct aes_gmac_ctx *agc;
70 
71 	agc = ctx;
72 	KASSERT(ivlen <= sizeof agc->counter, ("passed ivlen too large!"));
73 	bcopy(iv, agc->counter, ivlen);
74 }
75 
76 int
77 AES_GMAC_Update(void *ctx, const void *vdata, u_int len)
78 {
79 	struct aes_gmac_ctx *agc;
80 	const uint8_t *data;
81 	struct gf128 v;
82 	uint8_t buf[GMAC_BLOCK_LEN] = {};
83 	int i;
84 
85 	agc = ctx;
86 	data = vdata;
87 	v = agc->hash;
88 
89 	while (len > 0) {
90 		if (len >= 4*GMAC_BLOCK_LEN) {
91 			i = 4*GMAC_BLOCK_LEN;
92 			v = gf128_mul4b(v, data, &agc->ghashtbl);
93 		} else if (len >= GMAC_BLOCK_LEN) {
94 			i = GMAC_BLOCK_LEN;
95 			v = gf128_add(v, gf128_read(data));
96 			v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
97 		} else {
98 			i = len;
99 			bcopy(data, buf, i);
100 			v = gf128_add(v, gf128_read(&buf[0]));
101 			v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
102 			explicit_bzero(buf, sizeof buf);
103 		}
104 		len -= i;
105 		data += i;
106 	}
107 
108 	agc->hash = v;
109 	explicit_bzero(&v, sizeof v);
110 
111 	return (0);
112 }
113 
114 void
115 AES_GMAC_Final(uint8_t *digest, void *ctx)
116 {
117 	struct aes_gmac_ctx *agc;
118 	uint8_t enccntr[GMAC_BLOCK_LEN];
119 	struct gf128 a;
120 
121 	/* XXX - zero additional bytes? */
122 	agc = ctx;
123 	agc->counter[GMAC_BLOCK_LEN - 1] = 1;
124 
125 	rijndaelEncrypt(agc->keysched, agc->rounds, agc->counter, enccntr);
126 	a = gf128_add(agc->hash, gf128_read(enccntr));
127 	gf128_write(a, digest);
128 
129 	explicit_bzero(enccntr, sizeof enccntr);
130 }
131