"Fossies" - the Fresh Open Source Software Archive

Member "dhcpcd-9.4.1/compat/consttime_memequal.h" (22 Oct 2021, 700 Bytes) of package /linux/misc/dhcpcd-9.4.1.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 /*
    2  * Written by Matthias Drochner <drochner@NetBSD.org>.
    3  * Public domain.
    4  */
    5 
    6 #ifndef CONSTTIME_MEMEQUAL_H
    7 #define CONSTTIME_MEMEQUAL_H
    8 inline static int
    9 consttime_memequal(const void *b1, const void *b2, size_t len)
   10 {
   11     const unsigned char *c1 = b1, *c2 = b2;
   12     unsigned int res = 0;
   13 
   14     while (len--)
   15         res |= *c1++ ^ *c2++;
   16 
   17     /*
   18      * Map 0 to 1 and [1, 256) to 0 using only constant-time
   19      * arithmetic.
   20      *
   21      * This is not simply `!res' because although many CPUs support
   22      * branchless conditional moves and many compilers will take
   23      * advantage of them, certain compilers generate branches on
   24      * certain CPUs for `!res'.
   25      */
   26     return (1 & ((res - 1) >> 8));
   27 }
   28 #endif /* CONSTTIME_MEMEQUAL_H */