1 /**
2 ******************************************************************************
3 * @file stm32l4xx_hal_cryp.c
4 * @author MCD Application Team
5 * @brief CRYP HAL module driver.
6 * This file provides firmware functions to manage the following
7 * functionalities of the Cryptography (CRYP) peripheral:
8 * + Initialization and de-initialization functions
9 * + Processing functions using polling mode
10 * + Processing functions using interrupt mode
11 * + Processing functions using DMA mode
12 * + Peripheral State functions
13 *
14 @verbatim
15 ==============================================================================
16 ##### How to use this driver #####
17 ==============================================================================
18 [..]
19 The CRYP HAL driver can be used as follows:
20
21 (#)Initialize the CRYP low level resources by implementing the HAL_CRYP_MspInit():
22 (++) Enable the CRYP interface clock using __HAL_RCC_AES_CLK_ENABLE()
23 (++) In case of using interrupts (e.g. HAL_CRYP_AES_IT())
24 (+++) Configure the CRYP interrupt priority using HAL_NVIC_SetPriority()
25 (+++) Enable the AES IRQ handler using HAL_NVIC_EnableIRQ()
26 (+++) In AES IRQ handler, call HAL_CRYP_IRQHandler()
27 (++) In case of using DMA to control data transfer (e.g. HAL_CRYPEx_AES_DMA())
28 (+++) Enable the DMA2 interface clock using
29 __HAL_RCC_DMA2_CLK_ENABLE()
30 (+++) Configure and enable two DMA channels one for managing data transfer from
31 memory to peripheral (input channel) and another channel for managing data
32 transfer from peripheral to memory (output channel)
33 (+++) Associate the initialized DMA handle to the CRYP DMA handle
34 using __HAL_LINKDMA()
35 (+++) Configure the priority and enable the NVIC for the transfer complete
36 interrupt on the two DMA channels. The output channel should have higher
37 priority than the input channel.
38 Resort to HAL_NVIC_SetPriority() and HAL_NVIC_EnableIRQ()
39
40 (#)Initialize the CRYP HAL using HAL_CRYP_Init(). This function configures:
41 (++) The data type: 1-bit, 8-bit, 16-bit and 32-bit
42 (++) The AES operating mode (encryption, key derivation and/or decryption)
43 (++) The AES chaining mode (ECB, CBC, CTR, GCM, GMAC, CMAC when applicable, CCM when applicable)
44 (++) The encryption/decryption key if so required
45 (++) The initialization vector or nonce if applicable (not used in ECB mode).
46
47 (#)Three processing (encryption/decryption) functions are available:
48 (++) Polling mode: encryption and decryption APIs are blocking functions
49 i.e. they process the data and wait till the processing is finished
50 (++) Interrupt mode: encryption and decryption APIs are not blocking functions
51 i.e. they process the data under interrupt
52 (++) DMA mode: encryption and decryption APIs are not blocking functions
53 i.e. the data transfer is ensured by DMA
54
55 (#)Call HAL_CRYP_DeInit() to deinitialize the CRYP peripheral.
56
57 *** Callback registration ***
58 ===================================
59 [..]
60 (#) The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS when set to 1
61 allows the user to configure dynamically the driver callbacks.
62 Use function @ref HAL_CRYP_RegisterCallback() to register a user callback.
63
64 (#) Function @ref HAL_CRYP_RegisterCallback() allows to register following callbacks:
65 (+) InCpltCallback : callback for input DMA transfer completion.
66 (+) OutCpltCallback : callback for output DMA transfer completion.
67 (+) CompCpltCallback : callback for computation completion.
68 (+) ErrorCallback : callback for error.
69 (+) MspInitCallback : CRYP MspInit.
70 (+) MspDeInitCallback : CRYP MspDeInit.
71 This function takes as parameters the HAL peripheral handle, the Callback ID
72 and a pointer to the user callback function.
73
74 (#) Use function @ref HAL_CRYP_UnRegisterCallback() to reset a callback to the default
75 weak (surcharged) function.
76 @ref HAL_CRYP_UnRegisterCallback() takes as parameters the HAL peripheral handle,
77 and the Callback ID.
78 This function allows to reset following callbacks:
79 (+) InCpltCallback : callback for input DMA transfer completion.
80 (+) OutCpltCallback : callback for output DMA transfer completion.
81 (+) CompCpltCallback : callback for computation completion.
82 (+) ErrorCallback : callback for error.
83 (+) MspInitCallback : CRYP MspInit.
84 (+) MspDeInitCallback : CRYP MspDeInit.
85
86 (#) By default, after the @ref HAL_CRYP_Init and if the state is HAL_CRYP_STATE_RESET
87 all callbacks are reset to the corresponding legacy weak (surcharged) functions:
88 examples @ref HAL_CRYP_InCpltCallback(), @ref HAL_CRYP_ErrorCallback()
89 Exception done for MspInit and MspDeInit callbacks that are respectively
90 reset to the legacy weak (surcharged) functions in the @ref HAL_CRYP_Init
91 and @ref HAL_CRYP_DeInit only when these callbacks are null (not registered beforehand)
92 If not, MspInit or MspDeInit are not null, the @ref HAL_CRYP_Init and @ref HAL_CRYP_DeInit
93 keep and use the user MspInit/MspDeInit callbacks (registered beforehand).
94
95 Callbacks can be registered/unregistered in READY state only.
96 Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
97 in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
98 during the Init/DeInit.
99 In that case first register the MspInit/MspDeInit user callbacks
100 using @ref HAL_CRYP_RegisterCallback before calling @ref HAL_CRYP_DeInit
101 or @ref HAL_�CRYP_Init function.
102
103 When The compilation define USE_HAL_CRYP_REGISTER_CALLBACKS is set to 0 or
104 not defined, the callback registering feature is not available
105 and weak (surcharged) callbacks are used.
106
107
108 @endverbatim
109 ******************************************************************************
110 * @attention
111 *
112 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
113 *
114 * Redistribution and use in source and binary forms, with or without modification,
115 * are permitted provided that the following conditions are met:
116 * 1. Redistributions of source code must retain the above copyright notice,
117 * this list of conditions and the following disclaimer.
118 * 2. Redistributions in binary form must reproduce the above copyright notice,
119 * this list of conditions and the following disclaimer in the documentation
120 * and/or other materials provided with the distribution.
121 * 3. Neither the name of STMicroelectronics nor the names of its contributors
122 * may be used to endorse or promote products derived from this software
123 * without specific prior written permission.
124 *
125 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
126 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
127 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
128 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
129 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
130 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
131 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
132 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
133 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
134 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
135 *
136 ******************************************************************************
137 */
138
139 /* Includes ------------------------------------------------------------------*/
140 #include "stm32l4xx_hal.h"
141
142 #ifdef HAL_CRYP_MODULE_ENABLED
143
144 #if defined(AES)
145
146 /** @addtogroup STM32L4xx_HAL_Driver
147 * @{
148 */
149
150 /** @defgroup CRYP CRYP
151 * @brief CRYP HAL module driver.
152 * @{
153 */
154
155
156
157 /* Private typedef -----------------------------------------------------------*/
158 /* Private define ------------------------------------------------------------*/
159 /* Private macro -------------------------------------------------------------*/
160 /* Private variables ---------------------------------------------------------*/
161 /* Private functions --------------------------------------------------------*/
162
163 /** @defgroup CRYP_Private_Functions CRYP Private Functions
164 * @{
165 */
166
167 static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp);
168 static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp);
169 static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp);
170
171 /**
172 * @}
173 */
174
175 /* Exported functions ---------------------------------------------------------*/
176
177 /** @defgroup CRYP_Exported_Functions CRYP Exported Functions
178 * @{
179 */
180
181 /** @defgroup CRYP_Exported_Functions_Group1 Initialization and deinitialization functions
182 * @brief Initialization and Configuration functions.
183 *
184 @verbatim
185 ==============================================================================
186 ##### Initialization and deinitialization functions #####
187 ==============================================================================
188 [..] This section provides functions allowing to:
189 (+) Initialize the CRYP according to the specified parameters
190 in the CRYP_InitTypeDef and creates the associated handle
191 (+) DeInitialize the CRYP peripheral
192 (+) Initialize the CRYP MSP (MCU Specific Package)
193 (+) De-Initialize the CRYP MSP
194
195 [..]
196 (@) Specific care must be taken to format the key and the Initialization Vector IV!
197
198 [..] If the key is defined as a 128-bit long array key[127..0] = {b127 ... b0} where
199 b127 is the MSB and b0 the LSB, the key must be stored in MCU memory
200 (+) as a sequence of words where the MSB word comes first (occupies the
201 lowest memory address)
202 (+) where each word is byte-swapped:
203 (++) address n+0 : 0b b103 .. b96 b111 .. b104 b119 .. b112 b127 .. b120
204 (++) address n+4 : 0b b71 .. b64 b79 .. b72 b87 .. b80 b95 .. b88
205 (++) address n+8 : 0b b39 .. b32 b47 .. b40 b55 .. b48 b63 .. b56
206 (++) address n+C : 0b b7 .. b0 b15 .. b8 b23 .. b16 b31 .. b24
207 [..] Hereafter, another illustration when considering a 128-bit long key made of 16 bytes {B15..B0}.
208 The 4 32-bit words that make the key must be stored as follows in MCU memory:
209 (+) address n+0 : 0x B12 B13 B14 B15
210 (+) address n+4 : 0x B8 B9 B10 B11
211 (+) address n+8 : 0x B4 B5 B6 B7
212 (+) address n+C : 0x B0 B1 B2 B3
213 [..] which leads to the expected setting
214 (+) AES_KEYR3 = 0x B15 B14 B13 B12
215 (+) AES_KEYR2 = 0x B11 B10 B9 B8
216 (+) AES_KEYR1 = 0x B7 B6 B5 B4
217 (+) AES_KEYR0 = 0x B3 B2 B1 B0
218
219 [..] Same format must be applied for a 256-bit long key made of 32 bytes {B31..B0}.
220 The 8 32-bit words that make the key must be stored as follows in MCU memory:
221 (+) address n+00 : 0x B28 B29 B30 B31
222 (+) address n+04 : 0x B24 B25 B26 B27
223 (+) address n+08 : 0x B20 B21 B22 B23
224 (+) address n+0C : 0x B16 B17 B18 B19
225 (+) address n+10 : 0x B12 B13 B14 B15
226 (+) address n+14 : 0x B8 B9 B10 B11
227 (+) address n+18 : 0x B4 B5 B6 B7
228 (+) address n+1C : 0x B0 B1 B2 B3
229 [..] which leads to the expected setting
230 (+) AES_KEYR7 = 0x B31 B30 B29 B28
231 (+) AES_KEYR6 = 0x B27 B26 B25 B24
232 (+) AES_KEYR5 = 0x B23 B22 B21 B20
233 (+) AES_KEYR4 = 0x B19 B18 B17 B16
234 (+) AES_KEYR3 = 0x B15 B14 B13 B12
235 (+) AES_KEYR2 = 0x B11 B10 B9 B8
236 (+) AES_KEYR1 = 0x B7 B6 B5 B4
237 (+) AES_KEYR0 = 0x B3 B2 B1 B0
238
239 [..] Initialization Vector IV (4 32-bit words) format must follow the same as
240 that of a 128-bit long key.
241
242 [..]
243
244 @endverbatim
245 * @{
246 */
247
248 /**
249 * @brief Initialize the CRYP according to the specified
250 * parameters in the CRYP_InitTypeDef and initialize the associated handle.
251 * @note Specific care must be taken to format the key and the Initialization Vector IV
252 * stored in the MCU memory before calling HAL_CRYP_Init(). Refer to explanations
253 * hereabove.
254 * @retval HAL status
255 */
HAL_CRYP_Init(CRYP_HandleTypeDef * hcryp)256 HAL_StatusTypeDef HAL_CRYP_Init(CRYP_HandleTypeDef *hcryp)
257 {
258 /* Check the CRYP handle allocation */
259 if(hcryp == NULL)
260 {
261 return HAL_ERROR;
262 }
263
264 /* Check the instance */
265 assert_param(IS_AES_ALL_INSTANCE(hcryp->Instance));
266
267 /* Check the parameters */
268 assert_param(IS_CRYP_KEYSIZE(hcryp->Init.KeySize));
269 assert_param(IS_CRYP_DATATYPE(hcryp->Init.DataType));
270 assert_param(IS_CRYP_ALGOMODE(hcryp->Init.OperatingMode));
271 /* ChainingMode parameter is irrelevant when mode is set to Key derivation */
272 if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
273 {
274 assert_param(IS_CRYP_CHAINMODE(hcryp->Init.ChainingMode));
275 }
276 assert_param(IS_CRYP_WRITE(hcryp->Init.KeyWriteFlag));
277
278 /*========================================================*/
279 /* Check the proper operating/chaining modes combinations */
280 /*========================================================*/
281 /* Check the proper chaining when the operating mode is key derivation and decryption */
282 #if defined(AES_CR_NPBLB)
283 if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
284 ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \
285 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \
286 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM)))
287 #else
288 if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION_DECRYPT) &&\
289 ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CTR) \
290 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC) \
291 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)))
292 #endif
293 {
294 return HAL_ERROR;
295 }
296 /* Check that key derivation is not set in CMAC mode or CCM mode when applicable */
297 #if defined(AES_CR_NPBLB)
298 if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
299 && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
300 #else
301 if ((hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
302 && (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
303 #endif
304 {
305 return HAL_ERROR;
306 }
307
308
309 /*================*/
310 /* Initialization */
311 /*================*/
312 /* Initialization start */
313 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
314 if (hcryp->State == HAL_CRYP_STATE_RESET)
315 {
316 /* Allocate lock resource and initialize it */
317 hcryp->Lock = HAL_UNLOCKED;
318
319 /* Reset Callback pointers in HAL_CRYP_STATE_RESET only */
320 hcryp->InCpltCallback = HAL_CRYP_InCpltCallback; /* Legacy weak (surcharged) input DMA transfer completion callback */
321 hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback; /* Legacy weak (surcharged) output DMA transfer completion callback */
322 hcryp->CompCpltCallback = HAL_CRYPEx_ComputationCpltCallback; /* Legacy weak (surcharged) computation completion callback */
323 hcryp->ErrorCallback = HAL_CRYP_ErrorCallback; /* Legacy weak (surcharged) error callback */
324 if(hcryp->MspInitCallback == NULL)
325 {
326 hcryp->MspInitCallback = HAL_CRYP_MspInit;
327 }
328
329 /* Init the low level hardware */
330 hcryp->MspInitCallback(hcryp);
331 }
332 #else
333 if(hcryp->State == HAL_CRYP_STATE_RESET)
334 {
335 /* Allocate lock resource and initialize it */
336 hcryp->Lock = HAL_UNLOCKED;
337
338 /* Init the low level hardware */
339 HAL_CRYP_MspInit(hcryp);
340 }
341 #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
342
343 /* Change the CRYP state */
344 hcryp->State = HAL_CRYP_STATE_BUSY;
345
346 /* Disable the Peripheral */
347 __HAL_CRYP_DISABLE(hcryp);
348
349 /*=============================================================*/
350 /* AES initialization common to all operating modes */
351 /*=============================================================*/
352 /* Set the Key size selection */
353 MODIFY_REG(hcryp->Instance->CR, AES_CR_KEYSIZE, hcryp->Init.KeySize);
354
355 /* Set the default CRYP phase when this parameter is not used.
356 Phase is updated below in case of GCM/GMAC(/CMAC)(/CCM) setting. */
357 hcryp->Phase = HAL_CRYP_PHASE_NOT_USED;
358
359
360
361 /*=============================================================*/
362 /* Carry on the initialization based on the AES operating mode */
363 /*=============================================================*/
364 /* Key derivation */
365 if (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION)
366 {
367 MODIFY_REG(hcryp->Instance->CR, AES_CR_MODE, CRYP_ALGOMODE_KEYDERIVATION);
368
369 /* Configure the Key registers */
370 if (CRYP_SetKey(hcryp) != HAL_OK)
371 {
372 return HAL_ERROR;
373 }
374 }
375 else
376 /* Encryption / Decryption (with or without key derivation) / authentication */
377 {
378 #if !defined(AES_CR_NPBLB)
379 /* Set data type, operating and chaining modes.
380 In case of GCM or GMAC, data type is forced to 0b00 */
381 if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
382 {
383 MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
384 }
385 else
386 #endif
387 {
388 MODIFY_REG(hcryp->Instance->CR, AES_CR_DATATYPE|AES_CR_MODE|AES_CR_CHMOD, hcryp->Init.DataType|hcryp->Init.OperatingMode|hcryp->Init.ChainingMode);
389 }
390
391
392 /* Specify the encryption/decryption phase in case of Galois counter mode (GCM),
393 Galois message authentication code (GMAC), cipher message authentication code (CMAC) when applicable
394 or Counter with Cipher Mode (CCM) when applicable */
395 #if defined(AES_CR_NPBLB)
396 if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
397 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
398 #else
399 if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
400 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
401 #endif
402 {
403 MODIFY_REG(hcryp->Instance->CR, AES_CR_GCMPH, hcryp->Init.GCMCMACPhase);
404 hcryp->Phase = HAL_CRYP_PHASE_START;
405 }
406
407
408 /* Configure the Key registers if no need to bypass this step */
409 if (hcryp->Init.KeyWriteFlag == CRYP_KEY_WRITE_ENABLE)
410 {
411 if (CRYP_SetKey(hcryp) != HAL_OK)
412 {
413 return HAL_ERROR;
414 }
415 }
416
417 /* If applicable, configure the Initialization Vector */
418 if (hcryp->Init.ChainingMode != CRYP_CHAINMODE_AES_ECB)
419 {
420 if (CRYP_SetInitVector(hcryp) != HAL_OK)
421 {
422 return HAL_ERROR;
423 }
424 }
425 }
426
427 #if defined(AES_CR_NPBLB)
428 /* Clear NPBLB field */
429 CLEAR_BIT(hcryp->Instance->CR, AES_CR_NPBLB);
430 #endif
431
432 /* Reset CrypInCount and CrypOutCount */
433 hcryp->CrypInCount = 0;
434 hcryp->CrypOutCount = 0;
435
436 /* Reset ErrorCode field */
437 hcryp->ErrorCode = HAL_CRYP_ERROR_NONE;
438
439 /* Reset Mode suspension request */
440 hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
441
442 /* Change the CRYP state */
443 hcryp->State = HAL_CRYP_STATE_READY;
444
445 /* Enable the Peripheral */
446 __HAL_CRYP_ENABLE(hcryp);
447
448 /* Return function status */
449 return HAL_OK;
450 }
451
452 /**
453 * @brief DeInitialize the CRYP peripheral.
454 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
455 * the configuration information for CRYP module
456 * @retval HAL status
457 */
HAL_CRYP_DeInit(CRYP_HandleTypeDef * hcryp)458 HAL_StatusTypeDef HAL_CRYP_DeInit(CRYP_HandleTypeDef *hcryp)
459 {
460 /* Check the CRYP handle allocation */
461 if(hcryp == NULL)
462 {
463 return HAL_ERROR;
464 }
465
466 /* Change the CRYP state */
467 hcryp->State = HAL_CRYP_STATE_BUSY;
468
469 /* Set the default CRYP phase */
470 hcryp->Phase = HAL_CRYP_PHASE_READY;
471
472 /* Reset CrypInCount and CrypOutCount */
473 hcryp->CrypInCount = 0;
474 hcryp->CrypOutCount = 0;
475
476 /* Disable the CRYP Peripheral Clock */
477 __HAL_CRYP_DISABLE(hcryp);
478
479 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
480 if(hcryp->MspDeInitCallback == NULL)
481 {
482 hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit;
483 }
484
485 /* DeInit the low level hardware */
486 hcryp->MspDeInitCallback(hcryp);
487 #else
488 /* DeInit the low level hardware: CLOCK, NVIC.*/
489 HAL_CRYP_MspDeInit(hcryp);
490 #endif /* (USE_HAL_CRYP_REGISTER_CALLBACKS) */
491
492 /* Change the CRYP state */
493 hcryp->State = HAL_CRYP_STATE_RESET;
494
495 /* Release Lock */
496 __HAL_UNLOCK(hcryp);
497
498 /* Return function status */
499 return HAL_OK;
500 }
501
502 /**
503 * @brief Initialize the CRYP MSP.
504 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
505 * the configuration information for CRYP module
506 * @retval None
507 */
HAL_CRYP_MspInit(CRYP_HandleTypeDef * hcryp)508 __weak void HAL_CRYP_MspInit(CRYP_HandleTypeDef *hcryp)
509 {
510 /* Prevent unused argument(s) compilation warning */
511 UNUSED(hcryp);
512
513 /* NOTE : This function should not be modified; when the callback is needed,
514 the HAL_CRYP_MspInit can be implemented in the user file
515 */
516 }
517
518 /**
519 * @brief DeInitialize CRYP MSP.
520 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
521 * the configuration information for CRYP module
522 * @retval None
523 */
HAL_CRYP_MspDeInit(CRYP_HandleTypeDef * hcryp)524 __weak void HAL_CRYP_MspDeInit(CRYP_HandleTypeDef *hcryp)
525 {
526 /* Prevent unused argument(s) compilation warning */
527 UNUSED(hcryp);
528
529 /* NOTE : This function should not be modified; when the callback is needed,
530 the HAL_CRYP_MspDeInit can be implemented in the user file
531 */
532 }
533
534 /**
535 * @}
536 */
537
538 /** @defgroup CRYP_Exported_Functions_Group2 AES processing functions
539 * @brief Processing functions.
540 *
541 @verbatim
542 ==============================================================================
543 ##### AES processing functions #####
544 ==============================================================================
545 [..] This section provides functions allowing to:
546 (+) Encrypt plaintext using AES algorithm in different chaining modes
547 (+) Decrypt cyphertext using AES algorithm in different chaining modes
548 [..] Three processing functions are available:
549 (+) Polling mode
550 (+) Interrupt mode
551 (+) DMA mode
552
553 @endverbatim
554 * @{
555 */
556
557
558 /**
559 * @brief Encrypt pPlainData in AES ECB encryption mode. The cypher data are available in pCypherData.
560 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
561 * the configuration information for CRYP module
562 * @param pPlainData: Pointer to the plaintext buffer
563 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
564 * @param pCypherData: Pointer to the cyphertext buffer
565 * @param Timeout: Specify Timeout value
566 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
567 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
568 * @retval HAL status
569 */
HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData,uint32_t Timeout)570 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
571 {
572 /* Re-initialize AES IP with proper parameters */
573 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
574 {
575 return HAL_ERROR;
576 }
577 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
578 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
579 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
580 if (HAL_CRYP_Init(hcryp) != HAL_OK)
581 {
582 return HAL_ERROR;
583 }
584
585 return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
586 }
587
588
589 /**
590 * @brief Encrypt pPlainData in AES CBC encryption mode with key derivation. The cypher data are available in pCypherData.
591 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
592 * the configuration information for CRYP module
593 * @param pPlainData: Pointer to the plaintext buffer
594 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
595 * @param pCypherData: Pointer to the cyphertext buffer
596 * @param Timeout: Specify Timeout value
597 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
598 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
599 * @retval HAL status
600 */
HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData,uint32_t Timeout)601 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
602 {
603 /* Re-initialize AES IP with proper parameters */
604 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
605 {
606 return HAL_ERROR;
607 }
608 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
609 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
610 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
611 if (HAL_CRYP_Init(hcryp) != HAL_OK)
612 {
613 return HAL_ERROR;
614 }
615
616 return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
617 }
618
619
620 /**
621 * @brief Encrypt pPlainData in AES CTR encryption mode. The cypher data are available in pCypherData
622 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
623 * the configuration information for CRYP module
624 * @param pPlainData: Pointer to the plaintext buffer
625 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
626 * @param pCypherData: Pointer to the cyphertext buffer
627 * @param Timeout: Specify Timeout value
628 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
629 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
630 * @retval HAL status
631 */
HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData,uint32_t Timeout)632 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData, uint32_t Timeout)
633 {
634 /* Re-initialize AES IP with proper parameters */
635 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
636 {
637 return HAL_ERROR;
638 }
639 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
640 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
641 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
642 if (HAL_CRYP_Init(hcryp) != HAL_OK)
643 {
644 return HAL_ERROR;
645 }
646
647 return HAL_CRYPEx_AES(hcryp, pPlainData, Size, pCypherData, Timeout);
648 }
649
650 /**
651 * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation,
652 * the decyphered data are available in pPlainData.
653 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
654 * the configuration information for CRYP module
655 * @param pCypherData: Pointer to the cyphertext buffer
656 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
657 * @param pPlainData: Pointer to the plaintext buffer
658 * @param Timeout: Specify Timeout value
659 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
660 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
661 * @retval HAL status
662 */
HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData,uint32_t Timeout)663 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
664 {
665 /* Re-initialize AES IP with proper parameters */
666 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
667 {
668 return HAL_ERROR;
669 }
670 hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
671 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
672 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
673 if (HAL_CRYP_Init(hcryp) != HAL_OK)
674 {
675 return HAL_ERROR;
676 }
677
678 return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
679 }
680
681 /**
682 * @brief Decrypt pCypherData in AES ECB decryption mode with key derivation,
683 * the decyphered data are available in pPlainData.
684 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
685 * the configuration information for CRYP module
686 * @param pCypherData: Pointer to the cyphertext buffer
687 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
688 * @param pPlainData: Pointer to the plaintext buffer
689 * @param Timeout: Specify Timeout value
690 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
691 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
692 * @retval HAL status
693 */
HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData,uint32_t Timeout)694 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
695 {
696 /* Re-initialize AES IP with proper parameters */
697 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
698 {
699 return HAL_ERROR;
700 }
701 hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
702 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
703 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
704 if (HAL_CRYP_Init(hcryp) != HAL_OK)
705 {
706 return HAL_ERROR;
707 }
708
709 return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
710 }
711
712 /**
713 * @brief Decrypt pCypherData in AES CTR decryption mode,
714 * the decyphered data are available in pPlainData.
715 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
716 * the configuration information for CRYP module
717 * @param pCypherData: Pointer to the cyphertext buffer
718 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
719 * @param pPlainData: Pointer to the plaintext buffer
720 * @param Timeout: Specify Timeout value
721 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
722 * resort to generic HAL_CRYPEx_AES() API instead (usage recommended).
723 * @retval HAL status
724 */
HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData,uint32_t Timeout)725 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData, uint32_t Timeout)
726 {
727 /* Re-initialize AES IP with proper parameters */
728 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
729 {
730 return HAL_ERROR;
731 }
732 hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
733 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
734 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
735 if (HAL_CRYP_Init(hcryp) != HAL_OK)
736 {
737 return HAL_ERROR;
738 }
739
740 return HAL_CRYPEx_AES(hcryp, pCypherData, Size, pPlainData, Timeout);
741 }
742
743 /**
744 * @brief Encrypt pPlainData in AES ECB encryption mode using Interrupt,
745 * the cypher data are available in pCypherData.
746 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
747 * the configuration information for CRYP module
748 * @param pPlainData: Pointer to the plaintext buffer
749 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
750 * @param pCypherData: Pointer to the cyphertext buffer
751 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
752 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
753 * @retval HAL status
754 */
HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData)755 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
756 {
757 /* Re-initialize AES IP with proper parameters */
758 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
759 {
760 return HAL_ERROR;
761 }
762 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
763 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
764 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
765 if (HAL_CRYP_Init(hcryp) != HAL_OK)
766 {
767 return HAL_ERROR;
768 }
769
770 return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
771 }
772
773 /**
774 * @brief Encrypt pPlainData in AES CBC encryption mode using Interrupt,
775 * the cypher data are available in pCypherData.
776 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
777 * the configuration information for CRYP module
778 * @param pPlainData: Pointer to the plaintext buffer
779 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
780 * @param pCypherData: Pointer to the cyphertext buffer
781 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
782 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
783 * @retval HAL status
784 */
HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData)785 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
786 {
787 /* Re-initialize AES IP with proper parameters */
788 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
789 {
790 return HAL_ERROR;
791 }
792 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
793 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
794 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
795 if (HAL_CRYP_Init(hcryp) != HAL_OK)
796 {
797 return HAL_ERROR;
798 }
799
800 return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
801 }
802
803
804 /**
805 * @brief Encrypt pPlainData in AES CTR encryption mode using Interrupt,
806 * the cypher data are available in pCypherData.
807 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
808 * the configuration information for CRYP module
809 * @param pPlainData: Pointer to the plaintext buffer
810 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
811 * @param pCypherData: Pointer to the cyphertext buffer
812 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
813 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
814 * @retval HAL status
815 */
HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData)816 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
817 {
818 /* Re-initialize AES IP with proper parameters */
819 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
820 {
821 return HAL_ERROR;
822 }
823 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
824 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
825 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
826 if (HAL_CRYP_Init(hcryp) != HAL_OK)
827 {
828 return HAL_ERROR;
829 }
830
831 return HAL_CRYPEx_AES_IT(hcryp, pPlainData, Size, pCypherData);
832 }
833
834 /**
835 * @brief Decrypt pCypherData in AES ECB decryption mode using Interrupt,
836 * the decyphered data are available in pPlainData.
837 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
838 * the configuration information for CRYP module
839 * @param pCypherData: Pointer to the cyphertext buffer
840 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
841 * @param pPlainData: Pointer to the plaintext buffer.
842 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
843 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
844 * @retval HAL status
845 */
HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData)846 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
847 {
848 /* Re-initialize AES IP with proper parameters */
849 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
850 {
851 return HAL_ERROR;
852 }
853 hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
854 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
855 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
856 if (HAL_CRYP_Init(hcryp) != HAL_OK)
857 {
858 return HAL_ERROR;
859 }
860
861 return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
862 }
863
864 /**
865 * @brief Decrypt pCypherData in AES CBC decryption mode using Interrupt,
866 * the decyphered data are available in pPlainData.
867 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
868 * the configuration information for CRYP module
869 * @param pCypherData: Pointer to the cyphertext buffer
870 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
871 * @param pPlainData: Pointer to the plaintext buffer
872 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
873 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
874 * @retval HAL status
875 */
HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData)876 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
877 {
878 /* Re-initialize AES IP with proper parameters */
879 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
880 {
881 return HAL_ERROR;
882 }
883 hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
884 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
885 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
886 if (HAL_CRYP_Init(hcryp) != HAL_OK)
887 {
888 return HAL_ERROR;
889 }
890
891 return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
892 }
893
894 /**
895 * @brief Decrypt pCypherData in AES CTR decryption mode using Interrupt,
896 * the decyphered data are available in pPlainData.
897 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
898 * the configuration information for CRYP module
899 * @param pCypherData: Pointer to the cyphertext buffer
900 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
901 * @param pPlainData: Pointer to the plaintext buffer
902 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
903 * resort to generic HAL_CRYPEx_AES_IT() API instead (usage recommended).
904 * @retval HAL status
905 */
HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData)906 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_IT(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
907 {
908 /* Re-initialize AES IP with proper parameters */
909 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
910 {
911 return HAL_ERROR;
912 }
913 hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
914 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
915 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
916 if (HAL_CRYP_Init(hcryp) != HAL_OK)
917 {
918 return HAL_ERROR;
919 }
920
921 return HAL_CRYPEx_AES_IT(hcryp, pCypherData, Size, pPlainData);
922 }
923
924 /**
925 * @brief Encrypt pPlainData in AES ECB encryption mode using DMA,
926 * the cypher data are available in pCypherData.
927 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
928 * the configuration information for CRYP module
929 * @param pPlainData: Pointer to the plaintext buffer
930 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
931 * @param pCypherData: Pointer to the cyphertext buffer
932 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
933 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
934 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
935 * @retval HAL status
936 */
HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData)937 HAL_StatusTypeDef HAL_CRYP_AESECB_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
938 {
939 /* Re-initialize AES IP with proper parameters */
940 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
941 {
942 return HAL_ERROR;
943 }
944 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
945 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
946 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
947 if (HAL_CRYP_Init(hcryp) != HAL_OK)
948 {
949 return HAL_ERROR;
950 }
951
952 return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
953 }
954
955
956
957 /**
958 * @brief Encrypt pPlainData in AES CBC encryption mode using DMA,
959 * the cypher data are available in pCypherData.
960 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
961 * the configuration information for CRYP module
962 * @param pPlainData: Pointer to the plaintext buffer
963 * @param Size: Length of the plaintext buffer, must be a multiple of 16.
964 * @param pCypherData: Pointer to the cyphertext buffer
965 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
966 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
967 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
968 * @retval HAL status
969 */
HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData)970 HAL_StatusTypeDef HAL_CRYP_AESCBC_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
971 {
972 /* Re-initialize AES IP with proper parameters */
973 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
974 {
975 return HAL_ERROR;
976 }
977 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
978 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
979 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
980 if (HAL_CRYP_Init(hcryp) != HAL_OK)
981 {
982 return HAL_ERROR;
983 }
984
985 return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
986 }
987
988 /**
989 * @brief Encrypt pPlainData in AES CTR encryption mode using DMA,
990 * the cypher data are available in pCypherData.
991 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
992 * the configuration information for CRYP module
993 * @param pPlainData: Pointer to the plaintext buffer
994 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
995 * @param pCypherData: Pointer to the cyphertext buffer.
996 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
997 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
998 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
999 * @retval HAL status
1000 */
HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef * hcryp,uint8_t * pPlainData,uint16_t Size,uint8_t * pCypherData)1001 HAL_StatusTypeDef HAL_CRYP_AESCTR_Encrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pPlainData, uint16_t Size, uint8_t *pCypherData)
1002 {
1003 /* Re-initialize AES IP with proper parameters */
1004 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
1005 {
1006 return HAL_ERROR;
1007 }
1008 hcryp->Init.OperatingMode = CRYP_ALGOMODE_ENCRYPT;
1009 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
1010 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
1011 if (HAL_CRYP_Init(hcryp) != HAL_OK)
1012 {
1013 return HAL_ERROR;
1014 }
1015
1016 return HAL_CRYPEx_AES_DMA(hcryp, pPlainData, Size, pCypherData);
1017 }
1018
1019 /**
1020 * @brief Decrypt pCypherData in AES ECB decryption mode using DMA,
1021 * the decyphered data are available in pPlainData.
1022 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1023 * the configuration information for CRYP module
1024 * @param pCypherData: Pointer to the cyphertext buffer
1025 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
1026 * @param pPlainData: Pointer to the plaintext buffer
1027 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
1028 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
1029 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
1030 * @retval HAL status
1031 */
HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData)1032 HAL_StatusTypeDef HAL_CRYP_AESECB_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1033 {
1034 /* Re-initialize AES IP with proper parameters */
1035 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
1036 {
1037 return HAL_ERROR;
1038 }
1039 hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
1040 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_ECB;
1041 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
1042 if (HAL_CRYP_Init(hcryp) != HAL_OK)
1043 {
1044 return HAL_ERROR;
1045 }
1046
1047 return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
1048 }
1049
1050 /**
1051 * @brief Decrypt pCypherData in AES CBC decryption mode using DMA,
1052 * the decyphered data are available in pPlainData.
1053 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1054 * the configuration information for CRYP module
1055 * @param pCypherData: Pointer to the cyphertext buffer
1056 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
1057 * @param pPlainData: Pointer to the plaintext buffer
1058 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
1059 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
1060 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
1061 * @retval HAL status
1062 */
HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData)1063 HAL_StatusTypeDef HAL_CRYP_AESCBC_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1064 {
1065 /* Re-initialize AES IP with proper parameters */
1066 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
1067 {
1068 return HAL_ERROR;
1069 }
1070 hcryp->Init.OperatingMode = CRYP_ALGOMODE_KEYDERIVATION_DECRYPT;
1071 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CBC;
1072 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
1073 if (HAL_CRYP_Init(hcryp) != HAL_OK)
1074 {
1075 return HAL_ERROR;
1076 }
1077
1078 return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
1079 }
1080
1081 /**
1082 * @brief Decrypt pCypherData in AES CTR decryption mode using DMA,
1083 * the decyphered data are available in pPlainData.
1084 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1085 * the configuration information for CRYP module
1086 * @param pCypherData: Pointer to the cyphertext buffer
1087 * @param Size: Length of the plaintext buffer in bytes, must be a multiple of 16.
1088 * @param pPlainData: Pointer to the plaintext buffer
1089 * @note This API is provided only to maintain compatibility with legacy software. Users should directly
1090 * resort to generic HAL_CRYPEx_AES_DMA() API instead (usage recommended).
1091 * @note pPlainData and pCypherData buffers must be 32-bit aligned to ensure a correct DMA transfer to and from the IP.
1092 * @retval HAL status
1093 */
HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef * hcryp,uint8_t * pCypherData,uint16_t Size,uint8_t * pPlainData)1094 HAL_StatusTypeDef HAL_CRYP_AESCTR_Decrypt_DMA(CRYP_HandleTypeDef *hcryp, uint8_t *pCypherData, uint16_t Size, uint8_t *pPlainData)
1095 {
1096 /* Re-initialize AES IP with proper parameters */
1097 if (HAL_CRYP_DeInit(hcryp) != HAL_OK)
1098 {
1099 return HAL_ERROR;
1100 }
1101 hcryp->Init.OperatingMode = CRYP_ALGOMODE_DECRYPT;
1102 hcryp->Init.ChainingMode = CRYP_CHAINMODE_AES_CTR;
1103 hcryp->Init.KeyWriteFlag = CRYP_KEY_WRITE_ENABLE;
1104 if (HAL_CRYP_Init(hcryp) != HAL_OK)
1105 {
1106 return HAL_ERROR;
1107 }
1108
1109 return HAL_CRYPEx_AES_DMA(hcryp, pCypherData, Size, pPlainData);
1110 }
1111
1112
1113 /**
1114 * @}
1115 */
1116
1117 /** @defgroup CRYP_Exported_Functions_Group3 Callback functions
1118 * @brief Callback functions.
1119 *
1120 @verbatim
1121 ==============================================================================
1122 ##### Callback functions #####
1123 ==============================================================================
1124 [..] This section provides Interruption and DMA callback functions:
1125 (+) DMA Input data transfer complete
1126 (+) DMA Output data transfer complete
1127 (+) DMA or Interrupt error
1128
1129 @endverbatim
1130 * @{
1131 */
1132
1133 /**
1134 * @brief CRYP error callback.
1135 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1136 * the configuration information for CRYP module
1137 * @retval None
1138 */
HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef * hcryp)1139 __weak void HAL_CRYP_ErrorCallback(CRYP_HandleTypeDef *hcryp)
1140 {
1141 /* Prevent unused argument(s) compilation warning */
1142 UNUSED(hcryp);
1143
1144 /* NOTE : This function should not be modified; when the callback is needed,
1145 the HAL_CRYP_ErrorCallback can be implemented in the user file
1146 */
1147 }
1148
1149 /**
1150 * @brief Input DMA transfer complete callback.
1151 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1152 * the configuration information for CRYP module
1153 * @retval None
1154 */
HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef * hcryp)1155 __weak void HAL_CRYP_InCpltCallback(CRYP_HandleTypeDef *hcryp)
1156 {
1157 /* Prevent unused argument(s) compilation warning */
1158 UNUSED(hcryp);
1159
1160 /* NOTE : This function should not be modified; when the callback is needed,
1161 the HAL_CRYP_InCpltCallback can be implemented in the user file
1162 */
1163 }
1164
1165 /**
1166 * @brief Output DMA transfer complete callback.
1167 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1168 * the configuration information for CRYP module
1169 * @retval None
1170 */
HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef * hcryp)1171 __weak void HAL_CRYP_OutCpltCallback(CRYP_HandleTypeDef *hcryp)
1172 {
1173 /* Prevent unused argument(s) compilation warning */
1174 UNUSED(hcryp);
1175
1176 /* NOTE : This function should not be modified; when the callback is needed,
1177 the HAL_CRYP_OutCpltCallback can be implemented in the user file
1178 */
1179 }
1180
1181 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1182 /**
1183 * @brief Register a User CRYP Callback
1184 * To be used instead of the weak (surcharged) predefined callback
1185 * @param hcryp CRYP handle
1186 * @param CallbackID ID of the callback to be registered
1187 * This parameter can be one of the following values:
1188 * @arg @ref HAL_CRYP_INPUTCPLT_CB_ID CRYP input DMA transfer completion Callback ID
1189 * @arg @ref HAL_CRYP_OUTPUTCPLT_CB_ID CRYP output DMA transfer completion Callback ID
1190 * @arg @ref HAL_CRYP_COMPCPLT_CB_ID CRYP computation completion Callback ID
1191 * @arg @ref HAL_CRYP_ERROR_CB_ID CRYP error callback ID
1192 * @arg @ref HAL_CRYP_MSPINIT_CB_ID CRYP MspDeInit callback ID
1193 * @arg @ref HAL_CRYP_MSPDEINIT_CB_ID CRYP MspDeInit callback ID
1194 * @param pCallback pointer to the Callback function
1195 * @retval status
1196 */
HAL_CRYP_RegisterCallback(CRYP_HandleTypeDef * hcryp,HAL_CRYP_CallbackIDTypeDef CallbackID,pCRYP_CallbackTypeDef pCallback)1197 HAL_StatusTypeDef HAL_CRYP_RegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID, pCRYP_CallbackTypeDef pCallback)
1198 {
1199 HAL_StatusTypeDef status = HAL_OK;
1200
1201 if(pCallback == NULL)
1202 {
1203 /* Update the error code */
1204 hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
1205 return HAL_ERROR;
1206 }
1207 /* Process locked */
1208 __HAL_LOCK(hcryp);
1209
1210 if(HAL_CRYP_STATE_READY == hcryp->State)
1211 {
1212 switch (CallbackID)
1213 {
1214 case HAL_CRYP_INPUTCPLT_CB_ID :
1215 hcryp->InCpltCallback = pCallback;
1216 break;
1217
1218 case HAL_CRYP_OUTPUTCPLT_CB_ID :
1219 hcryp->OutCpltCallback = pCallback;
1220 break;
1221
1222 case HAL_CRYP_COMPCPLT_CB_ID :
1223 hcryp->CompCpltCallback = pCallback;
1224 break;
1225
1226 case HAL_CRYP_ERROR_CB_ID :
1227 hcryp->ErrorCallback = pCallback;
1228 break;
1229
1230 case HAL_CRYP_MSPINIT_CB_ID :
1231 hcryp->MspInitCallback = pCallback;
1232 break;
1233
1234 case HAL_CRYP_MSPDEINIT_CB_ID :
1235 hcryp->MspDeInitCallback = pCallback;
1236 break;
1237
1238 default :
1239 /* Update the error code */
1240 hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
1241 /* update return status */
1242 status = HAL_ERROR;
1243 break;
1244 }
1245 }
1246 else if(HAL_CRYP_STATE_RESET == hcryp->State)
1247 {
1248 switch (CallbackID)
1249 {
1250 case HAL_CRYP_MSPINIT_CB_ID :
1251 hcryp->MspInitCallback = pCallback;
1252 break;
1253
1254 case HAL_CRYP_MSPDEINIT_CB_ID :
1255 hcryp->MspDeInitCallback = pCallback;
1256 break;
1257
1258 default :
1259 /* Update the error code */
1260 hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
1261 /* update return status */
1262 status = HAL_ERROR;
1263 break;
1264 }
1265 }
1266 else
1267 {
1268 /* Update the error code */
1269 hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
1270 /* update return status */
1271 status = HAL_ERROR;
1272 }
1273
1274 /* Release Lock */
1275 __HAL_UNLOCK(hcryp);
1276 return status;
1277 }
1278
1279 /**
1280 * @brief Unregister a CRYP Callback
1281 * CRYP Callback is redirected to the weak (surcharged) predefined callback
1282 * @param hcryp CRYP handle
1283 * @param CallbackID ID of the callback to be unregistered
1284 * This parameter can be one of the following values:
1285 * @arg @ref HAL_CRYP_INPUTCPLT_CB_ID CRYP input DMA transfer completion Callback ID
1286 * @arg @ref HAL_CRYP_OUTPUTCPLT_CB_ID CRYP output DMA transfer completion Callback ID
1287 * @arg @ref HAL_CRYP_COMPCPLT_CB_ID CRYP computation completion Callback ID
1288 * @arg @ref HAL_CRYP_ERROR_CB_ID CRYP error callback ID
1289 * @arg @ref HAL_CRYP_MSPINIT_CB_ID CRYP MspDeInit callback ID
1290 * @arg @ref HAL_CRYP_MSPDEINIT_CB_ID CRYP MspDeInit callback ID
1291 * @retval status
1292 */
HAL_CRYP_UnRegisterCallback(CRYP_HandleTypeDef * hcryp,HAL_CRYP_CallbackIDTypeDef CallbackID)1293 HAL_StatusTypeDef HAL_CRYP_UnRegisterCallback(CRYP_HandleTypeDef *hcryp, HAL_CRYP_CallbackIDTypeDef CallbackID)
1294 {
1295 HAL_StatusTypeDef status = HAL_OK;
1296
1297 /* Process locked */
1298 __HAL_LOCK(hcryp);
1299
1300 if(HAL_CRYP_STATE_READY == hcryp->State)
1301 {
1302 switch (CallbackID)
1303 {
1304 case HAL_CRYP_INPUTCPLT_CB_ID :
1305 hcryp->InCpltCallback = HAL_CRYP_InCpltCallback; /* Legacy weak (surcharged) input DMA transfer completion callback */
1306 break;
1307
1308 case HAL_CRYP_OUTPUTCPLT_CB_ID :
1309 hcryp->OutCpltCallback = HAL_CRYP_OutCpltCallback; /* Legacy weak (surcharged) output DMA transfer completion callback */
1310 break;
1311
1312 case HAL_CRYP_COMPCPLT_CB_ID :
1313 hcryp->CompCpltCallback = HAL_CRYPEx_ComputationCpltCallback; /* Legacy weak (surcharged) computation completion callback */
1314 break;
1315
1316 case HAL_CRYP_ERROR_CB_ID :
1317 hcryp->ErrorCallback = HAL_CRYP_ErrorCallback; /* Legacy weak (surcharged) error callback */
1318 break;
1319
1320 case HAL_CRYP_MSPINIT_CB_ID :
1321 hcryp->MspInitCallback = HAL_CRYP_MspInit; /* Legacy weak (surcharged) Msp DeInit */
1322 break;
1323
1324 case HAL_CRYP_MSPDEINIT_CB_ID :
1325 hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit; /* Legacy weak (surcharged) Msp DeInit */
1326 break;
1327
1328 default :
1329 /* Update the error code */
1330 hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
1331 /* update return status */
1332 status = HAL_ERROR;
1333 break;
1334 }
1335 }
1336 else if(HAL_CRYP_STATE_RESET == hcryp->State)
1337 {
1338 switch (CallbackID)
1339 {
1340 case HAL_CRYP_MSPINIT_CB_ID :
1341 hcryp->MspInitCallback = HAL_CRYP_MspInit; /* Legacy weak (surcharged) Msp Init */
1342 break;
1343
1344 case HAL_CRYP_MSPDEINIT_CB_ID :
1345 hcryp->MspDeInitCallback = HAL_CRYP_MspDeInit; /* Legacy weak (surcharged) Msp DeInit */
1346 break;
1347
1348 default :
1349 /* Update the error code */
1350 hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
1351 /* update return status */
1352 status = HAL_ERROR;
1353 break;
1354 }
1355 }
1356 else
1357 {
1358 /* Update the error code */
1359 hcryp->ErrorCode |= HAL_CRYP_ERROR_INVALID_CALLBACK;
1360 /* update return status */
1361 status = HAL_ERROR;
1362 }
1363
1364 /* Release Lock */
1365 __HAL_UNLOCK(hcryp);
1366 return status;
1367 }
1368 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1369
1370 /**
1371 * @}
1372 */
1373
1374 /** @defgroup CRYP_Exported_Functions_Group4 CRYP IRQ handler
1375 * @brief AES IRQ handler.
1376 *
1377 @verbatim
1378 ==============================================================================
1379 ##### AES IRQ handler management #####
1380 ==============================================================================
1381 [..] This section provides AES IRQ handler function.
1382
1383 @endverbatim
1384 * @{
1385 */
1386
1387 /**
1388 * @brief Handle AES interrupt request.
1389 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1390 * the configuration information for CRYP module
1391 * @retval None
1392 */
HAL_CRYP_IRQHandler(CRYP_HandleTypeDef * hcryp)1393 void HAL_CRYP_IRQHandler(CRYP_HandleTypeDef *hcryp)
1394 {
1395 /* Check if error occurred */
1396 if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_ERRIE) != RESET)
1397 {
1398 /* If Write Error occurred */
1399 if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_WRERR) != RESET)
1400 {
1401 hcryp->ErrorCode |= HAL_CRYP_WRITE_ERROR;
1402 hcryp->State = HAL_CRYP_STATE_ERROR;
1403 }
1404 /* If Read Error occurred */
1405 if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_RDERR) != RESET)
1406 {
1407 hcryp->ErrorCode |= HAL_CRYP_READ_ERROR;
1408 hcryp->State = HAL_CRYP_STATE_ERROR;
1409 }
1410
1411 /* If an error has been reported */
1412 if (hcryp->State == HAL_CRYP_STATE_ERROR)
1413 {
1414 /* Disable Error and Computation Complete Interrupts */
1415 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
1416 /* Clear all Interrupt flags */
1417 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_ERR_CLEAR|CRYP_CCF_CLEAR);
1418
1419 /* Process Unlocked */
1420 __HAL_UNLOCK(hcryp);
1421
1422 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1423 hcryp->ErrorCallback(hcryp);
1424 #else
1425 HAL_CRYP_ErrorCallback(hcryp);
1426 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1427
1428 return;
1429 }
1430
1431 }
1432
1433 /* Check if computation complete interrupt is enabled
1434 and if the computation complete flag is raised */
1435 if (__HAL_CRYP_GET_FLAG(hcryp, CRYP_IT_CCF) != RESET)
1436 {
1437 if (__HAL_CRYP_GET_IT_SOURCE(hcryp, CRYP_IT_CCFIE) != RESET)
1438 {
1439 #if defined(AES_CR_NPBLB)
1440 if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
1441 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CCM))
1442 #else
1443 if ((hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_GCM_GMAC)
1444 || (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC))
1445 #endif
1446 {
1447 /* To ensure proper suspension requests management, CCF flag
1448 is reset in CRYP_AES_Auth_IT() according to the current
1449 phase under handling */
1450 if (CRYP_AES_Auth_IT(hcryp) != HAL_OK)
1451 {
1452 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1453 hcryp->ErrorCallback(hcryp);
1454 #else
1455 HAL_CRYP_ErrorCallback(hcryp);
1456 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1457 }
1458 }
1459 else
1460 {
1461 /* Clear Computation Complete Flag */
1462 __HAL_CRYP_CLEAR_FLAG(hcryp, CRYP_CCF_CLEAR);
1463 if (CRYP_AES_IT(hcryp) != HAL_OK)
1464 {
1465 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1466 hcryp->ErrorCallback(hcryp);
1467 #else
1468 HAL_CRYP_ErrorCallback(hcryp);
1469 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1470 }
1471 }
1472 }
1473 }
1474 }
1475
1476 /**
1477 * @}
1478 */
1479
1480 /** @defgroup CRYP_Exported_Functions_Group5 Peripheral State functions
1481 * @brief Peripheral State functions.
1482 *
1483 @verbatim
1484 ==============================================================================
1485 ##### Peripheral State functions #####
1486 ==============================================================================
1487 [..]
1488 This subsection permits to get in run-time the status of the peripheral.
1489
1490 @endverbatim
1491 * @{
1492 */
1493
1494 /**
1495 * @brief Return the CRYP handle state.
1496 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1497 * the configuration information for CRYP module
1498 * @retval HAL state
1499 */
HAL_CRYP_GetState(CRYP_HandleTypeDef * hcryp)1500 HAL_CRYP_STATETypeDef HAL_CRYP_GetState(CRYP_HandleTypeDef *hcryp)
1501 {
1502 /* Return CRYP handle state */
1503 return hcryp->State;
1504 }
1505
1506 /**
1507 * @brief Return the CRYP peripheral error.
1508 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1509 * the configuration information for CRYP module
1510 * @note The returned error is a bit-map combination of possible errors
1511 * @retval Error bit-map
1512 */
HAL_CRYP_GetError(CRYP_HandleTypeDef * hcryp)1513 uint32_t HAL_CRYP_GetError(CRYP_HandleTypeDef *hcryp)
1514 {
1515 return hcryp->ErrorCode;
1516 }
1517
1518 /**
1519 * @}
1520 */
1521
1522 /**
1523 * @}
1524 */
1525
1526 /** @addtogroup CRYP_Private_Functions
1527 * @{
1528 */
1529
1530
1531 /**
1532 * @brief Write the Key in KeyRx registers.
1533 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1534 * the configuration information for CRYP module
1535 * @retval None
1536 */
CRYP_SetKey(CRYP_HandleTypeDef * hcryp)1537 static HAL_StatusTypeDef CRYP_SetKey(CRYP_HandleTypeDef *hcryp)
1538 {
1539 uint32_t keyaddr;
1540
1541 if (hcryp->Init.pKey == NULL)
1542 {
1543 return HAL_ERROR;
1544 }
1545
1546
1547 keyaddr = (uint32_t)(hcryp->Init.pKey);
1548
1549 if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
1550 {
1551 hcryp->Instance->KEYR7 = __REV(*(uint32_t*)(keyaddr));
1552 keyaddr+=4U;
1553 hcryp->Instance->KEYR6 = __REV(*(uint32_t*)(keyaddr));
1554 keyaddr+=4U;
1555 hcryp->Instance->KEYR5 = __REV(*(uint32_t*)(keyaddr));
1556 keyaddr+=4U;
1557 hcryp->Instance->KEYR4 = __REV(*(uint32_t*)(keyaddr));
1558 keyaddr+=4U;
1559 }
1560
1561 hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
1562 keyaddr+=4U;
1563 hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
1564 keyaddr+=4U;
1565 hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
1566 keyaddr+=4U;
1567 hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));
1568
1569 return HAL_OK;
1570 }
1571
1572 /**
1573 * @brief Write the InitVector/InitCounter in IVRx registers.
1574 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1575 * the configuration information for CRYP module
1576 * @retval None
1577 */
CRYP_SetInitVector(CRYP_HandleTypeDef * hcryp)1578 static HAL_StatusTypeDef CRYP_SetInitVector(CRYP_HandleTypeDef *hcryp)
1579 {
1580 uint32_t ivaddr;
1581
1582 #if !defined(AES_CR_NPBLB)
1583 if (hcryp->Init.ChainingMode == CRYP_CHAINMODE_AES_CMAC)
1584 {
1585 hcryp->Instance->IVR3 = 0;
1586 hcryp->Instance->IVR2 = 0;
1587 hcryp->Instance->IVR1 = 0;
1588 hcryp->Instance->IVR0 = 0;
1589 }
1590 else
1591 #endif
1592 {
1593 if (hcryp->Init.pInitVect == NULL)
1594 {
1595 return HAL_ERROR;
1596 }
1597
1598 ivaddr = (uint32_t)(hcryp->Init.pInitVect);
1599
1600 hcryp->Instance->IVR3 = __REV(*(uint32_t*)(ivaddr));
1601 ivaddr+=4U;
1602 hcryp->Instance->IVR2 = __REV(*(uint32_t*)(ivaddr));
1603 ivaddr+=4U;
1604 hcryp->Instance->IVR1 = __REV(*(uint32_t*)(ivaddr));
1605 ivaddr+=4U;
1606 hcryp->Instance->IVR0 = __REV(*(uint32_t*)(ivaddr));
1607 }
1608 return HAL_OK;
1609 }
1610
1611
1612
1613 /**
1614 * @brief Handle CRYP block input/output data handling under interruption.
1615 * @note The function is called under interruption only, once
1616 * interruptions have been enabled by HAL_CRYPEx_AES_IT().
1617 * @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
1618 * the configuration information for CRYP module.
1619 * @retval HAL status
1620 */
CRYP_AES_IT(CRYP_HandleTypeDef * hcryp)1621 static HAL_StatusTypeDef CRYP_AES_IT(CRYP_HandleTypeDef *hcryp)
1622 {
1623 uint32_t inputaddr;
1624 uint32_t outputaddr = (uint32_t)hcryp->pCrypOutBuffPtr;
1625
1626 if(hcryp->State == HAL_CRYP_STATE_BUSY)
1627 {
1628 if (hcryp->Init.OperatingMode != CRYP_ALGOMODE_KEYDERIVATION)
1629 {
1630 /* Read the last available output block from the Data Output Register */
1631 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
1632 outputaddr+=4U;
1633 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
1634 outputaddr+=4U;
1635 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
1636 outputaddr+=4U;
1637 *(uint32_t*)(outputaddr) = hcryp->Instance->DOUTR;
1638 hcryp->pCrypOutBuffPtr += 16;
1639 hcryp->CrypOutCount -= 16U;
1640
1641 }
1642 else
1643 {
1644 /* Read the derived key from the Key registers */
1645 if (hcryp->Init.KeySize == CRYP_KEYSIZE_256B)
1646 {
1647 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR7);
1648 outputaddr+=4U;
1649 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR6);
1650 outputaddr+=4U;
1651 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR5);
1652 outputaddr+=4U;
1653 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR4);
1654 outputaddr+=4U;
1655 }
1656
1657 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR3);
1658 outputaddr+=4U;
1659 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR2);
1660 outputaddr+=4U;
1661 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR1);
1662 outputaddr+=4U;
1663 *(uint32_t*)(outputaddr) = __REV(hcryp->Instance->KEYR0);
1664 }
1665
1666 /* In case of ciphering or deciphering, check if all output text has been retrieved;
1667 In case of key derivation, stop right there */
1668 if ((hcryp->CrypOutCount == 0U) || (hcryp->Init.OperatingMode == CRYP_ALGOMODE_KEYDERIVATION))
1669 {
1670 /* Disable Computation Complete Flag and Errors Interrupts */
1671 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
1672 /* Change the CRYP state */
1673 hcryp->State = HAL_CRYP_STATE_READY;
1674
1675 /* Process Unlocked */
1676 __HAL_UNLOCK(hcryp);
1677
1678 /* Call computation complete callback */
1679 #if (USE_HAL_CRYP_REGISTER_CALLBACKS == 1)
1680 hcryp->CompCpltCallback(hcryp);
1681 #else
1682 HAL_CRYPEx_ComputationCpltCallback(hcryp);
1683 #endif /* USE_HAL_CRYP_REGISTER_CALLBACKS */
1684
1685 return HAL_OK;
1686 }
1687 /* If suspension flag has been raised, suspend processing */
1688 else if (hcryp->SuspendRequest == HAL_CRYP_SUSPEND)
1689 {
1690 /* reset ModeSuspend */
1691 hcryp->SuspendRequest = HAL_CRYP_SUSPEND_NONE;
1692
1693 /* Disable Computation Complete Flag and Errors Interrupts */
1694 __HAL_CRYP_DISABLE_IT(hcryp, CRYP_IT_CCFIE|CRYP_IT_ERRIE);
1695 /* Change the CRYP state */
1696 hcryp->State = HAL_CRYP_STATE_SUSPENDED;
1697
1698 /* Process Unlocked */
1699 __HAL_UNLOCK(hcryp);
1700
1701 return HAL_OK;
1702 }
1703 else /* Process the rest of input data */
1704 {
1705 /* Get the Intput data address */
1706 inputaddr = (uint32_t)hcryp->pCrypInBuffPtr;
1707
1708 /* Increment/decrement instance pointer/counter */
1709 hcryp->pCrypInBuffPtr += 16;
1710 hcryp->CrypInCount -= 16U;
1711
1712 /* Write the next input block in the Data Input register */
1713 hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1714 inputaddr+=4U;
1715 hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1716 inputaddr+=4U;
1717 hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1718 inputaddr+=4U;
1719 hcryp->Instance->DINR = *(uint32_t*)(inputaddr);
1720
1721 return HAL_OK;
1722 }
1723 }
1724 else
1725 {
1726 return HAL_BUSY;
1727 }
1728 }
1729
1730
1731
1732
1733 /**
1734 * @}
1735 */
1736
1737
1738
1739 /**
1740 * @}
1741 */
1742
1743 /**
1744 * @}
1745 */
1746
1747 #endif /* AES */
1748
1749 #endif /* HAL_CRYP_MODULE_ENABLED */
1750
1751 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
1752