From: "plaisthos (Code Review)" Date: Tue, 10 Sep 2024 09:24:24 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641..., lstipakov@...277... Message-ID: <17553a2a599ca1de28f7e9dc3e58d36b1d14e5b0-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="Iy1K01MvZco="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: Implement support for larger packet counter sizes List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld , stipa Cc: openvpn-devel --Iy1K01MvZco= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld, stipa. Hello flichtenheld, stipa, I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/507?usp=3Demail to look at the new patch set (#10). The following approvals got outdated and were removed: Code-Review+1 by flichtenheld, Code-Review+1 by stipa Change subject: Implement support for larger packet counter sizes ...................................................................... Implement support for larger packet counter sizes With DCO and possible future hardware assisted OpenVPN acceleration we are approaching the point where 32 bit IVs are not cutting it any more. To illustrate the problem, some back of the envelope math here: If we want to keep the current 3600s renegotiation interval and have a safety margin of 25% (when we trigger renegotiation) we have about 3.2 million packets (2*32 * 0.7) to work with. That translates to about 835k packets per second. With 1300 Byte packets that translates into 8-9 Gbit/s. That is far from unrealistic any more. Current DCO implementations are already in spitting distance to that or might even reach (for a single client connection) that if you have extremely fast single core performance CPU. This introduces the 64bit packet counters for AEAD data channel ciphers in TLS mode ciphers. No effort has been made to support larger packet counters in any other scenario since those are all legacy. While we still keep the old --secret logic around we use the same weird unix timestamp + packet counter format to avoid refactoring the code now and again when we remove --secret code but DCO implementations are free to use just a single 64 bit counter. One other small downside of this approach is that when rollover happens and we get reordering all the older packets are thrown away since the distance between the packet before and after the rollover is quite large as we probably jump forward more than 1s (or more than 2^32 packet ids). But this is an obscure edge that we can (currently) live with. While this implementation under hood allows one of the two to be enabled individually we do not expose this functionality but require the two protocol flags aead-tag-end and pkt-id-64-bit to be always come together. This allows other data channel implementations to only support a limited set of data channel formats. Change-Id: I01e258e97351b5aa4b9e561f5b35ddc2318569e2 Signed-off-by: Arne Schwabe --- M src/openvpn/crypto.c M src/openvpn/crypto.h M src/openvpn/dco.h M src/openvpn/init.c M src/openvpn/multi.c M src/openvpn/options.c M src/openvpn/packet_id.c M src/openvpn/packet_id.h M src/openvpn/push.c M src/openvpn/ssl.c M src/openvpn/ssl.h M src/openvpn/ssl_common.h M src/openvpn/ssl_ncp.c M tests/unit_tests/openvpn/test_ssl.c 14 files changed, 405 insertions(+), 44 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/07/507/10 diff --git a/src/openvpn/crypto.c b/src/openvpn/crypto.c index c226727..6a639b7 100644 --- a/src/openvpn/crypto.c +++ b/src/openvpn/crypto.c @@ -68,6 +68,7 @@ const struct key_ctx *ctx =3D &opt->key_ctx_bi.encrypt; uint8_t *mac_out =3D NULL; const int mac_len =3D OPENVPN_AEAD_TAG_LENGTH; + bool longiv =3D opt->flags & CO_64_BIT_PKT_ID; /* IV, packet-ID and implicit IV required for this mode. */ ASSERT(ctx->cipher); @@ -86,7 +87,7 @@ buf_set_write(&iv_buffer, iv, iv_len); /* IV starts with packet id to make the IV unique for packet */ - if (!packet_id_write(&opt->packet_id.send, &iv_buffer, false, fals= e)) + if (!packet_id_write_flat(&opt->packet_id.send, &iv_buffer, longiv= )) { msg(D_CRYPT_ERRORS, "ENCRYPT ERROR: packet ID roll over"); goto err; @@ -355,6 +356,9 @@ * Set buf->len to 0 and return false on decrypt error. * * On success, buf is set to point to plaintext, true is returned. + * + * This method assumes that everything between ad_start and BPTR(buf) is + * authenticated data and therefore has no ad_len parameter */ static bool openvpn_decrypt_aead(struct buffer *buf, struct buffer work, @@ -384,7 +388,11 @@ /* IV and Packet ID required for this mode */ ASSERT(packet_id_initialized(&opt->packet_id)); - /* Combine IV from explicit part from packet and implicit part from co= ntext */ + bool longiv =3D opt->flags & CO_64_BIT_PKT_ID; + + /* Combine IV from explicit part from packet and implicit part from co= ntext. + * packet_iv_len and implicit_iv are initialised in init_key_contexts + * when keys are initialised as well */ { uint8_t iv[OPENVPN_MAX_IV_LENGTH] =3D { 0 }; const int iv_len =3D cipher_ctx_iv_length(ctx->cipher); @@ -409,7 +417,7 @@ } /* Read packet ID from packet */ - if (!packet_id_read(&pin, buf, false)) + if (!packet_id_read_flat(&pin, buf, longiv)) { CRYPT_ERROR("error reading packet-id"); } diff --git a/src/openvpn/crypto.h b/src/openvpn/crypto.h index 61184bc..ccaba7c 100644 --- a/src/openvpn/crypto.h +++ b/src/openvpn/crypto.h @@ -248,8 +248,10 @@ * OpenVPN process startups. */ #define CO_PACKET_ID_LONG_FORM (1<<0) - /**< Bit-flag indicating whether to use - * OpenVPN's long packet ID format. */ + /**< Bit-flag indicating whether to use OpenVPN's long packet ID forma= t. + * This format puts [4 byte counter][4byte timestamp] on the wire in + * big endian/network endian format. + **/ #define CO_IGNORE_PACKET_ID (1<<1) /**< Bit-flag indicating whether to ignore * the packet ID of a received packet. @@ -283,6 +285,20 @@ /**< Bit-flag indicating that the AEAD tag is at the end of the * packet. */ +#define CO_64_BIT_PKT_ID (1<<9) + /**< Bit-flag indicating that we should use a 64 bit (8 byte) packet + * counter instead of the 32 bit that we use by default. The differenc= e to + * the normal CO_PACKET_ID_LONG_FORM packet ID is that this a real 64 = bit + * big endian number in the wire format. + * + * This is only used for AEAD encryption. Other encryption (--static, + * --tls-crypt, --tls-auth,...) uses the old format for compatibility + */ + + /* Note that even though this software implementation allows to define + * CO_AEAD_TAG_AT_THE_END and CO_64_BIT_PKT_ID independently, we only + * allow both to be used together to avoid having to implement + * the other variations in other data channel (DCO) implementations */ unsigned int flags; /**< Bit-flags determining behavior of * security operation functions. */ diff --git a/src/openvpn/dco.h b/src/openvpn/dco.h index 035474f..3ce2c31 100644 --- a/src/openvpn/dco.h +++ b/src/openvpn/dco.h @@ -249,6 +249,16 @@ */ const char *dco_get_supported_ciphers(void); +/** + * Return whether the dco implementation supports the new protocol feature= s of + * a 64 bit packet counter and AEAD tag at the end. + */ +static inline bool +dco_supports_data_v3(struct context *c) +{ + return false; +} + #else /* if defined(ENABLE_DCO) */ typedef void *dco_context_t; @@ -380,5 +390,10 @@ return ""; } +static inline bool +dco_supports_data_v3(struct context *c) +{ + return false; +} #endif /* defined(ENABLE_DCO) */ #endif /* ifndef DCO_H */ diff --git a/src/openvpn/init.c b/src/openvpn/init.c index dd56961..72d79c6 100644 --- a/src/openvpn/init.c +++ b/src/openvpn/init.c @@ -2311,6 +2311,10 @@ { buf_printf(&out, " aead-tag-end"); } + if (o->imported_protocol_flags & CO_64_BIT_PKT_ID) + { + buf_printf(&out, " pkt-id-64-bit"); + } } if (buf_len(&out) > strlen(header)) @@ -2679,6 +2683,19 @@ "this server"); return false; } + + /* Ensure that for proto v3 is fully enabled (both tag at end and + * 64 bit counter) or not at all to avoid having to test/implement + * 4 modes in data channels instead of just two */ + bool aead_end =3D (c->options.imported_protocol_flags & CO_AEAD_TA= G_AT_THE_END); + bool longpktiud =3D (c->options.imported_protocol_flags & CO_64_BI= T_PKT_ID); + + if (aead_end !=3D longpktiud) + { + msg(D_PUSH_ERRORS, "OPTIONS ERROR: AEAD tag at the end and 64 = bit " + "packet counter must be enabled together."); + return false; + } } if (found & OPT_P_PUSH_MTU) @@ -3277,6 +3294,16 @@ to.push_peer_info_detail =3D 1; } + /* Check if the DCO drivers support the new 64bit packet counter and + * AEAD tag at the end */ + if (dco_enabled(options)) + { + to.data_v3_features_supported =3D dco_supports_data_v3(c); + } + else + { + to.data_v3_features_supported =3D true; + } /* should we not xmit any packets until we get an initial * response from client? */ diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 0509911..b7d30bf 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -1851,6 +1851,13 @@ o->imported_protocol_flags |=3D CO_USE_CC_EXIT_NOTIFY; } + if (tls_multi->session[TM_ACTIVE].opt->data_v3_features_supported + && (proto & IV_PROTO_DATA_V3)) + { + o->imported_protocol_flags |=3D CO_AEAD_TAG_AT_THE_END; + o->imported_protocol_flags |=3D CO_64_BIT_PKT_ID; + } + /* Select cipher if client supports Negotiable Crypto Parameters */ /* if we have already created our key, we cannot *change* our own diff --git a/src/openvpn/options.c b/src/openvpn/options.c index 649f48b..2fd2e81 100644 --- a/src/openvpn/options.c +++ b/src/openvpn/options.c @@ -8723,6 +8723,10 @@ { options->imported_protocol_flags |=3D CO_AEAD_TAG_AT_THE_E= ND; } + else if (streq(p[j], "pkt-id-64-bit")) + { + options->imported_protocol_flags |=3D CO_64_BIT_PKT_ID; + } else { msg(msglevel, "Unknown protocol-flags flag: %s", p[j]); diff --git a/src/openvpn/packet_id.c b/src/openvpn/packet_id.c index be28999..5224879 100644 --- a/src/openvpn/packet_id.c +++ b/src/openvpn/packet_id.c @@ -320,6 +320,31 @@ return true; } +bool +packet_id_read_flat(struct packet_id_net *pin, struct buffer *buf, bool lo= ng_form) +{ + packet_id_type net_id; + net_time_t net_time; + + pin->id =3D 0; + pin->time =3D 0; + + if (long_form) + { + if (!buf_read(buf, &net_time, sizeof(net_time))) + { + return false; + } + pin->time =3D ntohtime(net_time); + } + if (!buf_read(buf, &net_id, sizeof(net_id))) + { + return false; + } + pin->id =3D ntohpid(net_id); + return true; +} + static bool packet_id_send_update(struct packet_id_send *p, bool long_form) { @@ -344,6 +369,30 @@ } bool +packet_id_write_flat(struct packet_id_send *p, struct buffer *buf, bool lo= ng_form) +{ + if (!packet_id_send_update(p, long_form)) + { + return false; + } + + const packet_id_type net_id =3D htonpid(p->id); + const net_time_t net_time =3D htontime(p->time); + + if (long_form && !buf_write(buf, &net_time, sizeof(net_time))) + { + return false; + } + + if (!buf_write(buf, &net_id, sizeof(net_id))) + { + return false; + } + + return true; +} + +bool packet_id_write(struct packet_id_send *p, struct buffer *buf, bool long_fo= rm, bool prepend) { diff --git a/src/openvpn/packet_id.h b/src/openvpn/packet_id.h index 558361a..d4b5f1e 100644 --- a/src/openvpn/packet_id.h +++ b/src/openvpn/packet_id.h @@ -244,10 +244,17 @@ * Read/write a packet ID to/from the buffer. Short form is sequence numb= er * only. Long form is sequence number and timestamp. */ - bool packet_id_read(struct packet_id_net *pin, struct buffer *buf, bool lo= ng_form); /** + * Variant of packet_id_read that expects the timestamp first and packet + * counter after that to form a flat 64bit counter on the wire if we are + * using the long form. + */ +bool packet_id_read_flat(struct packet_id_net *pin, struct buffer *buf, bo= ol long_form); + + +/** * Write a packet ID to buf, and update the packet ID state. * * @param p Packet ID state. @@ -260,6 +267,22 @@ bool packet_id_write(struct packet_id_send *p, struct buffer *buf, bool long_form, bool prepend); + +/** + * Write a packet ID to buf, and update the packet ID state. This variant + * will always use a variant of the packet id that can just be seen as + * a flat 64 bit counter. + * + * @param p Packet ID state. + * @param buf Buffer to write the packet ID to + * @param long_form If true, also update and write time_t to buf + * + * @return true if successful, false otherwise. + */ +bool +packet_id_write_flat(struct packet_id_send *p, struct buffer *buf, + bool long_form); + /* * Inline functions. */ diff --git a/src/openvpn/push.c b/src/openvpn/push.c index 6c06374..0397e0c 100644 --- a/src/openvpn/push.c +++ b/src/openvpn/push.c @@ -693,6 +693,10 @@ { buf_printf(&proto_flags, " aead-tag-end"); } + if (o->imported_protocol_flags & CO_64_BIT_PKT_ID) + { + buf_printf(&proto_flags, " pkt-id-64-bit"); + } if (buf_len(&proto_flags) > 0) { diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c index e2be614..aafa9f2 100644 --- a/src/openvpn/ssl.c +++ b/src/openvpn/ssl.c @@ -105,9 +105,11 @@ * @param ctx Encrypt/decrypt key context * @param key HMAC key, used to calculate implicit IV * @param key_len HMAC key length + * @param long_pkt_id 64-bit packet counters are used */ static void -key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_l= en); +key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_l= en, + bool long_pkt_id); /** @@ -1388,13 +1390,15 @@ } else { + bool longiv =3D ks->crypto_options.flags & CO_64_BIT_PKT_ID; 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->encrypt, + key2->keys[(int)server].hmac, + MAX_HMAC_KEY_LENGTH, longiv); key_ctx_update_implicit_iv(&key->decrypt, key2->keys[1 - (int)server].hmac, - MAX_HMAC_KEY_LENGTH); + MAX_HMAC_KEY_LENGTH, longiv); } } @@ -1532,14 +1536,14 @@ } static void -key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_l= en) +key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, + size_t key_len, bool longiv) { /* 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); + size_t impl_iv_len =3D cipher_ctx_iv_length(ctx->cipher) - packet_= id_size(longiv); ASSERT(impl_iv_len <=3D OPENVPN_MAX_IV_LENGTH); ASSERT(impl_iv_len <=3D key_len); memcpy(ctx->implicit_iv, key, impl_iv_len); @@ -1975,6 +1979,12 @@ iv_proto |=3D IV_PROTO_DYN_TLS_CRYPT; #endif + /* support for AEAD tag at the end and 8 byte IV */ + if (session->opt->data_v3_features_supported) + { + iv_proto |=3D IV_PROTO_DATA_V3; + } + buf_printf(&out, "IV_PROTO=3D%d\n", iv_proto); if (session->opt->push_peer_info_detail > 1) diff --git a/src/openvpn/ssl.h b/src/openvpn/ssl.h index eea1323..e72b0e5 100644 --- a/src/openvpn/ssl.h +++ b/src/openvpn/ssl.h @@ -107,6 +107,9 @@ /** Support to dynamic tls-crypt (renegotiation with TLS-EKM derived tls-c= rypt key) */ #define IV_PROTO_DYN_TLS_CRYPT (1<<9) +/** Support for the AEAD tag at the end and larger AEAD packet id */ +#define IV_PROTO_DATA_V3 (1<<10) + /** Supports the --dns option after all the incompatible changes */ #define IV_PROTO_DNS_OPTION_V2 (1<<11) diff --git a/src/openvpn/ssl_common.h b/src/openvpn/ssl_common.h index 5bc2f2a..80bb502 100644 --- a/src/openvpn/ssl_common.h +++ b/src/openvpn/ssl_common.h @@ -314,7 +314,6 @@ /* from command line */ bool single_session; - bool disable_occ; int mode; bool pull; /** @@ -364,6 +363,11 @@ const char *config_ciphername; const char *config_ncp_ciphers; + + /** whether our underlying data channel supports new data channel + * features. This is always true for the internal implementation but + * can be false for DCO implementations */ + bool data_v3_features_supported; bool tls_crypt_v2; const char *tls_crypt_v2_verify_script; @@ -493,8 +497,6 @@ */ int key_id; - int limit_next; /* used for traffic shaping on the control= channel */ - int verify_maxlevel; char *common_name; diff --git a/src/openvpn/ssl_ncp.c b/src/openvpn/ssl_ncp.c index 968858e..a7a7c2f 100644 --- a/src/openvpn/ssl_ncp.c +++ b/src/openvpn/ssl_ncp.c @@ -430,6 +430,12 @@ session->opt->crypto_flags |=3D CO_USE_CC_EXIT_NOTIFY; } + if (session->opt->data_v3_features_supported && (iv_proto_peer & IV_PR= OTO_DATA_V3)) + { + session->opt->crypto_flags |=3D CO_AEAD_TAG_AT_THE_END; + session->opt->crypto_flags |=3D CO_64_BIT_PKT_ID; + } + #if defined(HAVE_EXPORT_KEYING_MATERIAL) if (iv_proto_peer & IV_PROTO_TLS_KEY_EXPORT) { diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/unit_tests/openvpn= /test_ssl.c index a1ca344..e3d03e6 100644 --- a/tests/unit_tests/openvpn/test_ssl.c +++ b/tests/unit_tests/openvpn/test_ssl.c @@ -192,7 +192,7 @@ { struct gc_arena gc =3D gc_new(); - struct tls_root_ctx ctx =3D { 0 }; + struct tls_root_ctx ctx =3D {0}; tls_ctx_client_new(&ctx); tls_ctx_load_cert_file(&ctx, unittest_cert, true); @@ -278,19 +278,22 @@ } static void -init_implicit_iv(struct crypto_options *co) +init_implicit_iv(struct crypto_options *co, struct key2 *key2) { 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(packe= t_id_type); + bool longiv =3D co->flags & CO_64_BIT_PKT_ID; + + size_t impl_iv_len =3D cipher_ctx_iv_length(cipher) - packet_id_si= ze(longiv); 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)); + ASSERT(memcpy(co->key_ctx_bi.encrypt.implicit_iv, key2->keys[0].hm= ac, + OPENVPN_MAX_IV_LENGTH)); co->key_ctx_bi.encrypt.implicit_iv_len =3D impl_iv_len; memcpy(co->key_ctx_bi.decrypt.implicit_iv, @@ -349,7 +352,6 @@ /* init work */ ASSERT(buf_init(&work, frame.buf.headroom)); - init_implicit_iv(co); update_time(); /* Test encryption, decryption for all packet sizes */ @@ -389,24 +391,36 @@ gc_free(&gc); } - - struct crypto_options -init_crypto_options(const char *cipher, const char *auth) +init_crypto_options(const char *cipher, const char *auth, int flags, + struct key2 *statickey) { - struct key2 key2 =3D { .n =3D 2}; + struct key2 key2 =3D {.n =3D 2}; - ASSERT(rand_bytes(key2.keys[0].cipher, sizeof(key2.keys[0].cipher))); - ASSERT(rand_bytes(key2.keys[0].hmac, sizeof(key2.keys[0].hmac))); - ASSERT(rand_bytes(key2.keys[1].cipher, sizeof(key2.keys[1].cipher))); - ASSERT(rand_bytes(key2.keys[1].hmac, sizeof(key2.keys)[1].hmac)); + if (statickey) + { + /* Use chosen static key instead of random key when defined */ + key2 =3D *statickey; + } + else + { + ASSERT(rand_bytes(key2.keys[0].cipher, sizeof(key2.keys[0].cipher)= )); + ASSERT(rand_bytes(key2.keys[0].hmac, sizeof(key2.keys[0].hmac))); + ASSERT(rand_bytes(key2.keys[1].cipher, sizeof(key2.keys[1].cipher)= )); + ASSERT(rand_bytes(key2.keys[1].hmac, sizeof(key2.keys)[1].hmac)); - struct crypto_options co =3D { 0 }; + } + + struct crypto_options co =3D {0}; struct key_type kt =3D create_kt(cipher, auth, "ssl-test"); init_key_ctx_bi(&co.key_ctx_bi, &key2, 0, &kt, "unit-test-ssl"); - packet_id_init(&co.packet_id, 5, 5, "UNITTEST", 0); + packet_id_init(&co.packet_id, 5, 5, "UNITTEST", 0); + + co.flags |=3D flags; + + init_implicit_iv(&co, &key2); return co; } @@ -416,7 +430,6 @@ { packet_id_free(&co->packet_id); free_key_ctx_bi(&co->key_ctx_bi); - } /* This adds a few more methods than strictly necessary but this allows @@ -425,8 +438,27 @@ static void run_data_channel_with_cipher_end(const char *cipher) { - struct crypto_options co =3D init_crypto_options(cipher, "none"); - co.flags |=3D CO_AEAD_TAG_AT_THE_END; + struct crypto_options co =3D init_crypto_options(cipher, "none", + CO_AEAD_TAG_AT_THE_END,= NULL); + + do_data_channel_round_trip(&co); + uninit_crypto_options(&co); +} + +static void +run_data_channel_with_cipher_end_and_long_pkt_counter(const char *cipher) +{ + struct crypto_options co =3D init_crypto_options(cipher, "none", + CO_AEAD_TAG_AT_THE_END = | CO_64_BIT_PKT_ID, NULL); + do_data_channel_round_trip(&co); + uninit_crypto_options(&co); +} + +static void +run_data_channel_with_long_pkt_counter(const char *cipher) +{ + struct crypto_options co =3D init_crypto_options(cipher, "none", + CO_64_BIT_PKT_ID, NULL); do_data_channel_round_trip(&co); uninit_crypto_options(&co); } @@ -434,31 +466,36 @@ static void run_data_channel_with_cipher(const char *cipher, const char *auth) { - struct crypto_options co =3D init_crypto_options(cipher, auth); + struct crypto_options co =3D init_crypto_options(cipher, auth, 0, NULL= ); do_data_channel_round_trip(&co); uninit_crypto_options(&co); } +static void +run_aead_channel_tests(const char *cipher) +{ + run_data_channel_with_cipher_end(cipher); + run_data_channel_with_cipher(cipher, "none"); + run_data_channel_with_cipher_end_and_long_pkt_counter(cipher); + run_data_channel_with_long_pkt_counter(cipher); +} static void test_data_channel_roundtrip_aes_128_gcm(void **state) { - run_data_channel_with_cipher_end("AES-128-GCM"); - run_data_channel_with_cipher("AES-128-GCM", "none"); + run_aead_channel_tests("AES-128-GCM"); } static void test_data_channel_roundtrip_aes_192_gcm(void **state) { - run_data_channel_with_cipher_end("AES-192-GCM"); - run_data_channel_with_cipher("AES-192-GCM", "none"); + run_aead_channel_tests("AES-192-GCM"); } static void test_data_channel_roundtrip_aes_256_gcm(void **state) { - run_data_channel_with_cipher_end("AES-256-GCM"); - run_data_channel_with_cipher("AES-256-GCM", "none"); + run_aead_channel_tests("AES-256-GCM"); } static void @@ -488,8 +525,7 @@ return; } - run_data_channel_with_cipher_end("ChaCha20-Poly1305"); - run_data_channel_with_cipher("ChaCha20-Poly1305", "none"); + run_aead_channel_tests("ChaCha20-Poly1305"); } static void @@ -503,6 +539,155 @@ run_data_channel_with_cipher("BF-CBC", "SHA1"); } +static struct key2 +create_key(void) +{ + struct key2 key2 =3D {.n =3D 2}; + + const uint8_t key[] =3D + {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', '0', '1', '2', '3', '4', '5',= '6', '7', 'A', 'B', 'C', 'D', 'E', 'F', + 'G', 'H', 'j', 'k', 'u', 'c', 'h', 'e', 'n', 'l'}; + + static_assert(sizeof(key) =3D=3D 32, "Size of key should be 32 bytes"); + + /* copy the key a few times to ensure to have the size we need for + * Statickey but XOR it to not repeat it */ + uint8_t keydata[sizeof(key2.keys)]; + + for (int i =3D 0; i < sizeof(key2.keys); i++) + { + keydata[i] =3D (uint8_t) (key[i % sizeof(key)] ^ i); + } + + + ASSERT(memcpy(key2.keys[0].cipher, keydata, sizeof(key2.keys[0].cipher= ))); + ASSERT(memcpy(key2.keys[0].hmac, keydata + 64, sizeof(key2.keys[0].hma= c))); + ASSERT(memcpy(key2.keys[1].cipher, keydata + 128, sizeof(key2.keys[1].= cipher))); + ASSERT(memcpy(key2.keys[1].hmac, keydata + 192, sizeof(key2.keys)[1].h= mac)); + + return key2; +} + +static void +test_data_channel_known_vectors_run(bool longpktcounter) +{ + struct key2 key2 =3D create_key(); + + int flags =3D longpktcounter ? CO_64_BIT_PKT_ID : 0; + flags |=3D CO_AEAD_TAG_AT_THE_END; + + struct crypto_options co =3D init_crypto_options("AES-256-GCM", "none"= , flags, + &key2); + + struct gc_arena gc =3D gc_new(); + + /* initialise frame for the test */ + struct frame frame; + init_frame_parameters(&frame); + + struct buffer src =3D alloc_buf_gc(frame.buf.payload_size, &gc); + struct buffer work =3D alloc_buf_gc(BUF_SIZE(&frame), &gc); + struct buffer encrypt_workspace =3D alloc_buf_gc(BUF_SIZE(&frame), &gc= ); + struct buffer decrypt_workspace =3D alloc_buf_gc(BUF_SIZE(&frame), &gc= ); + struct buffer buf =3D clear_buf(); + void *buf_p; + + /* init work */ + ASSERT(buf_init(&work, frame.buf.headroom)); + + now =3D 0; + + /* msg(M_INFO, "TESTING ENCRYPT/DECRYPT of packet length=3D%d", i); */ + + /* + * Load src with known data. + */ + ASSERT(buf_init(&src, 0)); + const char *plaintext =3D "The quick little fox jumps over the bureauc= ratic hurdles"; + + ASSERT(buf_write(&src, plaintext, strlen(plaintext))); + + /* copy source to input buf */ + buf =3D work; + buf_p =3D buf_write_alloc(&buf, BLEN(&src)); + ASSERT(buf_p); + memcpy(buf_p, BPTR(&src), BLEN(&src)); + + /* initialize work buffer with buf.headroom bytes of prepend capacity = */ + ASSERT(buf_init(&encrypt_workspace, frame.buf.headroom)); + + /* add packet opcode and peer id */ + buf_write_u8(&encrypt_workspace, 7); + buf_write_u8(&encrypt_workspace, 0); + buf_write_u8(&encrypt_workspace, 0); + buf_write_u8(&encrypt_workspace, 23); + + /* encrypt */ + openvpn_encrypt(&buf, encrypt_workspace, &co); + + /* separate buffer in authenticated data and encrypted data */ + uint8_t *ad_start =3D BPTR(&buf); + buf_advance(&buf, 4); + + if (longpktcounter) + { + uint8_t packetid1[8] =3D {0, 0, 0, 0, 0, 0, 0, 1}; + assert_memory_equal(BPTR(&buf), packetid1, 8); + } + else + { + uint8_t packetid1[4] =3D {0, 0, 0, 1}; + assert_memory_equal(BPTR(&buf), packetid1, 4); + } + + uint8_t *tag_location =3D BEND(&buf) - OPENVPN_AEAD_TAG_LENGTH; + + if (longpktcounter) + { + const uint8_t exp_tag_long[16] =3D + {0x52, 0xee, 0xef, 0xdb, 0x34, 0xb7, 0xbd, 0x79, 0xfe, 0xbf, 0x69,= 0xd0, 0x4e, 0x92, 0xfe, 0x4b}; + assert_memory_equal(tag_location, exp_tag_long, OPENVPN_AEAD_TAG_L= ENGTH); + } + else + { + const uint8_t exp_tag_short[16] =3D + {0x1f, 0xdd, 0x90, 0x8f, 0x0e, 0x9d, 0xc2, 0x5e, 0x79, 0xd8, 0x32,= 0x02, 0x0d, 0x58, 0xe7, 0x3f}; + assert_memory_equal(tag_location, exp_tag_short, OPENVPN_AEAD_TAG_= LENGTH); + } + + if (longpktcounter) + { + const uint8_t bytesat14[6] =3D {0xc7, 0x40, 0x47, 0x81, 0xac, 0x8c= }; + assert_memory_equal(BPTR(&buf) + 14, bytesat14, sizeof(bytesat14)); + } + else + { + const uint8_t bytesat14[6] =3D {0xa8, 0x2e, 0x6b, 0x17, 0x06, 0xd9= }; + assert_memory_equal(BPTR(&buf) + 14, bytesat14, sizeof(bytesat14)); + } + + /* decrypt */ + openvpn_decrypt(&buf, decrypt_workspace, &co, &frame, ad_start); + + /* compare */ + assert_int_equal(buf.len, strlen(plaintext)); + assert_memory_equal(BPTR(&buf), plaintext, strlen(plaintext)); + + uninit_crypto_options(&co); + gc_free(&gc); +} + +static void +test_data_channel_known_vectors_longpktid(void **state) +{ + test_data_channel_known_vectors_run(true); +} + +static void +test_data_channel_known_vectors_shortpktid(void **state) +{ + test_data_channel_known_vectors_run(false); +} int main(void) @@ -521,6 +706,8 @@ cmocka_unit_test(test_data_channel_roundtrip_aes_192_cbc), cmocka_unit_test(test_data_channel_roundtrip_aes_256_cbc), cmocka_unit_test(test_data_channel_roundtrip_bf_cbc), + cmocka_unit_test(test_data_channel_known_vectors_longpktid), + cmocka_unit_test(test_data_channel_known_vectors_shortpktid) }; #if defined(ENABLE_CRYPTO_OPENSSL) -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/507?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: I01e258e97351b5aa4b9e561f5b35ddc2318569e2 Gerrit-Change-Number: 507 Gerrit-PatchSet: 10 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-Reviewer: stipa Gerrit-CC: openvpn-devel Gerrit-Attention: flichtenheld Gerrit-Attention: stipa Gerrit-MessageType: newpatchset --Iy1K01MvZco= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is currently required from: flichtenheld, stipa.

plaisthos uploaded patch set #10 to this change.

= View Chan= ge

The following approvals got outdated and were removed: Code-Review+1 by flichtenheld, Code-Review+1 by stipa

Implement support for=
 larger packet counter sizes

With DCO and possible future hardware a= ssisted OpenVPN acceleration we
are approaching the point where 32 bit I= Vs are not cutting it any more.

To illustrate the problem, some back= of the envelope math here:

If we want to keep the current 3600s ren= egotiation interval and have
a safety margin of 25% (when we trigger ren= egotiation) we have about
3.2 million packets (2*32 * 0.7) to work with.= That translates to
about 835k packets per second.

With 1300 Byte= packets that translates into 8-9 Gbit/s. That is far
from unrealistic a= ny more. Current DCO implementations are already in
spitting distance to= that or might even reach (for a single client
connection) that if you h= ave extremely fast
single core performance CPU.

This introduces t= he 64bit packet counters for AEAD data channel
ciphers in TLS mode ciphe= rs. No effort has been made to support
larger packet counters in any oth= er scenario since those are all legacy.

While we still keep the old = --secret logic around we use the same
weird unix timestamp + packet coun= ter format to avoid refactoring the
code now and again when we remove --= secret code but DCO
implementations are free to use just a single 64 bit= counter. One
other small downside of this approach is that when rollove= r happens
and we get reordering all the older packets are thrown away si= nce
the distance between the packet before and after the rollover is
= quite large as we probably jump forward more than 1s (or more than
2^32 = packet ids). But this is an obscure edge that we can
(currently) live wi= th.

While this implementation under hood allows one of the two
to= be enabled individually we do not expose this functionality
but require= the two protocol flags aead-tag-end and pkt-id-64-bit
to be always come= together. This allows other data channel
implementations to only suppor= t a limited set of data channel
formats.

Change-Id: I01e258e97351= b5aa4b9e561f5b35ddc2318569e2
Signed-off-by: Arne Schwabe <arne@...2068...= 7...>
---
M src/openvpn/crypto.c
M src/openvpn/crypto.h
M sr= c/openvpn/dco.h
M src/openvpn/init.c
M src/openvpn/multi.c
M src/o= penvpn/options.c
M src/openvpn/packet_id.c
M src/openvpn/packet_id.h<= br>M src/openvpn/push.c
M src/openvpn/ssl.c
M src/openvpn/ssl.h
M = src/openvpn/ssl_common.h
M src/openvpn/ssl_ncp.c
M tests/unit_tests/o= penvpn/test_ssl.c
14 files changed, 405 insertions(+), 44 deletions(-)
git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/07/50=
7/10
diff --git a/src/openvpn/crypto.c b/src/openvpn/crypto.c
index c226727..6a639b7 100644
--- a/src/openvpn/= crypto.c
+++ b/src/openvpn/crypto.c
@@ -68,= 6 +68,7 @@
const struct key_ctx *ctx =3D &opt->= key_ctx_bi.encrypt;
uint8_t *mac_out =3D NULL;<= br> const int mac_len =3D OPENVPN_AEAD_TAG_LENGTH;
+ bool longiv =3D opt->flags = & CO_64_BIT_PKT_ID;


/* IV, packe= t-ID and implicit IV required for this mode. */
ASSERT= (ctx->cipher);
@@ -86,7 +87,7 @@
= buf_set_write(&iv_buffer, iv, iv_len);

/* IV starts with packet id to make the IV unique for packet *= /
- if (!packet_= id_write(&opt->packet_id.send, &iv_buffer, false, false))=
+ if (!packet_id_wri= te_flat(&opt->packet_id.send, &iv_buffer, longiv))
{
msg(D_CRYPT_ERRORS, "ENCRYP= T ERROR: packet ID roll over");
goto err;=
@@ -355,6 +356,9 @@
* Set buf->len to= 0 and return false on decrypt error.
*
= * On success, buf is set to point to plaintext, true is returned.+ *
+ * This method assumes that everything betwee= n ad_start and BPTR(buf) is
+ * authenticated data and therefore has no ad_len parameter<= br> */
static bool
openvpn_decrypt= _aead(struct buffer *buf, struct buffer work,
@@ -384,7 +38= 8,11 @@
/* IV and Packet ID required for this mode */<= /span>
ASSERT(packet_id_initialized(&opt->packet_id));=

- = /* Combine IV from explicit part from packet and implicit part from contex= t */
+ bool longiv= =3D opt->flags & CO_64_BIT_PKT_ID;
+
+ /* Combine IV from explicit part from packet and implicit part from= context.
+ * pac= ket_iv_len and implicit_iv are initialised in init_key_contexts
<= span style=3D"color: hsl(120, 100%, 40%);">+ * when keys are initialise= d as well */

{
uint8_t iv[OPE= NVPN_MAX_IV_LENGTH] =3D { 0 };
const int iv_len = =3D cipher_ctx_iv_length(ctx->cipher);
@@ -409,7 +417,7 = @@
}

/* Read pa= cket ID from packet */
= - if (!packet_id_read(&pin, buf, false))
+ if (!packet_id_read_flat(&pin, buf, lon= giv))
{
CRYPT_ERROR("err= or reading packet-id");
}
diff --= git a/src/openvpn/crypto.h b/src/openvpn/crypto.h
index 611= 84bc..ccaba7c 100644
--- a/src/openvpn/crypto.h
<= span>+++ b/src/openvpn/crypto.h
@@ -248,8 +248,10 @@=
* OpenVPN process startups. */
#define CO_PACKET_ID_LONG_FORM (1<<0)
- /**< Bit-flag indicating whether to = use
- * OpenVPN= 9;s long packet ID format. */
+ /**< Bit-flag indicating whether to use OpenVPN's long= packet ID format.
+ = * This format puts [4 byte counter][4byte timestamp] on the wire in

+ * big endian/netwo= rk endian format.
+ = **/
#define CO_IGNORE_PACKET_ID (1<<1)=
/**< Bit-flag indicating whether to ignore
* the packet ID of a received packet.
@@ -283,6 += 285,20 @@
/**< Bit-flag indicating that the AEAD ta= g is at the end of the
* packet.
= */
+#define CO_64= _BIT_PKT_ID (1<<9)
+ /**< Bit-flag indicating that we should use a 64 bit (8 byte) = packet
+ * counte= r instead of the 32 bit that we use by default. The difference to+ * the normal CO_PACKET_I= D_LONG_FORM packet ID is that this a real 64 bit
+ * big endian number in the wire format.
+ *
+ * This is only used for AEAD = encryption. Other encryption (--static,

+ * --tls-crypt, --tls-auth,...) uses the old format= for compatibility
+ = */
+
+ /* Note that even though thi= s software implementation allows to define
+ * CO_AEAD_TAG_AT_THE_END and CO_64_BIT_PKT_ID i= ndependently, we only
+ * allow both to be used together to avoid having to implement=
+ * the other variation= s in other data channel (DCO) implementations */
unsigned int flags; /**< Bit-flags determining beha= vior of
* security oper= ation functions. */
diff --git a/src/openvpn/dco.h b/src/op= envpn/dco.h
index 035474f..3ce2c31 100644
-= -- a/src/openvpn/dco.h
+++ b/src/openvpn/dco.h
@@ -249,6 +249,16 @@
*/
const char *= dco_get_supported_ciphers(void);

+/**
+ * Return whether the dco implementation supports the new pr= otocol features of
+ = * a 64 bit packet counter and AEAD tag at the end.
+ */
+static inline bool
+dco_supports_data_v3(struct context *c)
+{
+ return false;
+}
+
#else /* if defined(ENABLE_DCO) */
typedef void *dco_context_t;
@@ -380,5 +390,10 @@<= /span>
return "";
}

+static inline bo= ol
+dco_supports_data= _v3(struct context *c)
+ return fals= e;
+}
#endif /* defined(ENABLE_DCO) */

