Skip to contentSkip to conversation

crypto: create function to initialize encrypt and decrypt key

PATCH In review Antonio Quartulli 3 participants 5 messages Jul 7, 2017 – Aug 17, 2017

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 crypto: create function to initialize encrypt and decrypt key

From: Steffan Karger <[email protected]...>

Instead of always initialize the encrypt and decrypt keys separately, implement an helper function init_key_ctx_bi() that takes care of both of them for us.

Reduces code duplication and improves readability.

---
 src/openvpn/crypto.c | 29 +++++++++++++++++++++--------
 src/openvpn/crypto.h |  4 ++++
 src/openvpn/ssl.c    | 17 ++---------------
 3 files changed, 27 insertions(+), 23 deletions(-)
src/openvpn/crypto.c +21 -8
index 78ca4197..4ea0af09 100644
@@ -873,6 +873,26 @@ init_key_ctx(struct key_ctx *ctx, const struct key *key,
 }
 
 void
+init_key_ctx_bi(struct key_ctx_bi *ctx, const struct key2 *key2,
+                int key_direction, const struct key_type *kt, const char *name)
+{
+    char log_prefix[128] = { 0 };
+    struct key_direction_state kds;
+
+    key_direction_state_init(&kds, key_direction);
+
+    openvpn_snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", name);
+    init_key_ctx(&ctx->encrypt, &key2->keys[kds.out_key], kt,
+                 OPENVPN_OP_ENCRYPT, log_prefix);
+
+    openvpn_snprintf(log_prefix, sizeof(log_prefix), "Incoming %s", name);
+    init_key_ctx(&ctx->decrypt, &key2->keys[kds.in_key], kt,
+                 OPENVPN_OP_DECRYPT, log_prefix);
+
+    ctx->initialized = true;
+}
+
+void
 free_key_ctx(struct key_ctx *ctx)
 {
     if (ctx->cipher)
@@ -1161,7 +1181,6 @@ crypto_read_openvpn_key(const struct key_type *key_type,
 {
     struct key2 key2;
     struct key_direction_state kds;
-    char log_prefix[128] = { 0 };
 
     if (key_inline)
     {
@@ -1186,13 +1205,7 @@ crypto_read_openvpn_key(const struct key_type *key_type,
     must_have_n_keys(key_file, opt_name, &key2, kds.need_keys);
 
     /* initialize key in both directions */
-    openvpn_snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", key_name);
-    init_key_ctx(&ctx->encrypt, &key2.keys[kds.out_key], key_type,
-                 OPENVPN_OP_ENCRYPT, log_prefix);
-    openvpn_snprintf(log_prefix, sizeof(log_prefix), "Incoming %s", key_name);
-    init_key_ctx(&ctx->decrypt, &key2.keys[kds.in_key], key_type,
-                 OPENVPN_OP_DECRYPT, log_prefix);
-
+    init_key_ctx_bi(ctx, &key2, key_direction, key_type, key_name);
     secure_memzero(&key2, sizeof(key2));
 }
 
src/openvpn/crypto.h +4 -0
index fec2eea7..86d2cfcc 100644
@@ -318,6 +318,10 @@ void init_key_ctx(struct key_ctx *ctx, const struct key *key,
 
 void free_key_ctx(struct key_ctx *ctx);
 
+void init_key_ctx_bi(struct key_ctx_bi *ctx, const struct key2 *key2,
+                     int key_direction, const struct key_type *kt,
+		     const char *name);
+
 void free_key_ctx_bi(struct key_ctx_bi *ctx);
 
 
src/openvpn/ssl.c +2 -15
index df232894..56daec38 100644
@@ -1840,20 +1840,8 @@ generate_key_expansion(struct key_ctx_bi *key,
     }
 
     /* Initialize OpenSSL key contexts */
-
-    ASSERT(server == true || server == false);
-
-    init_key_ctx(&key->encrypt,
-                 &key2.keys[(int)server],
-                 key_type,
-                 OPENVPN_OP_ENCRYPT,
-                 "Data Channel Encrypt");
-
-    init_key_ctx(&key->decrypt,
-                 &key2.keys[1-(int)server],
-                 key_type,
-                 OPENVPN_OP_DECRYPT,
-                 "Data Channel Decrypt");
+    int key_direction = server ? KEY_DIRECTION_INVERSE : KEY_DIRECTION_NORMAL;
+    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,
@@ -1861,7 +1849,6 @@ generate_key_expansion(struct key_ctx_bi *key,
     key_ctx_update_implicit_iv(&key->decrypt, key2.keys[1-(int)server].hmac,
                                MAX_HMAC_KEY_LENGTH);
 
-    key->initialized = true;
     ret = true;
 
 exit:
Signature · 1 line
2.13.2
#1 lore raw

Antonio Quartulli:

Quoted 10 lines

From: Steffan Karger <[email protected]...>

Instead of always initialize the encrypt and decrypt keys separately, implement an helper function init_key_ctx_bi() that takes care of both of them for us.

Reduces code duplication and improves readability.

I have to retreat my ACK. This patch is generating some new warnings that have to be fixed before being considered for merging.

Cheers,

Signature · 1 line
Antonio Quartulli
#2 lore raw

Antonio Quartulli:

Quoted 6 lines
Quoted 2 lines

I have to retreat my ACK. This patch is generating some new warnings that have to be fixed before being considered for merging.

Sorry, please ignore my previous email. I am working with too many branches at the same time and I mixed up the warnings.

Cheers,

Signature · 1 line
Antonio Quartulli
#3 lore raw
In reply to Antonio Quartulli

Hi,

Quoted 114 lines · Antonio Quartulli

From: Steffan Karger <[email protected]...>

Instead of always initialize the encrypt and decrypt keys separately, implement an helper function init_key_ctx_bi() that takes care of both of them for us.

Reduces code duplication and improves readability.

---
 src/openvpn/crypto.c | 29 +++++++++++++++++++++--------
 src/openvpn/crypto.h |  4 ++++
 src/openvpn/ssl.c    | 17 ++---------------
 3 files changed, 27 insertions(+), 23 deletions(-)
src/openvpn/crypto.c +21 -8
index 78ca4197..4ea0af09 100644
@@ -873,6 +873,26 @@ init_key_ctx(struct key_ctx *ctx, const struct key *key,
 }
 
 void
+init_key_ctx_bi(struct key_ctx_bi *ctx, const struct key2 *key2,
+                int key_direction, const struct key_type *kt, const char *name)
+{
+    char log_prefix[128] = { 0 };
+    struct key_direction_state kds;
+
+    key_direction_state_init(&kds, key_direction);
+
+    openvpn_snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", name);
+    init_key_ctx(&ctx->encrypt, &key2->keys[kds.out_key], kt,
+                 OPENVPN_OP_ENCRYPT, log_prefix);
+
+    openvpn_snprintf(log_prefix, sizeof(log_prefix), "Incoming %s", name);
+    init_key_ctx(&ctx->decrypt, &key2->keys[kds.in_key], kt,
+                 OPENVPN_OP_DECRYPT, log_prefix);
+
+    ctx->initialized = true;
+}
+
+void
 free_key_ctx(struct key_ctx *ctx)
 {
     if (ctx->cipher)
@@ -1161,7 +1181,6 @@ crypto_read_openvpn_key(const struct key_type *key_type,
 {
     struct key2 key2;
     struct key_direction_state kds;
-    char log_prefix[128] = { 0 };
 
     if (key_inline)
     {
@@ -1186,13 +1205,7 @@ crypto_read_openvpn_key(const struct key_type *key_type,
     must_have_n_keys(key_file, opt_name, &key2, kds.need_keys);
 
     /* initialize key in both directions */
-    openvpn_snprintf(log_prefix, sizeof(log_prefix), "Outgoing %s", key_name);
-    init_key_ctx(&ctx->encrypt, &key2.keys[kds.out_key], key_type,
-                 OPENVPN_OP_ENCRYPT, log_prefix);
-    openvpn_snprintf(log_prefix, sizeof(log_prefix), "Incoming %s", key_name);
-    init_key_ctx(&ctx->decrypt, &key2.keys[kds.in_key], key_type,
-                 OPENVPN_OP_DECRYPT, log_prefix);
-
+    init_key_ctx_bi(ctx, &key2, key_direction, key_type, key_name);
     secure_memzero(&key2, sizeof(key2));
 }
 
src/openvpn/crypto.h +4 -0
index fec2eea7..86d2cfcc 100644
@@ -318,6 +318,10 @@ void init_key_ctx(struct key_ctx *ctx, const struct key *key,
 
 void free_key_ctx(struct key_ctx *ctx);
 
+void init_key_ctx_bi(struct key_ctx_bi *ctx, const struct key2 *key2,
+                     int key_direction, const struct key_type *kt,
+		     const char *name);
+
 void free_key_ctx_bi(struct key_ctx_bi *ctx);
 
 
src/openvpn/ssl.c +2 -14
index df232894..56daec38 100644
@@ -1840,20 +1840,8 @@ generate_key_expansion(struct key_ctx_bi *key,
     }
 
     /* Initialize OpenSSL key contexts */
-
-    ASSERT(server == true || server == false);
-
-    init_key_ctx(&key->encrypt,
-                 &key2.keys[(int)server],
-                 key_type,
-                 OPENVPN_OP_ENCRYPT,
-                 "Data Channel Encrypt");
-
-    init_key_ctx(&key->decrypt,
-                 &key2.keys[1-(int)server],
-                 key_type,
-                 OPENVPN_OP_DECRYPT,
-                 "Data Channel Decrypt");
+    int key_direction = server ? KEY_DIRECTION_INVERSE : KEY_DIRECTION_NORMAL;
+    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,

src/openvpn/ssl.c generate_key_expansion(struct key_ctx_bi *key, · line 1854

Quoted 7 lines
src/openvpn/ssl.c +0 -1
@@ -1861,7 +1849,6 @@ generate_key_expansion(struct key_ctx_bi *key,
     key_ctx_update_implicit_iv(&key->decrypt, key2.keys[1-(int)server].hmac,
                                MAX_HMAC_KEY_LENGTH);
 
-    key->initialized = true;
     ret = true;
 
 exit:

Apart from three extra newlines (which increase code readability) and a slightly improved commit message, this is the same commit as in my tls-crypt-v2-preview branch:
https://github.com/syzzer/openvpn/commit/9837b498

So I confirm that I agree to include this patch with me as the author.

-Steffan

#4 lore raw
In reply to Antonio Quartulli

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512

Your patch has been applied to the following branches

commit 974513ea64020c956b531b1cabd76fdbac6655d8 (master) commit 9df6a9f66463e0b3ffe3c186b213e80942c13b52 (release/2.4)

Author: Steffan Karger
Date:   Fri Jul 7 12:47:04 2017 +0800
     crypto: create function to initialize encrypt and decrypt key

     Signed-off-by: Steffan Karger <[email protected]...>
     Acked-by: Antonio Quartulli <[email protected]...>
     Message-Id: <[email protected]...>
     URL: https://www.mail-archive.com/[email protected]/msg15011.html
     Signed-off-by: David Sommerseth <[email protected]...>

- --
kind regards,

David Sommerseth

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.22 (GNU/Linux)

iQIcBAEBCgAGBQJZlbiUAAoJEIbPlEyWcf3yN60QAJqE7LguoLJis3whFL4Iv+Hg lGOXbNQdEkGyMPh0P4QBWrjX2kJuxdg925sdocy1Rj4f8nIAQSr0j5ENIHn0fQI9 eXG0FpKlsdIbd1rcVEAJ7/Zk3P/FrhbMKoVIm0aSxDgmrT9r7tBCg3UlIXBkF5Iu 8k5akJYg1CA2sjRf3SXs629OojNHyiZ0RUB41gZfHd0JOCNbzlDuioLWbPJ6rmyV GBsTy4tiehTJdljFeaaHCbRGzjcIO4cNkeR5Gqhk7QUQeInips8QQlzst/nTlEsN DMtDM0xdeoWDQKu8Yd8iGAzXpMLHI0TtEuvugZL7u2l5T+DcyFQ5kueKts9A+zMn rRe9H16Xgp0mudVw34C5dJIe/NuN1+l85kgHFH3yozg35ggZuYPE/yxSUAaDBPMm E4tlC+tJ3EL/CUM3WaZYxxYtcN2nAdF3Atzoo6badi70B4IBQjbUUw70LGemlASx Me9FZxVWuCSxzHQrCR6GOaE+ZNHY9sk4GF7LqDU/Qg5qrDYgbfo1LKHjD4Oeso+v wFHRLN83LVP6TEl2+vSALylQb1XMfILrR85/rarN2krToh4sOws2C6bV5zmc5RyW MUlhxRvDs9LnIKHVa2c1vmiQTS7K9P6pBIkawwGX14QgSMJMjXwdWE5egVXvgyEx aDCLpgMOasYSeMvJP3S0
=BRt7
-----END PGP SIGNATURE-----