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

Can Themis be used from a Swift project on Linux? #198

Closed
popaaaandrei opened this issue May 18, 2017 · 15 comments
Closed

Can Themis be used from a Swift project on Linux? #198

popaaaandrei opened this issue May 18, 2017 · 15 comments
Labels
core Themis Core written in C, its packages

Comments

@popaaaandrei
Copy link

Hello,

Can anyone tried integrating Themis library in a Swift project on Linux?
Theoretically it should work by creating a module map around the C++ library but has anyone succeeded?

I want to exchange data securely between iOS and a Vapor backend deployed on Ubuntu.

Thank you!

@vixentael
Copy link
Contributor

Well, Themis works on Linux, and Themis works on iOS Swift via ObjC bridging headers. I believe there shouldn't be any issues that prevent Themis to work directly in Swift projects :)

If you have any experience in building module map around C core, could you help us? Our Linux-gur @mnaza can help with any core-related issues.

@vixentael vixentael added core Themis Core written in C, its packages help wanted ❤️ labels May 18, 2017
@mnaza
Copy link
Contributor

mnaza commented May 19, 2017

TR;DL Yes. Themis can be used from a Swift project on Linux
I newer before use Swift, but decide to try:

  1. create package for Themis (I call it CThemis):
[]$ mkdir CThemis
[]$ cd CThemis
[]$ echo "module CThemis [system] {
    header \"/usr/include/themis/themis.h\"
    link \"themis\"
    export *
}" > module.modulemap
[]$ cd -
  1. for key pair generation with Temis create file main.swift with next context:
import CThemis

var res: Int32
var private_key_length: Int = 0
var public_key_length: Int = 0

// private and public keys length determination
res = themis_gen_ec_key_pair(nil, &private_key_length, nil, &public_key_length)
if res != SOTER_BUFFER_TOO_SMALL {
    print("private & public keys length determination error")
    exit(-1)
}

// private and public key memory allocation
var private_key = [UInt8](repeating: 0, count: private_key_length)
var public_key = [UInt8](repeating: 0, count: public_key_length)

// private and public keys generation
res = themis_gen_ec_key_pair(UnsafeMutablePointer(mutating: private_key), &private_key_length, UnsafeMutablePointer(mutating: public_key), &public_key_length)
if res != SOTER_SUCCESS {
    print("private & public keys generation error")
    exit(-1)
}

//result output
print(private_key, private_key_length)
print(private_key, private_key_length)
  1. build app with
swiftc -I ./CThemis main.swift
  1. run:
./main

all other Themis functions can be imported to Swift by same way.

@vixentael
Copy link
Contributor

Thank you, @mnaza!

I think we should consider building "pure Swift" wrapper for those who use Swift on server.

@popaaaandrei
Copy link
Author

Thank you, @mnaza Really appreciate your effort 👍

@gene-eu-zz
Copy link

@mnaza, it feels like @popaaaandrei will need to know some internals to succeed in his goal (e.g. understanding the sequence of C Themis calls to get correct results). If you've went the most of the way already, maybe you can invest a few minutes to draft out the rest of the functions?

Style-inferiority and "Swift-ness" is something @vixentael and @popaaaandrei (if he wants to) can easily correct, but if @popaaaandrei's goal is to use "secure data exchange", that's Secure Session, and it may be not very trivial to write a wrapper for it without really good understanding of how it works.

Maybe you could invest some more time on this and push this to /wrappers/?

@popaaaandrei
Copy link
Author

Thank you all for support!
I can help with Swift-ness :) My goal is to send encrypted data from the hospital (Swift on Ubuntu) to the patients (iOS and Android) based on public key crypto and I think Secure Message is would fit perfectly. Secure Session seems like too much for what I need.

@popaaaandrei
Copy link
Author

@gene-eu @mnaza I made a wrapper for Secure Message using your code and ObjC wrappers as a reference.

@vixentael
Copy link
Contributor

wow, can you share it please @popaaaandrei

@popaaaandrei
Copy link
Author

popaaaandrei commented May 26, 2017

CThemis.zip
test.zip

First is the C module required. Second is a test project with key generation and encryption + decryption.

Works on Ubuntu 16.04, Swift-3.1-RELEASE

Thank you all!

@popaaaandrei
Copy link
Author

I didn't know how to suppress the ... was never mutated, consider changing to 'let'. I am a little concerned with changing to let while using UnsafeMutablePointer(mutating: ...)

@mnaza
Copy link
Contributor

mnaza commented May 28, 2017

I create Themis wrapper for Swift for Linux for all Themis primities (except Secure Comparator, for now):
modulemap
wrapper

@popaaaandrei , @vixentael , check it please.

Note: wrapper include examples folder with example project. Before use it, Themis must be installed with: make install

@gene-eu-zz
Copy link

... it would be great when you finish polishing this, come up with distribution scheme that doesn't involve 3 repos, and document things properly ;)

@popaaaandrei
Copy link
Author

I can only confirm that Secure Message (both modes encrypt/decrypt and sign/verify) works for me bi-directionally between an Ubuntu endpoint and an iOS app. Thank you @mnaza

@gene-eu-zz
Copy link

gene-eu-zz commented May 31, 2017

I can only confirm that Secure Message (both modes encrypt/decrypt and sign/verify) works for me bi-directionally between an Ubuntu endpoint and an iOS app.

Great to hear! Thanks for your help and bringing this up.

@vixentael
Copy link
Contributor

Thank you guys, I've separated issue about making Swift Themis better into separate issue (#226)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Themis Core written in C, its packages
Projects
None yet
Development

No branches or pull requests

4 participants