From 0c8de7b5c56a11c98eb08bcc0599e5892ed204ed Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Sun, 30 Aug 2020 17:56:12 +0200 Subject: [PATCH] Adding crypto_auth --- .../com.ionspin.kotlin.crypto/auth/Auth.kt | 32 ++++ .../com/ionspin/kotlin/crypto/auth/Auth.kt | 48 +++++ .../com/ionspin/kotlin/crypto/auth/Auth.kt | 48 +++++ .../com/ionspin/kotlin/crypto/auth/Auth.kt | 166 ++++++++++++++++++ 4 files changed, 294 insertions(+) create mode 100644 multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/auth/Auth.kt create mode 100644 multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/auth/Auth.kt create mode 100644 multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/auth/Auth.kt create mode 100644 multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/auth/Auth.kt diff --git a/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/auth/Auth.kt b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/auth/Auth.kt new file mode 100644 index 0000000..51fd66e --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/commonMain/kotlin/com.ionspin.kotlin.crypto/auth/Auth.kt @@ -0,0 +1,32 @@ +package com.ionspin.kotlin.crypto.auth + +/** + * Created by Ugljesa Jovanovic + * ugljesa.jovanovic@ionspin.com + * on 30-Aug-2020 + */ + +val crypto_auth_BYTES = 32 +val crypto_auth_KEYBYTES = 32 + +val crypto_auth_hmacsha256_KEYBYTES = 32 +val crypto_auth_hmacsha256_BYTES =32 + +val crypto_auth_hmacsha512_KEYBYTES = 32 +val crypto_auth_hmacsha512_BYTES = 64 + +expect object Auth { + + fun authKeygen() : UByteArray + fun auth(message: UByteArray, key: UByteArray) : UByteArray + fun authVerify(mac: UByteArray, message: UByteArray, key: UByteArray) : Boolean + + fun authHmacSha256Keygen() : UByteArray + fun authHmacSha256(message: UByteArray, key: UByteArray) : UByteArray + fun authHmacSha256Verify(mac: UByteArray, message: UByteArray, key: UByteArray) : Boolean + + fun authHmacSha512Keygen() : UByteArray + fun authHmacSha512(message: UByteArray, key: UByteArray) : UByteArray + fun authHmacSha512Verify(mac: UByteArray, message: UByteArray, key: UByteArray) : Boolean + +} diff --git a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/auth/Auth.kt b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/auth/Auth.kt new file mode 100644 index 0000000..7ad35a1 --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/auth/Auth.kt @@ -0,0 +1,48 @@ +package com.ionspin.kotlin.crypto.auth + +actual object Auth { + actual fun authKeygen(): UByteArray { + TODO("not implemented yet") + } + + actual fun auth(message: UByteArray, key: UByteArray): UByteArray { + TODO("not implemented yet") + } + + actual fun authVerify(mac: UByteArray, message: UByteArray, key: UByteArray): Boolean { + TODO("not implemented yet") + } + + actual fun authHmacSha256Keygen(): UByteArray { + TODO("not implemented yet") + } + + actual fun authHmacSha256(message: UByteArray, key: UByteArray): UByteArray { + TODO("not implemented yet") + } + + actual fun authHmacSha256Verify( + mac: UByteArray, + message: UByteArray, + key: UByteArray + ): Boolean { + TODO("not implemented yet") + } + + actual fun authHmacSha512Keygen(): UByteArray { + TODO("not implemented yet") + } + + actual fun authHmacSha512(message: UByteArray, key: UByteArray): UByteArray { + TODO("not implemented yet") + } + + actual fun authHmacSha512Verify( + mac: UByteArray, + message: UByteArray, + key: UByteArray + ): Boolean { + TODO("not implemented yet") + } + +} diff --git a/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/auth/Auth.kt b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/auth/Auth.kt new file mode 100644 index 0000000..7ad35a1 --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/jvmMain/kotlin/com/ionspin/kotlin/crypto/auth/Auth.kt @@ -0,0 +1,48 @@ +package com.ionspin.kotlin.crypto.auth + +actual object Auth { + actual fun authKeygen(): UByteArray { + TODO("not implemented yet") + } + + actual fun auth(message: UByteArray, key: UByteArray): UByteArray { + TODO("not implemented yet") + } + + actual fun authVerify(mac: UByteArray, message: UByteArray, key: UByteArray): Boolean { + TODO("not implemented yet") + } + + actual fun authHmacSha256Keygen(): UByteArray { + TODO("not implemented yet") + } + + actual fun authHmacSha256(message: UByteArray, key: UByteArray): UByteArray { + TODO("not implemented yet") + } + + actual fun authHmacSha256Verify( + mac: UByteArray, + message: UByteArray, + key: UByteArray + ): Boolean { + TODO("not implemented yet") + } + + actual fun authHmacSha512Keygen(): UByteArray { + TODO("not implemented yet") + } + + actual fun authHmacSha512(message: UByteArray, key: UByteArray): UByteArray { + TODO("not implemented yet") + } + + actual fun authHmacSha512Verify( + mac: UByteArray, + message: UByteArray, + key: UByteArray + ): Boolean { + TODO("not implemented yet") + } + +} diff --git a/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/auth/Auth.kt b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/auth/Auth.kt new file mode 100644 index 0000000..5cf01dd --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/nativeMain/kotlin/com/ionspin/kotlin/crypto/auth/Auth.kt @@ -0,0 +1,166 @@ +package com.ionspin.kotlin.crypto.auth + +import com.ionspin.kotlin.crypto.util.toPtr +import kotlinx.cinterop.convert +import kotlinx.cinterop.pin +import libsodium.crypto_auth +import libsodium.crypto_auth_hmacsha256 +import libsodium.crypto_auth_hmacsha256_keygen +import libsodium.crypto_auth_hmacsha256_verify +import libsodium.crypto_auth_hmacsha512 +import libsodium.crypto_auth_hmacsha512_keygen +import libsodium.crypto_auth_hmacsha512_verify +import libsodium.crypto_auth_keygen +import libsodium.crypto_auth_verify + +actual object Auth { + actual fun authKeygen(): UByteArray { + val generatedKey = UByteArray(crypto_auth_KEYBYTES) + val generatedKeyPinned = generatedKey.pin() + crypto_auth_keygen(generatedKeyPinned.toPtr()) + generatedKeyPinned.unpin() + return generatedKey + } + + actual fun auth(message: UByteArray, key: UByteArray): UByteArray { + val messagePinned = message.pin() + val keyPinned = key.pin() + + val mac = UByteArray(crypto_auth_BYTES) + val macPinned = mac.pin() + + crypto_auth( + macPinned.toPtr(), + messagePinned.toPtr(), + message.size.convert(), + keyPinned.toPtr() + ) + + macPinned.unpin() + messagePinned.unpin() + keyPinned.unpin() + + return mac + } + + actual fun authVerify(mac: UByteArray, message: UByteArray, key: UByteArray): Boolean { + val macPinned = mac.pin() + val messagePinned = message.pin() + val keyPinned = key.pin() + + val verify = crypto_auth_verify( + macPinned.toPtr(), + messagePinned.toPtr(), + message.size.convert(), + keyPinned.toPtr(), + ) + + keyPinned.unpin() + messagePinned.unpin() + keyPinned.unpin() + return verify == 0 + } + + actual fun authHmacSha256Keygen(): UByteArray { + val generatedKey = UByteArray(crypto_auth_hmacsha256_KEYBYTES) + val generatedKeyPinned = generatedKey.pin() + crypto_auth_hmacsha256_keygen(generatedKeyPinned.toPtr()) + generatedKeyPinned.unpin() + return generatedKey + } + + actual fun authHmacSha256(message: UByteArray, key: UByteArray): UByteArray { + val messagePinned = message.pin() + val keyPinned = key.pin() + + val mac = UByteArray(crypto_auth_hmacsha256_BYTES) + val macPinned = mac.pin() + + crypto_auth_hmacsha256( + macPinned.toPtr(), + messagePinned.toPtr(), + message.size.convert(), + keyPinned.toPtr() + ) + + macPinned.unpin() + messagePinned.unpin() + keyPinned.unpin() + + return mac + } + + actual fun authHmacSha256Verify( + mac: UByteArray, + message: UByteArray, + key: UByteArray + ): Boolean { + val macPinned = mac.pin() + val messagePinned = message.pin() + val keyPinned = key.pin() + + val verify = crypto_auth_hmacsha256_verify( + macPinned.toPtr(), + messagePinned.toPtr(), + message.size.convert(), + keyPinned.toPtr(), + ) + + keyPinned.unpin() + messagePinned.unpin() + keyPinned.unpin() + return verify == 0 + } + + actual fun authHmacSha512Keygen(): UByteArray { + val generatedKey = UByteArray(crypto_auth_hmacsha512_KEYBYTES) + val generatedKeyPinned = generatedKey.pin() + crypto_auth_hmacsha512_keygen(generatedKeyPinned.toPtr()) + generatedKeyPinned.unpin() + return generatedKey + } + + actual fun authHmacSha512(message: UByteArray, key: UByteArray): UByteArray { + val messagePinned = message.pin() + val keyPinned = key.pin() + + val mac = UByteArray(crypto_auth_hmacsha512_BYTES) + val macPinned = mac.pin() + + crypto_auth_hmacsha512( + macPinned.toPtr(), + messagePinned.toPtr(), + message.size.convert(), + keyPinned.toPtr() + ) + + macPinned.unpin() + messagePinned.unpin() + keyPinned.unpin() + + return mac + } + + actual fun authHmacSha512Verify( + mac: UByteArray, + message: UByteArray, + key: UByteArray + ): Boolean { + val macPinned = mac.pin() + val messagePinned = message.pin() + val keyPinned = key.pin() + + val verify = crypto_auth_hmacsha512_verify( + macPinned.toPtr(), + messagePinned.toPtr(), + message.size.convert(), + keyPinned.toPtr(), + ) + + keyPinned.unpin() + messagePinned.unpin() + keyPinned.unpin() + return verify == 0 + } + +}