Multipart API continuation
This commit is contained in:
1 parent
f107db3312
commit
233ee1bf55
14 files changed
+226
-102
No files matched your search
+50
-7
@@ -1,7 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto
|
||||
|
||||
import com.ionspin.kotlin.crypto.authenticated.XChaCha20Poly1305Pure
|
||||
import com.ionspin.kotlin.crypto.hash.UpdatableHash
|
||||
import com.ionspin.kotlin.crypto.authenticated.*
|
||||
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bProperties
|
||||
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bPure
|
||||
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
|
||||
@@ -115,17 +114,17 @@ data class EncryptedData internal constructor(val ciphertext: UByteArray, val no
|
||||
|
||||
object PublicApi {
|
||||
|
||||
object Hash {
|
||||
object Hashing {
|
||||
fun hash(data: UByteArray, key : UByteArray = ubyteArrayOf()) : HashedData {
|
||||
return HashedData(Blake2bPureStateless.digest(data, key))
|
||||
}
|
||||
|
||||
fun updateableHash(key: UByteArray? = null) : UpdatableHash {
|
||||
fun multipartHash(key: UByteArray? = null) : com.ionspin.kotlin.crypto.hash.MultiPartHash {
|
||||
return Blake2bPure(key)
|
||||
}
|
||||
}
|
||||
object Symmetric {
|
||||
fun encrypt(key: SymmetricKey, data : Encryptable<*>, additionalData : UByteArray = ubyteArrayOf()) : EncryptedData {
|
||||
object Encryption {
|
||||
fun authenticatedEncryption(key: SymmetricKey, data : Encryptable<*>, additionalData : UByteArray = ubyteArrayOf()) : EncryptedData {
|
||||
if (key.value.size != 32) {
|
||||
throw RuntimeException("Invalid key size! Required 32, supplied ${key.value.size}")
|
||||
}
|
||||
@@ -138,5 +137,49 @@ object PublicApi {
|
||||
return byteArrayDeserializer(XChaCha20Poly1305Pure.decrypt(key.value, encryptedData.nonce, encryptedData.ciphertext, additionalData))
|
||||
|
||||
}
|
||||
|
||||
fun multipartAuthenticatedEncrypt(key: SymmetricKey, additionalData: UByteArray) : MultipartAuthenticatedEncryption {
|
||||
return MultipartAuthenticatedEncryptor(key, additionalData)
|
||||
}
|
||||
|
||||
fun getMultipartVerificator(key: SymmetricKey, dataDescriptor: MultipartEncryptedDataDescriptor, additionalData: UByteArray) : MultipartAuthenticatedVerification {
|
||||
return MultiplatformAuthenticatedVerificator(key, dataDescriptor, additionalData)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class MultipartAuthenticatedEncryptor internal constructor(val key : SymmetricKey, additionalData: UByteArray) : MultipartAuthenticatedEncryption {
|
||||
val primitive = XChaCha20Poly1305Pure(key.value, additionalData)
|
||||
override fun encryptPartialData(data: UByteArray): EncryptedDataPart {
|
||||
return EncryptedDataPart(primitive.encryptPartialData(data))
|
||||
}
|
||||
|
||||
override fun finish(): MultipartEncryptedDataDescriptor {
|
||||
val finished = primitive.finish()
|
||||
return MultipartEncryptedDataDescriptor(finished.first, finished.second)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MultiplatformAuthenticatedVerificator internal constructor(key: SymmetricKey, multipartEncryptedDataDescriptor: MultipartEncryptedDataDescriptor, additionalData: UByteArray) : MultipartAuthenticatedVerification {
|
||||
val primitive = XChaCha20Poly1305Pure(key.value, additionalData)
|
||||
val tag = multipartEncryptedDataDescriptor.data.sliceArray(
|
||||
multipartEncryptedDataDescriptor.data.size - 16 until multipartEncryptedDataDescriptor.data.size
|
||||
)
|
||||
override fun verifyPartialData(data: EncryptedDataPart) {
|
||||
primitive.encryptPartialData(data.data)
|
||||
}
|
||||
|
||||
override fun finalizeVerificationAndPrepareDecryptor(): MultipartAuthenticatedDecryption {
|
||||
primitive.finalizeVerificationAndPrepareDecryptor(tag)
|
||||
return MultipartAuthenticatedDecryptor(primitive)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class MultipartAuthenticatedDecryptor internal constructor(val encryptor: XChaCha20Poly1305Pure) : MultipartAuthenticatedDecryption {
|
||||
override fun decryptPartialData(data: EncryptedDataPart): DecryptedDataPart {
|
||||
encryptor.decrypt(data.data)
|
||||
}
|
||||
|
||||
}
|
||||
+35
-14
@@ -1,5 +1,6 @@
|
||||
package com.ionspin.kotlin.crypto.authenticated
|
||||
|
||||
import com.ionspin.kotlin.crypto.SRNG
|
||||
import com.ionspin.kotlin.crypto.mac.Poly1305
|
||||
import com.ionspin.kotlin.crypto.symmetric.ChaCha20Pure
|
||||
import com.ionspin.kotlin.crypto.symmetric.XChaCha20Pure
|
||||
@@ -10,10 +11,10 @@ import com.ionspin.kotlin.crypto.util.toLittleEndianUByteArray
|
||||
* ugljesa.jovanovic@ionspin.com
|
||||
* on 17-Jun-2020
|
||||
*/
|
||||
class XChaCha20Poly1305Pure(val key: UByteArray, val nonce: UByteArray, val additionalData: UByteArray) {
|
||||
companion object {
|
||||
class XChaCha20Poly1305Pure(val key: UByteArray, val additionalData: UByteArray) : {
|
||||
companion object : AuthenticatedEncryption {
|
||||
|
||||
fun encrypt(key: UByteArray, nonce: UByteArray, message: UByteArray, additionalData: UByteArray) : UByteArray {
|
||||
override fun encrypt(key: UByteArray, nonce: UByteArray, message: UByteArray, additionalData: UByteArray) : UByteArray {
|
||||
val subKey = XChaCha20Pure.hChacha(key, nonce)
|
||||
val authKey =
|
||||
ChaCha20Pure.encrypt(
|
||||
@@ -36,7 +37,7 @@ class XChaCha20Poly1305Pure(val key: UByteArray, val nonce: UByteArray, val addi
|
||||
return cipherText + tag
|
||||
}
|
||||
|
||||
fun decrypt(key: UByteArray, nonce: UByteArray, cipherText: UByteArray, additionalData: UByteArray) : UByteArray {
|
||||
override fun decrypt(key: UByteArray, nonce: UByteArray, cipherText: UByteArray, additionalData: UByteArray) : UByteArray {
|
||||
val subKey = XChaCha20Pure.hChacha(key, nonce)
|
||||
val authKey =
|
||||
ChaCha20Pure.encrypt(
|
||||
@@ -62,8 +63,11 @@ class XChaCha20Poly1305Pure(val key: UByteArray, val nonce: UByteArray, val addi
|
||||
//4. Decrypt data
|
||||
return XChaCha20Pure.xorWithKeystream(key, nonce, cipherTextWithoutTag, 1U)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
val nonce = SRNG.getRandomBytes(24)
|
||||
|
||||
private val updateableEncryptionPrimitive = XChaCha20Pure(key, nonce, initialCounter = 1U)
|
||||
private val updateableMacPrimitive : Poly1305
|
||||
|
||||
@@ -93,11 +97,34 @@ class XChaCha20Poly1305Pure(val key: UByteArray, val nonce: UByteArray, val addi
|
||||
|
||||
fun encryptPartialData(data: UByteArray) : UByteArray {
|
||||
processedBytes += data.size
|
||||
val encrypted = updateableEncryptionPrimitive.encryptPartialData(data)
|
||||
val encrypted = updateableEncryptionPrimitive.xorWithKeystream(data)
|
||||
processPolyBytes(encrypted)
|
||||
return encrypted
|
||||
}
|
||||
|
||||
fun verifyPartialData(data: UByteArray) {
|
||||
processPolyBytes(data)
|
||||
}
|
||||
|
||||
fun finalizeVerificationAndPrepareDecryptor(expectedTag: UByteArray): MultipartAuthenticatedDecryption {
|
||||
val cipherTextPad = UByteArray(16 - processedBytes % 16) { 0U }
|
||||
val macData = cipherTextPad +
|
||||
additionalData.size.toULong().toLittleEndianUByteArray() +
|
||||
processedBytes.toULong().toLittleEndianUByteArray()
|
||||
processPolyBytes(macData)
|
||||
val tag = updateableMacPrimitive.finalizeMac()
|
||||
if (!tag.contentEquals(expectedTag)) {
|
||||
throw RuntimeException("Invalid tag") //TODO Replace with proper exception
|
||||
}
|
||||
}
|
||||
|
||||
fun decrypt(data: UByteArray) : UByteArray {
|
||||
processedBytes += data.size
|
||||
val decrypted = updateableEncryptionPrimitive.xorWithKeystream(data)
|
||||
processPolyBytes(decrypted)
|
||||
return decrypted
|
||||
}
|
||||
|
||||
private fun processPolyBytes(data: UByteArray) {
|
||||
if (polyBufferByteCounter == 0) {
|
||||
val polyBlocks = data.size / 16
|
||||
@@ -139,12 +166,7 @@ class XChaCha20Poly1305Pure(val key: UByteArray, val nonce: UByteArray, val addi
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
fun finish() : UByteArray {
|
||||
fun finish() : Pair<UByteArray, UByteArray> {
|
||||
|
||||
val cipherTextPad = UByteArray(16 - processedBytes % 16) { 0U }
|
||||
val macData = cipherTextPad +
|
||||
@@ -152,9 +174,8 @@ class XChaCha20Poly1305Pure(val key: UByteArray, val nonce: UByteArray, val addi
|
||||
processedBytes.toULong().toLittleEndianUByteArray()
|
||||
processPolyBytes(macData)
|
||||
val tag = updateableMacPrimitive.finalizeMac()
|
||||
return tag
|
||||
return Pair(tag, nonce)
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
+2
-2
@@ -132,7 +132,7 @@ class XChaCha20Pure(key: UByteArray, nonce: UByteArray, initialCounter: UInt = 0
|
||||
hChaChaKey.overwriteWithZeroes()
|
||||
}
|
||||
|
||||
fun encryptPartialData(data: UByteArray) : UByteArray {
|
||||
fun xorWithKeystream(data: UByteArray) : UByteArray {
|
||||
val ciphertext = UByteArray(data.size) { 0U }
|
||||
//First use remaining keystream
|
||||
var processedBytes = 0
|
||||
@@ -177,4 +177,4 @@ class XChaCha20Pure(key: UByteArray, nonce: UByteArray, initialCounter: UInt = 0
|
||||
return ciphertext
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -344,17 +344,6 @@ fun Array<UByte>.fromBigEndianArrayToUInt() : UInt {
|
||||
operator fun UInt.plus(other : UByteArray) : UByteArray {
|
||||
return this.toLittleEndianUByteArray() + other
|
||||
}
|
||||
fun UByteArray.overwriteWithZeroes() {
|
||||
for (i in 0 until size) {
|
||||
this[i] = 0U
|
||||
}
|
||||
}
|
||||
|
||||
fun UIntArray.overwriteWithZeroes() {
|
||||
for (i in 0 until size) {
|
||||
this[i] = 0U
|
||||
}
|
||||
}
|
||||
|
||||
//AES Flatten
|
||||
fun Collection<UByteArray>.flattenToUByteArray(): UByteArray {
|
||||
|
||||
+7
-7
@@ -236,9 +236,9 @@ class XChaCha20Test {
|
||||
)
|
||||
|
||||
val xChaCha = XChaCha20Pure(key, nonce, 1U)
|
||||
val firstChunk = xChaCha.encryptPartialData(message.sliceArray(0 until 5))
|
||||
val secondChunk = xChaCha.encryptPartialData(message.sliceArray(5 until 90))
|
||||
val thirdChunk = xChaCha.encryptPartialData(message.sliceArray(90 until message.size))
|
||||
val firstChunk = xChaCha.xorWithKeystream(message.sliceArray(0 until 5))
|
||||
val secondChunk = xChaCha.xorWithKeystream(message.sliceArray(5 until 90))
|
||||
val thirdChunk = xChaCha.xorWithKeystream(message.sliceArray(90 until message.size))
|
||||
|
||||
assertTrue {
|
||||
(firstChunk + secondChunk + thirdChunk).contentEquals(expected)
|
||||
@@ -325,9 +325,9 @@ class XChaCha20Test {
|
||||
)
|
||||
|
||||
val xChaCha = XChaCha20Pure(key, nonce, 1U)
|
||||
val firstChunk = xChaCha.encryptPartialData(message.sliceArray(0 until 50))
|
||||
val secondChunk = xChaCha.encryptPartialData(message.sliceArray(50 until 200))
|
||||
val thirdChunk = xChaCha.encryptPartialData(message.sliceArray(200 until message.size))
|
||||
val firstChunk = xChaCha.xorWithKeystream(message.sliceArray(0 until 50))
|
||||
val secondChunk = xChaCha.xorWithKeystream(message.sliceArray(50 until 200))
|
||||
val thirdChunk = xChaCha.xorWithKeystream(message.sliceArray(200 until message.size))
|
||||
val result = (firstChunk + secondChunk + thirdChunk)
|
||||
assertTrue {
|
||||
result.contentEquals(expected)
|
||||
@@ -337,4 +337,4 @@ class XChaCha20Test {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user