2 * Copyright (c) 2014 The FreeBSD Foundation
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
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.
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
32 #include <sys/types.h>
33 #include <sys/systm.h>
34 #include <opencrypto/gfmult.h>
35 #include <opencrypto/gmac.h>
38 AES_GMAC_Init(void *ctx)
40 struct aes_gmac_ctx *agc;
43 bzero(agc, sizeof *agc);
47 AES_GMAC_Setkey(void *ctx, const uint8_t *key, u_int klen)
49 struct aes_gmac_ctx *agc;
50 const uint8_t zeros[GMAC_BLOCK_LEN] = {};
52 uint8_t hbuf[GMAC_BLOCK_LEN];
55 agc->rounds = rijndaelKeySetupEnc(agc->keysched, key, klen * 8);
57 rijndaelEncrypt(agc->keysched, agc->rounds, zeros, hbuf);
60 gf128_genmultable4(h, &agc->ghashtbl);
62 explicit_bzero(&h, sizeof h);
63 explicit_bzero(hbuf, sizeof hbuf);
67 AES_GMAC_Reinit(void *ctx, const uint8_t *iv, u_int ivlen)
69 struct aes_gmac_ctx *agc;
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;
77 memset(&agc->hash, 0, sizeof(agc->hash));
81 AES_GMAC_Update(void *ctx, const void *vdata, u_int len)
83 struct aes_gmac_ctx *agc;
86 uint8_t buf[GMAC_BLOCK_LEN] = {};
94 if (len >= 4*GMAC_BLOCK_LEN) {
96 v = gf128_mul4b(v, data, &agc->ghashtbl);
97 } else if (len >= GMAC_BLOCK_LEN) {
99 v = gf128_add(v, gf128_read(data));
100 v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
104 v = gf128_add(v, gf128_read(&buf[0]));
105 v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
106 explicit_bzero(buf, sizeof buf);
113 explicit_bzero(&v, sizeof v);
119 AES_GMAC_Final(uint8_t *digest, void *ctx)
121 struct aes_gmac_ctx *agc;
122 uint8_t enccntr[GMAC_BLOCK_LEN];
127 rijndaelEncrypt(agc->keysched, agc->rounds, agc->counter, enccntr);
128 a = gf128_add(agc->hash, gf128_read(enccntr));
129 gf128_write(a, digest);
131 explicit_bzero(enccntr, sizeof enccntr);