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.
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.
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]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #include <sys/zfs_context.h>
27 #include <modes/modes.h>
28 #include <sys/crypto/common.h>
29 #include <sys/crypto/impl.h>
31 #ifdef HAVE_EFFICIENT_UNALIGNED_ACCESS
32 #include <sys/byteorder.h>
33 #define UNALIGNED_POINTERS_PERMITTED
37 * Encrypt multiple blocks of data in CCM mode. Decrypt for CCM mode
38 * is done in another function.
41 ccm_mode_encrypt_contiguous_blocks(ccm_ctx_t *ctx, char *data, size_t length,
42 crypto_data_t *out, size_t block_size,
43 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
44 void (*copy_block)(uint8_t *, uint8_t *),
45 void (*xor_block)(uint8_t *, uint8_t *))
47 size_t remainder = length;
49 uint8_t *datap = (uint8_t *)data;
56 size_t out_data_1_len;
60 if (length + ctx->ccm_remainder_len < block_size) {
61 /* accumulate bytes here and return */
63 (uint8_t *)ctx->ccm_remainder + ctx->ccm_remainder_len,
65 ctx->ccm_remainder_len += length;
66 ctx->ccm_copy_to = datap;
67 return (CRYPTO_SUCCESS);
70 lastp = (uint8_t *)ctx->ccm_cb;
71 crypto_init_ptrs(out, &iov_or_mp, &offset);
73 mac_buf = (uint8_t *)ctx->ccm_mac_buf;
76 /* Unprocessed data from last call. */
77 if (ctx->ccm_remainder_len > 0) {
78 need = block_size - ctx->ccm_remainder_len;
81 return (CRYPTO_DATA_LEN_RANGE);
83 bcopy(datap, &((uint8_t *)ctx->ccm_remainder)
84 [ctx->ccm_remainder_len], need);
86 blockp = (uint8_t *)ctx->ccm_remainder;
94 * XOR the previous cipher block current clear block.
95 * mac_buf always contain previous cipher block.
97 xor_block(blockp, mac_buf);
98 encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
100 /* ccm_cb is the counter block */
101 encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb,
102 (uint8_t *)ctx->ccm_tmp);
104 lastp = (uint8_t *)ctx->ccm_tmp;
107 * Increment counter. Counter bits are confined
108 * to the bottom 64 bits of the counter block.
110 #ifdef _ZFS_LITTLE_ENDIAN
111 counter = ntohll(ctx->ccm_cb[1] & ctx->ccm_counter_mask);
112 counter = htonll(counter + 1);
114 counter = ctx->ccm_cb[1] & ctx->ccm_counter_mask;
116 #endif /* _ZFS_LITTLE_ENDIAN */
117 counter &= ctx->ccm_counter_mask;
119 (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter;
122 * XOR encrypted counter block with the current clear block.
124 xor_block(blockp, lastp);
126 ctx->ccm_processed_data_len += block_size;
128 crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1,
129 &out_data_1_len, &out_data_2, block_size);
131 /* copy block to where it belongs */
132 if (out_data_1_len == block_size) {
133 copy_block(lastp, out_data_1);
135 bcopy(lastp, out_data_1, out_data_1_len);
136 if (out_data_2 != NULL) {
137 bcopy(lastp + out_data_1_len,
139 block_size - out_data_1_len);
143 out->cd_offset += block_size;
145 /* Update pointer to next block of data to be processed. */
146 if (ctx->ccm_remainder_len != 0) {
148 ctx->ccm_remainder_len = 0;
153 remainder = (size_t)&data[length] - (size_t)datap;
155 /* Incomplete last block. */
156 if (remainder > 0 && remainder < block_size) {
157 bcopy(datap, ctx->ccm_remainder, remainder);
158 ctx->ccm_remainder_len = remainder;
159 ctx->ccm_copy_to = datap;
162 ctx->ccm_copy_to = NULL;
164 } while (remainder > 0);
167 return (CRYPTO_SUCCESS);
171 calculate_ccm_mac(ccm_ctx_t *ctx, uint8_t *ccm_mac,
172 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *))
175 uint8_t *counterp, *mac_buf;
178 mac_buf = (uint8_t *)ctx->ccm_mac_buf;
180 /* first counter block start with index 0 */
182 ctx->ccm_cb[1] = (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter;
184 counterp = (uint8_t *)ctx->ccm_tmp;
185 encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, counterp);
187 /* calculate XOR of MAC with first counter block */
188 for (i = 0; i < ctx->ccm_mac_len; i++) {
189 ccm_mac[i] = mac_buf[i] ^ counterp[i];
195 ccm_encrypt_final(ccm_ctx_t *ctx, crypto_data_t *out, size_t block_size,
196 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
197 void (*xor_block)(uint8_t *, uint8_t *))
199 uint8_t *lastp, *mac_buf, *ccm_mac_p, *macp = NULL;
204 size_t out_data_1_len;
207 if (out->cd_length < (ctx->ccm_remainder_len + ctx->ccm_mac_len)) {
208 return (CRYPTO_DATA_LEN_RANGE);
212 * When we get here, the number of bytes of payload processed
213 * plus whatever data remains, if any,
214 * should be the same as the number of bytes that's being
215 * passed in the argument during init time.
217 if ((ctx->ccm_processed_data_len + ctx->ccm_remainder_len)
218 != (ctx->ccm_data_len)) {
219 return (CRYPTO_DATA_LEN_RANGE);
222 mac_buf = (uint8_t *)ctx->ccm_mac_buf;
224 if (ctx->ccm_remainder_len > 0) {
226 /* ccm_mac_input_buf is not used for encryption */
227 macp = (uint8_t *)ctx->ccm_mac_input_buf;
228 bzero(macp, block_size);
230 /* copy remainder to temporary buffer */
231 bcopy(ctx->ccm_remainder, macp, ctx->ccm_remainder_len);
233 /* calculate the CBC MAC */
234 xor_block(macp, mac_buf);
235 encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
237 /* calculate the counter mode */
238 lastp = (uint8_t *)ctx->ccm_tmp;
239 encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, lastp);
241 /* XOR with counter block */
242 for (i = 0; i < ctx->ccm_remainder_len; i++) {
245 ctx->ccm_processed_data_len += ctx->ccm_remainder_len;
248 /* Calculate the CCM MAC */
249 ccm_mac_p = (uint8_t *)ctx->ccm_tmp;
250 calculate_ccm_mac(ctx, ccm_mac_p, encrypt_block);
252 crypto_init_ptrs(out, &iov_or_mp, &offset);
253 crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1,
254 &out_data_1_len, &out_data_2,
255 ctx->ccm_remainder_len + ctx->ccm_mac_len);
257 if (ctx->ccm_remainder_len > 0) {
259 /* copy temporary block to where it belongs */
260 if (out_data_2 == NULL) {
261 /* everything will fit in out_data_1 */
262 bcopy(macp, out_data_1, ctx->ccm_remainder_len);
263 bcopy(ccm_mac_p, out_data_1 + ctx->ccm_remainder_len,
267 if (out_data_1_len < ctx->ccm_remainder_len) {
269 size_t data_2_len_used;
271 bcopy(macp, out_data_1, out_data_1_len);
273 data_2_len_used = ctx->ccm_remainder_len
276 bcopy((uint8_t *)macp + out_data_1_len,
277 out_data_2, data_2_len_used);
278 bcopy(ccm_mac_p, out_data_2 + data_2_len_used,
281 bcopy(macp, out_data_1, out_data_1_len);
282 if (out_data_1_len == ctx->ccm_remainder_len) {
283 /* mac will be in out_data_2 */
284 bcopy(ccm_mac_p, out_data_2,
287 size_t len_not_used = out_data_1_len -
288 ctx->ccm_remainder_len;
290 * part of mac in will be in
291 * out_data_1, part of the mac will be
295 out_data_1 + ctx->ccm_remainder_len,
297 bcopy(ccm_mac_p + len_not_used,
299 ctx->ccm_mac_len - len_not_used);
305 /* copy block to where it belongs */
306 bcopy(ccm_mac_p, out_data_1, out_data_1_len);
307 if (out_data_2 != NULL) {
308 bcopy(ccm_mac_p + out_data_1_len, out_data_2,
309 block_size - out_data_1_len);
312 out->cd_offset += ctx->ccm_remainder_len + ctx->ccm_mac_len;
313 ctx->ccm_remainder_len = 0;
314 return (CRYPTO_SUCCESS);
318 * This will only deal with decrypting the last block of the input that
319 * might not be a multiple of block length.
322 ccm_decrypt_incomplete_block(ccm_ctx_t *ctx,
323 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *))
325 uint8_t *datap, *outp, *counterp;
328 datap = (uint8_t *)ctx->ccm_remainder;
329 outp = &((ctx->ccm_pt_buf)[ctx->ccm_processed_data_len]);
331 counterp = (uint8_t *)ctx->ccm_tmp;
332 encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, counterp);
334 /* XOR with counter block */
335 for (i = 0; i < ctx->ccm_remainder_len; i++) {
336 outp[i] = datap[i] ^ counterp[i];
341 * This will decrypt the cipher text. However, the plaintext won't be
342 * returned to the caller. It will be returned when decrypt_final() is
343 * called if the MAC matches
347 ccm_mode_decrypt_contiguous_blocks(ccm_ctx_t *ctx, char *data, size_t length,
348 crypto_data_t *out, size_t block_size,
349 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
350 void (*copy_block)(uint8_t *, uint8_t *),
351 void (*xor_block)(uint8_t *, uint8_t *))
353 size_t remainder = length;
355 uint8_t *datap = (uint8_t *)data;
359 size_t pt_len, total_decrypted_len, mac_len, pm_len, pd_len;
363 pm_len = ctx->ccm_processed_mac_len;
368 * all ciphertext has been processed, just waiting for
369 * part of the value of the mac
371 if ((pm_len + length) > ctx->ccm_mac_len) {
372 return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
374 tmp = (uint8_t *)ctx->ccm_mac_input_buf;
376 bcopy(datap, tmp + pm_len, length);
378 ctx->ccm_processed_mac_len += length;
379 return (CRYPTO_SUCCESS);
383 * If we decrypt the given data, what total amount of data would
384 * have been decrypted?
386 pd_len = ctx->ccm_processed_data_len;
387 total_decrypted_len = pd_len + length + ctx->ccm_remainder_len;
389 if (total_decrypted_len >
390 (ctx->ccm_data_len + ctx->ccm_mac_len)) {
391 return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
394 pt_len = ctx->ccm_data_len;
396 if (total_decrypted_len > pt_len) {
398 * part of the input will be the MAC, need to isolate that
399 * to be dealt with later. The left-over data in
400 * ccm_remainder_len from last time will not be part of the
401 * MAC. Otherwise, it would have already been taken out
402 * when this call is made last time.
404 size_t pt_part = pt_len - pd_len - ctx->ccm_remainder_len;
406 mac_len = length - pt_part;
408 ctx->ccm_processed_mac_len = mac_len;
409 bcopy(data + pt_part, ctx->ccm_mac_input_buf, mac_len);
411 if (pt_part + ctx->ccm_remainder_len < block_size) {
413 * since this is last of the ciphertext, will
414 * just decrypt with it here
416 bcopy(datap, &((uint8_t *)ctx->ccm_remainder)
417 [ctx->ccm_remainder_len], pt_part);
418 ctx->ccm_remainder_len += pt_part;
419 ccm_decrypt_incomplete_block(ctx, encrypt_block);
420 ctx->ccm_processed_data_len += ctx->ccm_remainder_len;
421 ctx->ccm_remainder_len = 0;
422 return (CRYPTO_SUCCESS);
424 /* let rest of the code handle this */
427 } else if (length + ctx->ccm_remainder_len < block_size) {
428 /* accumulate bytes here and return */
430 (uint8_t *)ctx->ccm_remainder + ctx->ccm_remainder_len,
432 ctx->ccm_remainder_len += length;
433 ctx->ccm_copy_to = datap;
434 return (CRYPTO_SUCCESS);
438 /* Unprocessed data from last call. */
439 if (ctx->ccm_remainder_len > 0) {
440 need = block_size - ctx->ccm_remainder_len;
442 if (need > remainder)
443 return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE);
445 bcopy(datap, &((uint8_t *)ctx->ccm_remainder)
446 [ctx->ccm_remainder_len], need);
448 blockp = (uint8_t *)ctx->ccm_remainder;
453 /* Calculate the counter mode, ccm_cb is the counter block */
454 cbp = (uint8_t *)ctx->ccm_tmp;
455 encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, cbp);
459 * Counter bits are confined to the bottom 64 bits
461 #ifdef _ZFS_LITTLE_ENDIAN
462 counter = ntohll(ctx->ccm_cb[1] & ctx->ccm_counter_mask);
463 counter = htonll(counter + 1);
465 counter = ctx->ccm_cb[1] & ctx->ccm_counter_mask;
467 #endif /* _ZFS_LITTLE_ENDIAN */
468 counter &= ctx->ccm_counter_mask;
470 (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter;
472 /* XOR with the ciphertext */
473 xor_block(blockp, cbp);
475 /* Copy the plaintext to the "holding buffer" */
476 resultp = (uint8_t *)ctx->ccm_pt_buf +
477 ctx->ccm_processed_data_len;
478 copy_block(cbp, resultp);
480 ctx->ccm_processed_data_len += block_size;
482 ctx->ccm_lastp = blockp;
484 /* Update pointer to next block of data to be processed. */
485 if (ctx->ccm_remainder_len != 0) {
487 ctx->ccm_remainder_len = 0;
492 remainder = (size_t)&data[length] - (size_t)datap;
494 /* Incomplete last block */
495 if (remainder > 0 && remainder < block_size) {
496 bcopy(datap, ctx->ccm_remainder, remainder);
497 ctx->ccm_remainder_len = remainder;
498 ctx->ccm_copy_to = datap;
499 if (ctx->ccm_processed_mac_len > 0) {
501 * not expecting anymore ciphertext, just
502 * compute plaintext for the remaining input
504 ccm_decrypt_incomplete_block(ctx,
506 ctx->ccm_processed_data_len += remainder;
507 ctx->ccm_remainder_len = 0;
511 ctx->ccm_copy_to = NULL;
513 } while (remainder > 0);
516 return (CRYPTO_SUCCESS);
520 ccm_decrypt_final(ccm_ctx_t *ctx, crypto_data_t *out, size_t block_size,
521 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
522 void (*copy_block)(uint8_t *, uint8_t *),
523 void (*xor_block)(uint8_t *, uint8_t *))
525 size_t mac_remain, pt_len;
526 uint8_t *pt, *mac_buf, *macp, *ccm_mac_p;
529 pt_len = ctx->ccm_data_len;
531 /* Make sure output buffer can fit all of the plaintext */
532 if (out->cd_length < pt_len) {
533 return (CRYPTO_DATA_LEN_RANGE);
536 pt = ctx->ccm_pt_buf;
537 mac_remain = ctx->ccm_processed_data_len;
538 mac_buf = (uint8_t *)ctx->ccm_mac_buf;
540 macp = (uint8_t *)ctx->ccm_tmp;
542 while (mac_remain > 0) {
544 if (mac_remain < block_size) {
545 bzero(macp, block_size);
546 bcopy(pt, macp, mac_remain);
549 copy_block(pt, macp);
550 mac_remain -= block_size;
554 /* calculate the CBC MAC */
555 xor_block(macp, mac_buf);
556 encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
559 /* Calculate the CCM MAC */
560 ccm_mac_p = (uint8_t *)ctx->ccm_tmp;
561 calculate_ccm_mac((ccm_ctx_t *)ctx, ccm_mac_p, encrypt_block);
563 /* compare the input CCM MAC value with what we calculated */
564 if (bcmp(ctx->ccm_mac_input_buf, ccm_mac_p, ctx->ccm_mac_len)) {
565 /* They don't match */
566 return (CRYPTO_INVALID_MAC);
568 rv = crypto_put_output_data(ctx->ccm_pt_buf, out, pt_len);
569 if (rv != CRYPTO_SUCCESS)
571 out->cd_offset += pt_len;
573 return (CRYPTO_SUCCESS);
577 ccm_validate_args(CK_AES_CCM_PARAMS *ccm_param, boolean_t is_encrypt_init)
579 size_t macSize, nonceSize;
584 * Check the length of the MAC. The only valid
585 * lengths for the MAC are: 4, 6, 8, 10, 12, 14, 16
587 macSize = ccm_param->ulMACSize;
588 if ((macSize < 4) || (macSize > 16) || ((macSize % 2) != 0)) {
589 return (CRYPTO_MECHANISM_PARAM_INVALID);
592 /* Check the nonce length. Valid values are 7, 8, 9, 10, 11, 12, 13 */
593 nonceSize = ccm_param->ulNonceSize;
594 if ((nonceSize < 7) || (nonceSize > 13)) {
595 return (CRYPTO_MECHANISM_PARAM_INVALID);
598 /* q is the length of the field storing the length, in bytes */
599 q = (uint8_t)((15 - nonceSize) & 0xFF);
603 * If it is decrypt, need to make sure size of ciphertext is at least
604 * bigger than MAC len
606 if ((!is_encrypt_init) && (ccm_param->ulDataSize < macSize)) {
607 return (CRYPTO_MECHANISM_PARAM_INVALID);
611 * Check to make sure the length of the payload is within the
612 * range of values allowed by q
615 maxValue = (1ULL << (q * 8)) - 1;
617 maxValue = ULONG_MAX;
620 if (ccm_param->ulDataSize > maxValue) {
621 return (CRYPTO_MECHANISM_PARAM_INVALID);
623 return (CRYPTO_SUCCESS);
627 * Format the first block used in CBC-MAC (B0) and the initial counter
628 * block based on formatting functions and counter generation functions
629 * specified in RFC 3610 and NIST publication 800-38C, appendix A
631 * b0 is the first block used in CBC-MAC
632 * cb0 is the first counter block
634 * It's assumed that the arguments b0 and cb0 are preallocated AES blocks
638 ccm_format_initial_blocks(uchar_t *nonce, ulong_t nonceSize,
639 ulong_t authDataSize, uint8_t *b0, ccm_ctx_t *aes_ctx)
641 uint64_t payloadSize;
642 uint8_t t, q, have_adata = 0;
648 q = (uint8_t)((15 - nonceSize) & 0xFF);
649 t = (uint8_t)((aes_ctx->ccm_mac_len) & 0xFF);
651 /* Construct the first octet of b0 */
652 if (authDataSize > 0) {
655 b0[0] = (have_adata << 6) | (((t - 2) / 2) << 3) | (q - 1);
657 /* copy the nonce value into b0 */
658 bcopy(nonce, &(b0[1]), nonceSize);
660 /* store the length of the payload into b0 */
661 bzero(&(b0[1+nonceSize]), q);
663 payloadSize = aes_ctx->ccm_data_len;
664 limit = 8 < q ? 8 : q;
666 for (i = 0, j = 0, k = 15; i < limit; i++, j += 8, k--) {
667 b0[k] = (uint8_t)((payloadSize >> j) & 0xFF);
670 /* format the counter block */
672 cb = (uint8_t *)aes_ctx->ccm_cb;
674 cb[0] = 0x07 & (q-1); /* first byte */
676 /* copy the nonce value into the counter block */
677 bcopy(nonce, &(cb[1]), nonceSize);
679 bzero(&(cb[1+nonceSize]), q);
681 /* Create the mask for the counter field based on the size of nonce */
687 #ifdef _ZFS_LITTLE_ENDIAN
690 aes_ctx->ccm_counter_mask = mask;
693 * During calculation, we start using counter block 1, we will
694 * set it up right here.
695 * We can just set the last byte to have the value 1, because
696 * even with the biggest nonce of 13, the last byte of the
697 * counter block will be used for the counter value.
703 * Encode the length of the associated data as
704 * specified in RFC 3610 and NIST publication 800-38C, appendix A
707 encode_adata_len(ulong_t auth_data_len, uint8_t *encoded, size_t *encoded_len)
709 #ifdef UNALIGNED_POINTERS_PERMITTED
710 uint32_t *lencoded_ptr;
712 uint64_t *llencoded_ptr;
714 #endif /* UNALIGNED_POINTERS_PERMITTED */
716 if (auth_data_len < ((1ULL<<16) - (1ULL<<8))) {
717 /* 0 < a < (2^16-2^8) */
719 encoded[0] = (auth_data_len & 0xff00) >> 8;
720 encoded[1] = auth_data_len & 0xff;
722 } else if ((auth_data_len >= ((1ULL<<16) - (1ULL<<8))) &&
723 (auth_data_len < (1ULL << 31))) {
724 /* (2^16-2^8) <= a < 2^32 */
728 #ifdef UNALIGNED_POINTERS_PERMITTED
729 lencoded_ptr = (uint32_t *)&encoded[2];
730 *lencoded_ptr = htonl(auth_data_len);
732 encoded[2] = (auth_data_len & 0xff000000) >> 24;
733 encoded[3] = (auth_data_len & 0xff0000) >> 16;
734 encoded[4] = (auth_data_len & 0xff00) >> 8;
735 encoded[5] = auth_data_len & 0xff;
736 #endif /* UNALIGNED_POINTERS_PERMITTED */
740 /* 2^32 <= a < 2^64 */
744 #ifdef UNALIGNED_POINTERS_PERMITTED
745 llencoded_ptr = (uint64_t *)&encoded[2];
746 *llencoded_ptr = htonl(auth_data_len);
748 encoded[2] = (auth_data_len & 0xff00000000000000) >> 56;
749 encoded[3] = (auth_data_len & 0xff000000000000) >> 48;
750 encoded[4] = (auth_data_len & 0xff0000000000) >> 40;
751 encoded[5] = (auth_data_len & 0xff00000000) >> 32;
752 encoded[6] = (auth_data_len & 0xff000000) >> 24;
753 encoded[7] = (auth_data_len & 0xff0000) >> 16;
754 encoded[8] = (auth_data_len & 0xff00) >> 8;
755 encoded[9] = auth_data_len & 0xff;
756 #endif /* UNALIGNED_POINTERS_PERMITTED */
762 ccm_init(ccm_ctx_t *ctx, unsigned char *nonce, size_t nonce_len,
763 unsigned char *auth_data, size_t auth_data_len, size_t block_size,
764 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
765 void (*xor_block)(uint8_t *, uint8_t *))
767 uint8_t *mac_buf, *datap, *ivp, *authp;
768 size_t remainder, processed;
769 uint8_t encoded_a[10]; /* max encoded auth data length is 10 octets */
770 size_t encoded_a_len = 0;
772 mac_buf = (uint8_t *)&(ctx->ccm_mac_buf);
775 * Format the 1st block for CBC-MAC and construct the
778 * aes_ctx->ccm_iv is used for storing the counter block
779 * mac_buf will store b0 at this time.
781 ccm_format_initial_blocks(nonce, nonce_len,
782 auth_data_len, mac_buf, ctx);
784 /* The IV for CBC MAC for AES CCM mode is always zero */
785 ivp = (uint8_t *)ctx->ccm_tmp;
786 bzero(ivp, block_size);
788 xor_block(ivp, mac_buf);
790 /* encrypt the nonce */
791 encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
793 /* take care of the associated data, if any */
794 if (auth_data_len == 0) {
795 return (CRYPTO_SUCCESS);
798 encode_adata_len(auth_data_len, encoded_a, &encoded_a_len);
800 remainder = auth_data_len;
802 /* 1st block: it contains encoded associated data, and some data */
803 authp = (uint8_t *)ctx->ccm_tmp;
804 bzero(authp, block_size);
805 bcopy(encoded_a, authp, encoded_a_len);
806 processed = block_size - encoded_a_len;
807 if (processed > auth_data_len) {
808 /* in case auth_data is very small */
809 processed = auth_data_len;
811 bcopy(auth_data, authp+encoded_a_len, processed);
812 /* xor with previous buffer */
813 xor_block(authp, mac_buf);
814 encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
815 remainder -= processed;
816 if (remainder == 0) {
817 /* a small amount of associated data, it's all done now */
818 return (CRYPTO_SUCCESS);
822 if (remainder < block_size) {
824 * There's not a block full of data, pad rest of
827 bzero(authp, block_size);
828 bcopy(&(auth_data[processed]), authp, remainder);
829 datap = (uint8_t *)authp;
832 datap = (uint8_t *)(&(auth_data[processed]));
833 processed += block_size;
834 remainder -= block_size;
837 xor_block(datap, mac_buf);
838 encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf);
840 } while (remainder > 0);
842 return (CRYPTO_SUCCESS);
846 * The following function should be call at encrypt or decrypt init time
850 ccm_init_ctx(ccm_ctx_t *ccm_ctx, char *param, int kmflag,
851 boolean_t is_encrypt_init, size_t block_size,
852 int (*encrypt_block)(const void *, const uint8_t *, uint8_t *),
853 void (*xor_block)(uint8_t *, uint8_t *))
856 CK_AES_CCM_PARAMS *ccm_param;
859 ccm_param = (CK_AES_CCM_PARAMS *)param;
861 if ((rv = ccm_validate_args(ccm_param,
862 is_encrypt_init)) != 0) {
866 ccm_ctx->ccm_mac_len = ccm_param->ulMACSize;
867 if (is_encrypt_init) {
868 ccm_ctx->ccm_data_len = ccm_param->ulDataSize;
870 ccm_ctx->ccm_data_len =
871 ccm_param->ulDataSize - ccm_ctx->ccm_mac_len;
872 ccm_ctx->ccm_processed_mac_len = 0;
874 ccm_ctx->ccm_processed_data_len = 0;
876 ccm_ctx->ccm_flags |= CCM_MODE;
878 return (CRYPTO_MECHANISM_PARAM_INVALID);
881 if (ccm_init(ccm_ctx, ccm_param->nonce, ccm_param->ulNonceSize,
882 ccm_param->authData, ccm_param->ulAuthDataSize, block_size,
883 encrypt_block, xor_block) != 0) {
884 return (CRYPTO_MECHANISM_PARAM_INVALID);
886 if (!is_encrypt_init) {
887 /* allocate buffer for storing decrypted plaintext */
888 ccm_ctx->ccm_pt_buf = vmem_alloc(ccm_ctx->ccm_data_len,
890 if (ccm_ctx->ccm_pt_buf == NULL) {
891 rv = CRYPTO_HOST_MEMORY;
898 ccm_alloc_ctx(int kmflag)
902 if ((ccm_ctx = kmem_zalloc(sizeof (ccm_ctx_t), kmflag)) == NULL)
905 ccm_ctx->ccm_flags = CCM_MODE;