15
15
* limitations under the License.
16
16
*/
17
17
18
- #include < platform/nxp/k32w/k32w1/FactoryDataProviderImpl.h>
19
-
20
- #if CHIP_DEVICE_CONFIG_SECURE_DAC_PRIVATE_KEY
21
18
#include " fsl_adapter_flash.h"
22
- #endif
19
+ #include < platform/nxp/k32w/k32w1/FactoryDataProviderImpl.h >
23
20
24
21
namespace chip {
25
22
namespace DeviceLayer {
26
23
24
+ #if !CHIP_USE_PLAIN_DAC_KEY
27
25
// SSS adds 24 bytes of metadata when creating the blob
28
26
static constexpr size_t kSssBlobMetadataLength = 24 ;
29
27
static constexpr size_t kPrivateKeyBlobLength = Crypto::kP256_PrivateKey_Length + kSssBlobMetadataLength ;
28
+ #endif
30
29
31
30
FactoryDataProviderImpl::~FactoryDataProviderImpl ()
32
31
{
32
+ #if !CHIP_USE_PLAIN_DAC_KEY
33
33
SSS_KEY_OBJ_FREE (&mContext );
34
+ #endif
34
35
}
35
36
36
37
CHIP_ERROR FactoryDataProviderImpl::Init ()
37
38
{
38
39
CHIP_ERROR error = CHIP_NO_ERROR;
39
40
40
- #if CHIP_DEVICE_CONFIG_ENABLE_SSS_API_TEST
41
+ #if CHIP_DEVICE_CONFIG_ENABLE_SSS_API_TEST && !CHIP_USE_PLAIN_DAC_KEY
41
42
SSS_RunApiTest ();
42
43
#endif
43
44
@@ -47,16 +48,56 @@ CHIP_ERROR FactoryDataProviderImpl::Init()
47
48
ChipLogError (DeviceLayer, " Factory data init failed with: %s" , ErrorStr (error));
48
49
}
49
50
51
+ #if !CHIP_USE_PLAIN_DAC_KEY
50
52
ReturnErrorOnFailure (SSS_InitContext ());
51
- #if CHIP_DEVICE_CONFIG_SECURE_DAC_PRIVATE_KEY
52
53
ReturnErrorOnFailure (SSS_ConvertDacKey ());
53
- ReturnErrorOnFailure (Validate ());
54
- #endif
55
54
ReturnErrorOnFailure (SSS_ImportPrivateKeyBlob ());
55
+ #endif
56
56
57
57
return error;
58
58
}
59
59
60
+ #if CHIP_USE_PLAIN_DAC_KEY
61
+ CHIP_ERROR FactoryDataProviderImpl::SignWithDacKey (const ByteSpan & messageToSign, MutableByteSpan & outSignBuffer)
62
+ {
63
+ CHIP_ERROR error = CHIP_NO_ERROR;
64
+ Crypto::P256ECDSASignature signature;
65
+ Crypto::P256Keypair keypair;
66
+ Crypto::P256SerializedKeypair serializedKeypair;
67
+ uint8_t keyBuf[Crypto::kP256_PrivateKey_Length ];
68
+ MutableByteSpan dacPrivateKeySpan (keyBuf);
69
+ uint16_t keySize = 0 ;
70
+
71
+ VerifyOrExit (!outSignBuffer.empty (), error = CHIP_ERROR_INVALID_ARGUMENT);
72
+ VerifyOrExit (!messageToSign.empty (), error = CHIP_ERROR_INVALID_ARGUMENT);
73
+ VerifyOrExit (outSignBuffer.size () >= signature.Capacity (), error = CHIP_ERROR_BUFFER_TOO_SMALL);
74
+
75
+ /* Get private key of DAC certificate from reserved section */
76
+ error = SearchForId (FactoryDataId::kDacPrivateKeyId , dacPrivateKeySpan.data (), dacPrivateKeySpan.size (), keySize);
77
+ SuccessOrExit (error);
78
+ dacPrivateKeySpan.reduce_size (keySize);
79
+ VerifyOrExit (keySize == Crypto::kP256_PrivateKey_Length , error = CHIP_ERROR_WRONG_KEY_TYPE);
80
+
81
+ /* Only the private key is used when signing */
82
+ error = serializedKeypair.SetLength (Crypto::kP256_PublicKey_Length + dacPrivateKeySpan.size ());
83
+ SuccessOrExit (error);
84
+ memcpy (serializedKeypair.Bytes () + Crypto::kP256_PublicKey_Length , dacPrivateKeySpan.data (), dacPrivateKeySpan.size ());
85
+
86
+ error = keypair.Deserialize (serializedKeypair);
87
+ SuccessOrExit (error);
88
+
89
+ error = keypair.ECDSA_sign_msg (messageToSign.data (), messageToSign.size (), signature);
90
+ SuccessOrExit (error);
91
+
92
+ error = CopySpanToMutableSpan (ByteSpan{ signature.ConstBytes (), signature.Length () }, outSignBuffer);
93
+
94
+ exit :
95
+ /* Sanitize temporary buffer */
96
+ memset (keyBuf, 0 , Crypto::kP256_PrivateKey_Length );
97
+ return error;
98
+ }
99
+
100
+ #else
60
101
CHIP_ERROR FactoryDataProviderImpl::SignWithDacKey (const ByteSpan & messageToSign, MutableByteSpan & outSignBuffer)
61
102
{
62
103
Crypto::P256ECDSASignature signature;
@@ -118,18 +159,25 @@ CHIP_ERROR FactoryDataProviderImpl::SSS_Sign(uint8_t * digest, Crypto::P256ECDSA
118
159
return error;
119
160
}
120
161
121
- #if CHIP_DEVICE_CONFIG_SECURE_DAC_PRIVATE_KEY
122
162
CHIP_ERROR FactoryDataProviderImpl::SSS_ConvertDacKey ()
123
163
{
124
164
size_t blobSize = kPrivateKeyBlobLength ;
125
165
size_t newSize = sizeof (FactoryDataProvider::Header) + mHeader .size + kSssBlobMetadataLength ;
126
166
uint8_t blob[kPrivateKeyBlobLength ] = { 0 };
127
167
uint8_t * data = static_cast <uint8_t *>(chip::Platform::MemoryAlloc (newSize));
128
168
uint32_t offset = 0 ;
169
+ bool convNeeded = true ;
129
170
130
171
VerifyOrReturnError (data != nullptr , CHIP_ERROR_INTERNAL);
131
172
132
- ReturnErrorOnFailure (SSS_ExportBlob (blob, &blobSize, offset));
173
+ ReturnErrorOnFailure (SSS_ExportBlob (blob, &blobSize, offset, convNeeded));
174
+ if (!convNeeded)
175
+ {
176
+ ChipLogError (DeviceLayer, " SSS: DAC private key already converted to blob" );
177
+ chip::Platform::MemoryFree (data);
178
+ return CHIP_NO_ERROR;
179
+ }
180
+
133
181
ChipLogError (DeviceLayer, " SSS: extracted blob from DAC private key" );
134
182
135
183
hal_flash_status_t status = HAL_FlashRead (kFactoryDataStart , newSize - kSssBlobMetadataLength , data);
@@ -149,22 +197,31 @@ CHIP_ERROR FactoryDataProviderImpl::SSS_ConvertDacKey()
149
197
chip::Platform::MemoryFree (data);
150
198
ChipLogError (DeviceLayer, " SSS: sanitized RAM cache" );
151
199
200
+ ReturnErrorOnFailure (Validate ());
201
+
152
202
return CHIP_NO_ERROR;
153
203
}
154
204
155
- CHIP_ERROR FactoryDataProviderImpl::SSS_ExportBlob (uint8_t * data, size_t * dataLen, uint32_t & offset)
205
+ CHIP_ERROR FactoryDataProviderImpl::SSS_ExportBlob (uint8_t * data, size_t * dataLen, uint32_t & offset, bool & isNeeded )
156
206
{
157
207
CHIP_ERROR error = CHIP_NO_ERROR;
158
208
auto res = kStatus_SSS_Success ;
159
209
160
- uint8_t keyBuf[Crypto:: kP256_PrivateKey_Length ];
210
+ uint8_t keyBuf[kPrivateKeyBlobLength ];
161
211
MutableByteSpan dacPrivateKeySpan (keyBuf);
162
212
uint16_t keySize = 0 ;
213
+ isNeeded = true ;
163
214
164
215
error = SearchForId (FactoryDataId::kDacPrivateKeyId , dacPrivateKeySpan.data (), dacPrivateKeySpan.size (), keySize, &offset);
165
216
SuccessOrExit (error);
166
217
dacPrivateKeySpan.reduce_size (keySize);
167
218
219
+ if (keySize == kPrivateKeyBlobLength )
220
+ {
221
+ isNeeded = false ;
222
+ return CHIP_NO_ERROR;
223
+ }
224
+
168
225
res = SSS_KEY_STORE_SET_KEY (&mContext , dacPrivateKeySpan.data (), Crypto::kP256_PrivateKey_Length , keySize * 8 ,
169
226
kSSS_KeyPart_Private );
170
227
VerifyOrExit (res == kStatus_SSS_Success , error = CHIP_ERROR_INTERNAL);
@@ -197,7 +254,6 @@ CHIP_ERROR FactoryDataProviderImpl::ReplaceWithBlob(uint8_t * data, uint8_t * bl
197
254
198
255
return CHIP_NO_ERROR;
199
256
}
200
- #endif // CHIP_DEVICE_CONFIG_SECURE_DAC_PRIVATE_KEY
201
257
202
258
#if CHIP_DEVICE_CONFIG_ENABLE_SSS_API_TEST
203
259
@@ -298,6 +354,7 @@ void FactoryDataProviderImpl::SSS_RunApiTest()
298
354
SSS_KEY_OBJ_FREE (&mContext );
299
355
}
300
356
#endif // CHIP_DEVICE_CONFIG_ENABLE_SSS_API_TEST
357
+ #endif // CHIP_USE_PLAIN_DAC_KEY
301
358
302
359
} // namespace DeviceLayer
303
360
} // namespace chip
0 commit comments