Skip to content

Commit

Permalink
Update BLAKE2Mac API to utilize BLAKE2b/s digest constructors (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
05nelsonm authored Jan 14, 2025
1 parent f715c3d commit aa72768
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 11 deletions.
6 changes: 5 additions & 1 deletion library/blake2/api/blake2.api
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
public abstract class org/kotlincrypto/macs/blake2/BLAKE2Mac : org/kotlincrypto/core/mac/Mac {
public synthetic fun <init> (Lorg/kotlincrypto/macs/blake2/BLAKE2Mac$Engine;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> (Lorg/kotlincrypto/macs/blake2/BLAKE2Mac;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> ([BI[BLorg/kotlincrypto/hash/blake2/BLAKE2Digest$KeyedHashFactory;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public synthetic fun <init> ([BI[BLorg/kotlincrypto/macs/blake2/BLAKE2Mac$DigestFactory;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
public abstract fun copy ()Lorg/kotlincrypto/macs/blake2/BLAKE2Mac;
}

protected abstract interface class org/kotlincrypto/macs/blake2/BLAKE2Mac$DigestFactory {
public abstract fun newInstance (II[B[B)Lorg/kotlincrypto/hash/blake2/BLAKE2Digest;
}

public final class org/kotlincrypto/macs/blake2/BLAKE2b : org/kotlincrypto/macs/blake2/BLAKE2Mac {
public fun <init> ([BI)V
public fun <init> ([BI[B)V
Expand Down
6 changes: 5 additions & 1 deletion library/blake2/api/blake2.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ final class org.kotlincrypto.macs.blake2/BLAKE2s : org.kotlincrypto.macs.blake2/
}

sealed class org.kotlincrypto.macs.blake2/BLAKE2Mac : org.kotlincrypto.core.mac/Mac { // org.kotlincrypto.macs.blake2/BLAKE2Mac|null[0]
constructor <init>(kotlin/ByteArray, kotlin/Int, kotlin/ByteArray?, org.kotlincrypto.hash.blake2/BLAKE2Digest.KeyedHashFactory<*>) // org.kotlincrypto.macs.blake2/BLAKE2Mac.<init>|<init>(kotlin.ByteArray;kotlin.Int;kotlin.ByteArray?;org.kotlincrypto.hash.blake2.BLAKE2Digest.KeyedHashFactory<*>){}[0]
constructor <init>(kotlin/ByteArray, kotlin/Int, kotlin/ByteArray?, org.kotlincrypto.macs.blake2/BLAKE2Mac.DigestFactory) // org.kotlincrypto.macs.blake2/BLAKE2Mac.<init>|<init>(kotlin.ByteArray;kotlin.Int;kotlin.ByteArray?;org.kotlincrypto.macs.blake2.BLAKE2Mac.DigestFactory){}[0]
constructor <init>(org.kotlincrypto.macs.blake2/BLAKE2Mac) // org.kotlincrypto.macs.blake2/BLAKE2Mac.<init>|<init>(org.kotlincrypto.macs.blake2.BLAKE2Mac){}[0]

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

abstract fun interface DigestFactory { // org.kotlincrypto.macs.blake2/BLAKE2Mac.DigestFactory|null[0]
abstract fun newInstance(kotlin/Int, kotlin/Int, kotlin/ByteArray?, kotlin/ByteArray?): org.kotlincrypto.hash.blake2/BLAKE2Digest // org.kotlincrypto.macs.blake2/BLAKE2Mac.DigestFactory.newInstance|newInstance(kotlin.Int;kotlin.Int;kotlin.ByteArray?;kotlin.ByteArray?){}[0]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.kotlincrypto.macs.blake2

import org.kotlincrypto.core.Algorithm
import org.kotlincrypto.core.InternalKotlinCryptoApi
import org.kotlincrypto.core.mac.Mac
import org.kotlincrypto.hash.blake2.BLAKE2Digest

Expand All @@ -34,34 +33,44 @@ public sealed class BLAKE2Mac: Mac {
key: ByteArray,
bitStrength: Int,
personalization: ByteArray?,
factory: BLAKE2Digest.KeyedHashFactory<*>,
factory: DigestFactory,
): this(Engine(key, bitStrength, personalization, factory))

protected constructor(other: BLAKE2Mac): super(other)

protected fun interface DigestFactory {

@Throws(IllegalArgumentException::class)
public fun newInstance(
bitStrength: Int,
keyLength: Int,
salt: ByteArray?,
personalization: ByteArray?,
): BLAKE2Digest
}

private constructor(engine: Engine): super(engine.algorithm(), engine)

public abstract override fun copy(): BLAKE2Mac

@OptIn(InternalKotlinCryptoApi::class)
private class Engine: Mac.Engine, Algorithm {

private val bitStrength: Int
private val keyBlock: ByteArray
private val personalization: ByteArray?
private val factory: BLAKE2Digest.KeyedHashFactory<*>
private val factory: DigestFactory
private var digest: BLAKE2Digest

@Throws(IllegalArgumentException::class)
constructor(
key: ByteArray,
bitStrength: Int,
personalization: ByteArray?,
factory: BLAKE2Digest.KeyedHashFactory<*>,
factory: DigestFactory,
): super(key) {
this.bitStrength = bitStrength
this.personalization = personalization?.copyOf()
this.digest = factory.keyedHashInstance(
this.digest = factory.newInstance(
bitStrength = bitStrength,
keyLength = key.size,
salt = null,
Expand Down Expand Up @@ -95,7 +104,7 @@ public sealed class BLAKE2Mac: Mac {

override fun reset(newKey: ByteArray) {
val oldDigest = digest
this.digest = factory.keyedHashInstance(
this.digest = factory.newInstance(
bitStrength = bitStrength,
keyLength = newKey.size,
salt = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
**/
package org.kotlincrypto.macs.blake2

import org.kotlincrypto.core.InternalKotlinCryptoApi

/**
* BLAKE2b Keyed Hashing implementation
*
Expand Down Expand Up @@ -65,10 +67,26 @@ public class BLAKE2b: BLAKE2Mac {
key = key,
bitStrength = bitStrength,
personalization = personalization,
factory = org.kotlincrypto.hash.blake2.BLAKE2b,
factory = Companion,
)

private constructor(other: BLAKE2b): super(other)

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

private companion object: DigestFactory {

@OptIn(InternalKotlinCryptoApi::class)
override fun newInstance(
bitStrength: Int,
keyLength: Int,
salt: ByteArray?,
personalization: ByteArray?,
): org.kotlincrypto.hash.blake2.BLAKE2b = org.kotlincrypto.hash.blake2.BLAKE2b(
bitStrength = bitStrength,
keyLength = keyLength,
salt = salt,
personalization = personalization,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
**/
package org.kotlincrypto.macs.blake2

import org.kotlincrypto.core.InternalKotlinCryptoApi

/**
* BLAKE2s Keyed Hashing implementation
*
Expand Down Expand Up @@ -65,10 +67,26 @@ public class BLAKE2s: BLAKE2Mac {
key = key,
bitStrength = bitStrength,
personalization = personalization,
factory = org.kotlincrypto.hash.blake2.BLAKE2s,
factory = Companion,
)

private constructor(other: BLAKE2s): super(other)

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

private companion object: DigestFactory {

@OptIn(InternalKotlinCryptoApi::class)
override fun newInstance(
bitStrength: Int,
keyLength: Int,
salt: ByteArray?,
personalization: ByteArray?,
): org.kotlincrypto.hash.blake2.BLAKE2s = org.kotlincrypto.hash.blake2.BLAKE2s(
bitStrength = bitStrength,
keyLength = keyLength,
salt = salt,
personalization = personalization,
)
}
}

0 comments on commit aa72768

Please sign in to comment.