More explicitly link TOTP key length with the TOTP HMAC algorithm

This commit is contained in:
Jon Chambers authored and Jon Chambers committed 2026-08-27 17:35:05 -04:00
1 parent c09e167f3f
commit d1ec5d7d9c
2 files changed
+14 -7

No files matched your search

@@ -203,9 +203,6 @@ public class AccountsManager extends RedisPubSubAdapter<String, String> 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<String, String> 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<String, String> 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 -> {
@@ -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()),