Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ascon128 config #108

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ abstract class RekeyTest: TestHelperNonEphemeral() {
Triple(keyRawWithSalt, keyPassphrase) { encryption { sqlCipher { v4() } } },
Triple(keyRawWithSalt, keyPassphrase) { encryption { chaCha20 { default() } } },
Triple(keyRawWithSalt, keyPassphrase) { encryption { chaCha20 { sqleet() } } },

Triple(keyPassphrase, Key.passphrase("new password")) { encryption { ascon128 { default() } } },
Triple(keyPassphrase, Key.passphrase("new password")) { encryption { rc4 { default() } } },
Triple(keyPassphrase, Key.passphrase("new password")) { encryption { wxAES128 { default() } } },
Triple(keyPassphrase, Key.passphrase("new password")) { encryption { wxAES256 { default() } } },
).forEach { (key1, key2, filesystem) ->
testLogger("RUN - ${i++}")

Expand Down
30 changes: 30 additions & 0 deletions library/driver/api/android/driver.api
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ public final class io/toxicity/sqlite/mc/driver/config/encryption/Cipher$AES256C
public static final field INSTANCE Lio/toxicity/sqlite/mc/driver/config/encryption/Cipher$AES256CBC;
}

public final class io/toxicity/sqlite/mc/driver/config/encryption/Cipher$ASCON128 : io/toxicity/sqlite/mc/driver/config/encryption/Cipher {
public static final field INSTANCE Lio/toxicity/sqlite/mc/driver/config/encryption/Cipher$ASCON128;
}

public final class io/toxicity/sqlite/mc/driver/config/encryption/Cipher$CHACHA20 : io/toxicity/sqlite/mc/driver/config/encryption/Cipher {
public static final field INSTANCE Lio/toxicity/sqlite/mc/driver/config/encryption/Cipher$CHACHA20;
}
Expand Down Expand Up @@ -274,6 +278,10 @@ public final class io/toxicity/sqlite/mc/driver/config/encryption/EncryptionConf
}

