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
30 #include <sys/types.h>
31 #include <sys/systm.h>
32 #include <opencrypto/gfmult.h>
33 #include <opencrypto/gmac.h>
36 AES_GMAC_Init(void *ctx)
38 struct aes_gmac_ctx *agc;
41 bzero(agc, sizeof *agc);
45 AES_GMAC_Setkey(void *ctx, const uint8_t *key, u_int klen)
47 struct aes_gmac_ctx *agc;
48 const uint8_t zeros[GMAC_BLOCK_LEN] = {};
50 uint8_t hbuf[GMAC_BLOCK_LEN];
53 agc->rounds = rijndaelKeySetupEnc(agc->keysched, key, klen * 8);
55 rijndaelEncrypt(agc->keysched, agc->rounds, zeros, hbuf);
58 gf128_genmultable4(h, &agc->ghashtbl);
60 explicit_bzero(&h, sizeof h);
61 explicit_bzero(hbuf, sizeof hbuf);
65 AES_GMAC_Reinit(void *ctx, const uint8_t *iv, u_int ivlen)
67 struct aes_gmac_ctx *agc;
70 KASSERT(ivlen <= sizeof agc->counter, ("passed ivlen too large!"));
71 memset(agc->counter, 0, sizeof(agc->counter));
72 bcopy(iv, agc->counter, ivlen);
73 agc->counter[GMAC_BLOCK_LEN - 1] = 1;
75 memset(&agc->hash, 0, sizeof(agc->hash));
79 AES_GMAC_Update(void *ctx, const void *vdata, u_int len)
81 struct aes_gmac_ctx *agc;
84 uint8_t buf[GMAC_BLOCK_LEN] = {};
92 if (len >= 4*GMAC_BLOCK_LEN) {
94 v = gf128_mul4b(v, data, &agc->ghashtbl);
95 } else if (len >= GMAC_BLOCK_LEN) {
97 v = gf128_add(v, gf128_read(data));
98 v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
102 v = gf128_add(v, gf128_read(&buf[0]));
103 v = gf128_mul(v, &agc->ghashtbl.tbls[0]);
104 explicit_bzero(buf, sizeof buf);
111 explicit_bzero(&v, sizeof v);
117 AES_GMAC_Final(uint8_t *digest, void *ctx)
119 struct aes_gmac_ctx *agc;
120 uint8_t enccntr[GMAC_BLOCK_LEN];
125 rijndaelEncrypt(agc->keysched, agc->rounds, agc->counter, enccntr);
126 a = gf128_add(agc->hash, gf128_read(enccntr));
127 gf128_write(a, digest);
129 explicit_bzero(enccntr, sizeof enccntr);