From 432fd4b2bfb2085ef680fe3a7c1191049e803fd9 Mon Sep 17 00:00:00 2001 From: Ahmed Moussa Date: Wed, 21 Feb 2024 22:17:43 +0200 Subject: [PATCH] fix: Apple crash when creating seed with empty string passphrase Signed-off-by: Ahmed Moussa --- .../prism/apollo/hashing/PBKDF2SHA512.kt | 2 +- .../prism/apollo/derivation/MnemonicTests.kt | 55 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/MnemonicTests.kt diff --git a/apollo/src/appleMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt b/apollo/src/appleMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt index d97c1eebe..bcc04e109 100644 --- a/apollo/src/appleMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt +++ b/apollo/src/appleMain/kotlin/io/iohk/atala/prism/apollo/hashing/PBKDF2SHA512.kt @@ -41,7 +41,7 @@ actual object PBKDF2SHA512 { algorithm = kCCPBKDF2, password = p, passwordLen = p.length.convert(), - salt = saltPin.addressOf(0), + salt = if (saltPin.get().isNotEmpty()) saltPin.addressOf(0) else null, saltLen = saltPin.get().size.convert(), prf = kCCPRFHmacAlgSHA512, rounds = rounds, diff --git a/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/MnemonicTests.kt b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/MnemonicTests.kt new file mode 100644 index 000000000..357933d51 --- /dev/null +++ b/apollo/src/commonTest/kotlin/io/iohk/atala/prism/apollo/derivation/MnemonicTests.kt @@ -0,0 +1,55 @@ +package io.iohk.atala.prism.apollo.derivation + +import kotlin.test.Test +import kotlin.test.fail + +class MnemonicTests { + @Test + fun testCreatingSeedWithEmptyPassphrase() { + val words = listOf( + "bicycle", + "monster", + "swap", + "cave", + "bulk", + "fossil", + "nominee", + "crisp", + "tail", + "parent", + "fossil", + "eyebrow", + "fold", + "manage", + "custom", + "burst", + "flight", + "lawn", + "survey", + "snake", + "brown", + "bridge", + "hard", + "perfect" + ) + val passphrase = "" + + assertDoesNotThrow { + MnemonicHelper.Companion.createSeed(words, passphrase) + } + } +} + +/** + * Asserts that the given executable does not throw an exception. + * + * @param block The executable function to be tested. + * @throws AssertionError if the executable throws any exception. + */ +inline fun assertDoesNotThrow(block: () -> Unit) { + try { + block() + } catch (e: Exception) { + fail("Expected no exception to be thrown, but got ${e.stackTraceToString()}") + } +}