From: "plaisthos (Code Review)" Date: Thu, 25 Jan 2024 12:38:17 +0000 References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <7b5f4c01bb8b841ba64349804e2d44ecd34178ec-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="0tzd33YRv7o="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Implement support for AEAD tag at the end List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: openvpn-devel --0tzd33YRv7o= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld. Hello flichtenheld, I'd like you to do a code review. Please visit http://gerrit.openvpn.net/c/openvpn/+/506?usp=3Demail to review the following change. Change subject: Implement support for AEAD tag at the end ...................................................................... Implement support for AEAD tag at the end Change-Id: I00821d75342daf3f813b829812d648fe298bea81 --- M src/openvpn/crypto.c M src/openvpn/crypto.h M src/openvpn/init.c M src/openvpn/options.c M src/openvpn/push.c M src/openvpn/ssl.h M tests/unit_tests/openvpn/test_ssl.c 7 files changed, 85 insertions(+), 26 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/06/506/1 diff --git a/src/openvpn/crypto.c b/src/openvpn/crypto.c index 2fca131..9988ebe 100644 --- a/src/openvpn/crypto.c +++ b/src/openvpn/crypto.c @@ -104,14 +104,10 @@ ASSERT(cipher_ctx_reset(ctx->cipher, iv)); } - /* Reserve space for authentication tag */ - mac_out =3D buf_write_alloc(&work, mac_len); - ASSERT(mac_out); - dmsg(D_PACKET_CONTENT, "ENCRYPT FROM: %s", format_hex(BPTR(buf), BLEN(= buf), 80, &gc)); /* Buffer overflow check */ - if (!buf_safe(&work, buf->len + cipher_ctx_block_size(ctx->cipher))) + if (!buf_safe(&work, buf->len + mac_len + cipher_ctx_block_size(ctx->c= ipher))) { msg(D_CRYPT_ERRORS, "ENCRYPT: buffer size error, bc=3D%d bo=3D%d bl=3D%d wc=3D%d w= o=3D%d wl=3D%d", @@ -121,9 +117,16 @@ } /* For AEAD ciphers, authenticate Additional Data, including opcode */ - ASSERT(cipher_ctx_update_ad(ctx->cipher, BPTR(&work), BLEN(&work) - ma= c_len)); + ASSERT(cipher_ctx_update_ad(ctx->cipher, BPTR(&work), BLEN(&work))); dmsg(D_PACKET_CONTENT, "ENCRYPT AD: %s", - format_hex(BPTR(&work), BLEN(&work) - mac_len, 0, &gc)); + format_hex(BPTR(&work), BLEN(&work), 0, &gc)); + + if (!(opt->flags & CO_AEAD_TAG_AT_THE_END)) + { + /* Reserve space for authentication tag */ + mac_out =3D buf_write_alloc(&work, mac_len); + ASSERT(mac_out); + } /* Encrypt packet ID, payload */ ASSERT(cipher_ctx_update(ctx->cipher, BEND(&work), &outlen, BPTR(buf),= BLEN(buf))); @@ -133,6 +136,14 @@ ASSERT(cipher_ctx_final(ctx->cipher, BEND(&work), &outlen)); ASSERT(buf_inc_len(&work, outlen)); + /* if the tag is at end the end, allocate it now */ + if (opt->flags & CO_AEAD_TAG_AT_THE_END) + { + /* Reserve space for authentication tag */ + mac_out =3D buf_write_alloc(&work, mac_len); + ASSERT(mac_out); + } + /* Write authentication tag */ ASSERT(cipher_ctx_get_tag(ctx->cipher, mac_out, mac_len)); =20 @@ -353,7 +364,6 @@ static const char error_prefix[] =3D "AEAD Decrypt error"; struct packet_id_net pin =3D { 0 }; const struct key_ctx *ctx =3D &opt->key_ctx_bi.decrypt; - uint8_t *tag_ptr =3D NULL; int outlen; struct gc_arena gc; @@ -406,19 +416,29 @@ /* keep the tag value to feed in later */ const int tag_size =3D OPENVPN_AEAD_TAG_LENGTH; - if (buf->len < tag_size) + if (buf->len < tag_size + 1) { - CRYPT_ERROR("missing tag"); + CRYPT_ERROR("missing tag or no payload"); } - tag_ptr =3D BPTR(buf); - ASSERT(buf_advance(buf, tag_size)); + + const int ad_size =3D BPTR(buf) - ad_start; + + uint8_t *tag_ptr =3D NULL; + int data_len =3D 0; + + if (opt->flags & CO_AEAD_TAG_AT_THE_END) + { + data_len =3D BLEN(buf) - tag_size; + tag_ptr =3D BPTR(buf) + data_len; + } + else + { + tag_ptr =3D BPTR(buf); + ASSERT(buf_advance(buf, tag_size)); + data_len =3D BLEN(buf); + } + dmsg(D_PACKET_CONTENT, "DECRYPT MAC: %s", format_hex(tag_ptr, tag_size= , 0, &gc)); - - if (buf->len < 1) - { - CRYPT_ERROR("missing payload"); - } - dmsg(D_PACKET_CONTENT, "DECRYPT FROM: %s", format_hex(BPTR(buf), BLEN(= buf), 0, &gc)); /* Buffer overflow check (should never fail) */ @@ -427,20 +447,19 @@ CRYPT_ERROR("potential buffer overflow"); } - { - /* feed in tag and the authenticated data */ - const int ad_size =3D BPTR(buf) - ad_start - tag_size; - ASSERT(cipher_ctx_update_ad(ctx->cipher, ad_start, ad_size)); - dmsg(D_PACKET_CONTENT, "DECRYPT AD: %s", - format_hex(BPTR(buf) - ad_size - tag_size, ad_size, 0, &gc)); - } + + /* feed in tag and the authenticated data */ + ASSERT(cipher_ctx_update_ad(ctx->cipher, ad_start, ad_size)); + dmsg(D_PACKET_CONTENT, "DECRYPT AD: %s", + format_hex(ad_start, ad_size, 0, &gc)); /* Decrypt and authenticate packet */ if (!cipher_ctx_update(ctx->cipher, BPTR(&work), &outlen, BPTR(buf), - BLEN(buf))) + data_len)) { CRYPT_ERROR("cipher update failed"); } + ASSERT(buf_inc_len(&work, outlen)); if (!cipher_ctx_final_check_tag(ctx->cipher, BPTR(&work) + outlen, &outlen, tag_ptr, tag_size)) diff --git a/src/openvpn/crypto.h b/src/openvpn/crypto.h index 4201524..95a5b31 100644 --- a/src/openvpn/crypto.h +++ b/src/openvpn/crypto.h @@ -279,6 +279,10 @@ /**< Bit-flag indicating that renegotiations are using tls-crypt * with a TLS-EKM derived key. */ +#define CO_AEAD_TAG_AT_THE_END (1<<8) + /**< Bit-flag indicating that the AEAD tag is at the end of the + * packet. + */ unsigned int flags; /**< Bit-flags determining behavior of * security operation functions. */ diff --git a/src/openvpn/init.c b/src/openvpn/init.c index c5cc154..cd37b36 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -2323,6 +2323,10 @@ { buf_printf(&out, " dyn-tls-crypt"); } + if (o->imported_protocol_flags & CO_AEAD_TAG_AT_THE_END) + { + buf_printf(&out, " aead-tag-end"); + } } if (buf_len(&out) > strlen(header)) diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 2c79a1e..39f00c0 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -8686,6 +8686,10 @@ options->imported_protocol_flags |=3D CO_USE_DYNAMIC_TLS_C= RYPT; } #endif + else if (streq(p[j], "aead-tag-end")) + { + options->imported_protocol_flags |=3D CO_AEAD_TAG_AT_THE_E= ND; + } else { msg(msglevel, "Unknown protocol-flags flag: %s", p[j]); diff --git a/src/openvpn/push.c b/src/openvpn/push.c index 2249434..e4c122c 100644 --- a/src/openvpn/push.c +++ b/src/openvpn/push.c @@ -29,6 +29,7 @@ #include "push.h" #include "options.h" +#include "crypto.h" #include "ssl.h" #include "ssl_verify.h" #include "ssl_ncp.h" @@ -686,6 +687,11 @@ buf_printf(&proto_flags, " dyn-tls-crypt"); } + if (o->imported_protocol_flags & CO_AEAD_TAG_AT_THE_END) + { + buf_printf(&proto_flags, " aead-tag-end"); + } + if (buf_len(&proto_flags) > 0) { push_option_fmt(gc, push_list, M_USAGE, "protocol-flags%s", buf_st= r(&proto_flags)); diff --git a/src/openvpn/ssl.h b/src/openvpn/ssl.h index 71b99db..a1c67a2 100644 --- a/src/openvpn/ssl.h +++ b/src/openvpn/ssl.h @@ -107,6 +107,9 @@ /** Support to dynamic tls-crypt (renegotiation with TLS-EKM derived tls-c= rypt key) */ #define IV_PROTO_DYN_TLS_CRYPT (1<<9) +/** Suport for the AEAD tag at the end a larger peer id and IV */ +#define IV_PROTO_DATA_V3 (1<<10) + /* Default field in X509 to be username */ #define X509_USERNAME_FIELD_DEFAULT "CN" diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn= /test_ssl.c index 8c1fb5b..0ded052 100644 --- a/tests/unit_tests/openvpn/test_ssl.c +++ b/tests/unit_tests/openvpn/test_ssl.c @@ -266,6 +266,19 @@ } +/* This adds a few more methods that strictly necessary but this allows + * us to see which exact test was run from the backtrace of the test + * when it fails */ + +static void +run_data_channel_with_cipher_end(const char *cipher) +{ + struct crypto_options co =3D init_crypto_options(cipher, "none"); + co.flags |=3D CO_AEAD_TAG_AT_THE_END; + do_data_channel_round_trip(&co); + uninit_crypto_options(&co); +} + static void run_data_channel_with_cipher(const char *cipher, const char *auth) { @@ -274,21 +287,25 @@ uninit_crypto_options(&co); } + static void test_data_channel_roundtrip_aes_128_gcm(void **state) { + run_data_channel_with_cipher_end("AES-128-GCM"); 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_end("AES-192-GCM"); 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_end("AES-256-GCM"); run_data_channel_with_cipher("AES-256-GCM", "none"); } @@ -318,6 +335,8 @@ skip(); return; } + + run_data_channel_with_cipher_end("ChaCha20-Poly1305"); run_data_channel_with_cipher("ChaCha20-Poly1305", "none"); } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/506?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: I00821d75342daf3f813b829812d648fe298bea81 Gerrit-Change-Number: 506 Gerrit-PatchSet: 1 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: flichtenheld Gerrit-MessageType: newchange --0tzd33YRv7o= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld.

