Completed crypto auth and added tests
This commit is contained in:
1 parent
d2fda34807
commit
16cb5c2dee
6 files changed
+136
-37
No files matched your search
+2
-1
@@ -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
|
||||
|
||||
+14
-1
@@ -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 }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+14
@@ -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)
|
||||
|
||||
|
||||
+36
-9
@@ -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()
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
+53
-9
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user