|
| 1 | +# SipHash |
| 2 | + |
| 3 | +[](http://creativecommons.org/publicdomain/zero/1.0/) |
| 5 | + |
| 6 | +[](https://opensource.org/licenses/MIT) |
| 7 | + |
| 8 | + |
| 9 | +SipHash is a family of pseudorandom functions (PRFs) optimized for speed on short messages. |
| 10 | +This is the reference C code of SipHash: portable, simple, optimized for clarity and debugging. |
| 11 | + |
| 12 | +SipHash was designed in 2012 by [Jean-Philippe Aumasson](https://aumasson.jp) |
| 13 | +and [Daniel J. Bernstein](https://cr.yp.to) as a defense against [hash-flooding |
| 14 | +DoS attacks](https://aumasson.jp/siphash/siphashdos_29c3_slides.pdf). |
| 15 | + |
| 16 | +SipHash is: |
| 17 | + |
| 18 | +* *Simpler and faster* on short messages than previous cryptographic |
| 19 | +algorithms, such as MACs based on universal hashing. |
| 20 | + |
| 21 | +* *Competitive in performance* with insecure non-cryptographic algorithms, such as [fhhash](https://github.com/cbreeden/fxhash). |
| 22 | + |
| 23 | +* *Cryptographically secure*, with no sign of weakness despite multiple [cryptanalysis](https://eprint.iacr.org/2019/865) [projects](https://eprint.iacr.org/2019/865) by leading cryptographers. |
| 24 | + |
| 25 | +* *Battle-tested*, with successful integration in OSs (Linux kernel, OpenBSD, |
| 26 | +FreeBSD, FreeRTOS), languages (Perl, Python, Ruby, etc.), libraries (OpenSSL libcrypto, |
| 27 | +Sodium, etc.) and applications (Wireguard, Redis, etc.). |
| 28 | + |
| 29 | +As a secure pseudorandom function (a.k.a. keyed hash function), SipHash can also be used as a secure message authentication code (MAC). |
| 30 | +But SipHash is *not a hash* in the sense of general-purpose key-less hash function such as BLAKE3 or SHA-3. |
| 31 | +SipHash should therefore always be used with a secret key in order to be secure. |
| 32 | + |
| 33 | + |
| 34 | +## Variants |
| 35 | + |
| 36 | +The default SipHash is *SipHash-2-4*: it takes a 128-bit key, does 2 compression |
| 37 | +rounds, 4 finalization rounds, and returns a 64-bit tag. |
| 38 | + |
| 39 | +Variants can use a different number of rounds. For example, we proposed *SipHash-4-8* as a conservative version. |
| 40 | + |
| 41 | +The following versions are not described in the paper but were designed and analyzed to fulfill applications' needs: |
| 42 | + |
| 43 | +* *SipHash-128* returns a 128-bit tag instead of 64-bit. Versions with specified number of rounds are SipHash-2-4-128, SipHash4-8-128, and so on. |
| 44 | + |
| 45 | +* *HalfSipHash* works with 32-bit words instead of 64-bit, takes a 64-bit key, |
| 46 | +and returns 32-bit or 64-bit tags. For example, HalfSipHash-2-4-32 has 2 |
| 47 | +compression rounds, 4 finalization rounds, and returns a 32-bit tag. |
| 48 | + |
| 49 | + |
| 50 | +## Security |
| 51 | + |
| 52 | +(Half)SipHash-*c*-*d* with *c* ≥ 2 and *d* ≥ 4 is expected to provide the maximum PRF |
| 53 | +security for any function with the same key and output size. |
| 54 | + |
| 55 | +The standard PRF security goal allow the attacker access to the output of SipHash on messages chosen adaptively by the attacker. |
| 56 | + |
| 57 | +Security is limited by the key size (128 bits for SipHash), such that |
| 58 | +attackers searching 2<sup>*s*</sup> keys have chance 2<sup>*s*−128</sup> of finding |
| 59 | +the SipHash key. |
| 60 | +Security is also limited by the output size. In particular, when |
| 61 | +SipHash is used as a MAC, an attacker who blindly tries 2<sup>*s*</sup> tags will |
| 62 | +succeed with probability 2<sup>*s*-*t*</sup>, if *t* is that tag's bit size. |
| 63 | + |
| 64 | + |
| 65 | +## Research |
| 66 | + |
| 67 | +* [Research paper](https://www.aumasson.jp/siphash/siphash.pdf) "SipHash: a fast short-input PRF" (accepted at INDOCRYPT 2012) |
| 68 | +* [Slides](https://cr.yp.to/talks/2012.12.12/slides.pdf) of the presentation of SipHash at INDOCRYPT 2012 (Bernstein) |
| 69 | +* [Slides](https://www.aumasson.jp/siphash/siphash_slides.pdf) of the presentation of SipHash at the DIAC workshop (Aumasson) |
| 70 | + |
| 71 | + |
| 72 | +## Usage |
| 73 | + |
| 74 | +Running |
| 75 | + |
| 76 | +```sh |
| 77 | + make |
| 78 | +``` |
| 79 | + |
| 80 | +will build tests for |
| 81 | + |
| 82 | +* SipHash-2-4-64 |
| 83 | +* SipHash-2-4-128 |
| 84 | +* HalfSipHash-2-4-32 |
| 85 | +* HalfSipHash-2-4-64 |
| 86 | + |
| 87 | + |
| 88 | +```C |
| 89 | + ./test |
| 90 | +``` |
| 91 | + |
| 92 | +verifies 64 test vectors, and |
| 93 | + |
| 94 | +```C |
| 95 | + ./debug |
| 96 | +``` |
| 97 | + |
| 98 | +does the same and prints intermediate values. |
| 99 | + |
| 100 | +The code can be adapted to implement SipHash-*c*-*d*, the version of SipHash |
| 101 | +with *c* compression rounds and *d* finalization rounds, by defining `cROUNDS` |
| 102 | +or `dROUNDS` when compiling. This can be done with `-D` command line arguments |
| 103 | +to many compilers such as below. |
| 104 | + |
| 105 | +```sh |
| 106 | +gcc -Wall --std=c99 -DcROUNDS=2 -DdROUNDS=4 siphash.c halfsiphash.c test.c -o test |
| 107 | +``` |
| 108 | + |
| 109 | +The `makefile` also takes *c* and *d* rounds values as parameters. |
| 110 | + |
| 111 | +```sh |
| 112 | +make cROUNDS=2 dROUNDS=4 |
| 113 | +``` |
| 114 | + |
| 115 | +Obviously, if the number of rounds is modified then the test vectors |
| 116 | +won't verify. |
| 117 | + |
| 118 | +## Intellectual property |
| 119 | + |
| 120 | +This code is copyright (c) 2014-2023 Jean-Philippe Aumasson, Daniel J. |
| 121 | +Bernstein. It is multi-licensed under |
| 122 | + |
| 123 | +* [CC0](./LICENCE_CC0) |
| 124 | +* [MIT](./LICENSE_MIT). |
| 125 | +* [Apache 2.0 with LLVM exceptions](./LICENSE_A2LLVM). |
| 126 | + |
0 commit comments