blob: 09ae5e854fed94c98e93a940976043b96feb268e [file] [log] [blame]
Thai Duong7689ed62015-03-20 16:50:18 -07001/*
2 * Copyright 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef SYSTEM_KEYMASTER_HMAC_H_
18#define SYSTEM_KEYMASTER_HMAC_H_
19
20#include <keymaster/serializable.h>
21
22namespace keymaster {
23
24
25// Only HMAC-SHA256 is supported.
26class HmacSha256 {
27 public:
28 HmacSha256() {};
29
30 // DigestLength returns the length, in bytes, of the resulting digest.
31 size_t DigestLength() const;
32
33 // Initializes this instance using |key|. Call Init only once. It returns
34 // false on the second of later calls.
35 bool Init(const uint8_t* key, size_t key_length);
36 bool Init(const Buffer& key);
37
38 // Sign calculates the HMAC of |data| with the key supplied to the Init
39 // method. At most |digest_len| bytes of the resulting digest are written
40 // to |digest|.
41 bool Sign(const Buffer& data, uint8_t* digest, size_t digest_len) const;
42 bool Sign(const uint8_t* data, size_t data_len, uint8_t* digest, size_t digest_len) const;
43
44 // Verify returns true if |digest| is a valid HMAC of |data| using the key
45 // supplied to Init. |digest| must be exactly |DigestLength()| bytes long.
46 // Use of this method is strongly recommended over using Sign() with a manual
47 // comparison (such as memcmp), as such comparisons may result in
48 // side-channel disclosures, such as timing, that undermine the cryptographic
49 // integrity.
50 bool Verify(const Buffer& data, const Buffer& digest) const;
51 bool Verify(const uint8_t* data, size_t data_len, const uint8_t* digest,
52 size_t digest_len) const;
53
54 private:
55 Buffer key_;
56};
57
58} // namespace keymaster
59
60#endif // SYSTEM_KEYMASTER_HMAC_H_