xref: /freebsd/sys/opencrypto/gmac.c (revision fcb560670601b2a4d87bb31d7531c8dcc37ee71b)
1 /*-
2  * Copyright (c) 2014 The FreeBSD Foundation
3  * All rights reserved.
4  *
5  * This software was developed by John-Mark Gurney under
6  * the sponsorship of the FreeBSD Foundation and
7  * Rubicon Communications, LLC (Netgate).
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1.  Redistributions of source code must retain the above copyright
12  *     notice, this list of conditions and the following disclaimer.
13  * 2.  Redistributions in binary form must reproduce the above copyright
14  *     notice, this list of conditions and the following disclaimer in the
15  *     documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$FreeBSD$
30  *
31  */
32 
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <opencrypto/gfmult.h>
36 #include <opencrypto/gmac.h>
37 
38 void
39 AES_GMAC_Init(struct aes_gmac_ctx *agc)
40 {
41 
42 	bzero(agc, sizeof *agc);
43 }
44 
45 void
46 AES_GMAC_Setkey(struct aes_gmac_ctx *agc, const uint8_t *key, uint16_t klen)
47 {
48 	const uint8_t zeros[GMAC_BLOCK_LEN] = {};
49 	struct gf128 h;
50 	uint8_t hbuf[GMAC_BLOCK_LEN];
51 
52 	agc->rounds = rijndaelKeySetupEnc(agc->keysched, key, klen * 8);
53 
54 	rijndaelEncrypt(agc->keysched, agc->rounds, zeros, hbuf);
55 
56 	h = gf128_read(hbuf);
57 	gf128_genmultable4(h, &agc->ghashtbl);
58 
59 	explicit_bzero(&h, sizeof h);
60 	explicit_bzero(hbuf, sizeof hbuf);
61 }
62 
63 void
64 AES_GMAC_Reinit(struct aes_gmac_ctx *agc, const uint8_t *iv, uint16_t ivlen)
65 {
66 
67 	KASSERT(ivlen <= sizeof agc->counter, ("passed ivlen too large!"));
68 	bcopy(iv, agc->counter, ivlen);
69 }
70 
71 int
72 AES_GMAC_Update(struct aes_gmac_ctx *agc, const uint8_t *data, uint16_t len)
73 {
74 	struct gf128 v;
75 	uint8_t buf[GMAC_BLOCK_LEN] = {};
76 	int i;
77 
78 	v = agc->hash;
79 
80 	while (len > 0) {
81 		if (len >= 4*GMAC_BLOCK_LEN) {
82 			i = 4*GMAC_BLOCK_LEN;
83 			v = gf128_mul4b(v, data, &agc->ghashtbl);
84 		} else if (len >= GMAC_BLOCK_LEN) {
85 			i = GMAC_BLOCK_LEN;
86 			v = gf128_add(v, gf128_read(data));
87 			v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
88 		} else {
89 			i = len;
90 			bcopy(data, buf, i);
91 			v = gf128_add(v, gf128_read(&buf[0]));
92 			v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
93 			explicit_bzero(buf, sizeof buf);
94 		}
95 		len -= i;
96 		data += i;
97 	}
98 
99 	agc->hash = v;
100 	explicit_bzero(&v, sizeof v);
101 
102 	return (0);
103 }
104 
105 void
106 AES_GMAC_Final(uint8_t digest[GMAC_DIGEST_LEN], struct aes_gmac_ctx *agc)
107 {
108 	uint8_t enccntr[GMAC_BLOCK_LEN];
109 	struct gf128 a;
110 
111 	/* XXX - zero additional bytes? */
112 	agc->counter[GMAC_BLOCK_LEN - 1] = 1;
113 
114 	rijndaelEncrypt(agc->keysched, agc->rounds, agc->counter, enccntr);
115 	a = gf128_add(agc->hash, gf128_read(enccntr));
116 	gf128_write(a, digest);
117 
118 	explicit_bzero(enccntr, sizeof enccntr);
119 }
120