10#include <mbedtls/config.h>
11#include <mbedtls/platform.h>
12#include <mbedtls/entropy.h>
13#include <mbedtls/ctr_drbg.h>
14#include <mbedtls/pk.h>
15#include <mbedtls/rsa.h>
16#include <mbedtls/sha256.h>
23 Step_3_ReceiveKeyResponse,
31static int sign(
const uint8_t *seed,
size_t seed_len, uint8_t *key,
size_t key_len) {
33 mbedtls_pk_context pk;
34 mbedtls_entropy_context entropy;
35 mbedtls_ctr_drbg_context ctr_drbg;
36 mbedtls_entropy_init(&entropy);
37 mbedtls_ctr_drbg_init(&ctr_drbg);
38 const char *pers =
"rsa_sign";
39 mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy, (
const unsigned char *)pers,
44 const char *private_key_pem =
"private_key.pem";
46 if (mbedtls_pk_parse_keyfile(&pk, private_key_pem, NULL) != 0) {
47 mbedtls_printf(
"Failed to parse private key\n");
53 if (mbedtls_pk_get_type(&pk) != MBEDTLS_PK_RSA) {
54 mbedtls_printf(
"Loaded key is not an RSA key\n");
59 mbedtls_rsa_context *rsa = mbedtls_pk_rsa(pk);
62 size_t sig_len = mbedtls_rsa_get_len(rsa);
63 if (sig_len != key_len) {
64 fprintf(stderr,
"sig_len: %zu != %zu\n", sig_len, key_len);
70 if (mbedtls_rsa_rsassa_pkcs1_v15_sign(rsa, mbedtls_ctr_drbg_random, &ctr_drbg,
71 MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256, seed_len, seed,
73 mbedtls_printf(
"Failed to sign data\n");
81 mbedtls_rsa_free(rsa);
82 mbedtls_ctr_drbg_free(&ctr_drbg);
83 mbedtls_entropy_free(&entropy);
90 UDS_LOGI(__FILE__,
"%s (%d)", UDSEventToStr(evt), evt);
93 UDS_LOGE(__FILE__,
"Exiting on step %d with error: %s", c->step,
94 UDSErrToStr(*(UDSErr_t *)ev_data));
95 c->err = *(UDSErr_t *)ev_data;
99 case Step_0_RequestSeed: {
100 c->err = UDSSendSecurityAccess(client, 3, NULL, 0);
102 UDS_LOGE(__FILE__,
"UDSSendSecurityAccess failed with err: %s", UDSErrToStr(c->err));
105 c->step = Step_1_ReceiveSeed;
108 case Step_1_ReceiveSeed: {
111 c->err = UDSUnpackSecurityAccessResponse(client, &sar);
113 UDS_LOGE(__FILE__,
"UDSUnpackSecurityAccessResponse failed with err: %s",
114 UDSErrToStr(c->err));
126 bool all_zero =
true;
135 UDS_LOGI(__FILE__,
"seed is all zero, already unlocked");
143 c->step = Step_2_SendKey;
147 case Step_2_SendKey: {
148 uint8_t key[512] = {0};
149 if (sign(c->seed, c->seed_len, key,
sizeof(key))) {
150 UDS_LOGE(__FILE__,
"sign failed");
156 c->err = UDSSendSecurityAccess(client, 4, key,
sizeof(key));
158 UDS_LOGE(__FILE__,
"UDSSendSecurityAccess failed with err: %s", UDSErrToStr(c->err));
162 c->step = Step_3_ReceiveKeyResponse;
165 case Step_3_ReceiveKeyResponse: {
167 UDS_LOGI(__FILE__,
"Security access unlocked");
179int main(
int ac,
char **av) {
181#if defined(UDS_TP_ISOTP_SOCK)
183 if (UDSTpIsoTpSockInitClient(&tp,
"vcan0", 0x7E8, 0x7E0, 0x7DF)) {
184 UDS_LOGE(__FILE__,
"UDSTpIsoTpSockInitClient failed");
187#elif defined(UDS_TP_ISOTP_C_SOCKETCAN)
189 if (UDSTpISOTpCInit((
UDSTpISOTpC_t *)&tp,
"vcan0", 0x7E8, 0x7E0, 0x7DF, 0x7FF)) {
190 UDS_LOGE(__FILE__,
"UDSTpISOTpCInit failed");
194#error "no transport defined"
197 if (UDSClientInit(&client)) {
207 UDS_LOGI(__FILE__,
"polling");
208 while (ctx.step != Step_DONE) {
209 UDSClientPoll(&client);
ISO14229-1 (UDS) library.
@ UDS_EVT_ResponseReceived
Security access response structure.
uint16_t securitySeedLength
const uint8_t * securitySeed
int(* fn)(struct UDSClient *client, UDSEvent_t evt, void *ev_data)