xref: /freebsd/sys/opencrypto/gmac.c (revision 9768746b)
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 	memset(agc->counter, 0, sizeof(agc->counter));
74 	bcopy(iv, agc->counter, ivlen);
75 	agc->counter[GMAC_BLOCK_LEN - 1] = 1;
76 
77 	memset(&agc->hash, 0, sizeof(agc->hash));
78 }
79 
80 int
81 AES_GMAC_Update(void *ctx, const void *vdata, u_int len)
82 {
83 	struct aes_gmac_ctx *agc;
84 	const uint8_t *data;
85 	struct gf128 v;
86 	uint8_t buf[GMAC_BLOCK_LEN] = {};
87 	int i;
88 
89 	agc = ctx;
90 	data = vdata;
91 	v = agc->hash;
92 
93 	while (len > 0) {
94 		if (len >= 4*GMAC_BLOCK_LEN) {
95 			i = 4*GMAC_BLOCK_LEN;
96 			v = gf128_mul4b(v, data, &agc->ghashtbl);
97 		} else if (len >= GMAC_BLOCK_LEN) {
98 			i = GMAC_BLOCK_LEN;
99 			v = gf128_add(v, gf128_read(data));
100 			v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
101 		} else {
102 			i = len;
103 			bcopy(data, buf, i);
104 			v = gf128_add(v, gf128_read(&buf[0]));
105 			v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
106 			explicit_bzero(buf, sizeof buf);
107 		}
108 		len -= i;
109 		data += i;
110 	}
111 
112 	agc->hash = v;
113 	explicit_bzero(&v, sizeof v);
114 
115 	return (0);
116 }
117 
118 void
119 AES_GMAC_Final(uint8_t *digest, void *ctx)
120 {
121 	struct aes_gmac_ctx *agc;
122 	uint8_t enccntr[GMAC_BLOCK_LEN];
123 	struct gf128 a;
124 
125 	agc = ctx;
126 
127 	rijndaelEncrypt(agc->keysched, agc->rounds, agc->counter, enccntr);
128 	a = gf128_add(agc->hash, gf128_read(enccntr));
129 	gf128_write(a, digest);
130 
131 	explicit_bzero(enccntr, sizeof enccntr);
132 }
133