esp enc -> subdir

This commit is contained in:
ok-home
2023-10-09 09:57:55 +07:00
committed by GitHub
parent 2ae66989be
commit e611fd16fe
16 changed files with 1493 additions and 3 deletions

View File

@@ -0,0 +1,6 @@
idf_component_register(SRC_DIRS "."
PRIV_INCLUDE_DIRS "."
REQUIRES unity
PRIV_REQUIRES cmock esp_encrypted_img
EMBED_TXTFILES certs/test_rsa_private_key.pem
EMBED_FILES image.bin)

View File

@@ -0,0 +1,39 @@
-----BEGIN RSA PRIVATE KEY-----
MIIG4wIBAAKCAYEA9eoohqolow2z6JsuBvqLLP+jfA/ha8t03skR0F4cKMXNFoc2
QbqdYjRBaFOrkoUD/KC2TzvCjU3OZ3wuGDDjFWURsezUeYCH+r4PcF/qE6vXW1kV
ueE5jImTtxSsnt1HLFAGiUnILUJ7xi3cd86Y8mF0VH2wfmDKo8ESBRbev0eChCJA
xdynuuyo0m+IN9r7cNStCKz3jglipjnI5+OxtJgw/0fMaCfjtn7KIFktGEeXqJ1r
uRBALN+i70zZjjDtJj37FL5t4LCgrkImwLpBALVYFXy1wXhMXq7h5cU2Ec6bRVBl
6GH1xx2lABQ1LVFwrilgklLjY5UTdx8GHE/veR4Sla3dYyE5MKLJ3Kmc7NxlPlWn
WTmUKAWYIJiRIwBHSBntUgGyBYBqzfBSYFWL3gmTmvV/JQ41laX7qykdabvgdGSR
LJF6wZMdpJpvafYDBRwV7psvhPkHEXBUSe2dtfWKStiUPT7gcZE1ICMxT26Qlaf9
T2GvYwyq4WBkEntzAgMBAAECggGAJKYUChW7bDRzlnvh/SpDqZ4jmC6psq3sqfMf
U4Vi/vSTnwLhpCQSpnsRMGIf1MM8F98/rElEslhhJW0NVY+bmCmq3HBmLgFowoam
uGGi+fGHM9bv9PbK49XxDLzpCPgDTmhSwQ0c5xncZmmZTMWeZ6j8dEcTEZKNQKBa
diW1Zp5apiSQsKw01xfEBTCYBXL+PA+GBh/4+NMPP6Sm+2AksLxpuPHTVcZ0GlOE
/hMsNE0fHgLv9fGlD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0WU8/YGK51co
bodTAPZ5T/5Rh3CvC9+aVMURYho7Fz3cnH36AlZC1/8Hkm+Rcf7eg9ih5p3j5CGN
BAcoCC+gpnKrLc0+n0ZpmoHn+iI3peIKPtr3zIr1Kt0P5L4vq66HPdQ7gx2ufvvT
CAnYnZ0bknPsDYWKx9BV8/0kgq/BXnyMxmBmujpqllBdRP4J5RZy7BvlOHWNuE37
OP+ZsNzRdyBh9n9uxQWYABswtLrOSWAVp6E7PrHYmgg26kKpAoHABDv9fxlBrrCh
VGCRevH4ojHOoSvF0FqDzg0ixttPBxOYlKB6ggf+vOBjZVkE5ahON7IvTBvfiPAY
/H9J3uV6OGufzZURuXNdad1NFbcQvfHWvIgGhBozZe5b1seImG21PXDWIb2uZotw
Fg7eaXVYZDeA9jMA9roBbDnwypLAXO4Ve6/J9VXZmOBeXV9rbb0RuWEXgQ9FI1fT
CG3od/wm0D0sbZsAoNMD/fVOYRhbxQESpzynsgoxlWfzv2+t3Wjn
-----END RSA PRIVATE KEY-----

Binary file not shown.

View File

