blob: 63e8655659bd6221731241c9cd906bc7bc312036 [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 Willden63ac0432014-12-29 14:07:08 -070028class EcdsaSignOperationFactory : public OperationFactory {
29 public:
Shawn Willden9c65b2b2015-04-07 17:01:10 -060030 virtual KeyType registry_key() const { return KeyType(KM_ALGORITHM_EC, KM_PURPOSE_SIGN); }
Shawn Willden63ac0432014-12-29 14:07:08 -070031
Shawn Willden898f5642015-04-15 13:39:38 -060032 virtual Operation* CreateOperation(const Key& key, const AuthorizationSet& /* begin_params */,
33 keymaster_error_t* error) {
Thai Duongf862a762015-03-18 14:10:56 -070034 const EcKey* ecdsa_key = static_cast<const EcKey*>(&key);
Shawn Willden63ac0432014-12-29 14:07:08 -070035 if (!ecdsa_key) {
36 *error = KM_ERROR_UNKNOWN_ERROR;
37 return NULL;
38 }
39
Shawn Willden84b8da52015-03-11 07:21:32 -060040 keymaster_digest_t digest;
Shawn Willdend9d7acf2015-02-25 17:37:20 -070041 if (!ecdsa_key->authorizations().GetTagValue(TAG_DIGEST, &digest) &&
42 !ecdsa_key->authorizations().GetTagValue(TAG_DIGEST_OLD, &digest)) {
Shawn Willden84b8da52015-03-11 07:21:32 -060043 *error = KM_ERROR_UNSUPPORTED_DIGEST;
44 return NULL;
45 }
46
47 Operation* op = new EcdsaSignOperation(KM_PURPOSE_SIGN, digest, ecdsa_key->key());
Shawn Willden63ac0432014-12-29 14:07:08 -070048 if (!op)
49 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
50 return op;
51 }
Shawn Willden84b8da52015-03-11 07:21:32 -060052
53 virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const {
54 *digest_count = array_length(supported_digests);
55 return supported_digests;
56 }
Shawn Willden63ac0432014-12-29 14:07:08 -070057};
58static OperationFactoryRegistry::Registration<EcdsaSignOperationFactory> sign_registration;
59
60class EcdsaVerifyOperationFactory : public OperationFactory {
61 public:
Shawn Willden9c65b2b2015-04-07 17:01:10 -060062 virtual KeyType registry_key() const { return KeyType(KM_ALGORITHM_EC, KM_PURPOSE_VERIFY); }
Shawn Willden63ac0432014-12-29 14:07:08 -070063
Shawn Willden898f5642015-04-15 13:39:38 -060064 virtual Operation* CreateOperation(const Key& key, const AuthorizationSet& /* begin_params */,
65 keymaster_error_t* error) {
Thai Duongf862a762015-03-18 14:10:56 -070066 const EcKey* ecdsa_key = static_cast<const EcKey*>(&key);
Shawn Willden63ac0432014-12-29 14:07:08 -070067 if (!ecdsa_key) {
68 *error = KM_ERROR_UNKNOWN_ERROR;
69 return NULL;
70 }
71
Shawn Willden84b8da52015-03-11 07:21:32 -060072 keymaster_digest_t digest;
Shawn Willdend9d7acf2015-02-25 17:37:20 -070073 if (!ecdsa_key->authorizations().GetTagValue(TAG_DIGEST, &digest) &&
74 !ecdsa_key->authorizations().GetTagValue(TAG_DIGEST_OLD, &digest)) {
Shawn Willden84b8da52015-03-11 07:21:32 -060075 *error = KM_ERROR_UNSUPPORTED_DIGEST;
76 return NULL;
77 }
78
79 Operation* op = new EcdsaVerifyOperation(KM_PURPOSE_VERIFY, digest, ecdsa_key->key());
Shawn Willden63ac0432014-12-29 14:07:08 -070080 if (!op)
81 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
82 return op;
83 }
84
Shawn Willden63ac0432014-12-29 14:07:08 -070085 virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const {
Shawn Willden84b8da52015-03-11 07:21:32 -060086 *digest_count = array_length(supported_digests);
87 return supported_digests;
Shawn Willden63ac0432014-12-29 14:07:08 -070088 }
89};
90static OperationFactoryRegistry::Registration<EcdsaVerifyOperationFactory> verify_registration;
91
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060092EcdsaOperation::~EcdsaOperation() {
93 if (ecdsa_key_ != NULL)
94 EC_KEY_free(ecdsa_key_);
95}
96
Shawn Willden6bfbff02015-02-06 19:48:24 -070097keymaster_error_t EcdsaOperation::Update(const AuthorizationSet& /* additional_params */,
98 const Buffer& input, Buffer* /* output */,
Shawn Willdenb7361132014-12-08 08:15:14 -070099 size_t* input_consumed) {
100 assert(input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600101 switch (purpose()) {
102 default:
103 return KM_ERROR_UNIMPLEMENTED;
104 case KM_PURPOSE_SIGN:
105 case KM_PURPOSE_VERIFY:
Shawn Willdenb7361132014-12-08 08:15:14 -0700106 return StoreData(input, input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600107 }
108}
109
Shawn Willdenb7361132014-12-08 08:15:14 -0700110keymaster_error_t EcdsaOperation::StoreData(const Buffer& input, size_t* input_consumed) {
Shawn Willdenb3407022014-08-27 06:30:52 -0600111 if (!data_.reserve(data_.available_read() + input.available_read()) ||
Shawn Willdend67afae2014-08-19 12:36:27 -0600112 !data_.write(input.peek_read(), input.available_read()))
113 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willdenb7361132014-12-08 08:15:14 -0700114 *input_consumed = input.available_read();
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600115 return KM_ERROR_OK;
116}
117
Shawn Willden6bfbff02015-02-06 19:48:24 -0700118keymaster_error_t EcdsaSignOperation::Finish(const AuthorizationSet& /* additional_params */,
119 const Buffer& /* signature */, Buffer* output) {
Shawn Willdenb7361132014-12-08 08:15:14 -0700120 assert(output);
Shawn Willdend67afae2014-08-19 12:36:27 -0600121 output->Reinitialize(ECDSA_size(ecdsa_key_));
122 unsigned int siglen;
123 if (!ECDSA_sign(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
124 output->peek_write(), &siglen, ecdsa_key_))
Shawn Willden567a4a02014-12-31 12:14:46 -0700125 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -0600126 output->advance_write(siglen);
127 return KM_ERROR_OK;
128}
129
Shawn Willden6bfbff02015-02-06 19:48:24 -0700130keymaster_error_t EcdsaVerifyOperation::Finish(const AuthorizationSet& /* additional_params */,
131 const Buffer& signature, Buffer* /* output */) {
Shawn Willdend67afae2014-08-19 12:36:27 -0600132 int result = ECDSA_verify(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
133 signature.peek_read(), signature.available_read(), ecdsa_key_);
134 if (result < 0)
Shawn Willden567a4a02014-12-31 12:14:46 -0700135 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -0600136 else if (result == 0)
137 return KM_ERROR_VERIFICATION_FAILED;
138 else
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600139 return KM_ERROR_OK;
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600140}
141
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600142} // namespace keymaster