ADded helper extension toPtr instead of addressOf(0), implemented jvm and native secret stream

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-08-27 19:27:46 +02:00
1 parent 2c92a8142f
commit 9e10677165
10 files changed
+165 -28

No files matched your search

@@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.authenticated
import com.ionspin.kotlin.bignum.integer.util.hexColumsPrint
import com.ionspin.kotlin.crypto.InvalidTagException
import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.*
import libsodium.*
@@ -22,7 +23,7 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
val ciphertext = UByteArray(ciphertextLength)
val ciphertextPinned = ciphertext.pin()
crypto_aead_xchacha20poly1305_ietf_encrypt(
ciphertextPinned.addressOf(0),
ciphertextPinned.toPtr(),
ulongArrayOf(ciphertextLength.convert()).toCValues(),
message.toCValues(),
message.size.convert(),
@@ -46,7 +47,7 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
val message = UByteArray(messageLength)
val messagePinned = message.pin()
crypto_aead_xchacha20poly1305_ietf_decrypt(
messagePinned.addressOf(0),
messagePinned.toPtr(),
ulongArrayOf(messageLength.convert()).toCValues(),
null,
ciphertext.toCValues(),
@@ -95,7 +96,7 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
actual fun initializeForEncryption(key: UByteArray) : UByteArray {
val pinnedHeader = header.pin()
crypto_secretstream_xchacha20poly1305_init_push(state.ptr, pinnedHeader.addressOf(0), key.toCValues())
crypto_secretstream_xchacha20poly1305_init_push(state.ptr, pinnedHeader.toPtr(), key.toCValues())
println("state-----------")
state.ptr.readBytes(crypto_secretstream_xchacha20poly1305_state.size.toInt()).asUByteArray().hexColumsPrint()
println("state-----------")
@@ -116,7 +117,7 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
val ciphertextWithTagPinned = ciphertextWithTag.pin()
crypto_secretstream_xchacha20poly1305_push(
state.ptr,
ciphertextWithTagPinned.addressOf(0),
ciphertextWithTagPinned.toPtr(),
null,
data.toCValues(),
data.size.convert(),
@@ -136,7 +137,7 @@ actual class XChaCha20Poly1305Delegated internal actual constructor() {
val plaintextPinned = plaintext.pin()
val validTag = crypto_secretstream_xchacha20poly1305_pull(
state.ptr,
plaintextPinned.addressOf(0),
plaintextPinned.toPtr(),
null,
null,
data.toCValues(),
@@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.hash.blake2b
import com.ionspin.kotlin.crypto.util.toHexString
import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.*
import libsodium.*
import platform.posix.free
@@ -30,7 +31,7 @@ actual class Blake2bDelegated actual constructor(key: UByteArray?, hashLength: I
override fun digest(): UByteArray {
val hashResult = UByteArray(requestedHashLength)
val hashResultPinned = hashResult.pin()
crypto_generichash_final(state.ptr, hashResultPinned.addressOf(0), requestedHashLength.convert())
crypto_generichash_final(state.ptr, hashResultPinned.toPtr(), requestedHashLength.convert())
free(state.ptr)
return hashResult
}
@@ -44,7 +45,7 @@ actual object Blake2bDelegatedStateless : Blake2b {
val hashResult = UByteArray(MAX_HASH_BYTES)
val hashResultPinned = hashResult.pin()
crypto_generichash(
hashResultPinned.addressOf(0),
hashResultPinned.toPtr(),
hashLength.convert(),
inputMessage.toCValues(),
inputMessage.size.convert(),
@@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless
import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.*
import libsodium.*
import platform.posix.free
@@ -32,7 +33,7 @@ actual class Sha256Delegated : Sha256 {
override fun digest(): UByteArray {
val hashResult = UByteArray(Sha256Properties.MAX_HASH_BYTES)
val hashResultPinned = hashResult.pin()
crypto_hash_sha256_final(state.ptr, hashResultPinned.addressOf(0))
crypto_hash_sha256_final(state.ptr, hashResultPinned.toPtr())
sodium_free(state.ptr)
return hashResult
}
@@ -46,7 +47,7 @@ actual object Sha256StatelessDelegated : StatelessSha256 {
override fun digest(inputMessage: UByteArray): UByteArray {
val hashResult = UByteArray(MAX_HASH_BYTES)
val hashResultPinned = hashResult.pin()
crypto_hash_sha256(hashResultPinned.addressOf(0), inputMessage.toCValues(), inputMessage.size.convert())
crypto_hash_sha256(hashResultPinned.toPtr(), inputMessage.toCValues(), inputMessage.size.convert())
hashResultPinned.unpin()
return hashResult
}
@@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.hash.sha
import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.*
import libsodium.*
import platform.posix.free
@@ -30,7 +31,7 @@ actual class Sha512Delegated : Sha512Multipart {
override fun digest(): UByteArray {
val hashResult = UByteArray(Sha512Properties.MAX_HASH_BYTES)
val hashResultPinned = hashResult.pin()
crypto_hash_sha512_final(state.ptr, hashResultPinned.addressOf(0))
crypto_hash_sha512_final(state.ptr, hashResultPinned.toPtr())
free(state.ptr)
return hashResult
}
@@ -42,7 +43,7 @@ actual object Sha512StatelessDelegated : Sha512 {
override fun digest(inputMessage: UByteArray): UByteArray {
val hashResult = UByteArray(Sha512StatelessDelegated.MAX_HASH_BYTES)
val hashResultPinned = hashResult.pin()
crypto_hash_sha512(hashResultPinned.addressOf(0), inputMessage.toCValues(), inputMessage.size.convert())
crypto_hash_sha512(hashResultPinned.toPtr(), inputMessage.toCValues(), inputMessage.size.convert())
hashResultPinned.unpin()
return hashResult
}
@@ -0,0 +1,13 @@
package com.ionspin.kotlin.crypto.util
import kotlinx.cinterop.CPointer
import kotlinx.cinterop.Pinned
import kotlinx.cinterop.UByteVar
import kotlinx.cinterop.addressOf
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 27-Aug-2020
*/
fun Pinned<UByteArray>.toPtr() : CPointer<UByteVar> = addressOf(0)
@@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.toHexString
import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.*
import libsodium.*
import platform.posix.free
@@ -39,7 +40,7 @@ class HelperTest {
}
val result = UByteArray(32)
val resultPinned = result.pin()
crypto_hash_sha256_final(state, resultPinned.addressOf(0))
crypto_hash_sha256_final(state, resultPinned.toPtr())
println("$target to \"${result.toHexString()}\",")
free(state)
}
@@ -69,7 +70,7 @@ class HelperTest {
}
val result = UByteArray(32)
val resultPinned = result.pin()
crypto_hash_sha512_final(state, resultPinned.addressOf(0))
crypto_hash_sha512_final(state, resultPinned.toPtr())
println("$target to \"${result.toHexString()}\",")
free(state)
}