Create Session Tokens that are Resilient to Misuse and Highjacking
This library is inspired by research A Secure Cookie Protocol paper by Liu et.al which advocates for tokens that do not need to be stored in a database and are also resistant to a whole class of attacks on session tokens including attacks like Volume attacks , Denning-Sacco Attack and stealing session tokens.
LiteSession is a token generator for secure tokens that can be used in HTTP authentication headers, cookies, in place of Json Web Tokens, in IoT and anywhere else where secure tokens are needed for communication between clients and servers. It provides Keyed-Hash Message Authentication tokens with associated client data in either encrypted (default settings) or unencrypted form.
The symmetric encryption used is ChaCha8 which is good enough, refer to the paper Too Much Crypto by Jean-Philippe Aumasson which shows that the encryption scheme is accurate while still yielding about 2.5 times the speed of its increased round ChaCha20 option. ChaCha8 is also lightweight and fast even without hardware acceleration allowing it to be used even on devices with low CPU and RAM resources.
The algorithm is as follows:
identifier | issued | expiry | (data)k | nonce | ConfidentialityMode | Blake3HMAC( identifier | issued | expiration | data | session key, k)
where `k = Blake3HMAC(identifier | issued | expiry | ConfidentialityMode, sk)
The security design used for HMAC and Encryption are:
- TAI64N - handles issued time down to the nanosecond without the need to handle leap seconds and timezones.
- ChaCha8 - handles symetric encryption of the data to prevent it from being read by a party other than the server that issued the token.
- Blake3 - a crazy fast non-cryptographic hashing algorithm used in keyed-mode to act as the Keyed-Hash Message Authentication Code
- Nanorand - used as a cryptographically secure random number generator (CSPRNG) with
ChaChamode enabled - Secrecy - used to hold the keys or token in memory to prevent them from being logged by logging tools, cloning and being moved around.
-
Generate a
random identifier -
Generate an
issued timeandexpiry timein nanoseconds accuracy -
Generate the
encryption keyto encrypt the data portion of the token using algorithmk = Blake3HMAC(identifier | issued | expiry | ConfidentialityMode, sk)- Create an empty string
encryption_key - Append
identifiertoencryption_key - Append
issuedtoencryption_key - Append
expirytoencryption_key - Append
ConfidentialityModetoencryption_key - Perform a HMAC function to the
encryption_keyusing Blake3 in keyed mode and theserver_keyas the key - Return the result of the Blake3 operation above in
hexor as astring
- Create an empty string
-
Encrypt the data using
ChaCha8encryption using the Blake3Hash above as the encryption key -
Return the encrypted data and
nonce -
Perform a Blake3Hmac on
identifier | issued | expiry | (data)k | nonce | ConfidentialityMode -
Generate the token:
- Create an empty string called
token - Append
identifiertotoken - Append
issuedtotoken - Append
expirytotoken - Append
encrypted datatotoken - Append
noncetotoken - Append
ConfidentialityModetotoken - Append
Blake3Hmactotoken - Return the token as a string or hex
- The token generated is in the format
identifier⊕issued⊕expiry⊕ciphertext⊕nonce⊕confidentiality⊕hmac
-
Check if the token structure is valid
-
Destructure the token into its component fields
-
Compare the
expiryto the server'scurrent timeand returnSessionExpiredas theTokenOutcome -
Compute the encryption key as follows:
k=HMAC(identifier | issued | expiry | ConfidentialityMode, sk) -
Decrypt the encrypted data using
k. -
Compute
Blake3HMAC(identifier |issued | expiry | ciphertext | nonce | ConfidentialityMode | session key, k), -
Return
TokenOutcome::TokenAutheticif the token matches orTokenOutcome::TokenRejectedif the token does not matchThe
Blake3algorithm is used inkeyedmode where the key is a32byte/256bitin length TheChaCha8algorithm takes a32byte/256bitkey and12byte/96bit nonceInternational Atomic Time(TAI)is used for nanosecond accuracy and not having to deal with leap seconds and timezones Using thesession keypreventsvolumeandDenning-Saccoattacks