From: "cron2 (Code Review)" Date: Thu, 13 Feb 2025 21:27:25 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., max@...2606... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="HppJgNW4sFs="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Extend the unit test for data channel packets with aead limit tests List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos , flichtenheld , MaxF Cc: openvpn-devel --HppJgNW4sFs= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#5) to the change originally created by= plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/868?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by MaxF Change subject: Extend the unit test for data channel packets with aead lim= it tests ...................................................................... Extend the unit test for data channel packets with aead limit tests Change-Id: I15c7cfdddb06d4530d669b222a3c65db5169b29a Signed-off-by: Arne Schwabe Acked-by: MaxF Message-Id: <20250213193942.26423-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg30= 864.html Signed-off-by: Gert Doering --- M tests/unit_tests/openvpn/test_ssl.c 1 file changed, 101 insertions(+), 2 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/68/868/5 diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn= /test_ssl.c index 486e298..c383034 100644 --- a/tests/unit_tests/openvpn/test_ssl.c +++ b/tests/unit_tests/openvpn/test_ssl.c @@ -363,14 +363,108 @@ /* compare */ assert_int_equal(buf.len, src.len); assert_memory_equal(BPTR(&src), BPTR(&buf), i); - } gc_free(&gc); } +static void +encrypt_one_packet(struct crypto_options *co, int len) +{ + struct frame frame; + init_frame_parameters(&frame); + + struct gc_arena gc =3D gc_new(); + 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 work =3D alloc_buf_gc(BUF_SIZE(&frame), &gc); + struct buffer buf =3D clear_buf(); + struct buffer src =3D alloc_buf_gc(frame.buf.payload_size, &gc); + void *buf_p; + + ASSERT(buf_init(&work, frame.buf.headroom)); + + /* + * Load src with random data. + */ + ASSERT(buf_init(&src, 0)); + ASSERT(len <=3D src.capacity); + src.len =3D len; + 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)); + + ASSERT(buf_init(&encrypt_workspace, frame.buf.headroom)); + 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), len); + + gc_free(&gc); +} -struct crypto_options +static void +check_aead_limits(struct crypto_options *co, bool chachapoly) +{ + + /* Check that we correctly react when we have a nearing AEAD limits */ + + /* manually increase the send counter to be past + * the GCM usage limit */ + co->key_ctx_bi.encrypt.plaintext_blocks =3D 0x1ull << 40; + + + bool epoch =3D (co->flags & CO_EPOCH_DATA_KEY_FORMAT); + + int expected_epoch =3D epoch ? 4 : 0; + + /* Ensure that we are still on the initial key (our init_crypto_options + * unit test method iterates the initial key to 4) or that it is 0 when + * epoch is not in use + */ + assert_int_equal(co->key_ctx_bi.encrypt.epoch, expected_epoch); + + encrypt_one_packet(co, 1000); + + /* either epoch key has been updated or warning is enabled */ + if (epoch && !chachapoly) + { + expected_epoch++; + } + + assert_int_equal(co->key_ctx_bi.encrypt.epoch, expected_epoch); + + if (!epoch) + { + /* Check always against the GCM usage limit here to see if that + * check works */ + assert_true(aead_usage_limit_reached((1ull << 36), + &co->key_ctx_bi.encrypt, + co->packet_id.send.id)); + return; + } + + /* Move to the end of the epoch data key send PID range, ChachaPoly + * should now also move to a new epoch data key */ + co->packet_id.send.id =3D PACKET_ID_EPOCH_MAX; + + encrypt_one_packet(co, 1000); + encrypt_one_packet(co, 1000); + + expected_epoch++; + assert_int_equal(co->key_ctx_bi.encrypt.epoch, expected_epoch); +} + + +static struct crypto_options init_crypto_options(const char *cipher, const char *auth, bool epoch, struct key2 *statickey) { @@ -428,16 +522,21 @@ static void run_data_channel_with_cipher_epoch(const char *cipher) { + bool ischacha =3D !strcmp(cipher, "ChaCha20-Poly1305"); + struct crypto_options co =3D init_crypto_options(cipher, "none", true,= NULL); do_data_channel_round_trip(&co); + check_aead_limits(&co, ischacha); uninit_crypto_options(&co); } static void run_data_channel_with_cipher(const char *cipher, const char *auth) { + bool ischacha =3D !strcmp(cipher, "ChaCha20-Poly1305"); struct crypto_options co =3D init_crypto_options(cipher, auth, false, = NULL); do_data_channel_round_trip(&co); + check_aead_limits(&co, ischacha); uninit_crypto_options(&co); } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/868?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: I15c7cfdddb06d4530d669b222a3c65db5169b29a Gerrit-Change-Number: 868 Gerrit-PatchSet: 5 Gerrit-Owner: plaisthos Gerrit-Reviewer: MaxF Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-MessageType: newpatchset --HppJgNW4sFs= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

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

View Change

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

Extend the unit test for data channel packets with ae=
ad limit tests

Change-Id: I15c7cfdddb06d4530d669b222a3c65db5169b29a<= br>Signed-off-by: Arne Schwabe <arne@...1227...>
Acked-by: MaxF &l= t;max@...2606...>
Message-Id: <20250213193942.26423-1-gert@...1726...= ...>
URL: https://www.mail-archive.com/openvpn-devel@...1483...= e.net/msg30864.html
Signed-off-by: Gert Doering <gert@...1296...><= br>---
M tests/unit_tests/openvpn/test_ssl.c
1 file changed, 101 inse= rtions(+), 2 deletions(-)

