mirror of
https://github.com/signalapp/Signal-Server.git
synced 2026-11-30 01:16:50 +00:00
More explicitly link TOTP key length with the TOTP HMAC algorithm
This commit is contained in:
1 parent
c09e167f3f
commit
d1ec5d7d9c
2 files changed
+14
-7
No files matched your search
+11
-4
@@ -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 -> {
|
||||
|
||||
+3
-3
@@ -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()),
|
||||
|
||||
Reference in new issue
Block a user