schastsp/PROTOCOL.md

16 KiB
Raw Blame History

Stateless Time-Scrambled Cyclic Hash Protocol

SaTS CHiP

Vocabulary

  • A token is a fixed-length string sent by a client to a server to authenticate.
  • A private key is a fairly unique and hardly guessable data that is generated by a machine and never shared.
  • A request is sent from a client to a server, the server sends back a response.

Abstract

After designing some APIs, I found out that you must have at some point a token system to authenticate to your server. For the simplest design, the server generates a unique token for each client, this token will then be sent over each request to authenticate.

The server may also regenerate the token at some time to avoid token theft.

An easy MITM attack could be to repeat a previously sent request while the token is still valid or even catch the client token and build a malicious request with its authentication.

In this document we will assume that what travels through the network is public, so any MITM can store it. Obviously I highly recommend using TLS for communicating, but we look here for a consistent token system, it only can be better through TLS.

A good alternative could be to use a one-time token where each request features a unique token. This design implies that the token travels through the network - which we consider public - before being used so any MITM can catch it. To overcome this flaw we could have a system where one key can generate all the tokens in a consistent manner. If so, the tokens do not have to be sent and can be guessed from the key. But this also implies that the key is shared at some point.

A better solution would be to generate a private key on each client and use it to generate a token for each request. Also, to avoid to share the key and allow the server to easily check tokens, we would like a system where each token can be checked from the previous token without having to know the private key (because it is private...). It would avoid attackers to repeat requests (because each token is unique) or guess the private key because it is never over the network but only on the client. In addition short-lived one-time passwords have a mechanism that we could use to build a time-dependent system.

What we need

  1. Generate a public token for each request from a fixed private key
  2. The token never to be the same
  3. The token to be easily checked directly from the previous one
  4. Each token to be only valid a few seconds after sending it
  5. Each token to give no clue that could help guessing the next token.
  6. A system where no key is shared ; only clients do the key processing and the server only to process simple checking algorithm

Technology requirements

  1. Mixing 2 hashes in a way that without one of them, the other is cryptographically impossible to guess (i.e. one-time pad).
  2. Having a time-dependent unique hash, that could be found only a few seconds after sending it (as for TOTP).
  3. A cryptographic hash function that, from an input of any length, outputs a fixed-length digest in a way that is impossible to guess the input back from it.

Protocols to define

This document will define and bundle 2 distinct protocols to make up a token system that implements the previous statements.

  1. a Stateless Time Scrambling Protocol to take care of the request's expiration over time.
  2. a Stateless Cyclic Hash Algorithm to generate several public tokens from a private key in a way that no clue is given over published tokens (i.e. one-way function).

General knowledge & Notations

Notation
Symbols Description
\parallel a\parallel The absolute value of a ; e.g. \parallel a \parallel = \parallel -a \parallel
\mid a \mid The integer value of a ; e.g. \mid 12.34 \mid = 12
a \oplus b The bitwise operation XOR between binary words a and b
h(m) The digest of the message m by a (consistent) secure cryptographic hash function h() ; e.g. sha512
h^n(m) The digest of the n-recursive hash function h() with the input data m ;
h^2(m) \equiv h(h(m)), h^1(m) \equiv h(m), and h^0(m) \equiv m.
a \mod b The result of a modulo b ; i.e. the remainder of the Euclidean division of a by b
t_{now} The current Unix Timestamp in seconds
Entities
  • A client C (typically a client)
  • A server S (typically a server)

Variables

The whole system share a common context, each client holds a private keyset, and the server stores the last valid token of each client.

Common context

These variables are both on the server and clients. They are specific to the server so each client must match its values. These variables shape the system's context (W, min, belt, max).

Notation Name Description
W time window A number of seconds that is typically the maximum transmission time from end to end. It will be used by the time-scrambling aspect. The lower the number, the less time an attacker has to try to brute-force the tokens.
min resynchonization range A number that is used to resynchronize the client if there is a communication issue (e.g. lost request, lost response, attack). The higher the value, the higher the challenge for the client to recover the authentication, thus the harder for an attacker to guess it.
sec security range A number that is used to resynchronize the client if there is a communication shift (e.g. lost request, lost response, attack). It corresponds of the number of desynchronizations the client can handle before never being able to gain the authentication again.
max maximum nonce A number that is used to cap the value of client's nonces. A too high value will result on keys that will never be replaced, thus making them open to long-processing attacks (e.g. brute-force).
Client keyset