#endif /* ifndef DCO_H = */
diff --git a/src/openvpn/init.c b/src/openvpn/init.c
index dd56961..72d79c6 100644
--- a/src/openvp= n/init.c
+++ b/src/openvpn/init.c
@@ -2311,= 6 +2311,10 @@
{
buf_p= rintf(&out, " aead-tag-end");
}

+ if (o->import= ed_protocol_flags & CO_64_BIT_PKT_ID)
+ {
+ buf_printf(&out, " pkt-id-64-bit");
+ }
<= span> }


if (buf_len(&out) &g= t; strlen(header))
@@ -2679,6 +2683,19 @@
= "this server");
retu= rn false;
}
+
+ = /* Ensure that for proto v3 is fully enabled (both tag at end and
+ * 64 bit count= er) or not at all to avoid having to test/implement
+ * 4 modes in data channels inste= ad of just two */
+ = bool aead_end =3D (c->options.imported_protocol_flags & CO_AEA= D_TAG_AT_THE_END);
+ = bool longpktiud =3D (c->options.imported_protocol_flags & CO_= 64_BIT_PKT_ID);
+
+ if (aead_end != =3D longpktiud)
+ = {
+ ms= g(D_PUSH_ERRORS, "OPTIONS ERROR: AEAD tag at the end and 64 bit "=
+ &qu= ot;packet counter must be enabled together.");
+ return false;
+ }

}


