Using Themis with Go

Introduction

The gothemis package provides access to the features and functions of the Themis cryptographic library:

  • Key generation: the creation of public/private key pairs, used in Secure Message and Secure Session.
  • Secure Message: the secure exchange of messages between two parties. RSA + PSS + PKCS#7 or ECC + ECDSA (based on key choice), AES GCM container.
  • Secure Storage (aka Secure Cell): provides secure storage of record based data through symmetric encryption and data authentication. AES GCM / AES CTR containers.
  • Secure Session: the establishment of a session between two peers, within which the data can be securely exchanged with higher security guarantees. EC + ECDH, AES container.

There are also example console utils available for the Go wrapper for Themis (as well as for some other wrappers — see the full list here). They help understand the specific mechanics of encryption/decryption processes of this specific wrapper. You can find the example console utils for the Go wrapper here.

Quickstart

Getting gothemis

Requirements

gothemis is a wrapper for Themis. You have to install or build Themis before using gothemis. In addition to the common set of build tools, Themis currently requires either the OpenSSL or LibreSSL package with the developer version of the package (as it provides header files). In either case, we strongly recommend you using the most recent version of these packages.

Installing Themis

Get Themis source code from GitHub:

git clone https://github.com/cossacklabs/themis.git

Note that the default installation process assumes the use of the standard LibreSSL/OpenSSL library and will install Themis to the standard /usr/lib and /usr/include locations.

In a typical case, all you need to do is (depending on your rights, sudo might be necessary):

make install

If your path is different, please see Building and installing for more details on how to change it.

To build and run basic tests, you can use:

make test

To build and run Go test suit, you can use:

make prepare_tests_all test_go

All the tests need to be passed with no errors.

Getting gothemis

If you have proper GoLang installation and $GOPATH set, just use go get to get gothemis and use it in your Go project:

    go get github.com/cossacklabs/themis/gothemis/...

Examples

  • gothemis test files are simple self-describing and easy-to-understand examples of our APIs usage scenarios. Feel free to explore them.

  • More code samples for Themis objects are available in the docs/examples/go folder.

Using Themis

Keypair generation

Themis supports both Elliptic Curve and RSA algorithms for asymmetric cryptography. Algorithm type is chosen according to the generated key type. Asymmetric keys are needed for Secure Message and Secure Session objects.

WARNING: When you distribute private keys to your users, make sure the keys are sufficiently protected. You can find the guidelines here.

Note: When using public keys of other peers, make sure they come from trusted sources.

Keypair generation

You can generate keypairs like this:

import "github.com/cossacklabs/themis/gothemis/keys"

/* or keys.KEYTYPE_RSA */
keyPair, err := keys.New(keys.KEYTYPE_EC)
priv := keyPair.Private
pub := keyPair.Public

WARNING: When you distribute private keys to your users, make sure the keys are sufficiently protected. You can find the guidelines here.

NOTE: When using public keys of other peers, make sure they come from trusted sources.

Secure Message

The Secure Message functions provide a sequence-independent, stateless, contextless messaging system. This may be preferred in cases that don't require frequent sequential message exchange and/or in low-bandwidth contexts. This is secure enough to exchange messages from time to time, but if you'd like to have Perfect Forward Secrecy and higher security guarantees, please consider using Secure Session instead.

The Secure Message functions offer two modes of operation:

