1 /*--------------------------------------------------------------------------*/
2 /* Copyright 2020-2021 NXP                                                  */
3 /*                                                                          */
4 /* NXP Confidential. This software is owned or controlled by NXP and may    */
5 /* only be used strictly in accordance with the applicable license terms.   */
6 /* By expressly accepting such terms or by downloading, installing,         */
7 /* activating and/or otherwise using the software, you are agreeing that    */
8 /* you have read, and that you agree to comply with and are bound by, such  */
9 /* license terms. If you do not agree to be bound by the applicable license */
10 /* terms, then you may not retain, install, activate or otherwise use the   */
11 /* software.                                                                */
12 /*--------------------------------------------------------------------------*/
13 
14 /** @file  impl/mcuxClCss_Cipher.c
15  *  @brief CSSv2 implementation for symmetric ciphers.
16  * This file implements the functions declared in mcuxClCss_Cipher.h. */
17 
18 #include <mcuxCsslFlowProtection.h>
19 #include <platform_specific_headers.h>
20 #include <mcuxClCss.h>
21 #include <internal/mcuxClCss_Internal.h>
22 
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_Cipher_Async)23 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_Cipher_Async)
24 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_Cipher_Async(mcuxClCss_CipherOption_t options,
25                                                                   mcuxClCss_KeyIndex_t keyIdx,
26                                                                   uint8_t const *pKey,
27                                                                   size_t keyLength,
28                                                                   uint8_t const *pInput,
29                                                                   size_t inputLength,
30                                                                   uint8_t *pIV,
31                                                                   uint8_t *pOutput)
32 {
33     MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_Cipher_Async);
34 
35     // ignored misra violation -> false positive
36     // misra_c_2012_rule_11_9_violation: Literal 0 shall not be used as null pointer constant.
37     MCUXCLCSS_INPUT_PARAM_CHECK_PROTECTED(
38         mcuxClCss_Cipher_Async,
39         (0U == inputLength) || (0U != (inputLength % MCUXCLCSS_CIPHER_BLOCK_SIZE_AES)) ||
40             (MCUXCLCSS_CIPHER_INTERNAL_KEY == options.bits.extkey && CSS_KS_CNT <= keyIdx) ||
41             ((MCUXCLCSS_CIPHER_EXTERNAL_KEY == options.bits.extkey) &&
42              ((MCUXCLCSS_CIPHER_KEY_SIZE_AES_128 != keyLength) && (MCUXCLCSS_CIPHER_KEY_SIZE_AES_192 != keyLength) &&
43               (MCUXCLCSS_CIPHER_KEY_SIZE_AES_256 != keyLength))) ||
44             (MCUXCLCSS_CIPHERPARAM_ALGORITHM_AES_CTR < options.bits.cphmde)
45             /* ECB doesn't support importing or exporting an IV */
46             || ((MCUXCLCSS_CIPHERPARAM_ALGORITHM_AES_ECB == options.bits.cphmde) &&
47                 ((MCUXCLCSS_CIPHER_STATE_IN_ENABLE == options.bits.cphsie) ||
48                  (MCUXCLCSS_CIPHER_STATE_OUT_ENABLE == options.bits.cphsoe))));
49 
50     /* CSS SFRs are not cached => Tell SW to wait for CSS to come back from BUSY state before modifying the SFRs */
51     if (MCUXCLCSS_ISBUSY)
52     {
53         MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_Cipher_Async, MCUXCLCSS_STATUS_SW_CANNOT_INTERRUPT);
54     }
55 
56     MCUXCLCSS_SETCSSINPUT0(pInput, inputLength);
57     if (0U == options.bits.extkey)
58     {
59         MCUXCLCSS_SETKEYSTOREINDEX0(keyIdx);
60     }
61     else
62     {
63         MCUXCLCSS_SETCSSINPUT2(pKey, keyLength);
64     }
65 
66     MCUXCLCSS_SETCSSINPUT1_FIXEDSIZE(pIV);
67     MCUXCLCSS_SETCSSOUTPUT_FIXEDSIZE(pOutput);
68     MCUXCLCSS_STARTCOMMAND(ID_CFG_CSS_CMD_CIPHER, options.word.value, CSS_CMD_BIG_ENDIAN);
69 
70     MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_Cipher_Async, MCUXCLCSS_STATUS_OK_WAIT);
71 }
72