xref: /titanic_51/usr/src/common/crypto/modes/ccm.c (revision 4b56a00321e0ce508e55cc5e43e3ad7b00005a39)
123c57df7Smcpowers /*
223c57df7Smcpowers  * CDDL HEADER START
323c57df7Smcpowers  *
423c57df7Smcpowers  * The contents of this file are subject to the terms of the
523c57df7Smcpowers  * Common Development and Distribution License (the "License").
623c57df7Smcpowers  * You may not use this file except in compliance with the License.
723c57df7Smcpowers  *
823c57df7Smcpowers  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
923c57df7Smcpowers  * or http://www.opensolaris.org/os/licensing.
1023c57df7Smcpowers  * See the License for the specific language governing permissions
1123c57df7Smcpowers  * and limitations under the License.
1223c57df7Smcpowers  *
1323c57df7Smcpowers  * When distributing Covered Code, include this CDDL HEADER in each
1423c57df7Smcpowers  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1523c57df7Smcpowers  * If applicable, add the following below this CDDL HEADER, with the
1623c57df7Smcpowers  * fields enclosed by brackets "[]" replaced with your own identifying
1723c57df7Smcpowers  * information: Portions Copyright [yyyy] [name of copyright owner]
1823c57df7Smcpowers  *
1923c57df7Smcpowers  * CDDL HEADER END
2023c57df7Smcpowers  */
2123c57df7Smcpowers /*
2223c57df7Smcpowers  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
2323c57df7Smcpowers  * Use is subject to license terms.
2423c57df7Smcpowers  */
2523c57df7Smcpowers 
2623c57df7Smcpowers #ifndef _KERNEL
2723c57df7Smcpowers #include <strings.h>
2823c57df7Smcpowers #include <limits.h>
2923c57df7Smcpowers #include <assert.h>
3023c57df7Smcpowers #include <security/cryptoki.h>
3123c57df7Smcpowers #endif
3223c57df7Smcpowers 
3323c57df7Smcpowers #include <sys/types.h>
3423c57df7Smcpowers #include <sys/kmem.h>
3523c57df7Smcpowers #include <modes/modes.h>
3623c57df7Smcpowers #include <sys/crypto/common.h>
3723c57df7Smcpowers #include <sys/crypto/impl.h>
3823c57df7Smcpowers 
39*4b56a003SDaniel Anderson #if defined(__i386) || defined(__amd64)
40*4b56a003SDaniel Anderson #include <sys/byteorder.h>
41*4b56a003SDaniel Anderson #define	UNALIGNED_POINTERS_PERMITTED
42*4b56a003SDaniel Anderson #endif
43*4b56a003SDaniel Anderson 
4423c57df7Smcpowers /*
4523c57df7Smcpowers  * Encrypt multiple blocks of data in CCM mode.  Decrypt for CCM mode
4623c57df7Smcpowers  * is done in another function.
4723c57df7Smcpowers  */
4823c57df7Smcpowers int
4923c57df7Smcpowers ccm_mode_encrypt_contiguous_blocks(ccm_ctx_t *ctx, char *data, size_t length,
5023c57df7Smcpowers     crypto_data_t *out, size_t block_size,
5123c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
5223c57df7Smcpowers     void (*copy_block)(uint8_t *, uint8_t *),
5323c57df7Smcpowers     void (*xor_block)(uint8_t *, uint8_t *))
5423c57df7Smcpowers {
5523c57df7Smcpowers 	size_t remainder = length;
5623c57df7Smcpowers 	size_t need;
5723c57df7Smcpowers 	uint8_t *datap = (uint8_t *)data;
5823c57df7Smcpowers 	uint8_t *blockp;
5923c57df7Smcpowers 	uint8_t *lastp;
6023c57df7Smcpowers 	void *iov_or_mp;
6123c57df7Smcpowers 	offset_t offset;
6223c57df7Smcpowers 	uint8_t *out_data_1;
6323c57df7Smcpowers 	uint8_t *out_data_2;
6423c57df7Smcpowers 	size_t out_data_1_len;
6523c57df7Smcpowers 	uint64_t counter;
6623c57df7Smcpowers 	uint8_t *mac_buf;
6723c57df7Smcpowers 
6823c57df7Smcpowers 	if (length + ctx->ccm_remainder_len < block_size) {
6923c57df7Smcpowers 		/* accumulate bytes here and return */
7023c57df7Smcpowers 		bcopy(datap,
7123c57df7Smcpowers 		    (uint8_t *)ctx->ccm_remainder + ctx->ccm_remainder_len,
7223c57df7Smcpowers 		    length);
7323c57df7Smcpowers 		ctx->ccm_remainder_len += length;
7423c57df7Smcpowers 		ctx->ccm_copy_to = datap;
7523c57df7Smcpowers 		return (CRYPTO_SUCCESS);
7623c57df7Smcpowers 	}
7723c57df7Smcpowers 
7823c57df7Smcpowers 	lastp = (uint8_t *)ctx->ccm_cb;
7923c57df7Smcpowers 	if (out != NULL)
8023c57df7Smcpowers 		crypto_init_ptrs(out, &iov_or_mp, &offset);
8123c57df7Smcpowers 
8223c57df7Smcpowers 	mac_buf = (uint8_t *)ctx->ccm_mac_buf;
8323c57df7Smcpowers 
8423c57df7Smcpowers 	do {
8523c57df7Smcpowers 		/* Unprocessed data from last call. */
8623c57df7Smcpowers 		if (ctx->ccm_remainder_len > 0) {
8723c57df7Smcpowers 			need = block_size - ctx->ccm_remainder_len;
8823c57df7Smcpowers 
8923c57df7Smcpowers 			if (need > remainder)
9023c57df7Smcpowers 				return (CRYPTO_DATA_LEN_RANGE);
9123c57df7Smcpowers 
9223c57df7Smcpowers 			bcopy(datap, &((uint8_t *)ctx->ccm_remainder)
9323c57df7Smcpowers 			    [ctx->ccm_remainder_len], need);
9423c57df7Smcpowers 
9523c57df7Smcpowers 			blockp = (uint8_t *)ctx->ccm_remainder;
9623c57df7Smcpowers 		} else {
9723c57df7Smcpowers 			blockp = datap;
9823c57df7Smcpowers 		}
9923c57df7Smcpowers 
10023c57df7Smcpowers 		/*
10123c57df7Smcpowers 		 * do CBC MAC
10223c57df7Smcpowers 		 *
10323c57df7Smcpowers 		 * XOR the previous cipher block current clear block.
10423c57df7Smcpowers 		 * mac_buf always contain previous cipher block.
10523c57df7Smcpowers 		 */
10623c57df7Smcpowers 		xor_block(blockp, mac_buf);
10723c57df7Smcpowers 		encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
10823c57df7Smcpowers 
10923c57df7Smcpowers 		/* ccm_cb is the counter block */
11023c57df7Smcpowers 		encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb,
11123c57df7Smcpowers 		    (uint8_t *)ctx->ccm_tmp);
11223c57df7Smcpowers 
11323c57df7Smcpowers 		lastp = (uint8_t *)ctx->ccm_tmp;
11423c57df7Smcpowers 
11523c57df7Smcpowers 		/*
11623c57df7Smcpowers 		 * Increment counter. Counter bits are confined
11723c57df7Smcpowers 		 * to the bottom 64 bits of the counter block.
11823c57df7Smcpowers 		 */
119*4b56a003SDaniel Anderson #ifdef _LITTLE_ENDIAN
120*4b56a003SDaniel Anderson 		counter = ntohll(ctx->ccm_cb[1] & ctx->ccm_counter_mask);
121*4b56a003SDaniel Anderson 		counter = htonll(counter + 1);
122*4b56a003SDaniel Anderson #else
12323c57df7Smcpowers 		counter = ctx->ccm_cb[1] & ctx->ccm_counter_mask;
12423c57df7Smcpowers 		counter++;
125*4b56a003SDaniel Anderson #endif	/* _LITTLE_ENDIAN */
12623c57df7Smcpowers 		counter &= ctx->ccm_counter_mask;
12723c57df7Smcpowers 		ctx->ccm_cb[1] =
12823c57df7Smcpowers 		    (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter;
12923c57df7Smcpowers 
13023c57df7Smcpowers 		/*
131f02d2799SMark Powers 		 * XOR encrypted counter block with the current clear block.
13223c57df7Smcpowers 		 */
133f02d2799SMark Powers 		xor_block(blockp, lastp);
13423c57df7Smcpowers 
13523c57df7Smcpowers 		ctx->ccm_processed_data_len += block_size;
13623c57df7Smcpowers 
13723c57df7Smcpowers 		if (out == NULL) {
13823c57df7Smcpowers 			if (ctx->ccm_remainder_len > 0) {
13923c57df7Smcpowers 				bcopy(blockp, ctx->ccm_copy_to,
14023c57df7Smcpowers 				    ctx->ccm_remainder_len);
14123c57df7Smcpowers 				bcopy(blockp + ctx->ccm_remainder_len, datap,
14223c57df7Smcpowers 				    need);
14323c57df7Smcpowers 			}
14423c57df7Smcpowers 		} else {
14523c57df7Smcpowers 			crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1,
14623c57df7Smcpowers 			    &out_data_1_len, &out_data_2, block_size);
14723c57df7Smcpowers 
14823c57df7Smcpowers 			/* copy block to where it belongs */
14923c57df7Smcpowers 			if (out_data_1_len == block_size) {
15023c57df7Smcpowers 				copy_block(lastp, out_data_1);
15123c57df7Smcpowers 			} else {
15223c57df7Smcpowers 				bcopy(lastp, out_data_1, out_data_1_len);
15323c57df7Smcpowers 				if (out_data_2 != NULL) {
15423c57df7Smcpowers 					bcopy(lastp + out_data_1_len,
15523c57df7Smcpowers 					    out_data_2,
15623c57df7Smcpowers 					    block_size - out_data_1_len);
15723c57df7Smcpowers 				}
15823c57df7Smcpowers 			}
15923c57df7Smcpowers 			/* update offset */
16023c57df7Smcpowers 			out->cd_offset += block_size;
16123c57df7Smcpowers 		}
16223c57df7Smcpowers 
16323c57df7Smcpowers 		/* Update pointer to next block of data to be processed. */
16423c57df7Smcpowers 		if (ctx->ccm_remainder_len != 0) {
16523c57df7Smcpowers 			datap += need;
16623c57df7Smcpowers 			ctx->ccm_remainder_len = 0;
16723c57df7Smcpowers 		} else {
16823c57df7Smcpowers 			datap += block_size;
16923c57df7Smcpowers 		}
17023c57df7Smcpowers 
17123c57df7Smcpowers 		remainder = (size_t)&data[length] - (size_t)datap;
17223c57df7Smcpowers 
17323c57df7Smcpowers 		/* Incomplete last block. */
17423c57df7Smcpowers 		if (remainder > 0 && remainder < block_size) {
17523c57df7Smcpowers 			bcopy(datap, ctx->ccm_remainder, remainder);
17623c57df7Smcpowers 			ctx->ccm_remainder_len = remainder;
17723c57df7Smcpowers 			ctx->ccm_copy_to = datap;
17823c57df7Smcpowers 			goto out;
17923c57df7Smcpowers 		}
18023c57df7Smcpowers 		ctx->ccm_copy_to = NULL;
18123c57df7Smcpowers 
18223c57df7Smcpowers 	} while (remainder > 0);
18323c57df7Smcpowers 
18423c57df7Smcpowers out:
18523c57df7Smcpowers 	return (CRYPTO_SUCCESS);
18623c57df7Smcpowers }
18723c57df7Smcpowers 
18823c57df7Smcpowers void
18923c57df7Smcpowers calculate_ccm_mac(ccm_ctx_t *ctx, uint8_t *ccm_mac,
19023c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *))
19123c57df7Smcpowers {
19223c57df7Smcpowers 	uint64_t counter;
19323c57df7Smcpowers 	uint8_t *counterp, *mac_buf;
19423c57df7Smcpowers 	int i;
19523c57df7Smcpowers 
19623c57df7Smcpowers 	mac_buf = (uint8_t *)ctx->ccm_mac_buf;
19723c57df7Smcpowers 
19823c57df7Smcpowers 	/* first counter block start with index 0 */
19923c57df7Smcpowers 	counter = 0;
20023c57df7Smcpowers 	ctx->ccm_cb[1] = (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter;
20123c57df7Smcpowers 
20223c57df7Smcpowers 	counterp = (uint8_t *)ctx->ccm_tmp;
20323c57df7Smcpowers 	encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, counterp);
20423c57df7Smcpowers 
20523c57df7Smcpowers 	/* calculate XOR of MAC with first counter block */
20623c57df7Smcpowers 	for (i = 0; i < ctx->ccm_mac_len; i++) {
20723c57df7Smcpowers 		ccm_mac[i] = mac_buf[i] ^ counterp[i];
20823c57df7Smcpowers 	}
20923c57df7Smcpowers }
21023c57df7Smcpowers 
21123c57df7Smcpowers /* ARGSUSED */
21223c57df7Smcpowers int
21323c57df7Smcpowers ccm_encrypt_final(ccm_ctx_t *ctx, crypto_data_t *out, size_t block_size,
21423c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
21523c57df7Smcpowers     void (*xor_block)(uint8_t *, uint8_t *))
21623c57df7Smcpowers {
21723c57df7Smcpowers 	uint8_t *lastp, *mac_buf, *ccm_mac_p, *macp;
21823c57df7Smcpowers 	void *iov_or_mp;
21923c57df7Smcpowers 	offset_t offset;
22023c57df7Smcpowers 	uint8_t *out_data_1;
22123c57df7Smcpowers 	uint8_t *out_data_2;
22223c57df7Smcpowers 	size_t out_data_1_len;
22323c57df7Smcpowers 	int i;
22423c57df7Smcpowers 
22523c57df7Smcpowers 	if (out->cd_length < (ctx->ccm_remainder_len + ctx->ccm_mac_len)) {
22623c57df7Smcpowers 		return (CRYPTO_DATA_LEN_RANGE);
22723c57df7Smcpowers 	}
22823c57df7Smcpowers 
22923c57df7Smcpowers 	/*
23023c57df7Smcpowers 	 * When we get here, the number of bytes of payload processed
23123c57df7Smcpowers 	 * plus whatever data remains, if any,
23223c57df7Smcpowers 	 * should be the same as the number of bytes that's being
23323c57df7Smcpowers 	 * passed in the argument during init time.
23423c57df7Smcpowers 	 */
23523c57df7Smcpowers 	if ((ctx->ccm_processed_data_len + ctx->ccm_remainder_len)
23623c57df7Smcpowers 	    != (ctx->ccm_data_len)) {
23723c57df7Smcpowers 		return (CRYPTO_DATA_LEN_RANGE);
23823c57df7Smcpowers 	}
23923c57df7Smcpowers 
24023c57df7Smcpowers 	mac_buf = (uint8_t *)ctx->ccm_mac_buf;
24123c57df7Smcpowers 
24223c57df7Smcpowers 	if (ctx->ccm_remainder_len > 0) {
24323c57df7Smcpowers 
24423c57df7Smcpowers 		/* ccm_mac_input_buf is not used for encryption */
24523c57df7Smcpowers 		macp = (uint8_t *)ctx->ccm_mac_input_buf;
24623c57df7Smcpowers 		bzero(macp, block_size);
24723c57df7Smcpowers 
24823c57df7Smcpowers 		/* copy remainder to temporary buffer */
24923c57df7Smcpowers 		bcopy(ctx->ccm_remainder, macp, ctx->ccm_remainder_len);
25023c57df7Smcpowers 
25123c57df7Smcpowers 		/* calculate the CBC MAC */
25223c57df7Smcpowers 		xor_block(macp, mac_buf);
25323c57df7Smcpowers 		encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
25423c57df7Smcpowers 
25523c57df7Smcpowers 		/* calculate the counter mode */
25623c57df7Smcpowers 		lastp = (uint8_t *)ctx->ccm_tmp;
25723c57df7Smcpowers 		encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, lastp);
25823c57df7Smcpowers 
25923c57df7Smcpowers 		/* XOR with counter block */
26023c57df7Smcpowers 		for (i = 0; i < ctx->ccm_remainder_len; i++) {
26123c57df7Smcpowers 			macp[i] ^= lastp[i];
26223c57df7Smcpowers 		}
26323c57df7Smcpowers 		ctx->ccm_processed_data_len += ctx->ccm_remainder_len;
26423c57df7Smcpowers 	}
26523c57df7Smcpowers 
26623c57df7Smcpowers 	/* Calculate the CCM MAC */
26723c57df7Smcpowers 	ccm_mac_p = (uint8_t *)ctx->ccm_tmp;
26823c57df7Smcpowers 	calculate_ccm_mac(ctx, ccm_mac_p, encrypt_block);
26923c57df7Smcpowers 
27023c57df7Smcpowers 	crypto_init_ptrs(out, &iov_or_mp, &offset);
27123c57df7Smcpowers 	crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1,
27223c57df7Smcpowers 	    &out_data_1_len, &out_data_2,
27323c57df7Smcpowers 	    ctx->ccm_remainder_len + ctx->ccm_mac_len);
27423c57df7Smcpowers 
27523c57df7Smcpowers 	if (ctx->ccm_remainder_len > 0) {
27623c57df7Smcpowers 
27723c57df7Smcpowers 		/* copy temporary block to where it belongs */
27823c57df7Smcpowers 		if (out_data_2 == NULL) {
27923c57df7Smcpowers 			/* everything will fit in out_data_1 */
28023c57df7Smcpowers 			bcopy(macp, out_data_1, ctx->ccm_remainder_len);
28123c57df7Smcpowers 			bcopy(ccm_mac_p, out_data_1 + ctx->ccm_remainder_len,
28223c57df7Smcpowers 			    ctx->ccm_mac_len);
28323c57df7Smcpowers 		} else {
28423c57df7Smcpowers 
28523c57df7Smcpowers 			if (out_data_1_len < ctx->ccm_remainder_len) {
28623c57df7Smcpowers 
28723c57df7Smcpowers 				size_t data_2_len_used;
28823c57df7Smcpowers 
28923c57df7Smcpowers 				bcopy(macp, out_data_1, out_data_1_len);
29023c57df7Smcpowers 
29123c57df7Smcpowers 				data_2_len_used = ctx->ccm_remainder_len
29223c57df7Smcpowers 				    - out_data_1_len;
29323c57df7Smcpowers 
29423c57df7Smcpowers 				bcopy((uint8_t *)macp + out_data_1_len,
29523c57df7Smcpowers 				    out_data_2, data_2_len_used);
29623c57df7Smcpowers 				bcopy(ccm_mac_p, out_data_2 + data_2_len_used,
29723c57df7Smcpowers 				    ctx->ccm_mac_len);
29823c57df7Smcpowers 			} else {
29923c57df7Smcpowers 				bcopy(macp, out_data_1, out_data_1_len);
30023c57df7Smcpowers 				if (out_data_1_len == ctx->ccm_remainder_len) {
30123c57df7Smcpowers 					/* mac will be in out_data_2 */
30223c57df7Smcpowers 					bcopy(ccm_mac_p, out_data_2,
30323c57df7Smcpowers 					    ctx->ccm_mac_len);
30423c57df7Smcpowers 				} else {
305*4b56a003SDaniel Anderson 					size_t len_not_used = out_data_1_len -
30623c57df7Smcpowers 					    ctx->ccm_remainder_len;
30723c57df7Smcpowers 					/*
30823c57df7Smcpowers 					 * part of mac in will be in
30923c57df7Smcpowers 					 * out_data_1, part of the mac will be
31023c57df7Smcpowers 					 * in out_data_2
31123c57df7Smcpowers 					 */
31223c57df7Smcpowers 					bcopy(ccm_mac_p,
31323c57df7Smcpowers 					    out_data_1 + ctx->ccm_remainder_len,
31423c57df7Smcpowers 					    len_not_used);
31523c57df7Smcpowers 					bcopy(ccm_mac_p + len_not_used,
31623c57df7Smcpowers 					    out_data_2,
31723c57df7Smcpowers 					    ctx->ccm_mac_len - len_not_used);
31823c57df7Smcpowers 
31923c57df7Smcpowers 				}
32023c57df7Smcpowers 			}
32123c57df7Smcpowers 		}
32223c57df7Smcpowers 	} else {
32323c57df7Smcpowers 		/* copy block to where it belongs */
32423c57df7Smcpowers 		bcopy(ccm_mac_p, out_data_1, out_data_1_len);
32523c57df7Smcpowers 		if (out_data_2 != NULL) {
32623c57df7Smcpowers 			bcopy(ccm_mac_p + out_data_1_len, out_data_2,
32723c57df7Smcpowers 			    block_size - out_data_1_len);
32823c57df7Smcpowers 		}
32923c57df7Smcpowers 	}
33023c57df7Smcpowers 	out->cd_offset += ctx->ccm_remainder_len + ctx->ccm_mac_len;
33123c57df7Smcpowers 	ctx->ccm_remainder_len = 0;
33223c57df7Smcpowers 	return (CRYPTO_SUCCESS);
33323c57df7Smcpowers }
33423c57df7Smcpowers 
33523c57df7Smcpowers /*
33623c57df7Smcpowers  * This will only deal with decrypting the last block of the input that
33723c57df7Smcpowers  * might not be a multiple of block length.
33823c57df7Smcpowers  */
33923c57df7Smcpowers void
34023c57df7Smcpowers ccm_decrypt_incomplete_block(ccm_ctx_t *ctx,
34123c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *))
34223c57df7Smcpowers {
34323c57df7Smcpowers 	uint8_t *datap, *outp, *counterp;
34423c57df7Smcpowers 	int i;
34523c57df7Smcpowers 
34623c57df7Smcpowers 	datap = (uint8_t *)ctx->ccm_remainder;
34723c57df7Smcpowers 	outp = &((ctx->ccm_pt_buf)[ctx->ccm_processed_data_len]);
34823c57df7Smcpowers 
34923c57df7Smcpowers 	counterp = (uint8_t *)ctx->ccm_tmp;
35023c57df7Smcpowers 	encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, counterp);
35123c57df7Smcpowers 
35223c57df7Smcpowers 	/* XOR with counter block */
35323c57df7Smcpowers 	for (i = 0; i < ctx->ccm_remainder_len; i++) {
35423c57df7Smcpowers 		outp[i] = datap[i] ^ counterp[i];
35523c57df7Smcpowers 	}
35623c57df7Smcpowers }
35723c57df7Smcpowers 
35823c57df7Smcpowers /*
35923c57df7Smcpowers  * This will decrypt the cipher text.  However, the plaintext won't be
36023c57df7Smcpowers  * returned to the caller.  It will be returned when decrypt_final() is
36123c57df7Smcpowers  * called if the MAC matches
36223c57df7Smcpowers  */
36323c57df7Smcpowers /* ARGSUSED */
36423c57df7Smcpowers int
36523c57df7Smcpowers ccm_mode_decrypt_contiguous_blocks(ccm_ctx_t *ctx, char *data, size_t length,
36623c57df7Smcpowers     crypto_data_t *out, size_t block_size,
36723c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
36823c57df7Smcpowers     void (*copy_block)(uint8_t *, uint8_t *),
36923c57df7Smcpowers     void (*xor_block)(uint8_t *, uint8_t *))
37023c57df7Smcpowers {
37123c57df7Smcpowers 	size_t remainder = length;
37223c57df7Smcpowers 	size_t need;
37323c57df7Smcpowers 	uint8_t *datap = (uint8_t *)data;
37423c57df7Smcpowers 	uint8_t *blockp;
37523c57df7Smcpowers 	uint8_t *cbp;
37623c57df7Smcpowers 	uint64_t counter;
37723c57df7Smcpowers 	size_t pt_len, total_decrypted_len, mac_len, pm_len, pd_len;
37823c57df7Smcpowers 	uint8_t *resultp;
37923c57df7Smcpowers #ifdef _LITTLE_ENDIAN
38023c57df7Smcpowers 	uint8_t *p;
38123c57df7Smcpowers #endif	/* _LITTLE_ENDIAN */
38223c57df7Smcpowers 
38323c57df7Smcpowers 
38423c57df7Smcpowers 	pm_len = ctx->ccm_processed_mac_len;
38523c57df7Smcpowers 
38623c57df7Smcpowers 	if (pm_len > 0) {
38723c57df7Smcpowers 		uint8_t *tmp;
38823c57df7Smcpowers 		/*
38923c57df7Smcpowers 		 * all ciphertext has been processed, just waiting for
39023c57df7Smcpowers 		 * part of the value of the mac
39123c57df7Smcpowers 		 */
39223c57df7Smcpowers 		if ((pm_len + length) > ctx->ccm_mac_len) {
39323c57df7Smcpowers 			return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
39423c57df7Smcpowers 		}
39523c57df7Smcpowers 		tmp = (uint8_t *)ctx->ccm_mac_input_buf;
39623c57df7Smcpowers 
39723c57df7Smcpowers 		bcopy(datap, tmp + pm_len, length);
39823c57df7Smcpowers 
39923c57df7Smcpowers 		ctx->ccm_processed_mac_len += length;
40023c57df7Smcpowers 		return (CRYPTO_SUCCESS);
40123c57df7Smcpowers 	}
40223c57df7Smcpowers 
40323c57df7Smcpowers 	/*
40423c57df7Smcpowers 	 * If we decrypt the given data, what total amount of data would
40523c57df7Smcpowers 	 * have been decrypted?
40623c57df7Smcpowers 	 */
40723c57df7Smcpowers 	pd_len = ctx->ccm_processed_data_len;
40823c57df7Smcpowers 	total_decrypted_len = pd_len + length + ctx->ccm_remainder_len;
40923c57df7Smcpowers 
41023c57df7Smcpowers 	if (total_decrypted_len >
41123c57df7Smcpowers 	    (ctx->ccm_data_len + ctx->ccm_mac_len)) {
41223c57df7Smcpowers 		return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
41323c57df7Smcpowers 	}
41423c57df7Smcpowers 
41523c57df7Smcpowers 	pt_len = ctx->ccm_data_len;
41623c57df7Smcpowers 
41723c57df7Smcpowers 	if (total_decrypted_len > pt_len) {
41823c57df7Smcpowers 		/*
41923c57df7Smcpowers 		 * part of the input will be the MAC, need to isolate that
42023c57df7Smcpowers 		 * to be dealt with later.  The left-over data in
42123c57df7Smcpowers 		 * ccm_remainder_len from last time will not be part of the
42223c57df7Smcpowers 		 * MAC.  Otherwise, it would have already been taken out
42323c57df7Smcpowers 		 * when this call is made last time.
42423c57df7Smcpowers 		 */
42523c57df7Smcpowers 		size_t pt_part = pt_len - pd_len - ctx->ccm_remainder_len;
42623c57df7Smcpowers 
42723c57df7Smcpowers 		mac_len = length - pt_part;
42823c57df7Smcpowers 
42923c57df7Smcpowers 		ctx->ccm_processed_mac_len = mac_len;
43023c57df7Smcpowers 		bcopy(data + pt_part, ctx->ccm_mac_input_buf, mac_len);
43123c57df7Smcpowers 
43223c57df7Smcpowers 		if (pt_part + ctx->ccm_remainder_len < block_size) {
43323c57df7Smcpowers 			/*
43423c57df7Smcpowers 			 * since this is last of the ciphertext, will
43523c57df7Smcpowers 			 * just decrypt with it here
43623c57df7Smcpowers 			 */
43723c57df7Smcpowers 			bcopy(datap, &((uint8_t *)ctx->ccm_remainder)
43823c57df7Smcpowers 			    [ctx->ccm_remainder_len], pt_part);
43923c57df7Smcpowers 			ctx->ccm_remainder_len += pt_part;
44023c57df7Smcpowers 			ccm_decrypt_incomplete_block(ctx, encrypt_block);
44123c57df7Smcpowers 			ctx->ccm_remainder_len = 0;
44223c57df7Smcpowers 			ctx->ccm_processed_data_len += pt_part;
44323c57df7Smcpowers 			return (CRYPTO_SUCCESS);
44423c57df7Smcpowers 		} else {
44523c57df7Smcpowers 			/* let rest of the code handle this */
44623c57df7Smcpowers 			length = pt_part;
44723c57df7Smcpowers 		}
44823c57df7Smcpowers 	} else if (length + ctx->ccm_remainder_len < block_size) {
44923c57df7Smcpowers 			/* accumulate bytes here and return */
45023c57df7Smcpowers 		bcopy(datap,
45123c57df7Smcpowers 		    (uint8_t *)ctx->ccm_remainder + ctx->ccm_remainder_len,
45223c57df7Smcpowers 		    length);
45323c57df7Smcpowers 		ctx->ccm_remainder_len += length;
45423c57df7Smcpowers 		ctx->ccm_copy_to = datap;
45523c57df7Smcpowers 		return (CRYPTO_SUCCESS);
45623c57df7Smcpowers 	}
45723c57df7Smcpowers 
45823c57df7Smcpowers 	do {
45923c57df7Smcpowers 		/* Unprocessed data from last call. */
46023c57df7Smcpowers 		if (ctx->ccm_remainder_len > 0) {
46123c57df7Smcpowers 			need = block_size - ctx->ccm_remainder_len;
46223c57df7Smcpowers 
46323c57df7Smcpowers 			if (need > remainder)
46423c57df7Smcpowers 				return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
46523c57df7Smcpowers 
46623c57df7Smcpowers 			bcopy(datap, &((uint8_t *)ctx->ccm_remainder)
46723c57df7Smcpowers 			    [ctx->ccm_remainder_len], need);
46823c57df7Smcpowers 
46923c57df7Smcpowers 			blockp = (uint8_t *)ctx->ccm_remainder;
47023c57df7Smcpowers 		} else {
47123c57df7Smcpowers 			blockp = datap;
47223c57df7Smcpowers 		}
47323c57df7Smcpowers 
47423c57df7Smcpowers 		/* Calculate the counter mode, ccm_cb is the counter block */
47523c57df7Smcpowers 		cbp = (uint8_t *)ctx->ccm_tmp;
47623c57df7Smcpowers 		encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, cbp);
47723c57df7Smcpowers 
47823c57df7Smcpowers 		/*
47923c57df7Smcpowers 		 * Increment counter.
48023c57df7Smcpowers 		 * Counter bits are confined to the bottom 64 bits
48123c57df7Smcpowers 		 */
482*4b56a003SDaniel Anderson #ifdef _LITTLE_ENDIAN
483*4b56a003SDaniel Anderson 		counter = ntohll(ctx->ccm_cb[1] & ctx->ccm_counter_mask);
484*4b56a003SDaniel Anderson 		counter = htonll(counter + 1);
485*4b56a003SDaniel Anderson #else
48623c57df7Smcpowers 		counter = ctx->ccm_cb[1] & ctx->ccm_counter_mask;
48723c57df7Smcpowers 		counter++;
488*4b56a003SDaniel Anderson #endif	/* _LITTLE_ENDIAN */
48923c57df7Smcpowers 		counter &= ctx->ccm_counter_mask;
49023c57df7Smcpowers 		ctx->ccm_cb[1] =
49123c57df7Smcpowers 		    (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter;
49223c57df7Smcpowers 
49323c57df7Smcpowers 		/* XOR with the ciphertext */
49423c57df7Smcpowers 		xor_block(blockp, cbp);
49523c57df7Smcpowers 
49623c57df7Smcpowers 		/* Copy the plaintext to the "holding buffer" */
49723c57df7Smcpowers 		resultp = (uint8_t *)ctx->ccm_pt_buf +
49823c57df7Smcpowers 		    ctx->ccm_processed_data_len;
49923c57df7Smcpowers 		copy_block(cbp, resultp);
50023c57df7Smcpowers 
50123c57df7Smcpowers 		ctx->ccm_processed_data_len += block_size;
50223c57df7Smcpowers 
50323c57df7Smcpowers 		ctx->ccm_lastp = blockp;
50423c57df7Smcpowers 
50523c57df7Smcpowers 		/* Update pointer to next block of data to be processed. */
50623c57df7Smcpowers 		if (ctx->ccm_remainder_len != 0) {
50723c57df7Smcpowers 			datap += need;
50823c57df7Smcpowers 			ctx->ccm_remainder_len = 0;
50923c57df7Smcpowers 		} else {
51023c57df7Smcpowers 			datap += block_size;
51123c57df7Smcpowers 		}
51223c57df7Smcpowers 
51323c57df7Smcpowers 		remainder = (size_t)&data[length] - (size_t)datap;
51423c57df7Smcpowers 
51523c57df7Smcpowers 		/* Incomplete last block */
51623c57df7Smcpowers 		if (remainder > 0 && remainder < block_size) {
51723c57df7Smcpowers 			bcopy(datap, ctx->ccm_remainder, remainder);
51823c57df7Smcpowers 			ctx->ccm_remainder_len = remainder;
51923c57df7Smcpowers 			ctx->ccm_copy_to = datap;
52023c57df7Smcpowers 			if (ctx->ccm_processed_mac_len > 0) {
52123c57df7Smcpowers 				/*
52223c57df7Smcpowers 				 * not expecting anymore ciphertext, just
52323c57df7Smcpowers 				 * compute plaintext for the remaining input
52423c57df7Smcpowers 				 */
52523c57df7Smcpowers 				ccm_decrypt_incomplete_block(ctx,
52623c57df7Smcpowers 				    encrypt_block);
52723c57df7Smcpowers 				ctx->ccm_processed_data_len += remainder;
52823c57df7Smcpowers 				ctx->ccm_remainder_len = 0;
52923c57df7Smcpowers 			}
53023c57df7Smcpowers 			goto out;
53123c57df7Smcpowers 		}
53223c57df7Smcpowers 		ctx->ccm_copy_to = NULL;
53323c57df7Smcpowers 
53423c57df7Smcpowers 	} while (remainder > 0);
53523c57df7Smcpowers 
53623c57df7Smcpowers out:
53723c57df7Smcpowers 	return (CRYPTO_SUCCESS);
53823c57df7Smcpowers }
53923c57df7Smcpowers 
54023c57df7Smcpowers int
54123c57df7Smcpowers ccm_decrypt_final(ccm_ctx_t *ctx, crypto_data_t *out, size_t block_size,
54223c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
54323c57df7Smcpowers     void (*copy_block)(uint8_t *, uint8_t *),
54423c57df7Smcpowers     void (*xor_block)(uint8_t *, uint8_t *))
54523c57df7Smcpowers {
54623c57df7Smcpowers 	size_t mac_remain, pt_len;
54723c57df7Smcpowers 	uint8_t *pt, *mac_buf, *macp, *ccm_mac_p;
54823c57df7Smcpowers 	void *iov_or_mp;
54923c57df7Smcpowers 	offset_t offset;
55023c57df7Smcpowers 	uint8_t *out_data_1, *out_data_2;
55123c57df7Smcpowers 	size_t out_data_1_len;
55223c57df7Smcpowers 
55323c57df7Smcpowers 	pt_len = ctx->ccm_data_len;
55423c57df7Smcpowers 
55523c57df7Smcpowers 	/* Make sure output buffer can fit all of the plaintext */
55623c57df7Smcpowers 	if (out->cd_length < pt_len) {
55723c57df7Smcpowers 		return (CRYPTO_DATA_LEN_RANGE);
55823c57df7Smcpowers 	}
55923c57df7Smcpowers 
56023c57df7Smcpowers 	pt = ctx->ccm_pt_buf;
56123c57df7Smcpowers 	mac_remain = ctx->ccm_processed_data_len;
56223c57df7Smcpowers 	mac_buf = (uint8_t *)ctx->ccm_mac_buf;
56323c57df7Smcpowers 
56423c57df7Smcpowers 	macp = (uint8_t *)ctx->ccm_tmp;
56523c57df7Smcpowers 
56623c57df7Smcpowers 	while (mac_remain > 0) {
56723c57df7Smcpowers 
56823c57df7Smcpowers 		if (mac_remain < block_size) {
56923c57df7Smcpowers 			bzero(macp, block_size);
57023c57df7Smcpowers 			bcopy(pt, macp, mac_remain);
57123c57df7Smcpowers 			mac_remain = 0;
57223c57df7Smcpowers 		} else {
57323c57df7Smcpowers 			copy_block(pt, macp);
57423c57df7Smcpowers 			mac_remain -= block_size;
57523c57df7Smcpowers 			pt += block_size;
57623c57df7Smcpowers 		}
57723c57df7Smcpowers 
57823c57df7Smcpowers 		/* calculate the CBC MAC */
57923c57df7Smcpowers 		xor_block(macp, mac_buf);
58023c57df7Smcpowers 		encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
58123c57df7Smcpowers 	}
58223c57df7Smcpowers 
58323c57df7Smcpowers 	/* Calculate the CCM MAC */
58423c57df7Smcpowers 	ccm_mac_p = (uint8_t *)ctx->ccm_tmp;
58523c57df7Smcpowers 	calculate_ccm_mac((ccm_ctx_t *)ctx, ccm_mac_p, encrypt_block);
58623c57df7Smcpowers 
58723c57df7Smcpowers 	/* compare the input CCM MAC value with what we calculated */
58823c57df7Smcpowers 	if (bcmp(ctx->ccm_mac_input_buf, ccm_mac_p, ctx->ccm_mac_len)) {
58923c57df7Smcpowers 		/* They don't match */
59023c57df7Smcpowers 		return (CRYPTO_INVALID_MAC);
59123c57df7Smcpowers 	} else {
59223c57df7Smcpowers 		crypto_init_ptrs(out, &iov_or_mp, &offset);
59323c57df7Smcpowers 		crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1,
59423c57df7Smcpowers 		    &out_data_1_len, &out_data_2, pt_len);
59523c57df7Smcpowers 		bcopy(ctx->ccm_pt_buf, out_data_1, out_data_1_len);
59623c57df7Smcpowers 		if (out_data_2 != NULL) {
59723c57df7Smcpowers 			bcopy((ctx->ccm_pt_buf) + out_data_1_len,
59823c57df7Smcpowers 			    out_data_2, pt_len - out_data_1_len);
59923c57df7Smcpowers 		}
60023c57df7Smcpowers 		out->cd_offset += pt_len;
60123c57df7Smcpowers 	}
60223c57df7Smcpowers 	return (CRYPTO_SUCCESS);
60323c57df7Smcpowers }
60423c57df7Smcpowers 
60523c57df7Smcpowers int
60623c57df7Smcpowers ccm_validate_args(CK_AES_CCM_PARAMS *ccm_param, boolean_t is_encrypt_init)
60723c57df7Smcpowers {
60823c57df7Smcpowers 	size_t macSize, nonceSize;
60923c57df7Smcpowers 	uint8_t q;
61023c57df7Smcpowers 	uint64_t maxValue;
61123c57df7Smcpowers 
61223c57df7Smcpowers 	/*
61323c57df7Smcpowers 	 * Check the length of the MAC.  The only valid
61423c57df7Smcpowers 	 * lengths for the MAC are: 4, 6, 8, 10, 12, 14, 16
61523c57df7Smcpowers 	 */
61623c57df7Smcpowers 	macSize = ccm_param->ulMACSize;
61723c57df7Smcpowers 	if ((macSize < 4) || (macSize > 16) || ((macSize % 2) != 0)) {
61823c57df7Smcpowers 		return (CRYPTO_MECHANISM_PARAM_INVALID);
61923c57df7Smcpowers 	}
62023c57df7Smcpowers 
62123c57df7Smcpowers 	/* Check the nonce length.  Valid values are 7, 8, 9, 10, 11, 12, 13 */
62223c57df7Smcpowers 	nonceSize = ccm_param->ulNonceSize;
62323c57df7Smcpowers 	if ((nonceSize < 7) || (nonceSize > 13)) {
62423c57df7Smcpowers 		return (CRYPTO_MECHANISM_PARAM_INVALID);
62523c57df7Smcpowers 	}
62623c57df7Smcpowers 
62723c57df7Smcpowers 	/* q is the length of the field storing the length, in bytes */
62823c57df7Smcpowers 	q = (uint8_t)((15 - nonceSize) & 0xFF);
62923c57df7Smcpowers 
63023c57df7Smcpowers 
63123c57df7Smcpowers 	/*
63223c57df7Smcpowers 	 * If it is decrypt, need to make sure size of ciphertext is at least
63323c57df7Smcpowers 	 * bigger than MAC len
63423c57df7Smcpowers 	 */
63523c57df7Smcpowers 	if ((!is_encrypt_init) && (ccm_param->ulDataSize < macSize)) {
63623c57df7Smcpowers 		return (CRYPTO_MECHANISM_PARAM_INVALID);
63723c57df7Smcpowers 	}
63823c57df7Smcpowers 
63923c57df7Smcpowers 	/*
64023c57df7Smcpowers 	 * Check to make sure the length of the payload is within the
64123c57df7Smcpowers 	 * range of values allowed by q
64223c57df7Smcpowers 	 */
64323c57df7Smcpowers 	if (q < 8) {
64423c57df7Smcpowers 		maxValue = (1ULL << (q * 8)) - 1;
64523c57df7Smcpowers 	} else {
64623c57df7Smcpowers 		maxValue = ULONG_MAX;
64723c57df7Smcpowers 	}
64823c57df7Smcpowers 
64923c57df7Smcpowers 	if (ccm_param->ulDataSize > maxValue) {
65023c57df7Smcpowers 		return (CRYPTO_MECHANISM_PARAM_INVALID);
65123c57df7Smcpowers 	}
65223c57df7Smcpowers 	return (CRYPTO_SUCCESS);
65323c57df7Smcpowers }
65423c57df7Smcpowers 
65523c57df7Smcpowers /*
65623c57df7Smcpowers  * Format the first block used in CBC-MAC (B0) and the initial counter
65723c57df7Smcpowers  * block based on formatting functions and counter generation functions
65823c57df7Smcpowers  * specified in RFC 3610 and NIST publication 800-38C, appendix A
65923c57df7Smcpowers  *
66023c57df7Smcpowers  * b0 is the first block used in CBC-MAC
66123c57df7Smcpowers  * cb0 is the first counter block
66223c57df7Smcpowers  *
66323c57df7Smcpowers  * It's assumed that the arguments b0 and cb0 are preallocated AES blocks
66423c57df7Smcpowers  *
66523c57df7Smcpowers  */
66623c57df7Smcpowers static void
66723c57df7Smcpowers ccm_format_initial_blocks(uchar_t *nonce, ulong_t nonceSize,
66823c57df7Smcpowers     ulong_t authDataSize, uint8_t *b0, ccm_ctx_t *aes_ctx)
66923c57df7Smcpowers {
67023c57df7Smcpowers 	uint64_t payloadSize;
67123c57df7Smcpowers 	uint8_t t, q, have_adata = 0;
67223c57df7Smcpowers 	size_t limit;
67323c57df7Smcpowers 	int i, j, k;
67423c57df7Smcpowers 	uint64_t mask = 0;
67523c57df7Smcpowers 	uint8_t *cb;
67623c57df7Smcpowers 
67723c57df7Smcpowers 	q = (uint8_t)((15 - nonceSize) & 0xFF);
67823c57df7Smcpowers 	t = (uint8_t)((aes_ctx->ccm_mac_len) & 0xFF);
67923c57df7Smcpowers 
68023c57df7Smcpowers 	/* Construct the first octet of b0 */
68123c57df7Smcpowers 	if (authDataSize > 0) {
68223c57df7Smcpowers 		have_adata = 1;
68323c57df7Smcpowers 	}
68423c57df7Smcpowers 	b0[0] = (have_adata << 6) | (((t - 2)  / 2) << 3) | (q - 1);
68523c57df7Smcpowers 
68623c57df7Smcpowers 	/* copy the nonce value into b0 */
68723c57df7Smcpowers 	bcopy(nonce, &(b0[1]), nonceSize);
68823c57df7Smcpowers 
68923c57df7Smcpowers 	/* store the length of the payload into b0 */
69023c57df7Smcpowers 	bzero(&(b0[1+nonceSize]), q);
69123c57df7Smcpowers 
69223c57df7Smcpowers 	payloadSize = aes_ctx->ccm_data_len;
69323c57df7Smcpowers 	limit = 8 < q ? 8 : q;
69423c57df7Smcpowers 
69523c57df7Smcpowers 	for (i = 0, j = 0, k = 15; i < limit; i++, j += 8, k--) {
69623c57df7Smcpowers 		b0[k] = (uint8_t)((payloadSize >> j) & 0xFF);
69723c57df7Smcpowers 	}
69823c57df7Smcpowers 
69923c57df7Smcpowers 	/* format the counter block */
70023c57df7Smcpowers 
70123c57df7Smcpowers 	cb = (uint8_t *)aes_ctx->ccm_cb;
70223c57df7Smcpowers 
70323c57df7Smcpowers 	cb[0] = 0x07 & (q-1); /* first byte */
70423c57df7Smcpowers 
70523c57df7Smcpowers 	/* copy the nonce value into the counter block */
70623c57df7Smcpowers 	bcopy(nonce, &(cb[1]), nonceSize);
70723c57df7Smcpowers 
70823c57df7Smcpowers 	bzero(&(cb[1+nonceSize]), q);
70923c57df7Smcpowers 
71023c57df7Smcpowers 	/* Create the mask for the counter field based on the size of nonce */
71123c57df7Smcpowers 	q <<= 3;
71223c57df7Smcpowers 	while (q-- > 0) {
71323c57df7Smcpowers 		mask |= (1ULL << q);
71423c57df7Smcpowers 	}
71523c57df7Smcpowers 
71623c57df7Smcpowers #ifdef _LITTLE_ENDIAN
717*4b56a003SDaniel Anderson 	mask = htonll(mask);
71823c57df7Smcpowers #endif
71923c57df7Smcpowers 	aes_ctx->ccm_counter_mask = mask;
72023c57df7Smcpowers 
72123c57df7Smcpowers 	/*
72223c57df7Smcpowers 	 * During calculation, we start using counter block 1, we will
72323c57df7Smcpowers 	 * set it up right here.
72423c57df7Smcpowers 	 * We can just set the last byte to have the value 1, because
72523c57df7Smcpowers 	 * even with the biggest nonce of 13, the last byte of the
72623c57df7Smcpowers 	 * counter block will be used for the counter value.
72723c57df7Smcpowers 	 */
72823c57df7Smcpowers 	cb[15] = 0x01;
72923c57df7Smcpowers }
73023c57df7Smcpowers 
73123c57df7Smcpowers /*
73223c57df7Smcpowers  * Encode the length of the associated data as
73323c57df7Smcpowers  * specified in RFC 3610 and NIST publication 800-38C, appendix A
73423c57df7Smcpowers  */
73523c57df7Smcpowers static void
73623c57df7Smcpowers encode_adata_len(ulong_t auth_data_len, uint8_t *encoded, size_t *encoded_len)
73723c57df7Smcpowers {
738*4b56a003SDaniel Anderson #ifdef UNALIGNED_POINTERS_PERMITTED
739*4b56a003SDaniel Anderson 	uint32_t	*lencoded_ptr;
740*4b56a003SDaniel Anderson #ifdef _LP64
741*4b56a003SDaniel Anderson 	uint64_t	*llencoded_ptr;
742*4b56a003SDaniel Anderson #endif
743*4b56a003SDaniel Anderson #endif	/* UNALIGNED_POINTERS_PERMITTED */
744*4b56a003SDaniel Anderson 
74523c57df7Smcpowers 	if (auth_data_len < ((1ULL<<16) - (1ULL<<8))) {
74623c57df7Smcpowers 		/* 0 < a < (2^16-2^8) */
74723c57df7Smcpowers 		*encoded_len = 2;
74823c57df7Smcpowers 		encoded[0] = (auth_data_len & 0xff00) >> 8;
74923c57df7Smcpowers 		encoded[1] = auth_data_len & 0xff;
75023c57df7Smcpowers 
75123c57df7Smcpowers 	} else if ((auth_data_len >= ((1ULL<<16) - (1ULL<<8))) &&
75223c57df7Smcpowers 	    (auth_data_len < (1ULL << 31))) {
75323c57df7Smcpowers 		/* (2^16-2^8) <= a < 2^32 */
75423c57df7Smcpowers 		*encoded_len = 6;
75523c57df7Smcpowers 		encoded[0] = 0xff;
75623c57df7Smcpowers 		encoded[1] = 0xfe;
757*4b56a003SDaniel Anderson #ifdef UNALIGNED_POINTERS_PERMITTED
758*4b56a003SDaniel Anderson 		lencoded_ptr = (uint32_t *)&encoded[2];
759*4b56a003SDaniel Anderson 		*lencoded_ptr = htonl(auth_data_len);
760*4b56a003SDaniel Anderson #else
76123c57df7Smcpowers 		encoded[2] = (auth_data_len & 0xff000000) >> 24;
76223c57df7Smcpowers 		encoded[3] = (auth_data_len & 0xff0000) >> 16;
76323c57df7Smcpowers 		encoded[4] = (auth_data_len & 0xff00) >> 8;
76423c57df7Smcpowers 		encoded[5] = auth_data_len & 0xff;
765*4b56a003SDaniel Anderson #endif	/* UNALIGNED_POINTERS_PERMITTED */
766*4b56a003SDaniel Anderson 
76723c57df7Smcpowers #ifdef _LP64
76823c57df7Smcpowers 	} else {
76923c57df7Smcpowers 		/* 2^32 <= a < 2^64 */
77023c57df7Smcpowers 		*encoded_len = 10;
77123c57df7Smcpowers 		encoded[0] = 0xff;
77223c57df7Smcpowers 		encoded[1] = 0xff;
773*4b56a003SDaniel Anderson #ifdef UNALIGNED_POINTERS_PERMITTED
774*4b56a003SDaniel Anderson 		llencoded_ptr = (uint64_t *)&encoded[2];
775*4b56a003SDaniel Anderson 		*llencoded_ptr = htonl(auth_data_len);
776*4b56a003SDaniel Anderson #else
77723c57df7Smcpowers 		encoded[2] = (auth_data_len & 0xff00000000000000) >> 56;
77823c57df7Smcpowers 		encoded[3] = (auth_data_len & 0xff000000000000) >> 48;
77923c57df7Smcpowers 		encoded[4] = (auth_data_len & 0xff0000000000) >> 40;
78023c57df7Smcpowers 		encoded[5] = (auth_data_len & 0xff00000000) >> 32;
78123c57df7Smcpowers 		encoded[6] = (auth_data_len & 0xff000000) >> 24;
78223c57df7Smcpowers 		encoded[7] = (auth_data_len & 0xff0000) >> 16;
78323c57df7Smcpowers 		encoded[8] = (auth_data_len & 0xff00) >> 8;
78423c57df7Smcpowers 		encoded[9] = auth_data_len & 0xff;
785*4b56a003SDaniel Anderson #endif	/* UNALIGNED_POINTERS_PERMITTED */
78623c57df7Smcpowers #endif	/* _LP64 */
78723c57df7Smcpowers 	}
78823c57df7Smcpowers }
78923c57df7Smcpowers 
79023c57df7Smcpowers /*
79123c57df7Smcpowers  * The following function should be call at encrypt or decrypt init time
79223c57df7Smcpowers  * for AES CCM mode.
79323c57df7Smcpowers  */
79423c57df7Smcpowers int
79523c57df7Smcpowers ccm_init(ccm_ctx_t *ctx, unsigned char *nonce, size_t nonce_len,
79623c57df7Smcpowers     unsigned char *auth_data, size_t auth_data_len, size_t block_size,
79723c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
79823c57df7Smcpowers     void (*xor_block)(uint8_t *, uint8_t *))
79923c57df7Smcpowers {
80023c57df7Smcpowers 	uint8_t *mac_buf, *datap, *ivp, *authp;
80123c57df7Smcpowers 	size_t remainder, processed;
80223c57df7Smcpowers 	uint8_t encoded_a[10]; /* max encoded auth data length is 10 octets */
80323c57df7Smcpowers 	size_t encoded_a_len = 0;
80423c57df7Smcpowers 
80523c57df7Smcpowers 	mac_buf = (uint8_t *)&(ctx->ccm_mac_buf);
80623c57df7Smcpowers 
80723c57df7Smcpowers 	/*
80823c57df7Smcpowers 	 * Format the 1st block for CBC-MAC and construct the
80923c57df7Smcpowers 	 * 1st counter block.
81023c57df7Smcpowers 	 *
81123c57df7Smcpowers 	 * aes_ctx->ccm_iv is used for storing the counter block
81223c57df7Smcpowers 	 * mac_buf will store b0 at this time.
81323c57df7Smcpowers 	 */
81423c57df7Smcpowers 	ccm_format_initial_blocks(nonce, nonce_len,
81523c57df7Smcpowers 	    auth_data_len, mac_buf, ctx);
81623c57df7Smcpowers 
81723c57df7Smcpowers 	/* The IV for CBC MAC for AES CCM mode is always zero */
81823c57df7Smcpowers 	ivp = (uint8_t *)ctx->ccm_tmp;
81923c57df7Smcpowers 	bzero(ivp, block_size);
82023c57df7Smcpowers 
82123c57df7Smcpowers 	xor_block(ivp, mac_buf);
82223c57df7Smcpowers 
82323c57df7Smcpowers 	/* encrypt the nonce */
82423c57df7Smcpowers 	encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
82523c57df7Smcpowers 
82623c57df7Smcpowers 	/* take care of the associated data, if any */
82723c57df7Smcpowers 	if (auth_data_len == 0) {
82823c57df7Smcpowers 		return (CRYPTO_SUCCESS);
82923c57df7Smcpowers 	}
83023c57df7Smcpowers 
83123c57df7Smcpowers 	encode_adata_len(auth_data_len, encoded_a, &encoded_a_len);
83223c57df7Smcpowers 
83323c57df7Smcpowers 	remainder = auth_data_len;
83423c57df7Smcpowers 
83523c57df7Smcpowers 	/* 1st block: it contains encoded associated data, and some data */
83623c57df7Smcpowers 	authp = (uint8_t *)ctx->ccm_tmp;
83723c57df7Smcpowers 	bzero(authp, block_size);
83823c57df7Smcpowers 	bcopy(encoded_a, authp, encoded_a_len);
83923c57df7Smcpowers 	processed = block_size - encoded_a_len;
84023c57df7Smcpowers 	if (processed > auth_data_len) {
84123c57df7Smcpowers 		/* in case auth_data is very small */
84223c57df7Smcpowers 		processed = auth_data_len;
84323c57df7Smcpowers 	}
84423c57df7Smcpowers 	bcopy(auth_data, authp+encoded_a_len, processed);
84523c57df7Smcpowers 	/* xor with previous buffer */
84623c57df7Smcpowers 	xor_block(authp, mac_buf);
84723c57df7Smcpowers 	encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
84823c57df7Smcpowers 	remainder -= processed;
84923c57df7Smcpowers 	if (remainder == 0) {
85023c57df7Smcpowers 		/* a small amount of associated data, it's all done now */
85123c57df7Smcpowers 		return (CRYPTO_SUCCESS);
85223c57df7Smcpowers 	}
85323c57df7Smcpowers 
85423c57df7Smcpowers 	do {
85523c57df7Smcpowers 		if (remainder < block_size) {
85623c57df7Smcpowers 			/*
85723c57df7Smcpowers 			 * There's not a block full of data, pad rest of
85823c57df7Smcpowers 			 * buffer with zero
85923c57df7Smcpowers 			 */
86023c57df7Smcpowers 			bzero(authp, block_size);
86123c57df7Smcpowers 			bcopy(&(auth_data[processed]), authp, remainder);
86223c57df7Smcpowers 			datap = (uint8_t *)authp;
86323c57df7Smcpowers 			remainder = 0;
86423c57df7Smcpowers 		} else {
86523c57df7Smcpowers 			datap = (uint8_t *)(&(auth_data[processed]));
86623c57df7Smcpowers 			processed += block_size;
86723c57df7Smcpowers 			remainder -= block_size;
86823c57df7Smcpowers 		}
86923c57df7Smcpowers 
87023c57df7Smcpowers 		xor_block(datap, mac_buf);
87123c57df7Smcpowers 		encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
87223c57df7Smcpowers 
87323c57df7Smcpowers 	} while (remainder > 0);
87423c57df7Smcpowers 
87523c57df7Smcpowers 	return (CRYPTO_SUCCESS);
87623c57df7Smcpowers }
87723c57df7Smcpowers 
87823c57df7Smcpowers int
87923c57df7Smcpowers ccm_init_ctx(ccm_ctx_t *ccm_ctx, char *param, int kmflag,
88023c57df7Smcpowers     boolean_t is_encrypt_init, size_t block_size,
88123c57df7Smcpowers     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
88223c57df7Smcpowers     void (*xor_block)(uint8_t *, uint8_t *))
88323c57df7Smcpowers {
88423c57df7Smcpowers 	int rv;
88523c57df7Smcpowers 	CK_AES_CCM_PARAMS *ccm_param;
88623c57df7Smcpowers 
88723c57df7Smcpowers 	if (param != NULL) {
88823c57df7Smcpowers 		ccm_param = (CK_AES_CCM_PARAMS *)param;
88923c57df7Smcpowers 
89023c57df7Smcpowers 		if ((rv = ccm_validate_args(ccm_param,
89123c57df7Smcpowers 		    is_encrypt_init)) != 0) {
89223c57df7Smcpowers 			return (rv);
89323c57df7Smcpowers 		}
89423c57df7Smcpowers 
89523c57df7Smcpowers 		ccm_ctx->ccm_mac_len = ccm_param->ulMACSize;
89623c57df7Smcpowers 		if (is_encrypt_init) {
89723c57df7Smcpowers 			ccm_ctx->ccm_data_len = ccm_param->ulDataSize;
89823c57df7Smcpowers 		} else {
89923c57df7Smcpowers 			ccm_ctx->ccm_data_len =
90023c57df7Smcpowers 			    ccm_param->ulDataSize - ccm_ctx->ccm_mac_len;
90123c57df7Smcpowers 			ccm_ctx->ccm_processed_mac_len = 0;
90223c57df7Smcpowers 		}
90323c57df7Smcpowers 		ccm_ctx->ccm_processed_data_len = 0;
90423c57df7Smcpowers 
90523c57df7Smcpowers 		ccm_ctx->ccm_flags |= CCM_MODE;
90623c57df7Smcpowers 	} else {
90723c57df7Smcpowers 		rv = CRYPTO_MECHANISM_PARAM_INVALID;
90823c57df7Smcpowers 		goto out;
90923c57df7Smcpowers 	}
91023c57df7Smcpowers 
91123c57df7Smcpowers 	if (ccm_init(ccm_ctx, ccm_param->nonce, ccm_param->ulNonceSize,
91223c57df7Smcpowers 	    ccm_param->authData, ccm_param->ulAuthDataSize, block_size,
91323c57df7Smcpowers 	    encrypt_block, xor_block) != 0) {
91423c57df7Smcpowers 		rv = CRYPTO_MECHANISM_PARAM_INVALID;
91523c57df7Smcpowers 		goto out;
91623c57df7Smcpowers 	}
91723c57df7Smcpowers 	if (!is_encrypt_init) {
91823c57df7Smcpowers 		/* allocate buffer for storing decrypted plaintext */
91923c57df7Smcpowers #ifdef _KERNEL
92023c57df7Smcpowers 		ccm_ctx->ccm_pt_buf = kmem_alloc(ccm_ctx->ccm_data_len,
92123c57df7Smcpowers 		    kmflag);
92223c57df7Smcpowers #else
92323c57df7Smcpowers 		ccm_ctx->ccm_pt_buf = malloc(ccm_ctx->ccm_data_len);
92423c57df7Smcpowers #endif
92523c57df7Smcpowers 		if (ccm_ctx->ccm_pt_buf == NULL) {
92623c57df7Smcpowers 			rv = CRYPTO_HOST_MEMORY;
92723c57df7Smcpowers 		}
92823c57df7Smcpowers 	}
92923c57df7Smcpowers out:
93023c57df7Smcpowers 	return (rv);
93123c57df7Smcpowers }
93223c57df7Smcpowers 
93323c57df7Smcpowers void *
93423c57df7Smcpowers ccm_alloc_ctx(int kmflag)
93523c57df7Smcpowers {
93623c57df7Smcpowers 	ccm_ctx_t *ccm_ctx;
93723c57df7Smcpowers 
93823c57df7Smcpowers #ifdef _KERNEL
93923c57df7Smcpowers 	if ((ccm_ctx = kmem_zalloc(sizeof (ccm_ctx_t), kmflag)) == NULL)
94023c57df7Smcpowers #else
94123c57df7Smcpowers 	if ((ccm_ctx = calloc(1, sizeof (ccm_ctx_t))) == NULL)
94223c57df7Smcpowers #endif
94323c57df7Smcpowers 		return (NULL);
94423c57df7Smcpowers 
94523c57df7Smcpowers 	ccm_ctx->ccm_flags = CCM_MODE;
94623c57df7Smcpowers 	return (ccm_ctx);
94723c57df7Smcpowers }
948