1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2019,Softathome
10 #include <openssl/bn.h>
11 #include <openssl/rsa.h>
12 #include <openssl/pem.h>
13 #include <openssl/err.h>
14 #include <openssl/ssl.h>
15 #include <openssl/evp.h>
16 #include <openssl/engine.h>
17 #include <uboot_aes.h>
19 #if OPENSSL_VERSION_NUMBER >= 0x10000000L
20 #define HAVE_ERR_REMOVE_THREAD_STATE
23 int image_aes_encrypt(struct image_cipher_info *info,
24 unsigned char *data, int size,
25 unsigned char **cipher, int *cipher_len)
28 unsigned char *buf = NULL;
29 int buf_len, len, ret = 0;
31 /* create and initialise the context */
32 ctx = EVP_CIPHER_CTX_new();
34 printf("Can't create context\n");
38 /* allocate a buffer for the result */
39 buf = malloc(size + AES_BLOCK_LENGTH);
41 printf("Can't allocate memory to encrypt\n");
46 if (EVP_EncryptInit_ex(ctx, info->cipher->calculate_type(),
47 NULL, info->key, info->iv) != 1) {
48 printf("Can't init encryption\n");
53 if (EVP_EncryptUpdate(ctx, buf, &len, data, size) != 1) {
54 printf("Can't encrypt data\n");
61 if (EVP_EncryptFinal_ex(ctx, buf + len, &len) != 1) {
62 printf("Can't finalise the encryption\n");
70 *cipher_len = buf_len;
73 EVP_CIPHER_CTX_free(ctx);
77 int image_aes_add_cipher_data(struct image_cipher_info *info, void *keydest)
83 /* Either create or overwrite the named cipher node */
84 parent = fdt_subnode_offset(keydest, 0, FIT_CIPHER_NODENAME);
85 if (parent == -FDT_ERR_NOTFOUND) {
86 parent = fdt_add_subnode(keydest, 0, FIT_CIPHER_NODENAME);
89 if (ret != -FDT_ERR_NOSPACE) {
91 "Couldn't create cipher node: %s\n",
92 fdt_strerror(parent));
99 /* Either create or overwrite the named key node */
100 snprintf(name, sizeof(name), "key-%s-%s-%s",
101 info->name, info->keyname, info->ivname);
102 node = fdt_subnode_offset(keydest, parent, name);
103 if (node == -FDT_ERR_NOTFOUND) {
104 node = fdt_add_subnode(keydest, parent, name);
107 if (ret != -FDT_ERR_NOSPACE) {
109 "Could not create key subnode: %s\n",
113 } else if (node < 0) {
114 fprintf(stderr, "Cannot select keys parent: %s\n",
120 ret = fdt_setprop(keydest, node, "iv",
121 info->iv, info->cipher->iv_len);
124 ret = fdt_setprop(keydest, node, "key",
125 info->key, info->cipher->key_len);
128 ret = fdt_setprop_u32(keydest, node, "key-len",
129 info->cipher->key_len);
133 ret = ret == -FDT_ERR_NOSPACE ? -ENOSPC : -EIO;