jedisct1 / swift-sodium

Safe and easy to use crypto for iOS and macOS
ISC License
519 stars 185 forks source link

Is it possible to use the C level libsodium functions at all? #174

Closed aldigjo closed 5 years ago

aldigjo commented 5 years ago

I need to use one of the functions not exposed in the swift API, I know this isn't exactly encouraged, but my alternative to this lib is to install libsodium directly. I need to be able to derive a publickey for a corresponding secret key, so I need to use crypto_scalarmult_base, how can I use this function directly?

jedisct1 commented 5 years ago

If you have a secret key, it includes a copy of the public key in the second 32-bytes half.

aldigjo commented 5 years ago

How? the secret key itself is 32 bytes

jedisct1 commented 5 years ago

Keys used for signatures include a copy of the public key.

For key exchange, if you need to store a single key, you should store the seed, so that you can call KeyExchange.keypairFromSeed() with the same seed later in order to recover the public key.

For signing, the C library has a crypto_sign_ed25519_sk_to_pk() function, so maybe a crypto_kx_sk_to_pk() function could be added as well, but I'm not convinced that this is necessary since it would be redundant with the _from_seed() function, and a secret key can be much bigger than the seed.

That being said, you should be able to call existing C functions from Swift by importing Clibsodium.