Working chacha20poly1305

This commit is contained in:
Ugljesa Jovanovic 2020-06-20 12:10:24 +02:00 committed by Ugljesa Jovanovic
parent 8ffa354d93
commit 9456772828
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
9 changed files with 328 additions and 73 deletions

View File

@ -0,0 +1,58 @@
package com.ionspin.kotlin.crypto.authenticated
import com.ionspin.kotlin.crypto.mac.Poly1305
import com.ionspin.kotlin.crypto.symmetric.ChaCha20Pure
import com.ionspin.kotlin.crypto.symmetric.XChaCha20Pure
import com.ionspin.kotlin.crypto.util.fromLittleEndianArrayToUIntWithPosition
import com.ionspin.kotlin.crypto.util.hexColumsPrint
import com.ionspin.kotlin.crypto.util.toLittleEndianUByteArray
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 17-Jun-2020
*/
class ChaCha20Poly1305Pure {
companion object {
fun encrypt(key: UByteArray, nonce: UByteArray, message: UByteArray, additionalData: UByteArray) : UByteArray {
val state = UIntArray(16) {
when (it) {
0 -> ChaCha20Pure.sigma0_32
1 -> ChaCha20Pure.sigma1_32
2 -> ChaCha20Pure.sigma2_32
3 -> ChaCha20Pure.sigma3_32
4 -> key.fromLittleEndianArrayToUIntWithPosition(0)
5 -> key.fromLittleEndianArrayToUIntWithPosition(4)
6 -> key.fromLittleEndianArrayToUIntWithPosition(8)
7 -> key.fromLittleEndianArrayToUIntWithPosition(12)
8 -> key.fromLittleEndianArrayToUIntWithPosition(16)
9 -> key.fromLittleEndianArrayToUIntWithPosition(20)
10 -> key.fromLittleEndianArrayToUIntWithPosition(24)
11 -> key.fromLittleEndianArrayToUIntWithPosition(28)
12 -> 0U
13 -> nonce.fromLittleEndianArrayToUIntWithPosition(0)
14 -> nonce.fromLittleEndianArrayToUIntWithPosition(4)
15 -> nonce.fromLittleEndianArrayToUIntWithPosition(8)
else -> 0U
}
}
val oneTimeKey = ChaCha20Pure.hash(state).sliceArray(0 until 32)
println("Poly key:")
oneTimeKey.hexColumsPrint()
val cipherText = ChaCha20Pure.encrypt(key, nonce, message, 1U)
val additionalDataPad = UByteArray(16 - additionalData.size % 16) { 0U }
val cipherTextPad = UByteArray(16 - cipherText.size % 16) { 0U }
val macData = additionalData + additionalDataPad +
cipherText + cipherTextPad +
additionalData.size.toULong().toLittleEndianUByteArray() +
cipherText.size.toULong().toLittleEndianUByteArray()
println("Mac data")
macData.hexColumsPrint()
val tag = Poly1305.poly1305Authenticate(oneTimeKey, macData)
println("Tag:")
tag.hexColumsPrint()
return cipherText + tag
}
}
}

View File

@ -0,0 +1,27 @@
package com.ionspin.kotlin.crypto.authenticated
import com.ionspin.kotlin.bignum.Endianness
import com.ionspin.kotlin.bignum.integer.BigInteger
import com.ionspin.kotlin.crypto.symmetric.ChaCha20Pure
import com.ionspin.kotlin.crypto.symmetric.XChaCha20Pure
import com.ionspin.kotlin.crypto.util.fromLittleEndianArrayToUIntWithPosition
import com.ionspin.kotlin.crypto.util.hexColumsPrint
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 17-Jun-2020
*/
class XChaCha20Poly1305Pure {
companion object {
fun encrypt(key: UByteArray, nonce: UByteArray, message: UByteArray, additionalData: UByteArray) : UByteArray {
val oneTimeKey = XChaCha20Pure.hChacha(key, ubyteArrayOf(0U, 0U, 0U, 0U) + nonce.sliceArray(0 until 16))
// val cipherText = XChaCha20Pure.encrypt(key, nonce, message, 1U)
oneTimeKey.hexColumsPrint()
// println("ciphertext")
// cipherText.hexColumsPrint()
return ubyteArrayOf()
}
}
}

