summaryrefslogtreecommitdiff
path: root/regress/lib/libcrypto
diff options
context:
space:
mode:
authorJoel Sing <jsing@cvs.openbsd.org>2024-04-09 14:59:58 +0000
committerJoel Sing <jsing@cvs.openbsd.org>2024-04-09 14:59:58 +0000
commita0b2025b93d8fa14995394330977f590fe0b7d8e (patch)
tree81965d1f182910ee1c7aeaa551d7f1c574271928 /regress/lib/libcrypto
parent105388b3f0ce0d1a9dc368ada8d785c93fa494e2 (diff)
Add initial regress for BN_set_bit(), BN_clear_bit() and BN_mask_bits().
Diffstat (limited to 'regress/lib/libcrypto')
-rw-r--r--regress/lib/libcrypto/bn/Makefile3
-rw-r--r--regress/lib/libcrypto/bn/bn_bits.c227
2 files changed, 229 insertions, 1 deletions
diff --git a/regress/lib/libcrypto/bn/Makefile b/regress/lib/libcrypto/bn/Makefile
index 6342a6142e6..8f490506158 100644
--- a/regress/lib/libcrypto/bn/Makefile
+++ b/regress/lib/libcrypto/bn/Makefile
@@ -1,6 +1,7 @@
-# $OpenBSD: Makefile,v 1.37 2023/10/19 10:23:38 tb Exp $
+# $OpenBSD: Makefile,v 1.38 2024/04/09 14:59:57 jsing Exp $
PROGS += bn_add_sub
+PROGS += bn_bits
PROGS += bn_cmp
PROGS += bn_convert
PROGS += bn_gcd
diff --git a/regress/lib/libcrypto/bn/bn_bits.c b/regress/lib/libcrypto/bn/bn_bits.c
new file mode 100644
index 00000000000..4ed13e424b2
--- /dev/null
+++ b/regress/lib/libcrypto/bn/bn_bits.c
@@ -0,0 +1,227 @@
+/* $OpenBSD: bn_bits.c,v 1.1 2024/04/09 14:59:57 jsing Exp $ */
+/*
+ * Copyright (c) 2024 Joel Sing <jsing@openbsd.org>
+ *
+ * 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 <stdio.h>
+#include <string.h>
+
+#include <openssl/bn.h>
+
+static int
+test_bn_set_bit(void)
+{
+ BIGNUM *bn = NULL;
+ char *out_str = NULL;
+ size_t i;
+ int failed = 1;
+
+ if ((bn = BN_new()) == NULL)
+ goto failure;
+
+ for (i = 0; i < 128; i++) {
+ if (i % 2 == 0) {
+ if (!BN_set_bit(bn, i)) {
+ fprintf(stderr, "FAIL: failed to set bit\n");
+ goto failure;
+ }
+ }
+ if (BN_is_bit_set(bn, i) != (i % 2 == 0)) {
+ fprintf(stderr, "FAIL: BN_is_bit_set() = %d, want %d\n",
+ BN_is_bit_set(bn, i), (i % 2 == 0));
+ goto failure;
+ }
+ }
+
+ if ((out_str = BN_bn2hex(bn)) == NULL)
+ goto failure;
+ if (strcmp(out_str, "55555555555555555555555555555555") != 0) {
+ fprintf(stderr, "FAIL: got 0x%s, want 0x%s\n", out_str,
+ "55555555555555555555555555555555");
+ goto failure;
+ }
+
+ failed = 0;
+
+ failure:
+ BN_free(bn);
+ free(out_str);
+
+ return failed;
+}
+
+static int
+test_bn_clear_bit(void)
+{
+ BIGNUM *bn = NULL;
+ char *out_str = NULL;
+ size_t i;
+ int failed = 1;
+
+ if ((bn = BN_new()) == NULL)
+ goto failure;
+
+ for (i = 0; i < 128; i++) {
+ if (!BN_set_bit(bn, i)) {
+ fprintf(stderr, "FAIL: failed to set bit\n");
+ goto failure;
+ }
+ if (i % 2 == 0) {
+ if (!BN_clear_bit(bn, i)) {
+ fprintf(stderr, "FAIL: failed to clear bit\n");
+ goto failure;
+ }
+ }
+ if (BN_is_bit_set(bn, i) != (i % 2 == 1)) {
+ fprintf(stderr, "FAIL: BN_is_bit_set() = %d, want %d\n",
+ BN_is_bit_set(bn, i), (i % 2 == 1));
+ goto failure;
+ }
+ }
+
+ if ((out_str = BN_bn2hex(bn)) == NULL)
+ goto failure;
+ if (strcmp(out_str, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") != 0) {
+ fprintf(stderr, "FAIL: got 0x%s, want 0x%s\n", out_str,
+ "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
+ goto failure;
+ }
+
+ /* Ensure that clearing results in non-negative zero. */
+ if (!BN_one(bn))
+ goto failure;
+ BN_set_negative(bn, 1);
+ if (!BN_clear_bit(bn, 0)) {
+ fprintf(stderr, "FAIL: failed to clear bit\n");
+ goto failure;
+ }
+ if (!BN_is_zero(bn)) {
+ fprintf(stderr, "FAIL: clear bit did not result in zero\n");
+ goto failure;
+ }
+#if 0
+ if (BN_is_negative(bn)) {
+ fprintf(stderr, "FAIL: clear bit resulted in -0\n");
+ goto failure;
+ }
+#endif
+
+ failed = 0;
+
+ failure:
+ BN_free(bn);
+ free(out_str);
+
+ return failed;
+}
+
+static int
+test_bn_mask_bits(void)
+{
+ BIGNUM *bn = NULL;
+ char *out_str = NULL;
+ size_t i;
+ int failed = 1;
+
+ if ((bn = BN_new()) == NULL)
+ goto failure;
+
+ if (BN_mask_bits(bn, 0)) {
+ fprintf(stderr, "FAIL: mask bits should have failed\n");
+ goto failure;
+ }
+
+ for (i = 0; i < 128; i++) {
+ if (!BN_set_bit(bn, i)) {
+ fprintf(stderr, "FAIL: failed to set bit\n");
+ goto failure;
+ }
+ }
+
+ if ((out_str = BN_bn2hex(bn)) == NULL)
+ goto failure;
+ if (strcmp(out_str, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") != 0) {
+ fprintf(stderr, "FAIL: got 0x%s, want 0x%s\n", out_str,
+ "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
+ goto failure;
+ }
+
+ if (!BN_mask_bits(bn, 127)) {
+ fprintf(stderr, "FAIL: failed to mask bits\n");
+ goto failure;
+ }
+
+ free(out_str);
+ if ((out_str = BN_bn2hex(bn)) == NULL)
+ goto failure;
+ if (strcmp(out_str, "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF") != 0) {
+ fprintf(stderr, "FAIL: got 0x%s, want 0x%s\n", out_str,
+ "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
+ goto failure;
+ }
+
+ if (!BN_mask_bits(bn, 65)) {
+ fprintf(stderr, "FAIL: failed to mask bits\n");
+ goto failure;
+ }
+
+ free(out_str);
+ if ((out_str = BN_bn2hex(bn)) == NULL)
+ goto failure;
+ if (strcmp(out_str, "01FFFFFFFFFFFFFFFF") != 0) {
+ fprintf(stderr, "FAIL: got 0x%s, want 0x%s\n", out_str,
+ "01FFFFFFFFFFFFFFFF");
+ goto failure;
+ }
+
+ /* Ensure that masking results in non-negative zero. */
+ if (!BN_one(bn))
+ goto failure;
+ BN_set_negative(bn, 1);
+ if (!BN_mask_bits(bn, 0)) {
+ fprintf(stderr, "FAIL: failed to mask bits\n");
+ goto failure;
+ }
+ if (!BN_is_zero(bn)) {
+ fprintf(stderr, "FAIL: mask bits did not result in zero\n");
+ goto failure;
+ }
+#if 0
+ if (BN_is_negative(bn)) {
+ fprintf(stderr, "FAIL: mask bits resulted in -0\n");
+ goto failure;
+ }
+#endif
+
+ failed = 0;
+
+ failure:
+ BN_free(bn);
+ free(out_str);
+
+ return failed;
+}
+
+int
+main(int argc, char **argv)
+{
+ int failed = 0;
+
+ failed |= test_bn_set_bit();
+ failed |= test_bn_clear_bit();
+ failed |= test_bn_mask_bits();
+
+ return failed;
+}