MCUX CLNS
MCUX Crypto Library Normal Secure
Loading...
Searching...
No Matches
mcuxClExample_RFC3394_Helper.h
1/*--------------------------------------------------------------------------*/
2/* Copyright 2022-2024 NXP */
3/* */
4/* SPDX-License-Identifier: BSD-3-Clause */
5/* */
6/* Redistribution and use in source and binary forms, with or without */
7/* modification, are permitted provided that the following conditions are */
8/* met: */
9/* */
10/* 1. Redistributions of source code must retain the above copyright */
11/* notice, this list of conditions and the following disclaimer. */
12/* */
13/* 2. Redistributions in binary form must reproduce the above copyright */
14/* notice, this list of conditions and the following disclaimer in the */
15/* documentation and/or other materials provided with the distribution. */
16/* */
17/* 3. Neither the name of the copyright holder nor the names of its */
18/* contributors may be used to endorse or promote products derived from */
19/* this software without specific prior written permission. */
20/* */
21/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS */
22/* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED */
23/* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A */
24/* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT */
25/* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, */
26/* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED */
27/* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR */
28/* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */
29/* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */
30/* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
31/* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
32/*--------------------------------------------------------------------------*/
33
34#ifndef MCUXCLEXAMPLE_RFC3394_HELPER_H_
35#define MCUXCLEXAMPLE_RFC3394_HELPER_H_
36
37#include <mcuxClConfig.h> // Exported features flags header
38#include <mcuxClCore_Platform.h>
39#include <mcuxClEls.h>
42#include <mcuxClMemory_Copy.h>
43
48static inline bool mcuxClExample_rfc3394_wrap(
49 const uint32_t * pInput, //< pointer to key to be wrapped
50 size_t inputLength, //< length of key to be wrapped in bytes
51 const uint8_t * pKek_in, //< pointer to key wrapping key
52 mcuxClEls_KeyIndex_t keyIdx, //< keyslot index of key wrapping key
53 uint8_t extkey, //< 0-use key stored internally at keyIdx as wrapping key, 1-use external pKek_in as wrapping key
54 size_t kekLength, //< length of key wrapping key in bytes
55 uint32_t * pOutput, //< pointer to output buffer, size has to be inputLength + 16 bytes
56 mcuxClEls_KeyProp_t properties //< properties of the key to be wrapped
57)
58{
59 uint32_t concat[MCUXCLELS_CIPHER_BLOCK_SIZE_AES/sizeof(uint32_t)] = { 0u };
60
61 uint32_t input[MCUXCLELS_CIPHER_BLOCK_SIZE_AES/sizeof(uint32_t)] = { 0u };
62 uint8_t pKek[MCUXCLELS_CIPHER_KEY_SIZE_AES_256] = { 0u };
63
65 {
67 pKek_in,
68 kekLength,
69 kekLength));
70
72 {
73 return false;
74 }
75
77 }
78
79 // Intermediate state is stored in pOutput as it is large enough
80
81 // initialize buffer
82 concat[0] = 0xA6A6A6A6u; // first half of concat is the A from the standard, initialized to RFC3394 IV
83 concat[1] = 0xA6A6A6A6u;
84 concat[2] = properties.word.value; // second half of concat is R(input)/B(output), first R consists of properties...
85 concat[3] = 0x00000000u; // ... and ELS padding (zeros)
86 concat[2] = (concat[2] << 24u) | (concat[2] >> 24u) | ((concat[2] & 0x0000ff00u) << 8u) | ((concat[2] >> 8u) & 0x0000ff00u); // swap endianness
87
88 // initialize ELS encryption parameters
89 mcuxClEls_CipherOption_t cipher_options;
90 cipher_options.word.value = 0;
92 cipher_options.bits.dcrpt = MCUXCLELS_CIPHER_ENCRYPT;
93 cipher_options.bits.extkey = extkey;
94
95 // input has to be multiple of 64 bits
96 if (inputLength % sizeof(uint64_t) != 0u)
97 {
98 return false;
99 }
100
101 const uint32_t *pSource = pInput;
102 uint32_t *pDest = pOutput;
103 uint32_t std_n = inputLength/sizeof(uint64_t) + 1u; // n value from standard
104 for(size_t jdx = 0u; jdx < 6u; jdx++)
105 {
106 for(size_t idx = 0u; idx < std_n; idx++)
107 {
108 input[0]=concat[0];
109 input[1]=concat[1];
110 input[2]=concat[2];
111 input[3]=concat[3];
112
113 // Encrypt concatenated A and chunk to be processed
114 MCUX_CSSL_ANALYSIS_START_PATTERN_NULL_POINTER_CONSTANT()
115 MCUX_CSSL_ANALYSIS_START_SUPPRESS_ESCAPING_LOCAL_ADDRESS("Address of concat is for local use only. The call to mcuxClEls_WaitForOperation ensures it cannot escape.")
117 cipher_options,
118 keyIdx,
119 pKek,
120 kekLength,
121 (uint8_t*) input,
123 NULL,
124 (uint8_t*) concat));
125 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ESCAPING_LOCAL_ADDRESS()
126 MCUX_CSSL_ANALYSIS_STOP_PATTERN_NULL_POINTER_CONSTANT()
128 {
129 return false;
130 }
132
135 {
136 return false;
137 }
139
140 // Write out processed key chunk
141 pDest[2u*idx + 2u] = concat[2u];
142 pDest[2u*idx + 3u] = concat[3u];
143
144 if( idx == std_n - 1u)
145 {
146 // Load next key chunk
147 concat[2u] = pDest[2u];
148 concat[3u] = pDest[3u];
149 pSource = pDest + 4;
150 }
151 else
152 {
153 // Load next key chunk
154 concat[2u] = pSource[2u*idx + 0u];
155 concat[3u] = pSource[2u*idx + 1u];
156 }
157
158 // XOR round constant into A
159 uint32_t gdx = std_n * jdx + (idx + 1u); // all values should fit into a uint32_t
160 gdx = (gdx << 24u) | (gdx >> 24u) | ((gdx & 0x0000ff00u) << 8u) | ((gdx >> 8u) & 0x0000ff00u); // swap endianness
161 concat[1u] ^= gdx;
162 }
163 }
164 // Write out processed key chunk
165 pDest[0u] = concat[0u];
166 pDest[1u] = concat[1u];
167
168 return true;
169}
170
175static inline bool mcuxClExample_rfc3394_unwrap(
176 const uint32_t * pInput, //< pointer to rfc3394 blob to be wrapped
177 size_t inputLength, //< length of key the rfc3394 blob in bytes
178 const uint8_t * pKek_in, //< pointer to key wrapping key
179 mcuxClEls_KeyIndex_t keyIdx, //< keyslot index of key wrapping key
180 uint8_t extkey, //< 0-use key stored internally at keyIdx as wrapping key, 1-use external pKek_in as wrapping key
181 size_t kekLength, //< length of key wrapping key in bytes
182 uint32_t * pOutput //< pointer to output buffer, size has to inputLength - 8 bytes, contents will be properties|zeros|key
183)
184{
185 uint32_t concat[MCUXCLELS_CIPHER_BLOCK_SIZE_AES/sizeof(uint32_t)] = { 0u };
186
187 uint32_t input[MCUXCLELS_CIPHER_BLOCK_SIZE_AES/sizeof(uint32_t)] = { 0u };
188 uint8_t pKek[MCUXCLELS_CIPHER_KEY_SIZE_AES_256] = { 0u };
189
190 if (extkey == MCUXCLELS_CIPHER_EXTERNAL_KEY)
191 {
193 pKek_in,
194 kekLength,
195 kekLength));
196
198 {
199 return false;
200 }
201
203 }
204
205 // initialize buffer
206 concat[0] = pInput[0]; // first half of concat is the A from the standard, to first chunk of input
207 concat[1] = pInput[1];
208
209 // initialize ELS encryption parameters
210 mcuxClEls_CipherOption_t cipher_options;
211 cipher_options.word.value = 0;
213 cipher_options.bits.dcrpt = MCUXCLELS_CIPHER_DECRYPT;
214 cipher_options.bits.extkey = extkey;
215
216 // input has to be multiple of 64 bits, and needs to be in range
217 if ((inputLength % sizeof(uint64_t) != 0u)
218#ifdef MCUXCL_FEATURE_ELS_PUK_INTERNAL
219 || (inputLength > MCUXCLELS_RFC3394_CONTAINER_SIZE_P256))
220#else
221 || (inputLength > MCUXCLELS_RFC3394_CONTAINER_SIZE_256))
222#endif
223 {
224 return false;
225 }
226
227 uint32_t std_n = inputLength/sizeof(uint64_t) - 1u; // n value from standard
228 const uint32_t *pSource = pInput + 2u; // skip first 64 bits
229 uint32_t *pDest = pOutput;
230 for(size_t jdx = 6u; jdx > 0u; jdx--)
231 {
232 for(size_t idx = std_n; idx > 0u; idx--)
233 {
234 // Load next key chunk
235 MCUX_CSSL_ANALYSIS_START_SUPPRESS_INTEGER_OVERFLOW("Index calculation cannot wrap.")
236 concat[2u] = pSource[2u * (idx-1u) + 0u];
237 concat[3u] = pSource[2u * (idx-1u) + 1u];
238 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_INTEGER_OVERFLOW()
239
240 // XOR round constant into A
241 MCUX_CSSL_ANALYSIS_START_SUPPRESS_INTEGER_WRAP("The result will fit into 32 bit, as inputLength has an upper bound as checked above.")
242 uint32_t gdx = std_n * (jdx-1u) + idx; // all values should fit into a uint32_t
243 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_INTEGER_WRAP()
244 gdx = (gdx << 24u) | (gdx >> 24u) | ((gdx & 0x0000ff00u) << 8u) | ((gdx >> 8u) & 0x0000ff00u); // swap endianness
245 concat[1u] ^= gdx;
246
247 input[0]=concat[0];
248 input[1]=concat[1];
249 input[2]=concat[2];
250 input[3]=concat[3];
251
252 // Decrypt concatenated A and chunk to be processed
253 MCUX_CSSL_ANALYSIS_START_PATTERN_NULL_POINTER_CONSTANT()
254 MCUX_CSSL_ANALYSIS_START_SUPPRESS_ESCAPING_LOCAL_ADDRESS("Address of concat is for local use only. The call to mcuxClEls_WaitForOperation ensures it cannot escape.")
256 cipher_options,
257 keyIdx,
258 pKek,
259 kekLength,
260 (uint8_t*) input,
262 NULL,
263 (uint8_t*) concat));
264 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_ESCAPING_LOCAL_ADDRESS()
265 MCUX_CSSL_ANALYSIS_STOP_PATTERN_NULL_POINTER_CONSTANT()
267 {
268 return false;
269 }
271
274 {
275 return false;
276 }
278
279 // Write out processed key chunk
280 MCUX_CSSL_ANALYSIS_START_SUPPRESS_INTEGER_OVERFLOW("Index calculation cannot wrap.")
281 pDest[2u * (idx-1u) + 0u] = concat[2u];
282 pDest[2u * (idx-1u) + 1u] = concat[3u];
283 MCUX_CSSL_ANALYSIS_STOP_SUPPRESS_INTEGER_OVERFLOW()
284 }
285 pSource = pDest;
286 }
287 pDest[0] = (pDest[0] << 24u) | (pDest[0] >> 24u) | ((pDest[0] & 0x0000ff00u) << 8u) | ((pDest[0] >> 8u) & 0x0000ff00u); // swap endianness
288 // Check padding
289 if((concat[0u] != 0xA6A6A6A6u) || (concat[1u] != 0xA6A6A6A6u))
290 {
291 return false;
292 }
293 return true;
294}
295
296#endif /* MCUXCLEXAMPLE_RFC3394_HELPER_H_ */
Definition of function identifiers for the flow protection mechanism.
Top-level include file for the ELS driver.
Memory header for copy functions.
Provides the API for the CSSL flow protection mechanism.
#define MCUXCLELS_CIPHER_BLOCK_SIZE_AES
Size of an AES input block: 128 bit (16 bytes).
Definition mcuxClEls_Cipher.h:107
#define MCUXCLELS_CIPHER_DECRYPT
Set this option at mcuxClEls_CipherOption_t.dcrpt to perform a decryption.
Definition mcuxClEls_Cipher.h:83
#define MCUXCLELS_CIPHERPARAM_ALGORITHM_AES_ECB
Set this option at mcuxClEls_CipherOption_t.cphmde to use AES engine in Electornic Code Book (ECB) mo...
Definition mcuxClEls_Cipher.h:96
#define MCUXCLELS_CIPHER_ENCRYPT
Set this option at mcuxClEls_CipherOption_t.dcrpt to perform an encryption.
Definition mcuxClEls_Cipher.h:82
#define MCUXCLELS_CIPHER_EXTERNAL_KEY
Set this option at mcuxClEls_CipherOption_t.extkey to use a key located in CPU memory provided by pKe...
Definition mcuxClEls_Cipher.h:93
#define MCUXCLELS_CIPHER_KEY_SIZE_AES_256
Size of an AES192 key: 256 bit (32 bytes).
Definition mcuxClEls_Cipher.h:118
MCUXCLELS_API mcuxClEls_Status_t mcuxClEls_Cipher_Async(mcuxClEls_CipherOption_t options, mcuxClEls_KeyIndex_t keyIdx, uint8_t const *pKey, size_t keyLength, uint8_t const *pInput, size_t inputLength, uint8_t *pIV, uint8_t *pOutput)
Performs AES encryption/decryption.
#define MCUXCLELS_ERROR_FLAGS_CLEAR
Set this option at mcuxClEls_ErrorHandling_t to clear all ELS error flags.
Definition mcuxClEls_Common.h:130
MCUXCLELS_API mcuxClEls_Status_t mcuxClEls_WaitForOperation(mcuxClEls_ErrorHandling_t errorHandling)
Wait for an ELS operation and optionally clear the error status.
#define MCUXCLELS_RFC3394_CONTAINER_SIZE_256
Size of RFC3394 container for 256 bit key.
Definition mcuxClEls_KeyManagement.h:127
#define MCUXCLELS_STATUS_OK
No error occurred.
Definition mcuxClEls_Types.h:191
#define MCUXCLELS_STATUS_OK_WAIT
An _Async function successfully started an ELS command. Call mcuxClEls_WaitForOperation to complete i...
Definition mcuxClEls_Types.h:192
uint32_t mcuxClEls_KeyIndex_t
Type for ELS keystore indices.
Definition mcuxClEls_Types.h:242
void mcuxClMemory_copy(uint8_t *pDst, uint8_t const *pSrc, size_t length, size_t bufLength)
Copies a memory buffer to another location.
#define MCUX_CSSL_FP_FUNCTION_CALL_VOID_BEGIN(...)
Call a flow protected void function and check the protection token.
Definition mcuxCsslFlowProtection.h:672
#define MCUX_CSSL_FP_FUNCTION_CALL_VOID_END(...)
End a void function call section started by MCUX_CSSL_FP_FUNCTION_CALL_VOID_BEGIN.
Definition mcuxCsslFlowProtection.h:703
#define MCUX_CSSL_FP_FUNCTION_CALL_BEGIN(...)
Call a flow protected function and check the protection token.
Definition mcuxCsslFlowProtection.h:601
#define MCUX_CSSL_FP_FUNCTION_CALLED(...)
Expectation of a called function.
Definition mcuxCsslFlowProtection.h:755
#define MCUX_CSSL_FP_FUNCTION_CALL_END(...)
End a function call section started by MCUX_CSSL_FP_FUNCTION_CALL_BEGIN.
Definition mcuxCsslFlowProtection.h:636
Command option bit field for mcuxClEls_Cipher_Async.
Definition mcuxClEls_Cipher.h:141
uint32_t value
Accesses the bit field as a full word.
Definition mcuxClEls_Cipher.h:144
uint32_t cphmde
Define cipher mode.
Definition mcuxClEls_Cipher.h:150
uint32_t extkey
Define whether an external key from memory or ELS internal key should be used.
Definition mcuxClEls_Cipher.h:158
struct mcuxClEls_CipherOption_t::@040146127063252077303251341061227266321102231343 word
Access mcuxClEls_CipherOption_t word-wise.
struct mcuxClEls_CipherOption_t::@211376152271043115107205214261204042163004233364 bits
Access mcuxClEls_CipherOption_t bit-wise.
uint32_t dcrpt
Define operation mode.
Definition mcuxClEls_Cipher.h:149
Type for ELS key store key properties.
Definition mcuxClEls_Types.h:246
struct mcuxClEls_KeyProp_t::@275171221237375205140351133234315312172366173335 word
Access mcuxClEls_KeyProp_t word-wise.
uint32_t value
Accesses the bit field as a full word; initialize with a combination of constants from MCUXCLELS_KEYP...
Definition mcuxClEls_Types.h:249