xref: /titanic_51/usr/src/common/crypto/modes/ccm.c (revision 8d8453de6847166cd4079123d746fa7e23023bca)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef _KERNEL
27 #include <strings.h>
28 #include <limits.h>
29 #include <assert.h>
30 #include <security/cryptoki.h>
31 #endif
32 
33 #include <sys/types.h>
34 #include <sys/kmem.h>
35 #include <modes/modes.h>
36 #include <sys/crypto/common.h>
37 #include <sys/crypto/impl.h>
38 
39 #if defined(__i386) || defined(__amd64)
40 #include <sys/byteorder.h>
41 #define	UNALIGNED_POINTERS_PERMITTED
42 #endif
43 
44 /*
45  * Encrypt multiple blocks of data in CCM mode.  Decrypt for CCM mode
46  * is done in another function.
47  */
48 int
49 ccm_mode_encrypt_contiguous_blocks(ccm_ctx_t *ctx, char *data, size_t length,
50     crypto_data_t *out, size_t block_size,
51     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
52     void (*copy_block)(uint8_t *, uint8_t *),
53     void (*xor_block)(uint8_t *, uint8_t *))
54 {
55 	size_t remainder = length;
56 	size_t need;
57 	uint8_t *datap = (uint8_t *)data;
58 	uint8_t *blockp;
59 	uint8_t *lastp;
60 	void *iov_or_mp;
61 	offset_t offset;
62 	uint8_t *out_data_1;
63 	uint8_t *out_data_2;
64 	size_t out_data_1_len;
65 	uint64_t counter;
66 	uint8_t *mac_buf;
67 
68 	if (length + ctx->ccm_remainder_len < block_size) {
69 		/* accumulate bytes here and return */
70 		bcopy(datap,
71 		    (uint8_t *)ctx->ccm_remainder + ctx->ccm_remainder_len,
72 		    length);
73 		ctx->ccm_remainder_len += length;
74 		ctx->ccm_copy_to = datap;
75 		return (CRYPTO_SUCCESS);
76 	}
77 
78 	lastp = (uint8_t *)ctx->ccm_cb;
79 	if (out != NULL)
80 		crypto_init_ptrs(out, &iov_or_mp, &offset);
81 
82 	mac_buf = (uint8_t *)ctx->ccm_mac_buf;
83 
84 	do {
85 		/* Unprocessed data from last call. */
86 		if (ctx->ccm_remainder_len > 0) {
87 			need = block_size - ctx->ccm_remainder_len;
88 
89 			if (need > remainder)
90 				return (CRYPTO_DATA_LEN_RANGE);
91 
92 			bcopy(datap, &((uint8_t *)ctx->ccm_remainder)
93 			    [ctx->ccm_remainder_len], need);
94 
95 			blockp = (uint8_t *)ctx->ccm_remainder;
96 		} else {
97 			blockp = datap;
98 		}
99 
100 		/*
101 		 * do CBC MAC
102 		 *
103 		 * XOR the previous cipher block current clear block.
104 		 * mac_buf always contain previous cipher block.
105 		 */
106 		xor_block(blockp, mac_buf);
107 		encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
108 
109 		/* ccm_cb is the counter block */
110 		encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb,
111 		    (uint8_t *)ctx->ccm_tmp);
112 
113 		lastp = (uint8_t *)ctx->ccm_tmp;
114 
115 		/*
116 		 * Increment counter. Counter bits are confined
117 		 * to the bottom 64 bits of the counter block.
118 		 */
119 #ifdef _LITTLE_ENDIAN
120 		counter = ntohll(ctx->ccm_cb[1] & ctx->ccm_counter_mask);
121 		counter = htonll(counter + 1);
122 #else
123 		counter = ctx->ccm_cb[1] & ctx->ccm_counter_mask;
124 		counter++;
125 #endif	/* _LITTLE_ENDIAN */
126 		counter &= ctx->ccm_counter_mask;
127 		ctx->ccm_cb[1] =
128 		    (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter;
129 
130 		/*
131 		 * XOR encrypted counter block with the current clear block.
132 		 */
133 		xor_block(blockp, lastp);
134 
135 		ctx->ccm_processed_data_len += block_size;
136 
137 		if (out == NULL) {
138 			if (ctx->ccm_remainder_len > 0) {
139 				bcopy(blockp, ctx->ccm_copy_to,
140 				    ctx->ccm_remainder_len);
141 				bcopy(blockp + ctx->ccm_remainder_len, datap,
142 				    need);
143 			}
144 		} else {
145 			crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1,
146 			    &out_data_1_len, &out_data_2, block_size);
147 
148 			/* copy block to where it belongs */
149 			if (out_data_1_len == block_size) {
150 				copy_block(lastp, out_data_1);
151 			} else {
152 				bcopy(lastp, out_data_1, out_data_1_len);
153 				if (out_data_2 != NULL) {
154 					bcopy(lastp + out_data_1_len,
155 					    out_data_2,
156 					    block_size - out_data_1_len);
157 				}
158 			}
159 			/* update offset */
160 			out->cd_offset += block_size;
161 		}
162 
163 		/* Update pointer to next block of data to be processed. */
164 		if (ctx->ccm_remainder_len != 0) {
165 			datap += need;
166 			ctx->ccm_remainder_len = 0;
167 		} else {
168 			datap += block_size;
169 		}
170 
171 		remainder = (size_t)&data[length] - (size_t)datap;
172 
173 		/* Incomplete last block. */
174 		if (remainder > 0 && remainder < block_size) {
175 			bcopy(datap, ctx->ccm_remainder, remainder);
176 			ctx->ccm_remainder_len = remainder;
177 			ctx->ccm_copy_to = datap;
178 			goto out;
179 		}
180 		ctx->ccm_copy_to = NULL;
181 
182 	} while (remainder > 0);
183 
184 out:
185 	return (CRYPTO_SUCCESS);
186 }
187 
188 void
189 calculate_ccm_mac(ccm_ctx_t *ctx, uint8_t *ccm_mac,
190     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *))
191 {
192 	uint64_t counter;
193 	uint8_t *counterp, *mac_buf;
194 	int i;
195 
196 	mac_buf = (uint8_t *)ctx->ccm_mac_buf;
197 
198 	/* first counter block start with index 0 */
199 	counter = 0;
200 	ctx->ccm_cb[1] = (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter;
201 
202 	counterp = (uint8_t *)ctx->ccm_tmp;
203 	encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, counterp);
204 
205 	/* calculate XOR of MAC with first counter block */
206 	for (i = 0; i < ctx->ccm_mac_len; i++) {
207 		ccm_mac[i] = mac_buf[i] ^ counterp[i];
208 	}
209 }
210 
211 /* ARGSUSED */
212 int
213 ccm_encrypt_final(ccm_ctx_t *ctx, crypto_data_t *out, size_t block_size,
214     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
215     void (*xor_block)(uint8_t *, uint8_t *))
216 {
217 	uint8_t *lastp, *mac_buf, *ccm_mac_p, *macp;
218 	void *iov_or_mp;
219 	offset_t offset;
220 	uint8_t *out_data_1;
221 	uint8_t *out_data_2;
222 	size_t out_data_1_len;
223 	int i;
224 
225 	if (out->cd_length < (ctx->ccm_remainder_len + ctx->ccm_mac_len)) {
226 		return (CRYPTO_DATA_LEN_RANGE);
227 	}
228 
229 	/*
230 	 * When we get here, the number of bytes of payload processed
231 	 * plus whatever data remains, if any,
232 	 * should be the same as the number of bytes that's being
233 	 * passed in the argument during init time.
234 	 */
235 	if ((ctx->ccm_processed_data_len + ctx->ccm_remainder_len)
236 	    != (ctx->ccm_data_len)) {
237 		return (CRYPTO_DATA_LEN_RANGE);
238 	}
239 
240 	mac_buf = (uint8_t *)ctx->ccm_mac_buf;
241 
242 	if (ctx->ccm_remainder_len > 0) {
243 
244 		/* ccm_mac_input_buf is not used for encryption */
245 		macp = (uint8_t *)ctx->ccm_mac_input_buf;
246 		bzero(macp, block_size);
247 
248 		/* copy remainder to temporary buffer */
249 		bcopy(ctx->ccm_remainder, macp, ctx->ccm_remainder_len);
250 
251 		/* calculate the CBC MAC */
252 		xor_block(macp, mac_buf);
253 		encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
254 
255 		/* calculate the counter mode */
256 		lastp = (uint8_t *)ctx->ccm_tmp;
257 		encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, lastp);
258 
259 		/* XOR with counter block */
260 		for (i = 0; i < ctx->ccm_remainder_len; i++) {
261 			macp[i] ^= lastp[i];
262 		}
263 		ctx->ccm_processed_data_len += ctx->ccm_remainder_len;
264 	}
265 
266 	/* Calculate the CCM MAC */
267 	ccm_mac_p = (uint8_t *)ctx->ccm_tmp;
268 	calculate_ccm_mac(ctx, ccm_mac_p, encrypt_block);
269 
270 	crypto_init_ptrs(out, &iov_or_mp, &offset);
271 	crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1,
272 	    &out_data_1_len, &out_data_2,
273 	    ctx->ccm_remainder_len + ctx->ccm_mac_len);
274 
275 	if (ctx->ccm_remainder_len > 0) {
276 
277 		/* copy temporary block to where it belongs */
278 		if (out_data_2 == NULL) {
279 			/* everything will fit in out_data_1 */
280 			bcopy(macp, out_data_1, ctx->ccm_remainder_len);
281 			bcopy(ccm_mac_p, out_data_1 + ctx->ccm_remainder_len,
282 			    ctx->ccm_mac_len);
283 		} else {
284 
285 			if (out_data_1_len < ctx->ccm_remainder_len) {
286 
287 				size_t data_2_len_used;
288 
289 				bcopy(macp, out_data_1, out_data_1_len);
290 
291 				data_2_len_used = ctx->ccm_remainder_len
292 				    - out_data_1_len;
293 
294 				bcopy((uint8_t *)macp + out_data_1_len,
295 				    out_data_2, data_2_len_used);
296 				bcopy(ccm_mac_p, out_data_2 + data_2_len_used,
297 				    ctx->ccm_mac_len);
298 			} else {
299 				bcopy(macp, out_data_1, out_data_1_len);
300 				if (out_data_1_len == ctx->ccm_remainder_len) {
301 					/* mac will be in out_data_2 */
302 					bcopy(ccm_mac_p, out_data_2,
303 					    ctx->ccm_mac_len);
304 				} else {
305 					size_t len_not_used = out_data_1_len -
306 					    ctx->ccm_remainder_len;
307 					/*
308 					 * part of mac in will be in
309 					 * out_data_1, part of the mac will be
310 					 * in out_data_2
311 					 */
312 					bcopy(ccm_mac_p,
313 					    out_data_1 + ctx->ccm_remainder_len,
314 					    len_not_used);
315 					bcopy(ccm_mac_p + len_not_used,
316 					    out_data_2,
317 					    ctx->ccm_mac_len - len_not_used);
318 
319 				}
320 			}
321 		}
322 	} else {
323 		/* copy block to where it belongs */
324 		bcopy(ccm_mac_p, out_data_1, out_data_1_len);
325 		if (out_data_2 != NULL) {
326 			bcopy(ccm_mac_p + out_data_1_len, out_data_2,
327 			    block_size - out_data_1_len);
328 		}
329 	}
330 	out->cd_offset += ctx->ccm_remainder_len + ctx->ccm_mac_len;
331 	ctx->ccm_remainder_len = 0;
332 	return (CRYPTO_SUCCESS);
333 }
334 
335 /*
336  * This will only deal with decrypting the last block of the input that
337  * might not be a multiple of block length.
338  */
339 void
340 ccm_decrypt_incomplete_block(ccm_ctx_t *ctx,
341     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *))
342 {
343 	uint8_t *datap, *outp, *counterp;
344 	int i;
345 
346 	datap = (uint8_t *)ctx->ccm_remainder;
347 	outp = &((ctx->ccm_pt_buf)[ctx->ccm_processed_data_len]);
348 
349 	counterp = (uint8_t *)ctx->ccm_tmp;
350 	encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, counterp);
351 
352 	/* XOR with counter block */
353 	for (i = 0; i < ctx->ccm_remainder_len; i++) {
354 		outp[i] = datap[i] ^ counterp[i];
355 	}
356 }
357 
358 /*
359  * This will decrypt the cipher text.  However, the plaintext won't be
360  * returned to the caller.  It will be returned when decrypt_final() is
361  * called if the MAC matches
362  */
363 /* ARGSUSED */
364 int
365 ccm_mode_decrypt_contiguous_blocks(ccm_ctx_t *ctx, char *data, size_t length,
366     crypto_data_t *out, size_t block_size,
367     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
368     void (*copy_block)(uint8_t *, uint8_t *),
369     void (*xor_block)(uint8_t *, uint8_t *))
370 {
371 	size_t remainder = length;
372 	size_t need;
373 	uint8_t *datap = (uint8_t *)data;
374 	uint8_t *blockp;
375 	uint8_t *cbp;
376 	uint64_t counter;
377 	size_t pt_len, total_decrypted_len, mac_len, pm_len, pd_len;
378 	uint8_t *resultp;
379 
380 
381 	pm_len = ctx->ccm_processed_mac_len;
382 
383 	if (pm_len > 0) {
384 		uint8_t *tmp;
385 		/*
386 		 * all ciphertext has been processed, just waiting for
387 		 * part of the value of the mac
388 		 */
389 		if ((pm_len + length) > ctx->ccm_mac_len) {
390 			return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
391 		}
392 		tmp = (uint8_t *)ctx->ccm_mac_input_buf;
393 
394 		bcopy(datap, tmp + pm_len, length);
395 
396 		ctx->ccm_processed_mac_len += length;
397 		return (CRYPTO_SUCCESS);
398 	}
399 
400 	/*
401 	 * If we decrypt the given data, what total amount of data would
402 	 * have been decrypted?
403 	 */
404 	pd_len = ctx->ccm_processed_data_len;
405 	total_decrypted_len = pd_len + length + ctx->ccm_remainder_len;
406 
407 	if (total_decrypted_len >
408 	    (ctx->ccm_data_len + ctx->ccm_mac_len)) {
409 		return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
410 	}
411 
412 	pt_len = ctx->ccm_data_len;
413 
414 	if (total_decrypted_len > pt_len) {
415 		/*
416 		 * part of the input will be the MAC, need to isolate that
417 		 * to be dealt with later.  The left-over data in
418 		 * ccm_remainder_len from last time will not be part of the
419 		 * MAC.  Otherwise, it would have already been taken out
420 		 * when this call is made last time.
421 		 */
422 		size_t pt_part = pt_len - pd_len - ctx->ccm_remainder_len;
423 
424 		mac_len = length - pt_part;
425 
426 		ctx->ccm_processed_mac_len = mac_len;
427 		bcopy(data + pt_part, ctx->ccm_mac_input_buf, mac_len);
428 
429 		if (pt_part + ctx->ccm_remainder_len < block_size) {
430 			/*
431 			 * since this is last of the ciphertext, will
432 			 * just decrypt with it here
433 			 */
434 			bcopy(datap, &((uint8_t *)ctx->ccm_remainder)
435 			    [ctx->ccm_remainder_len], pt_part);
436 			ctx->ccm_remainder_len += pt_part;
437 			ccm_decrypt_incomplete_block(ctx, encrypt_block);
438 			ctx->ccm_remainder_len = 0;
439 			ctx->ccm_processed_data_len += pt_part;
440 			return (CRYPTO_SUCCESS);
441 		} else {
442 			/* let rest of the code handle this */
443 			length = pt_part;
444 		}
445 	} else if (length + ctx->ccm_remainder_len < block_size) {
446 			/* accumulate bytes here and return */
447 		bcopy(datap,
448 		    (uint8_t *)ctx->ccm_remainder + ctx->ccm_remainder_len,
449 		    length);
450 		ctx->ccm_remainder_len += length;
451 		ctx->ccm_copy_to = datap;
452 		return (CRYPTO_SUCCESS);
453 	}
454 
455 	do {
456 		/* Unprocessed data from last call. */
457 		if (ctx->ccm_remainder_len > 0) {
458 			need = block_size - ctx->ccm_remainder_len;
459 
460 			if (need > remainder)
461 				return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
462 
463 			bcopy(datap, &((uint8_t *)ctx->ccm_remainder)
464 			    [ctx->ccm_remainder_len], need);
465 
466 			blockp = (uint8_t *)ctx->ccm_remainder;
467 		} else {
468 			blockp = datap;
469 		}
470 
471 		/* Calculate the counter mode, ccm_cb is the counter block */
472 		cbp = (uint8_t *)ctx->ccm_tmp;
473 		encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, cbp);
474 
475 		/*
476 		 * Increment counter.
477 		 * Counter bits are confined to the bottom 64 bits
478 		 */
479 #ifdef _LITTLE_ENDIAN
480 		counter = ntohll(ctx->ccm_cb[1] & ctx->ccm_counter_mask);
481 		counter = htonll(counter + 1);
482 #else
483 		counter = ctx->ccm_cb[1] & ctx->ccm_counter_mask;
484 		counter++;
485 #endif	/* _LITTLE_ENDIAN */
486 		counter &= ctx->ccm_counter_mask;
487 		ctx->ccm_cb[1] =
488 		    (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter;
489 
490 		/* XOR with the ciphertext */
491 		xor_block(blockp, cbp);
492 
493 		/* Copy the plaintext to the "holding buffer" */
494 		resultp = (uint8_t *)ctx->ccm_pt_buf +
495 		    ctx->ccm_processed_data_len;
496 		copy_block(cbp, resultp);
497 
498 		ctx->ccm_processed_data_len += block_size;
499 
500 		ctx->ccm_lastp = blockp;
501 
502 		/* Update pointer to next block of data to be processed. */
503 		if (ctx->ccm_remainder_len != 0) {
504 			datap += need;
505 			ctx->ccm_remainder_len = 0;
506 		} else {
507 			datap += block_size;
508 		}
509 
510 		remainder = (size_t)&data[length] - (size_t)datap;
511 
512 		/* Incomplete last block */
513 		if (remainder > 0 && remainder < block_size) {
514 			bcopy(datap, ctx->ccm_remainder, remainder);
515 			ctx->ccm_remainder_len = remainder;
516 			ctx->ccm_copy_to = datap;
517 			if (ctx->ccm_processed_mac_len > 0) {
518 				/*
519 				 * not expecting anymore ciphertext, just
520 				 * compute plaintext for the remaining input
521 				 */
522 				ccm_decrypt_incomplete_block(ctx,
523 				    encrypt_block);
524 				ctx->ccm_processed_data_len += remainder;
525 				ctx->ccm_remainder_len = 0;
526 			}
527 			goto out;
528 		}
529 		ctx->ccm_copy_to = NULL;
530 
531 	} while (remainder > 0);
532 
533 out:
534 	return (CRYPTO_SUCCESS);
535 }
536 
537 int
538 ccm_decrypt_final(ccm_ctx_t *ctx, crypto_data_t *out, size_t block_size,
539     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
540     void (*copy_block)(uint8_t *, uint8_t *),
541     void (*xor_block)(uint8_t *, uint8_t *))
542 {
543 	size_t mac_remain, pt_len;
544 	uint8_t *pt, *mac_buf, *macp, *ccm_mac_p;
545 	int rv;
546 
547 	pt_len = ctx->ccm_data_len;
548 
549 	/* Make sure output buffer can fit all of the plaintext */
550 	if (out->cd_length < pt_len) {
551 		return (CRYPTO_DATA_LEN_RANGE);
552 	}
553 
554 	pt = ctx->ccm_pt_buf;
555 	mac_remain = ctx->ccm_processed_data_len;
556 	mac_buf = (uint8_t *)ctx->ccm_mac_buf;
557 
558 	macp = (uint8_t *)ctx->ccm_tmp;
559 
560 	while (mac_remain > 0) {
561 
562 		if (mac_remain < block_size) {
563 			bzero(macp, block_size);
564 			bcopy(pt, macp, mac_remain);
565 			mac_remain = 0;
566 		} else {
567 			copy_block(pt, macp);
568 			mac_remain -= block_size;
569 			pt += block_size;
570 		}
571 
572 		/* calculate the CBC MAC */
573 		xor_block(macp, mac_buf);
574 		encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
575 	}
576 
577 	/* Calculate the CCM MAC */
578 	ccm_mac_p = (uint8_t *)ctx->ccm_tmp;
579 	calculate_ccm_mac((ccm_ctx_t *)ctx, ccm_mac_p, encrypt_block);
580 
581 	/* compare the input CCM MAC value with what we calculated */
582 	if (bcmp(ctx->ccm_mac_input_buf, ccm_mac_p, ctx->ccm_mac_len)) {
583 		/* They don't match */
584 		return (CRYPTO_INVALID_MAC);
585 	} else {
586 		rv = crypto_put_output_data(ctx->ccm_pt_buf, out, pt_len);
587 		if (rv != CRYPTO_SUCCESS)
588 			return (rv);
589 		out->cd_offset += pt_len;
590 	}
591 	return (CRYPTO_SUCCESS);
592 }
593 
594 int
595 ccm_validate_args(CK_AES_CCM_PARAMS *ccm_param, boolean_t is_encrypt_init)
596 {
597 	size_t macSize, nonceSize;
598 	uint8_t q;
599 	uint64_t maxValue;
600 
601 	/*
602 	 * Check the length of the MAC.  The only valid
603 	 * lengths for the MAC are: 4, 6, 8, 10, 12, 14, 16
604 	 */
605 	macSize = ccm_param->ulMACSize;
606 	if ((macSize < 4) || (macSize > 16) || ((macSize % 2) != 0)) {
607 		return (CRYPTO_MECHANISM_PARAM_INVALID);
608 	}
609 
610 	/* Check the nonce length.  Valid values are 7, 8, 9, 10, 11, 12, 13 */
611 	nonceSize = ccm_param->ulNonceSize;
612 	if ((nonceSize < 7) || (nonceSize > 13)) {
613 		return (CRYPTO_MECHANISM_PARAM_INVALID);
614 	}
615 
616 	/* q is the length of the field storing the length, in bytes */
617 	q = (uint8_t)((15 - nonceSize) & 0xFF);
618 
619 
620 	/*
621 	 * If it is decrypt, need to make sure size of ciphertext is at least
622 	 * bigger than MAC len
623 	 */
624 	if ((!is_encrypt_init) && (ccm_param->ulDataSize < macSize)) {
625 		return (CRYPTO_MECHANISM_PARAM_INVALID);
626 	}
627 
628 	/*
629 	 * Check to make sure the length of the payload is within the
630 	 * range of values allowed by q
631 	 */
632 	if (q < 8) {
633 		maxValue = (1ULL << (q * 8)) - 1;
634 	} else {
635 		maxValue = ULONG_MAX;
636 	}
637 
638 	if (ccm_param->ulDataSize > maxValue) {
639 		return (CRYPTO_MECHANISM_PARAM_INVALID);
640 	}
641 	return (CRYPTO_SUCCESS);
642 }
643 
644 /*
645  * Format the first block used in CBC-MAC (B0) and the initial counter
646  * block based on formatting functions and counter generation functions
647  * specified in RFC 3610 and NIST publication 800-38C, appendix A
648  *
649  * b0 is the first block used in CBC-MAC
650  * cb0 is the first counter block
651  *
652  * It's assumed that the arguments b0 and cb0 are preallocated AES blocks
653  *
654  */
655 static void
656 ccm_format_initial_blocks(uchar_t *nonce, ulong_t nonceSize,
657     ulong_t authDataSize, uint8_t *b0, ccm_ctx_t *aes_ctx)
658 {
659 	uint64_t payloadSize;
660 	uint8_t t, q, have_adata = 0;
661 	size_t limit;
662 	int i, j, k;
663 	uint64_t mask = 0;
664 	uint8_t *cb;
665 
666 	q = (uint8_t)((15 - nonceSize) & 0xFF);
667 	t = (uint8_t)((aes_ctx->ccm_mac_len) & 0xFF);
668 
669 	/* Construct the first octet of b0 */
670 	if (authDataSize > 0) {
671 		have_adata = 1;
672 	}
673 	b0[0] = (have_adata << 6) | (((t - 2)  / 2) << 3) | (q - 1);
674 
675 	/* copy the nonce value into b0 */
676 	bcopy(nonce, &(b0[1]), nonceSize);
677 
678 	/* store the length of the payload into b0 */
679 	bzero(&(b0[1+nonceSize]), q);
680 
681 	payloadSize = aes_ctx->ccm_data_len;
682 	limit = 8 < q ? 8 : q;
683 
684 	for (i = 0, j = 0, k = 15; i < limit; i++, j += 8, k--) {
685 		b0[k] = (uint8_t)((payloadSize >> j) & 0xFF);
686 	}
687 
688 	/* format the counter block */
689 
690 	cb = (uint8_t *)aes_ctx->ccm_cb;
691 
692 	cb[0] = 0x07 & (q-1); /* first byte */
693 
694 	/* copy the nonce value into the counter block */
695 	bcopy(nonce, &(cb[1]), nonceSize);
696 
697 	bzero(&(cb[1+nonceSize]), q);
698 
699 	/* Create the mask for the counter field based on the size of nonce */
700 	q <<= 3;
701 	while (q-- > 0) {
702 		mask |= (1ULL << q);
703 	}
704 
705 #ifdef _LITTLE_ENDIAN
706 	mask = htonll(mask);
707 #endif
708 	aes_ctx->ccm_counter_mask = mask;
709 
710 	/*
711 	 * During calculation, we start using counter block 1, we will
712 	 * set it up right here.
713 	 * We can just set the last byte to have the value 1, because
714 	 * even with the biggest nonce of 13, the last byte of the
715 	 * counter block will be used for the counter value.
716 	 */
717 	cb[15] = 0x01;
718 }
719 
720 /*
721  * Encode the length of the associated data as
722  * specified in RFC 3610 and NIST publication 800-38C, appendix A
723  */
724 static void
725 encode_adata_len(ulong_t auth_data_len, uint8_t *encoded, size_t *encoded_len)
726 {
727 #ifdef UNALIGNED_POINTERS_PERMITTED
728 	uint32_t	*lencoded_ptr;
729 #ifdef _LP64
730 	uint64_t	*llencoded_ptr;
731 #endif
732 #endif	/* UNALIGNED_POINTERS_PERMITTED */
733 
734 	if (auth_data_len < ((1ULL<<16) - (1ULL<<8))) {
735 		/* 0 < a < (2^16-2^8) */
736 		*encoded_len = 2;
737 		encoded[0] = (auth_data_len & 0xff00) >> 8;
738 		encoded[1] = auth_data_len & 0xff;
739 
740 	} else if ((auth_data_len >= ((1ULL<<16) - (1ULL<<8))) &&
741 	    (auth_data_len < (1ULL << 31))) {
742 		/* (2^16-2^8) <= a < 2^32 */
743 		*encoded_len = 6;
744 		encoded[0] = 0xff;
745 		encoded[1] = 0xfe;
746 #ifdef UNALIGNED_POINTERS_PERMITTED
747 		lencoded_ptr = (uint32_t *)&encoded[2];
748 		*lencoded_ptr = htonl(auth_data_len);
749 #else
750 		encoded[2] = (auth_data_len & 0xff000000) >> 24;
751 		encoded[3] = (auth_data_len & 0xff0000) >> 16;
752 		encoded[4] = (auth_data_len & 0xff00) >> 8;
753 		encoded[5] = auth_data_len & 0xff;
754 #endif	/* UNALIGNED_POINTERS_PERMITTED */
755 
756 #ifdef _LP64
757 	} else {
758 		/* 2^32 <= a < 2^64 */
759 		*encoded_len = 10;
760 		encoded[0] = 0xff;
761 		encoded[1] = 0xff;
762 #ifdef UNALIGNED_POINTERS_PERMITTED
763 		llencoded_ptr = (uint64_t *)&encoded[2];
764 		*llencoded_ptr = htonl(auth_data_len);
765 #else
766 		encoded[2] = (auth_data_len & 0xff00000000000000) >> 56;
767 		encoded[3] = (auth_data_len & 0xff000000000000) >> 48;
768 		encoded[4] = (auth_data_len & 0xff0000000000) >> 40;
769 		encoded[5] = (auth_data_len & 0xff00000000) >> 32;
770 		encoded[6] = (auth_data_len & 0xff000000) >> 24;
771 		encoded[7] = (auth_data_len & 0xff0000) >> 16;
772 		encoded[8] = (auth_data_len & 0xff00) >> 8;
773 		encoded[9] = auth_data_len & 0xff;
774 #endif	/* UNALIGNED_POINTERS_PERMITTED */
775 #endif	/* _LP64 */
776 	}
777 }
778 
779 /*
780  * The following function should be call at encrypt or decrypt init time
781  * for AES CCM mode.
782  */
783 int
784 ccm_init(ccm_ctx_t *ctx, unsigned char *nonce, size_t nonce_len,
785     unsigned char *auth_data, size_t auth_data_len, size_t block_size,
786     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
787     void (*xor_block)(uint8_t *, uint8_t *))
788 {
789 	uint8_t *mac_buf, *datap, *ivp, *authp;
790 	size_t remainder, processed;
791 	uint8_t encoded_a[10]; /* max encoded auth data length is 10 octets */
792 	size_t encoded_a_len = 0;
793 
794 	mac_buf = (uint8_t *)&(ctx->ccm_mac_buf);
795 
796 	/*
797 	 * Format the 1st block for CBC-MAC and construct the
798 	 * 1st counter block.
799 	 *
800 	 * aes_ctx->ccm_iv is used for storing the counter block
801 	 * mac_buf will store b0 at this time.
802 	 */
803 	ccm_format_initial_blocks(nonce, nonce_len,
804 	    auth_data_len, mac_buf, ctx);
805 
806 	/* The IV for CBC MAC for AES CCM mode is always zero */
807 	ivp = (uint8_t *)ctx->ccm_tmp;
808 	bzero(ivp, block_size);
809 
810 	xor_block(ivp, mac_buf);
811 
812 	/* encrypt the nonce */
813 	encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
814 
815 	/* take care of the associated data, if any */
816 	if (auth_data_len == 0) {
817 		return (CRYPTO_SUCCESS);
818 	}
819 
820 	encode_adata_len(auth_data_len, encoded_a, &encoded_a_len);
821 
822 	remainder = auth_data_len;
823 
824 	/* 1st block: it contains encoded associated data, and some data */
825 	authp = (uint8_t *)ctx->ccm_tmp;
826 	bzero(authp, block_size);
827 	bcopy(encoded_a, authp, encoded_a_len);
828 	processed = block_size - encoded_a_len;
829 	if (processed > auth_data_len) {
830 		/* in case auth_data is very small */
831 		processed = auth_data_len;
832 	}
833 	bcopy(auth_data, authp+encoded_a_len, processed);
834 	/* xor with previous buffer */
835 	xor_block(authp, mac_buf);
836 	encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
837 	remainder -= processed;
838 	if (remainder == 0) {
839 		/* a small amount of associated data, it's all done now */
840 		return (CRYPTO_SUCCESS);
841 	}
842 
843 	do {
844 		if (remainder < block_size) {
845 			/*
846 			 * There's not a block full of data, pad rest of
847 			 * buffer with zero
848 			 */
849 			bzero(authp, block_size);
850 			bcopy(&(auth_data[processed]), authp, remainder);
851 			datap = (uint8_t *)authp;
852 			remainder = 0;
853 		} else {
854 			datap = (uint8_t *)(&(auth_data[processed]));
855 			processed += block_size;
856 			remainder -= block_size;
857 		}
858 
859 		xor_block(datap, mac_buf);
860 		encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
861 
862 	} while (remainder > 0);
863 
864 	return (CRYPTO_SUCCESS);
865 }
866 
867 int
868 ccm_init_ctx(ccm_ctx_t *ccm_ctx, char *param, int kmflag,
869     boolean_t is_encrypt_init, size_t block_size,
870     int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
871     void (*xor_block)(uint8_t *, uint8_t *))
872 {
873 	int rv;
874 	CK_AES_CCM_PARAMS *ccm_param;
875 
876 	if (param != NULL) {
877 		ccm_param = (CK_AES_CCM_PARAMS *)param;
878 
879 		if ((rv = ccm_validate_args(ccm_param,
880 		    is_encrypt_init)) != 0) {
881 			return (rv);
882 		}
883 
884 		ccm_ctx->ccm_mac_len = ccm_param->ulMACSize;
885 		if (is_encrypt_init) {
886 			ccm_ctx->ccm_data_len = ccm_param->ulDataSize;
887 		} else {
888 			ccm_ctx->ccm_data_len =
889 			    ccm_param->ulDataSize - ccm_ctx->ccm_mac_len;
890 			ccm_ctx->ccm_processed_mac_len = 0;
891 		}
892 		ccm_ctx->ccm_processed_data_len = 0;
893 
894 		ccm_ctx->ccm_flags |= CCM_MODE;
895 	} else {
896 		rv = CRYPTO_MECHANISM_PARAM_INVALID;
897 		goto out;
898 	}
899 
900 	if (ccm_init(ccm_ctx, ccm_param->nonce, ccm_param->ulNonceSize,
901 	    ccm_param->authData, ccm_param->ulAuthDataSize, block_size,
902 	    encrypt_block, xor_block) != 0) {
903 		rv = CRYPTO_MECHANISM_PARAM_INVALID;
904 		goto out;
905 	}
906 	if (!is_encrypt_init) {
907 		/* allocate buffer for storing decrypted plaintext */
908 #ifdef _KERNEL
909 		ccm_ctx->ccm_pt_buf = kmem_alloc(ccm_ctx->ccm_data_len,
910 		    kmflag);
911 #else
912 		ccm_ctx->ccm_pt_buf = malloc(ccm_ctx->ccm_data_len);
913 #endif
914 		if (ccm_ctx->ccm_pt_buf == NULL) {
915 			rv = CRYPTO_HOST_MEMORY;
916 		}
917 	}
918 out:
919 	return (rv);
920 }
921 
922 void *
923 ccm_alloc_ctx(int kmflag)
924 {
925 	ccm_ctx_t *ccm_ctx;
926 
927 #ifdef _KERNEL
928 	if ((ccm_ctx = kmem_zalloc(sizeof (ccm_ctx_t), kmflag)) == NULL)
929 #else
930 	if ((ccm_ctx = calloc(1, sizeof (ccm_ctx_t))) == NULL)
931 #endif
932 		return (NULL);
933 
934 	ccm_ctx->ccm_flags = CCM_MODE;
935 	return (ccm_ctx);
936 }
937