blob: 8e1bd088a402049cf27fd2a8bb2470d8f0ed84b5 [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
Shawn Willden567a4a02014-12-31 12:14:46 -070022#include "openssl_err.h"
Shawn Willden63ac0432014-12-29 14:07:08 -070023#include "symmetric_key.h"
24
Adam Langleya550fba2015-02-13 14:44:14 -080025#if defined(OPENSSL_IS_BORINGSSL)
26typedef size_t openssl_size_t;
27#else
28typedef int openssl_size_t;
29#endif
30
Shawn Willden0d560bf2014-12-15 17:44:02 -070031namespace keymaster {
32
Shawn Willden63ac0432014-12-29 14:07:08 -070033/**
34 * Abstract base for HMAC operation factories. This class does all of the work to create
35 * HMAC operations.
36 */
37class HmacOperationFactory : public OperationFactory {
38 public:
39 virtual KeyType registry_key() const { return KeyType(KM_ALGORITHM_HMAC, purpose()); }
40
Shawn Willden898f5642015-04-15 13:39:38 -060041 virtual Operation* CreateOperation(const Key& key, const AuthorizationSet& begin_params,
42 keymaster_error_t* error);
Shawn Willden63ac0432014-12-29 14:07:08 -070043
44 virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const;
45
46 virtual keymaster_purpose_t purpose() const = 0;
47};
48
Shawn Willden898f5642015-04-15 13:39:38 -060049Operation* HmacOperationFactory::CreateOperation(const Key& key,
Shawn Willden616bdf32015-04-15 13:49:49 -060050 const AuthorizationSet& begin_params,
Shawn Willden898f5642015-04-15 13:39:38 -060051 keymaster_error_t* error) {
Shawn Willden616bdf32015-04-15 13:49:49 -060052 uint32_t tag_length = 0;
53 begin_params.GetTagValue(TAG_MAC_LENGTH, &tag_length);
Shawn Willden63ac0432014-12-29 14:07:08 -070054
Shawn Willden616bdf32015-04-15 13:49:49 -060055 keymaster_digest_t digest = KM_DIGEST_NONE;
56 key.authorizations().GetTagValue(TAG_DIGEST, &digest);
Shawn Willden63ac0432014-12-29 14:07:08 -070057
58 const SymmetricKey* symmetric_key = static_cast<const SymmetricKey*>(&key);
Shawn Willden616bdf32015-04-15 13:49:49 -060059 UniquePtr<HmacOperation> op(new HmacOperation(
60 purpose(), symmetric_key->key_data(), symmetric_key->key_data_size(), digest, tag_length));
61 if (!op.get())
Shawn Willden63ac0432014-12-29 14:07:08 -070062 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden616bdf32015-04-15 13:49:49 -060063 else
64 *error = op->error();
65
66 if (*error != KM_ERROR_OK)
67 return nullptr;
68
69 return op.release();
Shawn Willden63ac0432014-12-29 14:07:08 -070070}
71
72static keymaster_digest_t supported_digests[] = {KM_DIGEST_SHA1, KM_DIGEST_SHA_2_224,
73 KM_DIGEST_SHA_2_256, KM_DIGEST_SHA_2_384,
74 KM_DIGEST_SHA_2_512};
75const keymaster_digest_t* HmacOperationFactory::SupportedDigests(size_t* digest_count) const {
76 *digest_count = array_length(supported_digests);
77 return supported_digests;
78}
79
80/**
81 * Concrete factory for creating HMAC signing operations.
82 */
83class HmacSignOperationFactory : public HmacOperationFactory {
84 keymaster_purpose_t purpose() const { return KM_PURPOSE_SIGN; }
85};
86static OperationFactoryRegistry::Registration<HmacSignOperationFactory> sign_registration;
87
88/**
89 * Concrete factory for creating HMAC verification operations.
90 */
91class HmacVerifyOperationFactory : public HmacOperationFactory {
92 keymaster_purpose_t purpose() const { return KM_PURPOSE_VERIFY; }
93};
94static OperationFactoryRegistry::Registration<HmacVerifyOperationFactory> verify_registration;
95
Shawn Willden567a4a02014-12-31 12:14:46 -070096HmacOperation::HmacOperation(keymaster_purpose_t purpose, const uint8_t* key_data,
97 size_t key_data_size, keymaster_digest_t digest, size_t tag_length)
98 : Operation(purpose), error_(KM_ERROR_OK), tag_length_(tag_length) {
Shawn Willden0d560bf2014-12-15 17:44:02 -070099 // Initialize CTX first, so dtor won't crash even if we error out later.
100 HMAC_CTX_init(&ctx_);
101
Shawn Willden616bdf32015-04-15 13:49:49 -0600102 const EVP_MD* md = nullptr;
Shawn Willden0d560bf2014-12-15 17:44:02 -0700103 switch (digest) {
Shawn Willden616bdf32015-04-15 13:49:49 -0600104 case KM_DIGEST_NONE:
105 case KM_DIGEST_MD5:
106 error_ = KM_ERROR_UNSUPPORTED_DIGEST;
107 break;
Shawn Willden51d5e0e2014-12-18 10:44:03 -0700108 case KM_DIGEST_SHA1:
109 md = EVP_sha1();
110 break;
Shawn Willden62c22862014-12-17 08:36:20 -0700111 case KM_DIGEST_SHA_2_224:
112 md = EVP_sha224();
113 break;
Shawn Willden0d560bf2014-12-15 17:44:02 -0700114 case KM_DIGEST_SHA_2_256:
115 md = EVP_sha256();
116 break;
Shawn Willden62c22862014-12-17 08:36:20 -0700117 case KM_DIGEST_SHA_2_384:
118 md = EVP_sha384();
119 break;
120 case KM_DIGEST_SHA_2_512:
121 md = EVP_sha512();
122 break;
Shawn Willden0d560bf2014-12-15 17:44:02 -0700123 }
124
Shawn Willden616bdf32015-04-15 13:49:49 -0600125 if (md == nullptr) {
126 error_ = KM_ERROR_UNSUPPORTED_DIGEST;
Shawn Willden0d560bf2014-12-15 17:44:02 -0700127 return;
128 }
129
130 HMAC_Init_ex(&ctx_, key_data, key_data_size, md, NULL /* engine */);
131}
132
133HmacOperation::~HmacOperation() {
134 HMAC_CTX_cleanup(&ctx_);
135}
136
Shawn Willden111edb32015-02-05 22:44:24 -0700137keymaster_error_t HmacOperation::Begin(const AuthorizationSet& /* input_params */,
138 AuthorizationSet* /* output_params */) {
Shawn Willden0d560bf2014-12-15 17:44:02 -0700139 return error_;
140}
141
Shawn Willden6bfbff02015-02-06 19:48:24 -0700142keymaster_error_t HmacOperation::Update(const AuthorizationSet& /* additional_params */,
143 const Buffer& input, Buffer* /* output */,
Shawn Willden0d560bf2014-12-15 17:44:02 -0700144 size_t* input_consumed) {
145 if (!HMAC_Update(&ctx_, input.peek_read(), input.available_read()))
Shawn Willden567a4a02014-12-31 12:14:46 -0700146 return TranslateLastOpenSslError();
Shawn Willden0d560bf2014-12-15 17:44:02 -0700147 *input_consumed = input.available_read();
148 return KM_ERROR_OK;
149}
150
151keymaster_error_t HmacOperation::Abort() {
152 return KM_ERROR_OK;
153}
154
Shawn Willden6bfbff02015-02-06 19:48:24 -0700155keymaster_error_t HmacOperation::Finish(const AuthorizationSet& /* additional_params */,
156 const Buffer& signature, Buffer* output) {
Shawn Willden0d560bf2014-12-15 17:44:02 -0700157 uint8_t digest[EVP_MAX_MD_SIZE];
158 unsigned int digest_len;
159 if (!HMAC_Final(&ctx_, digest, &digest_len))
Shawn Willden567a4a02014-12-31 12:14:46 -0700160 return TranslateLastOpenSslError();
Shawn Willden0d560bf2014-12-15 17:44:02 -0700161
162 switch (purpose()) {
163 case KM_PURPOSE_SIGN:
Shawn Willden616bdf32015-04-15 13:49:49 -0600164 if (tag_length_ > digest_len)
165 return KM_ERROR_UNSUPPORTED_MAC_LENGTH;
166 if (!output->reserve(tag_length_) || !output->write(digest, tag_length_))
167 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden0d560bf2014-12-15 17:44:02 -0700168 return KM_ERROR_OK;
169 case KM_PURPOSE_VERIFY:
Shawn Willden616bdf32015-04-15 13:49:49 -0600170 if (signature.available_read() > digest_len)
Shawn Willden0d560bf2014-12-15 17:44:02 -0700171 return KM_ERROR_INVALID_INPUT_LENGTH;
Shawn Willden616bdf32015-04-15 13:49:49 -0600172 if (CRYPTO_memcmp(signature.peek_read(), digest, signature.available_read()) != 0)
Shawn Willden0d560bf2014-12-15 17:44:02 -0700173 return KM_ERROR_VERIFICATION_FAILED;
174 return KM_ERROR_OK;
175 default:
176 return KM_ERROR_UNSUPPORTED_PURPOSE;
177 }
178}
179
180} // namespace keymaster