summaryrefslogtreecommitdiff
path: root/board
diff options
context:
space:
mode:
authorVadim Bendebury <vbendeb@chromium.org>2015-11-13 10:37:22 -0800
committerchrome-bot <chrome-bot@chromium.org>2015-11-18 00:14:04 -0800
commitbea9bfc15db4b58e22d0b39a3590bb16b3c51d4f (patch)
tree6058874257b31e5bd0b5640b9635b5bd769c87f7 /board
parent5630c63aefb24b1f7cfa7842a58394cc619fe1d6 (diff)
downloadchrome-ec-bea9bfc15db4b58e22d0b39a3590bb16b3c51d4f.tar.gz
cr50: do not assume that data and keys are aligned
Many architectures do not mind accessing unaligned data, but many do. Defining special packed accessor structure makes sure that the compiler takes care of generating proper code. Decreased performance is the price paid for improved robustness. Should the performance hit prove too high, we might have to copy keys, vectors and data into aligned buffers before processing them. BRANCH=none BUG=chrome-os-partner:43025, chrome-os-partner:47524 TEST=attempts to use AES driver with unaligned data do not cause exceptions any more. Change-Id: I22e8e049ac74d0d1b6e455dca4430a5147b6d711 Signed-off-by: Vadim Bendebury <vbendeb@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/312589 Reviewed-by: Bill Richardson <wfrichar@chromium.org>
Diffstat (limited to 'board')
-rw-r--r--board/cr50/dcrypto/aes.c20
-rw-r--r--board/cr50/dcrypto/internal.h11
2 files changed, 19 insertions, 12 deletions
diff --git a/board/cr50/dcrypto/aes.c b/board/cr50/dcrypto/aes.c
index 86e31fcca4..3773536176 100644
--- a/board/cr50/dcrypto/aes.c
+++ b/board/cr50/dcrypto/aes.c
@@ -36,7 +36,7 @@ int DCRYPTO_aes_init(const uint8_t *key, uint32_t key_len, const uint8_t *iv,
enum cipher_mode c_mode, enum encrypt_mode e_mode)
{
int i;
- const uint32_t *p;
+ const struct access_helper *p;
uint32_t key_mode;
switch (key_len) {
@@ -56,9 +56,9 @@ int DCRYPTO_aes_init(const uint8_t *key, uint32_t key_len, const uint8_t *iv,
set_control_register(c_mode, key_mode, e_mode);
/* Initialize hardware with AES key */
- p = (uint32_t *) key;
+ p = (struct access_helper *) key;
for (i = 0; i < (key_len >> 5); i++)
- GR_KEYMGR_AES_KEY(i) = p[i];
+ GR_KEYMGR_AES_KEY(i) = p[i].udata;
/* Trigger key expansion. */
GREG32(KEYMGR, AES_KEY_START) = 1;
@@ -70,9 +70,9 @@ int DCRYPTO_aes_init(const uint8_t *key, uint32_t key_len, const uint8_t *iv,
/* Initialize IV for modes that require it. */
if (iv) {
- p = (uint32_t *) iv;
+ p = (struct access_helper *) iv;
for (i = 0; i < 4; i++)
- GR_KEYMGR_AES_CTR(i) = p[i];
+ GR_KEYMGR_AES_CTR(i) = p[i].udata;
}
return 1;
}
@@ -80,12 +80,12 @@ int DCRYPTO_aes_init(const uint8_t *key, uint32_t key_len, const uint8_t *iv,
int DCRYPTO_aes_block(const uint8_t *in, uint8_t *out)
{
int i;
- uint32_t *outw;
- const uint32_t *inw = (const uint32_t *) in;
+ struct access_helper *outw;
+ const struct access_helper *inw = (struct access_helper *) in;
/* Write plaintext. */
for (i = 0; i < 4; i++)
- GREG32(KEYMGR, AES_WFIFO_DATA) = inw[i];
+ GREG32(KEYMGR, AES_WFIFO_DATA) = inw[i].udata;
/* Wait for the result. */
if (!wait_read_data(GREG32_ADDR(KEYMGR, AES_RFIFO_EMPTY))) {
@@ -94,9 +94,9 @@ int DCRYPTO_aes_block(const uint8_t *in, uint8_t *out)
}
/* Read ciphertext. */
- outw = (uint32_t *) out;
+ outw = (struct access_helper *) out;
for (i = 0; i < 4; i++)
- outw[i] = GREG32(KEYMGR, AES_RFIFO_DATA);
+ outw[i].udata = GREG32(KEYMGR, AES_RFIFO_DATA);
return 1;
}
diff --git a/board/cr50/dcrypto/internal.h b/board/cr50/dcrypto/internal.h
index 11f5c9a35d..5eb76ecc8a 100644
--- a/board/cr50/dcrypto/internal.h
+++ b/board/cr50/dcrypto/internal.h
@@ -9,12 +9,19 @@
#ifndef EC_BOARD_CR50_DCRYPTO_INTERNAL_H_
#define EC_BOARD_CR50_DCRYPTO_INTERNAL_H_
-#include "registers.h"
-#include <inttypes.h>
+#include "common.h"
#define CTRL_CTR_BIG_ENDIAN (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
#define CTRL_ENABLE 1
#define CTRL_ENCRYPT 1
#define CTRL_NO_SOFT_RESET 0
+/*
+ * Use this structure to avoid alignment problems with input and output
+ * pointers.
+ */
+struct access_helper {
+ uint32_t udata;
+} __packed;
+
#endif /* ! EC_BOARD_CR50_DCRYPTO_INTERNAL_H_ */