Skip to content

Commit

Permalink
Upadate libs
Browse files Browse the repository at this point in the history
  • Loading branch information
marukami committed Mar 9, 2021
1 parent 77b1f82 commit 684caea
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 110 deletions.
10 changes: 5 additions & 5 deletions armadillo-datastore/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,18 @@ android {
dependencies {
implementation(project(":armadillo"))

implementation("org.jetbrains.kotlin:kotlin-stdlib:1.4.10")
implementation("org.jetbrains.kotlin:kotlin-stdlib:1.4.30")

implementation("androidx.core:core-ktx:1.3.2")
implementation("androidx.appcompat:appcompat:1.2.0")
implementation("androidx.datastore:datastore-core:1.0.0-alpha01")
implementation("androidx.datastore:datastore-core:1.0.0-alpha07")

androidTestImplementation("org.jetbrains.kotlinx:kotlinx-serialization-protobuf:1.0.0")
implementation("org.jetbrains.kotlinx:kotlinx-serialization-protobuf:1.0.0")

testImplementation("junit:junit:4.13")
testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.2")
androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
androidTestImplementation("org.bouncycastle:bcprov-jdk15on:1.60")
androidTestImplementation("org.bouncycastle:bcprov-jdk15on:1.67")
androidTestImplementation("org.mindrot:jbcrypt:0.4")
androidTestImplementation("androidx.test.ext:junit:1.1.2")
androidTestImplementation("androidx.test:rules:1.3.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package at.favre.lib.armadillo.datastore

import android.content.Context
import androidx.datastore.DataStore
import androidx.datastore.createDataStore
import androidx.datastore.core.DataStore
import androidx.datastore.core.createDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.protobuf.ProtoBuf
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package at.favre.lib.armadillo.datastore

import android.content.Context
import android.os.Build
import androidx.datastore.Serializer
import androidx.datastore.core.Serializer
import at.favre.lib.armadillo.*
import at.favre.lib.armadillo.Armadillo.CONTENT_KEY_OUT_BYTE_LENGTH
import at.favre.lib.armadillo.BuildConfig
Expand All @@ -12,116 +12,119 @@ import java.security.Provider
import java.security.SecureRandom

class ArmadilloSerializer<T>(
context: Context,
private val protocol: ProtobufProtocol<T>,
password: CharArray? = null,
fingerprintData: List<String> = emptyList(),
secureRandom: SecureRandom = SecureRandom(),
additionalDecryptionConfigs: List<EncryptionProtocolConfig> = listOf(),
enabledKitkatSupport: Boolean = false,
provider: Provider? = null,
preferencesSalt: ByteArray = BuildConfig.PREF_SALT
context: Context,
private val protocol: ProtobufProtocol<T>,
password: CharArray? = null,
fingerprintData: List<String> = emptyList(),
secureRandom: SecureRandom = SecureRandom(),
additionalDecryptionConfigs: List<EncryptionProtocolConfig> = listOf(),
enabledKitkatSupport: Boolean = false,
provider: Provider? = null,
preferencesSalt: ByteArray = BuildConfig.PREF_SALT
) : Serializer<T> {

private val serializerPassword: ByteArrayRuntimeObfuscator?
private val encryptionProtocol: EncryptionProtocol
private val fingerprint: EncryptionFingerprint = EncryptionFingerprintFactory.create(
context,
buildString { fingerprintData.forEach(::append) }
)
private val defaultConfig = EncryptionProtocolConfig.newDefaultConfig()
private val kitKatConfig by lazy {
@Suppress("DEPRECATION")
EncryptionProtocolConfig.newBuilder(defaultConfig.build())
.authenticatedEncryption(AesCbcEncryption(secureRandom, provider))
.protocolVersion(Armadillo.KITKAT_PROTOCOL_VERSION)
.build()
}

init {

val stringMessageDigest = HkdfMessageDigest(
BuildConfig.PREF_SALT,
CONTENT_KEY_OUT_BYTE_LENGTH
private val serializerPassword: ByteArrayRuntimeObfuscator?
private val encryptionProtocol: EncryptionProtocol
private val fingerprint: EncryptionFingerprint = EncryptionFingerprintFactory.create(
context,
buildString { fingerprintData.forEach(::append) }
)
private val defaultConfig = EncryptionProtocolConfig.newDefaultConfig()
private val kitKatConfig by lazy {
@Suppress("DEPRECATION")
EncryptionProtocolConfig.newBuilder(defaultConfig.build())
.authenticatedEncryption(AesCbcEncryption(secureRandom, provider))
.protocolVersion(Armadillo.KITKAT_PROTOCOL_VERSION)
.build()
}

init {

val stringMessageDigest = HkdfMessageDigest(
BuildConfig.PREF_SALT,
CONTENT_KEY_OUT_BYTE_LENGTH
)

val config =
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
kitKatConfig
} else {
EncryptionProtocolConfig
.newBuilder(defaultConfig.build())
.authenticatedEncryption(AesGcmEncryption(secureRandom, provider))
.build()
}
checkKitKatSupport(config.authenticatedEncryption)

val factory = DefaultEncryptionProtocol.Factory(
config,
fingerprint,
stringMessageDigest,
secureRandom,
false, // enableDerivedPasswordCache,
if (enabledKitkatSupport) {
additionalDecryptionConfigs + kitKatConfig
} else {
additionalDecryptionConfigs
},
)

encryptionProtocol = factory.create(preferencesSalt)
serializerPassword = password?.let(factory::obfuscatePassword)
}

val config =
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
kitKatConfig
} else {
EncryptionProtocolConfig
.newBuilder(defaultConfig.build())
.authenticatedEncryption(AesGcmEncryption(secureRandom, provider))
.build()

private fun checkKitKatSupport(authenticatedEncryption: AuthenticatedEncryption) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT && authenticatedEncryption.javaClass == AesGcmEncryption::class.java) {
throw UnsupportedOperationException("aes gcm is not supported with KitKat, add support " +
"manually with Armadillo.Builder.enableKitKatSupport()")
}
checkKitKatSupport(config.authenticatedEncryption)

val factory = DefaultEncryptionProtocol.Factory(
config,
fingerprint,
stringMessageDigest,
secureRandom,
false, // enableDerivedPasswordCache,
if (enabledKitkatSupport) {
additionalDecryptionConfigs + kitKatConfig
} else {
additionalDecryptionConfigs
},
)
}

encryptionProtocol = factory.create(preferencesSalt)
serializerPassword = password?.let(factory::obfuscatePassword)
}
companion object {
private const val CRYPTO_KEY = "ArmadilloStoreSerializer"
}


private fun checkKitKatSupport(authenticatedEncryption: AuthenticatedEncryption) {
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT && authenticatedEncryption.javaClass == AesGcmEncryption::class.java) {
throw UnsupportedOperationException("aes gcm is not supported with KitKat, add support " +
"manually with Armadillo.Builder.enableKitKatSupport()")
private fun encrypt(content: ByteArray): ByteArray = with(encryptionProtocol) {
encrypt(
deriveContentKey(CRYPTO_KEY),
deobfuscatePassword(serializerPassword),
content
)
}
}

companion object {
private const val CRYPTO_KEY = "ArmadilloStoreSerializer"
}

private fun decrypt(encrypted: ByteArray): ByteArray? =
if (encrypted.isEmpty()) {
null
} else {
encryptionProtocol
.decrypt(
encryptionProtocol.deriveContentKey(CRYPTO_KEY),
encryptionProtocol.deobfuscatePassword(serializerPassword),
encrypted
)
}

override fun readFrom(input: InputStream): T =
input
.readBytes()
.let(::decrypt)
.let {
val bytes = it ?: byteArrayOf()
if (bytes.isEmpty()) defaultValue
else protocol.decode(bytes)
}


override fun writeTo(t: T, output: OutputStream) {
protocol
.encode(t)
.let(::encrypt)
.also(output::write)
}

private fun encrypt(content: ByteArray): ByteArray = with(encryptionProtocol) {
encrypt(
deriveContentKey(CRYPTO_KEY),
deobfuscatePassword(serializerPassword),
content
)
}


private fun decrypt(encrypted: ByteArray): ByteArray? =
if (encrypted.isEmpty()) {
null
} else {
encryptionProtocol
.decrypt(
encryptionProtocol.deriveContentKey(CRYPTO_KEY),
encryptionProtocol.deobfuscatePassword(serializerPassword),
encrypted
)
}

override fun readFrom(input: InputStream): T =
input
.readBytes()
.let(::decrypt)
.let {
val bytes = it ?: byteArrayOf()
if (bytes.isEmpty()) protocol.default()
else protocol.decode(bytes)
}


override fun writeTo(t: T, output: OutputStream) {
protocol
.encode(t)
.let(::encrypt)
.also(output::write)
}
override val defaultValue: T
get() = protocol.default()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ package at.favre.lib.armadillo.datastore
* Datastore will always return an empty
*/
interface ProtobufProtocol<T> {
// /**
// * If the file contents has change you must migrate the data
// */
// val version: Int
/**
* un-encrypted proto byte encoding of [T]
*/
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:4.1.0'
classpath 'com.android.tools.build:gradle:4.1.2'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5'
classpath 'org.kt3k.gradle.plugin:coveralls-gradle-plugin:2.8.3'
classpath 'com.vanniktech:gradle-android-junit-jacoco-plugin:0.16.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.10"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.30"
}
}

Expand Down

0 comments on commit 684caea

Please sign in to comment.