| 1 | **Note: For support with using SideTrail project outside of s2n-tls, please contact |
| 2 | the main project repository: https://github.com/danielsn/smack** |
| 3 | |
| 4 | # Constant Time Verification Tests for s2n |
| 5 | |
| 6 | This repository contains tests which ensure that key s2n functions |
| 7 | are not susceptible to timing attacks. |
| 8 | For more details, see https://github.com/awslabs/s2n/issues/463 |
| 9 | |
| 10 | <!-- START doctoc generated TOC please keep comment here to allow auto update --> |
| 11 | <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> |
| 12 | **Table of Contents** |
| 13 | |
| 14 | - [What are timing side channels](#what-are-timing-side-channels) |
| 15 | - [s2n countermeasures against timing side channels](#s2n-countermeasures-against-timing-side-channels) |
| 16 | - [Why use formal methods to prove the correctness of s2n's countermeasures?](#why-use-formal-methods-to-prove-the-correctness-of-s2ns-countermeasures) |
| 17 | - [How does SideTrail prove the correctness of code blinding countermeasures](#how-does-sidetrail-prove-the-correctness-of-code-blinding-countermeasures) |
| 18 | - [High level overview](#high-level-overview) |
| 19 | - [User annotations](#user-annotations) |
| 20 | - [The gory details](#the-gory-details) |
| 21 | - [How to execute the tests](#how-to-execute-the-tests) |
| 22 | - [Get Docker](#get-docker) |
| 23 | - [Building the image](#building-the-image) |
| 24 | - [Starting docker](#starting-docker) |
| 25 | - [Running a proof inside docker](#running-a-proof-inside-docker) |
| 26 | - [Debugging SideTrail failures](#debugging-sidetrail-failures) |
| 27 | - [Questions?](#questions) |
| 28 | |
| 29 | <!-- END doctoc generated TOC please keep comment here to allow auto update --> |
| 30 | |
| 31 | |
| 32 | ## What are timing side channels |
| 33 | |
| 34 | Cryptographic protocols such as TLS are supposed to keep secret information secret. |
| 35 | They do this by ensuring that WHICH bytes go over the wire is hidden using encryption. |
| 36 | However, if the code is not carefully written, WHEN bytes go over the wire may depend |
| 37 | on values that were supposed to remain secret. |
| 38 | |
| 39 | For example, if code checks a password as follows |
| 40 | |
| 41 | ```C |
| 42 | for (i = 0; i < length; ++i) { |
| 43 | if password[i] != input[i] { |
| 44 | send("bad password"); |
| 45 | } |
| 46 | } |
| 47 | ``` |
| 48 | |
| 49 | then the amount of time until the reply message is received will depend on which byte in the password is incorrect. |
| 50 | An attacker can simply guess |
| 51 | |
| 52 | * a******* |
| 53 | * b******* |
| 54 | * c******* |
| 55 | |
| 56 | until the time to receive the error message changes, and then they know the first letter in the password. |
| 57 | Repeating for the remaining characters turns an exponential guessing challenge into a linear one. |
| 58 | |
| 59 | A variant of this attack, called LUCKY13, has been demonstrated against some implementations the Cipher Block Chaining (CBC) mode of SSL/TLS. |
| 60 | In this attack, the TLS server is tricked into treating a (secret) encrypted byte as a padding length field. |
| 61 | A naive TLS implementation will remove the specified amount of padding, then calculate a hash on the remaining bytes in the packet. |
| 62 | If the value in the secret byte was large, a small number of bytes will be hashed; if it was small, a larger number of bytes will be hashed, creating a timing difference, which in theory can reveal the value in the secret byte. |
| 63 | |
| 64 | For a detailed discussion of the LUCKY13 attack and how s2n mitigates against it, see [this blog post](https://aws.amazon.com/blogs/security/s2n-and-lucky-13/). |
| 65 | |
| 66 | ## s2n countermeasures against timing side channels |
| 67 | |
| 68 | s2n takes a belt and suspenders approach to preventing side-channel attacks. |
| 69 | 1. First of all, it uses code-balancing to ensure that the same number of hash compression rounds are processed, no matter the value in the secret byte. |
| 70 | 2. Second, it adds a delay of up to 10 seconds whenever any error is triggered, which increases by orders of magnitude the number of timing samples an attacker would need, even if they found a way around countermeasure 1. |
| 71 | |
| 72 | ## Why use formal methods to prove the correctness of s2n's countermeasures? |
| 73 | Side channels are notoriously difficult to defend against, since code with a side-channel has the same functional behaviour as code that is side-channel free. |
| 74 | Testing can find bugs, but it cannot prove their absence. |
| 75 | In order to prove the absence of a timing side-channel (up to a timing model of system execution), you need a formal proof. |
| 76 | |
| 77 | Formal proofs are also useful because they help prevent regressions. |
| 78 | If a code change causes a timing side-channel to be introduced, the proof will fail, and the developer will be notified before the bug goes to production. |
| 79 | |
| 80 | ## How does SideTrail prove the correctness of code blinding countermeasures |
| 81 | |
| 82 | ### High level overview |
| 83 | A program has a timing side channel if the amount of time needed to execute it depends on the value of a secret input to the program. |
| 84 | SideTrail take a program, annotates every instruction with its runtime cost, and then generates a mathematical formula which symbolically represents the cost of executing the program given an input. |
| 85 | SideTrail then asks an automated theorem prover, called a Satisfiability Modulo Theories (SMT) solver, if there is a pair of secret inputs to the program that would take different amounts of time to process. |
| 86 | The SMT solver, using clever heuristics, exhaustively searches the state of all possible program inputs, and either returns a proof that there is no timing side-channel, or an example of a pair of inputs that lead to a timing channel, along with the estimated leakage. |
| 87 | |
| 88 | ### User annotations |
| 89 | In addition to the standard `assert()/assume()/` annotations supported through SMACK, there are several annotations supported by SideTrail, which allow the user to pass information to the SideTrail proof: |
| 90 | |
| 91 | 1. `__VERIFIER_ASSUME_LEAKAGE(arg)`: When the timing-modeling transformation encounters this call, it increments the leakage tracking variables by "arg" |
| 92 | 2. `S2N_PUBLIC_INPUT(arg)`: the argument given here is taken to be public. |
| 93 | All other variables are assumed private by default. |
| 94 | 3. `S2N_INVARIANT(arg)`: asserts that the given argument is an invariant of the loop, and as such holds on each execution of the loop, and at loop exit. |
| 95 | 4. `__VERIFIER_ASSERT_MAX_LEAKAGE(arg)`: asserts that the given function is time-balanced, with a leakage of less than "arg" time units. |
| 96 | |
| 97 | ### The gory details |
| 98 | |
| 99 | Mathematically, a program `P(secret, public)` has runtime `Time(P(secret,public))`. |
| 100 | A program has a timing side-channel of delta if `|Time(P(secret_1,public)) - Time(P(secret_2,public))| = delta`. |
| 101 | If we can represent `Time(P(secret,public))` as a mathematical formula, we can use a theorem prover to mathematically prove that the timing leakage of the program, for all choices of `secret_1, secret_2`, is less than `delta`. |
| 102 | |
| 103 | SideTrail proceeds in several steps: |
| 104 | |
| 105 | 1. Compile the code to llvm bitcode - this allows SideTrail to accurately represent the effect of compiler optimizations on runtime |
| 106 | 2. Use LLVM's timing model to associate every bitcode operation with a timing cost |
| 107 | 3. Use SMACK to compile the annotated LLVM code to BoogiePL (this represents `(P(secret,public))` in the above formula |
| 108 | 4. Use bam-bam-boogieman to |
| 109 | * Add a timing cost variable to the program, generating `Time(P(secret,public))` |
| 110 | * Make two copies of the resulting program, one which has inputs `(secret_1, public)`, the other of which has inputs `(secret_2, public)` |
| 111 | * Assert that the difference in time between the two programs is less than `delta` |
| 112 | 5. Use boogie to prove (via the z3 theorem prover) that either |
| 113 | * The time is indeed less than `delta` |
| 114 | * or, the time is greater than `delta`, in which case the prover can emit a trace leading to the error |
| 115 | |
| 116 | |
| 117 | ## How to execute the tests |
| 118 | |
| 119 | ### Get Docker |
| 120 | |
| 121 | We assume you have docker installed and running on your machine. |
| 122 | If not, follow the instructions to install it https://docs.docker.com/get-docker/ |
| 123 | |
| 124 | We will assume that you have a variable `$S2N` which encodes the path to s2n on your machine |
| 125 | |
| 126 | ```shell |
| 127 | export S2N=<path_to_s2n> |
| 128 | ``` |
| 129 | |
| 130 | ### Building the image |
| 131 | |
| 132 | To build the image, start with `s2n/codebuild/spec/sidetrail/Dockerfile` |
| 133 | |
| 134 | ```shell |
| 135 | cd $S2N |
| 136 | docker build -f codebuild/spec/sidetrail/Dockerfile --tag sidetrail . |
| 137 | ``` |
| 138 | |
| 139 | This step takes about 25 minutes on my laptop. |
| 140 | |
| 141 | ### Starting docker |
| 142 | |
| 143 | ```shell |
| 144 | cd $S2N |
| 145 | docker run -u `id -u` \ |
| 146 | -v `pwd`:/home/s2n \ |
| 147 | -w /home/s2n/tests/sidetrail/working \ |
| 148 | --entrypoint /bin/bash \ |
| 149 | -it sidetrail |
| 150 | ``` |
| 151 | |
| 152 | You will now be presented with a docker shell. |
| 153 | Inside this shell, run: |
| 154 | |
| 155 | ```shell |
| 156 | source /sidetrail-install-dir/smack.environment |
| 157 | ``` |
| 158 | |
| 159 | This step is important. |
| 160 | If you do not source this file when you begin working, SideTrail may appear to run, but not actually analyze the code. |
| 161 | |
| 162 | ### Running a proof inside docker |
| 163 | |
| 164 | ```shell |
| 165 | cd <testname> |
| 166 | ./clean.sh && ./run.sh |
| 167 | ``` |
| 168 | |
| 169 | You should see output that looks something like this |
| 170 | |
| 171 | ``` |
| 172 | ... |
| 173 | ... |
| 174 | warning: memory intrinsic length exceeds threshold (0); adding quantifiers. |
| 175 | SMACK generated s2n_record_parse_wrapper@s2n_record_read_wrapper.c.compiled.bpl |
| 176 | warning: module contains undefined functions: malloc, __CONTRACT_invariant, nondet |
| 177 | |
| 178 | ____ _ _ |
| 179 | | _ \ _ __ ___ __| |_ _ ___| |_ |
| 180 | | |_) | '__/ _ \ / _` | | | |/ __| __| |
| 181 | | __/| | | (_) | (_| | |_| | (__| |_ |
| 182 | |_| |_| \___/ \__,_|\__,_|\___|\__| |
| 183 | |
| 184 | s2n_record_parse_wrapper@s2n_record_read_wrapper.c |
| 185 | |
| 186 | |
| 187 | |
| 188 | __ __ _ __ |
| 189 | \ \ / /__ _ __(_)/ _|_ _ |
| 190 | \ \ / / _ \ '__| | |_| | | | |
| 191 | \ V / __/ | | | _| |_| | |
| 192 | \_/ \___|_| |_|_| \__, | |
| 193 | |___/ |
| 194 | s2n_record_parse_wrapper@s2n_record_read_wrapper.c |
| 195 | |
| 196 | + boogie /printModel 4 /doModSetAnalysis s2n_record_parse_wrapper@s2n_record_read_wrapper.c.product.bpl |
| 197 | Boogie program verifier version 2.3.0.61016, Copyright (c) 2003-2014, Microsoft. |
| 198 | |
| 199 | Boogie program verifier finished with 1 verified, 0 errors |
| 200 | |
| 201 | real 0m23.334s |
| 202 | user 0m20.983s |
| 203 | sys 0m2.395s |
| 204 | ``` |
| 205 | |
| 206 | If you do not see the line, |
| 207 | |
| 208 | ``` |
| 209 | Boogie program verifier finished with 1 verified, 0 errors |
| 210 | ``` |
| 211 | |
| 212 | then you probably forgot to source `smack.environment`. |
| 213 | Go back and do so. |
| 214 | |
| 215 | ## Debugging SideTrail failures |
| 216 | |
| 217 | Consult [the debugging guide](DEBUGGING.md). |
| 218 | |
| 219 | ## Questions? |
| 220 | |
| 221 | contact [email protected] |