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  mcuxClCss_Kdf.c
15  *  @brief CSSv2 implementation for key derivation.
16  * This file implements the functions declared in mcuxClCss_Kdf.h. */
17 
18 #include <mcuxClCss_Kdf.h>
19 #include <mcuxClCss_Hash.h>
20 #include <mcuxCsslFlowProtection.h>
21 #include <platform_specific_headers.h>
22 #include <mcuxClCss.h>
23 #include <internal/mcuxClCss_Internal.h>
24 
25 
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_Ckdf_Sp800108_Async)26 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_Ckdf_Sp800108_Async)
27 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_Ckdf_Sp800108_Async(
28     mcuxClCss_KeyIndex_t derivationKeyIdx,
29     mcuxClCss_KeyIndex_t targetKeyIdx,
30     mcuxClCss_KeyProp_t targetKeyProperties,
31     uint8_t const * pDerivationData)
32 {
33     MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_Ckdf_Sp800108_Async);
34     MCUXCLCSS_INPUT_PARAM_CHECK_PROTECTED(mcuxClCss_Ckdf_Sp800108_Async, (CSS_KS_CNT <= derivationKeyIdx) || (CSS_KS_CNT <= targetKeyIdx));
35 
36     /* CSS SFRs are not cached => Tell SW to wait for CSS to come back from BUSY state before modifying the SFRs */
37     if (MCUXCLCSS_ISBUSY)
38     {
39         MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_Ckdf_Sp800108_Async, MCUXCLCSS_STATUS_SW_CANNOT_INTERRUPT);
40     }
41 
42     mcuxClCss_CkdfOption_t option = {0};
43     option.bits.ckdf_algo = MCUXCLCSS_CKDF_ALGO_SP800108 ;
44 
45     MCUXCLCSS_SETKEYSTOREINDEX0(derivationKeyIdx);
46     MCUXCLCSS_SETKEYSTOREINDEX1(targetKeyIdx);
47     MCUXCLCSS_SETREQUESTEDKEYPROPERTIES(targetKeyProperties);
48     MCUXCLCSS_SETCSSINPUT0_FIXEDSIZE(pDerivationData);
49     MCUXCLCSS_STARTCOMMAND(ID_CFG_CSS_CMD_CKDF, option.word.value, CSS_CMD_BIG_ENDIAN);
50 
51     MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_Ckdf_Sp800108_Async, MCUXCLCSS_STATUS_OK_WAIT);
52 }
53 
54 
55 
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_Hkdf_Rfc5869_Async)56 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_Hkdf_Rfc5869_Async)
57 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_Hkdf_Rfc5869_Async(
58     mcuxClCss_HkdfOption_t options,
59     mcuxClCss_KeyIndex_t derivationKeyIdx,
60     mcuxClCss_KeyIndex_t targetKeyIdx,
61     mcuxClCss_KeyProp_t targetKeyProperties,
62     uint8_t const * pDerivationData)
63 {
64     MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_Hkdf_Rfc5869_Async);
65     MCUXCLCSS_INPUT_PARAM_CHECK_PROTECTED(mcuxClCss_Hkdf_Rfc5869_Async, (CSS_KS_CNT <= derivationKeyIdx) || (CSS_KS_CNT <= targetKeyIdx));
66 
67     /* CSS SFRs are not cached => Tell SW to wait for CSS to come back from BUSY state before modifying the SFRs */
68     if (MCUXCLCSS_ISBUSY)
69     {
70         MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_Hkdf_Rfc5869_Async, MCUXCLCSS_STATUS_SW_CANNOT_INTERRUPT);
71     }
72 
73     options.bits.hkdf_algo = MCUXCLCSS_HKDF_ALGO_RFC5869;
74 
75     MCUXCLCSS_SETKEYSTOREINDEX0(derivationKeyIdx);
76     MCUXCLCSS_SETKEYSTOREINDEX1(targetKeyIdx);
77     MCUXCLCSS_SETREQUESTEDKEYPROPERTIES(targetKeyProperties);
78     MCUXCLCSS_SETCSSINPUT0_FIXEDSIZE(pDerivationData);
79     MCUXCLCSS_STARTCOMMAND(ID_CFG_CSS_CMD_HKDF, options.word.value, CSS_CMD_BIG_ENDIAN);
80 
81     MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_Hkdf_Rfc5869_Async, MCUXCLCSS_STATUS_OK_WAIT);
82 }
83 
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_Hkdf_Sp80056c_Async)84 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_Hkdf_Sp80056c_Async)
85 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_Hkdf_Sp80056c_Async(
86     mcuxClCss_KeyIndex_t derivationKeyIdx,
87     uint8_t * pTagetKey,
88     uint8_t const * pDerivationData,
89     size_t derivationDataLength)
90 {
91     MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_Hkdf_Sp80056c_Async);
92     MCUXCLCSS_INPUT_PARAM_CHECK_PROTECTED(mcuxClCss_Hkdf_Rfc5869_Async, (CSS_KS_CNT <= derivationKeyIdx) );
93 
94     /* CSS SFRs are not cached => Tell SW to wait for CSS to come back from BUSY state before modifying the SFRs */
95     if (MCUXCLCSS_ISBUSY)
96     {
97         MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_Hkdf_Rfc5869_Async, MCUXCLCSS_STATUS_SW_CANNOT_INTERRUPT);
98     }
99 
100     mcuxClCss_HkdfOption_t options = {0};
101     options.bits.hkdf_algo = MCUXCLCSS_HKDF_ALGO_SP80056C;
102 
103     MCUXCLCSS_SETKEYSTOREINDEX0(derivationKeyIdx);
104     MCUXCLCSS_SETCSSINPUT0(pDerivationData, derivationDataLength);
105     MCUXCLCSS_SETCSSOUTPUT_FIXEDSIZE(pTagetKey);
106     MCUXCLCSS_STARTCOMMAND(ID_CFG_CSS_CMD_HKDF, options.word.value, CSS_CMD_BIG_ENDIAN);
107 
108     MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_Hkdf_Sp80056c_Async, MCUXCLCSS_STATUS_OK_WAIT);
109 }
110 
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_TlsGenerateMasterKeyFromPreMasterKey_Async)111 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_TlsGenerateMasterKeyFromPreMasterKey_Async)
112 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_TlsGenerateMasterKeyFromPreMasterKey_Async(
113     uint8_t const * pDerivationData,
114     mcuxClCss_KeyProp_t keyProperties,
115     mcuxClCss_KeyIndex_t keyIdx)
116 {
117     MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_TlsGenerateMasterKeyFromPreMasterKey_Async);
118     MCUXCLCSS_INPUT_PARAM_CHECK_PROTECTED(mcuxClCss_TlsGenerateMasterKeyFromPreMasterKey_Async, (CSS_KS_CNT <= keyIdx));
119 
120     /* CSS SFRs are not cached => Tell SW to wait for CSS to come back from BUSY state before modifying the SFRs */
121     if (MCUXCLCSS_ISBUSY)
122     {
123         MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_TlsGenerateMasterKeyFromPreMasterKey_Async, MCUXCLCSS_STATUS_SW_CANNOT_INTERRUPT);
124     }
125 
126     mcuxClCss_TlsOption_t options = {0};
127     options.bits.mode = MCUXCLCSS_TLS_INIT;
128 
129     MCUXCLCSS_SETKEYSTOREINDEX0(keyIdx);
130     MCUXCLCSS_SETREQUESTEDKEYPROPERTIES(keyProperties);
131     MCUXCLCSS_SETCSSINPUT0_FIXEDSIZE(pDerivationData);
132     MCUXCLCSS_STARTCOMMAND(ID_CFG_CSS_CMD_TLS, options.word.value, CSS_CMD_BIG_ENDIAN);
133 
134     MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_TlsGenerateMasterKeyFromPreMasterKey_Async, MCUXCLCSS_STATUS_OK_WAIT);
135 }
136 
MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_TlsGenerateSessionKeysFromMasterKey_Async)137 MCUX_CSSL_FP_FUNCTION_DEF(mcuxClCss_TlsGenerateSessionKeysFromMasterKey_Async)
138 MCUXCLCSS_API mcuxClCss_Status_Protected_t mcuxClCss_TlsGenerateSessionKeysFromMasterKey_Async(
139     uint8_t const * pDerivationData,
140     mcuxClCss_KeyProp_t keyProperties,
141     mcuxClCss_KeyIndex_t keyIdx)
142 {
143     MCUX_CSSL_FP_FUNCTION_ENTRY(mcuxClCss_TlsGenerateSessionKeysFromMasterKey_Async);
144     MCUXCLCSS_INPUT_PARAM_CHECK_PROTECTED(mcuxClCss_TlsGenerateSessionKeysFromMasterKey_Async, ((CSS_KS_CNT - 4U) <= keyIdx));
145 
146     /* CSS SFRs are not cached => Tell SW to wait for CSS to come back from BUSY state before modifying the SFRs */
147     if (MCUXCLCSS_ISBUSY)
148     {
149         MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_TlsGenerateSessionKeysFromMasterKey_Async, MCUXCLCSS_STATUS_SW_CANNOT_INTERRUPT);
150     }
151 
152     mcuxClCss_TlsOption_t options = {0};
153     options.bits.mode = MCUXCLCSS_TLS_FINALIZE;
154 
155     MCUXCLCSS_SETKEYSTOREINDEX0(keyIdx);
156     MCUXCLCSS_SETREQUESTEDKEYPROPERTIES(keyProperties);
157     MCUXCLCSS_SETCSSINPUT0_FIXEDSIZE(pDerivationData);
158     MCUXCLCSS_STARTCOMMAND(ID_CFG_CSS_CMD_TLS, options.word.value, CSS_CMD_BIG_ENDIAN);
159 
160     MCUX_CSSL_FP_FUNCTION_EXIT(mcuxClCss_TlsGenerateSessionKeysFromMasterKey_Async, MCUXCLCSS_STATUS_OK_WAIT);
161 }
162