From: "cron2 (Code Review)" Date: Mon, 23 Dec 2024 09:28:38 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <25370394eee36ac26712499971b436ac6fa7ef37-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="TKbkRdTeZfA="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Move initialisation of implicit IVs to init_key_ctx_bi methods List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: plaisthos Cc: flichtenheld , openvpn-devel --TKbkRdTeZfA= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has submitted this change. ( http://gerrit.openvpn.net/c/openvpn/+/80= 0?usp=3Demail ) Change subject: Move initialisation of implicit IVs to init_key_ctx_bi meth= ods ...................................................................... Move initialisation of implicit IVs to init_key_ctx_bi methods This is really more a function of initialising the data cipher and key context and putting it into the init_key_ctx_bi makes more sense. It will allow calling init_key_ctx_bi to fully initialise a data channel key without calling some extra functions after that which will make the (upcoming) epoch key implementation cleaner. Also ensure that free_ctx_bi actually also sets initialized to false. Change-Id: Id223612c7bcab91d49c013fb775024bd64ab0836 Signed-off-by: Arne Schwabe Acked-by: Gert Doering Message-Id: <20241222214541.11021-1-gert@...1296...> URL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg30= 170.html Signed-off-by: Gert Doering --- M src/openvpn/crypto.c M src/openvpn/ssl.c M tests/unit_tests/openvpn/test_ssl.c 3 files changed, 30 insertions(+), 59 deletions(-) diff --git a/src/openvpn/crypto.c b/src/openvpn/crypto.c index 982fb01..da503b3 100644 --- a/src/openvpn/crypto.c +++ b/src/openvpn/crypto.c @@ -891,6 +891,33 @@ } } +/** + * Update the implicit IV for a key_ctx_bi based on TLS session ids and ci= pher + * used. + * + * Note that the implicit IV is based on the HMAC key, but only in AEAD mo= des + * where the HMAC key is not used for an actual HMAC. + * + * @param ctx Encrypt/decrypt key context + * @param key key, hmac part used to calculate implicit = IV + */ +static void +key_ctx_update_implicit_iv(struct key_ctx *ctx, const struct key *key) +{ + /* Only use implicit IV in AEAD cipher mode, where HMAC key is not use= d */ + if (cipher_ctx_mode_aead(ctx->cipher)) + { + size_t impl_iv_len =3D 0; + ASSERT(cipher_ctx_iv_length(ctx->cipher) >=3D OPENVPN_AEAD_MIN_IV_= LEN); + impl_iv_len =3D cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_= id_type); + ASSERT(impl_iv_len + sizeof(packet_id_type) <=3D OPENVPN_MAX_IV_LE= NGTH); + ASSERT(impl_iv_len <=3D MAX_HMAC_KEY_LENGTH); + CLEAR(ctx->implicit_iv); + /* The first bytes of the IV are filled with the packet id */ + memcpy(ctx->implicit_iv + sizeof(packet_id_type), key->hmac, impl_= iv_len); + } +} + /* given a key and key_type, build a key_ctx */ void init_key_ctx(struct key_ctx *ctx, const struct key *key, @@ -949,7 +976,7 @@ snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", name); init_key_ctx(ctx, &key2->keys[kds.out_key], kt, OPENVPN_OP_ENCRYPT, log_prefix); - + key_ctx_update_implicit_iv(ctx, &key2->keys[kds.out_key]); } void @@ -964,7 +991,7 @@ snprintf(log_prefix, sizeof(log_prefix), "Incoming %s", name); init_key_ctx(ctx, &key2->keys[kds.in_key], kt, OPENVPN_OP_DECRYPT, log_prefix); - + key_ctx_update_implicit_iv(ctx, &key2->keys[kds.in_key]); } void @@ -999,6 +1026,7 @@ { free_key_ctx(&ctx->encrypt); free_key_ctx(&ctx->decrypt); + ctx->initialized =3D false; } static bool diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index c77c4ed..a222d5b 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -96,21 +96,6 @@ #endif /* ifdef MEASURE_TLS_HANDSHAKE_STATS */ /** - * Update the implicit IV for a key_ctx_bi based on TLS session ids and ci= pher - * used. - * - * Note that the implicit IV is based on the HMAC key, but only in AEAD mo= des - * where the HMAC key is not used for an actual HMAC. - * - * @param ctx Encrypt/decrypt key context - * @param key HMAC key, used to calculate implicit IV - * @param key_len HMAC key length - */ -static void -key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_l= en); - - -/** * Limit the reneg_bytes value when using a small-block (<128 bytes) ciphe= r. * * @param cipher The current cipher (may be NULL). @@ -1411,12 +1396,6 @@ else { init_key_ctx_bi(key, key2, key_direction, key_type, "Data Channel"= ); - /* Initialize implicit IVs */ - key_ctx_update_implicit_iv(&key->encrypt, key2->keys[(int)server].= hmac, - MAX_HMAC_KEY_LENGTH); - key_ctx_update_implicit_iv(&key->decrypt, - key2->keys[1 - (int)server].hmac, - MAX_HMAC_KEY_LENGTH); } } @@ -1553,23 +1532,6 @@ return ret; } -static void -key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_l= en) -{ - /* Only use implicit IV in AEAD cipher mode, where HMAC key is not use= d */ - if (cipher_ctx_mode_aead(ctx->cipher)) - { - size_t impl_iv_len =3D 0; - ASSERT(cipher_ctx_iv_length(ctx->cipher) >=3D OPENVPN_AEAD_MIN_IV_= LEN); - impl_iv_len =3D cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_= id_type); - ASSERT(impl_iv_len + sizeof(packet_id_type) <=3D OPENVPN_MAX_IV_LE= NGTH); - ASSERT(impl_iv_len <=3D key_len); - CLEAR(ctx->implicit_iv); - /* The first bytes of the IV are filled with the packet id */ - memcpy(ctx->implicit_iv + sizeof(packet_id_type), key, impl_iv_len= ); - } -} - /** * Generate data channel keys for the supplied TLS session. * diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn= /test_ssl.c index ae33cc6..caacd9e 100644 --- a/tests/unit_tests/openvpn/test_ssl.c +++ b/tests/unit_tests/openvpn/test_ssl.c @@ -277,24 +277,6 @@ #endif /* HAVE_OPENSSL_STORE */ } -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)) - { - 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)); - - memcpy(co->key_ctx_bi.decrypt.implicit_iv, - co->key_ctx_bi.encrypt.implicit_iv, OPENVPN_MAX_IV_LENGTH); - } -} static void init_frame_parameters(struct frame *frame) @@ -346,7 +328,6 @@ /* init work */ ASSERT(buf_init(&work, frame.buf.headroom)); - init_implicit_iv(co); update_time(); /* Test encryption, decryption for all packet sizes */ -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/800?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: Id223612c7bcab91d49c013fb775024bd64ab0836 Gerrit-Change-Number: 800 Gerrit-PatchSet: 9 Gerrit-Owner: plaisthos Gerrit-Reviewer: cron2 Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-MessageType: merged --TKbkRdTeZfA= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

cron2 submitted this change.

View Change

Move initialisation of implicit IVs to init_ke=
y_ctx_bi methods

This is really more a function of initialising the = data cipher and key
context and putting it into the init_key_ctx_bi make= s more sense.

It will allow calling init_key_ctx_bi to fully initial= ise a
data channel key without calling some extra functions after thatwhich will make the (upcoming) epoch key implementation cleaner.

A= lso ensure that free_ctx_bi actually also sets initialized to false.
Change-Id: Id223612c7bcab91d49c013fb775024bd64ab0836
Signed-off-by: Arn= e Schwabe <arne@...1227...>
Acked-by: Gert Doering <gert@...1826...= 6...>
Message-Id: <20241222214541.11021-1-gert@...1296...>
U= RL: https://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg301= 70.html
Signed-off-by: Gert Doering <gert@...1296...>
---
M = src/openvpn/crypto.c
M src/openvpn/ssl.c
M tests/unit_tests/openvpn/t= est_ssl.c
3 files changed, 30 insertions(+), 59 deletions(-)

diff --git a/src/openvpn/crypto.c b/src/openvpn/crypto.c
index 982fb01..da503b3 100644
--- a/src/openvpn/crypto.c
+++ b/src/openvpn/crypto.c
@@ -891,6 +891,33= @@
}
}

