2019-04-13 16:47:29 +01:00
|
|
|
#include "verify.h"
|
|
|
|
|
2019-09-10 10:45:01 +01:00
|
|
|
#include <stddef.h>
|
2019-05-06 13:50:27 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2019-01-15 15:03:38 +00:00
|
|
|
/*************************************************
|
2019-05-06 13:50:27 +01:00
|
|
|
* Name: verify
|
|
|
|
*
|
|
|
|
* Description: Compare two arrays for equality in constant time.
|
|
|
|
*
|
2019-09-10 10:45:01 +01:00
|
|
|
* Arguments: const uint8_t *a: pointer to first byte array
|
|
|
|
* const uint8_t *b: pointer to second byte array
|
2019-05-06 13:50:27 +01:00
|
|
|
* size_t len: length of the byte arrays
|
|
|
|
*
|
|
|
|
* Returns 0 if the byte arrays are equal, 1 otherwise
|
|
|
|
**************************************************/
|
2019-09-10 10:45:01 +01:00
|
|
|
uint8_t PQCLEAN_KYBER768_CLEAN_verify(const uint8_t *a, const uint8_t *b, size_t len) {
|
2019-01-16 10:02:32 +00:00
|
|
|
uint64_t r;
|
|
|
|
size_t i;
|
2019-05-06 13:50:27 +01:00
|
|
|
r = 0;
|
2019-09-10 10:45:01 +01:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
r |= a[i] ^ b[i];
|
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-09-10 10:45:01 +01:00
|
|
|
r = (-r) >> 63;
|
|
|
|
return (uint8_t)r;
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************
|
2019-05-06 13:50:27 +01:00
|
|
|
* Name: cmov
|
|
|
|
*
|
|
|
|
* Description: Copy len bytes from x to r if b is 1;
|
|
|
|
* don't modify x if b is 0. Requires b to be in {0,1};
|
|
|
|
* assumes two's complement representation of negative integers.
|
|
|
|
* Runs in constant time.
|
|
|
|
*
|
2019-09-10 10:45:01 +01:00
|
|
|
* Arguments: uint8_t *r: pointer to output byte array
|
|
|
|
* const uint8_t *x: pointer to input byte array
|
2019-05-06 13:50:27 +01:00
|
|
|
* size_t len: Amount of bytes to be copied
|
2019-09-10 10:45:01 +01:00
|
|
|
* uint8_t b: Condition bit; has to be in {0,1}
|
2019-05-06 13:50:27 +01:00
|
|
|
**************************************************/
|
2019-09-10 10:45:01 +01:00
|
|
|
void PQCLEAN_KYBER768_CLEAN_cmov(uint8_t *r, const uint8_t *x, size_t len, uint8_t b) {
|
2019-01-16 10:02:32 +00:00
|
|
|
size_t i;
|
2019-01-15 15:03:38 +00:00
|
|
|
|
2019-01-16 10:02:32 +00:00
|
|
|
b = -b;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
r[i] ^= b & (x[i] ^ r[i]);
|
|
|
|
}
|
2019-01-15 15:03:38 +00:00
|
|
|
}
|