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 possibility to provide SecureRandom instance as parameter #20

Merged
merged 1 commit into from
May 4, 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 @@ -82,9 +82,12 @@ class EncryptedValue(

fun fromBase64(base64: String) = fromBinary(base64.decodeBase64ToArray())

fun fromBinary(bytes: ByteArray): EncryptedValue {
fun fromBinary(
bytes: ByteArray,
random: SecureRandom = SecureRandom()
): EncryptedValue {
val salt = ByteArray(bytes.size)
SecureRandom().nextBytes(salt)
random.nextBytes(salt)

for (i in bytes.indices) {
bytes[i] = bytes[i] xor salt[i]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import app.keemobile.kotpass.models.DatabaseElement
import app.keemobile.kotpass.models.Entry
import app.keemobile.kotpass.models.Group
import app.keemobile.kotpass.models.Meta
import java.security.SecureRandom
import java.util.UUID

/**
Expand All @@ -32,14 +33,16 @@ sealed class KeePassDatabase {
* @param rootName Required name of the top group.
* @param meta Database metadata.
* @param credentials Database credentials.
* @param random optional custom random generator.
*/
fun create(
rootName: String,
meta: Meta,
credentials: Credentials
credentials: Credentials,
random: SecureRandom = SecureRandom()
) = Ver3x(
credentials = credentials,
header = DatabaseHeader.Ver3x.create(),
header = DatabaseHeader.Ver3x.create(random),
content = DatabaseContent(
meta = meta,
group = Group(
Expand Down Expand Up @@ -67,14 +70,16 @@ sealed class KeePassDatabase {
* @param rootName Required name of the top group.
* @param meta Database metadata.
* @param credentials Database credentials.
* @param random optional custom random generator.
*/
fun create(
rootName: String,
meta: Meta,
credentials: Credentials
credentials: Credentials,
random: SecureRandom = SecureRandom()
) = Ver4x(
credentials = credentials,
header = DatabaseHeader.Ver4x.create(),
header = DatabaseHeader.Ver4x.create(random),
content = DatabaseContent(
meta = meta,
group = Group(
Expand All @@ -85,7 +90,7 @@ sealed class KeePassDatabase {
),
deletedObjects = listOf()
),
innerHeader = DatabaseInnerHeader.create()
innerHeader = DatabaseInnerHeader.create(random)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ sealed class DatabaseHeader {
val streamStartBytes: ByteString
) : DatabaseHeader() {
companion object {
fun create() = with(SecureRandom()) {
/**
* Create an instance of [DatabaseHeader] with the default parameters.
*/
fun create(random: SecureRandom = SecureRandom()) = with(random) {
Ver3x(
signature = Signature.Default,
version = FormatVersion(3, 1),
Expand Down Expand Up @@ -70,7 +73,10 @@ sealed class DatabaseHeader {
val publicCustomData: Map<String, VariantItem>
) : DatabaseHeader() {
companion object {
fun create() = with(SecureRandom()) {
/**
* Create an instance of [DatabaseHeader] with the default parameters.
*/
fun create(random: SecureRandom = SecureRandom()) = with(random) {
Ver4x(
signature = Signature.Default,
version = FormatVersion(4, 1),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,11 @@ data class DatabaseInnerHeader(
}

companion object {
fun create() = with(SecureRandom()) {
DatabaseInnerHeader(
randomStreamId = CrsAlgorithm.ChaCha20,
randomStreamKey = nextByteString(64),
binaries = linkedMapOf()
)
}
fun create(random: SecureRandom = SecureRandom()) = DatabaseInnerHeader(
randomStreamId = CrsAlgorithm.ChaCha20,
randomStreamKey = random.nextByteString(64),
binaries = linkedMapOf()
)

internal fun readFrom(source: BufferedSource): DatabaseInnerHeader {
val binaries = linkedMapOf<ByteString, BinaryData>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package app.keemobile.kotpass.extensions

import okio.ByteString
import okio.ByteString.Companion.toByteString
import java.security.SecureRandom
import java.util.Random

internal fun SecureRandom.nextByteString(length: Int): ByteString {
internal fun Random.nextByteString(length: Int): ByteString {
return ByteArray(length)
.apply { nextBytes(this) }
.toByteString()
Expand Down
Loading