All Projects → ionspin → kotlin-multiplatform-libsodium

ionspin / kotlin-multiplatform-libsodium

Licence: Apache-2.0 license
A kotlin multiplatform wrapper for libsodium, using directly built libsodium for jvm and native, and libsodium.js for js targets.

Programming Languages

kotlin
9241 projects
shell
77523 projects

Projects that are alternatives of or similar to kotlin-multiplatform-libsodium

moko-network
Network components with codegeneration of rest api for mobile (android & ios) Kotlin Multiplatform development
Stars: ✭ 107 (+181.58%)
Mutual labels:  kotlin-multiplatform
moko-maps
Control your map from common code for mobile (android & ios) Kotlin Multiplatform development
Stars: ✭ 47 (+23.68%)
Mutual labels:  kotlin-multiplatform
KmLogging
Kotlin multiplatform logging. High performance, composable and simple to use.
Stars: ✭ 21 (-44.74%)
Mutual labels:  kotlin-multiplatform
Sheasy
This an Android App that helps you share/manage your files on your Android Device through a WebInterface in the Browser - Built with Ktor and Kotlin-React
Stars: ✭ 34 (-10.53%)
Mutual labels:  kotlin-multiplatform
moko-web3
Ethereum Web3 implementation for mobile (android & ios) Kotlin Multiplatform development
Stars: ✭ 32 (-15.79%)
Mutual labels:  kotlin-multiplatform
rawr-x3dh
TypeScript Implementation of X3DH
Stars: ✭ 51 (+34.21%)
Mutual labels:  libsodium
trikot.foundation
Core utilities for Kotlin Multiplatform
Stars: ✭ 21 (-44.74%)
Mutual labels:  kotlin-multiplatform
practical cryptography engineering
Cryptography code examples using libsodium and mbedtls C libraries and Python cryptography and PyNaCl modules
Stars: ✭ 60 (+57.89%)
Mutual labels:  libsodium
kgql
GraphQL Document wrapper generator for Kotlin Multiplatform Project and Android
Stars: ✭ 54 (+42.11%)
Mutual labels:  kotlin-multiplatform
lazysodium-java
A Java implementation of the Libsodium crypto library. For the lazy dev.
Stars: ✭ 110 (+189.47%)
Mutual labels:  libsodium
Notflix
Kotlin Multiplatform playground
Stars: ✭ 272 (+615.79%)
Mutual labels:  kotlin-multiplatform
Kryptor
A simple, modern, and secure encryption and signing tool that aims to be a better version of age and Minisign.
Stars: ✭ 267 (+602.63%)
Mutual labels:  libsodium
sweekt
🍭 Some sugar to sweeten Kotlin development.
Stars: ✭ 35 (-7.89%)
Mutual labels:  kotlin-multiplatform
tv-maniac
Tv-Maniac is a Multiplatform app (Android & iOS) for viewing TV Shows from TMDB.
Stars: ✭ 55 (+44.74%)
Mutual labels:  kotlin-multiplatform
Dads
*BA DUM TSSS*
Stars: ✭ 240 (+531.58%)
Mutual labels:  kotlin-multiplatform
secrets
Not Yet Another Password Manager written in Go using libsodium
Stars: ✭ 28 (-26.32%)
Mutual labels:  libsodium
kmpapp
👨‍💻 Kotlin Mobile Multiplatform App (Android & iOS). One Code To Rule Them All. MVVM, DI (Kodein), coroutines, livedata, ktor, serialization, mockk, detekt, ktlint, jacoco
Stars: ✭ 34 (-10.53%)
Mutual labels:  kotlin-multiplatform
PopKorn
DI can be simple. Forget about modules and components. Just use it!
Stars: ✭ 139 (+265.79%)
Mutual labels:  kotlin-multiplatform
kmp mobile template
Template for the development of native iOS and Android apps with shared business-logic using KMP, Redux architecture and XcodeGen.
Stars: ✭ 21 (-44.74%)
Mutual labels:  kotlin-multiplatform
cache4k
In-memory Cache for Kotlin Multiplatform.
Stars: ✭ 124 (+226.32%)
Mutual labels:  kotlin-multiplatform

pipeline status

Danger: Experimental

Libsodium bindings for Kotlin Multiplatform

Libsodium bindings project uses libsodium c sources and libsodium.js to provide a kotlin multiplatform wrapper library for libsodium. The library is feature complete and usable.

Warning

While this library is just a wrapper around the well known Libsodium library it still comes with high potential of introducing new attack surfaces, bugs and other issues and you shouldn't use it in production until it has been reviewed by community.

Installation

Gradle

implementation("com.ionspin.kotlin:multiplatform-crypto-libsodium-bindings:0.8.6")

Snapshot builds

repositories {
    maven("https://oss.sonatype.org/content/repositories/snapshots")
}
implementation("com.ionspin.kotlin:multiplatform-crypto-libsodium-bindings:0.8.7-SNAPSHOT")

Usage

