Blame view

cmd/aes.c 3.3 KB
83d290c56   Tom Rini   SPDX: Convert all...
1
  // SPDX-License-Identifier: GPL-2.0+
b401b73d0   Marek Vasut   aes: Add 'aes' co...
2
3
4
  /*
   * Copyright (C) 2014 Marek Vasut <marex@denx.de>
   *
8302d1708   Philippe Reynes   aes: add support ...
5
   * Command for en/de-crypting block of memory with AES-[128/192/256]-CBC cipher.
b401b73d0   Marek Vasut   aes: Add 'aes' co...
6
7
8
9
   */
  
  #include <common.h>
  #include <command.h>
b80c0b993   Stefano Babic   Rename aes.h to u...
10
  #include <uboot_aes.h>
b401b73d0   Marek Vasut   aes: Add 'aes' co...
11
12
13
  #include <malloc.h>
  #include <asm/byteorder.h>
  #include <linux/compiler.h>
ed7ea058a   Philippe Reynes   cmd: aes: use map...
14
  #include <mapmem.h>
b401b73d0   Marek Vasut   aes: Add 'aes' co...
15

8302d1708   Philippe Reynes   aes: add support ...
16
17
18
19
20
21
22
23
24
25
26
  u32 aes_get_key_len(char *command)
  {
  	u32 key_len = AES128_KEY_LENGTH;
  
  	if (!strcmp(command, "aes.192"))
  		key_len = AES192_KEY_LENGTH;
  	else if (!strcmp(command, "aes.256"))
  		key_len = AES256_KEY_LENGTH;
  
  	return key_len;
  }
b401b73d0   Marek Vasut   aes: Add 'aes' co...
27
28
29
30
31
32
33
34
35
36
37
38
  /**
   * 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...
39
40
  	uint32_t key_addr, iv_addr, src_addr, dst_addr, len;
  	uint8_t *key_ptr, *iv_ptr, *src_ptr, *dst_ptr;
8302d1708   Philippe Reynes   aes: add support ...
41
42
  	u8 key_exp[AES256_EXPAND_KEY_LENGTH];
  	u32 aes_blocks, key_len;
b401b73d0   Marek Vasut   aes: Add 'aes' co...
43
  	int enc;
af09eba64   Андрей Мозжухин   aes: Allow non-ze...
44
  	if (argc != 7)
b401b73d0   Marek Vasut   aes: Add 'aes' co...
45
  		return CMD_RET_USAGE;
8302d1708   Philippe Reynes   aes: add support ...
46
  	key_len = aes_get_key_len(argv[0]);
b401b73d0   Marek Vasut   aes: Add 'aes' co...
47
48
49
50
51
52
53
54
  	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...
55
56
57
58
  	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   Marek Vasut   aes: Add 'aes' co...
59

8302d1708   Philippe Reynes   aes: add support ...
60
  	key_ptr = (uint8_t *)map_sysmem(key_addr, key_len);
ed7ea058a   Philippe Reynes   cmd: aes: use map...
61
62
63
  	iv_ptr = (uint8_t *)map_sysmem(iv_addr, 128 / 8);
  	src_ptr = (uint8_t *)map_sysmem(src_addr, len);
  	dst_ptr = (uint8_t *)map_sysmem(dst_addr, len);
b401b73d0   Marek Vasut   aes: Add 'aes' co...
64
65
  
  	/* First we expand the key. */
8302d1708   Philippe Reynes   aes: add support ...
66
  	aes_expand_key(key_ptr, key_len, key_exp);
b401b73d0   Marek Vasut   aes: Add 'aes' co...
67
68
  
  	/* Calculate the number of AES blocks to encrypt. */
7012c04ef   Philippe Reynes   aes: add a define...
69
  	aes_blocks = DIV_ROUND_UP(len, AES_BLOCK_LENGTH);
b401b73d0   Marek Vasut   aes: Add 'aes' co...
70
71
  
  	if (enc)
8302d1708   Philippe Reynes   aes: add support ...
72
73
  		aes_cbc_encrypt_blocks(key_len, key_exp, iv_ptr, src_ptr,
  				       dst_ptr, aes_blocks);
b401b73d0   Marek Vasut   aes: Add 'aes' co...
74
  	else
8302d1708   Philippe Reynes   aes: add support ...
75
76
  		aes_cbc_decrypt_blocks(key_len, key_exp, iv_ptr, src_ptr,
  				       dst_ptr, aes_blocks);
b401b73d0   Marek Vasut   aes: Add 'aes' co...
77

ed7ea058a   Philippe Reynes   cmd: aes: use map...
78
79
80
81
  	unmap_sysmem(key_ptr);
  	unmap_sysmem(iv_ptr);
  	unmap_sysmem(src_ptr);
  	unmap_sysmem(dst_ptr);
b401b73d0   Marek Vasut   aes: Add 'aes' co...
82
83
84
85
86
87
  	return 0;
  }
  
  /***************************************************/
  #ifdef CONFIG_SYS_LONGHELP
  static char aes_help_text[] =
8302d1708   Philippe Reynes   aes: add support ...
88
89
  	"[.128,.192,.256] enc key iv src dst len - Encrypt block of data $len bytes long
  "
af09eba64   Андрей Мозжухин   aes: Allow non-ze...
90
91
92
93
94
95
96
97
98
99
  	"                             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.
  "
8302d1708   Philippe Reynes   aes: add support ...
100
101
  	"aes [.128,.192,.256] dec key iv src dst len - Decrypt block of data $len bytes long
  "
af09eba64   Андрей Мозжухин   aes: Allow non-ze...
102
103
104
105
106
107
108
109
110
  	"                             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   Marek Vasut   aes: Add 'aes' co...
111
112
113
  #endif
  
  U_BOOT_CMD(
af09eba64   Андрей Мозжухин   aes: Allow non-ze...
114
  	aes, 7, 1, do_aes,
8302d1708   Philippe Reynes   aes: add support ...
115
  	"AES 128/192/256 CBC encryption",
b401b73d0   Marek Vasut   aes: Add 'aes' co...
116
117
  	aes_help_text
  );