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 f11cbc4..2b0d120 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,11 +9,12 @@ package com.ionspin.kotlin.crypto.auth val crypto_auth_BYTES = 32 val crypto_auth_KEYBYTES = 32 +// do the 512 hmac and the deliver just 32 bytes of result val crypto_auth_hmacsha512256_BYTES = 32 val crypto_auth_hmacsha512256_KEYBYTES = 32 val crypto_auth_hmacsha256_KEYBYTES = 32 -val crypto_auth_hmacsha256_BYTES =32 +val crypto_auth_hmacsha256_BYTES = 32 val crypto_auth_hmacsha512_KEYBYTES = 32 val crypto_auth_hmacsha512_BYTES = 64 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 index a3cd065..9650457 100644 --- 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 @@ -21,7 +21,6 @@ class AuthTest { " 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() @@ -92,4 +91,18 @@ class AuthTest { } } } + + @Test + fun simpleKeygenTest() { + LibsodiumInitializer.initializeWithCallback { + val authKey = Auth.authKeygen() + assertTrue { authKey.size == crypto_auth_KEYBYTES } + val auth256Key = Auth.authHmacSha256Keygen() + assertTrue { auth256Key.size == crypto_auth_hmacsha256_KEYBYTES } + val auth512Key = Auth.authHmacSha512Keygen() + assertTrue { auth512Key.size == crypto_auth_hmacsha512_KEYBYTES } + } + + + } } diff --git a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt index e214503..ed83523 100644 --- a/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt +++ b/multiplatform-crypto-libsodium-bindings/src/jsMain/kotlin/com/ionspin/kotlin/crypto/JsSodiumInterface.kt @@ -91,6 +91,20 @@ interface JsSodiumInterface { // ---- AEAD end ---- + // ---- Auth ---- + + fun crypto_auth(message: Uint8Array, key: Uint8Array) : Uint8Array + fun crypto_auth_keygen() : Uint8Array + fun crypto_auth_verify(tag: Uint8Array, message: Uint8Array, key: Uint8Array) : Boolean + fun crypto_auth_hmacsha256(message: Uint8Array, key: Uint8Array) : Uint8Array + fun crypto_auth_hmacsha256_keygen() : Uint8Array + fun crypto_auth_hmacsha256_verify(tag: Uint8Array, message: Uint8Array, key: Uint8Array) : Boolean + fun crypto_auth_hmacsha512(message: Uint8Array, key: Uint8Array) : Uint8Array + fun crypto_auth_hmacsha512_keygen() : Uint8Array + fun crypto_auth_hmacsha512_verify(tag: Uint8Array, message: Uint8Array, key: Uint8Array) : Boolean + + // ---- Auth end ---- + //util fun memzero(array: Uint8Array) 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 index 7ad35a1..50cf35a 100644 --- 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 @@ -1,24 +1,40 @@ package com.ionspin.kotlin.crypto.auth +import com.ionspin.kotlin.crypto.getSodium +import ext.libsodium.com.ionspin.kotlin.crypto.toUByteArray +import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array +import org.khronos.webgl.Uint8Array + actual object Auth { actual fun authKeygen(): UByteArray { - TODO("not implemented yet") + return getSodium().crypto_auth_keygen().toUByteArray() } actual fun auth(message: UByteArray, key: UByteArray): UByteArray { - TODO("not implemented yet") + return getSodium().crypto_auth( + message.toUInt8Array(), + key.toUInt8Array() + ).toUByteArray() + } actual fun authVerify(mac: UByteArray, message: UByteArray, key: UByteArray): Boolean { - TODO("not implemented yet") + return getSodium().crypto_auth_verify( + mac.toUInt8Array(), + message.toUInt8Array(), + key.toUInt8Array() + ) } actual fun authHmacSha256Keygen(): UByteArray { - TODO("not implemented yet") + return getSodium().crypto_auth_hmacsha256_keygen().toUByteArray() } actual fun authHmacSha256(message: UByteArray, key: UByteArray): UByteArray { - TODO("not implemented yet") + return getSodium().crypto_auth_hmacsha256( + message.toUInt8Array(), + key.toUInt8Array() + ).toUByteArray() } actual fun authHmacSha256Verify( @@ -26,15 +42,22 @@ actual object Auth { message: UByteArray, key: UByteArray ): Boolean { - TODO("not implemented yet") + return getSodium().crypto_auth_hmacsha256_verify( + mac.toUInt8Array(), + message.toUInt8Array(), + key.toUInt8Array() + ) } actual fun authHmacSha512Keygen(): UByteArray { - TODO("not implemented yet") + return getSodium().crypto_auth_hmacsha512_keygen().toUByteArray() } actual fun authHmacSha512(message: UByteArray, key: UByteArray): UByteArray { - TODO("not implemented yet") + return getSodium().crypto_auth_hmacsha512( + message.toUInt8Array(), + key.toUInt8Array() + ).toUByteArray() } actual fun authHmacSha512Verify( @@ -42,7 +65,11 @@ actual object Auth { message: UByteArray, key: UByteArray ): Boolean { - TODO("not implemented yet") + return getSodium().crypto_auth_hmacsha512_verify( + mac.toUInt8Array(), + message.toUInt8Array(), + key.toUInt8Array() + ) } } 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 index 7ad35a1..afaec45 100644 --- 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 @@ -1,24 +1,49 @@ package com.ionspin.kotlin.crypto.auth +import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodium + actual object Auth { actual fun authKeygen(): UByteArray { - TODO("not implemented yet") + val generatedKey = UByteArray(crypto_auth_KEYBYTES) + sodium.crypto_auth_keygen(generatedKey.asByteArray()) + return generatedKey } actual fun auth(message: UByteArray, key: UByteArray): UByteArray { - TODO("not implemented yet") + val mac = UByteArray(crypto_auth_BYTES) + sodium.crypto_auth( + mac.asByteArray(), + message.asByteArray(), + message.size.toLong(), + key.asByteArray() + ) + return mac } actual fun authVerify(mac: UByteArray, message: UByteArray, key: UByteArray): Boolean { - TODO("not implemented yet") + return sodium.crypto_auth_verify( + mac.asByteArray(), + message.asByteArray(), + message.size.toLong(), + key.asByteArray() + ) == 0 } actual fun authHmacSha256Keygen(): UByteArray { - TODO("not implemented yet") + val generatedKey = UByteArray(crypto_auth_hmacsha256_KEYBYTES) + sodium.crypto_auth_hmacsha256_keygen(generatedKey.asByteArray()) + return generatedKey } actual fun authHmacSha256(message: UByteArray, key: UByteArray): UByteArray { - TODO("not implemented yet") + val mac = UByteArray(crypto_auth_hmacsha256_BYTES) + sodium.crypto_auth_hmacsha256( + mac.asByteArray(), + message.asByteArray(), + message.size.toLong(), + key.asByteArray() + ) + return mac } actual fun authHmacSha256Verify( @@ -26,15 +51,29 @@ actual object Auth { message: UByteArray, key: UByteArray ): Boolean { - TODO("not implemented yet") + return sodium.crypto_auth_hmacsha256_verify( + mac.asByteArray(), + message.asByteArray(), + message.size.toLong(), + key.asByteArray() + ) == 0 } actual fun authHmacSha512Keygen(): UByteArray { - TODO("not implemented yet") + val generatedKey = UByteArray(crypto_auth_hmacsha512_KEYBYTES) + sodium.crypto_auth_hmacsha512_keygen(generatedKey.asByteArray()) + return generatedKey } actual fun authHmacSha512(message: UByteArray, key: UByteArray): UByteArray { - TODO("not implemented yet") + val mac = UByteArray(crypto_auth_hmacsha512_BYTES) + sodium.crypto_auth_hmacsha512( + mac.asByteArray(), + message.asByteArray(), + message.size.toLong(), + key.asByteArray() + ) + return mac } actual fun authHmacSha512Verify( @@ -42,7 +81,12 @@ actual object Auth { message: UByteArray, key: UByteArray ): Boolean { - TODO("not implemented yet") + return sodium.crypto_auth_hmacsha512_verify( + mac.asByteArray(), + message.asByteArray(), + message.size.toLong(), + key.asByteArray() + ) == 0 } } diff --git a/supported_bindings_list.md b/supported_bindings_list.md index b53e853..90ae5f0 100644 --- a/supported_bindings_list.md +++ b/supported_bindings_list.md @@ -27,15 +27,15 @@ | crypto_aead_xchacha20poly1305_ietf_encrypt | :heavy_check_mark: | | crypto_aead_xchacha20poly1305_ietf_encrypt_detached | :heavy_check_mark: | | crypto_aead_xchacha20poly1305_ietf_keygen | :heavy_check_mark: | -| crypto_auth | | -| crypto_auth_hmacsha256 | | -| crypto_auth_hmacsha256_keygen | | -| crypto_auth_hmacsha256_verify | | -| crypto_auth_hmacsha512 | | -| crypto_auth_hmacsha512_keygen | | -| crypto_auth_hmacsha512_verify | | -| crypto_auth_keygen | | -| crypto_auth_verify | | +| crypto_auth | :heavy_check_mark: | +| crypto_auth_hmacsha256 | :heavy_check_mark: | +| crypto_auth_hmacsha256_keygen | :heavy_check_mark: | +| crypto_auth_hmacsha256_verify | :heavy_check_mark: | +| crypto_auth_hmacsha512 | :heavy_check_mark: | +| crypto_auth_hmacsha512_keygen | :heavy_check_mark: | +| crypto_auth_hmacsha512_verify | :heavy_check_mark: | +| crypto_auth_keygen | :heavy_check_mark: | +| crypto_auth_verify | :heavy_check_mark: | | crypto_box_beforenm | | | crypto_box_curve25519xchacha20poly1305_keypair | | | crypto_box_curve25519xchacha20poly1305_seal | | @@ -174,14 +174,14 @@ | crypto_aead_xchacha20poly1305_ietf_MESSAGEBYTES_MAX | | | crypto_aead_xchacha20poly1305_ietf_NPUBBYTES | :heavy_check_mark: | | crypto_aead_xchacha20poly1305_ietf_NSECBYTES | | -| crypto_auth_BYTES | | -| crypto_auth_KEYBYTES | | -| crypto_auth_hmacsha256_BYTES | | -| crypto_auth_hmacsha256_KEYBYTES | | -| crypto_auth_hmacsha512256_BYTES | | -| crypto_auth_hmacsha512256_KEYBYTES | | -| crypto_auth_hmacsha512_BYTES | | -| crypto_auth_hmacsha512_KEYBYTES | | +| crypto_auth_BYTES | :heavy_check_mark: | +| crypto_auth_KEYBYTES | :heavy_check_mark: | +| crypto_auth_hmacsha256_BYTES | :heavy_check_mark: | +| crypto_auth_hmacsha256_KEYBYTES | :heavy_check_mark: | +| crypto_auth_hmacsha512256_BYTES | :heavy_check_mark: | +| crypto_auth_hmacsha512256_KEYBYTES | :heavy_check_mark: | +| crypto_auth_hmacsha512_BYTES | :heavy_check_mark: | +| crypto_auth_hmacsha512_KEYBYTES | :heavy_check_mark: | | crypto_box_BEFORENMBYTES | | | crypto_box_MACBYTES | | | crypto_box_MESSAGEBYTES_MAX | |