Objective-C bindings for the excellent TweetNaCl cryptographic library.
Because TweetNaCl is a complete implementation of all the public functions of the original NaCl library, the bindings should work with that library as well.
This is work in progress and the bindings are neither complete nor stable yet. Stay tuned for updates.
Public-key authenticated encryption: ObjcNaClBox
ObjcNaClBox is one of the two end-points of a public-key authenticated encryption communications channel. Each end-point holds 1) its secret key and 2) the public key corresponding to the secret key of the other end-point.
ObjcNaClBoxPublicKey is the public part of a keypair. It is derived completely from its corresponding secret key.
ObjcNaClBoxSecretKey is the secret part of a keypair. Must be kept secret.
Alice wants to send a secret message to Bob.
Alice obtains her secret key and Bobs public key.
ObjcNaClBoxSecretKey *alicesSecretKey = [ObjcNaClBoxSecretKey keyWithData:sk error:&error]; ObjcNaClBoxPublicKey *bobsPublicKey = [ObjcNaClBoxPublicKey keyWithData:pk error:&error];
Next, she creates her communications end-point.
ObjcNaClBox *box = [ObjcNaClBox boxWithSecretKey:alicesSecretKey publicKey:bobsPublicKey];
Finally, she encrypts the
messagewith a nonce and sends the
cipherto Bob, e.g., over a TCP socket.
NSData *message = ...; ObjcNaClBoxNonce *nonce = [ObjcNaClBoxNonce nonceWithData:n error:&error]; NSData *cipher = [box encryptMessage:message withNonce:nonce error:&error];
Bob recieves a message purportedly from Alice.
Bob extracts a
noncefrom the message.
NSData *cipher = ...; ObjcNaClBoxNonce *nonce = [ObjcNaClBoxNonce nonceWithData:n error:&error];
Bob obtains his secret key and Alice's public key and creates his encryption box.
ObjcNaClBoxSecretKey *bobsSecretKey = [ObjcNaClBoxSecretKey keyWithData:sk error:&error]; ObjcNaClBoxPublicKey *alicesPublicKey = [ObjcNaClBoxPublicKey keyWithData:pk error:&error]; ObjcNaClBox *box = [ObjcNaClBox boxWithSecretKey:bobsSecretKey publicKey:alicesPublicKey];
He decrypts the message.
NSData *message = [box decryptCipher:cipher withNonce:nonce error:&error];
That's the simple case. In the real world, a proper security protocol has to be established. In the example above, if Eve is able to record the encrypted messages (
nonce) passed between Alice and Bob, and then manages to get hold of Bob's secret key, she will be able to decrypt the whole communication.
The standard way to deal with this is for Alice and Bob to generate temporary keypairs and send each other the public counterparts of these new keys using the encryption boxes described above, i.e., with their "long-term" or "long-lived" keys. When this key exchange has completed, all further communication is done using the new temporary keys. When communication ends the temporary keys are discarded. New keys might need to be negotiated periodically duriong communication.
There are more details that need to be taken care of as well: nonce selection, discarding messages with already seen nonces, timestamps during key exchange, etc, but it all depends on the high-level protcol.