• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef CRYPTO_SECURE_UTIL_H_
6 #define CRYPTO_SECURE_UTIL_H_
7 
8 #include <stddef.h>
9 
10 #include "base/containers/span.h"
11 #include "crypto/crypto_export.h"
12 
13 namespace crypto {
14 
15 // Performs a constant-time comparison of two strings, returning true if the
16 // strings are equal. Note that while the contents of the strings are not
17 // leaked, their *lengths* may be leaked - there is no way to do a comparison
18 // whose timing does not depend, at least coarsely, on the length of the data
19 // being compared.
20 //
21 // For cryptographic operations, comparison functions such as memcmp() may
22 // expose side-channel information about input, allowing an attacker to
23 // perform timing analysis to determine what the expected bits should be. In
24 // order to avoid such attacks, the comparison must execute in constant time,
25 // so as to not to reveal to the attacker where the difference(s) are.
26 // For an example attack, see
27 // http://groups.google.com/group/keyczar-discuss/browse_thread/thread/5571eca0948b2a13
28 CRYPTO_EXPORT bool SecureMemEqual(base::span<const uint8_t> s1,
29                                   base::span<const uint8_t> s2);
30 
31 }  // namespace crypto
32 
33 #endif  // CRYPTO_SECURE_UTIL_H_
34 
35