blob: 7ed64a034612af28b4e8e124a18f5da6994b2c58 [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 Willden567a4a02014-12-31 12:14:46 -070032 virtual Operation* CreateOperation(const Key& key, keymaster_error_t* error) {
Thai Duongf862a762015-03-18 14:10:56 -070033 const EcKey* ecdsa_key = static_cast<const EcKey*>(&key);
Shawn Willden63ac0432014-12-29 14:07:08 -070034 if (!ecdsa_key) {
35 *error = KM_ERROR_UNKNOWN_ERROR;
36 return NULL;
37 }
38
Shawn Willden84b8da52015-03-11 07:21:32 -060039 keymaster_digest_t digest;
Shawn Willdend9d7acf2015-02-25 17:37:20 -070040 if (!ecdsa_key->authorizations().GetTagValue(TAG_DIGEST, &digest) &&
41 !ecdsa_key->authorizations().GetTagValue(TAG_DIGEST_OLD, &digest)) {
Shawn Willden84b8da52015-03-11 07:21:32 -060042 *error = KM_ERROR_UNSUPPORTED_DIGEST;
43 return NULL;
44 }
45
46 Operation* op = new EcdsaSignOperation(KM_PURPOSE_SIGN, digest, ecdsa_key->key());
Shawn Willden63ac0432014-12-29 14:07:08 -070047 if (!op)
48 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
49 return op;
50 }
Shawn Willden84b8da52015-03-11 07:21:32 -060051
52 virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const {
53 *digest_count = array_length(supported_digests);
54 return supported_digests;
55 }
Shawn Willden63ac0432014-12-29 14:07:08 -070056};
57static OperationFactoryRegistry::Registration<EcdsaSignOperationFactory> sign_registration;
58
59class EcdsaVerifyOperationFactory : public OperationFactory {
60 public:
Shawn Willden9c65b2b2015-04-07 17:01:10 -060061 virtual KeyType registry_key() const { return KeyType(KM_ALGORITHM_EC, KM_PURPOSE_VERIFY); }
Shawn Willden63ac0432014-12-29 14:07:08 -070062
Shawn Willden567a4a02014-12-31 12:14:46 -070063 virtual Operation* CreateOperation(const Key& key, keymaster_error_t* error) {
Thai Duongf862a762015-03-18 14:10:56 -070064 const EcKey* ecdsa_key = static_cast<const EcKey*>(&key);
Shawn Willden63ac0432014-12-29 14:07:08 -070065 if (!ecdsa_key) {
66 *error = KM_ERROR_UNKNOWN_ERROR;
67 return NULL;
68 }
69
Shawn Willden84b8da52015-03-11 07:21:32 -060070 keymaster_digest_t digest;
Shawn Willdend9d7acf2015-02-25 17:37:20 -070071 if (!ecdsa_key->authorizations().GetTagValue(TAG_DIGEST, &digest) &&
72 !ecdsa_key->authorizations().GetTagValue(TAG_DIGEST_OLD, &digest)) {
Shawn Willden84b8da52015-03-11 07:21:32 -060073 *error = KM_ERROR_UNSUPPORTED_DIGEST;
74 return NULL;
75 }
76
77 Operation* op = new EcdsaVerifyOperation(KM_PURPOSE_VERIFY, digest, ecdsa_key->key());
Shawn Willden63ac0432014-12-29 14:07:08 -070078 if (!op)
79 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
80 return op;
81 }
82
Shawn Willden63ac0432014-12-29 14:07:08 -070083 virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const {
Shawn Willden84b8da52015-03-11 07:21:32 -060084 *digest_count = array_length(supported_digests);
85 return supported_digests;
Shawn Willden63ac0432014-12-29 14:07:08 -070086 }
87};
88static OperationFactoryRegistry::Registration<EcdsaVerifyOperationFactory> verify_registration;
89
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060090EcdsaOperation::~EcdsaOperation() {
91 if (ecdsa_key_ != NULL)
92 EC_KEY_free(ecdsa_key_);
93}
94
Shawn Willden6bfbff02015-02-06 19:48:24 -070095keymaster_error_t EcdsaOperation::Update(const AuthorizationSet& /* additional_params */,
96 const Buffer& input, Buffer* /* output */,
Shawn Willdenb7361132014-12-08 08:15:14 -070097 size_t* input_consumed) {
98 assert(input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060099 switch (purpose()) {
100 default:
101 return KM_ERROR_UNIMPLEMENTED;
102 case KM_PURPOSE_SIGN:
103 case KM_PURPOSE_VERIFY:
Shawn Willdenb7361132014-12-08 08:15:14 -0700104 return StoreData(input, input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600105 }
106}
107
Shawn Willdenb7361132014-12-08 08:15:14 -0700108keymaster_error_t EcdsaOperation::StoreData(const Buffer& input, size_t* input_consumed) {
Shawn Willdenb3407022014-08-27 06:30:52 -0600109 if (!data_.reserve(data_.available_read() + input.available_read()) ||
Shawn Willdend67afae2014-08-19 12:36:27 -0600110 !data_.write(input.peek_read(), input.available_read()))
111 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willdenb7361132014-12-08 08:15:14 -0700112 *input_consumed = input.available_read();
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600113 return KM_ERROR_OK;
114}
115
Shawn Willden6bfbff02015-02-06 19:48:24 -0700116keymaster_error_t EcdsaSignOperation::Finish(const AuthorizationSet& /* additional_params */,
117 const Buffer& /* signature */, Buffer* output) {
Shawn Willdenb7361132014-12-08 08:15:14 -0700118 assert(output);
Shawn Willdend67afae2014-08-19 12:36:27 -0600119 output->Reinitialize(ECDSA_size(ecdsa_key_));
120 unsigned int siglen;
121 if (!ECDSA_sign(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
122 output->peek_write(), &siglen, ecdsa_key_))
Shawn Willden567a4a02014-12-31 12:14:46 -0700123 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -0600124 output->advance_write(siglen);
125 return KM_ERROR_OK;
126}
127
Shawn Willden6bfbff02015-02-06 19:48:24 -0700128keymaster_error_t EcdsaVerifyOperation::Finish(const AuthorizationSet& /* additional_params */,
129 const Buffer& signature, Buffer* /* output */) {
Shawn Willdend67afae2014-08-19 12:36:27 -0600130 int result = ECDSA_verify(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
131 signature.peek_read(), signature.available_read(), ecdsa_key_);
132 if (result < 0)
Shawn Willden567a4a02014-12-31 12:14:46 -0700133 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -0600134 else if (result == 0)
135 return KM_ERROR_VERIFICATION_FAILED;
136 else
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600137 return KM_ERROR_OK;
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600138}
139
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600140} // namespace keymaster