blob: 78e254de6004112da39a8fb0134a3d5560f63042 [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#include "hmac.h"
18
19#include <assert.h>
20#include <openssl/evp.h>
21#include <openssl/hmac.h>
22#include <openssl/sha.h>
23
24namespace keymaster {
25
26size_t HmacSha256::DigestLength() const {
27 return SHA256_DIGEST_LENGTH;
28}
29
Thai Duong60eebdc2015-03-25 17:20:24 -070030bool HmacSha256::Init(const Buffer& key) {
31 return Init(key.peek_read(), key.available_read());
32}
33
Thai Duong7689ed62015-03-20 16:50:18 -070034bool HmacSha256::Init(const uint8_t* key, size_t key_len) {
35 if (!key)
36 return false;
37
Thai Duong60eebdc2015-03-25 17:20:24 -070038 key_len_ = key_len;
39 key_.reset(new uint8_t[key_len]);
40 if (!key_.get()) {
41 return false;
42 }
43 memcpy(key_.get(), key, key_len);
Thai Duong7689ed62015-03-20 16:50:18 -070044 return true;
45}
46
Thai Duong7689ed62015-03-20 16:50:18 -070047bool HmacSha256::Sign(const Buffer& data, uint8_t* out_digest, size_t digest_len) const {
48 return Sign(data.peek_read(), data.available_read(), out_digest, digest_len);
49}
50
51bool HmacSha256::Sign(const uint8_t* data, size_t data_len, uint8_t* out_digest,
Thai Duong60eebdc2015-03-25 17:20:24 -070052 size_t digest_len) const {
Thai Duong7689ed62015-03-20 16:50:18 -070053 assert(digest_len);
54
55 uint8_t tmp[SHA256_DIGEST_LENGTH];
56 uint8_t* digest = tmp;
57 if (digest_len >= SHA256_DIGEST_LENGTH)
58 digest = out_digest;
59
Thai Duong60eebdc2015-03-25 17:20:24 -070060 if (nullptr == ::HMAC(EVP_sha256(), key_.get(), key_len_, data, data_len, digest, nullptr)) {
Thai Duong7689ed62015-03-20 16:50:18 -070061 return false;
62 }
63 if (digest_len < SHA256_DIGEST_LENGTH)
64 memcpy(out_digest, tmp, digest_len);
65
66 return true;
67}
68
69bool HmacSha256::Verify(const Buffer& data, const Buffer& digest) const {
70 return Verify(data.peek_read(), data.available_read(), digest.peek_read(),
71 digest.available_read());
72}
73
74bool HmacSha256::Verify(const uint8_t* data, size_t data_len, const uint8_t* digest,
Thai Duong60eebdc2015-03-25 17:20:24 -070075 size_t digest_len) const {
Thai Duong7689ed62015-03-20 16:50:18 -070076 if (digest_len != SHA256_DIGEST_LENGTH)
77 return false;
78
79 uint8_t computed_digest[SHA256_DIGEST_LENGTH];
80 if (!Sign(data, data_len, computed_digest, sizeof(computed_digest)))
81 return false;
82
83 return 0 == CRYPTO_memcmp(digest, computed_digest, SHA256_DIGEST_LENGTH);
84}
85
86} // namespace keymaster