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(void *ctx)
41 struct aes_gmac_ctx *agc;
44 bzero(agc, sizeof *agc);
48 AES_GMAC_Setkey(void *ctx, const uint8_t *key, u_int klen)
50 struct aes_gmac_ctx *agc;
51 const uint8_t zeros[GMAC_BLOCK_LEN] = {};
53 uint8_t hbuf[GMAC_BLOCK_LEN];
56 agc->rounds = rijndaelKeySetupEnc(agc->keysched, key, klen * 8);
58 rijndaelEncrypt(agc->keysched, agc->rounds, zeros, hbuf);
61 gf128_genmultable4(h, &agc->ghashtbl);
63 explicit_bzero(&h, sizeof h);
64 explicit_bzero(hbuf, sizeof hbuf);
68 AES_GMAC_Reinit(void *ctx, const uint8_t *iv, u_int ivlen)
70 struct aes_gmac_ctx *agc;
73 KASSERT(ivlen <= sizeof agc->counter, ("passed ivlen too large!"));
74 bcopy(iv, agc->counter, ivlen);
78 AES_GMAC_Update(void *ctx, const void *vdata, u_int len)
80 struct aes_gmac_ctx *agc;
83 uint8_t buf[GMAC_BLOCK_LEN] = {};
91 if (len >= 4*GMAC_BLOCK_LEN) {
93 v = gf128_mul4b(v, data, &agc->ghashtbl);
94 } else if (len >= GMAC_BLOCK_LEN) {
96 v = gf128_add(v, gf128_read(data));
97 v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
101 v = gf128_add(v, gf128_read(&buf[0]));
102 v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
103 explicit_bzero(buf, sizeof buf);
110 explicit_bzero(&v, sizeof v);
116 AES_GMAC_Final(uint8_t *digest, void *ctx)
118 struct aes_gmac_ctx *agc;
119 uint8_t enccntr[GMAC_BLOCK_LEN];
122 /* XXX - zero additional bytes? */
124 agc->counter[GMAC_BLOCK_LEN - 1] = 1;
126 rijndaelEncrypt(agc->keysched, agc->rounds, agc->counter, enccntr);
127 a = gf128_add(agc->hash, gf128_read(enccntr));
128 gf128_write(a, digest);
130 explicit_bzero(enccntr, sizeof enccntr);