1 // This file is dual licensed under the terms of the Apache License, Version
2 // 2.0, and the BSD License. See the LICENSE file in the root of this
3 // repository for complete details.
4 
Cryptography_constant_time_bytes_eq(uint8_t * a,size_t len_a,uint8_t * b,size_t len_b)5 uint8_t Cryptography_constant_time_bytes_eq(uint8_t *a, size_t len_a,
6                                             uint8_t *b, size_t len_b) {
7     size_t i = 0;
8     uint8_t mismatch = 0;
9     if (len_a != len_b) {
10         return 0;
11     }
12     for (i = 0; i < len_a; i++) {
13         mismatch |= a[i] ^ b[i];
14     }
15 
16     /* Make sure any bits set are copied to the lowest bit */
17     mismatch |= mismatch >> 4;
18     mismatch |= mismatch >> 2;
19     mismatch |= mismatch >> 1;
20     /* Now check the low bit to see if it's set */
21     return (mismatch & 1) == 0;
22 }
23