git pull ssh://gerrit.openvpn.net:29418=
/openvpn refs/changes/68/868/5
diff --git a/tests/unit_tests/openvp=
n/test_ssl.c b/tests/unit_tests/openvpn/test_ssl.c
index 48= 6e298..c383034 100644
--- a/tests/unit_tests/openvpn/test_s= sl.c
+++ b/tests/unit_tests/openvpn/test_ssl.c
@@ -363,14 +363,108 @@
/* compare */ assert_int_equal(buf.len, src.len);
= assert_memory_equal(BPTR(&src), BPTR(&buf), i);
-
}
gc_free(&gc);
}

+static void
+encrypt_one_packet(struct crypto_options = *co, int len)
+{

+ struct frame frame;<= /span>
+ init_frame_param= eters(&frame);
+<= /span>
+ struct gc_arena = gc =3D gc_new();
+ = struct buffer encrypt_workspace =3D alloc_buf_gc(BUF_SIZE(&frame), &am= p;gc);
+ struct bu= ffer decrypt_workspace =3D alloc_buf_gc(BUF_SIZE(&frame), &gc);
+ struct buffer work = =3D alloc_buf_gc(BUF_SIZE(&frame), &gc);
+ struct buffer buf =3D clear_buf();<= br>+ struct buffer src =3D a= lloc_buf_gc(frame.buf.payload_size, &gc);
+ void *buf_p;
+
+ ASSERT(buf_init(&work, frame.buf.headroom));
+
+ /*
+ * Load src with random data.
+ */
+ ASSERT(buf_init(&src, 0));
+ ASSERT(len <=3D src.capacity);
+ src.len =3D len;

+ ASSERT(rand_bytes(BPTR(&sr= c), BLEN(&src)));

+
+ /* copy sourc= e to input buf */
+ = buf =3D work;
+ = buf_p =3D buf_write_alloc(&buf, BLEN(&src));
+ ASSERT(buf_p);
+ memcpy(buf_p, BPTR(&src), BLEN(&a= mp;src));
++ ASSERT(buf_init(&encr= ypt_workspace, frame.buf.headroom));
+ openvpn_encrypt(&buf, encrypt_workspace, co);
+
+ /* decrypt */
+ openvpn_decrypt(&buf, decrypt_wor= kspace, co, &frame, BPTR(&buf));
+
+ /* compare */
+= assert_int_equal(buf.len, src.len);
+ assert_memory_equal(BPTR(&src), BPTR(&buf),= len);
+
+ gc_free(&gc);

= +}

=
-struct crypto_= options
+static void<= /span>
+check_aead_limits(st= ruct crypto_options *co, bool chachapoly)
+{
+
+ /* Check th= at we correctly react when we have a nearing AEAD limits */
+

+ /* manually increase the send counter to be past
+ * the GCM usage = limit */
+ co->= key_ctx_bi.encrypt.plaintext_blocks =3D 0x1ull << 40;
+

+
+ bool epoch =3D (co->flags & CO_EPOCH_DATA_KEY_FORMAT);<= br>+
+ int expected_epoch =3D epoch ? 4 : 0;
+
+ /* Ensure that we are still on the in= itial key (our init_crypto_options
+ * unit test method iterates the initial key to 4) or th= at it is 0 when
+ = * epoch is not in use
+ asser= t_int_equal(co->key_ctx_bi.encrypt.epoch, expected_epoch);
+

+ encrypt_one_packet(co, 1000);
+
+ /* either epoch key has been updated or warning is en= abled */
+ if (epo= ch && !chachapoly)
+ {
+ = expected_epoch++;
+ = }
+
+ assert_int_equal(co->key_ct= x_bi.encrypt.epoch, expected_epoch);

+
+ = if (!epoch)
+ {<= /span>
+ /* Check alw= ays against the GCM usage limit here to see if that
+ * check works */
+ assert_true(aead_usage_limi= t_reached((1ull << 36),

+ &co->key_ctx_= bi.encrypt,
+ = co->packet_id.send.id));
= + return;
+ }
+
+ /* Move to the end of the epoch data key send PID range, Chacha= Poly
+ * should n= ow also move to a new epoch data key */
+ co->packet_id.send.id =3D PACKET_ID_EPOCH_MAX;
+
+ encrypt_one_packet(co, 1000);=
+ encrypt_one_packet(co,= 1000);
+
<= span style=3D"color: hsl(120, 100%, 40%);">+ expected_epoch++;
+ assert_int_equal(co->k= ey_ctx_bi.encrypt.epoch, expected_epoch);
+}
+
+
+static struct crypto_options

init_crypto_options(const char *cipher, const char *auth, bool = epoch,
struct key2 *statickey)<= br> {
@@ -428,16 +522,21 @@
static v= oid
run_data_channel_with_cipher_epoch(const char *cipher)=
{
+= bool ischacha =3D !strcmp(cipher, "ChaCha20-Poly1305");
+
str= uct crypto_options co =3D init_crypto_options(cipher, "none", tru= e, NULL);
do_data_channel_round_trip(&co);<= br>+ check_aead_limits(&= co, ischacha);
uninit_crypto_options(&co);<= br> }

static void
= run_data_channel_with_cipher(const char *cipher, const char *auth)<= br> {
+ bool= ischacha =3D !strcmp(cipher, "ChaCha20-Poly1305");
struct crypto_options co =3D init_crypto_options(cipher, auth, fals= e, NULL);

do_data_channel_round_trip(&co);<= br>+ check_aead_limits(&= co, ischacha);
uninit_crypto_options(&co);<= br> }


To view, = visit cha= nge 868. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I15c7cfdddb06d4530d669b222a3= c65db5169b29a
Gerrit-Change-Number: 868
Gerrit-PatchSet: 5
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: MaxF <max@...2606...> <= /div>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: newpatchset
--HppJgNW4sFs=--