Add a missing carry to the generic poly1305 implementation

See also https://github.com/floodyberry/poly1305-donna/commit/e4c7e7daa0
which was not quite so theoretical.

We do not believe this to have security consequences. We end up
computing a Poly1305 tag that is 2^26 less than it should be. That
means, in these cases, we effectively have 105 bits worth of MAC instead
of 106 bits. (Poly1305's 128-bit r values get clamped to 106 bits.)

The lost bit, in turn, only comes up in a vanishingly small number of
cases (some 120-odd bits in the unreduced tag have to be all ones).
Finally, whether we hit those cases at all is not under attacker control
since it depends on the Poly1305 key. (In ChaCha20-Poly1305, the
Poly1305 key is derived from running ChaCha20.)

Nonetheless, carry bugs are remarkably difficult to catch, so this is
very good to have gotten fixed. Thanks to David Benbennick for catching
this!

Note for reviewing: p in poly1305 is 2^130 - 5. The scheme is designed
around reduction mod p being very easy (take bits above 130, multiply by
5, add back in). This particular implementation uses five unsaturated
limbs with base 2^26, which works out nicely because 5 * 26 = 130. This
is why you'll see 5 and 26 show up throughout here.

Fixed: 590064899
Change-Id: I6c1add065d290ec3ef171c86c08250f38dc6e950
Reviewed-on: https://boringssl-review.googlesource.com/c/boringssl/+/106470
Auto-Submit: David Benjamin <davidben@google.com>
Commit-Queue: David Benjamin <davidben@google.com>
Reviewed-by: Lily Chen <chlily@google.com>
diff --git a/crypto/cipher/test/chacha20_poly1305_tests.txt b/crypto/cipher/test/chacha20_poly1305_tests.txt
index 4e65529..0529f7d 100644
--- a/crypto/cipher/test/chacha20_poly1305_tests.txt
+++ b/crypto/cipher/test/chacha20_poly1305_tests.txt
@@ -573,3 +573,11 @@
 AD: 85c112a1efe0a20ef3a550526a7afbc98f6367ebbede4e703099abd78f51
 CT: b5cc754f6dd19ef2d66f90e6bc9a322ddf216ef248cbe76b5ab6dd53bc36
 TAG: d3f7b9c295f374651a84138648a591
+
+# Regression test for https://crbug.com/590064899
+KEY: 0000000000000000000000000000000000000000000000000000000000000000
+NONCE: 000000000000000000000000
+IN: 03052a6a2cb1347e6872c8b9481459fd
+AD: ""
+CT: 9c02cdd479e00c04f0c85fc53b3951f0
+TAG: bdd219c0a08ded1aa836efcc8b770dc7
diff --git a/crypto/poly1305/poly1305.cc b/crypto/poly1305/poly1305.cc
index 869722d..b9133d8 100644
--- a/crypto/poly1305/poly1305.cc
+++ b/crypto/poly1305/poly1305.cc
@@ -260,6 +260,8 @@
     poly1305_update(state, state->buf, state->buf_used);
   }
 
+  // h1..h4 are 26-bit, but h0 may have excess bits. We need a fully-reduced
+  // value. First, carry the excess bits across each limb.
   b = state->h0 >> 26;
   state->h0 = state->h0 & 0x3ffffff;
   state->h1 += b;
@@ -274,8 +276,20 @@
   state->h4 += b;
   b = state->h4 >> 26;
   state->h4 = state->h4 & 0x3ffffff;
+
+  // This may carry beyond 130 bits. Reduce to clear the carry bit.
   state->h0 += b * 5;
 
+  // Reduction may have given h0 one more excess bit, so carry again.
+  b = state->h0 >> 26;
+  state->h0 = state->h0 & 0x3ffffff;
+  state->h1 += b;
+  // h1 cannot overflow 26 bits here. For `b` to be non-zero, h1 must have
+  // overflowed in the first carry chain, leaving it less than 2^6.
+  declassify_assert(state->h1 >> 26 == 0);
+
+  // Each limb is 26-bit, but the overall value may be >= p. Compute
+  // g = h - p = h + 5 - 2^130.
   g0 = state->h0 + 5;
   b = g0 >> 26;
   g0 &= 0x3ffffff;
@@ -290,6 +304,7 @@
   g3 &= 0x3ffffff;
   g4 = state->h4 + b - (1 << 26);
 
+  // Select h or h - p as appropriate.
   b = (g4 >> 31) - 1;
   nb = ~b;
   state->h0 = (state->h0 & nb) | (g0 & b);
diff --git a/crypto/poly1305/poly1305_tests.txt b/crypto/poly1305/poly1305_tests.txt
index adaa022..311cb23 100644
--- a/crypto/poly1305/poly1305_tests.txt
+++ b/crypto/poly1305/poly1305_tests.txt
@@ -157,3 +157,9 @@
 Key = 00ff000000000000000000000000000000000000001e00000000000000007b7b
 Input = 7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7a7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b5c7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b6e7b007b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7a7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b5c7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b7b6e7b001300000000b300000000000000000000000000000000000000000000f20000000000000000000000000000000000002000efff0009000000000000000000000000100000000009000000640000000000000000000000001300000000b300000000000000000000000000000000000000000000f20000000000000000000000000000000000002000efff00090000000000000000007a000010000000000900000064000000000000000000000000000000000000000000000000fc
 MAC = 33205bbf9e9f8f7212ab9e2ab9b7e4a5
+
+# An input that needs a longer final carry chain in a 26-bit representation.
+# This is a regression test for https://crbug.com/590064899
+Key = feffff0300000000000000000000000000000000000000000000000000000000
+Input = af97d05e2fa1bd5e427bbd84f67a09ed
+MAC = 04000008000000000000000000000000