Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. // returns 1 is same, 0 otherwise
  2. int timing_safe_compare(char *known, size_t known_len, char *unknown, size_t unknown_len) {
  3. // Safe since all strings **will** be null terminated
  4. size_t mod_len = known_len + 1;
  5. size_t i;
  6. int result = 0;
  7.  
  8. result = known_len - unknown_len;
  9. for (i = 0; i < unknown_len; i++) {
  10. result |= known[i % mod_len] ^ unknown[i];
  11. }
  12.  
  13. return result == 0 ? 1 : 0;
  14. }
  15.  
  16. int check(char *known, size_t known_len, char *unknown, size_t unknown_len, size_t max_len) {
  17.  
  18. size_t i;
  19. int result = 0;
  20.  
  21. // Constant time check, only gives away maximum length.
  22. if (unknown_len > max_len)
  23. return 0;
  24.  
  25. // Will only give away the length of the attackers string, unless it was already too large (condition above). Don't bother doing an extra memcpy on your known or the attackers.
  26. for (i = 0; i < unknown_len; i++) {
  27. result |= known[i] ^ unknown[i];
  28. }
  29.  
  30. return result == 0 ? 1 : 0;
  31. }
  32.  
  33. bool timing_safe_equal(const std::string &s1, const std::string &s2)
  34. {
  35. const std::string hash1=sha384(s1);
  36. const std::string hash2=sha384(s2);
  37. int result=0;
  38. for (uint i = 0; i < 48; ++i)
  39. {
  40. result |= hash1[i] ^ hash2[i];
  41. }
  42. return (result == 0);
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement