Skip to content

Commit c44c4fa

Browse files
committed
1)Added random number generator using TrustM
1 parent 28da03c commit c44c4fa

File tree

4 files changed

+103
-0
lines changed

4 files changed

+103
-0
lines changed

src/platform/Infineon/crypto/trustm/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ static_library("infineon_crypto_lib") {
4343
"CHIPCryptoPALHsm_HKDF_trustm.cpp",
4444
"CHIPCryptoPALHsm_HMAC_trustm.cpp",
4545
"CHIPCryptoPALHsm_P256_trustm.cpp",
46+
"CHIPCryptoPALHsm_rng_trustm.cpp",
4647
"CHIPCryptoPALHsm_utils_trustm.cpp",
4748
"DeviceAttestationCredsExampleTrustM.cpp",
4849
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
*
3+
* Copyright (c) 2024 Project CHIP Authors
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* @file
20+
* HSM based implementation of CHIP crypto primitives
21+
* Based on configurations in CHIPCryptoPALHsm_config.h file,
22+
* chip crypto apis use either HSM or rollback to software implementation.
23+
*/
24+
25+
#include "CHIPCryptoPALHsm_utils_trustm.h"
26+
#include "optiga/optiga_util.h"
27+
#include "optiga_crypt.h"
28+
#include "optiga_lib_types.h"
29+
#include <lib/core/CHIPEncoding.h>
30+
31+
32+
namespace chip {
33+
namespace Crypto {
34+
35+
CHIP_ERROR DRBG_get_bytes(uint8_t * out_buffer, const size_t out_length)
36+
{
37+
CHIP_ERROR error = CHIP_ERROR_INTERNAL;
38+
optiga_lib_status_t return_status = OPTIGA_LIB_BUSY;
39+
40+
VerifyOrReturnError(out_buffer != nullptr, CHIP_ERROR_INVALID_ARGUMENT);
41+
VerifyOrReturnError(out_length > 0, CHIP_ERROR_INVALID_ARGUMENT);
42+
// Trust M init
43+
trustm_Open();
44+
45+
ChipLogDetail(Crypto, "Random Number: Using TrustM for Rondom Number Generate !");
46+
return_status = optiga_crypt_rng(out_buffer, out_length);
47+
48+
VerifyOrExit(return_status == OPTIGA_LIB_SUCCESS, error = CHIP_ERROR_INTERNAL);
49+
50+
error = CHIP_NO_ERROR;
51+
52+
exit:
53+
if (error != CHIP_NO_ERROR)
54+
{
55+
trustm_close();
56+
}
57+
return CHIP_NO_ERROR;
58+
}
59+
60+
} // namespace Crypto
61+
} // namespace chip

src/platform/Infineon/crypto/trustm/CHIPCryptoPALHsm_utils_trustm.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,47 @@ optiga_lib_status_t hmac_sha256(optiga_hmac_type_t type, const uint8_t * input_d
536536
}
537537
return return_status;
538538
}
539+
optiga_lib_status_t optiga_crypt_rng(uint8_t * random_data, uint16_t random_data_length)
540+
{
541+
optiga_lib_status_t return_status;
542+
do
543+
{
544+
// Create an instance of optiga_crypt_t
545+
p_local_crypt = optiga_crypt_create(0, optiga_crypt_callback, NULL);
546+
if (NULL == p_local_crypt)
547+
{
548+
optiga_lib_print_message("optiga_crypt_create failed !!!", OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
549+
break;
550+
}
551+
552+
return_status = OPTIGA_LIB_BUSY;
553+
return_status = optiga_crypt_random(p_local_crypt,
554+
OPTIGA_RNG_TYPE_DRNG,
555+
random_data,
556+
random_data_length);
557+
if (OPTIGA_LIB_SUCCESS != return_status)
558+
{
559+
// optiga_crypt_random api returns error !!!
560+
optiga_lib_print_message("optiga_crypt_random api returns error !!!", OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
561+
break;
562+
}
539563

564+
while (optiga_lib_status == OPTIGA_LIB_BUSY)
565+
;
566+
if (OPTIGA_LIB_SUCCESS != optiga_lib_status)
567+
{
568+
// optiga_crypt_random failed
569+
optiga_lib_print_message("optiga_crypt_random failed", OPTIGA_UTIL_SERVICE, OPTIGA_UTIL_SERVICE_COLOR);
570+
break;
571+
}
572+
} while (0);
573+
574+
if (p_local_crypt)
575+
{
576+
optiga_crypt_destroy(p_local_crypt);
577+
}
578+
return return_status;
579+
}
540580
optiga_lib_status_t trustm_ecc_keygen(uint16_t optiga_key_id, uint8_t key_type, optiga_ecc_curve_t curve_id, uint8_t * pubkey,
541581
uint16_t *pubkey_length)
542582
{

src/platform/Infineon/crypto/trustm/CHIPCryptoPALHsm_utils_trustm.h

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ void trustm_close(void);
7575
CHIP_ERROR trustmGetCertificate(uint16_t optiga_oid, uint8_t * buf, uint16_t * buflen);
7676
optiga_lib_status_t trustm_ecdh_derive_secret(optiga_key_id_t optiga_key_id, uint8_t * public_key, uint16_t public_key_length,
7777
uint8_t * shared_secret, uint8_t shared_secret_length);
78+
optiga_lib_status_t optiga_crypt_rng(uint8_t * random_data, uint16_t random_data_length);
7879
optiga_lib_status_t trustm_PBKDF2_HMAC(const unsigned char * salt, size_t slen, unsigned int iteration_count, uint32_t key_length,
7980
unsigned char * output);
8081
#ifdef __cplusplus

0 commit comments

Comments
 (0)