Use single shared method for return code checking.
This defers the return code logic to a named method `isLibsodiumSuccessCode` to reduce the liklihood of error when checking codes. This will also now throw an `IllegalStateException` in the event that an unknown code is returned.
This commit is contained in:
parent
c9c5f320af
commit
c2b91c2c69
@ -1,9 +1,17 @@
|
|||||||
package com.ionspin.kotlin.crypto
|
package com.ionspin.kotlin.crypto
|
||||||
|
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
|
|
||||||
class GeneralLibsodiumException : RuntimeException("Libsodium reported error! Returned value was -1") {
|
class GeneralLibsodiumException : RuntimeException("Libsodium reported error! Returned value was -1") {
|
||||||
companion object {
|
companion object {
|
||||||
|
/**
|
||||||
|
* Throws a [GeneralLibsodiumException] if the return code is not
|
||||||
|
* successful.
|
||||||
|
*
|
||||||
|
* This will throw an [IllegalStateException] if the return code is invalid/unknown.
|
||||||
|
*/
|
||||||
fun Int.ensureLibsodiumSuccess() {
|
fun Int.ensureLibsodiumSuccess() {
|
||||||
if (this == -1) {
|
if (!isLibsodiumSuccessCode()) {
|
||||||
throw GeneralLibsodiumException()
|
throw GeneralLibsodiumException()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,3 +36,16 @@ fun UByteArray.hexColumnsPrint(chunk: Int = 16) {
|
|||||||
val printout = this.map { it.toString(16).padStart(2, '0') }.chunked(chunk)
|
val printout = this.map { it.toString(16).padStart(2, '0') }.chunked(chunk)
|
||||||
printout.forEach { println(it.joinToString(separator = " ") { it.uppercase() }) }
|
printout.forEach { println(it.joinToString(separator = " ") { it.uppercase() }) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Functions returning an int return 0 on success and -1 to indicate an error.
|
||||||
|
*
|
||||||
|
* This will throw an [IllegalStateException] if the return code is invalid/unknown.
|
||||||
|
*/
|
||||||
|
fun Int.isLibsodiumSuccessCode(): Boolean {
|
||||||
|
return when (this) {
|
||||||
|
0 -> true
|
||||||
|
-1 -> false
|
||||||
|
else -> throw IllegalStateException("Libsodium returned an unexpected return code $this")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.aead
|
|||||||
|
|
||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
|
|
||||||
actual object AuthenticatedEncryptionWithAssociatedData {
|
actual object AuthenticatedEncryptionWithAssociatedData {
|
||||||
|
|
||||||
@ -47,7 +48,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
|||||||
nonce.asByteArray(),
|
nonce.asByteArray(),
|
||||||
key.asByteArray(),
|
key.asByteArray(),
|
||||||
)
|
)
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw AeadCorrupedOrTamperedDataException()
|
throw AeadCorrupedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
return message
|
return message
|
||||||
@ -95,7 +96,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
|||||||
nonce.asByteArray(),
|
nonce.asByteArray(),
|
||||||
key.asByteArray(),
|
key.asByteArray(),
|
||||||
)
|
)
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw AeadCorrupedOrTamperedDataException()
|
throw AeadCorrupedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
return message
|
return message
|
||||||
@ -140,7 +141,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
|||||||
nonce.asByteArray(),
|
nonce.asByteArray(),
|
||||||
key.asByteArray(),
|
key.asByteArray(),
|
||||||
)
|
)
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw AeadCorrupedOrTamperedDataException()
|
throw AeadCorrupedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
return message
|
return message
|
||||||
@ -188,7 +189,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
|||||||
nonce.asByteArray(),
|
nonce.asByteArray(),
|
||||||
key.asByteArray(),
|
key.asByteArray(),
|
||||||
)
|
)
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw AeadCorrupedOrTamperedDataException()
|
throw AeadCorrupedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
return message
|
return message
|
||||||
@ -233,7 +234,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
|||||||
nonce.asByteArray(),
|
nonce.asByteArray(),
|
||||||
key.asByteArray(),
|
key.asByteArray(),
|
||||||
)
|
)
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw AeadCorrupedOrTamperedDataException()
|
throw AeadCorrupedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
return message
|
return message
|
||||||
@ -281,7 +282,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
|||||||
nonce.asByteArray(),
|
nonce.asByteArray(),
|
||||||
key.asByteArray(),
|
key.asByteArray(),
|
||||||
)
|
)
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw AeadCorrupedOrTamperedDataException()
|
throw AeadCorrupedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
return message
|
return message
|
||||||
|
@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.auth
|
|||||||
|
|
||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
|
|
||||||
actual object Auth {
|
actual object Auth {
|
||||||
actual fun authKeygen(): UByteArray {
|
actual fun authKeygen(): UByteArray {
|
||||||
@ -27,7 +28,7 @@ actual object Auth {
|
|||||||
message.asByteArray(),
|
message.asByteArray(),
|
||||||
message.size.toLong(),
|
message.size.toLong(),
|
||||||
key.asByteArray()
|
key.asByteArray()
|
||||||
) == 0
|
).isLibsodiumSuccessCode()
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun authHmacSha256Keygen(): UByteArray {
|
actual fun authHmacSha256Keygen(): UByteArray {
|
||||||
@ -57,7 +58,7 @@ actual object Auth {
|
|||||||
message.asByteArray(),
|
message.asByteArray(),
|
||||||
message.size.toLong(),
|
message.size.toLong(),
|
||||||
key.asByteArray()
|
key.asByteArray()
|
||||||
) == 0
|
).isLibsodiumSuccessCode()
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun authHmacSha512Keygen(): UByteArray {
|
actual fun authHmacSha512Keygen(): UByteArray {
|
||||||
@ -87,7 +88,7 @@ actual object Auth {
|
|||||||
message.asByteArray(),
|
message.asByteArray(),
|
||||||
message.size.toLong(),
|
message.size.toLong(),
|
||||||
key.asByteArray()
|
key.asByteArray()
|
||||||
) == 0
|
).isLibsodiumSuccessCode()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.box
|
|||||||
|
|
||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
|
|
||||||
actual object Box {
|
actual object Box {
|
||||||
/**
|
/**
|
||||||
@ -73,7 +74,7 @@ actual object Box {
|
|||||||
sendersPublicKey.asByteArray(),
|
sendersPublicKey.asByteArray(),
|
||||||
recipientsSecretKey.asByteArray()
|
recipientsSecretKey.asByteArray()
|
||||||
)
|
)
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw BoxCorruptedOrTamperedDataException()
|
throw BoxCorruptedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -128,7 +129,7 @@ actual object Box {
|
|||||||
precomputedKey.asByteArray()
|
precomputedKey.asByteArray()
|
||||||
)
|
)
|
||||||
|
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw BoxCorruptedOrTamperedDataException()
|
throw BoxCorruptedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,7 +189,7 @@ actual object Box {
|
|||||||
recipientsSecretKey.asByteArray()
|
recipientsSecretKey.asByteArray()
|
||||||
)
|
)
|
||||||
|
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw BoxCorruptedOrTamperedDataException()
|
throw BoxCorruptedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -216,7 +217,7 @@ actual object Box {
|
|||||||
recipientsSecretKey.asByteArray()
|
recipientsSecretKey.asByteArray()
|
||||||
)
|
)
|
||||||
|
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw BoxCorruptedOrTamperedDataException()
|
throw BoxCorruptedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.pwhash
|
|||||||
|
|
||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
|
|
||||||
actual object PasswordHash {
|
actual object PasswordHash {
|
||||||
/**
|
/**
|
||||||
@ -88,7 +89,7 @@ actual object PasswordHash {
|
|||||||
password.length.toLong()
|
password.length.toLong()
|
||||||
)
|
)
|
||||||
|
|
||||||
return result == 0
|
return result.isLibsodiumSuccessCode()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.secretbox
|
|||||||
|
|
||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
|
|
||||||
actual object SecretBox {
|
actual object SecretBox {
|
||||||
actual fun easy(message: UByteArray, nonce: UByteArray, key: UByteArray): UByteArray {
|
actual fun easy(message: UByteArray, nonce: UByteArray, key: UByteArray): UByteArray {
|
||||||
@ -29,7 +30,7 @@ actual object SecretBox {
|
|||||||
nonce.asByteArray(),
|
nonce.asByteArray(),
|
||||||
key.asByteArray()
|
key.asByteArray()
|
||||||
)
|
)
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
|
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
|
||||||
}
|
}
|
||||||
return decrypted
|
return decrypted
|
||||||
@ -68,7 +69,7 @@ actual object SecretBox {
|
|||||||
nonce.asByteArray(),
|
nonce.asByteArray(),
|
||||||
key.asByteArray()
|
key.asByteArray()
|
||||||
)
|
)
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
|
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
|
||||||
}
|
}
|
||||||
return message
|
return message
|
||||||
|
@ -3,6 +3,7 @@ package com.ionspin.kotlin.crypto.secretstream
|
|||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||||
import com.ionspin.kotlin.crypto.SecretStreamXChaCha20Poly1305State
|
import com.ionspin.kotlin.crypto.SecretStreamXChaCha20Poly1305State
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
|
|
||||||
actual typealias SecretStreamState = SecretStreamXChaCha20Poly1305State
|
actual typealias SecretStreamState = SecretStreamXChaCha20Poly1305State
|
||||||
|
|
||||||
@ -60,7 +61,7 @@ actual object SecretStream {
|
|||||||
associatedData.asByteArray(),
|
associatedData.asByteArray(),
|
||||||
associatedData.size.toLong()
|
associatedData.size.toLong()
|
||||||
)
|
)
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw SecretStreamCorruptedOrTamperedDataException()
|
throw SecretStreamCorruptedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
return DecryptedDataAndTag(result, tagArray[0])
|
return DecryptedDataAndTag(result, tagArray[0])
|
||||||
|
@ -3,6 +3,7 @@ package com.ionspin.kotlin.crypto.signature
|
|||||||
import com.ionspin.kotlin.crypto.Ed25519SignatureState
|
import com.ionspin.kotlin.crypto.Ed25519SignatureState
|
||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
|
|
||||||
actual typealias SignatureState = Ed25519SignatureState
|
actual typealias SignatureState = Ed25519SignatureState
|
||||||
|
|
||||||
@ -39,7 +40,7 @@ actual object Signature {
|
|||||||
signature.asByteArray(),
|
signature.asByteArray(),
|
||||||
publicKey.asByteArray()
|
publicKey.asByteArray()
|
||||||
)
|
)
|
||||||
if (verificationResult == -1) {
|
if (!verificationResult.isLibsodiumSuccessCode()) {
|
||||||
throw InvalidSignatureException()
|
throw InvalidSignatureException()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,7 +109,7 @@ actual object Signature {
|
|||||||
signedMessage.size.toLong(),
|
signedMessage.size.toLong(),
|
||||||
publicKey.asByteArray()
|
publicKey.asByteArray()
|
||||||
)
|
)
|
||||||
if (verificationResult == -1) {
|
if (!verificationResult.isLibsodiumSuccessCode()) {
|
||||||
throw InvalidSignatureException()
|
throw InvalidSignatureException()
|
||||||
}
|
}
|
||||||
return message
|
return message
|
||||||
@ -150,7 +151,7 @@ actual object Signature {
|
|||||||
publicKey.asByteArray()
|
publicKey.asByteArray()
|
||||||
)
|
)
|
||||||
|
|
||||||
if (verificationResult == -1) {
|
if (!verificationResult.isLibsodiumSuccessCode()) {
|
||||||
throw InvalidSignatureException()
|
throw InvalidSignatureException()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ actual object LibsodiumUtil {
|
|||||||
if (first.size != second.size) {
|
if (first.size != second.size) {
|
||||||
throw RuntimeException("Sodium memcmp() only supports comparing same length arrays")
|
throw RuntimeException("Sodium memcmp() only supports comparing same length arrays")
|
||||||
}
|
}
|
||||||
return sodiumJna.sodium_memcmp(first.asByteArray(), second.asByteArray(), first.size) == 0
|
return sodiumJna.sodium_memcmp(first.asByteArray(), second.asByteArray(), first.size).isLibsodiumSuccessCode()
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun memzero(target: UByteArray) {
|
actual fun memzero(target: UByteArray) {
|
||||||
@ -34,7 +34,7 @@ actual object LibsodiumUtil {
|
|||||||
blocksize,
|
blocksize,
|
||||||
newSize
|
newSize
|
||||||
)
|
)
|
||||||
if (resultCode != 0) {
|
if (!resultCode.isLibsodiumSuccessCode()) {
|
||||||
throw RuntimeException("Padding failed")
|
throw RuntimeException("Padding failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -104,7 +104,7 @@ actual object LibsodiumUtil {
|
|||||||
binLenReference.pointer,
|
binLenReference.pointer,
|
||||||
null
|
null
|
||||||
)
|
)
|
||||||
if (resultCode != 0) {
|
if (!resultCode.isLibsodiumSuccessCode()) {
|
||||||
throw ConversionException()
|
throw ConversionException()
|
||||||
}
|
}
|
||||||
return result.slice(0 until binLenReference.value).toByteArray().asUByteArray()
|
return result.slice(0 until binLenReference.value).toByteArray().asUByteArray()
|
||||||
@ -129,7 +129,7 @@ actual object LibsodiumUtil {
|
|||||||
variant.value
|
variant.value
|
||||||
)
|
)
|
||||||
|
|
||||||
if (resultCode != 0) {
|
if (!resultCode.isLibsodiumSuccessCode()) {
|
||||||
throw ConversionException()
|
throw ConversionException()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.ionspin.kotlin.crypto.aead
|
package com.ionspin.kotlin.crypto.aead
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
import com.ionspin.kotlin.crypto.util.toPtr
|
import com.ionspin.kotlin.crypto.util.toPtr
|
||||||
import kotlinx.cinterop.convert
|
import kotlinx.cinterop.convert
|
||||||
import kotlinx.cinterop.pin
|
import kotlinx.cinterop.pin
|
||||||
@ -94,7 +95,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
|||||||
noncePinned.unpin()
|
noncePinned.unpin()
|
||||||
keyPinned.unpin()
|
keyPinned.unpin()
|
||||||
|
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw AeadCorrupedOrTamperedDataException()
|
throw AeadCorrupedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -177,7 +178,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
|||||||
noncePinned.unpin()
|
noncePinned.unpin()
|
||||||
keyPinned.unpin()
|
keyPinned.unpin()
|
||||||
|
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw AeadCorrupedOrTamperedDataException()
|
throw AeadCorrupedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -253,7 +254,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
|||||||
noncePinned.unpin()
|
noncePinned.unpin()
|
||||||
keyPinned.unpin()
|
keyPinned.unpin()
|
||||||
|
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw AeadCorrupedOrTamperedDataException()
|
throw AeadCorrupedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -336,7 +337,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
|||||||
noncePinned.unpin()
|
noncePinned.unpin()
|
||||||
keyPinned.unpin()
|
keyPinned.unpin()
|
||||||
|
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw AeadCorrupedOrTamperedDataException()
|
throw AeadCorrupedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -412,7 +413,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
|||||||
noncePinned.unpin()
|
noncePinned.unpin()
|
||||||
keyPinned.unpin()
|
keyPinned.unpin()
|
||||||
|
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw AeadCorrupedOrTamperedDataException()
|
throw AeadCorrupedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -495,7 +496,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
|||||||
noncePinned.unpin()
|
noncePinned.unpin()
|
||||||
keyPinned.unpin()
|
keyPinned.unpin()
|
||||||
|
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw AeadCorrupedOrTamperedDataException()
|
throw AeadCorrupedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.ionspin.kotlin.crypto.auth
|
package com.ionspin.kotlin.crypto.auth
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
import com.ionspin.kotlin.crypto.util.toPtr
|
import com.ionspin.kotlin.crypto.util.toPtr
|
||||||
import kotlinx.cinterop.convert
|
import kotlinx.cinterop.convert
|
||||||
import kotlinx.cinterop.pin
|
import kotlinx.cinterop.pin
|
||||||
@ -58,7 +59,7 @@ actual object Auth {
|
|||||||
keyPinned.unpin()
|
keyPinned.unpin()
|
||||||
messagePinned.unpin()
|
messagePinned.unpin()
|
||||||
macPinned.unpin()
|
macPinned.unpin()
|
||||||
return verify == 0
|
return verify.isLibsodiumSuccessCode()
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun authHmacSha256Keygen(): UByteArray {
|
actual fun authHmacSha256Keygen(): UByteArray {
|
||||||
@ -109,7 +110,7 @@ actual object Auth {
|
|||||||
keyPinned.unpin()
|
keyPinned.unpin()
|
||||||
messagePinned.unpin()
|
messagePinned.unpin()
|
||||||
macPinned.unpin()
|
macPinned.unpin()
|
||||||
return verify == 0
|
return verify.isLibsodiumSuccessCode()
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun authHmacSha512Keygen(): UByteArray {
|
actual fun authHmacSha512Keygen(): UByteArray {
|
||||||
@ -160,7 +161,7 @@ actual object Auth {
|
|||||||
keyPinned.unpin()
|
keyPinned.unpin()
|
||||||
messagePinned.unpin()
|
messagePinned.unpin()
|
||||||
macPinned.unpin()
|
macPinned.unpin()
|
||||||
return verify == 0
|
return verify.isLibsodiumSuccessCode()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.ionspin.kotlin.crypto.box
|
package com.ionspin.kotlin.crypto.box
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
import com.ionspin.kotlin.crypto.util.toPtr
|
import com.ionspin.kotlin.crypto.util.toPtr
|
||||||
import kotlinx.cinterop.convert
|
import kotlinx.cinterop.convert
|
||||||
import kotlinx.cinterop.pin
|
import kotlinx.cinterop.pin
|
||||||
@ -124,7 +125,7 @@ actual object Box {
|
|||||||
sendersPublicKeyPinned.unpin()
|
sendersPublicKeyPinned.unpin()
|
||||||
recipientsSecretKeyPinned.unpin()
|
recipientsSecretKeyPinned.unpin()
|
||||||
|
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw BoxCorruptedOrTamperedDataException()
|
throw BoxCorruptedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -212,7 +213,7 @@ actual object Box {
|
|||||||
noncePinned.unpin()
|
noncePinned.unpin()
|
||||||
precomputedKeyPinned.unpin()
|
precomputedKeyPinned.unpin()
|
||||||
|
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw BoxCorruptedOrTamperedDataException()
|
throw BoxCorruptedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -301,7 +302,7 @@ actual object Box {
|
|||||||
recipientsSecretKeyPinned.unpin()
|
recipientsSecretKeyPinned.unpin()
|
||||||
sendersPublicKeyPinned.unpin()
|
sendersPublicKeyPinned.unpin()
|
||||||
|
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw BoxCorruptedOrTamperedDataException()
|
throw BoxCorruptedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -351,7 +352,7 @@ actual object Box {
|
|||||||
recipientsPublicKeyPinned.unpin()
|
recipientsPublicKeyPinned.unpin()
|
||||||
recipientsSecretKeyPinned.unpin()
|
recipientsSecretKeyPinned.unpin()
|
||||||
|
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw BoxCorruptedOrTamperedDataException()
|
throw BoxCorruptedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.ionspin.kotlin.crypto.pwhash
|
package com.ionspin.kotlin.crypto.pwhash
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
import com.ionspin.kotlin.crypto.util.toPtr
|
import com.ionspin.kotlin.crypto.util.toPtr
|
||||||
import kotlinx.cinterop.addressOf
|
import kotlinx.cinterop.addressOf
|
||||||
import kotlinx.cinterop.convert
|
import kotlinx.cinterop.convert
|
||||||
@ -44,7 +45,7 @@ actual object PasswordHash {
|
|||||||
)
|
)
|
||||||
saltPinned.unpin()
|
saltPinned.unpin()
|
||||||
hashedPasswordPinned.unpin()
|
hashedPasswordPinned.unpin()
|
||||||
if (hashingResult != 0) {
|
if (!hashingResult.isLibsodiumSuccessCode()) {
|
||||||
throw PasswordHashingFailed()
|
throw PasswordHashingFailed()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,7 +109,7 @@ actual object PasswordHash {
|
|||||||
password,
|
password,
|
||||||
password.length.convert()
|
password.length.convert()
|
||||||
)
|
)
|
||||||
return result == 0
|
return result.isLibsodiumSuccessCode()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.ionspin.kotlin.crypto.secretbox
|
package com.ionspin.kotlin.crypto.secretbox
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
import com.ionspin.kotlin.crypto.util.toPtr
|
import com.ionspin.kotlin.crypto.util.toPtr
|
||||||
import kotlinx.cinterop.convert
|
import kotlinx.cinterop.convert
|
||||||
import kotlinx.cinterop.pin
|
import kotlinx.cinterop.pin
|
||||||
@ -52,7 +53,7 @@ actual object SecretBox {
|
|||||||
messagePinned.unpin()
|
messagePinned.unpin()
|
||||||
noncePinned.unpin()
|
noncePinned.unpin()
|
||||||
keyPinned.unpin()
|
keyPinned.unpin()
|
||||||
if (verificationResult != 0) {
|
if (!verificationResult.isLibsodiumSuccessCode()) {
|
||||||
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
|
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
|
||||||
}
|
}
|
||||||
return message
|
return message
|
||||||
@ -110,7 +111,7 @@ actual object SecretBox {
|
|||||||
messagePinned.unpin()
|
messagePinned.unpin()
|
||||||
noncePinned.unpin()
|
noncePinned.unpin()
|
||||||
keyPinned.unpin()
|
keyPinned.unpin()
|
||||||
if (verificationResult != 0) {
|
if (!verificationResult.isLibsodiumSuccessCode()) {
|
||||||
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
|
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
|
||||||
}
|
}
|
||||||
return message
|
return message
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.ionspin.kotlin.crypto.secretstream
|
package com.ionspin.kotlin.crypto.secretstream
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
import com.ionspin.kotlin.crypto.util.toPtr
|
import com.ionspin.kotlin.crypto.util.toPtr
|
||||||
import kotlinx.cinterop.convert
|
import kotlinx.cinterop.convert
|
||||||
import kotlinx.cinterop.pin
|
import kotlinx.cinterop.pin
|
||||||
@ -116,7 +117,7 @@ actual object SecretStream {
|
|||||||
messagePinned.unpin()
|
messagePinned.unpin()
|
||||||
associatedDataPinned?.unpin()
|
associatedDataPinned?.unpin()
|
||||||
tagPinned.unpin()
|
tagPinned.unpin()
|
||||||
if (validationResult != 0) {
|
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||||
throw SecretStreamCorruptedOrTamperedDataException()
|
throw SecretStreamCorruptedOrTamperedDataException()
|
||||||
}
|
}
|
||||||
return DecryptedDataAndTag(message, tag[0])
|
return DecryptedDataAndTag(message, tag[0])
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.ionspin.kotlin.crypto.signature
|
package com.ionspin.kotlin.crypto.signature
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
import com.ionspin.kotlin.crypto.util.toPtr
|
import com.ionspin.kotlin.crypto.util.toPtr
|
||||||
import kotlinx.cinterop.convert
|
import kotlinx.cinterop.convert
|
||||||
import kotlinx.cinterop.pin
|
import kotlinx.cinterop.pin
|
||||||
@ -74,7 +75,7 @@ actual object Signature {
|
|||||||
signaturePinned.unpin()
|
signaturePinned.unpin()
|
||||||
publicKeyPinned.unpin()
|
publicKeyPinned.unpin()
|
||||||
|
|
||||||
if (verificationResult == -1) {
|
if (!verificationResult.isLibsodiumSuccessCode()) {
|
||||||
throw InvalidSignatureException()
|
throw InvalidSignatureException()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -162,7 +163,7 @@ actual object Signature {
|
|||||||
messagePinned.unpin()
|
messagePinned.unpin()
|
||||||
signedMessagePinned.unpin()
|
signedMessagePinned.unpin()
|
||||||
publicKeyPinned.unpin()
|
publicKeyPinned.unpin()
|
||||||
if (verificationResult == -1) {
|
if (!verificationResult.isLibsodiumSuccessCode()) {
|
||||||
throw InvalidSignatureException()
|
throw InvalidSignatureException()
|
||||||
}
|
}
|
||||||
return message
|
return message
|
||||||
@ -214,7 +215,7 @@ actual object Signature {
|
|||||||
messagePinned.unpin()
|
messagePinned.unpin()
|
||||||
publicKeyPinned.unpin()
|
publicKeyPinned.unpin()
|
||||||
|
|
||||||
if (verificationResult == -1) {
|
if (!verificationResult.isLibsodiumSuccessCode()) {
|
||||||
throw InvalidSignatureException()
|
throw InvalidSignatureException()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.ionspin.kotlin.crypto.util
|
package com.ionspin.kotlin.crypto.util
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||||
|
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||||
import kotlinx.cinterop.addressOf
|
import kotlinx.cinterop.addressOf
|
||||||
import kotlinx.cinterop.convert
|
import kotlinx.cinterop.convert
|
||||||
import kotlinx.cinterop.pin
|
import kotlinx.cinterop.pin
|
||||||
@ -30,7 +31,7 @@ actual object LibsodiumUtil {
|
|||||||
val result = sodium_memcmp(firstPinned.toPtr(), secondPinned.toPtr(), first.size.convert())
|
val result = sodium_memcmp(firstPinned.toPtr(), secondPinned.toPtr(), first.size.convert())
|
||||||
firstPinned.unpin()
|
firstPinned.unpin()
|
||||||
secondPinned.unpin()
|
secondPinned.unpin()
|
||||||
return result == 0
|
return result.isLibsodiumSuccessCode()
|
||||||
}
|
}
|
||||||
|
|
||||||
actual fun memzero(target: UByteArray) {
|
actual fun memzero(target: UByteArray) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user