Skip to content
This repository has been archived by the owner on Jul 8, 2022. It is now read-only.

Commit

Permalink
Allows to seed extra random bytes (#509)
Browse files Browse the repository at this point in the history
  • Loading branch information
soywiz authored Mar 20, 2022
1 parent b32d449 commit 228e0c8
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ actual fun fillRandomBytes(array: ByteArray) {
jrandom.nextBytes(array)
}

actual fun seedExtraRandomBytes(array: ByteArray) {
jrandom.setSeed(array)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import com.soywiz.krypto.internal.arraycopy
import kotlin.random.Random

expect fun fillRandomBytes(array: ByteArray)
expect fun seedExtraRandomBytes(array: ByteArray)
var randomUnittesting = false

object SecureRandom : Random() {
fun addSeed(array: ByteArray) {
seedExtraRandomBytes(array)
}

private fun getInt(): Int {
val temp = ByteArray(4)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class SecureRandomTest {
@Test
fun test() {
randomUnittesting = true // Required for android not mocking some stuff required for PRNGFixes
SecureRandom.addSeed(byteArrayOf(1, 2, 3)) // This shouldn't reduce entropy
println(SecureRandom.nextBytes(15).toList())
println(SecureRandom.nextBytes(15).toList())
assertNotEquals(SecureRandom.nextBytes(16).toList(), SecureRandom.nextBytes(16).toList())
Expand Down
6 changes: 6 additions & 0 deletions krypto/src/jsMain/kotlin/com/soywiz/krypto/SecureRandomJs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ private fun require_node(name: String): dynamic = eval("(${REQ}uire('$name'))")

actual fun fillRandomBytes(array: ByteArray) {
if (isNodeJs) {
// https://nodejs.org/api/crypto.html#cryptorandomfillsyncbuffer-offset-size
require_node("crypto").randomFillSync(Uint8Array(array.unsafeCast<Int8Array>().buffer))
} else {
// https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
_global.crypto.getRandomValues(array)
}
}

actual fun seedExtraRandomBytes(array: ByteArray) {
// No implementation for this?
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ private val jrandom = SecureRandom()
actual fun fillRandomBytes(array: ByteArray) {
jrandom.nextBytes(array)
}

actual fun seedExtraRandomBytes(array: ByteArray) {
jrandom.setSeed(array)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ private val BCryptGenRandomDynamic by lazy {
}

actual fun fillRandomBytes(array: ByteArray) {
memScoped {
val temp1 = allocArray<ByteVar>(array.size)
val ptr = temp1.getPointer(this)
if (array.size == 0) return

array.usePinned { pin ->
val ptr = pin.addressOf(0)
val status = BCryptGenRandomDynamic(null, ptr.reinterpret(), array.size.convert(), 2.convert())
//println("status = $status")
for (n in 0 until array.size) array[n] = ptr[n]
}
}

actual fun seedExtraRandomBytes(array: ByteArray) {
// No implementation for this?
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import kotlinx.cinterop.*
import platform.posix.*

actual fun fillRandomBytes(array: ByteArray) {
memScoped {
val temp1 = allocArray<ByteVar>(1024)
val ptr = temp1.getPointer(this)
if (array.isEmpty()) return

array.usePinned { pin ->
val ptr = pin.addressOf(0)
val file = fopen("/dev/urandom", "rb")
if (file != null) {
fread(ptr, 1.convert(), array.size.convert(), file)
Expand All @@ -15,3 +16,17 @@ actual fun fillRandomBytes(array: ByteArray) {
}
}
}

actual fun seedExtraRandomBytes(array: ByteArray) {
if (array.isEmpty()) return

array.usePinned { pin ->
val ptr = pin.addressOf(0)
val file = fopen("/dev/urandom", "wb")
if (file != null) {
fwrite(ptr, 1.convert(), array.size.convert(), file)
for (n in 0 until array.size) array[n] = ptr[n]
fclose(file)
}
}
}

0 comments on commit 228e0c8

Please sign in to comment.