diff options
| author | tb <> | 2023-03-31 19:40:08 +0000 |
|---|---|---|
| committer | tb <> | 2023-03-31 19:40:08 +0000 |
| commit | 3bd97f5b9d6c76335593be456ad70eaf11897f78 (patch) | |
| tree | de62e8386e8b5bfcf6effc12eb19aa0d40fd80c9 /src | |
| parent | b2827ca9b69bbf2e96f407b0afc9c50634c2271a (diff) | |
| download | openbsd-3bd97f5b9d6c76335593be456ad70eaf11897f78.tar.gz openbsd-3bd97f5b9d6c76335593be456ad70eaf11897f78.tar.bz2 openbsd-3bd97f5b9d6c76335593be456ad70eaf11897f78.zip | |
Add regress coverage for the new behavior of BN_copy() with respect to
flags.
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libcrypto/bn/bn_unit.c | 162 |
1 files changed, 161 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/bn/bn_unit.c b/src/regress/lib/libcrypto/bn/bn_unit.c index 6c5b210739..95764dfce1 100644 --- a/src/regress/lib/libcrypto/bn/bn_unit.c +++ b/src/regress/lib/libcrypto/bn/bn_unit.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: bn_unit.c,v 1.3 2023/02/14 15:08:15 tb Exp $ */ | 1 | /* $OpenBSD: bn_unit.c,v 1.4 2023/03/31 19:40:08 tb Exp $ */ |
| 2 | 2 | ||
| 3 | /* | 3 | /* |
| 4 | * Copyright (c) 2022 Theo Buehler <tb@openbsd.org> | 4 | * Copyright (c) 2022 Theo Buehler <tb@openbsd.org> |
| @@ -92,6 +92,163 @@ test_bn_num_bits_word(void) | |||
| 92 | return failed; | 92 | return failed; |
| 93 | } | 93 | } |
| 94 | 94 | ||
| 95 | #define BN_FLG_ALL_KNOWN \ | ||
| 96 | (BN_FLG_STATIC_DATA | BN_FLG_CONSTTIME | BN_FLG_MALLOCED) | ||
| 97 | |||
| 98 | static int | ||
| 99 | bn_check_expected_flags(const BIGNUM *bn, int expected, const char *fn, | ||
| 100 | const char *descr) | ||
| 101 | { | ||
| 102 | int flags, got; | ||
| 103 | int ret = 1; | ||
| 104 | |||
| 105 | flags = BN_get_flags(bn, BN_FLG_ALL_KNOWN); | ||
| 106 | |||
| 107 | if ((got = flags & expected) != expected) { | ||
| 108 | fprintf(stderr, "%s: %s: expected flags: want %x, got %x\n", | ||
| 109 | fn, descr, expected, got); | ||
| 110 | ret = 0; | ||
| 111 | } | ||
| 112 | |||
| 113 | if ((got = flags & ~expected) != 0) { | ||
| 114 | fprintf(stderr, "%s: %s: unexpected flags: want %x, got %x\n", | ||
| 115 | fn, descr, 0, got); | ||
| 116 | ret = 0; | ||
| 117 | } | ||
| 118 | |||
| 119 | return ret; | ||
| 120 | } | ||
| 121 | |||
| 122 | static int | ||
| 123 | test_bn_copy_copies_flags(void) | ||
| 124 | { | ||
| 125 | BIGNUM *dst, *src; | ||
| 126 | int failed = 0; | ||
| 127 | |||
| 128 | if ((dst = BN_new()) == NULL) | ||
| 129 | errx(1, "%s: src = BN_new()", __func__); | ||
| 130 | |||
| 131 | if (!bn_check_expected_flags(dst, BN_FLG_MALLOCED, | ||
| 132 | __func__, "dst after BN_new")) | ||
| 133 | failed |= 1; | ||
| 134 | |||
| 135 | if (BN_copy(dst, BN_value_one()) == NULL) | ||
| 136 | errx(1, "%s: bn_copy()", __func__); | ||
| 137 | |||
| 138 | if (!bn_check_expected_flags(dst, BN_FLG_MALLOCED, | ||
| 139 | __func__, "dst after bn_copy")) | ||
| 140 | failed |= 1; | ||
| 141 | |||
| 142 | if ((src = BN_new()) == NULL) | ||
| 143 | errx(1, "%s: src = BN_new()", __func__); | ||
| 144 | |||
| 145 | BN_set_flags(src, BN_FLG_CONSTTIME); | ||
| 146 | |||
| 147 | if (!bn_check_expected_flags(src, BN_FLG_MALLOCED | BN_FLG_CONSTTIME, | ||
| 148 | __func__, "src after BN_set_flags")) | ||
| 149 | failed |= 1; | ||
| 150 | |||
| 151 | if (!BN_set_word(src, 57)) | ||
| 152 | errx(1, "%s: BN_set_word(src, 57)", __func__); | ||
| 153 | |||
| 154 | if (BN_copy(dst, src) == NULL) | ||
| 155 | errx(1, "%s: BN_copy(dst, src)", __func__); | ||
| 156 | |||
| 157 | if (BN_cmp(src, dst) != 0) { | ||
| 158 | fprintf(stderr, "copy not equal to original\n"); | ||
| 159 | failed |= 1; | ||
| 160 | } | ||
| 161 | |||
| 162 | if (!bn_check_expected_flags(dst, BN_FLG_MALLOCED | BN_FLG_CONSTTIME, | ||
| 163 | __func__, "dst after BN_copy(dst, src)")) | ||
| 164 | failed |= 1; | ||
| 165 | |||
| 166 | BN_free(dst); | ||
| 167 | BN_free(src); | ||
| 168 | |||
| 169 | return failed; | ||
| 170 | } | ||
| 171 | |||
| 172 | static int | ||
| 173 | test_bn_copy_consttime_is_sticky(void) | ||
| 174 | { | ||
| 175 | BIGNUM *src, *dst; | ||
| 176 | int failed = 0; | ||
| 177 | |||
| 178 | if ((src = BN_new()) == NULL) | ||
| 179 | errx(1, "%s: src = BN_new()", __func__); | ||
| 180 | |||
| 181 | if (!bn_check_expected_flags(src, BN_FLG_MALLOCED, | ||
| 182 | __func__, "src after BN_new")) | ||
| 183 | failed |= 1; | ||
| 184 | |||
| 185 | if ((dst = BN_new()) == NULL) | ||
| 186 | errx(1, "%s: dst = BN_new()", __func__); | ||
| 187 | |||
| 188 | if (!bn_check_expected_flags(dst, BN_FLG_MALLOCED, | ||
| 189 | __func__, "dst after BN_new")) | ||
| 190 | failed |= 1; | ||
| 191 | |||
| 192 | BN_set_flags(dst, BN_FLG_CONSTTIME); | ||
| 193 | |||
| 194 | if (!bn_check_expected_flags(dst, BN_FLG_MALLOCED | BN_FLG_CONSTTIME, | ||
| 195 | __func__, "src after BN_new")) | ||
| 196 | failed |= 1; | ||
| 197 | |||
| 198 | if (BN_copy(dst, BN_value_one()) == NULL) | ||
| 199 | errx(1, "%s: bn_copy()", __func__); | ||
| 200 | |||
| 201 | if (!bn_check_expected_flags(dst, BN_FLG_MALLOCED | BN_FLG_CONSTTIME, | ||
| 202 | __func__, "dst after bn_copy")) | ||
| 203 | failed |= 1; | ||
| 204 | |||
| 205 | BN_free(dst); | ||
| 206 | BN_free(src); | ||
| 207 | |||
| 208 | return failed; | ||
| 209 | } | ||
| 210 | |||
| 211 | static int | ||
| 212 | test_bn_dup_consttime_is_sticky(void) | ||
| 213 | { | ||
| 214 | BIGNUM *src, *dst; | ||
| 215 | int failed = 0; | ||
| 216 | |||
| 217 | if (!bn_check_expected_flags(BN_value_one(), BN_FLG_STATIC_DATA, | ||
| 218 | __func__, "flags on BN_value_one()")) | ||
| 219 | failed |= 1; | ||
| 220 | |||
| 221 | if ((dst = BN_dup(BN_value_one())) == NULL) | ||
| 222 | errx(1, "%s: dst = BN_dup(BN_value_one())", __func__); | ||
| 223 | |||
| 224 | if (!bn_check_expected_flags(dst, BN_FLG_MALLOCED, | ||
| 225 | __func__, "dst after BN_dup(BN_value_one())")) | ||
| 226 | failed |= 1; | ||
| 227 | |||
| 228 | BN_free(dst); | ||
| 229 | |||
| 230 | if ((src = BN_new()) == NULL) | ||
| 231 | errx(1, "%s: src = BN_new()", __func__); | ||
| 232 | |||
| 233 | BN_set_flags(src, BN_FLG_CONSTTIME); | ||
| 234 | |||
| 235 | if (!bn_check_expected_flags(src, BN_FLG_MALLOCED | BN_FLG_CONSTTIME, | ||
| 236 | __func__, "src after BN_new")) | ||
| 237 | failed |= 1; | ||
| 238 | |||
| 239 | if ((dst = BN_dup(src)) == NULL) | ||
| 240 | errx(1, "%s: dst = BN_dup(src)", __func__); | ||
| 241 | |||
| 242 | if (!bn_check_expected_flags(dst, BN_FLG_MALLOCED | BN_FLG_CONSTTIME, | ||
| 243 | __func__, "dst after bn_copy")) | ||
| 244 | failed |= 1; | ||
| 245 | |||
| 246 | BN_free(dst); | ||
| 247 | BN_free(src); | ||
| 248 | |||
| 249 | return failed; | ||
| 250 | } | ||
| 251 | |||
| 95 | int | 252 | int |
| 96 | main(void) | 253 | main(void) |
| 97 | { | 254 | { |
| @@ -99,6 +256,9 @@ main(void) | |||
| 99 | 256 | ||
| 100 | failed |= test_bn_print_null_derefs(); | 257 | failed |= test_bn_print_null_derefs(); |
| 101 | failed |= test_bn_num_bits_word(); | 258 | failed |= test_bn_num_bits_word(); |
| 259 | failed |= test_bn_copy_copies_flags(); | ||
| 260 | failed |= test_bn_copy_consttime_is_sticky(); | ||
| 261 | failed |= test_bn_dup_consttime_is_sticky(); | ||
| 102 | 262 | ||
| 103 | return failed; | 263 | return failed; |
| 104 | } | 264 | } |
