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 394b56a003SDaniel Anderson #if defined(__i386) || defined(__amd64) 404b56a003SDaniel Anderson #include <sys/byteorder.h> 414b56a003SDaniel Anderson #define UNALIGNED_POINTERS_PERMITTED 424b56a003SDaniel Anderson #endif 434b56a003SDaniel 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 */ 1194b56a003SDaniel Anderson #ifdef _LITTLE_ENDIAN 1204b56a003SDaniel Anderson counter = ntohll(ctx->ccm_cb[1] & ctx->ccm_counter_mask); 1214b56a003SDaniel Anderson counter = htonll(counter + 1); 1224b56a003SDaniel Anderson #else 12323c57df7Smcpowers counter = ctx->ccm_cb[1] & ctx->ccm_counter_mask; 12423c57df7Smcpowers counter++; 1254b56a003SDaniel 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 { 3054b56a003SDaniel 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 38023c57df7Smcpowers 38123c57df7Smcpowers pm_len = ctx->ccm_processed_mac_len; 38223c57df7Smcpowers 38323c57df7Smcpowers if (pm_len > 0) { 38423c57df7Smcpowers uint8_t *tmp; 38523c57df7Smcpowers /* 38623c57df7Smcpowers * all ciphertext has been processed, just waiting for 38723c57df7Smcpowers * part of the value of the mac 38823c57df7Smcpowers */ 38923c57df7Smcpowers if ((pm_len + length) > ctx->ccm_mac_len) { 39023c57df7Smcpowers return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE); 39123c57df7Smcpowers } 39223c57df7Smcpowers tmp = (uint8_t *)ctx->ccm_mac_input_buf; 39323c57df7Smcpowers 39423c57df7Smcpowers bcopy(datap, tmp + pm_len, length); 39523c57df7Smcpowers 39623c57df7Smcpowers ctx->ccm_processed_mac_len += length; 39723c57df7Smcpowers return (CRYPTO_SUCCESS); 39823c57df7Smcpowers } 39923c57df7Smcpowers 40023c57df7Smcpowers /* 40123c57df7Smcpowers * If we decrypt the given data, what total amount of data would 40223c57df7Smcpowers * have been decrypted? 40323c57df7Smcpowers */ 40423c57df7Smcpowers pd_len = ctx->ccm_processed_data_len; 40523c57df7Smcpowers total_decrypted_len = pd_len + length + ctx->ccm_remainder_len; 40623c57df7Smcpowers 40723c57df7Smcpowers if (total_decrypted_len > 40823c57df7Smcpowers (ctx->ccm_data_len + ctx->ccm_mac_len)) { 40923c57df7Smcpowers return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE); 41023c57df7Smcpowers } 41123c57df7Smcpowers 41223c57df7Smcpowers pt_len = ctx->ccm_data_len; 41323c57df7Smcpowers 41423c57df7Smcpowers if (total_decrypted_len > pt_len) { 41523c57df7Smcpowers /* 41623c57df7Smcpowers * part of the input will be the MAC, need to isolate that 41723c57df7Smcpowers * to be dealt with later. The left-over data in 41823c57df7Smcpowers * ccm_remainder_len from last time will not be part of the 41923c57df7Smcpowers * MAC. Otherwise, it would have already been taken out 42023c57df7Smcpowers * when this call is made last time. 42123c57df7Smcpowers */ 42223c57df7Smcpowers size_t pt_part = pt_len - pd_len - ctx->ccm_remainder_len; 42323c57df7Smcpowers 42423c57df7Smcpowers mac_len = length - pt_part; 42523c57df7Smcpowers 42623c57df7Smcpowers ctx->ccm_processed_mac_len = mac_len; 42723c57df7Smcpowers bcopy(data + pt_part, ctx->ccm_mac_input_buf, mac_len); 42823c57df7Smcpowers 42923c57df7Smcpowers if (pt_part + ctx->ccm_remainder_len < block_size) { 43023c57df7Smcpowers /* 43123c57df7Smcpowers * since this is last of the ciphertext, will 43223c57df7Smcpowers * just decrypt with it here 43323c57df7Smcpowers */ 43423c57df7Smcpowers bcopy(datap, &((uint8_t *)ctx->ccm_remainder) 43523c57df7Smcpowers [ctx->ccm_remainder_len], pt_part); 43623c57df7Smcpowers ctx->ccm_remainder_len += pt_part; 43723c57df7Smcpowers ccm_decrypt_incomplete_block(ctx, encrypt_block); 43823c57df7Smcpowers ctx->ccm_remainder_len = 0; 43923c57df7Smcpowers ctx->ccm_processed_data_len += pt_part; 44023c57df7Smcpowers return (CRYPTO_SUCCESS); 44123c57df7Smcpowers } else { 44223c57df7Smcpowers /* let rest of the code handle this */ 44323c57df7Smcpowers length = pt_part; 44423c57df7Smcpowers } 44523c57df7Smcpowers } else if (length + ctx->ccm_remainder_len < block_size) { 44623c57df7Smcpowers /* accumulate bytes here and return */ 44723c57df7Smcpowers bcopy(datap, 44823c57df7Smcpowers (uint8_t *)ctx->ccm_remainder + ctx->ccm_remainder_len, 44923c57df7Smcpowers length); 45023c57df7Smcpowers ctx->ccm_remainder_len += length; 45123c57df7Smcpowers ctx->ccm_copy_to = datap; 45223c57df7Smcpowers return (CRYPTO_SUCCESS); 45323c57df7Smcpowers } 45423c57df7Smcpowers 45523c57df7Smcpowers do { 45623c57df7Smcpowers /* Unprocessed data from last call. */ 45723c57df7Smcpowers if (ctx->ccm_remainder_len > 0) { 45823c57df7Smcpowers need = block_size - ctx->ccm_remainder_len; 45923c57df7Smcpowers 46023c57df7Smcpowers if (need > remainder) 46123c57df7Smcpowers return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE); 46223c57df7Smcpowers 46323c57df7Smcpowers bcopy(datap, &((uint8_t *)ctx->ccm_remainder) 46423c57df7Smcpowers [ctx->ccm_remainder_len], need); 46523c57df7Smcpowers 46623c57df7Smcpowers blockp = (uint8_t *)ctx->ccm_remainder; 46723c57df7Smcpowers } else { 46823c57df7Smcpowers blockp = datap; 46923c57df7Smcpowers } 47023c57df7Smcpowers 47123c57df7Smcpowers /* Calculate the counter mode, ccm_cb is the counter block */ 47223c57df7Smcpowers cbp = (uint8_t *)ctx->ccm_tmp; 47323c57df7Smcpowers encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, cbp); 47423c57df7Smcpowers 47523c57df7Smcpowers /* 47623c57df7Smcpowers * Increment counter. 47723c57df7Smcpowers * Counter bits are confined to the bottom 64 bits 47823c57df7Smcpowers */ 4794b56a003SDaniel Anderson #ifdef _LITTLE_ENDIAN 4804b56a003SDaniel Anderson counter = ntohll(ctx->ccm_cb[1] & ctx->ccm_counter_mask); 4814b56a003SDaniel Anderson counter = htonll(counter + 1); 4824b56a003SDaniel Anderson #else 48323c57df7Smcpowers counter = ctx->ccm_cb[1] & ctx->ccm_counter_mask; 48423c57df7Smcpowers counter++; 4854b56a003SDaniel Anderson #endif /* _LITTLE_ENDIAN */ 48623c57df7Smcpowers counter &= ctx->ccm_counter_mask; 48723c57df7Smcpowers ctx->ccm_cb[1] = 48823c57df7Smcpowers (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter; 48923c57df7Smcpowers 49023c57df7Smcpowers /* XOR with the ciphertext */ 49123c57df7Smcpowers xor_block(blockp, cbp); 49223c57df7Smcpowers 49323c57df7Smcpowers /* Copy the plaintext to the "holding buffer" */ 49423c57df7Smcpowers resultp = (uint8_t *)ctx->ccm_pt_buf + 49523c57df7Smcpowers ctx->ccm_processed_data_len; 49623c57df7Smcpowers copy_block(cbp, resultp); 49723c57df7Smcpowers 49823c57df7Smcpowers ctx->ccm_processed_data_len += block_size; 49923c57df7Smcpowers 50023c57df7Smcpowers ctx->ccm_lastp = blockp; 50123c57df7Smcpowers 50223c57df7Smcpowers /* Update pointer to next block of data to be processed. */ 50323c57df7Smcpowers if (ctx->ccm_remainder_len != 0) { 50423c57df7Smcpowers datap += need; 50523c57df7Smcpowers ctx->ccm_remainder_len = 0; 50623c57df7Smcpowers } else { 50723c57df7Smcpowers datap += block_size; 50823c57df7Smcpowers } 50923c57df7Smcpowers 51023c57df7Smcpowers remainder = (size_t)&data[length] - (size_t)datap; 51123c57df7Smcpowers 51223c57df7Smcpowers /* Incomplete last block */ 51323c57df7Smcpowers if (remainder > 0 && remainder < block_size) { 51423c57df7Smcpowers bcopy(datap, ctx->ccm_remainder, remainder); 51523c57df7Smcpowers ctx->ccm_remainder_len = remainder; 51623c57df7Smcpowers ctx->ccm_copy_to = datap; 51723c57df7Smcpowers if (ctx->ccm_processed_mac_len > 0) { 51823c57df7Smcpowers /* 51923c57df7Smcpowers * not expecting anymore ciphertext, just 52023c57df7Smcpowers * compute plaintext for the remaining input 52123c57df7Smcpowers */ 52223c57df7Smcpowers ccm_decrypt_incomplete_block(ctx, 52323c57df7Smcpowers encrypt_block); 52423c57df7Smcpowers ctx->ccm_processed_data_len += remainder; 52523c57df7Smcpowers ctx->ccm_remainder_len = 0; 52623c57df7Smcpowers } 52723c57df7Smcpowers goto out; 52823c57df7Smcpowers } 52923c57df7Smcpowers ctx->ccm_copy_to = NULL; 53023c57df7Smcpowers 53123c57df7Smcpowers } while (remainder > 0); 53223c57df7Smcpowers 53323c57df7Smcpowers out: 53423c57df7Smcpowers return (CRYPTO_SUCCESS); 53523c57df7Smcpowers } 53623c57df7Smcpowers 53723c57df7Smcpowers int 53823c57df7Smcpowers ccm_decrypt_final(ccm_ctx_t *ctx, crypto_data_t *out, size_t block_size, 53923c57df7Smcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 54023c57df7Smcpowers void (*copy_block)(uint8_t *, uint8_t *), 54123c57df7Smcpowers void (*xor_block)(uint8_t *, uint8_t *)) 54223c57df7Smcpowers { 54323c57df7Smcpowers size_t mac_remain, pt_len; 54423c57df7Smcpowers uint8_t *pt, *mac_buf, *macp, *ccm_mac_p; 545*7e0cc741SMark Powers int rv; 54623c57df7Smcpowers 54723c57df7Smcpowers pt_len = ctx->ccm_data_len; 54823c57df7Smcpowers 54923c57df7Smcpowers /* Make sure output buffer can fit all of the plaintext */ 55023c57df7Smcpowers if (out->cd_length < pt_len) { 55123c57df7Smcpowers return (CRYPTO_DATA_LEN_RANGE); 55223c57df7Smcpowers } 55323c57df7Smcpowers 55423c57df7Smcpowers pt = ctx->ccm_pt_buf; 55523c57df7Smcpowers mac_remain = ctx->ccm_processed_data_len; 55623c57df7Smcpowers mac_buf = (uint8_t *)ctx->ccm_mac_buf; 55723c57df7Smcpowers 55823c57df7Smcpowers macp = (uint8_t *)ctx->ccm_tmp; 55923c57df7Smcpowers 56023c57df7Smcpowers while (mac_remain > 0) { 56123c57df7Smcpowers 56223c57df7Smcpowers if (mac_remain < block_size) { 56323c57df7Smcpowers bzero(macp, block_size); 56423c57df7Smcpowers bcopy(pt, macp, mac_remain); 56523c57df7Smcpowers mac_remain = 0; 56623c57df7Smcpowers } else { 56723c57df7Smcpowers copy_block(pt, macp); 56823c57df7Smcpowers mac_remain -= block_size; 56923c57df7Smcpowers pt += block_size; 57023c57df7Smcpowers } 57123c57df7Smcpowers 57223c57df7Smcpowers /* calculate the CBC MAC */ 57323c57df7Smcpowers xor_block(macp, mac_buf); 57423c57df7Smcpowers encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf); 57523c57df7Smcpowers } 57623c57df7Smcpowers 57723c57df7Smcpowers /* Calculate the CCM MAC */ 57823c57df7Smcpowers ccm_mac_p = (uint8_t *)ctx->ccm_tmp; 57923c57df7Smcpowers calculate_ccm_mac((ccm_ctx_t *)ctx, ccm_mac_p, encrypt_block); 58023c57df7Smcpowers 58123c57df7Smcpowers /* compare the input CCM MAC value with what we calculated */ 58223c57df7Smcpowers if (bcmp(ctx->ccm_mac_input_buf, ccm_mac_p, ctx->ccm_mac_len)) { 58323c57df7Smcpowers /* They don't match */ 58423c57df7Smcpowers return (CRYPTO_INVALID_MAC); 58523c57df7Smcpowers } else { 586*7e0cc741SMark Powers rv = crypto_put_output_data(ctx->ccm_pt_buf, out, pt_len); 587*7e0cc741SMark Powers if (rv != CRYPTO_SUCCESS) 588*7e0cc741SMark Powers return (rv); 58923c57df7Smcpowers out->cd_offset += pt_len; 59023c57df7Smcpowers } 59123c57df7Smcpowers return (CRYPTO_SUCCESS); 59223c57df7Smcpowers } 59323c57df7Smcpowers 59423c57df7Smcpowers int 59523c57df7Smcpowers ccm_validate_args(CK_AES_CCM_PARAMS *ccm_param, boolean_t is_encrypt_init) 59623c57df7Smcpowers { 59723c57df7Smcpowers size_t macSize, nonceSize; 59823c57df7Smcpowers uint8_t q; 59923c57df7Smcpowers uint64_t maxValue; 60023c57df7Smcpowers 60123c57df7Smcpowers /* 60223c57df7Smcpowers * Check the length of the MAC. The only valid 60323c57df7Smcpowers * lengths for the MAC are: 4, 6, 8, 10, 12, 14, 16 60423c57df7Smcpowers */ 60523c57df7Smcpowers macSize = ccm_param->ulMACSize; 60623c57df7Smcpowers if ((macSize < 4) || (macSize > 16) || ((macSize % 2) != 0)) { 60723c57df7Smcpowers return (CRYPTO_MECHANISM_PARAM_INVALID); 60823c57df7Smcpowers } 60923c57df7Smcpowers 61023c57df7Smcpowers /* Check the nonce length. Valid values are 7, 8, 9, 10, 11, 12, 13 */ 61123c57df7Smcpowers nonceSize = ccm_param->ulNonceSize; 61223c57df7Smcpowers if ((nonceSize < 7) || (nonceSize > 13)) { 61323c57df7Smcpowers return (CRYPTO_MECHANISM_PARAM_INVALID); 61423c57df7Smcpowers } 61523c57df7Smcpowers 61623c57df7Smcpowers /* q is the length of the field storing the length, in bytes */ 61723c57df7Smcpowers q = (uint8_t)((15 - nonceSize) & 0xFF); 61823c57df7Smcpowers 61923c57df7Smcpowers 62023c57df7Smcpowers /* 62123c57df7Smcpowers * If it is decrypt, need to make sure size of ciphertext is at least 62223c57df7Smcpowers * bigger than MAC len 62323c57df7Smcpowers */ 62423c57df7Smcpowers if ((!is_encrypt_init) && (ccm_param->ulDataSize < macSize)) { 62523c57df7Smcpowers return (CRYPTO_MECHANISM_PARAM_INVALID); 62623c57df7Smcpowers } 62723c57df7Smcpowers 62823c57df7Smcpowers /* 62923c57df7Smcpowers * Check to make sure the length of the payload is within the 63023c57df7Smcpowers * range of values allowed by q 63123c57df7Smcpowers */ 63223c57df7Smcpowers if (q < 8) { 63323c57df7Smcpowers maxValue = (1ULL << (q * 8)) - 1; 63423c57df7Smcpowers } else { 63523c57df7Smcpowers maxValue = ULONG_MAX; 63623c57df7Smcpowers } 63723c57df7Smcpowers 63823c57df7Smcpowers if (ccm_param->ulDataSize > maxValue) { 63923c57df7Smcpowers return (CRYPTO_MECHANISM_PARAM_INVALID); 64023c57df7Smcpowers } 64123c57df7Smcpowers return (CRYPTO_SUCCESS); 64223c57df7Smcpowers } 64323c57df7Smcpowers 64423c57df7Smcpowers /* 64523c57df7Smcpowers * Format the first block used in CBC-MAC (B0) and the initial counter 64623c57df7Smcpowers * block based on formatting functions and counter generation functions 64723c57df7Smcpowers * specified in RFC 3610 and NIST publication 800-38C, appendix A 64823c57df7Smcpowers * 64923c57df7Smcpowers * b0 is the first block used in CBC-MAC 65023c57df7Smcpowers * cb0 is the first counter block 65123c57df7Smcpowers * 65223c57df7Smcpowers * It's assumed that the arguments b0 and cb0 are preallocated AES blocks 65323c57df7Smcpowers * 65423c57df7Smcpowers */ 65523c57df7Smcpowers static void 65623c57df7Smcpowers ccm_format_initial_blocks(uchar_t *nonce, ulong_t nonceSize, 65723c57df7Smcpowers ulong_t authDataSize, uint8_t *b0, ccm_ctx_t *aes_ctx) 65823c57df7Smcpowers { 65923c57df7Smcpowers uint64_t payloadSize; 66023c57df7Smcpowers uint8_t t, q, have_adata = 0; 66123c57df7Smcpowers size_t limit; 66223c57df7Smcpowers int i, j, k; 66323c57df7Smcpowers uint64_t mask = 0; 66423c57df7Smcpowers uint8_t *cb; 66523c57df7Smcpowers 66623c57df7Smcpowers q = (uint8_t)((15 - nonceSize) & 0xFF); 66723c57df7Smcpowers t = (uint8_t)((aes_ctx->ccm_mac_len) & 0xFF); 66823c57df7Smcpowers 66923c57df7Smcpowers /* Construct the first octet of b0 */ 67023c57df7Smcpowers if (authDataSize > 0) { 67123c57df7Smcpowers have_adata = 1; 67223c57df7Smcpowers } 67323c57df7Smcpowers b0[0] = (have_adata << 6) | (((t - 2) / 2) << 3) | (q - 1); 67423c57df7Smcpowers 67523c57df7Smcpowers /* copy the nonce value into b0 */ 67623c57df7Smcpowers bcopy(nonce, &(b0[1]), nonceSize); 67723c57df7Smcpowers 67823c57df7Smcpowers /* store the length of the payload into b0 */ 67923c57df7Smcpowers bzero(&(b0[1+nonceSize]), q); 68023c57df7Smcpowers 68123c57df7Smcpowers payloadSize = aes_ctx->ccm_data_len; 68223c57df7Smcpowers limit = 8 < q ? 8 : q; 68323c57df7Smcpowers 68423c57df7Smcpowers for (i = 0, j = 0, k = 15; i < limit; i++, j += 8, k--) { 68523c57df7Smcpowers b0[k] = (uint8_t)((payloadSize >> j) & 0xFF); 68623c57df7Smcpowers } 68723c57df7Smcpowers 68823c57df7Smcpowers /* format the counter block */ 68923c57df7Smcpowers 69023c57df7Smcpowers cb = (uint8_t *)aes_ctx->ccm_cb; 69123c57df7Smcpowers 69223c57df7Smcpowers cb[0] = 0x07 & (q-1); /* first byte */ 69323c57df7Smcpowers 69423c57df7Smcpowers /* copy the nonce value into the counter block */ 69523c57df7Smcpowers bcopy(nonce, &(cb[1]), nonceSize); 69623c57df7Smcpowers 69723c57df7Smcpowers bzero(&(cb[1+nonceSize]), q); 69823c57df7Smcpowers 69923c57df7Smcpowers /* Create the mask for the counter field based on the size of nonce */ 70023c57df7Smcpowers q <<= 3; 70123c57df7Smcpowers while (q-- > 0) { 70223c57df7Smcpowers mask |= (1ULL << q); 70323c57df7Smcpowers } 70423c57df7Smcpowers 70523c57df7Smcpowers #ifdef _LITTLE_ENDIAN 7064b56a003SDaniel Anderson mask = htonll(mask); 70723c57df7Smcpowers #endif 70823c57df7Smcpowers aes_ctx->ccm_counter_mask = mask; 70923c57df7Smcpowers 71023c57df7Smcpowers /* 71123c57df7Smcpowers * During calculation, we start using counter block 1, we will 71223c57df7Smcpowers * set it up right here. 71323c57df7Smcpowers * We can just set the last byte to have the value 1, because 71423c57df7Smcpowers * even with the biggest nonce of 13, the last byte of the 71523c57df7Smcpowers * counter block will be used for the counter value. 71623c57df7Smcpowers */ 71723c57df7Smcpowers cb[15] = 0x01; 71823c57df7Smcpowers } 71923c57df7Smcpowers 72023c57df7Smcpowers /* 72123c57df7Smcpowers * Encode the length of the associated data as 72223c57df7Smcpowers * specified in RFC 3610 and NIST publication 800-38C, appendix A 72323c57df7Smcpowers */ 72423c57df7Smcpowers static void 72523c57df7Smcpowers encode_adata_len(ulong_t auth_data_len, uint8_t *encoded, size_t *encoded_len) 72623c57df7Smcpowers { 7274b56a003SDaniel Anderson #ifdef UNALIGNED_POINTERS_PERMITTED 7284b56a003SDaniel Anderson uint32_t *lencoded_ptr; 7294b56a003SDaniel Anderson #ifdef _LP64 7304b56a003SDaniel Anderson uint64_t *llencoded_ptr; 7314b56a003SDaniel Anderson #endif 7324b56a003SDaniel Anderson #endif /* UNALIGNED_POINTERS_PERMITTED */ 7334b56a003SDaniel Anderson 73423c57df7Smcpowers if (auth_data_len < ((1ULL<<16) - (1ULL<<8))) { 73523c57df7Smcpowers /* 0 < a < (2^16-2^8) */ 73623c57df7Smcpowers *encoded_len = 2; 73723c57df7Smcpowers encoded[0] = (auth_data_len & 0xff00) >> 8; 73823c57df7Smcpowers encoded[1] = auth_data_len & 0xff; 73923c57df7Smcpowers 74023c57df7Smcpowers } else if ((auth_data_len >= ((1ULL<<16) - (1ULL<<8))) && 74123c57df7Smcpowers (auth_data_len < (1ULL << 31))) { 74223c57df7Smcpowers /* (2^16-2^8) <= a < 2^32 */ 74323c57df7Smcpowers *encoded_len = 6; 74423c57df7Smcpowers encoded[0] = 0xff; 74523c57df7Smcpowers encoded[1] = 0xfe; 7464b56a003SDaniel Anderson #ifdef UNALIGNED_POINTERS_PERMITTED 7474b56a003SDaniel Anderson lencoded_ptr = (uint32_t *)&encoded[2]; 7484b56a003SDaniel Anderson *lencoded_ptr = htonl(auth_data_len); 7494b56a003SDaniel Anderson #else 75023c57df7Smcpowers encoded[2] = (auth_data_len & 0xff000000) >> 24; 75123c57df7Smcpowers encoded[3] = (auth_data_len & 0xff0000) >> 16; 75223c57df7Smcpowers encoded[4] = (auth_data_len & 0xff00) >> 8; 75323c57df7Smcpowers encoded[5] = auth_data_len & 0xff; 7544b56a003SDaniel Anderson #endif /* UNALIGNED_POINTERS_PERMITTED */ 7554b56a003SDaniel Anderson 75623c57df7Smcpowers #ifdef _LP64 75723c57df7Smcpowers } else { 75823c57df7Smcpowers /* 2^32 <= a < 2^64 */ 75923c57df7Smcpowers *encoded_len = 10; 76023c57df7Smcpowers encoded[0] = 0xff; 76123c57df7Smcpowers encoded[1] = 0xff; 7624b56a003SDaniel Anderson #ifdef UNALIGNED_POINTERS_PERMITTED 7634b56a003SDaniel Anderson llencoded_ptr = (uint64_t *)&encoded[2]; 7644b56a003SDaniel Anderson *llencoded_ptr = htonl(auth_data_len); 7654b56a003SDaniel Anderson #else 76623c57df7Smcpowers encoded[2] = (auth_data_len & 0xff00000000000000) >> 56; 76723c57df7Smcpowers encoded[3] = (auth_data_len & 0xff000000000000) >> 48; 76823c57df7Smcpowers encoded[4] = (auth_data_len & 0xff0000000000) >> 40; 76923c57df7Smcpowers encoded[5] = (auth_data_len & 0xff00000000) >> 32; 77023c57df7Smcpowers encoded[6] = (auth_data_len & 0xff000000) >> 24; 77123c57df7Smcpowers encoded[7] = (auth_data_len & 0xff0000) >> 16; 77223c57df7Smcpowers encoded[8] = (auth_data_len & 0xff00) >> 8; 77323c57df7Smcpowers encoded[9] = auth_data_len & 0xff; 7744b56a003SDaniel Anderson #endif /* UNALIGNED_POINTERS_PERMITTED */ 77523c57df7Smcpowers #endif /* _LP64 */ 77623c57df7Smcpowers } 77723c57df7Smcpowers } 77823c57df7Smcpowers 77923c57df7Smcpowers /* 78023c57df7Smcpowers * The following function should be call at encrypt or decrypt init time 78123c57df7Smcpowers * for AES CCM mode. 78223c57df7Smcpowers */ 78323c57df7Smcpowers int 78423c57df7Smcpowers ccm_init(ccm_ctx_t *ctx, unsigned char *nonce, size_t nonce_len, 78523c57df7Smcpowers unsigned char *auth_data, size_t auth_data_len, size_t block_size, 78623c57df7Smcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 78723c57df7Smcpowers void (*xor_block)(uint8_t *, uint8_t *)) 78823c57df7Smcpowers { 78923c57df7Smcpowers uint8_t *mac_buf, *datap, *ivp, *authp; 79023c57df7Smcpowers size_t remainder, processed; 79123c57df7Smcpowers uint8_t encoded_a[10]; /* max encoded auth data length is 10 octets */ 79223c57df7Smcpowers size_t encoded_a_len = 0; 79323c57df7Smcpowers 79423c57df7Smcpowers mac_buf = (uint8_t *)&(ctx->ccm_mac_buf); 79523c57df7Smcpowers 79623c57df7Smcpowers /* 79723c57df7Smcpowers * Format the 1st block for CBC-MAC and construct the 79823c57df7Smcpowers * 1st counter block. 79923c57df7Smcpowers * 80023c57df7Smcpowers * aes_ctx->ccm_iv is used for storing the counter block 80123c57df7Smcpowers * mac_buf will store b0 at this time. 80223c57df7Smcpowers */ 80323c57df7Smcpowers ccm_format_initial_blocks(nonce, nonce_len, 80423c57df7Smcpowers auth_data_len, mac_buf, ctx); 80523c57df7Smcpowers 80623c57df7Smcpowers /* The IV for CBC MAC for AES CCM mode is always zero */ 80723c57df7Smcpowers ivp = (uint8_t *)ctx->ccm_tmp; 80823c57df7Smcpowers bzero(ivp, block_size); 80923c57df7Smcpowers 81023c57df7Smcpowers xor_block(ivp, mac_buf); 81123c57df7Smcpowers 81223c57df7Smcpowers /* encrypt the nonce */ 81323c57df7Smcpowers encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf); 81423c57df7Smcpowers 81523c57df7Smcpowers /* take care of the associated data, if any */ 81623c57df7Smcpowers if (auth_data_len == 0) { 81723c57df7Smcpowers return (CRYPTO_SUCCESS); 81823c57df7Smcpowers } 81923c57df7Smcpowers 82023c57df7Smcpowers encode_adata_len(auth_data_len, encoded_a, &encoded_a_len); 82123c57df7Smcpowers 82223c57df7Smcpowers remainder = auth_data_len; 82323c57df7Smcpowers 82423c57df7Smcpowers /* 1st block: it contains encoded associated data, and some data */ 82523c57df7Smcpowers authp = (uint8_t *)ctx->ccm_tmp; 82623c57df7Smcpowers bzero(authp, block_size); 82723c57df7Smcpowers bcopy(encoded_a, authp, encoded_a_len); 82823c57df7Smcpowers processed = block_size - encoded_a_len; 82923c57df7Smcpowers if (processed > auth_data_len) { 83023c57df7Smcpowers /* in case auth_data is very small */ 83123c57df7Smcpowers processed = auth_data_len; 83223c57df7Smcpowers } 83323c57df7Smcpowers bcopy(auth_data, authp+encoded_a_len, processed); 83423c57df7Smcpowers /* xor with previous buffer */ 83523c57df7Smcpowers xor_block(authp, mac_buf); 83623c57df7Smcpowers encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf); 83723c57df7Smcpowers remainder -= processed; 83823c57df7Smcpowers if (remainder == 0) { 83923c57df7Smcpowers /* a small amount of associated data, it's all done now */ 84023c57df7Smcpowers return (CRYPTO_SUCCESS); 84123c57df7Smcpowers } 84223c57df7Smcpowers 84323c57df7Smcpowers do { 84423c57df7Smcpowers if (remainder < block_size) { 84523c57df7Smcpowers /* 84623c57df7Smcpowers * There's not a block full of data, pad rest of 84723c57df7Smcpowers * buffer with zero 84823c57df7Smcpowers */ 84923c57df7Smcpowers bzero(authp, block_size); 85023c57df7Smcpowers bcopy(&(auth_data[processed]), authp, remainder); 85123c57df7Smcpowers datap = (uint8_t *)authp; 85223c57df7Smcpowers remainder = 0; 85323c57df7Smcpowers } else { 85423c57df7Smcpowers datap = (uint8_t *)(&(auth_data[processed])); 85523c57df7Smcpowers processed += block_size; 85623c57df7Smcpowers remainder -= block_size; 85723c57df7Smcpowers } 85823c57df7Smcpowers 85923c57df7Smcpowers xor_block(datap, mac_buf); 86023c57df7Smcpowers encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf); 86123c57df7Smcpowers 86223c57df7Smcpowers } while (remainder > 0); 86323c57df7Smcpowers 86423c57df7Smcpowers return (CRYPTO_SUCCESS); 86523c57df7Smcpowers } 86623c57df7Smcpowers 86723c57df7Smcpowers int 86823c57df7Smcpowers ccm_init_ctx(ccm_ctx_t *ccm_ctx, char *param, int kmflag, 86923c57df7Smcpowers boolean_t is_encrypt_init, size_t block_size, 87023c57df7Smcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 87123c57df7Smcpowers void (*xor_block)(uint8_t *, uint8_t *)) 87223c57df7Smcpowers { 87323c57df7Smcpowers int rv; 87423c57df7Smcpowers CK_AES_CCM_PARAMS *ccm_param; 87523c57df7Smcpowers 87623c57df7Smcpowers if (param != NULL) { 87723c57df7Smcpowers ccm_param = (CK_AES_CCM_PARAMS *)param; 87823c57df7Smcpowers 87923c57df7Smcpowers if ((rv = ccm_validate_args(ccm_param, 88023c57df7Smcpowers is_encrypt_init)) != 0) { 88123c57df7Smcpowers return (rv); 88223c57df7Smcpowers } 88323c57df7Smcpowers 88423c57df7Smcpowers ccm_ctx->ccm_mac_len = ccm_param->ulMACSize; 88523c57df7Smcpowers if (is_encrypt_init) { 88623c57df7Smcpowers ccm_ctx->ccm_data_len = ccm_param->ulDataSize; 88723c57df7Smcpowers } else { 88823c57df7Smcpowers ccm_ctx->ccm_data_len = 88923c57df7Smcpowers ccm_param->ulDataSize - ccm_ctx->ccm_mac_len; 89023c57df7Smcpowers ccm_ctx->ccm_processed_mac_len = 0; 89123c57df7Smcpowers } 89223c57df7Smcpowers ccm_ctx->ccm_processed_data_len = 0; 89323c57df7Smcpowers 89423c57df7Smcpowers ccm_ctx->ccm_flags |= CCM_MODE; 89523c57df7Smcpowers } else { 89623c57df7Smcpowers rv = CRYPTO_MECHANISM_PARAM_INVALID; 89723c57df7Smcpowers goto out; 89823c57df7Smcpowers } 89923c57df7Smcpowers 90023c57df7Smcpowers if (ccm_init(ccm_ctx, ccm_param->nonce, ccm_param->ulNonceSize, 90123c57df7Smcpowers ccm_param->authData, ccm_param->ulAuthDataSize, block_size, 90223c57df7Smcpowers encrypt_block, xor_block) != 0) { 90323c57df7Smcpowers rv = CRYPTO_MECHANISM_PARAM_INVALID; 90423c57df7Smcpowers goto out; 90523c57df7Smcpowers } 90623c57df7Smcpowers if (!is_encrypt_init) { 90723c57df7Smcpowers /* allocate buffer for storing decrypted plaintext */ 90823c57df7Smcpowers #ifdef _KERNEL 90923c57df7Smcpowers ccm_ctx->ccm_pt_buf = kmem_alloc(ccm_ctx->ccm_data_len, 91023c57df7Smcpowers kmflag); 91123c57df7Smcpowers #else 91223c57df7Smcpowers ccm_ctx->ccm_pt_buf = malloc(ccm_ctx->ccm_data_len); 91323c57df7Smcpowers #endif 91423c57df7Smcpowers if (ccm_ctx->ccm_pt_buf == NULL) { 91523c57df7Smcpowers rv = CRYPTO_HOST_MEMORY; 91623c57df7Smcpowers } 91723c57df7Smcpowers } 91823c57df7Smcpowers out: 91923c57df7Smcpowers return (rv); 92023c57df7Smcpowers } 92123c57df7Smcpowers 92223c57df7Smcpowers void * 92323c57df7Smcpowers ccm_alloc_ctx(int kmflag) 92423c57df7Smcpowers { 92523c57df7Smcpowers ccm_ctx_t *ccm_ctx; 92623c57df7Smcpowers 92723c57df7Smcpowers #ifdef _KERNEL 92823c57df7Smcpowers if ((ccm_ctx = kmem_zalloc(sizeof (ccm_ctx_t), kmflag)) == NULL) 92923c57df7Smcpowers #else 93023c57df7Smcpowers if ((ccm_ctx = calloc(1, sizeof (ccm_ctx_t))) == NULL) 93123c57df7Smcpowers #endif 93223c57df7Smcpowers return (NULL); 93323c57df7Smcpowers 93423c57df7Smcpowers ccm_ctx->ccm_flags = CCM_MODE; 93523c57df7Smcpowers return (ccm_ctx); 93623c57df7Smcpowers } 937