2 * Copyright (c) 2014 The FreeBSD Foundation
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
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.
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
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <opencrypto/gfmult.h>
36 #include <opencrypto/gmac.h>
39 AES_GMAC_Init(struct aes_gmac_ctx *agc)
42 bzero(agc, sizeof *agc);
46 AES_GMAC_Setkey(struct aes_gmac_ctx *agc, const uint8_t *key, uint16_t klen)
48 const uint8_t zeros[GMAC_BLOCK_LEN] = {};
50 uint8_t hbuf[GMAC_BLOCK_LEN];
52 agc->rounds = rijndaelKeySetupEnc(agc->keysched, key, klen * 8);
54 rijndaelEncrypt(agc->keysched, agc->rounds, zeros, hbuf);
57 gf128_genmultable4(h, &agc->ghashtbl);
59 explicit_bzero(&h, sizeof h);
60 explicit_bzero(hbuf, sizeof hbuf);
64 AES_GMAC_Reinit(struct aes_gmac_ctx *agc, const uint8_t *iv, uint16_t ivlen)
67 KASSERT(ivlen <= sizeof agc->counter, ("passed ivlen too large!"));
68 bcopy(iv, agc->counter, ivlen);
72 AES_GMAC_Update(struct aes_gmac_ctx *agc, const uint8_t *data, uint16_t len)
75 uint8_t buf[GMAC_BLOCK_LEN] = {};
81 if (len >= 4*GMAC_BLOCK_LEN) {
83 v = gf128_mul4b(v, data, &agc->ghashtbl);
84 } else if (len >= GMAC_BLOCK_LEN) {
86 v = gf128_add(v, gf128_read(data));
87 v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
91 v = gf128_add(v, gf128_read(&buf[0]));
92 v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
93 explicit_bzero(buf, sizeof buf);
100 explicit_bzero(&v, sizeof v);
106 AES_GMAC_Final(uint8_t digest[GMAC_DIGEST_LEN], struct aes_gmac_ctx *agc)
108 uint8_t enccntr[GMAC_BLOCK_LEN];
111 /* XXX - zero additional bytes? */
112 agc->counter[GMAC_BLOCK_LEN - 1] = 1;
114 rijndaelEncrypt(agc->keysched, agc->rounds, agc->counter, enccntr);
115 a = gf128_add(agc->hash, gf128_read(enccntr));
116 gf128_write(a, digest);
118 explicit_bzero(enccntr, sizeof enccntr);