xref: /freebsd/crypto/openssl/test/cipher_overhead_test.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 /*
2  * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9 
10 #include "internal/nelem.h"
11 #include "testutil.h"
12 #include "../ssl/ssl_local.h"
13 
14 static int cipher_enabled(const SSL_CIPHER *ciph)
15 {
16     /*
17      * ssl_cipher_get_overhead() actually works with AEAD ciphers even if the
18      * underlying implementation is not present.
19      */
20     if ((ciph->algorithm_mac & SSL_AEAD) != 0)
21         return 1;
22 
23     if (ciph->algorithm_enc != SSL_eNULL
24             && EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(ciph)) == NULL)
25         return 0;
26 
27     if (EVP_get_digestbynid(SSL_CIPHER_get_digest_nid(ciph)) == NULL)
28         return 0;
29 
30     return 1;
31 }
32 
33 static int cipher_overhead(void)
34 {
35     int ret = 1, i, n = ssl3_num_ciphers();
36     const SSL_CIPHER *ciph;
37     size_t mac, in, blk, ex;
38 
39     for (i = 0; i < n; i++) {
40         ciph = ssl3_get_cipher(i);
41         if (!ciph->min_dtls)
42             continue;
43         if (!cipher_enabled(ciph)) {
44             TEST_skip("Skipping disabled cipher %s", ciph->name);
45             continue;
46         }
47         if (!TEST_true(ssl_cipher_get_overhead(ciph, &mac, &in, &blk, &ex))) {
48             TEST_info("Failed getting %s", ciph->name);
49             ret = 0;
50         } else {
51             TEST_info("Cipher %s: %zu %zu %zu %zu",
52                       ciph->name, mac, in, blk, ex);
53         }
54     }
55     return ret;
56 }
57 
58 int setup_tests(void)
59 {
60     ADD_TEST(cipher_overhead);
61     return 1;
62 }
63