WIP: Remove *_from_string functions as they're not yet part of a libsodium release

This commit is contained in:
Johannes Leupold 2024-08-14 12:11:20 +02:00
parent 9a4a776bbd
commit 6d5b2ba6e1
12 changed files with 0 additions and 277 deletions

View File

@ -17,17 +17,10 @@ const val crypto_core_ed25519_NONREDUCEDSCALARBYTES = 64
const val crypto_scalarmult_ed25519_BYTES = 32U
const val crypto_scalarmult_ed25519_SCALARBYTES = 32U
enum class HashToCurveAlgorithm(val id: Int) {
SHA256(1),
SHA512(2),
}
expect abstract class Ed25519LowLevel() {
fun isValidPoint(encoded: UByteArray): Boolean
fun addPoints(p: UByteArray, q: UByteArray): UByteArray
fun subtractPoints(p: UByteArray, q: UByteArray): UByteArray
fun encodedPointFromString(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray
fun encodedPointFromStringRo(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray
fun encodedPointFromUniform(uniform: UByteArray): UByteArray
fun randomEncodedPoint(): UByteArray
fun randomEncodedScalar(): UByteArray
@ -51,12 +44,6 @@ object Ed25519 : Ed25519LowLevel() {
fun subtract(p: Point, q: Point): Point =
Point(subtractPoints(p.encoded, q.encoded))
fun pointFromString(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): Point =
Point(encodedPointFromString(ctx, msg, hashAlg))
fun pointFromStringRo(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): Point =
Point(encodedPointFromStringRo(ctx, msg, hashAlg))
fun pointFromUniform(uniform: UByteArray): Point = Point(encodedPointFromUniform(uniform))
fun randomPoint(): Point = Point(randomEncodedPoint())

View File

@ -1,8 +1,6 @@
package com.ionspin.kotlin.crypto.ristretto255
import com.ionspin.kotlin.crypto.ed25519.HashToCurveAlgorithm
import com.ionspin.kotlin.crypto.util.LibsodiumUtil
import kotlin.UByteArray
/**
* Created by Johannes Leupold
@ -23,8 +21,6 @@ expect abstract class Ristretto255LowLevel() {
fun addPoints(p: UByteArray, q: UByteArray): UByteArray
fun subtractPoints(p: UByteArray, q: UByteArray): UByteArray
fun encodedPointFromHash(hash: UByteArray): UByteArray
fun encodedPointFromString(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray
fun encodedPointFromStringRo(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray
fun randomEncodedPoint(): UByteArray
fun randomEncodedScalar(): UByteArray
fun invertScalar(scalar: UByteArray): UByteArray
@ -47,12 +43,6 @@ object Ristretto255 : Ristretto255LowLevel() {
fun pointFromHash(hash: UByteArray): Point = Point(encodedPointFromHash(hash))
fun pointFromString(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): Point =
Point(encodedPointFromString(ctx, msg, hashAlg))
fun pointFromStringRo(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): Point =
Point(encodedPointFromStringRo(ctx, msg, hashAlg))
fun randomPoint(): Point = Point(randomEncodedPoint())
fun randomScalar(): Scalar = Scalar(randomEncodedScalar())

View File

@ -375,12 +375,6 @@ external object JsSodiumInterface {
@JsName("crypto_core_ristretto255_from_hash")
fun crypto_core_ristretto255_from_hash(r: Uint8Array): Uint8Array
@JsName("crypto_core_ristretto255_from_string")
fun crypto_core_ed25519_from_string(ctx: String?, message: Uint8Array, hashAlg: Int): Uint8Array
@JsName("crypto_core_ristretto255_from_string_ro")
fun crypto_core_ed25519_from_string_ro(ctx: String?, message: Uint8Array, hashAlg: Int): Uint8Array
@JsName("crypto_core_ristretto255_add")
fun crypto_core_ristretto255_add(p: Uint8Array, q: Uint8Array): Uint8Array
@ -430,12 +424,6 @@ external object JsSodiumInterface {
@JsName("crypto_core_ed25519_random")
fun crypto_core_ed25519_random(): Uint8Array
@JsName("crypto_core_ed25519_from_string")
fun crypto_core_ed25519_from_string(ctx: String, message: Uint8Array, hashAlg: Int): Uint8Array
@JsName("crypto_core_ed25519_from_string_ro")
fun crypto_core_ed25519_from_string_ro(ctx: String, message: Uint8Array, hashAlg: Int): Uint8Array
@JsName("crypto_core_ed25519_from_uniform")
fun crypto_core_ed25519_from_uniform(r: Uint8Array): Uint8Array

View File

@ -20,18 +20,6 @@ actual abstract class Ed25519LowLevel actual constructor() {
return result.toUByteArray()
}
actual fun encodedPointFromString(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray {
val result = getSodium().crypto_core_ed25519_from_string(ctx, msg.toUInt8Array(), hashAlg.id)
return result.toUByteArray()
}
actual fun encodedPointFromStringRo(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray {
val result = getSodium().crypto_core_ed25519_from_string_ro(ctx, msg.toUInt8Array(), hashAlg.id)
return result.toUByteArray()
}
actual fun encodedPointFromUniform(uniform: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_from_uniform(uniform.toUInt8Array())

View File

@ -26,18 +26,6 @@ actual abstract class Ristretto255LowLevel actual constructor() {
return result.toUByteArray()
}
actual fun encodedPointFromString(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray {
val result = getSodium().crypto_core_ristretto255_from_string(ctx, msg.toUInt8Array(), hashAlg.id)
return result.toUByteArray()
}
actual fun encodedPointFromStringRo(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray {
val result = getSodium().crypto_core_ristretto255_from_string_ro(ctx, msg.toUInt8Array(), hashAlg.id)
return result.toUByteArray()
}
actual fun randomEncodedPoint(): UByteArray {
val result = getSodium().crypto_core_ristretto255_random()

View File

@ -1309,22 +1309,6 @@ interface JnaLibsodiumInterface : Library {
fun crypto_core_ristretto255_from_hash(p: ByteArray, r: ByteArray): Int
fun crypto_core_ristretto255_from_string(
p: ByteArray,
ctx: ByteArray?,
msg: ByteArray,
msgLen: Int,
hashAlg: Int,
): Int
fun crypto_core_ristretto255_from_string_ro(
p: ByteArray,
ctx: ByteArray?,
msg: ByteArray,
msgLen: Int,
hashAlg: Int,
): Int
fun crypto_core_ristretto255_add(r: ByteArray, p: ByteArray, q: ByteArray): Int
fun crypto_core_ristretto255_sub(r: ByteArray, p: ByteArray, q: ByteArray): Int
@ -1359,10 +1343,6 @@ interface JnaLibsodiumInterface : Library {
fun crypto_core_ed25519_random(p: ByteArray)
fun crypto_core_ed25519_from_string(p: ByteArray, ctx: ByteArray?, msg: ByteArray, msgLen: Int, hashAlg: Int): Int
fun crypto_core_ed25519_from_string_ro(p: ByteArray, ctx: ByteArray?, msg: ByteArray, msgLen: Int, hashAlg: Int): Int
fun crypto_core_ed25519_from_uniform(p: ByteArray, r: ByteArray): Int
fun crypto_core_ed25519_add(r: ByteArray, p: ByteArray, q: ByteArray): Int

View File

@ -2,7 +2,6 @@ package com.ionspin.kotlin.crypto.ed25519
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
import com.ionspin.kotlin.crypto.util.toCString
actual abstract class Ed25519LowLevel actual constructor() {
actual fun isValidPoint(encoded: UByteArray): Boolean =
@ -26,36 +25,6 @@ actual abstract class Ed25519LowLevel actual constructor() {
return result
}
actual fun encodedPointFromString(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray {
val result = UByteArray(crypto_core_ed25519_BYTES)
val ctxEncoded = ctx?.toCString()
sodiumJna.crypto_core_ed25519_from_string(
result.asByteArray(),
ctxEncoded?.asByteArray(),
msg.asByteArray(),
msg.size,
hashAlg.id
).ensureLibsodiumSuccess()
return result
}
actual fun encodedPointFromStringRo(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray {
val result = UByteArray(crypto_core_ed25519_BYTES)
val ctxEncoded = ctx?.toCString()
sodiumJna.crypto_core_ed25519_from_string_ro(
result.asByteArray(),
ctxEncoded?.asByteArray(),
msg.asByteArray(),
msg.size,
hashAlg.id
).ensureLibsodiumSuccess()
return result
}
actual fun encodedPointFromUniform(uniform: UByteArray): UByteArray {
val result = UByteArray(crypto_core_ed25519_BYTES)

View File

@ -2,8 +2,6 @@ package com.ionspin.kotlin.crypto.ristretto255
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
import com.ionspin.kotlin.crypto.ed25519.HashToCurveAlgorithm
import com.ionspin.kotlin.crypto.util.toCString
actual abstract class Ristretto255LowLevel actual constructor() {
actual fun isValidPoint(encoded: UByteArray): Boolean =
@ -35,36 +33,6 @@ actual abstract class Ristretto255LowLevel actual constructor() {
return result
}
actual fun encodedPointFromString(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray {
val result = UByteArray(crypto_core_ristretto255_BYTES)
val ctxEncoded = ctx?.toCString()
sodiumJna.crypto_core_ristretto255_from_string(
result.asByteArray(),
ctxEncoded?.asByteArray(),
msg.asByteArray(),
msg.size,
hashAlg.id
).ensureLibsodiumSuccess()
return result
}
actual fun encodedPointFromStringRo(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray {
val result = UByteArray(crypto_core_ristretto255_BYTES)
val ctxEncoded = ctx?.toCString()
sodiumJna.crypto_core_ristretto255_from_string_ro(
result.asByteArray(),
ctxEncoded?.asByteArray(),
msg.asByteArray(),
msg.size,
hashAlg.id
).ensureLibsodiumSuccess()
return result
}
actual fun randomEncodedPoint(): UByteArray = UByteArray(crypto_core_ristretto255_BYTES).also {
sodiumJna.crypto_core_ristretto255_random(it.asByteArray())
}

View File

@ -1,12 +0,0 @@
package com.ionspin.kotlin.crypto.util
fun String.toCString(): UByteArray {
val encoded = encodeToUByteArray()
val cStr = UByteArray(encoded.size + 1)
encoded.copyInto(cStr)
LibsodiumUtil.memzero(encoded)
return cStr
}

View File

@ -1,11 +1,9 @@
package com.ionspin.kotlin.crypto.ed25519
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.util.toCString
import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.usePinned
import libsodium.crypto_core_ed25519_add
import libsodium.crypto_core_ed25519_from_string
import libsodium.crypto_core_ed25519_from_uniform
import libsodium.crypto_core_ed25519_is_valid_point
import libsodium.crypto_core_ed25519_random
@ -59,59 +57,6 @@ actual abstract class Ed25519LowLevel actual constructor() {
return result
}
actual fun encodedPointFromString(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray {
val result = UByteArray(crypto_core_ed25519_BYTES)
val ctxEncoded = ctx?.toCString()
result.usePinned { resultPinned ->
msg.usePinned { msgPinned ->
if (ctxEncoded == null) {
crypto_core_ed25519_from_string(resultPinned.toPtr(), null, msgPinned.toPtr(), msg.size, hashAlg.id)
.ensureLibsodiumSuccess()
} else {
ctxEncoded.usePinned { ctxPinned ->
crypto_core_ed25519_from_string(
resultPinned.toPtr(),
ctxPinned.toPtr(),
msgPinned.toPtr(),
msg.size,
hashAlg.id
)
.ensureLibsodiumSuccess()
}
}
}
}
return result
}
actual fun encodedPointFromStringRo(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray {
val result = UByteArray(crypto_core_ed25519_BYTES)
val ctxEncoded = ctx?.toCString()
result.usePinned { resultPinned ->
msg.usePinned { msgPinned ->
if (ctxEncoded == null) {
crypto_core_ed25519_from_string_ro(resultPinned.toPtr(), null, msgPinned.toPtr(), msg.size, hashAlg.id)
.ensureLibsodiumSuccess()
} else {
ctxEncoded.usePinned { ctxPinned ->
crypto_core_ed25519_from_string_ro(
resultPinned.toPtr(),
ctxPinned.toPtr(),
msgPinned.toPtr(),
msg.size,
hashAlg.id
).ensureLibsodiumSuccess()
}
}
}
}
return result
}
actual fun encodedPointFromUniform(uniform: UByteArray): UByteArray {
val result = UByteArray(crypto_core_ed25519_BYTES)

View File

@ -1,14 +1,10 @@
package com.ionspin.kotlin.crypto.ristretto255
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.ed25519.HashToCurveAlgorithm
import com.ionspin.kotlin.crypto.util.toCString
import com.ionspin.kotlin.crypto.util.toPtr
import kotlinx.cinterop.usePinned
import libsodium.crypto_core_ristretto255_add
import libsodium.crypto_core_ristretto255_from_hash
import libsodium.crypto_core_ristretto255_from_string
import libsodium.crypto_core_ristretto255_from_string_ro
import libsodium.crypto_core_ristretto255_is_valid_point
import libsodium.crypto_core_ristretto255_random
import libsodium.crypto_core_ristretto255_scalar_add
@ -71,59 +67,6 @@ actual abstract class Ristretto255LowLevel actual constructor() {
return result
}
actual fun encodedPointFromString(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray {
val result = UByteArray(crypto_core_ristretto255_BYTES)
val ctxEncoded = ctx?.toCString()
result.usePinned { resultPinned ->
msg.usePinned { msgPinned ->
if (ctxEncoded == null) {
crypto_core_ristretto255_from_string(resultPinned.toPtr(), null, msgPinned.toPtr(), msg.size, hashAlg.id)
.ensureLibsodiumSuccess()
} else {
ctxEncoded.usePinned { ctxPinned ->
crypto_core_ristretto255_from_string(
resultPinned.toPtr(),
ctxPinned.toPtr(),
msgPinned.toPtr(),
msg.size,
hashAlg.id
)
.ensureLibsodiumSuccess()
}
}
}
}
return result
}
actual fun encodedPointFromStringRo(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray {
val result = UByteArray(crypto_core_ristretto255_BYTES)
val ctxEncoded = ctx?.toCString()
result.usePinned { resultPinned ->
msg.usePinned { msgPinned ->
if (ctxEncoded == null) {
crypto_core_ristretto255_from_string_ro(resultPinned.toPtr(), null, msgPinned.toPtr(), msg.size, hashAlg.id)
.ensureLibsodiumSuccess()
} else {
ctxEncoded.usePinned { ctxPinned ->
crypto_core_ristretto255_from_string_ro(
resultPinned.toPtr(),
ctxPinned.toPtr(),
msgPinned.toPtr(),
msg.size,
hashAlg.id
).ensureLibsodiumSuccess()
}
}
}
}
return result
}
actual fun randomEncodedPoint(): UByteArray = UByteArray(crypto_core_ristretto255_BYTES).apply {
usePinned { crypto_core_ristretto255_random(it.toPtr()) }
}

View File

@ -17,14 +17,3 @@ fun Pinned<UByteArray>.toPtr() : CPointer<UByteVar>? {
null
}
}
fun String.toCString(): UByteArray {
val encoded = encodeToUByteArray()
val cStr = UByteArray(encoded.size + 1)
encoded.copyInto(cStr)
LibsodiumUtil.memzero(encoded)
return cStr
}