From 1928fb64fa67483c17276e0713543a6866f717e8 Mon Sep 17 00:00:00 2001 From: James Rich <2199651+jamesarich@users.noreply.github.com> Date: Thu, 19 Jun 2025 01:30:08 +0000 Subject: [PATCH] fix: Ensure valid private key generation (#2160) --- .../components/SecurityConfigItemList.kt | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/geeksville/mesh/ui/radioconfig/components/SecurityConfigItemList.kt b/app/src/main/java/com/geeksville/mesh/ui/radioconfig/components/SecurityConfigItemList.kt index 326078d5e..c3982b3c9 100644 --- a/app/src/main/java/com/geeksville/mesh/ui/radioconfig/components/SecurityConfigItemList.kt +++ b/app/src/main/java/com/geeksville/mesh/ui/radioconfig/components/SecurityConfigItemList.kt @@ -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) },