From: "plaisthos (Code Review)" Date: Sat, 23 Nov 2024 21:02:24 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., gert@...1296..., a@...2181... Message-ID: <5b16124bc747e266305e43a21c76497bdf903504-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="EMbkeWwoSBQ="; charset=UTF-8 Subject: [Openvpn-devel] [M] Change in openvpn[master]: Use XOR instead of concatenation for calculation of IV from implicit IV List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: cron2 , openvpn-devel --EMbkeWwoSBQ= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: cron2, flichtenheld, plaisthos. Hello flichtenheld, ordex, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/797?usp=3Demail to look at the new patch set (#6). The change is no longer submittable: checks~ChecksSubmitRule is unsatisfied= now. Change subject: Use XOR instead of concatenation for calculation of IV fro= m implicit IV ...................................................................... Use XOR instead of concatenation for calculation of IV from implicit IV This change prepares the extended packet id data where also the packet id part of the IV will be derived using xor. Using xor also in the AEAD case where this degenerates to a concatenation allows using the same IV generation code later. Change-Id: I74216d776d3e0a8dc987ec7b1671c8e8dcccdbd6 Signed-off-by: Arne Schwabe --- M src/openvpn/crypto.c M src/openvpn/crypto.h M src/openvpn/ssl.c M tests/unit_tests/openvpn/test_ssl.c 4 files changed, 32 insertions(+), 20 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/97/797/6 diff --git a/src/openvpn/crypto.c b/src/openvpn/crypto.c index 0aa310d..c193bdb 100644 --- a/src/openvpn/crypto.c +++ b/src/openvpn/crypto.c @@ -94,12 +94,16 @@ goto err; } - /* Remainder of IV consists of implicit part (unique per session) = */ - ASSERT(buf_write(&iv_buffer, ctx->implicit_iv, ctx->implicit_iv_le= n)); - ASSERT(iv_buffer.len =3D=3D iv_len); + /* Write packet id part of IV to work buffer */ + ASSERT(buf_write(&work, iv, packet_id_size(false))); - /* Write explicit part of IV to work buffer */ - ASSERT(buf_write(&work, iv, iv_len - ctx->implicit_iv_len)); + /* This generates the IV by XORing the implicit part of the IV + * with the packet id already written to the iv buffer */ + for (int i =3D 0; i < iv_len; i++) + { + iv[i] =3D iv[i] ^ ctx->implicit_iv[i]; + } + dmsg(D_PACKET_CONTENT, "ENCRYPT IV: %s", format_hex(iv, iv_len, 0,= &gc)); /* Init cipher_ctx with IV. key & keylen are already initialized = */ @@ -427,16 +431,21 @@ { uint8_t iv[OPENVPN_MAX_IV_LENGTH] =3D { 0 }; const int iv_len =3D cipher_ctx_iv_length(ctx->cipher); - const size_t packet_iv_len =3D iv_len - ctx->implicit_iv_len; + const size_t packet_iv_len =3D packet_id_size(false); - ASSERT(ctx->implicit_iv_len <=3D iv_len); - if (buf->len + ctx->implicit_iv_len < iv_len) + if (buf->len < packet_iv_len) { CRYPT_ERROR("missing IV info"); } memcpy(iv, BPTR(buf), packet_iv_len); - memcpy(iv + packet_iv_len, ctx->implicit_iv, ctx->implicit_iv_len); + + /* This generates the IV by XORing the implicit part of the IV + * with the packet id already written to the iv buffer */ + for (int i =3D 0; i < iv_len; i++) + { + iv[i] =3D iv[i] ^ ctx->implicit_iv[i]; + } dmsg(D_PACKET_CONTENT, "DECRYPT IV: %s", format_hex(iv, iv_len, 0,= &gc)); @@ -962,7 +971,6 @@ hmac_ctx_free(ctx->hmac); ctx->hmac =3D NULL; } - ctx->implicit_iv_len =3D 0; } void @@ -1078,18 +1086,15 @@ cipher_ctx_t *cipher =3D co->key_ctx_bi.encrypt.cipher; if (cipher_ctx_mode_aead(cipher)) { - size_t impl_iv_len =3D cipher_ctx_iv_length(cipher) - sizeof(p= acket_id_type); 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)); - co->key_ctx_bi.encrypt.implicit_iv_len =3D impl_iv_len; memcpy(co->key_ctx_bi.decrypt.implicit_iv, co->key_ctx_bi.encrypt.implicit_iv, OPENVPN_MAX_IV_LENG= TH); - co->key_ctx_bi.decrypt.implicit_iv_len =3D impl_iv_len; } } diff --git a/src/openvpn/crypto.h b/src/openvpn/crypto.h index b742880..ba451e7 100644 --- a/src/openvpn/crypto.h +++ b/src/openvpn/crypto.h @@ -163,9 +163,18 @@ { cipher_ctx_t *cipher; /**< Generic cipher %context. */ hmac_ctx_t *hmac; /**< Generic HMAC %context. */ + /** + * This implicit IV will be always XORed with the packet id that is se= nt on + * the wire to get the IV. For the common AEAD ciphers of AES-GCM and + * Chacha20-Poly1305, the length of the IV is 12 bytes (96 bits). + * + * For non-epoch 32bit packet id AEAD format we set the first 32 + * bits of implicit_iv to 0. + * Xor with the packet id in this case works as concatenation: + * after xor the lower 32 bit of the IV are the packet id and + * the rest of the IV is from the implicit IV. + */ uint8_t implicit_iv[OPENVPN_MAX_IV_LENGTH]; - /**< The implicit part of the IV */ - size_t implicit_iv_len; /**< The length of implicit_iv */ /** Counter for the number of plaintext encrypted using this cipher * in number of 128 bit blocks (only used for AEAD ciphers) */ uint64_t plaintext_blocks; diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index a8cc83b..2d6cf64 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -1562,8 +1562,9 @@ 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); - memcpy(ctx->implicit_iv, key, impl_iv_len); - ctx->implicit_iv_len =3D impl_iv_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= ); } } diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn= /test_ssl.c index a1ca344..ae33cc6 100644 --- a/tests/unit_tests/openvpn/test_ssl.c +++ b/tests/unit_tests/openvpn/test_ssl.c @@ -284,18 +284,15 @@ if (cipher_ctx_mode_aead(cipher)) { - size_t impl_iv_len =3D cipher_ctx_iv_length(cipher) - sizeof(packe= t_id_type); 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)); - co->key_ctx_bi.encrypt.implicit_iv_len =3D impl_iv_len; memcpy(co->key_ctx_bi.decrypt.implicit_iv, co->key_ctx_bi.encrypt.implicit_iv, OPENVPN_MAX_IV_LENGTH); - co->key_ctx_bi.decrypt.implicit_iv_len =3D impl_iv_len; } } -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/797?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: I74216d776d3e0a8dc987ec7b1671c8e8dcccdbd6 Gerrit-Change-Number: 797 Gerrit-PatchSet: 6 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: ordex Gerrit-CC: cron2 Gerrit-CC: openvpn-devel Gerrit-Attention: plaisthos Gerrit-Attention: cron2 Gerrit-Attention: flichtenheld Gerrit-MessageType: newpatchset --EMbkeWwoSBQ= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: cron2, flichtenheld, plaisthos. <= /p>

plaisthos uploaded patch set #6 to this change.

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

The change is no longer submittable: checks~ChecksSubmitRule is= unsatisfied now.

Use XOR instead of concatenation for calculation of  IV f=
rom implicit IV

This change prepares the extended packet id data whe= re also the packet id
part of the IV will be derived using xor. Using x= or also in the AEAD
case where this degenerates to a concatenation allow= s using the same
IV generation code later.

Change-Id: I74216d776d= 3e0a8dc987ec7b1671c8e8dcccdbd6
Signed-off-by: Arne Schwabe <arne@...1386...= 227...>
---
M src/openvpn/crypto.c
M src/openvpn/crypto.h
M = src/openvpn/ssl.c
M tests/unit_tests/openvpn/test_ssl.c
4 files chang= ed, 32 insertions(+), 20 deletions(-)

git pull ssh://gerrit.openv=
pn.net:29418/openvpn refs/changes/97/797/6
diff --git a/src/openvpn=
/crypto.c b/src/openvpn/crypto.c
index 0aa310d..c193bdb 100= 644
--- a/src/openvpn/crypto.c
+++ b/src/op= envpn/crypto.c
@@ -94,12 +94,16 @@
= goto err;
}

