mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-08-05 22:11:04 +08:00
feat(util): Add faster AES for ESP8266 SoC
This commit is contained in:
@ -13,6 +13,10 @@
|
||||
* See README and COPYING for more details.
|
||||
*/
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#ifndef CONFIG_ESP_AES
|
||||
|
||||
#include "crypto/includes.h"
|
||||
|
||||
#include "crypto/common.h"
|
||||
@ -86,3 +90,5 @@ aes_128_cbc_decrypt(const u8 *key, const u8 *iv, u8 *data, size_t data_len)
|
||||
aes_decrypt_deinit(ctx);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_ESP_AES */
|
||||
|
@ -21,6 +21,10 @@
|
||||
* See README and COPYING for more details.
|
||||
*/
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#ifndef CONFIG_ESP_AES
|
||||
|
||||
#include "crypto/includes.h"
|
||||
|
||||
#include "crypto/common.h"
|
||||
@ -170,3 +174,41 @@ void aes_decrypt_deinit(void *ctx)
|
||||
os_memset(ctx, 0, AES_PRIV_SIZE);
|
||||
os_free(ctx);
|
||||
}
|
||||
|
||||
#else /* CONFIG_ESP_AES */
|
||||
|
||||
#include <stdint.h>
|
||||
#include "esp_aes.h"
|
||||
#include "esp_heap_caps.h"
|
||||
|
||||
void *aes_decrypt_init(const uint8_t *key, size_t len)
|
||||
{
|
||||
int ret;
|
||||
esp_aes_t *ctx;
|
||||
|
||||
ctx = heap_caps_malloc(sizeof(esp_aes_t), MALLOC_CAP_8BIT);
|
||||
if (!ctx)
|
||||
return NULL;
|
||||
|
||||
ret = esp_aes_set_decrypt_key(ctx, key, len * 8);
|
||||
if (ret)
|
||||
goto fail;
|
||||
|
||||
return ctx;
|
||||
|
||||
fail:
|
||||
heap_caps_free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void aes_decrypt(void *ctx, const uint8_t *crypt, uint8_t *plain)
|
||||
{
|
||||
esp_aes_decrypt_ecb(ctx, crypt, plain);
|
||||
}
|
||||
|
||||
void aes_decrypt_deinit(void *ctx)
|
||||
{
|
||||
heap_caps_free(ctx);
|
||||
}
|
||||
|
||||
#endif /* CONFIG_ESP_AES */
|
||||
|
@ -21,6 +21,10 @@
|
||||
* See README and COPYING for more details.
|
||||
*/
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#ifndef CONFIG_ESP_AES
|
||||
|
||||
#include "crypto/includes.h"
|
||||
#include "crypto/common.h"
|
||||
#include "crypto/crypto.h"
|
||||
@ -132,3 +136,41 @@ void aes_encrypt_deinit(void *ctx)
|
||||
os_memset(ctx, 0, AES_PRIV_SIZE);
|
||||
os_free(ctx);
|
||||
}
|
||||
|
||||
#else /* CONFIG_ESP_AES */
|
||||
|
||||
#include <stdint.h>
|
||||
#include "esp_aes.h"
|
||||
#include "esp_heap_caps.h"
|
||||
|
||||
void *aes_encrypt_init(const uint8_t *key, size_t len)
|
||||
{
|
||||
int ret;
|
||||
esp_aes_t *ctx;
|
||||
|
||||
ctx = heap_caps_malloc(sizeof(esp_aes_t), MALLOC_CAP_8BIT);
|
||||
if (!ctx)
|
||||
return NULL;
|
||||
|
||||
ret = esp_aes_set_encrypt_key(ctx, key, len * 8);
|
||||
if (ret)
|
||||
goto fail;
|
||||
|
||||
return ctx;
|
||||
|
||||
fail:
|
||||
heap_caps_free(ctx);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void aes_encrypt(void *ctx, const uint8_t *crypt, uint8_t *plain)
|
||||
{
|
||||
esp_aes_encrypt_ecb(ctx, crypt, plain);
|
||||
}
|
||||
|
||||
void aes_encrypt_deinit(void *ctx)
|
||||
{
|
||||
heap_caps_free(ctx);
|
||||
}
|
||||
|
||||
#endif /* CONFIG_ESP_AES */
|
||||
|
@ -54,6 +54,10 @@
|
||||
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#ifndef CONFIG_ESP_AES
|
||||
|
||||
#define AES_SMALL_TABLES
|
||||
|
||||
/*
|
||||
@ -852,3 +856,5 @@ int rijndaelKeySetupEnc(u32 rk[], const u8 cipherKey[], int keyBits)
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_ESP_AES */
|
||||
|
@ -13,6 +13,10 @@
|
||||
* See README and COPYING for more details.
|
||||
*/
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#ifndef CONFIG_ESP_AES
|
||||
|
||||
#include "crypto/includes.h"
|
||||
|
||||
#include "crypto/common.h"
|
||||
@ -78,3 +82,58 @@ aes_unwrap(const u8 *kek, int n, const u8 *cipher, u8 *plain)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
#include <string.h>
|
||||
#include "esp_aes.h"
|
||||
|
||||
int aes_unwrap(const uint8_t *kek, int n, const uint8_t *cipher, uint8_t *plain)
|
||||
{
|
||||
int ret;
|
||||
uint8_t a[8], *r, b[16];
|
||||
esp_aes_t ctx;
|
||||
|
||||
/* 1) Initialize variables. */
|
||||
memcpy(a, cipher, 8);
|
||||
r = plain;
|
||||
memcpy(r, cipher + 8, 8 * n);
|
||||
|
||||
ret = esp_aes_set_decrypt_key(&ctx, kek, 128);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
/* 2) Compute intermediate values.
|
||||
* For j = 5 to 0
|
||||
* For i = n to 1
|
||||
* B = AES-1(K, (A ^ t) | R[i]) where t = n*j+i
|
||||
* A = MSB(64, B)
|
||||
* R[i] = LSB(64, B)
|
||||
*/
|
||||
for (int j = 5; j >= 0; j--) {
|
||||
r = plain + (n - 1) * 8;
|
||||
for (int i = n; i >= 1; i--) {
|
||||
memcpy(b, a, 8);
|
||||
b[7] ^= n * j + i;
|
||||
|
||||
memcpy(b + 8, r, 8);
|
||||
esp_aes_decrypt_ecb(&ctx, b, b);
|
||||
memcpy(a, b, 8);
|
||||
memcpy(r, b + 8, 8);
|
||||
r -= 8;
|
||||
}
|
||||
}
|
||||
|
||||
/* 3) Output results.
|
||||
*
|
||||
* These are already in @plain due to the location of temporary
|
||||
* variables. Just verify that the IV matches with the expected value.
|
||||
*/
|
||||
for (int i = 0; i < 8; i++) {
|
||||
if (a[i] != 0xa6)
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_ESP_AES */
|
||||
|
@ -7,6 +7,10 @@
|
||||
* See README for more details.
|
||||
*/
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#ifndef CONFIG_ESP_AES
|
||||
|
||||
#include "crypto/includes.h"
|
||||
|
||||
#include "crypto/common.h"
|
||||
@ -68,3 +72,54 @@ int aes_wrap(const u8 *kek, int n, const u8 *plain, u8 *cipher)
|
||||
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
#include <string.h>
|
||||
#include "esp_aes.h"
|
||||
|
||||
int aes_wrap(const uint8_t *kek, int n, const uint8_t *plain, uint8_t *cipher)
|
||||
{
|
||||
int ret;
|
||||
uint8_t *a, *r, b[16];
|
||||
esp_aes_t ctx;
|
||||
|
||||
a = cipher;
|
||||
r = cipher + 8;
|
||||
|
||||
/* 1) Initialize variables. */
|
||||
memset(a, 0xa6, 8);
|
||||
memcpy(r, plain, 8 * n);
|
||||
|
||||
ret = esp_aes_set_encrypt_key(&ctx, kek, 128);
|
||||
if (ret)
|
||||
return ret;
|
||||
|
||||
/* 2) Calculate intermediate values.
|
||||
* For j = 0 to 5
|
||||
* For i=1 to n
|
||||
* B = AES(K, A | R[i])
|
||||
* A = MSB(64, B) ^ t where t = (n*j)+i
|
||||
* R[i] = LSB(64, B)
|
||||
*/
|
||||
for (int j = 0; j <= 5; j++) {
|
||||
r = cipher + 8;
|
||||
for (int i = 1; i <= n; i++) {
|
||||
memcpy(b, a, 8);
|
||||
memcpy(b + 8, r, 8);
|
||||
esp_aes_encrypt_ecb(&ctx, b, b);
|
||||
memcpy(a, b, 8);
|
||||
a[7] ^= n * j + i;
|
||||
memcpy(r, b + 8, 8);
|
||||
r += 8;
|
||||
}
|
||||
}
|
||||
|
||||
/* 3) Output the results.
|
||||
*
|
||||
* These are already in @cipher due to the location of temporary
|
||||
* variables.
|
||||
*/
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_ESP_AES */
|
||||
|
Reference in New Issue
Block a user