/* * AES_A_DMA_DMA_MODE register. * Default: 0x00000000. * bit[31] ACTIVE * This bit activates the DMA. When the DMA finishes, it resets * this bit to zero. * bit[30:26] Unused by this driver. * bit[25] SRC_LINK_LIST_EN * Source link list enable bit. When the linked list is terminated * this bit is reset by the DMA. * bit[24] DST_LINK_LIST_EN * Destination link list enable bit. When the linked list is * terminated this bit is reset by the DMA. * bit[23:0] Unused by this driver.
*/ #define AES_A_DMA_DMA_MODE_ACTIVE BIT(31) #define AES_A_DMA_DMA_MODE_SRC_LINK_LIST_EN BIT(25) #define AES_A_DMA_DMA_MODE_DST_LINK_LIST_EN BIT(24)
/* * There is an inconsistency in the documentation. This is documented as a * 11-bit value, but it is actually 10-bits.
*/ #define AES_DMA_STATUS_INPUT_BUFFER_OCCUPANCY_MASK 0x3FF
/* * During CCM decrypt, the OCS block needs to finish processing the ciphertext * before the tag is written. For 128-bit mode this required delay is 28 OCS * clock cycles. For 256-bit mode it is 36 OCS clock cycles.
*/ #define CCM_DECRYPT_DELAY_TAG_CLK_COUNT 36UL
/* * During CCM decrypt there must be a delay of at least 42 OCS clock cycles * between setting the TRIGGER bit in AES_ACTIVE and setting the LAST_CCM_GCM * bit in the same register (as stated in the OCS databook)
*/ #define CCM_DECRYPT_DELAY_LAST_GCX_CLK_COUNT 42UL
/* See RFC3610 section 2.2 */ #define L_PRIME_MIN (1) #define L_PRIME_MAX (7) /* * CCM IV format from RFC 3610 section 2.3 * * Octet Number Contents * ------------ --------- * 0 Flags * 1 ... 15-L Nonce N * 16-L ... 15 Counter i * * Flags = L' = L - 1
*/ #define L_PRIME_IDX 0 #define COUNTER_START(lprime) (16 - ((lprime) + 1)) #define COUNTER_LEN(lprime) ((lprime) + 1)
/** * struct ocs_dma_linked_list - OCS DMA linked list entry. * @src_addr: Source address of the data. * @src_len: Length of data to be fetched. * @next: Next dma_list to fetch. * @ll_flags: Flags (Freeze @ terminate) for the DMA engine.
*/ struct ocs_dma_linked_list {
u32 src_addr;
u32 src_len;
u32 next;
u32 ll_flags;
} __packed;
/* * Set endianness of inputs and outputs * AES_BYTE_ORDER_CFG * default 0x00000000 * bit [10] - KEY_HI_LO_SWAP * bit [9] - KEY_HI_SWAP_DWORDS_IN_OCTWORD * bit [8] - KEY_HI_SWAP_BYTES_IN_DWORD * bit [7] - KEY_LO_SWAP_DWORDS_IN_OCTWORD * bit [6] - KEY_LO_SWAP_BYTES_IN_DWORD * bit [5] - IV_SWAP_DWORDS_IN_OCTWORD * bit [4] - IV_SWAP_BYTES_IN_DWORD * bit [3] - DOUT_SWAP_DWORDS_IN_OCTWORD * bit [2] - DOUT_SWAP_BYTES_IN_DWORD * bit [1] - DOUT_SWAP_DWORDS_IN_OCTWORD * bit [0] - DOUT_SWAP_BYTES_IN_DWORD
*/ staticinlinevoid aes_a_set_endianness(conststruct ocs_aes_dev *aes_dev)
{
iowrite32(0x7FF, aes_dev->base_reg + AES_BYTE_ORDER_CFG_OFFSET);
}
/* Indicate last bulk of data. */ staticinlinevoid aes_a_op_termination(conststruct ocs_aes_dev *aes_dev)
{
iowrite32(AES_ACTIVE_TERMINATION,
aes_dev->base_reg + AES_ACTIVE_OFFSET);
}
/* * Set LAST_CCM_GCM in AES_ACTIVE register and clear all other bits. * * Called when DMA is programmed to fetch the last batch of data. * - For AES-CCM it is called for the last batch of Payload data and Ciphertext * data. * - For AES-GCM, it is called for the last batch of Plaintext data and * Ciphertext data.
*/ staticinlinevoid aes_a_set_last_gcx(conststruct ocs_aes_dev *aes_dev)
{
iowrite32(AES_ACTIVE_LAST_CCM_GCM,
aes_dev->base_reg + AES_ACTIVE_OFFSET);
}
/* Wait for LAST_CCM_GCM bit to be unset. */ staticinlinevoid aes_a_wait_last_gcx(conststruct ocs_aes_dev *aes_dev)
{
u32 aes_active_reg;
do {
aes_active_reg = ioread32(aes_dev->base_reg +
AES_ACTIVE_OFFSET);
} while (aes_active_reg & AES_ACTIVE_LAST_CCM_GCM);
}
/* Wait for 10 bits of input occupancy. */ staticvoid aes_a_dma_wait_input_buffer_occupancy(conststruct ocs_aes_dev *aes_dev)
{
u32 reg;
do {
reg = ioread32(aes_dev->base_reg + AES_A_DMA_STATUS_OFFSET);
} while (reg & AES_DMA_STATUS_INPUT_BUFFER_OCCUPANCY_MASK);
}
/* * Set LAST_CCM_GCM and LAST_ADATA bits in AES_ACTIVE register (and clear all * other bits). * * Called when DMA is programmed to fetch the last batch of Associated Data * (CCM case) or Additional Authenticated Data (GCM case).
*/ staticinlinevoid aes_a_set_last_gcx_and_adata(conststruct ocs_aes_dev *aes_dev)
{
iowrite32(AES_ACTIVE_LAST_ADATA | AES_ACTIVE_LAST_CCM_GCM,
aes_dev->base_reg + AES_ACTIVE_OFFSET);
}
/* Set DMA src and dst transfer size to 0 */ staticinlinevoid aes_a_dma_set_xfer_size_zero(conststruct ocs_aes_dev *aes_dev)
{
iowrite32(0, aes_dev->base_reg + AES_A_DMA_SRC_SIZE_OFFSET);
iowrite32(0, aes_dev->base_reg + AES_A_DMA_DST_SIZE_OFFSET);
}
/* Activate DMA for zero-byte transfer case. */ staticinlinevoid aes_a_dma_active(conststruct ocs_aes_dev *aes_dev)
{
iowrite32(AES_A_DMA_DMA_MODE_ACTIVE,
aes_dev->base_reg + AES_A_DMA_DMA_MODE_OFFSET);
}
/* Reset PERF_CNTR to 0 and activate it */ staticinlinevoid aes_a_dma_reset_and_activate_perf_cntr(conststruct ocs_aes_dev *aes_dev)
{
iowrite32(0x00000000, aes_dev->base_reg + AES_A_DMA_PERF_CNTR_OFFSET);
iowrite32(AES_ACTIVATE_PERF_CNTR,
aes_dev->base_reg + AES_A_DMA_WHILE_ACTIVE_MODE_OFFSET);
}
/* Wait until PERF_CNTR is > delay, then deactivate it */ staticinlinevoid aes_a_dma_wait_and_deactivate_perf_cntr(conststruct ocs_aes_dev *aes_dev, int delay)
{ while (ioread32(aes_dev->base_reg + AES_A_DMA_PERF_CNTR_OFFSET) < delay)
;
iowrite32(AES_DEACTIVATE_PERF_CNTR,
aes_dev->base_reg + AES_A_DMA_WHILE_ACTIVE_MODE_OFFSET);
}
/* Signal IRQ completion. */
complete(&aes_dev->irq_completion);
return IRQ_HANDLED;
}
/** * ocs_aes_set_key() - Write key into OCS AES hardware. * @aes_dev: The OCS AES device to write the key to. * @key_size: The size of the key (in bytes). * @key: The key to write. * @cipher: The cipher the key is for. * * For AES @key_size must be either 16 or 32. For SM4 @key_size must be 16. * * Return: 0 on success, negative error code otherwise.
*/ int ocs_aes_set_key(struct ocs_aes_dev *aes_dev, u32 key_size, const u8 *key, enum ocs_cipher cipher)
{ const u32 *key_u32;
u32 val; int i;
/* OCS AES supports 128-bit and 256-bit keys only. */ if (cipher == OCS_AES && !(key_size == 32 || key_size == 16)) {
dev_err(aes_dev->dev, "%d-bit keys not supported by AES cipher\n",
key_size * 8); return -EINVAL;
} /* OCS SM4 supports 128-bit keys only. */ if (cipher == OCS_SM4 && key_size != 16) {
dev_err(aes_dev->dev, "%d-bit keys not supported for SM4 cipher\n",
key_size * 8); return -EINVAL;
}
if (!key) return -EINVAL;
key_u32 = (const u32 *)key;
/* Write key to AES_KEY[0-7] registers */ for (i = 0; i < (key_size / sizeof(u32)); i++) {
iowrite32(key_u32[i],
aes_dev->base_reg + AES_KEY_0_OFFSET +
(i * sizeof(u32)));
} /* * Write key size * bits [31:1] - reserved * bit [0] - AES_KEY_SIZE * 0 - 128 bit key * 1 - 256 bit key
*/
val = (key_size == 16) ? AES_128_BIT_KEY : AES_256_BIT_KEY;
iowrite32(val, aes_dev->base_reg + AES_KEY_SIZE_OFFSET);
/* Set endianness recommended by data-sheet. */
aes_a_set_endianness(aes_dev);
/* Set AES_COMMAND register. */
set_ocs_aes_command(aes_dev, cipher, mode, instruction);
}
/* * Write the byte length of the last AES/SM4 block of Payload data (without * zero padding and without the length of the MAC) in register AES_PLEN.
*/ staticinlinevoid ocs_aes_write_last_data_blk_len(struct ocs_aes_dev *aes_dev,
u32 size)
{
u32 val;
if (size == 0) {
val = 0; gotoexit;
}
val = size % AES_BLOCK_SIZE; if (val == 0)
val = AES_BLOCK_SIZE;
/* * When instruction is OCS_BYPASS, OCS simply copies data from source * to destination using DMA. * * AES mode is irrelevant, but both source and destination DMA * linked-list must be defined.
*/ if (instruction == OCS_BYPASS) { if (src_dma_list == DMA_MAPPING_ERROR ||
dst_dma_list == DMA_MAPPING_ERROR) return -EINVAL;
return 0;
}
/* * For performance reasons switch based on mode to limit unnecessary * conditionals for each mode
*/ switch (mode) { case OCS_MODE_ECB: /* Ensure input length is multiple of block size */ if (src_size % AES_BLOCK_SIZE != 0) return -EINVAL;
/* Ensure source and destination linked lists are created */ if (src_dma_list == DMA_MAPPING_ERROR ||
dst_dma_list == DMA_MAPPING_ERROR) return -EINVAL;
return 0;
case OCS_MODE_CBC: /* Ensure input length is multiple of block size */ if (src_size % AES_BLOCK_SIZE != 0) return -EINVAL;
/* Ensure source and destination linked lists are created */ if (src_dma_list == DMA_MAPPING_ERROR ||
dst_dma_list == DMA_MAPPING_ERROR) return -EINVAL;
/* Ensure IV is present and block size in length */ if (!iv || iv_size != AES_BLOCK_SIZE) return -EINVAL;
return 0;
case OCS_MODE_CTR: /* Ensure input length of 1 byte or greater */ if (src_size == 0) return -EINVAL;
/* Ensure source and destination linked lists are created */ if (src_dma_list == DMA_MAPPING_ERROR ||
dst_dma_list == DMA_MAPPING_ERROR) return -EINVAL;
/* Ensure IV is present and block size in length */ if (!iv || iv_size != AES_BLOCK_SIZE) return -EINVAL;
return 0;
case OCS_MODE_CTS: /* Ensure input length >= block size */ if (src_size < AES_BLOCK_SIZE) return -EINVAL;
/* Ensure source and destination linked lists are created */ if (src_dma_list == DMA_MAPPING_ERROR ||
dst_dma_list == DMA_MAPPING_ERROR) return -EINVAL;
/* Ensure IV is present and block size in length */ if (!iv || iv_size != AES_BLOCK_SIZE) return -EINVAL;
return 0;
case OCS_MODE_GCM: /* Ensure IV is present and GCM_AES_IV_SIZE in length */ if (!iv || iv_size != GCM_AES_IV_SIZE) return -EINVAL;
/* * If input data present ensure source and destination linked * lists are created
*/ if (src_size && (src_dma_list == DMA_MAPPING_ERROR ||
dst_dma_list == DMA_MAPPING_ERROR)) return -EINVAL;
/* If aad present ensure aad linked list is created */ if (aad_size && aad_dma_list == DMA_MAPPING_ERROR) return -EINVAL;
/* Ensure tag destination is set */ if (!tag) return -EINVAL;
/* Just ensure that tag_size doesn't cause overflows. */ if (tag_size > (AES_MAX_TAG_SIZE_U32 * sizeof(u32))) return -EINVAL;
return 0;
case OCS_MODE_CCM: /* Ensure IV is present and block size in length */ if (!iv || iv_size != AES_BLOCK_SIZE) return -EINVAL;
/* 2 <= L <= 8, so 1 <= L' <= 7 */ if (iv[L_PRIME_IDX] < L_PRIME_MIN ||
iv[L_PRIME_IDX] > L_PRIME_MAX) return -EINVAL;
/* If aad present ensure aad linked list is created */ if (aad_size && aad_dma_list == DMA_MAPPING_ERROR) return -EINVAL;
/* Just ensure that tag_size doesn't cause overflows. */ if (tag_size > (AES_MAX_TAG_SIZE_U32 * sizeof(u32))) return -EINVAL;
if (instruction == OCS_DECRYPT) { /* * If input data present ensure source and destination * linked lists are created
*/ if (src_size && (src_dma_list == DMA_MAPPING_ERROR ||
dst_dma_list == DMA_MAPPING_ERROR)) return -EINVAL;
/* Ensure input tag is present */ if (!tag) return -EINVAL;
return 0;
}
/* Instruction == OCS_ENCRYPT */
/* * Destination linked list always required (for tag even if no * input data)
*/ if (dst_dma_list == DMA_MAPPING_ERROR) return -EINVAL;
/* If input data present ensure src linked list is created */ if (src_size && src_dma_list == DMA_MAPPING_ERROR) return -EINVAL;
return 0;
default: return -EINVAL;
}
}
/** * ocs_aes_op() - Perform AES/SM4 operation. * @aes_dev: The OCS AES device to use. * @mode: The mode to use (ECB, CBC, CTR, or CTS). * @cipher: The cipher to use (AES or SM4). * @instruction: The instruction to perform (encrypt or decrypt). * @dst_dma_list: The OCS DMA list mapping output memory. * @src_dma_list: The OCS DMA list mapping input payload data. * @src_size: The amount of data mapped by @src_dma_list. * @iv: The IV vector. * @iv_size: The size (in bytes) of @iv. * * Return: 0 on success, negative error code otherwise.
*/ int ocs_aes_op(struct ocs_aes_dev *aes_dev, enum ocs_mode mode, enum ocs_cipher cipher, enum ocs_instruction instruction,
dma_addr_t dst_dma_list,
dma_addr_t src_dma_list,
u32 src_size,
u8 *iv,
u32 iv_size)
{
u32 *iv32; int rc;
rc = ocs_aes_validate_inputs(src_dma_list, src_size, iv, iv_size, 0, 0,
NULL, 0, cipher, mode, instruction,
dst_dma_list); if (rc) return rc; /* * ocs_aes_validate_inputs() is a generic check, now ensure mode is not * GCM or CCM.
*/ if (mode == OCS_MODE_GCM || mode == OCS_MODE_CCM) return -EINVAL;
/* Cast IV to u32 array. */
iv32 = (u32 *)iv;
ocs_aes_init(aes_dev, mode, cipher, instruction);
if (mode == OCS_MODE_CTS) { /* Write the byte length of the last data block to engine. */
ocs_aes_write_last_data_blk_len(aes_dev, src_size);
}
/* ECB is the only mode that doesn't use IV. */ if (mode != OCS_MODE_ECB) {
iowrite32(iv32[0], aes_dev->base_reg + AES_IV_0_OFFSET);
iowrite32(iv32[1], aes_dev->base_reg + AES_IV_1_OFFSET);
iowrite32(iv32[2], aes_dev->base_reg + AES_IV_2_OFFSET);
iowrite32(iv32[3], aes_dev->base_reg + AES_IV_3_OFFSET);
}
/* Set AES_ACTIVE.TRIGGER to start the operation. */
aes_a_op_trigger(aes_dev);
if (mode == OCS_MODE_CTS) { /* * For CTS mode, instruct engine to activate ciphertext * stealing if last block of data is incomplete.
*/
aes_a_set_last_gcx(aes_dev);
} else { /* For all other modes, just write the 'termination' bit. */
aes_a_op_termination(aes_dev);
}
/* Wait for engine to complete processing. */
rc = ocs_aes_irq_enable_and_wait(aes_dev, AES_COMPLETE_INT); if (rc) return rc;
if (mode == OCS_MODE_CTR) { /* Read back IV for streaming mode */
iv32[0] = ioread32(aes_dev->base_reg + AES_IV_0_OFFSET);
iv32[1] = ioread32(aes_dev->base_reg + AES_IV_1_OFFSET);
iv32[2] = ioread32(aes_dev->base_reg + AES_IV_2_OFFSET);
iv32[3] = ioread32(aes_dev->base_reg + AES_IV_3_OFFSET);
}
/* * IV must be 12 bytes; Other sizes not supported as Linux crypto API * does only expects/allows 12 byte IV for GCM
*/
iowrite32(0x00000001, aes_dev->base_reg + AES_IV_0_OFFSET);
iowrite32(__swab32(j0[2]), aes_dev->base_reg + AES_IV_1_OFFSET);
iowrite32(__swab32(j0[1]), aes_dev->base_reg + AES_IV_2_OFFSET);
iowrite32(__swab32(j0[0]), aes_dev->base_reg + AES_IV_3_OFFSET);
}
/* Read GCM tag from engine registers. */ staticinlinevoid ocs_aes_gcm_read_tag(struct ocs_aes_dev *aes_dev,
u8 *tag, u32 tag_size)
{
u32 tag_u32[AES_MAX_TAG_SIZE_U32];
/* * The Authentication Tag T is stored in Little Endian order in the * registers with the most significant bytes stored from AES_T_MAC[3] * downward.
*/
tag_u32[0] = __swab32(ioread32(aes_dev->base_reg + AES_T_MAC_3_OFFSET));
tag_u32[1] = __swab32(ioread32(aes_dev->base_reg + AES_T_MAC_2_OFFSET));
tag_u32[2] = __swab32(ioread32(aes_dev->base_reg + AES_T_MAC_1_OFFSET));
tag_u32[3] = __swab32(ioread32(aes_dev->base_reg + AES_T_MAC_0_OFFSET));
memcpy(tag, tag_u32, tag_size);
}
/** * ocs_aes_gcm_op() - Perform GCM operation. * @aes_dev: The OCS AES device to use. * @cipher: The Cipher to use (AES or SM4). * @instruction: The instruction to perform (encrypt or decrypt). * @dst_dma_list: The OCS DMA list mapping output memory. * @src_dma_list: The OCS DMA list mapping input payload data. * @src_size: The amount of data mapped by @src_dma_list. * @iv: The input IV vector. * @aad_dma_list: The OCS DMA list mapping input AAD data. * @aad_size: The amount of data mapped by @aad_dma_list. * @out_tag: Where to store computed tag. * @tag_size: The size (in bytes) of @out_tag. * * Return: 0 on success, negative error code otherwise.
*/ int ocs_aes_gcm_op(struct ocs_aes_dev *aes_dev, enum ocs_cipher cipher, enum ocs_instruction instruction,
dma_addr_t dst_dma_list,
dma_addr_t src_dma_list,
u32 src_size, const u8 *iv,
dma_addr_t aad_dma_list,
u32 aad_size,
u8 *out_tag,
u32 tag_size)
{
u64 bit_len;
u32 val; int rc;
rc = ocs_aes_validate_inputs(src_dma_list, src_size, iv,
GCM_AES_IV_SIZE, aad_dma_list,
aad_size, out_tag, tag_size, cipher,
OCS_MODE_GCM, instruction,
dst_dma_list); if (rc) return rc;
/* Write the byte length of the last plaintext / ciphertext block. */
ocs_aes_write_last_data_blk_len(aes_dev, src_size);
/* Write ciphertext bit length */
bit_len = (u64)src_size * 8;
val = bit_len & 0xFFFFFFFF;
iowrite32(val, aes_dev->base_reg + AES_MULTIPURPOSE2_0_OFFSET);
val = bit_len >> 32;
iowrite32(val, aes_dev->base_reg + AES_MULTIPURPOSE2_1_OFFSET);
/* Write aad bit length */
bit_len = (u64)aad_size * 8;
val = bit_len & 0xFFFFFFFF;
iowrite32(val, aes_dev->base_reg + AES_MULTIPURPOSE2_2_OFFSET);
val = bit_len >> 32;
iowrite32(val, aes_dev->base_reg + AES_MULTIPURPOSE2_3_OFFSET);
/* Set AES_ACTIVE.TRIGGER to start the operation. */
aes_a_op_trigger(aes_dev);
/* Process AAD. */ if (aad_size) { /* If aad present, configure DMA to feed it to the engine. */
dma_to_ocs_aes_ll(aes_dev, aad_dma_list);
aes_a_dma_active_src_ll_en(aes_dev);
/* Instructs engine to pad last block of aad, if needed. */
aes_a_set_last_gcx_and_adata(aes_dev);
/* Wait for DMA transfer to complete. */
rc = ocs_aes_irq_enable_and_wait(aes_dev, AES_DMA_SRC_DONE_INT); if (rc) return rc;
} else {
aes_a_set_last_gcx_and_adata(aes_dev);
}
/* Wait until adata (if present) has been processed. */
aes_a_wait_last_gcx(aes_dev);
aes_a_dma_wait_input_buffer_occupancy(aes_dev);
/* Now process payload. */ if (src_size) { /* Configure and activate DMA for both input and output data. */
dma_to_ocs_aes_ll(aes_dev, src_dma_list);
dma_from_ocs_aes_ll(aes_dev, dst_dma_list);
aes_a_dma_active_src_dst_ll_en(aes_dev);
} else {
aes_a_dma_set_xfer_size_zero(aes_dev);
aes_a_dma_active(aes_dev);
}
/* Instruct AES/SMA4 engine payload processing is over. */
aes_a_set_last_gcx(aes_dev);
/* Wait for OCS AES engine to complete processing. */
rc = ocs_aes_irq_enable_and_wait(aes_dev, AES_COMPLETE_INT); if (rc) return rc;
ocs_aes_gcm_read_tag(aes_dev, out_tag, tag_size);
return 0;
}
/* Write encrypted tag to AES/SM4 engine. */ staticvoid ocs_aes_ccm_write_encrypted_tag(struct ocs_aes_dev *aes_dev, const u8 *in_tag, u32 tag_size)
{ int i;
/* Ensure DMA input buffer is empty */
aes_a_dma_wait_input_buffer_occupancy(aes_dev);
/* * During CCM decrypt, the OCS block needs to finish processing the * ciphertext before the tag is written. So delay needed after DMA has * completed writing the ciphertext
*/
aes_a_dma_reset_and_activate_perf_cntr(aes_dev);
aes_a_dma_wait_and_deactivate_perf_cntr(aes_dev,
CCM_DECRYPT_DELAY_TAG_CLK_COUNT);
/* Write encrypted tag to AES/SM4 engine. */ for (i = 0; i < tag_size; i++) {
iowrite8(in_tag[i], aes_dev->base_reg +
AES_A_DMA_INBUFFER_WRITE_FIFO_OFFSET);
}
}
/* * Write B0 CCM block to OCS AES HW. * * Note: B0 format is documented in NIST Special Publication 800-38C * https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38c.pdf * (see Section A.2.1)
*/ staticint ocs_aes_ccm_write_b0(conststruct ocs_aes_dev *aes_dev, const u8 *iv, u32 adata_size, u32 tag_size,
u32 cryptlen)
{
u8 b0[16]; /* CCM B0 block is 16 bytes long. */ int i, q;
/* Initialize B0 to 0. */
memset(b0, 0, sizeof(b0));
/* * B0[0] is the 'Flags Octet' and has the following structure: * bit 7: Reserved * bit 6: Adata flag * bit 5-3: t value encoded as (t-2)/2 * bit 2-0: q value encoded as q - 1
*/ /* If there is AAD data, set the Adata flag. */ if (adata_size)
b0[0] |= BIT(6); /* * t denotes the octet length of T. * t can only be an element of { 4, 6, 8, 10, 12, 14, 16} and is * encoded as (t - 2) / 2
*/
b0[0] |= (((tag_size - 2) / 2) & 0x7) << 3; /* * q is the octet length of Q. * q can only be an element of {2, 3, 4, 5, 6, 7, 8} and is encoded as * q - 1 == iv[0] & 0x7;
*/
b0[0] |= iv[0] & 0x7; /* * Copy the Nonce N from IV to B0; N is located in iv[1]..iv[15 - q] * and must be copied to b0[1]..b0[15-q]. * q == (iv[0] & 0x7) + 1
*/
q = (iv[0] & 0x7) + 1; for (i = 1; i <= 15 - q; i++)
b0[i] = iv[i]; /* * The rest of B0 must contain Q, i.e., the message length. * Q is encoded in q octets, in big-endian order, so to write it, we * start from the end of B0 and we move backward.
*/
i = sizeof(b0) - 1; while (q) {
b0[i] = cryptlen & 0xff;
cryptlen >>= 8;
i--;
q--;
} /* * If cryptlen is not zero at this point, it means that its original * value was too big.
*/ if (cryptlen) return -EOVERFLOW; /* Now write B0 to OCS AES input buffer. */ for (i = 0; i < sizeof(b0); i++)
iowrite8(b0[i], aes_dev->base_reg +
AES_A_DMA_INBUFFER_WRITE_FIFO_OFFSET); return 0;
}
/* * Write adata length to OCS AES HW. * * Note: adata len encoding is documented in NIST Special Publication 800-38C * https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38c.pdf * (see Section A.2.2)
*/ staticvoid ocs_aes_ccm_write_adata_len(conststruct ocs_aes_dev *aes_dev,
u64 adata_len)
{
u8 enc_a[10]; /* Maximum encoded size: 10 octets. */ int i, len;
/* * adata_len ('a') is encoded as follows: * If 0 < a < 2^16 - 2^8 ==> 'a' encoded as [a]16, i.e., two octets * (big endian). * If 2^16 - 2^8 ≤ a < 2^32 ==> 'a' encoded as 0xff || 0xfe || [a]32, * i.e., six octets (big endian). * If 2^32 ≤ a < 2^64 ==> 'a' encoded as 0xff || 0xff || [a]64, * i.e., ten octets (big endian).
*/ if (adata_len < 65280) {
len = 2;
*(__be16 *)enc_a = cpu_to_be16(adata_len);
} elseif (adata_len <= 0xFFFFFFFF) {
len = 6;
*(__be16 *)enc_a = cpu_to_be16(0xfffe);
*(__be32 *)&enc_a[2] = cpu_to_be32(adata_len);
} else { /* adata_len >= 2^32 */
len = 10;
*(__be16 *)enc_a = cpu_to_be16(0xffff);
*(__be64 *)&enc_a[2] = cpu_to_be64(adata_len);
} for (i = 0; i < len; i++)
iowrite8(enc_a[i],
aes_dev->base_reg +
AES_A_DMA_INBUFFER_WRITE_FIFO_OFFSET);
}
if (!adata_size) { /* Since no aad the LAST_GCX bit can be set now */
aes_a_set_last_gcx_and_adata(aes_dev); gotoexit;
}
/* Adata case. */
/* * Form the encoding of the Associated data length and write it * to the AES/SM4 input buffer.
*/
ocs_aes_ccm_write_adata_len(aes_dev, adata_size);
/* Configure the AES/SM4 DMA to fetch the Associated Data */
dma_to_ocs_aes_ll(aes_dev, adata_dma_list);
/* Activate DMA to fetch Associated data. */
aes_a_dma_active_src_ll_en(aes_dev);
/* Set LAST_GCX and LAST_ADATA in AES ACTIVE register. */
aes_a_set_last_gcx_and_adata(aes_dev);
/* Wait for DMA transfer to complete. */
rc = ocs_aes_irq_enable_and_wait(aes_dev, AES_DMA_SRC_DONE_INT); if (rc) return rc;
exit: /* Wait until adata (if present) has been processed. */
aes_a_wait_last_gcx(aes_dev);
aes_a_dma_wait_input_buffer_occupancy(aes_dev);
return 0;
}
staticint ocs_aes_ccm_encrypt_do_payload(struct ocs_aes_dev *aes_dev,
dma_addr_t dst_dma_list,
dma_addr_t src_dma_list,
u32 src_size)
{ if (src_size) { /* * Configure and activate DMA for both input and output * data.
*/
dma_to_ocs_aes_ll(aes_dev, src_dma_list);
dma_from_ocs_aes_ll(aes_dev, dst_dma_list);
aes_a_dma_active_src_dst_ll_en(aes_dev);
} else { /* Configure and activate DMA for output data only. */
dma_from_ocs_aes_ll(aes_dev, dst_dma_list);
aes_a_dma_active_dst_ll_en(aes_dev);
}
/* * Set the LAST GCX bit in AES_ACTIVE Register to instruct * AES/SM4 engine to pad the last block of data.
*/
aes_a_set_last_gcx(aes_dev);
/* We are done, wait for IRQ and return. */ return ocs_aes_irq_enable_and_wait(aes_dev, AES_COMPLETE_INT);
}
staticint ocs_aes_ccm_decrypt_do_payload(struct ocs_aes_dev *aes_dev,
dma_addr_t dst_dma_list,
dma_addr_t src_dma_list,
u32 src_size)
{ if (!src_size) { /* Let engine process 0-length input. */
aes_a_dma_set_xfer_size_zero(aes_dev);
aes_a_dma_active(aes_dev);
aes_a_set_last_gcx(aes_dev);
return 0;
}
/* * Configure and activate DMA for both input and output * data.
*/
dma_to_ocs_aes_ll(aes_dev, src_dma_list);
dma_from_ocs_aes_ll(aes_dev, dst_dma_list);
aes_a_dma_active_src_dst_ll_en(aes_dev); /* * Set the LAST GCX bit in AES_ACTIVE Register; this allows the * AES/SM4 engine to differentiate between encrypted data and * encrypted MAC.
*/
aes_a_set_last_gcx(aes_dev); /* * Enable DMA DONE interrupt; once DMA transfer is over, * interrupt handler will process the MAC/tag.
*/ return ocs_aes_irq_enable_and_wait(aes_dev, AES_DMA_SRC_DONE_INT);
}
/* * Compare Tag to Yr. * * Only used at the end of CCM decrypt. If tag == yr, message authentication * has succeeded.
*/ staticinlineint ccm_compare_tag_to_yr(struct ocs_aes_dev *aes_dev,
u8 tag_size_bytes)
{
u32 tag[AES_MAX_TAG_SIZE_U32];
u32 yr[AES_MAX_TAG_SIZE_U32];
u8 i;
/* Read Tag and Yr from AES registers. */ for (i = 0; i < AES_MAX_TAG_SIZE_U32; i++) {
tag[i] = ioread32(aes_dev->base_reg +
AES_T_MAC_0_OFFSET + (i * sizeof(u32)));
yr[i] = ioread32(aes_dev->base_reg +
AES_MULTIPURPOSE2_0_OFFSET +
(i * sizeof(u32)));
}
/** * ocs_aes_ccm_op() - Perform CCM operation. * @aes_dev: The OCS AES device to use. * @cipher: The Cipher to use (AES or SM4). * @instruction: The instruction to perform (encrypt or decrypt). * @dst_dma_list: The OCS DMA list mapping output memory. * @src_dma_list: The OCS DMA list mapping input payload data. * @src_size: The amount of data mapped by @src_dma_list. * @iv: The input IV vector. * @adata_dma_list: The OCS DMA list mapping input A-data. * @adata_size: The amount of data mapped by @adata_dma_list. * @in_tag: Input tag. * @tag_size: The size (in bytes) of @in_tag. * * Note: for encrypt the tag is appended to the ciphertext (in the memory * mapped by @dst_dma_list). * * Return: 0 on success, negative error code otherwise.
*/ int ocs_aes_ccm_op(struct ocs_aes_dev *aes_dev, enum ocs_cipher cipher, enum ocs_instruction instruction,
dma_addr_t dst_dma_list,
dma_addr_t src_dma_list,
u32 src_size,
u8 *iv,
dma_addr_t adata_dma_list,
u32 adata_size,
u8 *in_tag,
u32 tag_size)
{
u32 *iv_32;
u8 lprime; int rc;
rc = ocs_aes_validate_inputs(src_dma_list, src_size, iv,
AES_BLOCK_SIZE, adata_dma_list, adata_size,
in_tag, tag_size, cipher, OCS_MODE_CCM,
instruction, dst_dma_list); if (rc) return rc;
/* * Note: rfc 3610 and NIST 800-38C require counter of zero to encrypt * auth tag so ensure this is the case
*/
lprime = iv[L_PRIME_IDX];
memset(&iv[COUNTER_START(lprime)], 0, COUNTER_LEN(lprime));
/* * Nonce is already converted to ctr0 before being passed into this * function as iv.
*/
iv_32 = (u32 *)iv;
iowrite32(__swab32(iv_32[0]),
aes_dev->base_reg + AES_MULTIPURPOSE1_3_OFFSET);
iowrite32(__swab32(iv_32[1]),
aes_dev->base_reg + AES_MULTIPURPOSE1_2_OFFSET);
iowrite32(__swab32(iv_32[2]),
aes_dev->base_reg + AES_MULTIPURPOSE1_1_OFFSET);
iowrite32(__swab32(iv_32[3]),
aes_dev->base_reg + AES_MULTIPURPOSE1_0_OFFSET);
/* Write MAC/tag length in register AES_TLEN */
iowrite32(tag_size, aes_dev->base_reg + AES_TLEN_OFFSET); /* * Write the byte length of the last AES/SM4 block of Payload data * (without zero padding and without the length of the MAC) in register * AES_PLEN.
*/
ocs_aes_write_last_data_blk_len(aes_dev, src_size);
/* Set AES_ACTIVE.TRIGGER to start the operation. */
aes_a_op_trigger(aes_dev);
aes_a_dma_reset_and_activate_perf_cntr(aes_dev);
/* Form block B0 and write it to the AES/SM4 input buffer. */
rc = ocs_aes_ccm_write_b0(aes_dev, iv, adata_size, tag_size, src_size); if (rc) return rc; /* * Ensure there has been at least CCM_DECRYPT_DELAY_LAST_GCX_CLK_COUNT * clock cycles since TRIGGER bit was set
*/
aes_a_dma_wait_and_deactivate_perf_cntr(aes_dev,
CCM_DECRYPT_DELAY_LAST_GCX_CLK_COUNT);
/* Process Adata. */
ocs_aes_ccm_do_adata(aes_dev, adata_dma_list, adata_size);
/* For Encrypt case we just process the payload and return. */ if (instruction == OCS_ENCRYPT) { return ocs_aes_ccm_encrypt_do_payload(aes_dev, dst_dma_list,
src_dma_list, src_size);
} /* For Decypt we need to process the payload and then the tag. */
rc = ocs_aes_ccm_decrypt_do_payload(aes_dev, dst_dma_list,
src_dma_list, src_size); if (rc) return rc;
/* Process MAC/tag directly: feed tag to engine and wait for IRQ. */
ocs_aes_ccm_write_encrypted_tag(aes_dev, in_tag, tag_size);
rc = ocs_aes_irq_enable_and_wait(aes_dev, AES_COMPLETE_INT); if (rc) return rc;
/** * ocs_create_linked_list_from_sg() - Create OCS DMA linked list from SG list. * @aes_dev: The OCS AES device the list will be created for. * @sg: The SG list OCS DMA linked list will be created from. When * passed to this function, @sg must have been already mapped * with dma_map_sg(). * @sg_dma_count: The number of DMA-mapped entries in @sg. This must be the * value returned by dma_map_sg() when @sg was mapped. * @dll_desc: The OCS DMA dma_list to use to store information about the * created linked list. * @data_size: The size of the data (from the SG list) to be mapped into the * OCS DMA linked list. * @data_offset: The offset (within the SG list) of the data to be mapped. * * Return: 0 on success, negative error code otherwise.
*/ int ocs_create_linked_list_from_sg(conststruct ocs_aes_dev *aes_dev, struct scatterlist *sg, int sg_dma_count, struct ocs_dll_desc *dll_desc,
size_t data_size, size_t data_offset)
{ struct ocs_dma_linked_list *ll = NULL; struct scatterlist *sg_tmp; unsignedint tmp; int dma_nents; int i;
if (!dll_desc || !sg || !aes_dev) return -EINVAL;
/* Default values for when no ddl_desc is created. */
dll_desc->vaddr = NULL;
dll_desc->dma_addr = DMA_MAPPING_ERROR;
dll_desc->size = 0;
if (data_size == 0) return 0;
/* Loop over sg_list until we reach entry at specified offset. */ while (data_offset >= sg_dma_len(sg)) {
data_offset -= sg_dma_len(sg);
sg_dma_count--;
sg = sg_next(sg); /* If we reach the end of the list, offset was invalid. */ if (!sg || sg_dma_count == 0) return -EINVAL;
}
/* Compute number of DMA-mapped SG entries to add into OCS DMA list. */
dma_nents = 0;
tmp = 0;
sg_tmp = sg; while (tmp < data_offset + data_size) { /* If we reach the end of the list, data_size was invalid. */ if (!sg_tmp) return -EINVAL;
tmp += sg_dma_len(sg_tmp);
dma_nents++;
sg_tmp = sg_next(sg_tmp);
} if (dma_nents > sg_dma_count) return -EINVAL;
/* Allocate the DMA list, one entry for each SG entry. */
dll_desc->size = sizeof(struct ocs_dma_linked_list) * dma_nents;
dll_desc->vaddr = dma_alloc_coherent(aes_dev->dev, dll_desc->size,
&dll_desc->dma_addr, GFP_KERNEL); if (!dll_desc->vaddr) return -ENOMEM;
/* Populate DMA linked list entries. */
ll = dll_desc->vaddr; for (i = 0; i < dma_nents; i++, sg = sg_next(sg)) {
ll[i].src_addr = sg_dma_address(sg) + data_offset;
ll[i].src_len = min(sg_dma_len(sg) - data_offset, data_size);
data_offset = 0;
data_size -= ll[i].src_len; /* Current element points to the DMA address of the next one. */
ll[i].next = dll_desc->dma_addr + (sizeof(*ll) * (i + 1));
ll[i].ll_flags = 0;
} /* Terminate last element. */
ll[i - 1].next = 0;
ll[i - 1].ll_flags = OCS_LL_DMA_FLAG_TERMINATE;
return 0;
}
Messung V0.5
¤ Dauer der Verarbeitung: 0.15 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.