From: "cron2 (Code Review)" Date: Thu, 8 Feb 2024 11:15:12 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <18fbab60088607a60af5ba0d0e3f9edfb7fbca27-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="ZZxR+9cIwqo="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Add unit test for encrypting/decrypting data channel List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld Cc: openvpn-devel --ZZxR+9cIwqo= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#4) to the change originally created by= plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/505?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by flichtenheld Change subject: Add unit test for encrypting/decrypting data channel ...................................................................... Add unit test for encrypting/decrypting data channel This test is reusing code from --test-crypto but is modified to not rely on the static key functionality and also only tests the most common algorithm. So it does not yet completely replace --test-crypto Change-Id: Ifa5ae96165d17b3cae4afc53e844bb34d1610e58 Acked-by: Frank Lichtenheld Message-Id: <20240208085749.869-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg28= 195.html Signed-off-by: Gert Doering --- M tests/unit_tests/openvpn/test_ssl.c 1 file changed, 223 insertions(+), 2 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/05/505/4 diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn= /test_ssl.c index 18b9ec8..8c1fb5b 100644 --- a/tests/unit_tests/openvpn/test_ssl.c +++ b/tests/unit_tests/openvpn/test_ssl.c @@ -44,6 +44,9 @@ #include "ssl_verify_backend.h" #include "win32.h" #include "test_common.h" +#include "ssl.h" +#include "buffer.h" +#include "packet_id.h" /* Mock function to be allowed to include win32.c which is required for * getting the temp directory */ @@ -120,20 +123,238 @@ gc_free(&gc); } +static void +init_implicit_iv(struct crypto_options *co) +{ + cipher_ctx_t *cipher =3D co->key_ctx_bi.encrypt.cipher; + + if (cipher_ctx_mode_aead(cipher)) + { + size_t impl_iv_len =3D cipher_ctx_iv_length(cipher) - sizeof(packe= t_id_type); + ASSERT(cipher_ctx_iv_length(cipher) <=3D OPENVPN_MAX_IV_LENGTH); + ASSERT(cipher_ctx_iv_length(cipher) >=3D OPENVPN_AEAD_MIN_IV_LEN); + + /* Generate dummy implicit IV */ + ASSERT(rand_bytes(co->key_ctx_bi.encrypt.implicit_iv, + OPENVPN_MAX_IV_LENGTH)); + co->key_ctx_bi.encrypt.implicit_iv_len =3D impl_iv_len; + + memcpy(co->key_ctx_bi.decrypt.implicit_iv, + co->key_ctx_bi.encrypt.implicit_iv, OPENVPN_MAX_IV_LENGTH); + co->key_ctx_bi.decrypt.implicit_iv_len =3D impl_iv_len; + } +} + +static void +init_frame_parameters(struct frame *frame) +{ + int overhead =3D 0; + + /* tls-auth and tls-crypt */ + overhead +=3D 128; + + /* TCP length field and opcode */ + overhead +=3D 3; + + /* ACK array and remote SESSION ID (part of the ACK array) */ + overhead +=3D ACK_SIZE(RELIABLE_ACK_SIZE); + + /* Previous OpenVPN version calculated the maximum size and buffer of a + * control frame depending on the overhead of the data channel frame + * overhead and limited its maximum size to 1250. Since control frames + * also need to fit into data channel buffer we have the same + * default of 1500 + 100 as data channel buffers have. Increasing + * control channel mtu beyond this limit also increases the data chann= el + * buffers */ + int tls_mtu =3D 1500; + frame->buf.payload_size =3D tls_mtu + 100; + + frame->buf.headroom =3D overhead; + frame->buf.tailroom =3D overhead; + + frame->tun_mtu =3D tls_mtu; + +} + +static void +do_data_channel_round_trip(struct crypto_options *co) +{ + struct gc_arena gc =3D gc_new(); + + /* initialise frame for the test */ + struct frame frame; + init_frame_parameters(&frame); + + struct buffer src =3D alloc_buf_gc(frame.buf.payload_size, &gc); + struct buffer work =3D alloc_buf_gc(BUF_SIZE(&frame), &gc); + struct buffer encrypt_workspace =3D alloc_buf_gc(BUF_SIZE(&frame), &gc= ); + struct buffer decrypt_workspace =3D alloc_buf_gc(BUF_SIZE(&frame), &gc= ); + struct buffer buf =3D clear_buf(); + void *buf_p; + + /* init work */ + ASSERT(buf_init(&work, frame.buf.headroom)); + + init_implicit_iv(co); + update_time(); + + /* Test encryption, decryption for all packet sizes */ + for (int i =3D 1; i <=3D frame.buf.payload_size; ++i) + { + + /* msg(M_INFO, "TESTING ENCRYPT/DECRYPT of packet length=3D%d", i)= ; */ + + /* + * Load src with random data. + */ + ASSERT(buf_init(&src, 0)); + ASSERT(i <=3D src.capacity); + src.len =3D i; + ASSERT(rand_bytes(BPTR(&src), BLEN(&src))); + + /* copy source to input buf */ + buf =3D work; + buf_p =3D buf_write_alloc(&buf, BLEN(&src)); + ASSERT(buf_p); + memcpy(buf_p, BPTR(&src), BLEN(&src)); + + /* initialize work buffer with buf.headroom bytes of prepend capac= ity */ + ASSERT(buf_init(&encrypt_workspace, frame.buf.headroom)); + + /* encrypt */ + openvpn_encrypt(&buf, encrypt_workspace, co); + + /* decrypt */ + openvpn_decrypt(&buf, decrypt_workspace, co, &frame, BPTR(&buf)); + + /* compare */ + assert_int_equal(buf.len, src.len); + assert_memory_equal(BPTR(&src), BPTR(&buf), i); + + } + gc_free(&gc); +} + + + +struct crypto_options +init_crypto_options(const char *cipher, const char *auth) +{ + struct key2 key2 =3D { .n =3D 2}; + + ASSERT(rand_bytes(key2.keys[0].cipher, sizeof(key2.keys[0].cipher))); + ASSERT(rand_bytes(key2.keys[0].hmac, sizeof(key2.keys[0].hmac))); + ASSERT(rand_bytes(key2.keys[1].cipher, sizeof(key2.keys[1].cipher))); + ASSERT(rand_bytes(key2.keys[1].hmac, sizeof(key2.keys)[1].hmac)); + + struct crypto_options co =3D { 0 }; + + struct key_type kt =3D create_kt(cipher, auth, "ssl-test"); + + init_key_ctx_bi(&co.key_ctx_bi, &key2, 0, &kt, "unit-test-ssl"); + packet_id_init(&co.packet_id, 5, 5, "UNITTEST", 0); + + return co; +} + +static void +uninit_crypto_options(struct crypto_options *co) +{ + packet_id_free(&co->packet_id); + free_key_ctx_bi(&co->key_ctx_bi); + +} + + +static void +run_data_channel_with_cipher(const char *cipher, const char *auth) +{ + struct crypto_options co =3D init_crypto_options(cipher, auth); + do_data_channel_round_trip(&co); + uninit_crypto_options(&co); +} + +static void +test_data_channel_roundtrip_aes_128_gcm(void **state) +{ + run_data_channel_with_cipher("AES-128-GCM", "none"); +} + +static void +test_data_channel_roundtrip_aes_192_gcm(void **state) +{ + run_data_channel_with_cipher("AES-192-GCM", "none"); +} + +static void +test_data_channel_roundtrip_aes_256_gcm(void **state) +{ + run_data_channel_with_cipher("AES-256-GCM", "none"); +} + +static void +test_data_channel_roundtrip_aes_128_cbc(void **state) +{ + run_data_channel_with_cipher("AES-128-CBC", "SHA256"); +} + +static void +test_data_channel_roundtrip_aes_192_cbc(void **state) +{ + run_data_channel_with_cipher("AES-192-CBC", "SHA256"); +} + +static void +test_data_channel_roundtrip_aes_256_cbc(void **state) +{ + run_data_channel_with_cipher("AES-256-CBC", "SHA256"); +} + +static void +test_data_channel_roundtrip_chacha20_poly1305(void **state) +{ + if (!cipher_valid("ChaCha20-Poly1305")) + { + skip(); + return; + } + run_data_channel_with_cipher("ChaCha20-Poly1305", "none"); +} + +static void +test_data_channel_roundtrip_bf_cbc(void **state) +{ + if (!cipher_valid("BF-CBC")) + { + skip(); + return; + } + run_data_channel_with_cipher("BF-CBC", "SHA1"); +} + + int main(void) { openvpn_unit_test_setup(); const struct CMUnitTest tests[] =3D { - cmocka_unit_test(crypto_pem_encode_certificate) + cmocka_unit_test(crypto_pem_encode_certificate), + cmocka_unit_test(test_data_channel_roundtrip_aes_128_gcm), + cmocka_unit_test(test_data_channel_roundtrip_aes_192_gcm), + cmocka_unit_test(test_data_channel_roundtrip_aes_256_gcm), + cmocka_unit_test(test_data_channel_roundtrip_chacha20_poly1305), + cmocka_unit_test(test_data_channel_roundtrip_aes_128_cbc), + cmocka_unit_test(test_data_channel_roundtrip_aes_192_cbc), + cmocka_unit_test(test_data_channel_roundtrip_aes_256_cbc), + cmocka_unit_test(test_data_channel_roundtrip_bf_cbc), }; =20 #if defined(ENABLE_CRYPTO_OPENSSL) tls_init_lib(); #endif - int ret =3D cmocka_run_group_tests_name("crypto tests", tests, NULL, N= ULL); + int ret =3D cmocka_run_group_tests_name("ssl tests", tests, NULL, NULL= ); #if defined(ENABLE_CRYPTO_OPENSSL) tls_free_lib(); -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/505?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: Ifa5ae96165d17b3cae4afc53e844bb34d1610e58 Gerrit-Change-Number: 505 Gerrit-PatchSet: 4 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-MessageType: newpatchset --ZZxR+9cIwqo= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 uploaded patch set #4 to the change originally cr= eated by plaisthos.

