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:
18 files changed
+85
-49
No files matched your search
+7
-6
@@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.aead
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||
|
||||
actual object AuthenticatedEncryptionWithAssociatedData {
|
||||
|
||||
@@ -47,7 +48,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray(),
|
||||
)
|
||||
if (validationResult != 0) {
|
||||
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||
throw AeadCorrupedOrTamperedDataException()
|
||||
}
|
||||
return message
|
||||
@@ -95,7 +96,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray(),
|
||||
)
|
||||
if (validationResult != 0) {
|
||||
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||
throw AeadCorrupedOrTamperedDataException()
|
||||
}
|
||||
return message
|
||||
@@ -140,7 +141,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray(),
|
||||
)
|
||||
if (validationResult != 0) {
|
||||
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||
throw AeadCorrupedOrTamperedDataException()
|
||||
}
|
||||
return message
|
||||
@@ -188,7 +189,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray(),
|
||||
)
|
||||
if (validationResult != 0) {
|
||||
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||
throw AeadCorrupedOrTamperedDataException()
|
||||
}
|
||||
return message
|
||||
@@ -233,7 +234,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray(),
|
||||
)
|
||||
if (validationResult != 0) {
|
||||
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||
throw AeadCorrupedOrTamperedDataException()
|
||||
}
|
||||
return message
|
||||
@@ -281,7 +282,7 @@ actual object AuthenticatedEncryptionWithAssociatedData {
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray(),
|
||||
)
|
||||
if (validationResult != 0) {
|
||||
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||
throw AeadCorrupedOrTamperedDataException()
|
||||
}
|
||||
return message
|
||||
|
||||
+4
-3
@@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.auth
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||
|
||||
actual object Auth {
|
||||
actual fun authKeygen(): UByteArray {
|
||||
@@ -27,7 +28,7 @@ actual object Auth {
|
||||
message.asByteArray(),
|
||||
message.size.toLong(),
|
||||
key.asByteArray()
|
||||
) == 0
|
||||
).isLibsodiumSuccessCode()
|
||||
}
|
||||
|
||||
actual fun authHmacSha256Keygen(): UByteArray {
|
||||
@@ -57,7 +58,7 @@ actual object Auth {
|
||||
message.asByteArray(),
|
||||
message.size.toLong(),
|
||||
key.asByteArray()
|
||||
) == 0
|
||||
).isLibsodiumSuccessCode()
|
||||
}
|
||||
|
||||
actual fun authHmacSha512Keygen(): UByteArray {
|
||||
@@ -87,7 +88,7 @@ actual object Auth {
|
||||
message.asByteArray(),
|
||||
message.size.toLong(),
|
||||
key.asByteArray()
|
||||
) == 0
|
||||
).isLibsodiumSuccessCode()
|
||||
}
|
||||
|
||||
}
|
||||
+5
-4
@@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.box
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||
|
||||
actual object Box {
|
||||
/**
|
||||
@@ -73,7 +74,7 @@ actual object Box {
|
||||
sendersPublicKey.asByteArray(),
|
||||
recipientsSecretKey.asByteArray()
|
||||
)
|
||||
if (validationResult != 0) {
|
||||
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||
throw BoxCorruptedOrTamperedDataException()
|
||||
}
|
||||
|
||||
@@ -128,7 +129,7 @@ actual object Box {
|
||||
precomputedKey.asByteArray()
|
||||
)
|
||||
|
||||
if (validationResult != 0) {
|
||||
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||
throw BoxCorruptedOrTamperedDataException()
|
||||
}
|
||||
|
||||
@@ -188,7 +189,7 @@ actual object Box {
|
||||
recipientsSecretKey.asByteArray()
|
||||
)
|
||||
|
||||
if (validationResult != 0) {
|
||||
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||
throw BoxCorruptedOrTamperedDataException()
|
||||
}
|
||||
|
||||
@@ -216,7 +217,7 @@ actual object Box {
|
||||
recipientsSecretKey.asByteArray()
|
||||
)
|
||||
|
||||
if (validationResult != 0) {
|
||||
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||
throw BoxCorruptedOrTamperedDataException()
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.pwhash
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||
|
||||
actual object PasswordHash {
|
||||
/**
|
||||
@@ -88,7 +89,7 @@ actual object PasswordHash {
|
||||
password.length.toLong()
|
||||
)
|
||||
|
||||
return result == 0
|
||||
return result.isLibsodiumSuccessCode()
|
||||
}
|
||||
|
||||
}
|
||||
+3
-2
@@ -2,6 +2,7 @@ package com.ionspin.kotlin.crypto.secretbox
|
||||
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||
|
||||
actual object SecretBox {
|
||||
actual fun easy(message: UByteArray, nonce: UByteArray, key: UByteArray): UByteArray {
|
||||
@@ -29,7 +30,7 @@ actual object SecretBox {
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray()
|
||||
)
|
||||
if (validationResult != 0) {
|
||||
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
|
||||
}
|
||||
return decrypted
|
||||
@@ -68,7 +69,7 @@ actual object SecretBox {
|
||||
nonce.asByteArray(),
|
||||
key.asByteArray()
|
||||
)
|
||||
if (validationResult != 0) {
|
||||
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||
throw SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey()
|
||||
}
|
||||
return message
|
||||
|
||||
+2
-1
@@ -3,6 +3,7 @@ package com.ionspin.kotlin.crypto.secretstream
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
import com.ionspin.kotlin.crypto.SecretStreamXChaCha20Poly1305State
|
||||
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||
|
||||
actual typealias SecretStreamState = SecretStreamXChaCha20Poly1305State
|
||||
|
||||
@@ -60,7 +61,7 @@ actual object SecretStream {
|
||||
associatedData.asByteArray(),
|
||||
associatedData.size.toLong()
|
||||
)
|
||||
if (validationResult != 0) {
|
||||
if (!validationResult.isLibsodiumSuccessCode()) {
|
||||
throw SecretStreamCorruptedOrTamperedDataException()
|
||||
}
|
||||
return DecryptedDataAndTag(result, tagArray[0])
|
||||
|
||||
+4
-3
@@ -3,6 +3,7 @@ package com.ionspin.kotlin.crypto.signature
|
||||
import com.ionspin.kotlin.crypto.Ed25519SignatureState
|
||||
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
|
||||
import com.ionspin.kotlin.crypto.util.isLibsodiumSuccessCode
|
||||
|
||||
actual typealias SignatureState = Ed25519SignatureState
|
||||
|
||||
@@ -39,7 +40,7 @@ actual object Signature {
|
||||
signature.asByteArray(),
|
||||
publicKey.asByteArray()
|
||||
)
|
||||
if (verificationResult == -1) {
|
||||
if (!verificationResult.isLibsodiumSuccessCode()) {
|
||||
throw InvalidSignatureException()
|
||||
}
|
||||
}
|
||||
@@ -108,7 +109,7 @@ actual object Signature {
|
||||
signedMessage.size.toLong(),
|
||||
publicKey.asByteArray()
|
||||
)
|
||||
if (verificationResult == -1) {
|
||||
if (!verificationResult.isLibsodiumSuccessCode()) {
|
||||
throw InvalidSignatureException()
|
||||
}
|
||||
return message
|
||||
@@ -150,7 +151,7 @@ actual object Signature {
|
||||
publicKey.asByteArray()
|
||||
)
|
||||
|
||||
if (verificationResult == -1) {
|
||||
if (!verificationResult.isLibsodiumSuccessCode()) {
|
||||
throw InvalidSignatureException()
|
||||
}
|
||||
}
|
||||
|
||||
+4
-4
@@ -9,7 +9,7 @@ actual object LibsodiumUtil {
|
||||
if (first.size != second.size) {
|
||||
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) {
|
||||
@@ -34,7 +34,7 @@ actual object LibsodiumUtil {
|
||||
blocksize,
|
||||
newSize
|
||||
)
|
||||
if (resultCode != 0) {
|
||||
if (!resultCode.isLibsodiumSuccessCode()) {
|
||||
throw RuntimeException("Padding failed")
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ actual object LibsodiumUtil {
|
||||
binLenReference.pointer,
|
||||
null
|
||||
)
|
||||
if (resultCode != 0) {
|
||||
if (!resultCode.isLibsodiumSuccessCode()) {
|
||||
throw ConversionException()
|
||||
}
|
||||
return result.slice(0 until binLenReference.value).toByteArray().asUByteArray()
|
||||
@@ -129,7 +129,7 @@ actual object LibsodiumUtil {
|
||||
variant.value
|
||||
)
|
||||
|
||||
if (resultCode != 0) {
|
||||
if (!resultCode.isLibsodiumSuccessCode()) {
|
||||
throw ConversionException()
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user