public class io/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder {
public final fun ascon128 (Lio/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config;)Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;
public final fun ascon128 (Lio/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config;Lkotlin/jvm/functions/Function1;)Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;
public final fun ascon128 (Lkotlin/jvm/functions/Function1;)Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;
public static synthetic fun ascon128$default (Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;Lio/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;
public final fun chaCha20 (Lio/toxicity/sqlite/mc/driver/config/encryption/MCChaCha20Config;)Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;
public final fun chaCha20 (Lio/toxicity/sqlite/mc/driver/config/encryption/MCChaCha20Config;Lkotlin/jvm/functions/Function1;)Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;
public final fun chaCha20 (Lkotlin/jvm/functions/Function1;)Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;
Expand Down Expand Up @@ -386,6 +394,28 @@ public final class io/toxicity/sqlite/mc/driver/config/encryption/Key$Companion
public static synthetic fun raw$default (Lio/toxicity/sqlite/mc/driver/config/encryption/Key$Companion;[B[BZILjava/lang/Object;)Lio/toxicity/sqlite/mc/driver/config/encryption/Key;
}

public final class io/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config : io/toxicity/sqlite/mc/driver/config/encryption/MCCipherConfig {
public static final field Companion Lio/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config$Companion;
public static final field Default Lio/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config;
public final field kdfIter I
public synthetic fun <init> (IIILkotlin/jvm/internal/DefaultConstructorMarker;)V
}

public final class io/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config$Builder {
public final fun default ()V
public final fun kdfIter ()I
public final fun kdfIter (I)Lio/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config$Builder;
}

public final class io/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config$Companion {
}

public final class io/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config$Options {
public final fun default ()V
public final fun default (Lkotlin/jvm/functions/Function1;)V
public static synthetic fun default$default (Lio/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config$Options;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
}

public final class io/toxicity/sqlite/mc/driver/config/encryption/MCChaCha20Config : io/toxicity/sqlite/mc/driver/config/encryption/MCCipherConfig {
public static final field Companion Lio/toxicity/sqlite/mc/driver/config/encryption/MCChaCha20Config$Companion;
public static final field Default Lio/toxicity/sqlite/mc/driver/config/encryption/MCChaCha20Config;
Expand Down
30 changes: 30 additions & 0 deletions library/driver/api/jvm/driver.api
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ public final class io/toxicity/sqlite/mc/driver/config/encryption/Cipher$AES256C
public static final field INSTANCE Lio/toxicity/sqlite/mc/driver/config/encryption/Cipher$AES256CBC;
}

public final class io/toxicity/sqlite/mc/driver/config/encryption/Cipher$ASCON128 : io/toxicity/sqlite/mc/driver/config/encryption/Cipher {
public static final field INSTANCE Lio/toxicity/sqlite/mc/driver/config/encryption/Cipher$ASCON128;
}

public final class io/toxicity/sqlite/mc/driver/config/encryption/Cipher$CHACHA20 : io/toxicity/sqlite/mc/driver/config/encryption/Cipher {
public static final field INSTANCE Lio/toxicity/sqlite/mc/driver/config/encryption/Cipher$CHACHA20;
}
Expand Down Expand Up @@ -270,6 +274,10 @@ public final class io/toxicity/sqlite/mc/driver/config/encryption/EncryptionConf
}

public class io/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder {
public final fun ascon128 (Lio/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config;)Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;
public final fun ascon128 (Lio/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config;Lkotlin/jvm/functions/Function1;)Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;
public final fun ascon128 (Lkotlin/jvm/functions/Function1;)Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;
public static synthetic fun ascon128$default (Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;Lio/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;
public final fun chaCha20 (Lio/toxicity/sqlite/mc/driver/config/encryption/MCChaCha20Config;)Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;
public final fun chaCha20 (Lio/toxicity/sqlite/mc/driver/config/encryption/MCChaCha20Config;Lkotlin/jvm/functions/Function1;)Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;
public final fun chaCha20 (Lkotlin/jvm/functions/Function1;)Lio/toxicity/sqlite/mc/driver/config/encryption/EncryptionConfig$Builder;
Expand Down Expand Up @@ -382,6 +390,28 @@ public final class io/toxicity/sqlite/mc/driver/config/encryption/Key$Companion
public static synthetic fun raw$default (Lio/toxicity/sqlite/mc/driver/config/encryption/Key$Companion;[B[BZILjava/lang/Object;)Lio/toxicity/sqlite/mc/driver/config/encryption/Key;
}

public final class io/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config : io/toxicity/sqlite/mc/driver/config/encryption/MCCipherConfig {
public static final field Companion Lio/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config$Companion;
public static final field Default Lio/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config;
public final field kdfIter I
public synthetic fun <init> (IIILkotlin/jvm/internal/DefaultConstructorMarker;)V
}

public final class io/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config$Builder {
public final fun default ()V
public final fun kdfIter ()I
public final fun kdfIter (I)Lio/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config$Builder;
}

public final class io/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config$Companion {
}

public final class io/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config$Options {
public final fun default ()V
public final fun default (Lkotlin/jvm/functions/Function1;)V
public static synthetic fun default$default (Lio/toxicity/sqlite/mc/driver/config/encryption/MCAscon128Config$Options;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)V
}

public final class io/toxicity/sqlite/mc/driver/config/encryption/MCChaCha20Config : io/toxicity/sqlite/mc/driver/config/encryption/MCCipherConfig {
public static final field Companion Lio/toxicity/sqlite/mc/driver/config/encryption/MCChaCha20Config$Companion;
public static final field Default Lio/toxicity/sqlite/mc/driver/config/encryption/MCChaCha20Config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public sealed class Cipher private constructor(
public object CHACHA20: Cipher(name = "chacha20", id = 3)
public object SQLCIPHER: Cipher(name = "sqlcipher", id = 4)
public object RC4: Cipher(name = "rc4", id = 5)
public object ASCON128: Cipher(name = "ascon128", id = 6)

final override fun toString(): String = name
final override fun compareTo(other: Cipher): Int = id - other.id
Expand All @@ -51,6 +52,7 @@ public sealed class Cipher private constructor(
name.equals(CHACHA20.name, ignoreCase = true) -> CHACHA20
name.equals(SQLCIPHER.name, ignoreCase = true) -> SQLCIPHER
name.equals(RC4.name, ignoreCase = true) -> RC4
name.equals(ASCON128.name, ignoreCase = true) -> ASCON128
else -> null
}
}
Expand All @@ -68,6 +70,7 @@ public sealed class Cipher private constructor(
CHACHA20.id -> CHACHA20
SQLCIPHER.id -> SQLCIPHER
RC4.id -> RC4
ASCON128.id -> ASCON128
else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,24 @@ public class EncryptionConfig private constructor(
}
}

@MCConfigDsl
public fun ascon128(
block: MCAscon128Config.Options.() -> Unit,
): Builder {
MCAscon128Config.Options { cipherConfig = it }.apply(block)
return this
}

@MCConfigDsl
@JvmOverloads
public fun ascon128(
other: MCAscon128Config,
block: MCAscon128Config.Builder.() -> Unit = {},
): Builder {
MCAscon128Config.Builder(other).apply(block)
return this
}

@MCConfigDsl
public fun chaCha20(
block: MCChaCha20Config.Options.() -> Unit,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
/*
* Copyright (c) 2023 Toxicity
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package io.toxicity.sqlite.mc.driver.config.encryption

import io.toxicity.sqlite.mc.driver.MCCipherConfigDsl
import io.toxicity.sqlite.mc.driver.config.encryption.MCChaCha20Config.Companion.Default
import kotlin.jvm.JvmField
import kotlin.jvm.JvmName
import kotlin.jvm.JvmOverloads
import kotlin.jvm.JvmSynthetic

/**
* Config for Ascon: Ascon-128 v1.2
*
* @see [Builder]
* */
public class MCAscon128Config private constructor(
legacy: Int,
legacyPageSize: Int,
@JvmField
public val kdfIter: Int,
): MCCipherConfig(
cipher = Cipher.ASCON128,
legacy = legacy,
legacyPageSize = legacyPageSize,
) {

public companion object {

/** Default config */
@JvmField
public val Default: MCAscon128Config = Builder(null).apply { default() }.build()
}

// This forces API consumers to declare a setting to start off at before
// customizations occur (if any) so that the [Builder] is configured
// prior to gaining access to it.
//
// This helps mitigate any foot-guns that could arise out of default values
// changing on a dependency update (new crypto or something).
@MCCipherConfigDsl
public class Options internal constructor(
private val setCipher: (MCAscon128Config) -> Unit,
) {

/**
* Initial settings for Default configuration which
* can be further customized (if desired).
* */
@JvmOverloads
@MCCipherConfigDsl
public fun default(block: Builder.() -> Unit = {}) {
val cipher = Builder(Companion.Default).apply(block).build()
setCipher(cipher)
}
}

/**
* [Ascon: Ascon-128 v1.2](https://utelle.github.io/SQLite3MultipleCiphers/docs/ciphers/cipher_ascon/)
*
* @see [default]
* */
@MCCipherConfigDsl
public class Builder internal constructor(other: MCAscon128Config?) {

/**
* [PRAGMA legacy](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-legacy)
*
* Default: 0
* Min: 0
* Max 0
* */
private val legacy: Int = 0

/**
* [PRAGMA legacy_page_size](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-legacy_page_size)
*
* Default: 4096
* Min: 0
* Max: 65536
*
* **NOTE:** must be powers of 2 (e.g. 512, 1024, 4096, 8192, ...)
* */
private val legacyPageSize: Int = 4096


@get:JvmName("kdfIter")
public var kdfIter: Int = 64007
private set

/**
* [PRAGMA kdf_iter](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-kdf_iter)
*
* Default: 64007
* Min: 1
* Max: 2147483647
*
* @throws [IllegalArgumentException] if less than 1
* */
@MCCipherConfigDsl
@Throws(IllegalArgumentException::class)
public fun kdfIter(value: Int): Builder {
value.checkKdfIter()
kdfIter = value
return this
}

init {
if (other != null) {
kdfIter = other.kdfIter
}
}
/**
* Apply default settings
*
* @see [Default]
* */
public fun default() {
kdfIter = 64007
}

@JvmSynthetic
internal fun build(): MCAscon128Config = MCAscon128Config(
legacy = legacy,
legacyPageSize = legacyPageSize,
kdfIter = kdfIter,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public sealed class MCCipherConfig(
is MCSqlCipherConfig -> config.apply(pragmas, addAll)
is MCWxAES128Config -> config.apply(pragmas, addAll)
is MCWxAES256Config -> config.apply(pragmas, addAll)
is MCAscon128Config -> config.apply(pragmas, addAll)
}
}

Expand Down Expand Up @@ -126,6 +127,13 @@ public sealed class MCCipherConfig(
}
}

private fun MCAscon128Config.apply(pragmas: MutableMCPragmas, addAll: Boolean) {
val config = MCAscon128Config.Default
if (addAll || kdfIter != config.kdfIter) {
MCPragma.KDF_ITER.put(pragmas, kdfIter)
}
}

final override fun equals(other: Any?): Boolean {
return when (this) {
is MCChaCha20Config -> {
Expand Down Expand Up @@ -163,6 +171,12 @@ public sealed class MCCipherConfig(
&& other.legacyPageSize == legacyPageSize
&& other.kdfIter == kdfIter
}
is MCAscon128Config -> {
other is MCAscon128Config
&& other.legacy == legacy
&& other.legacyPageSize == legacyPageSize
&& other.kdfIter == kdfIter
}
}
}

Expand Down Expand Up @@ -200,6 +214,11 @@ public sealed class MCCipherConfig(
result = result * 31 + legacyPageSize.hashCode()
result = result * 31 + kdfIter.hashCode()
}
is MCAscon128Config -> {
result = result * 31 + legacy.hashCode()
result = result * 31 + legacyPageSize.hashCode()
result = result * 31 + kdfIter.hashCode()
}
}

return result
Expand Down Expand Up @@ -287,6 +306,17 @@ public sealed class MCCipherConfig(
appendColon()
appendLine(kdfIter)
}
is MCAscon128Config -> {
appendIndent(MCPragma.LEGACY)
appendColon()
appendLine(legacy)
appendIndent(MCPragma.LEGACY_PAGE_SIZE)
appendColon()
appendLine(legacyPageSize)
appendIndent(MCPragma.KDF_ITER)
appendColon()
appendLine(kdfIter)
}
}
append(']')
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public class MCRC4Config private constructor(
}

/**
* [System.Data.SQLite: RC4]https://utelle.github.io/SQLite3MultipleCiphers/docs/ciphers/cipher_sds_rc4/)
* [System.Data.SQLite: RC4](https://utelle.github.io/SQLite3MultipleCiphers/docs/ciphers/cipher_sds_rc4/)
*
* Nobody should use this, but it's here...
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public class MCWxAES256Config private constructor(
/**
* [PRAGMA legacy_page_size](https://utelle.github.io/SQLite3MultipleCiphers/docs/configuration/config_sql_pragmas/#pragma-legacy_page_size)
*
* Default: (default = 4096), (sqleet = 4096)
* Default: 4096
* Min: 0
* Max: 65536
*
Expand Down
Loading
Loading