Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to re-key Xof<Mac> #100

Merged
merged 1 commit into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion library/xof/api/xof.api
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
public abstract interface class org/kotlincrypto/core/xof/ReKeyableXofAlgorithm : org/kotlincrypto/core/xof/XofAlgorithm {
public abstract fun reset ([B)V
}

public abstract class org/kotlincrypto/core/xof/Xof : org/kotlincrypto/core/Algorithm, org/kotlincrypto/core/Copyable, org/kotlincrypto/core/Resettable, org/kotlincrypto/core/Updatable {
public static final field Companion Lorg/kotlincrypto/core/xof/Xof$Companion;
protected final field delegate Lorg/kotlincrypto/core/xof/XofAlgorithm;
public synthetic fun <init> (Lorg/kotlincrypto/core/xof/XofAlgorithm;Lkotlin/jvm/internal/DefaultConstructorMarker;)V
protected abstract fun newReader ()Lorg/kotlincrypto/core/xof/Xof$Reader;
public final fun reader ()Lorg/kotlincrypto/core/xof/Xof$Reader;
public final fun reader (Z)Lorg/kotlincrypto/core/xof/Xof$Reader;
public static synthetic fun reader$default (Lorg/kotlincrypto/core/xof/Xof;ZILjava/lang/Object;)Lorg/kotlincrypto/core/xof/Xof$Reader;
public static final fun reset (Lorg/kotlincrypto/core/xof/Xof;[B)V
public final fun toString ()Ljava/lang/String;
public final fun use (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public final fun use (ZLkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static synthetic fun use$default (Lorg/kotlincrypto/core/xof/Xof;ZLkotlin/jvm/functions/Function1;ILjava/lang/Object;)Ljava/lang/Object;
}

public final class org/kotlincrypto/core/xof/Xof$Companion {
public final fun reset (Lorg/kotlincrypto/core/xof/Xof;[B)V
}

public abstract class org/kotlincrypto/core/xof/Xof$Reader {
public fun <init> (Lorg/kotlincrypto/core/xof/Xof;)V
public final fun bytesRead ()J
Expand Down Expand Up @@ -43,7 +55,6 @@ protected abstract class org/kotlincrypto/core/xof/XofFactory$XofDelegate : org/
protected fun <init> (Lorg/kotlincrypto/core/xof/XofFactory;Lorg/kotlincrypto/core/xof/XofAlgorithm;)V
public fun algorithm ()Ljava/lang/String;
public final fun equals (Ljava/lang/Object;)Z
protected final fun getDelegate ()Lorg/kotlincrypto/core/xof/XofAlgorithm;
public final fun hashCode ()I
protected final fun newReader ()Lorg/kotlincrypto/core/xof/Xof$Reader;
protected abstract fun newReader (Lorg/kotlincrypto/core/xof/XofAlgorithm;)Lorg/kotlincrypto/core/xof/Xof$Reader;
Expand Down
16 changes: 12 additions & 4 deletions library/xof/api/xof.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
// - Show declarations: true

// Library unique name: <org.kotlincrypto.core:xof>
abstract interface org.kotlincrypto.core.xof/ReKeyableXofAlgorithm : org.kotlincrypto.core.xof/XofAlgorithm { // org.kotlincrypto.core.xof/ReKeyableXofAlgorithm|null[0]
abstract fun reset(kotlin/ByteArray) // org.kotlincrypto.core.xof/ReKeyableXofAlgorithm.reset|reset(kotlin.ByteArray){}[0]
}

abstract interface org.kotlincrypto.core.xof/XofAlgorithm : org.kotlincrypto.core/Algorithm // org.kotlincrypto.core.xof/XofAlgorithm|null[0]

abstract class <#A: org.kotlincrypto.core.xof/XofAlgorithm> org.kotlincrypto.core.xof/XofFactory { // org.kotlincrypto.core.xof/XofFactory|null[0]
Expand All @@ -14,9 +18,6 @@ abstract class <#A: org.kotlincrypto.core.xof/XofAlgorithm> org.kotlincrypto.cor
abstract inner class XofDelegate : org.kotlincrypto.core.xof/Xof<#A>, org.kotlincrypto.core/Algorithm, org.kotlincrypto.core/Resettable, org.kotlincrypto.core/Updatable { // org.kotlincrypto.core.xof/XofFactory.XofDelegate|null[0]
constructor <init>(#A) // org.kotlincrypto.core.xof/XofFactory.XofDelegate.<init>|<init>(2:0){}[0]

final val delegate // org.kotlincrypto.core.xof/XofFactory.XofDelegate.delegate|{}delegate[0]
final fun <get-delegate>(): #A // org.kotlincrypto.core.xof/XofFactory.XofDelegate.delegate.<get-delegate>|<get-delegate>(){}[0]

abstract fun newReader(#A): org.kotlincrypto.core.xof/Xof.Reader<#A> // org.kotlincrypto.core.xof/XofFactory.XofDelegate.newReader|newReader(2:0){}[0]
final fun equals(kotlin/Any?): kotlin/Boolean // org.kotlincrypto.core.xof/XofFactory.XofDelegate.equals|equals(kotlin.Any?){}[0]
final fun hashCode(): kotlin/Int // org.kotlincrypto.core.xof/XofFactory.XofDelegate.hashCode|hashCode(){}[0]
Expand All @@ -30,7 +31,10 @@ abstract class <#A: org.kotlincrypto.core.xof/XofAlgorithm> org.kotlincrypto.cor
}

sealed class <#A: org.kotlincrypto.core.xof/XofAlgorithm> org.kotlincrypto.core.xof/Xof : org.kotlincrypto.core/Algorithm, org.kotlincrypto.core/Copyable<org.kotlincrypto.core.xof/Xof<#A>>, org.kotlincrypto.core/Resettable, org.kotlincrypto.core/Updatable { // org.kotlincrypto.core.xof/Xof|null[0]
constructor <init>() // org.kotlincrypto.core.xof/Xof.<init>|<init>(){}[0]
constructor <init>(#A) // org.kotlincrypto.core.xof/Xof.<init>|<init>(1:0){}[0]

final val delegate // org.kotlincrypto.core.xof/Xof.delegate|{}delegate[0]
final fun <get-delegate>(): #A // org.kotlincrypto.core.xof/Xof.delegate.<get-delegate>|<get-delegate>(){}[0]

abstract fun newReader(): org.kotlincrypto.core.xof/Xof.Reader<#A> // org.kotlincrypto.core.xof/Xof.newReader|newReader(){}[0]
final fun <#A1: kotlin/Any?> use(kotlin/Boolean =..., kotlin/Function1<org.kotlincrypto.core.xof/Xof.Reader<#A>, #A1>): #A1 // org.kotlincrypto.core.xof/Xof.use|use(kotlin.Boolean;kotlin.Function1<org.kotlincrypto.core.xof.Xof.Reader<1:0>,0:0>){0§<kotlin.Any?>}[0]
Expand All @@ -55,6 +59,10 @@ sealed class <#A: org.kotlincrypto.core.xof/XofAlgorithm> org.kotlincrypto.core.
final fun toString(): kotlin/String // org.kotlincrypto.core.xof/Xof.Reader.toString|toString(){}[0]
}

final object Companion { // org.kotlincrypto.core.xof/Xof.Companion|null[0]
final fun <#A2: org.kotlincrypto.core.xof/ReKeyableXofAlgorithm> (org.kotlincrypto.core.xof/Xof<#A2>).reset(kotlin/ByteArray) // org.kotlincrypto.core.xof/Xof.Companion.reset|[email protected]<0:0>(kotlin.ByteArray){0§<org.kotlincrypto.core.xof.ReKeyableXofAlgorithm>}[0]
}

final object Utils { // org.kotlincrypto.core.xof/Xof.Utils|null[0]
final fun leftEncode(kotlin/Int): kotlin/ByteArray // org.kotlincrypto.core.xof/Xof.Utils.leftEncode|leftEncode(kotlin.Int){}[0]
final fun leftEncode(kotlin/Int, kotlin/Int): kotlin/ByteArray // org.kotlincrypto.core.xof/Xof.Utils.leftEncode|leftEncode(kotlin.Int;kotlin.Int){}[0]
Expand Down
19 changes: 17 additions & 2 deletions library/xof/src/commonMain/kotlin/org/kotlincrypto/core/xof/Xof.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.kotlincrypto.core.xof

import org.kotlincrypto.core.*
import kotlin.jvm.JvmField
import kotlin.jvm.JvmName
import kotlin.jvm.JvmOverloads
import kotlin.jvm.JvmStatic
Expand Down Expand Up @@ -52,7 +53,22 @@ import kotlin.jvm.JvmStatic
* @see [reader]
* @see [Reader]
* */
public sealed class Xof<A: XofAlgorithm>: Algorithm, Copyable<Xof<A>>, Resettable, Updatable {
public sealed class Xof<A: XofAlgorithm>(
@JvmField
protected val delegate: A,
): Algorithm, Copyable<Xof<A>>, Resettable, Updatable {

public companion object {

/**
* Helper to provide access to the instance backing [Xof], if said instance
* can be re-keyed (such as a [org.kotlincrypto.core.mac.Mac]).
*
* @throws [IllegalArgumentException] if [newKey] is unacceptable.
* */
@JvmStatic
public fun <A: ReKeyableXofAlgorithm> Xof<A>.reset(newKey: ByteArray) { delegate.reset(newKey) }
}

/**
* Takes a snapshot of the current [Xof]'s state and produces
Expand Down Expand Up @@ -298,5 +314,4 @@ public sealed class Xof<A: XofAlgorithm>: Algorithm, Copyable<Xof<A>>, Resettabl

/** @suppress */
public final override fun toString(): String = "Xof[${algorithm()}]@${hashCode()}"

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,22 @@ import org.kotlincrypto.core.Algorithm
* which supports Extendable-Output Functions (XOFs).
* */
public interface XofAlgorithm: Algorithm

/**
* Extended functionality, specifically for a [Xof] who's backing instance
* is a [org.kotlincrypto.core.mac.Mac].
*
*
* @see [Xof.Companion.reset]
* */
public interface ReKeyableXofAlgorithm: XofAlgorithm {

/**
* Resets and re-initializes the instance with the provided [newKey]
*
* This is useful if wanting to clear the key before de-referencing.
*
* @throws [IllegalArgumentException] if [newKey] is unacceptable.
* */
public fun reset(newKey: ByteArray)
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,7 @@ public abstract class XofFactory<A: XofAlgorithm> public constructor() {
* */
protected abstract inner class XofDelegate
@Throws(ClassCastException::class, IllegalArgumentException::class)
protected constructor(
protected val delegate: A,
) : Xof<A>(),
protected constructor(delegate: A) : Xof<A>(delegate),
Algorithm by delegate,
Resettable by delegate as Resettable,
Updatable by delegate as Updatable
Expand Down
Loading