In Sign/Verify mode, the message is signed using the sender's private key and is verified by the receiver using the sender's public key. The message is packed in a suitable container and ECDSA is used by default to sign the message (when RSA key is used, RSA+PSS+PKCS#7 digital signature is used).

In Encrypt/Decrypt mode, the message will be encrypted with a randomly generated key (in RSA) or a key derived by ECDH (in ECDSA), via symmetric algorithm with Secure Cell in seal mode (keys are 256 bits long).

The mode is selected by the sender supplying a valid public key of the receiver (encrypt/decrypt) or setting this parameter to NULL, or an empty string to use sign/verify.

You can read more about Secure Message's cryptographic internals.

Processing messages

1. Create SecureMessage object with your PrivateKey and recipient's PublicKey:

import "github.com/cossacklabs/themis/gothemis/message"

// for encryption
encryptor := message.New(yourPrivateKey, peerPublicKey)

// for signing
signer := message.New(yourPrivateKey, nil)

// for signature verification
verifier := message.New(nil, peerPublicKey)

2. Process each outgoing message:

encryptedMessage, err := encryptor.Wrap(messageToSend)
signedMessage, err := signer.Sign(messageToSign)

3. Process each incoming message:

receivedMessage, err := encryptor.Unwrap(encryptedMessage)
verifiedMessage, err := verifier.Verify(signedMessage)

Note: You may use encryptor here to sign/verify messages as well, but not vice versa.

Secure Cell

The Secure Сell functions provide the means of protection for arbitrary data contained in stores, such as database records or filesystem files. These functions provide both strong symmetric encryption and data authentication mechanisms.

The general approach is that given:

  • input: some source data to protect;
  • key: a password;
  • context: plus an optional "context information";

Secure Cell functions will produce:

  • cell: the encrypted data;
  • authentication tag: some authentication data.

The purpose of the optional "context information" (i.e. a database row number or filename) is to establish a secure association between this context and the protected data. In short, even when the password is known, if the context is incorrect, the decryption will fail.

The purpose of using the authentication data is to validate that given a correct password (and context), the decrypted data is indeed the same as the original source data.

The authentication data must be stored somewhere. The most convenient way is to simply append it to the encrypted data, but this is not always possible due to the storage architecture of your application. The Secure Cell functions offer variants that address this issue in different ways.

The encryption algorithm used by Secure Cell (by default) is AES-256. The length of the generated authentication data is 16 bytes.

Secure Cell is available in 3 modes:

  • Seal mode: the mode that is the most secure and easy to use. This is your best choice most of the time.
  • Token protect mode: the mode that is the most secure and easy to use. Also your best choice most of the time.
  • Context imprint mode: length-preserving version of Secure Cell with no additional data stored. Should be used carefully.

You can learn more about the underlying considerations, limitations, and features.

Initialising Secure Cell

Create SecureCell object to protect your data:

import "github.com/cossacklabs/themis/gothemis/cell"

secureCell := cell.New(secretKey, cell.CELL_MODE_SEAL)

// or
secureCell := cell.New(secretKey, cell.CELL_MODE_TOKEN_PROTECT)

// or
secureCell := cell.New(secretKey, cell.CELL_MODE_CONTEXT_IMPRINT)

NOTE: More about Secure Cell modes and which to choose here.

Secure Cell Seal Mode

Initialise cell:

secureCell := cell.New(secretKey, cell.CELL_MODE_SEAL)

Encrypt:

// context is optional
protectedData, _, err := secureCell.Protect(data, context)

Second parameter _ is additional data that is nil in this mode.

Decrypt:

The context should be same as in the protect function call for successful decryption.

// context is optional
data, err := secureCell.Unprotect(protectedData, nil, context)
Secure Cell Token-protect Mode

Initialise cell:

secureCell := cell.New(secretKey, cell.CELL_MODE_TOKEN_PROTECT)

Encrypt:

// context is optional
protectedData, additionalData, err := secureCell.Protect(data, context)

In this mode result has additional data (which is opaque to the user, but is necessary for successful decryption):

Decrypt:

The context should be same as in the protect function call for successful decryption.

// context is optional
data, err := secureCell.Unprotect(protectedData, additionalData, context)
Secure Cell Context-Imprint Mode

Initialise cell:

secureCell := cell.New(secretKey, cell.CELL_MODE_CONTEXT_IMPRINT)

Encrypt:

// context required
protectedData, _, err := secureCell.Protect(data, context)

Second parameter _ is additional data that is nil in this mode.

Decrypt:

The context should be same as in the protect function call for successful decryption.

// context required
data, err := secureCell.Unprotect(protectedData, nil, context)

Secure Session

Secure Session is a sequence- and session- dependent, stateful messaging system. It is suitable for protecting long-lived peer-to-peer message exchanges where the secure data exchange is bound to a specific session context.

Secure Session operates in two stages: session negotiation where the keys are established and cryptographic material is exchanged to generate ephemeral keys and data exchange the where exchanging of messages can be carried out between peers.

You can read a more detailed description of the process here.

Put simply, Secure Session takes the following form:

  • Both clients and server construct a Secure Session object, providing
    • an arbitrary identifier,
    • a private key, and
    • a callback function that enables it to acquire the public key of the peers with which they may establish communication.
  • A client will generate a "connect request" and by whatever means it will dispatch that to the server.
  • A server will enter a negotiation phase in response to a client's "connect request"
  • Clients and servers will exchange messages until a "connection" is established.
  • Once a connection is established, clients and servers may exchange secure messages according to whatever application level protocol was chosen.
Secure Session Workflow

Secure Session has two parties that are called client and server for the sake of simplicity, but they could be more precisely called initiator and acceptor - the only difference between them is in who starts the communication.

Secure Session relies on the user's passing a number of callback functions to send/receive messages - and the keys are retrieved from local storage (see more in Secure Session cryptosystem description).

Using Secure Session

1. Implement session.SessionCallbacks interface: * GetPublicKeyForId which will return peer's trusted public key when needed by the system * StateChanged is just a notification callback. You may use it for informational purpose, to update your UI or just have a dummy (do-nothing) implementation

import "github.com/cossacklabs/themis/gothemis/session"
import "github.com/cossacklabs/themis/gothemis/keys"

type callbacks struct {

}

func (clb *callbacks) GetPublicKeyForId(ss *session.SecureSession, id []byte) (*keys.PublicKey) {
    pub := getPublicKeyFromDatabaseOrOtherStorageOrSource(id)

    return pub /* or nil */
}

func (clb *callbacks) StateChanged(ss *session.SecureSession, state int) {
    // do something
}

2. Create SecureSession object:

session, err := session.New(yourId, yourPrivateKey, &callbacks{})

3. On the client side, initiate Secure Session negotiation by generating and sending connection request:

connectRequest, err = session.ConnectRequest();
// send connectRequest to the server

4. Start receiving and parsing incoming data on both sides:

// receive some data and store it in receiveBuffer

// receiveBuffer contained encrypted data from your peer, try to decrypt it
data, sendPeer, err := session.Unwrap(receiveBuffer)
// check err

if sendPeer {
    // receiveBuffer was part of the negotiation protocol,
    // so data contains the response to this protocol,
    // which needs to be forwarded to your peer.

    // just send data to your peer
} else {
    // data may be nil on the client when the Secure Session completes the negotiation
    if data != nil {
        // now data contains decrypted data

        // process data according to your application
    }
}

5. When protocol negotiation completes, you may send encrypted data to your peer:

wrappedData,err := session.Wrap(yourData);
// send wrappedData to your peer

Please refer to the Secure Session test and Secure Session Examples to get the complete vision how Secure Session works.

Secure Comparator

Secure Comparator is an interactive protocol for two parties that compares whether they share the same secret or not. It is built around a Zero Knowledge Proof-based protocol (Socialist Millionaire's Protocol), with a number of security enhancements.

Secure Comparator is transport-agnostic and only requires the user(s) to pass messages in a certain sequence. The protocol itself is ingrained into the functions and requires minimal integration efforts from the developer.

Secure Comparator workflow

Secure Comparator has two parties — called client and server — the only difference between them is in who starts the comparison.

Secure Comparator client
import "github.com/cossacklabs/themis/gothemis/compare"

// create secure comparator and append secret
scomparator, err := compare.New()
err = scomparator.Append(sharedSecret) // byte[]

// Initiating secure compare (client, step1)
buf, err := scomparator.Begin()

for {
    // check comparison result
    res, err := scomparator.Result()

    if compare.COMPARE_NOT_READY == res {
        // send `buf` on server and receive `readed_bytes`

        // proceed and send again
        buffer, err := scomparator.Proceed(buf[:readed_bytes])
    }  else {
        if compare.COMPARE_MATCH == res {
            fmt.Println("match")
        } else {
            fmt.Println("not match")
        }
        break
    }
}

After the loop finishes, the comparison is over and its result is checked calling sc.Result().

Secure Comparator server

Server part can be described in any language, let's pretend that both client and server are using Go.

import "github.com/cossacklabs/themis/gothemis/compare"

// create secure comparator and append secret
scomparator, err := compare.New()
err = scomparator.Append(sharedSecret) // byte[]

for {
    // check comparison result
    res, err := scomparator.Result()

    if compare.COMPARE_NOT_READY == res {
        // receive `readed_bytes` from client

        // proceed and send to client
        buffer, err := scomparator.Proceed(buf[:readed_bytes])
    }  else {
        if compare.COMPARE_MATCH == res {
            fmt.Println("match")
        } else {
            fmt.Println("not match")
        }
        break
    }
}

After the loop finishes, the comparison is over and its result is checked calling sc.Result().

Please refer to the Secure Comparator Examples to get the complete vision how Secure Comparator works.