Skip to content

Commit

Permalink
Merge pull request #20 from keemobile/refine/expose-rnd-source
Browse files Browse the repository at this point in the history
Add possibility to provide SecureRandom instance as parameter
  • Loading branch information
Anvell authored May 4, 2024
2 parents 376b7e4 + 309509a commit a3cdc93
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 18 deletions.
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

0 comments on commit a3cdc93

Please sign in to comment.