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 | * |
| 5 | * Command for en/de-crypting block of memory with AES-128-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 | |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 16 | /** |
| 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 | */ |
| 26 | static int do_aes(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
| 27 | { |
Андрей Мозжухин | 9aaa236 | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 28 | uint32_t key_addr, iv_addr, src_addr, dst_addr, len; |
| 29 | uint8_t *key_ptr, *iv_ptr, *src_ptr, *dst_ptr; |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 30 | uint8_t key_exp[AES_EXPAND_KEY_LENGTH]; |
| 31 | uint32_t aes_blocks; |
| 32 | int enc; |
| 33 | |
Андрей Мозжухин | 9aaa236 | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 34 | if (argc != 7) |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 35 | 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); |
Андрей Мозжухин | 9aaa236 | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 45 | 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 Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 49 | |
Philippe Reynes | 93f80ea | 2019-09-24 11:32:29 +0200 | [diff] [blame^] | 50 | key_ptr = (uint8_t *)map_sysmem(key_addr, 128 / 8); |
| 51 | iv_ptr = (uint8_t *)map_sysmem(iv_addr, 128 / 8); |
| 52 | src_ptr = (uint8_t *)map_sysmem(src_addr, len); |
| 53 | dst_ptr = (uint8_t *)map_sysmem(dst_addr, len); |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 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) |
Андрей Мозжухин | 9aaa236 | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 62 | aes_cbc_encrypt_blocks(key_exp, iv_ptr, src_ptr, dst_ptr, |
| 63 | aes_blocks); |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 64 | else |
Андрей Мозжухин | 9aaa236 | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 65 | aes_cbc_decrypt_blocks(key_exp, iv_ptr, src_ptr, dst_ptr, |
| 66 | aes_blocks); |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 67 | |
Philippe Reynes | 93f80ea | 2019-09-24 11:32:29 +0200 | [diff] [blame^] | 68 | unmap_sysmem(key_ptr); |
| 69 | unmap_sysmem(iv_ptr); |
| 70 | unmap_sysmem(src_ptr); |
| 71 | unmap_sysmem(dst_ptr); |
| 72 | |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | /***************************************************/ |
| 77 | #ifdef CONFIG_SYS_LONGHELP |
| 78 | static char aes_help_text[] = |
Андрей Мозжухин | 9aaa236 | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 79 | "enc key iv src dst len - Encrypt block of data $len bytes long\n" |
| 80 | " at address $src using a key at address\n" |
| 81 | " $key with initialization vector at address\n" |
| 82 | " $iv. Store the result at address $dst.\n" |
| 83 | " The $len size must be multiple of 16 bytes.\n" |
| 84 | " The $key and $iv must be 16 bytes long.\n" |
| 85 | "aes dec key iv src dst len - Decrypt block of data $len bytes long\n" |
| 86 | " at address $src using a key at address\n" |
| 87 | " $key with initialization vector at address\n" |
| 88 | " $iv. Store the result at address $dst.\n" |
| 89 | " The $len size must be multiple of 16 bytes.\n" |
| 90 | " The $key and $iv must be 16 bytes long."; |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 91 | #endif |
| 92 | |
| 93 | U_BOOT_CMD( |
Андрей Мозжухин | 9aaa236 | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 94 | aes, 7, 1, do_aes, |
Marek Vasut | c4d8a1b | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 95 | "AES 128 CBC encryption", |
| 96 | aes_help_text |
| 97 | ); |