fix macos and windows compilation issues

This commit is contained in:
Ugljesa Jovanovic 2020-08-08 23:05:27 +02:00 committed by Ugljesa Jovanovic
parent e6e7a7664c
commit d1a910b965
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
5 changed files with 24 additions and 43 deletions

View File

@ -15,39 +15,39 @@ actual typealias GenericHashState = Any
actual class Crypto internal actual constructor() { actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha256_init(): dynamic { actual fun crypto_hash_sha256_init(): dynamic {
println("Debug") println("Debug crypto_hash_sha256_init")
val result = js("getSodium().crypto_hash_sha256_init()") val result = js("getSodium().crypto_hash_sha256_init()")
return result return result
} }
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) { actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
println("Debug") println("Debug crypto_hash_sha256_update")
getSodium().crypto_hash_sha256_update(state, input.toUInt8Array(), ) getSodium().crypto_hash_sha256_update(state, input.toUInt8Array(), )
} }
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray { actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
println("Debug") println("Debug crypto_hash_sha256_final")
return getSodium().crypto_hash_sha256_final(state).toUByteArray() return getSodium().crypto_hash_sha256_final(state).toUByteArray()
} }
actual fun crypto_hash_sha512_init(): dynamic { actual fun crypto_hash_sha512_init(): dynamic {
println("Debug") println("Debug crypto_hash_sha512_init")
val result = js("getSodium().crypto_hash_sha512_init()") val result = js("getSodium().crypto_hash_sha512_init()")
return result return result
} }
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) { actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
println("Debug") println("Debug crypto_hash_sha512_update")
getSodium().crypto_hash_sha512_update(state, input.toUInt8Array(), ) getSodium().crypto_hash_sha512_update(state, input.toUInt8Array(), )
} }
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray { actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
println("Debug") println("Debug crypto_hash_sha512_final")
return getSodium().crypto_hash_sha512_final(state).toUByteArray() return getSodium().crypto_hash_sha512_final(state).toUByteArray()
} }
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): dynamic { actual fun crypto_generichash_init(key: UByteArray, outlen: Int): dynamic {
println("Debug") println("Debug crypto_generichash_init")
return getSodium().crypto_generichash_init(key.toUInt8Array(), outlen) return getSodium().crypto_generichash_init(key.toUInt8Array(), outlen)
} }
} }

View File

@ -17,45 +17,45 @@ actual typealias GenericHashState = ByteArray
actual class Crypto internal actual constructor() { actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha256_init(): Sha256State { actual fun crypto_hash_sha256_init(): Sha256State {
val state = debug.test.Sha256State() val state = debug.test.Sha256State()
println("Debug") println("Debug crypto_hash_sha256_init")
sodium.crypto_hash_sha256_init(state) sodium.crypto_hash_sha256_init(state)
return state return state
} }
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) { actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
println("Debug") println("Debug crypto_hash_sha256_update")
sodium.crypto_hash_sha256_update(state, input.asByteArray(), input.size.toLong()) sodium.crypto_hash_sha256_update(state, input.asByteArray(), input.size.toLong())
} }
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray { actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
val out = UByteArray(32) val out = UByteArray(32)
println("Debug") println("Debug crypto_hash_sha256_final")
sodium.crypto_hash_sha256_final(state, out.asByteArray()) sodium.crypto_hash_sha256_final(state, out.asByteArray())
return out return out
} }
actual fun crypto_hash_sha512_init(): Sha512State { actual fun crypto_hash_sha512_init(): Sha512State {
val state = debug.test.Sha512State() val state = debug.test.Sha512State()
println("Debug") println("Debug crypto_hash_sha512_init")
sodium.crypto_hash_sha512_init(state) sodium.crypto_hash_sha512_init(state)
return state return state
} }
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) { actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
println("Debug") println("Debug crypto_hash_sha512_update")
sodium.crypto_hash_sha512_update(state, input.asByteArray(), input.size.toLong()) sodium.crypto_hash_sha512_update(state, input.asByteArray(), input.size.toLong())
} }
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray { actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
val out = UByteArray(64) val out = UByteArray(64)
println("Debug") println("Debug crypto_hash_sha512_final")
sodium.crypto_hash_sha512_final(state, out.asByteArray()) sodium.crypto_hash_sha512_final(state, out.asByteArray())
return out return out
} }
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState { actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
val state = debug.test.GenericHashState(sodium.crypto_generichash_statebytes()) val state = debug.test.GenericHashState(sodium.crypto_generichash_statebytes())
println("Debug") println("Debug crypto_generichash_init")
sodium.crypto_generichash_init(state, key.asByteArray(), key.size, outlen) sodium.crypto_generichash_init(state, key.asByteArray(), key.size, outlen)
return state return state
} }

View File

