diff options
| author | tb <> | 2022-09-07 21:25:21 +0000 |
|---|---|---|
| committer | tb <> | 2022-09-07 21:25:21 +0000 |
| commit | 4ccf2baba3b7ebc9a0fc9f27d93da6692760b1a7 (patch) | |
| tree | 3e1a8fb58c8da17b3435d3cbef5b5cd76da8387f /src | |
| parent | 310a60fb2583beb1364c862f2983bf30bc0d5c13 (diff) | |
| download | openbsd-4ccf2baba3b7ebc9a0fc9f27d93da6692760b1a7.tar.gz openbsd-4ccf2baba3b7ebc9a0fc9f27d93da6692760b1a7.tar.bz2 openbsd-4ccf2baba3b7ebc9a0fc9f27d93da6692760b1a7.zip | |
Add EVP test coverage for RC2.
From Joshua Sing
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libcrypto/rc2/rc2_test.c | 195 |
1 files changed, 194 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/rc2/rc2_test.c b/src/regress/lib/libcrypto/rc2/rc2_test.c index e82d675db1..9ec1df0a8a 100644 --- a/src/regress/lib/libcrypto/rc2/rc2_test.c +++ b/src/regress/lib/libcrypto/rc2/rc2_test.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: rc2_test.c,v 1.1 2022/09/06 15:36:25 tb Exp $ */ | 1 | /* $OpenBSD: rc2_test.c,v 1.2 2022/09/07 21:25:21 tb Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2022 Joshua Sing <joshua@hypera.dev> | 3 | * Copyright (c) 2022 Joshua Sing <joshua@hypera.dev> |
| 4 | * | 4 | * |
| @@ -168,6 +168,7 @@ static const struct rc2_test rc2_tests[] = { | |||
| 168 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 168 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 169 | }, | 169 | }, |
| 170 | .key_len = 16, | 170 | .key_len = 16, |
| 171 | .key_bits = 1024, | ||
| 171 | .len = 8, | 172 | .len = 8, |
| 172 | .in = { | 173 | .in = { |
| 173 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 174 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| @@ -183,6 +184,7 @@ static const struct rc2_test rc2_tests[] = { | |||
| 183 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, | 184 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, |
| 184 | }, | 185 | }, |
| 185 | .key_len = 16, | 186 | .key_len = 16, |
| 187 | .key_bits = 1024, | ||
| 186 | .len = 8, | 188 | .len = 8, |
| 187 | .in = { | 189 | .in = { |
| 188 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 190 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| @@ -198,6 +200,7 @@ static const struct rc2_test rc2_tests[] = { | |||
| 198 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 200 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| 199 | }, | 201 | }, |
| 200 | .key_len = 16, | 202 | .key_len = 16, |
| 203 | .key_bits = 1024, | ||
| 201 | .len = 8, | 204 | .len = 8, |
| 202 | .in = { | 205 | .in = { |
| 203 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | 206 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, |
| @@ -213,6 +216,7 @@ static const struct rc2_test rc2_tests[] = { | |||
| 213 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, | 216 | 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, |
| 214 | }, | 217 | }, |
| 215 | .key_len = 16, | 218 | .key_len = 16, |
| 219 | .key_bits = 1024, | ||
| 216 | .len = 8, | 220 | .len = 8, |
| 217 | .in = { | 221 | .in = { |
| 218 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | 222 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, |
| @@ -257,9 +261,193 @@ rc2_ecb_test(size_t test_number, const struct rc2_test *rt) | |||
| 257 | } | 261 | } |
| 258 | 262 | ||
| 259 | static int | 263 | static int |
| 264 | rc2_evp_test(size_t test_number, const struct rc2_test *rt, const char *label, const EVP_CIPHER *cipher) | ||
| 265 | { | ||
| 266 | EVP_CIPHER_CTX *ctx; | ||
| 267 | uint8_t out[512]; | ||
| 268 | int in_len, out_len, total_len; | ||
| 269 | int i; | ||
| 270 | int success = 0; | ||
| 271 | |||
| 272 | if ((ctx = EVP_CIPHER_CTX_new()) == NULL) { | ||
| 273 | fprintf(stderr, "FAIL (%s:%zu): EVP_CIPHER_CTX_new failed\n", | ||
| 274 | label, test_number); | ||
| 275 | goto failed; | ||
| 276 | } | ||
| 277 | |||
| 278 | /* EVP encryption */ | ||
| 279 | total_len = 0; | ||
| 280 | memset(out, 0, sizeof(out)); | ||
| 281 | if (!EVP_EncryptInit(ctx, cipher, NULL, NULL)) { | ||
| 282 | fprintf(stderr, "FAIL (%s:%zu): EVP_EncryptInit failed\n", | ||
| 283 | label, test_number); | ||
| 284 | goto failed; | ||
| 285 | } | ||
| 286 | |||
| 287 | if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC2_KEY_BITS, | ||
| 288 | rt->key_bits, NULL) <= 0) { | ||
| 289 | fprintf(stderr, "FAIL (%s:%zu): EVP_CIPHER_CTX_ctrl failed\n", | ||
| 290 | label, test_number); | ||
| 291 | goto failed; | ||
| 292 | } | ||
| 293 | |||
| 294 | if (!EVP_CIPHER_CTX_set_key_length(ctx, rt->key_len)) { | ||
| 295 | fprintf(stderr, | ||
| 296 | "FAIL (%s:%zu): EVP_CIPHER_CTX_set_key_length failed\n", | ||
| 297 | label, test_number); | ||
| 298 | goto failed; | ||
| 299 | } | ||
| 300 | |||
| 301 | if (!EVP_CIPHER_CTX_set_padding(ctx, 0)) { | ||
| 302 | fprintf(stderr, | ||
| 303 | "FAIL (%s:%zu): EVP_CIPHER_CTX_set_padding failed\n", | ||
| 304 | label, test_number); | ||
| 305 | goto failed; | ||
| 306 | } | ||
| 307 | |||
| 308 | if (!EVP_EncryptInit(ctx, NULL, rt->key, NULL)) { | ||
| 309 | fprintf(stderr, "FAIL (%s:%zu): EVP_EncryptInit failed\n", | ||
| 310 | label, test_number); | ||
| 311 | goto failed; | ||
| 312 | } | ||
| 313 | |||
| 314 | for (i = 0; i < rt->len;) { | ||
| 315 | in_len = arc4random_uniform(sizeof(rt->len / 2)); | ||
| 316 | if (in_len > rt->len - i) | ||
| 317 | in_len = rt->len - i; | ||
| 318 | |||
| 319 | if (!EVP_EncryptUpdate(ctx, out + total_len, &out_len, | ||
| 320 | rt->in + i, in_len)) { | ||
| 321 | fprintf(stderr, | ||
| 322 | "FAIL (%s:%zu): EVP_EncryptUpdate failed\n", | ||
| 323 | label, test_number); | ||
| 324 | goto failed; | ||
| 325 | } | ||
| 326 | |||
| 327 | i += in_len; | ||
| 328 | total_len += out_len; | ||
| 329 | } | ||
| 330 | |||
| 331 | if (!EVP_EncryptFinal_ex(ctx, out + out_len, &out_len)) { | ||
| 332 | fprintf(stderr, "FAIL (%s:%zu): EVP_EncryptFinal_ex failed\n", | ||
| 333 | label, test_number); | ||
| 334 | goto failed; | ||
| 335 | } | ||
| 336 | total_len += out_len; | ||
| 337 | |||
| 338 | if (!EVP_CIPHER_CTX_reset(ctx)) { | ||
| 339 | fprintf(stderr, | ||
| 340 | "FAIL (%s:%zu): EVP_CIPHER_CTX_reset failed\n", | ||
| 341 | label, test_number); | ||
| 342 | goto failed; | ||
| 343 | } | ||
| 344 | |||
| 345 | if (total_len != rt->len) { | ||
| 346 | fprintf(stderr, | ||
| 347 | "FAIL (%s:%zu): EVP encryption length mismatch\n", | ||
| 348 | label, test_number); | ||
| 349 | goto failed; | ||
| 350 | } | ||
| 351 | |||
| 352 | if (memcmp(rt->out, out, rt->len) != 0) { | ||
| 353 | fprintf(stderr, "FAIL (%s:%zu): EVP encryption mismatch\n", | ||
| 354 | label, test_number); | ||
| 355 | goto failed; | ||
| 356 | } | ||
| 357 | |||
| 358 | /* EVP decryption */ | ||
| 359 | total_len = 0; | ||
| 360 | memset(out, 0, sizeof(out)); | ||
| 361 | if (!EVP_DecryptInit(ctx, cipher, NULL, NULL)) { | ||
| 362 | fprintf(stderr, "FAIL (%s:%zu): EVP_DecryptInit failed\n", | ||
| 363 | label, test_number); | ||
| 364 | goto failed; | ||
| 365 | } | ||
| 366 | |||
| 367 | if (EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_SET_RC2_KEY_BITS, | ||
| 368 | rt->key_bits, NULL) <= 0) { | ||
| 369 | fprintf(stderr, "FAIL (%s:%zu): EVP_CIPHER_CTX_ctrl failed\n", | ||
| 370 | label, test_number); | ||
| 371 | goto failed; | ||
| 372 | } | ||
| 373 | |||
| 374 | if (!EVP_CIPHER_CTX_set_key_length(ctx, rt->key_len)) { | ||
| 375 | fprintf(stderr, | ||
| 376 | "FAIL (%s:%zu): EVP_CIPHER_CTX_set_key_length failed\n", | ||
| 377 | label, test_number); | ||
| 378 | goto failed; | ||
| 379 | } | ||
| 380 | |||
| 381 | if (!EVP_CIPHER_CTX_set_padding(ctx, 0)) { | ||
| 382 | fprintf(stderr, | ||
| 383 | "FAIL (%s:%zu): EVP_CIPHER_CTX_set_padding failed\n", | ||
| 384 | label, test_number); | ||
| 385 | goto failed; | ||
| 386 | } | ||
| 387 | |||
| 388 | if (!EVP_DecryptInit(ctx, NULL, rt->key, NULL)) { | ||
| 389 | fprintf(stderr, "FAIL (%s:%zu): EVP_DecryptInit failed\n", | ||
| 390 | label, test_number); | ||
| 391 | goto failed; | ||
| 392 | } | ||
| 393 | |||
| 394 | for (i = 0; i < rt->len;) { | ||
| 395 | in_len = arc4random_uniform(sizeof(rt->len / 2)); | ||
| 396 | if (in_len > rt->len - i) | ||
| 397 | in_len = rt->len - i; | ||
| 398 | |||
| 399 | if (!EVP_DecryptUpdate(ctx, out + total_len, &out_len, | ||
| 400 | rt->out + i, in_len)) { | ||
| 401 | fprintf(stderr, | ||
| 402 | "FAIL (%s:%zu): EVP_DecryptUpdate failed\n", | ||
| 403 | label, test_number); | ||
| 404 | goto failed; | ||
| 405 | } | ||
| 406 | |||
| 407 | i += in_len; | ||
| 408 | total_len += out_len; | ||
| 409 | } | ||
| 410 | |||
| 411 | if (!EVP_DecryptFinal_ex(ctx, out + total_len, &out_len)) { | ||
| 412 | fprintf(stderr, "FAIL (%s:%zu): EVP_DecryptFinal_ex failed\n", | ||
| 413 | label, test_number); | ||
| 414 | goto failed; | ||
| 415 | } | ||
| 416 | total_len += out_len; | ||
| 417 | |||
| 418 | if (!EVP_CIPHER_CTX_reset(ctx)) { | ||
| 419 | fprintf(stderr, | ||
| 420 | "FAIL (%s:%zu): EVP_CIPHER_CTX_reset failed\n", | ||
| 421 | label, test_number); | ||
| 422 | goto failed; | ||
| 423 | } | ||
| 424 | |||
| 425 | if (total_len != rt->len) { | ||
| 426 | fprintf(stderr, | ||
| 427 | "FAIL (%s:%zu): EVP decryption length mismatch\n", | ||
| 428 | label, test_number); | ||
| 429 | goto failed; | ||
| 430 | } | ||
| 431 | |||
| 432 | if (memcmp(rt->in, out, rt->len) != 0) { | ||
| 433 | fprintf(stderr, "FAIL (%s:%zu): EVP decryption mismatch\n", | ||
| 434 | label, test_number); | ||
| 435 | goto failed; | ||
| 436 | } | ||
| 437 | |||
| 438 | success = 1; | ||
| 439 | |||
| 440 | failed: | ||
| 441 | EVP_CIPHER_CTX_free(ctx); | ||
| 442 | return success; | ||
| 443 | } | ||
| 444 | |||
| 445 | static int | ||
| 260 | rc2_test(void) | 446 | rc2_test(void) |
| 261 | { | 447 | { |
| 262 | const struct rc2_test *rt; | 448 | const struct rc2_test *rt; |
| 449 | const char *label; | ||
| 450 | const EVP_CIPHER *cipher; | ||
| 263 | size_t i; | 451 | size_t i; |
| 264 | int failed = 1; | 452 | int failed = 1; |
| 265 | 453 | ||
| @@ -267,6 +455,8 @@ rc2_test(void) | |||
| 267 | rt = &rc2_tests[i]; | 455 | rt = &rc2_tests[i]; |
| 268 | switch (rt->mode) { | 456 | switch (rt->mode) { |
| 269 | case NID_rc2_ecb: | 457 | case NID_rc2_ecb: |
| 458 | label = SN_rc2_ecb; | ||
| 459 | cipher = EVP_rc2_ecb(); | ||
| 270 | if (!rc2_ecb_test(i, rt)) | 460 | if (!rc2_ecb_test(i, rt)) |
| 271 | goto failed; | 461 | goto failed; |
| 272 | break; | 462 | break; |
| @@ -275,6 +465,9 @@ rc2_test(void) | |||
| 275 | rt->mode); | 465 | rt->mode); |
| 276 | goto failed; | 466 | goto failed; |
| 277 | } | 467 | } |
| 468 | |||
| 469 | if (!rc2_evp_test(i, rt, label, cipher)) | ||
| 470 | goto failed; | ||
| 278 | } | 471 | } |
| 279 | 472 | ||
| 280 | failed = 0; | 473 | failed = 0; |
