-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support getrandom sys call * use getrandom by default with fallback to urandom * cache file descriptor for urandom in ThreadLocal * test both urandom and getrandom implementations
- Loading branch information
Showing
8 changed files
with
123 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
package = dev.whyoleg.cryptography.random.internal.cinterop | ||
headers = linux/random.h |
21 changes: 1 addition & 20 deletions
21
cryptography-random/main/sources/linux/CryptographyRandom.linux.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,3 @@ | ||
package dev.whyoleg.cryptography.random | ||
|
||
import kotlinx.cinterop.* | ||
import platform.posix.* | ||
|
||
internal actual fun defaultCryptographyRandom(): CryptographyRandom = URandomCryptographyRandom | ||
|
||
private object URandomCryptographyRandom : PlatformRandom() { | ||
override fun fillBytes(array: ByteArray) { | ||
val file = checkNotNull(fopen("/dev/urandom", "rb")) { "Failed to open /dev/urandom" } | ||
val result = array.usePinned { pinned -> | ||
fread( | ||
__ptr = pinned.addressOf(0), | ||
__size = 1.convert(), | ||
__n = pinned.get().size.convert(), | ||
__stream = file | ||
) | ||
} | ||
fclose(file) | ||
if (result <= 0UL) error("Failed to read from /dev/urandom") | ||
} | ||
} | ||
internal actual fun defaultCryptographyRandom(): CryptographyRandom = createGetRandom() ?: createURandom() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package dev.whyoleg.cryptography.random | ||
|
||
import dev.whyoleg.cryptography.random.internal.cinterop.* | ||
import kotlinx.cinterop.* | ||
import platform.linux.* | ||
import platform.posix.* | ||
|
||
internal fun createGetRandom(): CryptographyRandom? = if (getRandomAvailable()) GetRandom else null | ||
|
||
private object GetRandom : LinuxRandom() { | ||
override fun fillBytes(pointer: CPointer<ByteVar>, size: Int): Int = getrandom(pointer, size.convert(), 0.convert()) | ||
} | ||
|
||
private fun getRandomAvailable(): Boolean { | ||
val stubArray = ByteArray(1) | ||
val stubSize = stubArray.size | ||
stubArray.usePinned { | ||
if (getrandom(it.addressOf(0), stubSize.convert(), GRND_NONBLOCK.convert()) >= 0) return true | ||
} | ||
|
||
return when (errno) { | ||
ENOSYS, EPERM -> false | ||
else -> true | ||
} | ||
} | ||
|
||
private fun getrandom(out: CPointer<ByteVar>?, outSize: size_t, flags: UInt): Int = | ||
syscall(SYS_getrandom.convert(), out, outSize, flags).convert() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dev.whyoleg.cryptography.random | ||
|
||
import kotlinx.cinterop.* | ||
|
||
internal abstract class LinuxRandom : PlatformRandom() { | ||
protected abstract fun fillBytes(pointer: CPointer<ByteVar>, size: Int): Int | ||
final override fun fillBytes(array: ByteArray) { | ||
val size = array.size | ||
array.usePinned { | ||
var filled = 0 | ||
while (filled < size) { | ||
val chunkSize = fillBytes(it.addressOf(filled), size - filled) | ||
if (chunkSize < 0) errnoCheck() | ||
filled += chunkSize | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package dev.whyoleg.cryptography.random | ||
|
||
import kotlinx.cinterop.* | ||
import platform.posix.* | ||
|
||
internal fun createURandom(): CryptographyRandom { | ||
awaitURandomReady() | ||
return URandom | ||
} | ||
|
||
private object URandom : LinuxRandom() { | ||
override fun fillBytes(pointer: CPointer<ByteVar>, size: Int): Int = read(FD.value, pointer, size.convert()).convert() | ||
} | ||
|
||
@ThreadLocal | ||
private object FD { | ||
val value = open("/dev/urandom") | ||
} | ||
|
||
private fun awaitURandomReady() { | ||
val randomFd = open("/dev/random") | ||
try { | ||
memScoped { | ||
val pollFd = alloc<pollfd> { | ||
fd = randomFd | ||
events = POLLIN.convert() | ||
revents = 0 | ||
} | ||
|
||
while (true) { | ||
if (poll(pollFd.ptr, 1, -1) >= 0) break | ||
|
||
when (errno) { | ||
EINTR, EAGAIN -> continue | ||
else -> errnoCheck() | ||
} | ||
} | ||
} | ||
} finally { | ||
close(randomFd) | ||
} | ||
} | ||
|
||
private fun open(path: String): Int { | ||
val fd = open(path, O_RDONLY, null) | ||
if (fd <= 0) errnoCheck() | ||
return fd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package dev.whyoleg.cryptography.random | ||
|
||
import platform.posix.* | ||
|
||
internal fun errnoCheck(): Nothing { | ||
val message = when (val value = errno) { | ||
EFAULT -> "The address referred to by buf is outside the accessible address space." | ||
EINTR -> "The call was interrupted by a signal handler; see the description of how interrupted read(2) calls on 'slow' devices are handled with and without the SA_RESTART flag in the signal(7) man page." | ||
EINVAL -> "An invalid flag was specified in flags." | ||
else -> "POSIX error: $value" | ||
} | ||
error(message) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package dev.whyoleg.cryptography.random | ||
|
||
class URandomTest : CryptographyRandomTest(createURandom()) | ||
|
||
class GetRandomTest : CryptographyRandomTest(createGetRandom()!!) |