@@ -33,6 +33,7 @@ private const val EPHEMERAL_KEY_LENGTH = 1024
3333private const val HEX = 16
3434private const val DERIVED_KEY_INFO = " Caldera Derived Key"
3535private const val DERIVED_KEY_SIZE = 16
36+ private const val SRP_A_GEN_WAIT = 50L
3637
3738// Precomputed safe 3072-bit prime 'N', as decimal.
3839// https://datatracker.ietf.org/doc/html/rfc5054#appendix-A (Page 16)
@@ -59,22 +60,24 @@ private enum class SrpGenerationState { NOT_STARTED, STARTED, COMPLETED }
5960class SRPHelper (userPool : String ) {
6061 @Suppress(" VariableNaming" )
6162 private val N = BigInteger .fromTwosComplementByteArray(nByteArray)
62-
6363 private val creator = ModularBigInteger .creatorForModulo(N )
6464 private val g = creator.fromInt(2 )
65-
6665 private val random = SecureRandom ()
67-
6866 private val k: BigInteger = BigInteger .fromTwosComplementByteArray(kByteArray)
67+ private val digest = SHA256 ()
68+ private val userPoolName: String
69+
6970 private var srpState: SrpGenerationState = SrpGenerationState .NOT_STARTED
71+
72+ @Suppress(" LateinitUsage" )
7073 private lateinit var privateA: BigInteger
74+
75+ @Suppress(" LateinitUsage" )
7176 private lateinit var publicA: ModularBigInteger
77+
7278 var timestamp: String = nowAsFormattedString()
7379 internal set
7480
75- private val digest = SHA256 ()
76- private val userPoolName: String
77-
7881 init {
7982 if (userPool.contains(" _" )) {
8083 this .userPoolName = userPool.split(Regex (" _" ), 2 )[1 ]
@@ -97,11 +100,12 @@ class SRPHelper(userPool: String) {
97100 // A = (g ^ a) % N
98101 publicA = g.pow(privateA)
99102 } while (publicA.residue == BigInteger .ZERO )
103+ srpState = SrpGenerationState .COMPLETED
100104 publicA.toString(HEX )
101105 }
102106
103107 SrpGenerationState .STARTED -> {
104- do { delay(10 ) } while (srpState != SrpGenerationState .COMPLETED )
108+ do { delay(SRP_A_GEN_WAIT ) } while (srpState != SrpGenerationState .COMPLETED )
105109 publicA.toString(HEX )
106110 }
107111 SrpGenerationState .COMPLETED -> publicA.toString(HEX )
@@ -214,21 +218,21 @@ class SRPHelper(userPool: String) {
214218 userIdForSrp : String ,
215219 password : String ,
216220 ): String {
217- val bigIntSRPB = BigInteger .parseString(srpB, HEX )
221+ val bigIntSrpB = BigInteger .parseString(srpB, HEX )
218222 val bigIntSalt = BigInteger .parseString(salt, HEX )
219223
220224 // Check B's validity
221- if (bigIntSRPB .mod(N ) == BigInteger .ZERO ) {
225+ if (bigIntSrpB .mod(N ) == BigInteger .ZERO ) {
222226 throw CognitoException .BadSrpB
223227 }
224228
225- val uValue = computeU(bigIntSRPB )
229+ val uValue = computeU(bigIntSrpB )
226230 if (uValue.mod(N ) == BigInteger .ZERO ) {
227231 throw CognitoException .HashOfAAndSrpBCannotBeZero
228232 }
229233
230234 val xValue = computeX(salt = bigIntSalt, userIdForSrp = userIdForSrp, password = password)
231- val sValue = computeS(uValue, xValue, bigIntSRPB )
235+ val sValue = computeS(uValue, xValue, bigIntSrpB )
232236 val key = computePasswordAuthenticationKey(sValue, uValue)
233237 val m1Signature = generateM1Signature(
234238 key = key,
0 commit comments