Hazelnet 3.0.0
Reference implementation of the CAN Bus Security (CBS) protocol
hzl_Server.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
44#ifndef HZL_SERVER_H_
45#define HZL_SERVER_H_
46
47#ifdef __cplusplus
48extern "C"
49{
50#endif
51
52#include "hzl.h"
53
64#define HZL_SERVER_MAX_AMOUNT_OF_CLIENTS 32U
65
70typedef uint32_t hzl_ServerBitMap_t;
71
77#define HZL_SERVER_LARGEST_MAX_COUNTER_NONCE_DELAY (1U << 22U)
78
84#define HZL_SERVER_MAX_COUNTER_NONCE_UPPER_LIMIT 0xFFFF80U
85
92typedef struct hzl_ServerConfig
93{
116
120 "The size of the Server Config struct must be exactly 3 B");
121
129{
152
156 "The size of the Server-side Client Config struct must be exactly 17 B");
157
165{
238 uint8_t unusedPadding[1];
240
244 "The size of the Server Group Config struct must be exactly 24 B");
245
254{
295
299 "The size of the Server Group State struct must be exactly 52 B");
300
306typedef struct hzl_ServerCtx
307{
365
416
439
472 const hzl_ServerCtx_t* ctx,
473 const uint8_t* userData,
474 size_t userDataLen,
475 hzl_Gid_t groupId);
476
506 hzl_ServerCtx_t* ctx,
507 const uint8_t* userData,
508 size_t userDataLen,
509 hzl_Gid_t groupId);
510
567 hzl_RxSduMsg_t* receivedUserData,
568 hzl_ServerCtx_t* ctx,
569 const uint8_t* receivedPdu,
570 size_t receivedPduLen,
571 hzl_CanId_t receivedCanId);
572
593 hzl_ServerCtx_t* ctx,
594 hzl_Gid_t groupId);
595
596
597#ifdef __cplusplus
598}
599#endif
600
601#endif /* HZL_SERVER_H_ */
General public definitions used used by both the Hazelnet Server and Client.
uint32_t hzl_Timestamp_t
Opaque timestamp (timer, rolling counter) with milliseconds accuracy.
Definition: hzl.h:492
#define HZL_LTK_LEN
Length of the Long Term Key in bytes.
Definition: hzl.h:137
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
uint8_t hzl_Gid_t
Group Identifier data type.
Definition: hzl.h:462
#define HZL_API
Identifier of the public library API functions.
Definition: hzl.h:122
uint32_t hzl_CtrNonce_t
Counter Nonce data type.
Definition: hzl.h:495
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
struct hzl_ServerGroupConfig hzl_ServerGroupConfig_t
Hazelnet Server constant Group configuration.
struct hzl_ServerGroupState hzl_ServerGroupState_t
Hazelnet Server variable State.
struct hzl_ServerCtx hzl_ServerCtx_t
Configuration and status of the HazelNet Server library.
HZL_API hzl_Err_t hzl_ServerProcessReceived(hzl_CbsPduMsg_t *reactionPdu, hzl_RxSduMsg_t *receivedUserData, hzl_ServerCtx_t *ctx, const uint8_t *receivedPdu, size_t receivedPduLen, hzl_CanId_t receivedCanId)
Validates, unpacks and decrypts (if necessary) any received message, preparing an automatic response ...
struct hzl_ServerClientsConfig hzl_ServerClientConfig_t
Hazelnet Server constant per-Client configuration.
uint32_t hzl_ServerBitMap_t
Integer data type used for the bitmap of Clients that must support HZL_SERVER_MAX_AMOUNT_OF_CLIENTS b...
Definition: hzl_Server.h:70
HZL_API hzl_Err_t hzl_ServerDeInit(hzl_ServerCtx_t *ctx)
Deinitialisation of the Server, securely clearing the state.
HZL_API hzl_Err_t hzl_ServerBuildSecuredFd(hzl_CbsPduMsg_t *securedPdu, hzl_ServerCtx_t *ctx, const uint8_t *userData, size_t userDataLen, hzl_Gid_t groupId)
Builds a secured message, encrypted, authenticated and timely, only for the given group to be able to...
HZL_API hzl_Err_t hzl_ServerInit(hzl_ServerCtx_t *ctx)
Initialisation of the Server.
HZL_API hzl_Err_t hzl_ServerBuildUnsecured(hzl_CbsPduMsg_t *unsecuredPdu, const hzl_ServerCtx_t *ctx, const uint8_t *userData, size_t userDataLen, hzl_Gid_t groupId)
Builds an unsecured message in plaintext.
_Static_assert(sizeof(hzl_ServerConfig_t)==3, "The size of the Server Config struct must be exactly 3 B")
Double-checking the size of the hzl_ServerConfig_t struct to avoid unexpected paddings.
struct hzl_ServerConfig hzl_ServerConfig_t
Hazelnet Server constant configuration.
HZL_API hzl_Err_t hzl_ServerForceSessionRenewal(hzl_CbsPduMsg_t *renewalPdu, hzl_ServerCtx_t *ctx, hzl_Gid_t groupId)
Forcibly start a Session Renewal Phase, unless one is already ongoing or no Clients are currently ena...
Packed CBS PDU (Protocol Data Unit message) ready to be transmitted by the library user.
Definition: hzl.h:512
Functions used by Hazelnet to interact with the rest of the system in order to obtain random numbers,...
Definition: hzl.h:568
Unpacked received SDU (Service Data Unit message) after validation (and optional decryption).
Definition: hzl.h:519
Hazelnet Server constant per-Client configuration.
Definition: hzl_Server.h:129
HZL_SET_BY_USER hzl_Sid_t sid
Source Identifier of the Client.
Definition: hzl_Server.h:143
HZL_SET_BY_USER uint8_t ltk[HZL_LTK_LEN]
Symmetric Long Term Key of the Client X (LTK_{XS}), uniquely identifying the Client against the Serve...
Definition: hzl_Server.h:150
Hazelnet Server constant configuration.
Definition: hzl_Server.h:93
HZL_SET_BY_USER uint8_t amountOfGroups
Amount of hzl_ServerGroupConfig_t instances.
Definition: hzl_Server.h:99
HZL_SET_BY_USER uint8_t amountOfClients
Amount of Clients this Server knows, i.e.
Definition: hzl_Server.h:106
HZL_SET_BY_USER uint8_t headerType
Header Type (h) used by the network of CBS-enabled nodes.
Definition: hzl_Server.h:114
Configuration and status of the HazelNet Server library.
Definition: hzl_Server.h:307
HZL_SET_BY_USER hzl_Io_t io
Set of function pointers binding the API to the rest of the system.
Definition: hzl_Server.h:363
HZL_SET_BY_USER const hzl_ServerClientConfig_t * clientConfigs
Pointer to an array of structs, each with the constant configuration of one Client.
Definition: hzl_Server.h:329
HZL_SET_BY_USER const hzl_ServerConfig_t * serverConfig
Pointer to one struct with the constant Server configuration.
Definition: hzl_Server.h:313
HZL_SET_BY_USER hzl_ServerGroupState_t * groupStates
Pointer to an array of structs, each with the variable state of one Group.
Definition: hzl_Server.h:357
HZL_SET_BY_USER const hzl_ServerGroupConfig_t * groupConfigs
Pointer to an array of structs, each with the constant configuration of one Group.
Definition: hzl_Server.h:345
Hazelnet Server constant Group configuration.
Definition: hzl_Server.h:165
HZL_SET_BY_USER hzl_CtrNonce_t ctrNonceUpperLimit
Counter Nonce upper limit (N^{exp}_G), used to know when the current Session expires due to the amoun...
Definition: hzl_Server.h:179
HZL_SET_BY_USER hzl_Gid_t gid
Group Identifier of this Group (id_G).
Definition: hzl_Server.h:236
HZL_SET_BY_USER hzl_ServerBitMap_t clientSidsInGroupBitmap
Bitmap of the Client SIDs included in this Group.
Definition: hzl_Server.h:215
HZL_SET_BY_USER uint32_t delayBetweenRenNotificationsMillis
Delay between consecutive Session renewal notification (REN) messages (t^{ntf}_G) in milliseconds.
Definition: hzl_Server.h:197
HZL_SET_BY_USER uint32_t maxCtrnonceDelayMsgs
Maximum Counter Nonce Delay (D^{max}_G) in milliseconds, used to filter out recent messages from old ...
Definition: hzl_Server.h:172
HZL_SET_BY_USER uint16_t maxSilenceIntervalMillis
Maximum Silence Interval (S^{max}_G) in milliseconds, used to filter out recent messages from old one...
Definition: hzl_Server.h:222
HZL_SET_BY_USER uint32_t sessionDurationMillis
Server-Side Session time Duration (s^{exp}_G) in milliseconds.
Definition: hzl_Server.h:188
uint8_t unusedPadding[1]
Padding to the next struct.
Definition: hzl_Server.h:238
Hazelnet Server variable State.
Definition: hzl_Server.h:254
hzl_Timestamp_t previousRxLastMessageInstant
Timestamp of when the last valid received secured message belonging to the previously active Session ...
Definition: hzl_Server.h:275
hzl_CtrNonce_t previousCtrNonce
Counter Nonce of the the previously active Session (N^{ctr,old}_G), currently about to expire.
Definition: hzl_Server.h:284
uint8_t previousStk[HZL_LTK_LEN]
Short Term Key of the the previously active Session (STK^{old}_G), currently about to expire.
Definition: hzl_Server.h:293
hzl_Timestamp_t currentRxLastMessageInstant
Timestamp of when the last valid received secured message belonging to the currently active Session w...
Definition: hzl_Server.h:268
uint8_t currentStk[HZL_LTK_LEN]
Short Term Key of the currently active Session (STK_G).
Definition: hzl_Server.h:288
hzl_CtrNonce_t currentCtrNonce
Counter Nonce of the the currently active Session (N^{ctr}_G).
Definition: hzl_Server.h:279
hzl_Timestamp_t sessionStartInstant
Timestamp of when the Session was started.
Definition: hzl_Server.h:260