Skip to content

Commit 2f13fbf

Browse files
committed
test: throw upon reaching too high nonces
Thanks to @breynders-cb and @tvandriessel-cb for spotting the risk.
1 parent f057fb0 commit 2f13fbf

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

src/test/kotlin/CipherTest.kt

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package nl.sanderdijkhuis.noise
2+
3+
import nl.sanderdijkhuis.noise.cryptography.AssociatedData
4+
import nl.sanderdijkhuis.noise.cryptography.CipherKey
5+
import nl.sanderdijkhuis.noise.cryptography.Nonce
6+
import nl.sanderdijkhuis.noise.cryptography.Plaintext
7+
import nl.sanderdijkhuis.noise.data.Data
8+
import org.junit.jupiter.api.assertThrows
9+
import kotlin.test.Test
10+
11+
@OptIn(ExperimentalStdlibApi::class)
12+
class CipherTest {
13+
private val data = AssociatedData(Data.empty)
14+
private val plaintext = Plaintext(Data.empty)
15+
16+
@Test
17+
fun `throws upon reaching nonce maximum while encrypting`() {
18+
val nonceTooHighToToUse = Nonce(ULong.MAX_VALUE - 1uL) // 2^64-2
19+
20+
assertThrows<IllegalStateException> { cipher(nonceTooHighToToUse).encrypt(data, plaintext) }
21+
}
22+
23+
@Test
24+
fun `throws upon reaching nonce maximum while decrypting`() {
25+
val nonceTooHighToEncrypt = Nonce(ULong.MAX_VALUE - 2uL) // 2^64-3
26+
val (cipher, ciphertext) = cipher(nonceTooHighToEncrypt).encrypt(data, plaintext)
27+
28+
assertThrows<IllegalStateException> { cipher.decrypt(data, ciphertext) }
29+
}
30+
31+
private fun cipher(nonce: Nonce) =
32+
Cipher(
33+
JavaCryptography,
34+
CipherKey(Data("76fef1ab184aa7539e3b62a43019ecafc621248b3ac2f5297dd5814e3bd560d3".hexToByteArray())),
35+
nonce
36+
)
37+
}

0 commit comments

Comments
 (0)