Add auth native and tests

This commit is contained in:
Ugljesa Jovanovic 2020-08-30 18:33:14 +02:00 committed by Ugljesa Jovanovic
parent 0c8de7b5c5
commit d2fda34807
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
3 changed files with 101 additions and 4 deletions

View File

@ -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

View File

@ -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)
}
}
}
}

View File

@ -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
}