Commit 1f66aad7b3 for frr
commit 1f66aad7b3a699180f94a3b860fa74509df0f67d
Author: Abdul Wasey <w453y.me@gmail.com>
Date: Fri Oct 30 18:01:17 2026 +0000
bfdd: compute keyed SHA1 as RFC 5880 does for sha-1 keys
bfdd computes keyed SHA1 as an HMAC-SHA1 over the packet with the
digest field zeroed. RFC 5880 Section 6.7.4 puts the key, zero padded,
in the digest field and takes the SHA1 of the packet, so bfdd does not
interoperate with implementations that follow it: keyed and meticulous
keyed SHA1 sessions with BIRD never come up.
Use the RFC construction for a key whose algorithm is sha-1, on send
and on receive. A hmac-sha-1 key keeps the HMAC, so existing
configurations and sessions with earlier releases are unchanged.
Signed-off-by: Abdul Wasey <w453y.me@gmail.com>
diff --git a/bfdd/bfd.c b/bfdd/bfd.c
index de413b2835..f5e747d2f6 100644
--- a/bfdd/bfd.c
+++ b/bfdd/bfd.c
@@ -665,6 +665,7 @@ enum bfd_auth_type map_keychain_algo_to_bfd_auth_type(enum keychain_hash_algo kc
{
switch (kc_algo) {
case KEYCHAIN_ALGO_HMAC_SHA1:
+ case KEYCHAIN_ALGO_SHA1:
return meticulous ? BFD_AUTH_TYPE_METICULOUS_KEYED_SHA1 : BFD_AUTH_TYPE_KEYED_SHA1;
case KEYCHAIN_ALGO_NULL:
case KEYCHAIN_ALGO_CLEARTEXT:
@@ -673,7 +674,6 @@ enum bfd_auth_type map_keychain_algo_to_bfd_auth_type(enum keychain_hash_algo kc
case KEYCHAIN_ALGO_HMAC_SHA256:
case KEYCHAIN_ALGO_HMAC_SHA384:
case KEYCHAIN_ALGO_HMAC_SHA512:
- case KEYCHAIN_ALGO_SHA1:
case KEYCHAIN_ALGO_MAX:
default:
return BFD_AUTH_TYPE_RESERVED;
@@ -703,10 +703,10 @@ struct key *bfd_keychain_key_find_active(const struct keychain *keychain, bool m
for (ALL_LIST_ELEMENTS_RO(keychain->key, node, key)) {
size_t keylen, maxlen;
- if (meticulous && key->hash_algo != KEYCHAIN_ALGO_HMAC_SHA1)
+ if (meticulous && !bfd_auth_algo_is_sha1(key->hash_algo))
continue;
if (key->hash_algo != KEYCHAIN_ALGO_CLEARTEXT &&
- key->hash_algo != KEYCHAIN_ALGO_HMAC_SHA1)
+ !bfd_auth_algo_is_sha1(key->hash_algo))
continue;
if (!key->string)
continue;
@@ -717,9 +717,8 @@ struct key *bfd_keychain_key_find_active(const struct keychain *keychain, bool m
* is 28 bytes whatever the key length, so the longer key does
* not grow the packet.
*/
- maxlen = key->hash_algo == KEYCHAIN_ALGO_HMAC_SHA1
- ? BFD_AUTH_SHA1_KEY_MAX_LEN
- : BFD_AUTH_SIMPLE_PASSWD_MAX_LEN;
+ maxlen = bfd_auth_algo_is_sha1(key->hash_algo) ? BFD_AUTH_SHA1_KEY_MAX_LEN
+ : BFD_AUTH_SIMPLE_PASSWD_MAX_LEN;
keylen = strlen(key->string);
if (keylen < BFD_AUTH_SIMPLE_PASSWD_MIN_LEN || keylen > maxlen)
continue;
diff --git a/bfdd/bfd.h b/bfdd/bfd.h
index 01f50df16b..bbf81d8053 100644
--- a/bfdd/bfd.h
+++ b/bfdd/bfd.h
@@ -744,6 +744,16 @@ const struct bfd_session *bfd_session_next(const struct bfd_session *bs, bool mh
uint32_t bfd_mode);
void bfd_sessions_remove_manual(void);
void bfd_profiles_remove(void);
+/*
+ * Key chain algorithms that give keyed SHA1. "sha-1" is the construction of
+ * RFC 5880 Section 6.7.4; "hmac-sha-1" is an HMAC-SHA1 over the packet,
+ * which earlier releases used and which only interoperates with them.
+ */
+static inline bool bfd_auth_algo_is_sha1(enum keychain_hash_algo algo)
+{
+ return algo == KEYCHAIN_ALGO_SHA1 || algo == KEYCHAIN_ALGO_HMAC_SHA1;
+}
+
extern enum bfd_auth_type map_keychain_algo_to_bfd_auth_type(enum keychain_hash_algo kc_algo,
bool meticulous);
extern const char *bfd_auth_type_get_description(enum bfd_auth_type auth_type);
diff --git a/bfdd/bfd_packet.c b/bfdd/bfd_packet.c
index 5c83f21dce..26191c7775 100644
--- a/bfdd/bfd_packet.c
+++ b/bfdd/bfd_packet.c
@@ -544,14 +544,22 @@ void ptm_bfd_snd(struct bfd_session *bfd, int fbit)
memcpy(auth_payload_ptr, &seq_num_n, sizeof(seq_num_n));
auth_payload_ptr += sizeof(seq_num_n);
- /* SHA1 Auth Data is 20 bytes.
- * Ensure we only HMAC the packet with the 20-byte digest area zeroed.
- */
+ /* SHA1 Auth Data is 20 bytes. */
memset(auth_payload_ptr, 0, 20);
-
- /* Note: HMAC-SHA1 results in 20 bytes */
- HMAC(md_alg, key->string, strlen(key->string), send_buffer, packet_len,
- digest, &digest_len);
+ if (key->hash_algo == KEYCHAIN_ALGO_SHA1) {
+ /*
+ * RFC 5880 Section 6.7.4: the key, zero
+ * padded, in place of the digest, then SHA1
+ * over the whole packet.
+ */
+ memcpy(auth_payload_ptr, key->string, strlen(key->string));
+ EVP_Digest(send_buffer, packet_len, digest, &digest_len, md_alg,
+ NULL);
+ } else {
+ /* HMAC-SHA1 over the packet, digest zeroed. */
+ HMAC(md_alg, key->string, strlen(key->string), send_buffer,
+ packet_len, digest, &digest_len);
+ }
memcpy(auth_payload_ptr, digest, 20);
}
@@ -1173,11 +1181,17 @@ static bool bfd_check_auth(struct bfd_session *bfd, const struct bfd_pkt *cp)
received_digest = auth_section + 8;
memcpy(msgbuf, cp, cp->len);
- /* Zero out the 20-byte digest field in the temp packet before hashing */
+ /* Recompute over the packet as sent, as in ptm_bfd_snd(). */
memset(msgbuf + BFD_PKT_LEN + 8, 0, digest_len);
-
- HMAC(md_alg, key->string, strlen(key->string), msgbuf, cp->len, computed_digest,
- &digest_len);
+ if (key->hash_algo == KEYCHAIN_ALGO_SHA1) {
+ if (strlen(key->string) > digest_len)
+ return false;
+ memcpy(msgbuf + BFD_PKT_LEN + 8, key->string, strlen(key->string));
+ EVP_Digest(msgbuf, cp->len, computed_digest, &digest_len, md_alg, NULL);
+ } else {
+ HMAC(md_alg, key->string, strlen(key->string), msgbuf, cp->len,
+ computed_digest, &digest_len);
+ }
if (memcmp(received_digest, computed_digest, digest_len) != 0) {
cp_debug(CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_MH), &peer_sa, &local_sa,
diff --git a/doc/user/bfd.rst b/doc/user/bfd.rst
index b49f1d52bd..74aa640c9e 100644
--- a/doc/user/bfd.rst
+++ b/doc/user/bfd.rst
@@ -267,14 +267,22 @@ BFD peers and profiles share the same BFD session configuration commands.
Configure peer or profile to use a key-chain. That key-chain refers
to a list of available key tuple made up of a crypto algorithm and
- a key-string. Those values are used in BFD packets. Clear text and
- hmac-sha1 algorithm is currently supported.
-
- A key is used only if its ``cryptographic-algorithm`` is one of those
- two, and if its ``key-string`` is within the length RFC 5880 allows:
- 1 to 16 bytes for clear text, up to 20 bytes for hmac-sha1. A key
- defaults to no algorithm, so configuring ``key-string`` on its own
- leaves nothing the session can use.
+ a key-string. Those values are used in BFD packets. The following
+ ``cryptographic-algorithm`` values are supported:
+
+ - ``cleartext``: Simple Password authentication.
+ - ``sha-1``: Keyed SHA1 as RFC 5880 Section 6.7.4 describes. Use this
+ to interoperate with other BFD implementations.
+ - ``hmac-sha-1``: Keyed SHA1 computed as an HMAC-SHA1 over the packet.
+ Earlier releases used this for keyed SHA1. It only interoperates
+ with FRR using ``hmac-sha-1``, so both ends of a session have to be
+ changed to ``sha-1`` together.
+
+ A key is used only if its ``cryptographic-algorithm`` is one of those,
+ and if its ``key-string`` is within the length RFC 5880 allows: 1 to 16
+ bytes for clear text, up to 20 bytes for keyed SHA1. A key defaults to
+ no algorithm, so configuring ``key-string`` on its own leaves nothing
+ the session can use.
A session whose key-chain holds no usable key stays down. It is not
brought up unauthenticated, since that would leave the link
@@ -289,7 +297,7 @@ BFD peers and profiles share the same BFD session configuration commands.
.. clicmd:: authentication algorithm meticulous
Configure peer or profile to use meticulous mode when the key-chain
- is configured with a hmac-sha1 crypto algorithm.
+ is configured with a ``sha-1`` or ``hmac-sha-1`` crypto algorithm.
BFD Peer Specific Commands
--------------------------
@@ -977,10 +985,10 @@ Apply the Profile to the BFD Peer:
peer 10.0.1.2 interface eth-rt2
profile 0
-Example 2: Applying hmac-sha-1 Authentication via a Profile
+Example 2: Applying Keyed SHA1 Authentication via a Profile
This method uses a BFD profile, which is useful for applying the same
-hmac-sha-1 authentication settings to multiple peers.
+keyed SHA1 authentication settings to multiple peers.
Define the Keychain and Key:
@@ -989,7 +997,7 @@ Define the Keychain and Key:
key chain 0
key 0
key-string secret123456
- cryptographic-algorithm hmac-sha-1
+ cryptographic-algorithm sha-1
Create a BFD Profile and Link the Keychain: