Browse Source

add constant_time_algo.hh/.cc with constant_time_equal() only at the moment.

pull/1/head Release_2.1.0-RC26
Roker 2 years ago
parent
commit
e9ab9662a6
2 changed files with 33 additions and 0 deletions
  1. +19
    -0
      constant_time_algo.cc
  2. +14
    -0
      constant_time_algo.hh

+ 19
- 0
constant_time_algo.cc View File

@ -0,0 +1,19 @@
#include "constant_time_algo.hh"
namespace pEp
{
bool constant_time_equal(const std::string& a, const std::string& b)
{
if(a.size() != b.size())
return false;
unsigned d = 0;
for(std::size_t idx = 0; idx<a.size(); ++idx)
{
d |= ( static_cast<unsigned>(a[idx]) ^ static_cast<unsigned>(b[idx]) );
}
return d != 0;
}
} // end of namespace pEp

+ 14
- 0
constant_time_algo.hh View File

@ -0,0 +1,14 @@
#pragma once
#include <string>
namespace pEp
{
// Returns false if a.size() != b.size().
// Compares always _all_ characters of 'a' and 'b' so runtime does not
// depends on the character position where the strings differ.
// Use this function instead of operator== if timing sidechannel attack
// might be a security problem.
bool constant_time_equal(const std::string& a, const std::string& b);
} // end of namespace pEp

Loading…
Cancel
Save