safebfuns.c (642B)
1 /* Public domain */ 2 3 #include <string.h> 4 5 #if defined( _MSC_VER ) 6 #pragma optimize( "", off ) 7 #elif defined( __clang__ ) 8 #pragma clang optimize off 9 #elif defined( __GNUC__ ) 10 #pragma GCC optimize ( "O0" ) 11 #else 12 #error new compiler 13 #endif 14 15 void explicit_bzero( void * buf, size_t n ) { 16 volatile char * volbuf = ( volatile char * ) buf; 17 for( size_t i = 0; i < n; i++ ) { 18 volbuf[ i ] = 0; 19 } 20 } 21 22 int timingsafe_bcmp( const void * b1, const void * b2, size_t n ) { 23 const unsigned char * p1 = b1; 24 const unsigned char * p2 = b2; 25 int result = 0; 26 27 for( size_t i = 0; i < n; i++ ) { 28 result |= p1[ i ] ^ p2[ i ]; 29 } 30 31 return result != 0; 32 }