]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Fix a bug with less optimized polyval variants.
authorNick Mathewson <nickm@torproject.org>
Thu, 22 May 2025 13:54:09 +0000 (09:54 -0400)
committerNick Mathewson <nickm@torproject.org>
Thu, 22 May 2025 13:54:09 +0000 (09:54 -0400)
Using "0" to mean "doesn't support multi-block processing"
ran us into trouble: (n > 0 * 16) is always true for n > 0,
so we were always running a loop with no termination condition.

Additionally, the >s in this block should have been >=s,
since we want to process multi-blocks as long as there are any.
This won't have a performance impact for our current input sizes,
but it's nice to be correct.

src/ext/polyval/polyval.c

index ae27377a7b881c7f703ef98308e1e5a3b8c36131..900a50385b508d37e141d8231a9516a91e60f974 100644 (file)
@@ -235,6 +235,11 @@ static inline void expand_key_none(const polyval_t *inp,
   (void) out;
 }
 
+/* Kludge: a special value to use for block_stride when we don't support
+ * processing multiple blocks at once.  Previously we used 0, but that
+ * caused warnings with some comparisons. */
+#define BLOCK_STRIDE_NONE  0xffff
+
 #define PV_DECLARE(prefix,                                              \
                    st,                                                  \
                    u128_from_bytes,                                     \
@@ -270,10 +275,12 @@ static inline void expand_key_none(const polyval_t *inp,
   st void                                                               \
   prefix ## polyval_add_zpad(polyval_t *pv, const uint8_t *data, size_t n) \
   {                                                                     \
-    if (n > block_stride * 16) {                                        \
+    /* since block_stride is a constant, this should get optimized */   \
+    if ((block_stride != BLOCK_STRIDE_NONE)                             \
+        && n >= (block_stride) * 16) {                                  \
       expanded_key_tp expanded_key;                                     \
       expand_fn(pv, &expanded_key);                                     \
-      while (n > block_stride * 16) {                                   \
+      while (n >= (block_stride) * 16) {                                \
         add_multiple_fn(pv, data, &expanded_key);                       \
         n -= block_stride*16;                                           \
         data += block_stride * 16;                                      \
@@ -326,7 +333,7 @@ PV_DECLARE(ctmul64_, static,
            u128_to_bytes_ctmul64,
            pv_xor_y_ctmul64,
            pv_mul_y_h_ctmul64,
-           0,
+           BLOCK_STRIDE_NONE,
            struct expanded_key_none,
            expand_key_none,
            add_multiple_none)
@@ -404,7 +411,7 @@ PV_DECLARE(, ,
            u128_to_bytes_ctmul64,
            pv_xor_y_ctmul64,
            pv_mul_y_h_ctmul64,
-           0,
+           BLOCK_STRIDE_NONE,
            struct expanded_key_none,
            expand_key_none,
            add_multiple_none)
@@ -414,7 +421,7 @@ PV_DECLARE(, , u128_from_bytes_ctmul,
            u128_to_bytes_ctmul,
            pv_xor_y_ctmul,
            pv_mul_y_h_ctmul,
-           0,
+           BLOCK_STRIDE_NONE,
            struct expanded_key_none,
            expand_key_none,
            add_multiple_none)