Skip to content

Commit e89eba5

Browse files
committed
Make SecureRandom private
We want to ensure the application doesn't access it directly and instead uses our utility functions that will mix in additional randomness.
1 parent 804ea1d commit e89eba5

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/commonMain/kotlin/fr/acinq/lightning/crypto/LocalKeyManager.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package fr.acinq.lightning.crypto
33
import fr.acinq.bitcoin.*
44
import fr.acinq.bitcoin.DeterministicWallet.derivePrivateKey
55
import fr.acinq.bitcoin.DeterministicWallet.hardened
6-
import fr.acinq.lightning.Lightning.secureRandom
76
import fr.acinq.lightning.channel.ChannelKeys
87
import fr.acinq.lightning.channel.RecoveredChannelKeys
8+
import fr.acinq.lightning.Lightning.randomLong
99
import fr.acinq.lightning.transactions.Transactions
1010

1111
data class LocalKeyManager(val seed: ByteVector, val chainHash: ByteVector32) : KeyManager {
@@ -58,7 +58,7 @@ data class LocalKeyManager(val seed: ByteVector, val chainHash: ByteVector32) :
5858

5959
override fun newFundingKeyPath(isFunder: Boolean): KeyPath {
6060
val last = hardened(if (isFunder) 1 else 0)
61-
fun next() = secureRandom.nextInt().toLong() and 0xFFFFFFFF
61+
fun next() = randomLong() and 0xFFFFFFFF
6262
return KeyPath(listOf(next(), next(), next(), next(), next(), next(), next(), next(), last))
6363
}
6464

src/commonMain/kotlin/fr/acinq/lightning/eclair.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import kotlin.random.Random
1010

1111
object Lightning {
1212

13-
val secureRandom = Random.secure()
13+
private val secureRandom = Random.secure()
1414

1515
fun randomBytes(length: Int): ByteArray {
1616
val buffer = ByteArray(length)
@@ -24,10 +24,14 @@ object Lightning {
2424

2525
fun randomKeyPath(length: Int): KeyPath {
2626
val path = mutableListOf<Long>()
27-
repeat(length) { path.add(secureRandom.nextLong()) }
27+
repeat(length) { path.add(randomLong()) }
2828
return KeyPath(path)
2929
}
3030

31+
fun randomLong(): Long {
32+
return secureRandom.nextLong()
33+
}
34+
3135
fun toLongId(fundingTxHash: ByteVector32, fundingOutputIndex: Int): ByteVector32 {
3236
require(fundingOutputIndex < 65536) { "fundingOutputIndex must not be greater than FFFF" }
3337
val x1 = fundingTxHash[30] xor (fundingOutputIndex.shr(8)).toByte()

0 commit comments

Comments
 (0)