From: "plaisthos (Code Review)" Date: Fri, 20 Dec 2024 14:49:42 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="qdjkdxcC9Ic="; 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: flichtenheld Cc: openvpn-devel --qdjkdxcC9Ic= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld. Hello flichtenheld,=20 I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/800?usp=3Demail to look at the new patch set (#8). 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 --- 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/8 diff --git a/src/openvpn/crypto.c b/src/openvpn/crypto.c index 53f50de..67c4d3b 100644 --- a/src/openvpn/crypto.c +++ b/src/openvpn/crypto.c @@ -900,6 +900,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, @@ -958,7 +985,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 @@ -973,7 +1000,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 @@ -1008,6 +1035,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 73201ef..5adc6b1 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: 8 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --qdjkdxcC9Ic= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld.

plaisthos uploaded patch set #8 to this change.

<= a href=3D"http://gerrit.openvpn.net/c/openvpn/+/800?usp=3Demail">View Chang= e

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 k= ey without calling some extra functions after that
which will make the (= upcoming) epoch key implementation cleaner.

Also ensure that free_ct= x_bi actually also sets initialized to false.

Change-Id: Id223612c7b= cab91d49c013fb775024bd64ab0836
Signed-off-by: Arne Schwabe <arne@...1386...= 227...>
---
M src/openvpn/crypto.c
M src/openvpn/ssl.c
M tes= ts/unit_tests/openvpn/test_ssl.c
3 files changed, 30 insertions(+), 59 d= eletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/=
changes/00/800/8
diff --git a/src/openvpn/crypto.c b/src/openvpn/cr=
ypto.c
index 53f50de..67c4d3b 100644
--- a/= src/openvpn/crypto.c
+++ b/src/openvpn/crypto.c
<= span>@@ -900,6 +900,33 @@
}
}<= br>
+/**
+ * Update the implicit IV= for a key_ctx_bi based on TLS session ids and cipher
+ * used.
+ *
+ * Note that the implicit IV is based on the HMAC key, but only in AE= AD modes
+ * where th= e 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)<= /span>
+{
+ /* Only use implicit IV in AEAD ci= pher mode, where HMAC key is not used */
+ if (cipher_ctx_mode_aead(ctx->cipher))+ {
+ size_t impl_iv_len =3D 0;<= br>+ ASSERT(cipher_ctx_i= v_length(ctx->cipher) >=3D OPENVPN_AEAD_MIN_IV_LEN);
+ impl_iv_len =3D cipher_ctx_i= v_length(ctx->cipher) - sizeof(packet_id_type);
+ ASSERT(impl_iv_len + sizeof(packet_i= d_type) <=3D OPENVPN_MAX_IV_LENGTH);
+ 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 */<= br>+ memcpy(ctx->impl= icit_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, c= onst struct key *key,
@@ -958,7 +985,7 @@
= snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", name)= ;
init_key_ctx(ctx, &key2->keys[kds.out_key], k= t,
OPENVPN_OP_ENCRYPT, log_prefix);
-
+ key_ctx_update_implicit_iv(ctx, &key2-= >keys[kds.out_key]);
}

void

@@ -973,7 +1000,7 @@
snprintf(= log_prefix, sizeof(log_prefix), "Incoming %s", name);
<= span> init_key_ctx(ctx, &key2->keys[kds.in_key], kt,

<= span> OPENVPN_OP_DECRYPT, log_prefix);
-
+ key_ctx_update_implicit_iv(ctx, &key2->keys[kds.i= n_key]);
}

void<= br>@@ -1008,6 +1035,7 @@
{
free= _key_ctx(&ctx->encrypt);
free_key_ctx(&ctx-= >decrypt);
+ ct= x->initialized =3D false;
}
static bool
diff --git a/src/openvpn/ssl.c b/src/op= envpn/ssl.c
index 73201ef..5adc6b1 100644
-= -- a/src/openvpn/ssl.c
+++ b/src/openvpn/ssl.c
@@ -96,21 +96,6 @@
#endif /* ifdef MEASURE_TLS_HANDSHA= KE_STATS */

/**
- * Update the implicit IV for a key_ctx_bi bas= ed on TLS session ids and cipher
- * used.
- *=
- * Note that the impl= icit 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 HMAC key, used to calculate impl= icit IV
- * @param key_= len HMAC key length
- */
-static= void
-key_ctx_update_i= mplicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len);
-

-
-/= **
* Limit the reneg_bytes value when using a small-block= (<128 bytes) cipher.
*
* @param cip= her The current cipher (may be NULL).
@@ -1411,12 +1= 396,6 @@
else
{
= init_key_ctx_bi(key, key2, key_direction, key_type, "Data Chan= nel");
- /*= Initialize implicit IVs */
- key_ctx_update_implicit_iv(&key->encrypt, key2->key= s[(int)server].hmac,
- = MAX_HMAC_KEY_LENGTH);
- key_ctx_update_implicit_iv(&k= ey->decrypt,
- = key2->keys[1 - (int)server].hmac,- = MAX_HMAC_KEY_LENGTH);
}
}<= br>
@@ -1553,23 +1532,6 @@
retu= rn ret;
}

-static void
-key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, = size_t key_len)
-{
- /* Only use implicit I= V 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_c= tx_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_LENGTH);
- ASSERT(impl_iv_len <=3D key_len);
<= span style=3D"color: hsl(0, 100%, 40%);">- CLEAR(ctx->implicit_iv= );
- /* The firs= t bytes of the IV are filled with the packet id */
- memcpy(ctx->implicit_iv + sizeof(pa= cket_id_type), key, impl_iv_len);
- }
-}

-
/**
* Generate data channel keys for the supplied TLS session.<= /span>
*
diff --git a/tests/unit_tests/openvpn/t= est_ssl.c b/tests/unit_tests/openvpn/test_ssl.c
index ae33c= c6..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)
-{
-
- 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_L= EN);
-
- /* Generate dummy implicit IV = */
- ASSERT(rand= _bytes(co->key_ctx_bi.encrypt.implicit_iv,
- OPENVPN_MAX_IV_LENGTH));<= /span>
-
- memcpy(co->key_ctx_bi.decrypt.im= plicit_iv,
- = co->key_ctx_bi.encrypt.implicit_iv, OPENVPN_MAX_IV_LENGTH);- }
-}

static vo= id
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 */
<= /span>

To view, visit change 800. To unsubscribe, or for help writin= g mail filters, visit settin= gs.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: Id223612c7bcab91d49c013fb775= 024bd64ab0836
Gerrit-Change-Number: 800
Gerrit-PatchSet: 8
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: newpatchset
--qdjkdxcC9Ic=--