2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2014-03-05 18:58:39 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Marek Vasut <marex@denx.de>
|
|
|
|
*
|
2020-01-06 14:22:35 +00:00
|
|
|
* Command for en/de-crypting block of memory with AES-[128/192/256]-CBC cipher.
|
2014-03-05 18:58:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <command.h>
|
2017-04-05 16:08:00 +00:00
|
|
|
#include <uboot_aes.h>
|
2014-03-05 18:58:39 +00:00
|
|
|
#include <malloc.h>
|
|
|
|
#include <asm/byteorder.h>
|
|
|
|
#include <linux/compiler.h>
|
2019-09-24 09:32:29 +00:00
|
|
|
#include <mapmem.h>
|
2014-03-05 18:58:39 +00:00
|
|
|
|
2020-01-06 14:22:35 +00:00
|
|
|
u32 aes_get_key_len(char *command)
|
|
|
|
{
|
|
|
|
u32 key_len = AES128_KEY_LENGTH;
|
|
|
|
|
|
|
|
if (!strcmp(command, "aes.192"))
|
|
|
|
key_len = AES192_KEY_LENGTH;
|
|
|
|
else if (!strcmp(command, "aes.256"))
|
|
|
|
key_len = AES256_KEY_LENGTH;
|
|
|
|
|
|
|
|
return key_len;
|
|
|
|
}
|
|
|
|
|
2014-03-05 18:58:39 +00:00
|
|
|
/**
|
|
|
|
* do_aes() - Handle the "aes" command-line command
|
|
|
|
* @cmdtp: Command data struct pointer
|
|
|
|
* @flag: Command flag
|
|
|
|
* @argc: Command-line argument count
|
|
|
|
* @argv: Array of command-line arguments
|
|
|
|
*
|
|
|
|
* 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[])
|
|
|
|
{
|
2018-01-03 12:43:56 +00:00
|
|
|
uint32_t key_addr, iv_addr, src_addr, dst_addr, len;
|
|
|
|
uint8_t *key_ptr, *iv_ptr, *src_ptr, *dst_ptr;
|
2020-01-06 14:22:35 +00:00
|
|
|
u8 key_exp[AES256_EXPAND_KEY_LENGTH];
|
|
|
|
u32 aes_blocks, key_len;
|
2014-03-05 18:58:39 +00:00
|
|
|
int enc;
|
|
|
|
|
2018-01-03 12:43:56 +00:00
|
|
|
if (argc != 7)
|
2014-03-05 18:58:39 +00:00
|
|
|
return CMD_RET_USAGE;
|
|
|
|
|
2020-01-06 14:22:35 +00:00
|
|
|
key_len = aes_get_key_len(argv[0]);
|
|
|
|
|
2014-03-05 18:58:39 +00:00
|
|
|
if (!strncmp(argv[1], "enc", 3))
|
|
|
|
enc = 1;
|
|
|
|
else if (!strncmp(argv[1], "dec", 3))
|
|
|
|
enc = 0;
|
|
|
|
else
|
|
|
|
return CMD_RET_USAGE;
|
|
|
|
|
|
|
|
key_addr = simple_strtoul(argv[2], NULL, 16);
|
2018-01-03 12:43:56 +00:00
|
|
|
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);
|
2014-03-05 18:58:39 +00:00
|
|
|
|
2020-01-06 14:22:35 +00:00
|
|
|
key_ptr = (uint8_t *)map_sysmem(key_addr, key_len);
|
2019-09-24 09:32:29 +00:00
|
|
|
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);
|
2014-03-05 18:58:39 +00:00
|
|
|
|
|
|
|
/* First we expand the key. */
|
2020-01-06 14:22:35 +00:00
|
|
|
aes_expand_key(key_ptr, key_len, key_exp);
|
2014-03-05 18:58:39 +00:00
|
|
|
|
|
|
|
/* Calculate the number of AES blocks to encrypt. */
|
2020-01-06 14:22:34 +00:00
|
|
|
aes_blocks = DIV_ROUND_UP(len, AES_BLOCK_LENGTH);
|
2014-03-05 18:58:39 +00:00
|
|
|
|
|
|
|
if (enc)
|
2020-01-06 14:22:35 +00:00
|
|
|
aes_cbc_encrypt_blocks(key_len, key_exp, iv_ptr, src_ptr,
|
|
|
|
dst_ptr, aes_blocks);
|
2014-03-05 18:58:39 +00:00
|
|
|
else
|
2020-01-06 14:22:35 +00:00
|
|
|
aes_cbc_decrypt_blocks(key_len, key_exp, iv_ptr, src_ptr,
|
|
|
|
dst_ptr, aes_blocks);
|
2014-03-05 18:58:39 +00:00
|
|
|
|
2019-09-24 09:32:29 +00:00
|
|
|
unmap_sysmem(key_ptr);
|
|
|
|
unmap_sysmem(iv_ptr);
|
|
|
|
unmap_sysmem(src_ptr);
|
|
|
|
unmap_sysmem(dst_ptr);
|
|
|
|
|
2014-03-05 18:58:39 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************/
|
|
|
|
#ifdef CONFIG_SYS_LONGHELP
|
|
|
|
static char aes_help_text[] =
|
2020-01-06 14:22:35 +00:00
|
|
|
"[.128,.192,.256] enc key iv src dst len - Encrypt block of data $len bytes long\n"
|
2018-01-03 12:43:56 +00:00
|
|
|
" 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"
|
2020-01-06 14:22:35 +00:00
|
|
|
"aes [.128,.192,.256] dec key iv src dst len - Decrypt block of data $len bytes long\n"
|
2018-01-03 12:43:56 +00:00
|
|
|
" 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.";
|
2014-03-05 18:58:39 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
U_BOOT_CMD(
|
2018-01-03 12:43:56 +00:00
|
|
|
aes, 7, 1, do_aes,
|
2020-01-06 14:22:35 +00:00
|
|
|
"AES 128/192/256 CBC encryption",
|
2014-03-05 18:58:39 +00:00
|
|
|
aes_help_text
|
|
|
|
);
|