| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <stddef.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
|
| 11 |
#define MLD_BUILD_INTERNAL |
| 12 |
#include "../../mldsa/mldsa_native.h" |
| 13 |
#include "../../mldsa/src/common.h" |
| 14 |
#include "../notrandombytes/notrandombytes.h" |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#define MLD_TOTAL_ALLOC_KEYPAIR__(LVL) MLD_TOTAL_ALLOC_##LVL##_KEYPAIR |
| 25 |
#define MLD_TOTAL_ALLOC_KEYPAIR_(LVL) MLD_TOTAL_ALLOC_KEYPAIR__(LVL) |
| 26 |
#define MLD_TOTAL_ALLOC_KEYPAIR \ |
| 27 |
MLD_TOTAL_ALLOC_KEYPAIR_(MLD_CONFIG_API_PARAMETER_SET) |
| 28 |
|
| 29 |
#define MLD_TOTAL_ALLOC_SIGN__(LVL) MLD_TOTAL_ALLOC_##LVL##_SIGN |
| 30 |
#define MLD_TOTAL_ALLOC_SIGN_(LVL) MLD_TOTAL_ALLOC_SIGN__(LVL) |
| 31 |
#define MLD_TOTAL_ALLOC_SIGN MLD_TOTAL_ALLOC_SIGN_(MLD_CONFIG_API_PARAMETER_SET) |
| 32 |
|
| 33 |
#define MLD_TOTAL_ALLOC_VERIFY__(LVL) MLD_TOTAL_ALLOC_##LVL##_VERIFY |
| 34 |
#define MLD_TOTAL_ALLOC_VERIFY_(LVL) MLD_TOTAL_ALLOC_VERIFY__(LVL) |
| 35 |
#define MLD_TOTAL_ALLOC_VERIFY \ |
| 36 |
MLD_TOTAL_ALLOC_VERIFY_(MLD_CONFIG_API_PARAMETER_SET) |
| 37 |
|
| 38 |
#define MLD_TOTAL_ALLOC__(LVL) MLD_TOTAL_ALLOC_##LVL |
| 39 |
#define MLD_TOTAL_ALLOC_(LVL) MLD_TOTAL_ALLOC__(LVL) |
| 40 |
#define MLD_TOTAL_ALLOC MLD_TOTAL_ALLOC_(MLD_CONFIG_API_PARAMETER_SET) |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
typedef struct |
| 63 |
{ |
| 64 |
void *addr; |
| 65 |
size_t size; |
| 66 |
const char *file; |
| 67 |
int line; |
| 68 |
const char *var; |
| 69 |
const char *type; |
| 70 |
} alloc_info_t; |
| 71 |
|
| 72 |
#define MLD_MAX_IN_FLIGHT_ALLOCS 100 |
| 73 |
#define MLD_BUMP_ALLOC_SIZE (128 * 1024) |
| 74 |
|
| 75 |
struct test_ctx_t |
| 76 |
{ |
| 77 |
|
| 78 |
uint8_t *buffer; |
| 79 |
size_t offset; |
| 80 |
size_t high_mark; |
| 81 |
size_t global_high_mark; |
| 82 |
size_t global_high_mark_keypair; |
| 83 |
size_t global_high_mark_sign; |
| 84 |
size_t global_high_mark_verify; |
| 85 |
|
| 86 |
|
| 87 |
alloc_info_t alloc_stack[MLD_MAX_IN_FLIGHT_ALLOCS]; |
| 88 |
int alloc_stack_top; |
| 89 |
|
| 90 |
|
| 91 |
int alloc_counter; |
| 92 |
int fail_on_counter; |
| 93 |
int print_debug_info; |
| 94 |
}; |
| 95 |
typedef struct test_ctx_t test_ctx_t; |
| 96 |
|
| 97 |
static void alloc_tracker_push(test_ctx_t *ctx, void *addr, size_t size, |
| 98 |
const char *file, int line, const char *var, |
| 99 |
const char *type) |
| 100 |
{ |
| 101 |
if (ctx->alloc_stack_top >= MLD_MAX_IN_FLIGHT_ALLOCS) |
| 102 |
{ |
| 103 |
fprintf(stderr, "ERROR: Allocation stack overflow\n"); |
| 104 |
exit(1); |
| 105 |
} |
| 106 |
ctx->alloc_stack[ctx->alloc_stack_top].addr = addr; |
| 107 |
ctx->alloc_stack[ctx->alloc_stack_top].size = size; |
| 108 |
ctx->alloc_stack[ctx->alloc_stack_top].file = file; |
| 109 |
ctx->alloc_stack[ctx->alloc_stack_top].line = line; |
| 110 |
ctx->alloc_stack[ctx->alloc_stack_top].var = var; |
| 111 |
ctx->alloc_stack[ctx->alloc_stack_top].type = type; |
| 112 |
ctx->alloc_stack_top++; |
| 113 |
} |
| 114 |
|
| 115 |
static void alloc_tracker_pop(test_ctx_t *ctx, void *addr, size_t size, |
| 116 |
const char *file, int line, const char *var) |
| 117 |
{ |
| 118 |
alloc_info_t *top; |
| 119 |
if (ctx->alloc_stack_top == 0) |
| 120 |
{ |
| 121 |
fprintf( |
| 122 |
stderr, |
| 123 |
"ERROR: Attempting to free %s at %s:%d but allocation stack is empty\n", |
| 124 |
var, file, line); |
| 125 |
exit(1); |
| 126 |
} |
| 127 |
|
| 128 |
top = &ctx->alloc_stack[ctx->alloc_stack_top - 1]; |
| 129 |
if (top->addr != addr || top->size != size) |
| 130 |
{ |
| 131 |
fprintf(stderr, |
| 132 |
"ERROR: Free order violation at %s:%d\n" |
| 133 |
" Attempting to free: %s (addr=%p, sz %d)\n" |
| 134 |
" Expected to free: %s (addr=%p, sz %d) allocated at %s:%d\n", |
| 135 |
file, line, var, addr, (int)size, top->var, top->addr, |
| 136 |
(int)top->size, top->file, top->line); |
| 137 |
exit(1); |
| 138 |
} |
| 139 |
|
| 140 |
ctx->alloc_stack_top--; |
| 141 |
} |
| 142 |
|
| 143 |
static void *bump_alloc(test_ctx_t *ctx, size_t sz) |
| 144 |
{ |
| 145 |
|
| 146 |
size_t aligned_sz = (sz + 31) & ~((size_t)31); |
| 147 |
void *p; |
| 148 |
|
| 149 |
if (sz > MLD_BUMP_ALLOC_SIZE || |
| 150 |
aligned_sz > MLD_BUMP_ALLOC_SIZE - ctx->offset) |
| 151 |
{ |
| 152 |
return NULL; |
| 153 |
} |
| 154 |
|
| 155 |
p = ctx->buffer + ctx->offset; |
| 156 |
ctx->offset += aligned_sz; |
| 157 |
|
| 158 |
if (ctx->offset > ctx->high_mark) |
| 159 |
{ |
| 160 |
ctx->high_mark = ctx->offset; |
| 161 |
} |
| 162 |
|
| 163 |
return p; |
| 164 |
} |
| 165 |
|
| 166 |
static int bump_free(test_ctx_t *ctx, void *p) |
| 167 |
{ |
| 168 |
if (p == NULL) |
| 169 |
{ |
| 170 |
return 0; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
if (p < (void *)ctx->buffer || p >= (void *)(ctx->buffer + ctx->offset)) |
| 175 |
{ |
| 176 |
return -1; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
ctx->offset = (size_t)((uint8_t *)p - ctx->buffer); |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
static void reset_all(test_ctx_t *ctx) |
| 185 |
{ |
| 186 |
randombytes_reset(); |
| 187 |
ctx->alloc_counter = 0; |
| 188 |
ctx->alloc_stack_top = 0; |
| 189 |
ctx->offset = 0; |
| 190 |
ctx->fail_on_counter = -1; |
| 191 |
} |
| 192 |
|
| 193 |
void *custom_alloc(test_ctx_t *ctx, size_t sz, const char *file, int line, |
| 194 |
const char *var, const char *type) |
| 195 |
{ |
| 196 |
void *p = NULL; |
| 197 |
if (ctx->alloc_counter++ == ctx->fail_on_counter) |
| 198 |
{ |
| 199 |
return NULL; |
| 200 |
} |
| 201 |
|
| 202 |
p = bump_alloc(ctx, sz); |
| 203 |
if (p == NULL) |
| 204 |
{ |
| 205 |
fprintf(stderr, |
| 206 |
"ERROR: Bump allocator (%d bytes) ran out of memory. " |
| 207 |
"%s *%s (%d bytes) at %s:%d\n", |
| 208 |
(int)MLD_BUMP_ALLOC_SIZE, type, var, (int)sz, file, line); |
| 209 |
exit(1); |
| 210 |
} |
| 211 |
|
| 212 |
alloc_tracker_push(ctx, p, sz, file, line, var, type); |
| 213 |
|
| 214 |
if (ctx->print_debug_info == 1) |
| 215 |
{ |
| 216 |
fprintf(stderr, "Alloc #%d: %s %s (%d bytes) at %s:%d\n", |
| 217 |
ctx->alloc_counter + 1, type, var, (int)sz, file, line); |
| 218 |
} |
| 219 |
|
| 220 |
return p; |
| 221 |
} |
| 222 |
|
| 223 |
void custom_free(test_ctx_t *ctx, void *p, size_t sz, const char *file, |
| 224 |
int line, const char *var, const char *type) |
| 225 |
{ |
| 226 |
(void)type; |
| 227 |
|
| 228 |
if (p != NULL) |
| 229 |
{ |
| 230 |
alloc_tracker_pop(ctx, p, sz, file, line, var); |
| 231 |
} |
| 232 |
|
| 233 |
if (bump_free(ctx, p) != 0) |
| 234 |
{ |
| 235 |
fprintf(stderr, "ERROR: Free failed: %s %s (%d bytes) at %s:%d\n", type, |
| 236 |
var, (int)sz, file, line); |
| 237 |
exit(1); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
#define TEST_ALLOC_FAILURE(test_name, call, alloc_limit, global_high_mark_ptr) \ |
| 242 |
do \ |
| 243 |
{ \ |
| 244 |
int num_allocs, i, rc; \ |
| 245 |
\ |
| 246 |
ctx->high_mark = 0; \ |
| 247 |
reset_all(ctx); \ |
| 248 |
rc = call; \ |
| 249 |
if (rc != 0) \ |
| 250 |
{ \ |
| 251 |
fprintf(stderr, "ERROR: %s failed with %d in counting pass\n", \ |
| 252 |
test_name, rc); \ |
| 253 |
return 1; \ |
| 254 |
} \ |
| 255 |
if (ctx->alloc_stack_top != 0) \ |
| 256 |
{ \ |
| 257 |
fprintf(stderr, "ERROR: %s leaked %d allocation(s) in counting pass\n", \ |
| 258 |
test_name, ctx->alloc_stack_top); \ |
| 259 |
return 1; \ |
| 260 |
} \ |
| 261 |
num_allocs = ctx->alloc_counter; \ |
| 262 |
\ |
| 263 |
for (i = 0; i < num_allocs; i++) \ |
| 264 |
{ \ |
| 265 |
reset_all(ctx); \ |
| 266 |
ctx->fail_on_counter = i; \ |
| 267 |
rc = call; \ |
| 268 |
if (rc != MLD_ERR_OUT_OF_MEMORY) \ |
| 269 |
{ \ |
| 270 |
int rc2; \ |
| 271 |
\ |
| 272 |
ctx->print_debug_info = 1; \ |
| 273 |
reset_all(ctx); \ |
| 274 |
rc2 = call; \ |
| 275 |
(void)rc2; \ |
| 276 |
if (rc == 0) \ |
| 277 |
{ \ |
| 278 |
fprintf(stderr, \ |
| 279 |
"ERROR: %s unexpectedly succeeded when allocation %d/%d " \ |
| 280 |
"was instrumented to fail\n", \ |
| 281 |
test_name, i + 1, num_allocs); \ |
| 282 |
} \ |
| 283 |
else \ |
| 284 |
{ \ |
| 285 |
fprintf(stderr, \ |
| 286 |
"ERROR: %s failed with wrong error code %d " \ |
| 287 |
"(expected %d) when allocation %d/%d was instrumented " \ |
| 288 |
"to fail\n", \ |
| 289 |
test_name, rc, MLD_ERR_OUT_OF_MEMORY, i + 1, num_allocs); \ |
| 290 |
} \ |
| 291 |
return 1; \ |
| 292 |
} \ |
| 293 |
if (ctx->alloc_stack_top != 0) \ |
| 294 |
{ \ |
| 295 |
fprintf(stderr, \ |
| 296 |
"ERROR: %s leaked %d allocation(s) when allocation %d/%d " \ |
| 297 |
"was instrumented to fail\n", \ |
| 298 |
test_name, ctx->alloc_stack_top, i + 1, num_allocs); \ |
| 299 |
return 1; \ |
| 300 |
} \ |
| 301 |
if (ctx->offset != 0) \ |
| 302 |
{ \ |
| 303 |
fprintf(stderr, \ |
| 304 |
"ERROR: %s leaked %d bytes when allocation %d/%d " \ |
| 305 |
"was instrumented to fail\n", \ |
| 306 |
test_name, (int)ctx->offset, i + 1, num_allocs); \ |
| 307 |
return 1; \ |
| 308 |
} \ |
| 309 |
} \ |
| 310 |
if (ctx->high_mark > (alloc_limit)) \ |
| 311 |
{ \ |
| 312 |
fprintf(stderr, "ERROR: max allocation %u in %s exceeded limit %d\n", \ |
| 313 |
(unsigned)ctx->high_mark, test_name, (int)(alloc_limit)); \ |
| 314 |
return 1; \ |
| 315 |
} \ |
| 316 |
printf( \ |
| 317 |
"Allocation test for %s PASSED.\n" \ |
| 318 |
" Max dynamic allocation: %d bytes\n", \ |
| 319 |
test_name, (int)ctx->high_mark); \ |
| 320 |
if (ctx->high_mark > ctx->global_high_mark) \ |
| 321 |
{ \ |
| 322 |
ctx->global_high_mark = ctx->high_mark; \ |
| 323 |
} \ |
| 324 |
if (ctx->high_mark > *(global_high_mark_ptr)) \ |
| 325 |
{ \ |
| 326 |
*(global_high_mark_ptr) = ctx->high_mark; \ |
| 327 |
} \ |
| 328 |
} while (0) |
| 329 |
|
| 330 |
static int test_keygen_alloc_failure(test_ctx_t *ctx) |
| 331 |
{ |
| 332 |
uint8_t pk[CRYPTO_PUBLICKEYBYTES]; |
| 333 |
uint8_t sk[CRYPTO_SECRETKEYBYTES]; |
| 334 |
|
| 335 |
TEST_ALLOC_FAILURE("mld_keypair", mld_keypair(pk, sk, ctx), |
| 336 |
MLD_TOTAL_ALLOC_KEYPAIR, &ctx->global_high_mark_keypair); |
| 337 |
return 0; |
| 338 |
} |
| 339 |
|
| 340 |
static int test_sign_alloc_failure(test_ctx_t *ctx) |
| 341 |
{ |
| 342 |
uint8_t pk[CRYPTO_PUBLICKEYBYTES]; |
| 343 |
uint8_t sk[CRYPTO_SECRETKEYBYTES]; |
| 344 |
uint8_t sig[CRYPTO_BYTES]; |
| 345 |
uint8_t msg[32] = {0}; |
| 346 |
const uint8_t sign_ctx[] = "test context"; |
| 347 |
size_t siglen; |
| 348 |
|
| 349 |
|
| 350 |
reset_all(ctx); |
| 351 |
if (mld_keypair(pk, sk, ctx) != 0) |
| 352 |
{ |
| 353 |
fprintf(stderr, "ERROR: mld_keypair failed in sign test setup\n"); |
| 354 |
return 1; |
| 355 |
} |
| 356 |
|
| 357 |
TEST_ALLOC_FAILURE("mld_signature", |
| 358 |
mld_signature(sig, &siglen, msg, sizeof(msg), sign_ctx, |
| 359 |
sizeof(sign_ctx) - 1, sk, ctx), |
| 360 |
MLD_TOTAL_ALLOC_SIGN, &ctx->global_high_mark_sign); |
| 361 |
return 0; |
| 362 |
} |
| 363 |
|
| 364 |
static int test_verify_alloc_failure(test_ctx_t *ctx) |
| 365 |
{ |
| 366 |
uint8_t pk[CRYPTO_PUBLICKEYBYTES]; |
| 367 |
uint8_t sk[CRYPTO_SECRETKEYBYTES]; |
| 368 |
uint8_t sig[CRYPTO_BYTES]; |
| 369 |
uint8_t msg[32] = {0}; |
| 370 |
const uint8_t sign_ctx[] = "test context"; |
| 371 |
size_t siglen; |
| 372 |
|
| 373 |
|
| 374 |
reset_all(ctx); |
| 375 |
if (mld_keypair(pk, sk, ctx) != 0) |
| 376 |
{ |
| 377 |
fprintf(stderr, "ERROR: mld_keypair failed in verify test setup\n"); |
| 378 |
return 1; |
| 379 |
} |
| 380 |
|
| 381 |
if (mld_signature(sig, &siglen, msg, sizeof(msg), sign_ctx, |
| 382 |
sizeof(sign_ctx) - 1, sk, ctx) != 0) |
| 383 |
{ |
| 384 |
fprintf(stderr, "ERROR: mld_signature failed in verify test setup\n"); |
| 385 |
return 1; |
| 386 |
} |
| 387 |
|
| 388 |
TEST_ALLOC_FAILURE("mld_verify", |
| 389 |
mld_verify(sig, siglen, msg, sizeof(msg), sign_ctx, |
| 390 |
sizeof(sign_ctx) - 1, pk, ctx), |
| 391 |
MLD_TOTAL_ALLOC_VERIFY, &ctx->global_high_mark_verify); |
| 392 |
return 0; |
| 393 |
} |
| 394 |
|
| 395 |
static int test_sign_combined_alloc_failure(test_ctx_t *ctx) |
| 396 |
{ |
| 397 |
uint8_t pk[CRYPTO_PUBLICKEYBYTES]; |
| 398 |
uint8_t sk[CRYPTO_SECRETKEYBYTES]; |
| 399 |
uint8_t sm[CRYPTO_BYTES + 32]; |
| 400 |
uint8_t msg[32] = {0}; |
| 401 |
const uint8_t sign_ctx[] = "test context"; |
| 402 |
size_t smlen; |
| 403 |
|
| 404 |
reset_all(ctx); |
| 405 |
if (mld_keypair(pk, sk, ctx) != 0) |
| 406 |
{ |
| 407 |
fprintf(stderr, "ERROR: mld_keypair failed in sign combined test setup\n"); |
| 408 |
return 1; |
| 409 |
} |
| 410 |
|
| 411 |
TEST_ALLOC_FAILURE("mld_sign", |
| 412 |
mld_sign(sm, &smlen, msg, sizeof(msg), sign_ctx, |
| 413 |
sizeof(sign_ctx) - 1, sk, ctx), |
| 414 |
MLD_TOTAL_ALLOC_SIGN, &ctx->global_high_mark_sign); |
| 415 |
return 0; |
| 416 |
} |
| 417 |
|
| 418 |
static int test_open_alloc_failure(test_ctx_t *ctx) |
| 419 |
{ |
| 420 |
uint8_t pk[CRYPTO_PUBLICKEYBYTES]; |
| 421 |
uint8_t sk[CRYPTO_SECRETKEYBYTES]; |
| 422 |
uint8_t sm[CRYPTO_BYTES + 32]; |
| 423 |
uint8_t msg[32] = {0}; |
| 424 |
uint8_t msg_out[CRYPTO_BYTES + 32]; |
| 425 |
const uint8_t sign_ctx[] = "test context"; |
| 426 |
size_t smlen, mlen; |
| 427 |
|
| 428 |
reset_all(ctx); |
| 429 |
if (mld_keypair(pk, sk, ctx) != 0) |
| 430 |
{ |
| 431 |
fprintf(stderr, "ERROR: mld_keypair failed in open test setup\n"); |
| 432 |
return 1; |
| 433 |
} |
| 434 |
|
| 435 |
if (mld_sign(sm, &smlen, msg, sizeof(msg), sign_ctx, sizeof(sign_ctx) - 1, sk, |
| 436 |
ctx) != 0) |
| 437 |
{ |
| 438 |
fprintf(stderr, "ERROR: mld_sign failed in open test setup\n"); |
| 439 |
return 1; |
| 440 |
} |
| 441 |
|
| 442 |
TEST_ALLOC_FAILURE("mld_open", |
| 443 |
mld_open(msg_out, &mlen, sm, smlen, sign_ctx, |
| 444 |
sizeof(sign_ctx) - 1, pk, ctx), |
| 445 |
MLD_TOTAL_ALLOC_VERIFY, &ctx->global_high_mark_verify); |
| 446 |
return 0; |
| 447 |
} |
| 448 |
|
| 449 |
static int test_signature_extmu_alloc_failure(test_ctx_t *ctx) |
| 450 |
{ |
| 451 |
uint8_t pk[CRYPTO_PUBLICKEYBYTES]; |
| 452 |
uint8_t sk[CRYPTO_SECRETKEYBYTES]; |
| 453 |
uint8_t sig[CRYPTO_BYTES]; |
| 454 |
uint8_t mu[64] = {0}; |
| 455 |
size_t siglen; |
| 456 |
|
| 457 |
reset_all(ctx); |
| 458 |
if (mld_keypair(pk, sk, ctx) != 0) |
| 459 |
{ |
| 460 |
fprintf(stderr, |
| 461 |
"ERROR: mld_keypair failed in signature_extmu test setup\n"); |
| 462 |
return 1; |
| 463 |
} |
| 464 |
|
| 465 |
TEST_ALLOC_FAILURE("mld_signature_extmu", |
| 466 |
mld_signature_extmu(sig, &siglen, mu, sk, ctx), |
| 467 |
MLD_TOTAL_ALLOC_SIGN, &ctx->global_high_mark_sign); |
| 468 |
return 0; |
| 469 |
} |
| 470 |
|
| 471 |
static int test_verify_extmu_alloc_failure(test_ctx_t *ctx) |
| 472 |
{ |
| 473 |
uint8_t pk[CRYPTO_PUBLICKEYBYTES]; |
| 474 |
uint8_t sk[CRYPTO_SECRETKEYBYTES]; |
| 475 |
uint8_t sig[CRYPTO_BYTES]; |
| 476 |
uint8_t mu[64] = {0}; |
| 477 |
size_t siglen; |
| 478 |
|
| 479 |
reset_all(ctx); |
| 480 |
if (mld_keypair(pk, sk, ctx) != 0) |
| 481 |
{ |
| 482 |
fprintf(stderr, "ERROR: mld_keypair failed in verify_extmu test setup\n"); |
| 483 |
return 1; |
| 484 |
} |
| 485 |
|
| 486 |
if (mld_signature_extmu(sig, &siglen, mu, sk, ctx) != 0) |
| 487 |
{ |
| 488 |
fprintf(stderr, |
| 489 |
"ERROR: mld_signature_extmu failed in verify_extmu test setup\n"); |
| 490 |
return 1; |
| 491 |
} |
| 492 |
|
| 493 |
TEST_ALLOC_FAILURE("mld_verify_extmu", |
| 494 |
mld_verify_extmu(sig, siglen, mu, pk, ctx), |
| 495 |
MLD_TOTAL_ALLOC_VERIFY, &ctx->global_high_mark_verify); |
| 496 |
return 0; |
| 497 |
} |
| 498 |
|
| 499 |
static int test_signature_pre_hash_shake256_alloc_failure(test_ctx_t *ctx) |
| 500 |
{ |
| 501 |
uint8_t pk[CRYPTO_PUBLICKEYBYTES]; |
| 502 |
uint8_t sk[CRYPTO_SECRETKEYBYTES]; |
| 503 |
uint8_t sig[CRYPTO_BYTES]; |
| 504 |
uint8_t msg[32] = {0}; |
| 505 |
uint8_t rnd[32] = {0}; |
| 506 |
const uint8_t sign_ctx[] = "test context"; |
| 507 |
size_t siglen; |
| 508 |
|
| 509 |
reset_all(ctx); |
| 510 |
if (mld_keypair(pk, sk, ctx) != 0) |
| 511 |
{ |
| 512 |
fprintf(stderr, |
| 513 |
"ERROR: mld_keypair failed in signature_pre_hash_shake256 test " |
| 514 |
"setup\n"); |
| 515 |
return 1; |
| 516 |
} |
| 517 |
|
| 518 |
TEST_ALLOC_FAILURE( |
| 519 |
"mld_signature_pre_hash_shake256", |
| 520 |
mld_signature_pre_hash_shake256(sig, &siglen, msg, sizeof(msg), sign_ctx, |
| 521 |
sizeof(sign_ctx) - 1, rnd, sk, ctx), |
| 522 |
MLD_TOTAL_ALLOC_SIGN, &ctx->global_high_mark_sign); |
| 523 |
return 0; |
| 524 |
} |
| 525 |
|
| 526 |
static int test_verify_pre_hash_shake256_alloc_failure(test_ctx_t *ctx) |
| 527 |
{ |
| 528 |
uint8_t pk[CRYPTO_PUBLICKEYBYTES]; |
| 529 |
uint8_t sk[CRYPTO_SECRETKEYBYTES]; |
| 530 |
uint8_t sig[CRYPTO_BYTES]; |
| 531 |
uint8_t msg[32] = {0}; |
| 532 |
uint8_t rnd[32] = {0}; |
| 533 |
const uint8_t sign_ctx[] = "test context"; |
| 534 |
size_t siglen; |
| 535 |
|
| 536 |
reset_all(ctx); |
| 537 |
if (mld_keypair(pk, sk, ctx) != 0) |
| 538 |
{ |
| 539 |
fprintf(stderr, |
| 540 |
"ERROR: mld_keypair failed in verify_pre_hash_shake256 test " |
| 541 |
"setup\n"); |
| 542 |
return 1; |
| 543 |
} |
| 544 |
|
| 545 |
if (mld_signature_pre_hash_shake256(sig, &siglen, msg, sizeof(msg), sign_ctx, |
| 546 |
sizeof(sign_ctx) - 1, rnd, sk, ctx) != 0) |
| 547 |
{ |
| 548 |
fprintf(stderr, |
| 549 |
"ERROR: mld_signature_pre_hash_shake256 failed in " |
| 550 |
"verify_pre_hash_shake256 test setup\n"); |
| 551 |
return 1; |
| 552 |
} |
| 553 |
|
| 554 |
TEST_ALLOC_FAILURE( |
| 555 |
"mld_verify_pre_hash_shake256", |
| 556 |
mld_verify_pre_hash_shake256(sig, siglen, msg, sizeof(msg), sign_ctx, |
| 557 |
sizeof(sign_ctx) - 1, pk, ctx), |
| 558 |
MLD_TOTAL_ALLOC_VERIFY, &ctx->global_high_mark_verify); |
| 559 |
return 0; |
| 560 |
} |
| 561 |
|
| 562 |
static int test_pk_from_sk_alloc_failure(test_ctx_t *ctx) |
| 563 |
{ |
| 564 |
uint8_t pk[CRYPTO_PUBLICKEYBYTES]; |
| 565 |
uint8_t sk[CRYPTO_SECRETKEYBYTES]; |
| 566 |
|
| 567 |
reset_all(ctx); |
| 568 |
if (mld_keypair(pk, sk, ctx) != 0) |
| 569 |
{ |
| 570 |
fprintf(stderr, "ERROR: mld_keypair failed in pk_from_sk test setup\n"); |
| 571 |
return 1; |
| 572 |
} |
| 573 |
|
| 574 |
TEST_ALLOC_FAILURE("mld_pk_from_sk", mld_pk_from_sk(pk, sk, ctx), |
| 575 |
MLD_TOTAL_ALLOC_KEYPAIR, &ctx->global_high_mark_keypair); |
| 576 |
return 0; |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
#define CHECK_ALLOC_MATCH(high_mark, expected) \ |
| 583 |
do \ |
| 584 |
{ \ |
| 585 |
if ((expected) != (high_mark)) \ |
| 586 |
{ \ |
| 587 |
fprintf(stderr, "ERROR: %s = %u does not match %s = %d\n", #high_mark, \ |
| 588 |
(unsigned)(high_mark), #expected, (int)(expected)); \ |
| 589 |
return 1; \ |
| 590 |
} \ |
| 591 |
} while (0) |
| 592 |
|
| 593 |
int main(void) |
| 594 |
{ |
| 595 |
MLD_ALIGN uint8_t bump_buffer[MLD_BUMP_ALLOC_SIZE]; |
| 596 |
|
| 597 |
test_ctx_t ctx = { |
| 598 |
NULL, |
| 599 |
0, |
| 600 |
0, |
| 601 |
0, |
| 602 |
0, |
| 603 |
0, |
| 604 |
0, |
| 605 |
{{0}}, |
| 606 |
0, |
| 607 |
0, |
| 608 |
-1, |
| 609 |
0 |
| 610 |
}; |
| 611 |
ctx.buffer = bump_buffer; |
| 612 |
|
| 613 |
if (test_keygen_alloc_failure(&ctx) != 0) |
| 614 |
{ |
| 615 |
return 1; |
| 616 |
} |
| 617 |
|
| 618 |
if (test_sign_alloc_failure(&ctx) != 0) |
| 619 |
{ |
| 620 |
return 1; |
| 621 |
} |
| 622 |
|
| 623 |
if (test_verify_alloc_failure(&ctx) != 0) |
| 624 |
{ |
| 625 |
return 1; |
| 626 |
} |
| 627 |
|
| 628 |
if (test_sign_combined_alloc_failure(&ctx) != 0) |
| 629 |
{ |
| 630 |
return 1; |
| 631 |
} |
| 632 |
|
| 633 |
if (test_open_alloc_failure(&ctx) != 0) |
| 634 |
{ |
| 635 |
return 1; |
| 636 |
} |
| 637 |
|
| 638 |
if (test_signature_extmu_alloc_failure(&ctx) != 0) |
| 639 |
{ |
| 640 |
return 1; |
| 641 |
} |
| 642 |
|
| 643 |
if (test_verify_extmu_alloc_failure(&ctx) != 0) |
| 644 |
{ |
| 645 |
return 1; |
| 646 |
} |
| 647 |
|
| 648 |
if (test_signature_pre_hash_shake256_alloc_failure(&ctx) != 0) |
| 649 |
{ |
| 650 |
return 1; |
| 651 |
} |
| 652 |
|
| 653 |
if (test_verify_pre_hash_shake256_alloc_failure(&ctx) != 0) |
| 654 |
{ |
| 655 |
return 1; |
| 656 |
} |
| 657 |
|
| 658 |
if (test_pk_from_sk_alloc_failure(&ctx) != 0) |
| 659 |
{ |
| 660 |
return 1; |
| 661 |
} |
| 662 |
|
| 663 |
|
| 664 |
CHECK_ALLOC_MATCH(ctx.global_high_mark_keypair, MLD_TOTAL_ALLOC_KEYPAIR); |
| 665 |
CHECK_ALLOC_MATCH(ctx.global_high_mark_sign, MLD_TOTAL_ALLOC_SIGN); |
| 666 |
CHECK_ALLOC_MATCH(ctx.global_high_mark_verify, MLD_TOTAL_ALLOC_VERIFY); |
| 667 |
CHECK_ALLOC_MATCH(ctx.global_high_mark, MLD_TOTAL_ALLOC); |
| 668 |
|
| 669 |
return 0; |
| 670 |
} |