1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019, Softathome
12 #include <asm/global_data.h>
13 DECLARE_GLOBAL_DATA_PTR;
14 #endif /* !USE_HOSdTCC*/
16 #include <uboot_aes.h>
17 #include <u-boot/aes.h>
19 struct cipher_algo cipher_algos[] = {
22 .key_len = AES128_KEY_LENGTH,
23 .iv_len = AES_BLOCK_LENGTH,
24 #if IMAGE_ENABLE_ENCRYPT
25 .calculate_type = EVP_aes_128_cbc,
27 .encrypt = image_aes_encrypt,
28 .decrypt = image_aes_decrypt,
29 .add_cipher_data = image_aes_add_cipher_data
33 .key_len = AES192_KEY_LENGTH,
34 .iv_len = AES_BLOCK_LENGTH,
35 #if IMAGE_ENABLE_ENCRYPT
36 .calculate_type = EVP_aes_192_cbc,
38 .encrypt = image_aes_encrypt,
39 .decrypt = image_aes_decrypt,
40 .add_cipher_data = image_aes_add_cipher_data
44 .key_len = AES256_KEY_LENGTH,
45 .iv_len = AES_BLOCK_LENGTH,
46 #if IMAGE_ENABLE_ENCRYPT
47 .calculate_type = EVP_aes_256_cbc,
49 .encrypt = image_aes_encrypt,
50 .decrypt = image_aes_decrypt,
51 .add_cipher_data = image_aes_add_cipher_data
55 struct cipher_algo *image_get_cipher_algo(const char *full_name)
60 for (i = 0; i < ARRAY_SIZE(cipher_algos); i++) {
61 name = cipher_algos[i].name;
62 if (!strncmp(name, full_name, strlen(name)))
63 return &cipher_algos[i];
69 static int fit_image_setup_decrypt(struct image_cipher_info *info,
70 const void *fit, int image_noffset,
73 const void *fdt = gd_fdt_blob();
74 const char *node_name;
80 node_name = fit_get_name(fit, image_noffset, NULL);
82 printf("Can't get node name\n");
86 if (fit_image_cipher_get_algo(fit, cipher_noffset, &algo_name)) {
87 printf("Can't get algo name for cipher '%s' in image '%s'\n",
88 node_name, node_name);
92 info->keyname = fdt_getprop(fit, cipher_noffset, FIT_KEY_HINT, NULL);
94 printf("Can't get key name\n");
98 info->iv = fdt_getprop(fit, cipher_noffset, "iv", NULL);
99 info->ivname = fdt_getprop(fit, cipher_noffset, "iv-name-hint", NULL);
101 if (!info->iv && !info->ivname) {
102 printf("Can't get IV or IV name\n");
107 info->node_noffset = image_noffset;
108 info->name = algo_name;
109 info->cipher = image_get_cipher_algo(algo_name);
111 printf("Can't get cipher\n");
115 ret = fit_image_get_data_size_unciphered(fit, image_noffset,
116 &info->size_unciphered);
118 printf("Can't get size of unciphered data\n");
123 * Search the cipher node in the u-boot fdt
124 * the path should be: /cipher/key-<algo>-<key>-<iv>
127 snprintf(node_path, sizeof(node_path), "/%s/key-%s-%s-%s",
128 FIT_CIPHER_NODENAME, algo_name, info->keyname, info->ivname);
130 snprintf(node_path, sizeof(node_path), "/%s/key-%s-%s",
131 FIT_CIPHER_NODENAME, algo_name, info->keyname);
133 noffset = fdt_path_offset(fdt, node_path);
135 printf("Can't found cipher node offset\n");
140 info->key = fdt_getprop(fdt, noffset, "key", NULL);
142 printf("Can't get key in cipher node '%s'\n", node_path);
148 info->iv = fdt_getprop(fdt, noffset, "iv", NULL);
150 printf("Can't get IV in cipher node '%s'\n", node_path);
158 int fit_image_decrypt_data(const void *fit,
159 int image_noffset, int cipher_noffset,
160 const void *data_ciphered, size_t size_ciphered,
161 void **data_unciphered, size_t *size_unciphered)
163 struct image_cipher_info info;
166 ret = fit_image_setup_decrypt(&info, fit, image_noffset,
171 ret = info.cipher->decrypt(&info, data_ciphered, size_ciphered,
172 data_unciphered, size_unciphered);