1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "utils/common.h"
8 #include "aes_wrap.h"
9 #include "sha256.h"
10 #include "crypto.h"
11 #include "md5.h"
12 #include "sha1.h"
13 #include "aes.h"
14 #include "esp_wpa.h"
15 #include "ccmp.h"
16 #include "esp_rom_crc.h"
17
18 #define DEFAULT_KEK_LEN 16
19
esp_aes_wrap(const u8 * kek,int n,const u8 * plain,u8 * cipher)20 static int esp_aes_wrap(const u8 *kek, int n, const u8 *plain, u8 *cipher)
21 {
22 return aes_wrap(kek, DEFAULT_KEK_LEN, n, plain, cipher);
23 }
24
esp_aes_unwrap(const u8 * kek,int n,const u8 * cipher,u8 * plain)25 static int esp_aes_unwrap(const u8 *kek, int n, const u8 *cipher, u8 *plain)
26 {
27 return aes_unwrap(kek, DEFAULT_KEK_LEN, n, cipher, plain);
28 }
29
esp_aes_encrypt(void * ctx,const u8 * plain,u8 * crypt)30 static void esp_aes_encrypt(void *ctx, const u8 *plain, u8 *crypt)
31 {
32 aes_encrypt(ctx, plain, crypt);
33 }
34
esp_aes_decrypt(void * ctx,const u8 * crypt,u8 * plain)35 static void esp_aes_decrypt(void *ctx, const u8 *crypt, u8 *plain)
36 {
37 aes_decrypt(ctx, crypt, plain);
38 }
39
esp_aes_gmac(const u8 * key,size_t key_len,const u8 * iv,size_t iv_len,const u8 * aad,size_t aad_len,u8 * tag)40 static int esp_aes_gmac(const u8 *key, size_t key_len, const u8 *iv, size_t iv_len,
41 const u8 *aad, size_t aad_len, u8 *tag)
42 {
43 #if CONFIG_GMAC
44 return aes_gmac(key, key_len, iv, iv_len, aad, aad_len, tag);
45 #else
46 return -1;
47 #endif
48 }
49
esp_supp_crc32(uint32_t crc,uint8_t const * buf,uint32_t len)50 static uint32_t esp_supp_crc32(uint32_t crc, uint8_t const *buf, uint32_t len)
51 {
52 return esp_rom_crc32_le(crc, buf, len);
53 }
54
55 /*
56 * This structure is used to set the cyrpto callback function for station to connect when in security mode.
57 * These functions either call MbedTLS API's if CONFIG_CRYPTO_MBEDTLS flag is set through Kconfig, or native
58 * API's otherwise. We recommend setting the flag since MbedTLS API's utilize hardware acceleration while
59 * native API's are use software implementations.
60 */
61 const wpa_crypto_funcs_t g_wifi_default_wpa_crypto_funcs = {
62 .size = sizeof(wpa_crypto_funcs_t),
63 .version = ESP_WIFI_CRYPTO_VERSION,
64 .aes_wrap = (esp_aes_wrap_t)esp_aes_wrap,
65 .aes_unwrap = (esp_aes_unwrap_t)esp_aes_unwrap,
66 .hmac_sha256_vector = (esp_hmac_sha256_vector_t)hmac_sha256_vector,
67 .sha256_prf = (esp_sha256_prf_t)sha256_prf,
68 .hmac_md5 = (esp_hmac_md5_t)hmac_md5,
69 .hamc_md5_vector = (esp_hmac_md5_vector_t)hmac_md5_vector,
70 .hmac_sha1 = (esp_hmac_sha1_t)hmac_sha1,
71 .hmac_sha1_vector = (esp_hmac_sha1_vector_t)hmac_sha1_vector,
72 .sha1_prf = (esp_sha1_prf_t)sha1_prf,
73 .sha1_vector = (esp_sha1_vector_t)sha1_vector,
74 .pbkdf2_sha1 = (esp_pbkdf2_sha1_t)pbkdf2_sha1,
75 .rc4_skip = (esp_rc4_skip_t)rc4_skip,
76 .md5_vector = (esp_md5_vector_t)md5_vector,
77 .aes_encrypt = (esp_aes_encrypt_t)esp_aes_encrypt,
78 .aes_encrypt_init = (esp_aes_encrypt_init_t)aes_encrypt_init,
79 .aes_encrypt_deinit = (esp_aes_encrypt_deinit_t)aes_encrypt_deinit,
80 .aes_decrypt = (esp_aes_decrypt_t)esp_aes_decrypt,
81 .aes_decrypt_init = (esp_aes_decrypt_init_t)aes_decrypt_init,
82 .aes_decrypt_deinit = (esp_aes_decrypt_deinit_t)aes_decrypt_deinit,
83 .aes_128_encrypt = (esp_aes_128_encrypt_t)aes_128_cbc_encrypt,
84 .aes_128_decrypt = (esp_aes_128_decrypt_t)aes_128_cbc_decrypt,
85 .omac1_aes_128 = (esp_omac1_aes_128_t)omac1_aes_128,
86 .ccmp_decrypt = (esp_ccmp_decrypt_t)ccmp_decrypt,
87 .ccmp_encrypt = (esp_ccmp_encrypt_t)ccmp_encrypt,
88 .aes_gmac = (esp_aes_gmac_t)esp_aes_gmac,
89 .sha256_vector = (esp_sha256_vector_t)sha256_vector,
90 .crc32 = (esp_crc32_le_t)esp_supp_crc32,
91 };
92
93 const mesh_crypto_funcs_t g_wifi_default_mesh_crypto_funcs = {
94 .aes_128_encrypt = (esp_aes_128_encrypt_t)aes_128_cbc_encrypt,
95 .aes_128_decrypt = (esp_aes_128_decrypt_t)aes_128_cbc_decrypt,
96 };
97