blob: a179655cfeaaf2cafabbf364f095babcb90623ca [file] [log] [blame]
Shawn Willden0d560bf2014-12-15 17:44:02 -07001/*
2 * Copyright 2014 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_operation.h"
18
19#include <openssl/evp.h>
20#include <openssl/hmac.h>
21
22namespace keymaster {
23
24HmacOperation::HmacOperation(keymaster_purpose_t purpose, const Logger& logger,
25 const uint8_t* key_data, size_t key_data_size,
26 keymaster_digest_t digest, size_t tag_length)
27 : Operation(purpose, logger), error_(KM_ERROR_OK), tag_length_(tag_length) {
28 // Initialize CTX first, so dtor won't crash even if we error out later.
29 HMAC_CTX_init(&ctx_);
30
31 const EVP_MD* md;
32 switch (digest) {
Shawn Willden62c22862014-12-17 08:36:20 -070033 case KM_DIGEST_SHA_2_224:
34 md = EVP_sha224();
35 break;
Shawn Willden0d560bf2014-12-15 17:44:02 -070036 case KM_DIGEST_SHA_2_256:
37 md = EVP_sha256();
38 break;
Shawn Willden62c22862014-12-17 08:36:20 -070039 case KM_DIGEST_SHA_2_384:
40 md = EVP_sha384();
41 break;
42 case KM_DIGEST_SHA_2_512:
43 md = EVP_sha512();
44 break;
Shawn Willden0d560bf2014-12-15 17:44:02 -070045 default:
46 error_ = KM_ERROR_UNSUPPORTED_DIGEST;
47 return;
48 }
49
50 if ((int)tag_length_ > EVP_MD_size(md)) {
51 error_ = KM_ERROR_UNSUPPORTED_MAC_LENGTH;
52 return;
53 }
54
55 HMAC_Init_ex(&ctx_, key_data, key_data_size, md, NULL /* engine */);
56}
57
58HmacOperation::~HmacOperation() {
59 HMAC_CTX_cleanup(&ctx_);
60}
61
62keymaster_error_t HmacOperation::Begin() {
63 return error_;
64}
65
66keymaster_error_t HmacOperation::Update(const Buffer& input, Buffer* /* output */,
67 size_t* input_consumed) {
68 if (!HMAC_Update(&ctx_, input.peek_read(), input.available_read()))
69 return KM_ERROR_UNKNOWN_ERROR;
70 *input_consumed = input.available_read();
71 return KM_ERROR_OK;
72}
73
74keymaster_error_t HmacOperation::Abort() {
75 return KM_ERROR_OK;
76}
77
78keymaster_error_t HmacOperation::Finish(const Buffer& signature, Buffer* output) {
79 uint8_t digest[EVP_MAX_MD_SIZE];
80 unsigned int digest_len;
81 if (!HMAC_Final(&ctx_, digest, &digest_len))
82 return KM_ERROR_UNKNOWN_ERROR;
83
84 switch (purpose()) {
85 case KM_PURPOSE_SIGN:
86 output->reserve(tag_length_);
87 output->write(digest, tag_length_);
88 return KM_ERROR_OK;
89 case KM_PURPOSE_VERIFY:
90 if (signature.available_read() != tag_length_)
91 return KM_ERROR_INVALID_INPUT_LENGTH;
92 if (memcmp(signature.peek_read(), digest, tag_length_) != 0)
93 return KM_ERROR_VERIFICATION_FAILED;
94 return KM_ERROR_OK;
95 default:
96 return KM_ERROR_UNSUPPORTED_PURPOSE;
97 }
98}
99
100} // namespace keymaster