fix: Ensure valid private key generation (#2160)

This commit is contained in:
James Rich 2025-06-19 01:30:08 +00:00 committed by GitHub
parent 1556019803
commit 1928fb64fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -118,6 +118,7 @@ fun SecurityConfigItemList(
onConfirm = { newConfig ->
securityInput = newConfig
showKeyGenerationDialog = false
onConfirm(securityInput)
},
onDismiss = { showKeyGenerationDialog = false }
)
@ -294,6 +295,7 @@ fun SecurityConfigItemList(
}
}
@Suppress("MagicNumber")
@Composable
fun PrivateKeyRegenerateDialog(
showKeyGenerationDialog: Boolean,
@ -312,12 +314,17 @@ fun PrivateKeyRegenerateDialog(
onClick = {
securityInput = securityInput.copy {
clearPrivateKey()
@Suppress("MagicNumber")
privateKey = ByteString.copyFrom(
ByteArray(32).apply {
SecureRandom().nextBytes(this)
}
)
// Generate a random "f" value
val f = ByteArray(32).apply {
SecureRandom().nextBytes(this)
}
// Adjust the value to make it valid as an "s" value for eval().
// According to the specification we need to mask off the 3
// right-most bits of f[0], mask off the left-most bit of f[31],
// and set the second to left-most bit of f[31].
f[0] = (f[0].toInt() and 0xF8).toByte()
f[31] = ((f[31].toInt() and 0x7F) or 0x40).toByte()
privateKey = ByteString.copyFrom(f)
}
onConfirm(securityInput)
},