diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/regress/lib/libcrypto/evp/evp_test.c | 269 |
1 files changed, 268 insertions, 1 deletions
diff --git a/src/regress/lib/libcrypto/evp/evp_test.c b/src/regress/lib/libcrypto/evp/evp_test.c index 4a671d978d..e00ed01759 100644 --- a/src/regress/lib/libcrypto/evp/evp_test.c +++ b/src/regress/lib/libcrypto/evp/evp_test.c | |||
| @@ -1,4 +1,4 @@ | |||
| 1 | /* $OpenBSD: evp_test.c,v 1.4 2023/03/11 14:27:38 jsing Exp $ */ | 1 | /* $OpenBSD: evp_test.c,v 1.5 2023/09/28 11:39:35 tb Exp $ */ |
| 2 | /* | 2 | /* |
| 3 | * Copyright (c) 2022 Joel Sing <jsing@openbsd.org> | 3 | * Copyright (c) 2022 Joel Sing <jsing@openbsd.org> |
| 4 | * | 4 | * |
| @@ -15,6 +15,9 @@ | |||
| 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 15 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 16 | */ | 16 | */ |
| 17 | 17 | ||
| 18 | #include <assert.h> | ||
| 19 | #include <stdio.h> | ||
| 20 | |||
| 18 | #include <openssl/evp.h> | 21 | #include <openssl/evp.h> |
| 19 | #include <openssl/ossl_typ.h> | 22 | #include <openssl/ossl_typ.h> |
| 20 | 23 | ||
| @@ -137,6 +140,269 @@ evp_pkey_method_test(void) | |||
| 137 | return failed; | 140 | return failed; |
| 138 | } | 141 | } |
| 139 | 142 | ||
| 143 | static const struct evp_iv_len_test { | ||
| 144 | const EVP_CIPHER *(*cipher)(void); | ||
| 145 | int iv_len; | ||
| 146 | int setlen; | ||
| 147 | int expect; | ||
| 148 | } evp_iv_len_tests[] = { | ||
| 149 | { | ||
| 150 | .cipher = EVP_aes_128_ccm, | ||
| 151 | .iv_len = 7, | ||
| 152 | .setlen = 11, | ||
| 153 | .expect = 1, | ||
| 154 | }, | ||
| 155 | { | ||
| 156 | .cipher = EVP_aes_128_ccm, | ||
| 157 | .iv_len = 7, | ||
| 158 | .setlen = 6, | ||
| 159 | .expect = 0, | ||
| 160 | }, | ||
| 161 | { | ||
| 162 | .cipher = EVP_aes_128_ccm, | ||
| 163 | .iv_len = 7, | ||
| 164 | .setlen = 13, | ||
| 165 | .expect = 1, | ||
| 166 | }, | ||
| 167 | { | ||
| 168 | .cipher = EVP_aes_128_ccm, | ||
| 169 | .iv_len = 7, | ||
| 170 | .setlen = 14, | ||
| 171 | .expect = 0, | ||
| 172 | }, | ||
| 173 | |||
| 174 | { | ||
| 175 | .cipher = EVP_aes_192_ccm, | ||
| 176 | .iv_len = 7, | ||
| 177 | .setlen = 11, | ||
| 178 | .expect = 1, | ||
| 179 | }, | ||
| 180 | { | ||
| 181 | .cipher = EVP_aes_192_ccm, | ||
| 182 | .iv_len = 7, | ||
| 183 | .setlen = 6, | ||
| 184 | .expect = 0, | ||
| 185 | }, | ||
| 186 | { | ||
| 187 | .cipher = EVP_aes_192_ccm, | ||
| 188 | .iv_len = 7, | ||
| 189 | .setlen = 13, | ||
| 190 | .expect = 1, | ||
| 191 | }, | ||
| 192 | { | ||
| 193 | .cipher = EVP_aes_192_ccm, | ||
| 194 | .iv_len = 7, | ||
| 195 | .setlen = 14, | ||
| 196 | .expect = 0, | ||
| 197 | }, | ||
| 198 | |||
| 199 | { | ||
| 200 | .cipher = EVP_aes_256_ccm, | ||
| 201 | .iv_len = 7, | ||
| 202 | .setlen = 11, | ||
| 203 | .expect = 1, | ||
| 204 | }, | ||
| 205 | { | ||
| 206 | .cipher = EVP_aes_256_ccm, | ||
| 207 | .iv_len = 7, | ||
| 208 | .setlen = 6, | ||
| 209 | .expect = 0, | ||
| 210 | }, | ||
| 211 | { | ||
| 212 | .cipher = EVP_aes_256_ccm, | ||
| 213 | .iv_len = 7, | ||
| 214 | .setlen = 13, | ||
| 215 | .expect = 1, | ||
| 216 | }, | ||
| 217 | { | ||
| 218 | .cipher = EVP_aes_256_ccm, | ||
| 219 | .iv_len = 7, | ||
| 220 | .setlen = 14, | ||
| 221 | .expect = 0, | ||
| 222 | }, | ||
| 223 | |||
| 224 | { | ||
| 225 | .cipher = EVP_aes_128_gcm, | ||
| 226 | .iv_len = 12, | ||
| 227 | .setlen = 16, | ||
| 228 | .expect = 1, | ||
| 229 | }, | ||
| 230 | { | ||
| 231 | .cipher = EVP_aes_128_gcm, | ||
| 232 | .iv_len = 12, | ||
| 233 | .setlen = 0, | ||
| 234 | .expect = 0, | ||
| 235 | }, | ||
| 236 | { | ||
| 237 | .cipher = EVP_aes_128_gcm, | ||
| 238 | .iv_len = 12, | ||
| 239 | .setlen = 1, | ||
| 240 | .expect = 1, | ||
| 241 | }, | ||
| 242 | /* XXX - GCM IV length isn't capped... */ | ||
| 243 | { | ||
| 244 | .cipher = EVP_aes_128_gcm, | ||
| 245 | .iv_len = 12, | ||
| 246 | .setlen = 1024 * 1024, | ||
| 247 | .expect = 1, | ||
| 248 | }, | ||
| 249 | |||
| 250 | { | ||
| 251 | .cipher = EVP_aes_192_gcm, | ||
| 252 | .iv_len = 12, | ||
| 253 | .setlen = 16, | ||
| 254 | .expect = 1, | ||
| 255 | }, | ||
| 256 | { | ||
| 257 | .cipher = EVP_aes_192_gcm, | ||
| 258 | .iv_len = 12, | ||
| 259 | .setlen = 0, | ||
| 260 | .expect = 0, | ||
| 261 | }, | ||
| 262 | { | ||
| 263 | .cipher = EVP_aes_192_gcm, | ||
| 264 | .iv_len = 12, | ||
| 265 | .setlen = 1, | ||
| 266 | .expect = 1, | ||
| 267 | }, | ||
| 268 | /* XXX - GCM IV length isn't capped... */ | ||
| 269 | { | ||
| 270 | .cipher = EVP_aes_128_gcm, | ||
| 271 | .iv_len = 12, | ||
| 272 | .setlen = 1024 * 1024, | ||
| 273 | .expect = 1, | ||
| 274 | }, | ||
| 275 | |||
| 276 | { | ||
| 277 | .cipher = EVP_aes_256_gcm, | ||
| 278 | .iv_len = 12, | ||
| 279 | .setlen = 16, | ||
| 280 | .expect = 1, | ||
| 281 | }, | ||
| 282 | { | ||
| 283 | .cipher = EVP_aes_256_gcm, | ||
| 284 | .iv_len = 12, | ||
| 285 | .setlen = 0, | ||
| 286 | .expect = 0, | ||
| 287 | }, | ||
| 288 | { | ||
| 289 | .cipher = EVP_aes_256_gcm, | ||
| 290 | .iv_len = 12, | ||
| 291 | .setlen = 1, | ||
| 292 | .expect = 1, | ||
| 293 | }, | ||
| 294 | /* XXX - GCM IV length isn't capped... */ | ||
| 295 | { | ||
| 296 | .cipher = EVP_aes_128_gcm, | ||
| 297 | .iv_len = 12, | ||
| 298 | .setlen = 1024 * 1024, | ||
| 299 | .expect = 1, | ||
| 300 | }, | ||
| 301 | |||
| 302 | { | ||
| 303 | .cipher = EVP_aes_128_ecb, | ||
| 304 | .iv_len = 0, | ||
| 305 | .setlen = 11, | ||
| 306 | .expect = 0, | ||
| 307 | }, | ||
| 308 | |||
| 309 | { | ||
| 310 | .cipher = EVP_chacha20_poly1305, | ||
| 311 | .iv_len = 12, | ||
| 312 | .setlen = 11, | ||
| 313 | .expect = 1, | ||
| 314 | }, | ||
| 315 | { | ||
| 316 | .cipher = EVP_chacha20_poly1305, | ||
| 317 | .iv_len = 12, | ||
| 318 | .setlen = 12, | ||
| 319 | .expect = 1, | ||
| 320 | }, | ||
| 321 | { | ||
| 322 | .cipher = EVP_chacha20_poly1305, | ||
| 323 | .iv_len = 12, | ||
| 324 | .setlen = 13, | ||
| 325 | .expect = 0, | ||
| 326 | }, | ||
| 327 | { | ||
| 328 | .cipher = EVP_chacha20_poly1305, | ||
| 329 | .iv_len = 12, | ||
| 330 | .setlen = 1, | ||
| 331 | .expect = 1, | ||
| 332 | }, | ||
| 333 | { | ||
| 334 | .cipher = EVP_chacha20_poly1305, | ||
| 335 | .iv_len = 12, | ||
| 336 | .setlen = 0, | ||
| 337 | .expect = 0, | ||
| 338 | }, | ||
| 339 | }; | ||
| 340 | |||
| 341 | #define N_EVP_IV_LEN_TESTS \ | ||
| 342 | (sizeof(evp_iv_len_tests) / sizeof(evp_iv_len_tests[0])) | ||
| 343 | |||
| 344 | static int | ||
| 345 | evp_pkey_iv_len_testcase(const struct evp_iv_len_test *test) | ||
| 346 | { | ||
| 347 | const EVP_CIPHER *cipher = test->cipher(); | ||
| 348 | const char *name = OBJ_nid2ln(EVP_CIPHER_nid(cipher)); | ||
| 349 | EVP_CIPHER_CTX *ctx; | ||
| 350 | int ret; | ||
| 351 | int failure = 1; | ||
| 352 | |||
| 353 | assert(name != NULL); | ||
| 354 | |||
| 355 | if ((ctx = EVP_CIPHER_CTX_new()) == NULL) { | ||
| 356 | fprintf(stderr, "FAIL: %s: EVP_CIPHER_CTX_new()\n", name); | ||
| 357 | goto failure; | ||
| 358 | } | ||
| 359 | |||
| 360 | if ((ret = EVP_EncryptInit_ex(ctx, cipher, NULL, NULL, NULL)) <= 0) { | ||
| 361 | fprintf(stderr, "FAIL: %s: EVP_EncryptInit_ex:" | ||
| 362 | " want %d, got %d\n", name, 1, ret); | ||
| 363 | goto failure; | ||
| 364 | } | ||
| 365 | if ((ret = EVP_CIPHER_CTX_iv_length(ctx)) != test->iv_len) { | ||
| 366 | fprintf(stderr, "FAIL: %s EVP_CIPHER_CTX_iv_length (before set)" | ||
| 367 | " want %d, got %d\n", name, test->iv_len, ret); | ||
| 368 | goto failure; | ||
| 369 | } | ||
| 370 | if ((ret = EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, | ||
| 371 | test->setlen, NULL)) != test->expect) { | ||
| 372 | fprintf(stderr, "FAIL: %s EVP_CIPHER_CTX_ctrl" | ||
| 373 | " want %d, got %d\n", name, test->expect, ret); | ||
| 374 | goto failure; | ||
| 375 | } | ||
| 376 | if (test->expect == 0) | ||
| 377 | goto done; | ||
| 378 | if ((ret = EVP_CIPHER_CTX_iv_length(ctx)) != test->setlen) { | ||
| 379 | fprintf(stderr, "FAIL: %s EVP_CIPHER_CTX_iv_length (after set)" | ||
| 380 | " want %d, got %d\n", name, test->setlen, ret); | ||
| 381 | goto failure; | ||
| 382 | } | ||
| 383 | |||
| 384 | done: | ||
| 385 | EVP_CIPHER_CTX_free(ctx); | ||
| 386 | |||
| 387 | failure = 0; | ||
| 388 | |||
| 389 | failure: | ||
| 390 | |||
| 391 | return failure; | ||
| 392 | } | ||
| 393 | |||
| 394 | static int | ||
| 395 | evp_pkey_iv_len_test(void) | ||
| 396 | { | ||
| 397 | size_t i; | ||
| 398 | int failure = 0; | ||
| 399 | |||
| 400 | for (i = 0; i < N_EVP_IV_LEN_TESTS; i++) | ||
| 401 | failure |= evp_pkey_iv_len_testcase(&evp_iv_len_tests[i]); | ||
| 402 | |||
| 403 | return failure; | ||
| 404 | } | ||
| 405 | |||
| 140 | int | 406 | int |
| 141 | main(int argc, char **argv) | 407 | main(int argc, char **argv) |
| 142 | { | 408 | { |
| @@ -144,6 +410,7 @@ main(int argc, char **argv) | |||
| 144 | 410 | ||
| 145 | failed |= evp_asn1_method_test(); | 411 | failed |= evp_asn1_method_test(); |
| 146 | failed |= evp_pkey_method_test(); | 412 | failed |= evp_pkey_method_test(); |
| 413 | failed |= evp_pkey_iv_len_test(); | ||
| 147 | 414 | ||
| 148 | OPENSSL_cleanup(); | 415 | OPENSSL_cleanup(); |
| 149 | 416 | ||