- /* Remainder of IV consists o= f implicit part (unique per session) */
- ASSERT(buf_write(&iv_buffer, ctx->implicit= _iv, ctx->implicit_iv_len));
- ASSERT(iv_buffer.len =3D=3D iv_len);
+ /* Write packet id part of IV to= work buffer */
+ = ASSERT(buf_write(&work, iv, packet_id_size(false)));

- /* Write e= xplicit part of IV to work buffer */
- ASSERT(buf_write(&work, iv, iv_len - ctx->imp= licit_iv_len));
+ = /* This generates the IV by XORing the implicit part of the IV+ * with the packet i= d already written to the iv buffer */
+ for (int i =3D 0; i < iv_len; i++)
<= span style=3D"color: hsl(120, 100%, 40%);">+ {
+ iv[i] =3D iv[i] ^ ctx->imp= licit_iv[i];
+ = }
+
= dmsg(D_PACKET_CONTENT, "ENCRYPT IV: %s", format_hex(iv, = iv_len, 0, &gc));

/* Init ci= pher_ctx with IV. key & keylen are already initialized */
@@ -427,16 +431,21 @@
{
u= int8_t iv[OPENVPN_MAX_IV_LENGTH] =3D { 0 };
const = int iv_len =3D cipher_ctx_iv_length(ctx->cipher);
- const size_t packet_iv_len =3D iv_l= en - ctx->implicit_iv_len;
+ const size_t packet_iv_len =3D packet_id_size(false);

- = ASSERT(ctx->implicit_iv_len <=3D iv_len);
- if (buf->len + ctx->implicit_iv_le= n < iv_len)
+ = if (buf->len < packet_iv_len)
{ CRYPT_ERROR("missing IV info");
}


memcpy(iv, BPTR(bu= f), packet_iv_len);
- = memcpy(iv + packet_iv_len, ctx->implicit_iv, ctx->implicit_iv_l= en);
+
+ /* This generates the IV b= y XORing the implicit part of the IV
+ * with the packet id already written to the iv bu= ffer */
+ for = (int i =3D 0; i < iv_len; i++)
+ {
+ iv[i] =3D iv[i] ^ ctx->implicit_iv[i];
+ }
dmsg(D_PACKET_CONTENT, "DECRYPT IV: %s", format_h= ex(iv, iv_len, 0, &gc));

@@ -962,7 +9= 71,6 @@
hmac_ctx_free(ctx->hmac);
ctx->hmac =3D NULL;
}
- ctx->implicit_iv_len =3D 0;

}

