TripleSec

Now in Python, JavaScript, Go, Haskell, and C#

TripleSec is a simple, double-paranoid, symmetric encryption library for a whole bunch of languages. It encrypts data with Salsa 20, AES, (and previously, Twofish), so that a someday compromise of one of the ciphers will not expose the secret.

Of course, encryption is only part of the story. TripleSec also: derives keys with scrypt to defend against password-cracking and rainbow tables; authenticates with HMAC to protect against adaptive chosen-ciphertext attacks; and in the JavaScript version supplements the native entropy sources for fear they are weak in old browsers.

In-Browser Magical Demo

Implementations

Triplesec was created at Keybase, and we officially maintain the Node.js, JavaScript, Go, and Python releases. Other releases, such as C#, are voluntary contributions by the community. We'll link to projects here that pass all our test cases, but nothing unofficial here has been fully audited by us.

Node.js

https://github.com/keybase/triplesec

JavaScript

https://github.com/keybase/triplesec/tree/master/rel

Go

https://github.com/keybase/go-triplesec

Python

https://github.com/keybase/python-triplesec

Also, pip install TripleSec makes a command line utility available. Read more about it in the Readme.

Haskell

Community release: https://github.com/SamProtas/hs-triplesec

C#

Community release: https://github.com/SparkDustJoe/TripleSecManaged

PHP

Community release: https://github.com/lyoshenka/php-triplesec

Encryption Example

Encryption is performed by the encrypt function. In JavaScript, it periodically yields control to not lock up your CPU, and finally it calls back with (err, buffer).

triplesec.encrypt

    data:          triplesec.Buffer.from 'Pssst. I believe I love you.'
    key:           triplesec.Buffer.from 'top-secret-pw'
    progress_hook: ({what, i, total}) -> # ...

, (err, buff) ->

    ciphertext = buff.toString 'hex' unless err
        

Decryption Example

Even easier!

triplesec.decrypt

    data:          triplesec.Buffer.from ciphertext, 'hex'
    key:           triplesec.Buffer.from 'top-secret-pw'
    progress_hook: ({what, i, total}) -> # ...

, (err, buff) ->

    console.log buff.toString() unless err
        

Command Line

If you pip install TripleSec to get the python version of TripleSec, you'll be pleasantly surprised to discover the triplesec command line program. For usage info, run it without any arguments. Or read the python-triplesec page on github.

Version 4 Changes

In TripleSec version 4, we are removing the Twofish phase of the algorithm.

Additionally, the hash algorithm is being upgraded from the Keccak-512 proposal to the standardized SHA3-512 function, which was not finalized when TripleSec was first created.

Of course, libraries will still be able to encrypt and decrypt for previous versions.

Anatomy of Output

The anatomy of the ciphertext in version 4 is below.

The anatomy of the ciphertext in version 3 is below.

Algorithm Design

The TripleSec library encrypts data in four steps:

  1. Key derivation. Given a user-provided password, and a random salt value, generate two (three in versions 3 and prior) separate secret keys, one for each cipher (see Step 3), and two final keys for signing the ciphertext (see Step 4). This "key stretching" is done via standard scrypt, with parameters N=215, r=8, p=1, and output length of 192 bytes.
  2. Initial value (IV) generation. A random number generator is queried to produce an initial vector for each of the two ciphers: a 192-bit IV for Salsa20; and a 128-bit IV for AES. In versions 3 and prior, a 128-bit IV is generated for Twofish in between the generations for Salsa20 and AES.
  3. Cascading encryption. Each of the ciphers runs with the keys generated in Step 1, and the IVs generated in Step 2.
    1. Salsa20. The innermost cipher is a Salsa20 variant called XSalsa20. Like Salsa20, XSalsa20 is a stream cipher, meaning it can encrypt input texts of arbitrary length without a a block cipher mode of operation. XSalsa20 takes a 192-bit nonce rather than Salsa20's 64-bit nonce, but is provably as secure. Given a key, and an IV, XSalsa20 generates a random pad, which is then XOR'ed with the input message. This step of the algorithm outputs the concatenation of the IV and the result of the XOR operation.

    2. Twofish-CTR. This step does not occur in version 4. The output of the previous step (call it C1) is the input of this step, which uses Twofish running in CTR mode. Let R2 be the the IV generated for Twofish in Step 2. Twofish-CTR works by encrypting R2, R2+1, R2+2,... with Twofish, and concatenating the result to yield a pad the size of C1. Call this pad P2 . Output (R2 || (P2C1)), where "||" denotes concatenation, and "⊕" denotes XOR.
    3. AES-256-CTR. In the final encryption step, apply AES-256 running in CTR mode to the output of the previous step. As above, first XOR the output of the previous step with the pad output by AES-256-CTR. Then prepend the IV used.
  4. HMAC (or "sign") the ciphertext. Finally, TripleSec "signs" the ciphertext to ensure that no adversary tampers with it. The data to be signed is everything generated to date: a small header that encapsulates the version of the algorithm (now at 4); the salt used in key derivation; and the output of the AES stage of the cascading encryption above. In versions 3 and prior, TripleSec uses HMAC-SHA-512 and HMAC-KECCAK-512, and in versions 4 and later, TripleSec uses HMAC-SHA-512 and HMAC-SHA3-512. Each HMAC uses a separate key. The final output is a concatenation of: the header; the salt; the signature; and the outermost ciphertext.

