Skip to content

Commit f0fee2e

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 99ea310 commit f0fee2e

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,7 +3,7 @@ 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
6+
import fr.acinq.lightning.Lightning.randomLong
77
import fr.acinq.lightning.transactions.Transactions
88

99
data class LocalKeyManager(val seed: ByteVector, val chainHash: ByteVector32) : KeyManager {
@@ -56,7 +56,7 @@ data class LocalKeyManager(val seed: ByteVector, val chainHash: ByteVector32) :
5656

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

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)