blob: d3b6e454bf46d61b4ccd9c9fae47b19434903c77 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Ruchika Gupta4345a572014-10-07 15:46:20 +05302/*
3 *
4 * Command for encapsulating/decapsulating blob of memory.
Ruchika Gupta4345a572014-10-07 15:46:20 +05305 */
6
7#include <common.h>
8#include <command.h>
9#include <environment.h>
10#include <malloc.h>
11#include <asm/byteorder.h>
12#include <linux/compiler.h>
13
Ruchika Gupta4345a572014-10-07 15:46:20 +053014/**
15 * blob_decap() - Decapsulate the data as a blob
16 * @key_mod: - Pointer to key modifier/key
17 * @src: - Address of data to be decapsulated
18 * @dst: - Address of data to be decapsulated
19 * @len: - Size of data to be decapsulated
20 *
21 * Returns zero on success,and negative on error.
22 */
23__weak int blob_decap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
24{
25 return 0;
26}
27
28/**
29 * blob_encap() - Encapsulate the data as a blob
30 * @key_mod: - Pointer to key modifier/key
31 * @src: - Address of data to be encapsulated
32 * @dst: - Address of data to be encapsulated
33 * @len: - Size of data to be encapsulated
34 *
35 * Returns zero on success,and negative on error.
36 */
37__weak int blob_encap(u8 *key_mod, u8 *src, u8 *dst, u32 len)
38{
39 return 0;
40}
41
42/**
43 * do_blob() - Handle the "blob" command-line command
44 * @cmdtp: Command data struct pointer
45 * @flag: Command flag
46 * @argc: Command-line argument count
47 * @argv: Array of command-line arguments
48 *
49 * Returns zero on success, CMD_RET_USAGE in case of misuse and negative
50 * on error.
51 */
52static int do_blob(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
53{
Sumit Gargff9eb052016-07-14 13:27:50 -040054 ulong key_addr, src_addr, dst_addr, len;
Ruchika Gupta4345a572014-10-07 15:46:20 +053055 uint8_t *km_ptr, *src_ptr, *dst_ptr;
56 int enc, ret = 0;
57
58 if (argc != 6)
59 return CMD_RET_USAGE;
60
61 if (!strncmp(argv[1], "enc", 3))
62 enc = 1;
63 else if (!strncmp(argv[1], "dec", 3))
64 enc = 0;
65 else
66 return CMD_RET_USAGE;
67
68 src_addr = simple_strtoul(argv[2], NULL, 16);
69 dst_addr = simple_strtoul(argv[3], NULL, 16);
70 len = simple_strtoul(argv[4], NULL, 16);
71 key_addr = simple_strtoul(argv[5], NULL, 16);
72
Aneesh Bansalb3e98202015-12-08 13:54:29 +053073 km_ptr = (uint8_t *)(uintptr_t)key_addr;
74 src_ptr = (uint8_t *)(uintptr_t)src_addr;
75 dst_ptr = (uint8_t *)(uintptr_t)dst_addr;
Ruchika Gupta4345a572014-10-07 15:46:20 +053076
77 if (enc)
78 ret = blob_encap(km_ptr, src_ptr, dst_ptr, len);
79 else
80 ret = blob_decap(km_ptr, src_ptr, dst_ptr, len);
81
82 return ret;
83}
84
85/***************************************************/
86static char blob_help_text[] =
87 "enc src dst len km - Encapsulate and create blob of data\n"
88 " $len bytes long at address $src and\n"
89 " store the result at address $dst.\n"
gaurav ranab64308e2015-02-25 09:37:09 +053090 " $km is the address where the key\n"
91 " modifier is stored.\n"
92 " The modifier is required for generation\n"
93 " /use as key for cryptographic operation.\n"
94 " Key modifier should be 16 byte long.\n"
Ruchika Gupta4345a572014-10-07 15:46:20 +053095 "blob dec src dst len km - Decapsulate the blob of data at address\n"
96 " $src and store result of $len byte at\n"
97 " addr $dst.\n"
gaurav ranab64308e2015-02-25 09:37:09 +053098 " $km is the address where the key\n"
99 " modifier is stored.\n"
100 " The modifier is required for generation\n"
101 " /use as key for cryptographic operation.\n"
102 " Key modifier should be 16 byte long.\n";
Ruchika Gupta4345a572014-10-07 15:46:20 +0530103
104U_BOOT_CMD(
105 blob, 6, 1, do_blob,
106 "Blob encapsulation/decryption",
107 blob_help_text
108);