xref: /freebsd/sys/opencrypto/cbc_mac.c (revision 535af610a4fdace6d50960c0ad9be0597eea7a1b)
1 /*
2  * Copyright (c) 2018-2019 iXsystems Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
27 
28 #include <sys/types.h>
29 #include <sys/systm.h>
30 #include <sys/param.h>
31 #include <sys/endian.h>
32 #include <opencrypto/cbc_mac.h>
33 #include <opencrypto/xform_auth.h>
34 
35 /*
36  * Given two CCM_CBC_BLOCK_LEN blocks, xor
37  * them into dst, and then encrypt dst.
38  */
39 static void
40 xor_and_encrypt(struct aes_cbc_mac_ctx *ctx,
41 		const uint8_t *src, uint8_t *dst)
42 {
43 #define	NWORDS	(CCM_CBC_BLOCK_LEN / sizeof(uint64_t))
44 	uint64_t b1[NWORDS], b2[NWORDS], temp[NWORDS];
45 
46 	memcpy(b1, src, CCM_CBC_BLOCK_LEN);
47 	memcpy(b2, dst, CCM_CBC_BLOCK_LEN);
48 
49 	for (size_t count = 0; count < NWORDS; count++)
50 		temp[count] = b1[count] ^ b2[count];
51 	rijndaelEncrypt(ctx->keysched, ctx->rounds, (void *)temp, dst);
52 #undef NWORDS
53 }
54 
55 void
56 AES_CBC_MAC_Init(void *vctx)
57 {
58 	struct aes_cbc_mac_ctx *ctx;
59 
60 	ctx = vctx;
61 	bzero(ctx, sizeof(*ctx));
62 }
63 
64 void
65 AES_CBC_MAC_Setkey(void *vctx, const uint8_t *key, u_int klen)
66 {
67 	struct aes_cbc_mac_ctx *ctx;
68 
69 	ctx = vctx;
70 	ctx->rounds = rijndaelKeySetupEnc(ctx->keysched, key, klen * 8);
71 }
72 
73 /*
74  * This is called to set the nonce, aka IV.
75  *
76  * Note that the caller is responsible for constructing b0 as well
77  * as the length and padding around the AAD and passing that data
78  * to _Update.
79  */
80 void
81 AES_CBC_MAC_Reinit(void *vctx, const uint8_t *nonce, u_int nonceLen)
82 {
83 	struct aes_cbc_mac_ctx *ctx = vctx;
84 
85 	ctx->nonce = nonce;
86 	ctx->nonceLength = nonceLen;
87 
88 	ctx->blockIndex = 0;
89 
90 	/* XOR b0 with all 0's on first call to _Update. */
91 	memset(ctx->block, 0, CCM_CBC_BLOCK_LEN);
92 }
93 
94 int
95 AES_CBC_MAC_Update(void *vctx, const void *vdata, u_int length)
96 {
97 	struct aes_cbc_mac_ctx *ctx;
98 	const uint8_t *data;
99 	size_t copy_amt;
100 
101 	ctx = vctx;
102 	data = vdata;
103 
104 	/*
105 	 * _Update can be called with non-aligned update lengths.  Use
106 	 * the staging block when necessary.
107 	 */
108 	while (length != 0) {
109 		uint8_t *ptr;
110 
111 		/*
112 		 * If there is no partial block and the length is at
113 		 * least a full block, encrypt the full block without
114 		 * copying to the staging block.
115 		 */
116 		if (ctx->blockIndex == 0 && length >= CCM_CBC_BLOCK_LEN) {
117 			xor_and_encrypt(ctx, data, ctx->block);
118 			length -= CCM_CBC_BLOCK_LEN;
119 			data += CCM_CBC_BLOCK_LEN;
120 			continue;
121 		}
122 
123 		copy_amt = MIN(sizeof(ctx->staging_block) - ctx->blockIndex,
124 		    length);
125 		ptr = ctx->staging_block + ctx->blockIndex;
126 		bcopy(data, ptr, copy_amt);
127 		data += copy_amt;
128 		ctx->blockIndex += copy_amt;
129 		length -= copy_amt;
130 		if (ctx->blockIndex == sizeof(ctx->staging_block)) {
131 			/* We've got a full block */
132 			xor_and_encrypt(ctx, ctx->staging_block, ctx->block);
133 			ctx->blockIndex = 0;
134 		}
135 	}
136 	return (0);
137 }
138 
139 void
140 AES_CBC_MAC_Final(uint8_t *buf, void *vctx)
141 {
142 	struct aes_cbc_mac_ctx *ctx;
143 	uint8_t s0[CCM_CBC_BLOCK_LEN];
144 
145 	ctx = vctx;
146 
147 	/*
148 	 * We first need to check to see if we've got any data
149 	 * left over to encrypt.
150 	 */
151 	if (ctx->blockIndex != 0) {
152 		memset(ctx->staging_block + ctx->blockIndex, 0,
153 		    CCM_CBC_BLOCK_LEN - ctx->blockIndex);
154 		xor_and_encrypt(ctx, ctx->staging_block, ctx->block);
155 	}
156 	explicit_bzero(ctx->staging_block, sizeof(ctx->staging_block));
157 
158 	bzero(s0, sizeof(s0));
159 	s0[0] = (15 - ctx->nonceLength) - 1;
160 	bcopy(ctx->nonce, s0 + 1, ctx->nonceLength);
161 	rijndaelEncrypt(ctx->keysched, ctx->rounds, s0, s0);
162 	for (size_t indx = 0; indx < AES_CBC_MAC_HASH_LEN; indx++)
163 		buf[indx] = ctx->block[indx] ^ s0[indx];
164 	explicit_bzero(s0, sizeof(s0));
165 }
166