Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: crypto/hmac.h

Issue 7522014: Add WARN_UNUSED_RESULT to crypto/hmac.h (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase onto CL 7532020 and update remoting Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/common/net/gaia/oauth_request_signer.cc ('k') | crypto/hmac_mac.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Utility class for calculating the HMAC for a given message. We currently 5 // Utility class for calculating the HMAC for a given message. We currently
6 // only support SHA1 for the hash algorithm, but this can be extended easily. 6 // only support SHA1 for the hash algorithm, but this can be extended easily.
7 7
8 #ifndef CRYPTO_HMAC_H_ 8 #ifndef CRYPTO_HMAC_H_
9 #define CRYPTO_HMAC_H_ 9 #define CRYPTO_HMAC_H_
10 #pragma once 10 #pragma once
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
14 #include "base/string_piece.h" 15 #include "base/string_piece.h"
15 #include "crypto/crypto_api.h" 16 #include "crypto/crypto_api.h"
16 17
17 namespace crypto { 18 namespace crypto {
18 19
19 // Simplify the interface and reduce includes by abstracting out the internals. 20 // Simplify the interface and reduce includes by abstracting out the internals.
20 struct HMACPlatformData; 21 struct HMACPlatformData;
21 22
22 class CRYPTO_API HMAC { 23 class CRYPTO_API HMAC {
23 public: 24 public:
24 // The set of supported hash functions. Extend as required. 25 // The set of supported hash functions. Extend as required.
25 enum HashAlgorithm { 26 enum HashAlgorithm {
26 SHA1, 27 SHA1,
27 SHA256, 28 SHA256,
28 }; 29 };
29 30
30 explicit HMAC(HashAlgorithm hash_alg); 31 explicit HMAC(HashAlgorithm hash_alg);
31 ~HMAC(); 32 ~HMAC();
32 33
33 // Returns the length of digest that this HMAC will create. 34 // Returns the length of digest that this HMAC will create.
34 size_t DigestLength() const; 35 size_t DigestLength() const;
35 36
36 // TODO(abarth): Add a PreferredKeyLength() member function. 37 // TODO(abarth): Add a PreferredKeyLength() member function.
37 38
38 // Initializes this instance using |key| of the length |key_length|. Call Init 39 // Initializes this instance using |key| of the length |key_length|. Call Init
39 // only once. It returns false on the second or later calls. 40 // only once. It returns false on the second or later calls.
40 // TODO(abarth): key_length should be a size_t. 41 // TODO(abarth): key_length should be a size_t.
41 bool Init(const unsigned char* key, int key_length); 42 bool Init(const unsigned char* key, int key_length) WARN_UNUSED_RESULT;
42 43
43 // Initializes this instance using |key|. Call Init only once. It returns 44 // Initializes this instance using |key|. Call Init only once. It returns
44 // false on the second or later calls. 45 // false on the second or later calls.
45 bool Init(const base::StringPiece& key) { 46 bool Init(const base::StringPiece& key) WARN_UNUSED_RESULT {
46 return Init(reinterpret_cast<const unsigned char*>(key.data()), 47 return Init(reinterpret_cast<const unsigned char*>(key.data()),
47 static_cast<int>(key.size())); 48 static_cast<int>(key.size()));
48 } 49 }
49 50
50 // Calculates the HMAC for the message in |data| using the algorithm supplied 51 // Calculates the HMAC for the message in |data| using the algorithm supplied
51 // to the constructor and the key supplied to the Init method. The HMAC is 52 // to the constructor and the key supplied to the Init method. The HMAC is
52 // returned in |digest|, which has |digest_length| bytes of storage available. 53 // returned in |digest|, which has |digest_length| bytes of storage available.
53 // TODO(abarth): digest_length should be a size_t. 54 // TODO(abarth): digest_length should be a size_t.
54 bool Sign(const base::StringPiece& data, unsigned char* digest, 55 bool Sign(const base::StringPiece& data, unsigned char* digest,
55 int digest_length) const; 56 int digest_length) const WARN_UNUSED_RESULT;
56 57
57 // Verifies that the HMAC for the message in |data| equals the HMAC provided 58 // Verifies that the HMAC for the message in |data| equals the HMAC provided
58 // in |digest|, using the algorithm supplied to the constructor and the key 59 // in |digest|, using the algorithm supplied to the constructor and the key
59 // supplied to the Init method. Use of this method is strongly recommended 60 // supplied to the Init method. Use of this method is strongly recommended
60 // over using Sign() with a manual comparison (such as memcmp), as such 61 // over using Sign() with a manual comparison (such as memcmp), as such
61 // comparisons may result in side-channel disclosures, such as timing, that 62 // comparisons may result in side-channel disclosures, such as timing, that
62 // undermine the cryptographic integrity. |digest| must be exactly 63 // undermine the cryptographic integrity. |digest| must be exactly
63 // |DigestLength()| bytes long. 64 // |DigestLength()| bytes long.
64 bool Verify(const base::StringPiece& data, 65 bool Verify(const base::StringPiece& data,
65 const base::StringPiece& digest) const; 66 const base::StringPiece& digest) const WARN_UNUSED_RESULT;
66 67
67 // Verifies a truncated HMAC, behaving identical to Verify(), except 68 // Verifies a truncated HMAC, behaving identical to Verify(), except
68 // that |digest| is allowed to be smaller than |DigestLength()|. 69 // that |digest| is allowed to be smaller than |DigestLength()|.
69 bool VerifyTruncated(const base::StringPiece& data, 70 bool VerifyTruncated(
70 const base::StringPiece& digest) const; 71 const base::StringPiece& data,
72 const base::StringPiece& digest) const WARN_UNUSED_RESULT;
71 73
72 private: 74 private:
73 HashAlgorithm hash_alg_; 75 HashAlgorithm hash_alg_;
74 scoped_ptr<HMACPlatformData> plat_; 76 scoped_ptr<HMACPlatformData> plat_;
75 77
76 DISALLOW_COPY_AND_ASSIGN(HMAC); 78 DISALLOW_COPY_AND_ASSIGN(HMAC);
77 }; 79 };
78 80
79 } // namespace crypto 81 } // namespace crypto
80 82
81 #endif // CRYPTO_HMAC_H_ 83 #endif // CRYPTO_HMAC_H_
OLDNEW
« no previous file with comments | « chrome/common/net/gaia/oauth_request_signer.cc ('k') | crypto/hmac_mac.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698