@ -30,11 +30,13 @@ actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha256_init(): Sha256State { actual fun crypto_hash_sha256_init(): Sha256State {
val allocated = sodium_malloc(debug.test.Sha256State.size.convert())!! val allocated = sodium_malloc(debug.test.Sha256State.size.convert())!!
val state = allocated.reinterpret<debug.test.Sha256State>().pointed val state = allocated.reinterpret<debug.test.Sha256State>().pointed
println("Debug crypto_hash_sha256_init")
libsodium.crypto_hash_sha256_init(state.ptr) libsodium.crypto_hash_sha256_init(state.ptr)
return state return state
} }
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) { actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
println("Debug crypto_hash_sha256_update")
val pinnedInput = input.pin() val pinnedInput = input.pin()
libsodium.crypto_hash_sha256_update(state.ptr, pinnedInput.addressOf(0), input.size.convert()) libsodium.crypto_hash_sha256_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
pinnedInput.unpin() pinnedInput.unpin()
@ -42,6 +44,7 @@ actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray { actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
val out = UByteArray(32) val out = UByteArray(32)
println("Debug crypto_hash_sha256_final")
val pinnedOut = out.pin() val pinnedOut = out.pin()
libsodium.crypto_hash_sha256_final(state.ptr, pinnedOut.addressOf(0)) libsodium.crypto_hash_sha256_final(state.ptr, pinnedOut.addressOf(0))
pinnedOut.unpin() pinnedOut.unpin()
@ -51,11 +54,13 @@ actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha512_init(): Sha512State { actual fun crypto_hash_sha512_init(): Sha512State {
val allocated = sodium_malloc(debug.test.Sha512State.size.convert())!! val allocated = sodium_malloc(debug.test.Sha512State.size.convert())!!
val state = allocated.reinterpret<debug.test.Sha512State>().pointed val state = allocated.reinterpret<debug.test.Sha512State>().pointed
println("Debug crypto_hash_sha512_init")
libsodium.crypto_hash_sha512_init(state.ptr) libsodium.crypto_hash_sha512_init(state.ptr)
return state return state
} }
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) { actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
println("Debug crypto_hash_sha512_update")
val pinnedInput = input.pin() val pinnedInput = input.pin()
libsodium.crypto_hash_sha512_update(state.ptr, pinnedInput.addressOf(0), input.size.convert()) libsodium.crypto_hash_sha512_update(state.ptr, pinnedInput.addressOf(0), input.size.convert())
pinnedInput.unpin() pinnedInput.unpin()
@ -63,6 +68,7 @@ actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray { actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
val out = UByteArray(64) val out = UByteArray(64)
println("Debug crypto_hash_sha512_final")
val pinnedOut = out.pin() val pinnedOut = out.pin()
libsodium.crypto_hash_sha512_final(state.ptr, pinnedOut.addressOf(0)) libsodium.crypto_hash_sha512_final(state.ptr, pinnedOut.addressOf(0))
pinnedOut.unpin() pinnedOut.unpin()
@ -72,6 +78,7 @@ actual class Crypto internal actual constructor() {
actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState { actual fun crypto_generichash_init(key: UByteArray, outlen: Int): GenericHashState {
val allocated = sodium_malloc(debug.test.GenericHashState.size.convert())!! val allocated = sodium_malloc(debug.test.GenericHashState.size.convert())!!
val state = allocated.reinterpret<debug.test.GenericHashState>().pointed val state = allocated.reinterpret<debug.test.GenericHashState>().pointed
println("Debug crypto_generichash_init")
val pinnedKey = key.pin() val pinnedKey = key.pin()
libsodium.crypto_generichash_init(state.ptr, pinnedKey.addressOf(0), key.size.convert(), libsodium.crypto_generichash_init(state.ptr, pinnedKey.addressOf(0), key.size.convert(),
outlen.convert()) outlen.convert())

View File

@ -5,6 +5,7 @@ import com.ionspin.kotlin.crypto.util.toHexString
import kotlinx.cinterop.* import kotlinx.cinterop.*
import libsodium.* import libsodium.*
import platform.posix.free import platform.posix.free
import platform.posix.malloc
import kotlin.test.Ignore import kotlin.test.Ignore
import kotlin.test.Test import kotlin.test.Test
@ -29,7 +30,7 @@ class HelperTest {
fun generateForRounds256(target: Long) { fun generateForRounds256(target: Long) {
val updateValue = val updateValue =
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno".encodeToUByteArray().toCValues() "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno".encodeToUByteArray().toCValues()
val state = platform.linux.malloc(crypto_hash_sha256_state.size.convert())!! val state = malloc(crypto_hash_sha256_state.size.convert())!!
.reinterpret<crypto_hash_sha256_state>() .reinterpret<crypto_hash_sha256_state>()
crypto_hash_sha256_init(state) crypto_hash_sha256_init(state)
@ -59,7 +60,7 @@ class HelperTest {
println("Wut") println("Wut")
val updateValue = val updateValue =
"abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno".encodeToUByteArray().toCValues() "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno".encodeToUByteArray().toCValues()
val state = platform.linux.malloc(crypto_hash_sha512_state.size.convert())!! val state = malloc(crypto_hash_sha512_state.size.convert())!!
.reinterpret<crypto_hash_sha512_state>() .reinterpret<crypto_hash_sha512_state>()
crypto_hash_sha512_init(state) crypto_hash_sha512_init(state)

View File

@ -1,27 +0,0 @@
/*
* Copyright 2019 Ugljesa Jovanovic
*
* 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 com.ionspin.kotlin.crypto.util
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 20-Jul-2019
*/
actual fun testBlocking(block: suspend () -> Unit) = runBlocking { block() }