View Change

The following approvals got outdated= and were removed: Code-Review+2 by flichtenheld

Add unit test for encrypting/decrypting data =
channel

This test is reusing code from --test-crypto but is modified= to not rely
on the static key functionality and also only tests the mos= t common
algorithm. So it does not yet completely replace --test-crypto<= br>
Change-Id: Ifa5ae96165d17b3cae4afc53e844bb34d1610e58
Acked-by: Fr= ank Lichtenheld <frank@...2641...>
Message-Id: <20240208085749.= 869-1-gert@...1296...>
URL: https://www.mail-archive.com/openvpn-deve= l@lists.sourceforge.net/msg28195.html
Signed-off-by: Gert Doering <ge= rt@...1296...>
---
M tests/unit_tests/openvpn/test_ssl.c
1 file= changed, 223 insertions(+), 2 deletions(-)

git pull ssh://gerrit=
.openvpn.net:29418/openvpn refs/changes/05/505/4
diff --git a/tests=
/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn/test_ssl.c=

index 18b9ec8..8c1fb5b 100644
--- a/tests/unit_te= sts/openvpn/test_ssl.c
+++ b/tests/unit_tests/openvpn/test_= ssl.c
@@ -44,6 +44,9 @@
#include "ssl= _verify_backend.h"
#include "win32.h"
#include "test_common.h"
+#include "ssl.h"
+#include "buffer.h"
<= span style=3D"color: hsl(120, 100%, 40%);">+#include "packet_id.h"= ;