plaisthos would like flichtenheld to review this change.

Vi= ew Change

Implement support for AEAD tag at the end

Change-Id: I= 00821d75342daf3f813b829812d648fe298bea81
---
M src/openvpn/crypto.cM src/openvpn/crypto.h
M src/openvpn/init.c
M src/openvpn/options.c=
M src/openvpn/push.c
M src/openvpn/ssl.h
M tests/unit_tests/openv= pn/test_ssl.c
7 files changed, 85 insertions(+), 26 deletions(-)

=
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/06/506/1
=
diff --git a/src/openvpn/crypto.c b/src/openvpn/crypto.c
index 2fca131..9988ebe 100644
--- a/src/openvpn/crypto.= c
+++ b/src/openvpn/crypto.c
@@ -104,14 +10= 4,10 @@
ASSERT(cipher_ctx_reset(ctx->cipher, iv= ));
}

- /* Reserve space for authentication tag */
- mac_out =3D buf_write_a= lloc(&work, mac_len);
- ASSERT(mac_out);
- if (!buf_safe(&work, buf->len + cipher_ctx_= block_size(ctx->cipher)))
+ if (!buf_safe(&work, buf->len + mac_len + cipher_ctx_bl= ock_size(ctx->cipher)))
{
= msg(D_CRYPT_ERRORS,
"ENCRYPT: buffer size= error, bc=3D%d bo=3D%d bl=3D%d wc=3D%d wo=3D%d wl=3D%d",
@@ -121,9 +117,16 @@

}
/* For AEAD ciphers, authenticate Additional Data, including op= code */
- ASSERT(cip= her_ctx_update_ad(ctx->cipher, BPTR(&work), BLEN(&work) - mac_le= n));
+ ASSERT(ciph= er_ctx_update_ad(ctx->cipher, BPTR(&work), BLEN(&work)));=
dmsg(D_PACKET_CONTENT, "ENCRYPT AD: %s",- format_hex(BPTR(&w= ork), BLEN(&work) - mac_len, 0, &gc));
+ format_hex(BPTR(&work), BLEN(&w= ork), 0, &gc));
+=
+ if (!(opt->f= lags & CO_AEAD_TAG_AT_THE_END))
+ {
+ /* Reserve space for authentication tag */
+ mac_out =3D buf_write_alloc(&= work, mac_len);
+ = ASSERT(mac_out);
= + }

