Skip to content

Commit e96b0c2

Browse files
committed
refactor: clean up domain
1 parent aaa5f8d commit e96b0c2

30 files changed

+143
-230
lines changed

src/main/kotlin/AssociatedData.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
package nl.sanderdijkhuis.noise
22

33
@JvmInline
4-
value class AssociatedData(val value: ByteArray) {
5-
6-
init {
7-
require(value.size <= MAX_SIZE)
8-
}
4+
value class AssociatedData(val data: Data) {
95

106
companion object {
117

12-
const val MAX_SIZE = Size.MAX_MESSAGE
13-
14-
fun empty() = AssociatedData(ByteArray(0))
8+
val empty get() = AssociatedData(Data.empty)
159
}
16-
}
10+
}

src/main/kotlin/ChainingKey.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
package nl.sanderdijkhuis.noise
22

33
@JvmInline
4-
value class ChainingKey(val digest: Digest) {
5-
6-
fun messageAuthenticationKey() = MessageAuthenticationKey(digest.value)
7-
}
4+
value class ChainingKey(val digest: Digest)

src/main/kotlin/CipherFunction.kt

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/main/kotlin/CipherKey.kt

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
11
package nl.sanderdijkhuis.noise
22

3+
import nl.sanderdijkhuis.noise.Size.Companion.valueSize
4+
35
@JvmInline
46
value class CipherKey(val value: ByteArray) {
57

68
init {
7-
require(value.size == SIZE)
9+
require(value.valueSize == SIZE)
810
}
911

10-
// fun encrypt(
11-
// nonce: Nonce, associatedData: AssociatedData,
12-
// plaintext: Plaintext
13-
// ) = CipherFunction.Encrypt(this, nonce, associatedData, plaintext)
14-
//
15-
// fun decrypt(
16-
// nonce: Nonce, associatedData: AssociatedData, ciphertext: Ciphertext
17-
// ) = CipherFunction.Decrypt(this, nonce, associatedData, ciphertext)
18-
//
19-
// fun rekey() = CipherFunction.Rekey(this)
20-
2112
companion object {
2213

23-
const val SIZE = 32
14+
val SIZE = Size(32)
2415
}
2516
}

src/main/kotlin/CipherState.kt

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
11
package nl.sanderdijkhuis.noise
22

3-
data class CipherState(val cryptography: Cryptography, val k: CipherKey? = null, val n: Nonce = Nonce.zero()) {
3+
data class CipherState(val cryptography: Cryptography, val key: CipherKey? = null, val nonce: Nonce = Nonce.zero) {
44

55
fun encryptWithAssociatedData(associatedData: AssociatedData, plaintext: Plaintext) =
6-
k?.let {
7-
println("Encrypting $k $n $associatedData $plaintext")
8-
State(copy(n = n.increment()), cryptography.encrypt(it, n, associatedData, plaintext))
6+
key?.let {
7+
println("Encrypting $key $nonce $associatedData $plaintext")
8+
State(copy(nonce = nonce.increment()), cryptography.encrypt(it, nonce, associatedData, plaintext))
99
} ?: let {
10-
println("Returning plaintext $plaintext $n")
10+
println("Returning plaintext $plaintext $nonce")
1111
State(this, plaintext.ciphertext)
1212
}
1313

14-
fun decryptWithAssociatedData(
15-
associatedData: AssociatedData,
16-
ciphertext: Ciphertext
17-
): State<CipherState, Plaintext>? = let {
18-
println("Decrypting $k $n $associatedData $ciphertext")
19-
if (k == null)
14+
fun decryptWithAssociatedData(data: AssociatedData, ciphertext: Ciphertext): State<CipherState, Plaintext>? = let {
15+
println("Decrypting $key $nonce $data $ciphertext")
16+
if (key == null)
2017
State(this, ciphertext.plaintext)
2118
else
22-
cryptography.decrypt(k, n, associatedData, ciphertext)?.let {
23-
State(copy(n = n.increment()), it)
19+
cryptography.decrypt(key, nonce, data, ciphertext)?.let {
20+
State(copy(nonce = nonce.increment()), it)
2421
}
2522
}
26-
27-
// fun rekey() = k?.rekey()
2823
}

src/main/kotlin/Ciphertext.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package nl.sanderdijkhuis.noise
33
@JvmInline
44
value class Ciphertext(val value: ByteArray) {
55

6-
fun data() = Data(value)
6+
val data get() = Data(value)
77

88
val plaintext get() = Plaintext(value)
99
}

src/main/kotlin/Data.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,26 @@
11
package nl.sanderdijkhuis.noise
22

3+
import nl.sanderdijkhuis.noise.Size.Companion.valueSize
34
import kotlin.experimental.xor
45

56
@JvmInline
67
value class Data(val value: ByteArray) {
78

9+
init {
10+
require(value.valueSize <= Size.MAX_MESSAGE)
11+
}
12+
813
operator fun plus(that: Data) = Data(this.value + that.value)
914

15+
val size get() = value.valueSize
16+
1017
fun xor(that: Data) = Data(let {
1118
require(value.size == that.value.size)
1219
ByteArray(value.size) { this.value[it].xor(that.value[it]) }
1320
})
1421

1522
companion object {
1623

17-
fun empty() = Data(ByteArray(0))
24+
val empty get() = Data(ByteArray(0))
1825
}
1926
}

src/main/kotlin/Digest.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package nl.sanderdijkhuis.noise
22

33
@JvmInline
4-
value class Digest(val value: ByteArray) {
4+
value class Digest(val data: Data) {
55

66
init {
7-
require(value.size == HashConfiguration.hashSize.value)
7+
require(data.size == HashFunction.HASH_SIZE)
88
}
99

10-
fun data() = Data(value)
10+
val associatedData get() = AssociatedData(data)
1111

12-
fun associatedData() = AssociatedData(value)
12+
val messageAuthenticationKey get() = MessageAuthenticationKey(data)
1313
}

src/main/kotlin/GeneratedKeyPair.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/main/kotlin/HandshakePattern.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package nl.sanderdijkhuis.noise
22

3-
import nl.sanderdijkhuis.noise.HandshakeState.Token
4-
53
data class HandshakePattern(val name: ProtocolName, val preSharedMessagePatterns: List<List<Token>>, val messagePatterns: List<List<Token>>) {
64

75
companion object {

0 commit comments

Comments
 (0)