stm32.h
1 /* stm32.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 
22 #ifndef _WOLFPORT_STM32_H_
23 #define _WOLFPORT_STM32_H_
24 
25 /* Generic STM32 Hashing and Crypto Functions */
26 /* Supports CubeMX HAL or Standard Peripheral Library */
27 
28 #include <wolfssl/wolfcrypt/settings.h>
30 
31 #if defined(WOLFSSL_STM32_PKA) && defined(HAVE_ECC)
32  #include <wolfssl/wolfcrypt/integer.h>
33  #include <wolfssl/wolfcrypt/ecc.h>
34 #endif
35 
36 #ifdef STM32_HASH
37 
38 #define WOLFSSL_NO_HASH_RAW
39 
40 #ifdef HASH_DIGEST
41  /* The HASH_DIGEST register indicates SHA224/SHA256 support */
42  #define STM32_HASH_SHA2
43  #define HASH_CR_SIZE 54
44  #define HASH_MAX_DIGEST 32
45 #else
46  #define HASH_CR_SIZE 50
47  #define HASH_MAX_DIGEST 20
48 #endif
49 
50 /* Handle hash differences between CubeMX and StdPeriLib */
51 #if !defined(HASH_ALGOMODE_HASH) && defined(HASH_AlgoMode_HASH)
52  #define HASH_ALGOMODE_HASH HASH_AlgoMode_HASH
53 #endif
54 #if !defined(HASH_DATATYPE_8B) && defined(HASH_DataType_8b)
55  #define HASH_DATATYPE_8B HASH_DataType_8b
56 #endif
57 
58 #ifndef STM32_HASH_TIMEOUT
59  #define STM32_HASH_TIMEOUT 0xFFFF
60 #endif
61 
62 
63 /* STM32 register size in bytes */
64 #define STM32_HASH_REG_SIZE 4
65 
66 /* STM32 Hash Context */
67 typedef struct {
68  /* Context switching registers */
69  uint32_t HASH_IMR;
70  uint32_t HASH_STR;
71  uint32_t HASH_CR;
72  uint32_t HASH_CSR[HASH_CR_SIZE];
73 
74  /* Hash state / buffers */
75  word32 buffer[STM32_HASH_REG_SIZE / sizeof(word32)]; /* partial word buffer */
76  word32 buffLen; /* partial word remain */
77  word32 loLen; /* total update bytes
78  (only lsb 6-bits is used for nbr valid bytes in last word) */
80 
81 
82 /* API's */
83 void wc_Stm32_Hash_Init(STM32_HASH_Context* stmCtx);
84 int wc_Stm32_Hash_Update(STM32_HASH_Context* stmCtx, word32 algo,
85  const byte* data, int len);
86 int wc_Stm32_Hash_Final(STM32_HASH_Context* stmCtx, word32 algo,
87  byte* hash, int digestSize);
88 
89 #endif /* STM32_HASH */
90 
91 
92 #ifdef STM32_CRYPTO
93 
94 #ifndef NO_AES
95  #if !defined(STM32_CRYPTO_AES_GCM) && (defined(WOLFSSL_STM32F4) || \
96  defined(WOLFSSL_STM32F7) || defined(WOLFSSL_STM32L4))
97  /* Hardware supports AES GCM acceleration */
98  #define STM32_CRYPTO_AES_GCM
99  #endif
100 
101  #ifdef WOLFSSL_STM32L4
102  #define STM32_CRYPTO_AES_ONLY /* crypto engine only supports AES */
103  #define CRYP AES
104  #endif
105 
106  /* Detect newer CubeMX crypto HAL (HAL_CRYP_Encrypt / HAL_CRYP_Decrypt) */
107  #if !defined(STM32_HAL_V2) && \
108  defined(WOLFSSL_STM32F7) && defined(CRYP_AES_GCM)
109  #define STM32_HAL_V2
110  #endif
111 
112  /* The datatype for STM32 CubeMX HAL Crypt calls */
113  #ifdef STM32_HAL_V2
114  #define STM_CRYPT_TYPE uint32_t
115  #else
116  #define STM_CRYPT_TYPE uint8_t
117  #endif
118 
119  /* CRYPT_AES_GCM starts the IV with 2 */
120  #define STM32_GCM_IV_START 2
121 
122  struct Aes;
123  #ifdef WOLFSSL_STM32_CUBEMX
124  int wc_Stm32_Aes_Init(struct Aes* aes, CRYP_HandleTypeDef* hcryp);
125  #else /* STD_PERI_LIB */
126  int wc_Stm32_Aes_Init(struct Aes* aes, CRYP_InitTypeDef* cryptInit,
127  CRYP_KeyInitTypeDef* keyInit);
128  #endif /* WOLFSSL_STM32_CUBEMX */
129 #endif /* !NO_AES */
130 
131 #endif /* STM32_CRYPTO */
132 
133 #if defined(WOLFSSL_STM32_PKA) && defined(HAVE_ECC)
134 int stm32_ecc_verify_hash_ex(mp_int *r, mp_int *s, const byte* hash,
135  word32 hashlen, int* res, ecc_key* key);
136 
137 int stm32_ecc_sign_hash_ex(const byte* hash, word32 hashlen, WC_RNG* rng,
138  ecc_key* key, mp_int *r, mp_int *s);
139 #endif
140 
141 
142 #endif /* _WOLFPORT_STM32_H_ */
Definition: aes.h:149
Definition: integer.h:200
Definition: stm32.h:67
Definition: ecc.h:357
Definition: random.h:153