blob: e1397d61d93cb16a9fe44bbdc3106769de3dda4e [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;
40 if (!ecdsa_key->authorizations().GetTagValue(TAG_DIGEST, &digest)) {
41 *error = KM_ERROR_UNSUPPORTED_DIGEST;
42 return NULL;
43 }
44
45 Operation* op = new EcdsaSignOperation(KM_PURPOSE_SIGN, digest, ecdsa_key->key());
Shawn Willden63ac0432014-12-29 14:07:08 -070046 if (!op)
47 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
48 return op;
49 }
Shawn Willden84b8da52015-03-11 07:21:32 -060050
51 virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const {
52 *digest_count = array_length(supported_digests);
53 return supported_digests;
54 }
Shawn Willden63ac0432014-12-29 14:07:08 -070055};
56static OperationFactoryRegistry::Registration<EcdsaSignOperationFactory> sign_registration;
57
58class EcdsaVerifyOperationFactory : public OperationFactory {
59 public:
Shawn Willden9c65b2b2015-04-07 17:01:10 -060060 virtual KeyType registry_key() const { return KeyType(KM_ALGORITHM_EC, KM_PURPOSE_VERIFY); }
Shawn Willden63ac0432014-12-29 14:07:08 -070061
Shawn Willden567a4a02014-12-31 12:14:46 -070062 virtual Operation* CreateOperation(const Key& key, keymaster_error_t* error) {
Thai Duongf862a762015-03-18 14:10:56 -070063 const EcKey* ecdsa_key = static_cast<const EcKey*>(&key);
Shawn Willden63ac0432014-12-29 14:07:08 -070064 if (!ecdsa_key) {
65 *error = KM_ERROR_UNKNOWN_ERROR;
66 return NULL;
67 }
68
Shawn Willden84b8da52015-03-11 07:21:32 -060069 keymaster_digest_t digest;
70 if (!ecdsa_key->authorizations().GetTagValue(TAG_DIGEST, &digest)) {
71 *error = KM_ERROR_UNSUPPORTED_DIGEST;
72 return NULL;
73 }
74
75 Operation* op = new EcdsaVerifyOperation(KM_PURPOSE_VERIFY, digest, ecdsa_key->key());
Shawn Willden63ac0432014-12-29 14:07:08 -070076 if (!op)
77 *error = KM_ERROR_MEMORY_ALLOCATION_FAILED;
78 return op;
79 }
80
Shawn Willden63ac0432014-12-29 14:07:08 -070081 virtual const keymaster_digest_t* SupportedDigests(size_t* digest_count) const {
Shawn Willden84b8da52015-03-11 07:21:32 -060082 *digest_count = array_length(supported_digests);
83 return supported_digests;
Shawn Willden63ac0432014-12-29 14:07:08 -070084 }
85};
86static OperationFactoryRegistry::Registration<EcdsaVerifyOperationFactory> verify_registration;
87
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060088EcdsaOperation::~EcdsaOperation() {
89 if (ecdsa_key_ != NULL)
90 EC_KEY_free(ecdsa_key_);
91}
92
Shawn Willden6bfbff02015-02-06 19:48:24 -070093keymaster_error_t EcdsaOperation::Update(const AuthorizationSet& /* additional_params */,
94 const Buffer& input, Buffer* /* output */,
Shawn Willdenb7361132014-12-08 08:15:14 -070095 size_t* input_consumed) {
96 assert(input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -060097 switch (purpose()) {
98 default:
99 return KM_ERROR_UNIMPLEMENTED;
100 case KM_PURPOSE_SIGN:
101 case KM_PURPOSE_VERIFY:
Shawn Willdenb7361132014-12-08 08:15:14 -0700102 return StoreData(input, input_consumed);
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600103 }
104}
105
Shawn Willdenb7361132014-12-08 08:15:14 -0700106keymaster_error_t EcdsaOperation::StoreData(const Buffer& input, size_t* input_consumed) {
Shawn Willdenb3407022014-08-27 06:30:52 -0600107 if (!data_.reserve(data_.available_read() + input.available_read()) ||
Shawn Willdend67afae2014-08-19 12:36:27 -0600108 !data_.write(input.peek_read(), input.available_read()))
109 return KM_ERROR_MEMORY_ALLOCATION_FAILED;
Shawn Willdenb7361132014-12-08 08:15:14 -0700110 *input_consumed = input.available_read();
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600111 return KM_ERROR_OK;
112}
113
Shawn Willden6bfbff02015-02-06 19:48:24 -0700114keymaster_error_t EcdsaSignOperation::Finish(const AuthorizationSet& /* additional_params */,
115 const Buffer& /* signature */, Buffer* output) {
Shawn Willdenb7361132014-12-08 08:15:14 -0700116 assert(output);
Shawn Willdend67afae2014-08-19 12:36:27 -0600117 output->Reinitialize(ECDSA_size(ecdsa_key_));
118 unsigned int siglen;
119 if (!ECDSA_sign(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
120 output->peek_write(), &siglen, ecdsa_key_))
Shawn Willden567a4a02014-12-31 12:14:46 -0700121 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -0600122 output->advance_write(siglen);
123 return KM_ERROR_OK;
124}
125
Shawn Willden6bfbff02015-02-06 19:48:24 -0700126keymaster_error_t EcdsaVerifyOperation::Finish(const AuthorizationSet& /* additional_params */,
127 const Buffer& signature, Buffer* /* output */) {
Shawn Willdend67afae2014-08-19 12:36:27 -0600128 int result = ECDSA_verify(0 /* type -- ignored */, data_.peek_read(), data_.available_read(),
129 signature.peek_read(), signature.available_read(), ecdsa_key_);
130 if (result < 0)
Shawn Willden567a4a02014-12-31 12:14:46 -0700131 return TranslateLastOpenSslError();
Shawn Willdend67afae2014-08-19 12:36:27 -0600132 else if (result == 0)
133 return KM_ERROR_VERIFICATION_FAILED;
134 else
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600135 return KM_ERROR_OK;
Shawn Willden5ac2f8f2014-08-18 15:33:10 -0600136}
137
Shawn Willdenc3864dd2014-08-18 15:20:01 -0600138} // namespace keymaster