WIP: Fix build/test issues

This commit is contained in:
Johannes Leupold 2024-08-14 11:47:29 +02:00
parent 338c1f0012
commit 9a4a776bbd
14 changed files with 1378 additions and 1152 deletions

View File

@ -17,11 +17,17 @@ 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 encodedPointFromHash(hash: 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
@ -45,7 +51,11 @@ object Ed25519 : Ed25519LowLevel() {
fun subtract(p: Point, q: Point): Point =
Point(subtractPoints(p.encoded, q.encoded))
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 pointFromUniform(uniform: UByteArray): Point = Point(encodedPointFromUniform(uniform))
@ -103,8 +113,6 @@ object Ed25519 : Ed25519LowLevel() {
val IDENTITY: Point = Point(UByteArray(crypto_core_ed25519_BYTES))
val BASE: Point = scalarMultiplicationBaseNoClamp(Scalar.ONE)
fun fromHash(hash: UByteArray): Point = pointFromHash(hash)
fun fromUniform(uniform: UByteArray): Point = pointFromUniform(uniform)
fun random(): Point = randomPoint()

View File

@ -1,5 +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
@ -22,6 +23,8 @@ 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
@ -44,6 +47,12 @@ 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

@ -1,9 +1,7 @@
package com.ionspin.kotlin.crypto.ed25519
import com.ionspin.kotlin.crypto.LibsodiumInitializer
import com.ionspin.kotlin.crypto.hash.Hash
import com.ionspin.kotlin.crypto.util.LibsodiumUtil
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
import com.ionspin.kotlin.crypto.util.runTest
import kotlin.test.Test
import kotlin.test.assertEquals
@ -183,18 +181,6 @@ class Ed25519Test {
}
}
@Test
fun testPointFromHash() = runTest {
LibsodiumInitializer.initializeWithCallback {
for ((input, output) in fromHashTestVectors) {
val outPoint = Ed25519.Point.fromHex(output)
val hash = Hash.sha512(input.encodeToUByteArray())
assertEquals(outPoint, Ed25519.Point.fromHash(hash))
}
}
}
@Test
fun testPointFromUniform() = runTest {
LibsodiumInitializer.initializeWithCallback {

View File

@ -63,8 +63,6 @@ class Ristretto255Test {
// Test vectors from https://ristretto.group/test_vectors/ristretto255.html
val basePointSmallMultiples = arrayOf(
// This is the identity point
"0000000000000000000000000000000000000000000000000000000000000000",
// This is the basepoint
"e2f2ae0a6abc4e71a884a961c500515f58e30b6aa582dd8db6a65945e08d2d76",
// These are small multiples of the basepoint
@ -131,7 +129,7 @@ class Ristretto255Test {
for (i in basePointSmallMultiples.indices) {
val p = Ristretto255.Point.fromHex(basePointSmallMultiples[i])
val b = Ristretto255.Point.BASE
val n = Ristretto255.Scalar.fromUInt(i.toUInt())
val n = Ristretto255.Scalar.fromUInt(i.toUInt() + 1U)
assertEquals(p, Ristretto255.scalarMultiplicationBase(n))
assertEquals(p, Ristretto255.scalarMultiplication(b, n))
@ -146,7 +144,7 @@ class Ristretto255Test {
assertEquals(q, p - b * m)
}
for (j in i..<basePointSmallMultiples.size) {
for (j in i + 1..<basePointSmallMultiples.size) {
val q = Ristretto255.Point.fromHex(basePointSmallMultiples[j])
val m = Ristretto255.Scalar.fromUInt(j.toUInt() - i.toUInt())

View File

@ -375,6 +375,12 @@ 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
@ -424,8 +430,11 @@ external object JsSodiumInterface {
@JsName("crypto_core_ed25519_random")
fun crypto_core_ed25519_random(): Uint8Array
@JsName("crypto_core_ed25519_from_hash")
fun crypto_core_ed25519_from_hash(r: Uint8Array): 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

@ -6,28 +6,34 @@ import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
actual abstract class Ed25519LowLevel actual constructor() {
actual fun isValidPoint(encoded: UByteArray): Boolean =
getSodium().crypto_core_ed25519_is_valid_point(encoded.toUint8Array())
getSodium().crypto_core_ed25519_is_valid_point(encoded.toUInt8Array())
actual fun addPoints(p: UByteArray, q: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_add(p.toUint8Array(), q.toUint8Array())
val result = getSodium().crypto_core_ed25519_add(p.toUInt8Array(), q.toUInt8Array())
return result.toUByteArray()
}
actual fun subtractPoints(p: UByteArray, q: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_sub(p.toUint8Array(), q.toUint8Array())
val result = getSodium().crypto_core_ed25519_sub(p.toUInt8Array(), q.toUInt8Array())
return result
return result.toUByteArray()
}
actual fun encodedPointFromHash(hash: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_from_hash(hash.toUint8Array())
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(hash.toUint8Array())
val result = getSodium().crypto_core_ed25519_from_uniform(uniform.toUInt8Array())
return result.toUByteArray()
}
@ -45,67 +51,67 @@ actual abstract class Ed25519LowLevel actual constructor() {
}
actual fun invertScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_invert(scalar.toUint8Array())
val result = getSodium().crypto_core_ed25519_scalar_invert(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun negateScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_negate(scalar.toUint8Array())
val result = getSodium().crypto_core_ed25519_scalar_negate(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun complementScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_complement(scalar.toUint8Array())
val result = getSodium().crypto_core_ed25519_scalar_complement(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun addScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_add(x.toUint8Array(), y.toUint8Array())
val result = getSodium().crypto_core_ed25519_scalar_add(x.toUInt8Array(), y.toUInt8Array())
return result.toUByteArray()
}
actual fun subtractScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_sub(x.toUint8Array(), y.toUint8Array())
val result = getSodium().crypto_core_ed25519_scalar_sub(x.toUInt8Array(), y.toUInt8Array())
return result.toUByteArray()
}
actual fun multiplyScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_mul(x.toUint8Array(), y.toUint8Array())
val result = getSodium().crypto_core_ed25519_scalar_mul(x.toUInt8Array(), y.toUInt8Array())
return result.toUByteArray()
}
actual fun reduceScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ed25519_scalar_reduce(scalar.toUint8Array())
val result = getSodium().crypto_core_ed25519_scalar_reduce(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun scalarMultiplication(n: UByteArray, p: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ed25519(n.toUint8Array(), p.toUint8Array())
val result = getSodium().crypto_scalarmult_ed25519(n.toUInt8Array(), p.toUInt8Array())
return result.toUByteArray()
}
actual fun scalarMultiplicationNoClamp(n: UByteArray, p: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ed25519_noclamp(n.toUint8Array(), p.toUint8Array())
val result = getSodium().crypto_scalarmult_ed25519_noclamp(n.toUInt8Array(), p.toUInt8Array())
return result.toUByteArray()
}
actual fun scalarMultiplicationBase(n: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ed25519_base(n.toUint8Array())
val result = getSodium().crypto_scalarmult_ed25519_base(n.toUInt8Array())
return result.toUByteArray()
}
actual fun scalarMultiplicationBaseNoClamp(n: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ed25519_base_noclamp(n.toUint8Array())
val result = getSodium().crypto_scalarmult_ed25519_base_noclamp(n.toUInt8Array())
return result.toUByteArray()
}

View File

@ -6,22 +6,34 @@ import ext.libsodium.com.ionspin.kotlin.crypto.toUInt8Array
actual abstract class Ristretto255LowLevel actual constructor() {
actual fun isValidPoint(encoded: UByteArray): Boolean =
getSodium().crypto_core_ristretto255_is_valid_point(encoded.toUint8Array())
getSodium().crypto_core_ristretto255_is_valid_point(encoded.toUInt8Array())
actual fun addPoints(p: UByteArray, q: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_add(p.toUint8Array(), q.toUint8Array())
val result = getSodium().crypto_core_ristretto255_add(p.toUInt8Array(), q.toUInt8Array())
return result.toUByteArray()
}
actual fun subtractPoints(p: UByteArray, q: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_sub(p.toUint8Array(), q.toUint8Array())
val result = getSodium().crypto_core_ristretto255_sub(p.toUInt8Array(), q.toUInt8Array())
return result
return result.toUByteArray()
}
actual fun encodedPointFromHash(hash: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_from_hash(hash.toUint8Array())
val result = getSodium().crypto_core_ristretto255_from_hash(hash.toUInt8Array())
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()
}
@ -39,55 +51,55 @@ actual abstract class Ristretto255LowLevel actual constructor() {
}
actual fun invertScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_invert(scalar.toUint8Array())
val result = getSodium().crypto_core_ristretto255_scalar_invert(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun negateScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_negate(scalar.toUint8Array())
val result = getSodium().crypto_core_ristretto255_scalar_negate(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun complementScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_complement(scalar.toUint8Array())
val result = getSodium().crypto_core_ristretto255_scalar_complement(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun addScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_add(x.toUint8Array(), y.toUint8Array())
val result = getSodium().crypto_core_ristretto255_scalar_add(x.toUInt8Array(), y.toUInt8Array())
return result.toUByteArray()
}
actual fun subtractScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_sub(x.toUint8Array(), y.toUint8Array())
val result = getSodium().crypto_core_ristretto255_scalar_sub(x.toUInt8Array(), y.toUInt8Array())
return result.toUByteArray()
}
actual fun multiplyScalars(x: UByteArray, y: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_mul(x.toUint8Array(), y.toUint8Array())
val result = getSodium().crypto_core_ristretto255_scalar_mul(x.toUInt8Array(), y.toUInt8Array())
return result.toUByteArray()
}
actual fun reduceScalar(scalar: UByteArray): UByteArray {
val result = getSodium().crypto_core_ristretto255_scalar_reduce(scalar.toUint8Array())
val result = getSodium().crypto_core_ristretto255_scalar_reduce(scalar.toUInt8Array())
return result.toUByteArray()
}
actual fun scalarMultiplication(n: UByteArray, p: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ristretto255(n.toUint8Array(), p.toUint8Array())
val result = getSodium().crypto_scalarmult_ristretto255(n.toUInt8Array(), p.toUInt8Array())
return result.toUByteArray()
}
actual fun scalarMultiplicationBase(n: UByteArray): UByteArray {
val result = getSodium().crypto_scalarmult_ristretto255_base(n.toUint8Array())
val result = getSodium().crypto_scalarmult_ristretto255_base(n.toUInt8Array())
return result.toUByteArray()
}

View File

@ -75,7 +75,7 @@ interface JnaLibsodiumInterface : Library {
// ---- Initialization ---
fun sodium_init() : Int
fun sodium_init(): Int
// ---- Initialization end ---
@ -91,16 +91,16 @@ interface JnaLibsodiumInterface : Library {
fun randombytes_buf_deterministic(
buffer: ByteArray,
size: Int,
seed: ByteArray
seed: ByteArray,
)
// uint32_t randombytes_random(void)
fun randombytes_random() : Long
fun randombytes_random(): Long
// uint32_t randombytes_uniform(const uint32_t upper_bound);
fun randombytes_uniform(
upperBound: Long
) : Long
upperBound: Long,
): Long
// void sodium_memzero(void * const pnt, const size_t len);
fun sodium_memzero(array: ByteArray, len: Int)
@ -114,7 +114,7 @@ interface JnaLibsodiumInterface : Library {
hex: ByteArray,
hexMaxlen: Int,
bin: ByteArray,
binLen: Int
binLen: Int,
): String
// int sodium_hex2bin(
@ -129,7 +129,7 @@ interface JnaLibsodiumInterface : Library {
hexLen: Int,
ignore: ByteArray?,
binLen: Pointer,
hexEnd: Pointer?
hexEnd: Pointer?,
): Int
// int sodium_pad(size_t *padded_buflen_p, unsigned char *buf,
@ -139,7 +139,7 @@ interface JnaLibsodiumInterface : Library {
buffer: ByteArray,
unpaddedBufferLength: Int,
blockSize: Int,
maxBufferLength: Int
maxBufferLength: Int,
): Int
// int sodium_unpad(size_t *unpadded_buflen_p, const unsigned char *buf,
@ -148,8 +148,8 @@ interface JnaLibsodiumInterface : Library {
unpaddedBufferLength: Pointer,
buffer: ByteArray,
paddedBufferLength: Int,
blockSize: Int
) : Int
blockSize: Int,
): Int
// char *sodium_bin2base64(char * const b64, const size_t b64_maxlen,
@ -160,7 +160,7 @@ interface JnaLibsodiumInterface : Library {
base64MaxLength: Int,
bin: ByteArray,
binLength: Int,
variant: Int
variant: Int,
)
// int sodium_base642bin(
@ -176,7 +176,7 @@ interface JnaLibsodiumInterface : Library {
ignore: ByteArray?,
binLength: Pointer,
base64End: Pointer?,
variant: Int
variant: Int,
): Int
// size_t sodium_base64_encoded_len(const size_t bin_len, const int variant)
@ -193,7 +193,7 @@ interface JnaLibsodiumInterface : Library {
input: ByteArray,
inputLength: Long,
key: ByteArray,
keylen: Int
keylen: Int,
): Int
// int crypto_hash_sha256(unsigned char *out, const unsigned char *in,
@ -243,7 +243,7 @@ interface JnaLibsodiumInterface : Library {
input: ByteArray,
inputLength: Long,
key: ByteArray,
keylen: Int
keylen: Int,
): Int
// int crypto_generichash_blake2b_init(crypto_generichash_blake2b_state *state,
@ -334,7 +334,7 @@ interface JnaLibsodiumInterface : Library {
additionalDataLength: Long,
nsec: ByteArray?,
npub: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// int crypto_aead_xchacha20poly1305_ietf_decrypt(
@ -356,7 +356,7 @@ interface JnaLibsodiumInterface : Library {
additionalData: ByteArray,
additionalDataLength: Long,
npub: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// int crypto_aead_xchacha20poly1305_ietf_encrypt_detached(
@ -380,7 +380,7 @@ interface JnaLibsodiumInterface : Library {
additionalDataLength: Long,
nsec: ByteArray?,
npub: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// int crypto_aead_xchacha20poly1305_ietf_decrypt_detached(
@ -402,7 +402,7 @@ interface JnaLibsodiumInterface : Library {
additionalData: ByteArray,
additionalDataLength: Long,
npub: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// void crypto_aead_xchacha20poly1305_ietf_keygen(unsigned char k[crypto_aead_xchacha20poly1305_ietf_KEYBYTES])
@ -432,8 +432,8 @@ interface JnaLibsodiumInterface : Library {
additionalDataLength: Long,
nsec: ByteArray?,
npub: ByteArray,
key: ByteArray
) : Int
key: ByteArray,
): Int
// int crypto_aead_chacha20poly1305_ietf_decrypt(
// unsigned char *m,
@ -454,7 +454,7 @@ interface JnaLibsodiumInterface : Library {
additionalData: ByteArray,
additionalDataLength: Long,
npub: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// int crypto_aead_chacha20poly1305_ietf_encrypt_detached(
@ -478,7 +478,7 @@ interface JnaLibsodiumInterface : Library {
additionalDataLength: Long,
nsec: ByteArray?,
npub: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// int crypto_aead_chacha20poly1305_ietf_decrypt_detached(
@ -500,7 +500,7 @@ interface JnaLibsodiumInterface : Library {
additionalData: ByteArray,
additionalDataLength: Long,
npub: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// void crypto_aead_chacha20poly1305_ietf_keygen(unsigned char k[crypto_aead_xchacha20poly1305_ietf_KEYBYTES])
@ -532,7 +532,7 @@ interface JnaLibsodiumInterface : Library {
additionalDataLength: Long,
nsec: ByteArray?,
npub: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// int crypto_aead_xchacha20poly1305_decrypt(unsigned char *m,
@ -553,7 +553,7 @@ interface JnaLibsodiumInterface : Library {
additionalData: ByteArray,
additionalDataLength: Long,
npub: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// int crypto_aead_chacha20poly1305_encrypt_detached(
@ -577,7 +577,7 @@ interface JnaLibsodiumInterface : Library {
additionalDataLength: Long,
nsec: ByteArray?,
npub: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// int crypto_aead_chacha20poly1305_decrypt_detached(
@ -599,7 +599,7 @@ interface JnaLibsodiumInterface : Library {
additionalData: ByteArray,
additionalDataLength: Long,
npub: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// void crypto_aead_chacha20poly1305_keygen(unsigned char k[crypto_aead_xchacha20poly1305_ietf_KEYBYTES])
@ -622,7 +622,7 @@ interface JnaLibsodiumInterface : Library {
fun crypto_secretstream_xchacha20poly1305_init_push(
state: SecretStreamXChaCha20Poly1305State,
header: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// int crypto_secretstream_xchacha20poly1305_push
@ -638,7 +638,7 @@ interface JnaLibsodiumInterface : Library {
messageLength: Long,
additionalData: ByteArray,
additionalDataLength: Long,
tag: Byte
tag: Byte,
): Int
// decrypt
@ -650,7 +650,7 @@ interface JnaLibsodiumInterface : Library {
fun crypto_secretstream_xchacha20poly1305_init_pull(
state: SecretStreamXChaCha20Poly1305State,
header: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// int crypto_secretstream_xchacha20poly1305_pull
@ -666,7 +666,7 @@ interface JnaLibsodiumInterface : Library {
ciphertext: ByteArray,
ciphertextLength: Long,
additionalData: ByteArray,
additionalDataLength: Long
additionalDataLength: Long,
): Int
//keygen and rekey
@ -696,7 +696,7 @@ interface JnaLibsodiumInterface : Library {
message: ByteArray,
messageLength: Long,
nonce: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// int crypto_secretbox_easy(
@ -708,7 +708,7 @@ interface JnaLibsodiumInterface : Library {
message: ByteArray,
messageLength: Long,
nonce: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// void crypto_secretbox_keygen(unsigned char k[crypto_secretbox_KEYBYTES])
@ -727,7 +727,7 @@ interface JnaLibsodiumInterface : Library {
mac: ByteArray,
ciphertextLength: Long,
nonce: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// int crypto_secretbox_open_easy(
@ -739,7 +739,7 @@ interface JnaLibsodiumInterface : Library {
ciphertext: ByteArray,
ciphertextLength: Long,
nonce: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// ---- SecretBox End ----
@ -752,7 +752,7 @@ interface JnaLibsodiumInterface : Library {
out: ByteArray,
input: ByteArray,
inputLength: Long,
key: ByteArray
key: ByteArray,
): Int
// void crypto_auth_keygen(unsigned char k[crypto_auth_KEYBYTES])
@ -764,7 +764,7 @@ interface JnaLibsodiumInterface : Library {
hash: ByteArray,
input: ByteArray,
inputLength: Long,
key: ByteArray
key: ByteArray,
): Int
//Same params as general variant
@ -772,7 +772,7 @@ interface JnaLibsodiumInterface : Library {
out: ByteArray,
input: ByteArray,
inputLength: Long,
key: ByteArray
key: ByteArray,
): Int
//Same params as general variant
@ -783,7 +783,7 @@ interface JnaLibsodiumInterface : Library {
hash: ByteArray,
input: ByteArray,
inputLength: Long,
key: ByteArray
key: ByteArray,
): Int
//Same params as general variant
@ -791,7 +791,7 @@ interface JnaLibsodiumInterface : Library {
out: ByteArray,
input: ByteArray,
inputLength: Long,
key: ByteArray
key: ByteArray,
): Int
//Same params as general variant
@ -802,7 +802,7 @@ interface JnaLibsodiumInterface : Library {
hash: ByteArray,
input: ByteArray,
inputLength: Long,
key: ByteArray
key: ByteArray,
): Int
//
@ -818,7 +818,7 @@ interface JnaLibsodiumInterface : Library {
fun crypto_box_seed_keypair(
publicKey: ByteArray,
secretKey: ByteArray,
seed: ByteArray
seed: ByteArray,
): Int
// int crypto_box_easy(unsigned char *c, const unsigned char *m,
@ -830,7 +830,7 @@ interface JnaLibsodiumInterface : Library {
messageLength: Long,
nonce: ByteArray,
recipientPublicKey: ByteArray,
senderSecretKey: ByteArray
senderSecretKey: ByteArray,
): Int
// int crypto_box_open_easy(unsigned char *m, const unsigned char *c,
@ -842,7 +842,7 @@ interface JnaLibsodiumInterface : Library {
ciphertextLength: Long,
nonce: ByteArray,
senderPublickKey: ByteArray,
recipientSecretKey: ByteArray
recipientSecretKey: ByteArray,
): Int
// int crypto_box_detached(unsigned char *c, unsigned char *mac,
@ -856,7 +856,7 @@ interface JnaLibsodiumInterface : Library {
messageLength: Long,
nonce: ByteArray,
recipientPublicKey: ByteArray,
senderSecretKey: ByteArray
senderSecretKey: ByteArray,
): Int
// int crypto_box_open_detached(
@ -873,7 +873,7 @@ interface JnaLibsodiumInterface : Library {
ciphertextLength: Long,
nonce: ByteArray,
senderPublickKey: ByteArray,
recipientSecretKey: ByteArray
recipientSecretKey: ByteArray,
): Int
// int crypto_box_beforenm(unsigned char *k, const unsigned char *pk,
@ -881,7 +881,7 @@ interface JnaLibsodiumInterface : Library {
fun crypto_box_beforenm(
sessionKey: ByteArray,
publicKey: ByteArray,
secretKey: ByteArray
secretKey: ByteArray,
): Int
// int crypto_box_easy_afternm(unsigned char *c, const unsigned char *m,
@ -892,7 +892,7 @@ interface JnaLibsodiumInterface : Library {
message: ByteArray,
messageLength: Long,
nonce: ByteArray,
sessionKey: ByteArray
sessionKey: ByteArray,
): Int
// int crypto_box_open_easy_afternm(unsigned char *m, const unsigned char *c,
@ -903,7 +903,7 @@ interface JnaLibsodiumInterface : Library {
ciphertext: ByteArray,
ciphertextLength: Long,
nonce: ByteArray,
sessionKey: ByteArray
sessionKey: ByteArray,
): Int
// int crypto_box_seal(unsigned char *c, const unsigned char *m,
@ -912,7 +912,7 @@ interface JnaLibsodiumInterface : Library {
ciphertext: ByteArray,
message: ByteArray,
messageLength: Long,
recipientPublicKey: ByteArray
recipientPublicKey: ByteArray,
): Int
@ -924,7 +924,7 @@ interface JnaLibsodiumInterface : Library {
ciphertext: ByteArray,
ciphertextLength: Long,
senderPublickKey: ByteArray,
recipientSecretKey: ByteArray
recipientSecretKey: ByteArray,
): Int
//
// // ---- Box end ----
@ -940,7 +940,7 @@ interface JnaLibsodiumInterface : Library {
signedMessageLength: LongArray?,
message: ByteArray,
messageLength: Long,
secretKey: ByteArray
secretKey: ByteArray,
): Int
// int crypto_sign_open(
@ -952,7 +952,7 @@ interface JnaLibsodiumInterface : Library {
messageLength: LongArray?,
signedMessage: ByteArray,
signedMessageLength: Long,
publicKey: ByteArray
publicKey: ByteArray,
): Int
// int crypto_sign_detached(
@ -964,7 +964,7 @@ interface JnaLibsodiumInterface : Library {
signatureLength: LongArray?,
message: ByteArray,
messageLength: Long,
secretKey: ByteArray
secretKey: ByteArray,
): Int
// int crypto_sign_verify_detached(
@ -976,7 +976,7 @@ interface JnaLibsodiumInterface : Library {
signature: ByteArray,
message: ByteArray,
messageLength: Long,
publicKey: ByteArray
publicKey: ByteArray,
): Int
// int crypto_sign_ed25519_pk_to_curve25519(
@ -984,27 +984,27 @@ interface JnaLibsodiumInterface : Library {
// const unsigned char *ed25519_pk)
fun crypto_sign_ed25519_pk_to_curve25519(
curve25519PublicKey: ByteArray,
ed25519PublicKey: ByteArray
ed25519PublicKey: ByteArray,
): Int
// int crypto_sign_ed25519_sk_to_curve25519(unsigned char *curve25519_sk,
// const unsigned char *ed25519_sk)
fun crypto_sign_ed25519_sk_to_curve25519(
curve25519SecretKey: ByteArray,
ed25519SecretKey: ByteArray
ed25519SecretKey: ByteArray,
): Int
// int crypto_sign_ed25519_sk_to_pk(unsigned char *pk, const unsigned char *sk)
fun crypto_sign_ed25519_sk_to_pk(
ed25519PublicKey: ByteArray,
ed25519SecretKey: ByteArray
ed25519SecretKey: ByteArray,
): Int
// int crypto_sign_ed25519_sk_to_seed(unsigned char *seed,
// const unsigned char *sk)
fun crypto_sign_ed25519_sk_to_seed(
seed: ByteArray,
ed25519SecretKey: ByteArray
ed25519SecretKey: ByteArray,
): Int
// int crypto_sign_init(crypto_sign_state *state);
@ -1015,7 +1015,7 @@ interface JnaLibsodiumInterface : Library {
fun crypto_sign_update(
state: Ed25519SignatureState,
message: ByteArray,
messageLength: Long
messageLength: Long,
): Int
// int crypto_sign_final_create(crypto_sign_state *state, unsigned char *sig,
@ -1025,7 +1025,7 @@ interface JnaLibsodiumInterface : Library {
state: Ed25519SignatureState,
signature: ByteArray,
signatureLength: LongArray?,
secretKey: ByteArray
secretKey: ByteArray,
): Int
// int crypto_sign_final_verify(crypto_sign_state *state, const unsigned char *sig,
@ -1033,12 +1033,12 @@ interface JnaLibsodiumInterface : Library {
fun crypto_sign_final_verify(
state: Ed25519SignatureState,
signature: ByteArray,
publicKey: ByteArray
publicKey: ByteArray,
): Int
// int crypto_sign_keypair(unsigned char *pk, unsigned char *sk)
fun crypto_sign_keypair(
publicKey: ByteArray, secretKey: ByteArray
publicKey: ByteArray, secretKey: ByteArray,
): Int
// int crypto_sign_seed_keypair(unsigned char *pk, unsigned char *sk,
@ -1046,7 +1046,7 @@ interface JnaLibsodiumInterface : Library {
fun crypto_sign_seed_keypair(
publicKey: ByteArray,
secretKey: ByteArray,
seed: ByteArray
seed: ByteArray,
): Int
@ -1067,12 +1067,12 @@ interface JnaLibsodiumInterface : Library {
subkeyLength: Int,
subkeyId: Long,
context: ByteArray,
key: ByteArray
key: ByteArray,
): Int
// void crypto_kdf_keygen(unsigned char k[crypto_kdf_KEYBYTES])
fun crypto_kdf_keygen(
key: ByteArray
key: ByteArray,
)
//
// // ---- KDF end -----
@ -1093,8 +1093,8 @@ interface JnaLibsodiumInterface : Library {
salt: ByteArray,
opslimit: Long,
memlimit: Long,
algorithm : Int
) : Int
algorithm: Int,
): Int
// int crypto_pwhash_str(char out[crypto_pwhash_STRBYTES],
// const char * const passwd, unsigned long long passwdlen,
@ -1104,31 +1104,31 @@ interface JnaLibsodiumInterface : Library {
password: String,
passwordLength: Long,
opslimit: Long,
memlimit: Long
) : Int
memlimit: Long,
): Int
// int crypto_pwhash_str_needs_rehash(const char str[crypto_pwhash_STRBYTES],
// unsigned long long opslimit, size_t memlimit)
fun crypto_pwhash_str_needs_rehash(
output: String,
opslimit: Long,
memlimit: Long
) : Int
memlimit: Long,
): Int
// int crypto_pwhash_str_verify(const char str[crypto_pwhash_STRBYTES],
// const char * const passwd,
// unsigned long long passwdlen)
fun crypto_pwhash_str_verify(
hash: String,
password: String,
passwordLength: Long
) : Int
passwordLength: Long,
): Int
//
// // ---- Password hashing end ----
//
// // ---- Key exchange ----
@ -1136,7 +1136,7 @@ interface JnaLibsodiumInterface : Library {
// unsigned char sk[crypto_kx_SECRETKEYBYTES])
fun crypto_kx_keypair(
publicKey: ByteArray,
secretKey: ByteArray
secretKey: ByteArray,
): Int
@ -1146,20 +1146,22 @@ interface JnaLibsodiumInterface : Library {
fun crypto_kx_seed_keypair(
publicKey: ByteArray,
secretKey: ByteArray,
seed: ByteArray
seed: ByteArray,
): Int
// int crypto_kx_client_session_keys(unsigned char rx[crypto_kx_SESSIONKEYBYTES],
// unsigned char tx[crypto_kx_SESSIONKEYBYTES],
// const unsigned char client_pk[crypto_kx_PUBLICKEYBYTES],
// const unsigned char client_sk[crypto_kx_SECRETKEYBYTES],
// const unsigned char server_pk[crypto_kx_PUBLICKEYBYTES])
fun crypto_kx_client_session_keys(
receiveKey : ByteArray,
receiveKey: ByteArray,
sendKey: ByteArray,
clientPublicKey: ByteArray,
clientSecretKey: ByteArray,
serverPublicKey: ByteArray
serverPublicKey: ByteArray,
): Int
// int crypto_kx_server_session_keys(unsigned char rx[crypto_kx_SESSIONKEYBYTES],
// unsigned char tx[crypto_kx_SESSIONKEYBYTES],
// const unsigned char server_pk[crypto_kx_PUBLICKEYBYTES],
@ -1170,7 +1172,7 @@ interface JnaLibsodiumInterface : Library {
sendKey: ByteArray,
serverPublicKey: ByteArray,
serverSecretKey: ByteArray,
clientPublicKey: ByteArray
clientPublicKey: ByteArray,
): Int
//
@ -1184,8 +1186,9 @@ interface JnaLibsodiumInterface : Library {
stream: ByteArray,
streamLength: Long,
nonce: ByteArray,
key: ByteArray
) : Int
key: ByteArray,
): Int
// int crypto_stream_chacha20_xor(unsigned char *c, const unsigned char *m,
// unsigned long long mlen, const unsigned char *n,
// const unsigned char *k)
@ -1194,8 +1197,9 @@ interface JnaLibsodiumInterface : Library {
message: ByteArray,
messageLength: Long,
nonce: ByteArray,
key: ByteArray
) : Int
key: ByteArray,
): Int
// int crypto_stream_chacha20_xor_ic(unsigned char *c, const unsigned char *m,
// unsigned long long mlen,
// const unsigned char *n, uint64_t ic,
@ -1205,9 +1209,9 @@ interface JnaLibsodiumInterface : Library {
message: ByteArray,
messageLength: Long,
nonce: ByteArray,
initialCounter : Long,
key: ByteArray
) : Int
initialCounter: Long,
key: ByteArray,
): Int
// int crypto_stream_chacha20_ietf(unsigned char *c, unsigned long long clen,
// const unsigned char *n, const unsigned char *k)
@ -1215,8 +1219,8 @@ interface JnaLibsodiumInterface : Library {
stream: ByteArray,
streamLength: Long,
nonce: ByteArray,
key: ByteArray
) : Int
key: ByteArray,
): Int
// int crypto_stream_chacha20_ietf_xor(unsigned char *c, const unsigned char *m,
// unsigned long long mlen, const unsigned char *n,
@ -1226,8 +1230,8 @@ interface JnaLibsodiumInterface : Library {
message: ByteArray,
messageLength: Long,
nonce: ByteArray,
key: ByteArray
) : Int
key: ByteArray,
): Int
// int crypto_stream_chacha20_ietf_xor_ic(unsigned char *c, const unsigned char *m,
// unsigned long long mlen,
@ -1238,9 +1242,9 @@ interface JnaLibsodiumInterface : Library {
message: ByteArray,
messageLength: Long,
nonce: ByteArray,
initialCounter : Int,
key: ByteArray
) : Int
initialCounter: Int,
key: ByteArray,
): Int
// void crypto_stream_chacha20_keygen(unsigned char k[crypto_stream_chacha20_KEYBYTES])
fun crypto_stream_chacha20_keygen(key: ByteArray)
@ -1251,8 +1255,8 @@ interface JnaLibsodiumInterface : Library {
stream: ByteArray,
streamLength: Long,
nonce: ByteArray,
key: ByteArray
) : Int
key: ByteArray,
): Int
// int crypto_stream_xchacha20_xor(unsigned char *c, const unsigned char *m,
// unsigned long long mlen, const unsigned char *n,
@ -1262,8 +1266,8 @@ interface JnaLibsodiumInterface : Library {
message: ByteArray,
messageLength: Long,
nonce: ByteArray,
key: ByteArray
) : Int
key: ByteArray,
): Int
// int crypto_stream_xchacha20_xor_ic(unsigned char *c, const unsigned char *m,
// unsigned long long mlen,
@ -1274,14 +1278,15 @@ interface JnaLibsodiumInterface : Library {
message: ByteArray,
messageLength: Long,
nonce: ByteArray,
initialCounter : Long,
key: ByteArray
) : Int
initialCounter: Long,
key: ByteArray,
): Int
// void crypto_stream_xchacha20_keygen(unsigned char k[crypto_stream_xchacha20_KEYBYTES])
fun crypto_stream_xchacha20_keygen(key: ByteArray)
//
//
// // ---- Stream end ----
//
// // ---- Scalar multiplication ----
@ -1289,6 +1294,7 @@ interface JnaLibsodiumInterface : Library {
// int crypto_scalarmult(unsigned char *q, const unsigned char *n,
// const unsigned char *p)
fun crypto_scalarmult(q: ByteArray, n: ByteArray, p: ByteArray): Int
// int crypto_scalarmult_base(unsigned char *q, const unsigned char *n)
fun crypto_scalarmult_base(q: ByteArray, b: ByteArray): Int
//
@ -1303,6 +1309,22 @@ 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
@ -1337,7 +1359,9 @@ interface JnaLibsodiumInterface : Library {
fun crypto_core_ed25519_random(p: ByteArray)
fun crypto_core_ed25519_from_hash(p: ByteArray, r: ByteArray): Int
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

View File

@ -2,7 +2,7 @@ package com.ionspin.kotlin.crypto.ed25519
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
import kotlin.UByteArray
import com.ionspin.kotlin.crypto.util.toCString
actual abstract class Ed25519LowLevel actual constructor() {
actual fun isValidPoint(encoded: UByteArray): Boolean =
@ -26,10 +26,32 @@ actual abstract class Ed25519LowLevel actual constructor() {
return result
}
actual fun encodedPointFromHash(hash: UByteArray): UByteArray {
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_hash(result.asByteArray(), hash.asByteArray())
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
}

View File

@ -2,7 +2,8 @@ package com.ionspin.kotlin.crypto.ristretto255
import com.ionspin.kotlin.crypto.GeneralLibsodiumException.Companion.ensureLibsodiumSuccess
import com.ionspin.kotlin.crypto.LibsodiumInitializer.sodiumJna
import kotlin.UByteArray
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 =
@ -34,6 +35,36 @@ 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

@ -0,0 +1,12 @@
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,10 +1,11 @@
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_hash
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
@ -58,12 +59,53 @@ actual abstract class Ed25519LowLevel actual constructor() {
return result
}
actual fun encodedPointFromHash(hash: UByteArray): UByteArray {
actual fun encodedPointFromString(ctx: String?, msg: UByteArray, hashAlg: HashToCurveAlgorithm): UByteArray {
val result = UByteArray(crypto_core_ed25519_BYTES)
val ctxEncoded = ctx?.toCString()
result.usePinned { resultPinned ->
hash.usePinned { hashPinned ->
crypto_core_ed25519_from_hash(resultPinned.toPtr(), hashPinned.toPtr())
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()
}
}
}
}

View File

@ -1,10 +1,14 @@
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
@ -67,6 +71,59 @@ 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

@ -4,7 +4,6 @@ import kotlinx.cinterop.CPointer
import kotlinx.cinterop.Pinned
import kotlinx.cinterop.UByteVar
import kotlinx.cinterop.addressOf
import kotlinx.cinterop.toCPointer
/**
* Created by Ugljesa Jovanovic
@ -18,3 +17,14 @@ 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
}