|
10 | 10 | #include "s2_protocol.h"
|
11 | 11 | #include "s2_classcmd.h"
|
12 | 12 | #include "../inclusion/s2_inclusion_internal.h"
|
13 |
| -#include<string.h> |
| 13 | + |
| 14 | +#include <assert.h> |
| 15 | +#include <string.h> |
| 16 | + |
14 | 17 | #include "ccm.h"
|
15 | 18 | #include "aes_cmac.h"
|
16 | 19 | #include "nextnonce.h"
|
@@ -1282,13 +1285,25 @@ S2_init_ctx(uint32_t home)
|
1282 | 1285 |
|
1283 | 1286 | // Erase sensitive memory safely
|
1284 | 1287 | #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L
|
1285 |
| - memset_explicit(ctx, 0, sizeof(struct S2)); |
1286 |
| -#elif defined(HAVE_EXPLICIT_BZERO) // for gcc-12 |
| 1288 | + void *result = memset_explicit(ctx, 0, sizeof(struct S2)); |
| 1289 | + assert(result == ctx); |
| 1290 | + if (result != ctx) { |
| 1291 | + return (NULL); |
| 1292 | + } |
| 1293 | +#elif defined(HAVE_EXPLICIT_BZERO) // for gcc-12 |
1287 | 1294 | explicit_bzero(ctx, sizeof(struct S2));
|
1288 |
| -#elif defined(__APPLE__) // for MacOS |
1289 |
| - memset_s(ctx, 0, sizeof(struct S2)); |
| 1295 | +#elif defined(__APPLE__) // for MacOS |
| 1296 | + errno_t result = memset_s(ctx, sizeof(struct S2), 0, sizeof(struct S2)); |
| 1297 | + assert(result == 0); |
| 1298 | + if (result != 0) { |
| 1299 | + return (NULL); |
| 1300 | + } |
1290 | 1301 | #else
|
1291 |
| - memset(ctx, 0, sizeof(struct S2)); //NOSONAR: Fallback option |
| 1302 | + void *result = memset(ctx, 0, sizeof(struct S2)); //NOSONAR: Fallback option |
| 1303 | + assert(result == ctx); |
| 1304 | + if (result != ctx) { |
| 1305 | + return (NULL); |
| 1306 | + } |
1292 | 1307 | #endif
|
1293 | 1308 |
|
1294 | 1309 | ctx->my_home_id = home;
|
@@ -1339,7 +1354,20 @@ void
|
1339 | 1354 | S2_destroy(struct S2* p_context)
|
1340 | 1355 | {
|
1341 | 1356 | CTX_DEF
|
| 1357 | + // Erase sensitive memory safely |
| 1358 | +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L |
| 1359 | + void *result = memset_explicit(ctxt, 0, sizeof(struct S2)); |
| 1360 | + assert(result == ctxt); |
| 1361 | +#elif defined(HAVE_EXPLICIT_BZERO) // for gcc-12 |
1342 | 1362 | explicit_bzero(ctxt, sizeof(struct S2));
|
| 1363 | +#elif defined(__APPLE__) // for MacOS |
| 1364 | + errno_t result = memset_s(ctxt, sizeof(struct S2), 0, sizeof(struct S2)); |
| 1365 | + assert(result == 0); |
| 1366 | +#else |
| 1367 | + void *result = memset(ctxt, 0, sizeof(struct S2)); //NOSONAR: Fallback option |
| 1368 | + assert(result == ctxt); |
| 1369 | +#endif |
| 1370 | + |
1343 | 1371 | #ifndef SINGLE_CONTEXT
|
1344 | 1372 | free(ctxt);
|
1345 | 1373 | #endif
|
|
0 commit comments