<= span style=3D"color: hsl(120, 100%, 40%);">+/**

+ * Update the implicit IV for a key_ctx_bi base= d on TLS session ids and cipher
+ * used.
= + *
+ * Note that the= implicit IV is based on the HMAC key, but only in AEAD modes
+ * where the HMAC key is not used= for an actual HMAC.
= + *
+ * @param ctx = Encrypt/decrypt key context
+ * @param key key, hmac part used= to calculate implicit IV
+ */
+static voi= d
+key_ctx_update_imp= licit_iv(struct key_ctx *ctx, const struct key *key)
+{
+ /* Only use implicit IV in AEAD cipher mode, where HMAC= key is not used */
+= if (cipher_ctx_mode_aead(ctx->cipher))
+ {
+ size_t impl_iv_len =3D 0;
+ ASSERT(cipher_ctx_iv_length(ctx->ciphe= r) >=3D OPENVPN_AEAD_MIN_IV_LEN);
+ impl_iv_len =3D cipher_ctx_iv_length(ctx->ciphe= r) - sizeof(packet_id_type);
+ ASSERT(impl_iv_len + sizeof(packet_id_type) <=3D OPENVP= N_MAX_IV_LENGTH);
+ = ASSERT(impl_iv_len <=3D MAX_HMAC_KEY_LENGTH);
+ CLEAR(ctx->implicit_iv);
+ /* The first byt= es of the IV are filled with the packet id */
+ memcpy(ctx->implicit_iv + sizeof(packe= t_id_type), key->hmac, impl_iv_len);
+ }
+}
+
<= span> /* given a key and key_type, build a key_ctx */

