blob: 9d1a740beeadbb5b3861364bf0a8de7720d3edf6 [file] [log] [blame]
Marek Vasutc4d8a1b2014-03-05 19:58:39 +01001/*
2 * Copyright (C) 2014 Marek Vasut <marex@denx.de>
3 *
4 * Command for en/de-crypting block of memory with AES-128-CBC cipher.
5 *
6 * SPDX-License-Identifier: GPL-2.0+
7 */
8
9#include <common.h>
10#include <command.h>
11#include <environment.h>
Stefano Babic4a243082017-04-05 18:08:00 +020012#include <uboot_aes.h>
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010013#include <malloc.h>
14#include <asm/byteorder.h>
15#include <linux/compiler.h>
16
17DECLARE_GLOBAL_DATA_PTR;
18
19/**
20 * do_aes() - Handle the "aes" command-line command
21 * @cmdtp: Command data struct pointer
22 * @flag: Command flag
23 * @argc: Command-line argument count
24 * @argv: Array of command-line arguments
25 *
26 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
27 * on error.
28 */
29static int do_aes(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
30{
Андрей Мозжухин9aaa2362018-01-03 15:43:56 +030031 uint32_t key_addr, iv_addr, src_addr, dst_addr, len;
32 uint8_t *key_ptr, *iv_ptr, *src_ptr, *dst_ptr;
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010033 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
34 uint32_t aes_blocks;
35 int enc;
36
Андрей Мозжухин9aaa2362018-01-03 15:43:56 +030037 if (argc != 7)
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010038 return CMD_RET_USAGE;
39
40 if (!strncmp(argv[1], "enc", 3))
41 enc = 1;
42 else if (!strncmp(argv[1], "dec", 3))
43 enc = 0;
44 else
45 return CMD_RET_USAGE;
46
47 key_addr = simple_strtoul(argv[2], NULL, 16);
Андрей Мозжухин9aaa2362018-01-03 15:43:56 +030048 iv_addr = simple_strtoul(argv[3], NULL, 16);
49 src_addr = simple_strtoul(argv[4], NULL, 16);
50 dst_addr = simple_strtoul(argv[5], NULL, 16);
51 len = simple_strtoul(argv[6], NULL, 16);
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010052
53 key_ptr = (uint8_t *)key_addr;
Андрей Мозжухин9aaa2362018-01-03 15:43:56 +030054 iv_ptr = (uint8_t *)iv_addr;
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010055 src_ptr = (uint8_t *)src_addr;
56 dst_ptr = (uint8_t *)dst_addr;
57
58 /* First we expand the key. */
59 aes_expand_key(key_ptr, key_exp);
60
61 /* Calculate the number of AES blocks to encrypt. */
62 aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
63
64 if (enc)
Андрей Мозжухин9aaa2362018-01-03 15:43:56 +030065 aes_cbc_encrypt_blocks(key_exp, iv_ptr, src_ptr, dst_ptr,
66 aes_blocks);
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010067 else
Андрей Мозжухин9aaa2362018-01-03 15:43:56 +030068 aes_cbc_decrypt_blocks(key_exp, iv_ptr, src_ptr, dst_ptr,
69 aes_blocks);
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010070
71 return 0;
72}
73
74/***************************************************/
75#ifdef CONFIG_SYS_LONGHELP
76static char aes_help_text[] =
Андрей Мозжухин9aaa2362018-01-03 15:43:56 +030077 "enc key iv src dst len - Encrypt block of data $len bytes long\n"
78 " at address $src using a key at address\n"
79 " $key with initialization vector at address\n"
80 " $iv. Store the result at address $dst.\n"
81 " The $len size must be multiple of 16 bytes.\n"
82 " The $key and $iv must be 16 bytes long.\n"
83 "aes dec key iv src dst len - Decrypt block of data $len bytes long\n"
84 " at address $src using a key at address\n"
85 " $key with initialization vector at address\n"
86 " $iv. Store the result at address $dst.\n"
87 " The $len size must be multiple of 16 bytes.\n"
88 " The $key and $iv must be 16 bytes long.";
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010089#endif
90
91U_BOOT_CMD(
Андрей Мозжухин9aaa2362018-01-03 15:43:56 +030092 aes, 7, 1, do_aes,
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010093 "AES 128 CBC encryption",
94 aes_help_text
95);