Some more sketching of the public API, added workaround for uint backing class kotln bug, worked around new js bug

This commit is contained in:
Ugljesa Jovanovic 2020-06-20 23:35:52 +02:00 committed by Ugljesa Jovanovic
parent fc2d7b701d
commit 1a4fc90922
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
10 changed files with 170 additions and 112 deletions

View File

@ -2,8 +2,10 @@ package com.ionspin.kotlin.crypto
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bProperties import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bProperties
import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bPure import com.ionspin.kotlin.crypto.hash.blake2b.Blake2bPure
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.hash.sha.Sha256Pure import com.ionspin.kotlin.crypto.hash.sha.Sha256Pure
import com.ionspin.kotlin.crypto.hash.sha.Sha512Pure import com.ionspin.kotlin.crypto.hash.sha.Sha512Pure
import com.ionspin.kotlin.crypto.util.toHexString
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic
@ -64,4 +66,48 @@ object Crypto : CryptoProvider {
// Nothing to do atm // Nothing to do atm
} }
}
inline class EncryptableString(val content: String) : Encryptable {
override fun encryptableData(): UByteArray {
return content.encodeToUByteArray()
}
fun asString() : String = content
}
fun String.asEncryptableString() : EncryptableString {
return EncryptableString(this)
}
interface Encryptable {
fun encryptableData() : UByteArray
}
object PublicApi {
data class HashedData(val hash: UByteArray) {
fun toHexString() : String {
return hash.toHexString()
}
}
data class EncryptedData(val encrypted: UByteArray)
object Hash {
fun hash() : HashedData {
TODO()
}
}
object Symmetric {
fun encrypt(data : Encryptable) : EncryptedData {
TODO()
}
fun <T: Encryptable> decrypt(encryptedData : EncryptedData) : T {
TODO()
}
}
} }

View File

