/
URandom.kt
142 lines (123 loc) · 4.18 KB
/
URandom.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
* Copyright (c) 2023 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
@file:Suppress("SpellCheckingInspection", "UnnecessaryOptInAnnotation")
package io.matthewnelson.secure.random.internal
import io.matthewnelson.secure.random.SecRandomCopyException
import kotlinx.cinterop.*
import platform.posix.*
import kotlin.native.concurrent.AtomicInt
/**
* Helper for:
* - Ensuring /dev/random has been seeded
* - Obtaining bytes from /dev/urandom
*
* Polling of /dev/random via [ensureSeeded] is always called
* to ensure that no data is read from /dev/urandom until there
* is adequate randomness generated. [ensureSeeded] only ever
* polls /dev/random once, afterwhich it does nothing.
*
* @see [SecRandomSynchronized]
* @see [readBytesTo]
* */
internal class URandom private constructor(): SecRandomSynchronized() {
internal companion object {
private const val INFINITE_TIMEOUT = -1
internal val instance = URandom()
}
private val lock = Lock()
/**
* Reads bytes from /dev/urandom of specified [buflen] into [buf].
*
* @see [withReadOnlyFD]
* @see [fillCompletely]
* */
@Throws(SecRandomCopyException::class)
internal fun readBytesTo(buf: Pinned<ByteArray>, buflen: Int) {
ensureSeeded()
lock.withLock {
@OptIn(UnsafeNumber::class)
withReadOnlyFD("/dev/urandom") { fd ->
buf.fillCompletely(buflen) { ptr, length ->
read(fd, ptr, length.toULong().convert()).convert()
}
}
}
}
/**
* Polls /dev/random once and only once to ensure /dev/urandom
* is ready to read from. Any sucessive calls to [ensureSeeded]
* will block until [synchronizedRemember]'s lambda is executed.
* */
private fun ensureSeeded() {
synchronizedRemember { _, _ ->
// Will only ever be invoked once
withReadOnlyFD("/dev/random") { fd ->
memScoped {
val pollFd = nativeHeap.alloc<pollfd>()
pollFd.apply {
this.fd = fd
events = POLLIN.convert()
revents = 0
}
while (true) {
@OptIn(UnsafeNumber::class)
val result = poll(pollFd.ptr, 1, INFINITE_TIMEOUT)
if (result >= 0) {
break
}
when (val err = errno) {
EINTR,
EAGAIN -> continue
else -> throw errnoToSecRandomCopyException(err)
}
}
}
}
true
}
}
/**
* Opens the file descriptor for [path] and then closes
* it once [block] completes.
* */
private fun withReadOnlyFD(path: String, block: (fd: Int) -> Unit) {
val fd = open(path, O_RDONLY, null)
if (fd == -1) throw errnoToSecRandomCopyException(errno)
try {
block.invoke(fd)
} finally {
close(fd)
}
}
private inner class Lock {
// 0: Unlocked
// *: Locked
private val lock = AtomicInt(0)
fun <T> withLock(block: () -> T): T {
val lockId = Any().hashCode()
try {
while (true) {
if (lock.compareAndSet(0, lockId)) {
break
}
}
return block.invoke()
} finally {
lock.compareAndSet(lockId, 0)
}
}
}
}