implement Blake2b and Blake2s

This commit is contained in:
2025-12-02 19:34:26 +01:00
parent 3ff0ad1b91
commit 85ab748c07
6 changed files with 389 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
package com.infendro.hash.blake2
import com.infendro.bytearray.ByteOrder
import com.infendro.bytearray.toUByteArray
import com.infendro.bytearray.toULongArray
import com.infendro.hash.HashFunction
class Blake2b(
override val bytes: Int,
) : HashFunction() {
init {
if (bytes !in 1..64) throw IllegalArgumentException()
}
override val block: Int
get() = 128
private val initial = ulongArrayOf(
0x6a09e667f3bcc908UL, 0xbb67ae8584caa73bUL,
0x3c6ef372fe94f82bUL, 0xa54ff53a5f1d36f1UL,
0x510e527fade682d1UL, 0x9b05688c2b3e6c1fUL,
0x1f83d9abfb41bd6bUL, 0x5be0cd19137e2179UL,
)
private val sigma = arrayOf(
intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
intArrayOf(14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3),
intArrayOf(11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4),
intArrayOf(7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8),
intArrayOf(9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13),
intArrayOf(2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9),
intArrayOf(12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11),
intArrayOf(13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10),
intArrayOf(6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5),
intArrayOf(10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0),
)
override fun hash(value: UByteArray): UByteArray {
val h = initial.copyOf()
h[0] = h[0] xor (0x01010000UL or bytes.toULong())
var t = 0UL
val l = value.size.toULong()
// padding
val paddedValue = buildList {
addAll(value)
if (isEmpty()) add(0x00U)
while (size % 128 != 0) {
add(0x00U)
}
}.toUByteArray()
// compress
val blocks = paddedValue
.chunked(128) { it.toUByteArray() }
for ((i, block) in blocks.withIndex()) {
t += minOf(l - t, 128UL)
val m = block.toULongArray()
compress(h, m, t, i == blocks.lastIndex)
}
return h.toUByteArray(ByteOrder.LITTLE_ENDIAN).sliceArray(0..<bytes)
}
private fun compress(
h: ULongArray,
m: ULongArray,
t: ULong,
last: Boolean,
) {
// initialization
val v = ULongArray(16)
for (i in 0..<8) v[i] = h[i]
for (i in 0..<8) v[i + 8] = initial[i]
v[12] = v[12] xor t
if (last) v[14] = v[14].inv()
// rounds
for (r in 0..<12) {
val s = sigma[r % 10]
// column
mix(v, 0, 4, 8, 12, m[s[0]], m[s[1]])
mix(v, 1, 5, 9, 13, m[s[2]], m[s[3]])
mix(v, 2, 6, 10, 14, m[s[4]], m[s[5]])
mix(v, 3, 7, 11, 15, m[s[6]], m[s[7]])
// diagonal
mix(v, 0, 5, 10, 15, m[s[8]], m[s[9]])
mix(v, 1, 6, 11, 12, m[s[10]], m[s[11]])
mix(v, 2, 7, 8, 13, m[s[12]], m[s[13]])
mix(v, 3, 4, 9, 14, m[s[14]], m[s[15]])
}
// finalization
for (i in 0..7) {
h[i] = h[i] xor v[i] xor v[i + 8]
}
}
private fun mix(
v: ULongArray,
a: Int,
b: Int,
c: Int,
d: Int,
x: ULong,
y: ULong,
) {
v[a] += v[b] + x
v[d] = (v[d] xor v[a]).rotateRight(32)
v[c] += v[d]
v[b] = (v[b] xor v[c]).rotateRight(24)
v[a] += v[b] + y
v[d] = (v[d] xor v[a]).rotateRight(16)
v[c] += v[d]
v[b] = (v[b] xor v[c]).rotateRight(63)
}
}

View File