if (found & OPT_P_PUSH_MTU)=
@@ -3277,6 +3294,16 @@
to.push_peer_info= _detail =3D 1;
}

+ /* Check if the DCO drivers support= the new 64bit packet counter and
+ * AEAD tag at the end */
+ if (dco_enabled(options))
+ {
+ to.data_v3_features_supported =3D dco_supports_= data_v3(c);
+ }
+ else
+ {

+ to.data_v3_features_supported =3D true;=
+ }

/* should we not xmit any packets until we get an= initial
* response from client? */
d= iff --git a/src/openvpn/multi.c b/src/openvpn/multi.c
index= 0509911..b7d30bf 100644
--- a/src/openvpn/multi.c+++ b/src/openvpn/multi.c
@@ -1851,6 +1851,13 @@
o->imported_protocol_flags |=3D CO_USE_CC_EXIT_NO= TIFY;
}

+ if (tls_multi->session[TM_ACTIVE].opt->= ;data_v3_features_supported
+ && (proto & IV_PROTO_DATA_V3))
+ {
+ o->imported_protocol_flags |=3D CO_AEAD= _TAG_AT_THE_END;
+ = o->imported_protocol_flags |=3D CO_64_BIT_PKT_ID;
+ }
+
/* Select cipher if client su= pports Negotiable Crypto Parameters */

= /* if we have already created our key, we cannot *change* our own=
diff --git a/src/openvpn/options.c b/src/openvpn/options.c=
index 649f48b..2fd2e81 100644
--- a/src/openvpn/o= ptions.c
+++ b/src/openvpn/options.c
@@ -87= 23,6 +8723,10 @@
{
= options->imported_protocol_flags |=3D CO_AEAD_TAG_AT_THE_END;
}
+ else if (streq(p[j], "pkt-id-64-bit"))<= br>+ {
+ options->impor= ted_protocol_flags |=3D CO_64_BIT_PKT_ID;

+ }
else
{
msg(msglevel, &q= uot;Unknown protocol-flags flag: %s", p[j]);
diff --gi= t a/src/openvpn/packet_id.c b/src/openvpn/packet_id.c
index= be28999..5224879 100644
--- a/src/openvpn/packet_id.c
+++ b/src/openvpn/packet_id.c
@@ -320,6 +320,31= @@
return true;
}
<= /span>
+bool
+packet_id_read_flat(struct packet_i= d_net *pin, struct buffer *buf, bool long_form)

+{
+ packet_id_type net_id;
+ net_time_t net_time;
+
+ pin->time =3D 0;
+
+ if (lo= ng_form)
+ {
+ if (!buf_read(buf= , &net_time, sizeof(net_time)))
+ {
+ return false;
+ }
+ pin->time =3D ntohtime(net_time);
+ }
+ if (!buf_read(buf, &net_id, sizeof(net_id)))+ {
+ return false;
+ }
+ pin->id =3D ntohpid(net_id);
+ return true;
+}
+
static bool
packet_id_s= end_update(struct packet_id_send *p, bool long_form)
{

@@ -344,6 +369,30 @@
}

bool
+pa= cket_id_write_flat(struct packet_id_send *p, struct buffer *buf, bool long_= form)
+{
+ if (!packet_id_send_update(p= , long_form))

+ {<= /span>
+ return false= ;
+ }
+

+ const packet_id_type net_id =3D htonpid(p->i= d);
+ const net_ti= me_t net_time =3D htontime(p->time);
+
= + if (long_form && !buf_write(buf, &net_time, sizeof(net_tim= e)))
+ {+ return false;<= br>+ }
+
+ if (!buf_write(buf, &net_id, sizeof(net_id)))=
+ {
+ return false;
+ }
+
+ return true;
= +}
+
+bool
packet_id_writ= e(struct packet_id_send *p, struct buffer *buf, bool long_form,
<= span> bool prepend)
{
diff= --git a/src/openvpn/packet_id.h b/src/openvpn/packet_id.h
= index 558361a..d4b5f1e 100644
--- a/src/openvpn/packet_id.h=
+++ b/src/openvpn/packet_id.h
@@ -244,10 += 244,17 @@
* Read/write a packet ID to/from the buffer. S= hort form is sequence number
* only. Long form is sequen= ce number and timestamp.
*/
-
bool packet_id_read(struct packet= _id_net *pin, struct buffer *buf, bool long_form);
=
/**
+ * Va= riant of packet_id_read that expects the timestamp first and packet<= br>+ * counter after that to fo= rm a flat 64bit counter on the wire if we are
+ * using the long form.
+ */
+bool packet_id_read_flat(struct packet_id_net *pin, struct bu= ffer *buf, bool long_form);
+
+
= +/**
* Write = a packet ID to buf, and update the packet ID state.
*
* @param p Packet ID state.
@@ -= 260,6 +267,22 @@
bool packet_id_write(struct packet_id_sen= d *p, struct buffer *buf,
bool long_f= orm, bool prepend);

+
+/= **
+ * Write a packet= ID to buf, and update the packet ID state. This variant
+ * will always use a variant of the pa= cket id that can just be seen as
+ * a flat 64 bit counter.
+ *
+ * @param p Packet ID state.
+ * @param buf Buffer to write the packet I= D to
+ * @param long_= form If true, also update and write time_t to buf
+ *
+ * @return true if successful, false otherwise.
= + */
+bool
+packet_id_write_flat(struct packet_id_send *p, struct buffer = *buf,
+ = bool long_form);
+
/*
* Inline functions.
*/

diff --git a/src/openvpn/push.c b/src/openvpn/push= .c
index 6c06374..0397e0c 100644
--- a/src/= openvpn/push.c
+++ b/src/openvpn/push.c
@@ = -693,6 +693,10 @@
{
buf_print= f(&proto_flags, " aead-tag-end");
}
+ if (o->imported_p= rotocol_flags & CO_64_BIT_PKT_ID)
+ {
+ buf_printf(&proto_flags, " pkt-id-64-bit");
+ }

if (buf_len(&proto_flags) > 0)
= {
diff --git a/src/openvpn/ssl.c b/src/openvpn/ssl.c
index e2be614..aafa9f2 100644
--- a/src/openv= pn/ssl.c
+++ b/src/openvpn/ssl.c
@@ -105,9 = +105,11 @@
* @param ctx Encrypt/decrypt= key context
* @param key HMAC key, use= d to calculate implicit IV
* @param key_len = HMAC key length
+ * = @param long_pkt_id 64-bit packet counters are used
*/

static void
-key_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key,= size_t key_len);
+ke= y_ctx_update_implicit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len,=
+ = bool long_pkt_id);


/**

@@ -1388,13 +1390,15 @@
}
else
{
+ bool longiv =3D ks->crypto_options.fla= gs & CO_64_BIT_PKT_ID;
init_key_ctx_bi(key, ke= y2, 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);
<= span style=3D"color: hsl(120, 100%, 40%);">+ key_ctx_update_implicit= _iv(&key->encrypt,

+ key2->keys[(int)server].hmac,
+ = MAX_HMAC_KEY_LENGTH, longiv);
key_ct= x_update_implicit_iv(&key->decrypt,
= key2->keys[1 - (int)server].hmac,
- MAX_H= MAC_KEY_LENGTH);
+ = MAX_HMAC_KEY_LENGTH, longiv);
}

}

@@ -1532,14 = +1536,14 @@
}

static vo= id
-key_ctx_update_impl= icit_iv(struct key_ctx *ctx, uint8_t *key, size_t key_len)
+key_ctx_update_implicit_iv(struct ke= y_ctx *ctx, uint8_t *key,
+ size_t key_len, bool longiv)
{

/* Only use implicit IV in AEAD cipher mode, whe= re HMAC key is not used */
if (cipher_ctx_mode_aead(ct= x->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);<= br>+ size_t impl_iv_len = =3D cipher_ctx_iv_length(ctx->cipher) - packet_id_size(longiv); ASSERT(impl_iv_len <=3D OPENVPN_MAX_IV_LENGTH);<= br> ASSERT(impl_iv_len <=3D key_len);
= memcpy(ctx->implicit_iv, key, impl_iv_len);
@@ -197= 5,6 +1979,12 @@
iv_proto |=3D IV_PROTO_DYN_TLS_CRY= PT;
#endif

+ /* support for AEAD tag at the end and 8 = byte IV */
+ i= f (session->opt->data_v3_features_supported)
+ {
+ iv_proto |=3D IV_PROTO_DATA_V3;
= + }
+
buf_printf(&am= p;out, "IV_PROTO=3D%d\n", iv_proto);

= if (session->opt->push_peer_info_detail > 1)=
diff --git a/src/openvpn/ssl.h b/src/openvpn/ssl.h
index eea1323..e72b0e5 100644

--- a/src/openvpn/ssl.h
+++ b/src/openvpn/ssl.h
@@ -107,6 +107,9 @@
/** Support to dynamic tls-crypt (renegotiation with TLS-EKM= derived tls-crypt key) */
#define IV_PROTO_DYN_TLS_CRYPT = (1<<9)

+/** Support for the AEAD tag at the end and larger AEAD packet= id */
+#define IV_PR= OTO_DATA_V3 (1<<10)
+
/** Supports the --dns option after all the = incompatible changes */
#define IV_PROTO_DNS_OPTION_V2 (= 1<<11)

diff --git a/src/openvpn/ssl= _common.h b/src/openvpn/ssl_common.h
index 5bc2f2a..80bb502= 100644
--- a/src/openvpn/ssl_common.h
+++ = b/src/openvpn/ssl_common.h
@@ -314,7 +314,6 @@

/* from command line */
boo= l single_session;
- = bool disable_occ;
int mode;
bool = pull;
/**
@@ -364,6 +363,11 @@<= br> const char *config_ciphername;
const cha= r *config_ncp_ciphers;

+
+ /** whether our underlying data channel supports new data channel
+ * features. This i= s always true for the internal implementation but
+ * can be false for DCO implementations *= /
+ bool data_v3_f= eatures_supported;
bool tls_crypt_v2;
= const char *tls_crypt_v2_verify_script;

@@ -493,8 +497,6 @@

*/
int k= ey_id;

-<= br> int verify_maxlevel;

c= har *common_name;
diff --git a/src/openvpn/ssl_ncp.c b/src/= openvpn/ssl_ncp.c
index 968858e..a7a7c2f 100644
<= span>--- a/src/openvpn/ssl_ncp.c

+++ b/src/openvpn/ssl_ncp.= c
@@ -430,6 +430,12 @@
session->= ;opt->crypto_flags |=3D CO_USE_CC_EXIT_NOTIFY;
}

