mkimage: fit: add support to encrypt image with aes
[oweals/u-boot.git] / common / image-cipher.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2019, Softathome
4  */
5
6 #ifdef USE_HOSTCC
7 #include "mkimage.h"
8 #include <time.h>
9 #else
10 #include <common.h>
11 #include <malloc.h>
12 DECLARE_GLOBAL_DATA_PTR;
13 #endif /* !USE_HOSTCC*/
14 #include <image.h>
15 #include <uboot_aes.h>
16 #include <u-boot/aes.h>
17
18 struct cipher_algo cipher_algos[] = {
19         {
20                 .name = "aes128",
21                 .key_len = AES128_KEY_LENGTH,
22                 .iv_len  = AES_BLOCK_LENGTH,
23 #if IMAGE_ENABLE_ENCRYPT
24                 .calculate_type = EVP_aes_128_cbc,
25 #endif
26                 .encrypt = image_aes_encrypt,
27                 .add_cipher_data = image_aes_add_cipher_data
28         },
29         {
30                 .name = "aes192",
31                 .key_len = AES192_KEY_LENGTH,
32                 .iv_len  = AES_BLOCK_LENGTH,
33 #if IMAGE_ENABLE_ENCRYPT
34                 .calculate_type = EVP_aes_192_cbc,
35 #endif
36                 .encrypt = image_aes_encrypt,
37                 .add_cipher_data = image_aes_add_cipher_data
38         },
39         {
40                 .name = "aes256",
41                 .key_len = AES256_KEY_LENGTH,
42                 .iv_len  = AES_BLOCK_LENGTH,
43 #if IMAGE_ENABLE_ENCRYPT
44                 .calculate_type = EVP_aes_256_cbc,
45 #endif
46                 .encrypt = image_aes_encrypt,
47                 .add_cipher_data = image_aes_add_cipher_data
48         }
49 };
50
51 struct cipher_algo *image_get_cipher_algo(const char *full_name)
52 {
53         int i;
54         const char *name;
55
56         for (i = 0; i < ARRAY_SIZE(cipher_algos); i++) {
57                 name = cipher_algos[i].name;
58                 if (!strncmp(name, full_name, strlen(name)))
59                         return &cipher_algos[i];
60         }
61
62         return NULL;
63 }