summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMarek Vasut <marex@denx.de>2014-03-05 19:58:37 +0100
committerTom Rini <trini@ti.com>2014-03-21 16:43:53 -0400
commit6e7b9f4fa0aed88051d6700afa510430b5d3fb69 (patch)
tree7c17953ff2ef5dc5e1279bd6b4fb894c9d72e3bd /lib
parent957ba85ce9bbf9d82716798287e50612afb07fc7 (diff)
downloadu-boot-imx-6e7b9f4fa0aed88051d6700afa510430b5d3fb69.zip
u-boot-imx-6e7b9f4fa0aed88051d6700afa510430b5d3fb69.tar.gz
u-boot-imx-6e7b9f4fa0aed88051d6700afa510430b5d3fb69.tar.bz2
aes: Move the AES-128-CBC encryption function to common code
Move the AES-128-CBC encryption function implemented in tegra20-common/crypto.c into lib/aes.c . This is well re-usable common code. Moreover, clean the code up a bit and fix the kerneldoc-style annotations. Signed-off-by: Marek Vasut <marex@denx.de>
Diffstat (limited to 'lib')
-rw-r--r--lib/aes.c52
1 files changed, 52 insertions, 0 deletions
diff --git a/lib/aes.c b/lib/aes.c
index e996b27..a6648f9 100644
--- a/lib/aes.c
+++ b/lib/aes.c
@@ -580,3 +580,55 @@ void aes_decrypt(u8 *in, u8 *expkey, u8 *out)
memcpy(out, state, sizeof(state));
}
+
+static void debug_print_vector(char *name, u32 num_bytes, u8 *data)
+{
+#ifdef DEBUG
+ printf("%s [%d] @0x%08x", name, num_bytes, (u32)data);
+ print_buffer(0, data, 1, num_bytes, 16);
+#endif
+}
+
+/**
+ * Apply chain data to the destination using EOR
+ *
+ * Each array is of length AES_KEY_LENGTH.
+ *
+ * @cbc_chain_data Chain data
+ * @src Source data
+ * @dst Destination data, which is modified here
+ */
+static void apply_cbc_chain_data(u8 *cbc_chain_data, u8 *src, u8 *dst)
+{
+ int i;
+
+ for (i = 0; i < AES_KEY_LENGTH; i++)
+ *dst++ = *src++ ^ *cbc_chain_data++;
+}
+
+void aes_cbc_encrypt_blocks(u8 *key_exp, u8 *src, u8 *dst, u32 num_aes_blocks)
+{
+ u8 zero_key[AES_KEY_LENGTH] = { 0 };
+ u8 tmp_data[AES_KEY_LENGTH];
+ /* Convenient array of 0's for IV */
+ u8 *cbc_chain_data = zero_key;
+ u32 i;
+
+ for (i = 0; i < num_aes_blocks; i++) {
+ debug("encrypt_object: block %d of %d\n", i, num_aes_blocks);
+ debug_print_vector("AES Src", AES_KEY_LENGTH, src);
+
+ /* Apply the chain data */
+ apply_cbc_chain_data(cbc_chain_data, src, tmp_data);
+ debug_print_vector("AES Xor", AES_KEY_LENGTH, tmp_data);
+
+ /* Encrypt the AES block */
+ aes_encrypt(tmp_data, key_exp, dst);
+ debug_print_vector("AES Dst", AES_KEY_LENGTH, dst);
+
+ /* Update pointers for next loop. */
+ cbc_chain_data = dst;
+ src += AES_KEY_LENGTH;
+ dst += AES_KEY_LENGTH;
+ }
+}