void
@@ -= 1078,18 +1086,15 @@
cipher_ctx_t *cipher =3D co-&g= t;key_ctx_bi.encrypt.cipher;
if (cipher_ctx_mode_a= ead(cipher))
{
- size_t impl_iv_len =3D cipher_ctx_iv_length(c= ipher) - sizeof(packet_id_type);
ASSERT(cipher= _ctx_iv_length(cipher) <=3D OPENVPN_MAX_IV_LENGTH);
= ASSERT(cipher_ctx_iv_length(cipher) >=3D OPENVPN_AEAD_MIN_IV_LE= N);

/* Generate dummy implic= it IV */
ASSERT(rand_bytes(co->key_ctx_bi.e= ncrypt.implicit_iv,
OPENVPN_= MAX_IV_LENGTH));
- = co->key_ctx_bi.encrypt.implicit_iv_len =3D impl_iv_len;
memcpy(co->key_ctx_bi.decrypt.impl= icit_iv,
co->key_ctx_bi.encrypt.impl= icit_iv, OPENVPN_MAX_IV_LENGTH);
- co->key_ctx_bi.decrypt.implicit_iv_len =3D impl_i= v_len;
}
}
<= /span>
diff --git a/src/openvpn/crypto.h b/src/openvpn/crypto.h

