Added nonce xoring with mac to pure implementation

This commit is contained in:
Ugljesa Jovanovic 2020-07-10 18:36:45 +02:00 committed by Ugljesa Jovanovic
parent 55b5641f14
commit 1293b9ea75
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
3 changed files with 20 additions and 24 deletions

View File

@ -19,11 +19,9 @@ class EncryptionTest {
@Test
fun testMultipartEncryption() = testBlocking {
Initializer.initialize()
val plaintext = ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
"Vestibulum maximus tincidunt urna. " +
"Nullam sit amet erat id arcu porttitor varius ut at metus. " +
"Nunc sit amet felis vel velit ornare gravida. " +
"Curabitur tellus lacus, pulvinar a diam at tincidunt.").encodeToUByteArray()
val plaintext = ("pUoR4JVXJUeMKNkt6ZGGzEdTo33ajNGXwXpivBKA0XKs8toGRYI9Eul4bELRDkaQDNhd4vZseEFU" +
"ojsAn3c9zIifIrMnydSivHVZ2pBtpAQwYoJhYmEsfE0tROGnOwFWyB9K6LRSv1gB3YqKR9VyM8mpRoUM3UCRRjyiX7bnKdCE1" +
"EiX0myiwcY1nUKTgB3keERWtMU07hX7bCtao5nRvDofSj3o3IInHRQh6opltr5asQwn4m1qn029QF").encodeToUByteArray()
val additionalData = "Additional data 1".encodeToUByteArray()
val keyValue = UByteArray(32) { it.toUByte() }
val key = SymmetricKey(keyValue)
@ -34,16 +32,11 @@ class EncryptionTest {
val ciphertext3 = encryptor.encryptPartialData(plaintext.sliceArray(200 until 250))
//decrypt
val decryptor = Crypto.Encryption.createMultipartDecryptor(key, header)
println("Initialized")
val plaintext1 = decryptor.decryptPartialData(ciphertext1, additionalData)
val plaintext2 = decryptor.decryptPartialData(ciphertext2)
val plaintext3 = decryptor.decryptPartialData(ciphertext3)
val combinedPlaintext = plaintext1.data + plaintext2.data + plaintext3.data
println("---- Plaintext -----")
plaintext.hexColumsPrint()
println("---- Plaintext result -----")
combinedPlaintext.hexColumsPrint()
assertTrue {
plaintext.contentEquals(combinedPlaintext)
}

View File

@ -5,9 +5,7 @@ import com.ionspin.kotlin.crypto.InvalidTagException
import com.ionspin.kotlin.crypto.mac.Poly1305
import com.ionspin.kotlin.crypto.symmetric.ChaCha20Pure
import com.ionspin.kotlin.crypto.symmetric.XChaCha20Pure
import com.ionspin.kotlin.crypto.util.hexColumsPrint
import com.ionspin.kotlin.crypto.util.overwriteWithZeroes
import com.ionspin.kotlin.crypto.util.toLittleEndianUByteArray
import com.ionspin.kotlin.crypto.util.*
/**
* Created by Ugljesa Jovanovic
@ -123,7 +121,10 @@ class XChaCha20Poly1305Pure(val key: UByteArray, val nonce: UByteArray) {
val finalMac = additionalData.size.toULong().toLittleEndianUByteArray() + (ciphertext.size + 64).toULong().toLittleEndianUByteArray()
processPolyBytes(poly1305, finalMac)
val mac = poly1305.finalizeMac(polyBuffer.sliceArray(0 until polyBufferByteCounter))
//TODO process state
calcNonce.xorWithPositionsAndInsertIntoArray(0, 12, mac, 0, calcNonce, 0)
println("Calcnonce---------")
calcNonce.hexColumsPrint()
println("Calcnonce---------")
println("Ciphertext ---------")
(ubyteArrayOf(encryptedTag) + ciphertext + mac).hexColumsPrint()
println("Ciphertext end ---------")
@ -131,6 +132,9 @@ class XChaCha20Poly1305Pure(val key: UByteArray, val nonce: UByteArray) {
}
fun streamDecrypt(data: UByteArray, additionalData: UByteArray, tag: UByte) : UByteArray {
println("Calcnonce start decrypt ---------")
calcNonce.hexColumsPrint()
println("Calcnonce start decrypt end---------")
val block = UByteArray(64) { 0U }
ChaCha20Pure.xorWithKeystream(calcKey, calcNonce, block, 0U).copyInto(block) // This is equivalent to the first 64 bytes of keystream
val poly1305 = Poly1305(block)
@ -166,13 +170,17 @@ class XChaCha20Poly1305Pure(val key: UByteArray, val nonce: UByteArray) {
expectedMac.hexColumsPrint()
println("--- expectedMac end")
//TODO process state
println("Plaintext ---------")
plaintext.hexColumsPrint()
println("Plaintext end ---------")
if (expectedMac.contentEquals(mac).not()){
throw InvalidTagException()
}
calcNonce.xorWithPositionsAndInsertIntoArray(0, 12, mac, 0, calcNonce, 0)
println("Calcnonce end decrypt ---------")
calcNonce.hexColumsPrint()
println("Calcnonce end decrypt end---------")
return plaintext
}

View File

@ -16,11 +16,9 @@ import kotlin.test.assertTrue
class EncryptionTest {
@Test
fun testMultipartEncryption() {
val plaintext = ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
"Vestibulum maximus tincidunt urna. " +
"Nullam sit amet erat id arcu porttitor varius ut at metus. " +
"Nunc sit amet felis vel velit ornare gravida. " +
"Curabitur tellus lacus, pulvinar a diam at tincidunt.").encodeToUByteArray()
val plaintext = ("pUoR4JVXJUeMKNkt6ZGGzEdTo33ajNGXwXpivBKA0XKs8toGRYI9Eul4bELRDkaQDNhd4vZseEFU" +
"ojsAn3c9zIifIrMnydSivHVZ2pBtpAQwYoJhYmEsfE0tROGnOwFWyB9K6LRSv1gB3YqKR9VyM8mpRoUM3UCRRjyiX7bnKdCE1" +
"EiX0myiwcY1nUKTgB3keERWtMU07hX7bCtao5nRvDofSj3o3IInHRQh6opltr5asQwn4m1qn029QF").encodeToUByteArray()
plaintext.hexColumsPrint()
val additionalData = "Additional data 1".encodeToUByteArray()
// val additionalData = ubyteArrayOf()
@ -33,15 +31,12 @@ class EncryptionTest {
val ciphertext3 = encryptor.encryptPartialData(plaintext.sliceArray(200 until 250))
//decrypt
val decryptor = Crypto.Encryption.createMultipartDecryptor(key, header)
println("Initialized")
val plaintext1 = decryptor.decryptPartialData(ciphertext1, additionalData)
val plaintext2 = decryptor.decryptPartialData(ciphertext2)
val plaintext3 = decryptor.decryptPartialData(ciphertext3)
val combinedPlaintext = plaintext1.data + plaintext2.data + plaintext3.data
println("---- Plaintext -----")
plaintext.hexColumsPrint()
println("---- Plaintext result -----")
combinedPlaintext.hexColumsPrint()
assertTrue {
plaintext.contentEquals(combinedPlaintext)