From d1ec5d7d9c46d6ad6d0080fe28f7ba23ca39ec42 Mon Sep 17 00:00:00 2001 From: Jon Chambers Date: Thu, 27 Aug 2026 08:54:58 -0400 Subject: [PATCH] More explicitly link TOTP key length with the TOTP HMAC algorithm --- .../textsecuregcm/storage/AccountsManager.java | 15 +++++++++++---- .../storage/AccountsManagerTest.java | 6 +++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java index 04608400f..b7dc33ffa 100644 --- a/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java +++ b/service/src/main/java/org/whispersystems/textsecuregcm/storage/AccountsManager.java @@ -203,9 +203,6 @@ public class AccountsManager extends RedisPubSubAdapter implemen @VisibleForTesting static final String LINK_DEVICE_VERIFICATION_TOKEN_ALGORITHM = "HmacSHA256"; - @VisibleForTesting - static final int TOTP_KEY_LENGTH_BITS = 160; - @VisibleForTesting public static final TimeBasedOneTimePasswordGenerator TOTP = new TimeBasedOneTimePasswordGenerator(); @@ -339,7 +336,7 @@ public class AccountsManager extends RedisPubSubAdapter implemen try { this.totpKeyGenerator = KeyGenerator.getInstance(TOTP.getAlgorithm()); - totpKeyGenerator.init(TOTP_KEY_LENGTH_BITS); + totpKeyGenerator.init(getTotpKeyLengthBits()); } catch (final NoSuchAlgorithmException e) { throw new AssertionError("Every implementation of the Java platform is required to support the HmacSHA256 KeyGenerator algorithm", e); } @@ -347,6 +344,16 @@ public class AccountsManager extends RedisPubSubAdapter implemen this.pubSubConnection = pubSubRedisClient.createPubSubConnection(); } + @VisibleForTesting + static int getTotpKeyLengthBits() { + try { + // The HOTP/TOTP spec recommends using a key length that's the same as the HMAC block length + return Mac.getInstance(TOTP.getAlgorithm()).getMacLength() * 8; + } catch (final NoSuchAlgorithmException e) { + throw new AssertionError("Algorithm used by TOTP generator not found", e); + } + } + @Override public void start() { pubSubConnection.usePubSubConnection(connection -> { diff --git a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java index b8c45e505..ddd1df7bb 100644 --- a/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java +++ b/service/src/test/java/org/whispersystems/textsecuregcm/storage/AccountsManagerTest.java @@ -2010,7 +2010,7 @@ class AccountsManagerTest { final AnnotatedTotpKey confirmedTotpKey; { final KeyGenerator totpKeyGenerator = KeyGenerator.getInstance(AccountsManager.TOTP.getAlgorithm()); - totpKeyGenerator.init(AccountsManager.TOTP_KEY_LENGTH_BITS); + totpKeyGenerator.init(AccountsManager.getTotpKeyLengthBits()); confirmedTotpKey = new AnnotatedTotpKey( new TotpKey(TOTP_PARAMETERS, totpKeyGenerator.generateKey().getEncoded()), @@ -2104,7 +2104,7 @@ class AccountsManagerTest { final AnnotatedTotpKey secondTotpKey; { final KeyGenerator totpKeyGenerator = KeyGenerator.getInstance(AccountsManager.TOTP.getAlgorithm()); - totpKeyGenerator.init(AccountsManager.TOTP_KEY_LENGTH_BITS); + totpKeyGenerator.init(AccountsManager.getTotpKeyLengthBits()); totpKey = new AnnotatedTotpKey( new TotpKey(TOTP_PARAMETERS, totpKeyGenerator.generateKey().getEncoded()), @@ -2141,7 +2141,7 @@ class AccountsManagerTest { final AnnotatedTotpKey totpKey; { final KeyGenerator totpKeyGenerator = KeyGenerator.getInstance(AccountsManager.TOTP.getAlgorithm()); - totpKeyGenerator.init(AccountsManager.TOTP_KEY_LENGTH_BITS); + totpKeyGenerator.init(AccountsManager.getTotpKeyLengthBits()); totpKey = new AnnotatedTotpKey( new TotpKey(TOTP_PARAMETERS, totpKeyGenerator.generateKey().getEncoded()),