blob: 25efb9642a2b23f30dbe56d236a9675af54c2d9c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutb401b732014-03-05 19:58:39 +01002/*
3 * Copyright (C) 2014 Marek Vasut <marex@denx.de>
4 *
5 * Command for en/de-crypting block of memory with AES-128-CBC cipher.
Marek Vasutb401b732014-03-05 19:58:39 +01006 */
7
8#include <common.h>
9#include <command.h>
10#include <environment.h>
Stefano Babicb80c0b92017-04-05 18:08:00 +020011#include <uboot_aes.h>
Marek Vasutb401b732014-03-05 19:58:39 +010012#include <malloc.h>
13#include <asm/byteorder.h>
14#include <linux/compiler.h>
15
Marek Vasutb401b732014-03-05 19:58:39 +010016/**
17 * do_aes() - Handle the "aes" command-line command
18 * @cmdtp: Command data struct pointer
19 * @flag: Command flag
20 * @argc: Command-line argument count
21 * @argv: Array of command-line arguments
22 *
23 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
24 * on error.
25 */
26static int do_aes(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
27{
Андрей Мозжухинaf09eba2018-01-03 15:43:56 +030028 uint32_t key_addr, iv_addr, src_addr, dst_addr, len;
29 uint8_t *key_ptr, *iv_ptr, *src_ptr, *dst_ptr;
Marek Vasutb401b732014-03-05 19:58:39 +010030 uint8_t key_exp[AES_EXPAND_KEY_LENGTH];
31 uint32_t aes_blocks;
32 int enc;
33
Андрей Мозжухинaf09eba2018-01-03 15:43:56 +030034 if (argc != 7)
Marek Vasutb401b732014-03-05 19:58:39 +010035 return CMD_RET_USAGE;
36
37 if (!strncmp(argv[1], "enc", 3))
38 enc = 1;
39 else if (!strncmp(argv[1], "dec", 3))
40 enc = 0;
41 else
42 return CMD_RET_USAGE;
43
44 key_addr = simple_strtoul(argv[2], NULL, 16);
Андрей Мозжухинaf09eba2018-01-03 15:43:56 +030045 iv_addr = simple_strtoul(argv[3], NULL, 16);
46 src_addr = simple_strtoul(argv[4], NULL, 16);
47 dst_addr = simple_strtoul(argv[5], NULL, 16);
48 len = simple_strtoul(argv[6], NULL, 16);
Marek Vasutb401b732014-03-05 19:58:39 +010049
50 key_ptr = (uint8_t *)key_addr;
Андрей Мозжухинaf09eba2018-01-03 15:43:56 +030051 iv_ptr = (uint8_t *)iv_addr;
Marek Vasutb401b732014-03-05 19:58:39 +010052 src_ptr = (uint8_t *)src_addr;
53 dst_ptr = (uint8_t *)dst_addr;
54
55 /* First we expand the key. */
56 aes_expand_key(key_ptr, key_exp);
57
58 /* Calculate the number of AES blocks to encrypt. */
59 aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH);
60
61 if (enc)
Андрей Мозжухинaf09eba2018-01-03 15:43:56 +030062 aes_cbc_encrypt_blocks(key_exp, iv_ptr, src_ptr, dst_ptr,
63 aes_blocks);
Marek Vasutb401b732014-03-05 19:58:39 +010064 else
Андрей Мозжухинaf09eba2018-01-03 15:43:56 +030065 aes_cbc_decrypt_blocks(key_exp, iv_ptr, src_ptr, dst_ptr,
66 aes_blocks);
Marek Vasutb401b732014-03-05 19:58:39 +010067
68 return 0;
69}
70
71/***************************************************/
72#ifdef CONFIG_SYS_LONGHELP
73static char aes_help_text[] =
Андрей Мозжухинaf09eba2018-01-03 15:43:56 +030074 "enc key iv src dst len - Encrypt block of data $len bytes long\n"
75 " at address $src using a key at address\n"
76 " $key with initialization vector at address\n"
77 " $iv. Store the result at address $dst.\n"
78 " The $len size must be multiple of 16 bytes.\n"
79 " The $key and $iv must be 16 bytes long.\n"
80 "aes dec key iv src dst len - Decrypt block of data $len bytes long\n"
81 " at address $src using a key at address\n"
82 " $key with initialization vector at address\n"
83 " $iv. Store the result at address $dst.\n"
84 " The $len size must be multiple of 16 bytes.\n"
85 " The $key and $iv must be 16 bytes long.";
Marek Vasutb401b732014-03-05 19:58:39 +010086#endif
87
88U_BOOT_CMD(
Андрей Мозжухинaf09eba2018-01-03 15:43:56 +030089 aes, 7, 1, do_aes,
Marek Vasutb401b732014-03-05 19:58:39 +010090 "AES 128 CBC encryption",
91 aes_help_text
92);