Skip to content

Commit

Permalink
WIP: Stub in API
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm committed Jan 8, 2025
1 parent 62df3c0 commit 73af67e
Show file tree
Hide file tree
Showing 11 changed files with 432 additions and 0 deletions.
1 change: 1 addition & 0 deletions library/blake2/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
35 changes: 35 additions & 0 deletions library/blake2/api/blake2.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
public final class org/kotlincrypto/hash/blake2/BLAKE2 {
public static final fun b_160 ()Lorg/kotlincrypto/hash/blake2/BLAKE2b;
public static final fun b_256 ()Lorg/kotlincrypto/hash/blake2/BLAKE2b;
public static final fun b_384 ()Lorg/kotlincrypto/hash/blake2/BLAKE2b;
public static final fun b_512 ()Lorg/kotlincrypto/hash/blake2/BLAKE2b;
public static final fun s_128 ()Lorg/kotlincrypto/hash/blake2/BLAKE2s;
public static final fun s_160 ()Lorg/kotlincrypto/hash/blake2/BLAKE2s;
public static final fun s_224 ()Lorg/kotlincrypto/hash/blake2/BLAKE2s;
public static final fun s_256 ()Lorg/kotlincrypto/hash/blake2/BLAKE2s;
}

public abstract class org/kotlincrypto/hash/blake2/BLAKE2Digest : org/kotlincrypto/core/digest/Digest {
protected static final field Companion Lorg/kotlincrypto/hash/blake2/BLAKE2Digest$Companion;
public synthetic fun <init> (IILkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Lorg/kotlincrypto/hash/blake2/BLAKE2Digest;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public abstract fun copy ()Lorg/kotlincrypto/hash/blake2/BLAKE2Digest;
}

protected final class org/kotlincrypto/hash/blake2/BLAKE2Digest$Companion {
}

public final class org/kotlincrypto/hash/blake2/BLAKE2b : org/kotlincrypto/hash/blake2/BLAKE2Digest {
public fun <init> (I)V
public synthetic fun copy ()Ljava/lang/Object;
public synthetic fun copy ()Lorg/kotlincrypto/hash/blake2/BLAKE2Digest;
public fun copy ()Lorg/kotlincrypto/hash/blake2/BLAKE2b;
}

public final class org/kotlincrypto/hash/blake2/BLAKE2s : org/kotlincrypto/hash/blake2/BLAKE2Digest {
public fun <init> (I)V
public synthetic fun copy ()Ljava/lang/Object;
public synthetic fun copy ()Lorg/kotlincrypto/hash/blake2/BLAKE2Digest;
public fun copy ()Lorg/kotlincrypto/hash/blake2/BLAKE2s;
}

37 changes: 37 additions & 0 deletions library/blake2/api/blake2.klib.api
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Klib ABI Dump
// Targets: [androidNativeArm32, androidNativeArm64, androidNativeX64, androidNativeX86, iosArm64, iosSimulatorArm64, iosX64, js, linuxArm64, linuxX64, macosArm64, macosX64, mingwX64, tvosArm64, tvosSimulatorArm64, tvosX64, wasmJs, wasmWasi, watchosArm32, watchosArm64, watchosDeviceArm64, watchosSimulatorArm64, watchosX64]
// Rendering settings:
// - Signature version: 2
// - Show manifest properties: true
// - Show declarations: true

// Library unique name: <org.kotlincrypto.hash:blake2>
final class org.kotlincrypto.hash.blake2/BLAKE2b : org.kotlincrypto.hash.blake2/BLAKE2Digest { // org.kotlincrypto.hash.blake2/BLAKE2b|null[0]
constructor <init>(kotlin/Int) // org.kotlincrypto.hash.blake2/BLAKE2b.<init>|<init>(kotlin.Int){}[0]

final fun copy(): org.kotlincrypto.hash.blake2/BLAKE2b // org.kotlincrypto.hash.blake2/BLAKE2b.copy|copy(){}[0]
}

final class org.kotlincrypto.hash.blake2/BLAKE2s : org.kotlincrypto.hash.blake2/BLAKE2Digest { // org.kotlincrypto.hash.blake2/BLAKE2s|null[0]
constructor <init>(kotlin/Int) // org.kotlincrypto.hash.blake2/BLAKE2s.<init>|<init>(kotlin.Int){}[0]

final fun copy(): org.kotlincrypto.hash.blake2/BLAKE2s // org.kotlincrypto.hash.blake2/BLAKE2s.copy|copy(){}[0]
}

sealed class org.kotlincrypto.hash.blake2/BLAKE2Digest : org.kotlincrypto.core.digest/Digest { // org.kotlincrypto.hash.blake2/BLAKE2Digest|null[0]
constructor <init>(kotlin/Int, kotlin/Int) // org.kotlincrypto.hash.blake2/BLAKE2Digest.<init>|<init>(kotlin.Int;kotlin.Int){}[0]
constructor <init>(org.kotlincrypto.hash.blake2/BLAKE2Digest) // org.kotlincrypto.hash.blake2/BLAKE2Digest.<init>|<init>(org.kotlincrypto.hash.blake2.BLAKE2Digest){}[0]

abstract fun copy(): org.kotlincrypto.hash.blake2/BLAKE2Digest // org.kotlincrypto.hash.blake2/BLAKE2Digest.copy|copy(){}[0]

final object Companion // org.kotlincrypto.hash.blake2/BLAKE2Digest.Companion|null[0]
}

final fun org.kotlincrypto.hash.blake2/BLAKE2b_160(): org.kotlincrypto.hash.blake2/BLAKE2b // org.kotlincrypto.hash.blake2/BLAKE2b_160|BLAKE2b_160(){}[0]
final fun org.kotlincrypto.hash.blake2/BLAKE2b_256(): org.kotlincrypto.hash.blake2/BLAKE2b // org.kotlincrypto.hash.blake2/BLAKE2b_256|BLAKE2b_256(){}[0]
final fun org.kotlincrypto.hash.blake2/BLAKE2b_384(): org.kotlincrypto.hash.blake2/BLAKE2b // org.kotlincrypto.hash.blake2/BLAKE2b_384|BLAKE2b_384(){}[0]
final fun org.kotlincrypto.hash.blake2/BLAKE2b_512(): org.kotlincrypto.hash.blake2/BLAKE2b // org.kotlincrypto.hash.blake2/BLAKE2b_512|BLAKE2b_512(){}[0]
final fun org.kotlincrypto.hash.blake2/BLAKE2s_128(): org.kotlincrypto.hash.blake2/BLAKE2s // org.kotlincrypto.hash.blake2/BLAKE2s_128|BLAKE2s_128(){}[0]
final fun org.kotlincrypto.hash.blake2/BLAKE2s_160(): org.kotlincrypto.hash.blake2/BLAKE2s // org.kotlincrypto.hash.blake2/BLAKE2s_160|BLAKE2s_160(){}[0]
final fun org.kotlincrypto.hash.blake2/BLAKE2s_224(): org.kotlincrypto.hash.blake2/BLAKE2s // org.kotlincrypto.hash.blake2/BLAKE2s_224|BLAKE2s_224(){}[0]
final fun org.kotlincrypto.hash.blake2/BLAKE2s_256(): org.kotlincrypto.hash.blake2/BLAKE2s // org.kotlincrypto.hash.blake2/BLAKE2s_256|BLAKE2s_256(){}[0]
36 changes: 36 additions & 0 deletions library/blake2/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright (c) 2025 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
plugins {
id("configuration")
id("bom-include")
}

kmpConfiguration {
configureShared(java9ModuleName = "org.kotlincrypto.hash.blake2", publish = true) {
common {
sourceSetMain {
dependencies {
api(libs.kotlincrypto.core.digest)
}
}
sourceSetTest {
dependencies {
implementation(project(":tools:testing"))
}
}
}
}
}
16 changes: 16 additions & 0 deletions library/blake2/gradle.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Copyright (c) 2025 Matthew Nelson
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
POM_ARTIFACT_ID=blake2
POM_NAME=KotlinCrypto BLAKE2
POM_DESCRIPTION=BLAKE2
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright (c) 2025 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
@file:JvmName("BLAKE2")
@file:Suppress("FunctionName")

package org.kotlincrypto.hash.blake2

import kotlin.jvm.JvmName

/**
* BLAKE2b-160
* */
@JvmName("b_160")
public fun BLAKE2b_160(): BLAKE2b = BLAKE2b(bitStrength = 160)

/**
* BLAKE2b-256
* */
@JvmName("b_256")
public fun BLAKE2b_256(): BLAKE2b = BLAKE2b(bitStrength = 256)

/**
* BLAKE2b-384
* */
@JvmName("b_384")
public fun BLAKE2b_384(): BLAKE2b = BLAKE2b(bitStrength = 384)

/**
* BLAKE2b-512
* */
@JvmName("b_512")
public fun BLAKE2b_512(): BLAKE2b = BLAKE2b(bitStrength = 512)

/**
* BLAKE2s-128
* */
@JvmName("s_128")
public fun BLAKE2s_128(): BLAKE2s = BLAKE2s(bitStrength = 128)

/**
* BLAKE2s-160
* */
@JvmName("s_160")
public fun BLAKE2s_160(): BLAKE2s = BLAKE2s(bitStrength = 160)

/**
* BLAKE2s-224
* */
@JvmName("s_224")
public fun BLAKE2s_224(): BLAKE2s = BLAKE2s(bitStrength = 224)

/**
* BLAKE2s-256
* */
@JvmName("s_256")
public fun BLAKE2s_256(): BLAKE2s = BLAKE2s(bitStrength = 256)
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* Copyright (c) 2025 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
@file:Suppress("UnnecessaryOptInAnnotation")

package org.kotlincrypto.hash.blake2

import org.kotlincrypto.core.digest.Digest
import kotlin.jvm.JvmStatic

/**
* Core abstraction for:
* - BLAKE2b
* - BLAKE2s
*
* https://datatracker.ietf.org/doc/rfc7693/
* */
public sealed class BLAKE2Digest: Digest {

@Throws(IllegalArgumentException::class)
protected constructor(
blockSize: Int,
bitStrength: Int,
): super(
algorithm = algorithmFrom(blockSize, bitStrength),
blockSize = blockSize,
digestLength = bitStrength / 8,
) {
// s: 64 * 4 = 256
// b: 128 * 4 = 512
val max = bitStrength * 4
require(bitStrength <= max) { "bitStrength must be less than or equal to $max" }
require(bitStrength >= 8) { "bitStrength must be greater than or equal to 8" }
require(bitStrength % 8 == 0) { "bitStrength must be a factor of 8" }
}

protected constructor(other: BLAKE2Digest): super(other)

public abstract override fun copy(): BLAKE2Digest

protected companion object {
internal const val BLOCK_SIZE_S = 64
internal const val BLOCK_SIZE_B = 128

internal val SIGMA = run {
val s0 =
byteArrayOf( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
val s1 =
byteArrayOf(14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3)

arrayOf(
s0,
s1,
byteArrayOf(11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4),
byteArrayOf( 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8),
byteArrayOf( 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13),
byteArrayOf( 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9),
byteArrayOf(12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11),
byteArrayOf(13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10),
byteArrayOf( 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5),
byteArrayOf(10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0),

// Extra rounds for BLAKE2b
s0,
s1,
)
}

@JvmStatic
@Throws(IllegalArgumentException::class)
private fun algorithmFrom(blockSize: Int, bitStrength: Int): String {
// TODO: xOfMode
// val mode = if (digestLength == 0) "x" else ""

val variant = when (blockSize) {
BLOCK_SIZE_S -> 's'
BLOCK_SIZE_B -> 'b'
else -> throw IllegalArgumentException("blockSize must be $BLOCK_SIZE_S or $BLOCK_SIZE_B")
}

return "BLAKE2$variant-$bitStrength"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) 2025 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.kotlincrypto.hash.blake2

/**
* BLAKE2b implementation
*
* https://datatracker.ietf.org/doc/rfc7693/
*
* @see [BLAKE2b_160]
* @see [BLAKE2b_256]
* @see [BLAKE2b_384]
* @see [BLAKE2b_512]
* */
public class BLAKE2b: BLAKE2Digest {

/**
* Primary constructor for creating a new [BLAKE2b] instance
*
* @throws [IllegalArgumentException] when:
* - [bitStrength] is less than 8
* - [bitStrength] is greater than 512
* - [bitStrength] is not a factor of 8
* */
@Throws(IllegalArgumentException::class)
public constructor(bitStrength: Int): super(BLOCK_SIZE_B, bitStrength) {
TODO("Not yet implemented")
}

private constructor(other: BLAKE2b): super(other) {
TODO("Not yet implemented")
}

public override fun copy(): BLAKE2b = BLAKE2b(this)

protected override fun compressProtected(input: ByteArray, offset: Int) {
TODO("Not yet implemented")
}

protected override fun digestProtected(buffer: ByteArray, offset: Int): ByteArray {
TODO("Not yet implemented")
}

protected override fun resetProtected() {
TODO("Not yet implemented")
}

private companion object {
private val IV = longArrayOf(
7640891576956012808, -4942790177534073029, 4354685564936845355, -6534734903238641935,
5840696475078001361, -7276294671716946913, 2270897969802886507, 6620516959819538809,
)
}
}
Loading

0 comments on commit 73af67e

Please sign in to comment.