-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathgmac.c
204 lines (162 loc) · 5.34 KB
/
gmac.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/****************************************************************************
* crypto/gmac.c
*
* SPDX-License-Identifier: ISC
* SPDX-FileCopyrightText: Copyright (c) 2010 Mike Belopuhov
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
****************************************************************************/
/****************************************************************************
*
* This code implements the Message Authentication part of the
* Galois/Counter Mode (as being described in the RFC 4543) using
* the AES cipher. FIPS SP 800-38D describes the algorithm details.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <endian.h>
#include <strings.h>
#include <sys/param.h>
#include <crypto/aes.h>
#include <crypto/gmac.h>
/****************************************************************************
* Public Functions
****************************************************************************/
void ghash_gfmul(FAR uint32_t *, FAR uint32_t *, FAR uint32_t *);
void ghash_update_mi(FAR GHASH_CTX *, FAR uint8_t *, size_t);
/* Allow overriding with optimized MD function */
CODE void (*ghash_update)(FAR GHASH_CTX *,
FAR uint8_t *,
size_t) = ghash_update_mi;
/* Computes a block multiplication in the GF(2^128) */
void ghash_gfmul(FAR uint32_t *X, FAR uint32_t *Y, FAR uint32_t *product)
{
uint32_t v[4];
uint32_t z[4] =
{
0, 0, 0, 0
};
FAR uint8_t *x = (FAR uint8_t *)X;
uint32_t mask;
int i;
v[0] = betoh32(Y[0]);
v[1] = betoh32(Y[1]);
v[2] = betoh32(Y[2]);
v[3] = betoh32(Y[3]);
for (i = 0; i < GMAC_BLOCK_LEN * 8; i++)
{
/* update Z */
mask = !!(x[i >> 3] & (1 << (~i & 7)));
mask = ~(mask - 1);
z[0] ^= v[0] & mask;
z[1] ^= v[1] & mask;
z[2] ^= v[2] & mask;
z[3] ^= v[3] & mask;
/* update V */
mask = ~((v[3] & 1) - 1);
v[3] = (v[2] << 31) | (v[3] >> 1);
v[2] = (v[1] << 31) | (v[2] >> 1);
v[1] = (v[0] << 31) | (v[1] >> 1);
v[0] = (v[0] >> 1) ^ (0xe1000000 & mask);
}
product[0] = htobe32(z[0]);
product[1] = htobe32(z[1]);
product[2] = htobe32(z[2]);
product[3] = htobe32(z[3]);
}
void ghash_update_mi(FAR GHASH_CTX *ctx, FAR uint8_t *X, size_t len)
{
FAR uint32_t *x = (FAR uint32_t *)X;
FAR uint32_t *s = (FAR uint32_t *)ctx->S;
FAR uint32_t *y = (FAR uint32_t *)ctx->Z;
int i;
for (i = 0; i < len / GMAC_BLOCK_LEN; i++)
{
s[0] = y[0] ^ x[0];
s[1] = y[1] ^ x[1];
s[2] = y[2] ^ x[2];
s[3] = y[3] ^ x[3];
ghash_gfmul((FAR uint32_t *)ctx->S, (FAR uint32_t *)ctx->H,
(FAR uint32_t *)ctx->S);
y = s;
x += 4;
}
bcopy(ctx->S, ctx->Z, GMAC_BLOCK_LEN);
}
#define AESCTR_NONCESIZE 4
void aes_gmac_init(FAR void *xctx)
{
FAR AES_GMAC_CTX *ctx = xctx;
bzero(ctx->ghash.H, GMAC_BLOCK_LEN);
bzero(ctx->ghash.S, GMAC_BLOCK_LEN);
bzero(ctx->ghash.Z, GMAC_BLOCK_LEN);
bzero(ctx->J, GMAC_BLOCK_LEN);
}
void aes_gmac_setkey(FAR void *xctx, FAR const uint8_t *key, uint16_t klen)
{
FAR AES_GMAC_CTX *ctx = xctx;
aes_setkey(&ctx->K, key, klen - AESCTR_NONCESIZE);
/* copy out salt to the counter block */
bcopy(key + klen - AESCTR_NONCESIZE, ctx->J, AESCTR_NONCESIZE);
/* prepare a hash subkey */
aes_encrypt(&ctx->K, ctx->ghash.H, ctx->ghash.H);
}
void aes_gmac_reinit(FAR void *xctx, FAR const uint8_t *iv, uint16_t ivlen)
{
FAR AES_GMAC_CTX *ctx = xctx;
/* copy out IV to the counter block */
bcopy(iv, ctx->J + AESCTR_NONCESIZE, ivlen);
}
int aes_gmac_update(FAR void *xctx, FAR const uint8_t *data, size_t len)
{
FAR AES_GMAC_CTX *ctx = xctx;
uint32_t blk[4] =
{
0, 0, 0, 0
};
int plen;
if (len > 0)
{
plen = len % GMAC_BLOCK_LEN;
if (len >= GMAC_BLOCK_LEN)
{
(*ghash_update)(&ctx->ghash, (FAR uint8_t *)data,
len - plen);
}
if (plen)
{
memcpy((FAR uint8_t *)blk, (FAR uint8_t *)data + (len - plen),
plen);
(*ghash_update)(&ctx->ghash, (FAR uint8_t *)blk,
GMAC_BLOCK_LEN);
}
}
return (0);
}
void aes_gmac_final(FAR uint8_t *digest, FAR void *xctx)
{
FAR AES_GMAC_CTX *ctx = xctx;
uint8_t keystream[GMAC_BLOCK_LEN];
int i;
/* do one round of GCTR */
ctx->J[GMAC_BLOCK_LEN - 1] = 1;
aes_encrypt(&ctx->K, ctx->J, keystream);
for (i = 0; i < GMAC_DIGEST_LEN; i++)
{
digest[i] = ctx->ghash.S[i] ^ keystream[i];
}
explicit_bzero(keystream, sizeof(keystream));
}