gmac.c revision 1.3 1 1.3 drochner /* $NetBSD: gmac.c,v 1.3 2011/06/09 14:47:42 drochner Exp $ */
2 1.1 drochner /* OpenBSD: gmac.c,v 1.3 2011/01/11 15:44:23 deraadt Exp */
3 1.1 drochner
4 1.1 drochner /*
5 1.1 drochner * Copyright (c) 2010 Mike Belopuhov <mike (at) vantronix.net>
6 1.1 drochner *
7 1.1 drochner * Permission to use, copy, modify, and distribute this software for any
8 1.1 drochner * purpose with or without fee is hereby granted, provided that the above
9 1.1 drochner * copyright notice and this permission notice appear in all copies.
10 1.1 drochner *
11 1.1 drochner * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 drochner * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 drochner * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 drochner * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 drochner * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 drochner * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 drochner * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 drochner */
19 1.1 drochner
20 1.1 drochner /*
21 1.1 drochner * This code implements the Message Authentication part of the
22 1.1 drochner * Galois/Counter Mode (as being described in the RFC 4543) using
23 1.1 drochner * the AES cipher. FIPS SP 800-38D describes the algorithm details.
24 1.1 drochner */
25 1.1 drochner
26 1.1 drochner #include <sys/param.h>
27 1.1 drochner #include <sys/systm.h>
28 1.1 drochner
29 1.1 drochner #include <crypto/rijndael/rijndael.h>
30 1.1 drochner #include <opencrypto/gmac.h>
31 1.1 drochner
32 1.3 drochner void ghash_gfmul(const GMAC_INT *, const GMAC_INT *, GMAC_INT *);
33 1.1 drochner void ghash_update(GHASH_CTX *, const uint8_t *, size_t);
34 1.1 drochner
35 1.1 drochner /* Computes a block multiplication in the GF(2^128) */
36 1.1 drochner void
37 1.3 drochner ghash_gfmul(const GMAC_INT *X, const GMAC_INT *Y, GMAC_INT *product)
38 1.1 drochner {
39 1.3 drochner GMAC_INT v[GMAC_BLOCK_LEN/GMAC_INTLEN];
40 1.1 drochner uint32_t mul;
41 1.1 drochner int i;
42 1.1 drochner
43 1.2 drochner memcpy(v, Y, GMAC_BLOCK_LEN);
44 1.2 drochner memset(product, 0, GMAC_BLOCK_LEN);
45 1.1 drochner
46 1.1 drochner for (i = 0; i < GMAC_BLOCK_LEN * 8; i++) {
47 1.1 drochner /* update Z */
48 1.3 drochner #if GMAC_INTLEN == 8
49 1.3 drochner if (X[i >> 6] & (1ULL << (~i & 63))) {
50 1.3 drochner product[0] ^= v[0];
51 1.3 drochner product[1] ^= v[1];
52 1.3 drochner } /* else: we preserve old values */
53 1.3 drochner #else
54 1.2 drochner if (X[i >> 5] & (1 << (~i & 31))) {
55 1.2 drochner product[0] ^= v[0];
56 1.2 drochner product[1] ^= v[1];
57 1.2 drochner product[2] ^= v[2];
58 1.2 drochner product[3] ^= v[3];
59 1.1 drochner } /* else: we preserve old values */
60 1.3 drochner #endif
61 1.1 drochner /* update V */
62 1.3 drochner #if GMAC_INTLEN == 8
63 1.3 drochner mul = v[1] & 1;
64 1.3 drochner v[1] = (v[0] << 63) | (v[1] >> 1);
65 1.3 drochner v[0] = (v[0] >> 1) ^ (0xe100000000000000ULL * mul);
66 1.3 drochner #else
67 1.1 drochner mul = v[3] & 1;
68 1.1 drochner v[3] = (v[2] << 31) | (v[3] >> 1);
69 1.1 drochner v[2] = (v[1] << 31) | (v[2] >> 1);
70 1.1 drochner v[1] = (v[0] << 31) | (v[1] >> 1);
71 1.1 drochner v[0] = (v[0] >> 1) ^ (0xe1000000 * mul);
72 1.3 drochner #endif
73 1.1 drochner }
74 1.1 drochner }
75 1.1 drochner
76 1.1 drochner void
77 1.1 drochner ghash_update(GHASH_CTX *ctx, const uint8_t *X, size_t len)
78 1.1 drochner {
79 1.3 drochner GMAC_INT x;
80 1.3 drochner GMAC_INT *s = ctx->S;
81 1.3 drochner GMAC_INT *y = ctx->Z;
82 1.3 drochner int i, j, k;
83 1.1 drochner
84 1.1 drochner for (i = 0; i < len / GMAC_BLOCK_LEN; i++) {
85 1.3 drochner for (j = 0; j < GMAC_BLOCK_LEN/GMAC_INTLEN; j++) {
86 1.3 drochner x = 0;
87 1.3 drochner for (k = 0; k < GMAC_INTLEN; k++) {
88 1.3 drochner x <<= 8;
89 1.3 drochner x |= X[k];
90 1.3 drochner }
91 1.2 drochner s[j] = y[j] ^ x;
92 1.3 drochner X += GMAC_INTLEN;
93 1.2 drochner }
94 1.1 drochner
95 1.2 drochner ghash_gfmul(ctx->H, ctx->S, ctx->S);
96 1.1 drochner
97 1.1 drochner y = s;
98 1.1 drochner }
99 1.1 drochner
100 1.1 drochner memcpy(ctx->Z, ctx->S, GMAC_BLOCK_LEN);
101 1.1 drochner }
102 1.1 drochner
103 1.1 drochner #define AESCTR_NONCESIZE 4
104 1.1 drochner
105 1.1 drochner void
106 1.1 drochner AES_GMAC_Init(AES_GMAC_CTX *ctx)
107 1.1 drochner {
108 1.1 drochner
109 1.1 drochner memset(ctx, 0, sizeof(AES_GMAC_CTX));
110 1.1 drochner }
111 1.1 drochner
112 1.1 drochner void
113 1.1 drochner AES_GMAC_Setkey(AES_GMAC_CTX *ctx, const uint8_t *key, uint16_t klen)
114 1.1 drochner {
115 1.2 drochner int i;
116 1.2 drochner
117 1.1 drochner ctx->rounds = rijndaelKeySetupEnc(ctx->K, (const u_char *)key,
118 1.1 drochner (klen - AESCTR_NONCESIZE) * 8);
119 1.1 drochner /* copy out salt to the counter block */
120 1.1 drochner memcpy(ctx->J, key + klen - AESCTR_NONCESIZE, AESCTR_NONCESIZE);
121 1.1 drochner /* prepare a hash subkey */
122 1.1 drochner rijndaelEncrypt(ctx->K, ctx->rounds, (void *)ctx->ghash.H,
123 1.1 drochner (void *)ctx->ghash.H);
124 1.3 drochner #if GMAC_INTLEN == 8
125 1.3 drochner for (i = 0; i < 2; i++)
126 1.3 drochner ctx->ghash.H[i] = be64toh(ctx->ghash.H[i]);
127 1.3 drochner #else
128 1.2 drochner for (i = 0; i < 4; i++)
129 1.2 drochner ctx->ghash.H[i] = be32toh(ctx->ghash.H[i]);
130 1.3 drochner #endif
131 1.1 drochner }
132 1.1 drochner
133 1.1 drochner void
134 1.1 drochner AES_GMAC_Reinit(AES_GMAC_CTX *ctx, const uint8_t *iv, uint16_t ivlen)
135 1.1 drochner {
136 1.1 drochner /* copy out IV to the counter block */
137 1.1 drochner memcpy(ctx->J + AESCTR_NONCESIZE, iv, ivlen);
138 1.1 drochner }
139 1.1 drochner
140 1.1 drochner int
141 1.1 drochner AES_GMAC_Update(AES_GMAC_CTX *ctx, const uint8_t *data, uint16_t len)
142 1.1 drochner {
143 1.2 drochner uint8_t blk[16] = { 0 };
144 1.1 drochner int plen;
145 1.1 drochner
146 1.1 drochner if (len > 0) {
147 1.1 drochner plen = len % GMAC_BLOCK_LEN;
148 1.1 drochner if (len >= GMAC_BLOCK_LEN)
149 1.2 drochner ghash_update(&ctx->ghash, data, len - plen);
150 1.1 drochner if (plen) {
151 1.1 drochner memcpy(blk, data + (len - plen), plen);
152 1.2 drochner ghash_update(&ctx->ghash, blk, GMAC_BLOCK_LEN);
153 1.1 drochner }
154 1.1 drochner }
155 1.1 drochner return (0);
156 1.1 drochner }
157 1.1 drochner
158 1.1 drochner void
159 1.1 drochner AES_GMAC_Final(uint8_t digest[GMAC_DIGEST_LEN], AES_GMAC_CTX *ctx)
160 1.1 drochner {
161 1.2 drochner uint8_t keystream[GMAC_BLOCK_LEN], *k, *d;
162 1.1 drochner int i;
163 1.1 drochner
164 1.1 drochner /* do one round of GCTR */
165 1.1 drochner ctx->J[GMAC_BLOCK_LEN - 1] = 1;
166 1.1 drochner rijndaelEncrypt(ctx->K, ctx->rounds, ctx->J, keystream);
167 1.2 drochner k = keystream;
168 1.2 drochner d = digest;
169 1.3 drochner #if GMAC_INTLEN == 8
170 1.3 drochner for (i = 0; i < GMAC_DIGEST_LEN/8; i++) {
171 1.3 drochner d[0] = (uint8_t)(ctx->ghash.S[i] >> 56) ^ k[0];
172 1.3 drochner d[1] = (uint8_t)(ctx->ghash.S[i] >> 48) ^ k[1];
173 1.3 drochner d[2] = (uint8_t)(ctx->ghash.S[i] >> 40) ^ k[2];
174 1.3 drochner d[3] = (uint8_t)(ctx->ghash.S[i] >> 32) ^ k[3];
175 1.3 drochner d[4] = (uint8_t)(ctx->ghash.S[i] >> 24) ^ k[4];
176 1.3 drochner d[5] = (uint8_t)(ctx->ghash.S[i] >> 16) ^ k[5];
177 1.3 drochner d[6] = (uint8_t)(ctx->ghash.S[i] >> 8) ^ k[6];
178 1.3 drochner d[7] = (uint8_t)ctx->ghash.S[i] ^ k[7];
179 1.3 drochner d += 8;
180 1.3 drochner k += 8;
181 1.3 drochner }
182 1.3 drochner #else
183 1.2 drochner for (i = 0; i < GMAC_DIGEST_LEN/4; i++) {
184 1.2 drochner d[0] = (uint8_t)(ctx->ghash.S[i] >> 24) ^ k[0];
185 1.2 drochner d[1] = (uint8_t)(ctx->ghash.S[i] >> 16) ^ k[1];
186 1.2 drochner d[2] = (uint8_t)(ctx->ghash.S[i] >> 8) ^ k[2];
187 1.2 drochner d[3] = (uint8_t)ctx->ghash.S[i] ^ k[3];
188 1.2 drochner d += 4;
189 1.2 drochner k += 4;
190 1.2 drochner }
191 1.3 drochner #endif
192 1.1 drochner memset(keystream, 0, sizeof(keystream));
193 1.1 drochner }
194