/* Encrypt packet ID, payload= */
ASSERT(cipher_ctx_update(ctx->cipher, BEND(&= ;work), &outlen, BPTR(buf), BLEN(buf)));
@@ -133,6 +136= ,14 @@
ASSERT(cipher_ctx_final(ctx->cipher, BEND(&a= mp;work), &outlen));
ASSERT(buf_inc_len(&work,= outlen));

+ /* if the tag is at end the end, allocate it now */
= + if (opt->flags & CO= _AEAD_TAG_AT_THE_END)
+ {
+ /* R= eserve space for authentication tag */
+ mac_out =3D buf_write_alloc(&work, mac_len);=
+ ASSERT(mac_= out);
+ }+
/* Wri= te authentication tag */
ASSERT(cipher_ctx_get_tag(ctx= ->cipher, mac_out, mac_len));

@@ -353,= 7 +364,6 @@
static const char error_prefix[] =3D "= ;AEAD Decrypt error";
struct packet_id_net pin = =3D { 0 };
const struct key_ctx *ctx =3D &opt->= key_ctx_bi.decrypt;
- = uint8_t *tag_ptr =3D NULL;
int outlen;
struct gc_arena gc;

@@ -406,19 +4= 16,29 @@

/* keep the tag value to fe= ed in later */
const int tag_size =3D OPENVPN_AEAD_TAG= _LENGTH;
- if (buf-&= gt;len < tag_size)
+ if (buf->len < tag_size + 1)
{
<= span style=3D"color: hsl(0, 100%, 40%);">- CRYPT_ERROR("missing= tag");
+ = CRYPT_ERROR("missing tag or no payload");
}=
- tag_ptr =3D BPTR(= buf);
- ASSERT(buf_a= dvance(buf, tag_size));
+
+ const int a= d_size =3D BPTR(buf) - ad_start;
+
+ ui= nt8_t *tag_ptr =3D NULL;
+ int data_len =3D 0;
+
+ if (op= t->flags & CO_AEAD_TAG_AT_THE_END)
+ {
+ data_len =3D BLEN(buf) - tag_size;
+ tag_ptr =3D BPTR(buf) + data_len;

+ }
+ else
+ {
+ tag_ptr =3D BPTR(buf);
+ ASSERT(buf_advance(buf, tag_size));=
+ data_len =3D BLEN(= buf);
+ }+
dmsg(D= _PACKET_CONTENT, "DECRYPT MAC: %s", format_hex(tag_ptr, tag_size,= 0, &gc));
-=
- if (buf->len < 1)<= /span>
- {
- CRYPT_ERROR("missing payl= oad");
- }
-
dmsg= (D_PACKET_CONTENT, "DECRYPT FROM: %s", format_hex(BPTR(buf), BLEN= (buf), 0, &gc));

