Added nonce xoring with mac to pure implementation
This commit is contained in:
		
							parent
							
								
									55b5641f14
								
							
						
					
					
						commit
						1293b9ea75
					
				@ -19,11 +19,9 @@ class EncryptionTest {
 | 
				
			|||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    fun testMultipartEncryption() = testBlocking {
 | 
					    fun testMultipartEncryption() = testBlocking {
 | 
				
			||||||
        Initializer.initialize()
 | 
					        Initializer.initialize()
 | 
				
			||||||
        val plaintext = ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
 | 
					        val plaintext = ("pUoR4JVXJUeMKNkt6ZGGzEdTo33ajNGXwXpivBKA0XKs8toGRYI9Eul4bELRDkaQDNhd4vZseEFU" +
 | 
				
			||||||
                "Vestibulum maximus tincidunt urna. " +
 | 
					                "ojsAn3c9zIifIrMnydSivHVZ2pBtpAQwYoJhYmEsfE0tROGnOwFWyB9K6LRSv1gB3YqKR9VyM8mpRoUM3UCRRjyiX7bnKdCE1" +
 | 
				
			||||||
                "Nullam sit amet erat id arcu porttitor varius ut at metus. " +
 | 
					                "EiX0myiwcY1nUKTgB3keERWtMU07hX7bCtao5nRvDofSj3o3IInHRQh6opltr5asQwn4m1qn029QF").encodeToUByteArray()
 | 
				
			||||||
                "Nunc sit amet felis vel velit ornare gravida. " +
 | 
					 | 
				
			||||||
                "Curabitur tellus lacus, pulvinar a diam at tincidunt.").encodeToUByteArray()
 | 
					 | 
				
			||||||
        val additionalData = "Additional data 1".encodeToUByteArray()
 | 
					        val additionalData = "Additional data 1".encodeToUByteArray()
 | 
				
			||||||
        val keyValue = UByteArray(32) { it.toUByte() }
 | 
					        val keyValue = UByteArray(32) { it.toUByte() }
 | 
				
			||||||
        val key = SymmetricKey(keyValue)
 | 
					        val key = SymmetricKey(keyValue)
 | 
				
			||||||
@ -34,16 +32,11 @@ class EncryptionTest {
 | 
				
			|||||||
        val ciphertext3 = encryptor.encryptPartialData(plaintext.sliceArray(200 until 250))
 | 
					        val ciphertext3 = encryptor.encryptPartialData(plaintext.sliceArray(200 until 250))
 | 
				
			||||||
        //decrypt
 | 
					        //decrypt
 | 
				
			||||||
        val decryptor = Crypto.Encryption.createMultipartDecryptor(key, header)
 | 
					        val decryptor = Crypto.Encryption.createMultipartDecryptor(key, header)
 | 
				
			||||||
        println("Initialized")
 | 
					 | 
				
			||||||
        val plaintext1 = decryptor.decryptPartialData(ciphertext1, additionalData)
 | 
					        val plaintext1 = decryptor.decryptPartialData(ciphertext1, additionalData)
 | 
				
			||||||
        val plaintext2 = decryptor.decryptPartialData(ciphertext2)
 | 
					        val plaintext2 = decryptor.decryptPartialData(ciphertext2)
 | 
				
			||||||
        val plaintext3 = decryptor.decryptPartialData(ciphertext3)
 | 
					        val plaintext3 = decryptor.decryptPartialData(ciphertext3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        val combinedPlaintext = plaintext1.data + plaintext2.data + plaintext3.data
 | 
					        val combinedPlaintext = plaintext1.data + plaintext2.data + plaintext3.data
 | 
				
			||||||
        println("---- Plaintext -----")
 | 
					 | 
				
			||||||
        plaintext.hexColumsPrint()
 | 
					 | 
				
			||||||
        println("---- Plaintext result -----")
 | 
					 | 
				
			||||||
        combinedPlaintext.hexColumsPrint()
 | 
					 | 
				
			||||||
        assertTrue {
 | 
					        assertTrue {
 | 
				
			||||||
            plaintext.contentEquals(combinedPlaintext)
 | 
					            plaintext.contentEquals(combinedPlaintext)
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
				
			|||||||
@ -5,9 +5,7 @@ import com.ionspin.kotlin.crypto.InvalidTagException
 | 
				
			|||||||
import com.ionspin.kotlin.crypto.mac.Poly1305
 | 
					import com.ionspin.kotlin.crypto.mac.Poly1305
 | 
				
			||||||
import com.ionspin.kotlin.crypto.symmetric.ChaCha20Pure
 | 
					import com.ionspin.kotlin.crypto.symmetric.ChaCha20Pure
 | 
				
			||||||
import com.ionspin.kotlin.crypto.symmetric.XChaCha20Pure
 | 
					import com.ionspin.kotlin.crypto.symmetric.XChaCha20Pure
 | 
				
			||||||
import com.ionspin.kotlin.crypto.util.hexColumsPrint
 | 
					import com.ionspin.kotlin.crypto.util.*
 | 
				
			||||||
import com.ionspin.kotlin.crypto.util.overwriteWithZeroes
 | 
					 | 
				
			||||||
import com.ionspin.kotlin.crypto.util.toLittleEndianUByteArray
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * Created by Ugljesa Jovanovic
 | 
					 * 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()
 | 
					        val finalMac = additionalData.size.toULong().toLittleEndianUByteArray() + (ciphertext.size + 64).toULong().toLittleEndianUByteArray()
 | 
				
			||||||
        processPolyBytes(poly1305, finalMac)
 | 
					        processPolyBytes(poly1305, finalMac)
 | 
				
			||||||
        val mac = poly1305.finalizeMac(polyBuffer.sliceArray(0 until polyBufferByteCounter))
 | 
					        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 ---------")
 | 
					        println("Ciphertext ---------")
 | 
				
			||||||
        (ubyteArrayOf(encryptedTag) + ciphertext + mac).hexColumsPrint()
 | 
					        (ubyteArrayOf(encryptedTag) + ciphertext + mac).hexColumsPrint()
 | 
				
			||||||
        println("Ciphertext end ---------")
 | 
					        println("Ciphertext end ---------")
 | 
				
			||||||
@ -131,6 +132,9 @@ class XChaCha20Poly1305Pure(val key: UByteArray, val nonce: UByteArray) {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fun streamDecrypt(data: UByteArray, additionalData: UByteArray, tag: UByte) : 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 }
 | 
					        val block = UByteArray(64) { 0U }
 | 
				
			||||||
        ChaCha20Pure.xorWithKeystream(calcKey, calcNonce, block, 0U).copyInto(block) // This is equivalent to the first 64 bytes of keystream
 | 
					        ChaCha20Pure.xorWithKeystream(calcKey, calcNonce, block, 0U).copyInto(block) // This is equivalent to the first 64 bytes of keystream
 | 
				
			||||||
        val poly1305 = Poly1305(block)
 | 
					        val poly1305 = Poly1305(block)
 | 
				
			||||||
@ -166,13 +170,17 @@ class XChaCha20Poly1305Pure(val key: UByteArray, val nonce: UByteArray) {
 | 
				
			|||||||
        expectedMac.hexColumsPrint()
 | 
					        expectedMac.hexColumsPrint()
 | 
				
			||||||
        println("--- expectedMac end")
 | 
					        println("--- expectedMac end")
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        //TODO process state
 | 
					
 | 
				
			||||||
        println("Plaintext ---------")
 | 
					        println("Plaintext ---------")
 | 
				
			||||||
        plaintext.hexColumsPrint()
 | 
					        plaintext.hexColumsPrint()
 | 
				
			||||||
        println("Plaintext end ---------")
 | 
					        println("Plaintext end ---------")
 | 
				
			||||||
        if (expectedMac.contentEquals(mac).not()){
 | 
					        if (expectedMac.contentEquals(mac).not()){
 | 
				
			||||||
            throw InvalidTagException()
 | 
					            throw InvalidTagException()
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					        calcNonce.xorWithPositionsAndInsertIntoArray(0, 12, mac, 0, calcNonce, 0)
 | 
				
			||||||
 | 
					        println("Calcnonce end decrypt ---------")
 | 
				
			||||||
 | 
					        calcNonce.hexColumsPrint()
 | 
				
			||||||
 | 
					        println("Calcnonce end decrypt end---------")
 | 
				
			||||||
        return plaintext
 | 
					        return plaintext
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -16,11 +16,9 @@ import kotlin.test.assertTrue
 | 
				
			|||||||
class EncryptionTest {
 | 
					class EncryptionTest {
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    fun testMultipartEncryption() {
 | 
					    fun testMultipartEncryption() {
 | 
				
			||||||
        val plaintext = ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. " +
 | 
					        val plaintext = ("pUoR4JVXJUeMKNkt6ZGGzEdTo33ajNGXwXpivBKA0XKs8toGRYI9Eul4bELRDkaQDNhd4vZseEFU" +
 | 
				
			||||||
                "Vestibulum maximus tincidunt urna. " +
 | 
					                "ojsAn3c9zIifIrMnydSivHVZ2pBtpAQwYoJhYmEsfE0tROGnOwFWyB9K6LRSv1gB3YqKR9VyM8mpRoUM3UCRRjyiX7bnKdCE1" +
 | 
				
			||||||
                "Nullam sit amet erat id arcu porttitor varius ut at metus. " +
 | 
					                "EiX0myiwcY1nUKTgB3keERWtMU07hX7bCtao5nRvDofSj3o3IInHRQh6opltr5asQwn4m1qn029QF").encodeToUByteArray()
 | 
				
			||||||
                "Nunc sit amet felis vel velit ornare gravida. " +
 | 
					 | 
				
			||||||
                "Curabitur tellus lacus, pulvinar a diam at tincidunt.").encodeToUByteArray()
 | 
					 | 
				
			||||||
        plaintext.hexColumsPrint()
 | 
					        plaintext.hexColumsPrint()
 | 
				
			||||||
        val additionalData = "Additional data 1".encodeToUByteArray()
 | 
					        val additionalData = "Additional data 1".encodeToUByteArray()
 | 
				
			||||||
//        val additionalData = ubyteArrayOf()
 | 
					//        val additionalData = ubyteArrayOf()
 | 
				
			||||||
@ -33,15 +31,12 @@ class EncryptionTest {
 | 
				
			|||||||
        val ciphertext3 = encryptor.encryptPartialData(plaintext.sliceArray(200 until 250))
 | 
					        val ciphertext3 = encryptor.encryptPartialData(plaintext.sliceArray(200 until 250))
 | 
				
			||||||
        //decrypt
 | 
					        //decrypt
 | 
				
			||||||
        val decryptor = Crypto.Encryption.createMultipartDecryptor(key, header)
 | 
					        val decryptor = Crypto.Encryption.createMultipartDecryptor(key, header)
 | 
				
			||||||
        println("Initialized")
 | 
					 | 
				
			||||||
        val plaintext1 = decryptor.decryptPartialData(ciphertext1, additionalData)
 | 
					        val plaintext1 = decryptor.decryptPartialData(ciphertext1, additionalData)
 | 
				
			||||||
        val plaintext2 = decryptor.decryptPartialData(ciphertext2)
 | 
					        val plaintext2 = decryptor.decryptPartialData(ciphertext2)
 | 
				
			||||||
        val plaintext3 = decryptor.decryptPartialData(ciphertext3)
 | 
					        val plaintext3 = decryptor.decryptPartialData(ciphertext3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        val combinedPlaintext = plaintext1.data + plaintext2.data + plaintext3.data
 | 
					        val combinedPlaintext = plaintext1.data + plaintext2.data + plaintext3.data
 | 
				
			||||||
        println("---- Plaintext -----")
 | 
					
 | 
				
			||||||
        plaintext.hexColumsPrint()
 | 
					 | 
				
			||||||
        println("---- Plaintext result -----")
 | 
					 | 
				
			||||||
        combinedPlaintext.hexColumsPrint()
 | 
					        combinedPlaintext.hexColumsPrint()
 | 
				
			||||||
        assertTrue {
 | 
					        assertTrue {
 | 
				
			||||||
            plaintext.contentEquals(combinedPlaintext)
 | 
					            plaintext.contentEquals(combinedPlaintext)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user