Marek Vasut | b401b73 | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 1 | /* |
| 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 Babic | b80c0b9 | 2017-04-05 18:08:00 +0200 | [diff] [blame] | 12 | #include <uboot_aes.h> |
Marek Vasut | b401b73 | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 13 | #include <malloc.h> |
| 14 | #include <asm/byteorder.h> |
| 15 | #include <linux/compiler.h> |
| 16 | |
Marek Vasut | b401b73 | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 17 | /** |
| 18 | * do_aes() - Handle the "aes" command-line command |
| 19 | * @cmdtp: Command data struct pointer |
| 20 | * @flag: Command flag |
| 21 | * @argc: Command-line argument count |
| 22 | * @argv: Array of command-line arguments |
| 23 | * |
| 24 | * Returns zero on success, CMD_RET_USAGE in case of misuse and negative |
| 25 | * on error. |
| 26 | */ |
| 27 | static int do_aes(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) |
| 28 | { |
Андрей Мозжухин | af09eba | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 29 | uint32_t key_addr, iv_addr, src_addr, dst_addr, len; |
| 30 | uint8_t *key_ptr, *iv_ptr, *src_ptr, *dst_ptr; |
Marek Vasut | b401b73 | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 31 | uint8_t key_exp[AES_EXPAND_KEY_LENGTH]; |
| 32 | uint32_t aes_blocks; |
| 33 | int enc; |
| 34 | |
Андрей Мозжухин | af09eba | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 35 | if (argc != 7) |
Marek Vasut | b401b73 | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 36 | return CMD_RET_USAGE; |
| 37 | |
| 38 | if (!strncmp(argv[1], "enc", 3)) |
| 39 | enc = 1; |
| 40 | else if (!strncmp(argv[1], "dec", 3)) |
| 41 | enc = 0; |
| 42 | else |
| 43 | return CMD_RET_USAGE; |
| 44 | |
| 45 | key_addr = simple_strtoul(argv[2], NULL, 16); |
Андрей Мозжухин | af09eba | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 46 | iv_addr = simple_strtoul(argv[3], NULL, 16); |
| 47 | src_addr = simple_strtoul(argv[4], NULL, 16); |
| 48 | dst_addr = simple_strtoul(argv[5], NULL, 16); |
| 49 | len = simple_strtoul(argv[6], NULL, 16); |
Marek Vasut | b401b73 | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 50 | |
| 51 | key_ptr = (uint8_t *)key_addr; |
Андрей Мозжухин | af09eba | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 52 | iv_ptr = (uint8_t *)iv_addr; |
Marek Vasut | b401b73 | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 53 | src_ptr = (uint8_t *)src_addr; |
| 54 | dst_ptr = (uint8_t *)dst_addr; |
| 55 | |
| 56 | /* First we expand the key. */ |
| 57 | aes_expand_key(key_ptr, key_exp); |
| 58 | |
| 59 | /* Calculate the number of AES blocks to encrypt. */ |
| 60 | aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH); |
| 61 | |
| 62 | if (enc) |
Андрей Мозжухин | af09eba | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 63 | aes_cbc_encrypt_blocks(key_exp, iv_ptr, src_ptr, dst_ptr, |
| 64 | aes_blocks); |
Marek Vasut | b401b73 | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 65 | else |
Андрей Мозжухин | af09eba | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 66 | aes_cbc_decrypt_blocks(key_exp, iv_ptr, src_ptr, dst_ptr, |
| 67 | aes_blocks); |
Marek Vasut | b401b73 | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 68 | |
| 69 | return 0; |
| 70 | } |
| 71 | |
| 72 | /***************************************************/ |
| 73 | #ifdef CONFIG_SYS_LONGHELP |
| 74 | static char aes_help_text[] = |
Андрей Мозжухин | af09eba | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 75 | "enc key iv src dst len - Encrypt block of data $len bytes long\n" |
| 76 | " at address $src using a key at address\n" |
| 77 | " $key with initialization vector at address\n" |
| 78 | " $iv. Store the result at address $dst.\n" |
| 79 | " The $len size must be multiple of 16 bytes.\n" |
| 80 | " The $key and $iv must be 16 bytes long.\n" |
| 81 | "aes dec key iv src dst len - Decrypt block of data $len bytes long\n" |
| 82 | " at address $src using a key at address\n" |
| 83 | " $key with initialization vector at address\n" |
| 84 | " $iv. Store the result at address $dst.\n" |
| 85 | " The $len size must be multiple of 16 bytes.\n" |
| 86 | " The $key and $iv must be 16 bytes long."; |
Marek Vasut | b401b73 | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 87 | #endif |
| 88 | |
| 89 | U_BOOT_CMD( |
Андрей Мозжухин | af09eba | 2018-01-03 15:43:56 +0300 | [diff] [blame] | 90 | aes, 7, 1, do_aes, |
Marek Vasut | b401b73 | 2014-03-05 19:58:39 +0100 | [diff] [blame] | 91 | "AES 128 CBC encryption", |
| 92 | aes_help_text |
| 93 | ); |