/* Buffer overf= low check (should never fail) */
@@ -427,20 +447,19 @@
CRYPT_ERROR("potential buffer overflow");
}

- {
= - /* feed in tag and the authenticated data */
- const int ad_size =3D BPTR(buf) - a= d_start - tag_size;
- = ASSERT(cipher_ctx_update_ad(ctx->cipher, ad_start, ad_size));
- dmsg(D_PACKET_CONT= ENT, "DECRYPT AD: %s",
- format_hex(BPTR(buf) - ad_size - tag_size, ad_size,= 0, &gc));
- }
+
+ /* feed in tag and the authenticated= data */
+ ASSERT(= cipher_ctx_update_ad(ctx->cipher, ad_start, ad_size));
+ dmsg(D_PACKET_CONTENT, "DECR= YPT AD: %s",
+ = format_hex(ad_start, ad_size, 0, &gc));
=
/* Decrypt and authenticate packet */
i= f (!cipher_ctx_update(ctx->cipher, BPTR(&work), &outlen, BPTR(bu= f),
- = BLEN(buf)))
= + data_len))
{
CRYPT_ERROR("cipher update failed");

= }
+
ASSERT(buf_inc_len(&work, outlen));

if (!= cipher_ctx_final_check_tag(ctx->cipher, BPTR(&work) + outlen,=
&outlen, tag_ptr, tag_si= ze))
diff --git a/src/openvpn/crypto.h b/src/openvpn/crypto= .h
index 4201524..95a5b31 100644
--- a/src/= openvpn/crypto.h
+++ b/src/openvpn/crypto.h
@@ -279,6 +279,10 @@

/**< Bit-flag indicating that= renegotiations are using tls-crypt
* with a TLS-EK= M derived key.
*/
+#define CO_AEAD_TAG_AT_THE_END (1<<8)
= + /**< Bit-flag indicatin= g that the AEAD tag is at the end of the
+ * packet.
+ */

unsigned i= nt flags; /**< Bit-flags determining behavior of
* security operation functions. */
diff --git a/src/openvpn/init.c b/src/openvpn/init.cindex c5cc154..cd37b36 100644
--- a/src/openvpn/init= .c
+++ b/src/openvpn/init.c
@@ -2323,6 +232= 3,10 @@
{
buf_printf(= &out, " dyn-tls-crypt");
}+ if (o->imported_pr= otocol_flags & CO_AEAD_TAG_AT_THE_END)
+ {
+ buf_printf(&out, " aead-tag-end");
+ }
<= span> }