View File

@ -1,7 +0,0 @@
package com.ionspin.kotlin.crypto.authenticated
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 14-Jun-2020
*/

View File

@ -7,9 +7,9 @@ import com.ionspin.kotlin.crypto.util.hexColumsPrint
/** /**
* Created by Ugljesa Jovanovic * Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com * ugljesa.jovanovic@ionspin.com
* on 17-Jun-2020 * on 18-Jun-2020
*/ */
class XChaCha20Poly1305Pure { class Poly1305 {
companion object { companion object {
fun clampR(r: UByteArray) { fun clampR(r: UByteArray) {
r[3] = r[3] and 0b00001111U r[3] = r[3] and 0b00001111U
@ -26,12 +26,12 @@ class XChaCha20Poly1305Pure {
val P = BigInteger.fromUByteArray( val P = BigInteger.fromUByteArray(
ubyteArrayOf( ubyteArrayOf(
0x03U, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xfbU 0x03U, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xffU, 0xfbU
).toTypedArray() //TODO remove to typed array after bignum update )
) )
val powersOfTwo = Array(129) { val powersOfTwo = Array(129) {
BigInteger.ONE shl it BigInteger.ONE shl it
} }
val resultMask = (BigInteger.ONE shl 129) - 1 val resultMask = (BigInteger.ONE shl 128) - 1
//Doesn't have to be every power, just divisible by 8 //Doesn't have to be every power, just divisible by 8
val twoToThe128 = BigInteger.ONE.shl(128) val twoToThe128 = BigInteger.ONE.shl(128)
@ -40,9 +40,15 @@ class XChaCha20Poly1305Pure {
val s= UByteArray(16) { key[it + 16]} val s= UByteArray(16) { key[it + 16]}
clampR(r) clampR(r)
println("P: ${P.toString(16)}") println("P: ${P.toString(16)}")
println("R:")
r.hexColumsPrint()
println("S:")
s.hexColumsPrint()
var accumulator = BigInteger.ZERO var accumulator = BigInteger.ZERO
val rAsBigInt = BigInteger.fromUByteArray(r, Endianness.LITTLE) val rAsBigInt = BigInteger.fromUByteArray(r, Endianness.LITTLE)
println("R: ${rAsBigInt.toString(16)}")
val sAsBigInt = BigInteger.fromUByteArray(s, Endianness.LITTLE) val sAsBigInt = BigInteger.fromUByteArray(s, Endianness.LITTLE)
println("S: ${sAsBigInt.toString(16)}")
val blocks = message.size / 16 val blocks = message.size / 16
val remainder = message.size % 16 val remainder = message.size % 16
@ -58,20 +64,22 @@ class XChaCha20Poly1305Pure {
accumulator %= P accumulator %= P
println("Accumlator: ${accumulator.toString(16)}") println("Accumlator: ${accumulator.toString(16)}")
} }
if (remainder != 0) {
val slice = message.sliceArray(blocks * 16 until blocks * 16 + remainder) val slice = message.sliceArray(blocks * 16 until blocks * 16 + remainder)
val blockAsInt = BigInteger.fromUByteArray(slice, Endianness.LITTLE) + powersOfTwo[remainder * 8] val blockAsInt = BigInteger.fromUByteArray(slice, Endianness.LITTLE) + powersOfTwo[remainder * 8]
println("blockAsInt: ${blockAsInt.toString(16)}") println("blockAsInt: ${blockAsInt.toString(16)}")
accumulator += blockAsInt accumulator += blockAsInt
println("Accumlator: ${accumulator.toString(16)}") println("Accumlator: ${accumulator.toString(16)}")
accumulator *= rAsBigInt accumulator *= rAsBigInt
println("Accumlator: ${accumulator.toString(16)}") println("Accumlator: ${accumulator.toString(16)}")
accumulator %= P accumulator %= P
println("Accumlator: ${accumulator.toString(16)}") println("Accumlator: ${accumulator.toString(16)}")
}
println("Result mask: ${resultMask.toString(2)}") println("Result mask: ${resultMask.toString(16)}")
accumulator += sAsBigInt accumulator += sAsBigInt
println("Before mask: ${accumulator.toString(16)}")
accumulator = accumulator and resultMask accumulator = accumulator and resultMask
println("Accumlator: ${accumulator.toString(16)}") println("Accumlator: ${accumulator.toString(16)}")
val result = accumulator.toUByteArray(Endianness.BIG) val result = accumulator.toUByteArray(Endianness.BIG)

View File

@ -84,6 +84,9 @@ internal class XChaCha20Pure {
for (i in 0 until blocks) { for (i in 0 until blocks) {
ChaCha20Pure.hash(state).xorWithPositionsAndInsertIntoArray(0, 64, message, i * 64, ciphertext, i * 64) ChaCha20Pure.hash(state).xorWithPositionsAndInsertIntoArray(0, 64, message, i * 64, ciphertext, i * 64)
state[12] += 1U state[12] += 1U
if (state[12] == 0U) {
state[13] += 1U
}
} }
ChaCha20Pure.hash(state).xorWithPositionsAndInsertIntoArray( ChaCha20Pure.hash(state).xorWithPositionsAndInsertIntoArray(
0, remainder, 0, remainder,

View File

@ -0,0 +1,56 @@
package com.ionspin.kotlin.crypto.authenticated
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.hexColumsPrint
import kotlin.test.Test
import kotlin.test.assertTrue
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 17-Jun-2020
*/
class ChaCha20Poly1305Test {
@Test
fun chaCha20Poly1305() {
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()
val additionalData = ubyteArrayOf(
0x50U, 0x51U, 0x52U, 0x53U, 0xc0U, 0xc1U, 0xc2U, 0xc3U, 0xc4U, 0xc5U, 0xc6U, 0xc7U
)
val key = ubyteArrayOf(
0x80U, 0x81U, 0x82U, 0x83U, 0x84U, 0x85U, 0x86U, 0x87U,
0x88U, 0x89U, 0x8aU, 0x8bU, 0x8cU, 0x8dU, 0x8eU, 0x8fU,
0x90U, 0x91U, 0x92U, 0x93U, 0x94U, 0x95U, 0x96U, 0x97U,
0x98U, 0x99U, 0x9aU, 0x9bU, 0x9cU, 0x9dU, 0x9eU, 0x9fU,
)
val nonce = ubyteArrayOf(
0x07U, 0x00U, 0x00U, 0x00U, 0x40U, 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U
)
//Ciphertext + Poly1305TAG
val expected = ubyteArrayOf(
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,
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,
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,
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,
0x06U, 0x91U
)
val result = ChaCha20Poly1305Pure.encrypt(key, nonce, message, additionalData)
result.hexColumsPrint()
assertTrue {
result.contentEquals(expected)
}
}
}

View File

@ -0,0 +1,38 @@
package com.ionspin.kotlin.crypto.authenticated
import com.ionspin.kotlin.crypto.hash.encodeToUByteArray
import kotlin.test.Test
import kotlin.test.assertTrue
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 17-Jun-2020
*/
class XChaCha20Poly1305Test {
@Test
fun xChaCha20Poly1305() {
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()
val additionalData = ubyteArrayOf(
0x50U, 0x51U, 0x52U, 0x53U, 0xc0U, 0xc1U, 0xc2U, 0xc3U, 0xc4U, 0xc5U, 0xc6U, 0xc7U
)
val key = ubyteArrayOf(
0x80U, 0x81U, 0x82U, 0x83U, 0x84U, 0x85U, 0x86U, 0x87U,
0x88U, 0x89U, 0x8aU, 0x8bU, 0x8cU, 0x8dU, 0x8eU, 0x8fU,
0x90U, 0x91U, 0x92U, 0x93U, 0x94U, 0x95U, 0x96U, 0x97U,
0x98U, 0x99U, 0x9aU, 0x9bU, 0x9cU, 0x9dU, 0x9eU, 0x9fU,
)
val nonce = ubyteArrayOf(
0x40U, 0x41U, 0x42U, 0x43U, 0x44U, 0x45U, 0x46U, 0x47U,
0x48U, 0x49U, 0x4aU, 0x4bU, 0x4cU, 0x4dU, 0x4eU, 0x4fU,
0x50U, 0x51U, 0x52U, 0x53U, 0x54U, 0x55U, 0x56U, 0x57U,
)
XChaCha20Poly1305Pure.encrypt(key, nonce, message, additionalData)
}
}

View File

@ -0,0 +1,123 @@
package com.ionspin.kotlin.crypto.mac
import kotlin.test.Test
import kotlin.test.assertTrue
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 18-Jun-2020
*/
class Poly1305Test {
/**
* From RFC7539
*/
@Test
fun testPoly1305() {
assertTrue {
val key = ubyteArrayOf(
0x85U, 0xd6U, 0xbeU, 0x78U, 0x57U, 0x55U, 0x6dU,
0x33U, 0x7fU, 0x44U, 0x52U, 0xfeU, 0x42U, 0xd5U,
0x06U, 0xa8U, 0x01U, 0x03U, 0x80U, 0x8aU, 0xfbU,
0x0dU, 0xb2U, 0xfdU, 0x4aU, 0xbfU, 0xf6U, 0xafU,
0x41U, 0x49U, 0xf5U, 0x1bU
)
val message = ubyteArrayOf(
0x43U, 0x72U, 0x79U, 0x70U, 0x74U, 0x6fU, 0x67U, 0x72U,
0x61U, 0x70U, 0x68U, 0x69U, 0x63U, 0x20U, 0x46U, 0x6fU,
0x72U, 0x75U, 0x6dU, 0x20U, 0x52U, 0x65U, 0x73U, 0x65U,
0x61U, 0x72U, 0x63U, 0x68U, 0x20U, 0x47U, 0x72U, 0x6fU,
0x75U, 0x70U
)
val expected = ubyteArrayOf(
0xA8U, 0x06U, 0x1DU, 0xC1U,
0x30U, 0x51U, 0x36U, 0xC6U,
0xC2U, 0x2BU, 0x8BU, 0xAFU,
0x0CU, 0x01U, 0x27U, 0xA9U,
)
val result =
Poly1305.poly1305Authenticate(
key,
message,
)
expected.contentEquals(result)
}
assertTrue {
val key = ubyteArrayOf(
0x85U, 0x1fU, 0xc4U, 0x0cU, 0x34U, 0x67U, 0xacU, 0x0bU,
0xe0U, 0x5cU, 0xc2U, 0x04U, 0x04U, 0xf3U, 0xf7U, 0x00U,
0x58U, 0x0bU, 0x3bU, 0x0fU, 0x94U, 0x47U, 0xbbU, 0x1eU,
0x69U, 0xd0U, 0x95U, 0xb5U, 0x92U, 0x8bU, 0x6dU, 0xbcU
)
val message = ubyteArrayOf(
0xf3U, 0xf6U
)
val expected = ubyteArrayOf(
0xf4U, 0xc6U, 0x33U, 0xc3U, 0x04U, 0x4fU, 0xc1U, 0x45U,
0xf8U, 0x4fU, 0x33U, 0x5cU, 0xb8U, 0x19U, 0x53U, 0xdeU
)
val result =
Poly1305.poly1305Authenticate(
key,
message,
)
expected.contentEquals(result)
}
assertTrue {
val key = ubyteArrayOf(
0x75U, 0xdeU, 0xaaU, 0x25U, 0xc0U, 0x9fU, 0x20U, 0x8eU,
0x1dU, 0xc4U, 0xceU, 0x6bU, 0x5cU, 0xadU, 0x3fU, 0xbfU,
0xddU, 0x3fU, 0xabU, 0x22U, 0x51U, 0xf1U, 0x1aU, 0xc7U,
0x59U, 0xf0U, 0x88U, 0x71U, 0x29U, 0xccU, 0x2eU, 0xe7U,
)
val message = ubyteArrayOf(
)
val expected = ubyteArrayOf(
0xddU, 0x3fU, 0xabU, 0x22U, 0x51U, 0xf1U, 0x1aU, 0xc7U,
0x59U, 0xf0U, 0x88U, 0x71U, 0x29U, 0xccU, 0x2eU, 0xe7U
)
val result =
Poly1305.poly1305Authenticate(
key,
message,
)
expected.contentEquals(result)
}
assertTrue {
val key = ubyteArrayOf(
0x12U, 0x97U, 0x6aU, 0x08U, 0xc4U, 0x42U, 0x6dU, 0x0cU,
0xe8U, 0xa8U, 0x24U, 0x07U, 0xc4U, 0xf4U, 0x82U, 0x07U,
0x80U, 0xf8U, 0xc2U, 0x0aU, 0xa7U, 0x12U, 0x02U, 0xd1U,
0xe2U, 0x91U, 0x79U, 0xcbU, 0xcbU, 0x55U, 0x5aU, 0x57U
)
val message = ubyteArrayOf(
0xabU, 0x08U, 0x12U, 0x72U, 0x4aU, 0x7fU, 0x1eU, 0x34U,
0x27U, 0x42U, 0xcbU, 0xedU, 0x37U, 0x4dU, 0x94U, 0xd1U,
0x36U, 0xc6U, 0xb8U, 0x79U, 0x5dU, 0x45U, 0xb3U, 0x81U,
0x98U, 0x30U, 0xf2U, 0xc0U, 0x44U, 0x91U, 0xfaU, 0xf0U,
0x99U, 0x0cU, 0x62U, 0xe4U, 0x8bU, 0x80U, 0x18U, 0xb2U,
0xc3U, 0xe4U, 0xa0U, 0xfaU, 0x31U, 0x34U, 0xcbU, 0x67U,
0xfaU, 0x83U, 0xe1U, 0x58U, 0xc9U, 0x94U, 0xd9U, 0x61U,
0xc4U, 0xcbU, 0x21U, 0x09U, 0x5cU, 0x1bU, 0xf9U,
)
val expected = ubyteArrayOf(
0x51U, 0x54U, 0xadU, 0x0dU, 0x2cU, 0xb2U, 0x6eU, 0x01U,
0x27U, 0x4fU, 0xc5U, 0x11U, 0x48U, 0x49U, 0x1fU, 0x1bU
)
val result =
Poly1305.poly1305Authenticate(
key,
message,
)
expected.contentEquals(result)
}
}
}

View File

@ -1,51 +0,0 @@
package com.ionspin.kotlin.crypto.mac
import com.ionspin.kotlin.crypto.util.hexColumsPrint
import kotlin.test.Test
import kotlin.test.assertTrue
/**
* Created by Ugljesa Jovanovic
* ugljesa.jovanovic@ionspin.com
* on 17-Jun-2020
*/
class XChaCha20Poly1305Test {
@Test
fun debugTest() {
XChaCha20Poly1305Pure.poly1305Authenticate(
UByteArray(32) { if (it < 16) { 0U } else {1U} },
UByteArray(37) { it.toUByte() },
)
}
/**
* From RFC7539
*/
@Test
fun testPoly1305() {
val key = ubyteArrayOf(
0x85U, 0xd6U, 0xbeU, 0x78U, 0x57U, 0x55U, 0x6dU, 0x33U, 0x7fU, 0x44U, 0x52U, 0xfeU, 0x42U, 0xd5U, 0x06U,
0xa8U, 0x01U, 0x03U, 0x80U, 0x8aU, 0xfbU, 0x0dU, 0xb2U, 0xfdU, 0x4aU, 0xbfU, 0xf6U, 0xafU, 0x41U, 0x49U,
0xf5U, 0x1bU
)
val message = ubyteArrayOf(
0x43U, 0x72U, 0x79U, 0x70U, 0x74U, 0x6fU, 0x67U, 0x72U, 0x61U, 0x70U, 0x68U, 0x69U, 0x63U, 0x20U, 0x46U, 0x6fU,
0x72U, 0x75U, 0x6dU, 0x20U, 0x52U, 0x65U, 0x73U, 0x65U, 0x61U, 0x72U, 0x63U, 0x68U, 0x20U, 0x47U, 0x72U, 0x6fU,
0x75U, 0x70U
)
val expected = ubyteArrayOf(
0xA8U, 0x06U, 0x1DU, 0xC1U,
0x30U, 0x51U, 0x36U, 0xC6U,
0xC2U, 0x2BU, 0x8BU, 0xAFU,
0x0CU, 0x01U, 0x27U, 0xA9U,
)
val result = XChaCha20Poly1305Pure.poly1305Authenticate(
key,
message,
)
assertTrue {
expected.contentEquals(result)
}
}
}