Starting delegated implementation
This commit is contained in:
parent
1848de0e8d
commit
e3fe276e4c
@ -1,5 +1,6 @@
|
|||||||
package com.ionspin.kotlin.crypto
|
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.Blake2bProperties
|
||||||
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegated
|
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegated
|
||||||
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bDelegatedStateless
|
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}")
|
throw RuntimeException("Invalid key size! Required 32, supplied ${key.value.size}")
|
||||||
}
|
}
|
||||||
val nonce = SRNG.getRandomBytes(24)
|
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 {
|
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 {
|
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 {
|
override fun encryptPartialData(data: UByteArray): EncryptedDataPart {
|
||||||
return EncryptedDataPart(primitive.encryptPartialData(data))
|
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 {
|
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(
|
val tag = multipartEncryptedDataDescriptor.data.sliceArray(
|
||||||
multipartEncryptedDataDescriptor.data.size - 16 until multipartEncryptedDataDescriptor.data.size
|
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 {
|
override fun decryptPartialData(data: EncryptedDataPart): DecryptedDataPart {
|
||||||
return DecryptedDataPart(encryptor.decrypt(data.data))
|
return DecryptedDataPart(encryptor.decrypt(data.data))
|
||||||
}
|
}
|
||||||
|
@ -5,3 +5,18 @@ package com.ionspin.kotlin.crypto.authenticated
|
|||||||
* ugljesa.jovanovic@ionspin.com
|
* ugljesa.jovanovic@ionspin.com
|
||||||
* on 14-Jun-2020
|
* 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>
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -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")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,7 +1,6 @@
|
|||||||
package com.ionspin.kotlin.crypto
|
package com.ionspin.kotlin.crypto
|
||||||
|
|
||||||
import com.ionspin.kotlin.crypto.authenticated.*
|
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.Blake2bMultipart
|
||||||
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bPure
|
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bPure
|
||||||
import com.ionspin.kotlin.crypto.hash.sha.Sha256Pure
|
import com.ionspin.kotlin.crypto.hash.sha.Sha256Pure
|
||||||
@ -134,7 +133,7 @@ class MultipartAuthenticatedEncryptor internal constructor(val key : SymmetricKe
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun finish(): MultipartEncryptedDataDescriptor {
|
override fun finish(): MultipartEncryptedDataDescriptor {
|
||||||
val finished = primitive.finish()
|
val finished = primitive.finishEncryption()
|
||||||
return MultipartEncryptedDataDescriptor(finished.first, finished.second)
|
return MultipartEncryptedDataDescriptor(finished.first, finished.second)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 cipherTextPad = UByteArray(16 - processedBytes % 16) { 0U }
|
||||||
val macData = cipherTextPad +
|
val macData = cipherTextPad +
|
||||||
|
@ -134,7 +134,7 @@ class XChaCha20Poly1305Test {
|
|||||||
)
|
)
|
||||||
val xChaChaPoly = XChaCha20Poly1305Pure(key, nonce, additionalData)
|
val xChaChaPoly = XChaCha20Poly1305Pure(key, nonce, additionalData)
|
||||||
val firstChunk = xChaChaPoly.encryptPartialData(message)
|
val firstChunk = xChaChaPoly.encryptPartialData(message)
|
||||||
val finalChunk = xChaChaPoly.finish()
|
val finalChunk = xChaChaPoly.finishEncryption()
|
||||||
val result = firstChunk + finalChunk
|
val result = firstChunk + finalChunk
|
||||||
|
|
||||||
result.contentEquals(expected)
|
result.contentEquals(expected)
|
||||||
@ -165,11 +165,11 @@ class XChaCha20Poly1305Test {
|
|||||||
)
|
)
|
||||||
val xChaChaPoly = XChaCha20Poly1305Pure(key, nonce, additionalData)
|
val xChaChaPoly = XChaCha20Poly1305Pure(key, nonce, additionalData)
|
||||||
val firstChunk = xChaChaPoly.encryptPartialData(message)
|
val firstChunk = xChaChaPoly.encryptPartialData(message)
|
||||||
val finalChunk = xChaChaPoly.finish()
|
val finalChunk = xChaChaPoly.finishEncryption()
|
||||||
val result = firstChunk + finalChunk
|
val result = firstChunk + finalChunk
|
||||||
result.contentEquals(expected)
|
result.contentEquals(expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user