26 #define AES_WAIT_FOR_STATUS(aes_, flag_, value_) \
27 while (mmio_region_get_bit32(aes->base_addr, AES_STATUS_REG_OFFSET, \
28 (flag_)) != value_) { \
31 static bool aes_idle(
const dif_aes_t *aes) {
36 static bool aes_stalled(
const dif_aes_t *aes) {
38 AES_STATUS_STALL_BIT);
41 static bool aes_output_lost(
const dif_aes_t *aes) {
43 AES_STATUS_OUTPUT_LOST_BIT);
46 static bool aes_output_valid(
const dif_aes_t *aes) {
48 AES_STATUS_OUTPUT_VALID_BIT);
51 static bool aes_input_ready(
const dif_aes_t *aes) {
53 AES_STATUS_INPUT_READY_BIT);
56 static bool aes_alert_fatal(
const dif_aes_t *aes) {
58 AES_STATUS_ALERT_FATAL_FAULT_BIT);
61 static bool aes_alert_recoverable(
const dif_aes_t *aes) {
63 AES_STATUS_ALERT_RECOV_CTRL_UPDATE_ERR_BIT);
66 static void aes_shadowed_write(
mmio_region_t base, ptrdiff_t offset,
68 mmio_region_write32(base, offset, value);
69 mmio_region_write32(base, offset, value);
72 static void aes_clear_internal_state(
const dif_aes_t *aes) {
74 AES_WAIT_FOR_STATUS(aes, AES_STATUS_IDLE_BIT,
true);
81 aes_shadowed_write(aes->
base_addr, AES_CTRL_SHADOWED_REG_OFFSET, ctrl_reg);
83 uint32_t trigger_reg =
89 mmio_region_write32(aes->
base_addr, AES_TRIGGER_REG_OFFSET, trigger_reg);
92 AES_WAIT_FOR_STATUS(aes, AES_STATUS_IDLE_BIT,
true);
105 transaction->operation);
111 transaction->key_len);
114 transaction->mask_reseeding);
122 aes_shadowed_write(aes->
base_addr, AES_CTRL_SHADOWED_REG_OFFSET, reg);
138 mmio_region_read32(aes->
base_addr, AES_CTRL_AUX_REGWEN_REG_OFFSET);
141 mmio_region_read32(aes->
base_addr, AES_CTRL_AUX_SHADOWED_REG_OFFSET);
143 reg_val, AES_CTRL_AUX_SHADOWED_KEY_TOUCH_FORCES_RESEED_BIT) !=
157 aes_shadowed_write(aes->
base_addr, AES_CTRL_AUX_SHADOWED_REG_OFFSET, reg_val);
160 mmio_region_write32(aes->
base_addr, AES_CTRL_AUX_REGWEN_REG_OFFSET, reg_val);
173 static void aes_set_multireg(
const dif_aes_t *aes,
const uint32_t *data,
174 size_t regs_num, ptrdiff_t reg0_offset) {
175 for (
int i = 0; i < regs_num; ++i) {
176 ptrdiff_t offset = reg0_offset + (ptrdiff_t)i * (ptrdiff_t)
sizeof(uint32_t);
178 mmio_region_write32(aes->
base_addr, offset, data[i]);
182 static void aes_read_multireg(
const dif_aes_t *aes, uint32_t *data,
183 size_t regs_num, ptrdiff_t reg0_offset) {
184 for (
int i = 0; i < regs_num; ++i) {
185 ptrdiff_t offset = reg0_offset + (ptrdiff_t)i * (ptrdiff_t)
sizeof(uint32_t);
187 data[i] = mmio_region_read32(aes->
base_addr, offset);
196 aes_clear_internal_state(aes);
200 AES_CTRL_SHADOWED_OPERATION_MASK);
203 AES_CTRL_SHADOWED_MODE_VALUE_AES_NONE);
206 AES_CTRL_SHADOWED_KEY_LEN_MASK);
208 aes_shadowed_write(aes->
base_addr, AES_CTRL_SHADOWED_REG_OFFSET, reg);
217 if (aes == NULL || transaction == NULL ||
224 if (!aes_idle(aes)) {
233 result = configure_aux(aes, transaction);
239 aes_set_multireg(aes, &key->share0[0], AES_KEY_SHARE0_MULTIREG_COUNT,
240 AES_KEY_SHARE0_0_REG_OFFSET);
242 aes_set_multireg(aes, &key->share1[0], AES_KEY_SHARE1_MULTIREG_COUNT,
243 AES_KEY_SHARE1_0_REG_OFFSET);
250 AES_WAIT_FOR_STATUS(aes, AES_STATUS_IDLE_BIT,
true);
251 aes_set_multireg(aes, &iv->iv[0], AES_IV_MULTIREG_COUNT,
252 AES_IV_0_REG_OFFSET);
263 if (!aes_idle(aes)) {
267 aes_clear_internal_state(aes);
278 if (!aes_input_ready(aes)) {
282 aes_set_multireg(aes, &data.data[0], AES_DATA_IN_MULTIREG_COUNT,
283 AES_DATA_IN_0_REG_OFFSET);
289 if (aes == NULL || data == NULL) {
293 if (!aes_output_valid(aes)) {
297 aes_read_multireg(aes, data->data, AES_DATA_OUT_MULTIREG_COUNT,
298 AES_DATA_OUT_0_REG_OFFSET);
306 size_t block_count) {
307 if (aes == NULL || plain_text == NULL || cipher_text == NULL ||
314 if (block_count < 2) {
320 if (!aes_input_ready(aes)) {
325 aes_set_multireg(aes, plain_text[0].data, AES_DATA_IN_MULTIREG_COUNT,
326 AES_DATA_IN_0_REG_OFFSET);
331 AES_WAIT_FOR_STATUS(aes, AES_STATUS_INPUT_READY_BIT,
true);
333 for (
size_t i = 0; i < block_count; ++i) {
338 if (i + 1 < block_count) {
339 aes_set_multireg(aes, plain_text[i + 1].data, AES_DATA_IN_MULTIREG_COUNT,
340 AES_DATA_IN_0_REG_OFFSET);
346 AES_WAIT_FOR_STATUS(aes, AES_STATUS_OUTPUT_VALID_BIT,
true);
350 aes_read_multireg(aes, cipher_text[i].data, AES_DATA_OUT_MULTIREG_COUNT,
351 AES_DATA_OUT_0_REG_OFFSET);
363 mmio_region_write32(aes->
base_addr, AES_TRIGGER_REG_OFFSET, reg);
370 if (aes == NULL || set == NULL) {
376 *set = aes_idle(aes);
379 *set = aes_stalled(aes);
382 *set = aes_output_lost(aes);
385 *set = aes_output_valid(aes);
388 *set = aes_input_ready(aes);
391 *set = aes_alert_fatal(aes);
394 *set = aes_alert_recoverable(aes);
404 if (aes == NULL || iv == NULL) {
408 if (!aes_idle(aes)) {
412 for (
int i = 0; i < AES_IV_MULTIREG_COUNT; ++i) {
414 AES_IV_0_REG_OFFSET + (ptrdiff_t)i * (ptrdiff_t)
sizeof(uint32_t);
416 iv->iv[i] = mmio_region_read32(aes->
base_addr, offset);