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