Upgrade to Pro — share decks privately, control downloads, hide ads and more …

K/NでCryptoKit使いたかった

RyuNen344
November 29, 2022

 K/NでCryptoKit使いたかった

RyuNen344

November 29, 2022
Tweet

More Decks by RyuNen344

Other Decks in Technology

Transcript

  1. Kotlin NativeͰCryptoKit࢖͍
    ͔ͨͬͨɾɾɾɾɾ
    2022.11.27 Mobileษڧձ Wantedly × νʔϜϥϘ #7
    Bunjiro Miyoshi

    View full-size slide

  2. About Me
    • νʔϜϥϘॴଐ


    • (ࣗশ)ϚϧνϓϥοτϑΥʔϜܳਓ


    • Jetpack Compose࠷ߴʂʂ

    View full-size slide

  3. ࿩͢͜ͱɾ࿩͞ͳ͍͜ͱ
    • ࿩͢͜ͱ


    • CryptoKitͱ͸


    • ͳͥCryptoKitΛ࢖͍͍͔ͨ


    • Kotlin NativeͷSwift/Obj-C Interop


    • ࿩͞ͳ͍͜ͱ


    • KMMͱ͸(͕࣌ؒ଍Γͳ͘ͳΔ)


    • androidଆʹدΓఴͬͨ಺༰(΄΅androidؔ܎ͳ͍࿩͔͠͠ͳ͍)


    • ඇରশ҉߸पΓ(K/NͰSecurity Framework࢖͑Δ)

    View full-size slide

  4. CryptoKitͱ͸
    • Appleެࣜͷ҉߸ԽϥΠϒϥϦ


    • iOS13͔Β࢖༻Ͱ͖Δ


    • LinuxͰ΋࢖͑ΔΑ͏ʹಛघͳϋʔυ΢ΣΞॲཧΛඞཁͱ͠ͳ͍APIΛ
    ൈ͖ग़ͨ͠OSS൛΋͋Δ


    • ݱࡏiOSͰAES΍Hash౳ͷ࣮૷Λ͍ͨ͠ͳΒ͜ΕΛ࢖͏ͷ͕σϑΝΫ
    τ

    View full-size slide

  5. ͳͥCryptoKitΛ࢖͍͍͔ͨ#Tink
    • KMMͰ҉߸ԽॲཧΛԿͰ࣮૷͢Δ͔໎ͬͨ


    • Google/TinkΛinterop͢Δ༧ఆͩͬͨ


    • androidͷ҉߸Խॲཧ͸ݱࡏTinkҰ୒ͳঢ়ଶͩ͠ɾɾɾɾ


    • arm64ରԠʹۤ͠ΜͰ͍ΔΒ͘͠ରԠΛ଴͍ͬͯΒΕͳ͍


    • → https://github.com/google/tink/issues/554

    View full-size slide

  6. ͳͥCryptoKitΛ࢖͍͍͔ͨ#CommonCrypto
    • iOSʹ͸RuntimeʹCommonCrypto͕ಉࠝ͞Ε͍ͯΔ


    • → https://opensource.apple.com/source/CommonCrypto


    • CͰ࣮૷͞Ε͍ͯΔFramework


    • ରশ҉߸(AES-CBC, AES-ECB)ɺϋογϡؔ਺ɺμΠδΣετؔ਺ͳ
    Ͳ͕࢖༻Ͱ͖Δ

    View full-size slide

  7. ͳͥCryptoKitΛ࢖͍͍͔ͨ#CommonCrypto

    View full-size slide

  8. ͳͥCryptoKitΛ࢖͍͍͔ͨ
    • CryptoKit͡Όͳ͍ͱͰ͖ͳ͍͜ͱ


    • AEAD(AES-GCM)ͳͲͷೝূϞʔυ


    • ॺ໊पΓͷॲཧ


    • ٯʹCryptoKitͩͱͰ͖ͳ͍ॲཧ΋͋Δ


    • MD2ɺ4ͳͲͷڧ౓ͷ௿͍μΠδΣετ΍͋·Γ࢖༻͠ͳ͍΋ͷͳ
    ͷͰؾʹ͠ͳͯ͘Α͍

    View full-size slide

  9. AES-GCM͕࢖͍͍ͨ
    • CommonCryptoʹ΋GCM࣮૷ͷϔομʔϑΝΠϧ͸͋Δɾɾɾɾ


    • https://opensource.apple.com/source/CommonCrypto/
    CommonCrypto-60061/lib/CommonCryptorGCM.c.auto.html


    • ඇެ։APIʹͳ͍ͬͯͯK/NͰ࢖͑ͳ͍ɾɾɾɾɾ

    View full-size slide

  10. Ͳ͏ͤϥούʔΛͭ͘ΔͳΒCryptoKitʹ͍ͨ͠
    • ͖ͬͱJetBrainsͷK/NνʔϜ΋࢖͍͍ͨͱࢥͬͯͨΜ͡Όͳ͍͔ͳͱ
    अਪ


    • ͓΋ΉΖʹKotlinͷϦϙδτϦΛோΊͯΈΔ


    • K/N͕औΓࠐΉDarwinͷAPI͸👇ʹ͓͍ͯ͋Δ


    • https://github.com/JetBrains/kotlin/tree/master/kotlin-native/
    platformLibs/src/platform/ios

    View full-size slide

  11. Μɾɾɾɾɾʁ

    View full-size slide

  12. Μɾɾɾɾɾʁ

    View full-size slide

  13. ͋ɺ͋Δʂʂʂ

    View full-size slide

  14. Swift/Objective-C Interop
    • def.disabledͱ͍͏ෆԺͳϑΝΠϧ໊ʹͳ͍ͬͯΔ


    • obj-c͔Βݺͼग़ͤΔΑ͏ʹͳͬͯΔSwift Framework͸࢖͑Δ͕pure
    SwiftͷFramework͸K/NʹऔΓࠐΊͳ੍͍໿͕͋ΔͨΊdisable

    View full-size slide

  15. Swift/Objective-C Interop
    • def.disabledͱ͍͏ෆԺͳϑΝΠϧ໊ʹͳ͍ͬͯΔ


    • obj-c͔Βݺͼग़ͤΔΑ͏ʹͳͬͯΔSwift Framework͸࢖͑Δ͕pure
    SwiftͷFramework͸K/NʹऔΓࠐΊͳ੍͍໿͕͋ΔͨΊdisable
    ཁ͢Δʹͦͷ··Ͱ͸࢖͑ͳ͍

    View full-size slide

  16. Swift/Objective-C Interop
    • ࢖͏ʹ͸@objc͕෇༩͞Ε͍ͯΔϥούʔͷϥΠϒϥϦΛ࡞੒͠ɺͦ
    ΕΛK/NʹऔΓࠐΉඞཁ͕͋Δ


    • ಉ͜͡ͱΛߟ͍͑ͯΔਓ͸͍Δ


    • https://youtrack.jetbrains.com/issue/KT-54729/KMM-Native-iOS-
    use-CryptoKit

    View full-size slide

  17. Ͳ͏͍͏ϥούʔϥΠϒϥϦ͕΄͍͔͠
    • K/NϥΠϒϥϦͱͯ͠ެ։͞Ε͍ͯΔ


    • -> KMMʹ௥Ճ͢Δ͚ͩͰCryptoKit࢖͑ΔΑ͏ʹͳͬͯ΄͍͠


    • ϥούʔϥΠϒϥϦ͸dynamic libraryͱͯ͠CryptoKitΛͱΓ͜ΜͰ΄͍͠


    • -> όΠφϦຊମʹCryptoKitͷϏϧυੜ੒෺ΛؚΜͰ΄͘͠ͳ͍


    • (౰ͨΓલ͕ͩ)CryptoKit͔ΒͷΤϥʔΛͪΌΜͱϋϯυϦϯάͰ͖ΔIFʹ
    ͳͬͯͯ΄͍͠

    View full-size slide

  18. ϥούʔϥΠϒϥϦΛ࡞Δ
    • Swift Interopʹ͸error఻ൖपΓʹ᠘͕͋Δ


    • Swift/Objective-C error-throwing methods aren't imported to Kotlin as exception-
    throwing


    • https://kotlinlang.org/docs/native-objc-interop.html#errors-and-exceptions


    • Kotlin -> Obj-CɺObj-C -> Kotlinͷ྆ํʹ੍໿͕͋Δ


    • ࠓճͷϢʔεέʔεͩͱSwift -> Obj-C ->KotlinͷܗʹͳΔͨΊͦΕͧΕͷݴޠײͷนΛߟྀͯ͠
    ࣮૷͢Δඞཁ͕͋Δ


    • iOSͷ։ൃݴޠͷσϑΝΫτ͕Obj-C͔ΒSwiftʹ੾ΓସΘͬͨλΠϛϯάͷࢿྉ͕໾ʹཱͪͦ͏

    View full-size slide

  19. ·ͱΊ
    • ࠓճ༻ҙͰ͖ͨ෦෼͸͜͜·Ͱ


    • ࢲࣗ਎͸ࠓճඞཁ෼ͷΈͷఆ͔ٛ͠ߦΘͣɺCryptoKitશମͷϥοϓʹ͸ͨͲΓண͔ͳ
    ͔ͬͨ(୭͔Ұॹʹ΍Γ·ͤΜ͔ɾɾɾ)


    • Swift InteropपΓʹ͸Τϥʔ఻ൖͷ᠘͕͋Δ


    • Swift/Kotlin௚ޓ׵ରԠͷissue͸͋Δ͕͕͔͔࣌ؒΓͦ͏ͳͷͰϥούʔϥΠϒϥϦΛ
    ࡞ΔҎ֎ʹݱঢ়͸खஈ͕ͳͦ͞͏


    • https://youtrack.jetbrains.com/issue/KT-49521/Support-direct-interoperability-with-
    Swift

    View full-size slide

  20. 12݄10೔ͷKotlin Festొஃ͠·͢


    ͓࣌ؒ͋Δ͔ͨͥͻ

    View full-size slide