Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null key issue for Android API 23 below #38

Merged
merged 4 commits into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 32 additions & 4 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ jobs:
fail-fast: false
matrix:
os: [ macos-latest, ubuntu-latest, windows-latest ]
job: [ test ]

runs-on: ${{ matrix.os }}

Expand Down Expand Up @@ -50,17 +49,46 @@ jobs:
java-version: 11

- name: Run macOS Tests
if: matrix.os == 'macos-latest' && matrix.job == 'test'
if: matrix.os == 'macos-latest'
run: >
./gradlew check --stacktrace
-PKMP_TARGETS="JVM,JS,IOS_ARM32,IOS_ARM64,IOS_X64,IOS_SIMULATOR_ARM64,MACOS_ARM64,MACOS_X64,TVOS_ARM64,TVOS_X64,TVOS_SIMULATOR_ARM64,WATCHOS_ARM32,WATCHOS_ARM64,WATCHOS_DEVICE_ARM64,WATCHOS_X64,WATCHOS_X86,WATCHOS_SIMULATOR_ARM64,WASM,WASM_32"
- name: Run Linux Tests
if: matrix.os == 'ubuntu-latest' && matrix.job == 'test'
if: matrix.os == 'ubuntu-latest'
run: >
./gradlew check --stacktrace
-PKMP_TARGETS="JVM,JS,ANDROID,ANDROID_ARM32,ANDROID_ARM64,ANDROID_X64,ANDROID_X86,LINUX_ARM32HFP,LINUX_ARM64,LINUX_MIPS32,LINUX_MIPSEL32,LINUX_X64,WASM,WASM_32"
- name: Run Windows Tests
if: matrix.os == 'windows-latest' && matrix.job == 'test'
if: matrix.os == 'windows-latest'
run: >
./gradlew check --stacktrace
-PKMP_TARGETS="JVM,JS,MINGW_X64,MINGW_X86,WASM,WASM_32"

emulator:
runs-on: macos-latest

strategy:
fail-fast: false
matrix:
api-level: [ 15, 23, 29 ]
steps:
- name: Checkout Repo
uses: actions/checkout@v3

- name: Validate Gradle Wrapper
uses: gradle/wrapper-validation-action@v1

- name: Setup JDK
uses: actions/setup-java@v3.11.0
with:
distribution: 'zulu'
java-version: 19

- name: Build
uses: gradle/gradle-build-action@v2

- name: Run Android Instrumented Tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: ${{ matrix.api-level }}
script: ./gradlew :test-android:connectedCheck -PKMP_TARGETS="ANDROID,JVM"
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.gradle.targets.js.yarn.YarnRootExtension
@Suppress("DSL_SCOPE_VIOLATION")
plugins {
alias(libs.plugins.multiplatform) apply(false)
alias(libs.plugins.android.library) apply(false)
alias(libs.plugins.binaryCompat)
alias(libs.plugins.gradleVersions)
}
Expand Down
5 changes: 5 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
org.gradle.parallel=true
org.gradle.caching=true

android.useAndroidX=true
android.enableJetifier=true
android.disableAutomaticComponentCreation=true

