Version1MasterKeyCrypto.kt 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* _____ _
  2. * |_ _| |_ _ _ ___ ___ _ __ __ _
  3. * | | | ' \| '_/ -_) -_) ' \/ _` |_
  4. * |_| |_||_|_| \___\___|_|_|_\__,_(_)
  5. *
  6. * Threema for Android
  7. * Copyright (c) 2025 Threema GmbH
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. package ch.threema.localcrypto
  22. import ch.threema.common.models.CryptographicByteArray
  23. import ch.threema.common.secureContentEquals
  24. import ch.threema.common.xor
  25. import ch.threema.domain.libthreema.LibthreemaJavaBridge.createScryptParameters
  26. import ch.threema.libthreema.scrypt
  27. import ch.threema.localcrypto.models.MasterKeyData
  28. import ch.threema.localcrypto.models.MasterKeyState
  29. import java.security.MessageDigest
  30. object Version1MasterKeyCrypto {
  31. fun checkVerification(masterKeyData: MasterKeyData, verification: CryptographicByteArray): Boolean =
  32. calculateVerification(masterKeyData).secureContentEquals(verification.value)
  33. private fun calculateVerification(masterKeyData: MasterKeyData): ByteArray {
  34. val messageDigest = MessageDigest.getInstance("SHA-1")
  35. messageDigest.update(masterKeyData.value)
  36. return messageDigest.digest().copyOfRange(0, MasterKeyConfig.VERSION1_VERIFICATION_LENGTH)
  37. }
  38. fun decryptPassphraseProtectedMasterKey(
  39. protection: MasterKeyState.WithPassphrase.PassphraseProtection.Version1,
  40. passphrase: CharArray,
  41. ): MasterKeyData {
  42. val passphraseKey = deriveVersion1PassphraseKey(passphrase, protection.salt)
  43. return MasterKeyData(protection.protectedKey.value xor passphraseKey)
  44. }
  45. private fun deriveVersion1PassphraseKey(
  46. passphrase: CharArray,
  47. salt: CryptographicByteArray,
  48. ): ByteArray =
  49. scrypt(
  50. password = String(passphrase).toByteArray(),
  51. salt = salt.value,
  52. parameters = createScryptParameters(outputLength = MasterKeyConfig.KEY_LENGTH.toByte()),
  53. )
  54. }