From: "cron2 (Code Review)" Date: Mon, 23 Dec 2024 09:28:35 +0000 In-Reply-To: References: Reply-To: gert@...1296..., arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <25370394eee36ac26712499971b436ac6fa7ef37-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="8HSGQfpAXX8="; 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 , flichtenheld Cc: openvpn-devel --8HSGQfpAXX8= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable cron2 has uploaded a new patch set (#9) to the change originally created by= plaisthos. ( http://gerrit.openvpn.net/c/openvpn/+/800?usp=3Demail ) The following approvals got outdated and were removed: Code-Review+2 by cron2 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(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/00/800/9 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 */ } =20 -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: newpatchset --8HSGQfpAXX8= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

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

View Change

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

Move initialisation of implicit IVs to init_key_ctx_=
bi methods

This is really more a function of initialising the data c= ipher 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<= br>data channel key without calling some extra functions after that
whic= h will make the (upcoming) epoch key implementation cleaner.

Also en= sure that free_ctx_bi actually also sets initialized to false.

Chang= e-Id: Id223612c7bcab91d49c013fb775024bd64ab0836
Signed-off-by: Arne Schw= abe <arne@...1227...>
Acked-by: Gert Doering <gert@...1296...&g= t;
Message-Id: <20241222214541.11021-1-gert@...1296...>
URL: ht= tps://www.mail-archive.com/openvpn-devel@lists.sourceforge.net/msg30170.htm= l
Signed-off-by: Gert Doering <gert@...1296...>
---
M src/op= envpn/crypto.c
M src/openvpn/ssl.c
M tests/unit_tests/openvpn/test_ss= l.c
3 files changed, 30 insertions(+), 59 deletions(-)

git pul= l ssh://gerrit.openvpn.net:29418/openvpn refs/changes/00/800/9
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 TL= S session ids and cipher
+ * used.
+ *
+ * Note that the implic= it 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 cal= culate implicit IV
+ = */
+static void
+key_ctx_update_implicit_i= v(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->cipher) >= =3D OPENVPN_AEAD_MIN_IV_LEN);
+ impl_iv_len =3D cipher_ctx_iv_length(ctx->cipher) - si= zeof(packet_id_type);
+ ASSERT(impl_iv_len + sizeof(packet_id_type) <=3D OPENVPN_MAX_I= V_LENGTH);
+ A= SSERT(impl_iv_len <=3D MAX_HMAC_KEY_LENGTH);
+ CLEAR(ctx->implicit_iv);
<= span style=3D"color: hsl(120, 100%, 40%);">+ /* The first bytes of t= he IV are filled with the packet id */

+ memcpy(ctx->implicit_iv + sizeof(packet_id_ty= pe), 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,<= br>@@ -949,7 +976,7 @@
snprintf(log_prefix, size= of(log_prefix), "Outgoing %s", name);
init_k= ey_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
@@ -9= 64,7 +991,7 @@
snprintf(log_prefix, sizeof(log_prefix)= , "Incoming %s", name);
init_key_ctx(ctx, &a= mp;key2->keys[kds.in_key], kt,
OPENVPN= _OP_DECRYPT, log_prefix);
-
+ key_ctx_upd= ate_implicit_iv(ctx, &key2->keys[kds.in_key]);
}

void
@@ -999,6 +1026,7 @@<= /span>
{
free_key_ctx(&ctx->encrypt);=
free_key_ctx(&ctx->decrypt);
+ ctx->initialized =3D false;
}

static bool
<= span>diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c

ind= ex 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 ciphe= r
- * used.
<= span style=3D"color: hsl(0, 100%, 40%);">- *

- * Note that the implicit IV is based on the HMAC ke= y, but only in AEAD modes
- * where the HMAC key is not used for an actual HMAC.
- *
- * @param ctx Encrypt/decrypt key contex= t
- * @param key = HMAC key, used to calculate implicit IV
- * @param key_len HMAC key le= ngth
- */
-static void

-key_ctx_update_implicit_iv(struct key_ctx *= ctx, uint8_t *key, size_t key_len);
-
-=
-/**
* Limi= t the reneg_bytes value when using a small-block (<128 bytes) cipher.
*
* @param cipher The current ciph= er (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_updat= e_implicit_iv(&key->encrypt, key2->keys[(int)server].hmac,=
- = MAX_HMAC_KEY_LENGTH);
- key_ctx_update_implicit_iv(&key->decrypt,
<= span style=3D"color: hsl(0, 100%, 40%);">- = key2->keys[1 - (int)server].hmac,

- MAX_HMAC_KEY_LENGTH);
}
}

= @@ -1553,23 +1532,6 @@
return ret;
}<= /span>

-stat= ic void
-key_ctx_update= _implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len)
<= span style=3D"color: hsl(0, 100%, 40%);">-{

- /* Only use implicit IV in AEAD cipher mode, wher= e 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->cipher)= >=3D OPENVPN_AEAD_MIN_IV_LEN);
- impl_iv_len =3D cipher_ctx_iv_length(ctx->cipher) -= sizeof(packet_id_type);
- AS= SERT(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_l= en);
- }
<= span style=3D"color: hsl(0, 100%, 40%);">-}

-
/**
* Generate da= ta 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 @@<= br> #endif /* HAVE_OPENSSL_STORE */
}

-static void=
-init_implicit_iv(struct cryp= to_options *co)
-{
- cipher_ctx_t *cipher = =3D co->key_ctx_bi.encrypt.cipher;
-
- i= f (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.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: newpatchset
--8HSGQfpAXX8=--