Though this is not the exact composition suggested by Schneier in Applied Cryptography (Section 15.8 in the Second Edition), it is close. TripleSec never uses the output of one block cipher as input into the next, which theoretically might allow a crack of one cipher to be used to crack another. Rather, by merit of CTR mode, the three ciphers run on statistically independent IVs, so a crack of one will not spread up or down the chain. The TripleSec technique takes one futher step not suggested by Schneier, which is to protect the inner IVs with the outer encryption algorithms, and only exposing the outermost IV in the clear. Though we can't prove this makes the scheme more secure, it seems like a reasonable idea: why reveal cipher inputs if we don't have to? Finally, this algorithm has the added advantage that the output ciphertext only increases by a constant additive term (i.e., the lengths of the header, the salt, the HMAC and the three IVs). Schneier's technique inflates ciphertexts by a factor N, where N is the number of independent ciphers used.

Similarly, TripleSec protects against a break in HMAC-SHA-512 by always combining it with an HMAC based on Keccak hash algorithm (in version four, we use the SHA-3 standard, which is slightly different). TripleSec concatenates the two results to preserve collision-resistance. Unlike the suspect compositions in TLS and SSH, this simple composition doesn't require either SHA-512 or SHA-3 to be strongly collision-resistant; rather, just weakly collision-resistant in line with the original construction. See Anja Lehmann's dissertation for more details on combinations of hashes.

Anticipated Questions

Multiple encryption is madness!

User data uploaded to a remote cloud-hosted server is nearly impossible to delete, so any encryption scheme has to be future-proof. The amount of time spent encrypting reasonably-sized plaintexts pales in comparison to (1) scrypt, which is intentionally slow; and (2) how long it will sit on the server. Why not go the extra mile?

In the JS/Coffee examples, what's triplesec.Buffer?

It is a wrapper around either Node.js's Buffer or a browser equivalent. When you generate encrypted data, you can use the output buffer however you like. In our above examples, we converted to and from hex strings.

How does TripleSec generate randomness/entropy? Can I provide my own?

TripleSec first derives a random seed from a variety of sources: from window.crypto.getRandomValues in the browser; from crypto.rng in Node.js; from the millisecond field of your system time; and finally, from more-entropy, which counts how many floating-point-heavy computations can be done in a set amount of time. This data is then stirred together and becomes the seed for HMAC_DRBG, whose HMAC is the XOR of HMAC-SHA-512 and HMAC-SHA3.

You may alternatively provide your own random number generator for encryption. Pass an rng function along with your other data. This function should take two arguments: the number of bytes needed, and a callback that you fire with a triplesec.WordArray containing the random data. You can create a WordArray from a triplesec.Buffer by simply calling WordArray.from_buffer(buffer).

How are passphrases salted?

scrypt takes as input a salt in addition to a secret passphrase, to prevent an adversary from cracking many TripleSec-encrypted ciphtertexts in parallel. TripleSec salts passphrases with a random 16-byte sequence that's included with the ciphertext. By default, TripleSec's triplesec.Encryptor object uses the same salt until you call triplesec.Encryptor.resalt. The advantage of salt reuse is that it's faster, since it avoids the intentionally slow scrypt step. On the other hand, an adversary can tell if two different ciphertexts were encrypted in the same session if the salt is not reset.

Can I encrypt files with it, in the browser?

Yes, using HTML5 features you can access file data without uploading it to a server, and convert it to a Buffer.

Why isn't library X good enough (for X in Clipperz, Forge, SJCL, CryptoJS, etc.)?

