Skip to content
This repository has been archived by the owner on Aug 12, 2024. It is now read-only.

Commit

Permalink
Add type field to Key
Browse files Browse the repository at this point in the history
  • Loading branch information
Cach30verfl0w committed Jun 14, 2024
1 parent fb872f2 commit c8bd593
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.karma.advcrypto.android

import android.annotation.SuppressLint
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import io.karma.advcrypto.algorithm.Algorithm
Expand All @@ -25,6 +26,7 @@ import io.karma.advcrypto.algorithm.delegates.SignatureDelegate
import io.karma.advcrypto.android.keys.AndroidKey
import io.karma.advcrypto.keys.Key
import io.karma.advcrypto.keys.KeyPair
import io.karma.advcrypto.keys.enum.KeyType
import java.security.KeyPairGenerator
import java.security.MessageDigest
import java.security.PrivateKey
Expand Down Expand Up @@ -59,16 +61,19 @@ fun KeyGeneratorDelegate<KeyPairGenerator>.androidKeyPairGenerator() {
KeyPair(
AndroidKey(
keyPair.public,
purposes and Key.PURPOSE_SIGNING.inv()
purposes and Key.PURPOSE_SIGNING.inv(),
KeyType.PUBLIC
),
AndroidKey(
keyPair.private,
purposes and Key.PURPOSE_VERIFY.inv()
purposes and Key.PURPOSE_VERIFY.inv(),
KeyType.PRIVATE
)
)
}
}

