blob: 87ad1ab82b9987f8d7e7af19b184f16639829a03 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutc4d8a1b2014-03-05 19:58:39 +01002/*
3 * Copyright (C) 2014 Marek Vasut <marex@denx.de>
4 *
Philippe Reynesa40315e2020-01-06 15:22:35 +01005 * Command for en/de-crypting block of memory with AES-[128/192/256]-CBC cipher.
Marek Vasutc4d8a1b2014-03-05 19:58:39 +01006 */
7
Marek Vasutc4d8a1b2014-03-05 19:58:39 +01008#include <command.h>
Stefano Babic4a243082017-04-05 18:08:00 +02009#include <uboot_aes.h>
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010010#include <malloc.h>
11#include <asm/byteorder.h>
12#include <linux/compiler.h>
Philippe Reynes93f80ea2019-09-24 11:32:29 +020013#include <mapmem.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060014#include <vsprintf.h>
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010015
Philippe Reynesa40315e2020-01-06 15:22:35 +010016u32 aes_get_key_len(char *command)
17{
18 u32 key_len = AES128_KEY_LENGTH;
19
20 if (!strcmp(command, "aes.192"))
21 key_len = AES192_KEY_LENGTH;
22 else if (!strcmp(command, "aes.256"))
23 key_len = AES256_KEY_LENGTH;
24
25 return key_len;
26}
27
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010028/**
29 * do_aes() - Handle the "aes" command-line command
30 * @cmdtp: Command data struct pointer
31 * @flag: Command flag
32 * @argc: Command-line argument count
33 * @argv: Array of command-line arguments
34 *
35 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
36 * on error.
37 */
Simon Glassed38aef2020-05-10 11:40:03 -060038static int do_aes(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010039{
Андрей Мозжухин9aaa2362018-01-03 15:43:56 +030040 uint32_t key_addr, iv_addr, src_addr, dst_addr, len;
41 uint8_t *key_ptr, *iv_ptr, *src_ptr, *dst_ptr;
Philippe Reynesa40315e2020-01-06 15:22:35 +010042 u8 key_exp[AES256_EXPAND_KEY_LENGTH];
43 u32 aes_blocks, key_len;
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010044 int enc;
45
Андрей Мозжухин9aaa2362018-01-03 15:43:56 +030046 if (argc != 7)
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010047 return CMD_RET_USAGE;
48
Philippe Reynesa40315e2020-01-06 15:22:35 +010049 key_len = aes_get_key_len(argv[0]);
50
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010051 if (!strncmp(argv[1], "enc", 3))
52 enc = 1;
53 else if (!strncmp(argv[1], "dec", 3))
54 enc = 0;
55 else
56 return CMD_RET_USAGE;
57
Simon Glass3ff49ec2021-07-24 09:03:29 -060058 key_addr = hextoul(argv[2], NULL);
59 iv_addr = hextoul(argv[3], NULL);
60 src_addr = hextoul(argv[4], NULL);
61 dst_addr = hextoul(argv[5], NULL);
62 len = hextoul(argv[6], NULL);
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010063
Philippe Reynesa40315e2020-01-06 15:22:35 +010064 key_ptr = (uint8_t *)map_sysmem(key_addr, key_len);
Philippe Reynes93f80ea2019-09-24 11:32:29 +020065 iv_ptr = (uint8_t *)map_sysmem(iv_addr, 128 / 8);
66 src_ptr = (uint8_t *)map_sysmem(src_addr, len);
67 dst_ptr = (uint8_t *)map_sysmem(dst_addr, len);
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010068
69 /* First we expand the key. */
Philippe Reynesa40315e2020-01-06 15:22:35 +010070 aes_expand_key(key_ptr, key_len, key_exp);
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010071
72 /* Calculate the number of AES blocks to encrypt. */
Philippe Reynes458db3c2020-01-06 15:22:34 +010073 aes_blocks = DIV_ROUND_UP(len, AES_BLOCK_LENGTH);
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010074
75 if (enc)
Philippe Reynesa40315e2020-01-06 15:22:35 +010076 aes_cbc_encrypt_blocks(key_len, key_exp, iv_ptr, src_ptr,
77 dst_ptr, aes_blocks);
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010078 else
Philippe Reynesa40315e2020-01-06 15:22:35 +010079 aes_cbc_decrypt_blocks(key_len, key_exp, iv_ptr, src_ptr,
80 dst_ptr, aes_blocks);
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010081
Philippe Reynes93f80ea2019-09-24 11:32:29 +020082 unmap_sysmem(key_ptr);
83 unmap_sysmem(iv_ptr);
84 unmap_sysmem(src_ptr);
85 unmap_sysmem(dst_ptr);
86
Marek Vasutc4d8a1b2014-03-05 19:58:39 +010087 return 0;
88}
89
90/***************************************************/
Tom Rini03f146c2023-10-07 15:13:08 -040091U_BOOT_LONGHELP(aes,
Philippe Reynesa40315e2020-01-06 15:22:35 +010092 "[.128,.192,.256] enc key iv src dst len - Encrypt block of data $len bytes long\n"
Андрей Мозжухин9aaa2362018-01-03 15:43:56 +030093 " at address $src using a key at address\n"
94 " $key with initialization vector at address\n"
95 " $iv. Store the result at address $dst.\n"
96 " The $len size must be multiple of 16 bytes.\n"
97 " The $key and $iv must be 16 bytes long.\n"
Philippe Reynesa40315e2020-01-06 15:22:35 +010098 "aes [.128,.192,.256] dec key iv src dst len - Decrypt block of data $len bytes long\n"
Андрей Мозжухин9aaa2362018-01-03 15:43:56 +030099 " at address $src using a key at address\n"
100 " $key with initialization vector at address\n"
101 " $iv. Store the result at address $dst.\n"
102 " The $len size must be multiple of 16 bytes.\n"
Tom Rini03f146c2023-10-07 15:13:08 -0400103 " The $key and $iv must be 16 bytes long.");
Marek Vasutc4d8a1b2014-03-05 19:58:39 +0100104
105U_BOOT_CMD(
Андрей Мозжухин9aaa2362018-01-03 15:43:56 +0300106 aes, 7, 1, do_aes,
Philippe Reynesa40315e2020-01-06 15:22:35 +0100107 "AES 128/192/256 CBC encryption",
Marek Vasutc4d8a1b2014-03-05 19:58:39 +0100108 aes_help_text
109);