There are lots of great JS Crypto libraries out there, and we've borrowed from some to build TripleSec. But combining cryptographic primitives to achieve IND-CCA2 security involves many fussy decisions and much avoidance of implementation pitfalls. We want all to have access to higher-level primitives that can be applied with little thought. Hence TripleSec!

Is this provably secure?

We don't have any exact proof of security for a cascade of block ciphers in CTR mode. But we're pretty sure TripleSec's encryption can only be broken if all the algorithms are broken. This paper gives a proof of security for double encryption (though, using the same algorithm). Since the entirety of both HMACs on the same data are independently presented, both would have to be broken in order to malleate a message, giving TripleSec IND-CCA2 security.

If the input message size is n, how big is the ciphertext in bytes?

In versions 3 and prior, n + 208. In version 4, n + 192. The additive term is broken down as:

  • 8 bytes for the header (which is [0x1c94d7de, 0x3]).
  • 16 bytes for scrypt salt
  • 64 bytes for the HMAC-SHA512 signature
  • 64 bytes for the HMAC-SHA3 signature
  • 16 bytes for AES-256 IV
  • 16 bytes for Twofish IV in versions 3 and prior
  • 24 bytes for Salsa20 IV

How do I verify the implementation against known test vectors?

If you have Node.js on your system, you can clone the github repo and run make test. We've checked all algorithms against known test vectors, with the exception of the XSalsa20 extension to Salsa20, which doesn't have published test vectors. For the XSalsa20 extension, we check outputs against the official Go Language Crypto library. We still check the underlying Salsa20 core against published test vectors.

I read someplace that it's impossible to write real crypto in JavaScript.

There are well-read articles on this topic, but we don't agree with a lot of the rhetoric. Of course you should deliver your Crypto libraries over TLS, and nowadays, that's accepted and common. And maybe JavaScript isn't the most convenient language to write Crypto code in, but it still can express all the necessary primitives. Browsers have good CSPRNGs now, and even if you don't trust Apple and/or Linux and/or Chrome, we have some good workarounds (see above). True, one needs to take care not to overflow 32-bits, but with a robust testing suite against known test vectors, one can rule out this class of bugs. Of course one shouldn't allow untrusted libraries to trample one's trusted primitives, but that's true of any language (see LD_PRELOAD attacks against libraries written in C). A shortcoming we encountered in writing TripleSec is that JavaScript doesn't offer destructors, so it's inconvenient to scrub buffers properly. TripleSec has taken care to do this job manually. If you spot some unscrubbed buffers, please let us know.

We are as worried as anyone else about XSS attacks, CSRF attacks and the ability of third party code to tamper with vetted Crypto code. But these attacks and the quality of Crypto libraries are othogonal concerns. Those sites with high quality JS libraries should feel confident encrypting data with TripleSec. Those with lots of unvetted third party JS code won't gain much.

Is there a streaming interface?

Not yet, it's in progress. The current interface requires the file to be fully loaded into memory before it's encrypted, but the current file format is compatible with streaming (with a single seek to write the HMACs).

Implementations Outside JavaScript

We welcome ports, and we'll list such projects here. The TripleSec checkout has test vectors which your implementation should match.

Who's Using it?

For starters, we are (Max Krohn & Chris Coyne).

If you use TripleSec for something public, please contact us. We'll mention you here.

Can I help?

Please! Above all else, we encourage review of both our algorithm and the source code.

How do I reach you?

Our email addresses are right here. Please enter the password peppermint patty in the demo box, and this as the ciphertext:

1c94d7de0000000469442fe0dfad4214eb04c3bd2b1d702c22b816da57bbff51bd7a573f547b0c2a6e196f08d4bb0c6cabc0024fa78aa05ae7b8529b1f6ae589d5479e2b641f7aa4d48258e67462537a0de9a742461de1ceb6a4ac28e58be6ad37c47361a0a3ca06b5179e8efd89e4f2d932e39526c7705410c0e5c2ba1fd93d181f40fd558dba95eac5d72faf25eb165c0f6c1d68c5f16c786b5acc6083e7d44914753e05a4506dd01943a9e72f5553393ca8ebee8287dcb9efad48ca4962fae4024e2a3ad44f396a9f64cb4b01cca8981e2b139a4b31273943cddf180c0c79f3b1c4ee78ccd99dfc29be1e4fab1fd48235f7913b992bd3c4f770aca1773b47a09681c892a47d89f1735505eca309a354144321c3a54c13ce21cb0cd58edac473442ff86d1565ea488264d5ef6c031d3fc4f27aee