-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: merge nonce and decryption fixes (#47)
- Loading branch information
Showing
5 changed files
with
70 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
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 | ||
import kotlin.test.assertNull | ||
|
||
@OptIn(ExperimentalStdlibApi::class) | ||
class CipherTest { | ||
private val data = AssociatedData(Data.empty) | ||
private val otherData = AssociatedData(Data("other".toByteArray())) | ||
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) } | ||
} | ||
|
||
@Test | ||
fun `signals an error to the caller upon authentication failure during decryption`() { | ||
val (cipher, ciphertext) = cipher(Nonce.zero).encrypt(data, plaintext) | ||
|
||
assertNull(cipher.decrypt(otherData, ciphertext)) | ||
} | ||
|
||
private fun cipher(nonce: Nonce) = | ||
Cipher( | ||
JavaCryptography, | ||
CipherKey(Data("76fef1ab184aa7539e3b62a43019ecafc621248b3ac2f5297dd5814e3bd560d3".hexToByteArray())), | ||
nonce | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters