efi_loader: size of secure boot variables
[oweals/u-boot.git] / cmd / aes.c
index ee1ae13c06ece13827475c42fb79aaeeed023133..4c0dad99326abd9160360e22d8c79d1d7f240456 100644 (file)
--- a/cmd/aes.c
+++ b/cmd/aes.c
@@ -1,20 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright (C) 2014 Marek Vasut <marex@denx.de>
  *
- * Command for en/de-crypting block of memory with AES-128-CBC cipher.
- *
- * SPDX-License-Identifier:    GPL-2.0+
+ * Command for en/de-crypting block of memory with AES-[128/192/256]-CBC cipher.
  */
 
 #include <common.h>
 #include <command.h>
-#include <environment.h>
 #include <uboot_aes.h>
 #include <malloc.h>
 #include <asm/byteorder.h>
 #include <linux/compiler.h>
+#include <mapmem.h>
+
+u32 aes_get_key_len(char *command)
+{
+       u32 key_len = AES128_KEY_LENGTH;
 
-DECLARE_GLOBAL_DATA_PTR;
+       if (!strcmp(command, "aes.192"))
+               key_len = AES192_KEY_LENGTH;
+       else if (!strcmp(command, "aes.256"))
+               key_len = AES256_KEY_LENGTH;
+
+       return key_len;
+}
 
 /**
  * do_aes() - Handle the "aes" command-line command
@@ -26,17 +35,19 @@ DECLARE_GLOBAL_DATA_PTR;
  * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
  * on error.
  */
-static int do_aes(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
+static int do_aes(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
 {
-       uint32_t key_addr, src_addr, dst_addr, len;
-       uint8_t *key_ptr, *src_ptr, *dst_ptr;
-       uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
-       uint32_t aes_blocks;
+       uint32_t key_addr, iv_addr, src_addr, dst_addr, len;
+       uint8_t *key_ptr, *iv_ptr, *src_ptr, *dst_ptr;
+       u8 key_exp[AES256_EXPAND_KEY_LENGTH];
+       u32 aes_blocks, key_len;
        int enc;
 
-       if (argc != 6)
+       if (argc != 7)
                return CMD_RET_USAGE;
 
+       key_len = aes_get_key_len(argv[0]);
+
        if (!strncmp(argv[1], "enc", 3))
                enc = 1;
        else if (!strncmp(argv[1], "dec", 3))
@@ -45,24 +56,33 @@ static int do_aes(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
                return CMD_RET_USAGE;
 
        key_addr = simple_strtoul(argv[2], NULL, 16);
-       src_addr = simple_strtoul(argv[3], NULL, 16);
-       dst_addr = simple_strtoul(argv[4], NULL, 16);
-       len = simple_strtoul(argv[5], NULL, 16);
+       iv_addr = simple_strtoul(argv[3], NULL, 16);
+       src_addr = simple_strtoul(argv[4], NULL, 16);
+       dst_addr = simple_strtoul(argv[5], NULL, 16);
+       len = simple_strtoul(argv[6], NULL, 16);
 
-       key_ptr = (uint8_t *)key_addr;
-       src_ptr = (uint8_t *)src_addr;
-       dst_ptr = (uint8_t *)dst_addr;
+       key_ptr = (uint8_t *)map_sysmem(key_addr, key_len);
+       iv_ptr = (uint8_t *)map_sysmem(iv_addr, 128 / 8);
+       src_ptr = (uint8_t *)map_sysmem(src_addr, len);
+       dst_ptr = (uint8_t *)map_sysmem(dst_addr, len);
 
        /* First we expand the key. */
-       aes_expand_key(key_ptr, key_exp);
+       aes_expand_key(key_ptr, key_len, key_exp);
 
        /* Calculate the number of AES blocks to encrypt. */
-       aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
+       aes_blocks = DIV_ROUND_UP(len, AES_BLOCK_LENGTH);
 
        if (enc)
-               aes_cbc_encrypt_blocks(key_exp, src_ptr, dst_ptr, aes_blocks);
+               aes_cbc_encrypt_blocks(key_len, key_exp, iv_ptr, src_ptr,
+                                      dst_ptr, aes_blocks);
        else
-               aes_cbc_decrypt_blocks(key_exp, src_ptr, dst_ptr, aes_blocks);
+               aes_cbc_decrypt_blocks(key_len, key_exp, iv_ptr, src_ptr,
+                                      dst_ptr, aes_blocks);
+
+       unmap_sysmem(key_ptr);
+       unmap_sysmem(iv_ptr);
+       unmap_sysmem(src_ptr);
+       unmap_sysmem(dst_ptr);
 
        return 0;
 }
@@ -70,20 +90,22 @@ static int do_aes(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
 /***************************************************/
 #ifdef CONFIG_SYS_LONGHELP
 static char aes_help_text[] =
-       "enc key src dst len - Encrypt block of data $len bytes long\n"
-       "                          at address $src using a key at address\n"
-       "                          $key and store the result at address\n"
-       "                          $dst. The $len size must be multiple of\n"
-       "                          16 bytes and $key must be 16 bytes long.\n"
-       "aes dec key src dst len - Decrypt block of data $len bytes long\n"
-       "                          at address $src using a key at address\n"
-       "                          $key and store the result at address\n"
-       "                          $dst. The $len size must be multiple of\n"
-       "                          16 bytes and $key must be 16 bytes long.";
+       "[.128,.192,.256] enc key iv src dst len - Encrypt block of data $len bytes long\n"
+       "                             at address $src using a key at address\n"
+       "                             $key with initialization vector at address\n"
+       "                             $iv. Store the result at address $dst.\n"
+       "                             The $len size must be multiple of 16 bytes.\n"
+       "                             The $key and $iv must be 16 bytes long.\n"
+       "aes [.128,.192,.256] dec key iv src dst len - Decrypt block of data $len bytes long\n"
+       "                             at address $src using a key at address\n"
+       "                             $key with initialization vector at address\n"
+       "                             $iv. Store the result at address $dst.\n"
+       "                             The $len size must be multiple of 16 bytes.\n"
+       "                             The $key and $iv must be 16 bytes long.";
 #endif
 
 U_BOOT_CMD(
-       aes, 6, 1, do_aes,
-       "AES 128 CBC encryption",
+       aes, 7, 1, do_aes,
+       "AES 128/192/256 CBC encryption",
        aes_help_text
 );