xref: /freebsd/sys/opencrypto/cbc_mac.c (revision edf8578117e8844e02c0121147f45e4609b30680)
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 #include <sys/types.h>
27 #include <sys/systm.h>
28 #include <sys/param.h>
29 #include <sys/endian.h>
30 #include <opencrypto/cbc_mac.h>
31 #include <opencrypto/xform_auth.h>
32 
33 /*
34  * Given two CCM_CBC_BLOCK_LEN blocks, xor
35  * them into dst, and then encrypt dst.
36  */
37 static void
38 xor_and_encrypt(struct aes_cbc_mac_ctx *ctx,
39 		const uint8_t *src, uint8_t *dst)
40 {
41 #define	NWORDS	(CCM_CBC_BLOCK_LEN / sizeof(uint64_t))
42 	uint64_t b1[NWORDS], b2[NWORDS], temp[NWORDS];
43 
44 	memcpy(b1, src, CCM_CBC_BLOCK_LEN);
45 	memcpy(b2, dst, CCM_CBC_BLOCK_LEN);
46 
47 	for (size_t count = 0; count < NWORDS; count++)
48 		temp[count] = b1[count] ^ b2[count];
49 	rijndaelEncrypt(ctx->keysched, ctx->rounds, (void *)temp, dst);
50 #undef NWORDS
51 }
52 
53 void
54 AES_CBC_MAC_Init(void *vctx)
55 {
56 	struct aes_cbc_mac_ctx *ctx;
57 
58 	ctx = vctx;
59 	bzero(ctx, sizeof(*ctx));
60 }
61 
62 void
63 AES_CBC_MAC_Setkey(void *vctx, const uint8_t *key, u_int klen)
64 {
65 	struct aes_cbc_mac_ctx *ctx;
66 
67 	ctx = vctx;
68 	ctx->rounds = rijndaelKeySetupEnc(ctx->keysched, key, klen * 8);
69 }
70 
71 /*
72  * This is called to set the nonce, aka IV.
73  *
74  * Note that the caller is responsible for constructing b0 as well
75  * as the length and padding around the AAD and passing that data
76  * to _Update.
77  */
78 void
79 AES_CBC_MAC_Reinit(void *vctx, const uint8_t *nonce, u_int nonceLen)
80 {
81 	struct aes_cbc_mac_ctx *ctx = vctx;
82 
83 	ctx->nonce = nonce;
84 	ctx->nonceLength = nonceLen;
85 
86 	ctx->blockIndex = 0;
87 
88 	/* XOR b0 with all 0's on first call to _Update. */
89 	memset(ctx->block, 0, CCM_CBC_BLOCK_LEN);
90 }
91 
92 int
93 AES_CBC_MAC_Update(void *vctx, const void *vdata, u_int length)
94 {
95 	struct aes_cbc_mac_ctx *ctx;
96 	const uint8_t *data;
97 	size_t copy_amt;
98 
99 	ctx = vctx;
100 	data = vdata;
101 
102 	/*
103 	 * _Update can be called with non-aligned update lengths.  Use
104 	 * the staging block when necessary.
105 	 */
106 	while (length != 0) {
107 		uint8_t *ptr;
108 
109 		/*
110 		 * If there is no partial block and the length is at
111 		 * least a full block, encrypt the full block without
112 		 * copying to the staging block.
113 		 */
114 		if (ctx->blockIndex == 0 && length >= CCM_CBC_BLOCK_LEN) {
115 			xor_and_encrypt(ctx, data, ctx->block);
116 			length -= CCM_CBC_BLOCK_LEN;
117 			data += CCM_CBC_BLOCK_LEN;
118 			continue;
119 		}
120 
121 		copy_amt = MIN(sizeof(ctx->staging_block) - ctx->blockIndex,
122 		    length);
123 		ptr = ctx->staging_block + ctx->blockIndex;
124 		bcopy(data, ptr, copy_amt);
125 		data += copy_amt;
126 		ctx->blockIndex += copy_amt;
127 		length -= copy_amt;
128 		if (ctx->blockIndex == sizeof(ctx->staging_block)) {
129 			/* We've got a full block */
130 			xor_and_encrypt(ctx, ctx->staging_block, ctx->block);
131 			ctx->blockIndex = 0;
132 		}
133 	}
134 	return (0);
135 }
136 
137 void
138 AES_CBC_MAC_Final(uint8_t *buf, void *vctx)
139 {
140 	struct aes_cbc_mac_ctx *ctx;
141 	uint8_t s0[CCM_CBC_BLOCK_LEN];
142 
143 	ctx = vctx;
144 
145 	/*
146 	 * We first need to check to see if we've got any data
147 	 * left over to encrypt.
148 	 */
149 	if (ctx->blockIndex != 0) {
150 		memset(ctx->staging_block + ctx->blockIndex, 0,
151 		    CCM_CBC_BLOCK_LEN - ctx->blockIndex);
152 		xor_and_encrypt(ctx, ctx->staging_block, ctx->block);
153 	}
154 	explicit_bzero(ctx->staging_block, sizeof(ctx->staging_block));
155 
156 	bzero(s0, sizeof(s0));
157 	s0[0] = (15 - ctx->nonceLength) - 1;
158 	bcopy(ctx->nonce, s0 + 1, ctx->nonceLength);
159 	rijndaelEncrypt(ctx->keysched, ctx->rounds, s0, s0);
160 	for (size_t indx = 0; indx < AES_CBC_MAC_HASH_LEN; indx++)
161 		buf[indx] = ctx->block[indx] ^ s0[indx];
162 	explicit_bzero(s0, sizeof(s0));
163 }
164