Skip to content

Commit

Permalink
test: throw upon reaching too high nonces
Browse files Browse the repository at this point in the history
  • Loading branch information
sander committed Jan 31, 2024
1 parent f057fb0 commit efa6b48
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/test/kotlin/CipherTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package nl.sanderdijkhuis.noise

import nl.sanderdijkhuis.noise.cryptography.AssociatedData
import nl.sanderdijkhuis.noise.cryptography.CipherKey
import nl.sanderdijkhuis.noise.cryptography.Nonce
import nl.sanderdijkhuis.noise.cryptography.Plaintext
import nl.sanderdijkhuis.noise.data.Data
import org.junit.jupiter.api.assertThrows
import kotlin.test.Test

@OptIn(ExperimentalStdlibApi::class)
class CipherTest {
private val data = AssociatedData(Data.empty)
private val plaintext = Plaintext(Data.empty)

@Test
fun `throws upon reaching nonce maximum while encrypting`() {
val nonceTooHighToToUse = Nonce(ULong.MAX_VALUE - 1uL) // 2^64-2

assertThrows<IllegalStateException> { cipher(nonceTooHighToToUse).encrypt(data, plaintext) }
}

@Test
fun `throws upon reaching nonce maximum while decrypting`() {
val nonceTooHighToEncrypt = Nonce(ULong.MAX_VALUE - 2uL) // 2^64-3
val (cipher, ciphertext) = cipher(nonceTooHighToEncrypt).encrypt(data, plaintext)

assertThrows<IllegalStateException> { cipher.decrypt(data, ciphertext) }
}

private fun cipher(nonce: Nonce) =
Cipher(
JavaCryptography,
CipherKey(Data("76fef1ab184aa7539e3b62a43019ecafc621248b3ac2f5297dd5814e3bd560d3".hexToByteArray())),
nonce
)
}

0 comments on commit efa6b48

Please sign in to comment.