oxedyne/fe2o3/fe2o3_crypto/src/c/verify.c
925 bytes, 1 run
created by r1870400018:224, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | /*------------------------------------------------- |
| 2 | This file has been adapted from the implementation |
| 3 | (available at https://github.com/pq-crystals/kyber) of |
| 4 | "CRYSTALS – Kyber: a CCA-secure module-lattice-based KEM" |
| 5 | by : Joppe Bos, Leo Ducas, Eike Kiltz, Tancrede Lepoint, |
| 6 | Vadim Lyubashevsky, John M. Schanck, Peter Schwabe & Damien stehle |
| 7 | ----------------------------------------------------*/ |
| 8 | #include <string.h> |
| 9 | #include <stdint.h> |
| 10 | #include "verify.h" |
| 11 | |
| 12 | /* returns 0 for equal strings, 1 for non-equal strings */ |
| 13 | int verify(const unsigned char *a, const unsigned char *b, size_t len) |
| 14 | { |
| 15 | uint64_t r; |
| 16 | size_t i; |
| 17 | r = 0; |
| 18 | |
| 19 | for (i = 0; i < len; i++) |
| 20 | r |= a[i] ^ b[i]; |
| 21 | |
| 22 | r = (-r) >> 63; |
| 23 | return r; |
| 24 | } |
| 25 | |
| 26 | /* b = 1 means mov, b = 0 means don't mov*/ |
| 27 | void cmov(unsigned char *r, const unsigned char *x, size_t len, unsigned char b) |
| 28 | { |
| 29 | size_t i; |
| 30 | |
| 31 | b = -b; |
| 32 | for (i = 0; i < len; i++) |
| 33 | r[i] ^= b & (x[i] ^ r[i]); |
| 34 | } |