Every client holds a keyset (K, n, s). It represents its private key and is used to generate the tokens. The secure hash function is extended as a one-way function to generate all the tokens from the keyset.

Notation Name Description
K private key A secret binary data that must be large and random enough not to be brute forced.
n key nonce A number that is decremented before each request. Before n reaches 0, a new keyset must be generated.
s key state A number that reflects the state of the keyset. It is used to know what to do on the next request :
- 0 : normal request
- 1 : will use the new key
- 2 : resynchronization processed, waiting for the server's acknoledgement

The token generation algorithm is :

  1. Decrement n
  2. Process token = h^n(K)
Server variables
Notation Name Description
T last valid token The server stores the last valid token from the client to check the next one.

If a client sends its token h^n(K) ; if valid the server stores it inside T. Note that for the system's initialisation, the server has to be given T = h^{n-1}(K).

Check a token

When the client sends its next token h^{n-1}(K), the server has to hash it and compare it with the last token T. In fact, tokens are generated according to the following property :

h(h^{n-1}) = h^n(K)

In other words, each token is the hash of the next one.

Because of the main property of cryptographic hash functions, the original data is cryprographically hard to find from its digest (i.e. the hashed data). Since the next token is always the digest of the previous one (and not the opposite), an attacker has no clue about the next token.

Limitations : It seams obvious that there is weaknesses due to hashing recursively a single data, but I do not know if such attack is known or even works.

Protocol properties

(1) Token Chain

As a result, each token is the digest* of its predecessor.

* : the output of the hash function.

This property allows the server to easily check if a token is valid by comparing it with the previous one T. It just has to check if the digest of the token is equal to the previous one.

As a result, each token is bound to the previous one. If a request fails (e.g. network issue) and a token is lost, the next one won't be valid.

(2) Time Limitation

Any request received more than W seconds after being sent is invalidated by the server. It protects against manual, slow and process-intensive request forgery. Also a same token is never observed over the network.

(3) Token unicity

The clients never sends a token more than once. It avoids attackers to reuse tokens. Note that usually the client must not generate a same token twice. The keyset (K, n, s) must never be used twice to generate tokens.

(4) One-way Hash Chain

For a given key, every token sent has a lower nonce n than every other that has already been sent. In that way it gives no clue about the next tokens to be used.

Constraints
  • The server must be able to recover the token if the data is received within the time window.
  • If the window expired, the token must be invalidated by the server.
  • The server must be able to validate a token if it holds the previous one.
  • The server must invalidate a token if it has not the previous one.
  • The client must be able to recover the authentication with a challenge.

Limitations

  • If an arbitrary catches, then blocks a request and sends it afterwards, it will be authenticated. This case is equivalent to being the client (with all secret variables), which can never occur if you use TLS. Notice that you won't be able to extract anything from the token anyway.
  • With requests meta data (e.g. HTTP headers containing the date), an attacker knowing W can forge the time hash h(t_C) and be able to recover the private key K by processing a simple XOR on the public token. Because the cyclic-hash algorithm generates a unique pseudo-random token from K for each request, this case does not give the attacker any clue about the next token to be sent.

Protocol definition

Each request will hold a pair of tokens (x_1, x_2). If the server's check fails (i.e. the client is not authenticated), it will send back to the client a pair of tokens (y_1, y_2) for resynchronization.

Each token has a size defined by the hash function, which is sha512 for this specification ; the size is then 512 bits.

For readability purposes, the tokens are sent as their hexadecimal representation ; each token is then a string of 128 characters.

1. Request

This case is the default where i is far from 1 so there is no key generation to do.

Step Description Formula
c1 Decrement the shifting nonce i = i - 1
c2 Calculate the one-time token T_C T_C = h^{i}(K)
c3 Get the current window id n_C n_C =\ \mid \frac{t_{now}}{W} \mid
c4 Calculate m_C, the parity of n_C m_C = n_C \mod 2
c5 Calculate the time hash h_{n_C} h_{n_C} = h(n_C)
c6 Calculate T_{req}, the scrambled request token T_{req} = T_C \oplus h_{n_C}

