From d2fda34807667a16b4a23f8fad23be54b6c15833 Mon Sep 17 00:00:00 2001 From: Ugljesa Jovanovic Date: Sun, 30 Aug 2020 18:33:14 +0200 Subject: [PATCH] Add auth native and tests --- .../com.ionspin.kotlin.crypto/auth/Auth.kt | 3 + .../ionspin/kotlin/crypto/auth/AuthTest.kt | 95 +++++++++++++++++++ .../com/ionspin/kotlin/crypto/auth/Auth.kt | 7 +- 3 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/auth/AuthTest.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 index 51fd66e..f11cbc4 100644 --- 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 @@ -9,6 +9,9 @@ package com.ionspin.kotlin.crypto.auth val crypto_auth_BYTES = 32 val crypto_auth_KEYBYTES = 32 +val crypto_auth_hmacsha512256_BYTES = 32 +val crypto_auth_hmacsha512256_KEYBYTES = 32 + val crypto_auth_hmacsha256_KEYBYTES = 32 val crypto_auth_hmacsha256_BYTES =32 diff --git a/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/auth/AuthTest.kt b/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/auth/AuthTest.kt new file mode 100644 index 0000000..a3cd065 --- /dev/null +++ b/multiplatform-crypto-libsodium-bindings/src/commonTest/kotlin/com/ionspin/kotlin/crypto/auth/AuthTest.kt @@ -0,0 +1,95 @@ +package com.ionspin.kotlin.crypto.auth + +import com.ionspin.kotlin.crypto.LibsodiumInitializer +import com.ionspin.kotlin.crypto.util.encodeToUByteArray +import com.ionspin.kotlin.crypto.util.hexStringToUByteArray +import com.ionspin.kotlin.crypto.util.toHexString +import kotlin.test.Test +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +/** + * Created by Ugljesa Jovanovic + * ugljesa.jovanovic@ionspin.com + * on 30-Aug-2020 + */ +class AuthTest { + @Test + fun testAuth() { + LibsodiumInitializer.initializeWithCallback { + val message = ("I wonder if it would be possible" + + " to get some lyrics in these tests").encodeToUByteArray() + + val key = "We'll see1We'll see1We'll see123".encodeToUByteArray() + println("Key size ${key.size}") + + val expected = "702beb4494a1d80795512668df016807ec052dc848a4c958eb1544ec1c8d6314".hexStringToUByteArray() + + val hashed = Auth.auth(message, key) + println(hashed.toHexString()) + assertTrue { + hashed.contentEquals(expected) + } + assertTrue { Auth.authVerify(hashed, message, key) } + + assertFalse { + val tampered = hashed.copyOf() + tampered[5] = 0U + Auth.authVerify(tampered, message, key) + } + } + + } + + @Test + fun testAuthHmacSha256() { + LibsodiumInitializer.initializeWithCallback { + val message = ("I wonder if it would be possible" + + " to get some lyrics in these tests").encodeToUByteArray() + + val key = "We'll see1We'll see1We'll see123".encodeToUByteArray() + + val expected = "b1b3cf73089e04106a135629ba586b6c94b81b87162302f3f32b4fb797f82e8a".hexStringToUByteArray() + + val hashed = Auth.authHmacSha256(message, key) + println(hashed.toHexString()) + assertTrue { + hashed.contentEquals(expected) + } + assertTrue { Auth.authHmacSha256Verify(hashed, message, key) } + + assertFalse { + val tampered = hashed.copyOf() + tampered[5] = 0U + Auth.authHmacSha256Verify(tampered, message, key) + } + } + } + + @Test + fun testAuthHmacSha512() { + LibsodiumInitializer.initializeWithCallback { + val message = ("I wonder if it would be possible" + + " to get some lyrics in these tests").encodeToUByteArray() + + val key = "We'll see1We'll see1We'll see123".encodeToUByteArray() + + val expected = ("702beb4494a1d80795512668df016807ec052dc848a4c958eb1544ec1c8d63145fd11513c2d" + + "aecb03780e2b8b121e87f0a171033489de92665d9a218f4ed9589").hexStringToUByteArray() + + val hashed = Auth.authHmacSha512(message, key) + println(hashed.toHexString()) + assertTrue { + hashed.contentEquals(expected) + } + println(hashed.toHexString()) + assertTrue { Auth.authHmacSha512Verify(hashed, message, key) } + + assertFalse { + val tampered = hashed.copyOf() + tampered[5] = 0U + Auth.authHmacSha512Verify(tampered, message, key) + } + } + } +} 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 index 5cf01dd..1964240 100644 --- 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 @@ -47,7 +47,6 @@ actual object Auth { val macPinned = mac.pin() val messagePinned = message.pin() val keyPinned = key.pin() - val verify = crypto_auth_verify( macPinned.toPtr(), messagePinned.toPtr(), @@ -57,7 +56,7 @@ actual object Auth { keyPinned.unpin() messagePinned.unpin() - keyPinned.unpin() + macPinned.unpin() return verify == 0 } @@ -108,7 +107,7 @@ actual object Auth { keyPinned.unpin() messagePinned.unpin() - keyPinned.unpin() + macPinned.unpin() return verify == 0 } @@ -159,7 +158,7 @@ actual object Auth { keyPinned.unpin() messagePinned.unpin() - keyPinned.unpin() + macPinned.unpin() return verify == 0 }