summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/opencrypto/cbc_mac.c
blob: 1bcf356a22812a7e7c99a451ed49a5e48e4859cf (plain) (blame)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
#include <machine/rtems-bsd-kernel-space.h>

/*
 * Copyright (c) 2018-2019 iXsystems Inc.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/types.h>
#include <sys/systm.h>
#include <sys/param.h>
#include <sys/endian.h>
#include <opencrypto/cbc_mac.h>
#include <opencrypto/xform_auth.h>

/*
 * Given two CCM_CBC_BLOCK_LEN blocks, xor
 * them into dst, and then encrypt dst.
 */
static void
xor_and_encrypt(struct aes_cbc_mac_ctx *ctx,
		const uint8_t *src, uint8_t *dst)
{
	const uint64_t *b1;
	uint64_t *b2;
	uint64_t temp_block[CCM_CBC_BLOCK_LEN/sizeof(uint64_t)];

	b1 = (const uint64_t*)src;
	b2 = (uint64_t*)dst;

	for (size_t count = 0;
	     count < CCM_CBC_BLOCK_LEN/sizeof(uint64_t);
	     count++) {
		temp_block[count] = b1[count] ^ b2[count];
	}
	rijndaelEncrypt(ctx->keysched, ctx->rounds, (void*)temp_block, dst);
}

void
AES_CBC_MAC_Init(struct aes_cbc_mac_ctx *ctx)
{
	bzero(ctx, sizeof(*ctx));
}

void
AES_CBC_MAC_Setkey(struct aes_cbc_mac_ctx *ctx, const uint8_t *key, uint16_t klen)
{
	ctx->rounds = rijndaelKeySetupEnc(ctx->keysched, key, klen * 8);
}

/*
 * This is called to set the nonce, aka IV.
 * Before this call, the authDataLength and cryptDataLength fields
 * MUST have been set.  Sadly, there's no way to return an error.
 *
 * The CBC-MAC algorithm requires that the first block contain the
 * nonce, as well as information about the sizes and lengths involved.
 */
void
AES_CBC_MAC_Reinit(struct aes_cbc_mac_ctx *ctx, const uint8_t *nonce, uint16_t nonceLen)
{
	uint8_t b0[CCM_CBC_BLOCK_LEN];
	uint8_t *bp = b0, flags = 0;
	uint8_t L = 0;
	uint64_t dataLength = ctx->cryptDataLength;

	KASSERT(nonceLen >= 7 && nonceLen <= 13,
	    ("nonceLen must be between 7 and 13 bytes"));

	ctx->nonce = nonce;
	ctx->nonceLength = nonceLen;
	
	ctx->authDataCount = 0;
	ctx->blockIndex = 0;
	explicit_bzero(ctx->staging_block, sizeof(ctx->staging_block));
	
	/*
	 * Need to determine the L field value.  This is the number of
	 * bytes needed to specify the length of the message; the length
	 * is whatever is left in the 16 bytes after specifying flags and
	 * the nonce.
	 */
	L = 15 - nonceLen;
	
	flags = ((ctx->authDataLength > 0) << 6) +
	    (((AES_CBC_MAC_HASH_LEN - 2) / 2) << 3) +
	    L - 1;
	/*
	 * Now we need to set up the first block, which has flags, nonce,
	 * and the message length.
	 */
	b0[0] = flags;
	bcopy(nonce, b0 + 1, nonceLen);
	bp = b0 + 1 + nonceLen;

	/* Need to copy L' [aka L-1] bytes of cryptDataLength */
	for (uint8_t *dst = b0 + sizeof(b0) - 1; dst >= bp; dst--) {
		*dst = dataLength;
		dataLength >>= 8;
	}
	/* Now need to encrypt b0 */
	rijndaelEncrypt(ctx->keysched, ctx->rounds, b0, ctx->block);
	/* If there is auth data, we need to set up the staging block */
	if (ctx->authDataLength) {
		size_t addLength;
		if (ctx->authDataLength < ((1<<16) - (1<<8))) {
			uint16_t sizeVal = htobe16(ctx->authDataLength);
			bcopy(&sizeVal, ctx->staging_block, sizeof(sizeVal));
			addLength = sizeof(sizeVal);
		} else if (ctx->authDataLength < (1ULL<<32)) {
			uint32_t sizeVal = htobe32(ctx->authDataLength);
			ctx->staging_block[0] = 0xff;
			ctx->staging_block[1] = 0xfe;
			bcopy(&sizeVal, ctx->staging_block+2, sizeof(sizeVal));
			addLength = 2 + sizeof(sizeVal);
		} else {
			uint64_t sizeVal = htobe64(ctx->authDataLength);
			ctx->staging_block[0] = 0xff;
			ctx->staging_block[1] = 0xff;
			bcopy(&sizeVal, ctx->staging_block+2, sizeof(sizeVal));
			addLength = 2 + sizeof(sizeVal);
		}
		ctx->blockIndex = addLength;
		/*
		 * The length descriptor goes into the AAD buffer, so we
		 * need to account for it.
		 */
		ctx->authDataLength += addLength;
		ctx->authDataCount = addLength;
	}
}

