From bd76828a7fcd510d0f0de2ae81755504fab92c3e Mon Sep 17 00:00:00 2001 From: jsing Date: Thu, 25 Apr 2024 14:27:29 +0000 Subject: [PATCH] Add regress coverage for crypto_ct_*_u8() --- regress/lib/libcrypto/Makefile | 3 +- regress/lib/libcrypto/crypto/Makefile | 12 +++ regress/lib/libcrypto/crypto/crypto_test.c | 97 ++++++++++++++++++++++ 3 files changed, 111 insertions(+), 1 deletion(-) create mode 100644 regress/lib/libcrypto/crypto/Makefile create mode 100644 regress/lib/libcrypto/crypto/crypto_test.c diff --git a/regress/lib/libcrypto/Makefile b/regress/lib/libcrypto/Makefile index 871ae2093a6..ffe08d04bdc 100644 --- a/regress/lib/libcrypto/Makefile +++ b/regress/lib/libcrypto/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.56 2024/03/29 07:13:38 joshua Exp $ +# $OpenBSD: Makefile,v 1.57 2024/04/25 14:27:29 jsing Exp $ SUBDIR += aead SUBDIR += aes @@ -14,6 +14,7 @@ SUBDIR += cast SUBDIR += certs SUBDIR += chacha SUBDIR += cms +SUBDIR += crypto SUBDIR += ct SUBDIR += curve25519 SUBDIR += des diff --git a/regress/lib/libcrypto/crypto/Makefile b/regress/lib/libcrypto/crypto/Makefile new file mode 100644 index 00000000000..34a4e7050bd --- /dev/null +++ b/regress/lib/libcrypto/crypto/Makefile @@ -0,0 +1,12 @@ +# $OpenBSD: Makefile,v 1.1 2024/04/25 14:27:29 jsing Exp $ + +PROG = crypto_test + +DPADD+= ${LIBCRYPTO} +WARNINGS= Yes +LDFLAGS+= -lcrypto +CFLAGS+= -DLIBRESSL_INTERNAL +CFLAGS+= -Wall -Wundef -Werror +CFLAGS+= -I${.CURDIR}/../../../../lib/libcrypto + +.include diff --git a/regress/lib/libcrypto/crypto/crypto_test.c b/regress/lib/libcrypto/crypto/crypto_test.c new file mode 100644 index 00000000000..38ee2d57d4c --- /dev/null +++ b/regress/lib/libcrypto/crypto/crypto_test.c @@ -0,0 +1,97 @@ +/* $OpenBSD: crypto_test.c,v 1.1 2024/04/25 14:27:29 jsing Exp $ */ +/* + * Copyright (c) 2024 Joel Sing + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include + +#include "crypto_internal.h" + +static int +test_ct_u8(void) +{ + uint8_t i, j, mask; + int failed = 1; + + i = 0; + + do { + if ((i != 0) != crypto_ct_ne_zero_u8(i)) { + fprintf(stderr, "FAIL: crypto_ct_ne_zero_u8(%d) = %d, " + "want %d\n", i, crypto_ct_ne_zero_u8(i), i != 0); + goto failure; + } + mask = (i != 0) ? 0xff : 0x00; + if (mask != crypto_ct_ne_zero_mask_u8(i)) { + fprintf(stderr, "FAIL: crypto_ct_ne_zero_mask_u8(%d) = %x, " + "want %x\n", i, crypto_ct_ne_zero_mask_u8(i), mask); + goto failure; + } + if ((i == 0) != crypto_ct_eq_zero_u8(i)) { + fprintf(stderr, "FAIL: crypto_ct_eq_zero_u8(%d) = %d, " + "want %d\n", i, crypto_ct_ne_zero_u8(i), i != 0); + goto failure; + } + mask = (i == 0) ? 0xff : 0x00; + if (mask != crypto_ct_eq_zero_mask_u8(i)) { + fprintf(stderr, "FAIL: crypto_ct_eq_zero_mask_u8(%d) = %x, " + "want %x\n", i, crypto_ct_ne_zero_mask_u8(i), mask); + goto failure; + } + + j = 0; + + do { + if ((i != j) != crypto_ct_ne_u8(i, j)) { + fprintf(stderr, "FAIL: crypto_ct_ne_u8(%d, %d) = %d, " + "want %d\n", i, j, crypto_ct_ne_u8(i, j), i != j); + goto failure; + } + mask = (i != j) ? 0xff : 0x00; + if (mask != crypto_ct_ne_mask_u8(i, j)) { + fprintf(stderr, "FAIL: crypto_ct_ne_mask_u8(%d, %d) = %x, " + "want %x\n", i, j, crypto_ct_ne_mask_u8(i, j), mask); + goto failure; + } + if ((i == j) != crypto_ct_eq_u8(i, j)) { + fprintf(stderr, "FAIL: crypto_ct_eq_u8(%d, %d) = %d, " + "want %d\n", i, j, crypto_ct_eq_u8(i, j), i != j); + goto failure; + } + mask = (i == j) ? 0xff : 0x00; + if (mask != crypto_ct_eq_mask_u8(i, j)) { + fprintf(stderr, "FAIL: crypto_ct_eq_mask_u8(%d, %d) = %x, " + "want %x\n", i, j, crypto_ct_eq_mask_u8(i, j), mask); + goto failure; + } + } while (++j != 0); + } while (++i != 0); + + failed = 0; + + failure: + return failed; +} + +int +main(int argc, char **argv) +{ + int failed = 0; + + failed |= test_ct_u8(); + + return failed; +} -- 2.20.1