Blame view
cmd/aes.c
2.82 KB
b401b73d0 aes: Add 'aes' co... |
1 2 3 4 5 6 7 8 9 10 11 |
/* * Copyright (C) 2014 Marek Vasut <marex@denx.de> * * Command for en/de-crypting block of memory with AES-128-CBC cipher. * * SPDX-License-Identifier: GPL-2.0+ */ #include <common.h> #include <command.h> #include <environment.h> |
b80c0b993 Rename aes.h to u... |
12 |
#include <uboot_aes.h> |
b401b73d0 aes: Add 'aes' co... |
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <malloc.h> #include <asm/byteorder.h> #include <linux/compiler.h> DECLARE_GLOBAL_DATA_PTR; /** * do_aes() - Handle the "aes" command-line command * @cmdtp: Command data struct pointer * @flag: Command flag * @argc: Command-line argument count * @argv: Array of command-line arguments * * Returns zero on success, CMD_RET_USAGE in case of misuse and negative * on error. */ static int do_aes(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[]) { |
af09eba64 aes: Allow non-ze... |
31 32 |
uint32_t key_addr, iv_addr, src_addr, dst_addr, len; uint8_t *key_ptr, *iv_ptr, *src_ptr, *dst_ptr; |
b401b73d0 aes: Add 'aes' co... |
33 34 35 |
uint8_t key_exp[AES_EXPAND_KEY_LENGTH]; uint32_t aes_blocks; int enc; |
af09eba64 aes: Allow non-ze... |
36 |
if (argc != 7) |
b401b73d0 aes: Add 'aes' co... |
37 38 39 40 41 42 43 44 45 46 |
return CMD_RET_USAGE; if (!strncmp(argv[1], "enc", 3)) enc = 1; else if (!strncmp(argv[1], "dec", 3)) enc = 0; else return CMD_RET_USAGE; key_addr = simple_strtoul(argv[2], NULL, 16); |
af09eba64 aes: Allow non-ze... |
47 48 49 50 |
iv_addr = simple_strtoul(argv[3], NULL, 16); src_addr = simple_strtoul(argv[4], NULL, 16); dst_addr = simple_strtoul(argv[5], NULL, 16); len = simple_strtoul(argv[6], NULL, 16); |
b401b73d0 aes: Add 'aes' co... |
51 52 |
key_ptr = (uint8_t *)key_addr; |
af09eba64 aes: Allow non-ze... |
53 |
iv_ptr = (uint8_t *)iv_addr; |
b401b73d0 aes: Add 'aes' co... |
54 55 56 57 58 59 60 61 62 63 |
src_ptr = (uint8_t *)src_addr; dst_ptr = (uint8_t *)dst_addr; /* First we expand the key. */ aes_expand_key(key_ptr, key_exp); /* Calculate the number of AES blocks to encrypt. */ aes_blocks = DIV_ROUND_UP(len, AES_KEY_LENGTH); if (enc) |
af09eba64 aes: Allow non-ze... |
64 65 |
aes_cbc_encrypt_blocks(key_exp, iv_ptr, src_ptr, dst_ptr, aes_blocks); |
b401b73d0 aes: Add 'aes' co... |
66 |
else |
af09eba64 aes: Allow non-ze... |
67 68 |
aes_cbc_decrypt_blocks(key_exp, iv_ptr, src_ptr, dst_ptr, aes_blocks); |
b401b73d0 aes: Add 'aes' co... |
69 70 71 72 73 74 75 |
return 0; } /***************************************************/ #ifdef CONFIG_SYS_LONGHELP static char aes_help_text[] = |
af09eba64 aes: Allow non-ze... |
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
"enc key iv src dst len - Encrypt block of data $len bytes long " " at address $src using a key at address " " $key with initialization vector at address " " $iv. Store the result at address $dst. " " The $len size must be multiple of 16 bytes. " " The $key and $iv must be 16 bytes long. " "aes dec key iv src dst len - Decrypt block of data $len bytes long " " at address $src using a key at address " " $key with initialization vector at address " " $iv. Store the result at address $dst. " " The $len size must be multiple of 16 bytes. " " The $key and $iv must be 16 bytes long."; |
b401b73d0 aes: Add 'aes' co... |
99 100 101 |
#endif U_BOOT_CMD( |
af09eba64 aes: Allow non-ze... |
102 |
aes, 7, 1, do_aes, |
b401b73d0 aes: Add 'aes' co... |
103 104 105 |
"AES 128 CBC encryption", aes_help_text ); |