Starting delegated implementation

This commit is contained in:
Ugljesa Jovanovic 2020-06-23 22:19:19 +02:00 committed by Ugljesa Jovanovic
parent 1848de0e8d
commit e3fe276e4c
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
6 changed files with 89 additions and 11 deletions

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.authenticated.XChaCha20Poly1305Delegated
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bProperties
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegated
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless
@ -152,12 +153,12 @@ object Crypto {
throw RuntimeException("Invalid key size! Required 32, supplied ${key.value.size}")
}
val nonce = SRNG.getRandomBytes(24)
return EncryptedData(XChaCha20Poly1305Pure.encrypt(key.value, nonce, data.toEncryptableForm(), additionalData), nonce)
return EncryptedData(XChaCha20Poly1305Delegated.encrypt(key.value, nonce, data.toEncryptableForm(), additionalData), nonce)
}
override fun <T: Encryptable<T>> decrypt(key: SymmetricKey, encryptedData : EncryptedData, additionalData: UByteArray, byteArrayDeserializer : (UByteArray) -> T) : T {
return byteArrayDeserializer(XChaCha20Poly1305Pure.decrypt(key.value, encryptedData.nonce, encryptedData.ciphertext, additionalData))
return byteArrayDeserializer(XChaCha20Poly1305Delegated.decrypt(key.value, encryptedData.nonce, encryptedData.ciphertext, additionalData))
}
@ -172,7 +173,7 @@ object Crypto {
}
class MultipartAuthenticatedEncryptor internal constructor(val key : SymmetricKey, additionalData: UByteArray) : MultipartAuthenticatedEncryption {
val primitive = XChaCha20Poly1305Pure(key.value, additionalData)
val primitive = XChaCha20Poly1305Delegated(key.value, additionalData)
override fun encryptPartialData(data: UByteArray): EncryptedDataPart {
return EncryptedDataPart(primitive.encryptPartialData(data))
}
@ -185,7 +186,7 @@ class MultipartAuthenticatedEncryptor internal constructor(val key : SymmetricKe
}
class MultiplatformAuthenticatedVerificator internal constructor(key: SymmetricKey, multipartEncryptedDataDescriptor: MultipartEncryptedDataDescriptor, additionalData: UByteArray) : MultipartAuthenticatedVerification {
val primitive = XChaCha20Poly1305Pure(key.value, additionalData)
val primitive = XChaCha20Poly1305Delegated(key.value, additionalData)
val tag = multipartEncryptedDataDescriptor.data.sliceArray(
multipartEncryptedDataDescriptor.data.size - 16 until multipartEncryptedDataDescriptor.data.size
)
@ -200,7 +201,7 @@ class MultiplatformAuthenticatedVerificator internal constructor(key: SymmetricK
}
class MultipartAuthenticatedDecryptor internal constructor(val encryptor: XChaCha20Poly1305Pure) : MultipartAuthenticatedDecryption {
class MultipartAuthenticatedDecryptor internal constructor(val encryptor: XChaCha20Poly1305Delegated) : MultipartAuthenticatedDecryption {
override fun decryptPartialData(data: EncryptedDataPart): DecryptedDataPart {
return DecryptedDataPart(encryptor.decrypt(data.data))
}

View File

@ -5,3 +5,18 @@ package com.ionspin.kotlin.crypto.authenticated
* ugljesa.jovanovic@ionspin.com
* on 14-Jun-2020
*/
expect class XChaCha20Poly1305Delegated {
companion object {
fun encrypt(key: UByteArray, nonce: UByteArray, message: UByteArray, additionalData: UByteArray) : UByteArray
fun decrypt(key: UByteArray, nonce: UByteArray, cipherText: UByteArray, additionalData: UByteArray) : UByteArray
}
fun encryptPartialData(data: UByteArray) : UByteArray
fun verifyPartialData(data: UByteArray)
fun checkTag(expectedTag: UByteArray)
fun decrypt(data: UByteArray) : UByteArray
fun finishEncryption() : Pair<UByteArray, UByteArray>
}

View File

@ -0,0 +1,63 @@
package com.ionspin.kotlin.crypto.authenticated
import com.goterl.lazycode.lazysodium.SodiumJava
import com.ionspin.kotlin.crypto.Initializer.sodium
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 14-Jun-2020
*/
actual class XChaCha20Poly1305Delegated {
actual companion object {
actual fun encrypt(
key: UByteArray,
nonce: UByteArray,
message: UByteArray,
additionalData: UByteArray
): UByteArray {
val ciphertext = ByteArray(message.size + sodium.crypto_secretstream_xchacha20poly1305_abytes())
SodiumJava().crypto_aead_xchacha20poly1305_ietf_encrypt(
ciphertext,
longArrayOf(ciphertext.size.toLong()),
message.toByteArray(),
message.size.toLong(),
additionalData.toByteArray(),
additionalData.size.toLong(),
null,
nonce.toByteArray(),
key.toByteArray()
)
return ciphertext.toUByteArray()
}
actual fun decrypt(
key: UByteArray,
nonce: UByteArray,
cipherText: UByteArray,
additionalData: UByteArray
): UByteArray {
TODO("not implemented yet")
}
}
actual fun encryptPartialData(data: UByteArray): UByteArray {
TODO("not implemented yet")
}
actual fun verifyPartialData(data: UByteArray) {
}
actual fun checkTag(expectedTag: UByteArray) {
}
actual fun decrypt(data: UByteArray): UByteArray {
TODO("not implemented yet")
}
actual fun finishEncryption(): Pair<UByteArray, UByteArray> {
TODO("not implemented yet")
}
}

View File

@ -1,7 +1,6 @@
package com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.authenticated.*
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2b
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bMultipart
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bPure
import com.ionspin.kotlin.crypto.hash.sha.Sha256Pure
@ -134,7 +133,7 @@ class MultipartAuthenticatedEncryptor internal constructor(val key : SymmetricKe
}
override fun finish(): MultipartEncryptedDataDescriptor {
val finished = primitive.finish()
val finished = primitive.finishEncryption()
return MultipartEncryptedDataDescriptor(finished.first, finished.second)
}

View File

@ -168,7 +168,7 @@ class XChaCha20Poly1305Pure(val key: UByteArray, val additionalData: UByteArray)
}
}
fun finish() : Pair<UByteArray, UByteArray> {
fun finishEncryption() : Pair<UByteArray, UByteArray> {
val cipherTextPad = UByteArray(16 - processedBytes % 16) { 0U }
val macData = cipherTextPad +

View File

@ -134,7 +134,7 @@ class XChaCha20Poly1305Test {
)
val xChaChaPoly = XChaCha20Poly1305Pure(key, nonce, additionalData)
val firstChunk = xChaChaPoly.encryptPartialData(message)
val finalChunk = xChaChaPoly.finish()
val finalChunk = xChaChaPoly.finishEncryption()
val result = firstChunk + finalChunk
result.contentEquals(expected)
@ -165,11 +165,11 @@ class XChaCha20Poly1305Test {
)
val xChaChaPoly = XChaCha20Poly1305Pure(key, nonce, additionalData)
val firstChunk = xChaChaPoly.encryptPartialData(message)
val finalChunk = xChaChaPoly.finish()
val finalChunk = xChaChaPoly.finishEncryption()
val result = firstChunk + finalChunk
result.contentEquals(expected)
}
}
}
}