@@ -0,0 +1,359 @@
/*
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include "unity.h"
#if __has_include("esp_random.h")
#include "esp_random.h"
#else
#include "esp_system.h"
#endif
#include "esp_encrypted_img.h"
extern const uint8_t rsa_private_pem_start[] asm("_binary_test_rsa_private_key_pem_start");
extern const uint8_t rsa_private_pem_end[] asm("_binary_test_rsa_private_key_pem_end");
extern const uint8_t bin_start[] asm("_binary_image_bin_start");
extern const uint8_t bin_end[] asm("_binary_image_bin_end");
TEST_CASE("Sending all data at once", "[encrypted_img]")
{
esp_decrypt_cfg_t cfg = {
.rsa_priv_key = (char *)rsa_private_pem_start,
.rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start,
};
esp_decrypt_handle_t ctx = esp_encrypted_img_decrypt_start(&cfg);
TEST_ASSERT_NOT_NULL(ctx);
pre_enc_decrypt_arg_t *args = calloc(1, sizeof(pre_enc_decrypt_arg_t));
TEST_ASSERT_NOT_NULL(args);
args->data_in = (char *)bin_start;
args->data_in_len = bin_end - bin_start;
esp_err_t err;
err = esp_encrypted_img_decrypt_data(ctx, args);
TEST_ESP_OK(err);
printf("Successful\n");
printf("\n");
err = esp_encrypted_img_decrypt_end(ctx);
TEST_ESP_OK(err);
if (args->data_out) {
free(args->data_out);
}
free(args);
}
TEST_CASE("Sending 1 byte data at once", "[encrypted_img]")
{
esp_decrypt_cfg_t cfg = {
.rsa_priv_key = (char *)rsa_private_pem_start,
.rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start,
};
esp_decrypt_handle_t ctx = esp_encrypted_img_decrypt_start(&cfg);
TEST_ASSERT_NOT_NULL(ctx);
pre_enc_decrypt_arg_t *args = calloc(1, sizeof(pre_enc_decrypt_arg_t));
TEST_ASSERT_NOT_NULL(args);
esp_err_t err;
int i = 0;
do {
args->data_in = (char *)(bin_start + i);
i++;
args->data_in_len = 1;
err = esp_encrypted_img_decrypt_data(ctx, args);
if (err == ESP_FAIL) {
printf("ESP_FAIL ERROR\n");
break;
}
} while (err != ESP_OK);
TEST_ESP_OK(err);
err = esp_encrypted_img_decrypt_end(ctx);
TEST_ESP_OK(err);
if (args->data_out) {
free(args->data_out);
}
free(args);
}
TEST_CASE("Invalid Magic", "[encrypted_img]")
{
uint8_t cipher[] = {
0x34, 0x12, 0xbc, 0xec,
0xf5, 0x3a, 0x72, 0x55, 0x36, 0x0f, 0x4a, 0x92,
0x5f, 0x15, 0xef, 0xbb, 0xe8, 0x9c, 0x7e, 0x43,
0x7c, 0x87, 0x3d, 0x00, 0xb4, 0x22, 0xcc, 0x35,
0x77, 0x92, 0x6a, 0x44, 0x80, 0x83, 0x9d, 0x0d,
0x4c, 0x28, 0x5b, 0x7d, 0xbd, 0xef, 0x59, 0x56,
0xba, 0x0f, 0x9d, 0xab, 0x3b, 0x09, 0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x04, 0x36, 0x96, 0x60, 0x48, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x5a, 0x2f, 0x2a, 0x16,
0x6c, 0x8b, 0x34, 0x4e, 0xc0
};
esp_decrypt_cfg_t cfg = {
.rsa_priv_key = (char *)rsa_private_pem_start,
.rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start,
};
esp_decrypt_handle_t ctx = esp_encrypted_img_decrypt_start(&cfg);
TEST_ASSERT_NOT_NULL(ctx);
pre_enc_decrypt_arg_t *args = calloc(1, sizeof(pre_enc_decrypt_arg_t));
TEST_ASSERT_NOT_NULL(args);
args->data_in = (char *)cipher;
args->data_in_len = 521;
esp_err_t err;
err = esp_encrypted_img_decrypt_data(ctx, args);
TEST_ESP_ERR(ESP_FAIL, err);
err = esp_encrypted_img_decrypt_end(ctx);
free(args);
}
TEST_CASE("Invalid Image", "[encrypted_img]")
{
//"Espressif" is encoded using GCM key. After successful decoding, "Espressif" will be printed.
uint8_t cipher[] = {
0xcf, 0xb6, 0x88, 0x07,
0xf5, 0x3a, 0x72, 0x55, 0x36, 0x0f, 0x4a, 0x92,
0x5f, 0x15, 0xef, 0xbb, 0xe8, 0x9c, 0x7e, 0x43,
0x7c, 0x87, 0x3d, 0x00, 0xb4, 0x22, 0xcc, 0x35,
0x77, 0x92, 0x6a, 0x44, 0x80, 0x83, 0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
0x09, 0x00, 0x00, 0x00, 0x00, 0xb5, 0x9a, 0xf1,
0xe2, 0x4a, 0xe8, 0xea, 0xed, 0x68, 0x15, 0xf4,
0x04, 0x36, 0x96, 0x60, 0x48, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x5a, 0x2f, 0x2a, 0x16,
0x6c, 0x8b, 0x34, 0x4e, 0xd0
};
esp_decrypt_cfg_t cfg = {
.rsa_priv_key = (char *)rsa_private_pem_start,
.rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start,
};
esp_decrypt_handle_t ctx = esp_encrypted_img_decrypt_start(&cfg);
TEST_ASSERT_NOT_NULL(ctx);
pre_enc_decrypt_arg_t *args = calloc(1, sizeof(pre_enc_decrypt_arg_t));
TEST_ASSERT_NOT_NULL(args);
args->data_in = (char *)cipher;
args->data_in_len = 521;
esp_err_t err;
err = esp_encrypted_img_decrypt_data(ctx, args);
TEST_ESP_OK(err);
err = esp_encrypted_img_decrypt_end(ctx);
TEST_ESP_ERR(ESP_FAIL, err);
if (args->data_out) {
free(args->data_out);
}
free(args);
}
TEST_CASE("Sending random size data at once", "[encrypted_img]")
{
esp_decrypt_cfg_t cfg = {
.rsa_priv_key = (char *)rsa_private_pem_start,
.rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start,
};
esp_decrypt_handle_t ctx = esp_encrypted_img_decrypt_start(&cfg);
TEST_ASSERT_NOT_NULL(ctx);
pre_enc_decrypt_arg_t *args = calloc(1, sizeof(pre_enc_decrypt_arg_t));
TEST_ASSERT_NOT_NULL(args);
esp_err_t err;
int i = 0;
do {
uint32_t y = esp_random();
y = (y % 16) + 1;
uint32_t x = y < ((bin_end - bin_start) - i) ? y : ((bin_end - bin_start) - i);
args->data_in = (char *)(bin_start + i);
i += x;
args->data_in_len = x;
err = esp_encrypted_img_decrypt_data(ctx, args);
if (err == ESP_FAIL) {
printf("ESP_FAIL ERROR\n");
break;
}
} while (err != ESP_OK);
TEST_ESP_OK(err);
err = esp_encrypted_img_decrypt_end(ctx);
TEST_ESP_OK(err);
if (args->data_out) {
free(args->data_out);
}
free(args);
}
TEST_CASE("Sending imcomplete data", "[encrypted_img]")
{
esp_decrypt_cfg_t cfg = {
.rsa_priv_key = (char *)rsa_private_pem_start,
.rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start,
};
esp_decrypt_handle_t ctx = esp_encrypted_img_decrypt_start(&cfg);
TEST_ASSERT_NOT_NULL(ctx);
pre_enc_decrypt_arg_t *args = calloc(1, sizeof(pre_enc_decrypt_arg_t));
TEST_ASSERT_NOT_NULL(args);
args->data_in = (char *)bin_start;
args->data_in_len = (bin_end - bin_start) - 1;
esp_err_t err;
err = esp_encrypted_img_decrypt_data(ctx, args);
TEST_ESP_ERR(ESP_ERR_NOT_FINISHED, err);
TEST_ESP_ERR(false, esp_encrypted_img_is_complete_data_received(ctx));
err = esp_encrypted_img_decrypt_abort(ctx);
TEST_ESP_OK(err);
if (args->data_out) {
free(args->data_out);
}
free(args);
}
TEST_CASE("Test canceling decryption frees memory", "[encrypted_img]")
{
esp_decrypt_cfg_t cfg = {
.rsa_priv_key = (char *)rsa_private_pem_start,
.rsa_priv_key_len = rsa_private_pem_end - rsa_private_pem_start,
};
int free_bytes_start = xPortGetFreeHeapSize();
esp_decrypt_handle_t ctx = esp_encrypted_img_decrypt_start(&cfg);
TEST_ASSERT_NOT_NULL(ctx);
esp_err_t err = esp_encrypted_img_decrypt_abort(ctx);
TEST_ESP_OK(err);
int free_bytes_end = xPortGetFreeHeapSize();
// +/- 16 bytes to allow for some small fluctuations
TEST_ASSERT(abs(free_bytes_start - free_bytes_end) <= 16);
}