Adding crypto_auth
This commit is contained in:
parent
4cf1d48225
commit
0c8de7b5c5
@ -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
|
||||||
|
|
||||||
|
}
|
@ -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")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user