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 (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
29 #include <security/cryptoki.h>
32 #include <sys/types.h>
34 #include <modes/modes.h>
35 #include <sys/crypto/common.h>
36 #include <sys/crypto/impl.h>
37 #include <sys/byteorder.h>
39 #if defined(__i386) || defined(__amd64)
40 #define UNALIGNED_POINTERS_PERMITTED
44 * Encrypt multiple blocks of data in CCM mode. Decrypt for CCM mode
45 * is done in another function.
48 ccm_mode_encrypt_contiguous_blocks(ccm_ctx_t
*ctx
, char *data
, size_t length
,
49 crypto_data_t
*out
, size_t block_size
,
50 int (*encrypt_block
)(const void *, const uint8_t *, uint8_t *),
51 void (*copy_block
)(uint8_t *, uint8_t *),
52 void (*xor_block
)(uint8_t *, uint8_t *))
54 size_t remainder
= length
;
56 uint8_t *datap
= (uint8_t *)data
;
63 size_t out_data_1_len
;
67 if (length
+ ctx
->ccm_remainder_len
< block_size
) {
68 /* accumulate bytes here and return */
70 (uint8_t *)ctx
->ccm_remainder
+ ctx
->ccm_remainder_len
,
72 ctx
->ccm_remainder_len
+= length
;
73 ctx
->ccm_copy_to
= datap
;
74 return (CRYPTO_SUCCESS
);
77 lastp
= (uint8_t *)ctx
->ccm_cb
;
79 crypto_init_ptrs(out
, &iov_or_mp
, &offset
);
81 mac_buf
= (uint8_t *)ctx
->ccm_mac_buf
;
84 /* Unprocessed data from last call. */
85 if (ctx
->ccm_remainder_len
> 0) {
86 need
= block_size
- ctx
->ccm_remainder_len
;
89 return (CRYPTO_DATA_LEN_RANGE
);
91 bcopy(datap
, &((uint8_t *)ctx
->ccm_remainder
)
92 [ctx
->ccm_remainder_len
], need
);
94 blockp
= (uint8_t *)ctx
->ccm_remainder
;
102 * XOR the previous cipher block current clear block.
103 * mac_buf always contain previous cipher block.
105 xor_block(blockp
, mac_buf
);
106 encrypt_block(ctx
->ccm_keysched
, mac_buf
, mac_buf
);
108 /* ccm_cb is the counter block */
109 encrypt_block(ctx
->ccm_keysched
, (uint8_t *)ctx
->ccm_cb
,
110 (uint8_t *)ctx
->ccm_tmp
);
112 lastp
= (uint8_t *)ctx
->ccm_tmp
;
115 * Increment counter. Counter bits are confined
116 * to the bottom 64 bits of the counter block.
118 counter
= ntohll(ctx
->ccm_cb
[1] & ctx
->ccm_counter_mask
);
119 counter
= htonll(counter
+ 1);
120 counter
&= ctx
->ccm_counter_mask
;
122 (ctx
->ccm_cb
[1] & ~(ctx
->ccm_counter_mask
)) | counter
;
125 * XOR encrypted counter block with the current clear block.
127 xor_block(blockp
, lastp
);
129 ctx
->ccm_processed_data_len
+= block_size
;
132 if (ctx
->ccm_remainder_len
> 0) {
133 bcopy(blockp
, ctx
->ccm_copy_to
,
134 ctx
->ccm_remainder_len
);
135 bcopy(blockp
+ ctx
->ccm_remainder_len
, datap
,
139 crypto_get_ptrs(out
, &iov_or_mp
, &offset
, &out_data_1
,
140 &out_data_1_len
, &out_data_2
, block_size
);
142 /* copy block to where it belongs */
143 if (out_data_1_len
== block_size
) {
144 copy_block(lastp
, out_data_1
);
146 bcopy(lastp
, out_data_1
, out_data_1_len
);
147 if (out_data_2
!= NULL
) {
148 bcopy(lastp
+ out_data_1_len
,
150 block_size
- out_data_1_len
);
154 out
->cd_offset
+= block_size
;
157 /* Update pointer to next block of data to be processed. */
158 if (ctx
->ccm_remainder_len
!= 0) {
160 ctx
->ccm_remainder_len
= 0;
165 remainder
= (size_t)&data
[length
] - (size_t)datap
;
167 /* Incomplete last block. */
168 if (remainder
> 0 && remainder
< block_size
) {
169 bcopy(datap
, ctx
->ccm_remainder
, remainder
);
170 ctx
->ccm_remainder_len
= remainder
;
171 ctx
->ccm_copy_to
= datap
;
174 ctx
->ccm_copy_to
= NULL
;
176 } while (remainder
> 0);
179 return (CRYPTO_SUCCESS
);
183 calculate_ccm_mac(ccm_ctx_t
*ctx
, uint8_t *ccm_mac
,
184 int (*encrypt_block
)(const void *, const uint8_t *, uint8_t *))
187 uint8_t *counterp
, *mac_buf
;
190 mac_buf
= (uint8_t *)ctx
->ccm_mac_buf
;
192 /* first counter block start with index 0 */
194 ctx
->ccm_cb
[1] = (ctx
->ccm_cb
[1] & ~(ctx
->ccm_counter_mask
)) | counter
;
196 counterp
= (uint8_t *)ctx
->ccm_tmp
;
197 encrypt_block(ctx
->ccm_keysched
, (uint8_t *)ctx
->ccm_cb
, counterp
);
199 /* calculate XOR of MAC with first counter block */
200 for (i
= 0; i
< ctx
->ccm_mac_len
; i
++) {
201 ccm_mac
[i
] = mac_buf
[i
] ^ counterp
[i
];
207 ccm_encrypt_final(ccm_ctx_t
*ctx
, crypto_data_t
*out
, size_t block_size
,
208 int (*encrypt_block
)(const void *, const uint8_t *, uint8_t *),
209 void (*xor_block
)(uint8_t *, uint8_t *))
211 uint8_t *lastp
, *mac_buf
, *ccm_mac_p
, *macp
;
216 size_t out_data_1_len
;
219 if (out
->cd_length
< (ctx
->ccm_remainder_len
+ ctx
->ccm_mac_len
)) {
220 return (CRYPTO_DATA_LEN_RANGE
);
224 * When we get here, the number of bytes of payload processed
225 * plus whatever data remains, if any,
226 * should be the same as the number of bytes that's being
227 * passed in the argument during init time.
229 if ((ctx
->ccm_processed_data_len
+ ctx
->ccm_remainder_len
)
230 != (ctx
->ccm_data_len
)) {
231 return (CRYPTO_DATA_LEN_RANGE
);
234 mac_buf
= (uint8_t *)ctx
->ccm_mac_buf
;
236 if (ctx
->ccm_remainder_len
> 0) {
238 /* ccm_mac_input_buf is not used for encryption */
239 macp
= (uint8_t *)ctx
->ccm_mac_input_buf
;
240 bzero(macp
, block_size
);
242 /* copy remainder to temporary buffer */
243 bcopy(ctx
->ccm_remainder
, macp
, ctx
->ccm_remainder_len
);
245 /* calculate the CBC MAC */
246 xor_block(macp
, mac_buf
);
247 encrypt_block(ctx
->ccm_keysched
, mac_buf
, mac_buf
);
249 /* calculate the counter mode */
250 lastp
= (uint8_t *)ctx
->ccm_tmp
;
251 encrypt_block(ctx
->ccm_keysched
, (uint8_t *)ctx
->ccm_cb
, lastp
);
253 /* XOR with counter block */
254 for (i
= 0; i
< ctx
->ccm_remainder_len
; i
++) {
257 ctx
->ccm_processed_data_len
+= ctx
->ccm_remainder_len
;
260 /* Calculate the CCM MAC */
261 ccm_mac_p
= (uint8_t *)ctx
->ccm_tmp
;
262 calculate_ccm_mac(ctx
, ccm_mac_p
, encrypt_block
);
264 crypto_init_ptrs(out
, &iov_or_mp
, &offset
);
265 crypto_get_ptrs(out
, &iov_or_mp
, &offset
, &out_data_1
,
266 &out_data_1_len
, &out_data_2
,
267 ctx
->ccm_remainder_len
+ ctx
->ccm_mac_len
);
269 if (ctx
->ccm_remainder_len
> 0) {
271 /* copy temporary block to where it belongs */
272 if (out_data_2
== NULL
) {
273 /* everything will fit in out_data_1 */
274 bcopy(macp
, out_data_1
, ctx
->ccm_remainder_len
);
275 bcopy(ccm_mac_p
, out_data_1
+ ctx
->ccm_remainder_len
,
279 if (out_data_1_len
< ctx
->ccm_remainder_len
) {
281 size_t data_2_len_used
;
283 bcopy(macp
, out_data_1
, out_data_1_len
);
285 data_2_len_used
= ctx
->ccm_remainder_len
288 bcopy((uint8_t *)macp
+ out_data_1_len
,
289 out_data_2
, data_2_len_used
);
290 bcopy(ccm_mac_p
, out_data_2
+ data_2_len_used
,
293 bcopy(macp
, out_data_1
, out_data_1_len
);
294 if (out_data_1_len
== ctx
->ccm_remainder_len
) {
295 /* mac will be in out_data_2 */
296 bcopy(ccm_mac_p
, out_data_2
,
299 size_t len_not_used
= out_data_1_len
-
300 ctx
->ccm_remainder_len
;
302 * part of mac in will be in
303 * out_data_1, part of the mac will be
307 out_data_1
+ ctx
->ccm_remainder_len
,
309 bcopy(ccm_mac_p
+ len_not_used
,
311 ctx
->ccm_mac_len
- len_not_used
);
317 /* copy block to where it belongs */
318 bcopy(ccm_mac_p
, out_data_1
, out_data_1_len
);
319 if (out_data_2
!= NULL
) {
320 bcopy(ccm_mac_p
+ out_data_1_len
, out_data_2
,
321 block_size
- out_data_1_len
);
324 out
->cd_offset
+= ctx
->ccm_remainder_len
+ ctx
->ccm_mac_len
;
325 ctx
->ccm_remainder_len
= 0;
326 return (CRYPTO_SUCCESS
);
330 * This will only deal with decrypting the last block of the input that
331 * might not be a multiple of block length.
334 ccm_decrypt_incomplete_block(ccm_ctx_t
*ctx
,
335 int (*encrypt_block
)(const void *, const uint8_t *, uint8_t *))
337 uint8_t *datap
, *outp
, *counterp
;
340 datap
= (uint8_t *)ctx
->ccm_remainder
;
341 outp
= &((ctx
->ccm_pt_buf
)[ctx
->ccm_processed_data_len
]);
343 counterp
= (uint8_t *)ctx
->ccm_tmp
;
344 encrypt_block(ctx
->ccm_keysched
, (uint8_t *)ctx
->ccm_cb
, counterp
);
346 /* XOR with counter block */
347 for (i
= 0; i
< ctx
->ccm_remainder_len
; i
++) {
348 outp
[i
] = datap
[i
] ^ counterp
[i
];
353 * This will decrypt the cipher text. However, the plaintext won't be
354 * returned to the caller. It will be returned when decrypt_final() is
355 * called if the MAC matches
359 ccm_mode_decrypt_contiguous_blocks(ccm_ctx_t
*ctx
, char *data
, size_t length
,
360 crypto_data_t
*out
, size_t block_size
,
361 int (*encrypt_block
)(const void *, const uint8_t *, uint8_t *),
362 void (*copy_block
)(uint8_t *, uint8_t *),
363 void (*xor_block
)(uint8_t *, uint8_t *))
365 size_t remainder
= length
;
367 uint8_t *datap
= (uint8_t *)data
;
371 size_t pt_len
, total_decrypted_len
, mac_len
, pm_len
, pd_len
;
375 pm_len
= ctx
->ccm_processed_mac_len
;
380 * all ciphertext has been processed, just waiting for
381 * part of the value of the mac
383 if ((pm_len
+ length
) > ctx
->ccm_mac_len
) {
384 return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE
);
386 tmp
= (uint8_t *)ctx
->ccm_mac_input_buf
;
388 bcopy(datap
, tmp
+ pm_len
, length
);
390 ctx
->ccm_processed_mac_len
+= length
;
391 return (CRYPTO_SUCCESS
);
395 * If we decrypt the given data, what total amount of data would
396 * have been decrypted?
398 pd_len
= ctx
->ccm_processed_data_len
;
399 total_decrypted_len
= pd_len
+ length
+ ctx
->ccm_remainder_len
;
401 if (total_decrypted_len
>
402 (ctx
->ccm_data_len
+ ctx
->ccm_mac_len
)) {
403 return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE
);
406 pt_len
= ctx
->ccm_data_len
;
408 if (total_decrypted_len
> pt_len
) {
410 * part of the input will be the MAC, need to isolate that
411 * to be dealt with later. The left-over data in
412 * ccm_remainder_len from last time will not be part of the
413 * MAC. Otherwise, it would have already been taken out
414 * when this call is made last time.
416 size_t pt_part
= pt_len
- pd_len
- ctx
->ccm_remainder_len
;
418 mac_len
= length
- pt_part
;
420 ctx
->ccm_processed_mac_len
= mac_len
;
421 bcopy(data
+ pt_part
, ctx
->ccm_mac_input_buf
, mac_len
);
423 if (pt_part
+ ctx
->ccm_remainder_len
< block_size
) {
425 * since this is last of the ciphertext, will
426 * just decrypt with it here
428 bcopy(datap
, &((uint8_t *)ctx
->ccm_remainder
)
429 [ctx
->ccm_remainder_len
], pt_part
);
430 ctx
->ccm_remainder_len
+= pt_part
;
431 ccm_decrypt_incomplete_block(ctx
, encrypt_block
);
432 ctx
->ccm_processed_data_len
+= ctx
->ccm_remainder_len
;
433 ctx
->ccm_remainder_len
= 0;
434 return (CRYPTO_SUCCESS
);
436 /* let rest of the code handle this */
439 } else if (length
+ ctx
->ccm_remainder_len
< block_size
) {
440 /* accumulate bytes here and return */
442 (uint8_t *)ctx
->ccm_remainder
+ ctx
->ccm_remainder_len
,
444 ctx
->ccm_remainder_len
+= length
;
445 ctx
->ccm_copy_to
= datap
;
446 return (CRYPTO_SUCCESS
);
450 /* Unprocessed data from last call. */
451 if (ctx
->ccm_remainder_len
> 0) {
452 need
= block_size
- ctx
->ccm_remainder_len
;
454 if (need
> remainder
)
455 return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE
);
457 bcopy(datap
, &((uint8_t *)ctx
->ccm_remainder
)
458 [ctx
->ccm_remainder_len
], need
);
460 blockp
= (uint8_t *)ctx
->ccm_remainder
;
465 /* Calculate the counter mode, ccm_cb is the counter block */
466 cbp
= (uint8_t *)ctx
->ccm_tmp
;
467 encrypt_block(ctx
->ccm_keysched
, (uint8_t *)ctx
->ccm_cb
, cbp
);
471 * Counter bits are confined to the bottom 64 bits
473 counter
= ntohll(ctx
->ccm_cb
[1] & ctx
->ccm_counter_mask
);
474 counter
= htonll(counter
+ 1);
475 counter
&= ctx
->ccm_counter_mask
;
477 (ctx
->ccm_cb
[1] & ~(ctx
->ccm_counter_mask
)) | counter
;
479 /* XOR with the ciphertext */
480 xor_block(blockp
, cbp
);
482 /* Copy the plaintext to the "holding buffer" */
483 resultp
= (uint8_t *)ctx
->ccm_pt_buf
+
484 ctx
->ccm_processed_data_len
;
485 copy_block(cbp
, resultp
);
487 ctx
->ccm_processed_data_len
+= block_size
;
489 ctx
->ccm_lastp
= blockp
;
491 /* Update pointer to next block of data to be processed. */
492 if (ctx
->ccm_remainder_len
!= 0) {
494 ctx
->ccm_remainder_len
= 0;
499 remainder
= (size_t)&data
[length
] - (size_t)datap
;
501 /* Incomplete last block */
502 if (remainder
> 0 && remainder
< block_size
) {
503 bcopy(datap
, ctx
->ccm_remainder
, remainder
);
504 ctx
->ccm_remainder_len
= remainder
;
505 ctx
->ccm_copy_to
= datap
;
506 if (ctx
->ccm_processed_mac_len
> 0) {
508 * not expecting anymore ciphertext, just
509 * compute plaintext for the remaining input
511 ccm_decrypt_incomplete_block(ctx
,
513 ctx
->ccm_processed_data_len
+= remainder
;
514 ctx
->ccm_remainder_len
= 0;
518 ctx
->ccm_copy_to
= NULL
;
520 } while (remainder
> 0);
523 return (CRYPTO_SUCCESS
);
527 ccm_decrypt_final(ccm_ctx_t
*ctx
, crypto_data_t
*out
, size_t block_size
,
528 int (*encrypt_block
)(const void *, const uint8_t *, uint8_t *),
529 void (*copy_block
)(uint8_t *, uint8_t *),
530 void (*xor_block
)(uint8_t *, uint8_t *))
532 size_t mac_remain
, pt_len
;
533 uint8_t *pt
, *mac_buf
, *macp
, *ccm_mac_p
;
536 pt_len
= ctx
->ccm_data_len
;
538 /* Make sure output buffer can fit all of the plaintext */
539 if (out
->cd_length
< pt_len
) {
540 return (CRYPTO_DATA_LEN_RANGE
);
543 pt
= ctx
->ccm_pt_buf
;
544 mac_remain
= ctx
->ccm_processed_data_len
;
545 mac_buf
= (uint8_t *)ctx
->ccm_mac_buf
;
547 macp
= (uint8_t *)ctx
->ccm_tmp
;
549 while (mac_remain
> 0) {
551 if (mac_remain
< block_size
) {
552 bzero(macp
, block_size
);
553 bcopy(pt
, macp
, mac_remain
);
556 copy_block(pt
, macp
);
557 mac_remain
-= block_size
;
561 /* calculate the CBC MAC */
562 xor_block(macp
, mac_buf
);
563 encrypt_block(ctx
->ccm_keysched
, mac_buf
, mac_buf
);
566 /* Calculate the CCM MAC */
567 ccm_mac_p
= (uint8_t *)ctx
->ccm_tmp
;
568 calculate_ccm_mac((ccm_ctx_t
*)ctx
, ccm_mac_p
, encrypt_block
);
570 /* compare the input CCM MAC value with what we calculated */
571 if (bcmp(ctx
->ccm_mac_input_buf
, ccm_mac_p
, ctx
->ccm_mac_len
)) {
572 /* They don't match */
573 return (CRYPTO_INVALID_MAC
);
575 rv
= crypto_put_output_data(ctx
->ccm_pt_buf
, out
, pt_len
);
576 if (rv
!= CRYPTO_SUCCESS
)
578 out
->cd_offset
+= pt_len
;
580 return (CRYPTO_SUCCESS
);
584 ccm_validate_args(CK_AES_CCM_PARAMS
*ccm_param
, boolean_t is_encrypt_init
)
586 size_t macSize
, nonceSize
;
591 * Check the length of the MAC. The only valid
592 * lengths for the MAC are: 4, 6, 8, 10, 12, 14, 16
594 macSize
= ccm_param
->ulMACSize
;
595 if ((macSize
< 4) || (macSize
> 16) || ((macSize
% 2) != 0)) {
596 return (CRYPTO_MECHANISM_PARAM_INVALID
);
599 /* Check the nonce length. Valid values are 7, 8, 9, 10, 11, 12, 13 */
600 nonceSize
= ccm_param
->ulNonceSize
;
601 if ((nonceSize
< 7) || (nonceSize
> 13)) {
602 return (CRYPTO_MECHANISM_PARAM_INVALID
);
605 /* q is the length of the field storing the length, in bytes */
606 q
= (uint8_t)((15 - nonceSize
) & 0xFF);
610 * If it is decrypt, need to make sure size of ciphertext is at least
611 * bigger than MAC len
613 if ((!is_encrypt_init
) && (ccm_param
->ulDataSize
< macSize
)) {
614 return (CRYPTO_MECHANISM_PARAM_INVALID
);
618 * Check to make sure the length of the payload is within the
619 * range of values allowed by q
622 maxValue
= (1ULL << (q
* 8)) - 1;
624 maxValue
= ULONG_MAX
;
627 if (ccm_param
->ulDataSize
> maxValue
) {
628 return (CRYPTO_MECHANISM_PARAM_INVALID
);
630 return (CRYPTO_SUCCESS
);
634 * Format the first block used in CBC-MAC (B0) and the initial counter
635 * block based on formatting functions and counter generation functions
636 * specified in RFC 3610 and NIST publication 800-38C, appendix A
638 * b0 is the first block used in CBC-MAC
639 * cb0 is the first counter block
641 * It's assumed that the arguments b0 and cb0 are preallocated AES blocks
645 ccm_format_initial_blocks(uchar_t
*nonce
, ulong_t nonceSize
,
646 ulong_t authDataSize
, uint8_t *b0
, ccm_ctx_t
*aes_ctx
)
648 uint64_t payloadSize
;
649 uint8_t t
, q
, have_adata
= 0;
655 q
= (uint8_t)((15 - nonceSize
) & 0xFF);
656 t
= (uint8_t)((aes_ctx
->ccm_mac_len
) & 0xFF);
658 /* Construct the first octet of b0 */
659 if (authDataSize
> 0) {
662 b0
[0] = (have_adata
<< 6) | (((t
- 2) / 2) << 3) | (q
- 1);
664 /* copy the nonce value into b0 */
665 bcopy(nonce
, &(b0
[1]), nonceSize
);
667 /* store the length of the payload into b0 */
668 bzero(&(b0
[1+nonceSize
]), q
);
670 payloadSize
= aes_ctx
->ccm_data_len
;
671 limit
= 8 < q
? 8 : q
;
673 for (i
= 0, j
= 0, k
= 15; i
< limit
; i
++, j
+= 8, k
--) {
674 b0
[k
] = (uint8_t)((payloadSize
>> j
) & 0xFF);
677 /* format the counter block */
679 cb
= (uint8_t *)aes_ctx
->ccm_cb
;
681 cb
[0] = 0x07 & (q
-1); /* first byte */
683 /* copy the nonce value into the counter block */
684 bcopy(nonce
, &(cb
[1]), nonceSize
);
686 bzero(&(cb
[1+nonceSize
]), q
);
688 /* Create the mask for the counter field based on the size of nonce */
694 aes_ctx
->ccm_counter_mask
= htonll(mask
);
697 * During calculation, we start using counter block 1, we will
698 * set it up right here.
699 * We can just set the last byte to have the value 1, because
700 * even with the biggest nonce of 13, the last byte of the
701 * counter block will be used for the counter value.
707 * Encode the length of the associated data as
708 * specified in RFC 3610 and NIST publication 800-38C, appendix A
711 encode_adata_len(ulong_t auth_data_len
, uint8_t *encoded
, size_t *encoded_len
)
713 #ifdef UNALIGNED_POINTERS_PERMITTED
714 uint32_t *lencoded_ptr
;
716 uint64_t *llencoded_ptr
;
718 #endif /* UNALIGNED_POINTERS_PERMITTED */
720 if (auth_data_len
< ((1ULL<<16) - (1ULL<<8))) {
721 /* 0 < a < (2^16-2^8) */
723 encoded
[0] = (auth_data_len
& 0xff00) >> 8;
724 encoded
[1] = auth_data_len
& 0xff;
726 } else if ((auth_data_len
>= ((1ULL<<16) - (1ULL<<8))) &&
727 (auth_data_len
< (1ULL << 31))) {
728 /* (2^16-2^8) <= a < 2^32 */
732 #ifdef UNALIGNED_POINTERS_PERMITTED
733 lencoded_ptr
= (uint32_t *)(void *)&encoded
[2];
734 *lencoded_ptr
= htonl(auth_data_len
);
736 encoded
[2] = (auth_data_len
& 0xff000000) >> 24;
737 encoded
[3] = (auth_data_len
& 0xff0000) >> 16;
738 encoded
[4] = (auth_data_len
& 0xff00) >> 8;
739 encoded
[5] = auth_data_len
& 0xff;
740 #endif /* UNALIGNED_POINTERS_PERMITTED */
744 /* 2^32 <= a < 2^64 */
748 #ifdef UNALIGNED_POINTERS_PERMITTED
749 llencoded_ptr
= (uint64_t *)(void *)&encoded
[2];
750 *llencoded_ptr
= htonl(auth_data_len
);
752 encoded
[2] = (auth_data_len
& 0xff00000000000000) >> 56;
753 encoded
[3] = (auth_data_len
& 0xff000000000000) >> 48;
754 encoded
[4] = (auth_data_len
& 0xff0000000000) >> 40;
755 encoded
[5] = (auth_data_len
& 0xff00000000) >> 32;
756 encoded
[6] = (auth_data_len
& 0xff000000) >> 24;
757 encoded
[7] = (auth_data_len
& 0xff0000) >> 16;
758 encoded
[8] = (auth_data_len
& 0xff00) >> 8;
759 encoded
[9] = auth_data_len
& 0xff;
760 #endif /* UNALIGNED_POINTERS_PERMITTED */
766 * The following function should be call at encrypt or decrypt init time
770 ccm_init(ccm_ctx_t
*ctx
, unsigned char *nonce
, size_t nonce_len
,
771 unsigned char *auth_data
, size_t auth_data_len
, size_t block_size
,
772 int (*encrypt_block
)(const void *, const uint8_t *, uint8_t *),
773 void (*xor_block
)(uint8_t *, uint8_t *))
775 uint8_t *mac_buf
, *datap
, *ivp
, *authp
;
776 size_t remainder
, processed
;
777 uint8_t encoded_a
[10]; /* max encoded auth data length is 10 octets */
778 size_t encoded_a_len
= 0;
780 mac_buf
= (uint8_t *)&(ctx
->ccm_mac_buf
);
783 * Format the 1st block for CBC-MAC and construct the
786 * aes_ctx->ccm_iv is used for storing the counter block
787 * mac_buf will store b0 at this time.
789 ccm_format_initial_blocks(nonce
, nonce_len
,
790 auth_data_len
, mac_buf
, ctx
);
792 /* The IV for CBC MAC for AES CCM mode is always zero */
793 ivp
= (uint8_t *)ctx
->ccm_tmp
;
794 bzero(ivp
, block_size
);
796 xor_block(ivp
, mac_buf
);
798 /* encrypt the nonce */
799 encrypt_block(ctx
->ccm_keysched
, mac_buf
, mac_buf
);
801 /* take care of the associated data, if any */
802 if (auth_data_len
== 0) {
803 return (CRYPTO_SUCCESS
);
806 encode_adata_len(auth_data_len
, encoded_a
, &encoded_a_len
);
808 remainder
= auth_data_len
;
810 /* 1st block: it contains encoded associated data, and some data */
811 authp
= (uint8_t *)ctx
->ccm_tmp
;
812 bzero(authp
, block_size
);
813 bcopy(encoded_a
, authp
, encoded_a_len
);
814 processed
= block_size
- encoded_a_len
;
815 if (processed
> auth_data_len
) {
816 /* in case auth_data is very small */
817 processed
= auth_data_len
;
819 bcopy(auth_data
, authp
+encoded_a_len
, processed
);
820 /* xor with previous buffer */
821 xor_block(authp
, mac_buf
);
822 encrypt_block(ctx
->ccm_keysched
, mac_buf
, mac_buf
);
823 remainder
-= processed
;
824 if (remainder
== 0) {
825 /* a small amount of associated data, it's all done now */
826 return (CRYPTO_SUCCESS
);
830 if (remainder
< block_size
) {
832 * There's not a block full of data, pad rest of
835 bzero(authp
, block_size
);
836 bcopy(&(auth_data
[processed
]), authp
, remainder
);
837 datap
= (uint8_t *)authp
;
840 datap
= (uint8_t *)(&(auth_data
[processed
]));
841 processed
+= block_size
;
842 remainder
-= block_size
;
845 xor_block(datap
, mac_buf
);
846 encrypt_block(ctx
->ccm_keysched
, mac_buf
, mac_buf
);
848 } while (remainder
> 0);
850 return (CRYPTO_SUCCESS
);
854 ccm_init_ctx(ccm_ctx_t
*ccm_ctx
, char *param
, int kmflag
,
855 boolean_t is_encrypt_init
, size_t block_size
,
856 int (*encrypt_block
)(const void *, const uint8_t *, uint8_t *),
857 void (*xor_block
)(uint8_t *, uint8_t *))
860 CK_AES_CCM_PARAMS
*ccm_param
;
863 ccm_param
= (CK_AES_CCM_PARAMS
*)(void *)param
;
865 if ((rv
= ccm_validate_args(ccm_param
,
866 is_encrypt_init
)) != 0) {
870 ccm_ctx
->ccm_mac_len
= ccm_param
->ulMACSize
;
871 if (is_encrypt_init
) {
872 ccm_ctx
->ccm_data_len
= ccm_param
->ulDataSize
;
874 ccm_ctx
->ccm_data_len
=
875 ccm_param
->ulDataSize
- ccm_ctx
->ccm_mac_len
;
876 ccm_ctx
->ccm_processed_mac_len
= 0;
878 ccm_ctx
->ccm_processed_data_len
= 0;
880 ccm_ctx
->ccm_flags
|= CCM_MODE
;
882 rv
= CRYPTO_MECHANISM_PARAM_INVALID
;
886 if (ccm_init(ccm_ctx
, ccm_param
->nonce
, ccm_param
->ulNonceSize
,
887 ccm_param
->authData
, ccm_param
->ulAuthDataSize
, block_size
,
888 encrypt_block
, xor_block
) != 0) {
889 rv
= CRYPTO_MECHANISM_PARAM_INVALID
;
892 if (!is_encrypt_init
) {
893 /* allocate buffer for storing decrypted plaintext */
895 ccm_ctx
->ccm_pt_buf
= kmem_alloc(ccm_ctx
->ccm_data_len
,
898 ccm_ctx
->ccm_pt_buf
= malloc(ccm_ctx
->ccm_data_len
);
900 if (ccm_ctx
->ccm_pt_buf
== NULL
) {
901 rv
= CRYPTO_HOST_MEMORY
;
909 ccm_alloc_ctx(int kmflag
)
914 if ((ccm_ctx
= kmem_zalloc(sizeof (ccm_ctx_t
), kmflag
)) == NULL
)
916 if ((ccm_ctx
= calloc(1, sizeof (ccm_ctx_t
))) == NULL
)
920 ccm_ctx
->ccm_flags
= CCM_MODE
;