-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmacsec_common.h
More file actions
317 lines (250 loc) · 11.7 KB
/
Copy pathmacsec_common.h
File metadata and controls
317 lines (250 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
* macsec_common.h
*
* Lightweight MACsec stack
* Common types, macros and utility functions.
*
* This file contains common definitions shared by the MACsec modules,
* including return codes, debug helpers, parameter checks, byte-order
* conversion helpers and secure memory handling.
*
* Copyright (c) 2026 Michal Sarnovsky
*
* SPDX-License-Identifier: MIT
*
* This file is part of the lightweight MACsec stack.
* See LICENSE file in the project root for full license text.
*/
#ifndef MACSEC_COMMON_H
#define MACSEC_COMMON_H
#include "macsec_config.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#ifdef __cplusplus
extern "C"
{
#endif
/* =========================================================================
* Common types
* ========================================================================= */
typedef uint8_t macsec_bool_t;
#define MACSEC_FALSE ((macsec_bool_t) 0u)
#define MACSEC_TRUE ((macsec_bool_t) 1u)
/* =========================================================================
* Return codes
* ========================================================================= */
#define MACSEC_ERR_OK 0
#define MACSEC_ERR_PARAM -1
#define MACSEC_ERR_BUFFER -2
#define MACSEC_ERR_CRYPTO -3
#define MACSEC_ERR_STATE -4
#define MACSEC_ERR_REPLAY -5
#define MACSEC_ERR_UNSUPPORTED -6
#define MACSEC_ERR_AUTH -7
#define MACSEC_ERR_TIMEOUT -8
#define MACSEC_ERR_NOT_READY -9
#define MACSEC_ERR_BUSY -10
/* =========================================================================
* Platform functions
* ========================================================================= */
/*
* Enter a non-returning platform-specific panic state.
*
* The integrating platform must provide this function.
*/
void macsec_sysPanic(void);
/*
* Debug output is provided by the platform only when debug output is enabled.
*/
#if (MACSEC_DEBUG_LEVEL > MACSEC_DEBUG_LEVEL_NONE)
void macsec_printf(const char *format, ...);
void macsec_printHex(const char *label, const uint8_t *buf, int len);
#else
#define macsec_printf(...) ((void) 0)
#define macsec_printHex(label, buf, len) ((void) 0)
#endif
/* =========================================================================
* General debug macros
* ========================================================================= */
/*
* MACSEC_PRINT is intended primarily for optional test and diagnostic output.
* It is enabled whenever any debug output is enabled.
*/
#if (MACSEC_DEBUG_LEVEL > MACSEC_DEBUG_LEVEL_NONE)
#define MACSEC_PRINT(MSG) \
do \
{ \
macsec_printf MSG; \
} while (0)
#define MACSEC_PRINT_HEX(MSG) \
do \
{ \
macsec_printHex MSG; \
} while (0)
#else
#define MACSEC_PRINT(MSG) ((void) 0)
#define MACSEC_PRINT_HEX(MSG) ((void) 0)
#endif
/* =========================================================================
* Error-level debug macros
* ========================================================================= */
#if (MACSEC_DEBUG_LEVEL >= MACSEC_DEBUG_LEVEL_ERROR)
#define MACSEC_ERROR(MSG) \
do \
{ \
macsec_printf MSG; \
} while (0)
#define MACSEC_ERROR_HEX(MSG) \
do \
{ \
macsec_printHex MSG; \
} while (0)
#else
#define MACSEC_ERROR(MSG) ((void) 0)
#define MACSEC_ERROR_HEX(MSG) ((void) 0)
#endif
/* =========================================================================
* Medium-level debug macros
* ========================================================================= */
#if (MACSEC_DEBUG_LEVEL >= MACSEC_DEBUG_LEVEL_MEDIUM)
#define MACSEC_MEDIUM(MSG) \
do \
{ \
macsec_printf MSG; \
} while (0)
#define MACSEC_MEDIUM_HEX(MSG) \
do \
{ \
macsec_printHex MSG; \
} while (0)
#else
#define MACSEC_MEDIUM(MSG) ((void) 0)
#define MACSEC_MEDIUM_HEX(MSG) ((void) 0)
#endif
/* =========================================================================
* Information-level debug macros
* ========================================================================= */
#if (MACSEC_DEBUG_LEVEL >= MACSEC_DEBUG_LEVEL_INFO)
#define MACSEC_INFO(MSG) \
do \
{ \
macsec_printf MSG; \
} while (0)
#define MACSEC_INFO_HEX(MSG) \
do \
{ \
macsec_printHex MSG; \
} while (0)
#else
#define MACSEC_INFO(MSG) ((void) 0)
#define MACSEC_INFO_HEX(MSG) ((void) 0)
#endif
/* =========================================================================
* Assertions and parameter checks
* ========================================================================= */
/*
* Assert an internal invariant.
*
* A failed assertion always enters macsec_sysPanic(). When debug output is
* disabled, the diagnostic message is removed but the assertion remains
* active.
*/
#define macsec_assert(X) \
do \
{ \
if (!(X)) \
{ \
MACSEC_ERROR(("MACsec failed assertion (%s:%d): '%s'\n", __FILE__, __LINE__, #X)); \
macsec_sysPanic(); \
} \
} while (0)
/*
* Check a condition in a function returning an error code.
*/
#define macsec_check(X, RET) \
do \
{ \
if (!(X)) \
{ \
MACSEC_ERROR(("MACsec failure (%d) (%s:%d): '%s'\n", (RET), __FILE__, __LINE__, #X)); \
return (RET); \
} \
} while (0)
/*
* Check a condition in a function returning void.
*/
#define macsec_check_void(X) \
do \
{ \
if (!(X)) \
{ \
MACSEC_ERROR(("MACsec failure (%s:%d): '%s'\n", __FILE__, __LINE__, #X)); \
return; \
} \
} while (0)
/* =========================================================================
* Byte-order helpers
* ========================================================================= */
uint16_t macsec_rd_be16(const uint8_t *p);
uint32_t macsec_rd_be32(const uint8_t *p);
uint64_t macsec_rd_be64(const uint8_t *p);
void macsec_wr_be16(uint8_t *p, uint16_t value);
void macsec_wr_be32(uint8_t *p, uint32_t value);
void macsec_wr_be64(uint8_t *p, uint64_t value);
/* =========================================================================
* Buffer helpers
* ========================================================================= */
/*
* Securely erase a memory region.
*
* The implementation uses volatile writes to prevent the compiler from
* removing the operation as a dead store.
*/
void macsec_zeroize(void *buf, size_t len);
/*
* Compare two memory regions in constant execution time with respect to
* their contents.
*
* Unlike memcmp(), this function does not provide lexicographical ordering.
* It returns zero when both regions are equal and a non-zero value when they
* differ.
*
* The function always examines exactly len bytes.
*
* @param buf1
* First input buffer.
*
* @param buf2
* Second input buffer.
*
* @param len
* Number of bytes to compare.
*
* @return
* Zero when the buffers are equal, or a non-zero value otherwise.
*/
int macsec_compare(const void *buf1, const void *buf2, size_t len);
/*
* Convert a hexadecimal text string to binary data.
*
* @param hex
* Input hexadecimal string.
*
* @param out
* Output binary buffer.
*
* @param out_len
* Receives the number of decoded bytes.
*
* @param out_max_len
* Capacity of the output buffer.
*
* @return
* MACSEC_ERR_OK on success or another MACSEC_ERR_* value on failure.
*/
int macsec_hex_to_bin(const char *hex, uint8_t *out, size_t *out_len, size_t out_max_len);
#ifdef __cplusplus
}
#endif
#endif /* MACSEC_COMMON_H */