int
AES_CBC_MAC_Update(struct aes_cbc_mac_ctx *ctx, const uint8_t *data,
    uint16_t length)
{
	size_t copy_amt;
	
	/*
	 * This will be called in one of two phases:
	 * (1)  Applying authentication data, or
	 * (2)  Applying the payload data.
	 *
	 * Because CBC-MAC puts the authentication data size before the
	 * data, subsequent calls won't be block-size-aligned.  Which
	 * complicates things a fair bit.
	 *
	 * The payload data doesn't have that problem.
	 */
				
	if (ctx->authDataCount < ctx->authDataLength) {
		/*
		 * We need to process data as authentication data.
		 * Since we may be out of sync, we may also need
		 * to pad out the staging block.
		 */
		const uint8_t *ptr = data;
		while (length > 0) {

			copy_amt = MIN(length,
			    sizeof(ctx->staging_block) - ctx->blockIndex);

			bcopy(ptr, ctx->staging_block + ctx->blockIndex,
			    copy_amt);
			ptr += copy_amt;
			length -= copy_amt;
			ctx->authDataCount += copy_amt;
			ctx->blockIndex += copy_amt;
			ctx->blockIndex %= sizeof(ctx->staging_block);

			if (ctx->blockIndex == 0 ||
			    ctx->authDataCount == ctx->authDataLength) {
				/*
				 * We're done with this block, so we
				 * xor staging_block with block, and then
				 * encrypt it.
				 */
				xor_and_encrypt(ctx, ctx->staging_block, ctx->block);
				bzero(ctx->staging_block, sizeof(ctx->staging_block));
				ctx->blockIndex = 0;
				if (ctx->authDataCount >= ctx->authDataLength)
					break;
			}
		}
		/*
		 * We'd like to be able to check length == 0 and return
		 * here, but the way OCF calls us, length is always
		 * blksize (16, in this case).  So we have to count on
		 * the fact that OCF calls us separately for the AAD and
		 * for the real data.
		 */
		return (0);
	}
	/*
	 * If we're here, then we're encoding payload data.
	 * This is marginally easier, except that _Update can
	 * be called with non-aligned update lengths. As a result,
	 * we still need to use the staging block.
	 */
	KASSERT((length + ctx->cryptDataCount) <= ctx->cryptDataLength,
	    ("More encryption data than allowed"));

	while (length) {
		uint8_t *ptr;
		
		copy_amt = MIN(sizeof(ctx->staging_block) - ctx->blockIndex,
		    length);
		ptr = ctx->staging_block + ctx->blockIndex;
		bcopy(data, ptr, copy_amt);
		data += copy_amt;
		ctx->blockIndex += copy_amt;
		ctx->cryptDataCount += copy_amt;
		length -= copy_amt;
		if (ctx->blockIndex == sizeof(ctx->staging_block)) {
			/* We've got a full block */
			xor_and_encrypt(ctx, ctx->staging_block, ctx->block);
			ctx->blockIndex = 0;
			bzero(ctx->staging_block, sizeof(ctx->staging_block));
		}
	}
	return (0);
}

void
AES_CBC_MAC_Final(uint8_t *buf, struct aes_cbc_mac_ctx *ctx)
{
	uint8_t s0[CCM_CBC_BLOCK_LEN];
	
	/*
	 * We first need to check to see if we've got any data
	 * left over to encrypt.
	 */
	if (ctx->blockIndex != 0) {
		xor_and_encrypt(ctx, ctx->staging_block, ctx->block);
		ctx->cryptDataCount += ctx->blockIndex;
		ctx->blockIndex = 0;
		explicit_bzero(ctx->staging_block, sizeof(ctx->staging_block));
	}
	bzero(s0, sizeof(s0));
	s0[0] = (15 - ctx->nonceLength) - 1;
	bcopy(ctx->nonce, s0 + 1, ctx->nonceLength);
	rijndaelEncrypt(ctx->keysched, ctx->rounds, s0, s0);
	for (size_t indx = 0; indx < AES_CBC_MAC_HASH_LEN; indx++)
		buf[indx] = ctx->block[indx] ^ s0[indx];
	explicit_bzero(s0, sizeof(s0));
}