@ -23,7 +23,9 @@ class XChaCha20Poly1305Pure {
ChaCha20Pure.encrypt( ChaCha20Pure.encrypt(
oneTimeKey.toLittleEndianUByteArray(), oneTimeKey.toLittleEndianUByteArray(),
ubyteArrayOf(0U, 0U, 0U, 0U) + nonce.sliceArray(16 until 24), ubyteArrayOf(0U, 0U, 0U, 0U) + nonce.sliceArray(16 until 24),
UByteArray(64) { 0U }) UByteArray(64) { 0U },
0U
)
println("Poly sub-key:") println("Poly sub-key:")
oneTimeKey.hexColumsPrint() oneTimeKey.hexColumsPrint()
println("Poly key:") println("Poly key:")

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.symmetric package com.ionspin.kotlin.crypto.symmetric
import com.ionspin.kotlin.crypto.symmetric.LatinDancesCommon.littleEndianInverted
import com.ionspin.kotlin.crypto.util.* import com.ionspin.kotlin.crypto.util.*
/** /**
@ -59,8 +60,6 @@ internal class ChaCha20Pure {
fun encrypt(key: UByteArray, nonce: UByteArray, message: UByteArray, initialCounter: UInt = 0U): UByteArray { fun encrypt(key: UByteArray, nonce: UByteArray, message: UByteArray, initialCounter: UInt = 0U): UByteArray {
val ciphertext = UByteArray(message.size) val ciphertext = UByteArray(message.size)
val state = UIntArray(16) { val state = UIntArray(16) {
when (it) { when (it) {
0 -> sigma0_32 0 -> sigma0_32
1 -> sigma1_32 1 -> sigma1_32

View File

@ -1,45 +0,0 @@
package com.ionspin.kotlin.crypto.symmetric
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 16-Jun-2020
*/
fun littleEndian(
input: UByteArray,
byte0Position: Int,
byte1Position: Int,
byte2Position: Int,
byte3Position: Int
): UInt {
var uint = 0U
uint = input[byte0Position].toUInt()
uint = uint or (input[byte1Position].toUInt() shl 8)
uint = uint or (input[byte2Position].toUInt() shl 16)
uint = uint or (input[byte3Position].toUInt() shl 24)
return uint
}
fun littleEndianInverted(
input: UIntArray,
startingPosition: Int,
output: UByteArray,
outputPosition: Int
) {
output[outputPosition] = (input[startingPosition] and 0xFFU).toUByte()
output[outputPosition + 1] = ((input[startingPosition] shr 8) and 0xFFU).toUByte()
output[outputPosition + 2] = ((input[startingPosition] shr 16) and 0xFFU).toUByte()
output[outputPosition + 3] = ((input[startingPosition] shr 24) and 0xFFU).toUByte()
}
fun littleEndianInverted(
input: UInt,
output: UByteArray,
outputPosition: Int
) {
output[outputPosition] = (input and 0xFFU).toUByte()
output[outputPosition + 1] = ((input shr 8) and 0xFFU).toUByte()
output[outputPosition + 2] = ((input shr 16) and 0xFFU).toUByte()
output[outputPosition + 3] = ((input shr 24) and 0xFFU).toUByte()
}

View File

@ -0,0 +1,55 @@
package com.ionspin.kotlin.crypto.symmetric
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 16-Jun-2020
*/
object LatinDancesCommon {
val _emitIntArray: IntArray = intArrayOf(1)
fun littleEndian(
input: UByteArray,
byte0Position: Int,
byte1Position: Int,
byte2Position: Int,
byte3Position: Int
): UInt {
var uint = 0U
uint = input[byte0Position].toUInt()
uint = uint or (input[byte1Position].toUInt() shl 8)
uint = uint or (input[byte2Position].toUInt() shl 16)
uint = uint or (input[byte3Position].toUInt() shl 24)
return uint
}
fun littleEndianInverted(
input: UIntArray,
startingPosition: Int,
output: UByteArray,
outputPosition: Int
) {
output[outputPosition] = (input[startingPosition] and 0xFFU).toUByte()
output[outputPosition + 1] = ((input[startingPosition] shr 8) and 0xFFU).toUByte()
output[outputPosition + 2] = ((input[startingPosition] shr 16) and 0xFFU).toUByte()
output[outputPosition + 3] = ((input[startingPosition] shr 24) and 0xFFU).toUByte()
}
fun littleEndianInverted(
input: UInt,
output: UByteArray,
outputPosition: Int
) {
output[outputPosition] = (input and 0xFFU).toUByte()
output[outputPosition + 1] = ((input shr 8) and 0xFFU).toUByte()
output[outputPosition + 2] = ((input shr 16) and 0xFFU).toUByte()
output[outputPosition + 3] = ((input shr 24) and 0xFFU).toUByte()
}
}

View File

@ -1,5 +1,6 @@
package com.ionspin.kotlin.crypto.symmetric package com.ionspin.kotlin.crypto.symmetric
import com.ionspin.kotlin.crypto.symmetric.LatinDancesCommon.littleEndianInverted
import com.ionspin.kotlin.crypto.util.* import com.ionspin.kotlin.crypto.util.*
/** /**
@ -49,10 +50,10 @@ internal class Salsa20Pure {
return result return result
} }
internal val sigma0_32_uint = 1634760805U //ubyteArrayOf(101U, 120U, 112U, 97U) internal var sigma0_32_uint = 1634760805U //ubyteArrayOf(101U, 120U, 112U, 97U)
internal val sigma1_32_uint = 857760878U //ubyteArrayOf(110U, 100U, 32U, 51U) internal var sigma1_32_uint = 857760878U //ubyteArrayOf(110U, 100U, 32U, 51U)
internal val sigma2_32_uint = 2036477234U //ubyteArrayOf(50U, 45U, 98U, 121U) internal var sigma2_32_uint = 2036477234U //ubyteArrayOf(50U, 45U, 98U, 121U)
internal val sigma3_32_uint = 1797285236U //ubyteArrayOf(116U, 101U, 32U, 107U) internal var sigma3_32_uint = 1797285236U //ubyteArrayOf(116U, 101U, 32U, 107U)
val sigma0_32 = ubyteArrayOf(101U, 120U, 112U, 97U) val sigma0_32 = ubyteArrayOf(101U, 120U, 112U, 97U)
val sigma1_32 = ubyteArrayOf(110U, 100U, 32U, 51U) val sigma1_32 = ubyteArrayOf(110U, 100U, 32U, 51U)

View File

@ -18,9 +18,9 @@
package com.ionspin.kotlin.crypto.util package com.ionspin.kotlin.crypto.util
import com.ionspin.kotlin.crypto.keyderivation.argon2.Argon2Utils
import com.ionspin.kotlin.crypto.keyderivation.argon2.xorWithBlock
val _emitIntArray: IntArray = intArrayOf(1)
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com * ugljesa.jovanovic@ionspin.com

View File

@ -1,56 +1,56 @@
package com.ionspin.kotlin.crypto.authenticated //package com.ionspin.kotlin.crypto.authenticated
//
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray //import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.hexColumsPrint //import com.ionspin.kotlin.crypto.util.hexColumsPrint
import kotlin.test.Test //import kotlin.test.Test
import kotlin.test.assertTrue //import kotlin.test.assertTrue
//
/** ///**
* Created by Ugljesa Jovanovic // * Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com // * ugljesa.jovanovic@ionspin.com
* on 17-Jun-2020 // * on 17-Jun-2020
*/ // */
class ChaCha20Poly1305Test { //class ChaCha20Poly1305Test {
//
//
//
@Test // @Test
fun chaCha20Poly1305() { // fun chaCha20Poly1305() {
val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " + // val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
"only one tip for the future, sunscreen would be it.").encodeToUByteArray() // "only one tip for the future, sunscreen would be it.").encodeToUByteArray()
//
val additionalData = ubyteArrayOf( // val additionalData = ubyteArrayOf(
0x50U, 0x51U, 0x52U, 0x53U, 0xc0U, 0xc1U, 0xc2U, 0xc3U, 0xc4U, 0xc5U, 0xc6U, 0xc7U // 0x50U, 0x51U, 0x52U, 0x53U, 0xc0U, 0xc1U, 0xc2U, 0xc3U, 0xc4U, 0xc5U, 0xc6U, 0xc7U
) // )
val key = ubyteArrayOf( // val key = ubyteArrayOf(
0x80U, 0x81U, 0x82U, 0x83U, 0x84U, 0x85U, 0x86U, 0x87U, // 0x80U, 0x81U, 0x82U, 0x83U, 0x84U, 0x85U, 0x86U, 0x87U,
0x88U, 0x89U, 0x8aU, 0x8bU, 0x8cU, 0x8dU, 0x8eU, 0x8fU, // 0x88U, 0x89U, 0x8aU, 0x8bU, 0x8cU, 0x8dU, 0x8eU, 0x8fU,
0x90U, 0x91U, 0x92U, 0x93U, 0x94U, 0x95U, 0x96U, 0x97U, // 0x90U, 0x91U, 0x92U, 0x93U, 0x94U, 0x95U, 0x96U, 0x97U,
0x98U, 0x99U, 0x9aU, 0x9bU, 0x9cU, 0x9dU, 0x9eU, 0x9fU, // 0x98U, 0x99U, 0x9aU, 0x9bU, 0x9cU, 0x9dU, 0x9eU, 0x9fU,
) // )
//
val nonce = ubyteArrayOf( // val nonce = ubyteArrayOf(
0x07U, 0x00U, 0x00U, 0x00U, 0x40U, 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U // 0x07U, 0x00U, 0x00U, 0x00U, 0x40U, 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U
) // )
//Ciphertext + Poly1305TAG // //Ciphertext + Poly1305TAG
val expected = ubyteArrayOf( // val expected = ubyteArrayOf(
0xd3U, 0x1aU, 0x8dU, 0x34U, 0x64U, 0x8eU, 0x60U, 0xdbU, 0x7bU, 0x86U, 0xafU, 0xbcU, 0x53U, 0xefU, 0x7eU, 0xc2U, // 0xd3U, 0x1aU, 0x8dU, 0x34U, 0x64U, 0x8eU, 0x60U, 0xdbU, 0x7bU, 0x86U, 0xafU, 0xbcU, 0x53U, 0xefU, 0x7eU, 0xc2U,
0xa4U, 0xadU, 0xedU, 0x51U, 0x29U, 0x6eU, 0x08U, 0xfeU, 0xa9U, 0xe2U, 0xb5U, 0xa7U, 0x36U, 0xeeU, 0x62U, 0xd6U, // 0xa4U, 0xadU, 0xedU, 0x51U, 0x29U, 0x6eU, 0x08U, 0xfeU, 0xa9U, 0xe2U, 0xb5U, 0xa7U, 0x36U, 0xeeU, 0x62U, 0xd6U,
0x3dU, 0xbeU, 0xa4U, 0x5eU, 0x8cU, 0xa9U, 0x67U, 0x12U, 0x82U, 0xfaU, 0xfbU, 0x69U, 0xdaU, 0x92U, 0x72U, 0x8bU, // 0x3dU, 0xbeU, 0xa4U, 0x5eU, 0x8cU, 0xa9U, 0x67U, 0x12U, 0x82U, 0xfaU, 0xfbU, 0x69U, 0xdaU, 0x92U, 0x72U, 0x8bU,
0x1aU, 0x71U, 0xdeU, 0x0aU, 0x9eU, 0x06U, 0x0bU, 0x29U, 0x05U, 0xd6U, 0xa5U, 0xb6U, 0x7eU, 0xcdU, 0x3bU, 0x36U, // 0x1aU, 0x71U, 0xdeU, 0x0aU, 0x9eU, 0x06U, 0x0bU, 0x29U, 0x05U, 0xd6U, 0xa5U, 0xb6U, 0x7eU, 0xcdU, 0x3bU, 0x36U,
0x92U, 0xddU, 0xbdU, 0x7fU, 0x2dU, 0x77U, 0x8bU, 0x8cU, 0x98U, 0x03U, 0xaeU, 0xe3U, 0x28U, 0x09U, 0x1bU, 0x58U, // 0x92U, 0xddU, 0xbdU, 0x7fU, 0x2dU, 0x77U, 0x8bU, 0x8cU, 0x98U, 0x03U, 0xaeU, 0xe3U, 0x28U, 0x09U, 0x1bU, 0x58U,
0xfaU, 0xb3U, 0x24U, 0xe4U, 0xfaU, 0xd6U, 0x75U, 0x94U, 0x55U, 0x85U, 0x80U, 0x8bU, 0x48U, 0x31U, 0xd7U, 0xbcU, // 0xfaU, 0xb3U, 0x24U, 0xe4U, 0xfaU, 0xd6U, 0x75U, 0x94U, 0x55U, 0x85U, 0x80U, 0x8bU, 0x48U, 0x31U, 0xd7U, 0xbcU,
0x3fU, 0xf4U, 0xdeU, 0xf0U, 0x8eU, 0x4bU, 0x7aU, 0x9dU, 0xe5U, 0x76U, 0xd2U, 0x65U, 0x86U, 0xceU, 0xc6U, 0x4bU, // 0x3fU, 0xf4U, 0xdeU, 0xf0U, 0x8eU, 0x4bU, 0x7aU, 0x9dU, 0xe5U, 0x76U, 0xd2U, 0x65U, 0x86U, 0xceU, 0xc6U, 0x4bU,
0x61U, 0x16U, 0x1aU, 0xe1U, 0x0bU, 0x59U, 0x4fU, 0x09U, 0xe2U, 0x6aU, 0x7eU, 0x90U, 0x2eU, 0xcbU, 0xd0U, 0x60U, // 0x61U, 0x16U, 0x1aU, 0xe1U, 0x0bU, 0x59U, 0x4fU, 0x09U, 0xe2U, 0x6aU, 0x7eU, 0x90U, 0x2eU, 0xcbU, 0xd0U, 0x60U,
0x06U, 0x91U // 0x06U, 0x91U
) // )
val result = ChaCha20Poly1305Pure.encrypt(key, nonce, message, additionalData) // val result = ChaCha20Poly1305Pure.encrypt(key, nonce, message, additionalData)
result.hexColumsPrint() // result.hexColumsPrint()
assertTrue { // assertTrue {
result.contentEquals(expected) // result.contentEquals(expected)
} // }
//
} // }
//
//
} //}

