blob: a28209aa26d4dfa512c92a8db39499aa6be837d3 [file] [log] [blame]
Shawn Willdenc3864dd2014-08-18 15:20:01 -06001/*
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 <openssl/ecdsa.h>
18
Thai Duongf862a762015-03-18 14:10:56 -070019#include "ec_key.h"
Shawn Willdenc3864dd2014-08-18 15:20:01 -060020#include "ecdsa_operation.h"
Shawn Willden567a4a02014-12-31 12:14:46 -070021#include "openssl_err.h"
Shawn Willdenc3864dd2014-08-18 15:20:01 -060022#include "openssl_utils.h"
23
24namespace keymaster {
25
Shawn Willden84b8da52015-03-11 07:21:32 -060026static const keymaster_digest_t supported_digests[] = {KM_DIGEST_NONE};
27
Shawn Willden13e29e02015-05-08 11:02:46 -060028Operation* EcdsaOperationFactory::CreateOperation(const Key& key,
Shawn Willden226746b2015-05-08 11:36:56 -060029 const AuthorizationSet& begin_params,
Shawn Willden13e29e02015-05-08 11:02:46 -060030 keymaster_error_t* error) {
31 const EcKey* ecdsa_key = static_cast<const EcKey*>(&key);
32 if (!ecdsa_key) {
33 *error = KM_ERROR_UNKNOWN_ERROR;
34 return nullptr;
Shawn Willden63ac0432014-12-29 14:07:08 -070035 }
Shawn Willden84b8da52015-03-11 07:21:32 -060036
Shawn Willden226746b2015-05-08 11:36:56 -060037 *error = KM_ERROR_UNSUPPORTED_DIGEST;
Shawn Willden13e29e02015-05-08 11:02:46 -060038 keymaster_digest_t digest;
Shawn Willden226746b2015-05-08 11:36:56 -060039 if (!begin_params.GetTagValue(TAG_DIGEST, &digest)) {
40 LOG_E("%d digests specified in begin params", begin_params.GetTagCount(TAG_DIGEST));
41 return nullptr;
42 } else if (!supported(digest)) {
43 LOG_E("Digest %d not supported", digest);
44 return nullptr;
45 } else if (!ecdsa_key->authorizations().Contains(TAG_DIGEST, digest) &&
46 !ecdsa_key->authorizations().Contains(TAG_DIGEST_OLD, digest)) {
47 LOG_E("Digest %d was specified, but not authorized by key", digest);
48 *error = KM_ERROR_INCOMPATIBLE_DIGEST;
Shawn Willden13e29e02015-05-08 11:02:46 -060049 return NULL;
50 }
Shawn Willden226746b2015-05-08 11:36:56 -060051 *error = KM_ERROR_OK;
52
Shawn Willden13e29e02015-05-08 11:02:46 -060053 Operation* op = InstantiateOperation(digest, ecdsa_key->key());
54 if (!op)
55 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
56 return op;
57}
58
59const keymaster_digest_t* EcdsaOperationFactory::SupportedDigests(size_t* digest_count) const {
60 *digest_count = array_length(supported_digests);
61 return supported_digests;
62}
63
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060064EcdsaOperation::~EcdsaOperation() {
65 if (ecdsa_key_ != NULL)
66 EC_KEY_free(ecdsa_key_);
67}
68
Shawn Willden6bfbff02015-02-06 19:48:24 -070069keymaster_error_t EcdsaOperation::Update(const AuthorizationSet& /* additional_params */,
70 const Buffer& input, Buffer* /* output */,
Shawn Willdenb7361132014-12-08 08:15:14 -070071 size_t* input_consumed) {
72 assert(input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060073 switch (purpose()) {
74 default:
75 return KM_ERROR_UNIMPLEMENTED;
76 case KM_PURPOSE_SIGN:
77 case KM_PURPOSE_VERIFY:
Shawn Willdenb7361132014-12-08 08:15:14 -070078 return StoreData(input, input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060079 }
80}
81
Shawn Willdenb7361132014-12-08 08:15:14 -070082keymaster_error_t EcdsaOperation::StoreData(const Buffer& input, size_t* input_consumed) {
Shawn Willdenb3407022014-08-27 06:30:52 -060083 if (!data_.reserve(data_.available_read() + input.available_read()) ||
Shawn Willdend67afae2014-08-19 12:36:27 -060084 !data_.write(input.peek_read(), input.available_read()))
85 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willdenb7361132014-12-08 08:15:14 -070086 *input_consumed = input.available_read();
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060087 return KM_ERROR_OK;
88}
89
Shawn Willden6bfbff02015-02-06 19:48:24 -070090keymaster_error_t EcdsaSignOperation::Finish(const AuthorizationSet& /* additional_params */,
91 const Buffer& /* signature */, Buffer* output) {
Shawn Willdenb7361132014-12-08 08:15:14 -070092 assert(output);
Shawn Willdend67afae2014-08-19 12:36:27 -060093 output->Reinitialize(ECDSA_size(ecdsa_key_));
94 unsigned int siglen;
95 if (!ECDSA_sign(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
96 output->peek_write(), &siglen, ecdsa_key_))
Shawn Willden567a4a02014-12-31 12:14:46 -070097 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -060098 output->advance_write(siglen);
99 return KM_ERROR_OK;
100}
101
Shawn Willden6bfbff02015-02-06 19:48:24 -0700102keymaster_error_t EcdsaVerifyOperation::Finish(const AuthorizationSet& /* additional_params */,
103 const Buffer& signature, Buffer* /* output */) {
Shawn Willdend67afae2014-08-19 12:36:27 -0600104 int result = ECDSA_verify(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
105 signature.peek_read(), signature.available_read(), ecdsa_key_);
106 if (result < 0)
Shawn Willden567a4a02014-12-31 12:14:46 -0700107 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -0600108 else if (result == 0)
109 return KM_ERROR_VERIFICATION_FAILED;
110 else
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600111 return KM_ERROR_OK;
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600112}
113
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600114} // namespace keymaster