index b742880..ba451e7 100644
--- a/src/openv= pn/crypto.h
+++ b/src/openvpn/crypto.h
@@ -= 163,9 +163,18 @@
{
cipher_ctx_t *ciph= er; /**< Generic cipher %context. */
hmac_ctx= _t *hmac; /**< Generic HMAC %context. */
+ /**
+ * This implicit IV will be always XORed with the= packet id that is sent on
+ * the wire to get the IV. For the common AEAD ciphers of AES-GC= M and
+ * Chacha2= 0-Poly1305, the length of the IV is 12 bytes (96 bits).
+ *
+ * For non-epoch 32bit packet id AEAD format we = set the first 32
+ = * bits of implicit_iv to 0.
+ * Xor with the packet id in this case works as concatenation= :
+ * after xor t= he lower 32 bit of the IV are the packet id and
+ * the rest of the IV is from the implicit = IV.
+ */ uint8_t implicit_iv[OPENVPN_MAX_IV_LENGTH];
- /**< The implicit part of the IV = */
- size_t implicit= _iv_len; /**< The length of implicit_iv */
/** = Counter for the number of plaintext encrypted using this cipher
<= span> * in number of 128 bit blocks (only used for AEAD ciphers) */
uint64_t plaintext_blocks;
diff --git a/= src/openvpn/ssl.c b/src/openvpn/ssl.c
index a8cc83b..2d6cf6= 4 100644
--- a/src/openvpn/ssl.c
+++ b/src/= openvpn/ssl.c
@@ -1562,8 +1562,9 @@
= impl_iv_len =3D cipher_ctx_iv_length(ctx->cipher) - sizeof(packet_id_t= ype);
ASSERT(impl_iv_len <=3D OPENVPN_MAX_IV_LE= NGTH);
ASSERT(impl_iv_len <=3D key_len);=
- memcpy(ctx->impli= cit_iv, key, impl_iv_len);
- ctx->implicit_iv_len =3D impl_iv_len;
+ CLEAR(ctx->implicit_iv);

+ /* The first byte= s of the IV are filled with the packet id */
+ memcpy(ctx->implicit_iv + sizeof(packet= _id_type), key, impl_iv_len);
}
}

diff --git a/tests/unit_tests/openvpn/test_s= sl.c b/tests/unit_tests/openvpn/test_ssl.c
index a1ca344..a= e33cc6 100644
--- a/tests/unit_tests/openvpn/test_ssl.c
+++ b/tests/unit_tests/openvpn/test_ssl.c
@@ -= 284,18 +284,15 @@

if (cipher_ctx_mod= e_aead(cipher))
{
- size_t impl_iv_len =3D cipher_ctx_iv_length(cipher= ) - sizeof(packet_id_type);
ASSERT(cipher_ctx_iv_l= ength(cipher) <=3D OPENVPN_MAX_IV_LENGTH);
ASSE= RT(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));- co->key_ctx_bi.encr= ypt.implicit_iv_len =3D impl_iv_len;

= memcpy(co->key_ctx_bi.decrypt.implicit_iv,
= co->key_ctx_bi.encrypt.implicit_iv, OPENVPN_MAX_IV_LENGTH);
- co->key_ctx_bi.= decrypt.implicit_iv_len =3D impl_iv_len;
}
<= span> }



To view, visi= t change = 797. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I74216d776d3e0a8dc987ec7b167= 1c8e8dcccdbd6
Gerrit-Change-Number: 797
Gerrit-PatchSet: 6
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: ordex <a@...2181...>
Gerrit-CC: cron2 <gert@...1296...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: plaisthos <arne-openvpn@...220...= ..1227...>
Gerrit-Attention: cron2 <gert@...1296...>= ;
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-MessageType: newpatchset
--EMbkeWwoSBQ=--