Before using the wrapper you need to initialize the underlying libsodium library. You can use either a callback or coroutines approach

    LibsodiumInitializer.initializeWithCallback {
        // Libsodium initialized
    }
    suspend fun initalizeProject() {
        ...
        LibsodiumInitializer.intialize()
        ...
    }

After intiailization you can call libsodium functions directly

The API is very close to libsodium but still adapted to kotlin standards, as an example here is the usage of authenticated encryption api:

libsodium:

    #define MESSAGE ((const unsigned char *) "test")
    #define MESSAGE_LEN 4
    #define CIPHERTEXT_LEN (crypto_secretbox_MACBYTES + MESSAGE_LEN)
    
    unsigned char key[crypto_secretbox_KEYBYTES];
    unsigned char nonce[crypto_secretbox_NONCEBYTES];
    unsigned char ciphertext[CIPHERTEXT_LEN];
    
    crypto_secretbox_keygen(key);
    randombytes_buf(nonce, sizeof nonce);
    crypto_secretbox_easy(ciphertext, MESSAGE, MESSAGE_LEN, nonce, key);
    
    unsigned char decrypted[MESSAGE_LEN];
    if (crypto_secretbox_open_easy(decrypted, ciphertext, CIPHERTEXT_LEN, nonce, key) != 0) {
        /* message forged! */
    }

kotlin:

    val message = ("Ladies and Gentlemen of the class of '99: If I could offer you " +
                   "only one tip for the future, sunscreen would be it.").encodeToUByteArray()

    val key = LibsodiumRandom.buf(32)

    val nonce = LibsodiumRandom.buf(24)

    val encrypted = SecretBox.easy(message, nonce, key)
    val decrypted = SecretBox.openEasy(encrypted, nonce, key)

If message cannot be verified, openEasy function will throw a SecretBoxCorruptedOrTamperedDataExceptionOrInvalidKey

In some cases libsodium C api returns two values, usually encrypted data and a autogenerated nonce. In situations like those, kotlin API returns a data class wrapping both objects. An example of this behavior is initializing the secret stream, where initialization funciton returns both the header and state:

libsodium:

    crypto_secretstream_xchacha20poly1305_state state;
    unsigned char key[crypto_secretstream_xchacha20poly1305_KEYBYTES];
    unsigned char header[crypto_secretstream_xchacha20poly1305_HEADERBYTES];
    
    /* Set up a new stream: initialize the state and create the header */
    crypto_secretstream_xchacha20poly1305_init_push(&state, header, key);

kotlin: This is what the response data class definition looks like:

    data class SecretStreamStateAndHeader(val state: SecretStreamState, val header : UByteArray)

And here is the usage sample

    val key = LibsodiumRandom.buf(crypto_secretstream_xchacha20poly1305_KEYBYTES)
    val stateAndHeader = SecretStream.xChaCha20Poly1305InitPush(key)
    val state = stateAndHeader.state
    val header = stateAndHeader.header 

The functions are mapped from libsodium to kotiln objects, so crypto_secretstream_xchacha20poly1305_init_push becomes SecretStream.xChaCha20Poly1305InitPush Alt text

At the moment you should refer to original libsodium documentation for instructions on how to use the library

Supported native platforms

Currently supported native platforms (Apple Silicon is supported since 0.8.5) :

Platform Supported
Linux X86 64 ✔️
Linux Arm 64 ✔️
Linux Arm 32
macOS X86 64 ✔️
macOS Arm 64 (Apple Silicon) ✔️
iOS x86 64 ✔️
iOS Arm 64 ✔️
iOS Arm 32 ✔️
iOS Simulator Arm 64 (Apple Silicon) ✔️
watchOS X86 32 ✔️
watchOS Arm 64(_32) ✔️
watchOS Arm 32 ✔️
watchOS Simulator Arm 64 (Apple Silicon) ✔️
tvOS X86 64 ✔️
tvOS Arm 64 ✔️
tvOS Simulator Arm 64 (Apple Silicon) ✔️
minGW X86 64 ✔️
minGW X86 32

List of supported bindings

Where do the compiled libraries used by JVM and Android come from

Android .so files come from running dist-build scripts in libsodium which you can find in the libsodium submodule Java Linux Arm/X86_64 and Mac so and dylib are the same as produced by multiplatform builds, also based on the same submodule commit Java Windows dll is from https://download.libsodium.org/libsodium/releases/libsodium-1.0.18-stable-msvc.zip

TODO:

  • Improve documentation
  • Running tests on Android
  • Complete exposing libsodium constants
  • Build MSVC so it's binaries are completely equal
  • Find a better way of fetching Konan dependencies than having a dummy project.

Building

Clone the git, init the submodule and run ./gradlew build. Note that current build settings are such that only linux builds js target.

Notes for Gitlab runners:

  • At the moment all runners need to have android sdk present even though not all are building Android build

Windows:

  • Needs android sdk
  • Git needs long file path enabled
  • msys2 needs to be installed and pacman update executed
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].