17 KiB
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 aresponse
.
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
- Generate a public token for each request from a fixed private key
- The token never to be the same
- The token to be easily checked directly from the previous one
- Each token to be only valid a few seconds after sending it
- Each token to give no clue that could help guessing the next token.
- A system where no key is shared ; only clients do the key processing and the server only to process simple checking algorithm
Technology requirements
- Mixing 2 hashes in a way that without one of them, the other is cryptographically impossible to guess (i.e. one-time pad).
- Having a time-dependent unique hash, that could be found only a few seconds after sending it (as for TOTP).
- 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.
- a Stateless Time Scrambling Protocol to take care of the request's expiration over time.
- 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 -12 \parallel = 12 |
\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 :
- Decrement
n
- 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.
Time Scrambling algorithm
In order for the requests/responses to be only valid a few seconds in time, the tokens are scrambled using a one-time pad.
The sender processes the data as follows:
Step | Description | Formula |
---|---|---|
1 | Process the sender's time id | t\_s = \mid \frac{t_{now}^s}{W} \mid |
2 | process the sender's time parity | m_s = t_s \mod 2 |
3 | Send the time parity | Send\ m_s |
The receiver has to guess t_s
with the following steps:
Step | Description | Formula |
---|---|---|
1 | receive the sender's time parity | Receive \ m_s |
2 | process the receiver's time id | t\_r = \mid \frac{t_{now}^r}{W} \mid |
3 | process the receiver's time parity | m_r = t_r \mod 2 |
4 | Try to guess the sender's time id | t_{s2} = t_r - \parallel m_s - m_r \parallel |
As a result, the receiver will only be able to retrieve the sender's time id if the reception time is at most W
seconds after the sending time :
t\_{now}^r \leq t\_{now}^s + W \implies t\_s = t_{s2}
In practice, the time id is hashed and used to achieve a one-time pad with the token. Because they both result from the same hash function their sizes are the same.
As a result from the sent token x_1 = token \oplus h(t_s)
, it is impossible to guess the token nor h(t_s)
without the other.
Explanation
-
The time id corresponds to the index of the time slice where slices are
W
seconds wide. By dividing the time in slices ofW
seconds, if we process the same calculation at an interval ofW
or less seconds, we will have either the same result or a result greater by 1. -
The window id parity
m_s
allows us to adjust the receiver's time idt_r
when it receives the request. This difference of 1 second is caused by the division of time in slices, the precision is also divided byW
.t_{now}\mod W = 0 \implies \mid \frac{t_{now}}{W} \mid = \mid \frac{t_{now}+(W-1)}{W} \mid
; no need for adjustmentt_{now}\mod W = 1 \implies \mid \frac{t_{now}}{W} \mid = \mid \frac{t_{now}+(W-1)}{W} \mid + 1
; need to subtract1
t_{now}\mod W = 2 \implies \mid \frac{t_{now}}{W} \mid = \mid \frac{t_{now}+(W-1)}{W} \mid + 1
; need to subtract1
...
t_{now}\mod W = (W-1) \implies \mid \frac{t_{now}}{W} \mid = \mid \frac{t_{now}+(W-1)}{W} \mid + 1
; need to subtract1
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 hashh(t_C)
and be able to recover the private keyK
by processing a simple XOR on the public token. Because the cyclic-hash algorithm generates a unique pseudo-random token fromK
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 t_C |
t_C =\ \mid \frac{t_{now}}{W} \mid |
c4 |
Calculate m_C , the parity of t_C |
m_C = t_C \mod 2 |
c5 |
Calculate the time hash h_{t_C} |
h_{t_C} = h(t_C) |
c6 |
Calculate x_1 , the scrambled request token |
x_1 = T_C \oplus h_{t_C} |
c7 |
Calculate x_2 , the next token + time parity |
x_2 = x_1 \oplus m_C |
Security : We process a one-time pad between T_C
and h_{t_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_{t_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 tokenm_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 thatC
sent the request0
to(W + \frac{W}{2})
seconds ago.
Steps explanation
s1
- IfC
andS
have the same values forK
andn^1
,f(K,n^1)
must result in the same output; in other wordsT_C=T_S
.s3
/s4
-\parallel m_C - m_s\parallel
is the difference betweenm_C
andm_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 \parallel m_C - m_S\parallel = 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 \parallel m_C - m_S \parallel = 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, letk \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 extractT_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 extractT_S
- If the receiver time window if further the sender by
s6
- By the non-idempotency (i.e.a\oplus b \oplus a = b
) and associativity properties of the XOR operator, consideringh_{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 ofC
have successfully been extracteds7
- IfK
andn^1
are the same on both machines,T_S=T_C
. Furthermore, if the time ids are the same (c.f. steps6
) 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)