Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build-logic/src/main/kotlin/dokka.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,6 @@ extensions.configure<DokkaExtension> {
}

pluginsConfiguration.html {
footerMessage.set("© 2025-${LocalDate.now().year} Copyright KotlinCrypto")
footerMessage.set("© 2023-${LocalDate.now().year} Copyright KotlinCrypto")
}
}
1 change: 1 addition & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ org.gradle.caching=true

kotlin.code.style=official
kotlin.mpp.applyDefaultHierarchyTemplate=false
kotlin.mpp.enableCInteropCommonization=true
kotlin.mpp.stability.nowarn=true
kotlin.native.ignoreDisabledTargets=true

Expand Down
17 changes: 17 additions & 0 deletions library/crypto-rand/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
* See the License for the specific language governing permissions and
* limitations under the License.
**/
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTarget
import org.jetbrains.kotlin.konan.target.Family

plugins {
id("configuration")
}
Expand Down Expand Up @@ -48,5 +51,19 @@ kmpConfiguration {
}
}
}

kotlin {
val cInteropDir = projectDir
.resolve("src")
.resolve("nativeInterop")
.resolve("cinterop")

targets.filterIsInstance<KotlinNativeTarget>().forEach target@ { target ->
if (target.konanTarget.family != Family.ANDROID) return@target
target.compilations["main"].cinterops.create("syscall") {
definitionFile.set(cInteropDir.resolve("$name.def"))
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) 2025 KotlinCrypto
*
* 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("FunctionName", "NOTHING_TO_INLINE", "KotlinRedundantDiagnosticSuppress", "SpellCheckingInspection")

package org.kotlincrypto.random.internal

import kotlinx.cinterop.ExperimentalForeignApi

@OptIn(ExperimentalForeignApi::class)
internal actual inline fun _SYS_getrandom(): Int = SYS_getrandom
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,20 @@ import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

// https://docs.piston.rs/dev_menu/libc/constant.SYS_getrandom.html
private const val SYS_getrandom: Long = 318L
// https://docs.piston.rs/dev_menu/libc/constant.GRND_NONBLOCK.html
private const val GRND_NONBLOCK: UInt = 0x0001u
@Suppress("FunctionName")
internal expect inline fun _SYS_getrandom(): Int

@OptIn(ExperimentalForeignApi::class, UnsafeNumber::class)
private inline fun getrandom2(buf: CPointer<ByteVar>, buflen: size_t, flags: u_int): Int {
return syscall(SYS_getrandom.convert(), buf, buflen, flags).convert()
return syscall(_SYS_getrandom().convert(), buf, buflen, flags).convert()
}

// getrandom(2) available for Linux Kernel 3.17+ (Android API 23+)
@OptIn(ExperimentalForeignApi::class, UnsafeNumber::class)
internal val HAS_GET_RANDOM: Boolean by lazy(LazyThreadSafetyMode.SYNCHRONIZED) {
val buf = ByteArray(1)
val result = buf.usePinned { pinned ->
getrandom2(pinned.addressOf(0), buf.size.toULong().convert(), GRND_NONBLOCK)
getrandom2(pinned.addressOf(0), buf.size.convert(), 0x0001u /* GRND_NONBLOCK */)
}
if (result >= 0) return@lazy true

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright (c) 2025 KotlinCrypto
*
* 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("FunctionName", "NOTHING_TO_INLINE", "KotlinRedundantDiagnosticSuppress", "SpellCheckingInspection")

package org.kotlincrypto.random.internal

import platform.linux.SYS_getrandom

internal actual inline fun _SYS_getrandom(): Int = SYS_getrandom
3 changes: 3 additions & 0 deletions library/crypto-rand/src/nativeInterop/cinterop/syscall.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SYS_getrandom for AndroidNative targets
package = org.kotlincrypto.random.internal
headers = sys/syscall.h