Adding crypto_auth
This commit is contained in:
1 parent
4cf1d48225
commit
0c8de7b5c5
4 files changed
+294
No files matched your search
+166
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user