Skip to content

Commit 4ebf4e5

Browse files
authored
detekt rules (#5)
* enable detekt rules for undocumented public classes and functions, enhance documentation, fix CryptoScope * remove pull request trigger from CodeQL workflow
1 parent bce3352 commit 4ebf4e5

File tree

20 files changed

+77
-53
lines changed

20 files changed

+77
-53
lines changed

.github/workflows/codeql.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ name: "CodeQL Analysis"
33
on:
44
push:
55
branches: [ "main" ]
6-
pull_request:
7-
branches: [ "main" ]
86
schedule:
97
- cron: '27 0 * * 2'
108

config/detekt/detekt.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ complexity:
1414
active: false
1515

1616
comments:
17-
# FIXME: Activate before launch
1817
UndocumentedPublicClass:
19-
active: false
18+
active: true
2019
UndocumentedPublicFunction:
21-
active: false
20+
active: true
2221

2322
naming:
2423
MatchingDeclarationName:

crypto-jvm-lib/src/main/kotlin/de/gematik/openhealth/crypto/internal/interop/NativeLoader.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ private val hostArch =
3030
.trim()
3131
.replace(" ", "")
3232

33+
/**
34+
* Loads the native crypto library for the current platform.
35+
*
36+
* @throws UnsatisfiedLinkError if the library cannot be loaded.
37+
*/
3338
@Suppress("UnsafeDynamicallyLoadedCode")
3439
fun loadNativeLibrary() {
3540
try {

crypto/src/commonMain/kotlin/de/gematik/openhealth/crypto/CryptoScope.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ abstract class CryptoScope {
5151
*/
5252
fun CmacSpec.createCmac(secret: SecretKey): Cmac = nativeCreateCmac(this@CryptoScope, secret)
5353

54+
/**
55+
* Creates a Hash instance.
56+
*/
5457
fun HashSpec.createHash(): Hash = nativeCreateHash(this@CryptoScope)
5558

5659
/**
@@ -99,8 +102,8 @@ fun <R : Any?> useCrypto(block: CryptoScope.() -> R): R = nativeUseCrypto(block)
99102
* ensuring that all resources are properly closed or released afterward.
100103
*/
101104
suspend fun <R : Any?> useCryptoAsync(block: suspend CryptoScope.() -> R): R =
102-
nativeUseCrypto(block)
105+
nativeUseCryptoSuspendable(block)
103106

104107
internal expect fun <R : Any?> nativeUseCrypto(block: CryptoScope.() -> R): R
105108

106-
internal expect suspend fun <R : Any?> nativeUseCrypto(block: suspend CryptoScope.() -> R): R
109+
internal expect suspend fun <R : Any?> nativeUseCryptoSuspendable(block: suspend CryptoScope.() -> R): R

crypto/src/commonMain/kotlin/de/gematik/openhealth/crypto/kem/Kem.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ data class KemDecapsulationResult(
8989
interface KemEncapsulation {
9090
val spec: KemSpec
9191

92+
/**
93+
* Encapsulates a key and returns the shared secret and wrapped key.
94+
*/
9295
fun encapsulate(): KemEncapsulationResult
9396
}
9497

@@ -99,8 +102,14 @@ interface KemEncapsulation {
99102
interface KemDecapsulation {
100103
val spec: KemSpec
101104

105+
/**
106+
* Returns the encapsulation key.
107+
*/
102108
fun encapsulationKey(): ByteArray
103109

110+
/**
111+
* Decapsulates the wrapped key and returns the shared secret.
112+
*/
104113
fun decapsulate(wrappedKey: ByteArray): KemDecapsulationResult
105114
}
106115

crypto/src/commonMain/kotlin/de/gematik/openhealth/crypto/key/EcKey.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@ enum class EcCurve(
168168

169169
val g: EcPoint get() = EcPoint(this, x, y)
170170

171+
/**
172+
* Creates a new EC point with the given x and y coordinates.
173+
*/
171174
fun point(
172175
x: BigInteger?,
173176
y: BigInteger?,
@@ -231,6 +234,7 @@ class EcPublicKey internal constructor(
231234

232235
override fun toString(): String = "EcPublicKey(data=${data.contentToString()}, curve=$curve)"
233236

237+
@Suppress("UndocumentedPublicClass")
234238
companion object {
235239
const val OID: String = "1.2.840.10045.2.1"
236240
}
@@ -354,6 +358,7 @@ class EcPrivateKey internal constructor(
354358

355359
override fun toString(): String = "EcPrivateKey(data=${data.contentToString()}, curve=$curve)"
356360

361+
@Suppress("UndocumentedPublicClass")
357362
companion object
358363
}
359364

@@ -395,6 +400,9 @@ fun EcPrivateKey.encodeToAsn1(): ByteArray =
395400
}
396401
}
397402

403+
/**
404+
* Encodes the private key as a PEM-encoded string.
405+
*/
398406
fun EcPrivateKey.encodeToPem(): String =
399407
Pem(type = "EC PRIVATE KEY", data = encodeToAsn1()).encodeToString()
400408

crypto/src/commonMain/kotlin/de/gematik/openhealth/crypto/key/EcPoint.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,28 @@ data class EcPoint(
9292
*/
9393
val isInfinity: Boolean get() = x == null && y == null
9494

95+
/**
96+
* Adds this EC point to another EC point.
97+
*/
9598
operator fun plus(other: EcPoint): EcPoint = nativePlus(other)
9699

100+
/**
101+
* Multiplies this EC point by a scalar value.
102+
*/
97103
operator fun times(k: BigInteger): EcPoint = nativeTimes(k)
98104

105+
/**
106+
* Negates this EC point.
107+
*/
99108
fun negate(): EcPoint = if (isInfinity) this else curve.point(x, (curve.p - y!!).mod(curve.p))
100109
}
101110

102111
internal expect fun EcPoint.nativeTimes(k: BigInteger): EcPoint
103112

104113
internal expect fun EcPoint.nativePlus(other: EcPoint): EcPoint
105114

115+
/**
116+
* Converts this [EcPoint] to an [EcPublicKey].
117+
*/
106118
@ExperimentalCryptoApi
107119
fun EcPoint.toEcPublicKey(): EcPublicKey = EcPublicKey(curve, uncompressed)

crypto/src/commonMain/kotlin/de/gematik/openhealth/crypto/key/Key.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,24 @@ import de.gematik.openhealth.crypto.ExperimentalCryptoApi
2121
import de.gematik.openhealth.crypto.bytes
2222
import kotlin.js.JsExport
2323

24+
/**
25+
* Represents a key in the cryptographic system.
26+
*/
2427
@JsExport
2528
@ExperimentalCryptoApi
2629
interface Key {
2730
val data: ByteArray
2831
}
2932

33+
/**
34+
* Represents a secret key in the cryptographic system.
35+
*/
3036
@JsExport
3137
@ExperimentalCryptoApi
3238
class SecretKey(
3339
override val data: ByteArray,
3440
) : Key {
35-
val length: ByteUnit = data.size.bytes
41+
private val length: ByteUnit = data.size.bytes
3642

3743
override fun equals(other: Any?): Boolean {
3844
if (this === other) return true

crypto/src/commonTest/kotlin/de/gematik/openhealth/crypto/key/EcPointBrainpoolP384r1Test.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import de.gematik.openhealth.crypto.runTestWithProvider
55
import kotlin.test.Test
66
import kotlin.test.assertEquals
77

8-
@Suppress("ktlint:standard:max-line-length", "MaxLineLength", "LongMethod")
8+
@Suppress("MaxLineLength", "LongMethod")
99
class EcPointBrainpoolP384r1Test {
1010
private val curve = EcCurve.BrainpoolP384r1
1111

crypto/src/commonTest/kotlin/de/gematik/openhealth/crypto/key/EcPointBrainpoolP512r1Test.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import de.gematik.openhealth.crypto.runTestWithProvider
55
import kotlin.test.Test
66
import kotlin.test.assertEquals
77

8-
@Suppress("ktlint:standard:max-line-length", "MaxLineLength", "LongMethod")
8+
@Suppress("MaxLineLength", "LongMethod")
99
class EcPointBrainpoolP512r1Test {
1010
private val curve = EcCurve.BrainpoolP512r1
1111

0 commit comments

Comments
 (0)