forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make constant-time comparison operators for cryptographic uses public.
Review URL: http://codereview.chromium.org/8124011 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@104502 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
palmer@chromium.org
committed
Oct 7, 2011
1 parent
11da0c8
commit 3cdf6d4
Showing
4 changed files
with
54 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright (c) 2011 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "crypto/secure_util.h" | ||
|
||
namespace crypto { | ||
|
||
bool SecureMemEqual(const void* s1, const void* s2, size_t n) { | ||
const unsigned char* s1_ptr = reinterpret_cast<const unsigned char*>(s1); | ||
const unsigned char* s2_ptr = reinterpret_cast<const unsigned char*>(s2); | ||
unsigned char tmp = 0; | ||
for (size_t i = 0; i < n; ++i, ++s1_ptr, ++s2_ptr) | ||
tmp |= *s1_ptr ^ *s2_ptr; | ||
return (tmp == 0); | ||
} | ||
|
||
} // namespace crypto | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright (c) 2011 The Chromium Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef CRYPTO_SECURE_UTIL_H_ | ||
#define CRYPTO_SECURE_UTIL_H_ | ||
#pragma once | ||
|
||
#include <stddef.h> | ||
|
||
#include "crypto/crypto_export.h" | ||
|
||
namespace crypto { | ||
|
||
// Performs a constant-time comparison of two strings, returning true if the | ||
// strings are equal. | ||
// | ||
// For cryptographic operations, comparison functions such as memcmp() may | ||
// expose side-channel information about input, allowing an attacker to | ||
// perform timing analysis to determine what the expected bits should be. In | ||
// order to avoid such attacks, the comparison must execute in constant time, | ||
// so as to not to reveal to the attacker where the difference(s) are. | ||
// For an example attack, see | ||
// http://groups.google.com/group/keyczar-discuss/browse_thread/thread/5571eca0948b2a13 | ||
CRYPTO_EXPORT bool SecureMemEqual(const void* s1, const void* s2, size_t n); | ||
|
||
} // namespace crypto | ||
|
||
#endif // CRYPTO_SECURE_UTIL_H_ | ||
|