Skip to content

Commit

Permalink
feat(keyset reader): impl JsonKeysetReader
Browse files Browse the repository at this point in the history
  • Loading branch information
RyuNen344 committed Apr 22, 2023
1 parent 46d3edc commit 09e3ab2
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 50 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.github.ryunen344.tink

typealias NativeJsonKeysetReader = com.google.crypto.tink.JsonKeysetReader

actual class JsonKeysetReader actual constructor(bytes: ByteArray) :
KeysetReader(NativeJsonKeysetReader.withBytes(bytes)) {
actual constructor(json: String) : this(json.encodeToByteArray())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.github.ryunen344.tink.exception

actual typealias JsonException = com.google.gson.JsonParseException

This file was deleted.

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

import io.github.ryunen344.tink.exception.JsonException

expect class JsonKeysetReader @Throws(JsonException::class) constructor(bytes: ByteArray) : KeysetReader {
@Throws(JsonException::class)
constructor(json: String)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.github.ryunen344.tink.exception

expect class JsonException : RuntimeException
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.github.ryunen344.tink.daead

import io.github.ryunen344.tink.JsonKeysetReader
import io.github.ryunen344.tink.KeyTemplateSet
import io.github.ryunen344.tink.KeysetHandleGenerator
import io.github.ryunen344.tink.exception.GeneralSecurityException
import io.github.ryunen344.tink.generateNew
import io.github.ryunen344.tink.getPrimitive
import io.github.ryunen344.tink.readClearText
import io.github.ryunen344.tink.template
import kotlin.test.BeforeTest
import kotlin.test.Test
Expand Down Expand Up @@ -102,4 +104,35 @@ class DeterministicAeadTest {
otherDaead.decryptDeterministically(ciphertext, associatedData)
}
}

@Test
fun test_encrypt_given_json_keyset_then_success() {
val handle = KeysetHandleGenerator.readClearText(JsonKeysetReader(JSON_DAEAD_KEYSET))
val daead = handle.getPrimitive(DeterministicAead::class)
val plaintext: ByteArray = "plaintext".encodeToByteArray()
val associatedData: ByteArray = "associatedData".encodeToByteArray()
val ciphertext = daead.encryptDeterministically(plaintext, associatedData)
val decrypted = daead.decryptDeterministically(ciphertext, associatedData)
assertContentEquals(plaintext, decrypted)
}

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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ actual fun KeysetHandleGenerator.Companion.read(
reader: KeysetReader,
aead: Aead,
): KeysetHandle = memScopedInstance(
block = { KeysetHandle(keysetReader = reader, andKey = (aead as DarwinAead).native, error = it.ptr) },
block = { KeysetHandle(keysetReader = reader.native, andKey = (aead as DarwinAead).native, error = it.ptr) },
onError = { throw GeneralSecurityException(cause = it.asThrowable()) }
)

@Throws(GeneralSecurityException::class)
actual fun KeysetHandleGenerator.Companion.readClearText(reader: KeysetReader): KeysetHandle = memScopedInstance(
block = {
TINKKeysetHandle.create(cleartextKeysetHandleWithKeysetReader = reader, error = it.ptr)
TINKKeysetHandle.create(cleartextKeysetHandleWithKeysetReader = reader.native, error = it.ptr)
?: throw GeneralSecurityException(cause = it.value?.asThrowable())
},
onError = { throw GeneralSecurityException(cause = it.asThrowable()) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package io.github.ryunen344.tink

actual typealias KeysetReader = com.google.crypto.tink.TINKKeysetReader
import com.google.crypto.tink.TINKKeysetReader

actual open class KeysetReader(val native: TINKKeysetReader)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.ryunen344.tink

import com.google.crypto.tink.TINKJSONKeysetReader
import io.github.ryunen344.tink.exception.JsonException
import io.github.ryunen344.tink.util.asThrowable
import io.github.ryunen344.tink.util.memScopedInstance
import io.github.ryunen344.tink.util.toNSData
import kotlinx.cinterop.ptr

actual class JsonKeysetReader actual constructor(bytes: ByteArray) : KeysetReader(
native = memScopedInstance(
block = { TINKJSONKeysetReader(serializedKeyset = bytes.toNSData(), error = it.ptr) },
onError = { throw JsonException(cause = it.asThrowable()) }
)
) {
actual constructor(json: String) : this(json.encodeToByteArray())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.github.ryunen344.tink.exception

actual class JsonException(message: String? = null, cause: Throwable? = null) : RuntimeException(message, cause)

0 comments on commit 09e3ab2

Please sign in to comment.