void=
init_key_ctx(struct key_ctx *ctx, const struct key *key,<= /span>
@@ -949,7 +976,7 @@
snprintf(log_prefi= x, sizeof(log_prefix), "Outgoing %s", name);
= init_key_ctx(ctx, &key2->keys[kds.out_key], kt,
= OPENVPN_OP_ENCRYPT, log_prefix);
-
+ key_ctx_update_implicit_iv(ctx, &key2->keys[kds.out_key])= ;
}

void
@@ -964,7 +991,7 @@

snprintf(log_prefix, sizeof(log_= prefix), "Incoming %s", name);
init_key_ctx(= ctx, &key2->keys[kds.in_key], kt,
= OPENVPN_OP_DECRYPT, log_prefix);
-
+ key_= ctx_update_implicit_iv(ctx, &key2->keys[kds.in_key]);
}

void
@@ -999,6 +102= 6,7 @@
{
free_key_ctx(&ctx->en= crypt);
free_key_ctx(&ctx->decrypt);
= + ctx->initialized =3D fa= lse;
}

static bool

diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c
index c77c4ed..a222d5b 100644
--- a/src/openvpn/ssl.c
+++ b/src/openvpn/ssl.c
@@ -96,21 +96,6 @@
#endif /* ifdef MEASURE_TLS_HANDSHAKE_STATS */

/**
- * Update the implicit IV for a key_ctx_bi based on TLS session ids an= d cipher
- * used.
- *
- * Note that the implicit IV is based on th= e HMAC key, but only in AEAD modes
- * where the HMAC key is not used for an actual HMAC.- *
- * @param ctx Encrypt/decrypt k= ey context
- * @param k= ey HMAC key, used to calculate implicit IV
- * @param key_len HMAC= key length

- */=
-static void
-key_ctx_update_implicit_iv(struct key_= ctx *ctx, uint8_t *key, size_t key_len);
-
-
-/**
*= Limit the reneg_bytes value when using a small-block (<128 bytes) ciphe= r.
*
* @param cipher The current= cipher (may be NULL).
@@ -1411,12 +1396,6 @@
else

{
init_key_ctx_b= i(key, key2, key_direction, key_type, "Data Channel");
= - /* Initialize implicit I= Vs */
- key_ctx_= update_implicit_iv(&key->encrypt, key2->keys[(int)server].hmac,
- = MAX_HMAC_KEY_LENGTH);
- key_ctx_update_implicit_iv(&key->decrypt,=
- = key2->keys[1 - (int)server].hmac,
- MAX_HMAC_KEY_LENGT= H);
}
}

<= span>@@ -1553,23 +1532,6 @@

return ret;
}


= -static void
-key_ctx_u= pdate_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len)=
-{
- /* Only use implicit IV in AEAD cipher mode,= where HMAC key is not used */
- if (cipher_ctx_mode_aead(ctx->cipher))
- {
- size_t impl_iv_len =3D 0;
- ASSERT(cipher_ctx_iv_length(ctx->ci= pher) >=3D OPENVPN_AEAD_MIN_IV_LEN);
- impl_iv_len =3D cipher_ctx_iv_length(ctx->ciph= er) - sizeof(packet_id_type);
- ASSERT(impl_iv_len + sizeof(packet_id_type) <=3D OPENVPN= _MAX_IV_LENGTH);
- = ASSERT(impl_iv_len <=3D key_len);
- CLEAR(ctx->implicit_iv);
- /* The first bytes of the IV are fi= lled with the packet id */
- memcpy(ctx->implicit_iv + sizeof(packet_id_type), key, impl= _iv_len);
- }=
-}
-
/**
* Genera= te data channel keys for the supplied TLS session.
*
diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_t= ests/openvpn/test_ssl.c
index ae33cc6..caacd9e 100644

--- a/tests/unit_tests/openvpn/test_ssl.c
+++ b/= tests/unit_tests/openvpn/test_ssl.c
@@ -277,24 +277,6 @@
#endif /* HAVE_OPENSSL_STORE */
}
-static void
-init_implicit_iv(struct= crypto_options *co)
-{=
- cipher_ctx_t *cip= her =3D co->key_ctx_bi.encrypt.cipher;
-
- = if (cipher_ctx_mode_aead(cipher))
- {
- = ASSERT(cipher_ctx_iv_length(cipher) <=3D OPENVPN_MAX_IV_LENGTH);<= /span>
- 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));
-
- memcpy(co->key_ctx_bi.decrypt.implicit_iv,
- co->key_ctx_bi.en= crypt.implicit_iv, OPENVPN_MAX_IV_LENGTH);

- }
-}

static void
init= _frame_parameters(struct frame *frame)
@@ -346,7 +328,6 @@<= /span>
/* init work */
ASSERT(buf_init(&= amp;work, frame.buf.headroom));

- init_implicit_iv(co);
= update_time();

/* Test encryption, d= ecryption for all packet sizes */

To vi= ew, visit change 800. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id223612c7bcab91d49c013fb775= 024bd64ab0836
Gerrit-Change-Number: 800
Gerrit-PatchSet: 9
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: cron2 <gert@...1296...>=
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-MessageType: merged
--TKbkRdTeZfA=--