Skip to content

Commit

Permalink
feat(writer): remove unavailable interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
RyuNen344 committed May 3, 2023
1 parent 9aa5b8f commit e93a7b3
Show file tree
Hide file tree
Showing 12 changed files with 63 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ typealias NativeBinaryKeysetWriter = com.google.crypto.tink.BinaryKeysetWriter
actual class BinaryKeysetWriter(
private val os: ByteArrayOutputStream = ByteArrayOutputStream(),
) : KeysetWriter(NativeBinaryKeysetWriter.withOutputStream(os)) {

actual constructor() : this(ByteArrayOutputStream())

override fun write(): ByteArray = os.toByteArray()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import java.io.ByteArrayOutputStream

typealias NativeJsonKeysetWriter = com.google.crypto.tink.JsonKeysetWriter

actual class JsonKeysetWriter(
class JsonKeysetWriter(
private val os: ByteArrayOutputStream = ByteArrayOutputStream(),
) : KeysetWriter(NativeJsonKeysetWriter.withOutputStream(os)) {

actual constructor() : this(ByteArrayOutputStream())

override fun write(): ByteArray = os.toByteArray()
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package io.github.ryunen344.tink
import com.google.crypto.tink.CleartextKeysetHandle
import io.github.ryunen344.tink.aead.Aead
import io.github.ryunen344.tink.aead.AndroidAead
import io.github.ryunen344.tink.aead.NativeAead
import io.github.ryunen344.tink.daead.AndroidDeterministicAead
import io.github.ryunen344.tink.daead.DeterministicAead
import io.github.ryunen344.tink.exception.GeneralSecurityException
Expand All @@ -22,13 +21,7 @@ import kotlin.reflect.KClass

actual typealias KeysetHandle = com.google.crypto.tink.KeysetHandle

@Throws(GeneralSecurityException::class, IOException::class)
actual fun KeysetHandle.write(writer: KeysetWriter, aead: Aead) = writeWithAssociatedData(writer, aead, ByteArray(0))

@Throws(GeneralSecurityException::class, IOException::class)
actual fun KeysetHandle.writeWithAssociatedData(writer: KeysetWriter, aead: Aead, associatedData: ByteArray) =
writeWithAssociatedData(writer, aead as NativeAead, associatedData)

@Suppress("EXTENSION_SHADOWED_BY_MEMBER")
@Throws(GeneralSecurityException::class, IOException::class)
actual fun KeysetHandle.writeNoSecret(writer: KeysetWriter) = writeNoSecret(writer)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package io.github.ryunen344.tink

import io.github.ryunen344.tink.aead.AeadConfig
import io.github.ryunen344.tink.aead.register
import io.github.ryunen344.tink.signature.SignatureConfig
import io.github.ryunen344.tink.signature.register
import kotlin.test.Test

class KeysetReaderTest {
Expand All @@ -17,4 +19,22 @@ class KeysetReaderTest {
it.printStackTrace()
}
}

@Test
fun test_signature() {
SignatureConfig.register()

val privateHandle = KeysetHandleGenerator.generateNew(KeyTemplateSet.ECDSA_P256.template())
val publicHandle = privateHandle.publicKeysetHandle()

JsonKeysetWriter().run {
privateHandle.writeCleartext(this)
println(write().decodeToString())
}

JsonKeysetWriter().run {
publicHandle.writeNoSecret(this)
println(write().decodeToString())
}
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package io.github.ryunen344.tink

expect class BinaryKeysetWriter : KeysetWriter
expect class BinaryKeysetWriter() : KeysetWriter

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package io.github.ryunen344.tink

import io.github.ryunen344.tink.aead.Aead
import io.github.ryunen344.tink.exception.GeneralSecurityException
import io.github.ryunen344.tink.exception.IOException
import kotlin.reflect.KClass

expect class KeysetHandle

@Throws(GeneralSecurityException::class, IOException::class)
expect fun KeysetHandle.write(writer: KeysetWriter, aead: Aead)

@Throws(GeneralSecurityException::class, IOException::class)
expect fun KeysetHandle.writeWithAssociatedData(writer: KeysetWriter, aead: Aead, associatedData: ByteArray)

@Throws(GeneralSecurityException::class, IOException::class)
expect fun KeysetHandle.writeNoSecret(writer: KeysetWriter)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
package io.github.ryunen344.tink

import io.github.ryunen344.tink.aead.AeadConfig
import io.github.ryunen344.tink.aead.register
import io.github.ryunen344.tink.daead.DeterministicAeadConfig
import io.github.ryunen344.tink.daead.register
import kotlin.test.Test
import kotlin.test.assertEquals

class KeysetWriterTest {
@Test
fun test_read() {
runCatching {
AeadConfig.register()
val handle = KeysetHandleGenerator.generateNew(KeyTemplateSet.AES256_GCM.template())
val writer = JsonKeysetWriter()
handle.writeCleartext(writer)
println(writer.write().decodeToString())
}.onFailure {
it.printStackTrace()
}
DeterministicAeadConfig.register()
val handle = KeysetHandleGenerator.readClearText(JsonKeysetReader(JSON_DAEAD_KEYSET))
val writer = BinaryKeysetWriter()
handle.writeCleartext(writer)
assertEquals(
DAEAD_KEYSET_CONTENT,
writer.write().contentToString()
)
}
}

// {"primaryKeyId":1767300617,"key":[{"keyData":{"typeUrl":"type.googleapis.com/google.crypto.tink.AesGcmKey","value":"GiAJ8tStZdr5C1KQ7EJUOO57IkyKuFUXLvSJBTcQTdhgkw==","keyMaterialType":"SYMMETRIC"},"status":"ENABLED","keyId":1767300617,"outputPrefixType":"TINK"}]}
private companion object {
val JSON_DAEAD_KEYSET = """
{
"primaryKeyId": 961932622,
"key": [
{
"keyData": {
"typeUrl": "type.googleapis.com/google.crypto.tink.AesSivKey",
"keyMaterialType": "SYMMETRIC",
"value": "EkCJ9r5iwc5uxq5ugFyrHXh5dijTa7qalWUgZ8Gf08RxNd545FjtLMYL7ObcaFtCSkvV2+7u6F2DN+kqUjAfkf2W"
},
"outputPrefixType": "TINK",
"keyId": 961932622,
"status": "ENABLED"
}
]
}
""".trimIndent()

// content [8, -70, -59, -34, -40, 12, 18, 100, 10, 88, 10, 48, 116, 121, 112, 101, 46, 103, 111, 111, 103, 108, 101, 97, 112, 105, 115, 46, 99, 111, 109, 47, 103, 111, 111, 103, 108, 101, 46, 99, 114, 121, 112, 116, 111, 46, 116, 105, 110, 107, 46, 65, 101, 115, 71, 99, 109, 75, 101, 121, 18, 34, 26, 32, 110, -6, 90, 23, -77, 39, 59, 107, -36, -94, 14, -51, 64, 20, -117, 48, -23, 121, 97, 100, 105, 2, -127, 98, 17, 104, 117, -88, 44, -53, 16, -119, 24, 1, 16, 1, 24, -70, -59, -34, -40, 12, 32, 1]
// nsstring null
// ���� d
// X
// 0type.googleapis.com/google.crypto.tink.AesGcmKey" n�Z�';kܢ�@�0�yadi�bhu�,������
val DAEAD_KEYSET_CONTENT =
"[8, -50, -38, -41, -54, 3, 18, -124, 1, 10, 120, 10, 48, 116, 121, 112, 101, 46, 103, 111, 111, 103, 108, 101, 97, 112, 105, 115, 46, 99, 111, 109, 47, 103, 111, 111, 103, 108, 101, 46, 99, 114, 121, 112, 116, 111, 46, 116, 105, 110, 107, 46, 65, 101, 115, 83, 105, 118, 75, 101, 121, 18, 66, 18, 64, -119, -10, -66, 98, -63, -50, 110, -58, -82, 110, -128, 92, -85, 29, 120, 121, 118, 40, -45, 107, -70, -102, -107, 101, 32, 103, -63, -97, -45, -60, 113, 53, -34, 120, -28, 88, -19, 44, -58, 11, -20, -26, -36, 104, 91, 66, 74, 75, -43, -37, -18, -18, -24, 93, -125, 55, -23, 42, 82, 48, 31, -111, -3, -106, 24, 1, 16, 1, 24, -50, -38, -41, -54, 3, 32, 1]"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package io.github.ryunen344.tink
import io.github.ryunen344.tink.util.toByteArray

actual class BinaryKeysetWriter : KeysetWriter() {
override fun write(): ByteArray = data?.toByteArray() ?: ByteArray(0)
override fun write(): ByteArray = value?.toByteArray() ?: ByteArray(0)
}

This file was deleted.

18 changes: 2 additions & 16 deletions tink/src/iosMain/kotlin/io/github/ryunen344/tink/KeysetHandle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import io.github.ryunen344.tink.signature.PublicKeySign
import io.github.ryunen344.tink.signature.PublicKeyVerify
import io.github.ryunen344.tink.util.asThrowable
import io.github.ryunen344.tink.util.memScopedInstance
import io.github.ryunen344.tink.util.toByteArray
import kotlinx.cinterop.ptr
import kotlinx.cinterop.value
import platform.Foundation.NSData
Expand All @@ -31,28 +30,15 @@ import kotlin.reflect.KClass

actual typealias KeysetHandle = com.google.crypto.tink.TINKKeysetHandle

@Throws(GeneralSecurityException::class, IOException::class)
actual fun KeysetHandle.write(writer: KeysetWriter, aead: Aead) = writeWithAssociatedData(writer, aead, ByteArray(0))

@Throws(GeneralSecurityException::class, IOException::class)
actual fun KeysetHandle.writeWithAssociatedData(writer: KeysetWriter, aead: Aead, associatedData: ByteArray) {
writer.data = serializedKeyset()
}

@Throws(GeneralSecurityException::class, IOException::class)
actual fun KeysetHandle.writeNoSecret(writer: KeysetWriter) = memScopedInstance(
block = { writer.data = serializedKeysetNoSecret(it.ptr) },
block = { writer.value = serializedKeysetNoSecret(it.ptr) },
onError = { throw GeneralSecurityException(cause = it.asThrowable()) }
)

@Throws(GeneralSecurityException::class, IOException::class)
actual fun KeysetHandle.writeCleartext(writer: KeysetWriter) {
val ret = serializedKeyset()

println("content ${ret.toByteArray().contentToString()}")
println("nsstring ${ret.toNSString()?.toKString()}")

writer.data = serializedKeyset()
writer.value = serializedKeyset()
}

@Suppress("CAST_NEVER_SUCCEEDS")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package io.github.ryunen344.tink
import platform.Foundation.NSData

actual abstract class KeysetWriter {
var data: NSData? = null
var value: NSData? = null
actual abstract fun write(): ByteArray
}

0 comments on commit e93a7b3

Please sign in to comment.