View File

@ -79,8 +79,6 @@ class XChaCha20Poly1305Test {
val expected = ubyteArrayOf( val expected = ubyteArrayOf(
0xbdU, 0x3bU, 0x8aU, 0xd7U, 0xa1U, 0x9dU, 0xe8U, 0xc4U, 0x55U, 0xbdU, 0x3bU, 0x8aU, 0xd7U, 0xa1U, 0x9dU, 0xe8U, 0xc4U, 0x55U,
0x84U, 0x6fU, 0xfcU, 0x75U, 0x31U, 0xbfU, 0x0cU, 0x2dU 0x84U, 0x6fU, 0xfcU, 0x75U, 0x31U, 0xbfU, 0x0cU, 0x2dU
// 0x17U, 0x4bU, 0x0aU, 0xb4U, 0x63U, 0x42U, 0xcbU, 0x76U,
// 0xf9U, 0xf8U, 0x9bU, 0x40U, 0xbfU, 0xdcU, 0x46U, 0x67U,
) )
val result = XChaCha20Poly1305Pure.encrypt(key, nonce, message, additionalData) val result = XChaCha20Poly1305Pure.encrypt(key, nonce, message, additionalData)
result.contentEquals(expected) result.contentEquals(expected)

View File

@ -1,5 +1,7 @@
package com.ionspin.kotlin.crypto.symmetric package com.ionspin.kotlin.crypto.symmetric
import com.ionspin.kotlin.crypto.symmetric.LatinDancesCommon.littleEndian
import com.ionspin.kotlin.crypto.symmetric.LatinDancesCommon.littleEndianInverted
import com.ionspin.kotlin.crypto.util.fromLittleEndianToUInt import com.ionspin.kotlin.crypto.util.fromLittleEndianToUInt
import com.ionspin.kotlin.crypto.util.hexStringToUByteArray import com.ionspin.kotlin.crypto.util.hexStringToUByteArray
import com.ionspin.kotlin.crypto.util.rotateLeft import com.ionspin.kotlin.crypto.util.rotateLeft