This repository has been archived by the owner on Aug 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e006e77
commit aa0dc47
Showing
14 changed files
with
141 additions
and
39 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
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
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
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
49 changes: 49 additions & 0 deletions
49
kmp-advcrypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/keys/OpenSSLKey.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 |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright (c) 2024 Cach30verfl0w | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
package io.karma.advcrypto.linux.keys | ||
|
||
import io.karma.advcrypto.keys.Key | ||
import io.karma.advcrypto.linux.utils.SecureHeap | ||
import kotlinx.cinterop.ExperimentalForeignApi | ||
import kotlinx.cinterop.UByteVar | ||
import kotlinx.cinterop.reinterpret | ||
import kotlinx.cinterop.toKString | ||
import libssl.ERR_func_error_string | ||
import libssl.ERR_get_error | ||
import libssl.RAND_bytes | ||
|
||
@OptIn(ExperimentalForeignApi::class, ExperimentalStdlibApi::class) | ||
class OpenSSLKey(private val secureHeap: SecureHeap, val keySize: Int, | ||
override val purposes: UByte, override val algorithm: String): AutoCloseable, Key { | ||
private val rawDataPtr = secureHeap.allocate((keySize / 8).toULong()).reinterpret<UByteVar>() | ||
|
||
override fun close() { | ||
secureHeap.free((keySize / 8).toULong(), rawDataPtr) | ||
} | ||
|
||
companion object { | ||
fun generateRandom(secureHeap: SecureHeap, keySize: Int, purposes: UByte, | ||
algorithm: String): OpenSSLKey = | ||
OpenSSLKey(secureHeap, keySize, purposes, algorithm).apply { | ||
if (RAND_bytes(rawDataPtr, 1) != 1) { | ||
throw Exception(ERR_func_error_string(ERR_get_error())?.toKString()) | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
28 changes: 0 additions & 28 deletions
28
...rypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/providers/DefaultCryptoProvider.kt
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
...rypto/src/linuxX64Main/kotlin/io/karma/advcrypto/linux/providers/OpenSSLCryptoProvider.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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) 2024 Cach30verfl0w | ||
* | ||
* 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 | ||
* | ||
* http://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. | ||
*/ | ||
|
||
package io.karma.advcrypto.linux.providers | ||
|
||
import io.karma.advcrypto.AbstractProvider | ||
import io.karma.advcrypto.algorithm.delegates.KeyGenContext | ||
import io.karma.advcrypto.keys.Key | ||
import io.karma.advcrypto.linux.keys.OpenSSLKey | ||
import io.karma.advcrypto.linux.utils.SecureHeap | ||
|
||
class OpenSSLCryptoProvider: AbstractProvider( | ||
"Default", | ||
"This class provides access to the default asymmetric and symmetric algorithms", | ||
"1.0.0-Dev" | ||
) { | ||
private val secureHeap = SecureHeap(UShort.MAX_VALUE.toULong() + 1u, 0u) // TODO: How to free with good API design | ||
|
||
init { | ||
algorithm("AES") { | ||
keyGenerator<Unit>(Key.PURPOSES_SYMMETRIC, arrayOf(128, 196, 256), 256) { | ||
initializer { spec -> KeyGenContext(spec, Unit) } | ||
generateKey { context -> | ||
OpenSSLKey.generateRandom( | ||
secureHeap, | ||
context.generatorSpec.keySize?: defaultKeySize, | ||
context.generatorSpec.purposes, | ||
"AES" | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
kmp-advcrypto/src/linuxX64Test/kotlin/io.karma.advcrypto.linux.tests/KeyGeneratorTests.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.karma.advcrypto.linux.tests | ||
|
||
import io.karma.advcrypto.Providers | ||
import io.karma.advcrypto.algorithm.specs.KeyGeneratorSpec | ||
import io.karma.advcrypto.keys.Key | ||
import io.karma.advcrypto.linux.keys.OpenSSLKey | ||
import io.karma.advcrypto.linux.providers.OpenSSLCryptoProvider | ||
import io.karma.advcrypto.wrapper.KeyGenerator | ||
import kotlin.test.Test | ||
|
||
class KeyGeneratorTests { | ||
|
||
@Test | ||
fun testAES() { | ||
if (Providers.getProviderByName("Default") == null) { | ||
Providers.addProvider(OpenSSLCryptoProvider()) | ||
} | ||
|
||
(KeyGenerator.getInstance("AES") | ||
.initialize(KeyGeneratorSpec.Builder(Key.PURPOSES_SYMMETRIC).setKeySize(256).build()) | ||
.generateKey() as OpenSSLKey).close() | ||
} | ||
|
||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
headers = openssl/crypto.h openssl/err.h | ||
headers = openssl/crypto.h openssl/err.h openssl/aes.h openssl/rand.h openssl/evp.h | ||
linkerOpts = -L/usr/lib/x86_64-linux-gnu -L/usr/lib -ldl -lpthread -lc -lm -lssl -lcrypto | ||
compilerOpts = -I/usr/include -I/usr/include/x86_64-linux-gnu |