kotlin.code.style=official
kotlin.js.compiler=both
kotlin.mpp.androidSourceSetLayoutVersion=2
kotlin.mpp.enableCompatibilityMetadataVariant=true
kotlin.mpp.stability.nowarn=true
kotlin.native.binary.memoryModel=experimental
Expand Down
6 changes: 6 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[versions]
android = "7.4.2"
androidxTestRunner = "1.5.2"
binaryCompat = "0.13.0"
configuration = "0.1.0-beta02"
endians = "0.1.0"
Expand All @@ -12,7 +14,11 @@ gradle-kmp-configuration = { module = "io.matthewnelson:gradle-kmp-configuration
gradle-kotlin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
gradle-maven-publish = { module = "com.vanniktech:gradle-maven-publish-plugin", version.ref = "publish" }

# Tests
androidx-test-runner = { module = "androidx.test:runner", version.ref = "androidxTestRunner" }

[plugins]
android-library = { id = "com.android.library", version.ref = "android" }
binaryCompat = { id = "org.jetbrains.kotlinx.binary-compatibility-validator", version.ref = "binaryCompat" }
multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
gradleVersions = { id = "com.github.ben-manes.versions", version.ref = "gradleVersions" }
7 changes: 5 additions & 2 deletions library/mac/src/jvmMain/kotlin/org/kotlincrypto/core/Mac.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import java.nio.ByteBuffer
import java.security.Key
import java.security.spec.AlgorithmParameterSpec
import javax.crypto.MacSpi
import javax.crypto.spec.SecretKeySpec

/**
* Core abstraction for Message Authentication Code implementations. Extends
Expand Down Expand Up @@ -56,8 +57,10 @@ protected actual constructor(
init {
commonInit(algorithm)

// So that 'javax.crypto.Mac.initialized' gets set to true
super.init(null)
// Engine.engineInit is overridden as no-op, so this does
// nothing other than set `javax.crypto.Mac.initialized`
// to true
super.init(SecretKeySpec(ByteArray(1), algorithm))
}

public actual final override fun algorithm(): String = algorithm
Expand Down
3 changes: 3 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pluginManagement {
repositories {
mavenCentral()
gradlePluginPortal()
google()
}
}

Expand All @@ -23,4 +24,6 @@ if (CHECK_PUBLICATION != null) {
).forEach { name ->
include(":library:$name")
}

include(":test-android")
}
1 change: 1 addition & 0 deletions test-android/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
Empty file.
58 changes: 58 additions & 0 deletions test-android/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (c) 2023 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
plugins {
id("configuration")
}

repositories {
google()
}

kmpConfiguration {
configure {
androidLibrary {
kotlinJvmTarget = JavaVersion.VERSION_11
compileSourceCompatibility = JavaVersion.VERSION_11
compileTargetCompatibility = JavaVersion.VERSION_11

android {
namespace = "org.kotlincrypto.test"
compileSdk = 33

defaultConfig {
minSdk = 14

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}

libraryVariants.all {
generateBuildConfigProvider.configure {
enabled = false
}
}
}

sourceSetTestInstrumented {
dependencies {
implementation(project(":library:digest"))
implementation(project(":library:mac"))
implementation(libs.androidx.test.runner)
implementation(kotlin("test"))
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2023 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
@file:Suppress("UnnecessaryOptInAnnotation")

package org.kotlincrypto.test

import org.kotlincrypto.core.InternalKotlinCryptoApi
import org.kotlincrypto.core.Mac
import javax.crypto.spec.SecretKeySpec
import kotlin.test.Test

class AndroidMacTest {

@OptIn(InternalKotlinCryptoApi::class)
class TestMac: Mac {

constructor(key: ByteArray): this(Engine("HmacSHA256", key))

private constructor(engine: Engine): super(engine.algorithm, engine)

private class Engine: Mac.Engine {

val algorithm: String
val delegate: javax.crypto.Mac

constructor(algorithm: String, key: ByteArray): super(key) {
this.algorithm = algorithm
this.delegate = getInstance(algorithm)
this.delegate.init(SecretKeySpec(key, "HmacSHA256"))
}

private constructor(state: State, engine: Engine): super(state) {
this.algorithm = engine.algorithm
this.delegate = engine.delegate.clone() as javax.crypto.Mac
}

override fun update(input: Byte) {
delegate.update(input)
}

override fun update(input: ByteArray, offset: Int, len: Int) {
delegate.update(input, offset, len)
}

override fun doFinal(): ByteArray = delegate.doFinal()
override fun macLength(): Int = delegate.macLength
override fun copy(): Mac.Engine = Engine(object : State() {}, this)
override fun reset() { delegate.reset() }
}

override fun copy(engineCopy: Mac.Engine): Mac = TestMac(engineCopy as Engine)
}

@Test
fun givenAndroid_whenMacInstantiated_thenPasses() {
// https://github.com/KotlinCrypto/core/issues/37
val key = ByteArray(50) { it.toByte() }
TestMac(key).doFinal()
}
}
18 changes: 18 additions & 0 deletions test-android/src/androidMain/kotlin/org/kotlincrypto/test/-Stub.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright (c) 2023 Matthew Nelson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
package org.kotlincrypto.test

internal fun stub() { /* no-op */ }