You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

verify.c 1.6 KiB

5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
5 jaren geleden
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include <stdint.h>
  2. #include <string.h>
  3. /*************************************************
  4. * Name: verify
  5. *
  6. * Description: Compare two arrays for equality in constant time.
  7. *
  8. * Arguments: const unsigned char *a: pointer to first byte array
  9. * const unsigned char *b: pointer to second byte array
  10. * size_t len: length of the byte arrays
  11. *
  12. * Returns 0 if the byte arrays are equal, 1 otherwise
  13. **************************************************/
  14. int PQCLEAN_KYBER768_verify(const unsigned char *a, const unsigned char *b,
  15. size_t len) {
  16. uint64_t r;
  17. size_t i;
  18. r = 0;
  19. for (i = 0; i < len; i++) {
  20. r |= a[i] ^ b[i];
  21. }
  22. r = (-r) >> 63;
  23. return r;
  24. }
  25. /*************************************************
  26. * Name: cmov
  27. *
  28. * Description: Copy len bytes from x to r if b is 1;
  29. * don't modify x if b is 0. Requires b to be in {0,1};
  30. * assumes two's complement representation of negative integers.
  31. * Runs in constant time.
  32. *
  33. * Arguments: unsigned char *r: pointer to output byte array
  34. * const unsigned char *x: pointer to input byte array
  35. * size_t len: Amount of bytes to be copied
  36. * unsigned char b: Condition bit; has to be in {0,1}
  37. **************************************************/
  38. void PQCLEAN_KYBER768_cmov(unsigned char *r, const unsigned char *x, size_t len,
  39. unsigned char b) {
  40. size_t i;
  41. b = -b;
  42. for (i = 0; i < len; i++) {
  43. r[i] ^= b & (x[i] ^ r[i]);
  44. }
  45. }