From: "plaisthos (Code Review)" Date: Mon, 11 Nov 2024 01:59:50 +0000 References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <1f3d1369ef5f4f20c559c21112f737d5dc72c22c-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="zaRe/kYpPOY="; 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 --zaRe/kYpPOY= 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/+/800?usp=3Demail to review the following change. 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/1 diff --git a/src/openvpn/crypto.c b/src/openvpn/crypto.c index f0b60a3..44c226c 100644 --- a/src/openvpn/crypto.c +++ b/src/openvpn/crypto.c @@ -892,6 +892,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 <=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 */ + 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, @@ -950,7 +977,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 @@ -965,7 +992,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 @@ -1000,6 +1027,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 2d6cf64..9eaf3ac 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). @@ -1409,12 +1394,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); } } @@ -1551,23 +1530,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 <=3D OPENVPN_MAX_IV_LENGTH); - 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: 1 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: flichtenheld Gerrit-MessageType: newchange --zaRe/kYpPOY= 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

Move initialisation of implicit IVs to init_key_ctx_bi method=
s

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 c= hannel key without calling some extra functions after that
which will ma= ke the (upcoming) epoch key implementation cleaner.

Also ensure that= free_ctx_bi actually also sets initialized to false.

Change-Id: Id2= 23612c7bcab91d49c013fb775024bd64ab0836
Signed-off-by: Arne Schwabe <a= rne@...1227...>
---
M src/openvpn/crypto.c
M src/openvpn/ssl.c<= br>M tests/unit_tests/openvpn/test_ssl.c
3 files changed, 30 insertions(= +), 59 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openv=
pn refs/changes/00/800/1
diff --git a/src/openvpn/crypto.c b/src/op=
envpn/crypto.c
index f0b60a3..44c226c 100644
--- a/src/openvpn/crypto.c
+++ b/src/openvpn/crypto.c
@@ -892,6 +892,33 @@
}
}=

+/= **
+ * Update the imp= licit 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 on= ly 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 void
+key_ctx_update_implicit_iv(struct key_ctx *ctx, const struct ke= y *key)
+{
= + /* Only use implicit IV in= AEAD cipher mode, where HMAC key is not used */
+ if (cipher_ctx_mode_aead(ctx->cipher))<= /span>
+ {
+ size_t impl_iv_len =3D 0;<= /span>
+ ASSERT(ciphe= r_ctx_iv_length(ctx->cipher) >=3D OPENVPN_AEAD_MIN_IV_LEN);+ impl_iv_len =3D ciphe= r_ctx_iv_length(ctx->cipher) - sizeof(packet_id_type);
+ ASSERT(impl_iv_len <=3D OP= ENVPN_MAX_IV_LENGTH);
+ ASSERT(impl_iv_len <=3D MAX_HMAC_KEY_LENGTH);
+ CLEAR(ctx->implicit_iv);<= /span>
+ /* The first= bytes of the IV are filled with the packet id */
+ memcpy(ctx->implicit_iv + sizeof(p= acket_id_type), key->hmac, impl_iv_len);
+ }
+}
+<= br> /* given a key and key_type, build a key_ctx */
= void
init_key_ctx(struct key_ctx *ctx, const struct key *k= ey,
@@ -950,7 +977,7 @@
snprintf(log_p= refix, 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@@ -965,7 +992,7 @@
snprintf(log_prefix, sizeo= f(log_prefix), "Incoming %s", name);
init_ke= y_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
@@ -1000= ,6 +1027,7 @@
{
free_key_ctx(&ctx= ->encrypt);
free_key_ctx(&ctx->decrypt);
+ ctx->initialized= =3D false;
}

static bo= ol
diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c
index 2d6cf64..9eaf3ac 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 cipher
- * use= d.
- *
- * 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/dec= rypt key context
- * @p= aram key HMAC key, used to calculate implicit IV- * @param key_len = HMAC key length
- */<= /span>
-static void
= -key_ctx_update_implicit_iv(struc= t key_ctx *ctx, uint8_t *key, size_t key_len);
-
-
-/**
* Limit the reneg_bytes value when using a small-block (<128 bytes)= cipher.

*
* @param cipher The c= urrent cipher (may be NULL).
@@ -1409,12 +1394,6 @@<= br> else
{
init_key= _ctx_bi(key, key2, key_direction, key_type, "Data Channel");

- /* Initialize impl= icit IVs */
- ke= y_ctx_update_implicit_iv(&key->encrypt, key2->keys[(int)server].h= mac,
- = MAX_HMAC_KEY_LENGTH);
- key_ctx_update_implicit_iv(&key->decrypt,<= /span>
- = key2->keys[1 - (int)server].hmac,
- MAX_HMAC_KEY= _LENGTH);
}
}

@@ -1551,23 +1530,6 @@
return ret; }

-static void
-key= _ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len)<= /span>
-{
- /* 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-&g= t;cipher) - sizeof(packet_id_type);
- ASSERT(impl_iv_len <=3D OPENVPN_MAX_IV_LENGTH);
- ASSERT(impl_iv_l= en <=3D key_len);
- = CLEAR(ctx->implicit_iv);
- /* The first bytes of the IV are filled with the packe= t id */
- memcpy= (ctx->implicit_iv + sizeof(packet_id_type), key, impl_iv_len);- }
-}
-
/**
* Generate data channel key= s for the supplied TLS session.
*
diff --= git a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn/test_s= sl.c
index ae33cc6..caacd9e 100644
--- a/te= sts/unit_tests/openvpn/test_ssl.c
+++ b/tests/unit_tests/op= envpn/test_ssl.c
@@ -277,24 +277,6 @@
#end= if /* 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_m= ode_aead(cipher))
- = {
- ASSERT(ciphe= r_ctx_iv_length(cipher) <=3D OPENVPN_MAX_IV_LENGTH);
- ASSERT(cipher_ctx_iv_length(ciphe= r) >=3D OPENVPN_AEAD_MIN_IV_LEN);
-
- = /* Generate dummy implicit IV */
- ASSERT(rand_bytes(co->key_ctx_bi.encrypt.implicit_i= v,
- = OPENVPN_MAX_IV_LENGTH));
-
- mem= cpy(co->key_ctx_bi.decrypt.implicit_iv,
- co->key_ctx_bi.encrypt.implicit_iv, = OPENVPN_MAX_IV_LENGTH);
- }
-}

static void
init_frame_parameters(s= truct 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 p= acket sizes */

To view, 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: 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
--zaRe/kYpPOY=--