-
Notifications
You must be signed in to change notification settings - Fork 13.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added constant time string comparison to avoid possible time-based attacks. #3836
Conversation
libraries/ArduinoOTA/ArduinoOTA.h
Outdated
@@ -93,6 +93,7 @@ class ArduinoOTAClass | |||
void _onRx(void); | |||
int parseInt(void); | |||
String readStringUntil(char end); | |||
bool constantTimeEquals(const String &string1, const String &string2); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: can this method be moved into String class, so reuse in other places is possible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok I'll do that
libraries/ArduinoOTA/ArduinoOTA.cpp
Outdated
|
||
// Preliminary check | ||
if(string1.length() != string2.length()) { | ||
return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indentation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
libraries/ArduinoOTA/ArduinoOTA.cpp
Outdated
bool equals = true; | ||
unsigned int len = string1.length(); | ||
for(unsigned int i = 0; i < len; i++){ | ||
equals &= (string1.charAt(i) == string2.charAt(i)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be interesting to see the generated code for this loop at -O2 optimization level... I wonder whether the compiler is smart enough to convert this 'and' into 'branch-if-zero'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. Maybe better to write not so easily optimizable code. Ill think about that
…unction body to assure constant time comparison despite compiler optimizations
# Conflicts: # libraries/ArduinoOTA/ArduinoOTA.cpp
@igrr tried to met all your requests |
Except for my two nitpick cleanups, this looks good to me. |
This should still be tested more thoroughly, but after careful inspection, the code looks good, so merging. |
This should resolve #1127.