diff --git a/cryptography-core/api/cryptography-core.api b/cryptography-core/api/cryptography-core.api index 7c5b2624..2bf238b7 100644 --- a/cryptography-core/api/cryptography-core.api +++ b/cryptography-core/api/cryptography-core.api @@ -672,10 +672,14 @@ public abstract interface class dev/whyoleg/cryptography/functions/UpdateFunctio } public abstract interface class dev/whyoleg/cryptography/functions/VerifyFunction : dev/whyoleg/cryptography/functions/UpdateFunction { - public fun verify (Lkotlinx/io/bytestring/ByteString;II)Z - public abstract fun verify ([BII)Z - public static synthetic fun verify$default (Ldev/whyoleg/cryptography/functions/VerifyFunction;Lkotlinx/io/bytestring/ByteString;IIILjava/lang/Object;)Z - public static synthetic fun verify$default (Ldev/whyoleg/cryptography/functions/VerifyFunction;[BIIILjava/lang/Object;)Z + public fun tryVerify (Lkotlinx/io/bytestring/ByteString;II)Z + public abstract fun tryVerify ([BII)Z + public static synthetic fun tryVerify$default (Ldev/whyoleg/cryptography/functions/VerifyFunction;Lkotlinx/io/bytestring/ByteString;IIILjava/lang/Object;)Z + public static synthetic fun tryVerify$default (Ldev/whyoleg/cryptography/functions/VerifyFunction;[BIIILjava/lang/Object;)Z + public fun verify (Lkotlinx/io/bytestring/ByteString;II)V + public abstract fun verify ([BII)V + public static synthetic fun verify$default (Ldev/whyoleg/cryptography/functions/VerifyFunction;Lkotlinx/io/bytestring/ByteString;IIILjava/lang/Object;)V + public static synthetic fun verify$default (Ldev/whyoleg/cryptography/functions/VerifyFunction;[BIIILjava/lang/Object;)V } public abstract interface class dev/whyoleg/cryptography/materials/key/EncodableKey : dev/whyoleg/cryptography/materials/key/Key { @@ -816,14 +820,23 @@ public abstract interface class dev/whyoleg/cryptography/operations/SignatureGen public abstract interface class dev/whyoleg/cryptography/operations/SignatureVerifier { public abstract fun createVerifyFunction ()Ldev/whyoleg/cryptography/functions/VerifyFunction; + public fun tryVerifySignature (Lkotlinx/io/RawSource;Lkotlinx/io/bytestring/ByteString;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun tryVerifySignature (Lkotlinx/io/bytestring/ByteString;Lkotlinx/io/bytestring/ByteString;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun tryVerifySignature ([B[BLkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static synthetic fun tryVerifySignature$suspendImpl (Ldev/whyoleg/cryptography/operations/SignatureVerifier;Lkotlinx/io/RawSource;Lkotlinx/io/bytestring/ByteString;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static synthetic fun tryVerifySignature$suspendImpl (Ldev/whyoleg/cryptography/operations/SignatureVerifier;Lkotlinx/io/bytestring/ByteString;Lkotlinx/io/bytestring/ByteString;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; + public static synthetic fun tryVerifySignature$suspendImpl (Ldev/whyoleg/cryptography/operations/SignatureVerifier;[B[BLkotlin/coroutines/Continuation;)Ljava/lang/Object; + public fun tryVerifySignatureBlocking (Lkotlinx/io/RawSource;Lkotlinx/io/bytestring/ByteString;)Z + public fun tryVerifySignatureBlocking (Lkotlinx/io/bytestring/ByteString;Lkotlinx/io/bytestring/ByteString;)Z + public fun tryVerifySignatureBlocking ([B[B)Z public fun verifySignature (Lkotlinx/io/RawSource;Lkotlinx/io/bytestring/ByteString;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun verifySignature (Lkotlinx/io/bytestring/ByteString;Lkotlinx/io/bytestring/ByteString;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public fun verifySignature ([B[BLkotlin/coroutines/Continuation;)Ljava/lang/Object; public static synthetic fun verifySignature$suspendImpl (Ldev/whyoleg/cryptography/operations/SignatureVerifier;Lkotlinx/io/RawSource;Lkotlinx/io/bytestring/ByteString;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static synthetic fun verifySignature$suspendImpl (Ldev/whyoleg/cryptography/operations/SignatureVerifier;Lkotlinx/io/bytestring/ByteString;Lkotlinx/io/bytestring/ByteString;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static synthetic fun verifySignature$suspendImpl (Ldev/whyoleg/cryptography/operations/SignatureVerifier;[B[BLkotlin/coroutines/Continuation;)Ljava/lang/Object; - public fun verifySignatureBlocking (Lkotlinx/io/RawSource;Lkotlinx/io/bytestring/ByteString;)Z - public fun verifySignatureBlocking (Lkotlinx/io/bytestring/ByteString;Lkotlinx/io/bytestring/ByteString;)Z - public fun verifySignatureBlocking ([B[B)Z + public fun verifySignatureBlocking (Lkotlinx/io/RawSource;Lkotlinx/io/bytestring/ByteString;)V + public fun verifySignatureBlocking (Lkotlinx/io/bytestring/ByteString;Lkotlinx/io/bytestring/ByteString;)V + public fun verifySignatureBlocking ([B[B)V } diff --git a/cryptography-core/api/cryptography-core.klib.api b/cryptography-core/api/cryptography-core.klib.api index 1e6fff32..cef21234 100644 --- a/cryptography-core/api/cryptography-core.klib.api +++ b/cryptography-core/api/cryptography-core.klib.api @@ -553,8 +553,10 @@ abstract interface dev.whyoleg.cryptography.functions/UpdateFunction : kotlin/Au } abstract interface dev.whyoleg.cryptography.functions/VerifyFunction : dev.whyoleg.cryptography.functions/UpdateFunction { // dev.whyoleg.cryptography.functions/VerifyFunction|null[0] - abstract fun verify(kotlin/ByteArray, kotlin/Int = ..., kotlin/Int = ...): kotlin/Boolean // dev.whyoleg.cryptography.functions/VerifyFunction.verify|verify(kotlin.ByteArray;kotlin.Int;kotlin.Int){}[0] - open fun verify(kotlinx.io.bytestring/ByteString, kotlin/Int = ..., kotlin/Int = ...): kotlin/Boolean // dev.whyoleg.cryptography.functions/VerifyFunction.verify|verify(kotlinx.io.bytestring.ByteString;kotlin.Int;kotlin.Int){}[0] + abstract fun tryVerify(kotlin/ByteArray, kotlin/Int = ..., kotlin/Int = ...): kotlin/Boolean // dev.whyoleg.cryptography.functions/VerifyFunction.tryVerify|tryVerify(kotlin.ByteArray;kotlin.Int;kotlin.Int){}[0] + abstract fun verify(kotlin/ByteArray, kotlin/Int = ..., kotlin/Int = ...) // dev.whyoleg.cryptography.functions/VerifyFunction.verify|verify(kotlin.ByteArray;kotlin.Int;kotlin.Int){}[0] + open fun tryVerify(kotlinx.io.bytestring/ByteString, kotlin/Int = ..., kotlin/Int = ...): kotlin/Boolean // dev.whyoleg.cryptography.functions/VerifyFunction.tryVerify|tryVerify(kotlinx.io.bytestring.ByteString;kotlin.Int;kotlin.Int){}[0] + open fun verify(kotlinx.io.bytestring/ByteString, kotlin/Int = ..., kotlin/Int = ...) // dev.whyoleg.cryptography.functions/VerifyFunction.verify|verify(kotlinx.io.bytestring.ByteString;kotlin.Int;kotlin.Int){}[0] } abstract interface dev.whyoleg.cryptography.materials.key/Key // dev.whyoleg.cryptography.materials.key/Key|null[0] @@ -635,12 +637,18 @@ abstract interface dev.whyoleg.cryptography.operations/SignatureGenerator { // d abstract interface dev.whyoleg.cryptography.operations/SignatureVerifier { // dev.whyoleg.cryptography.operations/SignatureVerifier|null[0] abstract fun createVerifyFunction(): dev.whyoleg.cryptography.functions/VerifyFunction // dev.whyoleg.cryptography.operations/SignatureVerifier.createVerifyFunction|createVerifyFunction(){}[0] - open fun verifySignatureBlocking(kotlin/ByteArray, kotlin/ByteArray): kotlin/Boolean // dev.whyoleg.cryptography.operations/SignatureVerifier.verifySignatureBlocking|verifySignatureBlocking(kotlin.ByteArray;kotlin.ByteArray){}[0] - open fun verifySignatureBlocking(kotlinx.io.bytestring/ByteString, kotlinx.io.bytestring/ByteString): kotlin/Boolean // dev.whyoleg.cryptography.operations/SignatureVerifier.verifySignatureBlocking|verifySignatureBlocking(kotlinx.io.bytestring.ByteString;kotlinx.io.bytestring.ByteString){}[0] - open fun verifySignatureBlocking(kotlinx.io/RawSource, kotlinx.io.bytestring/ByteString): kotlin/Boolean // dev.whyoleg.cryptography.operations/SignatureVerifier.verifySignatureBlocking|verifySignatureBlocking(kotlinx.io.RawSource;kotlinx.io.bytestring.ByteString){}[0] - open suspend fun verifySignature(kotlin/ByteArray, kotlin/ByteArray): kotlin/Boolean // dev.whyoleg.cryptography.operations/SignatureVerifier.verifySignature|verifySignature(kotlin.ByteArray;kotlin.ByteArray){}[0] - open suspend fun verifySignature(kotlinx.io.bytestring/ByteString, kotlinx.io.bytestring/ByteString): kotlin/Boolean // dev.whyoleg.cryptography.operations/SignatureVerifier.verifySignature|verifySignature(kotlinx.io.bytestring.ByteString;kotlinx.io.bytestring.ByteString){}[0] - open suspend fun verifySignature(kotlinx.io/RawSource, kotlinx.io.bytestring/ByteString): kotlin/Boolean // dev.whyoleg.cryptography.operations/SignatureVerifier.verifySignature|verifySignature(kotlinx.io.RawSource;kotlinx.io.bytestring.ByteString){}[0] + open fun tryVerifySignatureBlocking(kotlin/ByteArray, kotlin/ByteArray): kotlin/Boolean // dev.whyoleg.cryptography.operations/SignatureVerifier.tryVerifySignatureBlocking|tryVerifySignatureBlocking(kotlin.ByteArray;kotlin.ByteArray){}[0] + open fun tryVerifySignatureBlocking(kotlinx.io.bytestring/ByteString, kotlinx.io.bytestring/ByteString): kotlin/Boolean // dev.whyoleg.cryptography.operations/SignatureVerifier.tryVerifySignatureBlocking|tryVerifySignatureBlocking(kotlinx.io.bytestring.ByteString;kotlinx.io.bytestring.ByteString){}[0] + open fun tryVerifySignatureBlocking(kotlinx.io/RawSource, kotlinx.io.bytestring/ByteString): kotlin/Boolean // dev.whyoleg.cryptography.operations/SignatureVerifier.tryVerifySignatureBlocking|tryVerifySignatureBlocking(kotlinx.io.RawSource;kotlinx.io.bytestring.ByteString){}[0] + open fun verifySignatureBlocking(kotlin/ByteArray, kotlin/ByteArray) // dev.whyoleg.cryptography.operations/SignatureVerifier.verifySignatureBlocking|verifySignatureBlocking(kotlin.ByteArray;kotlin.ByteArray){}[0] + open fun verifySignatureBlocking(kotlinx.io.bytestring/ByteString, kotlinx.io.bytestring/ByteString) // dev.whyoleg.cryptography.operations/SignatureVerifier.verifySignatureBlocking|verifySignatureBlocking(kotlinx.io.bytestring.ByteString;kotlinx.io.bytestring.ByteString){}[0] + open fun verifySignatureBlocking(kotlinx.io/RawSource, kotlinx.io.bytestring/ByteString) // dev.whyoleg.cryptography.operations/SignatureVerifier.verifySignatureBlocking|verifySignatureBlocking(kotlinx.io.RawSource;kotlinx.io.bytestring.ByteString){}[0] + open suspend fun tryVerifySignature(kotlin/ByteArray, kotlin/ByteArray): kotlin/Boolean // dev.whyoleg.cryptography.operations/SignatureVerifier.tryVerifySignature|tryVerifySignature(kotlin.ByteArray;kotlin.ByteArray){}[0] + open suspend fun tryVerifySignature(kotlinx.io.bytestring/ByteString, kotlinx.io.bytestring/ByteString): kotlin/Boolean // dev.whyoleg.cryptography.operations/SignatureVerifier.tryVerifySignature|tryVerifySignature(kotlinx.io.bytestring.ByteString;kotlinx.io.bytestring.ByteString){}[0] + open suspend fun tryVerifySignature(kotlinx.io/RawSource, kotlinx.io.bytestring/ByteString): kotlin/Boolean // dev.whyoleg.cryptography.operations/SignatureVerifier.tryVerifySignature|tryVerifySignature(kotlinx.io.RawSource;kotlinx.io.bytestring.ByteString){}[0] + open suspend fun verifySignature(kotlin/ByteArray, kotlin/ByteArray) // dev.whyoleg.cryptography.operations/SignatureVerifier.verifySignature|verifySignature(kotlin.ByteArray;kotlin.ByteArray){}[0] + open suspend fun verifySignature(kotlinx.io.bytestring/ByteString, kotlinx.io.bytestring/ByteString) // dev.whyoleg.cryptography.operations/SignatureVerifier.verifySignature|verifySignature(kotlinx.io.bytestring.ByteString;kotlinx.io.bytestring.ByteString){}[0] + open suspend fun verifySignature(kotlinx.io/RawSource, kotlinx.io.bytestring/ByteString) // dev.whyoleg.cryptography.operations/SignatureVerifier.verifySignature|verifySignature(kotlinx.io.RawSource;kotlinx.io.bytestring.ByteString){}[0] } abstract interface dev.whyoleg.cryptography/CryptographyAlgorithm { // dev.whyoleg.cryptography/CryptographyAlgorithm|null[0] diff --git a/cryptography-core/src/commonMain/kotlin/functions/VerifyFunction.kt b/cryptography-core/src/commonMain/kotlin/functions/VerifyFunction.kt index ba296d2e..d92440ff 100644 --- a/cryptography-core/src/commonMain/kotlin/functions/VerifyFunction.kt +++ b/cryptography-core/src/commonMain/kotlin/functions/VerifyFunction.kt @@ -8,8 +8,13 @@ import dev.whyoleg.cryptography.* import kotlinx.io.bytestring.* public interface VerifyFunction : UpdateFunction { - public fun verify(signature: ByteArray, startIndex: Int = 0, endIndex: Int = signature.size): Boolean - public fun verify(signature: ByteString, startIndex: Int = 0, endIndex: Int = signature.size): Boolean { + public fun tryVerify(signature: ByteArray, startIndex: Int = 0, endIndex: Int = signature.size): Boolean + public fun tryVerify(signature: ByteString, startIndex: Int = 0, endIndex: Int = signature.size): Boolean { + return tryVerify(signature.asByteArray(), startIndex, endIndex) + } + + public fun verify(signature: ByteArray, startIndex: Int = 0, endIndex: Int = signature.size) + public fun verify(signature: ByteString, startIndex: Int = 0, endIndex: Int = signature.size) { return verify(signature.asByteArray(), startIndex, endIndex) } } diff --git a/cryptography-core/src/commonMain/kotlin/operations/Hasher.kt b/cryptography-core/src/commonMain/kotlin/operations/Hasher.kt index 1c723080..6ed7b274 100644 --- a/cryptography-core/src/commonMain/kotlin/operations/Hasher.kt +++ b/cryptography-core/src/commonMain/kotlin/operations/Hasher.kt @@ -13,18 +13,26 @@ import kotlinx.io.bytestring.* public interface Hasher { public fun createHashFunction(): HashFunction - public suspend fun hash(data: ByteArray): ByteArray = hashBlocking(data) + public suspend fun hash(data: ByteArray): ByteArray { + return hashBlocking(data) + } - public suspend fun hash(data: ByteString): ByteString = hash(data.asByteArray()).asByteString() + public suspend fun hash(data: ByteString): ByteString { + return hash(data.asByteArray()).asByteString() + } - public suspend fun hash(data: RawSource): ByteString = hashBlocking(data) + public suspend fun hash(data: RawSource): ByteString { + return hashBlocking(data) + } public fun hashBlocking(data: ByteArray): ByteArray = createHashFunction().use { it.update(data) it.hashToByteArray() } - public fun hashBlocking(data: ByteString): ByteString = hashBlocking(data.asByteArray()).asByteString() + public fun hashBlocking(data: ByteString): ByteString { + return hashBlocking(data.asByteArray()).asByteString() + } public fun hashBlocking(data: RawSource): ByteString = createHashFunction().use { it.update(data) diff --git a/cryptography-core/src/commonMain/kotlin/operations/Signature.kt b/cryptography-core/src/commonMain/kotlin/operations/Signature.kt index 7717653b..65cfd737 100644 --- a/cryptography-core/src/commonMain/kotlin/operations/Signature.kt +++ b/cryptography-core/src/commonMain/kotlin/operations/Signature.kt @@ -13,16 +13,27 @@ import kotlinx.io.bytestring.* public interface SignatureGenerator { public fun createSignFunction(): SignFunction - public suspend fun generateSignature(data: ByteArray): ByteArray = generateSignatureBlocking(data) - public suspend fun generateSignature(data: ByteString): ByteString = generateSignature(data.asByteArray()).asByteString() - public suspend fun generateSignature(data: RawSource): ByteString = generateSignatureBlocking(data) + public suspend fun generateSignature(data: ByteArray): ByteArray { + return generateSignatureBlocking(data) + } + + public suspend fun generateSignature(data: ByteString): ByteString { + return generateSignature(data.asByteArray()).asByteString() + } + + public suspend fun generateSignature(data: RawSource): ByteString { + return generateSignatureBlocking(data) + } public fun generateSignatureBlocking(data: ByteArray): ByteArray = createSignFunction().use { it.update(data) it.signToByteArray() } - public fun generateSignatureBlocking(data: ByteString): ByteString = generateSignatureBlocking(data.asByteArray()).asByteString() + public fun generateSignatureBlocking(data: ByteString): ByteString { + return generateSignatureBlocking(data.asByteArray()).asByteString() + } + public fun generateSignatureBlocking(data: RawSource): ByteString = createSignFunction().use { it.update(data) it.sign() @@ -33,21 +44,54 @@ public interface SignatureGenerator { public interface SignatureVerifier { public fun createVerifyFunction(): VerifyFunction - public suspend fun verifySignature(data: ByteArray, signature: ByteArray): Boolean = verifySignatureBlocking(data, signature) - public suspend fun verifySignature(data: ByteString, signature: ByteString): Boolean = - verifySignature(data.asByteArray(), signature.asByteArray()) + public suspend fun tryVerifySignature(data: ByteArray, signature: ByteArray): Boolean { + return tryVerifySignatureBlocking(data, signature) + } + + public suspend fun tryVerifySignature(data: ByteString, signature: ByteString): Boolean { + return tryVerifySignature(data.asByteArray(), signature.asByteArray()) + } + + public suspend fun tryVerifySignature(data: RawSource, signature: ByteString): Boolean { + return tryVerifySignatureBlocking(data, signature) + } + + public fun tryVerifySignatureBlocking(data: ByteArray, signature: ByteArray): Boolean = createVerifyFunction().use { + it.update(data) + it.tryVerify(signature) + } - public suspend fun verifySignature(data: RawSource, signature: ByteString): Boolean = verifySignatureBlocking(data, signature) + public fun tryVerifySignatureBlocking(data: ByteString, signature: ByteString): Boolean { + return tryVerifySignatureBlocking(data.asByteArray(), signature.asByteArray()) + } - public fun verifySignatureBlocking(data: ByteArray, signature: ByteArray): Boolean = createVerifyFunction().use { + public fun tryVerifySignatureBlocking(data: RawSource, signature: ByteString): Boolean = createVerifyFunction().use { + it.update(data) + it.tryVerify(signature) + } + + public suspend fun verifySignature(data: ByteArray, signature: ByteArray) { + return verifySignatureBlocking(data, signature) + } + + public suspend fun verifySignature(data: ByteString, signature: ByteString) { + return verifySignature(data.asByteArray(), signature.asByteArray()) + } + + public suspend fun verifySignature(data: RawSource, signature: ByteString) { + return verifySignatureBlocking(data, signature) + } + + public fun verifySignatureBlocking(data: ByteArray, signature: ByteArray): Unit = createVerifyFunction().use { it.update(data) it.verify(signature) } - public fun verifySignatureBlocking(data: ByteString, signature: ByteString): Boolean = - verifySignatureBlocking(data.asByteArray(), signature.asByteArray()) + public fun verifySignatureBlocking(data: ByteString, signature: ByteString) { + return verifySignatureBlocking(data.asByteArray(), signature.asByteArray()) + } - public fun verifySignatureBlocking(data: RawSource, signature: ByteString): Boolean = createVerifyFunction().use { + public fun verifySignatureBlocking(data: RawSource, signature: ByteString): Unit = createVerifyFunction().use { it.update(data) it.verify(signature) } diff --git a/cryptography-providers-tests/src/commonMain/kotlin/compatibility/EcdsaCompatibilityTest.kt b/cryptography-providers-tests/src/commonMain/kotlin/compatibility/EcdsaCompatibilityTest.kt index 873e53e0..b59268af 100644 --- a/cryptography-providers-tests/src/commonMain/kotlin/compatibility/EcdsaCompatibilityTest.kt +++ b/cryptography-providers-tests/src/commonMain/kotlin/compatibility/EcdsaCompatibilityTest.kt @@ -66,7 +66,7 @@ abstract class EcdsaCompatibilityTest( val signature = signer.generateSignature(data) logger.log { "signature.size = ${signature.size}" } - assertTrue(verifier.verifySignature(data, signature), "Initial Verify") + assertTrue(verifier.tryVerifySignature(data, signature), "Initial Verify") api.signatures.saveData(signatureParametersId, SignatureData(keyReference, data, signature)) } @@ -87,10 +87,10 @@ abstract class EcdsaCompatibilityTest( val generators = privateKeys.map { it.signatureGenerator(signatureParameters.digest, signatureParameters.signatureFormat) } verifiers.forEach { verifier -> - assertTrue(verifier.verifySignature(data, signature), "Verify") + assertTrue(verifier.tryVerifySignature(data, signature), "Verify") generators.forEach { generator -> - assertTrue(verifier.verifySignature(data, generator.generateSignature(data)), "Sign-Verify") + assertTrue(verifier.tryVerifySignature(data, generator.generateSignature(data)), "Sign-Verify") } } } diff --git a/cryptography-providers-tests/src/commonMain/kotlin/compatibility/HmacCompatibilityTest.kt b/cryptography-providers-tests/src/commonMain/kotlin/compatibility/HmacCompatibilityTest.kt index eb56af40..9ee8f360 100644 --- a/cryptography-providers-tests/src/commonMain/kotlin/compatibility/HmacCompatibilityTest.kt +++ b/cryptography-providers-tests/src/commonMain/kotlin/compatibility/HmacCompatibilityTest.kt @@ -52,7 +52,7 @@ abstract class HmacCompatibilityTest(provider: CryptographyProvider) : Compatibi val signature = signatureGenerator.generateSignature(data) logger.log { "signature.size = ${signature.size}" } - assertTrue(signatureVerifier.verifySignature(data, signature), "Initial Verify") + assertTrue(signatureVerifier.tryVerifySignature(data, signature), "Initial Verify") api.signatures.saveData(signatureParametersId, SignatureData(keyReference, data, signature)) } @@ -88,8 +88,8 @@ abstract class HmacCompatibilityTest(provider: CryptographyProvider) : Compatibi val verifier = key.signatureVerifier() val generator = key.signatureGenerator() - assertTrue(verifier.verifySignature(data, signature), "Verify") - assertTrue(verifier.verifySignature(data, generator.generateSignature(data)), "Sign-Verify") + assertTrue(verifier.tryVerifySignature(data, signature), "Verify") + assertTrue(verifier.tryVerifySignature(data, generator.generateSignature(data)), "Sign-Verify") } } } diff --git a/cryptography-providers-tests/src/commonMain/kotlin/compatibility/RsaPkcs1CompatibilityTest.kt b/cryptography-providers-tests/src/commonMain/kotlin/compatibility/RsaPkcs1CompatibilityTest.kt index 7a5a3947..82bda740 100644 --- a/cryptography-providers-tests/src/commonMain/kotlin/compatibility/RsaPkcs1CompatibilityTest.kt +++ b/cryptography-providers-tests/src/commonMain/kotlin/compatibility/RsaPkcs1CompatibilityTest.kt @@ -33,7 +33,7 @@ abstract class RsaPkcs1CompatibilityTest(provider: CryptographyProvider) : val signature = signer.generateSignature(data) logger.log { "signature.size = ${signature.size}" } - assertTrue(verifier.verifySignature(data, signature), "Initial Verify") + assertTrue(verifier.tryVerifySignature(data, signature), "Initial Verify") api.signatures.saveData(signatureParametersId, SignatureData(keyReference, data, signature)) } @@ -50,10 +50,10 @@ abstract class RsaPkcs1CompatibilityTest(provider: CryptographyProvider) : val generators = privateKeys.map { it.signatureGenerator() } verifiers.forEach { verifier -> - assertTrue(verifier.verifySignature(data, signature), "Verify") + assertTrue(verifier.tryVerifySignature(data, signature), "Verify") generators.forEach { generator -> - assertTrue(verifier.verifySignature(data, generator.generateSignature(data)), "Sign-Verify") + assertTrue(verifier.tryVerifySignature(data, generator.generateSignature(data)), "Sign-Verify") } } } diff --git a/cryptography-providers-tests/src/commonMain/kotlin/compatibility/RsaPssCompatibilityTest.kt b/cryptography-providers-tests/src/commonMain/kotlin/compatibility/RsaPssCompatibilityTest.kt index 996969fe..78d79b56 100644 --- a/cryptography-providers-tests/src/commonMain/kotlin/compatibility/RsaPssCompatibilityTest.kt +++ b/cryptography-providers-tests/src/commonMain/kotlin/compatibility/RsaPssCompatibilityTest.kt @@ -54,7 +54,7 @@ abstract class RsaPssCompatibilityTest(provider: CryptographyProvider) : val signature = signatureGenerator.generateSignature(data) logger.log { "signature.size = ${signature.size}" } - assertTrue(signatureVerifier.verifySignature(data, signature), "Initial Verify") + assertTrue(signatureVerifier.tryVerifySignature(data, signature), "Initial Verify") api.signatures.saveData(signatureParametersId, SignatureData(keyReference, data, signature)) } @@ -84,10 +84,10 @@ abstract class RsaPssCompatibilityTest(provider: CryptographyProvider) : } verifiers.forEach { verifier -> - assertTrue(verifier.verifySignature(data, signature), "Verify") + assertTrue(verifier.tryVerifySignature(data, signature), "Verify") generators.forEach { generator -> - assertTrue(verifier.verifySignature(data, generator.generateSignature(data)), "Sign-Verify") + assertTrue(verifier.tryVerifySignature(data, generator.generateSignature(data)), "Sign-Verify") } } } diff --git a/cryptography-providers-tests/src/commonMain/kotlin/default/EcdsaTest.kt b/cryptography-providers-tests/src/commonMain/kotlin/default/EcdsaTest.kt index e6e052c2..27b82791 100644 --- a/cryptography-providers-tests/src/commonMain/kotlin/default/EcdsaTest.kt +++ b/cryptography-providers-tests/src/commonMain/kotlin/default/EcdsaTest.kt @@ -56,7 +56,7 @@ abstract class EcdsaTest(provider: CryptographyProvider) : ProviderTest(provider val data = CryptographyRandom.nextBytes(size) val signature = generateSignature(data) assertEquals(rawSignatureSize, signature.size) - assertTrue(verifier.verifySignature(data, signature)) + assertTrue(verifier.tryVerifySignature(data, signature)) } } } @@ -76,7 +76,7 @@ abstract class EcdsaTest(provider: CryptographyProvider) : ProviderTest(provider val data = CryptographyRandom.nextBytes(size) val signature = generateSignature(data) assertSignatureSize(signature) - assertTrue(verifier.verifySignature(data, signature)) + assertTrue(verifier.tryVerifySignature(data, signature)) } } } diff --git a/cryptography-providers-tests/src/commonMain/kotlin/default/HmacTest.kt b/cryptography-providers-tests/src/commonMain/kotlin/default/HmacTest.kt index 63e1d439..fa86c296 100644 --- a/cryptography-providers-tests/src/commonMain/kotlin/default/HmacTest.kt +++ b/cryptography-providers-tests/src/commonMain/kotlin/default/HmacTest.kt @@ -59,9 +59,9 @@ abstract class HmacTest(provider: CryptographyProvider) : ProviderTest(provider) @Test fun verifyNoFail() = runTestForEachDigest { val key = algorithm.keyGenerator(digest).generateKey() - assertFalse(key.signatureVerifier().verifySignature(ByteArray(0), ByteArray(0))) - assertFalse(key.signatureVerifier().verifySignature(ByteArray(10), ByteArray(0))) - assertFalse(key.signatureVerifier().verifySignature(ByteArray(10), ByteArray(10))) + assertFalse(key.signatureVerifier().tryVerifySignature(ByteArray(0), ByteArray(0))) + assertFalse(key.signatureVerifier().tryVerifySignature(ByteArray(10), ByteArray(0))) + assertFalse(key.signatureVerifier().tryVerifySignature(ByteArray(10), ByteArray(10))) } @Test @@ -69,7 +69,7 @@ abstract class HmacTest(provider: CryptographyProvider) : ProviderTest(provider) val key = algorithm.keyGenerator(digest).generateKey() val data = CryptographyRandom.nextBytes(100) val signature = key.signatureGenerator().generateSignature(data) - assertTrue(key.signatureVerifier().verifySignature(data, signature)) + assertTrue(key.signatureVerifier().tryVerifySignature(data, signature)) } @Test @@ -79,6 +79,6 @@ abstract class HmacTest(provider: CryptographyProvider) : ProviderTest(provider) val wrongKey = keyGenerator.generateKey() val data = CryptographyRandom.nextBytes(100) val signature = key.signatureGenerator().generateSignature(data) - assertFalse(wrongKey.signatureVerifier().verifySignature(data, signature)) + assertFalse(wrongKey.signatureVerifier().tryVerifySignature(data, signature)) } } diff --git a/cryptography-providers-tests/src/commonMain/kotlin/default/RsaPkcs1Test.kt b/cryptography-providers-tests/src/commonMain/kotlin/default/RsaPkcs1Test.kt index c8f79567..9dc38b69 100644 --- a/cryptography-providers-tests/src/commonMain/kotlin/default/RsaPkcs1Test.kt +++ b/cryptography-providers-tests/src/commonMain/kotlin/default/RsaPkcs1Test.kt @@ -34,7 +34,7 @@ abstract class RsaPkcs1Test(provider: CryptographyProvider) : ProviderTest(provi val data = CryptographyRandom.nextBytes(size) val signature = signatureGenerator.generateSignature(data) assertEquals(keySize.inBytes, signature.size) - assertTrue(signatureVerifier.verifySignature(data, signature)) + assertTrue(signatureVerifier.tryVerifySignature(data, signature)) } } diff --git a/cryptography-providers-tests/src/commonMain/kotlin/default/RsaPssTest.kt b/cryptography-providers-tests/src/commonMain/kotlin/default/RsaPssTest.kt index 4ccf92f2..db746170 100644 --- a/cryptography-providers-tests/src/commonMain/kotlin/default/RsaPssTest.kt +++ b/cryptography-providers-tests/src/commonMain/kotlin/default/RsaPssTest.kt @@ -47,7 +47,7 @@ abstract class RsaPssTest(provider: CryptographyProvider) : ProviderTest(provide val data = CryptographyRandom.nextBytes(size) val signature = signatureGenerator.generateSignature(data) assertEquals(keySize.inBytes, signature.size) - assertTrue(signatureVerifier.verifySignature(data, signature)) + assertTrue(signatureVerifier.tryVerifySignature(data, signature)) } } } diff --git a/cryptography-providers/apple/src/commonMain/kotlin/algorithms/CCHmac.kt b/cryptography-providers/apple/src/commonMain/kotlin/algorithms/CCHmac.kt index f8237523..f6a28de1 100644 --- a/cryptography-providers/apple/src/commonMain/kotlin/algorithms/CCHmac.kt +++ b/cryptography-providers/apple/src/commonMain/kotlin/algorithms/CCHmac.kt @@ -130,8 +130,12 @@ private class HmacFunction( return output } - override fun verify(signature: ByteArray, startIndex: Int, endIndex: Int): Boolean { + override fun tryVerify(signature: ByteArray, startIndex: Int, endIndex: Int): Boolean { checkBounds(signature.size, startIndex, endIndex) return signToByteArray().contentEquals(signature.copyOfRange(startIndex, endIndex)) } + + override fun verify(signature: ByteArray, startIndex: Int, endIndex: Int) { + check(tryVerify(signature, startIndex, endIndex)) { "Invalid signature" } + } } diff --git a/cryptography-providers/apple/src/commonMain/kotlin/algorithms/SecEcdsa.kt b/cryptography-providers/apple/src/commonMain/kotlin/algorithms/SecEcdsa.kt index b2a9f9fd..72b2b058 100644 --- a/cryptography-providers/apple/src/commonMain/kotlin/algorithms/SecEcdsa.kt +++ b/cryptography-providers/apple/src/commonMain/kotlin/algorithms/SecEcdsa.kt @@ -298,7 +298,7 @@ private class EcdsaRawSignatureVerifier( derVerifyFunction.update(source, startIndex, endIndex) } - override fun verify(signature: ByteArray, startIndex: Int, endIndex: Int): Boolean { + override fun tryVerify(signature: ByteArray, startIndex: Int, endIndex: Int): Boolean { checkBounds(signature.size, startIndex, endIndex) check((endIndex - startIndex) == curveOrderSize * 2) { @@ -315,7 +315,11 @@ private class EcdsaRawSignatureVerifier( val derSignature = Der.encodeToByteArray(EcdsaSignatureValue.serializer(), signatureValue) - return derVerifyFunction.verify(derSignature) + return derVerifyFunction.tryVerify(derSignature) + } + + override fun verify(signature: ByteArray, startIndex: Int, endIndex: Int) { + check(tryVerify(signature, startIndex, endIndex)) { "Invalid signature" } } override fun close() { diff --git a/cryptography-providers/apple/src/commonMain/kotlin/internal/SecSignature.kt b/cryptography-providers/apple/src/commonMain/kotlin/internal/SecSignature.kt index cc81cba6..b908188e 100644 --- a/cryptography-providers/apple/src/commonMain/kotlin/internal/SecSignature.kt +++ b/cryptography-providers/apple/src/commonMain/kotlin/internal/SecSignature.kt @@ -43,7 +43,16 @@ private class SecVerifyFunction( accumulator += source.copyOfRange(startIndex, endIndex) } - override fun verify(signature: ByteArray, startIndex: Int, endIndex: Int): Boolean = memScoped { + override fun tryVerify(signature: ByteArray, startIndex: Int, endIndex: Int): Boolean { + return verifyError(signature, startIndex, endIndex) == null + } + + override fun verify(signature: ByteArray, startIndex: Int, endIndex: Int) { + val error = verifyError(signature, startIndex, endIndex) ?: return + error("Invalid signature: $error") + } + + private fun verifyError(signature: ByteArray, startIndex: Int, endIndex: Int): String? = memScoped { ensureNotClosed() checkBounds(signature.size, startIndex, endIndex) val error = alloc() @@ -56,11 +65,10 @@ private class SecVerifyFunction( error = error.ptr, signature = signature.retainBridgeAs() ) - if (!result) { - val nsError = error.value.releaseBridgeAs() - error("Failed to verify signature: ${nsError?.description}") + when { + result -> null + else -> error.value.releaseBridgeAs()?.description ?: "" } - result } } }.also { diff --git a/cryptography-providers/jdk/src/jvmMain/kotlin/algorithms/JdkEcdsa.kt b/cryptography-providers/jdk/src/jvmMain/kotlin/algorithms/JdkEcdsa.kt index 6f9e1353..a663a2d7 100644 --- a/cryptography-providers/jdk/src/jvmMain/kotlin/algorithms/JdkEcdsa.kt +++ b/cryptography-providers/jdk/src/jvmMain/kotlin/algorithms/JdkEcdsa.kt @@ -110,7 +110,7 @@ private class EcdsaRawSignatureVerifier( derVerifyFunction.update(source, startIndex, endIndex) } - override fun verify(signature: ByteArray, startIndex: Int, endIndex: Int): Boolean { + override fun tryVerify(signature: ByteArray, startIndex: Int, endIndex: Int): Boolean { checkBounds(signature.size, startIndex, endIndex) check((endIndex - startIndex) == curveOrderSize * 2) { @@ -127,7 +127,11 @@ private class EcdsaRawSignatureVerifier( val derSignature = Der.encodeToByteArray(EcdsaSignatureValue.serializer(), signatureValue) - return derVerifyFunction.verify(derSignature) + return derVerifyFunction.tryVerify(derSignature) + } + + override fun verify(signature: ByteArray, startIndex: Int, endIndex: Int) { + check(tryVerify(signature, startIndex, endIndex)) { "Invalid signature" } } override fun close() { diff --git a/cryptography-providers/jdk/src/jvmMain/kotlin/operations/JdkMacSignature.kt b/cryptography-providers/jdk/src/jvmMain/kotlin/operations/JdkMacSignature.kt index e1fb395b..0eb2de8b 100644 --- a/cryptography-providers/jdk/src/jvmMain/kotlin/operations/JdkMacSignature.kt +++ b/cryptography-providers/jdk/src/jvmMain/kotlin/operations/JdkMacSignature.kt @@ -46,11 +46,15 @@ private class JdkMacFunction(private val mac: Pooled.Resource) : SignFunct return mac.doFinal().also { close() } } - override fun verify(signature: ByteArray, startIndex: Int, endIndex: Int): Boolean { + override fun tryVerify(signature: ByteArray, startIndex: Int, endIndex: Int): Boolean { checkBounds(signature.size, startIndex, endIndex) return signToByteArray().contentEquals(signature.copyOfRange(startIndex, endIndex)) } + override fun verify(signature: ByteArray, startIndex: Int, endIndex: Int) { + check(tryVerify(signature, startIndex, endIndex)) { "Invalid signature" } + } + override fun close() { mac.close() } diff --git a/cryptography-providers/jdk/src/jvmMain/kotlin/operations/JdkSignatureVerifier.kt b/cryptography-providers/jdk/src/jvmMain/kotlin/operations/JdkSignatureVerifier.kt index 57849393..7e642876 100644 --- a/cryptography-providers/jdk/src/jvmMain/kotlin/operations/JdkSignatureVerifier.kt +++ b/cryptography-providers/jdk/src/jvmMain/kotlin/operations/JdkSignatureVerifier.kt @@ -32,13 +32,17 @@ private class JdkVerifyFunction(private val jsignature: Pooled.Resource