/* Mock function to be allowed to incl= ude win32.c which is required for
* getting the temp dire= ctory */
@@ -120,20 +123,238 @@
gc_fre= e(&gc);
}

+static void
+init_implicit_iv(struct crypto_options *co)
= +{
+ cipher_ctx_t *cipher =3D co->key_ctx_bi.e= ncrypt.cipher;
+
+ if (cipher_ctx_mode_= aead(cipher))
+ {<= /span>
+ size_t impl_= iv_len =3D cipher_ctx_iv_length(cipher) - sizeof(packet_id_type);+ ASSERT(cipher_ctx_iv_= length(cipher) <=3D OPENVPN_MAX_IV_LENGTH);
+ ASSERT(cipher_ctx_iv_length(cipher) >= =3D OPENVPN_AEAD_MIN_IV_LEN);
+
+ /= * Generate dummy implicit IV */
+ ASSERT(rand_bytes(co->key_ctx_bi.encrypt.implicit_iv= ,
+ = OPENVPN_MAX_IV_LENGTH));
+ co->key_ctx_bi.encrypt.implicit_iv_len =3D impl_iv_= len;
+
+ memcpy(co->key_ctx_bi.d= ecrypt.implicit_iv,

+= co->key_ctx_bi.encrypt.implicit_iv, OPENVPN_MAX_IV_LENGTH= );
+ co->ke= y_ctx_bi.decrypt.implicit_iv_len =3D impl_iv_len;
+ }
+}
+
+static void+init_frame_parameters(struct = frame *frame)
+{
+ int overhead =3D 0;<= /span>
+
+ /* tls-auth and tls-crypt */=
+ overhead +=3D 128;
+
+ /* TCP length field and opcode */
+ overhead +=3D 3;
+
+ /* ACK array and remote SESSION ID (p= art of the ACK array) */
+ overhead +=3D ACK_SIZE(RELIABLE_ACK_SIZE);
+
+ /* Previous OpenVPN version calculated the maximum size = and buffer of a
+ = * control frame depending on the overhead of the data channel frame=
+ * overhead and limite= d its maximum size to 1250. Since control frames
+ * also need to fit into data channel buff= er we have the same
+= * default of 1500 + 100 as data channel buffers have. Increasing
+ * control channel mt= u beyond this limit also increases the data channel
+ * buffers */
+ int tls_mtu =3D 1500;
+ frame->buf.payload_size =3D = tls_mtu + 100;

+
+ frame->buf.headro= om =3D overhead;
+ = frame->buf.tailroom =3D overhead;
+
+ = frame->tun_mtu =3D tls_mtu;
+
+}
+
+static void
+do_data_channel_round_trip(struct crypto_options = *co)
+{
+ struct gc_arena gc =3D gc_new= ();

+
+ /* initialise frame for the tes= t */

+ struct fram= e frame;
+ init_fr= ame_parameters(&frame);
+
+ struct = buffer src =3D alloc_buf_gc(frame.buf.payload_size, &gc);
+ struct buffer work =3D alloc_= buf_gc(BUF_SIZE(&frame), &gc);

+ struct buffer encrypt_workspace =3D alloc_buf_gc(BUF= _SIZE(&frame), &gc);
+ struct buffer decrypt_workspace =3D alloc_buf_gc(BUF_SIZE(&= ;frame), &gc);
+ = struct buffer buf =3D clear_buf();
+ void *buf_p;
+
+ = /* init work */
+ = ASSERT(buf_init(&work, frame.buf.headroom));
+
+ init_implicit_iv(co);
+ update_time();
+
+ = /* Test encryption, decryption for all packet sizes */
+ for (int i =3D 1; i <=3D frame.bu= f.payload_size; ++i)
= + {
+
+ /* msg(M_INFO, "TES= TING ENCRYPT/DECRYPT of packet length=3D%d", i); */

+
+ /*
+ * Load src with random data.
+ */
+ ASSERT(buf_init(&src, 0));
+ ASSERT(i <=3D src.capacity= );
+ src.len = =3D i;
+ ASSER= T(rand_bytes(BPTR(&src), BLEN(&src)));
+
+ /* copy source to input buf */
+ buf =3D work;
+ buf_p =3D buf_write_alloc(&buf, BL= EN(&src));
+ = ASSERT(buf_p);
+ = memcpy(buf_p, BPTR(&src), BLEN(&src));
+
+ /* initialize work buffer with buf.headroom bytes of= prepend capacity */
= + ASSERT(buf_init(&encrypt_workspace, frame.buf.headroom));
+
+ /* encrypt */
+ openvpn_encrypt(&buf, encry= pt_workspace, co);
+<= /span>
+ /* decrypt *= /
+ openvpn_de= crypt(&buf, decrypt_workspace, co, &frame, BPTR(&buf));<= br>+
+ /* compare */
+ assert_int_equal(buf.len, src.len);<= /span>
+ assert_memor= y_equal(BPTR(&src), BPTR(&buf), i);
+
+ }
+ gc_fr= ee(&gc);
+}
+
+
+
+struct = crypto_options
+init_= crypto_options(const char *cipher, const char *auth)
+{
+ struct key2 key2 =3D { .n =3D 2};
+
+ ASSERT(rand_bytes(key2.keys[0].cipher, sizeof(key2.keys[= 0].cipher)));
+ AS= SERT(rand_bytes(key2.keys[0].hmac, sizeof(key2.keys[0].hmac)));
<= span style=3D"color: hsl(120, 100%, 40%);">+ ASSERT(rand_bytes(key2.keys= [1].cipher, sizeof(key2.keys[1].cipher)));

+ ASSERT(rand_bytes(key2.keys[1].hmac, sizeof(key2= .keys)[1].hmac));
+
+ struct crypto_opt= ions co =3D { 0 };
+<= /span>
+ struct key_type = kt =3D create_kt(cipher, auth, "ssl-test");
+
+ init_key_ctx_bi(&co.key_ctx_bi, &key2, 0, &k= t, "unit-test-ssl");
+ packet_id_init(&co.packet_id, 5, 5, "UNITTEST"= ;, 0);
+
+ return co;

+}
+
= +static void
+uninit_= crypto_options(struct crypto_options *co)
+{
+ packet_id_free(&co->packet_id);
+ free_key_ctx_bi(&co->key_ctx_bi);
+
+}
+
+
+static void
= +run_data_channel_with_cipher(c= onst char *cipher, const char *auth)
+{
+ = struct crypto_options co =3D init_crypto_options(cipher, auth);+ do_data_channel_round_tr= ip(&co);
+ uni= nit_crypto_options(&co);
+}
++static void
+test_data_channel_roundtrip_aes_128_= gcm(void **state)
+{<= /span>
+ run_data_channel= _with_cipher("AES-128-GCM", "none");
+}
+
+= static void
+test_dat= a_channel_roundtrip_aes_192_gcm(void **state)
+{
+ run_data_channel_with_cipher("AES-192-GCM", "non= e");
+}+
+static void
+test_data_channel_roundtrip_aes_256_gcm(void **state)
+{
+ run_data_channel_with_cipher("= AES-256-GCM", "none");
+}
+=
+static void<= br>+test_data_channel_roundtrip= _aes_128_cbc(void **state)
+{
+ run_dat= a_channel_with_cipher("AES-128-CBC", "SHA256");<= br>+}
+
+static void
+{
+ run_data_channel_with_cipher("AES-192-CBC"= , "SHA256");
+
+static void

+test_data_channel_roundtrip_aes_256_cbc(v= oid **state)
+{
+ run_data_channel_with= _cipher("AES-256-CBC", "SHA256");
+}
+
+sta= tic void
+test_data_c= hannel_roundtrip_chacha20_poly1305(void **state)
+{
+ if (!cipher_valid("ChaCha20-Poly1305"))
= + {
+ skip();
+ return;
+ }
+ run_data_channel_with_cipher("ChaCha20-Poly1305", &q= uot;none");
+}
+
+static void
+test_data_channel_roundtrip_bf_cbc(void **state)=
+{
+ if (!cipher_valid("BF-CBC&qu= ot;))
+ {+ skip();
+ return;

+ }
+ run_data_channel_with_cipher("BF-CBC"= ;, "SHA1");
+}
+
+

int
main(void)

{
openvpn_unit_test_se= tup();

const struct CMUnitTest tests= [] =3D {
- cmock= a_unit_test(crypto_pem_encode_certificate)
+ cmocka_unit_test(crypto_pem_encode_certifica= te),
+ cmocka_= unit_test(test_data_channel_roundtrip_aes_128_gcm),
+ cmocka_unit_test(test_data_channe= l_roundtrip_aes_192_gcm),
+ cmocka_unit_test(test_data_channel_roundtrip_aes_256_gcm),
+ cmocka_unit_te= st(test_data_channel_roundtrip_chacha20_poly1305),
+ cmocka_unit_test(test_data_channel_r= oundtrip_aes_128_cbc),
+ cmocka_unit_test(= test_data_channel_roundtrip_aes_256_cbc),
+ cmocka_unit_test(test_data_channel_roundtrip_= bf_cbc),
};

#if def= ined(ENABLE_CRYPTO_OPENSSL)
tls_init_lib();
= #endif

- int ret =3D cmocka_run_group_tests_name("crypto tests&qu= ot;, tests, NULL, NULL);
+ int ret =3D cmocka_run_group_tests_name("ssl tests", tes= ts, NULL, NULL);

#if defined(ENABLE_CRYP= TO_OPENSSL)
tls_free_lib();

To view, visit change 505. To unsubscribe, or for help writing mail fi= lters, visit settings.

=
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Ifa5ae96165d17b3cae4afc53e84= 4bb34d1610e58
Gerrit-Change-Number: 505
Gerrit-PatchSet: 4
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: newpatchset
--ZZxR+9cIwqo=--