blob: f7c40b13b552f8a72b90f567dd32c0fe9979e319 [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>
Shawn Willdende4ffa92015-05-05 07:15:24 -060021#include <openssl/mem.h>
Shawn Willden0d560bf2014-12-15 17:44:02 -070022
Shawn Willden567a4a02014-12-31 12:14:46 -070023#include "openssl_err.h"
Shawn Willden63ac0432014-12-29 14:07:08 -070024#include "symmetric_key.h"
25
Shawn Willden0d560bf2014-12-15 17:44:02 -070026namespace keymaster {
27
Shawn Willden63ac0432014-12-29 14:07:08 -070028/**
29 * Abstract base for HMAC operation factories. This class does all of the work to create
30 * HMAC operations.
31 */
32class HmacOperationFactory : public OperationFactory {
33 public:
34 virtual KeyType registry_key() const { return KeyType(KM_ALGORITHM_HMAC, purpose()); }
35
Shawn Willden3ed6d062015-04-15 13:39:38 -060036 virtual Operation* CreateOperation(const Key& key, const AuthorizationSet& begin_params,
37 keymaster_error_t* error);
Shawn Willden63ac0432014-12-29 14:07:08 -070038
39 virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const;
40
41 virtual keymaster_purpose_t purpose() const = 0;
42};
43
Shawn Willden3ed6d062015-04-15 13:39:38 -060044Operation* HmacOperationFactory::CreateOperation(const Key& key,
Shawn Willden09f25272015-04-15 13:49:49 -060045 const AuthorizationSet& begin_params,
Shawn Willden3ed6d062015-04-15 13:39:38 -060046 keymaster_error_t* error) {
Shawn Willden09f25272015-04-15 13:49:49 -060047 uint32_t tag_length = 0;
48 begin_params.GetTagValue(TAG_MAC_LENGTH, &tag_length);
Shawn Willden0c60f6f2015-04-27 23:40:10 -060049 if (tag_length % 8 != 0) {
50 LOG_E("MAC length %d nod a multiple of 8 bits", tag_length);
51 *error = KM_ERROR_UNSUPPORTED_MAC_LENGTH;
52 return nullptr;
53 }
Shawn Willden63ac0432014-12-29 14:07:08 -070054
Shawn Willden09f25272015-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 Willden0c60f6f2015-04-27 23:40:10 -060059 UniquePtr<HmacOperation> op(new HmacOperation(purpose(), symmetric_key->key_data(),
60 symmetric_key->key_data_size(), digest,
61 tag_length / 8));
Shawn Willden09f25272015-04-15 13:49:49 -060062 if (!op.get())
Shawn Willden63ac0432014-12-29 14:07:08 -070063 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden09f25272015-04-15 13:49:49 -060064 else
65 *error = op->error();
66
67 if (*error != KM_ERROR_OK)
68 return nullptr;
69
70 return op.release();
Shawn Willden63ac0432014-12-29 14:07:08 -070071}
72
73static keymaster_digest_t supported_digests[] = {KM_DIGEST_SHA1, KM_DIGEST_SHA_2_224,
74 KM_DIGEST_SHA_2_256, KM_DIGEST_SHA_2_384,
75 KM_DIGEST_SHA_2_512};
76const keymaster_digest_t* HmacOperationFactory::SupportedDigests(size_t* digest_count) const {
77 *digest_count = array_length(supported_digests);
78 return supported_digests;
79}
80
81/**
82 * Concrete factory for creating HMAC signing operations.
83 */
84class HmacSignOperationFactory : public HmacOperationFactory {
85 keymaster_purpose_t purpose() const { return KM_PURPOSE_SIGN; }
86};
87static OperationFactoryRegistry::Registration<HmacSignOperationFactory> sign_registration;
88
89/**
90 * Concrete factory for creating HMAC verification operations.
91 */
92class HmacVerifyOperationFactory : public HmacOperationFactory {
93 keymaster_purpose_t purpose() const { return KM_PURPOSE_VERIFY; }
94};
95static OperationFactoryRegistry::Registration<HmacVerifyOperationFactory> verify_registration;
96
Shawn Willden567a4a02014-12-31 12:14:46 -070097HmacOperation::HmacOperation(keymaster_purpose_t purpose, const uint8_t* key_data,
98 size_t key_data_size, keymaster_digest_t digest, size_t tag_length)
99 : Operation(purpose), error_(KM_ERROR_OK), tag_length_(tag_length) {
Shawn Willden0d560bf2014-12-15 17:44:02 -0700100 // Initialize CTX first, so dtor won't crash even if we error out later.
101 HMAC_CTX_init(&ctx_);
102
Shawn Willden09f25272015-04-15 13:49:49 -0600103 const EVP_MD* md = nullptr;
Shawn Willden0d560bf2014-12-15 17:44:02 -0700104 switch (digest) {
Shawn Willden09f25272015-04-15 13:49:49 -0600105 case KM_DIGEST_NONE:
106 case KM_DIGEST_MD5:
107 error_ = KM_ERROR_UNSUPPORTED_DIGEST;
108 break;
Shawn Willden51d5e0e2014-12-18 10:44:03 -0700109 case KM_DIGEST_SHA1:
110 md = EVP_sha1();
111 break;
Shawn Willden62c22862014-12-17 08:36:20 -0700112 case KM_DIGEST_SHA_2_224:
113 md = EVP_sha224();
114 break;
Shawn Willden0d560bf2014-12-15 17:44:02 -0700115 case KM_DIGEST_SHA_2_256:
116 md = EVP_sha256();
117 break;
Shawn Willden62c22862014-12-17 08:36:20 -0700118 case KM_DIGEST_SHA_2_384:
119 md = EVP_sha384();
120 break;
121 case KM_DIGEST_SHA_2_512:
122 md = EVP_sha512();
123 break;
Shawn Willden0d560bf2014-12-15 17:44:02 -0700124 }
125
Shawn Willden09f25272015-04-15 13:49:49 -0600126 if (md == nullptr) {
127 error_ = KM_ERROR_UNSUPPORTED_DIGEST;
Shawn Willden0d560bf2014-12-15 17:44:02 -0700128 return;
129 }
130
131 HMAC_Init_ex(&ctx_, key_data, key_data_size, md, NULL /* engine */);
132}
133
134HmacOperation::~HmacOperation() {
135 HMAC_CTX_cleanup(&ctx_);
136}
137
Shawn Willden111edb32015-02-05 22:44:24 -0700138keymaster_error_t HmacOperation::Begin(const AuthorizationSet& /* input_params */,
139 AuthorizationSet* /* output_params */) {
Shawn Willden0d560bf2014-12-15 17:44:02 -0700140 return error_;
141}
142
Shawn Willden6bfbff02015-02-06 19:48:24 -0700143keymaster_error_t HmacOperation::Update(const AuthorizationSet& /* additional_params */,
144 const Buffer& input, Buffer* /* output */,
Shawn Willden0d560bf2014-12-15 17:44:02 -0700145 size_t* input_consumed) {
146 if (!HMAC_Update(&ctx_, input.peek_read(), input.available_read()))
Shawn Willden567a4a02014-12-31 12:14:46 -0700147 return TranslateLastOpenSslError();
Shawn Willden0d560bf2014-12-15 17:44:02 -0700148 *input_consumed = input.available_read();
149 return KM_ERROR_OK;
150}
151
152keymaster_error_t HmacOperation::Abort() {
153 return KM_ERROR_OK;
154}
155
Shawn Willden6bfbff02015-02-06 19:48:24 -0700156keymaster_error_t HmacOperation::Finish(const AuthorizationSet& /* additional_params */,
157 const Buffer& signature, Buffer* output) {
Shawn Willden0d560bf2014-12-15 17:44:02 -0700158 uint8_t digest[EVP_MAX_MD_SIZE];
159 unsigned int digest_len;
160 if (!HMAC_Final(&ctx_, digest, &digest_len))
Shawn Willden567a4a02014-12-31 12:14:46 -0700161 return TranslateLastOpenSslError();
Shawn Willden0d560bf2014-12-15 17:44:02 -0700162
163 switch (purpose()) {
164 case KM_PURPOSE_SIGN:
Shawn Willden09f25272015-04-15 13:49:49 -0600165 if (tag_length_ > digest_len)
166 return KM_ERROR_UNSUPPORTED_MAC_LENGTH;
167 if (!output->reserve(tag_length_) || !output->write(digest, tag_length_))
168 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willden0d560bf2014-12-15 17:44:02 -0700169 return KM_ERROR_OK;
170 case KM_PURPOSE_VERIFY:
Shawn Willden09f25272015-04-15 13:49:49 -0600171 if (signature.available_read() > digest_len)
Shawn Willden0d560bf2014-12-15 17:44:02 -0700172 return KM_ERROR_INVALID_INPUT_LENGTH;
Shawn Willden09f25272015-04-15 13:49:49 -0600173 if (CRYPTO_memcmp(signature.peek_read(), digest, signature.available_read()) != 0)
Shawn Willden0d560bf2014-12-15 17:44:02 -0700174 return KM_ERROR_VERIFICATION_FAILED;
175 return KM_ERROR_OK;
176 default:
177 return KM_ERROR_UNSUPPORTED_PURPOSE;
178 }
179}
180
181} // namespace keymaster