Further progress on delegated xchacha poly, working jvm , js in progress

This commit is contained in:
Ugljesa Jovanovic authored and Ugljesa Jovanovic committed 2020-06-25 21:16:14 +02:00
1 parent e3fe276e4c
commit 6228263978
29 files changed
+419 -131

No files matched your search

@@ -52,7 +52,7 @@ object CryptoPrimitives : PrimitivesApi {
return Sha256Pure.digest(inputMessage = message)
}
override fun hashSha512Multipart(): com.ionspin.kotlin.crypto.hash.sha.Sha512 {
override fun hashSha512Multipart(): com.ionspin.kotlin.crypto.hash.sha.Sha512Multipart {
checkInitialization()
return Sha512Pure()
}
@@ -1,24 +0,0 @@
package com.ionspin.kotlin.crypto.authenticated
import com.ionspin.kotlin.crypto.SRNG
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 14-Jun-2020
*/
internal class Aes256GcmStatelessPure : Aes256GcmStateless {
/**
* Nonce autogenerated
*/
override fun encrypt(message: UByteArray, additionalData: UByteArray, rawData : UByteArray, key: Aes256GcmKey) : Aes256GcmEncryptionResult {
TODO()
}
override fun decrypt(encryptedData: UByteArray, nonce: UByteArray, key: Aes256GcmKey): UByteArray {
TODO("not implemented yet")
}
}
@@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.authenticated
import com.ionspin.kotlin.crypto.AuthenticatedEncryption
import com.ionspin.kotlin.crypto.InvalidTagException
import com.ionspin.kotlin.crypto.MultipartAuthenticatedDecryption
import com.ionspin.kotlin.crypto.SRNG
@@ -13,7 +14,7 @@ import com.ionspin.kotlin.crypto.util.toLittleEndianUByteArray
* ugljesa.jovanovic@ionspin.com
* on 17-Jun-2020
*/
class XChaCha20Poly1305Pure(val key: UByteArray, val additionalData: UByteArray) : {
class XChaCha20Poly1305Pure(val key: UByteArray, val additionalData: UByteArray) {
companion object : AuthenticatedEncryption {
override fun encrypt(key: UByteArray, nonce: UByteArray, message: UByteArray, additionalData: UByteArray) : UByteArray {
@@ -68,7 +69,7 @@ class XChaCha20Poly1305Pure(val key: UByteArray, val additionalData: UByteArray)
}
val nonce = SRNG.getRandomBytes(24)
internal val nonce = SRNG.getRandomBytes(24)
private val updateableEncryptionPrimitive = XChaCha20Pure(key, nonce, initialCounter = 1U)
private val updateableMacPrimitive : Poly1305
@@ -25,11 +25,11 @@ import com.ionspin.kotlin.crypto.util.rotateRight
*/
class Sha512Pure : Sha512 {
class Sha512Pure : Sha512Multipart {
override val MAX_HASH_BYTES: Int = 32
companion object : MultipartSha512 {
companion object : Sha512 {
const val BLOCK_SIZE = 1024
const val BLOCK_SIZE_IN_BYTES = 128
const val CHUNK_SIZE = 80
@@ -19,8 +19,9 @@
package com.ionspin.kotlin.crypto.keyderivation.argon2
import com.ionspin.kotlin.bignum.integer.toBigInteger
import com.ionspin.kotlin.crypto.Blake2bPureStateless
import com.ionspin.kotlin.crypto.SRNG
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bPure
import com.ionspin.kotlin.crypto.keyderivation.ArgonResult
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.argonBlake2bArbitraryLenghtHash
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils.compressionFunctionG
@@ -290,7 +291,7 @@ class Argon2Pure(
salt.size.toUInt().toLittleEndianUByteArray() + salt +
key.size.toUInt().toLittleEndianUByteArray() + key +
associatedData.size.toUInt().toLittleEndianUByteArray() + associatedData
val h0 = Blake2bPureStateless.digest(
val h0 = Blake2bPure.digest(
blakeInput
)
@@ -26,30 +26,6 @@ val _emitIntArray: IntArray = intArrayOf(1)
* ugljesa.jovanovic@ionspin.com
* on 15-Jul-2019
*/
fun Array<Byte>.hexColumsPrint() {
val printout = this.map { it.toString(16) }.chunked(16)
printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) }
}
fun Array<UByte>.hexColumsPrint(chunk : Int = 16) {
val printout = this.map { it.toString(16).padStart(2, '0') }.chunked(chunk)
printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) }
}
fun UByteArray.hexColumsPrint(chunk : Int = 16) {
val printout = this.map { it.toString(16).padStart(2, '0') }.chunked(chunk)
printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) }
}
fun UIntArray.hexColumsPrint(chunk : Int = 4) {
val printout = this.map { it.toString(16).padStart(8, '0') }.chunked(chunk)
printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) }
}
fun Array<ULong>.hexColumsPrint(chunk: Int = 3) {
val printout = this.map { it.toString(16) }.chunked(chunk)
printout.forEach { println(it.joinToString(separator = " ") { it.toUpperCase() }) }
}
inline fun <reified T> Array<T>.chunked(sliceSize: Int): Array<Array<T>> {
val last = this.size % sliceSize
@@ -122,37 +98,6 @@ fun UByteArray.xorWithPositionsAndInsertIntoArray(
}
}
fun String.hexStringToTypedUByteArray() : Array<UByte> {
return this.chunked(2).map { it.toUByte(16) }.toTypedArray()
}
fun String.hexStringToUByteArray() : UByteArray {
return this.chunked(2).map { it.toUByte(16) }.toUByteArray()
}
fun Array<UByte>.toHexString() : String {
return this.joinToString(separator = "") {
if (it <= 0x0FU) {
"0${it.toString(16)}"
} else {
it.toString(16)
}
}
}
fun UByteArray.toHexString() : String {
return this.joinToString(separator = "") {
if (it <= 0x0FU) {
"0${it.toString(16)}"
} else {
it.toString(16)
}
}
}
// UInt / Array utils
fun UInt.toBigEndianUByteArray() : Array<UByte> {
@@ -356,4 +301,4 @@ fun Collection<UByteArray>.flattenToUByteArray(): UByteArray {
}
}
return result
}
}
@@ -159,9 +159,9 @@ class ReadmeTest {
@Test
fun debugTest() {
val result = Blake2bPureStateless.digest("test".encodeToUByteArray())
val result = Blake2bPure.digest("test".encodeToUByteArray())
println(result.toHexString())
}
}
}
@@ -1,6 +1,7 @@
package com.ionspin.kotlin.crypto.authenticated
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import kotlin.test.Ignore
import kotlin.test.Test
import kotlin.test.assertTrue
@@ -90,7 +91,7 @@ class XChaCha20Poly1305Test {
}
@Ignore() //"Will fail because nonce is not a parameter any more"
@Test
fun updateableXChaCha20Poly1305() {
assertTrue {
@@ -132,9 +133,9 @@ class XChaCha20Poly1305Test {
0x98U, 0x79U, 0x47U, 0xdeU, 0xafU, 0xd8U, 0x78U, 0x0aU,
0xcfU, 0x49U
)
val xChaChaPoly = XChaCha20Poly1305Pure(key, nonce, additionalData)
val xChaChaPoly = XChaCha20Poly1305Pure(key, additionalData)
val firstChunk = xChaChaPoly.encryptPartialData(message)
val finalChunk = xChaChaPoly.finishEncryption()
val finalChunk = xChaChaPoly.finishEncryption().first
val result = firstChunk + finalChunk
result.contentEquals(expected)
@@ -163,9 +164,9 @@ class XChaCha20Poly1305Test {
0xbdU, 0x3bU, 0x8aU, 0xd7U, 0xa1U, 0x9dU, 0xe8U, 0xc4U, 0x55U,
0x84U, 0x6fU, 0xfcU, 0x75U, 0x31U, 0xbfU, 0x0cU, 0x2dU
)
val xChaChaPoly = XChaCha20Poly1305Pure(key, nonce, additionalData)
val xChaChaPoly = XChaCha20Poly1305Pure(key, additionalData)
val firstChunk = xChaChaPoly.encryptPartialData(message)
val finalChunk = xChaChaPoly.finishEncryption()
val finalChunk = xChaChaPoly.finishEncryption().first
val result = firstChunk + finalChunk
result.contentEquals(expected)
}