Hazelnet 3.0.0
Reference implementation of the CAN Bus Security (CBS) protocol
hzl.h
Go to the documentation of this file.
1/*
2 * Copyright © 2020-2022, Matjaž Guštin <dev@matjaz.it>
3 * <https://matjaz.it>. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. Neither the name of nor the names of its contributors may be used to
14 * endorse or promote products derived from this software without specific
15 * prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS “AS IS”
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
34#ifndef HZL_H_
35#define HZL_H_
36
37#ifdef __cplusplus
38extern "C"
39{
40#endif
41
45#define HZL_VERSION "v3.0.0"
46
48#define HZL_CBS_PROTOCOL_VERSION_SUPPORTED "v1.3"
49
50// Dependencies (partial list: more includes in the if-elif-endif checking the OS below)
51#include <stdint.h> /* For uint8_t, uint16_t etc. */
52#include <stdbool.h> /* For bool, true, false */
53#include <stddef.h> /* For NULL, size_t */
54#include <string.h> /* For memcpy(), if available also memset_s() */
55
79#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) \
80 || defined(_WIN64) || defined(__NT__)
81
82#define HZL_OS_AVAILABLE 1
83#define HZL_OS_AVAILABLE_WIN 1
84#define HZL_OS_AVAILABLE_NIX 0
85
86#include <Windows.h> /* To avoid compilation errors for other Windows headers. */
87#include <sysinfoapi.h> /* For GetSystemTimeAsFileTime(), FILETIME, ULARGE_INTEGER */
88#include <bcrypt.h> /* For BCryptGenRandom() - requires explicit linking to `bcrypt` lib. */
89#include <stdio.h> /* For config file IO */
90#include <stdlib.h> /* For calloc(), free() */
91
92#if defined(_MSC_VER) && _MSC_VER <= 1916
93// Fix for compilation with Visual Studio 2017 not supporting C11 yet.
94#define _Static_assert static_assert
95#endif
96
97#elif defined(__linux__) \
98 || defined(__APPLE__) || defined(__MACH__) \
99 || defined(__unix__) || defined(__unix) || defined(unix)
100
101#define HZL_OS_AVAILABLE 1
102#define HZL_OS_AVAILABLE_WIN 0
103#define HZL_OS_AVAILABLE_NIX 1
104
105#include <sys/time.h> /* For gettimeofday() */
106#include <stdio.h> /* For config file IO and TRNG with /dev/urandom */
107#include <stdlib.h> /* For calloc(), free() */
108
109#else
110
111#define HZL_OS_AVAILABLE 0
112#define HZL_OS_AVAILABLE_WIN 0
113#define HZL_OS_AVAILABLE_NIX 0
114
115#endif
116
121#if HZL_OS_AVAILABLE_WIN
122#define HZL_API __declspec(dllexport)
123#else
124#define HZL_API
125#endif
126
128#define HZL_SET_BY_USER
129
131#define HZL_SERVER_SID 0U
132
134#define HZL_BROADCAST_GID 0U
135
137#define HZL_LTK_LEN 16U
138
140#define HZL_STK_LEN 16U
141
144#define HZL_IS_SECURITY_WARNING(err) ((err) >= 1 && (err) <= 15)
145
147#define HZL_MAX_CAN_FD_DATA_LEN 64U
148
158#define HZL_MAX_TRNG_TRIES_FOR_NONZERO_VALUE 20U
159
165#define HZL_LARGEST_MAX_COUNTER_NONCE_DELAY (1U << 22U)
166
168typedef enum hzl_Err
169{
170 // Success
171 HZL_OK = 0U,
172
173 // CBS standard security warnings
209
210 // Generic errors
214
215 // ClientInit, ServerInit
346
347 // TX and RX function functions
365
366 // TX functions
384
385 // RX functions
412
413 // Failed IO operation
430
431 // OS interaction functions
447
449typedef enum hzl_HeaderType
450{
458// Values [7, 32] are RFU.
460
462typedef uint8_t hzl_Gid_t;
463
465typedef uint8_t hzl_Sid_t;
466
468typedef uint8_t hzl_Pty_t;
469
471typedef uint32_t hzl_CanId_t;
472
474typedef uint64_t hzl_ReqNonce_t;
475
477typedef uint64_t hzl_ResNonce_t;
478
492typedef uint32_t hzl_Timestamp_t;
493
495typedef uint32_t hzl_CtrNonce_t;
496
498typedef struct hzl_Header
499{
504
505// TODO expose CBS header to allow the user to know which CAN ID to assign
506// TODO expose configuration/functions for packing the CBS header to allow the user to
507// choose where to pack the header (CAN ID, CAN payload or mixed)
511typedef struct hzl_CbsPduMsg
512{
513 size_t dataLen;
516
518typedef struct hzl_RxMsg
519{
520 size_t dataLen;
528
539typedef hzl_Err_t (* hzl_TrngFunc)(uint8_t* bytes, size_t amount);
540
562
567typedef struct
568{
576
585} hzl_Io_t;
586
587#ifdef __cplusplus
588}
589#endif
590
591#endif /* HZL_H_ */
uint32_t hzl_Timestamp_t
Opaque timestamp (timer, rolling counter) with milliseconds accuracy.
Definition: hzl.h:492
hzl_Err_t(* hzl_TimestampFunc)(hzl_Timestamp_t *timestamp)
Current-time timestamp generating function with millisecond accuracy.
Definition: hzl.h:561
uint8_t hzl_Sid_t
Source Identifier data type.
Definition: hzl.h:465
uint32_t hzl_CanId_t
CAN message identifier data type, able to hold both 11- or 29-bits values.
Definition: hzl.h:471
struct hzl_RxMsg hzl_RxSduMsg_t
Unpacked received SDU (Service Data Unit message) after validation (and optional decryption).
uint8_t hzl_Gid_t
Group Identifier data type.
Definition: hzl.h:462
uint64_t hzl_ResNonce_t
Response Nonce data type.
Definition: hzl.h:477
struct hzl_CbsPduMsg hzl_CbsPduMsg_t
Packed CBS PDU (Protocol Data Unit message) ready to be transmitted by the library user.
uint32_t hzl_CtrNonce_t
Counter Nonce data type.
Definition: hzl.h:495
uint8_t hzl_Pty_t
Payload Type data type.
Definition: hzl.h:468
struct hzl_Header hzl_Header_t
Unpacked CBS Header.
hzl_Err_t(* hzl_TrngFunc)(uint8_t *bytes, size_t amount)
True-random number generator function.
Definition: hzl.h:539
enum hzl_Err hzl_Err_t
Hazelnet error code, returned by all API functions.
#define HZL_SET_BY_USER
Identifier of the struct fields of the public API the user must set manually.
Definition: hzl.h:128
hzl_Err
Hazelnet error code, returned by all API functions.
Definition: hzl.h:169
@ HZL_ERR_SECWARN_DENIAL_OF_SERVICE
The Party is receiving too many suspect messages.
Definition: hzl.h:194
@ HZL_ERR_MALLOC_FAILED
Heap-memory allocation failure: out of memory.
Definition: hzl.h:445
@ HZL_ERR_NULL_TRNG_FUNC
The function pointer to the true-random number generating function is NULL.
Definition: hzl.h:345
@ HZL_ERR_SERVER_SID_ASSIGNED_TO_CLIENT
The Client configuration contains a Source Identifier (SID) equal to the one used by the Server HZL_S...
Definition: hzl.h:241
@ HZL_ERR_GAP_IN_SIDS
The array of Client configurations must have all SIDs from 1 to hzl_ServerConfig_t....
Definition: hzl.h:249
@ HZL_ERR_SID_TOO_LARGE_FOR_CONFIGURED_HEADER_TYPE
The Party configuration contains a Source Identifier (SID) that has a value that would not fit into t...
Definition: hzl.h:256
@ HZL_ERR_SECWARN_MESSAGE_FROM_MYSELF
Received message contained the receiver's Source Identfi␜er as the transmitter's identity.
Definition: hzl.h:180
@ HZL_ERR_TOO_SHORT_PDU_TO_CONTAIN_RES
The received Response (RES) message is too short to be a valid RES message.
Definition: hzl.h:401
@ HZL_ERR_TOO_SHORT_PDU_TO_CONTAIN_REQ
The received Request (REQ) message is too short to be a valid REQ message.
Definition: hzl.h:398
@ HZL_ERR_CLIENTS_BITMAP_ZERO_CLIENTS
The Server-side array of Group configuration has at least one Group where the bitmap of Clients in th...
Definition: hzl.h:326
@ HZL_ERR_NULL_SDU
The pointer to the Service Data Unit (unpacked user data) to transmit or the just-received one is NUL...
Definition: hzl.h:353
@ HZL_ERR_NULL_CONFIG_SERVER
The context contains a NULL pointer to the Server configuration struct.
Definition: hzl.h:223
@ HZL_ERR_SECWARN_RFU_3
Reserved warning code for future use.
Definition: hzl.h:206
@ HZL_ERR_TOO_LONG_CIPHERTEXT
The received Secured Application Data contains a too large value in the plaintext length field,...
Definition: hzl.h:407
@ HZL_ERR_SECWARN_RECEIVED_ZERO_KEY
Received Response message, once decrypted, contained an all-zeros STKG that cannot be used.
Definition: hzl.h:203
@ HZL_ERR_SECWARN_RECEIVED_OVERFLOWN_NONCE
Received message contained a Counter Nonce that exceeded its maximum allowed value.
Definition: hzl.h:200
@ HZL_ERR_GIDS_ARE_NOT_PRESORTED_STRICTLY_ASCENDING
The array of Group configurations must be sorted from smallest to largest GID without repeated GIDs.
Definition: hzl.h:290
@ HZL_ERR_PROGRAMMING
Default error value that should never appear, as it should be replaced with another one.
Definition: hzl.h:213
@ HZL_ERR_SECWARN_SERVER_ONLY_MESSAGE
Received message is of a type which only the Session Server can transmit, but the transmitter's ident...
Definition: hzl.h:186
@ HZL_ERR_INVALID_FILE_MAGIC_NUMBER
The magic number "HZL\0" (4 ASCII-encoded bytes) was not found at the beginning of the file as expect...
Definition: hzl.h:443
@ HZL_ERR_SECWARN_NOT_EXPECTING_A_RESPONSE
The Client received a Response addressed to it while not expecting any.
Definition: hzl.h:183
@ HZL_ERR_ZERO_CLIENTS
The Server configuration contains zero Clients.
Definition: hzl.h:266
@ HZL_ERR_UNEXPECTED_EOF
The configuration file was shorter than expected, some parts of the configuration are left out.
Definition: hzl.h:439
@ HZL_ERR_SECWARN_RFU_2
Reserved warning code for future use.
Definition: hzl.h:205
@ HZL_ERR_NULL_CTX
The function requires a pointer to the Hazelnet context, but NULL was provided.
Definition: hzl.h:217
@ HZL_ERR_TOO_LARGE_CTRNONCE_UPPER_LIMIT
The Server-side array of Group configuration has at least one Group where the Counter Nonce upper lim...
Definition: hzl.h:317
@ HZL_ERR_NULL_CONFIG_GROUPS
The context contains a NULL pointer to the Client configuration struct (on a Client) or array of (on ...
Definition: hzl.h:285
@ HZL_ERR_TOO_MANY_CLIENTS_FOR_CONFIGURED_HEADER_TYPE
The Server configurations contains an amount of Groups that implies there is at least one Client with...
Definition: hzl.h:277
@ HZL_ERR_CANNOT_GENERATE_NON_ZERO_RANDOM
The true-random number generation failed to provide a non-zero arrays of bytes for too many times in ...
Definition: hzl.h:429
@ HZL_ERR_TOO_LONG_SDU
The user-provided data to be transmitted is too long to fit into the specified message type.
Definition: hzl.h:369
@ HZL_ERR_NULL_CONFIG_CLIENTS
The context contains a NULL pointer to the array of Client configuration structs.
Definition: hzl.h:280
@ HZL_ERR_ZERO_GROUPS
The Party configuration contains zero Groups.
Definition: hzl.h:227
@ HZL_ERR_INVALID_DELAY_BETWEEN_REN_NOTIFICATIONS
The Server-side array of Group configuration has at least one Group where the delay between successiv...
Definition: hzl.h:322
@ HZL_ERR_SECWARN_INVALID_TAG
Received message is not intact or not authentic or does not use the proper values in the hashing/auth...
Definition: hzl.h:177
@ HZL_ERR_MISSING_GID_0
The array of Group configurations must have the GID == 0 (broadcast Group) HZL_BROADCAST_GID.
Definition: hzl.h:301
@ HZL_ERR_LTK_IS_ALL_ZEROS
The configured Client's Long Term Key (or at least 1 of them for the Server) is uninitialised,...
Definition: hzl.h:232
@ HZL_ERR_CLIENTS_BITMAP_UNKNOWN_SID
The Server-side array of Group configuration has at least one Group where the bitmap of Clients in th...
Definition: hzl.h:330
@ HZL_ERR_CANNOT_GET_CURRENT_TIME
The timestamping function failed to provide the current time.
Definition: hzl.h:417
@ HZL_ERR_GID_TOO_LARGE_FOR_CONFIGURED_HEADER_TYPE
The array of Group configurations has at least one Group with a GID value that would not fit into the...
Definition: hzl.h:313
@ HZL_ERR_NULL_CURRENT_TIME_FUNC
The function pointer to the timestamping function is NULL.
Definition: hzl.h:342
@ HZL_OK
Successful completion of the operation.
Definition: hzl.h:171
@ HZL_ERR_NULL_CONFIG_CLIENT
The context contains a NULL pointer to the Client configuration struct.
Definition: hzl.h:220
@ HZL_ERR_NULL_PDU
The pointer to the Protocol Data Unit (packed CBS message) to transmit or the just-received one is NU...
Definition: hzl.h:350
@ HZL_ERR_TOO_SHORT_PDU_TO_CONTAIN_REN
The received Session Renewal Notification (REN) message is too short to be a valid REN message.
Definition: hzl.h:404
@ HZL_ERR_GAP_IN_GIDS
The array of Group configurations must contain all GIDs from 0 to hzl_ServerConfig_t....
Definition: hzl.h:295
@ HZL_ERR_SECWARN_NOT_IN_GROUP
The Client the Request originated from does not belong into the requested Group.
Definition: hzl.h:197
@ HZL_ERR_SECWARN_RFU_1
Reserved warning code for future use.
Definition: hzl.h:204
@ HZL_ERR_SECWARN_RESPONSE_TIMEOUT
The Client did not receive a Response to its Request within the preconfi␜gured timeout.
Definition: hzl.h:189
@ HZL_ERR_SECWARN_RFU_5
Reserved warning code for future use.
Definition: hzl.h:208
@ HZL_ERR_NO_POTENTIAL_RECEIVER
The building of the Secured Application Data or a Session Renewal Notification could not be performed...
Definition: hzl.h:379
@ HZL_ERR_SECWARN_RFU_4
Reserved warning code for future use.
Definition: hzl.h:207
@ HZL_ERR_MSG_IGNORED
The received message is ignored as it's not addressed to this Party or is redundant.
Definition: hzl.h:409
@ HZL_ERR_TOO_MANY_CLIENTS
The Server configuration contains more Clients that it can hold in the bitmap of Clients in each Grou...
Definition: hzl.h:271
@ HZL_ERR_SECWARN_RECEIVED_ZERO_REQNONCE
The received Request message contained an all-zeros Request Nonce.
Definition: hzl.h:411
@ HZL_ERR_INVALID_HEADER_TYPE
The Party configuration contains an unknown or unsupported CBS Header Type.
Definition: hzl.h:236
@ HZL_ERR_TOO_SHORT_PDU_TO_CONTAIN_HEADER
The received message is too short to even contain the CBS header.
Definition: hzl.h:390
@ HZL_ERR_NULL_FILENAME
The configuration file name string is NULL.
Definition: hzl.h:433
@ HZL_ERR_CLIENTS_BITMAP_INVALID_BROADCAST_GROUP
The Server-side array of Group configuration has the broadcast Group with GID == HZL_BROADCAST_GID th...
Definition: hzl.h:335
@ HZL_ERR_UNKNOWN_SOURCE
The received message's SID field indicates a Source not available in the Server's configuration and t...
Definition: hzl.h:360
@ HZL_ERR_SECWARN_OLD_MESSAGE
Received message contained a too-old counter nonce.
Definition: hzl.h:191
@ HZL_ERR_SIDS_ARE_NOT_PRESORTED_STRICTLY_ASCENDING
The array of Client configurations must be sorted from smallest to largest SID without repeated SIDs.
Definition: hzl.h:245
@ HZL_ERR_UNKNOWN_GROUP
The user-specified Group (GID) for transmission or the received message's GID field indicate a Group ...
Definition: hzl.h:357
@ HZL_ERR_TOO_SHORT_PDU_TO_CONTAIN_SADFD
The received Secured Application Data over CAN FD (SADFD) message is too short to be a valid SADFD me...
Definition: hzl.h:395
@ HZL_ERR_RENEWAL_ONGOING
The building of the message could not be performed right now, as the Session renewal phase is ongoing...
Definition: hzl.h:383
@ HZL_ERR_INVALID_MAX_CTRNONCE_DELAY
The array of Group configuration has at least one Group where the max Counter Nonce Delay field is ou...
Definition: hzl.h:306
@ HZL_ERR_CANNOT_GENERATE_RANDOM
The true-random number generation function failed to provide a random number.
Definition: hzl.h:421
@ HZL_ERR_HANDSHAKE_ONGOING
The building of the message could not be performed right now, as the handshake process (exchange of R...
Definition: hzl.h:374
@ HZL_ERR_CANNOT_OPEN_CONFIG_FILE
The configuration file could not be opened.
Definition: hzl.h:436
@ HZL_ERR_SESSION_NOT_ESTABLISHED
The Group the message is addressed to has no valid Session information in the local state.
Definition: hzl.h:364
@ HZL_ERR_NULL_STATES_GROUPS
The context contains a NULL pointer to the Group states array.
Definition: hzl.h:339
@ HZL_ERR_INVALID_PAYLOAD_TYPE
The received message contains an unknown PTY field.
Definition: hzl.h:387
@ HZL_ERR_TOO_MANY_GROUPS_FOR_CONFIGURED_HEADER_TYPE
The party configurations contains an amount of Groups that implies there is at least one Group with a...
Definition: hzl.h:263
hzl_HeaderType
Standard CBS header types.
Definition: hzl.h:450
@ HZL_HEADER_1
Definition: hzl.h:452
@ HZL_HEADER_0
Definition: hzl.h:451
@ HZL_HEADER_5
Definition: hzl.h:456
@ HZL_HEADER_2
Definition: hzl.h:453
@ HZL_HEADER_4
Definition: hzl.h:455
@ HZL_HEADER_3
Definition: hzl.h:454
@ HZL_HEADER_6
Definition: hzl.h:457
#define HZL_MAX_CAN_FD_DATA_LEN
Maximum length of the CAN FD frame's payload in bytes.
Definition: hzl.h:147
enum hzl_HeaderType hzl_HeaderType_t
Standard CBS header types.
uint64_t hzl_ReqNonce_t
Request Nonce data type.
Definition: hzl.h:474
Packed CBS PDU (Protocol Data Unit message) ready to be transmitted by the library user.
Definition: hzl.h:512
size_t dataLen
Length in bytes of the CBS-Payload.
Definition: hzl.h:513
uint8_t data[HZL_MAX_CAN_FD_DATA_LEN]
CBS-Payload.
Definition: hzl.h:514
Unpacked CBS Header.
Definition: hzl.h:499
hzl_Gid_t gid
Group IDentifier: set of parties enabled for reception.
Definition: hzl.h:500
hzl_Pty_t pty
Payload TYpe: content of the CBS message.
Definition: hzl.h:502
hzl_Sid_t sid
Source IDentifier: transmitting party.
Definition: hzl.h:501
Functions used by Hazelnet to interact with the rest of the system in order to obtain random numbers,...
Definition: hzl.h:568
HZL_SET_BY_USER hzl_TimestampFunc currentTime
Current-time timestamp generating function.
Definition: hzl.h:584
HZL_SET_BY_USER hzl_TrngFunc trng
True random number generator function.
Definition: hzl.h:575
Unpacked received SDU (Service Data Unit message) after validation (and optional decryption).
Definition: hzl.h:519
hzl_Gid_t gid
Group IDentifier the message used (expected receivers).
Definition: hzl.h:522
hzl_Sid_t sid
Source IDentifier the message used (claimed sender).
Definition: hzl.h:523
size_t dataLen
Length in bytes of the unpacked/decrypted user data.
Definition: hzl.h:520
hzl_CanId_t canId
CAN ID the underlying frame used.
Definition: hzl.h:521
uint8_t data[HZL_MAX_CAN_FD_DATA_LEN]
User data in plaintext of dataLen bytes.
Definition: hzl.h:526
bool isForUser
True if the message contains useful data for the user, false if internal.
Definition: hzl.h:525
bool wasSecured
True if it was encrypted and authenticated during transmission.
Definition: hzl.h:524