Adding pull secretstream definition

This commit is contained in:
Ugljesa Jovanovic 2020-08-15 11:53:11 +02:00 committed by Ugljesa Jovanovic
parent 28995c065f
commit df87dae376
No known key found for this signature in database
GPG Key ID: 178E6DFCECCB0E0F
11 changed files with 223 additions and 30 deletions

View File

@ -126,7 +126,9 @@ enum class TypeDefinition(override val typeName: TypeName) : GeneralTypeDefiniti
INT(Int::class.asTypeName()),
STRING(String::class.asTypeName()),
UNIT(Unit::class.asTypeName()),
UBYTE(UByte::class.asTypeName())
UBYTE(UByte::class.asTypeName()),
IRRELEVANT(Unit::class.asTypeName()),
NULL(Unit::class.asTypeName())
}
enum class TargetPlatform {

View File

@ -68,7 +68,7 @@ fun ClassDefinition.defineSecretStreamFunctions() {
+funcDef(
"crypto_secretstream_xchacha20poly1305_init_push",
codeDocumentation = """
Initialize a state and generate a random header. Both are returned inside `SecretStreamStateAndHeader` object
Initialize a state and generate a random header. Both are returned inside `SecretStreamStateAndHeader` object.
""".trimIndent(),
returnType = CustomTypeDefinition(withPackageName("SecretStreamStateAndHeader")),
dynamicJsReturn = true,
@ -81,6 +81,32 @@ fun ClassDefinition.defineSecretStreamFunctions() {
)
}
+funcDef(
"crypto_secretstream_xchacha20poly1305_init_pull",
codeDocumentation = """
Initialize state from header and key. The state can then be used for decryption.
""".trimIndent(),
returnType = CustomTypeDefinition(withPackageName("SecretStreamState")),
dynamicJsReturn = true,
isStateCreationFunction = true,
) {
+ParameterDefinition(
"state",
parameterType = CustomTypeDefinition(withPackageName("SecretStreamState")),
dropParameterFromDefinition = true,
isStateType = true
)
+ParameterDefinition(
"header",
parameterType = TypeDefinition.ARRAY_OF_UBYTES_NO_SIZE
)
+ParameterDefinition(
"key",
parameterType = TypeDefinition.ARRAY_OF_UBYTES_NO_SIZE
)
}
+funcDef(
name = "crypto_secretstream_xchacha20poly1305_push",
codeDocumentation = """
@ -94,10 +120,15 @@ fun ClassDefinition.defineSecretStreamFunctions() {
)
+ParameterDefinition(
"c",
TypeDefinition.ARRAY_OF_UBYTES_LONG_SIZE,
TypeDefinition.ARRAY_OF_UBYTES_NO_SIZE,
isActuallyAnOutputParam = true,
dropParameterFromDefinition = true
)
+ParameterDefinition(
"clen",
TypeDefinition.NULL,
dropParameterFromDefinition = true
)
+ParameterDefinition(
"m",
TypeDefinition.ARRAY_OF_UBYTES_LONG_SIZE,
@ -113,5 +144,43 @@ fun ClassDefinition.defineSecretStreamFunctions() {
)
}
+funcDef(
name = "crypto_secretstream_xchacha20poly1305_pull",
codeDocumentation = """
Decrypt next block of data using the previously initialized state. Returns decrypted block.
""".trimIndent(),
returnType = TypeDefinition.ARRAY_OF_UBYTES
) {
+ParameterDefinition(
"state",
CustomTypeDefinition(withPackageName("SecretStreamState"))
)
+ParameterDefinition(
"m",
TypeDefinition.ARRAY_OF_UBYTES_NO_SIZE,
isActuallyAnOutputParam = true,
dropParameterFromDefinition = true
)
+ParameterDefinition(
"mlen",
TypeDefinition.NULL,
dropParameterFromDefinition = true
)
+ParameterDefinition(
"tag_p",
TypeDefinition.NULL
)
+ParameterDefinition(
"c",
TypeDefinition.ARRAY_OF_UBYTES_LONG_SIZE,
modifiesReturn = true
)
+ParameterDefinition(
"ad",
TypeDefinition.ARRAY_OF_UBYTES_LONG_SIZE
)
}
}

View File

@ -3,6 +3,7 @@ package com.ionspin.kotlin.crypto.generator.libsodium.generator
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.FunctionDefinition
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.InnerClassDefinition
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.KotlinFileDefinition
import com.ionspin.kotlin.crypto.generator.libsodium.definitions.TypeDefinition
import com.squareup.kotlinpoet.*
/**
@ -56,10 +57,12 @@ object CommonLibsodiumGenerator {
var actualReturnTypeFound : Boolean = false
for (paramDefinition in methodDefinition.parameterList) {
if ((paramDefinition.isStateType.not() || methodDefinition.isStateCreationFunction.not()) && paramDefinition.dropParameterFromDefinition.not()) {
if (paramDefinition.parameterType != TypeDefinition.NULL) {
val parameterSpec =
ParameterSpec.builder(paramDefinition.parameterName, paramDefinition.parameterType.typeName)
methodBuilder.addParameter(parameterSpec.build())
}
}
if (paramDefinition.isActuallyAnOutputParam) {
actualReturnTypeFound = true
actualReturnType = paramDefinition.parameterType.typeName

View File

@ -68,10 +68,12 @@ object JsLibsodiumGenerator {
var actualReturnTypeFound: Boolean = false
for (paramDefinition in methodDefinition.parameterList) {
if ((paramDefinition.isStateType.not() || methodDefinition.isStateCreationFunction.not()) && paramDefinition.isActuallyAnOutputParam.not()) {
if (paramDefinition.parameterType != TypeDefinition.NULL) {
val parameterSpec =
ParameterSpec.builder(paramDefinition.parameterName, paramDefinition.parameterType.typeName)
methodBuilder.addParameter(parameterSpec.build())
}
}
if (paramDefinition.modifiesReturn) {
if (returnModifierFound == true) {
throw RuntimeException("Return modifier already found")
@ -158,7 +160,7 @@ object JsLibsodiumGenerator {
paramsBuilder.append(paramDefinition.parameterName + ".toUInt8Array()" + separator)
}
TypeDefinition.ARRAY_OF_UBYTES_LONG_SIZE -> {
paramsBuilder.append(paramDefinition.parameterName + ".toUInt8Array(), " + separator)
paramsBuilder.append(paramDefinition.parameterName + ".toUInt8Array()" + separator)
}
TypeDefinition.ARRAY_OF_UBYTES_NO_SIZE -> {
paramsBuilder.append(paramDefinition.parameterName + ".toUInt8Array()" + separator)
@ -175,6 +177,10 @@ object JsLibsodiumGenerator {
TypeDefinition.UBYTE -> {
paramsBuilder.append(paramDefinition.parameterName + separator)
}
TypeDefinition.NULL -> {
println("Got null parameter in js")
// paramsBuilder.append("null" + separator)
}
}
}

View File

@ -63,10 +63,12 @@ object JvmLibsodiumGenerator {
createStateParam(paramDefinition, methodBuilder)
}
if ((paramDefinition.isStateType.not() || methodDefinition.isStateCreationFunction.not()) && paramDefinition.isActuallyAnOutputParam.not()) {
if (paramDefinition.parameterType != TypeDefinition.NULL) {
val parameterSpec =
ParameterSpec.builder(paramDefinition.parameterName, paramDefinition.parameterType.typeName)
methodBuilder.addParameter(parameterSpec.build())
}
}
if (paramDefinition.modifiesReturn) {
if (returnModifierFound == true) {
throw RuntimeException("Return modifier already found")
@ -213,7 +215,10 @@ object JvmLibsodiumGenerator {
paramsBuilder.append(paramDefinition.parameterName + separator)
}
TypeDefinition.UBYTE -> {
paramsBuilder.append(paramDefinition.parameterName + separator)
paramsBuilder.append(paramDefinition.parameterName + ".toByte()" + separator)
}
TypeDefinition.NULL -> {
paramsBuilder.append("null" + separator)
}
}
}

View File

@ -78,10 +78,12 @@ object NativeLibsodiumGenerator {
createStateParam(paramDefinition, methodBuilder)
}
if ((paramDefinition.isStateType.not() || methodDefinition.isStateCreationFunction.not()) && paramDefinition.isActuallyAnOutputParam.not()) {
if (paramDefinition.parameterType != TypeDefinition.NULL) {
val parameterSpec =
ParameterSpec.builder(paramDefinition.parameterName, paramDefinition.parameterType.typeName)
methodBuilder.addParameter(parameterSpec.build())
}
}
if (paramDefinition.modifiesReturn) {
if (returnModifierFound == true) {
throw RuntimeException("Return modifier already found")
@ -293,6 +295,9 @@ object NativeLibsodiumGenerator {
TypeDefinition.UBYTE -> {
paramsBuilder.append(paramDefinition.parameterName + separator)
}
TypeDefinition.NULL -> {
paramsBuilder.append("null" + separator)
}
}
}

View File

@ -12,3 +12,4 @@ import org.junit.Test
class DebugTest {
}

View File

@ -38,10 +38,16 @@ expect class Crypto internal constructor() {
/**
* Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object
* `SecretStreamStateAndHeader` object.
*/
fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): SecretStreamStateAndHeader
/**
* Initialize state from header and key. The state can then be used for decryption.
*/
fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
SecretStreamState
/**
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/
@ -51,4 +57,13 @@ expect class Crypto internal constructor() {
ad: UByteArray,
tag: UByte
): UByteArray
/**
* Decrypt next block of data using the previously initialized state. Returns decrypted block.
*/
fun crypto_secretstream_xchacha20poly1305_pull(
state: SecretStreamState,
c: UByteArray,
ad: UByteArray
): UByteArray
}

View File

@ -30,7 +30,7 @@ actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha256_update(state: Sha256State, input: UByteArray) {
println("Debug crypto_hash_sha256_update")
getSodium().crypto_hash_sha256_update(state, input.toUInt8Array(), )
getSodium().crypto_hash_sha256_update(state, input.toUInt8Array())
}
actual fun crypto_hash_sha256_final(state: Sha256State): UByteArray {
@ -46,7 +46,7 @@ actual class Crypto internal actual constructor() {
actual fun crypto_hash_sha512_update(state: Sha512State, input: UByteArray) {
println("Debug crypto_hash_sha512_update")
getSodium().crypto_hash_sha512_update(state, input.toUInt8Array(), )
getSodium().crypto_hash_sha512_update(state, input.toUInt8Array())
}
actual fun crypto_hash_sha512_final(state: Sha512State): UByteArray {
@ -61,7 +61,7 @@ actual class Crypto internal actual constructor() {
/**
* Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object
* `SecretStreamStateAndHeader` object.
*/
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray): dynamic {
println("Debug crypto_secretstream_xchacha20poly1305_init_push")
@ -72,6 +72,16 @@ actual class Crypto internal actual constructor() {
return SecretStreamStateAndHeader(state, header)
}
/**
* Initialize state from header and key. The state can then be used for decryption.
*/
actual fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
dynamic {
println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
return getSodium().crypto_secretstream_xchacha20poly1305_init_pull(header.toUInt8Array(),
key.toUInt8Array())
}
/**
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/
@ -82,7 +92,20 @@ actual class Crypto internal actual constructor() {
tag: UByte
): UByteArray {
println("Debug crypto_secretstream_xchacha20poly1305_push")
return getSodium().crypto_secretstream_xchacha20poly1305_push(state, m.toUInt8Array(), ,
ad.toUInt8Array(), , tag).toUByteArray()
return getSodium().crypto_secretstream_xchacha20poly1305_push(state, m.toUInt8Array(),
ad.toUInt8Array(), tag).toUByteArray()
}
/**
* Decrypt next block of data using the previously initialized state. Returns decrypted block.
*/
actual fun crypto_secretstream_xchacha20poly1305_pull(
state: SecretStreamState,
c: UByteArray,
ad: UByteArray
): UByteArray {
println("Debug crypto_secretstream_xchacha20poly1305_pull")
return getSodium().crypto_secretstream_xchacha20poly1305_pull(state, c.toUInt8Array(),
ad.toUInt8Array()).toUByteArray()
}
}

View File

@ -70,7 +70,7 @@ actual class Crypto internal actual constructor() {
/**
* Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object
* `SecretStreamStateAndHeader` object.
*/
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
SecretStreamStateAndHeader {
@ -82,6 +82,18 @@ actual class Crypto internal actual constructor() {
return SecretStreamStateAndHeader(state, header)
}
/**
* Initialize state from header and key. The state can then be used for decryption.
*/
actual fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
SecretStreamState {
val state = debug.test.SecretStreamState()
println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
sodium.crypto_secretstream_xchacha20poly1305_init_pull(state, header.asByteArray(),
key.asByteArray())
return state
}
/**
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/
@ -93,8 +105,23 @@ actual class Crypto internal actual constructor() {
): UByteArray {
val c = UByteArray(m.size)
println("Debug crypto_secretstream_xchacha20poly1305_push")
sodium.crypto_secretstream_xchacha20poly1305_push(state, c.asByteArray(), c.size.toLong(),
m.asByteArray(), m.size.toLong(), ad.asByteArray(), ad.size.toLong(), tag)
sodium.crypto_secretstream_xchacha20poly1305_push(state, c.asByteArray(), null, m.asByteArray(),
m.size.toLong(), ad.asByteArray(), ad.size.toLong(), tag.toByte())
return c
}
/**
* Decrypt next block of data using the previously initialized state. Returns decrypted block.
*/
actual fun crypto_secretstream_xchacha20poly1305_pull(
state: SecretStreamState,
c: UByteArray,
ad: UByteArray
): UByteArray {
val m = UByteArray(c.size)
println("Debug crypto_secretstream_xchacha20poly1305_pull")
sodium.crypto_secretstream_xchacha20poly1305_pull(state, m.asByteArray(), null, null,
c.asByteArray(), c.size.toLong(), ad.asByteArray(), ad.size.toLong())
return m
}
}

View File

@ -96,7 +96,7 @@ actual class Crypto internal actual constructor() {
/**
* Initialize a state and generate a random header. Both are returned inside
* `SecretStreamStateAndHeader` object
* `SecretStreamStateAndHeader` object.
*/
actual fun crypto_secretstream_xchacha20poly1305_init_push(key: UByteArray):
SecretStreamStateAndHeader {
@ -116,6 +116,23 @@ actual class Crypto internal actual constructor() {
return SecretStreamStateAndHeader(state, header)
}
/**
* Initialize state from header and key. The state can then be used for decryption.
*/
actual fun crypto_secretstream_xchacha20poly1305_init_pull(header: UByteArray, key: UByteArray):
SecretStreamState {
val allocated = sodium_malloc(debug.test.SecretStreamState.size.convert())!!
val state = allocated.reinterpret<debug.test.SecretStreamState>().pointed
println("Debug crypto_secretstream_xchacha20poly1305_init_pull")
val pinnedHeader = header.pin()
val pinnedKey = key.pin()
libsodium.crypto_secretstream_xchacha20poly1305_init_pull(state.ptr, pinnedHeader.addressOf(0),
pinnedKey.addressOf(0))
pinnedHeader.unpin()
pinnedKey.unpin()
return state
}
/**
* Encrypt next block of data using the previously initialized state. Returns encrypted block.
*/
@ -130,12 +147,32 @@ actual class Crypto internal actual constructor() {
val pinnedC = c.pin()
val pinnedM = m.pin()
val pinnedAd = ad.pin()
libsodium.crypto_secretstream_xchacha20poly1305_push(state.ptr, pinnedC.addressOf(0),
c.size.convert(), pinnedM.addressOf(0), m.size.convert(), pinnedAd.addressOf(0),
ad.size.convert(), tag)
libsodium.crypto_secretstream_xchacha20poly1305_push(state.ptr, pinnedC.addressOf(0), null,
pinnedM.addressOf(0), m.size.convert(), pinnedAd.addressOf(0), ad.size.convert(), tag)
pinnedC.unpin()
pinnedM.unpin()
pinnedAd.unpin()
return c
}
/**
* Decrypt next block of data using the previously initialized state. Returns decrypted block.
*/
actual fun crypto_secretstream_xchacha20poly1305_pull(
state: SecretStreamState,
c: UByteArray,
ad: UByteArray
): UByteArray {
val m = UByteArray(c.size)
println("Debug crypto_secretstream_xchacha20poly1305_pull")
val pinnedM = m.pin()
val pinnedC = c.pin()
val pinnedAd = ad.pin()
libsodium.crypto_secretstream_xchacha20poly1305_pull(state.ptr, pinnedM.addressOf(0), null,
null, pinnedC.addressOf(0), c.size.convert(), pinnedAd.addressOf(0), ad.size.convert())
pinnedM.unpin()
pinnedC.unpin()
pinnedAd.unpin()
return m
}
}