@@ -0,0 +1,121 @@
package com.infendro.hash.blake2
import com.infendro.bytearray.ByteOrder.LITTLE_ENDIAN
import com.infendro.bytearray.toUByteArray
import com.infendro.bytearray.toUIntArray
import com.infendro.hash.HashFunction
class Blake2s(
override val bytes: Int,
) : HashFunction() {
init {
if (bytes !in 1..32) throw IllegalArgumentException()
}
override val block: Int
get() = 64
private val initial = uintArrayOf(
0x6a09e667U, 0xbb67ae85U,
0x3c6ef372U, 0xa54ff53aU,
0x510e527fU, 0x9b05688cU,
0x1f83d9abU, 0x5be0cd19U,
)
private val sigma = arrayOf(
intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15),
intArrayOf(14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3),
intArrayOf(11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4),
intArrayOf(7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8),
intArrayOf(9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13),
intArrayOf(2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9),
intArrayOf(12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11),
intArrayOf(13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10),
intArrayOf(6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5),
intArrayOf(10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0),
)
override fun hash(value: UByteArray): UByteArray {
val h = initial.copyOf()
h[0] = h[0] xor (0x01010000U or bytes.toUInt())
var t = 0U
val l = value.size.toULong()
// padding
val paddedValue = buildList {
addAll(value)
if (isEmpty()) add(0x00U)
while (size % 64 != 0) {
add(0x00U)
}
}.toUByteArray()
// compress
val blocks = paddedValue
.chunked(64) { it.toUByteArray() }
for ((i, block) in blocks.withIndex()) {
t += minOf(l - t, 64U).toUInt()
val m = block.toUIntArray()
compress(h, m, t, i == blocks.lastIndex)
}
return h.toUByteArray(LITTLE_ENDIAN).sliceArray(0..<bytes)
}
private fun compress(
h: UIntArray,
m: UIntArray,
t: UInt,
last: Boolean,
) {
// initialization
val v = UIntArray(16)
for (i in 0..<8) v[i] = h[i]
for (i in 0..<8) v[i + 8] = initial[i]
v[12] = v[12] xor t
if (last) v[14] = v[14].inv()
// rounds
for (r in 0..<10) {
val s = sigma[r]
// column
mix(v, 0, 4, 8, 12, m[s[0]], m[s[1]])
mix(v, 1, 5, 9, 13, m[s[2]], m[s[3]])
mix(v, 2, 6, 10, 14, m[s[4]], m[s[5]])
mix(v, 3, 7, 11, 15, m[s[6]], m[s[7]])
// diagonal
mix(v, 0, 5, 10, 15, m[s[8]], m[s[9]])
mix(v, 1, 6, 11, 12, m[s[10]], m[s[11]])
mix(v, 2, 7, 8, 13, m[s[12]], m[s[13]])
mix(v, 3, 4, 9, 14, m[s[14]], m[s[15]])
}
// finalization
for (i in 0..7) {
h[i] = h[i] xor v[i] xor v[i + 8]
}
}
private fun mix(
v: UIntArray,
a: Int,
b: Int,
c: Int,
d: Int,
x: UInt,
y: UInt,
) {
v[a] += v[b] + x
v[d] = (v[d] xor v[a]).rotateRight(16)
v[c] += v[d]
v[b] = (v[b] xor v[c]).rotateRight(12)
v[a] += v[b] + y
v[d] = (v[d] xor v[a]).rotateRight(8)
v[c] += v[d]
v[b] = (v[b] xor v[c]).rotateRight(7)
}
}

View File

