esp32-crypt.h
1 /* esp32-crypt.h
2  *
3  * Copyright (C) 2006-2020 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL.
6  *
7  * wolfSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * wolfSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20  */
21 #ifndef __ESP32_CRYPT_H__
22 
23 #define __ESP32_CRYPT_H__
24 
25 #include "esp_idf_version.h"
26 #include "esp_types.h"
27 #include "esp_log.h"
28 
29 #ifdef WOLFSSL_ESP32WROOM32_CRYPT_DEBUG
30 #undef LOG_LOCAL_LEVEL
31 #define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
32 #else
33 #undef LOG_LOCAL_LEVEL
34 #define LOG_LOCAL_LEVEL ESP_LOG_ERROR
35 #endif
36 
37 #include <freertos/FreeRTOS.h>
38 #include "soc/dport_reg.h"
39 #include "soc/hwcrypto_reg.h"
40 #include "soc/cpu.h"
41 #include "driver/periph_ctrl.h"
42 #if ESP_IDF_VERSION_MAJOR >= 4 && ESP_IDF_VERSION_MINOR >= 1
43 #include <esp32/rom/ets_sys.h>
44 #else
45 #include <rom/ets_sys.h>
46 #endif
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 int esp_CryptHwMutexInit(wolfSSL_Mutex* mutex);
53 int esp_CryptHwMutexLock(wolfSSL_Mutex* mutex, TickType_t xBloxkTime);
54 int esp_CryptHwMutexUnLock(wolfSSL_Mutex* mutex);
55 
56 #ifndef NO_AES
57 
58 #if ESP_IDF_VERSION_MAJOR >= 4 && ESP_IDF_VERSION_MINOR >= 1
59 #include "esp32/rom/aes.h"
60 #else
61 #include "rom/aes.h"
62 #endif
63 
64 typedef enum tagES32_AES_PROCESS {
65  ESP32_AES_LOCKHW = 1,
66  ESP32_AES_UPDATEKEY_ENCRYPT = 2,
67  ESP32_AES_UPDATEKEY_DECRYPT = 3,
68  ESP32_AES_UNLOCKHW = 4
69 } ESP32_AESPROCESS;
70 
71 struct Aes;
72 int wc_esp32AesCbcEncrypt(struct Aes* aes, byte* out, const byte* in, word32 sz);
73 int wc_esp32AesCbcDecrypt(struct Aes* aes, byte* out, const byte* in, word32 sz);
74 int wc_esp32AesEncrypt(struct Aes *aes, const byte* in, byte* out);
75 int wc_esp32AesDecrypt(struct Aes *aes, const byte* in, byte* out);
76 
77 #endif
78 
79 #ifdef WOLFSSL_ESP32WROOM32_CRYPT_DEBUG
80 
81 void wc_esp32TimerStart();
82 uint64_t wc_esp32elapsedTime();
83 
84 #endif /* WOLFSSL_ESP32WROOM32_CRYPT_DEBUG */
85 
86 #if (!defined(NO_SHA) || !defined(NO_SHA256) || defined(WOLFSSL_SHA384) || \
87  defined(WOLFSSL_SHA512)) && \
88  !defined(NO_WOLFSSL_ESP32WROOM32_CRYPT_HASH)
89 
90 /* RAW hash function APIs are not implemented with esp32 hardware acceleration*/
91 #define WOLFSSL_NO_HASH_RAW
92 #if ESP_IDF_VERSION_MAJOR >= 4 && ESP_IDF_VERSION_MINOR >= 1
93 #include "esp32/rom/sha.h"
94 #else
95 #include "rom/sha.h"
96 #endif
97 
98 typedef enum {
99  ESP32_SHA_INIT = 0,
100  ESP32_SHA_HW = 1,
101  ESP32_SHA_SW = 2,
102 } ESP32_DOSHA;
103 
104 typedef struct {
105  byte isfirstblock;
106  /* 0 , 1 hard, 2 soft */
107  byte mode;
108  /* sha_type */
109  enum SHA_TYPE sha_type;
110 } WC_ESP32SHA;
111 
112 int esp_sha_try_hw_lock(WC_ESP32SHA* ctx);
113 void esp_sha_hw_unlock( void );
114 
115 struct wc_Sha;
116 int esp_sha_digest_process(struct wc_Sha* sha, byte bockprocess);
117 int esp_sha_process(struct wc_Sha* sha, const byte* data);
118 
119 #ifndef NO_SHA256
120  struct wc_Sha256;
121  int esp_sha256_digest_process(struct wc_Sha256* sha, byte bockprocess);
122  int esp_sha256_process(struct wc_Sha256* sha, const byte* data);
123 #endif
124 
125 #if defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA384)
126  struct wc_Sha512;
127  int esp_sha512_process(struct wc_Sha512* sha);
128  int esp_sha512_digest_process(struct wc_Sha512* sha, byte blockproc);
129 #endif
130 
131 #endif /* NO_SHA && */
132 
133 #if !defined(NO_RSA) || defined(HAVE_ECC)
134 
135 #ifndef ESP_RSA_TIMEOUT
136  #define ESP_RSA_TIMEOUT 0xFFFFF
137 #endif
138 
139 struct fp_int;
140 int esp_mp_mul(struct fp_int* X, struct fp_int* Y, struct fp_int* Z);
141 int esp_mp_exptmod(struct fp_int* G, struct fp_int* X, word32 Xbits, struct fp_int* P,
142  struct fp_int* Y);
143 int esp_mp_mulmod(struct fp_int* X, struct fp_int* Y, struct fp_int* M,
144  struct fp_int* Z);
145 
146 #endif /* NO_RSA || HAVE_ECC*/
147 
148 #ifdef __cplusplus
149 }
150 #endif
151 
152 #endif /* __ESP32_CRYPT_H__ */
Definition: sha256.h:132
Definition: tfm.h:317
Definition: aes.h:149
Definition: sha512.h:116
Definition: esp32-crypt.h:104
Definition: wolfcaam_sha.h:68