Skip to content

Commit

Permalink
Android: throw SlothException when decrypting with cached secret
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdapioneer committed Dec 10, 2023
1 parent 661418d commit 3f1dc1b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ class SlothLibTest {
hiddenSloth1.decryptFromStorage("wrong passphrase")
}

// does not decrypt under a different passphrase with pre-computed secrets
assertThatExceptionOfType(SlothDecryptionFailed::class.java).isThrownBy {
val wrongCachedSecrets = hiddenSloth1.computeCachedSecrets("wrong passphrase")
hiddenSloth1.decryptFromStorageWithCachedSecrets(wrongCachedSecrets)
}

// decrypts from another instance
val hiddenSloth3 = instance.getHiddenSlothInstance(
identifier = "hidden_sloth_test",
Expand Down
19 changes: 13 additions & 6 deletions android/sloth/src/main/java/com/lambdapioneer/sloth/HiddenSloth.kt
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,19 @@ class HiddenSloth internal constructor(
check(isInitialized)
val namespacedStorage = storage.getOrCreateNamespace(identifier)

return impl.decrypt(
storage = namespacedStorage,
pw = null,
cachedSecrets = cachedSecrets,
decryptionOffsetAndLength = decryptionOffsetAndLength
)
try {
return impl.decrypt(
storage = namespacedStorage,
pw = null,
cachedSecrets = cachedSecrets,
decryptionOffsetAndLength = decryptionOffsetAndLength
)
} catch (e: AEADBadTagException) {
throw SlothDecryptionFailed(
message = "Decryption failed for key $identifier. This might mean there was never any user data stored.",
cause = e
)
}
}

private fun identifierToHandle(identifier: String) =
Expand Down

0 comments on commit 3f1dc1b

Please sign in to comment.