Steps explanation

  • c2 - The window id corresponds to the index of the time slice where slices are W seconds wide. By dividing the time in slices of W seconds, if we process the same calculation at an interval of W or less seconds, we will have either the same result or a result greater by 1.
  • c3 - The window id parity m_C allows us to adjust the value of n_S made on S when it receives the request. This difference of 1 second is caused by the division of time in slices, the precision is also divided by W.
    • t_{now}\mod W = 0 \implies \mid \frac{t_{now}}{W} \mid = \mid \frac{t_{now}+(W-1)}{W} \mid; no need for adjustment
    • t_{now}\mod W = 1 \implies \mid \frac{t_{now}}{W} \mid = \mid \frac{t_{now}+(W-1)}{W} \mid + 1; need to subtract 1
    • t_{now}\mod W = 2 \implies \mid \frac{t_{now}}{W} \mid = \mid \frac{t_{now}+(W-1)}{W} \mid + 1; need to subtract 1
    • ...
    • t_{now}\mod W = (W-1) \implies \mid \frac{t_{now}}{W} \mid = \mid \frac{t_{now}+(W-1)}{W} \mid + 1; need to subtract 1
  • c4 - h(n_C) allows h_{n_C} to be L bits long and protects n_C to be predictable from h_{n_C}.
  • c5 - we process a one-time pad between T_C and h_{n_C} it is crucial that both values have the same size of L bits. It makes T_C impossible to extract without having the value h_{n_C}, this property applies in both ways.

Short formulas

Field to send Short formula
T_{req} h^{i}(K) \oplus h(\mid\frac{t_{now}}{W}\mid)
m_C \mid\frac{t_{now}}{W}\mid \mod 2

Note

  • In order to send all the data in one request, for instance you can simply concatenate the 2 variables.

2. Response

Received data

  • T_{req} the received request token
  • m_C the received time id parity
Step Description Formula
s1 Store the reception time window id n' n' = \mid \frac{t_{now}}{W}\mid
s2 Calculate m_S, the parity of n' m_S = n' \mod 2
s3 Use m_C to try to correct the reception window id and guess the request time id n_S = n' - \parallel m_C - m_S \parallel
s4 Calculate the time hash h_{n_S} h_{n_S} = h(n_S)
s5 Cancel h_{n_S} to extract T_{C'} T_{C'} = T_{req} \oplus h_{n_S}
s6 Check if T_{C'} matches T_S T_{C'} = T_S ?

If T_{C'} = T_S, S can consider that C sent the request 0 to (W + \frac{W}{2}) seconds ago.

Steps explanation

  • s1 - If C and S have the same values for K and n^1, f(K,n^1) must result in the same output; in other words T_C=T_S.
  • s3/s4 - \| m_C - m_s\| is the difference between m_C and m_S.
  • If the receiver time window id (n') is the same as the sender (n_C) n'=n_C \implies m_S=m_C m_C=m_S \implies \| m_C - m_S\| = 0 n_S = n_C - 0 n_S=n_C, the time ids are the same, S can now unscramble the request to check the token
    • If the receiver time window if further the sender by 1 n'=n_C+1 \implies \| m_C - m_S \| = 1 n_S = n_C + 1 - 1 n_S = n_C, the time ids are the same, S can now unscramble the request to check the token
    • If the receiver time window if further the sender by 2 or more, let k \in \N n'= n_C+2+k \implies \parallel m_C - m_S\parallel \in \{0,1\} - \parallel m_C - m_S\parallel \in \{-1,0\} n_S \in \{n_C + 2 + k - 1, n_C + 2 + k + 0\} n_S \in \{n_C + k + 1, n_C + k + 2\} \rarr n_S = n_C + k + 1 \implies \forall (k\in \N), n_S \gt n_C, the time ids differ, S cannot extract T_S \rarr n_S = n_C + k + 2 \implies \forall (k \in \N), n_S \gt n_C+1, the time ids differ, S cannot extract T_S
  • s6 - By the non-idempotency (i.e. a\oplus b \oplus a = b) and associativity properties of the XOR operator, considering h_{n_S}=h_{n_C}: T_{C'} = T_{req} \oplus h_{n_S} = (T_C \oplus h_{n_C}) \oplus h_{n_S} h_{n_S} = h_{n_C} \implies T_{C'} = T_C \oplus h_{n_C} \oplus h_{n_C} T_{C'} = T_C, the one-time token of C have successfully been extracted
  • s7 - If K and n^1 are the same on both machines, T_S=T_C. Furthermore, if the time ids are the same (c.f. step s6) the 2 tokens should match.

Short formulas

The whole unscrambling process can be shortened into the following formula resulting in 0 if the client is authenticated.

T_{req} \oplus h(\mid \frac{t_{now}}{W}\mid - \parallel m_C - (\mid \frac{t_{now}}{W}\mid \mod 2) \parallel) \oplus f(K, n^1)