if (buf_len(&out) &g= t; strlen(header))
diff --git a/src/openvpn/options.c b/src= /openvpn/options.c
index 2c79a1e..39f00c0 100644
= --- a/src/openvpn/options.c
+++ b/src/openvpn/options= .c
@@ -8686,6 +8686,10 @@
= options->imported_protocol_flags |=3D CO_USE_DYNAMIC_TLS_CRYPT; }
#endif
+ else if (streq(p[j], "aead-tag-= end"))
+ = {
+ = options->imported_protocol_flags |=3D CO_AEAD_TAG_AT_THE_END;+ }
else

{
= msg(msglevel, "Unknown protocol-flags flag: %s", p[j]);<= /span>
diff --git a/src/openvpn/push.c b/src/openvpn/push.c=
index 2249434..e4c122c 100644
--- a/src/openvpn/p= ush.c
+++ b/src/openvpn/push.c
@@ -29,6 +29= ,7 @@

#include "push.h"=
#include "options.h"
+#include "crypto.h"
#inclu= de "ssl.h"
#include "ssl_verify.h"
#include "ssl_ncp.h"
@@ -686,6 +687= ,11 @@
buf_printf(&proto_flags, " dyn-tls= -crypt");
}

+ if (o->imported_protocol_flags &= amp; CO_AEAD_TAG_AT_THE_END)
+ {
+ = buf_printf(&proto_flags, " aead-tag-end");
+ }

+
if (buf_len(&proto_flag= s) > 0)
{
push_option_fmt(= gc, push_list, M_USAGE, "protocol-flags%s", buf_str(&proto_fl= ags));
diff --git a/src/openvpn/ssl.h b/src/openvpn/ssl.h
index 71b99db..a1c67a2 100644
--- a/src/open= vpn/ssl.h
+++ b/src/openvpn/ssl.h
@@ -107,6= +107,9 @@
/** Support to dynamic tls-crypt (renegotiation= with TLS-EKM derived tls-crypt key) */
#define IV_PROTO_D= YN_TLS_CRYPT (1<<9)

+/** Suport for the AEAD tag at the end a larger p= eer id and IV */
+#de= fine IV_PROTO_DATA_V3 (1<<10)
+
/* Default field in X509 to be use= rname */
#define X509_USERNAME_FIELD_DEFAULT "CN"= ;

diff --git a/tests/unit_tests/openvpn/t= est_ssl.c b/tests/unit_tests/openvpn/test_ssl.c
index 8c1fb= 5b..0ded052 100644
--- a/tests/unit_tests/openvpn/test_ssl.= c
+++ b/tests/unit_tests/openvpn/test_ssl.c
@@ -266,6 +266,19 @@

}

=
+/* This adds a few= more methods that strictly necessary but this allows
+ * us to see which exact test was run fro= m the backtrace of the test
+ * when it fails */
+
+static void=
+run_data_channel_wi= th_cipher_end(const char *cipher)
+{
+ = struct crypto_options co =3D init_crypto_options(cipher, "none");=
+ co.flags |=3D C= O_AEAD_TAG_AT_THE_END;
+ uninit_crypto_options(&co);
+}

+
static void
run_da= ta_channel_with_cipher(const char *cipher, const char *auth)
{

@@ -274,21 +287,25 @@
uninit_cryp= to_options(&co);
}

+
static void<= br> test_data_channel_roundtrip_aes_128_gcm(void **state)
<= span> {

+ run_data= _channel_with_cipher_end("AES-128-GCM");
run= _data_channel_with_cipher("AES-128-GCM", "none");
}

static void
test_data_channel_roundtrip_aes_192_gcm(void **state)

{=
+ run_data_channe= l_with_cipher_end("AES-192-GCM");
run_data_c= hannel_with_cipher("AES-192-GCM", "none");
}

static void
test= _data_channel_roundtrip_aes_256_gcm(void **state)
{=
+ run_data_channel_with_= cipher_end("AES-256-GCM");
run_data_channel_= with_cipher("AES-256-GCM", "none");
}<= /span>

@@ -318,6 +335,8 @@
= skip();
return;
}<= br>+
+ run_data_channel_with_cipher_end("Cha= Cha20-Poly1305");
run_data_channel_with_cipher(&q= uot;ChaCha20-Poly1305", "none");
}

To view, visit change 506. To unsu= bscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I00821d75342daf3f813b829812d= 648fe298bea81
Gerrit-Change-Number: 506
Gerrit-PatchSet: 1
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newchange
--0tzd33YRv7o=--