@SuppressLint("WrongConstant")
fun KeyGeneratorDelegate<KeyPairGenerator>.androidKeyPair(defaultBlockMode: BlockMode, algorithm: String) {
initializer { initSpec ->
val purposes = purposesToAndroid(initSpec.purposes)
Expand All @@ -88,6 +93,7 @@ fun KeyGeneratorDelegate<KeyPairGenerator>.androidKeyPair(defaultBlockMode: Bloc
androidKeyPairGenerator()
}

@SuppressLint("WrongConstant")
fun KeyGeneratorDelegate<KeyGenerator>.androidKey(defaultBlockMode: BlockMode, algorithm: String) {
initializer { initSpec ->
val purposes = purposesToAndroid(initSpec.purposes)
Expand All @@ -107,7 +113,8 @@ fun KeyGeneratorDelegate<KeyGenerator>.androidKey(defaultBlockMode: BlockMode, a
generateKey { context ->
AndroidKey(
context.internalContext.generateKey(),
context.generatorSpec.purposes
context.generatorSpec.purposes,
KeyType.SECRET
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@
package io.karma.advcrypto.android.keys

import io.karma.advcrypto.keys.Key
import io.karma.advcrypto.keys.enum.KeyType

typealias RawKey = java.security.Key

class AndroidKey(val raw: RawKey, override val purposes: UByte): Key {
class AndroidKey(val raw: RawKey, override val purposes: UByte, override val type: KeyType): Key {
override val algorithm: String = raw.algorithm

override fun toString(): String {
Expand Down
11 changes: 11 additions & 0 deletions kmp-advcrypto/src/commonMain/kotlin/io/karma/advcrypto/keys/Key.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package io.karma.advcrypto.keys

import io.karma.advcrypto.keys.enum.KeyType

/**
* This interface represents every single key which can be generated by this library. These keys are
* used to perform operations like signing or encrypting data etc.
Expand Down Expand Up @@ -46,6 +48,15 @@ interface Key: AutoCloseable {
*/
val purposes: UByte

/**
* This value represents the type of this specific key. This type identifies a public, private
* or secret key.
*
* @author Cedric Hammes
* @since 14/06/2024
*/
val type: KeyType

companion object {
const val PURPOSES_ALL: UByte = 0b0000_1111U
const val PURPOSES_SYMMETRIC: UByte = 0b0000_1100U
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.karma.advcrypto.linux.keys

import io.karma.advcrypto.keys.Key
import io.karma.advcrypto.keys.enum.KeyType
import io.karma.advcrypto.linux.utils.SecureHeap
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.ExperimentalForeignApi
Expand All @@ -32,22 +33,29 @@ class OpenSSLKey(private val secureHeap: SecureHeap,
override val purposes: UByte,
override val algorithm: String,
private val rawDataPtr: CPointer<UByteVar>,
private val rawDataSize: ULong
private val rawDataSize: ULong,
override val type: KeyType
): Key {

override fun close() {
secureHeap.free(rawDataSize, rawDataPtr)
}

companion object {
fun generateRandom(secureHeap: SecureHeap, keySize: Int, purposes: UByte, algorithm: String): OpenSSLKey {
fun generateRandom(
secureHeap: SecureHeap,
keySize: Int,
purposes: UByte,
algorithm: String,
type: KeyType
): OpenSSLKey {
val dataSize = (keySize / 8).toULong()
val rawDataPtr = secureHeap.allocate((keySize / 8).toULong()).reinterpret<UByteVar>()
if (RAND_bytes(rawDataPtr, 1) != 1) {
throw Exception(ERR_func_error_string(ERR_get_error())?.toKString())
}

return OpenSSLKey(secureHeap, purposes, algorithm, rawDataPtr, dataSize)
return OpenSSLKey(secureHeap, purposes, algorithm, rawDataPtr, dataSize, type)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.karma.advcrypto.linux.keys

import io.karma.advcrypto.keys.Key
import io.karma.advcrypto.keys.enum.KeyType
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.toKString
Expand All @@ -28,7 +29,8 @@ import libssl.EVP_PKEY_get_base_id
import libssl.OBJ_nid2sn

@OptIn(ExperimentalForeignApi::class)
class OpenSSLPKey(private val rawKey: CPointer<EVP_PKEY>, override val purposes: UByte): Key {
class OpenSSLPKey(private val rawKey: CPointer<EVP_PKEY>, override val purposes: UByte,
override val type: KeyType): Key {
override val algorithm: String = when(val baseId = EVP_PKEY_get_base_id(rawKey)) {
EVP_PKEY_RSA -> "RSA"
EVP_PKEY_ED25519 -> "ED25519"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import io.karma.advcrypto.Providers
import io.karma.advcrypto.algorithm.delegates.KeyGenContext
import io.karma.advcrypto.keys.Key
import io.karma.advcrypto.keys.KeyPair
import io.karma.advcrypto.keys.enum.KeyType
import io.karma.advcrypto.linux.keys.OpenSSLKey
import io.karma.advcrypto.linux.keys.OpenSSLPKey
import io.karma.advcrypto.linux.utils.SecureHeap
Expand Down Expand Up @@ -88,7 +89,8 @@ class OpenSSLCryptoProvider: AbstractProvider(
secureHeap,
context.generatorSpec.keySize?: defaultKeySize,
context.generatorSpec.purposes,
"AES"
"AES",
KeyType.SECRET
)
}
}
Expand Down Expand Up @@ -135,8 +137,16 @@ class OpenSSLCryptoProvider: AbstractProvider(

// Return key pair
KeyPair(
OpenSSLPKey(publicKey!!, (purposes and (Key.PURPOSE_ENCRYPT or Key.PURPOSE_VERIFY))),
OpenSSLPKey(privateKey!!, (purposes and (Key.PURPOSE_DECRYPT or Key.PURPOSE_SIGNING)))
OpenSSLPKey(
publicKey!!,
(purposes and (Key.PURPOSE_ENCRYPT or Key.PURPOSE_VERIFY)),
KeyType.PUBLIC
),
OpenSSLPKey(
privateKey!!,
(purposes and (Key.PURPOSE_DECRYPT or Key.PURPOSE_SIGNING)),
KeyType.PRIVATE
)
)
}

Expand Down

0 comments on commit c8bd593

Please sign in to comment.