Poly1305
Poly1305 is a universal hash family designed by Daniel J. Bernstein in 2002 for use in cryptography.[1][2]
As with any universal hash family, Poly1305 can be used as a one-time message authentication code to authenticate a single message using a secret key shared between sender and recipient,[3] similar to the way that a one-time pad can be used to conceal the content of a single message using a secret key shared between sender and recipient.
Originally Poly1305 was proposed as part of Poly1305-AES,[2] a Carter–Wegman authenticator[4][5][1] that combines the Poly1305 hash with AES-128 to authenticate many messages using a single short key and distinct message numbers. Poly1305 was later applied with a single-use key generated for each message using XSalsa20 in the NaCl crypto_secretbox_xsalsa20poly1305 authenticated cipher,[6] and then using ChaCha in the ChaCha20-Poly1305 authenticated cipher[7][8][1] deployed in TLS on the internet.[9]
Description
Definition of Poly1305
Poly1305 takes a 16-byte secret key <math>r</math> and an <math>L</math>-byte message <math>m</math> and returns a 16-byte hash <math>\operatorname{Poly1305}_r(m)</math>. [2][1]
Steps:
- Interpret <math>r</math> as a 16-byte little-endian integer.
- Break the message <math>m</math> of bytes <math>m[0], m[1], m[2], \dotsc, m[L - 1]</math> into consecutive 16-byte chunks, possibly with a partial final chunk of fewer than 16 bytes.
- Append a
0x01byte to each chunk and interpret it as an integer; take the integers as the coefficients of a polynomial. - Evaluate the polynomial at the point <math>r</math> modulo the prime <math>2^{130} - 5</math>.
- Reduce the result modulo <math>2^{128}</math> encoded in little-endian return a 16-byte hash.
The coefficients <math>c_i</math> of the polynomial <math>c_1 r^q + c_2 r^{q - 1} + \cdots + c_q r</math>, where <math>q = \lceil L/16\rceil</math>, are:
<math display="block">
c_i = m[16i - 16] + 2^8 m[16i - 15] + 2^{16} m[16i - 14] + \cdots + 2^{120} m[16i - 1] + 2^{128},
</math>
with the exception that, if <math>L \not\equiv 0 \pmod{16}</math>, then:
<math display="block">
c_q = m[16q - 16] + 2^8 m[16q - 15] + \cdots + 2^{8(L \bmod 16) - 8} m[L - 1] + 2^{8 (L \bmod 16)}.
</math>
The secret key <math>r = (r[0], r[1], r[2], \dotsc, r[15])</math> is restricted to have the bytes <math>r[3], r[7], r[11], r[15] \in \{0,1,2,\dotsc,15\}</math>, i.e., to have their top four bits clear; and to have the bytes <math>r[4], r[8], r[12] \in \{0,4,8,\dotsc,252\}</math>, i.e., to have their bottom two bits clear. Thus there are <math>2^{106}</math> distinct possible values of <math>r</math>.
Use as a one-time authenticator
If <math>s</math> is a secret 16-byte string interpreted as a little-endian integer, then
<math display="block">
a := \bigl(\operatorname{Poly1305}_r(m) + s\bigr) \bmod 2^{128}
</math>
is called the authenticator for the message <math>m</math>. If a sender and recipient share the 32-byte secret key <math>(r, s)</math> in advance, chosen uniformly at random, then the sender can transmit an authenticated message <math>(a, m)</math>. When the recipient receives an alleged authenticated message <math>(a', m')</math> (which may have been modified in transmit by an adversary), they can verify its authenticity by testing whether
<math display="block">
a' \mathrel{\stackrel?=} \bigl(\operatorname{Poly1305}_r(m') + s\bigr) \bmod 2^{128}.
</math> Without knowledge of <math>(r, s)</math>, the adversary has probability <math>8\lceil L/16\rceil/2^{106}</math> of finding any <math>(a', m') \ne (a, m)</math> that will pass verification.
However, the same key <math>(r, s)</math> must not be reused for two messages. If the adversary learns
<math display="block"> \begin{align}
a_1 &= \bigl(\operatorname{Poly1305}_r(m_1) + s\bigr) \bmod 2^{128}, \\
a_2 &= \bigl(\operatorname{Poly1305}_r(m_2) + s\bigr) \bmod 2^{128},
\end{align} </math>
for <math>m_1 \ne m_2</math>, they can subtract
<math display="block">
a_1 - a_2 \equiv \operatorname{Poly1305}_r(m_1) - \operatorname{Poly1305}_r(m_2) \pmod{2^{128}}
</math>
and find a root of the resulting polynomial to recover a small list of candidates for the secret evaluation point <math>r</math>, and from that the secret pad <math>s</math>. The adversary can then use this to forge additional messages with high probability.
Use in Poly1305-AES as a Carter–Wegman authenticator
The original Poly1305-AES proposal[2] uses the Carter–Wegman structure[4][5] to authenticate many messages by taking <math>a_i := H_r(m_i) + p_i</math> to be the authenticator on the Template:Mathth message <math>m_i</math>, where <math>H_r</math> is a universal hash family and <math>p_i</math> is an independent uniform random hash value that serves as a one-time pad to conceal it. Poly1305-AES uses AES-128 to generate <math>p_i := \operatorname{AES}_k(i)</math>, where <math>i</math> is encoded as a 16-byte little-endian integer.
Specifically, a Poly1305-AES key is a 32-byte pair <math>(r, k)</math> of a 16-byte evaluation point <math>r</math>, as above, and a 16-byte AES key <math>k</math>. The Poly1305-AES authenticator on a message <math>m_i</math> is
<math display="block">
a_i := \bigl(\operatorname{Poly1305}_r(m_i) + \operatorname{AES}_k(i)\bigr) \bmod 2^{128},
</math>
where 16-byte strings and integers are identified by little-endian encoding. Note that <math>r</math> is reused between messages.
Without knowledge of <math>(r, k)</math>, the adversary has low probability of forging any authenticated messages that the recipient will accept as genuine. Suppose the adversary sees <math>C</math> authenticated messages and attempts <math>D</math> forgeries, and can distinguish <math>\operatorname{AES}_k</math> from a uniform random permutation with advantage at most <math>\delta</math>. (Unless AES is broken, <math>\delta</math> is very small.) The adversary's chance of success at a single forgery is at most:
<math display="block">
\delta + \frac{(1 - C/2^{128})^{-(C + 1)/2} \cdot 8 D \lceil L/16\rceil}{2^{106}}.
</math>
The message number <math>i</math> must never be repeated with the same key <math>(r, k)</math>. If it is, the adversary can recover a small list of candidates for <math>r</math> and <math>\operatorname{AES}_k(i)</math>, as with the one-time authenticator, and use that to forge messages.
Use in NaCl and ChaCha20-Poly1305
The NaCl crypto_secretbox_xsalsa20poly1305 authenticated cipher uses a message number <math>i</math> with the XSalsa20 stream cipher to generate a per-message key stream, the first 32 bytes of which are taken as a one-time Poly1305 key <math>(r_i, s_i)</math> and the rest of which is used for encrypting the message.
It then uses Poly1305 as a one-time authenticator for the ciphertext of the message.[6]
ChaCha20-Poly1305 uses the first 32 bytes of the ChaCha stream cipher output to generate a one-time Poly1305 key, discards the next 32 bytes, and uses the rest to encrypt the message.[8] XChaCha20-Poly1305 using XChaCha instead of ChaCha or XSalsa20 has also been described.[10]
Security
The security of Poly1305 and its derivatives against forgery follows from its bounded difference probability as a universal hash family: If <math>m_1</math> and <math>m_2</math> are messages of up to <math>L</math> bytes each, and <math>d</math> is any 16-byte string interpreted as a little-endian integer, then
<math display="block">
\Pr[\operatorname{Poly1305}_r(m_1) - \operatorname{Poly1305}_r(m_2) \equiv d \pmod{2^{128}}]
\leq \frac{8\lceil L/16\rceil}{2^{106}},
</math>
where <math>r</math> is a uniform random Poly1305 key.[2]Template:Rp
This property is sometimes called <math>\epsilon</math>-almost-Δ-universality over <math>\mathbb Z/2^{128}\mathbb Z</math>, or <math>\epsilon</math>-AΔU,[11] where <math>\epsilon = 8\lceil L/16\rceil/2^{106}</math> in this case.
Of one-time authenticator
With a one-time authenticator <math>a = \bigl(\operatorname{Poly1305}_r(m) + s\bigr) \bmod 2^{128}</math>, the adversary's success probability for any forgery attempt <math>(a', m')</math> on a message <math>m'</math> of up to <math>L</math> bytes is:
<math display="block"> \begin{align}
\Pr[&a' = \operatorname{Poly1305}_r(m') + s
\mathrel\mid a = \operatorname{Poly1305}_r(m) + s] \\
&= \Pr[a' = \operatorname{Poly1305}_r(m') + a - \operatorname{Poly1305}_r(m)] \\
&= \Pr[\operatorname{Poly1305}_r(m') - \operatorname{Poly1305}_r(m) = a' - a] \\
&\leq 8\lceil L/16\rceil/2^{106}.
\end{align} </math>
Here arithmetic inside the <math>\Pr[\cdots]</math> is taken to be in <math>\mathbb Z/2^{128}\mathbb Z</math> for simplicity.
Of NaCl and ChaCha20-Poly1305
For NaCl crypto_secretbox_xsalsa20poly1305 and ChaCha20-Poly1305, the adversary's success probability at forgery is the same for each message independently as for a one-time authenticator, plus the adversary's distinguishing advantage <math>\delta</math> against XSalsa20 or ChaCha as pseudorandom functions used to generate the per-message key. In other words, the probability that the adversary succeeds at a single forgery after <math>D</math> attempts of messages up to <math>L</math> bytes is at most:
<math display="block">
\delta + \frac{8D \lceil L/16\rceil}{2^{106}}.
</math>
Of Poly1305-AES
The security of Poly1305-AES against forgery follows from the Carter–Wegman–Shoup structure, which instantiates a Carter–Wegman authenticator with a permutation to generate the per-message pad.[12] If an adversary sees <math>C</math> authenticated messages and attempts <math>D</math> forgeries of messages of up to <math>L</math> bytes, and if the adversary has distinguishing advantage at most <math>\delta</math> against AES-128 as a pseudorandom permutation, then the probability the adversary succeeds at any one of the <math>D</math> forgeries is at most:[2]
<math display="block">
\delta + \frac{(1 - C/2^{128})^{-(C + 1)/2} \cdot 8 D \lceil L/16\rceil}{2^{106}}.
</math>
Speed
Poly1305-AES can be computed at high speed in various CPUs: for an n-byte message, no more than 3.1n + 780 Athlon cycles are needed,[2] for example. The author has released optimized source code for Athlon, Pentium Pro/II/III/M, PowerPC, and UltraSPARC, in addition to non-optimized reference implementations in C and C++ as public domain software.[13]
Implementations
Below is a list of cryptography libraries that support Poly1305:
- Botan
- Bouncy Castle
- Crypto++
- Libgcrypt
- libsodium
- Nettle
- OpenSSL
- LibreSSL
- wolfCrypt
- GnuTLS
- mbed TLS
- MatrixSSL
See also
- ChaCha20-Poly1305 – an AEAD scheme combining the stream cipher ChaCha20 with a variant of Poly1305
References
External links
- Poly1305-AES reference and optimized implementation by author D. J. Bernstein
- Fast Poly1305 implementation in C on github.com
- NaCl one-time authenticator and authenticated cipher using Poly1305
- ↑ 1.0 1.1 1.2 1.3 Template:Cite book
- ↑ 2.0 2.1 2.2 2.3 2.4 2.5 2.6 Template:Cite conference
- ↑ Template:Cite book
- ↑ 4.0 4.1 Template:Cite journal
- ↑ 5.0 5.1 Template:Cite book
- ↑ 6.0 6.1 Template:Cite tech report
- ↑ Template:Cite ietf
- ↑ 8.0 8.1 Template:Cite ietf
- ↑ Template:Cite ietf
- ↑ Template:Cite journal
- ↑ Template:Cite conference
- ↑ Template:Cite conference
- ↑ A state-of-the-art message-authentication code on cr.yp.to