aboutsummaryrefslogtreecommitdiff
path: root/sys/opencrypto/cbc_mac.c
blob: 9a030cd54173214e0495c71cb96f7c3fc037e502 (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
/*
 * 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(void *vctx)
{
	struct aes_cbc_mac_ctx *ctx;

	ctx = vctx;
	bzero(ctx, sizeof(*ctx));
}

void
AES_CBC_MAC_Setkey(void *vctx, const uint8_t *key, u_int klen)
{
	struct aes_cbc_mac_ctx *ctx;

	ctx = vctx;
	ctx->rounds = rijndaelKeySetupEnc(ctx->keysched, key, klen * 8);
}

/*
 * This is called to set the nonce, aka IV.
 *
 * Note that the caller is responsible for constructing b0 as well
 * as the length and padding around the AAD and passing that data
 * to _Update.
 */
void
AES_CBC_MAC_Reinit(void *vctx, const uint8_t *nonce, u_int nonceLen)
{
	struct aes_cbc_mac_ctx *ctx = vctx;

	ctx->nonce = nonce;
	ctx->nonceLength = nonceLen;

	ctx->blockIndex = 0;

	/* XOR b0 with all 0's on first call to _Update. */
	memset(ctx->block, 0, CCM_CBC_BLOCK_LEN);
}

int
AES_CBC_MAC_Update(void *vctx, const void *vdata, u_int length)
{
	struct aes_cbc_mac_ctx *ctx;
	const uint8_t *data;
	size_t copy_amt;
	
	ctx = vctx;
	data = vdata;

	/*
	 * _Update can be called with non-aligned update lengths.  Use
	 * the staging block when necessary.
	 */
	while (length != 0) {
		uint8_t *ptr;

		/*
		 * If there is no partial block and the length is at
		 * least a full block, encrypt the full block without
		 * copying to the staging block.
		 */
		if (ctx->blockIndex == 0 && length >= CCM_CBC_BLOCK_LEN) {
			xor_and_encrypt(ctx, data, ctx->block);
			length -= CCM_CBC_BLOCK_LEN;
			data += CCM_CBC_BLOCK_LEN;
			continue;
		}

		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;
		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;
		}
	}
	return (0);
}

void
AES_CBC_MAC_Final(uint8_t *buf, void *vctx)
{
	struct aes_cbc_mac_ctx *ctx;
	uint8_t s0[CCM_CBC_BLOCK_LEN];

	ctx = vctx;

	/*
	 * We first need to check to see if we've got any data
	 * left over to encrypt.
	 */
	if (ctx->blockIndex != 0) {
		memset(ctx->staging_block + ctx->blockIndex, 0,
		    CCM_CBC_BLOCK_LEN - ctx->blockIndex);
		xor_and_encrypt(ctx, ctx->staging_block, ctx->block);
	}
	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));
}