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 /* 22*54034eb2SDan OpenSolaris Anderson * Copyright 2009 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> 38*54034eb2SDan OpenSolaris Anderson #include <sys/byteorder.h> 3923c57df7Smcpowers 404b56a003SDaniel Anderson #if defined(__i386) || defined(__amd64) 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 counter = ntohll(ctx->ccm_cb[1] & ctx->ccm_counter_mask); 1204b56a003SDaniel Anderson counter = htonll(counter + 1); 12123c57df7Smcpowers counter &= ctx->ccm_counter_mask; 12223c57df7Smcpowers ctx->ccm_cb[1] = 12323c57df7Smcpowers (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter; 12423c57df7Smcpowers 12523c57df7Smcpowers /* 126f02d2799SMark Powers * XOR encrypted counter block with the current clear block. 12723c57df7Smcpowers */ 128f02d2799SMark Powers xor_block(blockp, lastp); 12923c57df7Smcpowers 13023c57df7Smcpowers ctx->ccm_processed_data_len += block_size; 13123c57df7Smcpowers 13223c57df7Smcpowers if (out == NULL) { 13323c57df7Smcpowers if (ctx->ccm_remainder_len > 0) { 13423c57df7Smcpowers bcopy(blockp, ctx->ccm_copy_to, 13523c57df7Smcpowers ctx->ccm_remainder_len); 13623c57df7Smcpowers bcopy(blockp + ctx->ccm_remainder_len, datap, 13723c57df7Smcpowers need); 13823c57df7Smcpowers } 13923c57df7Smcpowers } else { 14023c57df7Smcpowers crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1, 14123c57df7Smcpowers &out_data_1_len, &out_data_2, block_size); 14223c57df7Smcpowers 14323c57df7Smcpowers /* copy block to where it belongs */ 14423c57df7Smcpowers if (out_data_1_len == block_size) { 14523c57df7Smcpowers copy_block(lastp, out_data_1); 14623c57df7Smcpowers } else { 14723c57df7Smcpowers bcopy(lastp, out_data_1, out_data_1_len); 14823c57df7Smcpowers if (out_data_2 != NULL) { 14923c57df7Smcpowers bcopy(lastp + out_data_1_len, 15023c57df7Smcpowers out_data_2, 15123c57df7Smcpowers block_size - out_data_1_len); 15223c57df7Smcpowers } 15323c57df7Smcpowers } 15423c57df7Smcpowers /* update offset */ 15523c57df7Smcpowers out->cd_offset += block_size; 15623c57df7Smcpowers } 15723c57df7Smcpowers 15823c57df7Smcpowers /* Update pointer to next block of data to be processed. */ 15923c57df7Smcpowers if (ctx->ccm_remainder_len != 0) { 16023c57df7Smcpowers datap += need; 16123c57df7Smcpowers ctx->ccm_remainder_len = 0; 16223c57df7Smcpowers } else { 16323c57df7Smcpowers datap += block_size; 16423c57df7Smcpowers } 16523c57df7Smcpowers 16623c57df7Smcpowers remainder = (size_t)&data[length] - (size_t)datap; 16723c57df7Smcpowers 16823c57df7Smcpowers /* Incomplete last block. */ 16923c57df7Smcpowers if (remainder > 0 && remainder < block_size) { 17023c57df7Smcpowers bcopy(datap, ctx->ccm_remainder, remainder); 17123c57df7Smcpowers ctx->ccm_remainder_len = remainder; 17223c57df7Smcpowers ctx->ccm_copy_to = datap; 17323c57df7Smcpowers goto out; 17423c57df7Smcpowers } 17523c57df7Smcpowers ctx->ccm_copy_to = NULL; 17623c57df7Smcpowers 17723c57df7Smcpowers } while (remainder > 0); 17823c57df7Smcpowers 17923c57df7Smcpowers out: 18023c57df7Smcpowers return (CRYPTO_SUCCESS); 18123c57df7Smcpowers } 18223c57df7Smcpowers 18323c57df7Smcpowers void 18423c57df7Smcpowers calculate_ccm_mac(ccm_ctx_t *ctx, uint8_t *ccm_mac, 18523c57df7Smcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *)) 18623c57df7Smcpowers { 18723c57df7Smcpowers uint64_t counter; 18823c57df7Smcpowers uint8_t *counterp, *mac_buf; 18923c57df7Smcpowers int i; 19023c57df7Smcpowers 19123c57df7Smcpowers mac_buf = (uint8_t *)ctx->ccm_mac_buf; 19223c57df7Smcpowers 19323c57df7Smcpowers /* first counter block start with index 0 */ 19423c57df7Smcpowers counter = 0; 19523c57df7Smcpowers ctx->ccm_cb[1] = (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter; 19623c57df7Smcpowers 19723c57df7Smcpowers counterp = (uint8_t *)ctx->ccm_tmp; 19823c57df7Smcpowers encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, counterp); 19923c57df7Smcpowers 20023c57df7Smcpowers /* calculate XOR of MAC with first counter block */ 20123c57df7Smcpowers for (i = 0; i < ctx->ccm_mac_len; i++) { 20223c57df7Smcpowers ccm_mac[i] = mac_buf[i] ^ counterp[i]; 20323c57df7Smcpowers } 20423c57df7Smcpowers } 20523c57df7Smcpowers 20623c57df7Smcpowers /* ARGSUSED */ 20723c57df7Smcpowers int 20823c57df7Smcpowers ccm_encrypt_final(ccm_ctx_t *ctx, crypto_data_t *out, size_t block_size, 20923c57df7Smcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 21023c57df7Smcpowers void (*xor_block)(uint8_t *, uint8_t *)) 21123c57df7Smcpowers { 21223c57df7Smcpowers uint8_t *lastp, *mac_buf, *ccm_mac_p, *macp; 21323c57df7Smcpowers void *iov_or_mp; 21423c57df7Smcpowers offset_t offset; 21523c57df7Smcpowers uint8_t *out_data_1; 21623c57df7Smcpowers uint8_t *out_data_2; 21723c57df7Smcpowers size_t out_data_1_len; 21823c57df7Smcpowers int i; 21923c57df7Smcpowers 22023c57df7Smcpowers if (out->cd_length < (ctx->ccm_remainder_len + ctx->ccm_mac_len)) { 22123c57df7Smcpowers return (CRYPTO_DATA_LEN_RANGE); 22223c57df7Smcpowers } 22323c57df7Smcpowers 22423c57df7Smcpowers /* 22523c57df7Smcpowers * When we get here, the number of bytes of payload processed 22623c57df7Smcpowers * plus whatever data remains, if any, 22723c57df7Smcpowers * should be the same as the number of bytes that's being 22823c57df7Smcpowers * passed in the argument during init time. 22923c57df7Smcpowers */ 23023c57df7Smcpowers if ((ctx->ccm_processed_data_len + ctx->ccm_remainder_len) 23123c57df7Smcpowers != (ctx->ccm_data_len)) { 23223c57df7Smcpowers return (CRYPTO_DATA_LEN_RANGE); 23323c57df7Smcpowers } 23423c57df7Smcpowers 23523c57df7Smcpowers mac_buf = (uint8_t *)ctx->ccm_mac_buf; 23623c57df7Smcpowers 23723c57df7Smcpowers if (ctx->ccm_remainder_len > 0) { 23823c57df7Smcpowers 23923c57df7Smcpowers /* ccm_mac_input_buf is not used for encryption */ 24023c57df7Smcpowers macp = (uint8_t *)ctx->ccm_mac_input_buf; 24123c57df7Smcpowers bzero(macp, block_size); 24223c57df7Smcpowers 24323c57df7Smcpowers /* copy remainder to temporary buffer */ 24423c57df7Smcpowers bcopy(ctx->ccm_remainder, macp, ctx->ccm_remainder_len); 24523c57df7Smcpowers 24623c57df7Smcpowers /* calculate the CBC MAC */ 24723c57df7Smcpowers xor_block(macp, mac_buf); 24823c57df7Smcpowers encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf); 24923c57df7Smcpowers 25023c57df7Smcpowers /* calculate the counter mode */ 25123c57df7Smcpowers lastp = (uint8_t *)ctx->ccm_tmp; 25223c57df7Smcpowers encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, lastp); 25323c57df7Smcpowers 25423c57df7Smcpowers /* XOR with counter block */ 25523c57df7Smcpowers for (i = 0; i < ctx->ccm_remainder_len; i++) { 25623c57df7Smcpowers macp[i] ^= lastp[i]; 25723c57df7Smcpowers } 25823c57df7Smcpowers ctx->ccm_processed_data_len += ctx->ccm_remainder_len; 25923c57df7Smcpowers } 26023c57df7Smcpowers 26123c57df7Smcpowers /* Calculate the CCM MAC */ 26223c57df7Smcpowers ccm_mac_p = (uint8_t *)ctx->ccm_tmp; 26323c57df7Smcpowers calculate_ccm_mac(ctx, ccm_mac_p, encrypt_block); 26423c57df7Smcpowers 26523c57df7Smcpowers crypto_init_ptrs(out, &iov_or_mp, &offset); 26623c57df7Smcpowers crypto_get_ptrs(out, &iov_or_mp, &offset, &out_data_1, 26723c57df7Smcpowers &out_data_1_len, &out_data_2, 26823c57df7Smcpowers ctx->ccm_remainder_len + ctx->ccm_mac_len); 26923c57df7Smcpowers 27023c57df7Smcpowers if (ctx->ccm_remainder_len > 0) { 27123c57df7Smcpowers 27223c57df7Smcpowers /* copy temporary block to where it belongs */ 27323c57df7Smcpowers if (out_data_2 == NULL) { 27423c57df7Smcpowers /* everything will fit in out_data_1 */ 27523c57df7Smcpowers bcopy(macp, out_data_1, ctx->ccm_remainder_len); 27623c57df7Smcpowers bcopy(ccm_mac_p, out_data_1 + ctx->ccm_remainder_len, 27723c57df7Smcpowers ctx->ccm_mac_len); 27823c57df7Smcpowers } else { 27923c57df7Smcpowers 28023c57df7Smcpowers if (out_data_1_len < ctx->ccm_remainder_len) { 28123c57df7Smcpowers 28223c57df7Smcpowers size_t data_2_len_used; 28323c57df7Smcpowers 28423c57df7Smcpowers bcopy(macp, out_data_1, out_data_1_len); 28523c57df7Smcpowers 28623c57df7Smcpowers data_2_len_used = ctx->ccm_remainder_len 28723c57df7Smcpowers - out_data_1_len; 28823c57df7Smcpowers 28923c57df7Smcpowers bcopy((uint8_t *)macp + out_data_1_len, 29023c57df7Smcpowers out_data_2, data_2_len_used); 29123c57df7Smcpowers bcopy(ccm_mac_p, out_data_2 + data_2_len_used, 29223c57df7Smcpowers ctx->ccm_mac_len); 29323c57df7Smcpowers } else { 29423c57df7Smcpowers bcopy(macp, out_data_1, out_data_1_len); 29523c57df7Smcpowers if (out_data_1_len == ctx->ccm_remainder_len) { 29623c57df7Smcpowers /* mac will be in out_data_2 */ 29723c57df7Smcpowers bcopy(ccm_mac_p, out_data_2, 29823c57df7Smcpowers ctx->ccm_mac_len); 29923c57df7Smcpowers } else { 3004b56a003SDaniel Anderson size_t len_not_used = out_data_1_len - 30123c57df7Smcpowers ctx->ccm_remainder_len; 30223c57df7Smcpowers /* 30323c57df7Smcpowers * part of mac in will be in 30423c57df7Smcpowers * out_data_1, part of the mac will be 30523c57df7Smcpowers * in out_data_2 30623c57df7Smcpowers */ 30723c57df7Smcpowers bcopy(ccm_mac_p, 30823c57df7Smcpowers out_data_1 + ctx->ccm_remainder_len, 30923c57df7Smcpowers len_not_used); 31023c57df7Smcpowers bcopy(ccm_mac_p + len_not_used, 31123c57df7Smcpowers out_data_2, 31223c57df7Smcpowers ctx->ccm_mac_len - len_not_used); 31323c57df7Smcpowers 31423c57df7Smcpowers } 31523c57df7Smcpowers } 31623c57df7Smcpowers } 31723c57df7Smcpowers } else { 31823c57df7Smcpowers /* copy block to where it belongs */ 31923c57df7Smcpowers bcopy(ccm_mac_p, out_data_1, out_data_1_len); 32023c57df7Smcpowers if (out_data_2 != NULL) { 32123c57df7Smcpowers bcopy(ccm_mac_p + out_data_1_len, out_data_2, 32223c57df7Smcpowers block_size - out_data_1_len); 32323c57df7Smcpowers } 32423c57df7Smcpowers } 32523c57df7Smcpowers out->cd_offset += ctx->ccm_remainder_len + ctx->ccm_mac_len; 32623c57df7Smcpowers ctx->ccm_remainder_len = 0; 32723c57df7Smcpowers return (CRYPTO_SUCCESS); 32823c57df7Smcpowers } 32923c57df7Smcpowers 33023c57df7Smcpowers /* 33123c57df7Smcpowers * This will only deal with decrypting the last block of the input that 33223c57df7Smcpowers * might not be a multiple of block length. 33323c57df7Smcpowers */ 33423c57df7Smcpowers void 33523c57df7Smcpowers ccm_decrypt_incomplete_block(ccm_ctx_t *ctx, 33623c57df7Smcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *)) 33723c57df7Smcpowers { 33823c57df7Smcpowers uint8_t *datap, *outp, *counterp; 33923c57df7Smcpowers int i; 34023c57df7Smcpowers 34123c57df7Smcpowers datap = (uint8_t *)ctx->ccm_remainder; 34223c57df7Smcpowers outp = &((ctx->ccm_pt_buf)[ctx->ccm_processed_data_len]); 34323c57df7Smcpowers 34423c57df7Smcpowers counterp = (uint8_t *)ctx->ccm_tmp; 34523c57df7Smcpowers encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, counterp); 34623c57df7Smcpowers 34723c57df7Smcpowers /* XOR with counter block */ 34823c57df7Smcpowers for (i = 0; i < ctx->ccm_remainder_len; i++) { 34923c57df7Smcpowers outp[i] = datap[i] ^ counterp[i]; 35023c57df7Smcpowers } 35123c57df7Smcpowers } 35223c57df7Smcpowers 35323c57df7Smcpowers /* 35423c57df7Smcpowers * This will decrypt the cipher text. However, the plaintext won't be 35523c57df7Smcpowers * returned to the caller. It will be returned when decrypt_final() is 35623c57df7Smcpowers * called if the MAC matches 35723c57df7Smcpowers */ 35823c57df7Smcpowers /* ARGSUSED */ 35923c57df7Smcpowers int 36023c57df7Smcpowers ccm_mode_decrypt_contiguous_blocks(ccm_ctx_t *ctx, char *data, size_t length, 36123c57df7Smcpowers crypto_data_t *out, size_t block_size, 36223c57df7Smcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 36323c57df7Smcpowers void (*copy_block)(uint8_t *, uint8_t *), 36423c57df7Smcpowers void (*xor_block)(uint8_t *, uint8_t *)) 36523c57df7Smcpowers { 36623c57df7Smcpowers size_t remainder = length; 36723c57df7Smcpowers size_t need; 36823c57df7Smcpowers uint8_t *datap = (uint8_t *)data; 36923c57df7Smcpowers uint8_t *blockp; 37023c57df7Smcpowers uint8_t *cbp; 37123c57df7Smcpowers uint64_t counter; 37223c57df7Smcpowers size_t pt_len, total_decrypted_len, mac_len, pm_len, pd_len; 37323c57df7Smcpowers uint8_t *resultp; 37423c57df7Smcpowers 37523c57df7Smcpowers 37623c57df7Smcpowers pm_len = ctx->ccm_processed_mac_len; 37723c57df7Smcpowers 37823c57df7Smcpowers if (pm_len > 0) { 37923c57df7Smcpowers uint8_t *tmp; 38023c57df7Smcpowers /* 38123c57df7Smcpowers * all ciphertext has been processed, just waiting for 38223c57df7Smcpowers * part of the value of the mac 38323c57df7Smcpowers */ 38423c57df7Smcpowers if ((pm_len + length) > ctx->ccm_mac_len) { 38523c57df7Smcpowers return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE); 38623c57df7Smcpowers } 38723c57df7Smcpowers tmp = (uint8_t *)ctx->ccm_mac_input_buf; 38823c57df7Smcpowers 38923c57df7Smcpowers bcopy(datap, tmp + pm_len, length); 39023c57df7Smcpowers 39123c57df7Smcpowers ctx->ccm_processed_mac_len += length; 39223c57df7Smcpowers return (CRYPTO_SUCCESS); 39323c57df7Smcpowers } 39423c57df7Smcpowers 39523c57df7Smcpowers /* 39623c57df7Smcpowers * If we decrypt the given data, what total amount of data would 39723c57df7Smcpowers * have been decrypted? 39823c57df7Smcpowers */ 39923c57df7Smcpowers pd_len = ctx->ccm_processed_data_len; 40023c57df7Smcpowers total_decrypted_len = pd_len + length + ctx->ccm_remainder_len; 40123c57df7Smcpowers 40223c57df7Smcpowers if (total_decrypted_len > 40323c57df7Smcpowers (ctx->ccm_data_len + ctx->ccm_mac_len)) { 40423c57df7Smcpowers return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE); 40523c57df7Smcpowers } 40623c57df7Smcpowers 40723c57df7Smcpowers pt_len = ctx->ccm_data_len; 40823c57df7Smcpowers 40923c57df7Smcpowers if (total_decrypted_len > pt_len) { 41023c57df7Smcpowers /* 41123c57df7Smcpowers * part of the input will be the MAC, need to isolate that 41223c57df7Smcpowers * to be dealt with later. The left-over data in 41323c57df7Smcpowers * ccm_remainder_len from last time will not be part of the 41423c57df7Smcpowers * MAC. Otherwise, it would have already been taken out 41523c57df7Smcpowers * when this call is made last time. 41623c57df7Smcpowers */ 41723c57df7Smcpowers size_t pt_part = pt_len - pd_len - ctx->ccm_remainder_len; 41823c57df7Smcpowers 41923c57df7Smcpowers mac_len = length - pt_part; 42023c57df7Smcpowers 42123c57df7Smcpowers ctx->ccm_processed_mac_len = mac_len; 42223c57df7Smcpowers bcopy(data + pt_part, ctx->ccm_mac_input_buf, mac_len); 42323c57df7Smcpowers 42423c57df7Smcpowers if (pt_part + ctx->ccm_remainder_len < block_size) { 42523c57df7Smcpowers /* 42623c57df7Smcpowers * since this is last of the ciphertext, will 42723c57df7Smcpowers * just decrypt with it here 42823c57df7Smcpowers */ 42923c57df7Smcpowers bcopy(datap, &((uint8_t *)ctx->ccm_remainder) 43023c57df7Smcpowers [ctx->ccm_remainder_len], pt_part); 43123c57df7Smcpowers ctx->ccm_remainder_len += pt_part; 43223c57df7Smcpowers ccm_decrypt_incomplete_block(ctx, encrypt_block); 43323c57df7Smcpowers ctx->ccm_remainder_len = 0; 43423c57df7Smcpowers ctx->ccm_processed_data_len += pt_part; 43523c57df7Smcpowers return (CRYPTO_SUCCESS); 43623c57df7Smcpowers } else { 43723c57df7Smcpowers /* let rest of the code handle this */ 43823c57df7Smcpowers length = pt_part; 43923c57df7Smcpowers } 44023c57df7Smcpowers } else if (length + ctx->ccm_remainder_len < block_size) { 44123c57df7Smcpowers /* accumulate bytes here and return */ 44223c57df7Smcpowers bcopy(datap, 44323c57df7Smcpowers (uint8_t *)ctx->ccm_remainder + ctx->ccm_remainder_len, 44423c57df7Smcpowers length); 44523c57df7Smcpowers ctx->ccm_remainder_len += length; 44623c57df7Smcpowers ctx->ccm_copy_to = datap; 44723c57df7Smcpowers return (CRYPTO_SUCCESS); 44823c57df7Smcpowers } 44923c57df7Smcpowers 45023c57df7Smcpowers do { 45123c57df7Smcpowers /* Unprocessed data from last call. */ 45223c57df7Smcpowers if (ctx->ccm_remainder_len > 0) { 45323c57df7Smcpowers need = block_size - ctx->ccm_remainder_len; 45423c57df7Smcpowers 45523c57df7Smcpowers if (need > remainder) 45623c57df7Smcpowers return (CRYPTO_ENCRYPTED_DATA_LEN_RANGE); 45723c57df7Smcpowers 45823c57df7Smcpowers bcopy(datap, &((uint8_t *)ctx->ccm_remainder) 45923c57df7Smcpowers [ctx->ccm_remainder_len], need); 46023c57df7Smcpowers 46123c57df7Smcpowers blockp = (uint8_t *)ctx->ccm_remainder; 46223c57df7Smcpowers } else { 46323c57df7Smcpowers blockp = datap; 46423c57df7Smcpowers } 46523c57df7Smcpowers 46623c57df7Smcpowers /* Calculate the counter mode, ccm_cb is the counter block */ 46723c57df7Smcpowers cbp = (uint8_t *)ctx->ccm_tmp; 46823c57df7Smcpowers encrypt_block(ctx->ccm_keysched, (uint8_t *)ctx->ccm_cb, cbp); 46923c57df7Smcpowers 47023c57df7Smcpowers /* 47123c57df7Smcpowers * Increment counter. 47223c57df7Smcpowers * Counter bits are confined to the bottom 64 bits 47323c57df7Smcpowers */ 4744b56a003SDaniel Anderson counter = ntohll(ctx->ccm_cb[1] & ctx->ccm_counter_mask); 4754b56a003SDaniel Anderson counter = htonll(counter + 1); 47623c57df7Smcpowers counter &= ctx->ccm_counter_mask; 47723c57df7Smcpowers ctx->ccm_cb[1] = 47823c57df7Smcpowers (ctx->ccm_cb[1] & ~(ctx->ccm_counter_mask)) | counter; 47923c57df7Smcpowers 48023c57df7Smcpowers /* XOR with the ciphertext */ 48123c57df7Smcpowers xor_block(blockp, cbp); 48223c57df7Smcpowers 48323c57df7Smcpowers /* Copy the plaintext to the "holding buffer" */ 48423c57df7Smcpowers resultp = (uint8_t *)ctx->ccm_pt_buf + 48523c57df7Smcpowers ctx->ccm_processed_data_len; 48623c57df7Smcpowers copy_block(cbp, resultp); 48723c57df7Smcpowers 48823c57df7Smcpowers ctx->ccm_processed_data_len += block_size; 48923c57df7Smcpowers 49023c57df7Smcpowers ctx->ccm_lastp = blockp; 49123c57df7Smcpowers 49223c57df7Smcpowers /* Update pointer to next block of data to be processed. */ 49323c57df7Smcpowers if (ctx->ccm_remainder_len != 0) { 49423c57df7Smcpowers datap += need; 49523c57df7Smcpowers ctx->ccm_remainder_len = 0; 49623c57df7Smcpowers } else { 49723c57df7Smcpowers datap += block_size; 49823c57df7Smcpowers } 49923c57df7Smcpowers 50023c57df7Smcpowers remainder = (size_t)&data[length] - (size_t)datap; 50123c57df7Smcpowers 50223c57df7Smcpowers /* Incomplete last block */ 50323c57df7Smcpowers if (remainder > 0 && remainder < block_size) { 50423c57df7Smcpowers bcopy(datap, ctx->ccm_remainder, remainder); 50523c57df7Smcpowers ctx->ccm_remainder_len = remainder; 50623c57df7Smcpowers ctx->ccm_copy_to = datap; 50723c57df7Smcpowers if (ctx->ccm_processed_mac_len > 0) { 50823c57df7Smcpowers /* 50923c57df7Smcpowers * not expecting anymore ciphertext, just 51023c57df7Smcpowers * compute plaintext for the remaining input 51123c57df7Smcpowers */ 51223c57df7Smcpowers ccm_decrypt_incomplete_block(ctx, 51323c57df7Smcpowers encrypt_block); 51423c57df7Smcpowers ctx->ccm_processed_data_len += remainder; 51523c57df7Smcpowers ctx->ccm_remainder_len = 0; 51623c57df7Smcpowers } 51723c57df7Smcpowers goto out; 51823c57df7Smcpowers } 51923c57df7Smcpowers ctx->ccm_copy_to = NULL; 52023c57df7Smcpowers 52123c57df7Smcpowers } while (remainder > 0); 52223c57df7Smcpowers 52323c57df7Smcpowers out: 52423c57df7Smcpowers return (CRYPTO_SUCCESS); 52523c57df7Smcpowers } 52623c57df7Smcpowers 52723c57df7Smcpowers int 52823c57df7Smcpowers ccm_decrypt_final(ccm_ctx_t *ctx, crypto_data_t *out, size_t block_size, 52923c57df7Smcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 53023c57df7Smcpowers void (*copy_block)(uint8_t *, uint8_t *), 53123c57df7Smcpowers void (*xor_block)(uint8_t *, uint8_t *)) 53223c57df7Smcpowers { 53323c57df7Smcpowers size_t mac_remain, pt_len; 53423c57df7Smcpowers uint8_t *pt, *mac_buf, *macp, *ccm_mac_p; 5357e0cc741SMark Powers int rv; 53623c57df7Smcpowers 53723c57df7Smcpowers pt_len = ctx->ccm_data_len; 53823c57df7Smcpowers 53923c57df7Smcpowers /* Make sure output buffer can fit all of the plaintext */ 54023c57df7Smcpowers if (out->cd_length < pt_len) { 54123c57df7Smcpowers return (CRYPTO_DATA_LEN_RANGE); 54223c57df7Smcpowers } 54323c57df7Smcpowers 54423c57df7Smcpowers pt = ctx->ccm_pt_buf; 54523c57df7Smcpowers mac_remain = ctx->ccm_processed_data_len; 54623c57df7Smcpowers mac_buf = (uint8_t *)ctx->ccm_mac_buf; 54723c57df7Smcpowers 54823c57df7Smcpowers macp = (uint8_t *)ctx->ccm_tmp; 54923c57df7Smcpowers 55023c57df7Smcpowers while (mac_remain > 0) { 55123c57df7Smcpowers 55223c57df7Smcpowers if (mac_remain < block_size) { 55323c57df7Smcpowers bzero(macp, block_size); 55423c57df7Smcpowers bcopy(pt, macp, mac_remain); 55523c57df7Smcpowers mac_remain = 0; 55623c57df7Smcpowers } else { 55723c57df7Smcpowers copy_block(pt, macp); 55823c57df7Smcpowers mac_remain -= block_size; 55923c57df7Smcpowers pt += block_size; 56023c57df7Smcpowers } 56123c57df7Smcpowers 56223c57df7Smcpowers /* calculate the CBC MAC */ 56323c57df7Smcpowers xor_block(macp, mac_buf); 56423c57df7Smcpowers encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf); 56523c57df7Smcpowers } 56623c57df7Smcpowers 56723c57df7Smcpowers /* Calculate the CCM MAC */ 56823c57df7Smcpowers ccm_mac_p = (uint8_t *)ctx->ccm_tmp; 56923c57df7Smcpowers calculate_ccm_mac((ccm_ctx_t *)ctx, ccm_mac_p, encrypt_block); 57023c57df7Smcpowers 57123c57df7Smcpowers /* compare the input CCM MAC value with what we calculated */ 57223c57df7Smcpowers if (bcmp(ctx->ccm_mac_input_buf, ccm_mac_p, ctx->ccm_mac_len)) { 57323c57df7Smcpowers /* They don't match */ 57423c57df7Smcpowers return (CRYPTO_INVALID_MAC); 57523c57df7Smcpowers } else { 5767e0cc741SMark Powers rv = crypto_put_output_data(ctx->ccm_pt_buf, out, pt_len); 5777e0cc741SMark Powers if (rv != CRYPTO_SUCCESS) 5787e0cc741SMark Powers return (rv); 57923c57df7Smcpowers out->cd_offset += pt_len; 58023c57df7Smcpowers } 58123c57df7Smcpowers return (CRYPTO_SUCCESS); 58223c57df7Smcpowers } 58323c57df7Smcpowers 58423c57df7Smcpowers int 58523c57df7Smcpowers ccm_validate_args(CK_AES_CCM_PARAMS *ccm_param, boolean_t is_encrypt_init) 58623c57df7Smcpowers { 58723c57df7Smcpowers size_t macSize, nonceSize; 58823c57df7Smcpowers uint8_t q; 58923c57df7Smcpowers uint64_t maxValue; 59023c57df7Smcpowers 59123c57df7Smcpowers /* 59223c57df7Smcpowers * Check the length of the MAC. The only valid 59323c57df7Smcpowers * lengths for the MAC are: 4, 6, 8, 10, 12, 14, 16 59423c57df7Smcpowers */ 59523c57df7Smcpowers macSize = ccm_param->ulMACSize; 59623c57df7Smcpowers if ((macSize < 4) || (macSize > 16) || ((macSize % 2) != 0)) { 59723c57df7Smcpowers return (CRYPTO_MECHANISM_PARAM_INVALID); 59823c57df7Smcpowers } 59923c57df7Smcpowers 60023c57df7Smcpowers /* Check the nonce length. Valid values are 7, 8, 9, 10, 11, 12, 13 */ 60123c57df7Smcpowers nonceSize = ccm_param->ulNonceSize; 60223c57df7Smcpowers if ((nonceSize < 7) || (nonceSize > 13)) { 60323c57df7Smcpowers return (CRYPTO_MECHANISM_PARAM_INVALID); 60423c57df7Smcpowers } 60523c57df7Smcpowers 60623c57df7Smcpowers /* q is the length of the field storing the length, in bytes */ 60723c57df7Smcpowers q = (uint8_t)((15 - nonceSize) & 0xFF); 60823c57df7Smcpowers 60923c57df7Smcpowers 61023c57df7Smcpowers /* 61123c57df7Smcpowers * If it is decrypt, need to make sure size of ciphertext is at least 61223c57df7Smcpowers * bigger than MAC len 61323c57df7Smcpowers */ 61423c57df7Smcpowers if ((!is_encrypt_init) && (ccm_param->ulDataSize < macSize)) { 61523c57df7Smcpowers return (CRYPTO_MECHANISM_PARAM_INVALID); 61623c57df7Smcpowers } 61723c57df7Smcpowers 61823c57df7Smcpowers /* 61923c57df7Smcpowers * Check to make sure the length of the payload is within the 62023c57df7Smcpowers * range of values allowed by q 62123c57df7Smcpowers */ 62223c57df7Smcpowers if (q < 8) { 62323c57df7Smcpowers maxValue = (1ULL << (q * 8)) - 1; 62423c57df7Smcpowers } else { 62523c57df7Smcpowers maxValue = ULONG_MAX; 62623c57df7Smcpowers } 62723c57df7Smcpowers 62823c57df7Smcpowers if (ccm_param->ulDataSize > maxValue) { 62923c57df7Smcpowers return (CRYPTO_MECHANISM_PARAM_INVALID); 63023c57df7Smcpowers } 63123c57df7Smcpowers return (CRYPTO_SUCCESS); 63223c57df7Smcpowers } 63323c57df7Smcpowers 63423c57df7Smcpowers /* 63523c57df7Smcpowers * Format the first block used in CBC-MAC (B0) and the initial counter 63623c57df7Smcpowers * block based on formatting functions and counter generation functions 63723c57df7Smcpowers * specified in RFC 3610 and NIST publication 800-38C, appendix A 63823c57df7Smcpowers * 63923c57df7Smcpowers * b0 is the first block used in CBC-MAC 64023c57df7Smcpowers * cb0 is the first counter block 64123c57df7Smcpowers * 64223c57df7Smcpowers * It's assumed that the arguments b0 and cb0 are preallocated AES blocks 64323c57df7Smcpowers * 64423c57df7Smcpowers */ 64523c57df7Smcpowers static void 64623c57df7Smcpowers ccm_format_initial_blocks(uchar_t *nonce, ulong_t nonceSize, 64723c57df7Smcpowers ulong_t authDataSize, uint8_t *b0, ccm_ctx_t *aes_ctx) 64823c57df7Smcpowers { 64923c57df7Smcpowers uint64_t payloadSize; 65023c57df7Smcpowers uint8_t t, q, have_adata = 0; 65123c57df7Smcpowers size_t limit; 65223c57df7Smcpowers int i, j, k; 65323c57df7Smcpowers uint64_t mask = 0; 65423c57df7Smcpowers uint8_t *cb; 65523c57df7Smcpowers 65623c57df7Smcpowers q = (uint8_t)((15 - nonceSize) & 0xFF); 65723c57df7Smcpowers t = (uint8_t)((aes_ctx->ccm_mac_len) & 0xFF); 65823c57df7Smcpowers 65923c57df7Smcpowers /* Construct the first octet of b0 */ 66023c57df7Smcpowers if (authDataSize > 0) { 66123c57df7Smcpowers have_adata = 1; 66223c57df7Smcpowers } 66323c57df7Smcpowers b0[0] = (have_adata << 6) | (((t - 2) / 2) << 3) | (q - 1); 66423c57df7Smcpowers 66523c57df7Smcpowers /* copy the nonce value into b0 */ 66623c57df7Smcpowers bcopy(nonce, &(b0[1]), nonceSize); 66723c57df7Smcpowers 66823c57df7Smcpowers /* store the length of the payload into b0 */ 66923c57df7Smcpowers bzero(&(b0[1+nonceSize]), q); 67023c57df7Smcpowers 67123c57df7Smcpowers payloadSize = aes_ctx->ccm_data_len; 67223c57df7Smcpowers limit = 8 < q ? 8 : q; 67323c57df7Smcpowers 67423c57df7Smcpowers for (i = 0, j = 0, k = 15; i < limit; i++, j += 8, k--) { 67523c57df7Smcpowers b0[k] = (uint8_t)((payloadSize >> j) & 0xFF); 67623c57df7Smcpowers } 67723c57df7Smcpowers 67823c57df7Smcpowers /* format the counter block */ 67923c57df7Smcpowers 68023c57df7Smcpowers cb = (uint8_t *)aes_ctx->ccm_cb; 68123c57df7Smcpowers 68223c57df7Smcpowers cb[0] = 0x07 & (q-1); /* first byte */ 68323c57df7Smcpowers 68423c57df7Smcpowers /* copy the nonce value into the counter block */ 68523c57df7Smcpowers bcopy(nonce, &(cb[1]), nonceSize); 68623c57df7Smcpowers 68723c57df7Smcpowers bzero(&(cb[1+nonceSize]), q); 68823c57df7Smcpowers 68923c57df7Smcpowers /* Create the mask for the counter field based on the size of nonce */ 69023c57df7Smcpowers q <<= 3; 69123c57df7Smcpowers while (q-- > 0) { 69223c57df7Smcpowers mask |= (1ULL << q); 69323c57df7Smcpowers } 69423c57df7Smcpowers 695*54034eb2SDan OpenSolaris Anderson aes_ctx->ccm_counter_mask = htonll(mask); 69623c57df7Smcpowers 69723c57df7Smcpowers /* 69823c57df7Smcpowers * During calculation, we start using counter block 1, we will 69923c57df7Smcpowers * set it up right here. 70023c57df7Smcpowers * We can just set the last byte to have the value 1, because 70123c57df7Smcpowers * even with the biggest nonce of 13, the last byte of the 70223c57df7Smcpowers * counter block will be used for the counter value. 70323c57df7Smcpowers */ 70423c57df7Smcpowers cb[15] = 0x01; 70523c57df7Smcpowers } 70623c57df7Smcpowers 70723c57df7Smcpowers /* 70823c57df7Smcpowers * Encode the length of the associated data as 70923c57df7Smcpowers * specified in RFC 3610 and NIST publication 800-38C, appendix A 71023c57df7Smcpowers */ 71123c57df7Smcpowers static void 71223c57df7Smcpowers encode_adata_len(ulong_t auth_data_len, uint8_t *encoded, size_t *encoded_len) 71323c57df7Smcpowers { 7144b56a003SDaniel Anderson #ifdef UNALIGNED_POINTERS_PERMITTED 7154b56a003SDaniel Anderson uint32_t *lencoded_ptr; 7164b56a003SDaniel Anderson #ifdef _LP64 7174b56a003SDaniel Anderson uint64_t *llencoded_ptr; 7184b56a003SDaniel Anderson #endif 7194b56a003SDaniel Anderson #endif /* UNALIGNED_POINTERS_PERMITTED */ 7204b56a003SDaniel Anderson 72123c57df7Smcpowers if (auth_data_len < ((1ULL<<16) - (1ULL<<8))) { 72223c57df7Smcpowers /* 0 < a < (2^16-2^8) */ 72323c57df7Smcpowers *encoded_len = 2; 72423c57df7Smcpowers encoded[0] = (auth_data_len & 0xff00) >> 8; 72523c57df7Smcpowers encoded[1] = auth_data_len & 0xff; 72623c57df7Smcpowers 72723c57df7Smcpowers } else if ((auth_data_len >= ((1ULL<<16) - (1ULL<<8))) && 72823c57df7Smcpowers (auth_data_len < (1ULL << 31))) { 72923c57df7Smcpowers /* (2^16-2^8) <= a < 2^32 */ 73023c57df7Smcpowers *encoded_len = 6; 73123c57df7Smcpowers encoded[0] = 0xff; 73223c57df7Smcpowers encoded[1] = 0xfe; 7334b56a003SDaniel Anderson #ifdef UNALIGNED_POINTERS_PERMITTED 7344b56a003SDaniel Anderson lencoded_ptr = (uint32_t *)&encoded[2]; 7354b56a003SDaniel Anderson *lencoded_ptr = htonl(auth_data_len); 7364b56a003SDaniel Anderson #else 73723c57df7Smcpowers encoded[2] = (auth_data_len & 0xff000000) >> 24; 73823c57df7Smcpowers encoded[3] = (auth_data_len & 0xff0000) >> 16; 73923c57df7Smcpowers encoded[4] = (auth_data_len & 0xff00) >> 8; 74023c57df7Smcpowers encoded[5] = auth_data_len & 0xff; 7414b56a003SDaniel Anderson #endif /* UNALIGNED_POINTERS_PERMITTED */ 7424b56a003SDaniel Anderson 74323c57df7Smcpowers #ifdef _LP64 74423c57df7Smcpowers } else { 74523c57df7Smcpowers /* 2^32 <= a < 2^64 */ 74623c57df7Smcpowers *encoded_len = 10; 74723c57df7Smcpowers encoded[0] = 0xff; 74823c57df7Smcpowers encoded[1] = 0xff; 7494b56a003SDaniel Anderson #ifdef UNALIGNED_POINTERS_PERMITTED 7504b56a003SDaniel Anderson llencoded_ptr = (uint64_t *)&encoded[2]; 7514b56a003SDaniel Anderson *llencoded_ptr = htonl(auth_data_len); 7524b56a003SDaniel Anderson #else 75323c57df7Smcpowers encoded[2] = (auth_data_len & 0xff00000000000000) >> 56; 75423c57df7Smcpowers encoded[3] = (auth_data_len & 0xff000000000000) >> 48; 75523c57df7Smcpowers encoded[4] = (auth_data_len & 0xff0000000000) >> 40; 75623c57df7Smcpowers encoded[5] = (auth_data_len & 0xff00000000) >> 32; 75723c57df7Smcpowers encoded[6] = (auth_data_len & 0xff000000) >> 24; 75823c57df7Smcpowers encoded[7] = (auth_data_len & 0xff0000) >> 16; 75923c57df7Smcpowers encoded[8] = (auth_data_len & 0xff00) >> 8; 76023c57df7Smcpowers encoded[9] = auth_data_len & 0xff; 7614b56a003SDaniel Anderson #endif /* UNALIGNED_POINTERS_PERMITTED */ 76223c57df7Smcpowers #endif /* _LP64 */ 76323c57df7Smcpowers } 76423c57df7Smcpowers } 76523c57df7Smcpowers 76623c57df7Smcpowers /* 76723c57df7Smcpowers * The following function should be call at encrypt or decrypt init time 76823c57df7Smcpowers * for AES CCM mode. 76923c57df7Smcpowers */ 77023c57df7Smcpowers int 77123c57df7Smcpowers ccm_init(ccm_ctx_t *ctx, unsigned char *nonce, size_t nonce_len, 77223c57df7Smcpowers unsigned char *auth_data, size_t auth_data_len, size_t block_size, 77323c57df7Smcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 77423c57df7Smcpowers void (*xor_block)(uint8_t *, uint8_t *)) 77523c57df7Smcpowers { 77623c57df7Smcpowers uint8_t *mac_buf, *datap, *ivp, *authp; 77723c57df7Smcpowers size_t remainder, processed; 77823c57df7Smcpowers uint8_t encoded_a[10]; /* max encoded auth data length is 10 octets */ 77923c57df7Smcpowers size_t encoded_a_len = 0; 78023c57df7Smcpowers 78123c57df7Smcpowers mac_buf = (uint8_t *)&(ctx->ccm_mac_buf); 78223c57df7Smcpowers 78323c57df7Smcpowers /* 78423c57df7Smcpowers * Format the 1st block for CBC-MAC and construct the 78523c57df7Smcpowers * 1st counter block. 78623c57df7Smcpowers * 78723c57df7Smcpowers * aes_ctx->ccm_iv is used for storing the counter block 78823c57df7Smcpowers * mac_buf will store b0 at this time. 78923c57df7Smcpowers */ 79023c57df7Smcpowers ccm_format_initial_blocks(nonce, nonce_len, 79123c57df7Smcpowers auth_data_len, mac_buf, ctx); 79223c57df7Smcpowers 79323c57df7Smcpowers /* The IV for CBC MAC for AES CCM mode is always zero */ 79423c57df7Smcpowers ivp = (uint8_t *)ctx->ccm_tmp; 79523c57df7Smcpowers bzero(ivp, block_size); 79623c57df7Smcpowers 79723c57df7Smcpowers xor_block(ivp, mac_buf); 79823c57df7Smcpowers 79923c57df7Smcpowers /* encrypt the nonce */ 80023c57df7Smcpowers encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf); 80123c57df7Smcpowers 80223c57df7Smcpowers /* take care of the associated data, if any */ 80323c57df7Smcpowers if (auth_data_len == 0) { 80423c57df7Smcpowers return (CRYPTO_SUCCESS); 80523c57df7Smcpowers } 80623c57df7Smcpowers 80723c57df7Smcpowers encode_adata_len(auth_data_len, encoded_a, &encoded_a_len); 80823c57df7Smcpowers 80923c57df7Smcpowers remainder = auth_data_len; 81023c57df7Smcpowers 81123c57df7Smcpowers /* 1st block: it contains encoded associated data, and some data */ 81223c57df7Smcpowers authp = (uint8_t *)ctx->ccm_tmp; 81323c57df7Smcpowers bzero(authp, block_size); 81423c57df7Smcpowers bcopy(encoded_a, authp, encoded_a_len); 81523c57df7Smcpowers processed = block_size - encoded_a_len; 81623c57df7Smcpowers if (processed > auth_data_len) { 81723c57df7Smcpowers /* in case auth_data is very small */ 81823c57df7Smcpowers processed = auth_data_len; 81923c57df7Smcpowers } 82023c57df7Smcpowers bcopy(auth_data, authp+encoded_a_len, processed); 82123c57df7Smcpowers /* xor with previous buffer */ 82223c57df7Smcpowers xor_block(authp, mac_buf); 82323c57df7Smcpowers encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf); 82423c57df7Smcpowers remainder -= processed; 82523c57df7Smcpowers if (remainder == 0) { 82623c57df7Smcpowers /* a small amount of associated data, it's all done now */ 82723c57df7Smcpowers return (CRYPTO_SUCCESS); 82823c57df7Smcpowers } 82923c57df7Smcpowers 83023c57df7Smcpowers do { 83123c57df7Smcpowers if (remainder < block_size) { 83223c57df7Smcpowers /* 83323c57df7Smcpowers * There's not a block full of data, pad rest of 83423c57df7Smcpowers * buffer with zero 83523c57df7Smcpowers */ 83623c57df7Smcpowers bzero(authp, block_size); 83723c57df7Smcpowers bcopy(&(auth_data[processed]), authp, remainder); 83823c57df7Smcpowers datap = (uint8_t *)authp; 83923c57df7Smcpowers remainder = 0; 84023c57df7Smcpowers } else { 84123c57df7Smcpowers datap = (uint8_t *)(&(auth_data[processed])); 84223c57df7Smcpowers processed += block_size; 84323c57df7Smcpowers remainder -= block_size; 84423c57df7Smcpowers } 84523c57df7Smcpowers 84623c57df7Smcpowers xor_block(datap, mac_buf); 84723c57df7Smcpowers encrypt_block(ctx->ccm_keysched, mac_buf, mac_buf); 84823c57df7Smcpowers 84923c57df7Smcpowers } while (remainder > 0); 85023c57df7Smcpowers 85123c57df7Smcpowers return (CRYPTO_SUCCESS); 85223c57df7Smcpowers } 85323c57df7Smcpowers 85423c57df7Smcpowers int 85523c57df7Smcpowers ccm_init_ctx(ccm_ctx_t *ccm_ctx, char *param, int kmflag, 85623c57df7Smcpowers boolean_t is_encrypt_init, size_t block_size, 85723c57df7Smcpowers int (*encrypt_block)(const void *, const uint8_t *, uint8_t *), 85823c57df7Smcpowers void (*xor_block)(uint8_t *, uint8_t *)) 85923c57df7Smcpowers { 86023c57df7Smcpowers int rv; 86123c57df7Smcpowers CK_AES_CCM_PARAMS *ccm_param; 86223c57df7Smcpowers 86323c57df7Smcpowers if (param != NULL) { 86423c57df7Smcpowers ccm_param = (CK_AES_CCM_PARAMS *)param; 86523c57df7Smcpowers 86623c57df7Smcpowers if ((rv = ccm_validate_args(ccm_param, 86723c57df7Smcpowers is_encrypt_init)) != 0) { 86823c57df7Smcpowers return (rv); 86923c57df7Smcpowers } 87023c57df7Smcpowers 87123c57df7Smcpowers ccm_ctx->ccm_mac_len = ccm_param->ulMACSize; 87223c57df7Smcpowers if (is_encrypt_init) { 87323c57df7Smcpowers ccm_ctx->ccm_data_len = ccm_param->ulDataSize; 87423c57df7Smcpowers } else { 87523c57df7Smcpowers ccm_ctx->ccm_data_len = 87623c57df7Smcpowers ccm_param->ulDataSize - ccm_ctx->ccm_mac_len; 87723c57df7Smcpowers ccm_ctx->ccm_processed_mac_len = 0; 87823c57df7Smcpowers } 87923c57df7Smcpowers ccm_ctx->ccm_processed_data_len = 0; 88023c57df7Smcpowers 88123c57df7Smcpowers ccm_ctx->ccm_flags |= CCM_MODE; 88223c57df7Smcpowers } else { 88323c57df7Smcpowers rv = CRYPTO_MECHANISM_PARAM_INVALID; 88423c57df7Smcpowers goto out; 88523c57df7Smcpowers } 88623c57df7Smcpowers 88723c57df7Smcpowers if (ccm_init(ccm_ctx, ccm_param->nonce, ccm_param->ulNonceSize, 88823c57df7Smcpowers ccm_param->authData, ccm_param->ulAuthDataSize, block_size, 88923c57df7Smcpowers encrypt_block, xor_block) != 0) { 89023c57df7Smcpowers rv = CRYPTO_MECHANISM_PARAM_INVALID; 89123c57df7Smcpowers goto out; 89223c57df7Smcpowers } 89323c57df7Smcpowers if (!is_encrypt_init) { 89423c57df7Smcpowers /* allocate buffer for storing decrypted plaintext */ 89523c57df7Smcpowers #ifdef _KERNEL 89623c57df7Smcpowers ccm_ctx->ccm_pt_buf = kmem_alloc(ccm_ctx->ccm_data_len, 89723c57df7Smcpowers kmflag); 89823c57df7Smcpowers #else 89923c57df7Smcpowers ccm_ctx->ccm_pt_buf = malloc(ccm_ctx->ccm_data_len); 90023c57df7Smcpowers #endif 90123c57df7Smcpowers if (ccm_ctx->ccm_pt_buf == NULL) { 90223c57df7Smcpowers rv = CRYPTO_HOST_MEMORY; 90323c57df7Smcpowers } 90423c57df7Smcpowers } 90523c57df7Smcpowers out: 90623c57df7Smcpowers return (rv); 90723c57df7Smcpowers } 90823c57df7Smcpowers 90923c57df7Smcpowers void * 91023c57df7Smcpowers ccm_alloc_ctx(int kmflag) 91123c57df7Smcpowers { 91223c57df7Smcpowers ccm_ctx_t *ccm_ctx; 91323c57df7Smcpowers 91423c57df7Smcpowers #ifdef _KERNEL 91523c57df7Smcpowers if ((ccm_ctx = kmem_zalloc(sizeof (ccm_ctx_t), kmflag)) == NULL) 91623c57df7Smcpowers #else 91723c57df7Smcpowers if ((ccm_ctx = calloc(1, sizeof (ccm_ctx_t))) == NULL) 91823c57df7Smcpowers #endif 91923c57df7Smcpowers return (NULL); 92023c57df7Smcpowers 92123c57df7Smcpowers ccm_ctx->ccm_flags = CCM_MODE; 92223c57df7Smcpowers return (ccm_ctx); 92323c57df7Smcpowers } 924