+ = if (session->opt->data_v3_features_supported && (iv_proto_pee= r & IV_PROTO_DATA_V3))
+ {
+ = session->opt->crypto_flags |=3D CO_AEAD_TAG_AT_THE_END;
+ session->opt->cryp= to_flags |=3D CO_64_BIT_PKT_ID;

+ }
+
#if defined(HAVE_EXPORT_KEYING_MATERIAL)
= if (iv_proto_peer & IV_PROTO_TLS_KEY_EXPORT)
{<= /span>
diff --git a/tests/unit_tests/openvpn/test_ssl.c b/tests/un= it_tests/openvpn/test_ssl.c
index a1ca344..e3d03e6 100644
--- a/tests/unit_tests/openvpn/test_ssl.c
++= + b/tests/unit_tests/openvpn/test_ssl.c
@@ -192,7 +192,7 @@=
{
struct gc_arena gc =3D gc_new();

- s= truct tls_root_ctx ctx =3D { 0 };
+ struct tls_root_ctx ctx =3D {0};
tls= _ctx_client_new(&ctx);
tls_ctx_load_cert_file(&= ;ctx, unittest_cert, true);

@@ -278,19 +2= 78,22 @@
}

static void<= /span>
-init_implicit_iv(struc= t crypto_options *co)
+init_implicit_iv(struct crypto_options *co, struct key2 *key2)
= {
cipher_ctx_t *cipher =3D co->key_ctx_bi.en= crypt.cipher;

+
if (cipher_ctx_mode_aead(cipher))<= br> {
- = size_t impl_iv_len =3D cipher_ctx_iv_length(cipher) - sizeof(packet_id_ty= pe);
+ bool lo= ngiv =3D co->flags & CO_64_BIT_PKT_ID;
+
+ size_t impl_iv_len =3D cipher_ctx_iv_length(cipher) - packet= _id_size(longiv);
ASSERT(cipher_ctx_iv_length(ciph= er) <=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,
+ ASSERT(memcpy(co->key_ctx_bi.e= ncrypt.implicit_iv, key2->keys[0].hmac,
+ OPENVPN_MAX_IV_LENGTH));
co->key_ctx_bi.encrypt.implicit_iv_len =3D impl_iv_l= en;

memcpy(co->key_ctx_bi.dec= rypt.implicit_iv,
@@ -349,7 +352,6 @@
= /* init work */
ASSERT(buf_init(&work, frame.buf.h= eadroom));

- init_implicit_iv(co);
update_time();=

/* Test encryption, decryption for all pac= ket sizes */
@@ -389,24 +391,36 @@
gc_= free(&gc);
}

-
-
struct crypto_options
-init_crypto_options(const char *cipher, const = char *auth)
+init_cry= pto_options(const char *cipher, const char *auth, int flags,
+ struct key2 *s= tatickey)

{
- struct key2 key2 =3D { .n =3D 2};
+ struct key2 key2 =3D {.n =3D 2};

- ASSERT(rand_b= ytes(key2.keys[0].cipher, sizeof(key2.keys[0].cipher)));
- ASSERT(rand_bytes(key2.keys[0].hmac,= sizeof(key2.keys[0].hmac)));
- ASSERT(rand_bytes(key2.keys[1].cipher, sizeof(key2.keys[1].ciph= er)));
- ASSERT(rand= _bytes(key2.keys[1].hmac, sizeof(key2.keys)[1].hmac));
+ if (statickey)
+ {
+ /* Use chosen static key instead of random key = when defined */
+ = key2 =3D *statickey;
+ }
+ else<= /span>
+ {
+ ASSERT(rand_bytes(key2.key= s[0].cipher, sizeof(key2.keys[0].cipher)));

+ ASSERT(rand_bytes(key2.keys[0].hmac, sizeof= (key2.keys[0].hmac)));
+ ASSERT= (rand_bytes(key2.keys[1].hmac, sizeof(key2.keys)[1].hmac));

- struct crypto_o= ptions co =3D { 0 };
= + }
+
+ struct crypto_options co =3D= {0};


struct key_type kt =3D create_= kt(cipher, auth, "ssl-test");

= init_key_ctx_bi(&co.key_ctx_bi, &key2, 0, &kt, "unit-t= est-ssl");
- pa= cket_id_init(&co.packet_id, 5, 5, "UNITTEST", 0);
= + packet_id_init(&co.pac= ket_id, 5, 5, "UNITTEST", 0);
+
= + co.flags |=3D flags;
+
+ init_impl= icit_iv(&co, &key2);

return = co;
}
@@ -416,7 +430,6 @@
= {
packet_id_free(&co->packet_id);
free_key_ctx_bi(&co->key_ctx_bi);

-
}
/* This adds a few more methods than strictly necessary but this al= lows
@@ -425,8 +438,27 @@
static void
run_data_channel_with_cipher_end(const char *cipher) {
- struct c= rypto_options co =3D init_crypto_options(cipher, "none");<= br>- co.flags |=3D CO_AEAD_TAG= _AT_THE_END;
+ str= uct crypto_options co =3D init_crypto_options(cipher, "none",
+ = CO_AEAD_TAG_AT_THE_END, NULL);
+
+ do_data_channel_round_trip(&co);
+ uninit_crypto_options(&co)= ;

+}
+
+static void
+run_data_channel_with_cipher_end_and_long_pkt_counter(const cha= r *cipher)
+{<= br>+ struct crypto_options c= o =3D init_crypto_options(cipher, "none",
+ = CO_AEAD_TAG_AT_THE_END | CO_64_BIT_PKT_ID, NULL);
+ do_data_channel_round_trip(&= ;co);

+ uninit_cry= pto_options(&co);
+}
+
+static void

+run_data_channel_with_long_pkt_counter(co= nst char *cipher)
+{<= /span>
+ struct crypto_op= tions co =3D init_crypto_options(cipher, "none",
+ = CO_64_BIT_PKT_ID, NULL);
do_data_channe= l_round_trip(&co);
uninit_crypto_options(&co);=
}
@@ -434,31 +466,36 @@
= static void
run_data_channel_with_cipher(const char *ciphe= r, const char *auth)
{
- struct crypto_options co =3D init_crypto_options(ciph= er, auth);
+ struc= t crypto_options co =3D init_crypto_options(cipher, auth, 0, NULL);<= br> do_data_channel_round_trip(&co);
uni= nit_crypto_options(&co);
}
+static void
+run_aead_channel_tests(const char *ci= pher)
+{
+ run_data_channel_with_cipher= _end(cipher);

+ ru= n_data_channel_with_cipher(cipher, "none");
+ run_data_channel_with_cipher_end_and_= long_pkt_counter(cipher);
+ run_data_channel_with_long_pkt_counter(cipher);
+}

= static void
test_data_channel_roundtrip_aes_128_gcm(void *= *state)
{
- run_data_channel_with_cipher_end("AES-128-GCM");<= br>- run_data_channel_with_cip= her("AES-128-GCM", "none");
+ run_aead_channel_tests("AES-128-GCM&qu= ot;);
}

static void
test_data_channel_roundtrip_aes_192_gcm(void **state)<= br> {
- run_da= ta_channel_with_cipher_end("AES-192-GCM");
- run_data_channel_with_cipher("AES-= 192-GCM", "none");
+ run_aead_channel_tests("AES-192-GCM"); }

static void
= test_data_channel_roundtrip_aes_256_gcm(void **state)
{
- run_data_channel_wit= h_cipher_end("AES-256-GCM");
- run_data_channel_with_cipher("AES-256-GCM", = "none");
+ = run_aead_channel_tests("AES-256-GCM");
}

static void
@@ -488,8 +525,7= @@
return;
}

- run_data_channe= l_with_cipher_end("ChaCha20-Poly1305");
- run_data_channel_with_cipher("ChaCha20= -Poly1305", "none");
+ run_aead_channel_tests("ChaCha20-Poly1305");<= /span>
}

static void@@ -503,6 +539,155 @@
run_data_channel_with_cip= her("BF-CBC", "SHA1");
}

+static struct k= ey2
+create_key(void)=
+{
+ struct key2 key2 =3D {.n =3D 2};<= /span>
+
+ const uint8_t key[] =3D
<= span style=3D"color: hsl(120, 100%, 40%);">+ {'a', 'b', = 'c', 'd', 'e', 'f', 'g', 'h'= ;, '0', '1', '2', '3', '4', '5&= #39;, '6', '7', 'A', 'B', 'C', '= ;D', 'E', 'F',

+ 'G', 'H', 'j', 'k', = 9;u', 'c', 'h', 'e', 'n', 'l'};=
+
+ static_assert(sizeof(key) =3D=3D 3= 2, "Size of key should be 32 bytes");
+
+ /* copy the key a few times to ensure to have the size we need= for
+ * Staticke= y but XOR it to not repeat it */
+ uint8_t keydata[sizeof(key2.keys)];
+
+ for (int i =3D 0; i < sizeof(key2.keys); i++)<= br>+ {
+ keydata[i] =3D (uint8_t) (key[i %= sizeof(key)] ^ i);
+= }
+
+

+ ASSERT(memcpy(key2.keys[0].cipher, keydata, size= of(key2.keys[0].cipher)));
+ ASSERT(memcpy(key2.keys[0].hmac, keydata + 64, sizeof(key2.keys[= 0].hmac)));
+ ASSE= RT(memcpy(key2.keys[1].cipher, keydata + 128, sizeof(key2.keys[1].cipher)))= ;
+ ASSERT(memcpy(= key2.keys[1].hmac, keydata + 192, sizeof(key2.keys)[1].hmac));
+

+ return key2;
+}
+static void
+test_data_channel_known_= vectors_run(bool longpktcounter)
+{
+ s= truct key2 key2 =3D create_key();
+
+ i= nt flags =3D longpktcounter ? CO_64_BIT_PKT_ID : 0;
+ flags |=3D CO_AEAD_TAG_AT_THE_END;
+
+ struct crypto_options co =3D init_cry= pto_options("AES-256-GCM", "none", flags,
+ = &key2);

+
+ struc= t gc_arena gc =3D gc_new();
+
+ /* init= ialise frame for the test */
+ struct frame frame;
+ init_frame_parameters(&frame);
+
+ struct buffer src =3D alloc_buf_gc(frame.buf.payload_siz= e, &gc);
+ str= uct buffer work =3D alloc_buf_gc(BUF_SIZE(&frame), &gc);
= + struct buffer encrypt_work= space =3D alloc_buf_gc(BUF_SIZE(&frame), &gc);
+ struct buffer decrypt_workspace =3D = alloc_buf_gc(BUF_SIZE(&frame), &gc);
+ struct buffer buf =3D clear_buf();
<= span style=3D"color: hsl(120, 100%, 40%);">+ void *buf_p;

+

+ /* init work */
+ ASSERT(buf_init(&work, frame.buf.headroom));<= /span>
+
+ now =3D 0;
+
+ /* msg(M_INFO, "TESTING ENCRYPT/DECRYPT of packet leng= th=3D%d", i); */
+
+ /*
= + * Load src with known dat= a.
+ */+ ASSERT(buf_init(&src,= 0));
+ const char= *plaintext =3D "The quick little fox jumps over the bureaucratic hurd= les";
++ ASSERT(buf_write(&sr= c, plaintext, strlen(plaintext)));
+
+ = /* copy source to input buf */
+ buf =3D work;
+ buf_p =3D buf_write_alloc(&buf, BLEN(&src));+ ASSERT(buf_p);
= + memcpy(buf_p, BPTR(&sr= c), BLEN(&src));
= +
+ /* initialize = work buffer with buf.headroom bytes of prepend capacity */
+ ASSERT(buf_init(&encrypt_wor= kspace, frame.buf.headroom));
+
+ /* ad= d packet opcode and peer id */
+ buf_write_u8(&encrypt_workspace, 7);
+ buf_write_u8(&encrypt_workspace= , 0);
+ buf_write_= u8(&encrypt_workspace, 0);
+ buf_write_u8(&encrypt_workspace, 23);
+
+ /* encrypt */
+ openvpn_encrypt(&buf, encrypt_workspace, &co);<= /span>
+
+ /* separate buffer in authenticated= data and encrypted data */
+ uint8_t *ad_start =3D BPTR(&buf);
+ buf_advance(&buf, 4);
+

+ if (longpktcounter)
+ {
+ uint8_t packetid1[8] =3D {0, 0, 0, 0, 0, 0, 0, 1};
+ assert_memory_equ= al(BPTR(&buf), packetid1, 8);
+ }
+= else
+ {
+ uint8_t packetid1= [4] =3D {0, 0, 0, 1};
+ assert_memory_equal(BPTR(&buf), packetid1, 4);
+ }

+
+ uint8_t *tag_location =3D BEND(&buf) - OPENVPN_AEAD_TAG_LEN= GTH;
+
+ if (longpktcounter)

= + {
+ const uint8_t exp_tag_long[16] =3D
+ {0x52, 0xee, = 0xef, 0xdb, 0x34, 0xb7, 0xbd, 0x79, 0xfe, 0xbf, 0x69, 0xd0, 0x4e, 0x92, 0xf= e, 0x4b};
+ as= sert_memory_equal(tag_location, exp_tag_long, OPENVPN_AEAD_TAG_LENGTH);
+ }
+ else
+ {
+ const uint8_t exp_tag_short[16] =3D
+ {0x1f, 0xdd, 0x90, 0x8f, 0x0e,= 0x9d, 0xc2, 0x5e, 0x79, 0xd8, 0x32, 0x02, 0x0d, 0x58, 0xe7, 0x3f};<= br>+ assert_memory_equal= (tag_location, exp_tag_short, OPENVPN_AEAD_TAG_LENGTH);
+ }
+
+ {
+ = const uint8_t bytesat14[6] =3D {0xc7, 0x40, 0x47, 0x81, 0xac, 0x8c};
+ assert_memory_equ= al(BPTR(&buf) + 14, bytesat14, sizeof(bytesat14));
+ }
+ else
+ {
+ = const uint8_t bytesat14[6] =3D {0xa8, 0x2e, 0x6b, 0x17, 0x06, 0xd9};
+ assert_memory_= equal(BPTR(&buf) + 14, bytesat14, sizeof(bytesat14));
+ }
+
+ /* decrypt */
+
= + /* compare */
+ assert_int_equal(buf.len, st= rlen(plaintext));

+ = assert_memory_equal(BPTR(&buf), plaintext, strlen(plaintext));=
+
+ uninit_crypto_options(&co);+ gc_free(&gc);<= br>+}
+
+static void
+{
+ test_data_channel_known_vectors_run(true);<= br>+}
+
+static void
+{
+ test_data_channel_known_vectors_run(false);
+}

int
main(void)
@@ -521,6 +706,= 8 @@
cmocka_unit_test(test_data_channel_roundtrip_= aes_192_cbc),
cmocka_unit_test(test_data_channel_r= oundtrip_aes_256_cbc),
cmocka_unit_test(test_data_= channel_roundtrip_bf_cbc),
+ cmocka_unit_test(test_data_channel_known_vectors_longpktid),=
+ cmocka_unit= _test(test_data_channel_known_vectors_shortpktid)
};

#if defined(ENABLE_CRYPTO_OPENSSL)=

To view, visit change 507. To unsubscribe, or for = help writing mail filters, visit settings.

=
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I01e258e97351b5aa4b9e561f5b3= 5ddc2318569e2
Gerrit-Change-Number: 507
Gerrit-PatchSet: 10
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-Reviewer: stipa <lstipakov@...277...= >
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
Gerrit-Attention: stipa <lstipakov@...1663...= .>
Gerrit-MessageType: newpatchset
--Iy1K01MvZco=--