@@ -0,0 +1,38 @@
package com.infendro.hash.blake2
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `Blake2b-384 Test` {
val function = Blake2b(48)
@Test
fun `bytes and block`() {
assertEquals(48, function.bytes)
assertEquals(384, function.bits)
assertEquals(128, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0xb3U, 0x28U, 0x11U, 0x42U,
0x33U, 0x77U, 0xf5U, 0x2dU,
0x78U, 0x62U, 0x28U, 0x6eU,
0xe1U, 0xa7U, 0x2eU, 0xe5U,
0x40U, 0x52U, 0x43U, 0x80U,
0xfdU, 0xa1U, 0x72U, 0x4aU,
0x6fU, 0x25U, 0xd7U, 0x97U,
0x8cU, 0x6fU, 0xd3U, 0x24U,
0x4aU, 0x6cU, 0xafU, 0x04U,
0x98U, 0x81U, 0x26U, 0x73U,
0xc5U, 0xe0U, 0x5eU, 0xf5U,
0x83U, 0x82U, 0x51U, 0x00U,
)
val actual = function.hash(
UByteArray(0)
)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,42 @@
package com.infendro.hash.blake2
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `Blake2b-512 Test` {
val function = Blake2b(64)
@Test
fun `bytes and block`() {
assertEquals(64, function.bytes)
assertEquals(512, function.bits)
assertEquals(128, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0x78U, 0x6aU, 0x02U, 0xf7U,
0x42U, 0x01U, 0x59U, 0x03U,
0xc6U, 0xc6U, 0xfdU, 0x85U,
0x25U, 0x52U, 0xd2U, 0x72U,
0x91U, 0x2fU, 0x47U, 0x40U,
0xe1U, 0x58U, 0x47U, 0x61U,
0x8aU, 0x86U, 0xe2U, 0x17U,
0xf7U, 0x1fU, 0x54U, 0x19U,
0xd2U, 0x5eU, 0x10U, 0x31U,
0xafU, 0xeeU, 0x58U, 0x53U,
0x13U, 0x89U, 0x64U, 0x44U,
0x93U, 0x4eU, 0xb0U, 0x4bU,
0x90U, 0x3aU, 0x68U, 0x5bU,
0x14U, 0x48U, 0xb7U, 0x55U,
0xd5U, 0x6fU, 0x70U, 0x1aU,
0xfeU, 0x9bU, 0xe2U, 0xceU,
)
val actual = function.hash(
UByteArray(0)
)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,33 @@
package com.infendro.hash.blake2
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `Blake2s-224 Test` {
val function = Blake2s(28)
@Test
fun `bytes and block`() {
assertEquals(28, function.bytes)
assertEquals(224, function.bits)
assertEquals(64, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0x1fU, 0xa1U, 0x29U, 0x1eU,
0x65U, 0x24U, 0x8bU, 0x37U,
0xb3U, 0x43U, 0x34U, 0x75U,
0xb2U, 0xa0U, 0xddU, 0x63U,
0xd5U, 0x4aU, 0x11U, 0xecU,
0xc4U, 0xe3U, 0xe0U, 0x34U,
0xe7U, 0xbcU, 0x1eU, 0xf4U,
)
val actual = function.hash(
UByteArray(0)
)
assertContentEquals(expected, actual)
}
}

View File

@@ -0,0 +1,34 @@
package com.infendro.hash.blake2
import kotlin.test.Test
import kotlin.test.assertContentEquals
import kotlin.test.assertEquals
class `Blake2s-256 Test` {
val function = Blake2s(32)
@Test
fun `bytes and block`() {
assertEquals(32, function.bytes)
assertEquals(256, function.bits)
assertEquals(64, function.block)
}
@Test
fun `empty bytearray`() {
val expected = ubyteArrayOf(
0x69U, 0x21U, 0x7aU, 0x30U,
0x79U, 0x90U, 0x80U, 0x94U,
0xe1U, 0x11U, 0x21U, 0xd0U,
0x42U, 0x35U, 0x4aU, 0x7cU,
0x1fU, 0x55U, 0xb6U, 0x48U,
0x2cU, 0xa1U, 0xa5U, 0x1eU,
0x1bU, 0x25U, 0x0dU, 0xfdU,
0x1eU, 0xd0U, 0xeeU, 0xf9U,
)
val actual = function.hash(
UByteArray(0)
)
assertContentEquals(expected, actual)
}
}