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

Commit

Permalink
Fix random number generation in OpenSSLKey and fix error printing
Browse files Browse the repository at this point in the history
  • Loading branch information
Cach30verfl0w committed Jun 15, 2024
1 parent 6bd29ea commit d1754f4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import kotlinx.cinterop.UByteVar
import kotlinx.cinterop.get
import kotlinx.cinterop.reinterpret
import kotlinx.cinterop.toKString
import libssl.ERR_func_error_string
import libssl.ERR_error_string
import libssl.ERR_get_error
import libssl.RAND_bytes

Expand Down Expand Up @@ -57,8 +57,8 @@ class OpenSSLKey(private val secureHeap: SecureHeap,
): 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())
if (RAND_bytes(rawDataPtr, dataSize.toInt()) != 1) {
throw Exception(ERR_error_string(ERR_get_error(), null)?.toKString())
}

return OpenSSLKey(secureHeap, purposes, algorithm, rawDataPtr, dataSize, type)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ 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
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.CPointerVar
import kotlinx.cinterop.CValuesRef
import kotlinx.cinterop.ExperimentalForeignApi
import kotlinx.cinterop.IntVar
import kotlinx.cinterop.UByteVar
import kotlinx.cinterop.UIntVar
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.alloc
Expand All @@ -48,7 +44,6 @@ import libssl.EVP_DigestInit
import libssl.EVP_DigestUpdate
import libssl.EVP_MAX_MD_SIZE
import libssl.EVP_MD
import libssl.EVP_MD_CTX
import libssl.EVP_MD_CTX_free
import libssl.EVP_MD_CTX_new
import libssl.EVP_PKEY_free
Expand All @@ -64,21 +59,11 @@ import libssl.EVP_sha3_256
import libssl.EVP_sha3_384
import libssl.EVP_sha3_512
import libssl.EVP_sha512
import libssl.MD5
import libssl.RSAPublicKey_dup
import libssl.RSA_F4
import libssl.RSA_free
import libssl.RSA_generate_key_ex
import libssl.RSA_new
import libssl.SHA1
import libssl.SHA224
import libssl.SHA224_DIGEST_LENGTH
import libssl.SHA256
import libssl.SHA256_DIGEST_LENGTH
import libssl.SHA384
import libssl.SHA384_DIGEST_LENGTH
import libssl.SHA512
import libssl.SHA512_DIGEST_LENGTH

class OpenSSLCryptoProvider: AbstractProvider(
"Default",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import libssl.CRYPTO_secure_malloc
import libssl.CRYPTO_secure_malloc_done
import libssl.CRYPTO_secure_malloc_init
import libssl.CRYPTO_secure_malloc_initialized
import libssl.ERR_func_error_string
import libssl.ERR_error_string
import libssl.ERR_get_error

/**
Expand Down Expand Up @@ -66,7 +66,7 @@ class SecureHeap(size: ULong, minSize: ULong): AutoCloseable {
* @since 12/06/2024
*/
fun allocate(size: ULong): COpaquePointer = CRYPTO_secure_malloc(size, this.toString(), 47)
?: throw Exception(ERR_func_error_string(ERR_get_error())?.toKString())
?: throw Exception(ERR_error_string(ERR_get_error(), null)?.toKString())

/**
* This method frees the allocated memory from the secure heap. This also deletes the data
Expand Down

0 comments on commit d1754f4

Please sign in to comment.