Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2014 Marek Vasut <marex@denx.de> |
| 4 | * |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 5 | * Command for en/de-crypting block of memory with AES-[128/192/256]-CBC cipher. |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <command.h> |
Stefano Babic | 4a24308 | 2017-04-05 18:08:00 +0200 | [diff] [blame] | 10 | #include <uboot_aes.h> |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 11 | #include <malloc.h> |
| 12 | #include <asm/byteorder.h> |
| 13 | #include <linux/compiler.h> |
Philippe Reynes | 93f80ea | 2019-09-24 11:32:29 +0200 | [diff] [blame] | 14 | #include <mapmem.h> |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 15 | |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 16 | u32 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 Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 28 | /** |
| 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 Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 38 | static int do_aes(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 39 | { |
Андрей Мозжухин | 9aaa236 | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 40 | uint32_t key_addr, iv_addr, src_addr, dst_addr, len; |
| 41 | uint8_t *key_ptr, *iv_ptr, *src_ptr, *dst_ptr; |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 42 | u8 key_exp[AES256_EXPAND_KEY_LENGTH]; |
| 43 | u32 aes_blocks, key_len; |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 44 | int enc; |
| 45 | |
Андрей Мозжухин | 9aaa236 | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 46 | if (argc != 7) |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 47 | return CMD_RET_USAGE; |
| 48 | |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 49 | key_len = aes_get_key_len(argv[0]); |
| 50 | |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 51 | 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 Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 58 | 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 Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 63 | |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 64 | key_ptr = (uint8_t *)map_sysmem(key_addr, key_len); |
Philippe Reynes | 93f80ea | 2019-09-24 11:32:29 +0200 | [diff] [blame] | 65 | 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 Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 68 | |
| 69 | /* First we expand the key. */ |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 70 | aes_expand_key(key_ptr, key_len, key_exp); |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 71 | |
| 72 | /* Calculate the number of AES blocks to encrypt. */ |
Philippe Reynes | 458db3c | 2020-01-06 15:22:34 +0100 | [diff] [blame] | 73 | aes_blocks = DIV_ROUND_UP(len, AES_BLOCK_LENGTH); |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 74 | |
| 75 | if (enc) |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 76 | aes_cbc_encrypt_blocks(key_len, key_exp, iv_ptr, src_ptr, |
| 77 | dst_ptr, aes_blocks); |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 78 | else |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 79 | aes_cbc_decrypt_blocks(key_len, key_exp, iv_ptr, src_ptr, |
| 80 | dst_ptr, aes_blocks); |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 81 | |
Philippe Reynes | 93f80ea | 2019-09-24 11:32:29 +0200 | [diff] [blame] | 82 | unmap_sysmem(key_ptr); |
| 83 | unmap_sysmem(iv_ptr); |
| 84 | unmap_sysmem(src_ptr); |
| 85 | unmap_sysmem(dst_ptr); |
| 86 | |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | /***************************************************/ |
| 91 | #ifdef CONFIG_SYS_LONGHELP |
| 92 | static char aes_help_text[] = |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 93 | "[.128,.192,.256] enc key iv src dst len - Encrypt block of data $len bytes long\n" |
Андрей Мозжухин | 9aaa236 | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 94 | " at address $src using a key at address\n" |
| 95 | " $key with initialization vector at address\n" |
| 96 | " $iv. Store the result at address $dst.\n" |
| 97 | " The $len size must be multiple of 16 bytes.\n" |
| 98 | " The $key and $iv must be 16 bytes long.\n" |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 99 | "aes [.128,.192,.256] dec key iv src dst len - Decrypt block of data $len bytes long\n" |
Андрей Мозжухин | 9aaa236 | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 100 | " at address $src using a key at address\n" |
| 101 | " $key with initialization vector at address\n" |
| 102 | " $iv. Store the result at address $dst.\n" |
| 103 | " The $len size must be multiple of 16 bytes.\n" |
| 104 | " The $key and $iv must be 16 bytes long."; |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 105 | #endif |
| 106 | |
| 107 | U_BOOT_CMD( |
Андрей Мозжухин | 9aaa236 | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 108 | aes, 7, 1, do_aes, |
Philippe Reynes | a40315e | 2020-01-06 15:22:35 +0100 | [diff] [blame] | 109 | "AES 128/192/256 CBC encryption", |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 110 | aes_help_text |
| 111 | ); |